SPH
Triangle.h
Go to the documentation of this file.
1 #pragma once
2 
7 
9 #include "objects/geometry/Box.h"
10 
12 
14 class Triangle {
15 private:
17 
18 public:
19  Triangle() = default;
20 
21  Triangle(const Vector& v1, const Vector& v2, const Vector& v3)
22  : v{ v1, v2, v3 } {
24  }
25 
26  Triangle(const Triangle& other)
27  : v{ other[0], other[1], other[2] } {}
28 
29  Triangle& operator=(const Triangle& other) {
30  v[0] = other[0];
31  v[1] = other[1];
32  v[2] = other[2];
33  return *this;
34  }
35 
36  INLINE Vector& operator[](const Size idx) {
37  return v[idx];
38  }
39 
40  INLINE const Vector& operator[](const Size idx) const {
41  return v[idx];
42  }
43 
44  INLINE Vector center() const {
45  return (v[0] + v[1] + v[2]) / 3._f;
46  }
47 
48  INLINE Vector normal() const {
49  SPH_ASSERT(this->isValid());
50  const Vector v12 = v[2] - v[1];
51  const Vector v02 = v[2] - v[0];
52  return getNormalized(cross(v12, v02));
53  }
54 
55  INLINE Float area() const {
56  SPH_ASSERT(this->isValid());
57  const Vector v12 = v[2] - v[1];
58  const Vector v02 = v[2] - v[0];
59  return 0.5_f * getLength(cross(v12, v02));
60  }
61 
62  INLINE Box getBBox() const {
63  Box box;
64  for (Size i = 0; i < 3; ++i) {
65  box.extend(v[i]);
66  }
67  return box;
68  }
69 
70  INLINE bool isValid() const {
71  if (!isReal(v[0]) || !isReal(v[1]) || !isReal(v[2])) {
72  return false;
73  }
74  const Vector v12 = v[2] - v[1];
75  const Vector v02 = v[2] - v[0];
76  return sqr(dot(v12, v02)) < (1._f - EPS) * getSqrLength(v12) * getSqrLength(v02);
77  }
78 };
79 
INLINE bool isReal(const AntisymmetricTensor &t)
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Object representing a three-dimensional axis-aligned box.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr Float EPS
Definition: MathUtils.h:30
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
INLINE Float getLength(const Vector &v)
Returns the length of the vector. Enabled only for vectors of floating-point precision.
Definition: Vector.h:579
INLINE Float getSqrLength(const Vector &v)
Definition: Vector.h:574
INLINE BasicVector< float > cross(const BasicVector< float > &v1, const BasicVector< float > &v2)
Cross product between two vectors.
Definition: Vector.h:559
INLINE float dot(const BasicVector< float > &v1, const BasicVector< float > &v2)
Make sure the vector is trivially constructible and destructible, needed for fast initialization of a...
Definition: Vector.h:548
INLINE Vector getNormalized(const Vector &v)
Definition: Vector.h:590
Helper object defining three-dimensional interval (box).
Definition: Box.h:17
INLINE void extend(const Vector &v)
Enlarges the box to contain the vector.
Definition: Box.h:49
Array with fixed number of allocated elements.
Definition: StaticArray.h:19
Triangle.h.
Definition: Triangle.h:14
Triangle(const Vector &v1, const Vector &v2, const Vector &v3)
Definition: Triangle.h:21
INLINE const Vector & operator[](const Size idx) const
Definition: Triangle.h:40
INLINE Vector & operator[](const Size idx)
Definition: Triangle.h:36
Triangle(const Triangle &other)
Definition: Triangle.h:26
INLINE bool isValid() const
Definition: Triangle.h:70
Triangle & operator=(const Triangle &other)
Definition: Triangle.h:29
INLINE Box getBBox() const
Definition: Triangle.h:62
INLINE Vector normal() const
Definition: Triangle.h:48
INLINE Vector center() const
Definition: Triangle.h:44
Triangle()=default
INLINE Float area() const
Definition: Triangle.h:55