Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bsphere.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba 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  Mitsuba 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 #pragma once
20 #if !defined(__MITSUBA_CORE_BSPHERE_H_)
21 #define __MITSUBA_CORE_BSPHERE_H_
22 
23 #include <mitsuba/core/ray.h>
24 
26 
27 /** \brief Bounding sphere data structure in three dimensions
28  *
29  * \ingroup libcore
30  * \ingroup libpython
31  */
32 struct BSphere {
35 
36  /// Construct a bounding sphere at the origin having radius zero
37  inline BSphere() : center(0.0f), radius(0.0f) { }
38 
39  /// Unserialize a bounding sphere from a binary data stream
40  inline BSphere(Stream *stream) {
41  center = Point(stream);
42  radius = stream->readFloat();
43  }
44 
45  /// Create a bounding sphere from a given center point and radius
46  inline BSphere(const Point &center, Float radius)
47  : center(center), radius(radius) {
48  }
49 
50  /// Copy constructor
51  inline BSphere(const BSphere &boundingSphere)
52  : center(boundingSphere.center), radius(boundingSphere.radius) {
53  }
54 
55  /// Return whether this bounding sphere has a radius of zero or less.
56  inline bool isEmpty() const {
57  return radius <= 0.0f;
58  }
59 
60  /// Expand the bounding sphere radius to contain another point.
61  inline void expandBy(const Point p) {
62  radius = std::max(radius, (p-center).length());
63  }
64 
65  /// Check whether the specified point is inside or on the sphere
66  inline bool contains(const Point p) const {
67  return (p - center).length() <= radius;
68  }
69 
70  /// Equality test
71  inline bool operator==(const BSphere &boundingSphere) const {
72  return center == boundingSphere.center && radius == boundingSphere.radius;
73  }
74 
75  /// Inequality test
76  inline bool operator!=(const BSphere &boundingSphere) const {
77  return center != boundingSphere.center || radius != boundingSphere.radius;
78  }
79 
80  /**
81  * \brief Calculate the intersection points with the given ray
82  * \return \c true if the ray intersects the bounding sphere
83  *
84  * \remark In the Python bindings, this function returns the
85  * \c nearT and \c farT values as a tuple (or \c None, when no
86  * intersection was found)
87  */
88  inline bool rayIntersect(const Ray &ray, Float &nearHit, Float &farHit) const {
89  Vector o = ray.o - center;
90  Float A = ray.d.lengthSquared();
91  Float B = 2 * dot(o, ray.d);
92  Float C = o.lengthSquared() - radius*radius;
93 
94  return solveQuadratic(A, B, C, nearHit, farHit);
95  }
96 
97  /// Serialize this bounding sphere to a binary data stream
98  inline void serialize(Stream *stream) const {
99  center.serialize(stream);
100  stream->writeFloat(radius);
101  }
102 
103  /// Return a string representation of the bounding sphere
104  inline std::string toString() const {
105  std::ostringstream oss;
106  oss << "BSphere[center = " << center.toString()
107  << ", radius = " << radius << "]";
108  return oss.str();
109  }
110 };
111 
113 
114 #endif /* __MITSUBA_CORE_BSPHERE_H_ */
std::string toString() const
Return a string representation of the bounding sphere.
Definition: bsphere.h:104
BSphere()
Construct a bounding sphere at the origin having radius zero.
Definition: bsphere.h:37
void serialize(Stream *stream) const
Serialize this bounding sphere to a binary data stream.
Definition: bsphere.h:98
void expandBy(const Point p)
Expand the bounding sphere radius to contain another point.
Definition: bsphere.h:61
Bounding sphere data structure in three dimensions.
Definition: bsphere.h:32
Float readFloat()
Write a floating point number (configured precision) to the stream.
Definition: stream.h:424
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
bool operator==(const BSphere &boundingSphere) const
Equality test.
Definition: bsphere.h:71
BSphere(const BSphere &boundingSphere)
Copy constructor.
Definition: bsphere.h:51
Float radius
Definition: bsphere.h:34
TPoint3< Float > Point
Definition: fwd.h:136
bool operator!=(const BSphere &boundingSphere) const
Inequality test.
Definition: bsphere.h:76
MTS_EXPORT_CORE bool solveQuadratic(Float a, Float b, Float c, Float &x0, Float &x1)
Solve a quadratic equation of the form a*x^2 + b*x + c = 0.
void writeFloat(Float value)
Write a floating point number (configured precision) to the stream.
Definition: stream.h:271
Abstract seekable stream class.
Definition: stream.h:58
BSphere(const Point &center, Float radius)
Create a bounding sphere from a given center point and radius.
Definition: bsphere.h:46
Definition: fwd.h:65
Definition: fwd.h:96
bool isEmpty() const
Return whether this bounding sphere has a radius of zero or less.
Definition: bsphere.h:56
BSphere(Stream *stream)
Unserialize a bounding sphere from a binary data stream.
Definition: bsphere.h:40
Definition: fwd.h:100
bool rayIntersect(const Ray &ray, Float &nearHit, Float &farHit) const
Calculate the intersection points with the given ray.
Definition: bsphere.h:88
T dot(const TQuaternion< T > &q1, const TQuaternion< T > &q2)
Definition: quat.h:348
Point center
Definition: bsphere.h:33
bool contains(const Point p) const
Check whether the specified point is inside or on the sphere.
Definition: bsphere.h:66
#define MTS_NAMESPACE_END
Definition: platform.h:138