Nori  24
sphere.cpp
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 #include <nori/shape.h>
20 #include <nori/bsdf.h>
21 #include <nori/emitter.h>
22 #include <nori/warp.h>
23 
24 NORI_NAMESPACE_BEGIN
25 
26 class Sphere : public Shape {
27 public:
28  Sphere(const PropertyList & propList) {
29  m_position = propList.getPoint3("center", Point3f());
30  m_radius = propList.getFloat("radius", 1.f);
31 
32  m_bbox.expandBy(m_position - Vector3f(m_radius));
33  m_bbox.expandBy(m_position + Vector3f(m_radius));
34  }
35 
36  virtual BoundingBox3f getBoundingBox(uint32_t index) const override { return m_bbox; }
37 
38  virtual Point3f getCentroid(uint32_t index) const override { return m_position; }
39 
40  virtual bool rayIntersect(uint32_t index, const Ray3f &ray, float &u, float &v, float &t) const override {
41 
42  /* to be implemented */
43  return false;
44 
45  }
46 
47  virtual void setHitInformation(uint32_t index, const Ray3f &ray, Intersection & its) const override {
48  /* to be implemented */
49  }
50 
51  virtual void sampleSurface(ShapeQueryRecord & sRec, const Point2f & sample) const override {
53  sRec.p = m_position + m_radius * q;
54  sRec.n = q;
55  sRec.pdf = std::pow(1.f/m_radius,2) * Warp::squareToUniformSpherePdf(Vector3f(0.0f,0.0f,1.0f));
56  }
57  virtual float pdfSurface(const ShapeQueryRecord & sRec) const override {
58  return std::pow(1.f/m_radius,2) * Warp::squareToUniformSpherePdf(Vector3f(0.0f,0.0f,1.0f));
59  }
60 
61 
62  virtual std::string toString() const override {
63  return tfm::format(
64  "Sphere[\n"
65  " center = %s,\n"
66  " radius = %f,\n"
67  " bsdf = %s,\n"
68  " emitter = %s\n"
69  "]",
70  m_position.toString(),
71  m_radius,
72  m_bsdf ? indent(m_bsdf->toString()) : std::string("null"),
73  m_emitter ? indent(m_emitter->toString()) : std::string("null"));
74  }
75 
76 protected:
77  Point3f m_position;
78  float m_radius;
79 };
80 
81 NORI_REGISTER_CLASS(Sphere, "sphere");
82 NORI_NAMESPACE_END
virtual std::string toString() const =0
Return a brief string summary of the instance (for debugging purposes)
This is an associative container used to supply the constructors of NoriObject subclasses with parame...
Definition: proplist.h:32
float getFloat(const std::string &name) const
Get a float property, and throw an exception if it does not exist.
Point3f getPoint3(const std::string &name) const
Get a point property, and throw an exception if it does not exist.
Superclass of all shapes.
Definition: shape.h:97
BSDF * m_bsdf
BSDF of the surface.
Definition: shape.h:157
Emitter * m_emitter
Associated emitter, if any.
Definition: shape.h:158
BoundingBox3f m_bbox
Bounding box of the mesh.
Definition: shape.h:159
virtual void setHitInformation(uint32_t index, const Ray3f &ray, Intersection &its) const override
Set the intersection information: hit point, shading frame, UVs, etc.
Definition: sphere.cpp:47
virtual std::string toString() const override
Return a brief string summary of the instance (for debugging purposes)
Definition: sphere.cpp:62
virtual void sampleSurface(ShapeQueryRecord &sRec, const Point2f &sample) const override
Sample a point on the surface (potentially using the point sRec.ref to importance sample) This method...
Definition: sphere.cpp:51
virtual float pdfSurface(const ShapeQueryRecord &sRec) const override
Return the probability of sampling a point sRec.p by the sampleSurface() method (sRec....
Definition: sphere.cpp:57
static Vector3f squareToUniformSphere(const Point2f &sample)
Uniformly sample a vector on the unit sphere with respect to solid angles.
Definition: warp.cpp:65
static float squareToUniformSpherePdf(const Vector3f &v)
Probability density of squareToUniformSphere()
Definition: warp.cpp:69
Intersection data structure.
Definition: shape.h:37
Data record for conveniently querying and sampling the a point on a shape.
Definition: shape.h:74
Normal3f n
Sampled normal.
Definition: shape.h:80
Point3f p
Sampled point.
Definition: shape.h:78
float pdf
Probability of the sample.
Definition: shape.h:82
void expandBy(const PointType &p)
Expand the bounding box to contain another point.
Definition: bbox.h:288
std::string toString() const
Return a human-readable string summary.
Definition: vector.h:119