1/*---------------------------------------------------------------------------*
2  Project:  Horizon
3  File:     math_Vector3.ipp
4
5  Copyright (C)2009-2010 Nintendo Co., Ltd.  All rights reserved.
6
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law.  They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12
13  $Revision: 45840 $
14 *---------------------------------------------------------------------------*/
15
16#include <cmath>
17
18namespace nn {
19namespace math {
20
21/* ------------------------------------------------------------------------
22        VEC3
23   ------------------------------------------------------------------------ */
24
25NN_MATH_INLINE VEC3*
26VEC3Cross(VEC3* pOut, const VEC3* p1, const VEC3* p2)
27{
28    NN_NULL_ASSERT(pOut);
29    NN_NULL_ASSERT(p1);
30    NN_NULL_ASSERT(p2);
31
32    VEC3 tmpVec;
33
34    tmpVec.x = ( p1->y * p2->z ) - ( p1->z * p2->y );
35    tmpVec.y = ( p1->z * p2->x ) - ( p1->x * p2->z );
36    tmpVec.z = ( p1->x * p2->y ) - ( p1->y * p2->x );
37
38    pOut->x = tmpVec.x;
39    pOut->y = tmpVec.y;
40    pOut->z = tmpVec.z;
41
42    return pOut;
43}
44
45NN_MATH_INLINE bool
46VEC3IsZero(const VEC3* p)
47{
48    return p->x == 0.f && p->y == 0.f && p->z == 0.f;
49}
50
51NN_MATH_INLINE VEC3*
52VEC3Maximize(VEC3* pOut, const VEC3* p1, const VEC3* p2)
53{
54    pOut->x = (p1->x > p2->x) ? p1->x : p2->x;
55    pOut->y = (p1->y > p2->y) ? p1->y : p2->y;
56    pOut->z = (p1->z > p2->z) ? p1->z : p2->z;
57
58    return pOut;
59}
60
61NN_MATH_INLINE VEC3*
62VEC3Minimize(VEC3* pOut, const VEC3* p1, const VEC3* p2)
63{
64    pOut->x = (p1->x < p2->x) ? p1->x : p2->x;
65    pOut->y = (p1->y < p2->y) ? p1->y : p2->y;
66    pOut->z = (p1->z < p2->z) ? p1->z : p2->z;
67
68    return pOut;
69}
70
71NN_MATH_INLINE VEC3*
72VEC3SafeNormalize(VEC3* pOut, const VEC3* p, const VEC3& alt)
73{
74    NN_NULL_ASSERT(pOut);
75    NN_NULL_ASSERT(p);
76
77    register f32 x, y, z, mag;
78
79    x = p->x;
80    y = p->y;
81    z = p->z;
82
83    mag = (x * x) + (y * y) + (z * z);
84
85    if (mag == 0)
86    {
87        *pOut = alt;
88
89        return pOut;
90    }
91
92    mag = 1.0f / ::std::sqrtf(mag);
93
94    x *= mag;
95    y *= mag;
96    z *= mag;
97
98    pOut->x = x;
99    pOut->y = y;
100    pOut->z = z;
101
102    return pOut;
103}
104
105NN_MATH_INLINE f32
106VEC3SquareDist(const VEC3* p1, const VEC3* p2)
107{
108    NN_NULL_ASSERT( p1 );
109    NN_NULL_ASSERT( p2 );
110
111  VEC3 diff;
112
113    diff.x = p1->x - p2->x;
114    diff.y = p1->y - p2->y;
115    diff.z = p1->z - p2->z;
116
117    return (diff.x * diff.x) + (diff.y * diff.y) + (diff.z * diff.z);
118}
119
120}  // namespace math
121}  // namespace nn
122