Subversion Repositories gelsvn

Rev

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

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