[GRASS-SVN] r35461 - grass/trunk/vector/v.voronoi
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Jan 17 10:06:01 EST 2009
Author: neteler
Date: 2009-01-17 10:06:01 -0500 (Sat, 17 Jan 2009)
New Revision: 35461
Added:
grass/trunk/vector/v.voronoi/main.c
Removed:
grass/trunk/vector/v.voronoi/delaunay_DEPRECATED
grass/trunk/vector/v.voronoi/dt_main.c
grass/trunk/vector/v.voronoi/v.delaunay.html
grass/trunk/vector/v.voronoi/vo_main.c
Modified:
grass/trunk/vector/v.voronoi/Makefile
grass/trunk/vector/v.voronoi/README
Log:
removed outdated delaunay part
Modified: grass/trunk/vector/v.voronoi/Makefile
===================================================================
--- grass/trunk/vector/v.voronoi/Makefile 2009-01-17 14:55:30 UTC (rev 35460)
+++ grass/trunk/vector/v.voronoi/Makefile 2009-01-17 15:06:01 UTC (rev 35461)
@@ -1,24 +1,18 @@
MODULE_TOPDIR = ../..
-VORONOI = v.voronoi
-# DELAUNAY = v.delaunay
+PGM = v.voronoi
-PROGRAMS = $(VORONOI)
-# $(DELAUNAY)
-
SWEEP_OBJS = sw_edgelist.o sw_geometry.o sw_heap.o sw_main.o sw_memory.o sw_output.o \
sw_voronoi.o
-v_voronoi_OBJS = vo_main.o vo_extend.o vo_write.o dt_write.o $(SWEEP_OBJS)
+v_voronoi_OBJS = vo_main.o vo_extend.o vo_write.o $(SWEEP_OBJS)
-# v_delaunay_OBJS = dt_main.o vo_extend.o vo_write.o dt_write.o $(SWEEP_OBJS)
-
LIBES = $(VECTLIB) $(GISLIB)
DEPENDENCIES= $(VECTDEP) $(GISDEP)
EXTRA_INC = $(VECT_INC)
EXTRA_CFLAGS = $(VECT_CFLAGS)
-include $(MODULE_TOPDIR)/include/Make/Multi.make
+include $(MODULE_TOPDIR)/include/Make/Module.make
-default: multi
+default: cmd
Modified: grass/trunk/vector/v.voronoi/README
===================================================================
--- grass/trunk/vector/v.voronoi/README 2009-01-17 14:55:30 UTC (rev 35460)
+++ grass/trunk/vector/v.voronoi/README 2009-01-17 15:06:01 UTC (rev 35461)
@@ -58,3 +58,10 @@
- TODO: complete it for Voronoi if desired
Benjamin Ducke
+
+-----------------------------------------
+Jan 2009
+
+Removed delaunay part as rewritten in ../v.delaunay2/
+
+Markus Neteler
Deleted: grass/trunk/vector/v.voronoi/delaunay_DEPRECATED
===================================================================
--- grass/trunk/vector/v.voronoi/delaunay_DEPRECATED 2009-01-17 14:55:30 UTC (rev 35460)
+++ grass/trunk/vector/v.voronoi/delaunay_DEPRECATED 2009-01-17 15:06:01 UTC (rev 35461)
@@ -1,2 +0,0 @@
-no longer maintained.
-Continued in ../v.delaunay2/
Deleted: grass/trunk/vector/v.voronoi/dt_main.c
===================================================================
--- grass/trunk/vector/v.voronoi/dt_main.c 2009-01-17 14:55:30 UTC (rev 35460)
+++ grass/trunk/vector/v.voronoi/dt_main.c 2009-01-17 15:06:01 UTC (rev 35461)
@@ -1,158 +0,0 @@
-
-/*-s.delaunay
-**
-** Author: James Darrell McCauley (mccauley at ecn.purdue.edu)
-** USDA Fellow
-** Department of Agricultural Engineering
-** Purdue University
-** West Lafayette, Indiana 47907-1146 USA
-**
-** Permission to use, copy, modify, and distribute this software and its
-** documentation for any purpose and without fee is hereby granted. This
-** software is provided "as is" without express or implied warranty.
-**
-** Modification History:
-** 06 Feb 93 - James Darrell McCauley <mccauley at ecn.purdue.edu> pieced
-** this together from stuff he found on netlib (see the manpage).
-**
-** 4 2008: Benjamin Ducke - 3D support + better memory management
-**/
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <math.h>
-#include <grass/gis.h>
-#include <grass/Vect.h>
-#include <grass/glocale.h>
-#include "sw_defs.h"
-#include "defs.h"
-
-int main(int argc, char **argv)
-{
- struct Flag *reg_flag, *line_flag;
- struct Option *in_opt, *out_opt;
- struct GModule *module;
- struct line_pnts *Points;
- struct line_cats *Cats;
- int nareas, area;
-
- G_gisinit(argv[0]);
-
- module = G_define_module();
- module->keywords = _("vector");
- module->description = _("Creates a Delaunay triangulation from an input "
- "vector map containing points or centroids.");
-
- in_opt = G_define_standard_option(G_OPT_V_INPUT);
- out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
-
- reg_flag = G_define_flag();
- reg_flag->key = 'r';
- reg_flag->description = _("Use only points in current region");
-
- line_flag = G_define_flag();
- line_flag->key = 'l';
- line_flag->description =
- _("Output triangulation as a graph (lines), not areas");
-
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
- if (line_flag->answer)
- Type = GV_LINE;
- else
- Type = GV_BOUNDARY;
-
- All = reg_flag->answer ? 0 : 1;
-
- Points = Vect_new_line_struct();
- Cats = Vect_new_cats_struct();
-
- /* open files */
- Vect_set_open_level(2);
- Vect_open_old(&In, in_opt->answer, "");
-
- /* check if we have a 3D input points map */
- mode3d = 0;
- if (Vect_is_3d(&In)) {
- mode3d = 1;
- }
-
-
- if (mode3d) {
- if (0 > Vect_open_new(&Out, out_opt->answer, 1)) {
- G_fatal_error(_("Unable to create vector map <%s>"),
- out_opt->answer);
- }
- }
- else {
- if (0 > Vect_open_new(&Out, out_opt->answer, 0)) {
- G_fatal_error(_("Unable to create vector map <%s>"),
- out_opt->answer);
- }
-
- }
-
- Vect_hist_copy(&In, &Out);
- Vect_hist_command(&Out);
-
- Vect_build_partial(&Out, GV_BUILD_BASE);
-
- /* initialize working region */
- G_get_window(&Window);
- G_percent(0, 100, 1);
- Vect_region_box(&Window, &Box);
-
- freeinit(&sfl, sizeof *sites);
-
- readsites();
-
- siteidx = 0;
- geominit();
-
- triangulate = 1;
- plot = 0;
- debug = 0;
- voronoi(triangulate, nextone);
-
- Vect_close(&In);
-
- Vect_build_partial(&Out, GV_BUILD_ATTACH_ISLES);
-
- nareas = Vect_get_num_areas(&Out);
- G_debug(3, "nareas = %d", nareas);
- for (area = 1; area <= nareas; area++) {
- double x, y, z, angle, slope;
- int ret;
-
- G_percent(area, nareas, 2);
- Vect_reset_line(Points);
- Vect_reset_cats(Cats);
-
- ret = Vect_get_point_in_area(&Out, area, &x, &y);
- if (ret < 0) {
- G_warning(_("Cannot calculate area centroid"));
- continue;
- }
-
- ret = Vect_tin_get_z(&Out, x, y, &z, &angle, &slope);
- G_debug(3, "area centroid z: %f", z);
- if (ret < 0) {
- G_warning(_("Cannot calculate area centroid z coordinate"));
- continue;
- }
-
- Vect_append_point(Points, x, y, z);
- Vect_cat_set(Cats, 1, area);
-
- Vect_write_line(&Out, GV_CENTROID, Points, Cats);
- }
-
- Vect_build_partial(&Out, GV_BUILD_NONE);
- Vect_build(&Out);
- Vect_close(&Out);
-
- exit(EXIT_SUCCESS);
-}
Copied: grass/trunk/vector/v.voronoi/main.c (from rev 35460, grass/trunk/vector/v.voronoi/vo_main.c)
===================================================================
--- grass/trunk/vector/v.voronoi/main.c (rev 0)
+++ grass/trunk/vector/v.voronoi/main.c 2009-01-17 15:06:01 UTC (rev 35461)
@@ -0,0 +1,354 @@
+
+/****************************************************************************
+ *
+ * MODULE: v.voronoi
+ * AUTHOR(S): James McCauley <mccauley ecn.purdue.edu>, s.voronoi, based
+ * on netlib code (see README) (original contributor)
+ * Andrea Aime <aaime libero.it>
+ * Radim Blazek <radim.blazek gmail.com> (GRASS 5.1 v.voronoi)
+ * Glynn Clements <glynn gclements.plus.com>,
+ * Markus Neteler <neteler itc.it>
+ * PURPOSE: produce a Voronoi diagram using vector points
+ * COPYRIGHT: (C) 1993-2006 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.
+ *
+ *****************************************************************************/
+
+/*-s.voronoi
+**
+** Author: James Darrell McCauley (mccauley at ecn.purdue.edu)
+** USDA Fellow
+** Department of Agricultural Engineering
+** Purdue University
+** West Lafayette, Indiana 47907-1146 USA
+**
+** Permission to use, copy, modify, and distribute this software and its
+** documentation for any purpose and without fee is hereby granted. This
+** software is provided "as is" without express or implied warranty.
+**
+** Modification History:
+** 06 Feb 93 - James Darrell McCauley <mccauley at ecn.purdue.edu> pieced
+** this together from stuff he found on netlib (see the manpage).
+**/
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <math.h>
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/Vect.h>
+#include <grass/glocale.h>
+#include "sw_defs.h"
+#include "defs.h"
+
+typedef struct
+{
+ double x, y;
+} COOR;
+
+int cmp(void *a, void *b)
+{
+ COOR *ca = (COOR *) a;
+ COOR *cb = (COOR *) b;
+ double thresh = 1e-10;
+ double ma, mb;
+
+ /* calculate measure */
+ ma = mb = 0.0;
+
+ if (fabs(ca->y - Box.S) < thresh) { /* bottom */
+ ma = ca->x - Box.W;
+ }
+ else if (fabs(ca->x - Box.E) < thresh) { /* right */
+ ma = (Box.E - Box.W) + (ca->y - Box.S);
+ }
+ else if (fabs(ca->y - Box.N) < thresh) { /* top */
+ ma = (Box.E - Box.W) + (Box.N - Box.S) + (Box.E - ca->x);
+ }
+ else { /* left */
+ ma = 2 * (Box.E - Box.W) + (Box.N - Box.S) + (Box.N - ca->y);
+ }
+
+
+ if (fabs(cb->y - Box.S) < thresh) { /* bottom */
+ mb = cb->x - Box.W;
+ }
+ else if (fabs(cb->x - Box.E) < thresh) { /* right */
+ mb = (Box.E - Box.W) + (cb->y - Box.S);
+ }
+ else if (fabs(cb->y - Box.N) < thresh) { /* top */
+ mb = (Box.E - Box.W) + (Box.N - Box.S) + (Box.E - cb->x);
+ }
+ else { /* left */
+ mb = 2 * (Box.E - Box.W) + (Box.N - Box.S) + (Box.N - cb->y);
+ }
+
+ if (ma < mb)
+ return -1;
+ if (ma > mb)
+ return 1;
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ int i;
+ int **cats, *ncats, nfields, *fields;
+ struct Flag *line_flag;
+
+ /* struct Flag *all_flag; */
+ struct Option *in_opt, *out_opt;
+ struct Flag *table_flag;
+ struct GModule *module;
+ struct line_pnts *Points;
+ struct line_cats *Cats;
+ int node, nnodes;
+ COOR *coor;
+ int ncoor, acoor;
+
+ G_gisinit(argv[0]);
+
+ module = G_define_module();
+ module->keywords = _("vector");
+ module->description = _("Creates a Voronoi diagram from an input vector "
+ "map containing points or centroids.");
+
+ in_opt = G_define_standard_option(G_OPT_V_INPUT);
+ out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
+
+ /*
+ all_flag = G_define_flag ();
+ all_flag->key = 'a';
+ all_flag->description = _("Use all points (do not limit to current region)");
+ */
+
+ line_flag = G_define_flag();
+ line_flag->key = 'l';
+ line_flag->description =
+ _("Output tessellation as a graph (lines), not areas");
+
+ table_flag = G_define_flag();
+ table_flag->key = 't';
+ table_flag->description = _("Do not create attribute table");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+
+ if (line_flag->answer)
+ Type = GV_LINE;
+ else
+ Type = GV_BOUNDARY;
+
+ All = 0;
+
+ Points = Vect_new_line_struct();
+ Cats = Vect_new_cats_struct();
+
+ /* open files */
+ Vect_set_open_level(2);
+ Vect_open_old(&In, in_opt->answer, "");
+
+ if (Vect_open_new(&Out, out_opt->answer, 0) < 0)
+ G_fatal_error(_("Unable to create vector map <%s>"), out_opt->answer);
+
+ Vect_hist_copy(&In, &Out);
+ Vect_hist_command(&Out);
+
+ /* initialize working region */
+ G_get_window(&Window);
+ Vect_region_box(&Window, &Box);
+
+ freeinit(&sfl, sizeof *sites);
+
+ readsites();
+
+ siteidx = 0;
+ geominit();
+
+ triangulate = 0;
+ plot = 0;
+ debug = 0;
+ voronoi(triangulate, nextone);
+
+ /* Close free ends by cyrrent region */
+ Vect_build_partial(&Out, GV_BUILD_BASE);
+
+ ncoor = 0;
+ acoor = 100;
+ coor = (COOR *) G_malloc(sizeof(COOR) * acoor);
+
+ nnodes = Vect_get_num_nodes(&Out);
+ for (node = 1; node <= nnodes; node++) {
+ double x, y;
+
+ if (Vect_get_node_n_lines(&Out, node) < 2) { /* add coordinates */
+ Vect_get_node_coor(&Out, node, &x, &y, NULL);
+
+ if (ncoor == acoor - 5) { /* always space for 5 region corners */
+ acoor += 100;
+ coor = (COOR *) G_realloc(coor, sizeof(COOR) * acoor);
+ }
+
+ coor[ncoor].x = x;
+ coor[ncoor].y = y;
+ ncoor++;
+ }
+ }
+
+ /* Add region corners */
+ coor[ncoor].x = Box.W;
+ coor[ncoor].y = Box.S;
+ ncoor++;
+ coor[ncoor].x = Box.E;
+ coor[ncoor].y = Box.S;
+ ncoor++;
+ coor[ncoor].x = Box.E;
+ coor[ncoor].y = Box.N;
+ ncoor++;
+ coor[ncoor].x = Box.W;
+ coor[ncoor].y = Box.N;
+ ncoor++;
+
+ /* Sort */
+ qsort(coor, ncoor, sizeof(COOR), (void *)cmp);
+
+ /* add last (first corner) */
+ coor[ncoor].x = Box.W;
+ coor[ncoor].y = Box.S;
+ ncoor++;
+
+ for (i = 1; i < ncoor; i++) {
+ if (coor[i].x == coor[i - 1].x && coor[i].y == coor[i - 1].y)
+ continue; /* duplicate */
+
+ Vect_reset_line(Points);
+ Vect_append_point(Points, coor[i].x, coor[i].y, 0.0);
+ Vect_append_point(Points, coor[i - 1].x, coor[i - 1].y, 0.0);
+ Vect_write_line(&Out, Type, Points, Cats);
+ }
+
+ G_free(coor);
+
+ /* Copy input points as centroids */
+ nfields = Vect_cidx_get_num_fields(&In);
+ cats = (int **)G_malloc(nfields * sizeof(int *));
+ ncats = (int *)G_malloc(nfields * sizeof(int));
+ fields = (int *)G_malloc(nfields * sizeof(int));
+ for (i = 0; i < nfields; i++) {
+ ncats[i] = 0;
+ cats[i] =
+ (int *)G_malloc(Vect_cidx_get_num_cats_by_index(&In, i) *
+ sizeof(int));
+ fields[i] = Vect_cidx_get_field_number(&In, i);
+ }
+
+ if (1) {
+ int line, nlines, type;
+
+ nlines = Vect_get_num_lines(&In);
+
+ for (line = 1; line <= nlines; line++) {
+
+ type = Vect_read_line(&In, Points, Cats, line);
+ if (!(type & GV_POINTS))
+ continue;
+
+ if (!Vect_point_in_box(Points->x[0], Points->y[0], 0.0, &Box))
+ continue;
+
+ Vect_write_line(&Out, GV_CENTROID, Points, Cats);
+
+
+ for (i = 0; i < Cats->n_cats; i++) {
+ int f, j;
+
+ for (j = 0; j < nfields; j++) { /* find field */
+ if (fields[j] == Cats->field[i]) {
+ f = j;
+ break;
+ }
+ }
+ cats[f][ncats[f]] = Cats->cat[i];
+ ncats[f]++;
+ }
+ }
+
+ }
+
+ /* Copy tables */
+ if (!(table_flag->answer)) {
+ int ttype, ntabs = 0;
+ struct field_info *IFi, *OFi;
+
+ /* Number of output tabs */
+ for (i = 0; i < Vect_get_num_dblinks(&In); i++) {
+ int f, j;
+
+ IFi = Vect_get_dblink(&In, i);
+
+ for (j = 0; j < nfields; j++) { /* find field */
+ if (fields[j] == IFi->number) {
+ f = j;
+ break;
+ }
+ }
+ if (ncats[f] > 0)
+ ntabs++;
+ }
+
+ if (ntabs > 1)
+ ttype = GV_MTABLE;
+ else
+ ttype = GV_1TABLE;
+
+ for (i = 0; i < nfields; i++) {
+ int ret;
+
+ if (fields[i] == 0)
+ continue;
+
+ G_message(_("Layer %d"), fields[i]);
+
+ /* Make a list of categories */
+ IFi = Vect_get_field(&In, fields[i]);
+ if (!IFi) { /* no table */
+ G_message(_("No table"));
+ continue;
+ }
+
+ OFi =
+ Vect_default_field_info(&Out, IFi->number, IFi->name, ttype);
+
+ ret =
+ db_copy_table_by_ints(IFi->driver, IFi->database, IFi->table,
+ OFi->driver,
+ Vect_subst_var(OFi->database, &Out),
+ OFi->table, IFi->key, cats[i],
+ ncats[i]);
+
+ if (ret == DB_FAILED) {
+ G_warning(_("Cannot copy table"));
+ }
+ else {
+ Vect_map_add_dblink(&Out, OFi->number, OFi->name, OFi->table,
+ IFi->key, OFi->database, OFi->driver);
+ }
+ G_done_msg(" ");
+ }
+ }
+
+
+ Vect_close(&In);
+
+ Vect_build_partial(&Out, GV_BUILD_NONE);
+ Vect_build(&Out);
+ Vect_close(&Out);
+
+ exit(EXIT_SUCCESS);
+}
Property changes on: grass/trunk/vector/v.voronoi/main.c
___________________________________________________________________
Name: svn:mime-type
+ text/x-csrc
Name: svn:keywords
+ Author Date Id
Name: svn:mergeinfo
+
Name: svn:eol-style
+ native
Deleted: grass/trunk/vector/v.voronoi/v.delaunay.html
===================================================================
--- grass/trunk/vector/v.voronoi/v.delaunay.html 2009-01-17 14:55:30 UTC (rev 35460)
+++ grass/trunk/vector/v.voronoi/v.delaunay.html 2009-01-17 15:06:01 UTC (rev 35461)
@@ -1,65 +0,0 @@
-<H2>DESCRIPTION</H2>
-
-<EM>v.delaunay</EM> uses an existing vector points map (<B>input</B>)
-to create a Delaunay triangulation vector map (<B>output</B>).
-<P>
-
-<BR>
-Delaunay triangulation and Voronoi diagram example:
-<center>
-<img src=v_voronoi_delaunay.png border=1><BR>
-<table border=0 width=590>
-<tr><td><center>
-<i>Delaunay Triangulation (left pane), Voronoi diagram (center pane),
-and both (right pane)</i>
-</center></td></tr>
-</table>
-</center>
-
-
-<H2>EXAMPLE</H2>
-
-Commands used with the Spearfish dataset to create the above figure.
-<div class="code"><pre>
- g.region n=4927250 s=4919400 w=588650 e=594850
- d.frame -c fr=one at=0,100,0,33.3333
- d.frame -c fr=two at=0,100,33.3333,66.6667
- d.frame -c fr=three at=0,100,66.6667,100
-
- v.delaunay -lr in=archsites out=arch_delaunay
- d.frame -s one
- d.vect arch_delaunay
- d.vect archsites color=red fcolor=red size=5 icon=basic/circle
-
- v.voronoi -l in=archsites out=arch_voronoi
- d.frame -s two
- d.vect arch_voronoi type=line
- d.vect archsites color=red fcolor=red size=5 icon=basic/circle
-
- d.frame -s three
- d.vect arch_voronoi type=line
- d.vect arch_delaunay color=blue
- d.vect archsites color=red fcolor=red size=5 icon=basic/circle
-</pre></div>
-
-
-<H2>REFERENCES</H2>
-<EM>Steve J. Fortune, (1987). A Sweepline Algorithm for
- Voronoi Diagrams, Algorithmica 2, 153-174.</EM>
-
-
-<H2>SEE ALSO</H2>
-<EM>
-<A HREF="v.voronoi.html">v.voronoi</A>,
-<A HREF="v.hull.html">v.hull</A>
-</EM>
-
-
-<H2>AUTHORS</H2>
-James Darrell McCauley, Purdue University<br>
-GRASS 5 update, improvements: <a href="mailto:aaime at libero.it">Andrea Aime</a>,
- Modena, Italy<br>
-GRASS 5.7 update: Radim Blazek
-
-<p>
-<i>Last changed: $Date$</i></p>
Deleted: grass/trunk/vector/v.voronoi/vo_main.c
===================================================================
--- grass/trunk/vector/v.voronoi/vo_main.c 2009-01-17 14:55:30 UTC (rev 35460)
+++ grass/trunk/vector/v.voronoi/vo_main.c 2009-01-17 15:06:01 UTC (rev 35461)
@@ -1,354 +0,0 @@
-
-/****************************************************************************
- *
- * MODULE: v.voronoi
- * AUTHOR(S): James McCauley <mccauley ecn.purdue.edu>, s.voronoi, based
- * on netlib code (see README) (original contributor)
- * Andrea Aime <aaime libero.it>
- * Radim Blazek <radim.blazek gmail.com> (GRASS 5.1 v.voronoi)
- * Glynn Clements <glynn gclements.plus.com>,
- * Markus Neteler <neteler itc.it>
- * PURPOSE: produce a Voronoi diagram using vector points
- * COPYRIGHT: (C) 1993-2006 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.
- *
- *****************************************************************************/
-
-/*-s.voronoi
-**
-** Author: James Darrell McCauley (mccauley at ecn.purdue.edu)
-** USDA Fellow
-** Department of Agricultural Engineering
-** Purdue University
-** West Lafayette, Indiana 47907-1146 USA
-**
-** Permission to use, copy, modify, and distribute this software and its
-** documentation for any purpose and without fee is hereby granted. This
-** software is provided "as is" without express or implied warranty.
-**
-** Modification History:
-** 06 Feb 93 - James Darrell McCauley <mccauley at ecn.purdue.edu> pieced
-** this together from stuff he found on netlib (see the manpage).
-**/
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <math.h>
-#include <grass/gis.h>
-#include <grass/dbmi.h>
-#include <grass/Vect.h>
-#include <grass/glocale.h>
-#include "sw_defs.h"
-#include "defs.h"
-
-typedef struct
-{
- double x, y;
-} COOR;
-
-int cmp(void *a, void *b)
-{
- COOR *ca = (COOR *) a;
- COOR *cb = (COOR *) b;
- double thresh = 1e-10;
- double ma, mb;
-
- /* calculate measure */
- ma = mb = 0.0;
-
- if (fabs(ca->y - Box.S) < thresh) { /* bottom */
- ma = ca->x - Box.W;
- }
- else if (fabs(ca->x - Box.E) < thresh) { /* right */
- ma = (Box.E - Box.W) + (ca->y - Box.S);
- }
- else if (fabs(ca->y - Box.N) < thresh) { /* top */
- ma = (Box.E - Box.W) + (Box.N - Box.S) + (Box.E - ca->x);
- }
- else { /* left */
- ma = 2 * (Box.E - Box.W) + (Box.N - Box.S) + (Box.N - ca->y);
- }
-
-
- if (fabs(cb->y - Box.S) < thresh) { /* bottom */
- mb = cb->x - Box.W;
- }
- else if (fabs(cb->x - Box.E) < thresh) { /* right */
- mb = (Box.E - Box.W) + (cb->y - Box.S);
- }
- else if (fabs(cb->y - Box.N) < thresh) { /* top */
- mb = (Box.E - Box.W) + (Box.N - Box.S) + (Box.E - cb->x);
- }
- else { /* left */
- mb = 2 * (Box.E - Box.W) + (Box.N - Box.S) + (Box.N - cb->y);
- }
-
- if (ma < mb)
- return -1;
- if (ma > mb)
- return 1;
- return 0;
-}
-
-int main(int argc, char **argv)
-{
- int i;
- int **cats, *ncats, nfields, *fields;
- struct Flag *line_flag;
-
- /* struct Flag *all_flag; */
- struct Option *in_opt, *out_opt;
- struct Flag *table_flag;
- struct GModule *module;
- struct line_pnts *Points;
- struct line_cats *Cats;
- int node, nnodes;
- COOR *coor;
- int ncoor, acoor;
-
- G_gisinit(argv[0]);
-
- module = G_define_module();
- module->keywords = _("vector");
- module->description = _("Creates a Voronoi diagram from an input vector "
- "map containing points or centroids.");
-
- in_opt = G_define_standard_option(G_OPT_V_INPUT);
- out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
-
- /*
- all_flag = G_define_flag ();
- all_flag->key = 'a';
- all_flag->description = _("Use all points (do not limit to current region)");
- */
-
- line_flag = G_define_flag();
- line_flag->key = 'l';
- line_flag->description =
- _("Output tessellation as a graph (lines), not areas");
-
- table_flag = G_define_flag();
- table_flag->key = 't';
- table_flag->description = _("Do not create attribute table");
-
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
-
- if (line_flag->answer)
- Type = GV_LINE;
- else
- Type = GV_BOUNDARY;
-
- All = 0;
-
- Points = Vect_new_line_struct();
- Cats = Vect_new_cats_struct();
-
- /* open files */
- Vect_set_open_level(2);
- Vect_open_old(&In, in_opt->answer, "");
-
- if (Vect_open_new(&Out, out_opt->answer, 0) < 0)
- G_fatal_error(_("Unable to create vector map <%s>"), out_opt->answer);
-
- Vect_hist_copy(&In, &Out);
- Vect_hist_command(&Out);
-
- /* initialize working region */
- G_get_window(&Window);
- Vect_region_box(&Window, &Box);
-
- freeinit(&sfl, sizeof *sites);
-
- readsites();
-
- siteidx = 0;
- geominit();
-
- triangulate = 0;
- plot = 0;
- debug = 0;
- voronoi(triangulate, nextone);
-
- /* Close free ends by cyrrent region */
- Vect_build_partial(&Out, GV_BUILD_BASE);
-
- ncoor = 0;
- acoor = 100;
- coor = (COOR *) G_malloc(sizeof(COOR) * acoor);
-
- nnodes = Vect_get_num_nodes(&Out);
- for (node = 1; node <= nnodes; node++) {
- double x, y;
-
- if (Vect_get_node_n_lines(&Out, node) < 2) { /* add coordinates */
- Vect_get_node_coor(&Out, node, &x, &y, NULL);
-
- if (ncoor == acoor - 5) { /* always space for 5 region corners */
- acoor += 100;
- coor = (COOR *) G_realloc(coor, sizeof(COOR) * acoor);
- }
-
- coor[ncoor].x = x;
- coor[ncoor].y = y;
- ncoor++;
- }
- }
-
- /* Add region corners */
- coor[ncoor].x = Box.W;
- coor[ncoor].y = Box.S;
- ncoor++;
- coor[ncoor].x = Box.E;
- coor[ncoor].y = Box.S;
- ncoor++;
- coor[ncoor].x = Box.E;
- coor[ncoor].y = Box.N;
- ncoor++;
- coor[ncoor].x = Box.W;
- coor[ncoor].y = Box.N;
- ncoor++;
-
- /* Sort */
- qsort(coor, ncoor, sizeof(COOR), (void *)cmp);
-
- /* add last (first corner) */
- coor[ncoor].x = Box.W;
- coor[ncoor].y = Box.S;
- ncoor++;
-
- for (i = 1; i < ncoor; i++) {
- if (coor[i].x == coor[i - 1].x && coor[i].y == coor[i - 1].y)
- continue; /* duplicate */
-
- Vect_reset_line(Points);
- Vect_append_point(Points, coor[i].x, coor[i].y, 0.0);
- Vect_append_point(Points, coor[i - 1].x, coor[i - 1].y, 0.0);
- Vect_write_line(&Out, Type, Points, Cats);
- }
-
- G_free(coor);
-
- /* Copy input points as centroids */
- nfields = Vect_cidx_get_num_fields(&In);
- cats = (int **)G_malloc(nfields * sizeof(int *));
- ncats = (int *)G_malloc(nfields * sizeof(int));
- fields = (int *)G_malloc(nfields * sizeof(int));
- for (i = 0; i < nfields; i++) {
- ncats[i] = 0;
- cats[i] =
- (int *)G_malloc(Vect_cidx_get_num_cats_by_index(&In, i) *
- sizeof(int));
- fields[i] = Vect_cidx_get_field_number(&In, i);
- }
-
- if (1) {
- int line, nlines, type;
-
- nlines = Vect_get_num_lines(&In);
-
- for (line = 1; line <= nlines; line++) {
-
- type = Vect_read_line(&In, Points, Cats, line);
- if (!(type & GV_POINTS))
- continue;
-
- if (!Vect_point_in_box(Points->x[0], Points->y[0], 0.0, &Box))
- continue;
-
- Vect_write_line(&Out, GV_CENTROID, Points, Cats);
-
-
- for (i = 0; i < Cats->n_cats; i++) {
- int f, j;
-
- for (j = 0; j < nfields; j++) { /* find field */
- if (fields[j] == Cats->field[i]) {
- f = j;
- break;
- }
- }
- cats[f][ncats[f]] = Cats->cat[i];
- ncats[f]++;
- }
- }
-
- }
-
- /* Copy tables */
- if (!(table_flag->answer)) {
- int ttype, ntabs = 0;
- struct field_info *IFi, *OFi;
-
- /* Number of output tabs */
- for (i = 0; i < Vect_get_num_dblinks(&In); i++) {
- int f, j;
-
- IFi = Vect_get_dblink(&In, i);
-
- for (j = 0; j < nfields; j++) { /* find field */
- if (fields[j] == IFi->number) {
- f = j;
- break;
- }
- }
- if (ncats[f] > 0)
- ntabs++;
- }
-
- if (ntabs > 1)
- ttype = GV_MTABLE;
- else
- ttype = GV_1TABLE;
-
- for (i = 0; i < nfields; i++) {
- int ret;
-
- if (fields[i] == 0)
- continue;
-
- G_message(_("Layer %d"), fields[i]);
-
- /* Make a list of categories */
- IFi = Vect_get_field(&In, fields[i]);
- if (!IFi) { /* no table */
- G_message(_("No table"));
- continue;
- }
-
- OFi =
- Vect_default_field_info(&Out, IFi->number, IFi->name, ttype);
-
- ret =
- db_copy_table_by_ints(IFi->driver, IFi->database, IFi->table,
- OFi->driver,
- Vect_subst_var(OFi->database, &Out),
- OFi->table, IFi->key, cats[i],
- ncats[i]);
-
- if (ret == DB_FAILED) {
- G_warning(_("Cannot copy table"));
- }
- else {
- Vect_map_add_dblink(&Out, OFi->number, OFi->name, OFi->table,
- IFi->key, OFi->database, OFi->driver);
- }
- G_done_msg(" ");
- }
- }
-
-
- Vect_close(&In);
-
- Vect_build_partial(&Out, GV_BUILD_NONE);
- Vect_build(&Out);
- Vect_close(&Out);
-
- exit(EXIT_SUCCESS);
-}
More information about the grass-commit
mailing list