[80]  1  /********************************************************************** <BR>


 2  This file is part of Crack dot Com's free source code release of


 3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for


 4  information about compiling & licensing issues visit this URL</a>


 5  <PRE> If that doesn't help, contact Jonathan Clark at


 6  golgotha_source@usa.net (Subject should have "GOLG" in it)


 7  ***********************************************************************/


 8 


 9  #ifndef __VECTOR_TEMPLATE_HPP_


 10  #define __VECTOR_TEMPLATE_HPP_


 11 


 12  #include "math/num_type.hh"


 13  #include <math.h>


 14 


 15  template <int dimension, class T>


 16  class i4_vector_template


 17  {


 18  public:


 19  T elt[dimension];


 20 


 21  i4_vector_class() {}


 22 


 23  i4_vector_class(const T v)


 24  {


 25  for (int i=0; i<dimension; i++)


 26  elt[i] = v;


 27  }


 28 


 29  i4_vector_class(const i4_vector_class &b)


 30  {


 31  memcpy(elt,b.elt,sizeof(*this));


 32  }


 33 


 34  i4_vector_class& operator=(const i4_vector_class &b)


 35  {


 36  for (int i=0; i<dimension; i++)


 37  elt[i] = b.elt[i];


 38 


 39  return *this;


 40  }


 41 


 42  i4_vector_class& operator+=(const i4_vector_class &b)


 43  {


 44  for (int i=0; i<dimension; i++)


 45  elt[i] += b.elt[i];


 46 


 47  return *this;


 48  }


 49 


 50 


 51  i4_vector_class& operator=(const i4_vector_class &b)


 52  {


 53  for (int i=0; i<dimension; i++)


 54  elt[i] = b.elt[i];


 55 


 56  return *this;


 57  }


 58 


 59 


 60  i4_vector_class& operator*=(const T b)


 61  {


 62  for (int i=0; i<dimension; i++)


 63  elt[i] *= b;


 64 


 65  return *this;


 66  }


 67 


 68 


 69  i4_vector_class& operator/=(const T b)


 70  {


 71  for (int i=0; i<dimension; i++)


 72  elt[i] /= b;


 73 


 74  return *this;


 75  }


 76 


 77 


 78  T dot(const i4_vector_class b) const


 79  {


 80  T ret = 0;


 81 


 82  for (int i=0; i<dimension; i++)


 83  ret += elt[i]*b.elt[i];


 84 


 85  return ret;


 86  }


 87 


 88 


 89  T length() const


 90  {


 91  return (sqrt(dot(*this)));


 92  }


 93 


 94 


 95  i4_vector_class& normalize()


 96  {


 97  T len = length();


 98 


 99  for (int i=0; i<dimension; i++)


 100  elt[i] /= len;


 101 


 102  return *this;


 103  }


 104 


 105  };


 106 


 107  template <class T>


 108  class i4_vector3_class : public i4_vector_class<3,T>


 109  {


 110  enum { x,y,z };


 111  public:


 112  i4_vector3_class() {};


 113  i4_vector3_class(i4_float _x, i4_float _y, i4_float _z)


 114  {


 115  elt[x]=_x;


 116  elt[y]=_y;


 117  elt[z]=_z;


 118  }


 119 


 120  const i4_vector3_class& cross(i4_vector3_class b)


 121  {


 122  i4_vector3_class temp(*this);


 123 


 124  elt[x] = temp.elt[y]*b.elt[z]  temp.elt[z]*b.elt[y];


 125  elt[y] = temp.elt[z]*b.elt[x]  temp.elt[x]*b.elt[z];


 126  elt[z] = temp.elt[x]*b.elt[y]  temp.elt[y]*b.elt[x];


 127 


 128  return *this;


 129  }


 130 


 131  void set(i4_float _x, i4_float _y, i4_float _z)


 132  {


 133  elt[x]=_x;


 134  elt[y]=_y;


 135  elt[z]=_z;


 136  }


 137  };


 138 


 139 


 140 


 141  typedef i4_vector3_class<i4_float> i4_3d_vector;


 142  typedef i4_vector_class<4,i4_float> i4_4d_vector;


 143 


 144 


 145  #endif

