Triangle3.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Triangle3.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 3-D triangle geometry.
6 > Created Time: 2017/04/04
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_TRIANGLE3_H
10 #define CUBBYFLOW_TRIANGLE3_H
11 
12 #include <Core/Surface/Surface3.h>
13 
14 namespace CubbyFlow
15 {
22  class Triangle3 final : public Surface3
23  {
24  public:
25  class Builder;
26 
28  std::array<Vector3D, 3> points;
29 
31  std::array<Vector3D, 3> normals;
32 
34  std::array<Vector2D, 3> uvs;
35 
37  Triangle3(
38  const Transform3& transform = Transform3(),
39  bool isNormalFlipped = false);
40 
42  Triangle3(
43  const std::array<Vector3D, 3>& points,
44  const std::array<Vector3D, 3>& normals,
45  const std::array<Vector2D, 3>& uvs,
46  const Transform3& transform = Transform3(),
47  bool isNormalFlipped = false);
48 
50  Triangle3(const Triangle3& other);
51 
53  double Area() const;
54 
57  const Vector3D& pt,
58  double* b0,
59  double* b1,
60  double* b2) const;
61 
63  Vector3D FaceNormal() const;
64 
67 
69  static Builder GetBuilder();
70 
71  protected:
72  Vector3D ClosestPointLocal(const Vector3D& otherPoint) const override;
73 
74  bool IntersectsLocal(const Ray3D& ray) const override;
75 
76  BoundingBox3D BoundingBoxLocal() const override;
77 
78  Vector3D ClosestNormalLocal(const Vector3D& otherPoint) const override;
79 
80  SurfaceRayIntersection3 ClosestIntersectionLocal(const Ray3D& ray) const override;
81  };
82 
84  using Triangle3Ptr = std::shared_ptr<Triangle3>;
85 
89  class Triangle3::Builder final : public SurfaceBuilderBase3<Triangle3::Builder>
90  {
91  public:
93  Builder& WithPoints(const std::array<Vector3D, 3>& points);
94 
96  Builder& WithNormals(const std::array<Vector3D, 3>& normals);
97 
99  Builder& WithUVs(const std::array<Vector2D, 3>& uvs);
100 
102  Triangle3 Build() const;
103 
105  Triangle3Ptr MakeShared() const;
106 
107  private:
108  std::array<Vector3D, 3> m_points;
109  std::array<Vector3D, 3> m_normals;
110  std::array<Vector2D, 3> m_uvs;
111  };
112 }
113 
114 #endif
3-D vector class.
Definition: Vector3.h:26
std::array< Vector3D, 3 > points
Three points.
Definition: Triangle3.h:25
Builder & WithNormals(const std::array< Vector3D, 3 > &normals)
Returns builder with normals.
std::array< Vector3D, 3 > normals
Three normals.
Definition: Triangle3.h:31
BoundingBox3D BoundingBoxLocal() const override
Returns the bounding box of this surface object in local frame.
Triangle3Ptr MakeShared() const
Builds shared pointer of Triangle3 instance.
std::array< Vector2D, 3 > uvs
Three UV coordinates.
Definition: Triangle3.h:34
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.
Structure that represents ray-surface intersection point.
Definition: Surface3.h:23
void SetNormalsToFaceNormal()
Set Triangle3::normals to the face normal.
Triangle3 Build() const
Builds Triangle3.
Base class for 3-D surface builder.
Definition: Surface3.h:106
Definition: pybind11Utils.h:24
Triangle3(const Transform3 &transform=Transform3(), bool isNormalFlipped=false)
Constructs an empty triangle.
Abstract base class for 3-D surface.
Definition: Surface3.h:32
Builder & WithUVs(const std::array< Vector2D, 3 > &uvs)
Returns builder with uvs.
Transform3 transform
Local-to-world transform.
Definition: Surface3.h:36
bool IntersectsLocal(const Ray3D &ray) const override
SurfaceRayIntersection3 ClosestIntersectionLocal(const Ray3D &ray) const override
Returns the closest intersection point for given ray in local frame.
Front-end to create Triangle3 objects step by step.
Definition: Triangle3.h:89
3-D axis-aligned bounding box class.
Definition: BoundingBox3.h:44
Builder & WithPoints(const std::array< Vector3D, 3 > &points)
Returns builder with points.
Represents 3-D rigid body transform.
Definition: Transform3.h:22
bool isNormalFlipped
Flips normal when calling Surface3::closestNormal(...).
Definition: Surface3.h:39
double Area() const
Returns the area of this triangle.
3-D triangle geometry.
Definition: Triangle3.h:22
Vector3D ClosestPointLocal(const Vector3D &otherPoint) const override
std::shared_ptr< Triangle3 > Triangle3Ptr
Shared pointer for the Triangle3 type.
Definition: Triangle3.h:84
Class for 3-D ray.
Definition: Ray3.h:23
Vector3D ClosestNormalLocal(const Vector3D &otherPoint) const override
Vector3D FaceNormal() const
Returns the face normal of the triangle.