TriangleMesh3.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: TriangleMesh3.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 3-D triangle mesh geometry.
6 > Created Time: 2017/04/04
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_TRIANGLE_MESH3_H
10 #define CUBBYFLOW_TRIANGLE_MESH3_H
11 
12 #include <Core/Array/Array1.h>
13 #include <Core/Geometry/BVH3.h>
15 #include <Core/Point/Point3.h>
16 #include <Core/Surface/Surface3.h>
17 
18 namespace CubbyFlow
19 {
27  class TriangleMesh3 final : public Surface3
28  {
29  public:
30  class Builder;
31 
35 
39 
42  const Transform3& transform = Transform3(),
43  bool isNormalFlipped = false);
44 
47  const PointArray& points,
48  const NormalArray& normals,
49  const UVArray& uvs,
50  const IndexArray& pointIndices,
51  const IndexArray& normalIndices,
52  const IndexArray& uvIndices,
53  const Transform3& transform_ = Transform3(),
54  bool isNormalFlipped_ = false);
55 
57  TriangleMesh3(const TriangleMesh3& other);
58 
60  void UpdateQueryEngine() override;
61 
63  void Clear();
64 
66  void Set(const TriangleMesh3& other);
67 
69  void Swap(TriangleMesh3& other);
70 
72  double Area() const;
73 
75  double Volume() const;
76 
78  const Vector3D& Point(size_t i) const;
79 
81  Vector3D& Point(size_t i);
82 
84  const Vector3D& Normal(size_t i) const;
85 
87  Vector3D& Normal(size_t i);
88 
90  const Vector2D& UV(size_t i) const;
91 
93  Vector2D& UV(size_t i);
94 
96  const Point3UI& PointIndex(size_t i) const;
97 
99  Point3UI& PointIndex(size_t i);
100 
102  const Point3UI& NormalIndex(size_t i) const;
103 
105  Point3UI& NormalIndex(size_t i);
106 
108  const Point3UI& UVIndex(size_t i) const;
109 
111  Point3UI& UVIndex(size_t i);
112 
114  Triangle3 Triangle(size_t i) const;
115 
117  size_t NumberOfPoints() const;
118 
120  size_t NumberOfNormals() const;
121 
123  size_t NumberOfUVs() const;
124 
126  size_t NumberOfTriangles() const;
127 
129  bool HasNormals() const;
130 
132  bool HasUVs() const;
133 
135  void AddPoint(const Vector3D& pt);
136 
138  void AddNormal(const Vector3D& n);
139 
141  void AddUV(const Vector2D& t);
142 
144  void AddPointTriangle(const Point3UI& newPointIndices);
145 
148  const Point3UI& newPointIndices,
149  const Point3UI& newNormalIndices);
150 
153  const Point3UI& newPointIndices,
154  const Point3UI& newUVIndices,
155  const Point3UI& newNormalIndices);
156 
158  void AddPointUVTriangle(
159  const Point3UI& newPointIndices,
160  const Point3UI& newUVIndices);
161 
163  void AddTriangle(const Triangle3& tri);
164 
166  void SetFaceNormal();
167 
170 
172  void Scale(double factor);
173 
175  void Translate(const Vector3D& t);
176 
178  void Rotate(const QuaternionD& q);
179 
181  void WriteObj(std::ostream* stream) const;
182 
184  bool WriteObj(const std::string& fileName) const;
185 
187  bool ReadObj(std::istream* stream);
188 
190  bool ReadObj(const std::string& fileName);
191 
193  TriangleMesh3& operator=(const TriangleMesh3& other);
194 
196  static Builder GetBuilder();
197 
198  protected:
199  Vector3D ClosestPointLocal(const Vector3D& otherPoint) const override;
200 
201  double ClosestDistanceLocal(const Vector3D& otherPoint) const override;
202 
203  bool IntersectsLocal(const Ray3D& ray) const override;
204 
205  BoundingBox3D BoundingBoxLocal() const override;
206 
207  Vector3D ClosestNormalLocal(const Vector3D& otherPoint) const override;
208 
209  SurfaceRayIntersection3 ClosestIntersectionLocal(const Ray3D& ray) const override;
210 
211  private:
212  PointArray m_points;
213  NormalArray m_normals;
214  UVArray m_uvs;
215  IndexArray m_pointIndices;
216  IndexArray m_normalIndices;
217  IndexArray m_uvIndices;
218 
219  mutable BVH3<size_t> m_bvh;
220  mutable bool m_bvhInvalidated = true;
221 
222  void InvalidateBVH() const;
223 
224  void BuildBVH() const;
225  };
226 
228  using TriangleMesh3Ptr = std::shared_ptr<TriangleMesh3>;
229 
233  class TriangleMesh3::Builder final : public SurfaceBuilderBase3<TriangleMesh3::Builder>
234  {
235  public:
237  Builder& WithPoints(const PointArray& points);
238 
240  Builder& WithNormals(const NormalArray& normals);
241 
243  Builder& WithUVs(const UVArray& uvs);
244 
246  Builder& WithPointIndices(const IndexArray& pointIndices);
247 
249  Builder& WithNormalIndices(const IndexArray& normalIndices);
250 
252  Builder& WithUVIndices(const IndexArray& uvIndices);
253 
255  TriangleMesh3 Build() const;
256 
259 
260  private:
261  PointArray m_points;
262  NormalArray m_normals;
263  UVArray m_uvs;
264  IndexArray m_pointIndices;
265  IndexArray m_normalIndices;
266  IndexArray m_uvIndices;
267  };
268 }
269 
270 #endif
double Volume() const
Returns volume of this mesh.
3-D vector class.
Definition: Vector3.h:26
const Point3UI & UVIndex(size_t i) const
Returns constant reference to the UV indices of i-th triangle.
void AddUV(const Vector2D &t)
Adds a UV.
void AddPointTriangle(const Point3UI &newPointIndices)
Adds a triangle with points.
Vector3D ClosestPointLocal(const Vector3D &otherPoint) const override
const Point3UI & NormalIndex(size_t i) const
Returns constant reference to the normal indices of i-th triangle.
size_t NumberOfTriangles() const
Returns number of triangles.
double ClosestDistanceLocal(const Vector3D &otherPoint) const override
const Point3UI & PointIndex(size_t i) const
Returns constant reference to the point indices of i-th triangle.
void AddTriangle(const Triangle3 &tri)
Add a triangle.
void AddPoint(const Vector3D &pt)
Adds a point.
SurfaceRayIntersection3 ClosestIntersectionLocal(const Ray3D &ray) const override
Returns the closest intersection point for given ray in local frame.
double Area() const
Returns area of this mesh.
void Clear()
Clears all content.
size_t NumberOfUVs() const
Returns number of UV coordinates.
Builder & WithUVIndices(const IndexArray &uvIndices)
Returns builder with uv indices.
Builder & WithNormals(const NormalArray &normals)
Returns builder with normals.
Builder & WithPointIndices(const IndexArray &pointIndices)
Returns builder with point indices.
TriangleMesh3 & operator=(const TriangleMesh3 &other)
Copies other mesh.
BoundingBox3D BoundingBoxLocal() const override
Returns the bounding box of this surface object in local frame.
bool ReadObj(std::istream *stream)
Reads the mesh in obj format from the input stream.
Builder & WithUVs(const UVArray &uvs)
Returns builder with uvs.
3-D point class.
Definition: Point3.h:26
const Vector2D & UV(size_t i) const
Returns constant reference to the i-th UV coordinates.
void SetFaceNormal()
Sets entire normals to the face normals.
Array1< Vector3D > Vector3DArray
Definition: TriangleMesh3.h:33
const Vector3D & Normal(size_t i) const
Returns constant reference to the i-th normal.
void WriteObj(std::ostream *stream) const
Writes the mesh in obj format to the output stream.
Structure that represents ray-surface intersection point.
Definition: Surface3.h:23
Front-end to create TriangleMesh3 objects step by step.
Definition: TriangleMesh3.h:233
void UpdateQueryEngine() override
Updates internal spatial query engine.
void Scale(double factor)
Scales the mesh by given factor.
Builder & WithNormalIndices(const IndexArray &normalIndices)
Returns builder with normal indices.
1-D array class.
Definition: Array1.h:29
static Builder GetBuilder()
Returns builder fox TriangleMesh3.
Base class for 3-D surface builder.
Definition: Surface3.h:106
Definition: pybind11Utils.h:24
TriangleMesh3(const Transform3 &transform=Transform3(), bool isNormalFlipped=false)
Default constructor.
void AddPointUVTriangle(const Point3UI &newPointIndices, const Point3UI &newUVIndices)
Adds a triangle with point and UV.
std::shared_ptr< TriangleMesh3 > TriangleMesh3Ptr
Shared pointer for the TriangleMesh3 type.
Definition: TriangleMesh3.h:228
Array1< Vector2D > Vector2DArray
Definition: TriangleMesh3.h:32
bool HasUVs() const
Returns true if the mesh has UV coordinates.
Abstract base class for 3-D surface.
Definition: Surface3.h:32
Transform3 transform
Local-to-world transform.
Definition: Surface3.h:36
3-D axis-aligned bounding box class.
Definition: BoundingBox3.h:44
void AddNormal(const Vector3D &n)
Adds a normal.
void Swap(TriangleMesh3 &other)
Swaps the contents with other mesh.
Represents 3-D rigid body transform.
Definition: Transform3.h:22
Vector3D ClosestNormalLocal(const Vector3D &otherPoint) const override
void Translate(const Vector3D &t)
Translates the mesh.
bool isNormalFlipped
Flips normal when calling Surface3::closestNormal(...).
Definition: Surface3.h:39
TriangleMesh3Ptr MakeShared() const
Builds shared pointer of TriangleMesh3 instance.
Builder & WithPoints(const PointArray &points)
Returns builder with points.
const Vector3D & Point(size_t i) const
Returns constant reference to the i-th point.
2-D vector class.
Definition: Vector2.h:26
3-D triangle geometry.
Definition: Triangle3.h:22
void SetAngleWeightedVertexNormal()
Sets angle weighted vertex normal.
void AddPointNormalTriangle(const Point3UI &newPointIndices, const Point3UI &newNormalIndices)
Adds a triangle with point and normal.
TriangleMesh3 Build() const
Builds TriangleMesh3.
size_t NumberOfNormals() const
Returns number of normals.
Triangle3 Triangle(size_t i) const
Returns i-th triangle.
size_t NumberOfPoints() const
Returns number of points.
void AddPointUVNormalTriangle(const Point3UI &newPointIndices, const Point3UI &newUVIndices, const Point3UI &newNormalIndices)
Adds a triangle with point, normal, and UV.
void Set(const TriangleMesh3 &other)
Copies the contents from other mesh.
Class for 3-D ray.
Definition: Ray3.h:23
bool IntersectsLocal(const Ray3D &ray) const override
bool HasNormals() const
Returns true if the mesh has normals.
3-D triangle mesh geometry.
Definition: TriangleMesh3.h:27
void Rotate(const QuaternionD &q)
Rotates the mesh.