• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "Vector.hpp"
16 
17 #include "Matrix.hpp"
18 #include "Common/Math.hpp"
19 
20 namespace sw
21 {
operator +() const22 	Vector Vector::operator+() const
23 	{
24 		return *this;
25 	}
26 
operator -() const27 	Vector Vector::operator-() const
28 	{
29 		return Vector(-x, -y, -z);
30 	}
31 
operator +=(const Vector & v)32 	Vector &Vector::operator+=(const Vector &v)
33 	{
34 		x += v.x;
35 		y += v.y;
36 		z += v.z;
37 
38 		return *this;
39 	}
40 
operator -=(const Vector & v)41 	Vector &Vector::operator-=(const Vector &v)
42 	{
43 		x -= v.x;
44 		y -= v.y;
45 		z -= v.z;
46 
47 		return *this;
48 	}
49 
operator *=(float s)50 	Vector &Vector::operator*=(float s)
51 	{
52 		x *= s;
53 		y *= s;
54 		z *= s;
55 
56 		return *this;
57 	}
58 
operator /=(float s)59 	Vector &Vector::operator/=(float s)
60 	{
61 		float r = 1.0f / s;
62 
63 		return *this *= r;
64 	}
65 
operator ==(const Vector & U,const Vector & v)66 	bool operator==(const Vector &U, const Vector &v)
67 	{
68 		if(U.x == v.x && U.y == v.y && U.z == v.z)
69 			return true;
70 		else
71 			return false;
72 	}
73 
operator !=(const Vector & U,const Vector & v)74 	bool operator!=(const Vector &U, const Vector &v)
75 	{
76 		if(U.x != v.x || U.y != v.y || U.z != v.z)
77 			return true;
78 		else
79 			return false;
80 	}
81 
operator >(const Vector & u,const Vector & v)82 	bool operator>(const Vector &u, const Vector &v)
83 	{
84 		if((u^2) > (v^2))
85 			return true;
86 		else
87 			return false;
88 	}
89 
operator <(const Vector & u,const Vector & v)90 	bool operator<(const Vector &u, const Vector &v)
91 	{
92 		if((u^2) < (v^2))
93 			return true;
94 		else
95 			return false;
96 	}
97 
operator +(const Vector & u,const Vector & v)98 	Vector operator+(const Vector &u, const Vector &v)
99 	{
100 		return Vector(u.x + v.x, u.y + v.y, u.z + v.z);
101 	}
102 
operator -(const Vector & u,const Vector & v)103 	Vector operator-(const Vector &u, const Vector &v)
104 	{
105 		return Vector(u.x - v.x, u.y - v.y, u.z - v.z);
106 	}
107 
operator *(const Vector & u,const Vector & v)108 	float operator*(const Vector &u, const Vector &v)
109 	{
110 		return u.x * v.x + u.y * v.y + u.z * v.z;
111 	}
112 
operator *(float s,const Vector & v)113 	Vector operator*(float s, const Vector &v)
114 	{
115 		return Vector(s * v.x, s * v.y, s * v.z);
116 	}
117 
operator *(const Vector & v,float s)118 	Vector operator*(const Vector &v, float s)
119 	{
120 		return Vector(v.x * s, v.y * s, v.z * s);
121 	}
122 
operator /(const Vector & v,float s)123 	Vector operator/(const Vector &v, float s)
124 	{
125 		float r = 1.0f / s;
126 
127 		return Vector(v.x * r, v.y * r, v.z * r);
128 	}
129 
operator ^(const Vector & u,const Vector & v)130 	float operator^(const Vector &u, const Vector &v)
131 	{
132 		return acos(u / Vector::N(u) * v / Vector::N(v));
133 	}
134 
operator %(const Vector & u,const Vector & v)135 	Vector operator%(const Vector &u, const Vector &v)
136 	{
137 		return Vector(u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x);
138 	}
139 
operator *(const Matrix & M,const Vector & v)140 	Vector operator*(const Matrix &M, const Vector &v)
141 	{
142 		return Vector(M(1, 1) * v.x + M(1, 2) * v.y + M(1, 3) * v.z,
143 		              M(2, 1) * v.x + M(2, 2) * v.y + M(2, 3) * v.z,
144 		              M(3, 1) * v.x + M(3, 2) * v.y + M(3, 3) * v.z);
145 	}
146 
operator *(const Vector & v,const Matrix & M)147 	Vector operator*(const Vector &v, const Matrix &M)
148 	{
149 		return Vector(v.x * M(1, 1) + v.y * M(2, 1) + v.z * M(3, 1) + M(4, 1),
150 		              v.x * M(1, 2) + v.y * M(2, 2) + v.z * M(3, 2) + M(4, 2),
151 		              v.x * M(1, 3) + v.y * M(2, 3) + v.z * M(3, 3) + M(4, 3));
152 	}
153 
operator *=(Vector & v,const Matrix & M)154 	Vector &operator*=(Vector &v, const Matrix &M)
155 	{
156 		return v = v * M;
157 	}
158 
N(const Vector & v)159 	float Vector::N(const Vector &v)
160 	{
161 		return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
162 	}
163 
N2(const Vector & v)164 	float Vector::N2(const Vector &v)
165 	{
166 		return v.x*v.x + v.y*v.y + v.z*v.z;
167 	}
168 
lerp(const Vector & u,const Vector & v,float t)169 	Vector lerp(const Vector &u, const Vector &v, float t)
170 	{
171 		return Vector(u.x + t * (v.x - u.x),
172 		              u.y + t * (v.y - u.y),
173 		              u.z + t * (v.z - u.z));
174 	}
175 }
176