Figure 3: Operator functions for the GLpoint4 and GLvector3 classes

//********************************************************
// OpenGL reusable template classes GLpoint4 and GLvector3  
// By G.Bavestrelli,    Copyright Techint S.p.A. 1998-1999
// Any feedback is welcome:
// You can contact me at  : giovanni.bavestrelli@pomini.it
//********************************************************

// These are some operators between GLpoint4 and GLvector3

// Sum of vectors
template <class Numeric> 
inline GLvector3<Numeric> operator + 
   (const GLvector3<Numeric> & V1, const GLvector3<Numeric> & V2)
{ 
   return GLvector3<Numeric>(V1.x+V2.x, V1.y+V2.y, V1.z+V2.z); 
}

// Subtraction between vectors
template <class Numeric> 
inline GLvector3<Numeric> operator - 
(const GLvector3<Numeric> & V1, const GLvector3<Numeric> & V2)
{ 
   return GLvector3<Numeric>(V1.x-V2.x, V1.y-V2.y, V1.z-V2.z); 
}

// Point + vector
template <class Numeric>
inline GLpoint4<Numeric> operator + 
(const GLpoint4<Numeric> & P, const GLvector3<Numeric> & V) 
{ 
   return GLpoint4<Numeric>(P.x+V.x, P.y+V.y, P.z+V.z); 
}

// Cross product between vectors
template <class Numeric> 
inline GLvector3<Numeric> operator * 
(const GLvector3<Numeric> & V1, const GLvector3<Numeric> & V2) 
{ 
   return GLvector3<Numeric>(V1.y*V2.z-V2.y*V1.z, 
                             V2.x*V1.z-V1.x*V2.z, 
                             V1.x*V2.y-V2.x*V1.y); 
}

// Explicit Cross Product between vectors
template <class Numeric> 
inline GLvector3<Numeric> 
CrossProduct(const GLvector3<Numeric> & V1, 
             const GLvector3<Numeric> & V2)
{
   return GLvector3<Numeric>(V1.y*V2.z-V2.y*V1.z, 
                             V2.x*V1.z-V1.x*V2.z, 
                             V1.x*V2.y-V2.x*V1.y); 
}

// Dot product between vectors
template <class Numeric>
inline Numeric DotProduct(const GLvector3<Numeric> & V1,
                          const GLvector3<Numeric> & V2)
{ 
   return V1.x*V2.x+V1.y*V2.y+V1.z*V2.z; 
}

// Normalized Cross Product between vectors
template <class Numeric>
GLvector3<Numeric> 
NormalizedCrossProduct(const GLvector3<Numeric> & V1,
                       const GLvector3<Numeric> & V2)
{
   GLvector3<Numeric> V(V1.y*V2.z-V2.y*V1.z, 
                        V2.x*V1.z-V1.x*V2.z, 
                        V1.x*V2.y-V2.x*V1.y);  
   V.Normalize();
   return V;
}

// Difference between points yielding a vector
template <class Numeric>
GLvector3<Numeric> operator - (const GLpoint4<Numeric> & C1, 
                               const GLpoint4<Numeric> & C2) 
                               throw(std::runtime_error)
{ 
   ASSERT(C1.w!=0 && C2.w!=0); // Check that points are not 
                                  // at infinity
   if (C1.w==0 || C2.w==0) 
      throw std::runtime_error
         ("Cannot subtract points if any are at infinity");
   else if (C1.w==1 && C2.w==1) 
      return GLvector3<Numeric>(C1.x-C2.x, 
                                C1.y-C2.y, 
                                C1.z-C2.z);
   else
      return GLvector3<Numeric>(C1.x/C1.w-C2.x/C2.w, 
                                C1.y/C1.w-C2.y/C2.w, 
                                C1.z/C1.w-C2.z/C2.w);
}