src/geometry.cpp

Thu, 27 Feb 2020 12:23:42 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 27 Feb 2020 12:23:42 +0200
changeset 58
b7841cd31fb7
parent 55
cb81ecb5fb23
child 64
f99d52b1646b
permissions
-rw-r--r--

use glm::project instead of figuring out the conversion manually...

#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