123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- /****************************************************************
- *
- * MODULE: v.generalize
- *
- * AUTHOR(S): Daniel Bundala
- *
- * PURPOSE: Definition of a point in 3D and basic operations
- * with points
- *
- * COPYRIGHT: (C) 2002-2005 by the GRASS Development Team
- *
- * This program is free software under the
- * GNU General Public License (>=v2).
- * Read the file COPYING that comes with GRASS
- * for details.
- *
- ****************************************************************/
- #include <math.h>
- #include <grass/gis.h>
- #include <grass/vector.h>
- #include <grass/glocale.h>
- #include "point.h"
- inline void point_subtract(POINT a, POINT b, POINT * res)
- {
- res->x = a.x - b.x;
- res->y = a.y - b.y;
- res->z = a.z - b.z;
- return;
- }
- inline void point_add(POINT a, POINT b, POINT * res)
- {
- res->x = a.x + b.x;
- res->y = a.y + b.y;
- res->z = a.z + b.z;
- return;
- }
- double point_dot(POINT a, POINT b)
- {
- return a.x * b.x + a.y * b.y + a.z * b.z;
- }
- inline double point_dist2(POINT a)
- {
- return a.x * a.x + a.y * a.y + a.z * a.z;
- }
- inline void point_assign(struct line_pnts *Points, int index, int with_z,
- POINT * res, int is_loop)
- {
- if (is_loop) {
- while (index >= Points->n_points - 1)
- index -= Points->n_points - 1;
- }
- res->x = Points->x[index];
- res->y = Points->y[index];
- if (with_z) {
- res->z = Points->z[index];
- }
- else {
- res->z = 0;
- }
- return;
- }
- inline void point_scalar(POINT a, double k, POINT * res)
- {
- res->x = a.x * k;
- res->y = a.y * k;
- res->z = a.z * k;
- return;
- }
- inline void points_copy_last(struct line_pnts *Points, int pos)
- {
- int n = Points->n_points - 1;
- Points->x[pos] = Points->x[n];
- Points->y[pos] = Points->y[n];
- Points->z[pos] = Points->z[n];
- Points->n_points = pos + 1;
- return;
- }
- inline double point_dist(POINT a, POINT b)
- {
- return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) +
- (a.z - b.z) * (a.z - b.z));
- }
- inline double point_dist_square(POINT a, POINT b)
- {
- return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) +
- (a.z - b.z) * (a.z - b.z);
- }
- inline double point_angle_between(POINT a, POINT b, POINT c)
- {
- point_subtract(b, a, &a);
- point_subtract(c, b, &b);
- return acos(point_dot(a, b) / sqrt(point_dist2(a) * point_dist2(b)));
- }
- inline double point_dist_segment_square(POINT a, POINT b, POINT c, int with_z)
- {
- double px, py, pz, pdist;
- int status;
- return dig_distance2_point_to_line(a.x, a.y, a.z, b.x, b.y, b.z,
- c.x, c.y, c.z, with_z, &px, &py, &pz,
- &pdist, &status);
- }
- POINT_LIST *point_list_new(POINT p)
- {
- POINT_LIST *pl;
- pl = G_malloc(sizeof(POINT_LIST));
- pl->next = NULL;
- pl->p = p;
- return pl;
- }
- void point_list_add(POINT_LIST * l, POINT p)
- {
- POINT_LIST *n;
- n = point_list_new(p);
- n->next = l->next;
- l->next = n;
- return;
- }
- int point_list_copy_to_line_pnts(POINT_LIST l, struct line_pnts *Points)
- {
- int length, i;
- POINT_LIST *cur;
- cur = l.next;
- length = 0;
- while (cur != NULL) {
- length++;
- cur = cur->next;
- }
- if (length != Points->n_points)
- if (0 > dig_alloc_points(Points, length))
- return (-1);
- Points->n_points = length;
- cur = l.next;
- for (i = 0; i < length; i++) {
- Points->x[i] = cur->p.x;
- Points->y[i] = cur->p.y;
- Points->z[i] = cur->p.z;
- cur = cur->next;
- }
- return 0;
- }
- void point_list_free(POINT_LIST l)
- {
- POINT_LIST *p, *n;
- p = l.next;
- while (p != NULL) {
- n = p->next;
- G_free(p);
- p = n;
- }
- }
- extern void point_list_delete_next(POINT_LIST * p)
- {
- POINT_LIST *t = p->next;
- p->next = p->next->next;
- G_free(t);
- return;
- }
|