Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
frame.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_FRAME_H_)
21 #define __MITSUBA_CORE_FRAME_H_
22 
23 #include <mitsuba/mitsuba.h>
24 
26 
27 /**
28  * \brief Stores a three-dimensional orthonormal coordinate frame
29  *
30  * This class is mostly used to quickly convert between different
31  * cartesian coordinate systems and to efficiently compute certain
32  * quantities (e.g. \ref cosTheta(), \ref tanTheta, ..).
33  *
34  * \ingroup libcore
35  * \ingroup libpython
36  */
37 struct Frame {
38  Vector s, t;
40 
41  /// Default constructor -- performs no initialization!
42  inline Frame() { }
43 
44  /// Given a normal and tangent vectors, construct a new coordinate frame
45  inline Frame(const Vector &s, const Vector &t, const Normal &n)
46  : s(s), t(t), n(n) {
47  }
48 
49  /// Construct a frame from the given orthonormal vectors
50  inline Frame(const Vector &x, const Vector &y, const Vector &z)
51  : s(x), t(y), n(z) {
52  }
53 
54  /// Construct a new coordinate frame from a single vector
55  inline Frame(const Vector &n) : n(n) {
56  coordinateSystem(n, s, t);
57  }
58 
59  /// Unserialize from a binary data stream
60  inline Frame(Stream *stream) {
61  s = Vector(stream);
62  t = Vector(stream);
63  n = Normal(stream);
64  }
65 
66  /// Serialize to a binary data stream
67  inline void serialize(Stream *stream) const {
68  s.serialize(stream);
69  t.serialize(stream);
70  n.serialize(stream);
71  }
72 
73  /// Convert from world coordinates to local coordinates
74  inline Vector toLocal(const Vector &v) const {
75  return Vector(
76  dot(v, s),
77  dot(v, t),
78  dot(v, n)
79  );
80  }
81 
82  /// Convert from local coordinates to world coordinates
83  inline Vector toWorld(const Vector &v) const {
84  return s * v.x + t * v.y + n * v.z;
85  }
86 
87  /** \brief Assuming that the given direction is in the local coordinate
88  * system, return the squared cosine of the angle between the normal and v */
89  inline static Float cosTheta2(const Vector &v) {
90  return v.z * v.z;
91  }
92 
93  /** \brief Assuming that the given direction is in the local coordinate
94  * system, return the cosine of the angle between the normal and v */
95  inline static Float cosTheta(const Vector &v) {
96  return v.z;
97  }
98 
99  /** \brief Assuming that the given direction is in the local coordinate
100  * system, return the u and v coordinates of the vector 'v' */
101  inline static Vector2 uv(const Vector &v) {
102  return Vector2(v.x, v.y);
103  }
104 
105  /** \brief Assuming that the given direction is in the local coordinate
106  * system, return the squared sine of the angle between the normal and v */
107  inline static Float sinTheta2(const Vector &v) {
108  return 1.0f - v.z * v.z;
109  }
110 
111  /** \brief Assuming that the given direction is in the local coordinate
112  * system, return the sine of the angle between the normal and v */
113  inline static Float sinTheta(const Vector &v) {
114  Float temp = sinTheta2(v);
115  if (temp <= 0.0f)
116  return 0.0f;
117  return std::sqrt(temp);
118  }
119 
120  /** \brief Assuming that the given direction is in the local coordinate
121  * system, return the tangent of the angle between the normal and v */
122  inline static Float tanTheta(const Vector &v) {
123  Float temp = 1 - v.z*v.z;
124  if (temp <= 0.0f)
125  return 0.0f;
126  return std::sqrt(temp) / v.z;
127  }
128 
129  /** \brief Assuming that the given direction is in the local coordinate
130  * system, return the squared tangent of the angle between the normal and v */
131  inline static Float tanTheta2(const Vector &v) {
132  Float temp = 1 - v.z*v.z;
133  if (temp <= 0.0f)
134  return 0.0f;
135  return temp / (v.z * v.z);
136  }
137 
138  /** \brief Assuming that the given direction is in the local coordinate
139  * system, return the sine of the phi parameter in spherical coordinates */
140  inline static Float sinPhi(const Vector &v) {
141  Float sinTheta = Frame::sinTheta(v);
142  if (sinTheta == 0.0f)
143  return 1.0f;
144  return math::clamp(v.y / sinTheta, (Float) -1.0f, (Float) 1.0f);
145  }
146 
147  /** \brief Assuming that the given direction is in the local coordinate
148  * system, return the cosine of the phi parameter in spherical coordinates */
149  inline static Float cosPhi(const Vector &v) {
150  Float sinTheta = Frame::sinTheta(v);
151  if (sinTheta == 0.0f)
152  return 1.0f;
153  return math::clamp(v.x / sinTheta, (Float) -1.0f, (Float) 1.0f);
154  }
155 
156  /** \brief Assuming that the given direction is in the local coordinate
157  * system, return the squared sine of the phi parameter in spherical
158  * coordinates */
159  inline static Float sinPhi2(const Vector &v) {
160  return math::clamp(v.y * v.y / sinTheta2(v), (Float) 0.0f, (Float) 1.0f);
161  }
162 
163  /** \brief Assuming that the given direction is in the local coordinate
164  * system, return the squared cosine of the phi parameter in spherical
165  * coordinates */
166  inline static Float cosPhi2(const Vector &v) {
167  return math::clamp(v.x * v.x / sinTheta2(v), (Float) 0.0f, (Float) 1.0f);
168  }
169 
170  /// Equality test
171  inline bool operator==(const Frame &frame) const {
172  return frame.s == s && frame.t == t && frame.n == n;
173  }
174 
175  /// Inequality test
176  inline bool operator!=(const Frame &frame) const {
177  return !operator==(frame);
178  }
179 
180  /// Return a string representation of this frame
181  inline std::string toString() const {
182  std::ostringstream oss;
183  oss << "Frame[" << std::endl
184  << " s = " << s.toString() << "," << std::endl
185  << " t = " << t.toString() << "," << std::endl
186  << " n = " << n.toString() << std::endl
187  << "]";
188  return oss.str();
189  }
190 };
191 
193 
194 #endif /* __MITSUBA_CORE_FRAME_H_ */
static Float cosPhi2(const Vector &v)
Assuming that the given direction is in the local coordinate system, return the squared cosine of the...
Definition: frame.h:166
static Float cosTheta2(const Vector &v)
Assuming that the given direction is in the local coordinate system, return the squared cosine of the...
Definition: frame.h:89
Frame(Stream *stream)
Unserialize from a binary data stream.
Definition: frame.h:60
Three-dimensional normal data structure.
Definition: normal.h:39
Stores a three-dimensional orthonormal coordinate frame.
Definition: frame.h:37
static Float tanTheta(const Vector &v)
Assuming that the given direction is in the local coordinate system, return the tangent of the angle ...
Definition: frame.h:122
Normal n
Definition: frame.h:39
TVector3< Float > Vector
Definition: fwd.h:113
bool operator!=(const Frame &frame) const
Inequality test.
Definition: frame.h:176
static Float cosPhi(const Vector &v)
Assuming that the given direction is in the local coordinate system, return the cosine of the phi par...
Definition: frame.h:149
static Float sinPhi(const Vector &v)
Assuming that the given direction is in the local coordinate system, return the sine of the phi param...
Definition: frame.h:140
MTS_EXPORT_CORE void coordinateSystem(const Vector &a, Vector &b, Vector &c)
Complete the set {a} to an orthonormal base.
Vector s
Definition: frame.h:38
Frame()
Default constructor – performs no initialization!
Definition: frame.h:42
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
Vector t
Definition: frame.h:38
TVector2< Float > Vector2
Definition: fwd.h:106
Abstract seekable stream class.
Definition: stream.h:58
static Vector2 uv(const Vector &v)
Assuming that the given direction is in the local coordinate system, return the u and v coordinates o...
Definition: frame.h:101
Vector toWorld(const Vector &v) const
Convert from local coordinates to world coordinates.
Definition: frame.h:83
bool operator==(const Frame &frame) const
Equality test.
Definition: frame.h:171
Definition: fwd.h:96
static Float tanTheta2(const Vector &v)
Assuming that the given direction is in the local coordinate system, return the squared tangent of th...
Definition: frame.h:131
Frame(const Vector &s, const Vector &t, const Normal &n)
Given a normal and tangent vectors, construct a new coordinate frame.
Definition: frame.h:45
static Float sinPhi2(const Vector &v)
Assuming that the given direction is in the local coordinate system, return the squared sine of the p...
Definition: frame.h:159
Frame(const Vector &n)
Construct a new coordinate frame from a single vector.
Definition: frame.h:55
std::string toString() const
Return a string representation of this frame.
Definition: frame.h:181
static Float cosTheta(const Vector &v)
Assuming that the given direction is in the local coordinate system, return the cosine of the angle b...
Definition: frame.h:95
static Float sinTheta(const Vector &v)
Assuming that the given direction is in the local coordinate system, return the sine of the angle bet...
Definition: frame.h:113
T dot(const TQuaternion< T > &q1, const TQuaternion< T > &q2)
Definition: quat.h:348
void serialize(Stream *stream) const
Serialize to a binary data stream.
Definition: frame.h:67
static Float sinTheta2(const Vector &v)
Assuming that the given direction is in the local coordinate system, return the squared sine of the a...
Definition: frame.h:107
Definition: fwd.h:95
#define MTS_NAMESPACE_END
Definition: platform.h:138
Frame(const Vector &x, const Vector &y, const Vector &z)
Construct a frame from the given orthonormal vectors.
Definition: frame.h:50
Vector toLocal(const Vector &v) const
Convert from world coordinates to local coordinates.
Definition: frame.h:74
Scalar clamp(Scalar value, Scalar min, Scalar max)
Generic clamping function.
Definition: math.h:51