19 #include <nori/mesh.h>
20 #include <nori/bbox.h>
21 #include <nori/bsdf.h>
22 #include <nori/emitter.h>
23 #include <nori/warp.h>
24 #include <Eigen/Geometry>
44 Vector3f bc = Warp::squareToUniformTriangle(s);
46 sRec.
p = getInterpolatedVertex(idT,bc);
48 sRec.
n = getInterpolatedNormal(idT, bc);
54 Normal3f n = (p1-p0).cross(p2-p0).normalized();
63 Point3f Mesh::getInterpolatedVertex(uint32_t index,
const Vector3f &bc)
const {
64 return (bc.x() *
m_V.col(
m_F(0, index)) +
65 bc.y() *
m_V.col(
m_F(1, index)) +
66 bc.z() *
m_V.col(
m_F(2, index)));
69 Normal3f Mesh::getInterpolatedNormal(uint32_t index,
const Vector3f &bc)
const {
70 return (bc.x() *
m_N.col(
m_F(0, index)) +
71 bc.y() *
m_N.col(
m_F(1, index)) +
72 bc.z() *
m_N.col(
m_F(2, index))).normalized();
76 uint32_t i0 =
m_F(0, index), i1 =
m_F(1, index), i2 =
m_F(2, index);
80 return 0.5f *
Vector3f((p1 - p0).cross(p2 - p0)).norm();
84 uint32_t i0 =
m_F(0, index), i1 =
m_F(1, index), i2 =
m_F(2, index);
88 Vector3f edge1 = p1 - p0, edge2 = p2 - p0;
94 float det = edge1.dot(pvec);
96 if (det > -1e-8f && det < 1e-8f)
98 float inv_det = 1.0f / det;
104 u = tvec.dot(pvec) * inv_det;
105 if (u < 0.0 || u > 1.0)
112 v = ray.
d.dot(qvec) * inv_det;
113 if (v < 0.0 || u + v > 1.0)
117 t = edge2.dot(qvec) * inv_det;
119 return t >= ray.
mint && t <= ray.
maxt;
125 bary << 1-its.
uv.sum(), its.
uv;
128 uint32_t idx0 =
m_F(0, index), idx1 =
m_F(1, index), idx2 =
m_F(2, index);
134 its.
p = bary.x() * p0 + bary.y() * p1 + bary.z() * p2;
138 its.
uv = bary.x() *
m_UV.col(idx0) +
139 bary.y() *
m_UV.col(idx1) +
140 bary.z() *
m_UV.col(idx2);
145 if (
m_N.size() > 0) {
153 (bary.x() *
m_N.col(idx0) +
154 bary.y() *
m_N.col(idx1) +
155 bary.z() *
m_N.col(idx2)).normalized());
163 result.expandBy(
m_V.col(
m_F(1, index)));
164 result.expandBy(
m_V.col(
m_F(2, index)));
168 Point3f Mesh::getCentroid(uint32_t index)
const {
169 return (1.0f / 3.0f) *
180 " vertexCount = %i,\n"
181 " triangleCount = %i,\n"
MatrixXf m_UV
Vertex texture coordinates.
virtual void setHitInformation(uint32_t index, const Ray3f &ray, Intersection &its) const override
Set intersection information: hit point, shading frame, UVs.
virtual void sampleSurface(ShapeQueryRecord &sRec, const Point2f &sample) const override
Uniformly sample a position on the mesh with respect to surface area.
MatrixXf m_N
Vertex normals.
virtual std::string toString() const override
Return a human-readable summary of this instance.
std::string m_name
Identifying name.
Mesh()
Create an empty mesh.
virtual uint32_t getPrimitiveCount() const override
Return the total number of triangles in this shape.
virtual void activate() override
Initialize internal data structures (called once by the XML parser)
MatrixXf m_V
Vertex positions.
virtual bool rayIntersect(uint32_t index, const Ray3f &ray, float &u, float &v, float &t) const override
Ray-triangle intersection test.
virtual float pdfSurface(const ShapeQueryRecord &sRec) const override
Return the probability of sampling a point sRec.p by the sampleSurface() method (sRec....
float surfaceArea(uint32_t index) const
Return the surface area of the given triangle.
virtual std::string toString() const =0
Return a brief string summary of the instance (for debugging purposes)
virtual void activate() override
Initialize internal data structures (called once by the XML parser)
BSDF * m_bsdf
BSDF of the surface.
Emitter * m_emitter
Associated emitter, if any.
float getNormalization() const
Return the normalization factor (i.e. the inverse of getSum())
size_t sampleReuse(float &sampleValue) const
Transform a uniformly distributed sample to the stored distribution
void append(float pdfValue)
Append an entry with the specified discrete probability.
void reserve(size_t nEntries)
Reserve memory for a certain number of entries.
float normalize()
Normalize the distribution.
Stores a three-dimensional orthonormal coordinate frame.
Intersection data structure.
Frame shFrame
Shading frame (based on the shading normal)
Frame geoFrame
Geometric frame (based on the true geometry)
Point3f p
Position of the surface intersection.
Point2f uv
UV coordinates, if any.
3-dimensional surface normal representation
Data record for conveniently querying and sampling the a point on a shape.
Normal3f n
Sampled normal.
float pdf
Probability of the sample.
VectorType d
Ray direction.
Scalar mint
Minimum position on the ray segment.
Scalar maxt
Maximum position on the ray segment.