Subversion Repositories gelsvn

Rev

Rev 89 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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