moved most Vector functionality into the header so it could be inlined

git-svn-id: svn://anubis/fart/trunk@307 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2010-09-28 16:55:39 +00:00
parent 9e4191fb61
commit 3f0d6cd83e
2 changed files with 168 additions and 186 deletions

View File

@ -10,20 +10,6 @@ const Vector Vector::X(1, 0, 0);
const Vector Vector::Y(0, 1, 0);
const Vector Vector::Z(0, 0, 1);
Vector::Vector()
{
m_array[0] = 0.0;
m_array[1] = 0.0;
m_array[2] = 0.0;
}
Vector::Vector(double x, double y, double z)
{
m_array[0] = x;
m_array[1] = y;
m_array[2] = z;
}
Vector Vector::randomVector()
{
double x, y, z;
@ -36,157 +22,6 @@ Vector Vector::randomVector()
return Vector(x, y, z).normalize();
}
Vector & Vector::normalize()
{
double length = mag();
m_array[0] /= length;
m_array[1] /= length;
m_array[2] /= length;
return *this;
}
double Vector::mag() const
{
return sqrt(m_array[0] * m_array[0]
+ m_array[1] * m_array[1]
+ m_array[2] * m_array[2]);
}
double Vector::mag2() const
{
return m_array[0] * m_array[0]
+ m_array[1] * m_array[1]
+ m_array[2] * m_array[2];
}
double Vector::dist_to(const Vector & other) const
{
return (other - *this).mag();
}
Vector Vector::proj(const Vector & target) const
{
Vector target_normalized = target;
target_normalized.normalize();
return target_normalized * ((*this) % target_normalized);
}
Vector Vector::reflect(const Vector & target) const
{
Vector projected = proj(target);
Vector me_to_proj = projected - (*this);
return (*this) + me_to_proj * 2.0;
}
Vector Vector::getPerpendicular() const
{
Vector t = *this;
t.normalize();
Vector p = t * Vector(0, 0, 1);
if (p.mag() <= 0.1)
{
p = t * Vector(1, 0, 0);
}
return p;
}
Vector Vector::mult(const Vector & v2) const
{
Vector result;
result[0] = m_array[0] * v2.m_array[0];
result[1] = m_array[1] * v2.m_array[1];
result[2] = m_array[2] * v2.m_array[2];
return result;
}
Vector Vector::div(const Vector & v2) const
{
Vector result;
result[0] = m_array[0] / v2.m_array[0];
result[1] = m_array[1] / v2.m_array[1];
result[2] = m_array[2] / v2.m_array[2];
return result;
}
Vector Vector::operator-() const
{
Vector result;
result[0] = -m_array[0];
result[1] = -m_array[1];
result[2] = -m_array[2];
return result;
}
/* Compute the dot-product of two vectors */
double Vector::operator%(const Vector & v2) const
{
return m_array[0] * v2.m_array[0]
+ m_array[1] * v2.m_array[1]
+ m_array[2] * v2.m_array[2];
}
/* Compute the cross-product of two vectors */
Vector Vector::operator*(const Vector & v2) const
{
Vector result;
result[0] = m_array[1] * v2.m_array[2] - m_array[2] * v2.m_array[1];
result[1] = m_array[2] * v2.m_array[0] - m_array[0] * v2.m_array[2];
result[2] = m_array[0] * v2.m_array[1] - m_array[1] * v2.m_array[0];
return result;
}
Vector Vector::operator+(const Vector & v2) const
{
Vector result;
result[0] = m_array[0] + v2.m_array[0];
result[1] = m_array[1] + v2.m_array[1];
result[2] = m_array[2] + v2.m_array[2];
return result;
}
Vector Vector::operator-(const Vector & v2) const
{
Vector result;
result[0] = m_array[0] - v2.m_array[0];
result[1] = m_array[1] - v2.m_array[1];
result[2] = m_array[2] - v2.m_array[2];
return result;
}
Vector Vector::operator*(double scale) const
{
Vector result;
result[0] = m_array[0] * scale;
result[1] = m_array[1] * scale;
result[2] = m_array[2] * scale;
return result;
}
Vector Vector::operator/(double scale) const
{
Vector result;
result[0] = m_array[0] / scale;
result[1] = m_array[0] / scale;
result[2] = m_array[0] / scale;
return result;
}
Vector & Vector::operator+=(const Vector & v2)
{
m_array[0] += v2.m_array[0];
m_array[1] += v2.m_array[1];
m_array[2] += v2.m_array[2];
return *this;
}
Vector & Vector::operator-=(const Vector & v2)
{
m_array[0] -= v2.m_array[0];
m_array[1] -= v2.m_array[1];
m_array[2] -= v2.m_array[2];
return *this;
}
std::ostream & operator<<(std::ostream & out, const Vector & v)
{
out << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]";

View File

@ -2,35 +2,182 @@
#ifndef VECTOR_H
#define VECTOR_H VECTOR_H
#include <math.h> /* sqrt() */
#include <iostream>
class Vector
{
public:
Vector();
Vector(double x, double y, double z);
static Vector randomVector();
Vector()
{
m_array[0] = 0.0;
m_array[1] = 0.0;
m_array[2] = 0.0;
}
Vector(double x, double y, double z)
{
m_array[0] = x;
m_array[1] = y;
m_array[2] = z;
}
Vector & normalize()
{
double length = mag();
m_array[0] /= length;
m_array[1] /= length;
m_array[2] /= length;
return *this;
}
double mag() const
{
return sqrt(m_array[0] * m_array[0]
+ m_array[1] * m_array[1]
+ m_array[2] * m_array[2]);
}
double mag2() const
{
return m_array[0] * m_array[0]
+ m_array[1] * m_array[1]
+ m_array[2] * m_array[2];
}
double dist_to(const Vector & other) const
{
return (other - *this).mag();
}
Vector proj(const Vector & target) const
{
Vector target_normalized = target;
target_normalized.normalize();
return target_normalized * ((*this) % target_normalized);
}
Vector reflect(const Vector & target) const
{
Vector projected = proj(target);
Vector me_to_proj = projected - (*this);
return (*this) + me_to_proj * 2.0;
}
Vector getPerpendicular() const
{
Vector t = *this;
t.normalize();
Vector p = t * Vector(0, 0, 1);
if (p.mag() <= 0.1)
{
p = t * Vector(1, 0, 0);
}
return p;
}
Vector mult(const Vector & v2) const
{
Vector result;
result[0] = m_array[0] * v2.m_array[0];
result[1] = m_array[1] * v2.m_array[1];
result[2] = m_array[2] * v2.m_array[2];
return result;
}
Vector div(const Vector & v2) const
{
Vector result;
result[0] = m_array[0] / v2.m_array[0];
result[1] = m_array[1] / v2.m_array[1];
result[2] = m_array[2] / v2.m_array[2];
return result;
}
Vector operator-() const
{
Vector result;
result[0] = -m_array[0];
result[1] = -m_array[1];
result[2] = -m_array[2];
return result;
}
/* Compute the dot-product of two vectors */
double operator%(const Vector & v2) const
{
return m_array[0] * v2.m_array[0]
+ m_array[1] * v2.m_array[1]
+ m_array[2] * v2.m_array[2];
}
/* Compute the cross-product of two vectors */
Vector operator*(const Vector & v2) const
{
Vector result;
result[0] = m_array[1] * v2.m_array[2] - m_array[2] * v2.m_array[1];
result[1] = m_array[2] * v2.m_array[0] - m_array[0] * v2.m_array[2];
result[2] = m_array[0] * v2.m_array[1] - m_array[1] * v2.m_array[0];
return result;
}
Vector operator+(const Vector & v2) const
{
Vector result;
result[0] = m_array[0] + v2.m_array[0];
result[1] = m_array[1] + v2.m_array[1];
result[2] = m_array[2] + v2.m_array[2];
return result;
}
Vector operator-(const Vector & v2) const
{
Vector result;
result[0] = m_array[0] - v2.m_array[0];
result[1] = m_array[1] - v2.m_array[1];
result[2] = m_array[2] - v2.m_array[2];
return result;
}
Vector operator*(double scale) const
{
Vector result;
result[0] = m_array[0] * scale;
result[1] = m_array[1] * scale;
result[2] = m_array[2] * scale;
return result;
}
Vector operator/(double scale) const
{
Vector result;
result[0] = m_array[0] / scale;
result[1] = m_array[0] / scale;
result[2] = m_array[0] / scale;
return result;
}
Vector & operator+=(const Vector & v2)
{
m_array[0] += v2.m_array[0];
m_array[1] += v2.m_array[1];
m_array[2] += v2.m_array[2];
return *this;
}
Vector & operator-=(const Vector & v2)
{
m_array[0] -= v2.m_array[0];
m_array[1] -= v2.m_array[1];
m_array[2] -= v2.m_array[2];
return *this;
}
double & operator[](int idx) { return m_array[idx]; }
double operator[](int idx) const { return m_array[idx]; }
Vector & normalize();
double mag() const;
double mag2() const;
double dist_to(const Vector & other) const;
Vector proj(const Vector & target) const;
Vector reflect(const Vector & target) const;
Vector getPerpendicular() const;
Vector mult(const Vector & v2) const;
Vector div(const Vector & v2) const;
Vector operator-() const;
double operator%(const Vector & v2) const;
Vector operator*(const Vector & v2) const;
Vector operator+(const Vector & v2) const;
Vector operator-(const Vector & v2) const;
Vector operator*(double scale) const;
Vector operator/(double scale) const;
Vector & operator+=(const Vector & v2);
Vector & operator-=(const Vector & v2);
static Vector randomVector();
static const Vector X;
static const Vector Y;