1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 Vector Vector::operator+() const
27 Vector Vector::operator-() const
29 return Vector(-x, -y, -z);
32 Vector &Vector::operator+=(const Vector &v)
41 Vector &Vector::operator-=(const Vector &v)
50 Vector &Vector::operator*=(float s)
59 Vector &Vector::operator/=(float s)
66 bool operator==(const Vector &U, const Vector &v)
68 if(U.x == v.x && U.y == v.y && U.z == v.z)
74 bool operator!=(const Vector &U, const Vector &v)
76 if(U.x != v.x || U.y != v.y || U.z != v.z)
82 bool operator>(const Vector &u, const Vector &v)
90 bool operator<(const Vector &u, const Vector &v)
98 Vector operator+(const Vector &u, const Vector &v)
100 return Vector(u.x + v.x, u.y + v.y, u.z + v.z);
103 Vector operator-(const Vector &u, const Vector &v)
105 return Vector(u.x - v.x, u.y - v.y, u.z - v.z);
108 float operator*(const Vector &u, const Vector &v)
110 return u.x * v.x + u.y * v.y + u.z * v.z;
113 Vector operator*(float s, const Vector &v)
115 return Vector(s * v.x, s * v.y, s * v.z);
118 Vector operator*(const Vector &v, float s)
120 return Vector(v.x * s, v.y * s, v.z * s);
123 Vector operator/(const Vector &v, float s)
127 return Vector(v.x * r, v.y * r, v.z * r);
130 float operator^(const Vector &u, const Vector &v)
132 return acos(u / Vector::N(u) * v / Vector::N(v));
135 Vector operator%(const Vector &u, const Vector &v)
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);
140 Vector operator*(const Matrix &M, const Vector &v)
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);
147 Vector operator*(const Vector &v, const Matrix &M)
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));
154 Vector &operator*=(Vector &v, const Matrix &M)
159 float Vector::N(const Vector &v)
161 return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
164 float Vector::N2(const Vector &v)
166 return v.x*v.x + v.y*v.y + v.z*v.z;
169 Vector lerp(const Vector &u, const Vector &v, float t)
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.x));