src/geometry.cpp

changeset 55
cb81ecb5fb23
child 58
b7841cd31fb7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/geometry.cpp	Wed Feb 26 02:21:07 2020 +0200
@@ -0,0 +1,55 @@
+#include "geometry.h"
+
+/**
+ * @brief Computes a line from two points
+ * @param point_1
+ * @param point_2
+ * @return line
+ */
+geom::Line geom::lineFromPoints(const glm::vec3& point_1, const glm::vec3 point_2)
+{
+	return {point_2 - point_1, point_1};
+}
+
+/**
+ * @brief Computes line-plane intersection
+ * @param line
+ * @param plane
+ * @return point of intersection. Does not return a value if the line is in parallel to the plane.
+ */
+std::optional<glm::vec3> geom::linePlaneIntersection(const geom::Line& line, const geom::Plane& plane)
+{
+	const float denominator = glm::dot(line.direction, plane.normal);
+	if (std::abs(denominator) < 1e-8f)
+	{
+		return {};
+	}
+	else
+	{
+		const float d = glm::dot(plane.anchor - line.anchor, plane.normal) / denominator;
+		return line.anchor + d * line.direction;
+	}
+}
+
+/**
+ * @brief Computes the plane of a triangle
+ * @param triangle
+ * @return plane
+ */
+geom::Plane geom::planeFromTriangle(const geom::Triangle& triangle)
+{
+	return geom::Plane{triangle.points[0], normalVector(triangle)};
+}
+
+/**
+ * @brief Computes the normal vector of a triangle
+ * @param triangle
+ * @return normal vector
+ */
+glm::vec3 geom::normalVector(const geom::Triangle& triangle)
+{
+	return glm::normalize(
+		glm::cross(
+			triangle.points[1] - triangle.points[0],
+			triangle.points[2] - triangle.points[0]));
+}

mercurial