Subversion Repositories gelsvn

Rev

Rev 12 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 bj 1
#ifndef __CGLA_UNITVECTOR_H_
2
#define __CGLA_UNITVECTOR_H_
3
 
4
#include "CGLA.h"
5
#include "Vec3f.h"
6
#include "TableTrigonometry.h"
7
 
8
namespace CGLA
9
{
10
	namespace TT=TableTrigonometry;
11
 
12
	/** The UnitVector stores a unit length vector as two angles.
13
 
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)
22
		{
23
#ifndef OLD_C_HEADERS
24
			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]);
29
		}
30
 
31
	public:
32
 
33
		/// Construct unitvector from normal vector
34
		explicit UnitVector(const Vec3f& v) {encode(v);}
35
 
36
		/// Construct default unit vector
37
		explicit UnitVector(): theta(0), phi(0) {}
38
 
39
		/// Get theta angle
40
		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
 
61
 
62
	/// Inline output operator.
63
	inline std::ostream& operator<<(std::ostream& os, const UnitVector& u)
64
	{
65
		os << "<v=" << u.t() << " h=" << u.f() << ">";
66
		return os;
67
	}
68
 
69
}
70
#endif