Nori  23
shape.h
1 /*
2  This file is part of Nori, a simple educational ray tracer
3 
4  Copyright (c) 2015 by 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_SHAPE_H)
20 #define __NORI_SHAPE_H
21 
22 #include <nori/object.h>
23 #include <nori/frame.h>
24 #include <nori/bbox.h>
25 
26 NORI_NAMESPACE_BEGIN
27 
28 
37 struct Intersection {
41  float t;
49  const Shape *mesh;
50 
52  Intersection() : mesh(nullptr) { }
53 
55  Vector3f toLocal(const Vector3f &d) const {
56  return shFrame.toLocal(d);
57  }
58 
60  Vector3f toWorld(const Vector3f &d) const {
61  return shFrame.toWorld(d);
62  }
63 
65  std::string toString() const;
66 };
67 
68 
69 
82  float pdf;
83 
87  ShapeQueryRecord(const Point3f & ref_) : ref(ref_) {}
89  ShapeQueryRecord(const Point3f & ref_, const Point3f & p_) : ref(ref_), p(p_) {}
90 
91 };
92 
93 
97 class Shape : public NoriObject {
98 public:
100  virtual ~Shape();
101 
102  virtual void addChild(NoriObject *child) override;
103 
105  virtual void activate() override;
106 
108  const BoundingBox3f &getBoundingBox() const { return m_bbox; }
109 
111  bool isEmitter() const { return m_emitter != nullptr; }
112 
114  Emitter *getEmitter() { return m_emitter; }
115 
117  const Emitter *getEmitter() const { return m_emitter; }
118 
120  const BSDF *getBSDF() const { return m_bsdf; }
121 
122 
124  virtual uint32_t getPrimitiveCount() const { return 1; }
125 
127  virtual BoundingBox3f getBoundingBox(uint32_t index) const = 0;
128 
130  virtual Point3f getCentroid(uint32_t index) const = 0;
131 
133  virtual bool rayIntersect(uint32_t index, const Ray3f &ray, float &u, float &v, float &t) const = 0;
134 
136  virtual void setHitInformation(uint32_t index, const Ray3f &ray, Intersection & its) const = 0;
137 
143  virtual void sampleSurface(ShapeQueryRecord & sRec, const Point2f & sample) const = 0;
148  virtual float pdfSurface(const ShapeQueryRecord & sRec) const = 0;
149 
154  virtual EClassType getClassType() const override { return EMesh; }
155 
156 protected:
157  BSDF *m_bsdf = nullptr;
158  Emitter *m_emitter = nullptr;
160 
161 };
162 
163 NORI_NAMESPACE_END
164 
165 #endif /* __NORI_SHAPE_H */
Superclass of all bidirectional scattering distribution functions.
Definition: bsdf.h:63
Superclass of all emitters.
Definition: emitter.h:64
Base class of all objects.
Definition: object.h:32
Superclass of all shapes.
Definition: shape.h:97
bool isEmitter() const
Is this mesh an area emitter?
Definition: shape.h:111
virtual void addChild(NoriObject *child) override
Add a child object to the current instance.
Definition: shape.cpp:41
const Emitter * getEmitter() const
Return a pointer to an attached area emitter instance (const version)
Definition: shape.h:117
virtual void sampleSurface(ShapeQueryRecord &sRec, const Point2f &sample) const =0
Sample a point on the surface (potentially using the point sRec.ref to importance sample) This method...
const BSDF * getBSDF() const
Return a pointer to the BSDF associated with this mesh.
Definition: shape.h:120
virtual float pdfSurface(const ShapeQueryRecord &sRec) const =0
Return the probability of sampling a point sRec.p by the sampleSurface() method (sRec....
virtual void setHitInformation(uint32_t index, const Ray3f &ray, Intersection &its) const =0
Set the intersection information: hit point, shading frame, UVs, etc.
virtual EClassType getClassType() const override
Return the type of object (i.e. Mesh/BSDF/etc.) provided by this instance.
Definition: shape.h:154
virtual void activate() override
Initialize internal data structures (called once by the XML parser)
Definition: shape.cpp:32
BSDF * m_bsdf
BSDF of the surface.
Definition: shape.h:157
Emitter * getEmitter()
Return a pointer to an attached area emitter instance.
Definition: shape.h:114
Emitter * m_emitter
Associated emitter, if any.
Definition: shape.h:158
BoundingBox3f m_bbox
Bounding box of the mesh.
Definition: shape.h:159
virtual uint32_t getPrimitiveCount() const
Return the total number of primitives in this shape.
Definition: shape.h:124
virtual ~Shape()
Release all memory.
Definition: shape.cpp:27
Stores a three-dimensional orthonormal coordinate frame.
Definition: frame.h:33
Vector3f toWorld(const Vector3f &v) const
Convert from local coordinates to world coordinates.
Definition: frame.h:61
Vector3f toLocal(const Vector3f &v) const
Convert from world coordinates to local coordinates.
Definition: frame.h:54
Intersection data structure.
Definition: shape.h:37
const Shape * mesh
Pointer to the associated shape.
Definition: shape.h:49
Vector3f toWorld(const Vector3f &d) const
Transform a direction vector from local to world coordinates.
Definition: shape.h:60
std::string toString() const
Return a human-readable summary of the intersection record.
Definition: shape.cpp:64
Frame shFrame
Shading frame (based on the shading normal)
Definition: shape.h:45
float t
Unoccluded distance along the ray.
Definition: shape.h:41
Frame geoFrame
Geometric frame (based on the true geometry)
Definition: shape.h:47
Point3f p
Position of the surface intersection.
Definition: shape.h:39
Intersection()
Create an uninitialized intersection record.
Definition: shape.h:52
Vector3f toLocal(const Vector3f &d) const
Transform a direction vector into the local shading frame.
Definition: shape.h:55
Point2f uv
UV coordinates, if any.
Definition: shape.h:43
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
ShapeQueryRecord(const Point3f &ref_)
Data structure with ref to call sampleSurface()
Definition: shape.h:87
Normal3f n
Sampled normal.
Definition: shape.h:80
Point3f ref
Reference point.
Definition: shape.h:76
Point3f p
Sampled point.
Definition: shape.h:78
ShapeQueryRecord()
Empty constructor.
Definition: shape.h:85
float pdf
Probability of the sample.
Definition: shape.h:82
ShapeQueryRecord(const Point3f &ref_, const Point3f &p_)
Data structure with ref and p to call pdfSurface()
Definition: shape.h:89