src/geometry.cpp

changeset 55
cb81ecb5fb23
child 58
b7841cd31fb7
equal deleted inserted replaced
54:a4055f67b9c7 55:cb81ecb5fb23
1 #include "geometry.h"
2
3 /**
4 * @brief Computes a line from two points
5 * @param point_1
6 * @param point_2
7 * @return line
8 */
9 geom::Line geom::lineFromPoints(const glm::vec3& point_1, const glm::vec3 point_2)
10 {
11 return {point_2 - point_1, point_1};
12 }
13
14 /**
15 * @brief Computes line-plane intersection
16 * @param line
17 * @param plane
18 * @return point of intersection. Does not return a value if the line is in parallel to the plane.
19 */
20 std::optional<glm::vec3> geom::linePlaneIntersection(const geom::Line& line, const geom::Plane& plane)
21 {
22 const float denominator = glm::dot(line.direction, plane.normal);
23 if (std::abs(denominator) < 1e-8f)
24 {
25 return {};
26 }
27 else
28 {
29 const float d = glm::dot(plane.anchor - line.anchor, plane.normal) / denominator;
30 return line.anchor + d * line.direction;
31 }
32 }
33
34 /**
35 * @brief Computes the plane of a triangle
36 * @param triangle
37 * @return plane
38 */
39 geom::Plane geom::planeFromTriangle(const geom::Triangle& triangle)
40 {
41 return geom::Plane{triangle.points[0], normalVector(triangle)};
42 }
43
44 /**
45 * @brief Computes the normal vector of a triangle
46 * @param triangle
47 * @return normal vector
48 */
49 glm::vec3 geom::normalVector(const geom::Triangle& triangle)
50 {
51 return glm::normalize(
52 glm::cross(
53 triangle.points[1] - triangle.points[0],
54 triangle.points[2] - triangle.points[0]));
55 }

mercurial