PostGIS  3.0.6dev-r@@SVN_REVISION@@
cu_triangulate.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * Copyright (C) 2015 Daniel Baston <dbaston@gmail.com>
7  * Copyright (C) 2012 Sandro Santilli <strk@kbt.io>
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU General Public Licence. See the COPYING file.
11  *
12  **********************************************************************/
13 
14 #include "CUnit/Basic.h"
15 #include "cu_tester.h"
16 
17 #include "liblwgeom_internal.h"
18 
19 static void
21 {
22  LWGEOM *in, *tmp, *out;
23  char *wkt, *exp_wkt;
24 
25  /* Because i don't trust that much prior tests... ;) */
27 
28  in = lwgeom_from_wkt("MULTIPOINT(10 0, 20 0, 5 5)", LW_PARSER_CHECK_NONE);
29 
30  tmp = lwgeom_delaunay_triangulation(in, 0, 0);
31  lwgeom_free(in);
32  out = lwgeom_normalize(tmp);
33  lwgeom_free(tmp);
34 
35  wkt = lwgeom_to_ewkt(out);
36  lwgeom_free(out);
37 
38  exp_wkt = "GEOMETRYCOLLECTION(POLYGON((5 5,20 0,10 0,5 5)))";
39  if (strcmp(wkt, exp_wkt))
40  fprintf(stderr, "\nExp: %s\nObt: %s\n", exp_wkt, wkt);
41 
42  CU_ASSERT_STRING_EQUAL(wkt, exp_wkt);
43  lwfree(wkt);
44 }
45 
46 static void
48 {
49  LWGEOM *in = lwgeom_from_wkt("MULTIPOINT(4 4, 5 5, 6 6)", LW_PARSER_CHECK_NONE);
50 
51  LWGEOM *out_boundaries = lwgeom_voronoi_diagram(in, NULL, 0, 0);
52  LWGEOM *out_lines = lwgeom_voronoi_diagram(in, NULL, 0, 1);
53 
54  /* For boundaries we get a generic LWCOLLECTION */
55  CU_ASSERT_EQUAL(COLLECTIONTYPE, lwgeom_get_type(out_boundaries));
56  /* For lines we get a MULTILINETYPE */
57  CU_ASSERT_EQUAL(MULTILINETYPE, lwgeom_get_type(out_lines));
58 
59  lwgeom_free(in);
60  lwgeom_free(out_boundaries);
61  lwgeom_free(out_lines);
62 }
63 
64 static void
66 {
67  LWGEOM *in = lwgeom_from_wkt("MULTIPOINT(4 4, 5 5, 6 6)", LW_PARSER_CHECK_NONE);
68  LWGEOM *for_extent = lwgeom_from_wkt("LINESTRING (-10 -10, 10 10)", LW_PARSER_CHECK_NONE);
69  const GBOX *clipping_extent = lwgeom_get_bbox(for_extent);
70 
71  LWGEOM *out = lwgeom_voronoi_diagram(in, clipping_extent, 0, 0);
72  const GBOX *output_extent = lwgeom_get_bbox(out);
73 
74  CU_ASSERT_TRUE(gbox_same(clipping_extent, output_extent));
75 
76  lwgeom_free(in);
77  lwgeom_free(for_extent);
78  lwgeom_free(out);
79 }
80 
81 static void
82 assert_empty_diagram(char *wkt, double tolerance)
83 {
85  LWGEOM *out = lwgeom_voronoi_diagram(in, NULL, tolerance, 0);
86 
87  CU_ASSERT_TRUE(lwgeom_is_collection(out));
88  CU_ASSERT_EQUAL(COLLECTIONTYPE, lwgeom_get_type(out));
89 
90  lwgeom_free(in);
91  lwgeom_free(out);
92 }
93 
94 static void
96 {
97  assert_empty_diagram("POLYGON EMPTY", 0);
98  assert_empty_diagram("POINT (1 2)", 0);
99 
100  /* This one produces an empty diagram because our two unqiue points
101  * collapse onto one after considering the tolerance. */
102  assert_empty_diagram("MULTIPOINT (0 0, 0 0.00001)", 0.001);
103 }
104 
105 /*
106 ** Used by test harness to register the tests in this file.
107 */
108 void triangulate_suite_setup(void);
109 void
111 {
112  CU_pSuite suite = CU_add_suite("triangulate", NULL, NULL);
117 }
static void test_lwgeom_voronoi_diagram(void)
void triangulate_suite_setup(void)
static void test_lwgeom_voronoi_diagram_expected_empty(void)
static void test_lwgeom_voronoi_diagram_custom_envelope(void)
static void assert_empty_diagram(char *wkt, double tolerance)
static void test_lwgeom_delaunay_triangulation(void)
int gbox_same(const GBOX *g1, const GBOX *g2)
Check if 2 given Gbox are the same.
Definition: gbox.c:164
void cu_error_msg_reset()
#define PG_ADD_TEST(suite, testfunc)
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
LWGEOM * lwgeom_delaunay_triangulation(const LWGEOM *geom, double tolerance, int32_t edgeOnly)
Take vertices of a geometry and build a delaunay triangulation on them.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2060
#define MULTILINETYPE
Definition: liblwgeom.h:120
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:547
LWGEOM * lwgeom_voronoi_diagram(const LWGEOM *g, const GBOX *env, double tolerance, int output_edges)
Take vertices of a geometry and build the Voronoi diagram.
void lwfree(void *mem)
Definition: lwutil.c:242
LWGEOM * lwgeom_normalize(const LWGEOM *geom)
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1079
const GBOX * lwgeom_get_bbox(const LWGEOM *lwgeom)
Get a non-empty geometry bounding box, computing and caching it if not already there.
Definition: lwgeom.c:725
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:905
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwinline.h:135