PostGIS  2.4.9dev-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 #if POSTGIS_GEOS_VERSION >= 34
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  {
41  fprintf(stderr, "\nExp: %s\nObt: %s\n", exp_wkt, wkt);
42  }
43  CU_ASSERT_STRING_EQUAL(wkt, exp_wkt);
44  lwfree(wkt);
45 
46 #endif /* POSTGIS_GEOS_VERSION >= 34 */
47 }
48 
49 static void test_lwgeom_voronoi_diagram(void)
50 {
51 #if POSTGIS_GEOS_VERSION >= 35
52  LWGEOM* in = lwgeom_from_wkt("MULTIPOINT(4 4, 5 5, 6 6)", LW_PARSER_CHECK_NONE);
53 
54  LWGEOM* out_boundaries = lwgeom_voronoi_diagram(in, NULL, 0, 0);
55  LWGEOM* out_lines = lwgeom_voronoi_diagram(in, NULL, 0, 1);
56 
57  /* For boundaries we get a generic LWCOLLECTION */
58  CU_ASSERT_EQUAL(COLLECTIONTYPE, lwgeom_get_type(out_boundaries));
59  /* For lines we get a MULTILINETYPE */
60  CU_ASSERT_EQUAL(MULTILINETYPE, lwgeom_get_type(out_lines));
61 
62  lwgeom_free(in);
63  lwgeom_free(out_boundaries);
64  lwgeom_free(out_lines);
65 #endif /* POSTGIS_GEOS_VERSION >= 35 */
66 }
67 
69 {
70 #if POSTGIS_GEOS_VERSION >= 35
71  LWGEOM* in = lwgeom_from_wkt("MULTIPOINT(4 4, 5 5, 6 6)", LW_PARSER_CHECK_NONE);
72  LWGEOM* for_extent = lwgeom_from_wkt("LINESTRING (-10 -10, 10 10)", LW_PARSER_CHECK_NONE);
73  const GBOX* clipping_extent = lwgeom_get_bbox(for_extent);
74 
75  LWGEOM* out = lwgeom_voronoi_diagram(in, clipping_extent, 0, 0);
76  const GBOX* output_extent = lwgeom_get_bbox(out);
77 
78  CU_ASSERT_TRUE(gbox_same(clipping_extent, output_extent));
79 
80  lwgeom_free(in);
81  lwgeom_free(for_extent);
82  lwgeom_free(out);
83 #endif /* POSTGIS_GEOS_VERSION >= 35 */
84 }
85 
86 static void assert_empty_diagram(char* wkt, double tolerance)
87 {
89  LWGEOM* out = lwgeom_voronoi_diagram(in, NULL, 0, 0);
90 
91  CU_ASSERT_TRUE(lwgeom_is_collection(out));
92  CU_ASSERT_EQUAL(COLLECTIONTYPE, lwgeom_get_type(out));
93 
94  lwgeom_free(in);
95  lwgeom_free(out);
96 }
97 
99 {
100 #if POSTGIS_GEOS_VERSION >= 35
101  assert_empty_diagram("POLYGON EMPTY", 0);
102  assert_empty_diagram("POINT (1 2)", 0);
103 
104  /* This one produces an empty diagram because our two unqiue points
105  * collapse onto one after considering the tolerance. */
106  assert_empty_diagram("MULTIPOINT (0 0, 0 0.00001)", 0.001);
107 #endif /* POSTGIS_GEOS_VERSION >= 35 */
108 }
109 
110 /*
111 ** Used by test harness to register the tests in this file.
112 */
113 void triangulate_suite_setup(void);
115 {
116  CU_pSuite suite = CU_add_suite("triangulate", NULL, NULL);
121 }
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1040
void lwfree(void *mem)
Definition: lwutil.c:244
static void assert_empty_diagram(char *wkt, double tolerance)
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwgeom.c:878
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:518
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
static void test_lwgeom_voronoi_diagram(void)
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
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.
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2013
void cu_error_msg_reset()
LWGEOM * lwgeom_delaunay_triangulation(const LWGEOM *geom, double tolerance, int edgeOnly)
Take vertices of a geometry and build a delaunay triangulation on them.
#define PG_ADD_TEST(suite, testfunc)
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:689
LWGEOM * lwgeom_normalize(const LWGEOM *geom)
static void test_lwgeom_delaunay_triangulation(void)
static void test_lwgeom_voronoi_diagram_custom_envelope(void)
#define MULTILINETYPE
Definition: liblwgeom.h:89
static void test_lwgeom_voronoi_diagram_expected_empty(void)
int gbox_same(const GBOX *g1, const GBOX *g2)
Check if 2 given Gbox are the same.
Definition: g_box.c:176
void triangulate_suite_setup(void)
#define COLLECTIONTYPE
Definition: liblwgeom.h:91