Nori  24
shape.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 //#include <Eigen/Geometry>
24 
25 NORI_NAMESPACE_BEGIN
26 
28  delete m_bsdf;
29  //delete m_emitter; // scene is responsible for deleting the emitter
30 }
31 
33  if (!m_bsdf) {
34  /* If no material was assigned, instantiate a diffuse BRDF */
35  m_bsdf = static_cast<BSDF *>(
37  m_bsdf->activate();
38  }
39 }
40 
42  switch (obj->getClassType()) {
43  case EBSDF:
44  if (m_bsdf)
45  throw NoriException(
46  "Shape: tried to register multiple BSDF instances!");
47  m_bsdf = static_cast<BSDF *>(obj);
48  break;
49 
50  case EEmitter:
51  if (m_emitter)
52  throw NoriException(
53  "Shape: tried to register multiple Emitter instances!");
54  m_emitter = static_cast<Emitter *>(obj);
55  m_emitter->setShape(static_cast<Shape*>(this));
56  break;
57 
58  default:
59  throw NoriException("Shape::addChild(<%s>) is not supported!",
60  classTypeName(obj->getClassType()));
61  }
62 }
63 
64 std::string Intersection::toString() const {
65  if (!mesh)
66  return "Intersection[invalid]";
67 
68  return tfm::format(
69  "Intersection[\n"
70  " p = %s,\n"
71  " t = %f,\n"
72  " uv = %s,\n"
73  " shFrame = %s,\n"
74  " geoFrame = %s,\n"
75  " mesh = %s\n"
76  "]",
77  p.toString(),
78  t,
79  uv.toString(),
80  indent(shFrame.toString()),
81  indent(geoFrame.toString()),
82  mesh ? mesh->toString() : std::string("null")
83  );
84 }
85 
86 NORI_NAMESPACE_END
Superclass of all bidirectional scattering distribution functions.
Definition: bsdf.h:60
Superclass of all emitters.
Definition: emitter.h:64
void setShape(Shape *shape)
Set the shape if the emitter is attached to a shape.
Definition: emitter.h:125
Simple exception class, which stores a human-readable error description.
Definition: common.h:148
static NoriObject * createInstance(const std::string &name, const PropertyList &propList)
Construct an instance from the class of the given name.
Definition: object.h:158
Base class of all objects.
Definition: object.h:32
virtual void activate()
Perform some action associated with the object.
Definition: object.h:107
virtual std::string toString() const =0
Return a brief string summary of the instance (for debugging purposes)
static std::string classTypeName(EClassType type)
Turn a class type into a human-readable string.
Definition: object.h:51
virtual EClassType getClassType() const =0
Return the type of object (i.e. Mesh/BSDF/etc.) provided by this instance.
This is an associative container used to supply the constructors of NoriObject subclasses with parame...
Definition: proplist.h:32
Superclass of all shapes.
Definition: shape.h:97
virtual void addChild(NoriObject *child) override
Add a child object to the current instance.
Definition: shape.cpp:41
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 * m_emitter
Associated emitter, if any.
Definition: shape.h:158
virtual ~Shape()
Release all memory.
Definition: shape.cpp:27
std::string toString() const
Return a human-readable string summary of this frame.
Definition: frame.h:138
const Shape * mesh
Pointer to the associated shape.
Definition: shape.h:49
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
Point2f uv
UV coordinates, if any.
Definition: shape.h:43
std::string toString() const
Return a human-readable string summary.
Definition: vector.h:119