Library of Assembled Shared Sources
normalizing_policy.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44/** @defgroup NormalizingPolicy NormalizingPolicy
45 * @brief policies to specify if direction vectors (and normals) should be normalized.
46 * @author Bram de Greve
47 * @date 2003
48 *
49 * For direction vectors and normal vectors, you can be in the situation you want to
50 * have them normalized (unit length) and that you want this to be done automatically.
51 * Or you can be in the situation where you want them to be anything but normalized.
52 * These policies will let you set this.
53 *
54 * The content of the policies are rather implementation details, clients shouldn't
55 * worry about it.
56 *
57 * @note these policies can be used for vectors of any dimension.
58 */
59
60/** @struct lass::prim::Normalized
61 * @ingroup NormalizingPolicy
62 * @brief Policy to auto-normalize normals.
63 * @author Bram de Greve [BdG]
64 * @date 2003
65 *
66 * Using this policy will automatically normalize all direction vectors and normals.
67 * That way, you don't have to bother what you have as input, you know that they will
68 * be normalized anyway. Also, for the implementation of the primitives, optimized
69 * routines are provided that take advantage of the knowledge that the vectors are
70 * normalized. But this are implementation details you should not worry about.
71 */
72
73/** @struct lass::prim::Unnormalized
74 * @ingroup NormalizingPolicy
75 * @brief Policy to keep normals unnormalized.
76 * @author Bram de Greve [BdG]
77 * @date 2003
78 *
79 * Using this policy will keep direction vectors and normals at their original length.
80 * You can do this for precision reasons, for speed issues, or any other reason that
81 * is yours. However, the maths will still be correct, because the implementations
82 * will know of this unnormalized vectors, and will correct with extra factors where
83 * necessary. But, these corrections can sometimes be faster than using normalized
84 * vectors for start, because they often can avoid the square root.
85 */
86
87/** @struct lass::prim::IsAlreadyNormalized
88 * @ingroup NormalizingPolicy
89 * @date 2006
90 *
91 * Empty argument to tell a function the client assures the vector coming in is already
92 * normalized, so the function doesn't has to.
93 */
94
95#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_NORMALIZING_POLICY_H
96#define LASS_GUARDIAN_OF_INCLUSION_PRIM_NORMALIZING_POLICY_H
97
98#include "prim_common.h"
99
100
101
102namespace lass
103{
104
105namespace prim
106{
107
109{
110 /** Normalize a vector iSubject
111 */
112 template<typename VectorType>
113 static void normalize(VectorType& ioSubject)
114 {
115 ioSubject.normalize();
116 }
117
118 /** Normalize iSubject, and scale iExtraValue as well so iExtraValue / iSubject.norm() is constant.
119 * Typically used by cartesian equations.
120 * @post iExtraValue / iSubject.norm() is the same as before the call.
121 */
122 template<typename VectorType, typename ValueType>
123 static void normalizeAndScale(VectorType& ioSubject, ValueType& ioExtraValue)
124 {
125 const typename VectorType::TValue norm = ioSubject.norm();
126 LASS_ASSERT(norm != VectorType::TNumTraits::zero);
127 ioExtraValue /= norm;
128 ioSubject /= norm;
129 }
130
131 /** since iNormObject should be normalized by now, we can "divide by 1".
132 * @return iValue
133 * @pre iNormObject is supposed to be normalized!
134 */
135 template<typename ValueType, typename VectorType>
136 static ValueType divideByNorm(ValueType iValue, const VectorType& /*iNormObject*/)
137 {
138 return iValue;
139 }
140
141 /** since iNormObject should be normalized by now, we can "divide by 1".
142 * @return iValue
143 * @pre iNormObject is supposed to be normalized!
144 */
145 template<typename ValueType, typename VectorType>
146 static ValueType divideBySquaredNorm(ValueType iValue, const VectorType& /*iNormObject*/)
147 {
148 return iValue;
149 }
150};
151
152
153
155{
156 /** Don't normalize normals we want to keep unnormalized! :)
157 * this is a noop.
158 */
159 template<typename VectorType>
160 static void normalize(VectorType& /*ioSubject*/)
161 {
162 }
163
164
165 /** Don't normalize and don't scale
166 */
167 template<typename VectorType, typename ValueType>
168 static void normalizeAndScale(VectorType& /*ioSubject*/, ValueType& /*ioExtraValue*/)
169 {
170 }
171
172 /** @return iValue divided by the norm of iNormObject
173 */
174 template<typename ValueType, typename VectorType>
175 static ValueType divideByNorm(ValueType iValue, const VectorType& iNormObject)
176 {
177 return iValue / iNormObject.norm();
178 }
179
180 /** @return iValue divided by the squared norm of iNormObject
181 */
182 template<typename ValueType, typename VectorType>
183 static ValueType divideBySquaredNorm(ValueType iValue, const VectorType& iNormObject)
184 {
185 return iValue / iNormObject.squaredNorm();
186 }
187};
188
189
190
192{
193};
194
195
196
197
198}
199
200}
201
202#endif
T norm(const T &x)
return norm of x as if x is real part of complex number: sqr(x)
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53
Policy to auto-normalize normals.
static ValueType divideByNorm(ValueType iValue, const VectorType &)
since iNormObject should be normalized by now, we can "divide by 1".
static void normalize(VectorType &ioSubject)
Normalize a vector iSubject.
static ValueType divideBySquaredNorm(ValueType iValue, const VectorType &)
since iNormObject should be normalized by now, we can "divide by 1".
static void normalizeAndScale(VectorType &ioSubject, ValueType &ioExtraValue)
Normalize iSubject, and scale iExtraValue as well so iExtraValue / iSubject.norm() is constant.
Policy to keep normals unnormalized.
static ValueType divideBySquaredNorm(ValueType iValue, const VectorType &iNormObject)
static ValueType divideByNorm(ValueType iValue, const VectorType &iNormObject)
static void normalize(VectorType &)
Don't normalize normals we want to keep unnormalized!
static void normalizeAndScale(VectorType &, ValueType &)
Don't normalize and don't scale.