CPP-TOOLBOX
Loading...
Searching...
No Matches
linalg_utils.hpp
Go to the documentation of this file.
1#ifndef LINALG_UTILS_HPP
2#define LINALG_UTILS_HPP
3
4#include <cmath>
5#include <array>
6#include <glm/glm.hpp>
7#include <functional>
8
9#include <glm/glm.hpp>
10#include <glm/gtc/epsilon.hpp>
11#include <cmath>
12
13namespace linalg_utils {
14
15/* RIGHT HAND RULE FOR CROSS PRODUCT
16 *
17 * (A x B)
18 *
19 * |
20 * |
21 * |
22 * |
23 * |
24 * o---------- B
25 * /
26 * /
27 * /
28 * /
29 *
30 * A
31 *
32 */
33
34std::function<glm::vec4(const glm::vec4 &)> make_matrix_multiplier(const glm::mat4 &m);
35
36// NOTE: a plane can be specified by the equation (n . x) = 0 where n is the normal vector of the plane, this can
37// represent any plane going through the origin. If we want to allow the plane to instead pass through some arbitrary
38// point p, then the equation becomes (n . (p - x)) = 0 and now p is guarenteed to be on the plane, so we're offseting
39// the plane by p the equation can then become n.p - n.x = 0 which is equivalent to n.x + d = 0 fs d in R, so the most
40// compact way to express a plane is by n, d which is 4d vector
41
42class Plane {
43 public:
44 Plane(const glm::vec3 &n, const glm::vec3 &point) : normal(glm::normalize(n)), point_on_plane(point) {}
45
46 Plane(const glm::vec3 &p0, const glm::vec3 &p1, const glm::vec3 &p2) : point_on_plane(p0) {
47 glm::vec3 edge1 = p1 - p0;
48 glm::vec3 edge2 = p2 - p0;
49 normal = glm::normalize(glm::cross(edge1, edge2));
50 }
51
52 glm::vec3 get_normal() const { return normal; }
53 glm::vec3 get_point() const { return point_on_plane; }
54
55 float signed_distance(const glm::vec3 &point) const { return glm::dot(normal, point - point_on_plane); }
56
58
59 Side classify_point(const glm::vec3 &point, float eps = 1e-6f) const {
60 float dist = signed_distance(point);
61 if (glm::epsilonEqual(dist, 0.0f, eps))
62 return Side::OnPlane;
63 return (dist > 0.0f) ? Side::NormalSide : Side::OppositeSide;
64 }
65
66 private:
67 glm::vec3 normal; // normalized normal
68 glm::vec3 point_on_plane; // reference point on plane
69};
70
71} // namespace linalg_utils
72
73#endif // LINALG_UTILS_HPP
Side classify_point(const glm::vec3 &point, float eps=1e-6f) const
Definition linalg_utils.hpp:59
glm::vec3 get_point() const
Definition linalg_utils.hpp:53
float signed_distance(const glm::vec3 &point) const
Definition linalg_utils.hpp:55
Side
Definition linalg_utils.hpp:57
@ OppositeSide
Definition linalg_utils.hpp:57
@ NormalSide
Definition linalg_utils.hpp:57
@ OnPlane
Definition linalg_utils.hpp:57
Plane(const glm::vec3 &n, const glm::vec3 &point)
Definition linalg_utils.hpp:44
Plane(const glm::vec3 &p0, const glm::vec3 &p1, const glm::vec3 &p2)
Definition linalg_utils.hpp:46
glm::vec3 get_normal() const
Definition linalg_utils.hpp:52
@ m
Definition input_state.hpp:39
@ n
Definition input_state.hpp:40
Definition glm_printing.hpp:28
Definition linalg_utils.cpp:3
std::function< glm::vec4(const glm::vec4 &)> make_matrix_multiplier(const glm::mat4 &m)
Definition linalg_utils.cpp:5