src/geometry.h

Wed, 12 Apr 2023 01:53:42 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 12 Apr 2023 01:53:42 +0300
changeset 379
8d88adffb779
parent 374
75efc3ba5a56
permissions
-rw-r--r--

Circular primitive type is now an enum class

55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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>
206
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
3 #include <glm/glm.hpp>
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
4 #include <optional>
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
5
372
b2914aaeec1a Winding is now an enum class winding_e
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 371
diff changeset
6 enum class winding_e
206
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
7 {
372
b2914aaeec1a Winding is now an enum class winding_e
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 371
diff changeset
8 none,
b2914aaeec1a Winding is now an enum class winding_e
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 371
diff changeset
9 anticlockwise,
b2914aaeec1a Winding is now an enum class winding_e
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 371
diff changeset
10 clockwise
b2914aaeec1a Winding is now an enum class winding_e
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 371
diff changeset
11 };
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
13 struct Plane
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
14 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
15 glm::vec3 normal;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
16 glm::vec3 anchor;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
17 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
18
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
19 template<int N, typename T = float, glm::qualifier Q = glm::defaultp>
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
20 struct Line
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
21 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
22 glm::vec<N, T, Q> direction;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
23 glm::vec<N, T, Q> anchor;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
24 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
25
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
26 template<int N, typename T = float, glm::qualifier Q = glm::defaultp>
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
27 struct Ray
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
28 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
29 glm::vec<N, T, Q> direction;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
30 glm::vec<N, T, Q> anchor;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
31 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
32
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
33 inline const glm::vec3 origin = {0, 0, 0};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
34 inline const Plane XY = {{0, 0, 1}, origin};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
35 inline const Plane XZ = {{0, 1, 0}, origin};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
36 inline const Plane YZ = {{1, 0, 0}, origin};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
37
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
38 struct LineSegment
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
39 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
40 glm::vec3 p1, p2;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
41 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
42 struct Triangle
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
43 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
44 glm::vec3 p1, p2, p3;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
45 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
46 struct Quadrilateral
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 {
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
48 glm::vec3 p1, p2, p3, p4;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
49 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
50 struct ConditionalEdge
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
51 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
52 glm::vec3 p1, p2;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
53 glm::vec3 c1, c2;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
54 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
55 struct LineSegment2D
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
56 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
57 glm::vec2 p1, p2;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
58 };
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
60 /**
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
61 * @brief Computes a line from two points
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
62 * @param point_1
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
63 * @param point_2
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
64 * @return line
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
65 */
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
66 template<int N, typename T, glm::qualifier Q>
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
67 Line<N, T, Q> lineFromPoints(const glm::vec<N, T, Q>& point_1, const glm::vec<N, T, Q>& point_2)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
68 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
69 return {point_2 - point_1, point_1};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
70 }
71
198d25fe4e21 show axis directions on the screen
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
71
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
72 template<int N, typename T, glm::qualifier Q>
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
73 Ray<N, T, Q> rayFromPoints(const glm::vec<N, T, Q>& point_1, const glm::vec<N, T, Q>& point_2)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
74 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
75 return {point_2 - point_1, point_1};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
76 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
77
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
78 template<int N, typename T, glm::qualifier Q>
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
79 Line<N, T, Q> rayToLine(const Ray<N, T, Q>& ray)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
80 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
81 return {ray.direction, ray.anchor};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
82 }
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
84 enum class RectangleSide
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
85 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
86 Top,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
87 Left,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
88 Bottom,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
89 Right
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
90 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
91
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
92 struct PointOnRectagle
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
93 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
94 glm::vec2 position;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
95 RectangleSide side;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
96 };
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 187
diff changeset
97
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
98 std::optional<glm::vec2> lineLineIntersection(const Line<2>& line_1, const Line<2>& line_2);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
99 std::optional<glm::vec2> rayLineSegmentIntersection(const Ray<2>& ray, const LineSegment2D& line);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
100 std::optional<PointOnRectagle> rayRectangleIntersection(const Ray<2>& ray, const QRectF& rectangle);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
101 Plane planeFromTriangle(const Triangle& triangle);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
102 glm::vec3 normalVector(const Triangle& triangle);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
103 std::optional<glm::vec3> linePlaneIntersection(
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
104 const Line<3>& line,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
105 const Plane& plane, const float epsilon = 1e-6f);
371
171d3f9638a9 rename some functions
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 369
diff changeset
106 glm::vec3 calculate_matrix_scaling(const glm::mat4 matrix);
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
107 LineSegment2D top(const QRectF& rectangle);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
108 LineSegment2D bottom(const QRectF& rectangle);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
109 LineSegment2D left(const QRectF& rectangle);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
110 LineSegment2D right(const QRectF& rectangle);
369
57de8fab2237 isConvex renamed to quadrilateral_convexity, refactor, now returns an enum instead of bool
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 322
diff changeset
111 enum class convexity_e { concave, convex };
57de8fab2237 isConvex renamed to quadrilateral_convexity, refactor, now returns an enum instead of bool
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 322
diff changeset
112 convexity_e quadrilateral_convexity(const Quadrilateral& quad);
57de8fab2237 isConvex renamed to quadrilateral_convexity, refactor, now returns an enum instead of bool
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 322
diff changeset
113 convexity_e polygon_convexity(const std::vector<glm::vec3>& polygon);
372
b2914aaeec1a Winding is now an enum class winding_e
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 371
diff changeset
114 winding_e calculate_polygon_winding(const QPolygonF& polygon);
371
171d3f9638a9 rename some functions
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 369
diff changeset
115 struct unscaled_matrix
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
116 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
117 glm::vec3 scaling;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
118 glm::mat4 unscaled;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
119 };
371
171d3f9638a9 rename some functions
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 369
diff changeset
120 unscaled_matrix unscale_matrix(const glm::mat4& matrix);
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
121
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
122 inline constexpr bool isclose(const glm::vec3& a, const glm::vec3& b)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
123 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
124 return qFuzzyCompare(a.x, b.x)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
125 and qFuzzyCompare(a.y, b.y)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
126 and qFuzzyCompare(a.z, b.z);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
127 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
128
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
129 struct CircleF
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
130 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
131 QPointF center;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
132 qreal radius;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
133 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
134
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
135 /**
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
136 * @brief Inscribes a circle
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
137 * @param circle
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
138 * @return a QRectF that inscribes the specified circle
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
139 */
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
140 inline constexpr QRectF inscribe(const CircleF& circle)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
141 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
142 return {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
143 circle.center.x() - circle.radius,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
144 circle.center.y() - circle.radius,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
145 circle.radius * 2,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
146 circle.radius * 2
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 187
diff changeset
147 };
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
148 }
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149
321
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
150 template<typename Iter>
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
151 std::optional<glm::mat3> calculateNormal(Iter&& begin, Iter&& end)
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
152 {
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
153 const long int n = end - begin;
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
154 std::optional<glm::mat3> result;
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
155 for (long int i = 0; i < n; ++i) {
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
156 const glm::vec3& v1 = *(begin + (i + n - 1) % n);
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
157 const glm::vec3& v2 = *(begin + i);
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
158 const glm::vec3& v3 = *(begin + (i + 1) % n);
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
159 const glm::vec3 xvec = v1 - v2;
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
160 const glm::vec3 yvec = v3 - v2;
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
161 constexpr float threshold = 1e-6f;
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
162 if (glm::length(xvec) > threshold and glm::length(yvec) > threshold) {
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
163 const glm::vec3 zvec = glm::cross(glm::normalize(xvec), glm::normalize(yvec));
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
164 if (glm::length(zvec) > threshold) {
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
165 result = {xvec, yvec, zvec};
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
166 break;
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
167 }
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
168 }
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
169 }
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
170 return result;
180072db4a83 Fix polygonize not finding the normal properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
171 }
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 321
diff changeset
172
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 321
diff changeset
173 constexpr QPointF vecToQPoint(const glm::vec2& a)
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 321
diff changeset
174 {
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 321
diff changeset
175 return {a.x, a.y};
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 321
diff changeset
176 }

mercurial