blob: d0c5b46d74b18a7e437ed03bd3ca5330204e365f (
plain)
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
|
#include "contour-combiners.h"
#include "arithmetics.hpp"
namespace msdfgen {
static void initDistance(double &distance) {
distance = SignedDistance::INFINITE.distance;
}
static void initDistance(MultiDistance &distance) {
distance.r = SignedDistance::INFINITE.distance;
distance.g = SignedDistance::INFINITE.distance;
distance.b = SignedDistance::INFINITE.distance;
}
static double resolveDistance(double distance) {
return distance;
}
static double resolveDistance(const MultiDistance &distance) {
return median(distance.r, distance.g, distance.b);
}
template <class EdgeSelector>
SimpleContourCombiner<EdgeSelector>::SimpleContourCombiner(const Shape &shape) { }
template <class EdgeSelector>
void SimpleContourCombiner<EdgeSelector>::reset(const Point2 &p) {
shapeEdgeSelector.reset(p);
}
template <class EdgeSelector>
EdgeSelector & SimpleContourCombiner<EdgeSelector>::edgeSelector(int) {
return shapeEdgeSelector;
}
template <class EdgeSelector>
typename SimpleContourCombiner<EdgeSelector>::DistanceType SimpleContourCombiner<EdgeSelector>::distance() const {
return shapeEdgeSelector.distance();
}
template class SimpleContourCombiner<TrueDistanceSelector>;
template class SimpleContourCombiner<PseudoDistanceSelector>;
template class SimpleContourCombiner<MultiDistanceSelector>;
template class SimpleContourCombiner<MultiAndTrueDistanceSelector>;
template <class EdgeSelector>
OverlappingContourCombiner<EdgeSelector>::OverlappingContourCombiner(const Shape &shape) {
windings.reserve(shape.contours.size());
for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
windings.push_back(contour->winding());
edgeSelectors.resize(shape.contours.size());
}
template <class EdgeSelector>
void OverlappingContourCombiner<EdgeSelector>::reset(const Point2 &p) {
this->p = p;
for (typename std::vector<EdgeSelector>::iterator contourEdgeSelector = edgeSelectors.begin(); contourEdgeSelector != edgeSelectors.end(); ++contourEdgeSelector)
contourEdgeSelector->reset(p);
}
template <class EdgeSelector>
EdgeSelector & OverlappingContourCombiner<EdgeSelector>::edgeSelector(int i) {
return edgeSelectors[i];
}
template <class EdgeSelector>
typename OverlappingContourCombiner<EdgeSelector>::DistanceType OverlappingContourCombiner<EdgeSelector>::distance() const {
int contourCount = (int) edgeSelectors.size();
EdgeSelector shapeEdgeSelector;
EdgeSelector innerEdgeSelector;
EdgeSelector outerEdgeSelector;
shapeEdgeSelector.reset(p);
innerEdgeSelector.reset(p);
outerEdgeSelector.reset(p);
for (int i = 0; i < contourCount; ++i) {
DistanceType edgeDistance = edgeSelectors[i].distance();
shapeEdgeSelector.merge(edgeSelectors[i]);
if (windings[i] > 0 && resolveDistance(edgeDistance) >= 0)
innerEdgeSelector.merge(edgeSelectors[i]);
if (windings[i] < 0 && resolveDistance(edgeDistance) <= 0)
outerEdgeSelector.merge(edgeSelectors[i]);
}
DistanceType shapeDistance = shapeEdgeSelector.distance();
DistanceType innerDistance = innerEdgeSelector.distance();
DistanceType outerDistance = outerEdgeSelector.distance();
double innerScalarDistance = resolveDistance(innerDistance);
double outerScalarDistance = resolveDistance(outerDistance);
DistanceType distance;
initDistance(distance);
int winding = 0;
if (innerScalarDistance >= 0 && fabs(innerScalarDistance) <= fabs(outerScalarDistance)) {
distance = innerDistance;
winding = 1;
for (int i = 0; i < contourCount; ++i)
if (windings[i] > 0) {
DistanceType contourDistance = edgeSelectors[i].distance();
if (fabs(resolveDistance(contourDistance)) < fabs(outerScalarDistance) && resolveDistance(contourDistance) > resolveDistance(distance))
distance = contourDistance;
}
} else if (outerScalarDistance <= 0 && fabs(outerScalarDistance) < fabs(innerScalarDistance)) {
distance = outerDistance;
winding = -1;
for (int i = 0; i < contourCount; ++i)
if (windings[i] < 0) {
DistanceType contourDistance = edgeSelectors[i].distance();
if (fabs(resolveDistance(contourDistance)) < fabs(innerScalarDistance) && resolveDistance(contourDistance) < resolveDistance(distance))
distance = contourDistance;
}
} else
return shapeDistance;
for (int i = 0; i < contourCount; ++i)
if (windings[i] != winding) {
DistanceType contourDistance = edgeSelectors[i].distance();
if (resolveDistance(contourDistance)*resolveDistance(distance) >= 0 && fabs(resolveDistance(contourDistance)) < fabs(resolveDistance(distance)))
distance = contourDistance;
}
if (resolveDistance(distance) == resolveDistance(shapeDistance))
distance = shapeDistance;
return distance;
}
template class OverlappingContourCombiner<TrueDistanceSelector>;
template class OverlappingContourCombiner<PseudoDistanceSelector>;
template class OverlappingContourCombiner<MultiDistanceSelector>;
template class OverlappingContourCombiner<MultiAndTrueDistanceSelector>;
}
|