src/geometry.cpp

changeset 200
ca23936b455b
parent 187
30204975694a
child 201
5d201ee4a9c3
equal deleted inserted replaced
199:6988973515d2 200:ca23936b455b
31 * @param triangle 31 * @param triangle
32 * @return plane 32 * @return plane
33 */ 33 */
34 geom::Plane geom::planeFromTriangle(const geom::Triangle& triangle) 34 geom::Plane geom::planeFromTriangle(const geom::Triangle& triangle)
35 { 35 {
36 return geom::Plane{normalVector(triangle), triangle.points[0]}; 36 return geom::Plane{normalVector(triangle), triangle.p1};
37 } 37 }
38 38
39 /** 39 /**
40 * @brief Computes the normal vector of a triangle 40 * @brief Computes the normal vector of a triangle
41 * @param triangle 41 * @param triangle
43 */ 43 */
44 glm::vec3 geom::normalVector(const geom::Triangle& triangle) 44 glm::vec3 geom::normalVector(const geom::Triangle& triangle)
45 { 45 {
46 return glm::normalize( 46 return glm::normalize(
47 glm::cross( 47 glm::cross(
48 triangle.points[1] - triangle.points[0], 48 triangle.p2 - triangle.p1,
49 triangle.points[2] - triangle.points[0])); 49 triangle.p3 - triangle.p1));
50 } 50 }
51 51
52 /** 52 /**
53 * @brief Extracts the scaling component of the specified matrix into a vector and returns both the scaling 53 * @brief Extracts the scaling component of the specified matrix into a vector and returns both the scaling
54 * components as well as the unscaled matrix. 54 * components as well as the unscaled matrix.
105 105
106 std::optional<glm::vec2> geom::rayLineSegmentIntersection(const Ray<2>& ray, const LineSegment2D& line) 106 std::optional<glm::vec2> geom::rayLineSegmentIntersection(const Ray<2>& ray, const LineSegment2D& line)
107 { 107 {
108 std::optional<glm::vec2> result = lineLineIntersection( 108 std::optional<glm::vec2> result = lineLineIntersection(
109 rayToLine(ray), 109 rayToLine(ray),
110 lineFromPoints(line.points[0], line.points[1])); 110 lineFromPoints(line.p1, line.p2));
111 if (result.has_value()) 111 if (result.has_value())
112 { 112 {
113 const float d1 = glm::dot(*result - ray.anchor, ray.direction); 113 const float d1 = glm::dot(*result - ray.anchor, ray.direction);
114 if (d1 < 0) 114 if (d1 < 0)
115 { 115 {
116 result.reset(); 116 result.reset();
117 } 117 }
118 else 118 else
119 { 119 {
120 const float d2 = glm::dot(*result - line.points[0], *result - line.points[1]); 120 const float d2 = glm::dot(*result - line.p1, *result - line.p2);
121 if (d2 > 0) 121 if (d2 > 0)
122 { 122 {
123 result.reset(); 123 result.reset();
124 } 124 }
125 } 125 }

mercurial