-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathTriangulation.cpp
More file actions
172 lines (151 loc) · 4.8 KB
/
Triangulation.cpp
File metadata and controls
172 lines (151 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <cstdlib>
#include <string>
#include <chrono>
#include <cstdint>
#include "clipper2/clipper.h"
#include "../../Utils/clipper.svg.utils.h"
#include "clipper2/clipper.triangulation.h"
using namespace Clipper2Lib;
using namespace std;
void System(const std::string& filename)
{
#ifdef _WIN32
system(filename.c_str());
#else
system(("firefox " + filename).c_str());
#endif
}
inline PathD MakeRandomPoly(unsigned vertCnt, int width, int height)
{
PathD result;
result.reserve(vertCnt);
for (unsigned i = 0; i < vertCnt; ++i)
result.push_back(PointD(rand() % width, rand() % height));
return result;
}
inline PathsD MakeRandomPolys(unsigned polyCount, unsigned vertCnt, int width, int height)
{
PathsD result;
result.reserve(polyCount);
result.push_back(MakeRandomPoly(vertCnt, width, height));
return result;
}
static string TriangulateResultAsString(TriangulateResult tr)
{
switch (tr)
{
case TriangulateResult::fail: return "Fail!";
case TriangulateResult::no_polygons: return "Paths are not polygons!";
case TriangulateResult::paths_intersect: return "Paths intersect!";
default: return "Success!";
}
}
void DisplaySolution(const string filename, const PathsD& sol, bool multicolor)
{
SvgWriter svg;
//SvgAddSubject(svg, sub, FillRule::NonZero);
if (multicolor)
SvgAddRCSolution(svg, sol, FillRule::NonZero, false);
else
SvgAddSolution(svg, sol, FillRule::NonZero, false);
SvgSaveToFile(svg, filename, 400, 400, 0);
System(filename);
}
///////////////////////////////////////////////////////////////////////////////
// main
///////////////////////////////////////////////////////////////////////////////
enum class Test { single_test, range_test, random, sample_1, sample_2, sample_3};
int main()
{
//////////////////////////////////////////////////////////////////////
Test t = Test::sample_1; //Test::random; //Test::single_test; //Test::range_test; //Test::clipper; //
int minRange = 44, maxRange = 52;
bool doDelaunay = true;
bool multicolor = true;
//////////////////////////////////////////////////////////////////////
TriangulateResult tr;
PathsD subject, sol;
string folder = ".\\TriSamples\\";
srand((unsigned)time(0));
switch (t)
{
case Test::single_test:
{
string filename = "Test" + to_string(maxRange) + ".svg";
if (!FileExists(folder + filename)) break;
SvgReader sr = SvgReader(folder + filename);
PathsD subject = sr.paths, sol;
Triangulate(subject, 0, sol, doDelaunay);
DisplaySolution(filename, sol, multicolor);
break;
}
case Test::range_test:
{
for (int i = minRange; i <= maxRange; ++i)
{
string filename = "Test" + to_string(i) + ".svg";
if (!FileExists(folder + filename)) break;
SvgReader sr = SvgReader(folder + filename);
PathsD subject = sr.paths, sol;
tr = Triangulate(subject, 0, sol, doDelaunay);
switch (tr)
{
case TriangulateResult::success:
case TriangulateResult::fail:
{
std::cout << i << " " << TriangulateResultAsString(tr) << endl;
DisplaySolution(filename, sol, multicolor);
break; // break inner case
}
default: break;
}
if (tr != TriangulateResult::success) break; // break for() loop
}
break; // break case Test::range_test
}
case Test::random:
{
subject = MakeRandomPolys(1, 25, 800, 600);
// The Triangulate function below only accepts **non-intersecting** paths, so ...
subject = Union(subject, FillRule::NonZero);
//std::cout << subject << endl;
tr = Triangulate(subject, 0, sol, doDelaunay);
DisplaySolution("random.svg", sol, multicolor);
break;
}
case Test::sample_1:
{
SvgReader sr = SvgReader(folder + "coral3.svg");
PathsD subject = sr.paths;
Triangulate(subject, 0, sol, true);
DisplaySolution("coral3_t.svg", sol, multicolor);
break;
}
case Test::sample_2:
{
SvgReader sr = SvgReader("rabbit.svg");
PathsD subject = sr.paths;
Triangulate(subject, 0, sol, true);
DisplaySolution("rabbit_t.svg", sol, multicolor);
break;
}
case Test::sample_3:
{
PathsD subject = { MakePathD({
44.1,114.0, 11.9,114.0, 13.9,72.9, 0.0,59.9, 0.0,53.2, 12.2,41.0, 16.8,41.0, 10.1,37.2, 8.0,19.9,
11.3,6.7, 20.0,1.9, 20.0,0.0, 34.6,0.0, 44.9,6.8, 48.0,29.1, 45.7,37.3, 39.2,41.0, 43.8,41.0,
56.0,53.2, 56.0,59.9, 42.0,72.9
}) };
Triangulate(subject, 0, sol, true);
DisplaySolution("sample3.svg", sol, multicolor);
break;
}
}
//#ifdef _DEBUG
PathD* p = new PathD();
std::cout << "Press Enter to continue" << std::endl;
std::string s;
std::getline(std::cin, s);
//#endif
return 0;
}