Nori  24
mesh.h
1 /*
2  This file is part of Nori, a simple educational ray tracer
3 
4  Copyright (c) 2015 by Wenzel Jakob, Romain Prévost
5 
6  Nori is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Nori is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #if !defined(__NORI_MESH_H)
20 #define __NORI_MESH_H
21 
22 #include <nori/shape.h>
23 #include <nori/dpdf.h>
24 
25 NORI_NAMESPACE_BEGIN
26 
35 class Mesh : public Shape {
36 public:
38  virtual void activate() override;
39 
41  virtual uint32_t getPrimitiveCount() const override { return (uint32_t) m_F.cols(); }
42 
44  virtual BoundingBox3f getBoundingBox(uint32_t index) const override;
45 
47  virtual Point3f getCentroid(uint32_t index) const override;
48 
74  virtual bool rayIntersect(uint32_t index, const Ray3f &ray, float &u, float &v, float &t) const override;
75 
77  virtual void setHitInformation(uint32_t index, const Ray3f &ray, Intersection & its) const override;
78 
80  uint32_t getVertexCount() const { return (uint32_t) m_V.cols(); }
81 
86  virtual void sampleSurface(ShapeQueryRecord & sRec, const Point2f & sample) const override;
87  virtual float pdfSurface(const ShapeQueryRecord & sRec) const override;
88 
90  float surfaceArea(uint32_t index) const;
91 
92  Point3f getInterpolatedVertex(uint32_t index, const Vector3f & bc) const;
93  Normal3f getInterpolatedNormal(uint32_t index, const Vector3f & bc) const;
94 
96  const MatrixXf &getVertexPositions() const { return m_V; }
97 
99  const MatrixXf &getVertexNormals() const { return m_N; }
100 
102  const MatrixXf &getVertexTexCoords() const { return m_UV; }
103 
105  const MatrixXu &getIndices() const { return m_F; }
106 
107 
109  const std::string &getName() const { return m_name; }
110 
112  virtual std::string toString() const override;
113 
114 protected:
116  Mesh();
117 
118 protected:
119  std::string m_name;
120  MatrixXf m_V;
121  MatrixXf m_N;
122  MatrixXf m_UV;
123  MatrixXu m_F;
124 
125  DiscretePDF m_pdf;
126 };
127 
128 NORI_NAMESPACE_END
129 
130 #endif /* __NORI_MESH_H */
Triangle mesh.
Definition: mesh.h:35
MatrixXf m_UV
Vertex texture coordinates.
Definition: mesh.h:122
virtual void setHitInformation(uint32_t index, const Ray3f &ray, Intersection &its) const override
Set intersection information: hit point, shading frame, UVs.
Definition: mesh.cpp:122
virtual void sampleSurface(ShapeQueryRecord &sRec, const Point2f &sample) const override
Uniformly sample a position on the mesh with respect to surface area.
Definition: mesh.cpp:40
MatrixXf m_N
Vertex normals.
Definition: mesh.h:121
const MatrixXf & getVertexNormals() const
Return a pointer to the vertex normals (or nullptr if there are none)
Definition: mesh.h:99
uint32_t getVertexCount() const
Return the total number of vertices in this shape.
Definition: mesh.h:80
virtual std::string toString() const override
Return a human-readable summary of this instance.
Definition: mesh.cpp:176
const std::string & getName() const
Return the name of this mesh.
Definition: mesh.h:109
std::string m_name
Identifying name.
Definition: mesh.h:119
Mesh()
Create an empty mesh.
Definition: mesh.cpp:28
virtual uint32_t getPrimitiveCount() const override
Return the total number of triangles in this shape.
Definition: mesh.h:41
virtual void activate() override
Initialize internal data structures (called once by the XML parser)
Definition: mesh.cpp:30
const MatrixXf & getVertexPositions() const
Return a pointer to the vertex positions.
Definition: mesh.h:96
const MatrixXf & getVertexTexCoords() const
Return a pointer to the texture coordinates (or nullptr if there are none)
Definition: mesh.h:102
MatrixXf m_V
Vertex positions.
Definition: mesh.h:120
virtual bool rayIntersect(uint32_t index, const Ray3f &ray, float &u, float &v, float &t) const override
Ray-triangle intersection test.
Definition: mesh.cpp:83
virtual float pdfSurface(const ShapeQueryRecord &sRec) const override
Return the probability of sampling a point sRec.p by the sampleSurface() method (sRec....
Definition: mesh.cpp:59
const MatrixXu & getIndices() const
Return a pointer to the triangle vertex index list.
Definition: mesh.h:105
MatrixXu m_F
Faces.
Definition: mesh.h:123
float surfaceArea(uint32_t index) const
Return the surface area of the given triangle.
Definition: mesh.cpp:75
Superclass of all shapes.
Definition: shape.h:97
Discrete probability distribution.
Definition: dpdf.h:34
Intersection data structure.
Definition: shape.h:37
3-dimensional surface normal representation
Definition: vector.h:133
Data record for conveniently querying and sampling the a point on a shape.
Definition: shape.h:74