// gmVector3.h - 3 element vector class
//
// libgm++: Graphics Math Library
// Ferdi Scheepers and Stephen F May
// 15 June 1994

#ifndef GMVECTOR3_H
#define GMVECTOR3_H

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "gmUtils.h"

class gmVector3 {

protected:
  float v_[3];

public:
  gmVector3();
  gmVector3(const gmVector3&);
  gmVector3(float, float, float);

  // array access

  float& operator [](int);
  const float& operator [](int) const;

  // assignment

  gmVector3& assign(float, float, float);
  gmVector3& operator =(const gmVector3&);

  // math operators

  gmVector3& operator +=(const gmVector3&);
  gmVector3& operator -=(const gmVector3&);
  gmVector3& operator *=(float);
  gmVector3& operator /=(float);

  gmVector3 operator +(const gmVector3&) const;
  gmVector3 operator -(const gmVector3&) const;
  gmVector3 operator -() const;
  gmVector3 operator *(float) const;
  gmVector3 operator /(float) const;

friend gmVector3 operator *(float, const gmVector3&);

  bool operator ==(const gmVector3&) const;
  bool operator !=(const gmVector3&) const;

  // operations

  gmVector3& clamp(float, float);
  float length() const;
  float lengthSquared() const;
  gmVector3& normalize();

  void copyTo(float [3]) const;

friend gmVector3 cross(const gmVector3&, const gmVector3&);
friend float distance(const gmVector3&, const gmVector3&);
friend float distanceSquared(const gmVector3&, const gmVector3&);
friend float dot(const gmVector3&, const gmVector3&);
friend gmVector3 lerp(float, const gmVector3&, const gmVector3&);

  // output

friend std::ostream & operator << ( std::ostream &, const gmVector3 & );
};

// CONSTRUCTORS

inline gmVector3::gmVector3()
{
  v_[0] = v_[1] = v_[2] = 0;
}

inline gmVector3::gmVector3(const gmVector3& v)
{
  v_[0] = v.v_[0]; v_[1] = v.v_[1]; v_[2] = v.v_[2];
}

inline gmVector3::gmVector3(float x, float y, float z)
{
  v_[0] = x; v_[1] = y; v_[2] = z;
}

// ARRAY ACCESS

inline float& gmVector3::operator [](int i) 
{
  assert(i == 0 || i == 1 || i == 2);
  return v_[i];
}

inline const float& gmVector3::operator [](int i) const
{
  assert(i == 0 || i == 1 || i == 2);
  return v_[i];
}

// ASSIGNMENT

inline gmVector3& gmVector3::assign(float x, float y, float z)
{
  v_[0] = x; v_[1] = y; v_[2] = z;
  return *this;
}

inline gmVector3& gmVector3::operator =(const gmVector3& v)
{
  v_[0] = v[0]; v_[1] = v[1]; v_[2] = v[2];
  return *this;
}

// MATH OPERATORS

inline gmVector3& gmVector3::operator +=(const gmVector3& v)
{
  v_[0] += v[0]; v_[1] += v[1]; v_[2] += v[2];
  return *this;
}

inline gmVector3& gmVector3::operator -=(const gmVector3& v)
{
  v_[0] -= v[0]; v_[1] -= v[1]; v_[2] -= v[2];
  return *this;
}

inline gmVector3& gmVector3::operator *=(float c)
{
  v_[0] *= c; v_[1] *= c; v_[2] *= c;
  return *this;
}

inline gmVector3& gmVector3::operator /=(float c)
{
  assert(!gmIsZero(c));
  v_[0] /= c; v_[1] /= c; v_[2] /= c;
  return *this;
}

inline gmVector3 gmVector3::operator +(const gmVector3& v) const
{
  return gmVector3(v_[0] + v[0], v_[1] + v[1], v_[2] + v[2]);
}

inline gmVector3 gmVector3::operator -(const gmVector3& v) const
{
  return gmVector3(v_[0] - v[0], v_[1] - v[1], v_[2] - v[2]);
}

inline gmVector3 gmVector3::operator -() const
{
  return gmVector3(-v_[0], -v_[1], -v_[2]);
}

inline gmVector3 gmVector3::operator *(float c) const
{
  return gmVector3(v_[0] * c, v_[1] * c, v_[2] * c);
}

inline gmVector3 gmVector3::operator /(float c) const
{
  assert(!gmIsZero(c));
  return gmVector3(v_[0] / c, v_[1] / c, v_[2] / c);
}

inline gmVector3 operator *(float c, const gmVector3& v)
{
  return gmVector3(c * v[0], c * v[1], c * v[2]);
}

inline bool gmVector3::operator ==(const gmVector3& v) const
{
  return
    (gmFuzEQ(v_[0], v[0]) && gmFuzEQ(v_[1], v[1]) && gmFuzEQ(v_[2], v[2]));
}

inline bool gmVector3::operator !=(const gmVector3& v) const
{
  return (!(*this == v));
}

// OPERATIONS

inline gmVector3& gmVector3::clamp(float lo, float hi)
{
  gmClamp(v_[0], lo, hi); gmClamp(v_[1], lo, hi); gmClamp(v_[2], lo, hi);
  return *this;
}

inline float gmVector3::length() const
{
  return sqrt(gmSqr(v_[0]) + gmSqr(v_[1]) + gmSqr(v_[2]));
}

inline float gmVector3::lengthSquared() const
{
  return gmSqr(v_[0]) + gmSqr(v_[1]) + gmSqr(v_[2]);
}

inline gmVector3& gmVector3::normalize()
{
  float len = length();
  assert(!gmIsZero(len));
  *this /= len;
  return *this;
}

inline void gmVector3::copyTo(float f[3]) const
{
  f[0] = v_[0]; f[1] = v_[1]; f[2] = v_[2];
}

inline gmVector3 cross(const gmVector3& v1, const gmVector3& v2)
{
  return gmVector3(v1[1] * v2[2] - v1[2] * v2[1],
                   v1[2] * v2[0] - v1[0] * v2[2],
                   v1[0] * v2[1] - v1[1] * v2[0]);
}

inline float distance(const gmVector3& v1, const gmVector3& v2)
{
  return
    sqrt(gmSqr(v1[0] - v2[0]) + gmSqr(v1[1] - v2[1]) + gmSqr(v1[2] - v2[2]));
}

inline float distanceSquared(const gmVector3& v1, const gmVector3& v2)
{
  return gmSqr(v1[0] - v2[0]) + gmSqr(v1[1] - v2[1]) + gmSqr(v1[2] - v2[2]);
}

inline float dot(const gmVector3& v1, const gmVector3& v2)
{
  return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
}

inline gmVector3 lerp(float f, const gmVector3& v1, const gmVector3& v2)
{
  return v1 + ((v2 - v1) * f);
}

// OUTPUT

inline std::ostream & operator << ( std::ostream& os, const gmVector3& v)
{
  os << "< " << v[0] << " " << v[1] << " " << v[2] << " >";
  return os;
}

#endif 



