// -*- Mode: c++ -*-
// $Id: float4.C,v 1.4 1998/02/11 03:46:44 neel Exp $
// $Source: /u2/graphics/projects/6.838/gvis/gprims/float4.C,v $

static char 
__rcs_ref_float4_c(void)
{
   static char rcs_id_c[] = "$Header: /u2/graphics/projects/6.838/gvis/gprims/float4.C,v 1.4 1998/02/11 03:46:44 neel Exp $";
   return rcs_id_c[0];
}
// do not edit anything above this line
static char avoid_stupid_compiler_warnings = __rcs_ref_float4_c();

#include <iostream.h>
#include <math.h>
#include <assert.h>
#include "float4.H"
#include "float3.H"
#include "utils.H"


// constructors
inline
Float4::Float4( const Float4& c )
{ v[X] = c.v[X], v[Y] = c.v[Y], v[Z] = c.v[Z], v[3] = c.v[3]; }

inline
Float4::Float4( const float* a )
{ v[X] = a[X], v[Y] = a[Y], v[Z] = a[Z], v[3] = a[3]; }

inline
Float4::Float4( const double* a )
{ v[X] = a[X], v[Y] = a[Y], v[Z] = a[Z], v[3] = a[3]; }

// construct a plane from a point and a normal vector
inline
Float4::Float4( const Point3 &base, Vector3 norm )
{ 
  v[X] = norm.x(); v[Y] = norm.y(); v[Z] = norm.z(); 
  normal()->normalize(); v[3] = -normal()->dot( base );
}

// construct a plane from a point and a scalar
inline
Float4::Float4( const Point3 &pt, float s ) 
{
  v[X] = pt.x(); v[Y] = pt.y(); v[Z] = pt.z(); 
  v[3] = s;
}


// construct a plane from two points a, b 
// plane with normal ab, containing point b 
inline
Float4::Float4( const Point3 &a, const Point3 &b )
{ 
  // form vector from a to b
  v[X] = b.x() - a.x(); 
  v[Y] = b.y() - a.y(); 
  v[Z] = b.z() - a.z(); 
  
  // normalize vector AB
  normalize(); 
  
  // plane contains b 
  // ( v[X]*b.x + v[Y]*b.y + v[Z]*b.z );  
  v[3] = -dot( b );       
  
}


Float3*           
Float4::normal( void )
{ 
  return ( (Float3 *)this ); 
}

// normalize the vector
inline int         
Float4::normalize( void ) 
{
  float l = length(); 
  if(0==l) 
    return 0; 
  v[0] /= l;
  v[1] /= l;
  v[2] /= l;
  v[3] /= l;
  return 1;
}
  
// operator definitions / iostream
void 
Float4::print(ostream& os) const
{
  os << v[0] << " " << v[1] << " " << v[2] << " " << v[3];
}

ostream&
operator<<( ostream& os, const Float4& c )
{
  c.print( os );	
  return os;
}

istream&
operator>>( istream& is, Float4& f )
{
  return is >> f.v[0] >> f.v[1] >> f.v[2] >> f.v[3];
}


// numerical / geometric routines

// dots with plane normal
inline float 
Float4::dot( const Float3& f ) const
{ 
  return v[X] * f.x() + v[Y]*f.y() + v[Z]*f.z(); 
}


// distance from 3D input vector
inline float 
Float4::dist( const Float3& f ) const
{ 
  return v[X]*f.x() + v[Y]*f.y() + v[Z]*f.z() + v[3]; 
}

// raises a vector to a power
void            
Float4::power( double p ) 
{
  v[X] = pow ( v[X], p );
  v[Y] = pow ( v[Y], p );
  v[Z] = pow ( v[Z], p );
}




// useful: construction of plane

#if 0 

// given two vectors and point, return plane containing all three in r and
// signal success in return value
int
PlaneFromTwoVectorsAndPoint( Float4& r,
			     const Float3& a, const Float3& b,	
			     const Float3& p )
{
  Float3 nrm = a.cross( b );		// plane normal is cross
  if ( !nrm.normalize() ) return 0;	// must be normalizeable
  r = Float4( nrm.x(), nrm.y(), nrm.z(), -nrm.dot( p ) );
  return 1;
}

// return plane through three points, oriented up as abc in r and signal
// success in return value
int
PlaneFromThreePoints( Float4& r,
		      const Float3& a, const Float3& b,
		      const Float3& c )
{
  return PlaneFromTwoVectorsAndPoint( r, c - b, a - b, b );
}



#endif
