Subversion Repositories gelsvn

Rev

Rev 2 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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