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 #ifndef Vector_hpp 16 #define Vector_hpp 17 18 namespace sw 19 { 20 struct Point; 21 struct Matrix; 22 struct Plane; 23 24 struct Vector 25 { 26 Vector(); 27 Vector(const int i); 28 Vector(const Vector &v); 29 Vector(const Point &p); 30 Vector(float v_x, float v_y, float v_z); 31 32 Vector &operator=(const Vector &v); 33 34 union 35 { 36 float v[3]; 37 38 struct 39 { 40 float x; 41 float y; 42 float z; 43 }; 44 }; 45 46 float &operator[](int i); 47 float &operator()(int i); 48 49 const float &operator[](int i) const; 50 const float &operator()(int i) const; 51 52 Vector operator+() const; 53 Vector operator-() const; 54 55 Vector &operator+=(const Vector &v); 56 Vector &operator-=(const Vector &v); 57 Vector &operator*=(float s); 58 Vector &operator/=(float s); 59 60 friend bool operator==(const Vector &u, const Vector &v); 61 friend bool operator!=(const Vector &u, const Vector &v); 62 63 friend Vector operator+(const Vector &u, const Vector &v); 64 friend Vector operator-(const Vector &u, const Vector &v); 65 friend float operator*(const Vector &u, const Vector &v); // Dot product 66 friend Vector operator*(float s, const Vector &v); 67 friend Vector operator*(const Vector &v, float s); 68 friend Vector operator/(const Vector &v, float s); 69 friend float operator^(const Vector &u, const Vector &v); // Angle between vectors 70 friend Vector operator%(const Vector &u, const Vector &v); // Cross product 71 72 friend Vector operator*(const Matrix &M, const Vector& v); 73 friend Vector operator*(const Vector &v, const Matrix &M); 74 friend Vector &operator*=(Vector &v, const Matrix &M); 75 76 static float N(const Vector &v); // Norm 77 static float N2(const Vector &v); // Squared norm 78 79 static Vector mirror(const Vector &v, const Plane &p); 80 static Vector reflect(const Vector &v, const Plane &p); 81 static Vector lerp(const Vector &u, const Vector &v, float t); 82 }; 83 } 84 85 #include "Point.hpp" 86 87 namespace sw 88 { Vector()89 inline Vector::Vector() 90 { 91 } 92 Vector(const int i)93 inline Vector::Vector(const int i) 94 { 95 const float s = (float)i; 96 97 x = s; 98 y = s; 99 z = s; 100 } 101 Vector(const Vector & v)102 inline Vector::Vector(const Vector &v) 103 { 104 x = v.x; 105 y = v.y; 106 z = v.z; 107 } 108 Vector(const Point & P)109 inline Vector::Vector(const Point &P) 110 { 111 x = P.x; 112 y = P.y; 113 z = P.z; 114 } 115 Vector(float v_x,float v_y,float v_z)116 inline Vector::Vector(float v_x, float v_y, float v_z) 117 { 118 x = v_x; 119 y = v_y; 120 z = v_z; 121 } 122 operator =(const Vector & v)123 inline Vector &Vector::operator=(const Vector &v) 124 { 125 x = v.x; 126 y = v.y; 127 z = v.z; 128 129 return *this; 130 } 131 operator ()(int i)132 inline float &Vector::operator()(int i) 133 { 134 return v[i]; 135 } 136 operator [](int i)137 inline float &Vector::operator[](int i) 138 { 139 return v[i]; 140 } 141 operator ()(int i) const142 inline const float &Vector::operator()(int i) const 143 { 144 return v[i]; 145 } 146 operator [](int i) const147 inline const float &Vector::operator[](int i) const 148 { 149 return v[i]; 150 } 151 } 152 153 #endif // Vector_hpp 154