1 | /* |
2 | * Authors: kaen, raptor, sam686, watusimoto |
3 | * |
4 | * Originally from the bitfighter source code |
5 | * |
6 | * The MIT License (MIT) |
7 | * |
8 | * Copyright (c) 2014 Bitfighter developers |
9 | * |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
11 | * of this software and associated documentation files (the "Software"), to deal |
12 | * in the Software without restriction, including without limitation the rights |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
14 | * copies of the Software, and to permit persons to whom the Software is |
15 | * furnished to do so, subject to the following conditions: |
16 | * |
17 | * The above copyright notice and this permission notice shall be included in all |
18 | * copies or substantial portions of the Software. |
19 | * |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
26 | * SOFTWARE. |
27 | */ |
28 | |
29 | #ifndef CLIP2TRI_H_ |
30 | #define CLIP2TRI_H_ |
31 | |
32 | #include <vector> |
33 | #include <clipper.h> |
34 | |
35 | using namespace std; |
36 | using namespace QtClipperLib; |
37 | |
38 | namespace c2t |
39 | { |
40 | |
41 | typedef signed int S32; |
42 | typedef signed long long S64; |
43 | typedef unsigned int U32; |
44 | typedef float F32; |
45 | typedef double F64; |
46 | |
47 | |
48 | struct Point |
49 | { |
50 | F32 x; |
51 | F32 y; |
52 | |
53 | Point(); |
54 | Point(const Point &pt); |
55 | |
56 | template<class T, class U> |
57 | Point(T in_x, U in_y) { x = static_cast<F32>(in_x); y = static_cast<F32>(in_y); } |
58 | }; |
59 | |
60 | class clip2tri |
61 | { |
62 | private: |
63 | // |
64 | Path upscaleClipperPoints(const vector<Point> &inputPolygon); |
65 | |
66 | // These operate on a vector of polygons |
67 | Paths upscaleClipperPoints(const vector<vector<Point> > &inputPolygons); |
68 | vector<vector<Point> > downscaleClipperPoints(const Paths &inputPolygons); |
69 | |
70 | bool mergePolysToPolyTree(const vector<vector<Point> > &inputPolygons, PolyTree &solution); |
71 | |
72 | bool triangulateComplex(vector<Point> &outputTriangles, const Path &outline, |
73 | const PolyTree &polyTree, bool ignoreFills = true, bool ignoreHoles = false); |
74 | |
75 | public: |
76 | enum Operation { Union, Intersection, Difference, Xor }; |
77 | clip2tri(); |
78 | virtual ~clip2tri(); |
79 | |
80 | void triangulate(const vector<vector<Point> > &inputPolygons, vector<Point> &outputTriangles, |
81 | const vector<Point> &boundingPolygon); |
82 | |
83 | // Clip polygons are intended as closed, even if the first and last vertex aren't the same. |
84 | void addClipPolygon(const Path &path); |
85 | // Closed means the path has to be effectively closed. Meaning path[0] == path[path.size()-1] |
86 | void addSubjectPath(const Path &path, bool closed); |
87 | |
88 | void clearClipper(); |
89 | |
90 | Paths execute(const Operation op, |
91 | const PolyFillType subjFillType = pftNonZero, |
92 | const PolyFillType clipFillType = pftNonZero); |
93 | |
94 | static int pointInPolygon(const IntPoint &pt, const Path &path); |
95 | |
96 | Clipper clipper; |
97 | bool openSubject; |
98 | }; |
99 | |
100 | } /* namespace c2t */ |
101 | |
102 | #endif /* CLIP2TRI_H_ */ |
103 | |