localFrame.hpp 5.94 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
* Web site: http://cgogn.u-strasbg.fr/                                         *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN {


namespace Utils {

template<typename PFP>
LocalFrame<PFP>::LocalFrame(const VEC3& T, const VEC3& B, const VEC3& N)
{
	m_T = T ;
	m_B = B ;
	m_N = N ;
}

template<typename PFP>
LocalFrame<PFP>::LocalFrame(const VEC3& compressedFrame)
{
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
41
42
43
44
	// get known data
	const REAL& thetaN = compressedFrame[0] ;
	const REAL& phiN = compressedFrame[1] ;
	const REAL& thetaT = compressedFrame[2] ;
45
46

	// compute phiT
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
47
48
49
50
51
52
53
//	const REAL quot = std::tan(phiN)*(std::cos(thetaN)*std::cos(thetaT) + std::sin(thetaN)*std::sin(thetaT)) ; // Based on orthogonality
//	REAL phiT = -std::atan(1/quot) ; // if quot==0, atan returns Pi/2
	REAL phiT = -std::atan((std::cos(thetaN)*std::cos(thetaT) + std::sin(thetaN)*std::sin(thetaT))*std::cos(phiN) / std::sin(phiN)) ; // if quot==0, atan returns Pi/2

//	if (phiT < 0.0) {
		std::cout << " ; New set = " << compressedFrame << phiT << std::endl ;
//		phiT += M_PI ; // = Pi - |phiT|
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
54
55
//		std::cout << compressedFrame << phiT << std::endl ;
//		std::cout << "New phiT = " << -std::atan(1/Den) << std::endl ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
56
//	}
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
57
58
59

	VEC2 Nspher(thetaN,phiN) ;
	VEC2 Tspher(thetaT,phiT) ;
60
61
62
63
64
65
66
67
68
69

	// convert to carthesian
	m_N = sphericalToCarth(Nspher) ;
	m_T = sphericalToCarth(Tspher) ;

	// compute B
	m_B = m_N ^ m_T ;
}

template<typename PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
70
typename PFP::VEC3 LocalFrame<PFP>::getCompressed() const
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
{
	VEC3 res ;

	REAL& thetaN = res[0] ;
	REAL& phiN = res[1] ;
	REAL& thetaT = res[2] ;

	// convert to spherical coordinates
	VEC2 Nspher = carthToSpherical(m_N) ;
	VEC2 Tspher = carthToSpherical(m_T) ;

	// extract the three scalars
	thetaN = Nspher[0] ;
	phiN = Nspher[1] ;
	thetaT = Tspher[0] ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
86
87
88
	//if (thetaT == 0.0 && phiN == 0.0)
		//phiN = (Tspher[1] > 0 ? -1 : 1) * 1e-8 ;
	std::cout << "Original Set: " << res << Tspher[1] ;
89
90
91
92
93

	return res ;
}

template<typename PFP>
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
bool LocalFrame<PFP>::equals(const Utils::LocalFrame<PFP>& lf, REAL epsilon) const
{
	VEC3 dT = m_T - lf.getT() ;
	VEC3 dB = m_B - lf.getB() ;
	VEC3 dN = m_N - lf.getN() ;

	return dT.norm2() < epsilon && dB.norm2() < epsilon && dN.norm2() < epsilon ;
}

template<typename PFP>
bool LocalFrame<PFP>::operator==(const LocalFrame<PFP>& lf) const
{
	return this->equals(lf) ;
}

template<typename PFP>
bool LocalFrame<PFP>::operator!=(const LocalFrame<PFP>& lf) const
{
	return !(this->equals(lf)) ;
}

template<typename PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
116
bool LocalFrame<PFP>::isDirect(REAL epsilon) const
117
118
119
120
121
{
	VEC3 new_B = m_N ^ m_T ;		// direct
	VEC3 diffs = new_B - m_B ;		// differences with existing B
	REAL diffNorm = diffs.norm2() ;	// Norm of this differences vector

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
122
	return (diffNorm < epsilon) ;		// Verify that this difference is very small
123
124
125
}

template<typename PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
126
bool LocalFrame<PFP>::isOrthogonal(REAL epsilon) const
127
{
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
128
	return (fabs(m_T * m_B) < epsilon) && (fabs(m_N * m_B) < epsilon) && (fabs(m_T * m_N) < epsilon) ;
129
130
131
}

template<typename PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
132
bool LocalFrame<PFP>::isNormalized(REAL epsilon) const
133
{
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
134
135
136
	return 		(1-1e-5 < m_N.norm2() && m_N.norm2() < epsilon)
			&& 	(1-1e-5 < m_T.norm2() && m_T.norm2() < epsilon)
			&&	(1-1e-5 < m_B.norm2() && m_B.norm2() < epsilon) ;
137
138
139
}

template<typename PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
140
bool LocalFrame<PFP>::isOrthoNormalDirect(REAL epsilon) const
141
{
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
142
	return isOrthogonal(epsilon) && isNormalized(epsilon) && isDirect(epsilon) ;
143
144
145
}

template<typename PFP>
146
typename Geom::Vector<2,typename PFP::REAL> LocalFrame<PFP>::carthToSpherical (const VEC3& carth) const
147
148
149
150
151
152
153
{
	VEC2 res ;

	const REAL& x = carth[0] ;
	const REAL& y = carth[1] ;
	const REAL& z = carth[2] ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
154
155
	REAL theta = ((y < 0) ? -1 : 1) * std::acos(x / REAL(sqrt(x*x + y*y)) )  ;
	if (isnan(theta))
156
157
		theta = 0.0 ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
158
159
160
161
162
163
	REAL phi = std::asin(z) ;
/*	if (phi < -1.57)
		std::cout << theta << std::endl ;
	if (phi > 1.57)
		std::cout << theta << std::endl ;
*/
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
164
165
	res[0] = theta ;
	res[1] = phi ;
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

	return res ;
}

template<typename PFP>
typename PFP::VEC3 LocalFrame<PFP>::sphericalToCarth (const VEC2& sph) const
{
	VEC3 res ;

	const REAL& theta = sph[0] ;
	const REAL& phi = sph[1] ;

	REAL& x = res[0] ;
	REAL& y = res[1] ;
	REAL& z = res[2] ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
182
183
184
	x = cos(theta)*cos(phi) ;
	y = sin(theta)*cos(phi) ;
	z = sin(phi) ;
185
186
187
188
189
190
191
192
193
194
195

	assert(-1.000001 < x && x < 1.000001) ;
	assert(-1.000001 < y && y < 1.000001) ;
	assert(-1.000001 < z && z < 1.000001) ;

	return res ;
}

} // Utils

} // CGoGN