capi Voronoi Diagram Builder + tests
[geos.git] / tests / unit / capi / GEOSVoronoiDiagramBuilderTest.cpp
blobb0694501d8f7cb10bbce12a7d00c7314ec21c0b0
1 //
2 // Test Suite for C-API GEOSVoronoiDiagramBuilder
4 #include <tut.hpp>
5 // geos
6 #include <geos_c.h>
7 // std
8 #include <cstdarg>
9 #include <cstdio>
10 #include <cstdlib>
11 #include <memory>
13 namespace tut
16 // Test Group
19 // Common data used in test cases.
20 struct test_capigeosvoronoidiagrambuilder_data
22 GEOSGeometry* geom1_;
23 GEOSGeometry* geom2_;
24 GEOSWKTWriter* w_;
26 static void notice(const char *fmt, ...)
28 std::fprintf( stdout, "NOTICE: ");
30 va_list ap;
31 va_start(ap, fmt);
32 std::vfprintf(stdout, fmt, ap);
33 va_end(ap);
35 std::fprintf(stdout, "\n");
38 test_capigeosvoronoidiagrambuilder_data()
39 : geom1_(0), geom2_(0)
41 initGEOS(notice, notice);
42 w_ = GEOSWKTWriter_create();
43 GEOSWKTWriter_setTrim(w_, 1);
46 void ensure_equals_wkt(GEOSGeometry* g, const char* exp)
48 GEOSNormalize(g);
49 char* wkt_c = GEOSWKTWriter_write(w_, g);
50 std::string out(wkt_c);
51 free(wkt_c);
53 GEOSGeometry* exp_g = GEOSGeomFromWKT(exp);
54 GEOSNormalize(exp_g);
55 char* wkt_c2 = GEOSWKTWriter_write(w_, exp_g);
56 std::string out_exp(wkt_c2);
57 free(wkt_c2);
59 // cout << "OUTPUT STRING::" << out << endl << endl;
60 // cout << "Expected STRING::" << out_exp << endl << endl;
61 ensure_equals(out, out_exp);
62 GEOSGeom_destroy(exp_g);
65 ~test_capigeosvoronoidiagrambuilder_data()
67 GEOSGeom_destroy(geom1_);
68 GEOSGeom_destroy(geom2_);
69 GEOSWKTWriter_destroy(w_);
70 geom1_ = 0;
71 geom2_ = 0;
72 finishGEOS();
77 typedef test_group<test_capigeosvoronoidiagrambuilder_data> group;
78 typedef group::object object;
80 group test_capigeosvoronoidiagrambuilder_group("capi::GEOSVoronoiDiagram");
83 // Test Cases
86 // Single point
87 template<>
88 template<>
89 void object::test<1>()
91 geom1_ = GEOSGeomFromWKT("POINT(10 20)");
93 geom2_ = GEOSVoronoiDiagramBuilder(geom1_,0);
94 ensure_equals ( GEOSisEmpty(geom2_), 1 );
95 ensure_equals ( GEOSGeomTypeId(geom2_), GEOS_GEOMETRYCOLLECTION );
98 //More points:
99 template<>
100 template<>
101 void object::test<2>()
103 geom1_ = GEOSGeomFromWKT("MULTIPOINT ((280 300), (420 330), (380 230), (320 160))");
105 geom2_ = GEOSVoronoiDiagramBuilder(geom1_,0);
106 ensure_equals_wkt(geom2_ ,"GEOMETRYCOLLECTION (POLYGON ((110 175.71428571428572, 110 500, 310.35714285714283 500, 353.515625 298.59375, 306.875 231.96428571428572, 110 175.71428571428572)), POLYGON ((590 204, 590 -10, 589.1666666666666 -10, 306.875 231.96428571428572, 353.515625 298.59375, 590 204)), POLYGON ((110 -10, 110 175.71428571428572, 306.875 231.96428571428572, 589.1666666666666 -10, 110 -10)), POLYGON ((310.35714285714283 500, 590 500, 590 204, 353.515625 298.59375, 310.35714285714283 500)))" );
108 //Larger number of points:
109 template<>
110 template<>
111 void object::test<3>()
113 geom1_ = GEOSGeomFromWKT("MULTIPOINT ((170 270), (270 270), (230 310), (180 330), (250 340), (315 318), (330 260), (240 170), (220 220), (270 220))");
115 geom2_ = GEOSVoronoiDiagramBuilder(geom1_,0);
116 ensure_equals_wkt(geom2_,"GEOMETRYCOLLECTION (POLYGON ((0 329.1666666666667, 0 510, 190 510, 213.94736842105263 342.36842105263156, 195.625 296.5625, 0 329.1666666666667)), POLYGON ((0 76.50000000000001, 0 329.1666666666667, 195.625 296.5625, 216 266, 88.33333333333333 138.33333333333334, 0 76.50000000000001)), POLYGON ((216 266, 195.625 296.5625, 213.94736842105263 342.36842105263156, 267 307, 225 265, 216 266)), POLYGON ((245 245, 225 265, 267 307, 275.9160583941606 309.54744525547443, 303.1666666666667 284, 296.6666666666667 245, 245 245)), POLYGON ((225 265, 245 245, 245 201, 88.33333333333333 138.33333333333334, 216 266, 225 265)), POLYGON ((0 0, 0 76.50000000000001, 88.33333333333333 138.33333333333334, 245 201, 380 120, 500 0, 0 0)), POLYGON ((190 510, 343.76153846153846 510, 275.9160583941606 309.54744525547443, 267 307, 213.94736842105263 342.36842105263156, 190 510)), POLYGON ((245 201, 245 245, 296.6666666666667 245, 380 120, 245 201)), POLYGON ((343.76153846153846 510, 500 510, 500 334.9051724137931, 303.1666666666667 284, 275.9160583941606 309.54744525547443, 343.76153846153846 510)), POLYGON ((500 334.9051724137931, 500 0, 380 120, 296.6666666666667 245, 303.1666666666667 284, 500 334.9051724137931)))");
118 //Test with non-zero Tolerance value
119 template<>
120 template<>
121 void object::test<4>()
123 geom1_ = GEOSGeomFromWKT("MULTIPOINT ((150 210), (210 270), (150 220), (220 210), (215 269))");
125 geom2_ = GEOSVoronoiDiagramBuilder(geom1_,10);
126 ensure_equals_wkt(geom2_,"GEOMETRYCOLLECTION (POLYGON ((290 252.5, 290 140, 185 140, 185 215, 187.9268292682927 235.4878048780488, 290 252.5)), POLYGON ((80 215, 80 340, 100.83333333333336 340, 187.9268292682927 235.4878048780488, 185 215, 80 215)), POLYGON ((80 140, 80 215, 185 215, 185 140, 80 140)), POLYGON ((100.83333333333336 340, 290 340, 290 252.5, 187.9268292682927 235.4878048780488, 100.83333333333336 340)))");
128 template<>
129 template<>
130 void object::test<5>()
132 geom1_ = GEOSGeomFromWKT("MULTIPOINT ((40 420), (50 420), (210 290), (300 360), (350 150), (170 70), (134 135) ,(305 359), (351 145), (175 71))");
134 geom2_ = GEOSVoronoiDiagramBuilder(geom1_,10);
135 ensure_equals_wkt(geom2_, "GEOMETRYCOLLECTION (POLYGON ((-310 146.559649122807, -310 770, 45 770, 45 263.64736842105265, -310 146.559649122807)), POLYGON ((-310 -153.37692307692305, -310 146.559649122807, 45 263.64736842105265, 59.16911764705881 267.8235294117647, 239.43506493506493 179.43506493506493, 241.34156378600824 151.98148148148147, -310 -153.37692307692305)), POLYGON ((45 770, 266.20000000000005 770, 181.94323144104806 418.9301310043668, 59.16911764705881 267.8235294117647, 45 263.64736842105265, 45 770)), POLYGON ((59.16911764705881 267.8235294117647, 181.94323144104806 418.9301310043668, 311.875 251.875, 239.43506493506493 179.43506493506493, 59.16911764705881 267.8235294117647)), POLYGON ((-310 -280, -310 -153.37692307692305, 241.34156378600824 151.98148148148147, 433.3333333333333 -280, -310 -280)), POLYGON ((266.20000000000005 770, 701 770, 701 344.5238095238096, 311.875 251.875, 181.94323144104806 418.9301310043668, 266.20000000000005 770)), POLYGON ((701 344.5238095238096, 701 -280, 433.3333333333333 -280, 241.34156378600824 151.98148148148147, 239.43506493506493 179.43506493506493, 311.875 251.875, 701 344.5238095238096)))");
137 } // namespace tut