src/geometry.cpp

Wed, 26 Feb 2020 02:21:07 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 26 Feb 2020 02:21:07 +0200
changeset 55
cb81ecb5fb23
child 58
b7841cd31fb7
permissions
-rw-r--r--

grid stuff

#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