1
///////////////////////////////////////////////////////////////////////////
3
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6
// All rights reserved.
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
11
// * Redistributions of source code must retain the above copyright
12
// notice, this list of conditions and the following disclaimer.
13
// * Redistributions in binary form must reproduce the above
14
// copyright notice, this list of conditions and the following disclaimer
15
// in the documentation and/or other materials provided with the
17
// * Neither the name of Industrial Light & Magic nor the names of
18
// its contributors may be used to endorse or promote products derived
19
// from this software without specific prior written permission.
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
///////////////////////////////////////////////////////////////////////////
37
//----------------------------------------------------------------------------
39
// Specializations of the Vec2<T> and Vec3<T> templates.
41
//----------------------------------------------------------------------------
52
normalizeOrThrow(Vec2<T> &v)
55
for (int i = 0; i < 2; i ++)
61
throw IntVecNormalizeExc ("Cannot normalize an integer "
62
"vector unless it is parallel "
63
"to a principal axis");
68
v[axis] = (v[axis] > 0) ? 1 : -1;
75
normalizeOrThrow(Vec3<T> &v)
78
for (int i = 0; i < 3; i ++)
84
throw IntVecNormalizeExc ("Cannot normalize an integer "
85
"vector unless it is parallel "
86
"to a principal axis");
91
v[axis] = (v[axis] > 0) ? 1 : -1;
102
Vec2<short>::length () const
104
float lenF = Math<float>::sqrt (dot (*this));
105
short lenS = (short) (lenF + 0.5f);
111
Vec2<short>::normalize ()
113
normalizeOrThrow<short>(*this);
119
Vec2<short>::normalizeExc () throw (Iex::MathExc)
121
if ((x == 0) && (y == 0))
122
throw NullVecExc ("Cannot normalize null vector.");
124
normalizeOrThrow<short>(*this);
130
Vec2<short>::normalizeNonNull ()
132
normalizeOrThrow<short>(*this);
138
Vec2<short>::normalized () const
140
Vec2<short> v(*this);
141
normalizeOrThrow<short>(v);
147
Vec2<short>::normalizedExc () const throw (Iex::MathExc)
149
if ((x == 0) && (y == 0))
150
throw NullVecExc ("Cannot normalize null vector.");
152
Vec2<short> v(*this);
153
normalizeOrThrow<short>(v);
159
Vec2<short>::normalizedNonNull () const
161
Vec2<short> v(*this);
162
normalizeOrThrow<short>(v);
171
Vec2<int>::length () const
173
float lenF = Math<float>::sqrt (dot (*this));
174
int lenI = (int) (lenF + 0.5f);
180
Vec2<int>::normalize ()
182
normalizeOrThrow<int>(*this);
188
Vec2<int>::normalizeExc () throw (Iex::MathExc)
190
if ((x == 0) && (y == 0))
191
throw NullVecExc ("Cannot normalize null vector.");
193
normalizeOrThrow<int>(*this);
199
Vec2<int>::normalizeNonNull ()
201
normalizeOrThrow<int>(*this);
207
Vec2<int>::normalized () const
210
normalizeOrThrow<int>(v);
216
Vec2<int>::normalizedExc () const throw (Iex::MathExc)
218
if ((x == 0) && (y == 0))
219
throw NullVecExc ("Cannot normalize null vector.");
222
normalizeOrThrow<int>(v);
228
Vec2<int>::normalizedNonNull () const
231
normalizeOrThrow<int>(v);
240
Vec3<short>::length () const
242
float lenF = Math<float>::sqrt (dot (*this));
243
short lenS = (short) (lenF + 0.5f);
249
Vec3<short>::normalize ()
251
normalizeOrThrow<short>(*this);
257
Vec3<short>::normalizeExc () throw (Iex::MathExc)
259
if ((x == 0) && (y == 0) && (z == 0))
260
throw NullVecExc ("Cannot normalize null vector.");
262
normalizeOrThrow<short>(*this);
268
Vec3<short>::normalizeNonNull ()
270
normalizeOrThrow<short>(*this);
276
Vec3<short>::normalized () const
278
Vec3<short> v(*this);
279
normalizeOrThrow<short>(v);
285
Vec3<short>::normalizedExc () const throw (Iex::MathExc)
287
if ((x == 0) && (y == 0) && (z == 0))
288
throw NullVecExc ("Cannot normalize null vector.");
290
Vec3<short> v(*this);
291
normalizeOrThrow<short>(v);
297
Vec3<short>::normalizedNonNull () const
299
Vec3<short> v(*this);
300
normalizeOrThrow<short>(v);
309
Vec3<int>::length () const
311
float lenF = Math<float>::sqrt (dot (*this));
312
int lenI = (int) (lenF + 0.5f);
318
Vec3<int>::normalize ()
320
normalizeOrThrow<int>(*this);
326
Vec3<int>::normalizeExc () throw (Iex::MathExc)
328
if ((x == 0) && (y == 0) && (z == 0))
329
throw NullVecExc ("Cannot normalize null vector.");
331
normalizeOrThrow<int>(*this);
337
Vec3<int>::normalizeNonNull ()
339
normalizeOrThrow<int>(*this);
345
Vec3<int>::normalized () const
348
normalizeOrThrow<int>(v);
354
Vec3<int>::normalizedExc () const throw (Iex::MathExc)
356
if ((x == 0) && (y == 0) && (z == 0))
357
throw NullVecExc ("Cannot normalize null vector.");
360
normalizeOrThrow<int>(v);
366
Vec3<int>::normalizedNonNull () const
369
normalizeOrThrow<int>(v);