Subversion Repositories gelsvn

Rev

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

Rev 570 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
 
1
#include <algorithm>
7
#include <algorithm>
2
#include "ArithVec3Float.h"
8
#include "ArithVec3Float.h"
3
#include "Vec3f.h"
9
#include "Vec3f.h"
4
#include "Vec3d.h"
10
#include "Vec3d.h"
5
 
11
 
6
using namespace std;
12
using namespace std;
7
 
13
 
8
namespace CGLA {
14
namespace CGLA {
9
 
15
 
10
		template<class T, class V>
16
		template<class T, class V>
11
		void ArithVec3Float<T,V>::get_spherical(T &theta, T &phi, T &rlen ) const
17
		void ArithVec3Float<T,V>::get_spherical(T &theta, T &phi, T &rlen ) const
12
		{  
18
		{  
13
				rlen = this->length();
19
				rlen = this->length();
14
				theta = acos((*this)[2]/rlen);    
20
				theta = acos((*this)[2]/rlen);    
15
				if ((*this)[0]>0)
21
				if ((*this)[0]>0)
16
						phi = atan((*this)[1]/(*this)[0]);
22
						phi = atan((*this)[1]/(*this)[0]);
17
				else 
23
				else 
18
						if ((*this)[0]<0)
24
						if ((*this)[0]<0)
19
								phi = atan((*this)[1]/(*this)[0]) + M_PI;
25
								phi = atan((*this)[1]/(*this)[0]) + M_PI;
20
						else 
26
						else 
21
								phi = ((*this)[1]>0) ? M_PI_2 : -1 * M_PI_2;
27
								phi = ((*this)[1]>0) ? M_PI_2 : -1 * M_PI_2;
22
		}
28
		}
23
 
29
 
24
 
30
 
25
		template<class T, class V>
31
		template<class T, class V>
26
		void ArithVec3Float<T,V>::set_spherical(T theta, T phi, T rlen )
32
		void ArithVec3Float<T,V>::set_spherical(T theta, T phi, T rlen )
27
		{
33
		{
28
				(*this)[0] = rlen * sin(theta) * cos(phi);
34
				(*this)[0] = rlen * sin(theta) * cos(phi);
29
				(*this)[1] = rlen * sin(theta) * sin(phi);
35
				(*this)[1] = rlen * sin(theta) * sin(phi);
30
				(*this)[2] = rlen * cos(theta);
36
				(*this)[2] = rlen * cos(theta);
31
		}
37
		}
32
 
38
 
33
		template<class T, class V>
39
		template<class T, class V>
34
		void orthogonal(const ArithVec3Float<T,V>& n, 
40
		void orthogonal(const ArithVec3Float<T,V>& n, 
35
										ArithVec3Float<T,V>& b1, 
41
										ArithVec3Float<T,V>& b1, 
36
										ArithVec3Float<T,V>& b2)
42
										ArithVec3Float<T,V>& b2)
37
		{
43
		{
38
			onb(normalize(n), b1, b2);
44
			onb(normalize(n), b1, b2);
39
		}
45
		}
40
 
46
 
41
    template<class T, class V>
47
    template<class T, class V>
42
    void onb(const ArithVec3Float<T,V>& n, 
48
    void onb(const ArithVec3Float<T,V>& n, 
43
             ArithVec3Float<T,V>& b1, 
49
             ArithVec3Float<T,V>& b1, 
44
             ArithVec3Float<T,V>& b2)
50
             ArithVec3Float<T,V>& b2)
45
    {
51
    {
46
      if(n[2] < -0.9999999f) // Handle the singularity
52
      if(n[2] < -0.9999999f) // Handle the singularity
47
      {
53
      {
48
        b1 = V(1.0f,  0.0f, 0.0f);
54
        b1 = V(1.0f,  0.0f, 0.0f);
49
        b2 = V(0.0f, -1.0f, 0.0f);
55
        b2 = V(0.0f, -1.0f, 0.0f);
50
        return;
56
        return;
51
      }
57
      }
52
      const T a = 1.0f/(1.0f + n[2]);
58
      const T a = 1.0f/(1.0f + n[2]);
53
      const T b = n[0]*n[1]*a;
59
      const T b = n[0]*n[1]*a;
54
      b1 = V(b, n[1]*n[1]*a - 1.0f, n[1]);
60
      b1 = V(b, n[1]*n[1]*a - 1.0f, n[1]);
55
      b2 = V(1.0f - n[0]*n[0]*a, -b, -n[0]);
61
      b2 = V(1.0f - n[0]*n[0]*a, -b, -n[0]);
56
    }
62
    }
57
 
63
 
58
		template class ArithVec3Float<float, Vec3f>;
64
		template class ArithVec3Float<float, Vec3f>;
59
		template void orthogonal<float,Vec3f>(const ArithVec3Float<float,Vec3f>& n, 
65
		template void orthogonal<float,Vec3f>(const ArithVec3Float<float,Vec3f>& n, 
60
																					ArithVec3Float<float,Vec3f>& b1, 
66
																					ArithVec3Float<float,Vec3f>& b1, 
61
																					ArithVec3Float<float,Vec3f>& b2);
67
																					ArithVec3Float<float,Vec3f>& b2);
62
    template void onb<float,Vec3f>(const ArithVec3Float<float,Vec3f>& n, 
68
    template void onb<float,Vec3f>(const ArithVec3Float<float,Vec3f>& n, 
63
                                   ArithVec3Float<float,Vec3f>& b1, 
69
                                   ArithVec3Float<float,Vec3f>& b1, 
64
                                   ArithVec3Float<float,Vec3f>& b2);
70
                                   ArithVec3Float<float,Vec3f>& b2);
65
 
71
 
66
		template class ArithVec3Float<double, Vec3d>;
72
		template class ArithVec3Float<double, Vec3d>;
67
		template void orthogonal<double,Vec3d>(const ArithVec3Float<double,Vec3d>& n,
73
		template void orthogonal<double,Vec3d>(const ArithVec3Float<double,Vec3d>& n,
68
																					 ArithVec3Float<double,Vec3d>& b1, 
74
																					 ArithVec3Float<double,Vec3d>& b1, 
69
																					 ArithVec3Float<double,Vec3d>& b2);
75
																					 ArithVec3Float<double,Vec3d>& b2);
70
    template void onb<double,Vec3d>(const ArithVec3Float<double,Vec3d>& n, 
76
    template void onb<double,Vec3d>(const ArithVec3Float<double,Vec3d>& n, 
71
                                    ArithVec3Float<double,Vec3d>& b1, 
77
                                    ArithVec3Float<double,Vec3d>& b1, 
72
                                    ArithVec3Float<double,Vec3d>& b2);
78
                                    ArithVec3Float<double,Vec3d>& b2);
73
}
79
}
74
 
80