src/geometry.h

Fri, 17 Sep 2021 22:43:22 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 17 Sep 2021 22:43:22 +0300
changeset 131
e2080ac44039
parent 123
e3fe3617b631
child 168
24590af32ad6
permissions
-rw-r--r--

add missing change to document.cpp

55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #pragma once
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 #include "basics.h"
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 namespace geom
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 struct Plane
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 glm::vec3 normal;
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 glm::vec3 anchor;
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 };
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
12 template<int N, typename T = float, glm::qualifier Q = glm::defaultp>
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 struct Line
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 {
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
15 glm::vec<N, T, Q> direction;
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
16 glm::vec<N, T, Q> anchor;
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
17 };
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 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
20 struct Ray
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
21 {
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
22 glm::vec<N, T, Q> direction;
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
23 glm::vec<N, T, Q> anchor;
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 };
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 template<int N>
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 struct Polygon
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 {
87
93ec4d630346 added PolygonObject and refactored away a lot of boilerplate
Teemu Piippo <teemu@hecknology.net>
parents: 71
diff changeset
29 std::array<glm::vec3, N> points;
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 };
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
32 template<int N>
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
33 struct Polygon2D
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
34 {
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
35 glm::vec2 points[N];
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
36 };
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
37
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 inline const glm::vec3 origin = {0, 0, 0};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 inline const Plane XY = {{0, 0, 1}, origin};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 inline const Plane XZ = {{0, 1, 0}, origin};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 inline const Plane YZ = {{1, 0, 0}, origin};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 using Triangle = Polygon<3>;
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
43 using LineSegment2D = Polygon2D<2>;
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
45 /**
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
46 * @brief Computes a line from two points
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
47 * @param point_1
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
48 * @param point_2
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
49 * @return line
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
50 */
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
51 template<int N, typename T, glm::qualifier Q>
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
52 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
53 {
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
54 return {point_2 - point_1, point_1};
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
55 }
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 template<int N, typename T, glm::qualifier Q>
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
58 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
59 {
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
60 return {point_2 - point_1, point_1};
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
61 }
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 template<int N, typename T, glm::qualifier Q>
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
64 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
65 {
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
66 return {ray.direction, ray.anchor};
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
67 }
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 enum class RectangleSide
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
70 {
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
71 Top,
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
72 Left,
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
73 Bottom,
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
74 Right
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
75 };
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 struct PointOnRectagle
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
78 {
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
79 glm::vec2 position;
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
80 RectangleSide side;
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
81 };
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 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
84 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
85 std::optional<PointOnRectagle> rayRectangleIntersection(const Ray<2>& ray, const QRectF& rectangle);
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 Plane planeFromTriangle(const Triangle& triangle);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 glm::vec3 normalVector(const Triangle& triangle);
115
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
88 std::optional<glm::vec3> linePlaneIntersection(
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
89 const Line<3>& line,
ed884a2fb009 fix too long lines
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
90 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
91 glm::vec3 scalingVector(const glm::mat4 matrix);
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
92 LineSegment2D top(const QRectF& rectangle);
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
93 LineSegment2D bottom(const QRectF& rectangle);
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
94 LineSegment2D left(const QRectF& rectangle);
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
95 LineSegment2D right(const QRectF& rectangle);
123
e3fe3617b631 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 122
diff changeset
96 bool isConvex(const std::vector<glm::vec3>& polygon);
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
97 struct ScalingExtract
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
98 {
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
99 glm::vec3 scaling;
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
100 glm::mat4 unscaled;
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
101 };
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 58
diff changeset
102 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
103
6ca6e8c647d4 added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
104 struct NPolygon
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 std::vector<glm::vec3> points;
6ca6e8c647d4 added preview layer code and fixed build warnings
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
107 };
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 105
diff changeset
108
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
109 inline constexpr bool isclose(const glm::vec3& a, const glm::vec3& b)
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 105
diff changeset
110 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 105
diff changeset
111 return qFuzzyCompare(a.x, b.x)
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 105
diff changeset
112 and qFuzzyCompare(a.y, b.y)
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 105
diff changeset
113 and qFuzzyCompare(a.z, b.z);
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 105
diff changeset
114 }
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
115
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
116 struct CircleF
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
117 {
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
118 QPointF center;
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
119 qreal radius;
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
120 };
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
121
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
122 /**
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
123 * @brief Inscribes a circle
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
124 * @param circle
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
125 * @return a QRectF that inscribes the specified circle
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
126 */
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
127 inline constexpr QRectF inscribe(const CircleF& circle)
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
128 {
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
129 return {
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
130 circle.center.x() - circle.radius,
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
131 circle.center.y() - circle.radius,
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
132 circle.radius * 2,
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
133 circle.radius * 2
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
134 };
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
135 }
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 }

mercurial