1// 2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved. 3// Use of this source code is governed by a BSD-style license that can be 4// found in the LICENSE file. 5// 6 7#include "Vector.h" 8 9#include <math.h> 10 11Vector2::Vector2() 12 : x(0.0), 13 y(0.0) 14{ 15} 16 17Vector2::Vector2(float x, float y) 18 : x(x), 19 y(y) 20{ 21} 22 23float Vector2::length(const Vector2 &vec) 24{ 25 float lenSquared = lengthSquared(vec); 26 return (lenSquared != 0.0f) ? sqrt(lenSquared) : 0.0f; 27} 28 29float Vector2::lengthSquared(const Vector2 &vec) 30{ 31 return vec.x * vec.x + 32 vec.y * vec.y; 33} 34 35Vector2 Vector2::normalize(const Vector2 &vec) 36{ 37 Vector2 ret(0.0f, 0.0f); 38 float len = length(vec); 39 if (len != 0.0f) 40 { 41 float invLen = 1.0f / len; 42 ret.x = vec.x * invLen; 43 ret.y = vec.y * invLen; 44 } 45 return ret; 46} 47 48Vector3::Vector3() 49 : x(0.0), 50 y(0.0), 51 z(0.0) 52{ 53} 54 55Vector3::Vector3(float x, float y, float z) 56 : x(x), 57 y(y), 58 z(z) 59{ 60} 61 62float Vector3::length(const Vector3 &vec) 63{ 64 float lenSquared = lengthSquared(vec); 65 return (lenSquared != 0.0f) ? sqrt(lenSquared) : 0.0f; 66} 67 68float Vector3::lengthSquared(const Vector3 &vec) 69{ 70 return vec.x * vec.x + 71 vec.y * vec.y + 72 vec.z * vec.z; 73} 74 75Vector3 Vector3::normalize(const Vector3 &vec) 76{ 77 Vector3 ret(0.0f, 0.0f, 0.0f); 78 float len = length(vec); 79 if (len != 0.0f) 80 { 81 float invLen = 1.0f / len; 82 ret.x = vec.x * invLen; 83 ret.y = vec.y * invLen; 84 ret.z = vec.z * invLen; 85 } 86 return ret; 87} 88 89float Vector3::dot(const Vector3 &a, const Vector3 &b) 90{ 91 return a.x * b.x + 92 a.y * b.y + 93 a.z * b.z; 94} 95 96Vector3 Vector3::cross(const Vector3 &a, const Vector3 &b) 97{ 98 return Vector3(a.y * b.z - a.z * b.y, 99 a.z * b.x - a.x * b.z, 100 a.x * b.y - a.y * b.x); 101} 102 103Vector3 operator*(const Vector3 &a, const Vector3 &b) 104{ 105 return Vector3(a.x * b.x, 106 a.y * b.y, 107 a.z * b.z); 108} 109 110Vector3 operator*(const Vector3 &a, const float& b) 111{ 112 return Vector3(a.x * b, 113 a.y * b, 114 a.z * b); 115} 116 117Vector3 operator/(const Vector3 &a, const Vector3 &b) 118{ 119 return Vector3(a.x / b.x, 120 a.y / b.y, 121 a.z / b.z); 122} 123 124Vector3 operator/(const Vector3 &a, const float& b) 125{ 126 return Vector3(a.x / b, 127 a.y / b, 128 a.z / b); 129} 130 131Vector3 operator+(const Vector3 &a, const Vector3 &b) 132{ 133 return Vector3(a.x + b.x, 134 a.y + b.y, 135 a.z + b.z); 136} 137 138Vector3 operator-(const Vector3 &a, const Vector3 &b) 139{ 140 return Vector3(a.x - b.x, 141 a.y - b.y, 142 a.z - b.z); 143} 144 145Vector4::Vector4() 146 : x(0.0f), 147 y(0.0f), 148 z(0.0f), 149 w(0.0f) 150{ 151} 152 153Vector4::Vector4(float x, float y, float z, float w) 154 : x(x), 155 y(y), 156 z(z), 157 w(w) 158{ 159} 160 161float Vector4::length(const Vector4 &vec) 162{ 163 float lenSquared = lengthSquared(vec); 164 return (lenSquared != 0.0f) ? sqrt(lenSquared) : 0.0f; 165} 166 167float Vector4::lengthSquared(const Vector4 &vec) 168{ 169 return vec.x * vec.x + 170 vec.y * vec.y + 171 vec.z * vec.z + 172 vec.w * vec.w; 173} 174 175Vector4 Vector4::normalize(const Vector4 &vec) 176{ 177 Vector4 ret(0.0f, 0.0f, 0.0f, 1.0f); 178 if (vec.w != 0.0f) 179 { 180 float invLen = 1.0f / vec.w; 181 ret.x = vec.x * invLen; 182 ret.y = vec.y * invLen; 183 ret.z = vec.z * invLen; 184 } 185 return ret; 186} 187 188float Vector4::dot(const Vector4 &a, const Vector4 &b) 189{ 190 return a.x * b.x + 191 a.y * b.y + 192 a.z * b.z + 193 a.w * b.w; 194} 195