4
** Author: James Darrell McCauley (mccauley@ecn.purdue.edu)
6
** Department of Agricultural Engineering
8
** West Lafayette, Indiana 47907-1146 USA
10
** Permission to use, copy, modify, and distribute this software and its
11
** documentation for any purpose and without fee is hereby granted. This
12
** software is provided "as is" without express or implied warranty.
14
** Modification History:
15
** 06 Feb 93 - James Darrell McCauley <mccauley@ecn.purdue.edu> pieced
16
** this together from stuff he found on netlib (see the manpage).
18
** 4 2008: Benjamin Ducke - 3D support + better memory management
27
#include <grass/gis.h>
28
#include <grass/Vect.h>
29
#include <grass/glocale.h>
33
int main(int argc, char **argv)
36
struct Flag *reg_flag, *line_flag;
37
struct Option *in_opt, *out_opt;
38
struct GModule *module;
39
struct line_pnts *Points;
40
struct line_cats *Cats;
45
module = G_define_module();
46
module->keywords = _("vector, geometry, triangulation");
47
module->description = _("Creates a Delaunay triangulation from an input "
48
"vector map containing points or centroids.");
50
in_opt = G_define_standard_option(G_OPT_V_INPUT);
51
out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
53
reg_flag = G_define_flag();
55
reg_flag->description = _("Use only points in current region");
57
line_flag = G_define_flag();
59
line_flag->description =
60
_("Output triangulation as a graph (lines), not areas");
62
if (G_parser(argc, argv))
65
if (line_flag->answer)
70
All = reg_flag->answer ? 0 : 1;
72
Points = Vect_new_line_struct();
73
Cats = Vect_new_cats_struct();
76
if ((mapset = G_find_vector2(in_opt->answer, "")) == NULL) {
77
G_fatal_error(_("Vector map <%s> not found"), in_opt->answer);
80
Vect_set_open_level(2);
81
Vect_open_old(&In, in_opt->answer, mapset);
83
/* check if we have a 3D input points map */
85
if (Vect_is_3d(&In)) {
91
if (0 > Vect_open_new(&Out, out_opt->answer, 1)) {
92
G_fatal_error(_("Unable to create vector map <%s>"),
97
if (0 > Vect_open_new(&Out, out_opt->answer, 0)) {
98
G_fatal_error(_("Unable to create vector map <%s>"),
104
Vect_hist_copy(&In, &Out);
105
Vect_hist_command(&Out);
107
Vect_build_partial(&Out, GV_BUILD_BASE);
109
/* initialize working region */
110
G_get_window(&Window);
111
G_percent(0, 100, 1);
112
Vect_region_box(&Window, &Box);
114
freeinit(&sfl, sizeof *sites);
124
voronoi(triangulate, nextone);
128
Vect_build_partial(&Out, GV_BUILD_ATTACH_ISLES);
130
nareas = Vect_get_num_areas(&Out);
131
G_debug(3, "nareas = %d", nareas);
132
for (area = 1; area <= nareas; area++) {
133
double x, y, z, angle, slope;
136
G_percent(area, nareas, 2);
137
Vect_reset_line(Points);
138
Vect_reset_cats(Cats);
140
ret = Vect_get_point_in_area(&Out, area, &x, &y);
142
G_warning(_("Cannot calculate area centroid"));
146
ret = Vect_tin_get_z(&Out, x, y, &z, &angle, &slope);
147
G_debug(3, "area centroid z: %f", z);
149
G_warning(_("Cannot calculate area centroid z coordinate"));
153
Vect_append_point(Points, x, y, z);
154
Vect_cat_set(Cats, 1, area);
156
Vect_write_line(&Out, GV_CENTROID, Points, Cats);
159
Vect_build_partial(&Out, GV_BUILD_NONE);