Tue, 24 May 2022 16:11:10 +0300
more work on circle tool + cleanup
55 | 1 | #pragma once |
168
24590af32ad6
Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
2 | #include <QPolygonF> |
55 | 3 | #include "basics.h" |
4 | ||
5 | namespace geom | |
6 | { | |
7 | struct Plane | |
8 | { | |
9 | glm::vec3 normal; | |
10 | glm::vec3 anchor; | |
11 | }; | |
12 | ||
71
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
13 | template<int N, typename T = float, glm::qualifier Q = glm::defaultp> |
55 | 14 | struct Line |
15 | { | |
71
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
16 | glm::vec<N, T, Q> direction; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
17 | glm::vec<N, T, Q> anchor; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
18 | }; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
19 | |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
20 | template<int N, typename T = float, glm::qualifier Q = glm::defaultp> |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
21 | struct Ray |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
22 | { |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
23 | glm::vec<N, T, Q> direction; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
24 | glm::vec<N, T, Q> anchor; |
55 | 25 | }; |
26 | ||
27 | template<int N> | |
28 | struct Polygon | |
29 | { | |
87
93ec4d630346
added PolygonObject and refactored away a lot of boilerplate
Teemu Piippo <teemu@hecknology.net>
parents:
71
diff
changeset
|
30 | std::array<glm::vec3, N> points; |
55 | 31 | }; |
32 | ||
71
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
33 | template<int N> |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
34 | struct Polygon2D |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
35 | { |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
36 | glm::vec2 points[N]; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
37 | }; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
38 | |
55 | 39 | inline const glm::vec3 origin = {0, 0, 0}; |
40 | inline const Plane XY = {{0, 0, 1}, origin}; | |
41 | inline const Plane XZ = {{0, 1, 0}, origin}; | |
42 | inline const Plane YZ = {{1, 0, 0}, origin}; | |
43 | using Triangle = Polygon<3>; | |
71
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
44 | using LineSegment2D = Polygon2D<2>; |
55 | 45 | |
71
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
46 | /** |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
47 | * @brief Computes a line from two points |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
48 | * @param point_1 |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
49 | * @param point_2 |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
50 | * @return line |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
51 | */ |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
52 | template<int N, typename T, glm::qualifier Q> |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
53 | Line<N, T, Q> lineFromPoints(const glm::vec<N, T, Q>& point_1, const glm::vec<N, T, Q>& point_2) |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
54 | { |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
55 | return {point_2 - point_1, point_1}; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
56 | } |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
57 | |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
58 | template<int N, typename T, glm::qualifier Q> |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
59 | Ray<N, T, Q> rayFromPoints(const glm::vec<N, T, Q>& point_1, const glm::vec<N, T, Q>& point_2) |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
60 | { |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
61 | return {point_2 - point_1, point_1}; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
62 | } |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
63 | |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
64 | template<int N, typename T, glm::qualifier Q> |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
65 | Line<N, T, Q> rayToLine(const Ray<N, T, Q>& ray) |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
66 | { |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
67 | return {ray.direction, ray.anchor}; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
68 | } |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
69 | |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
70 | enum class RectangleSide |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
71 | { |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
72 | Top, |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
73 | Left, |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
74 | Bottom, |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
75 | Right |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
76 | }; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
77 | |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
78 | struct PointOnRectagle |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
79 | { |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
80 | glm::vec2 position; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
81 | RectangleSide side; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
82 | }; |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
83 | |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
84 | std::optional<glm::vec2> lineLineIntersection(const Line<2>& line_1, const Line<2>& line_2); |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
85 | std::optional<glm::vec2> rayLineSegmentIntersection(const Ray<2>& ray, const LineSegment2D& line); |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
86 | std::optional<PointOnRectagle> rayRectangleIntersection(const Ray<2>& ray, const QRectF& rectangle); |
55 | 87 | Plane planeFromTriangle(const Triangle& triangle); |
88 | glm::vec3 normalVector(const Triangle& triangle); | |
115 | 89 | std::optional<glm::vec3> linePlaneIntersection( |
90 | const Line<3>& line, | |
91 | const Plane& plane, const float epsilon = 1e-6f); | |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
92 | glm::vec3 scalingVector(const glm::mat4 matrix); |
71
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
93 | LineSegment2D top(const QRectF& rectangle); |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
94 | LineSegment2D bottom(const QRectF& rectangle); |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
95 | LineSegment2D left(const QRectF& rectangle); |
198d25fe4e21
show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents:
64
diff
changeset
|
96 | LineSegment2D right(const QRectF& rectangle); |
123 | 97 | bool isConvex(const std::vector<glm::vec3>& polygon); |
168
24590af32ad6
Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
98 | Winding winding(const QPolygonF& polygon); |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
99 | struct ScalingExtract |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
100 | { |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
101 | glm::vec3 scaling; |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
102 | glm::mat4 unscaled; |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
103 | }; |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
104 | ScalingExtract extractScaling(const glm::mat4& matrix); |
105
6ca6e8c647d4
added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
105 | |
6ca6e8c647d4
added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
106 | struct NPolygon |
6ca6e8c647d4
added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
107 | { |
6ca6e8c647d4
added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
108 | std::vector<glm::vec3> points; |
6ca6e8c647d4
added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents:
87
diff
changeset
|
109 | }; |
106 | 110 | |
108 | 111 | inline constexpr bool isclose(const glm::vec3& a, const glm::vec3& b) |
106 | 112 | { |
113 | return qFuzzyCompare(a.x, b.x) | |
114 | and qFuzzyCompare(a.y, b.y) | |
115 | and qFuzzyCompare(a.z, b.z); | |
116 | } | |
108 | 117 | |
118 | struct CircleF | |
119 | { | |
120 | QPointF center; | |
121 | qreal radius; | |
122 | }; | |
123 | ||
124 | /** | |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
125 | * @brief Inscribes a circle |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
126 | * @param circle |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
127 | * @return a QRectF that inscribes the specified circle |
108 | 128 | */ |
129 | inline constexpr QRectF inscribe(const CircleF& circle) | |
130 | { | |
131 | return { | |
132 | circle.center.x() - circle.radius, | |
133 | circle.center.y() - circle.radius, | |
134 | circle.radius * 2, | |
135 | circle.radius * 2 | |
136 | }; | |
137 | } | |
187 | 138 | |
139 | struct BezierCurve | |
140 | { | |
141 | glm::vec3 points[4]; | |
142 | const glm::vec3& operator[](int x) const | |
143 | { | |
144 | Q_ASSERT(x >= 0 and x < 4); | |
145 | return this->points[x]; | |
146 | } | |
147 | glm::vec3& operator[](int x) | |
148 | { | |
149 | Q_ASSERT(x >= 0 and x < 4); | |
150 | return this->points[x]; | |
151 | } | |
152 | }; | |
153 | ||
154 | glm::vec3 pointOnCurve(const BezierCurve& curve, float t); | |
155 | glm::vec3 derivativeOnCurve(const BezierCurve& curve, float t); | |
55 | 156 | } |