Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/msdfgen/core/edge-segments.h
9912 views
1
2
#pragma once
3
4
#include "Vector2.hpp"
5
#include "SignedDistance.hpp"
6
#include "EdgeColor.h"
7
8
namespace msdfgen {
9
10
// Parameters for iterative search of closest point on a cubic Bezier curve. Increase for higher precision.
11
#define MSDFGEN_CUBIC_SEARCH_STARTS 4
12
#define MSDFGEN_CUBIC_SEARCH_STEPS 4
13
14
/// An abstract edge segment.
15
class EdgeSegment {
16
17
public:
18
EdgeColor color;
19
20
static EdgeSegment *create(Point2 p0, Point2 p1, EdgeColor edgeColor = WHITE);
21
static EdgeSegment *create(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor = WHITE);
22
static EdgeSegment *create(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor = WHITE);
23
24
EdgeSegment(EdgeColor edgeColor = WHITE) : color(edgeColor) { }
25
virtual ~EdgeSegment() { }
26
/// Creates a copy of the edge segment.
27
virtual EdgeSegment *clone() const = 0;
28
/// Returns the numeric code of the edge segment's type.
29
virtual int type() const = 0;
30
/// Returns the array of control points.
31
virtual const Point2 *controlPoints() const = 0;
32
/// Returns the point on the edge specified by the parameter (between 0 and 1).
33
virtual Point2 point(double param) const = 0;
34
/// Returns the direction the edge has at the point specified by the parameter.
35
virtual Vector2 direction(double param) const = 0;
36
/// Returns the change of direction (second derivative) at the point specified by the parameter.
37
virtual Vector2 directionChange(double param) const = 0;
38
/// Returns the minimum signed distance between origin and the edge.
39
virtual SignedDistance signedDistance(Point2 origin, double &param) const = 0;
40
/// Converts a previously retrieved signed distance from origin to perpendicular distance.
41
virtual void distanceToPerpendicularDistance(SignedDistance &distance, Point2 origin, double param) const;
42
/// Outputs a list of (at most three) intersections (their X coordinates) with an infinite horizontal scanline at y and returns how many there are.
43
virtual int scanlineIntersections(double x[3], int dy[3], double y) const = 0;
44
/// Adjusts the bounding box to fit the edge segment.
45
virtual void bound(double &l, double &b, double &r, double &t) const = 0;
46
47
/// Reverses the edge (swaps its start point and end point).
48
virtual void reverse() = 0;
49
/// Moves the start point of the edge segment.
50
virtual void moveStartPoint(Point2 to) = 0;
51
/// Moves the end point of the edge segment.
52
virtual void moveEndPoint(Point2 to) = 0;
53
/// Splits the edge segments into thirds which together represent the original edge.
54
virtual void splitInThirds(EdgeSegment *&part0, EdgeSegment *&part1, EdgeSegment *&part2) const = 0;
55
56
};
57
58
/// A line segment.
59
class LinearSegment : public EdgeSegment {
60
61
public:
62
enum EdgeType {
63
EDGE_TYPE = 1
64
};
65
66
Point2 p[2];
67
68
LinearSegment(Point2 p0, Point2 p1, EdgeColor edgeColor = WHITE);
69
LinearSegment *clone() const;
70
int type() const;
71
const Point2 *controlPoints() const;
72
Point2 point(double param) const;
73
Vector2 direction(double param) const;
74
Vector2 directionChange(double param) const;
75
double length() const;
76
SignedDistance signedDistance(Point2 origin, double &param) const;
77
int scanlineIntersections(double x[3], int dy[3], double y) const;
78
void bound(double &l, double &b, double &r, double &t) const;
79
80
void reverse();
81
void moveStartPoint(Point2 to);
82
void moveEndPoint(Point2 to);
83
void splitInThirds(EdgeSegment *&part0, EdgeSegment *&part1, EdgeSegment *&part2) const;
84
85
};
86
87
/// A quadratic Bezier curve.
88
class QuadraticSegment : public EdgeSegment {
89
90
public:
91
enum EdgeType {
92
EDGE_TYPE = 2
93
};
94
95
Point2 p[3];
96
97
QuadraticSegment(Point2 p0, Point2 p1, Point2 p2, EdgeColor edgeColor = WHITE);
98
QuadraticSegment *clone() const;
99
int type() const;
100
const Point2 *controlPoints() const;
101
Point2 point(double param) const;
102
Vector2 direction(double param) const;
103
Vector2 directionChange(double param) const;
104
double length() const;
105
SignedDistance signedDistance(Point2 origin, double &param) const;
106
int scanlineIntersections(double x[3], int dy[3], double y) const;
107
void bound(double &l, double &b, double &r, double &t) const;
108
109
void reverse();
110
void moveStartPoint(Point2 to);
111
void moveEndPoint(Point2 to);
112
void splitInThirds(EdgeSegment *&part0, EdgeSegment *&part1, EdgeSegment *&part2) const;
113
114
EdgeSegment *convertToCubic() const;
115
116
};
117
118
/// A cubic Bezier curve.
119
class CubicSegment : public EdgeSegment {
120
121
public:
122
enum EdgeType {
123
EDGE_TYPE = 3
124
};
125
126
Point2 p[4];
127
128
CubicSegment(Point2 p0, Point2 p1, Point2 p2, Point2 p3, EdgeColor edgeColor = WHITE);
129
CubicSegment *clone() const;
130
int type() const;
131
const Point2 *controlPoints() const;
132
Point2 point(double param) const;
133
Vector2 direction(double param) const;
134
Vector2 directionChange(double param) const;
135
SignedDistance signedDistance(Point2 origin, double &param) const;
136
int scanlineIntersections(double x[3], int dy[3], double y) const;
137
void bound(double &l, double &b, double &r, double &t) const;
138
139
void reverse();
140
void moveStartPoint(Point2 to);
141
void moveEndPoint(Point2 to);
142
void splitInThirds(EdgeSegment *&part0, EdgeSegment *&part1, EdgeSegment *&part2) const;
143
144
};
145
146
}
147
148