Fri, 06 Mar 2020 16:08:45 +0200
default to XZ
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
1 | #include <glm/gtc/matrix_transform.hpp> |
55 | 2 | #include "geometry.h" |
3 | ||
4 | /** | |
5 | * @brief Computes a line from two points | |
6 | * @param point_1 | |
7 | * @param point_2 | |
8 | * @return line | |
9 | */ | |
10 | geom::Line geom::lineFromPoints(const glm::vec3& point_1, const glm::vec3 point_2) | |
11 | { | |
12 | return {point_2 - point_1, point_1}; | |
13 | } | |
14 | ||
15 | /** | |
16 | * @brief Computes line-plane intersection | |
17 | * @param line | |
18 | * @param plane | |
19 | * @return point of intersection. Does not return a value if the line is in parallel to the plane. | |
20 | */ | |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
21 | std::optional<glm::vec3> geom::linePlaneIntersection(const geom::Line& line, const geom::Plane& plane, const float epsilon) |
55 | 22 | { |
23 | const float denominator = glm::dot(line.direction, plane.normal); | |
58
b7841cd31fb7
use glm::project instead of figuring out the conversion manually...
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
24 | if (std::abs(denominator) < epsilon) |
55 | 25 | { |
26 | return {}; | |
27 | } | |
28 | else | |
29 | { | |
30 | const float d = glm::dot(plane.anchor - line.anchor, plane.normal) / denominator; | |
31 | return line.anchor + d * line.direction; | |
32 | } | |
33 | } | |
34 | ||
35 | /** | |
36 | * @brief Computes the plane of a triangle | |
37 | * @param triangle | |
38 | * @return plane | |
39 | */ | |
40 | geom::Plane geom::planeFromTriangle(const geom::Triangle& triangle) | |
41 | { | |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
42 | return geom::Plane{normalVector(triangle), triangle.points[0]}; |
55 | 43 | } |
44 | ||
45 | /** | |
46 | * @brief Computes the normal vector of a triangle | |
47 | * @param triangle | |
48 | * @return normal vector | |
49 | */ | |
50 | glm::vec3 geom::normalVector(const geom::Triangle& triangle) | |
51 | { | |
52 | return glm::normalize( | |
53 | glm::cross( | |
54 | triangle.points[1] - triangle.points[0], | |
55 | triangle.points[2] - triangle.points[0])); | |
56 | } | |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
57 | |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
58 | /** |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
59 | * @brief Extracts the scaling component of the specified matrix into a vector and returns both the scaling |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
60 | * components as well as the unscaled matrix. |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
61 | * @param matrix Matrix to compute |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
62 | * @return scaling vector and unscaled matrix |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
63 | */ |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
64 | geom::ScalingExtract geom::extractScaling(const glm::mat4& matrix) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
65 | { |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
66 | geom::ScalingExtract result; |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
67 | result.scaling = geom::scalingVector(matrix); |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
68 | result.unscaled = glm::scale(matrix, 1.0f / result.scaling); |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
69 | return result; |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
70 | } |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
71 | |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
72 | /** |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
73 | * @brief Computes the scaling vector, which contains the scaling of the specified matrix |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
74 | * @param matrix |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
75 | * @return scaling vector |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
76 | */ |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
77 | glm::vec3 geom::scalingVector(const glm::mat4 matrix) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
78 | { |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
79 | auto component = [](const glm::mat4& matrix, const int i) -> float |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
80 | { |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
81 | return std::hypot(std::hypot(matrix[i][0], matrix[i][1]), matrix[i][2]); |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
82 | }; |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
83 | return glm::vec3{component(matrix, 0), component(matrix, 1), component(matrix, 2)}; |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
58
diff
changeset
|
84 | } |