PostGIS  2.5.7dev-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 
20 {
21  LWGEOM *in, *tmp, *out;
22  char *wkt, *exp_wkt;
23 
24  /* Because i don't trust that much prior tests... ;) */
26 
27  in = lwgeom_from_wkt("MULTIPOINT(10 0, 20 0, 5 5)", LW_PARSER_CHECK_NONE);
28 
29  tmp = lwgeom_delaunay_triangulation(in, 0, 0);
30  lwgeom_free(in);
31  out = lwgeom_normalize(tmp);
32  lwgeom_free(tmp);
33 
34  wkt = lwgeom_to_ewkt(out);
35  lwgeom_free(out);
36 
37  exp_wkt = "GEOMETRYCOLLECTION(POLYGON((5 5,20 0,10 0,5 5)))";
38  if ( strcmp(wkt, exp_wkt) )
39  {
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 test_lwgeom_voronoi_diagram(void)
47 {
48 #if POSTGIS_GEOS_VERSION >= 35
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 #endif /* POSTGIS_GEOS_VERSION >= 35 */
63 }
64 
66 {
67 #if POSTGIS_GEOS_VERSION >= 35
68  LWGEOM* in = lwgeom_from_wkt("MULTIPOINT(4 4, 5 5, 6 6)", LW_PARSER_CHECK_NONE);
69  LWGEOM* for_extent = lwgeom_from_wkt("LINESTRING (-10 -10, 10 10)", LW_PARSER_CHECK_NONE);
70  const GBOX* clipping_extent = lwgeom_get_bbox(for_extent);
71 
72  LWGEOM* out = lwgeom_voronoi_diagram(in, clipping_extent, 0, 0);
73  const GBOX* output_extent = lwgeom_get_bbox(out);
74 
75  CU_ASSERT_TRUE(gbox_same(clipping_extent, output_extent));
76 
77  lwgeom_free(in);
78  lwgeom_free(for_extent);
79  lwgeom_free(out);
80 #endif /* POSTGIS_GEOS_VERSION >= 35 */
81 }
82 
83 #if POSTGIS_GEOS_VERSION >= 35
84 static void assert_empty_diagram(char* wkt, double tolerance)
85 {
87  LWGEOM* out = lwgeom_voronoi_diagram(in, NULL, tolerance, 0);
88 
89  CU_ASSERT_TRUE(lwgeom_is_collection(out));
90  CU_ASSERT_EQUAL(COLLECTIONTYPE, lwgeom_get_type(out));
91 
92  lwgeom_free(in);
93  lwgeom_free(out);
94 }
95 #endif /* POSTGIS_GEOS_VERSION >= 35 */
96 
98 {
99 #if POSTGIS_GEOS_VERSION >= 35
100  assert_empty_diagram("POLYGON EMPTY", 0);
101  assert_empty_diagram("POINT (1 2)", 0);
102 
103  /* This one produces an empty diagram because our two unqiue points
104  * collapse onto one after considering the tolerance. */
105  assert_empty_diagram("MULTIPOINT (0 0, 0 0.00001)", 0.001);
106 #endif /* POSTGIS_GEOS_VERSION >= 35 */
107 }
108 
109 /*
110 ** Used by test harness to register the tests in this file.
111 */
112 void triangulate_suite_setup(void);
114 {
115  CU_pSuite suite = CU_add_suite("triangulate", NULL, NULL);
120 }
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 test_lwgeom_delaunay_triangulation(void)
int gbox_same(const GBOX *g1, const GBOX *g2)
Check if 2 given Gbox are the same.
Definition: g_box.c:171
void cu_error_msg_reset()
#define PG_ADD_TEST(suite, testfunc)
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
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:1144
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2005
#define MULTILINETYPE
Definition: liblwgeom.h:89
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwgeom.c:923
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:556
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:244
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:1085
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:734
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904