Jet  v1.3.3
triangle_mesh3.h
Go to the documentation of this file.
1 // Copyright (c) 2019 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_TRIANGLE_MESH3_H_
8 #define INCLUDE_JET_TRIANGLE_MESH3_H_
9 
10 #include <jet/array1.h>
11 #include <jet/bvh3.h>
12 #include <jet/point3.h>
13 #include <jet/quaternion.h>
14 #include <jet/surface3.h>
15 #include <jet/triangle3.h>
16 
17 #include <iostream>
18 
19 namespace jet {
20 
28 class TriangleMesh3 final : public Surface3 {
29  public:
30  class Builder;
31 
35 
39 
42  bool isNormalFlipped = false);
43 
45  TriangleMesh3(const PointArray& points, const NormalArray& normals,
46  const UvArray& uvs, const IndexArray& pointIndices,
47  const IndexArray& normalIndices, const IndexArray& uvIndices,
48  const Transform3& transform_ = Transform3(),
49  bool isNormalFlipped = false);
50 
53 
55  void updateQueryEngine() override;
56 
58  void updateQueryEngine() const;
59 
61  void clear();
62 
64  void set(const TriangleMesh3& other);
65 
67  void swap(TriangleMesh3& other);
68 
70  double area() const;
71 
73  double volume() const;
74 
76  const Vector3D& point(size_t i) const;
77 
79  Vector3D& point(size_t i);
80 
82  const Vector3D& normal(size_t i) const;
83 
85  Vector3D& normal(size_t i);
86 
88  const Vector2D& uv(size_t i) const;
89 
91  Vector2D& uv(size_t i);
92 
94  const Point3UI& pointIndex(size_t i) const;
95 
97  Point3UI& pointIndex(size_t i);
98 
100  const Point3UI& normalIndex(size_t i) const;
101 
103  Point3UI& normalIndex(size_t i);
104 
106  const Point3UI& uvIndex(size_t i) const;
107 
109  Point3UI& uvIndex(size_t i);
110 
112  Triangle3 triangle(size_t i) const;
113 
115  size_t numberOfPoints() const;
116 
118  size_t numberOfNormals() const;
119 
121  size_t numberOfUvs() const;
122 
124  size_t numberOfTriangles() const;
125 
127  bool hasNormals() const;
128 
130  bool hasUvs() const;
131 
133  void addPoint(const Vector3D& pt);
134 
136  void addNormal(const Vector3D& n);
137 
139  void addUv(const Vector2D& t);
140 
142  void addPointTriangle(const Point3UI& newPointIndices);
143 
145  void addNormalTriangle(const Point3UI& newNormalIndices);
146 
148  void addUvTriangle(const Point3UI& newUvIndices);
149 
151  void addPointNormalTriangle(const Point3UI& newPointIndices,
152  const Point3UI& newNormalIndices);
153 
155  void addPointUvNormalTriangle(const Point3UI& newPointIndices,
156  const Point3UI& newUvIndices,
157  const Point3UI& newNormalIndices);
158 
160  void addPointUvTriangle(const Point3UI& newPointIndices,
161  const Point3UI& newUvIndices);
162 
164  void addTriangle(const Triangle3& tri);
165 
168 
171 
173  void scale(double factor);
174 
176  void translate(const Vector3D& t);
177 
179  void rotate(const QuaternionD& q);
180 
182  void writeObj(std::ostream* strm) const;
183 
185  bool writeObj(const std::string& filename) const;
186 
188  bool readObj(std::istream* strm);
189 
191  bool readObj(const std::string& filename);
192 
195 
197  static Builder builder();
198 
199  protected:
200  Vector3D closestPointLocal(const Vector3D& otherPoint) const override;
201 
202  double closestDistanceLocal(const Vector3D& otherPoint) const override;
203 
204  bool intersectsLocal(const Ray3D& ray) const override;
205 
206  BoundingBox3D boundingBoxLocal() const override;
207 
208  Vector3D closestNormalLocal(const Vector3D& otherPoint) const override;
209 
211  const Ray3D& ray) const override;
212 
213  bool isInsideLocal(const Vector3D& otherPoint) const override;
214 
215  private:
216  PointArray _points;
217  NormalArray _normals;
218  UvArray _uvs;
219  IndexArray _pointIndices;
220  IndexArray _normalIndices;
221  IndexArray _uvIndices;
222 
223  mutable Bvh3<size_t> _bvh;
224  mutable bool _bvhInvalidated = true;
225 
226  mutable Array1<Vector3D> _wnAreaWeightedNormalSums;
227  mutable Array1<Vector3D> _wnAreaWeightedAvgPositions;
228  mutable bool _wnInvalidated = true;
229 
230  void invalidateCache();
231 
232  void buildBvh() const;
233 
234  void buildWindingNumbers() const;
235 
236  double windingNumber(const Vector3D& queryPoint, size_t triIndex) const;
237 
238  double fastWindingNumber(const Vector3D& queryPoint, double accuracy) const;
239 
240  double fastWindingNumber(const Vector3D& queryPoint, size_t rootNodeIndex,
241  double accuracy) const;
242 };
243 
245 typedef std::shared_ptr<TriangleMesh3> TriangleMesh3Ptr;
246 
251  : public SurfaceBuilderBase3<TriangleMesh3::Builder> {
252  public:
254  Builder& withPoints(const PointArray& points);
255 
257  Builder& withNormals(const NormalArray& normals);
258 
260  Builder& withUvs(const UvArray& uvs);
261 
263  Builder& withPointIndices(const IndexArray& pointIndices);
264 
266  Builder& withNormalIndices(const IndexArray& normalIndices);
267 
269  Builder& withUvIndices(const IndexArray& uvIndices);
270 
273 
276 
277  private:
278  PointArray _points;
279  NormalArray _normals;
280  UvArray _uvs;
281  IndexArray _pointIndices;
282  IndexArray _normalIndices;
283  IndexArray _uvIndices;
284 };
285 
286 } // namespace jet
287 
288 #endif // INCLUDE_JET_TRIANGLE_MESH3_H_
jet::Array< T, 1 >
1-D array class.
Definition: array1.h:31
jet::TriangleMesh3::intersectsLocal
bool intersectsLocal(const Ray3D &ray) const override
jet::TriangleMesh3::normal
Vector3D & normal(size_t i)
Returns reference to the i-th normal.
jet::TriangleMesh3::IndexArray
Array1< Point3UI > IndexArray
Definition: triangle_mesh3.h:34
jet::TriangleMesh3::rotate
void rotate(const QuaternionD &q)
Rotates the mesh.
jet::TriangleMesh3::swap
void swap(TriangleMesh3 &other)
Swaps the contents with other mesh.
jet::TriangleMesh3::builder
static Builder builder()
Returns builder fox TriangleMesh3.
jet::TriangleMesh3::Builder::makeShared
TriangleMesh3Ptr makeShared() const
Builds shared pointer of TriangleMesh3 instance.
jet::TriangleMesh3::setAngleWeightedVertexNormal
void setAngleWeightedVertexNormal()
Sets angle weighted vertex normal.
jet::TriangleMesh3::Vector2DArray
Array1< Vector2D > Vector2DArray
Definition: triangle_mesh3.h:30
jet::TriangleMesh3::addPoint
void addPoint(const Vector3D &pt)
Adds a point.
jet::TriangleMesh3::closestNormalLocal
Vector3D closestNormalLocal(const Vector3D &otherPoint) const override
jet::TriangleMesh3::TriangleMesh3
TriangleMesh3(const PointArray &points, const NormalArray &normals, const UvArray &uvs, const IndexArray &pointIndices, const IndexArray &normalIndices, const IndexArray &uvIndices, const Transform3 &transform_=Transform3(), bool isNormalFlipped=false)
Constructs mesh with points, normals, uvs, and their indices.
jet::TriangleMesh3::numberOfNormals
size_t numberOfNormals() const
Returns number of normals.
jet::TriangleMesh3::writeObj
void writeObj(std::ostream *strm) const
Writes the mesh in obj format to the output stream.
jet::TriangleMesh3::Builder::withUvs
Builder & withUvs(const UvArray &uvs)
Returns builder with uvs.
jet::TriangleMesh3::translate
void translate(const Vector3D &t)
Translates the mesh.
surface3.h
jet::BoundingBox< T, 3 >
3-D axis-aligned bounding box class.
Definition: bounding_box3.h:41
quaternion.h
jet::TriangleMesh3::Builder
Front-end to create TriangleMesh3 objects step by step.
Definition: triangle_mesh3.h:251
jet::TriangleMesh3::uvIndex
Point3UI & uvIndex(size_t i)
Returns reference to the UV indices of i-th triangle.
jet::TriangleMesh3::readObj
bool readObj(std::istream *strm)
Reads the mesh in obj format from the input stream.
jet::TriangleMesh3::hasUvs
bool hasUvs() const
Returns true if the mesh has UV coordinates.
jet::TriangleMesh3::Builder::withUvIndices
Builder & withUvIndices(const IndexArray &uvIndices)
Returns builder with uv indices.
jet::TriangleMesh3::area
double area() const
Returns area of this mesh.
jet::SurfaceBuilderBase3
Base class for 3-D surface builder.
Definition: surface3.h:115
jet::TriangleMesh3::normalIndex
const Point3UI & normalIndex(size_t i) const
Returns constant reference to the normal indices of i-th triangle.
jet::TriangleMesh3
3-D triangle mesh geometry.
Definition: triangle_mesh3.h:28
jet::Surface3::isNormalFlipped
bool isNormalFlipped
Flips normal when calling Surface3::closestNormal(...).
Definition: surface3.h:33
jet::TriangleMesh3::addNormal
void addNormal(const Vector3D &n)
Adds a normal.
jet::TriangleMesh3::isInsideLocal
bool isInsideLocal(const Vector3D &otherPoint) const override
jet::TriangleMesh3::NormalArray
Vector3DArray NormalArray
Definition: triangle_mesh3.h:37
jet::TriangleMesh3::addPointNormalTriangle
void addPointNormalTriangle(const Point3UI &newPointIndices, const Point3UI &newNormalIndices)
Adds a triangle with point and normal.
jet::Quaternion< double >
point3.h
jet::TriangleMesh3::numberOfUvs
size_t numberOfUvs() const
Returns number of UV coordinates.
jet::TriangleMesh3::addPointTriangle
void addPointTriangle(const Point3UI &newPointIndices)
Adds a triangle with points.
jet::TriangleMesh3::UvArray
Vector2DArray UvArray
Definition: triangle_mesh3.h:38
jet::TriangleMesh3::Builder::withPointIndices
Builder & withPointIndices(const IndexArray &pointIndices)
Returns builder with point indices.
jet
Definition: advection_solver2.h:18
jet::TriangleMesh3::Builder::build
TriangleMesh3 build() const
Builds TriangleMesh3.
jet::TriangleMesh3::scale
void scale(double factor)
Scales the mesh by given factor.
jet::TriangleMesh3::addPointUvNormalTriangle
void addPointUvNormalTriangle(const Point3UI &newPointIndices, const Point3UI &newUvIndices, const Point3UI &newNormalIndices)
Adds a triangle with point, normal, and UV.
jet::TriangleMesh3::addUv
void addUv(const Vector2D &t)
Adds a UV.
jet::TriangleMesh3::updateQueryEngine
void updateQueryEngine() override
Updates internal spatial query engine.
jet::TriangleMesh3::clear
void clear()
Clears all content.
jet::TriangleMesh3::TriangleMesh3
TriangleMesh3(const TriangleMesh3 &other)
Copy constructor.
jet::TriangleMesh3::Builder::withNormals
Builder & withNormals(const NormalArray &normals)
Returns builder with normals.
jet::TriangleMesh3::point
const Vector3D & point(size_t i) const
Returns constant reference to the i-th point.
jet::TriangleMesh3::Builder::withPoints
Builder & withPoints(const PointArray &points)
Returns builder with points.
jet::Vector< T, 2 >
2-D vector class.
Definition: vector2.h:24
jet::Bvh3< size_t >
jet::TriangleMesh3::closestPointLocal
Vector3D closestPointLocal(const Vector3D &otherPoint) const override
bvh3.h
jet::TriangleMesh3::updateQueryEngine
void updateQueryEngine() const
Updates internal spatial query engine.
jet::TriangleMesh3::addPointUvTriangle
void addPointUvTriangle(const Point3UI &newPointIndices, const Point3UI &newUvIndices)
Adds a triangle with point and UV.
jet::TriangleMesh3::Builder::withNormalIndices
Builder & withNormalIndices(const IndexArray &normalIndices)
Returns builder with normal indices.
jet::Surface3::transform
Transform3 transform
Local-to-world transform.
Definition: surface3.h:30
jet::TriangleMesh3::numberOfPoints
size_t numberOfPoints() const
Returns number of points.
jet::TriangleMesh3::addNormalTriangle
void addNormalTriangle(const Point3UI &newNormalIndices)
Adds a triangle with normal.
jet::Ray< T, 3 >
Class for 2-D ray.
Definition: ray3.h:21
jet::TriangleMesh3::pointIndex
Point3UI & pointIndex(size_t i)
Returns reference to the point indices of i-th triangle.
jet::TriangleMesh3::closestDistanceLocal
double closestDistanceLocal(const Vector3D &otherPoint) const override
jet::Point< T, 3 >
3-D point class.
Definition: point3.h:23
jet::TriangleMesh3::pointIndex
const Point3UI & pointIndex(size_t i) const
Returns constant reference to the point indices of i-th triangle.
jet::TriangleMesh3::writeObj
bool writeObj(const std::string &filename) const
Writes the mesh in obj format to the file.
jet::SurfaceRayIntersection3
Struct that represents ray-surface intersection point.
Definition: surface3.h:19
jet::TriangleMesh3::setFaceNormal
void setFaceNormal()
Sets entire normals to the face normals.
jet::TriangleMesh3::addTriangle
void addTriangle(const Triangle3 &tri)
Add a triangle.
jet::TriangleMesh3::volume
double volume() const
Returns volume of this mesh.
jet::TriangleMesh3::normal
const Vector3D & normal(size_t i) const
Returns constant reference to the i-th normal.
jet::TriangleMesh3::readObj
bool readObj(const std::string &filename)
Reads the mesh in obj format from the file.
jet::Vector< T, 3 >
3-D vector class.
Definition: vector3.h:25
jet::TriangleMesh3::normalIndex
Point3UI & normalIndex(size_t i)
Returns reference to the normal indices of i-th triangle.
jet::Triangle3
3-D triangle geometry.
Definition: triangle3.h:20
triangle3.h
jet::TriangleMesh3::set
void set(const TriangleMesh3 &other)
Copies the contents from other mesh.
jet::TriangleMesh3::triangle
Triangle3 triangle(size_t i) const
Returns i-th triangle.
jet::TriangleMesh3::Vector3DArray
Array1< Vector3D > Vector3DArray
Definition: triangle_mesh3.h:33
jet::TriangleMesh3::closestIntersectionLocal
SurfaceRayIntersection3 closestIntersectionLocal(const Ray3D &ray) const override
Returns the closest intersection point for given ray in local frame.
jet::TriangleMesh3Ptr
std::shared_ptr< TriangleMesh3 > TriangleMesh3Ptr
Shared pointer for the TriangleMesh3 type.
Definition: triangle_mesh3.h:245
jet::TriangleMesh3::numberOfTriangles
size_t numberOfTriangles() const
Returns number of triangles.
jet::TriangleMesh3::uv
const Vector2D & uv(size_t i) const
Returns constant reference to the i-th UV coordinates.
jet::Transform3
Represents 3-D rigid body transform.
Definition: transform3.h:20
jet::TriangleMesh3::TriangleMesh3
TriangleMesh3(const Transform3 &transform=Transform3(), bool isNormalFlipped=false)
Default constructor.
jet::TriangleMesh3::addUvTriangle
void addUvTriangle(const Point3UI &newUvIndices)
Adds a triangle with UV.
jet::TriangleMesh3::PointArray
Vector3DArray PointArray
Definition: triangle_mesh3.h:36
jet::Surface3
Abstract base class for 3-D surface.
Definition: surface3.h:27
jet::TriangleMesh3::hasNormals
bool hasNormals() const
Returns true if the mesh has normals.
jet::TriangleMesh3::operator=
TriangleMesh3 & operator=(const TriangleMesh3 &other)
Copies other mesh.
jet::TriangleMesh3::point
Vector3D & point(size_t i)
Returns reference to the i-th point.
jet::TriangleMesh3::boundingBoxLocal
BoundingBox3D boundingBoxLocal() const override
Returns the bounding box of this surface object in local frame.
array1.h
jet::TriangleMesh3::uvIndex
const Point3UI & uvIndex(size_t i) const
Returns constant reference to the UV indices of i-th triangle.
jet::TriangleMesh3::uv
Vector2D & uv(size_t i)
Returns reference to the i-th UV coordinates.