667 |
khor |
1 |
/* ----------------------------------------------------------------------- *
|
|
|
2 |
* This file is part of GEL, http://www.imm.dtu.dk/GEL
|
|
|
3 |
* Copyright (C) the authors and DTU Informatics
|
|
|
4 |
* For license and list of authors, see ../../doc/intro.pdf
|
|
|
5 |
* ----------------------------------------------------------------------- */
|
|
|
6 |
|
|
|
7 |
/** @file UnitVector.h
|
|
|
8 |
* @brief Unitvector coded with two angles - possibly not efficient.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
#ifndef __CGLA_UNITVECTOR_H__
|
|
|
12 |
#define __CGLA_UNITVECTOR_H__
|
|
|
13 |
|
|
|
14 |
#include "CGLA.h"
|
|
|
15 |
#include "Vec3f.h"
|
|
|
16 |
#include "TableTrigonometry.h"
|
|
|
17 |
|
|
|
18 |
namespace CGLA
|
|
|
19 |
{
|
|
|
20 |
namespace TT=TableTrigonometry;
|
|
|
21 |
|
|
|
22 |
/** \brief The UnitVector stores a unit length vector as two angles.
|
|
|
23 |
|
|
|
24 |
A vector stored as two (fix point) angles is much smaller than
|
|
|
25 |
vector stored in the usual way. On a 32 bit architecture this
|
|
|
26 |
class should take up four bytes. not too bad. */
|
|
|
27 |
class UnitVector
|
|
|
28 |
{
|
|
|
29 |
TT::Angle theta, phi;
|
|
|
30 |
|
|
|
31 |
void encode(const Vec3f& v)
|
|
|
32 |
{
|
|
|
33 |
theta = TT::t_atan(std::sqrt(CGLA::sqr(v[0])+CGLA::sqr(v[1])), v[2]);
|
|
|
34 |
phi = TT::t_atan(v[0],v[1]);
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
public:
|
|
|
38 |
|
|
|
39 |
/// Construct unitvector from normal vector
|
|
|
40 |
explicit UnitVector(const Vec3f& v) {encode(v);}
|
|
|
41 |
|
|
|
42 |
/// Construct default unit vector
|
|
|
43 |
explicit UnitVector(): theta(0), phi(0) {}
|
|
|
44 |
|
|
|
45 |
/// Get theta angle
|
|
|
46 |
float t() const {return TT::angle2float(theta);}
|
|
|
47 |
|
|
|
48 |
/// Get phi angle
|
|
|
49 |
float f() const {return TT::angle2float(phi);}
|
|
|
50 |
|
|
|
51 |
/// Reconstruct Vec3f from unit vector
|
|
|
52 |
operator Vec3f() const
|
|
|
53 |
{
|
|
|
54 |
float costf = TT::t_cos(theta);
|
|
|
55 |
return Vec3f(TT::t_cos(phi)*costf ,
|
|
|
56 |
TT::t_sin(phi)*costf,
|
|
|
57 |
TT::t_sin(theta));
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/// Test for equality.
|
|
|
61 |
bool operator==(const UnitVector& u) const
|
|
|
62 |
{
|
|
|
63 |
return theta == u.theta && phi == u.phi;
|
|
|
64 |
}
|
|
|
65 |
};
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
/// Inline output operator.
|
|
|
69 |
inline std::ostream& operator<<(std::ostream& os, const UnitVector& u)
|
|
|
70 |
{
|
|
|
71 |
os << "<v=" << u.t() << " h=" << u.f() << ">";
|
|
|
72 |
return os;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
}
|
|
|
76 |
#endif
|