src/geometry.cpp

Fri, 28 Feb 2020 19:24:33 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 28 Feb 2020 19:24:33 +0200
changeset 63
f7dd937667a5
parent 58
b7841cd31fb7
child 64
f99d52b1646b
permissions
-rw-r--r--

omg functional programming

#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 epsilon)
{
	const float denominator = glm::dot(line.direction, plane.normal);
	if (std::abs(denominator) < epsilon)
	{
		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