Angel 3.2
A 2D Game Prototyping Engine
Vector3.cpp
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "stdafx.h"
31 #include "../Infrastructure/Vector3.h"
32 
33 #include "../Util/MathUtil.h"
34 
35 #include <math.h>
36 
37 Vector3 Vector3::Zero(0.0f, 0.0f, 0.0f);
38 Vector3 Vector3::One(1.0f, 1.0f, 1.0f);
39 Vector3 Vector3::UnitX(1.0f, 0.0f, 0.0f);
40 Vector3 Vector3::UnitY(0.0f, 1.0f, 0.0f);
41 Vector3 Vector3::UnitZ(0.0f, 0.0f, 1.0f);
42 
43 Vector3::Vector3(float x, float y, float z)
44 : X(x)
45 , Y(y)
46 , Z(z)
47 {}
48 
49 Vector3::Vector3(float value)
50 : X(value)
51 , Y(value)
52 , Z(value)
53 {}
54 
56 : X(0)
57 , Y(0)
58 , Z(0)
59 {}
60 
62 {
63  return sqrt(LengthSquared());
64 }
65 
67 {
68  return (X * X) + (Y * Y) + (Z * Z);
69 }
70 
71 /*static*/ float Vector3::Distance(const Vector3& value1, const Vector3& value2)
72 {
73  return Vector3(value1 - value2).Length();
74 }
75 
76 /*static*/ float Vector3::DistanceSquared(const Vector3& value1, const Vector3& value2)
77 {
78  return Vector3(value1 - value2).LengthSquared();
79 
80 }
81 
82 /*static*/ float Vector3::Dot(const Vector3& value1, const Vector3& value2)
83 {
84  return ((value1.X * value2.X) + (value1.Y * value2.Y) + (value1.Z * value2.Z));
85 }
86 
88 {
89  float len = Length();
90 
91 
92  if( len < MathUtil::Epsilon )
93  {
94  if( Y > X )
95  *this = UnitY;
96  else if (X > Z) //favoring the 2d components first
97  *this = UnitX;
98  else
99  *this = UnitZ;
100  }
101  else
102  {
103  *this = *this / len;
104  }
105 }
106 
107 /*static*/ Vector3 Vector3::Normalize(const Vector3& value)
108 {
109  Vector3 retVal(value);
110  retVal.Normalize();
111  return retVal;
112 }
113 
114 /*static*/ Vector3 Vector3::Reflect(const Vector3& vector, const Vector3& normal)
115 {
116  return vector - (normal * 2.0f * Dot(vector, normal));
117 }
118 
119 /*static*/ Vector3 Vector3::Min(const Vector3& value1, const Vector3& value2)
120 {
121  return Vector3(MathUtil::Min(value1.X, value2.X), MathUtil::Min(value1.Y, value2.Y), MathUtil::Min(value1.Z, value2.Z));
122 }
123 
124 /*static*/ Vector3 Vector3::Max(const Vector3& value1, const Vector3& value2)
125 {
126  return Vector3(MathUtil::Max(value1.X, value2.X), MathUtil::Max(value1.Y, value2.Y), MathUtil::Max(value1.Z, value2.Z));
127 }
128 
129 /*static*/ Vector3 Vector3::Clamp(const Vector3& value1, const Vector3& min, const Vector3& max)
130 {
131  return Vector3(MathUtil::Clamp(value1.X, min.X, max.X), MathUtil::Clamp(value1.Y, min.Y, max.Y), MathUtil::Clamp(value1.Z, min.Z, max.Z));
132 }
133 
134 /*static*/ Vector3 Vector3::Lerp(const Vector3& value1, const Vector3& value2, float amount)
135 {
136  return Vector3( MathUtil::Lerp( value1.X, value2.X, amount ), MathUtil::Lerp( value1.Y, value2.Y, amount ), MathUtil::Lerp(value1.Z, value2.Z, amount));
137 }
138 /*static*/ Vector3 Vector3::Negate(const Vector3& value)
139 {
140  return -value;
141 }
142 
143 bool Vector3::operator==(const Vector3 &v) const
144 {
145  return X == v.X && Y == v.Y && Z == v.Z;
146 }
147 
148 bool Vector3::operator!=(const Vector3 &v) const
149 {
150  return !(*this == v);
151 }
152 
153 Vector3 Vector3::operator-() const
154 {
155  return Vector3::Zero - *this;
156 }
157 
158 Vector3 Vector3::operator-(const Vector3 &v) const
159 {
160  return Vector3(X - v.X, Y - v.Y, Z - v.Z);
161 }
162 
163 Vector3 Vector3::operator+(const Vector3 &v) const
164 {
165  return Vector3(X + v.X, Y + v.Y, Z + v.Z);
166 }
167 
168 Vector3 Vector3::operator/(float divider) const
169 {
170  return Vector3(X / divider, Y / divider, Z / divider);
171 }
172 
173 Vector3 Vector3::operator*(float scaleFactor) const
174 {
175  return Vector3(X * scaleFactor, Y * scaleFactor, Z * scaleFactor);
176 }
177 
178 Vector3& Vector3::operator+=(const Vector3 &v)
179 {
180  X += v.X;
181  Y += v.Y;
182  Z += v.Z;
183  return *this;
184 }
185 
186 Vector3& Vector3::operator-=(const Vector3 &v)
187 {
188  X -= v.X;
189  Y -= v.Y;
190  Z -= v.Z;
191  return *this;
192 }
193 
194 Vector3& Vector3::operator*=(float scaleFactor)
195 {
196  X *= scaleFactor;
197  Y *= scaleFactor;
198  Z *= scaleFactor;
199  return *this;
200 }
201 
202 Vector3& Vector3::operator/=(float scaleFactor)
203 {
204  X /= scaleFactor;
205  Y /= scaleFactor;
206  Z /= scaleFactor;
207  return *this;
208 }
209 
210 
211 #include <iostream>
212 using namespace std;
213 
214 ostream& operator<<(ostream& stream, const Vector3& out)
215 {
216  return stream << out.X << ", " << out.Y << ", " << out.Z;
217 }