1
/***************************************************************
5
* AUTHOR(S): Martin Pavlovsky (Google SoC 2008, Paul Kelly mentor)
6
* Based on "dct" by Geoff Leach, Department of Computer
9
* PURPOSE: Creates a Delaunay triangulation vector map
11
* COPYRIGHT: (C) RMIT 1993
12
* (C) 2008-2009 by the GRASS Development Team
14
* This program is free software under the GNU General
15
* Public License (>=v2). Read the file COPYING that
16
* comes with GRASS for details.
18
* The following notices apply to portions of the code originally
19
* derived from work by Geoff Leach of RMIT:
21
* Author: Geoff Leach, Department of Computer Science, RMIT.
22
* email: gl@cs.rmit.edu.au
28
* Copyright (c) RMIT 1993. All rights reserved.
30
* License to copy and use this software purposes is granted provided
31
* that appropriate credit is given to both RMIT and the author.
33
* License is also granted to make and use derivative works provided
34
* that appropriate credit is given to both RMIT and the author.
36
* RMIT makes no representations concerning either the merchantability
37
* of this software or the suitability of this software for any particular
38
* purpose. It is provided "as is" without express or implied warranty
41
* These notices must be retained in any copies of any part of this software.
43
**************************************************************/
49
#include <grass/gis.h>
50
#include <grass/vector.h>
51
#include <grass/glocale.h>
53
#include "data_types.h"
57
/* compare first according to x-coordinate, if equal then y-coordinate */
58
int cmp(const void *a, const void *b)
60
struct vertex *p1 = (struct vertex *)a;
61
struct vertex *p2 = (struct vertex *)b;
65
else if (p1->x > p2->x)
70
else if (p1->y > p2->y)
77
void output_edges(unsigned int n, int mode3d, int type,
80
struct edge *e_start, *e;
83
double x1, y1, z1, x2, y2, z2;
85
static struct line_pnts *Points = NULL;
86
static struct line_cats *Cats = NULL;
89
Points = Vect_new_line_struct();
90
Cats = Vect_new_cats_struct();
93
G_message(_("Writing edges..."));
94
for (i = 0; i < n; i++) {
97
e_start = e = u->entry_pt;
99
v = OTHER_VERTEX(e, u);
100
if (cmp(u, v) == 1) {
108
Vect_reset_line(Points);
110
Vect_append_point(Points, x1, y1, z1);
111
Vect_append_point(Points, x2, y2, z2);
112
Vect_write_line(Out, type, Points, Cats);
115
} while (!SAME_EDGE(e, e_start));
120
/* Print the ring of triangles about each vertex. */
122
void output_triangles(unsigned int n,
123
int mode3d, int type, struct Map_info *Out)
125
struct edge *e_start, *e, *next;
126
struct vertex *u, *v, *w;
130
struct line_pnts *Points = Vect_new_line_struct();
131
struct line_cats *Cats = Vect_new_cats_struct();
133
double x1, y1, z1, x2, y2, z2, x3, y3, z3;
135
for (i = 0; i < n; i++) {
137
e_start = e = u->entry_pt;
139
v = OTHER_VERTEX(e, u);
140
if (cmp(u, v) == 1) {
142
w = OTHER_VERTEX(next, u);
144
if (SAME_EDGE(NEXT(next, w), PREV(e, v))) {
146
if (cmp(w, v) == 1) {
161
Vect_reset_line(Points);
162
Vect_append_point(Points, x1, y1, z1);
163
Vect_append_point(Points, x2, y2, z2);
164
Vect_write_line(Out, type, Points, Cats);
166
Vect_reset_line(Points);
167
Vect_append_point(Points, x2, y2, z2);
168
Vect_append_point(Points, x3, y3, z3);
169
Vect_write_line(Out, type, Points, Cats);
171
Vect_reset_line(Points);
172
Vect_append_point(Points, x3, y3, z3);
173
Vect_append_point(Points, x1, y1, z1);
174
Vect_write_line(Out, type, Points, Cats);
177
/* Next edge around u. */
179
} while (!SAME_EDGE(e, e_start));
183
void remove_duplicates(unsigned int *size)
185
unsigned int n = *size;
186
unsigned int prev = 0;
190
for (next = 1; next < n; next++) {
191
if (sites[prev].x != sites[next].x ||
192
sites[prev].y != sites[next].y)
193
sites[++prev] = sites[next];
199
/* returns number of sites read */
200
int read_sites(int mode3d, int complete_map, struct Map_info* map_in,
201
struct bound_box Box, int field)
203
int nlines, line, nsites;
204
struct line_pnts *Points;
205
struct line_cats *Cats;
207
Points = Vect_new_line_struct();
208
Cats = Vect_new_cats_struct();
210
nlines = Vect_get_num_lines(map_in);
214
G_message(_("Reading point features..."));
215
for (line = 1; line <= nlines; line++) {
218
G_percent(line, nlines, 2);
219
type = Vect_read_line(map_in, Points, Cats, line);
220
if (!(type & GV_POINTS))
223
if (field != -1 && Vect_cat_get(Cats, field, NULL) == 0)
227
if (!Vect_point_in_box(Points->x[0], Points->y[0], 0.0, &Box))
230
sites[nsites].x = Points->x[0];
231
sites[nsites].y = Points->y[0];
233
G_debug(3, "Points->z[0]: %f", Points->z[0]);
234
sites[nsites].z = Points->z[0];
237
sites[nsites].z = 0.0;
239
/* Initialise entry edge vertices. */
240
sites[nsites].entry_pt = NULL;
244
/* number 100 was arbitrarily chosen */
245
if (nsites == allocated && line != nlines){
247
realloc_sites(allocated);
251
if (nsites != nlines)
252
realloc_sites(nsites);
255
Vect_destroy_line_struct(Points);
256
Vect_destroy_cats_struct(Cats);