-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
edge-selectors.cpp
261 lines (230 loc) · 10.2 KB
/
edge-selectors.cpp
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
#include "edge-selectors.h"
#include "arithmetics.hpp"
namespace msdfgen {
#define DISTANCE_DELTA_FACTOR 1.001
TrueDistanceSelector::EdgeCache::EdgeCache() : absDistance(0) { }
void TrueDistanceSelector::reset(const Point2 &p) {
double delta = DISTANCE_DELTA_FACTOR*(p-this->p).length();
minDistance.distance += nonZeroSign(minDistance.distance)*delta;
this->p = p;
}
void TrueDistanceSelector::addEdge(EdgeCache &cache, const EdgeSegment *prevEdge, const EdgeSegment *edge, const EdgeSegment *nextEdge) {
double delta = DISTANCE_DELTA_FACTOR*(p-cache.point).length();
if (cache.absDistance-delta <= fabs(minDistance.distance)) {
double dummy;
SignedDistance distance = edge->signedDistance(p, dummy);
if (distance < minDistance)
minDistance = distance;
cache.point = p;
cache.absDistance = fabs(distance.distance);
}
}
void TrueDistanceSelector::merge(const TrueDistanceSelector &other) {
if (other.minDistance < minDistance)
minDistance = other.minDistance;
}
TrueDistanceSelector::DistanceType TrueDistanceSelector::distance() const {
return minDistance.distance;
}
PerpendicularDistanceSelectorBase::EdgeCache::EdgeCache() : absDistance(0), aDomainDistance(0), bDomainDistance(0), aPerpendicularDistance(0), bPerpendicularDistance(0) { }
bool PerpendicularDistanceSelectorBase::getPerpendicularDistance(double &distance, const Vector2 &ep, const Vector2 &edgeDir) {
double ts = dotProduct(ep, edgeDir);
if (ts > 0) {
double perpendicularDistance = crossProduct(ep, edgeDir);
if (fabs(perpendicularDistance) < fabs(distance)) {
distance = perpendicularDistance;
return true;
}
}
return false;
}
PerpendicularDistanceSelectorBase::PerpendicularDistanceSelectorBase() : minNegativePerpendicularDistance(-fabs(minTrueDistance.distance)), minPositivePerpendicularDistance(fabs(minTrueDistance.distance)), nearEdge(NULL), nearEdgeParam(0) { }
void PerpendicularDistanceSelectorBase::reset(double delta) {
minTrueDistance.distance += nonZeroSign(minTrueDistance.distance)*delta;
minNegativePerpendicularDistance = -fabs(minTrueDistance.distance);
minPositivePerpendicularDistance = fabs(minTrueDistance.distance);
nearEdge = NULL;
nearEdgeParam = 0;
}
bool PerpendicularDistanceSelectorBase::isEdgeRelevant(const EdgeCache &cache, const EdgeSegment *edge, const Point2 &p) const {
double delta = DISTANCE_DELTA_FACTOR*(p-cache.point).length();
return (
cache.absDistance-delta <= fabs(minTrueDistance.distance) ||
fabs(cache.aDomainDistance) < delta ||
fabs(cache.bDomainDistance) < delta ||
(cache.aDomainDistance > 0 && (cache.aPerpendicularDistance < 0 ?
cache.aPerpendicularDistance+delta >= minNegativePerpendicularDistance :
cache.aPerpendicularDistance-delta <= minPositivePerpendicularDistance
)) ||
(cache.bDomainDistance > 0 && (cache.bPerpendicularDistance < 0 ?
cache.bPerpendicularDistance+delta >= minNegativePerpendicularDistance :
cache.bPerpendicularDistance-delta <= minPositivePerpendicularDistance
))
);
}
void PerpendicularDistanceSelectorBase::addEdgeTrueDistance(const EdgeSegment *edge, const SignedDistance &distance, double param) {
if (distance < minTrueDistance) {
minTrueDistance = distance;
nearEdge = edge;
nearEdgeParam = param;
}
}
void PerpendicularDistanceSelectorBase::addEdgePerpendicularDistance(double distance) {
if (distance <= 0 && distance > minNegativePerpendicularDistance)
minNegativePerpendicularDistance = distance;
if (distance >= 0 && distance < minPositivePerpendicularDistance)
minPositivePerpendicularDistance = distance;
}
void PerpendicularDistanceSelectorBase::merge(const PerpendicularDistanceSelectorBase &other) {
if (other.minTrueDistance < minTrueDistance) {
minTrueDistance = other.minTrueDistance;
nearEdge = other.nearEdge;
nearEdgeParam = other.nearEdgeParam;
}
if (other.minNegativePerpendicularDistance > minNegativePerpendicularDistance)
minNegativePerpendicularDistance = other.minNegativePerpendicularDistance;
if (other.minPositivePerpendicularDistance < minPositivePerpendicularDistance)
minPositivePerpendicularDistance = other.minPositivePerpendicularDistance;
}
double PerpendicularDistanceSelectorBase::computeDistance(const Point2 &p) const {
double minDistance = minTrueDistance.distance < 0 ? minNegativePerpendicularDistance : minPositivePerpendicularDistance;
if (nearEdge) {
SignedDistance distance = minTrueDistance;
nearEdge->distanceToPerpendicularDistance(distance, p, nearEdgeParam);
if (fabs(distance.distance) < fabs(minDistance))
minDistance = distance.distance;
}
return minDistance;
}
SignedDistance PerpendicularDistanceSelectorBase::trueDistance() const {
return minTrueDistance;
}
void PerpendicularDistanceSelector::reset(const Point2 &p) {
double delta = DISTANCE_DELTA_FACTOR*(p-this->p).length();
PerpendicularDistanceSelectorBase::reset(delta);
this->p = p;
}
void PerpendicularDistanceSelector::addEdge(EdgeCache &cache, const EdgeSegment *prevEdge, const EdgeSegment *edge, const EdgeSegment *nextEdge) {
if (isEdgeRelevant(cache, edge, p)) {
double param;
SignedDistance distance = edge->signedDistance(p, param);
addEdgeTrueDistance(edge, distance, param);
cache.point = p;
cache.absDistance = fabs(distance.distance);
Vector2 ap = p-edge->point(0);
Vector2 bp = p-edge->point(1);
Vector2 aDir = edge->direction(0).normalize(true);
Vector2 bDir = edge->direction(1).normalize(true);
Vector2 prevDir = prevEdge->direction(1).normalize(true);
Vector2 nextDir = nextEdge->direction(0).normalize(true);
double add = dotProduct(ap, (prevDir+aDir).normalize(true));
double bdd = -dotProduct(bp, (bDir+nextDir).normalize(true));
if (add > 0) {
double pd = distance.distance;
if (getPerpendicularDistance(pd, ap, -aDir))
addEdgePerpendicularDistance(pd = -pd);
cache.aPerpendicularDistance = pd;
}
if (bdd > 0) {
double pd = distance.distance;
if (getPerpendicularDistance(pd, bp, bDir))
addEdgePerpendicularDistance(pd);
cache.bPerpendicularDistance = pd;
}
cache.aDomainDistance = add;
cache.bDomainDistance = bdd;
}
}
PerpendicularDistanceSelector::DistanceType PerpendicularDistanceSelector::distance() const {
return computeDistance(p);
}
void MultiDistanceSelector::reset(const Point2 &p) {
double delta = DISTANCE_DELTA_FACTOR*(p-this->p).length();
r.reset(delta);
g.reset(delta);
b.reset(delta);
this->p = p;
}
void MultiDistanceSelector::addEdge(EdgeCache &cache, const EdgeSegment *prevEdge, const EdgeSegment *edge, const EdgeSegment *nextEdge) {
if (
(edge->color&RED && r.isEdgeRelevant(cache, edge, p)) ||
(edge->color&GREEN && g.isEdgeRelevant(cache, edge, p)) ||
(edge->color&BLUE && b.isEdgeRelevant(cache, edge, p))
) {
double param;
SignedDistance distance = edge->signedDistance(p, param);
if (edge->color&RED)
r.addEdgeTrueDistance(edge, distance, param);
if (edge->color&GREEN)
g.addEdgeTrueDistance(edge, distance, param);
if (edge->color&BLUE)
b.addEdgeTrueDistance(edge, distance, param);
cache.point = p;
cache.absDistance = fabs(distance.distance);
Vector2 ap = p-edge->point(0);
Vector2 bp = p-edge->point(1);
Vector2 aDir = edge->direction(0).normalize(true);
Vector2 bDir = edge->direction(1).normalize(true);
Vector2 prevDir = prevEdge->direction(1).normalize(true);
Vector2 nextDir = nextEdge->direction(0).normalize(true);
double add = dotProduct(ap, (prevDir+aDir).normalize(true));
double bdd = -dotProduct(bp, (bDir+nextDir).normalize(true));
if (add > 0) {
double pd = distance.distance;
if (PerpendicularDistanceSelectorBase::getPerpendicularDistance(pd, ap, -aDir)) {
pd = -pd;
if (edge->color&RED)
r.addEdgePerpendicularDistance(pd);
if (edge->color&GREEN)
g.addEdgePerpendicularDistance(pd);
if (edge->color&BLUE)
b.addEdgePerpendicularDistance(pd);
}
cache.aPerpendicularDistance = pd;
}
if (bdd > 0) {
double pd = distance.distance;
if (PerpendicularDistanceSelectorBase::getPerpendicularDistance(pd, bp, bDir)) {
if (edge->color&RED)
r.addEdgePerpendicularDistance(pd);
if (edge->color&GREEN)
g.addEdgePerpendicularDistance(pd);
if (edge->color&BLUE)
b.addEdgePerpendicularDistance(pd);
}
cache.bPerpendicularDistance = pd;
}
cache.aDomainDistance = add;
cache.bDomainDistance = bdd;
}
}
void MultiDistanceSelector::merge(const MultiDistanceSelector &other) {
r.merge(other.r);
g.merge(other.g);
b.merge(other.b);
}
MultiDistanceSelector::DistanceType MultiDistanceSelector::distance() const {
MultiDistance multiDistance;
multiDistance.r = r.computeDistance(p);
multiDistance.g = g.computeDistance(p);
multiDistance.b = b.computeDistance(p);
return multiDistance;
}
SignedDistance MultiDistanceSelector::trueDistance() const {
SignedDistance distance = r.trueDistance();
if (g.trueDistance() < distance)
distance = g.trueDistance();
if (b.trueDistance() < distance)
distance = b.trueDistance();
return distance;
}
MultiAndTrueDistanceSelector::DistanceType MultiAndTrueDistanceSelector::distance() const {
MultiDistance multiDistance = MultiDistanceSelector::distance();
MultiAndTrueDistance mtd;
mtd.r = multiDistance.r;
mtd.g = multiDistance.g;
mtd.b = multiDistance.b;
mtd.a = trueDistance().distance;
return mtd;
}
}