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