Subversion Repositories gelsvn

Rev

Rev 45 | Go to most recent revision | Details | 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
 
10
	template<class T, class V>
11
	void ArithVec3Float<T,V>::get_spherical(T &theta, T &phi, T &rlen ) const
12
	{  
13
		rlen = length();
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
	}
23
 
24
 
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
	}
32
 
33
	template<class T, class V>
34
	void orthogonal(const ArithVec3Float<T,V>& _a, 
35
									ArithVec3Float<T,V>& b, 
36
									ArithVec3Float<T,V>& c)
37
	{
38
		V a = normalize(_a);
39
		T max_sqval=sqr(a[0]);
40
		int mi=0;
41
		for(int i=1;i<3;i++)
42
			{
43
				float sqval = sqr(a[i]);
44
				if(max_sqval<sqval)
45
					{
46
						max_sqval = sqval;
47
						mi = i;
48
					}
49
			}
50
		b[mi] = 0;
51
		b[(mi+1)%3] = 1;
52
		b[(mi+2)%3] = 0;
53
 
54
		b = normalize(b-a*dot(b,a));
55
		c = normalize(cross(a,b));
56
 
57
		if(dot(cross(b,c), a) < 0) swap(b,c);
58
	}
59
 
60
 
61
	template class ArithVec3Float<float, Vec3f>;
62
	template void orthogonal<float,Vec3f>(const ArithVec3Float<float,Vec3f>& a, 
63
																				ArithVec3Float<float,Vec3f>& b, 
64
																				ArithVec3Float<float,Vec3f>& c);
65
 
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);
70
 
71
}