Subversion Repositories gelsvn

Rev

Rev 125 | Rev 570 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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