/
GeometryManager.h
297 lines (229 loc) · 8.65 KB
/
GeometryManager.h
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Copyright 2014-2017 Rsyn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RSYN_GEOMETRY_MANAGER_H
#define RSYN_GEOMETRY_MANAGER_H
#ifdef __WXMAC__
#include <GLUT/glut.h>
#else
#include <GL/glew.h>
#include <GL/glut.h>
#endif
#include <tuple>
#include <map>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/index/rtree.hpp>
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
//BOOST_GEOMETRY_REGISTER_POINT_2D(DBUxy, std::int64_t, cs::cartesian, x, y)
#include "rsyn/util/Color.h"
#include "rsyn/util/dbu.h"
#include "rsyn/util/float2.h"
#include "rsyn/util/FloatRectangle.h"
#include "Stipple.h"
// -----------------------------------------------------------------------------
class GeometryManager {
public:
typedef int LayerId;
typedef std::tuple<LayerId, int> ObjectId; // <layer id, object id>
typedef int GroupId;
typedef bg::model::point<float, 2, bg::cs::cartesian> Point;
typedef bg::model::box<Point> Box;
typedef bg::model::d2::point_xy<float> PolygonPoint;
typedef bg::model::polygon<PolygonPoint> Polygon;
static const GroupId INVALID_GROUP_ID;
static const ObjectId INVALID_OBJECT_ID;
enum ObjectType {
OBJECT_TYPE_INVALID = -1,
RECTANGLE,
POLYGON,
PATH
}; // end enum
enum BoxOrientation {
BOX_ORIENTATION_INVALID,
BOX_ORIENTATION_SW,
BOX_ORIENTATION_SE,
BOX_ORIENTATION_NE,
BOX_ORIENTATION_NW
};
GeometryManager();
void reset();
void removeAllObjects();
LayerId createLayer(
const std::string &name,
const int zIndex = 0,
const Color lineColor = Color(0, 0, 0),
const Color fillColor = Color(0, 0, 0),
const LineStippleMask linePattern = LINE_STIPPLE_SOLID,
const FillStippleMask fillPattern = STIPPLE_MASK_EMPTY);
GroupId createGroup();
void addObjectToGroup(const ObjectId objectId, const GroupId groupId);
ObjectId addRectangle(const LayerId layerId, const Box &box, void * data = nullptr, const BoxOrientation orientation = BOX_ORIENTATION_INVALID, const GroupId groupId = INVALID_GROUP_ID);
ObjectId addPolygon(const LayerId layerId, const std::vector<DBUxy> &points, const float2 displacement, void * data = nullptr, const GroupId groupId = INVALID_GROUP_ID);
ObjectId addPath(const LayerId layerId, const std::vector<DBUxy> &points, const float thickness, void * data = nullptr, const GroupId groupId = INVALID_GROUP_ID);
ObjectId searchObjectAt(const float x, const float y) const;
void addObjectToHighlight(const ObjectId objectId);
void addGroupOfObjectToHighlight(const ObjectId objectId);
void addGroupToHighlight(const GroupId groupId);
void clearHighlight() { highlightedObjects.clear(); }
void setLayerVisibility(const LayerId layerId, const bool visible) {
layers[layerId].visible = visible;
}
void setLayerVisibility(const std::string &layerName, const bool visible) {
auto it = mapLayerNameToLayerId.find(layerName);
if (it != mapLayerNameToLayerId.end()) {
layers[it->second].visible = visible;
} // end if
} // end method
void setObjectFillColor(const ObjectId &objectId, const Color &color) {
Object &object = getObject(objectId);
object.hasFillColor = true;
object.fillColor = color;
} // end method
void clearObjectFillColor(const ObjectId &objectId) {
Object &object = getObject(objectId);
object.hasFillColor = false;
} // end method
void setObjectLineColor(const ObjectId &objectId, const Color &color) {
Object &object = getObject(objectId);
object.hasLineColor = true;
object.lineColor = color;
} // end method
void clearObjectLineColor(const ObjectId &objectId) {
Object &object = getObject(objectId);
object.hasLineColor = false;
} // end method
void * getObjectData(const ObjectId &objectId) const {
return isObjectIdValid(objectId)?
layers[std::get<0>(objectId)].objects[std::get<1>(objectId)].data :
nullptr;
} // end method
static bool isObjectIdValid(const ObjectId &objectId) {
return objectId != INVALID_OBJECT_ID;
} // end method
static bool isGroupIdValid(const GroupId &groupId) {
return groupId != INVALID_GROUP_ID;
} // end method
void render() const;
void renderFocusedObject(const ObjectId &objectId) const;
void renderHighlightedObjects() const;
private:
typedef std::pair<Box, ObjectId> RTreeEntry;
struct Object {
ObjectType type = OBJECT_TYPE_INVALID;
GroupId groupId = INVALID_GROUP_ID;
Box box;
Polygon polygon;
float area = 0;
bool hasFillColor : 1;
bool hasLineColor : 1;
BoxOrientation orientation : 3;
Color fillColor;
Color lineColor;
// TODO: memory optimization... Only store this when the object is a
// polygon.
std::vector<std::tuple<GLenum, int>> tessellationType;
std::vector<float2> tessellationPoints;
std::vector<DBUxy> pathPoints;
// Client data.
void * data = nullptr;
Object() :
hasFillColor(false),
hasLineColor(false),
orientation(BOX_ORIENTATION_INVALID) {}
}; // end struct
struct Layer {
std::string name;
FillStippleMask fillPattern = STIPPLE_MASK_EMPTY;
LineStippleMask linePattern = LINE_STIPPLE_SOLID;
Color fillColor;
Color lineColor;
int zIndex = 0;
bool visible = true;
float z = 0;
std::deque<Object> objects;
bgi::rtree<RTreeEntry, bgi::quadratic<16>> rtree;
}; // end struct
std::vector<Layer> layers;
std::vector<std::set<ObjectId>> groups;
std::map<std::string, LayerId> mapLayerNameToLayerId;
std::set<ObjectId> highlightedObjects;
GLUtriangulatorObj *tobj = nullptr;
mutable ObjectId previousFocusedObjectId = INVALID_OBJECT_ID;
mutable Color defaultColor;
Object &getObject(const ObjectId &objectId) {
return layers[std::get<0>(objectId)].objects[std::get<1>(objectId)];
} // end method
const Object &getObject(const ObjectId &objectId) const {
return layers[std::get<0>(objectId)].objects[std::get<1>(objectId)];
} // end method
Layer &getLayer(const LayerId &layerId) {
return layers[layerId];
} // end method
const Layer &getLayer(const LayerId &layerId) const {
return layers[layerId];
} // end method
void renderFocusedObject_Core(const ObjectId &objectId) const;
public:
LineStippleMask getLayerLinePattern (const LayerId &layerId) const {
return layers[layerId].linePattern;
} // end method
Color getLayerLineColor (const LayerId &layerId) const {
return layers[layerId].lineColor;
} // end method
FillStippleMask getLayerFillPattern (const LayerId &layerId) const {
return layers[layerId].fillPattern;
} // end method
Color getLayerFillColor (const LayerId &layerId) const {
return layers[layerId].fillColor;
} // end method
float getLayerZ(const LayerId &layerId) const {
return layers[layerId].z;
}
private:
void resortLayers();
void applyColor(const Color &color) const {
glColor3ub(color.r, color.g, color.b);
} // end method
void applyDefaultColor(const Color &color) const {
defaultColor = color;
glColor3ub(color.r, color.g, color.b);
} // end method
void restoreDefaultColor() const {
glColor3ub(defaultColor.r, defaultColor.g, defaultColor.b);
} // end method
void renderRectangleOutline(const Layer &layer, const Object &object) const;
void renderPolygonOutline(const Layer &layer, const Object &object) const;
void renderPathOutline(const Layer &layer, const Object &object) const;
void renderRectangleFill(const Layer &layer, const Object &object) const;
void renderPolygonFill(const Layer &layer, const Object &object) const;
void renderPathFill(const Layer &layer, const Object &object) const;
void tracePathOutline(
const std::vector<DBUxy> &pathPoints,
const float halfThickness,
std::vector<float2> &outlinePoints) const;
bool findMyPoint(float2 v0, float2 v1, float2 v2, const float thickness, std::vector<float2> &outlinePoints) const;
float2 findIntersection(float2 p1, float2 p2, float2 d1, float2 d2) const;
void initTessellator();
void tessellate(Object &object);
static void tessellate_beginCallback(GLenum type, void * data);
static void tessellate_endCallback(void * data);
static void tessellate_vertexCallback(GLdouble * vertex, void * data);
static void tessellate_errorCallback(GLenum errorCode);
}; // end method
#endif /* GEOMETRYMANAGER_H */