src/geometry.cpp

changeset 64
f99d52b1646b
parent 58
b7841cd31fb7
child 71
198d25fe4e21
equal deleted inserted replaced
63:f7dd937667a5 64:f99d52b1646b
1 #include <glm/gtc/matrix_transform.hpp>
1 #include "geometry.h" 2 #include "geometry.h"
2 3
3 /** 4 /**
4 * @brief Computes a line from two points 5 * @brief Computes a line from two points
5 * @param point_1 6 * @param point_1
36 * @param triangle 37 * @param triangle
37 * @return plane 38 * @return plane
38 */ 39 */
39 geom::Plane geom::planeFromTriangle(const geom::Triangle& triangle) 40 geom::Plane geom::planeFromTriangle(const geom::Triangle& triangle)
40 { 41 {
41 return geom::Plane{triangle.points[0], normalVector(triangle)}; 42 return geom::Plane{normalVector(triangle), triangle.points[0]};
42 } 43 }
43 44
44 /** 45 /**
45 * @brief Computes the normal vector of a triangle 46 * @brief Computes the normal vector of a triangle
46 * @param triangle 47 * @param triangle
51 return glm::normalize( 52 return glm::normalize(
52 glm::cross( 53 glm::cross(
53 triangle.points[1] - triangle.points[0], 54 triangle.points[1] - triangle.points[0],
54 triangle.points[2] - triangle.points[0])); 55 triangle.points[2] - triangle.points[0]));
55 } 56 }
57
58 /**
59 * @brief Extracts the scaling component of the specified matrix into a vector and returns both the scaling
60 * components as well as the unscaled matrix.
61 * @param matrix Matrix to compute
62 * @return scaling vector and unscaled matrix
63 */
64 geom::ScalingExtract geom::extractScaling(const glm::mat4& matrix)
65 {
66 geom::ScalingExtract result;
67 result.scaling = geom::scalingVector(matrix);
68 result.unscaled = glm::scale(matrix, 1.0f / result.scaling);
69 return result;
70 }
71
72 /**
73 * @brief Computes the scaling vector, which contains the scaling of the specified matrix
74 * @param matrix
75 * @return scaling vector
76 */
77 glm::vec3 geom::scalingVector(const glm::mat4 matrix)
78 {
79 auto component = [](const glm::mat4& matrix, const int i) -> float
80 {
81 return std::hypot(std::hypot(matrix[i][0], matrix[i][1]), matrix[i][2]);
82 };
83 return glm::vec3{component(matrix, 0), component(matrix, 1), component(matrix, 2)};
84 }

mercurial