vector_gen.hpp 8.79 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <sstream>

namespace CGoGN
{

namespace Geom
{

template <unsigned int DIM, typename T>
std::string Vector<DIM,T>::CGoGNnameOfType()
{
	std::stringstream ss ;
	ss << "Geom::Vector<" ;
	ss << DIM ;
	ss << "," ;
	ss << nameOfType(T()) ;
	ss << ">" ;

	return ss.str() ;
}

/**********************************************/
/*                CONSTRUCTORS                */
/**********************************************/

template <unsigned int DIM, typename T>
Vector<DIM,T>::Vector()
{
	CGoGN_STATIC_ASSERT(DIM > 0, invalid_zero_dimensional_Vector) ;
	zero() ;
}

template <unsigned int DIM, typename T>
Vector<DIM,T>::Vector(const Vector<DIM,T>& v)
{
	for(unsigned int i = 0; i < DIM; ++i)
		m_data[i] = v[i] ;
}

template <unsigned int DIM, typename T>
Vector<DIM,T>::Vector(T x, T y)
{
	CGoGN_STATIC_ASSERT(DIM==2, incompatible_Vector_constructor_dimension) ;
	m_data[0] = x ;
	m_data[1] = y ;
}

template <unsigned int DIM, typename T>
Vector<DIM,T>::Vector(T x, T y, T z)
{
	CGoGN_STATIC_ASSERT(DIM==3, incompatible_Vector_constructor_dimension) ;
	m_data[0] = x ;
	m_data[1] = y ;
	m_data[2] = z ;
}

template <unsigned int DIM, typename T>
Vector<DIM,T>::Vector(T x, T y, T z, T w)
{
	CGoGN_STATIC_ASSERT(DIM==4, incompatible_Vector_constructor_dimension) ;
	m_data[0] = x ;
	m_data[1] = y ;
	m_data[2] = z ;
	m_data[3] = w ;
}

template <unsigned int DIM, typename T>
Vector<DIM,T>::Vector(T x)
{
	set(x) ;
}

template <unsigned int DIM, typename T>
inline void Vector<DIM,T>::set(T a)
{
	for(unsigned int i = 0; i < DIM; ++i)
		m_data[i] = a ;
}

template <unsigned int DIM, typename T>
inline void Vector<DIM,T>::zero()
{
	set(T(0)) ;
}

/**********************************************/
/*                 ACCESSORS                  */
/**********************************************/

template <unsigned int DIM, typename T>
inline T& Vector<DIM,T>::operator[](unsigned int index)
{
	assert(index < DIM) ;
	return m_data[index] ;
}

template <unsigned int DIM, typename T>
inline const T& Vector<DIM,T>::operator[](unsigned int index) const
{
	assert(index < DIM) ;
	return m_data[index] ;
}

template <unsigned int DIM, typename T>
inline unsigned int Vector<DIM,T>::dimension() const
{
	return DIM ;
}

template <unsigned int DIM, typename T>
inline T* Vector<DIM,T>::data()
{
	return m_data ;
}

template <unsigned int DIM, typename T>
inline const T* Vector<DIM,T>::data() const
{
	return m_data ;
}

/**********************************************/
/*         ARITHMETIC SELF-OPERATORS          */
/**********************************************/

template <unsigned int DIM, typename T>
inline Vector<DIM,T>& Vector<DIM,T>::operator+=(const Vector<DIM,T>& v)
{
	for(unsigned int i = 0; i < DIM; ++i)
		m_data[i] += v[i] ;
	return *this ;
}

template <unsigned int DIM, typename T>
inline Vector<DIM,T>& Vector<DIM,T>::operator-=(const Vector<DIM,T>& v)
{
	for(unsigned int i = 0; i < DIM; ++i)
		m_data[i] -= v[i] ;
	return *this ;
}

template <unsigned int DIM, typename T>
inline Vector<DIM,T> Vector<DIM,T>::operator*=(T a)
{
	for(unsigned int i = 0; i < DIM; ++i)
		m_data[i] *= a ;
	return *this ;
}

template <unsigned int DIM, typename T>
inline Vector<DIM,T> Vector<DIM,T>::operator/=(T a)
{
	for(unsigned int i = 0; i < DIM; ++i)
		m_data[i] /= a ;
	return *this ;
}

/**********************************************/
/*            ARITHMETIC OPERATORS            */
/**********************************************/

template <unsigned int DIM, typename T>
inline Vector<DIM,T> Vector<DIM,T>::operator+(const Vector<DIM,T>& v) const
{
	Vector<DIM,T> res ;
	for(unsigned int i = 0; i < DIM; ++i)
		res[i] = m_data[i] + v[i] ;
	return res ;
}

template <unsigned int DIM, typename T>
inline Vector<DIM,T> Vector<DIM,T>::operator-(const Vector<DIM,T>& v) const
{
	Vector<DIM,T> res ;
	for(unsigned int i = 0; i < DIM; ++i)
		res[i] = m_data[i] - v[i] ;
	return res ;
}

template <unsigned int DIM, typename T>
inline Vector<DIM,T> Vector<DIM,T>::operator*(T a) const
{
	Vector<DIM,T> res ;
	for(unsigned int i = 0; i < DIM; ++i)
		res[i] = m_data[i] * a ;
	return res ;
}

template <unsigned int DIM, typename T>
inline Vector<DIM,T> Vector<DIM,T>::operator/(T a) const
{
	Vector<DIM,T> res ;
	for(unsigned int i = 0; i < DIM; ++i)
		res[i] = m_data[i] / a ;
	return res ;
}

/**********************************************/
/*             UTILITY FUNCTIONS              */
/**********************************************/

template <unsigned int DIM, typename T>
inline T Vector<DIM,T>::norm2() const
{
	T n(0) ;
	for(unsigned int i = 0; i < DIM; ++i)
		n += m_data[i] * m_data[i] ;
	return n ;
}

template <unsigned int DIM, typename T>
inline double Vector<DIM,T>::norm() const
{
	return sqrt(norm2()) ;
}

template <unsigned int DIM, typename T>
inline double Vector<DIM,T>::normalize()
{
	double n = norm() ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
245
246
	if(n==T(0.0))
		n = T(0.0001) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
	for(unsigned int i = 0; i < DIM; ++i)
		m_data[i] /= T(n) ;
	return n ;
}

template <unsigned int DIM, typename T>
inline T Vector<DIM,T>::operator*(const Vector<DIM,T> v) const
{
	T d(0) ;
	for(unsigned int i = 0; i < DIM; ++i)
		d += m_data[i] * v[i] ;
	return d ;
}

template <unsigned int DIM, typename T>
inline Vector<DIM,T> Vector<DIM,T>::operator^(const Vector<DIM,T> v) const
{
	CGoGN_STATIC_ASSERT(DIM == 3, incompatible_Vector_cross_product_dimension) ;
	Vector<DIM,T> c ;
	c[0] = m_data[1] * v[2] - m_data[2] * v[1] ;
	c[1] = m_data[2] * v[0] - m_data[0] * v[2] ;
	c[2] = m_data[0] * v[1] - m_data[1] * v[0] ;
	return c ;
}

template <unsigned int DIM, typename T>
inline bool Vector<DIM,T>::operator==(const Vector<DIM,T>& v) const
{
	for (unsigned int i = 0 ; i < DIM ; ++i)
		if (v[i] != m_data[i]) return false ;
	return true ;
}

/**********************************************/
/*             STREAM OPERATORS               */
/**********************************************/

template <unsigned int DIM, typename T>
std::ostream& operator<<(std::ostream& out, const Vector<DIM,T>& v)
{
	for(unsigned int i = 0; i < DIM; ++i)
		out << v[i] << " " ;
	return out ;
}

template <unsigned int DIM, typename T>
std::istream& operator>>(std::istream& in, Vector<DIM,T>& v)
{
	for(unsigned int i = 0; i < DIM; ++i)
		in >> v[i] ;
	return in ;
}



template <unsigned int DIM, typename T>
inline Vector<DIM,T> operator*(T a, const Vector<DIM,T>& v)
{
	return v * a ;
}


template <unsigned int DIM, typename T>
inline Vector<DIM,T> operator/(T a, const Vector<DIM,T>& v)
{
	return v / a ;
}

template <unsigned int DIM, typename T>
inline T tripleProduct(const Vector<DIM,T>& v1, const Vector<DIM,T>& v2, const Vector<DIM,T>& v3)
{
	return v1 * (v2 ^ v3) ;
}

template <unsigned int DIM, typename T>
inline Vector<DIM,T> slerp(const Vector<DIM,T> &v1, const Vector<DIM,T> &v2, const T &t) {
	Vector<DIM,T> res ;

325
	T omega = acos(v1 * v2) ;	// assume v1,v2 normalized
326
	T den = sin(omega) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
327

328
329
	if (-1e-8 < den && den < 1e-8)
		return t < 0.5 ? v1 : v2 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
330

331
	T f1 = sin(( T(1)-t ) * omega ) / den ;	// assume 0 <= t <= 1
332
	T f2 = sin( t * omega ) / den ;
Pierre Kraemer's avatar
Pierre Kraemer committed
333

334
335
	res += f1 * v1 ;
	res += f2 * v2 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
336
337
338
339
340
341
342
343

	return res ;
}


} // namespace Geom

} // namespace CGoGN