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 <glm/gtx/norm.hpp>
6 #include "primitive/triangle.hpp"
7 #include "util.hpp"
8 
PrimTriangle(const glm::vec3 & v1,const glm::vec3 & v2,const glm::vec3 & v3)9 PrimTriangle::PrimTriangle (const glm::vec3& v1, const glm::vec3& v2, const glm::vec3& v3)
10   : _vertex1 (v1)
11   , _vertex2 (v2)
12   , _vertex3 (v3)
13 {
14 }
15 
cross() const16 glm::vec3 PrimTriangle::cross () const
17 {
18   return glm::cross (this->_vertex2 - this->_vertex1, this->_vertex3 - this->_vertex1);
19 }
20 
normal() const21 glm::vec3 PrimTriangle::normal () const
22 {
23   const glm::vec3 c = this->cross ();
24   const float     l = glm::length (c);
25 
26   return l > 0.0f ? (c / l) : glm::vec3 (0.0f);
27 }
28 
center() const29 glm::vec3 PrimTriangle::center () const
30 {
31   return (this->_vertex1 + this->_vertex2 + this->_vertex3) / glm::vec3 (3.0f);
32 }
33 
minimum() const34 glm::vec3 PrimTriangle::minimum () const
35 {
36   return glm::min (glm::min (this->_vertex1, this->_vertex2), this->_vertex3);
37 }
38 
maximum() const39 glm::vec3 PrimTriangle::maximum () const
40 {
41   return glm::max (glm::max (this->_vertex1, this->_vertex2), this->_vertex3);
42 }
43 
maxExtent() const44 float PrimTriangle::maxExtent () const { return glm::length (this->maximum () - this->minimum ()); }
45 
maxDimExtent() const46 float PrimTriangle::maxDimExtent () const
47 {
48   const glm::vec3 extent = this->maximum () - this->minimum ();
49   return glm::max (glm::max (extent.x, extent.y), extent.z);
50 }
51 
incircleRadiusSqr() const52 float PrimTriangle::incircleRadiusSqr () const
53 {
54   const float a = glm::distance (this->_vertex1, this->_vertex2);
55   const float b = glm::distance (this->_vertex2, this->_vertex3);
56   const float c = glm::distance (this->_vertex1, this->_vertex3);
57   const float s = 0.5f * (a + b + c);
58   return (s - a) * (s - b) * (s - c) / s;
59 }
60 
longestEdgeSqr() const61 float PrimTriangle::longestEdgeSqr () const
62 {
63   return glm::max (glm::max (glm::distance2 (this->_vertex1, this->_vertex2),
64                              glm::distance2 (this->_vertex2, this->_vertex3)),
65                    glm::distance2 (this->_vertex1, this->_vertex3));
66 }
67