Subversion Repositories gelsvn

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12 jab 1
#ifndef __CGLA_UNITVECTOR_H__
2
#define __CGLA_UNITVECTOR_H__
2 bj 3
 
4
#include "CGLA.h"
5
#include "Vec3f.h"
6
#include "TableTrigonometry.h"
7
 
8
namespace CGLA
9
{
12 jab 10
  namespace TT=TableTrigonometry;
2 bj 11
 
12 jab 12
  /** The UnitVector stores a unit length vector as two angles.
2 bj 13
 
12 jab 14
  A vector stored as two (fix point) angles is much smaller than
15
  vector stored in the usual way. On a 32 bit architecture this
16
  class should take up four bytes. not too bad. */
17
  class UnitVector
18
    {
19
      TT::Angle theta, phi;
20
 
21
      void encode(const Vec3f& v)
2 bj 22
	{
12 jab 23
	  theta = TT::t_atan(std::sqrt(CGLA::sqr(v[0])+CGLA::sqr(v[1])), v[2]);
24
	  phi   = TT::t_atan(v[0],v[1]);
25
	}
2 bj 26
 
12 jab 27
    public:
2 bj 28
 
12 jab 29
      /// Construct unitvector from normal vector
30
      explicit UnitVector(const Vec3f& v) {encode(v);}
2 bj 31
 
12 jab 32
      /// Construct default unit vector
33
      explicit UnitVector(): theta(0), phi(0) {}
2 bj 34
 
12 jab 35
      /// Get theta angle
36
      float t() const {return TT::angle2float(theta);}
2 bj 37
 
12 jab 38
      /// Get phi angle
39
      float f() const {return TT::angle2float(phi);}
2 bj 40
 
12 jab 41
      /// Reconstruct Vec3f from unit vector
42
      operator Vec3f() const
43
	{
44
	  float costf = TT::t_cos(theta);
45
	  return Vec3f(TT::t_cos(phi)*costf , 
46
		       TT::t_sin(phi)*costf, 
47
		       TT::t_sin(theta));
48
	}
2 bj 49
 
12 jab 50
      /// Test for equality.
51
      bool operator==(const UnitVector& u) const
2 bj 52
	{
12 jab 53
	  return theta == u.theta && phi == u.phi;
2 bj 54
	}
12 jab 55
    };
2 bj 56
 
12 jab 57
 
58
  /// Inline output operator.
59
  inline std::ostream& operator<<(std::ostream& os, const UnitVector& u)
60
    {
61
      os << "<v=" << u.t() << " h=" << u.f() << ">";
62
      return os;
63
    }
64
 
2 bj 65
}
66
#endif