1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include "stdafx.h"
#include "Vector.h"
#include "Core/Str.h"
#include "Core/StrBuf.h"
namespace storm {
namespace geometry {
Vector::Vector() : x(0), y(0), z(0) {}
Vector::Vector(Float x, Float y, Float z) : x(x), y(y), z(z) {}
Vector::Vector(Point p) : x(p.x), y(p.y), z(0) {}
Float Vector::lengthSq() const {
return *this * *this;
}
Float Vector::length() const {
return sqrt(lengthSq());
}
Vector Vector::normalized() const {
Float len = length();
if (len == 0) {
return (*this);
} else {
return (*this) / len;
}
}
Float Vector::taxiLength() const {
return ::fabs(x) + ::fabs(y) + ::fabs(z);
}
wostream &operator <<(wostream &to, const Vector &s) {
return to << L"(" << s.x << L", " << s.y << L", " << s.z << L")";
}
void Vector::toS(StrBuf *to) const {
*to << S("(") << x << S(", ") << y << S(", ") << z << S(")");
}
Vector operator +(Vector a, Vector b) {
return Vector(a.x + b.x, a.y + b.y, a.z + b.z);
}
Vector operator -(Vector a, Vector b) {
return Vector(a.x - b.x, a.y - b.y, a.z - b.z);
}
Vector operator -(Vector a) {
return Vector(-a.x, -a.y, -a.z);
}
Vector &operator +=(Vector &a, Vector b) {
a.x += b.x;
a.y += b.y;
a.z += b.z;
return a;
}
Vector &operator -=(Vector &a, Vector b) {
a.x -= b.x;
a.y -= b.y;
a.z -= b.z;
return a;
}
Vector operator *(Vector a, Float b) {
return Vector(a.x * b, a.y * b, a.z * b);
}
Vector operator *(Float a, Vector b) {
return Vector(a * b.x, a * b.y, a * b.z);
}
Vector operator /(Vector a, Float b) {
return Vector(a.x / b, a.y / b, a.z / b);
}
Vector &operator *=(Vector &a, Float b) {
a.x *= b;
a.y *= b;
a.z *= b;
return a;
}
Vector &operator /=(Vector &a, Float b) {
a.x /= b;
a.y /= b;
a.z /= b;
return a;
}
// Dot and cross product.
Float operator *(Vector a, Vector b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
Vector operator /(Vector a, Vector b) {
return Vector(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y-b.x);
}
Vector abs(Vector a) {
return Vector(::fabs(a.x), ::fabs(a.y), ::fabs(a.z));
}
Vector project(Vector pt, Vector origin, Vector dir) {
pt -= origin;
float t = (pt * dir) / (dir * dir);
return origin + dir * t;
}
}
}
|