Triangle3.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_TRIANGLE3_HPP
12 #define CUBBYFLOW_TRIANGLE3_HPP
13 
15 
16 namespace CubbyFlow
17 {
24 class Triangle3 final : public Surface3
25 {
26  public:
27  class Builder;
28 
30  Triangle3(const Transform3& _transform = Transform3{},
31  bool _isNormalFlipped = false);
32 
34  Triangle3(std::array<Vector3D, 3> points, std::array<Vector3D, 3> normals,
35  std::array<Vector2D, 3> uvs,
36  const Transform3& _transform = Transform3{},
37  bool _isNormalFlipped = false);
38 
40  Triangle3(const Triangle3&) = default;
41 
43  Triangle3(Triangle3&&) noexcept = default;
44 
46  ~Triangle3() override = default;
47 
49  Triangle3& operator=(const Triangle3&) = default;
50 
52  Triangle3& operator=(Triangle3&&) noexcept = default;
53 
55  [[nodiscard]] double Area() const;
56 
58  void GetBarycentricCoords(const Vector3D& pt, double* b0, double* b1,
59  double* b2) const;
60 
62  [[nodiscard]] Vector3D FaceNormal() const;
63 
66 
68  [[nodiscard]] static Builder GetBuilder();
69 
71  std::array<Vector3D, 3> points;
72 
74  std::array<Vector3D, 3> normals;
75 
77  std::array<Vector2D, 3> uvs;
78 
79  protected:
80  [[nodiscard]] Vector3D ClosestPointLocal(
81  const Vector3D& otherPoint) const override;
82 
83  [[nodiscard]] bool IntersectsLocal(const Ray3D& ray) const override;
84 
85  [[nodiscard]] BoundingBox3D BoundingBoxLocal() const override;
86 
87  [[nodiscard]] Vector3D ClosestNormalLocal(
88  const Vector3D& otherPoint) const override;
89 
91  const Ray3D& ray) const override;
92 };
93 
95 using Triangle3Ptr = std::shared_ptr<Triangle3>;
96 
100 class Triangle3::Builder final : public SurfaceBuilderBase3<Builder>
101 {
102  public:
104  [[nodiscard]] Builder& WithPoints(const std::array<Vector3D, 3>& _points);
105 
107  [[nodiscard]] Builder& WithNormals(const std::array<Vector3D, 3>& _normals);
108 
110  [[nodiscard]] Builder& WithUVs(const std::array<Vector2D, 3>& _uvs);
111 
113  [[nodiscard]] Triangle3 Build() const;
114 
116  [[nodiscard]] Triangle3Ptr MakeShared() const;
117 
118  private:
119  std::array<Vector3D, 3> m_points;
120  std::array<Vector3D, 3> m_normals;
121  std::array<Vector2D, 3> m_uvs;
122 };
123 } // namespace CubbyFlow
124 
125 #endif
Triangle3 & operator=(const Triangle3 &)=default
Default copy assignment operator.
std::array< Vector3D, 3 > points
Three points.
Definition: Triangle3.hpp:71
std::array< Vector3D, 3 > normals
Three normals.
Definition: Triangle3.hpp:74
Class for N-D ray.
Definition: Ray.hpp:25
BoundingBox3D BoundingBoxLocal() const override
Returns the bounding box of this surface object in local frame.
~Triangle3() override=default
Default virtual destructor.
Triangle3(const Transform3 &_transform=Transform3{}, bool _isNormalFlipped=false)
Constructs an empty triangle.
std::array< Vector2D, 3 > uvs
Three UV coordinates.
Definition: Triangle3.hpp:77
N-D axis-aligned bounding box class.
Definition: BoundingBox.hpp:46
void GetBarycentricCoords(const Vector3D &pt, double *b0, double *b1, double *b2) const
Returns barycentric coordinates for the given point pt.
static Builder GetBuilder()
Returns builder fox Triangle3.
void SetNormalsToFaceNormal()
Set Triangle3::normals to the face normal.
Represents N-D rigid body transform.
Definition: Transform.hpp:82
Definition: Matrix.hpp:27
Definition: pybind11Utils.hpp:20
Abstract base class for N-D surface.
Definition: Surface.hpp:38
bool IntersectsLocal(const Ray3D &ray) const override
SurfaceRayIntersection3 ClosestIntersectionLocal(const Ray3D &ray) const override
Front-end to create Triangle3 objects step by step.
Definition: Triangle3.hpp:100
Struct that represents ray-surface intersection point.
Definition: Surface.hpp:25
double Area() const
Returns the area of this triangle.
3-D triangle geometry.
Definition: Triangle3.hpp:24
Vector3D ClosestPointLocal(const Vector3D &otherPoint) const override
std::shared_ptr< Triangle3 > Triangle3Ptr
Shared pointer for the Triangle3 type.
Definition: Triangle3.hpp:95
Vector3D ClosestNormalLocal(const Vector3D &otherPoint) const override
Base class for N-D surface builder.
Definition: Surface.hpp:153
Vector3D FaceNormal() const
Returns the face normal of the triangle.