Subversion Repositories gelsvn

Rev

Rev 89 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 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
 
12 jab 11
#ifndef __CGLA_UNITVECTOR_H__
12
#define __CGLA_UNITVECTOR_H__
2 bj 13
 
14
#include "CGLA.h"
15
#include "Vec3f.h"
16
#include "TableTrigonometry.h"
17
 
18
namespace CGLA
19
{
12 jab 20
  namespace TT=TableTrigonometry;
2 bj 21
 
89 jab 22
  /** \brief The UnitVector stores a unit length vector as two angles.
2 bj 23
 
12 jab 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)
2 bj 32
	{
12 jab 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
	}
2 bj 36
 
12 jab 37
    public:
2 bj 38
 
12 jab 39
      /// Construct unitvector from normal vector
40
      explicit UnitVector(const Vec3f& v) {encode(v);}
2 bj 41
 
12 jab 42
      /// Construct default unit vector
43
      explicit UnitVector(): theta(0), phi(0) {}
2 bj 44
 
12 jab 45
      /// Get theta angle
46
      float t() const {return TT::angle2float(theta);}
2 bj 47
 
12 jab 48
      /// Get phi angle
49
      float f() const {return TT::angle2float(phi);}
2 bj 50
 
12 jab 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
	}
2 bj 59
 
12 jab 60
      /// Test for equality.
61
      bool operator==(const UnitVector& u) const
2 bj 62
	{
12 jab 63
	  return theta == u.theta && phi == u.phi;
2 bj 64
	}
12 jab 65
    };
2 bj 66
 
12 jab 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
 
2 bj 75
}
76
#endif