1 /* This file is part of Dilay
2  * Copyright © 2015-2018 Alexander Bau
3  * Use and redistribute under the terms of the GNU General Public License
4  */
5 #include "primitive/plane.hpp"
6 #include "util.hpp"
7 
PrimPlane(const glm::vec3 & p,const glm::vec3 & n)8 PrimPlane::PrimPlane (const glm::vec3& p, const glm::vec3& n)
9   : _point (p)
10 {
11   this->normal (n);
12 }
13 
normal(const glm::vec3 & n)14 void PrimPlane::normal (const glm::vec3& n)
15 {
16   this->_normal = glm::normalize (n);
17   this->_u = glm::normalize (Util::orthogonal (this->_normal));
18   this->_v = glm::normalize (glm::cross (this->_normal, this->_u));
19 }
20 
distance(const glm::vec3 & p) const21 float PrimPlane::distance (const glm::vec3& p) const
22 {
23   return glm::dot (this->_normal, p - this->_point);
24 }
25 
absDistance(const glm::vec3 & p) const26 float PrimPlane::absDistance (const glm::vec3& p) const { return glm::abs (this->distance (p)); }
27 
onPlane(const glm::vec3 & p) const28 bool PrimPlane::onPlane (const glm::vec3& p) const
29 {
30   return this->absDistance (p) < Util::epsilon ();
31 }
32 
project(const glm::vec3 & p) const33 glm::vec3 PrimPlane::project (const glm::vec3& p) const
34 {
35   return p - (this->_normal * this->distance (p));
36 }
37 
project(const glm::vec2 & p) const38 glm::vec3 PrimPlane::project (const glm::vec2& p) const
39 {
40   return this->_point + (this->_u * p.x) + (this->_v * p.y);
41 }
42 
project2d(const glm::vec3 & p) const43 glm::vec2 PrimPlane::project2d (const glm::vec3& p) const
44 {
45   const glm::vec3 proj = this->project (p) - this->_point;
46 
47   return glm::vec2 (glm::dot (this->_u, proj), glm::dot (this->_v, proj));
48 }
49 
projectDirection(const glm::vec3 & d) const50 glm::vec3 PrimPlane::projectDirection (const glm::vec3& d) const
51 {
52   return this->project (d + this->_point) - this->_point;
53 }
54 
mirror(const glm::vec3 & p) const55 glm::vec3 PrimPlane::mirror (const glm::vec3& p) const
56 {
57   return p - (2.0f * this->_normal * this->distance (p));
58 }
59 
mirrorDirection(const glm::vec3 & d) const60 glm::vec3 PrimPlane::mirrorDirection (const glm::vec3& d) const
61 {
62   return this->mirror (d + this->_point) - this->_point;
63 }
64