~ubuntu-branches/ubuntu/saucy/openexr/saucy

« back to all changes in this revision

Viewing changes to Imath/ImathVec.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adeodato Simó
  • Date: 2008-03-24 23:00:21 UTC
  • mfrom: (3.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080324230021-gnofz9mnvcj1xlv3
Tags: 1.6.1-3
Disable (hopefully temporarily) the test suite on arm and ia64.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
///////////////////////////////////////////////////////////////////////////
2
 
//
3
 
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
4
 
// Digital Ltd. LLC
5
 
// 
6
 
// All rights reserved.
7
 
// 
8
 
// Redistribution and use in source and binary forms, with or without
9
 
// modification, are permitted provided that the following conditions are
10
 
// met:
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
16
 
// distribution.
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. 
20
 
// 
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.
32
 
//
33
 
///////////////////////////////////////////////////////////////////////////
34
 
 
35
 
 
36
 
 
37
 
//----------------------------------------------------------------------------
38
 
//
39
 
//      Specializations of the Vec2<T> and Vec3<T> templates.
40
 
//
41
 
//----------------------------------------------------------------------------
42
 
 
43
 
#include <ImathVec.h>
44
 
 
45
 
namespace Imath {
46
 
 
47
 
namespace
48
 
{
49
 
 
50
 
template<class T>
51
 
bool
52
 
normalizeOrThrow(Vec2<T> &v)
53
 
{
54
 
    int axis = -1;
55
 
    for (int i = 0; i < 2; i ++)
56
 
    {
57
 
        if (v[i] != 0)
58
 
        {
59
 
            if (axis != -1)
60
 
            {
61
 
                throw IntVecNormalizeExc ("Cannot normalize an integer "
62
 
                                          "vector unless it is parallel "
63
 
                                          "to a principal axis");
64
 
            }
65
 
            axis = i;
66
 
        }
67
 
    }
68
 
    v[axis] = (v[axis] > 0) ? 1 : -1;
69
 
    return true;
70
 
}
71
 
 
72
 
 
73
 
template<class T>
74
 
bool
75
 
normalizeOrThrow(Vec3<T> &v)
76
 
{
77
 
    int axis = -1;
78
 
    for (int i = 0; i < 3; i ++)
79
 
    {
80
 
        if (v[i] != 0)
81
 
        {
82
 
            if (axis != -1)
83
 
            {
84
 
                throw IntVecNormalizeExc ("Cannot normalize an integer "
85
 
                                          "vector unless it is parallel "
86
 
                                          "to a principal axis");
87
 
            }
88
 
            axis = i;
89
 
        }
90
 
    }
91
 
    v[axis] = (v[axis] > 0) ? 1 : -1;
92
 
    return true;
93
 
}
94
 
 
95
 
}
96
 
 
97
 
 
98
 
// Vec2<short>
99
 
 
100
 
template <> 
101
 
short
102
 
Vec2<short>::length () const
103
 
{
104
 
    float lenF = Math<float>::sqrt (dot (*this));
105
 
    short lenS = (short) (lenF + 0.5f);
106
 
    return lenS;
107
 
}
108
 
 
109
 
template <>
110
 
const Vec2<short> &
111
 
Vec2<short>::normalize ()
112
 
{
113
 
    normalizeOrThrow<short>(*this);
114
 
    return *this;
115
 
}
116
 
 
117
 
template <>
118
 
const Vec2<short> &
119
 
Vec2<short>::normalizeExc () throw (Iex::MathExc)
120
 
{
121
 
    if ((x == 0) && (y == 0))
122
 
        throw NullVecExc ("Cannot normalize null vector.");
123
 
 
124
 
    normalizeOrThrow<short>(*this);
125
 
    return *this;
126
 
}
127
 
 
128
 
template <>
129
 
const Vec2<short> &
130
 
Vec2<short>::normalizeNonNull ()
131
 
{
132
 
    normalizeOrThrow<short>(*this);
133
 
    return *this;
134
 
}
135
 
 
136
 
template <>
137
 
Vec2<short>
138
 
Vec2<short>::normalized () const
139
 
{
140
 
    Vec2<short> v(*this);
141
 
    normalizeOrThrow<short>(v);
142
 
    return v;
143
 
}
144
 
 
145
 
template <>
146
 
Vec2<short>
147
 
Vec2<short>::normalizedExc () const throw (Iex::MathExc)
148
 
{
149
 
    if ((x == 0) && (y == 0))
150
 
        throw NullVecExc ("Cannot normalize null vector.");
151
 
 
152
 
    Vec2<short> v(*this);
153
 
    normalizeOrThrow<short>(v);
154
 
    return v;
155
 
}
156
 
 
157
 
template <>
158
 
Vec2<short>
159
 
Vec2<short>::normalizedNonNull () const
160
 
{
161
 
    Vec2<short> v(*this);
162
 
    normalizeOrThrow<short>(v);
163
 
    return v;
164
 
}
165
 
 
166
 
 
167
 
// Vec2<int>
168
 
 
169
 
template <> 
170
 
int
171
 
Vec2<int>::length () const
172
 
{
173
 
    float lenF = Math<float>::sqrt (dot (*this));
174
 
    int lenI = (int) (lenF + 0.5f);
175
 
    return lenI;
176
 
}
177
 
 
178
 
template <>
179
 
const Vec2<int> &
180
 
Vec2<int>::normalize ()
181
 
{
182
 
    normalizeOrThrow<int>(*this);
183
 
    return *this;
184
 
}
185
 
 
186
 
template <>
187
 
const Vec2<int> &
188
 
Vec2<int>::normalizeExc () throw (Iex::MathExc)
189
 
{
190
 
    if ((x == 0) && (y == 0))
191
 
        throw NullVecExc ("Cannot normalize null vector.");
192
 
 
193
 
    normalizeOrThrow<int>(*this);
194
 
    return *this;
195
 
}
196
 
 
197
 
template <>
198
 
const Vec2<int> &
199
 
Vec2<int>::normalizeNonNull ()
200
 
{
201
 
    normalizeOrThrow<int>(*this);
202
 
    return *this;
203
 
}
204
 
 
205
 
template <>
206
 
Vec2<int>
207
 
Vec2<int>::normalized () const
208
 
{
209
 
    Vec2<int> v(*this);
210
 
    normalizeOrThrow<int>(v);
211
 
    return v;
212
 
}
213
 
 
214
 
template <>
215
 
Vec2<int>
216
 
Vec2<int>::normalizedExc () const throw (Iex::MathExc)
217
 
{
218
 
    if ((x == 0) && (y == 0))
219
 
        throw NullVecExc ("Cannot normalize null vector.");
220
 
 
221
 
    Vec2<int> v(*this);
222
 
    normalizeOrThrow<int>(v);
223
 
    return v;
224
 
}
225
 
 
226
 
template <>
227
 
Vec2<int>
228
 
Vec2<int>::normalizedNonNull () const
229
 
{
230
 
    Vec2<int> v(*this);
231
 
    normalizeOrThrow<int>(v);
232
 
    return v;
233
 
}
234
 
 
235
 
 
236
 
// Vec3<short>
237
 
 
238
 
template <> 
239
 
short
240
 
Vec3<short>::length () const
241
 
{
242
 
    float lenF = Math<float>::sqrt (dot (*this));
243
 
    short lenS = (short) (lenF + 0.5f);
244
 
    return lenS;
245
 
}
246
 
 
247
 
template <>
248
 
const Vec3<short> &
249
 
Vec3<short>::normalize ()
250
 
{
251
 
    normalizeOrThrow<short>(*this);
252
 
    return *this;
253
 
}
254
 
 
255
 
template <>
256
 
const Vec3<short> &
257
 
Vec3<short>::normalizeExc () throw (Iex::MathExc)
258
 
{
259
 
    if ((x == 0) && (y == 0) && (z == 0))
260
 
        throw NullVecExc ("Cannot normalize null vector.");
261
 
 
262
 
    normalizeOrThrow<short>(*this);
263
 
    return *this;
264
 
}
265
 
 
266
 
template <>
267
 
const Vec3<short> &
268
 
Vec3<short>::normalizeNonNull ()
269
 
{
270
 
    normalizeOrThrow<short>(*this);
271
 
    return *this;
272
 
}
273
 
 
274
 
template <>
275
 
Vec3<short>
276
 
Vec3<short>::normalized () const
277
 
{
278
 
    Vec3<short> v(*this);
279
 
    normalizeOrThrow<short>(v);
280
 
    return v;
281
 
}
282
 
 
283
 
template <>
284
 
Vec3<short>
285
 
Vec3<short>::normalizedExc () const throw (Iex::MathExc)
286
 
{
287
 
    if ((x == 0) && (y == 0) && (z == 0))
288
 
        throw NullVecExc ("Cannot normalize null vector.");
289
 
 
290
 
    Vec3<short> v(*this);
291
 
    normalizeOrThrow<short>(v);
292
 
    return v;
293
 
}
294
 
 
295
 
template <>
296
 
Vec3<short>
297
 
Vec3<short>::normalizedNonNull () const
298
 
{
299
 
    Vec3<short> v(*this);
300
 
    normalizeOrThrow<short>(v);
301
 
    return v;
302
 
}
303
 
 
304
 
 
305
 
// Vec3<int>
306
 
 
307
 
template <> 
308
 
int
309
 
Vec3<int>::length () const
310
 
{
311
 
    float lenF = Math<float>::sqrt (dot (*this));
312
 
    int lenI = (int) (lenF + 0.5f);
313
 
    return lenI;
314
 
}
315
 
 
316
 
template <>
317
 
const Vec3<int> &
318
 
Vec3<int>::normalize ()
319
 
{
320
 
    normalizeOrThrow<int>(*this);
321
 
    return *this;
322
 
}
323
 
 
324
 
template <>
325
 
const Vec3<int> &
326
 
Vec3<int>::normalizeExc () throw (Iex::MathExc)
327
 
{
328
 
    if ((x == 0) && (y == 0) && (z == 0))
329
 
        throw NullVecExc ("Cannot normalize null vector.");
330
 
 
331
 
    normalizeOrThrow<int>(*this);
332
 
    return *this;
333
 
}
334
 
 
335
 
template <>
336
 
const Vec3<int> &
337
 
Vec3<int>::normalizeNonNull ()
338
 
{
339
 
    normalizeOrThrow<int>(*this);
340
 
    return *this;
341
 
}
342
 
 
343
 
template <>
344
 
Vec3<int>
345
 
Vec3<int>::normalized () const
346
 
{
347
 
    Vec3<int> v(*this);
348
 
    normalizeOrThrow<int>(v);
349
 
    return v;
350
 
}
351
 
 
352
 
template <>
353
 
Vec3<int>
354
 
Vec3<int>::normalizedExc () const throw (Iex::MathExc)
355
 
{
356
 
    if ((x == 0) && (y == 0) && (z == 0))
357
 
        throw NullVecExc ("Cannot normalize null vector.");
358
 
 
359
 
    Vec3<int> v(*this);
360
 
    normalizeOrThrow<int>(v);
361
 
    return v;
362
 
}
363
 
 
364
 
template <>
365
 
Vec3<int>
366
 
Vec3<int>::normalizedNonNull () const
367
 
{
368
 
    Vec3<int> v(*this);
369
 
    normalizeOrThrow<int>(v);
370
 
    return v;
371
 
}
372
 
 
373
 
 
374
 
} // namespace Imath