Tulip  4.0.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
Vector.cxx
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 
21 #define VECTORTLP tlp::Vector<TYPE,SIZE, OTYPE>
22 
23 //======================================================
24 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
25 //VECTORTLP & VECTORTLP::operator*=(const OTYPE scalaire) {
26 // assert( scalaire >= std::numeric_limits<TYPE>::min() && scalaire <= std::numeric_limits<TYPE>::max());
27 // return (*this) *= static_cast<TYPE>(scalaire);
28 //}
29 //======================================================
30 template <typename TYPE,unsigned int SIZE, typename OTYPE>
31 VECTORTLP & VECTORTLP::operator*=(const TYPE scalaire) {
32  for (unsigned int i=0; i<SIZE; ++i)
33  (*this)[i]*=scalaire;
34 
35  return (*this);
36 }
37 //======================================================
38 template <typename TYPE,unsigned int SIZE, typename OTYPE>
39 VECTORTLP & VECTORTLP::operator*=(const VECTORTLP &vecto) {
40  for (unsigned int i=0; i<SIZE; ++i)
41  (*this)[i]*=vecto[i];
42 
43  return (*this);
44 }
45 //======================================================
46 template <typename TYPE,unsigned int SIZE, typename OTYPE>
47 VECTORTLP & VECTORTLP::operator/=(const TYPE scalaire) {
48  assert(scalaire!=static_cast<TYPE>(0));
49 
50  for (unsigned int i=0; i<SIZE; ++i)
51  (*this)[i]/=scalaire;
52 
53  return (*this);
54 }
55 //======================================================
56 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
57 //VECTORTLP & VECTORTLP::operator/=(const OTYPE scalaire) {
58 // assert( scalaire >= std::numeric_limits<TYPE>::min() && scalaire <= std::numeric_limits<TYPE>::max());
59 // return (*this) /= static_cast<TYPE>(scalaire);
60 //}
61 //======================================================
62 template <typename TYPE,unsigned int SIZE, typename OTYPE>
63 VECTORTLP & VECTORTLP::operator/=(const VECTORTLP &vecto) {
64  for (unsigned int i=0; i<SIZE; ++i) {
65  assert(vecto[i]!=static_cast<TYPE>(0));
66  (*this)[i]/=vecto[i];
67  }
68 
69  return (*this);
70 }
71 //======================================================
72 template <typename TYPE,unsigned int SIZE, typename OTYPE>
73 VECTORTLP & VECTORTLP::operator+=(const TYPE scalaire) {
74  for (unsigned int i=0; i<SIZE; ++i)
75  (*this)[i]+=scalaire;
76 
77  return (*this);
78 }
79 
80 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
81 //VECTORTLP & VECTORTLP::operator+=(const OTYPE scalaire) {
82 // assert( scalaire >= std::numeric_limits<TYPE>::min() && scalaire <= std::numeric_limits<TYPE>::max());
83 // return (*this) += static_cast<TYPE>(scalaire);
84 //}
85 //======================================================
86 template <typename TYPE,unsigned int SIZE, typename OTYPE>
87 VECTORTLP & VECTORTLP::operator+=(const VECTORTLP &vecto) {
88  for (unsigned int i=0; i<SIZE; ++i)
89  (*this)[i]+=vecto[i];
90 
91  return (*this);
92 }
93 //======================================================
94 template <typename TYPE,unsigned int SIZE, typename OTYPE>
95 VECTORTLP & VECTORTLP::operator-=(const TYPE scalaire) {
96  for (unsigned int i=0; i<SIZE; ++i)
97  (*this)[i]-=scalaire;
98 
99  return (*this);
100 }
101 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
102 //VECTORTLP & VECTORTLP::operator-=(const OTYPE scalaire) {
103 // assert( scalaire >= std::numeric_limits<TYPE>::min() && scalaire <= std::numeric_limits<TYPE>::max());
104 // return (*this) -= static_cast<TYPE>(scalaire);
105 //}
106 //======================================================
107 template <typename TYPE,unsigned int SIZE, typename OTYPE>
108 VECTORTLP & VECTORTLP::operator-=(const VECTORTLP &vecto) {
109  for (unsigned int i=0; i<SIZE; ++i)
110  (*this)[i]-=vecto[i];
111 
112  return (*this);
113 }
114 //======================================================
115 template <typename TYPE,unsigned int SIZE, typename OTYPE>
116 VECTORTLP & VECTORTLP::operator^=(const VECTORTLP &v) {
117  (*this) = (*this) ^ v;
118  return (*this);
119 }
120 //======================================================
121 template <typename TYPE,unsigned int SIZE, typename OTYPE>
122 VECTORTLP tlp::operator+(const VECTORTLP &u, const VECTORTLP &v) {
123  return VECTORTLP(u)+=v;
124 }
125 //======================================================
126 template <typename TYPE,unsigned int SIZE, typename OTYPE>
127 VECTORTLP tlp::operator+(const VECTORTLP &u, const TYPE scalaire) {
128  return VECTORTLP(u)+=scalaire;
129 }
130 //======================================================
131 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
132 //VECTORTLP tlp::operator+(const VECTORTLP &u, const OTYPE scalaire) {
133 // return VECTORTLP(u) += static_cast<TYPE>(scalaire);
134 //}
135 //======================================================
136 template <typename TYPE,unsigned int SIZE, typename OTYPE>
137 VECTORTLP tlp::operator-(const VECTORTLP &u, const VECTORTLP &v) {
138  return VECTORTLP(u) -= v;
139 }
140 //======================================================
141 template <typename TYPE,unsigned int SIZE, typename OTYPE>
142 VECTORTLP tlp::operator-(const VECTORTLP &u, const TYPE scalaire) {
143  return VECTORTLP(u) -= scalaire;
144 }
145 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
146 //VECTORTLP tlp::operator-(const VECTORTLP &u, const OTYPE scalaire) {
147 // return VECTORTLP(u) -= static_cast<TYPE>(scalaire);
148 //}
149 //======================================================
150 template <typename TYPE,unsigned int SIZE, typename OTYPE>
151 VECTORTLP tlp::operator*(const VECTORTLP &u, const VECTORTLP &v) {
152  return VECTORTLP(u)*=v;
153 }
154 //======================================================
155 template <typename TYPE,unsigned int SIZE, typename OTYPE>
156 VECTORTLP tlp::operator*(const VECTORTLP &u, const TYPE scalaire) {
157  return VECTORTLP(u)*=scalaire;
158 }
159 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
160 //VECTORTLP tlp::operator*(const VECTORTLP &u, const OTYPE scalaire) {
161 // return VECTORTLP(u)*=scalaire;
162 //}
163 //======================================================
164 template <typename TYPE,unsigned int SIZE, typename OTYPE>
165 VECTORTLP tlp::operator*(const TYPE scalaire, const VECTORTLP &u ) {
166  return VECTORTLP(u)*=scalaire;
167 }
168 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
169 //VECTORTLP tlp::operator*(const OTYPE scalaire, const VECTORTLP &u ) {
170 // return VECTORTLP(u)*=scalaire;
171 //}
172 //======================================================
173 template <typename TYPE,unsigned int SIZE, typename OTYPE>
174 VECTORTLP tlp::operator/(const VECTORTLP &u, const VECTORTLP &v) {
175  return VECTORTLP(u)/=v;
176 }
177 //======================================================
178 template <typename TYPE,unsigned int SIZE, typename OTYPE>
179 VECTORTLP tlp::operator/(const VECTORTLP &u, const TYPE scalaire) {
180  return VECTORTLP(u)/=scalaire;
181 }
182 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
183 //VECTORTLP tlp::operator/(const VECTORTLP &u, const OTYPE scalaire) {
184 // return VECTORTLP(u)/=scalaire;
185 //}
186 //======================================================
187 template <typename TYPE,unsigned int SIZE, typename OTYPE>
188 VECTORTLP tlp::operator^(const VECTORTLP &u, const VECTORTLP &v) {
189 
190  switch(SIZE) {
191  case 3:
192  return VECTORTLP(
193  static_cast<TYPE>(
194  static_cast<OTYPE>(u.y()) * static_cast<OTYPE>(v.z())
195  -
196  static_cast<OTYPE>(u.z()) * static_cast<OTYPE>(v.y())
197  )
198  ,
199  static_cast<TYPE>(
200  static_cast<OTYPE>(u.z()) * static_cast<OTYPE>(v.x())
201  -
202  static_cast<OTYPE>(u.x()) * static_cast<OTYPE>(v.z())
203  )
204  ,
205  static_cast<TYPE>(
206  static_cast<OTYPE>(u.x()) * static_cast<OTYPE>(v.y())
207  -
208  static_cast<OTYPE>(u.y()) * static_cast<OTYPE>(v.x())
209  )
210  );
211  break;
212 
213  default :
214  qWarning() << "cross product not implemented for dimension :" << SIZE;
215  VECTORTLP result;
216  return result;
217  break;
218  }
219 }
220 //======================================================
221 template <typename TYPE,unsigned int SIZE, typename OTYPE>
222 VECTORTLP tlp::operator-(const VECTORTLP &u) {
223  return VECTORTLP(u) *= static_cast<TYPE>(-1);
224 }
225 //======================================================
226 template <typename TYPE,unsigned int SIZE, typename OTYPE>
227 bool VECTORTLP::operator>(const VECTORTLP &vecto) const {
228  return vecto < (*this);
229 }
230 //======================================================
231 template <typename TYPE,unsigned int SIZE, typename OTYPE>
232 bool VECTORTLP::operator<(const VECTORTLP &v) const {
233  if (std::numeric_limits<TYPE>::is_integer) {
234  return memcmp(&((*this).array[0]), (void*)&(v.array[0]), SIZE * sizeof(TYPE)) < 0;
235  }
236 
237  for (unsigned int i=0; i<SIZE; ++i) {
238  OTYPE tmp = static_cast<OTYPE>((*this)[i]) - static_cast<OTYPE>(v[i]);
239 
240  if (tmp > sqrt(std::numeric_limits<TYPE>::epsilon()) || tmp < -sqrt(std::numeric_limits<TYPE>::epsilon())) {
241  if (tmp > 0) return false;
242 
243  if (tmp < 0) return true;
244  }
245  }
246 
247  return false;
248 }
249 //======================================================
250 template <typename TYPE,unsigned int SIZE, typename OTYPE>
251 bool VECTORTLP::operator!=(const VECTORTLP &vecto) const {
252  return (! ( (*this) == vecto ));
253 }
254 //======================================================
255 template <typename TYPE,unsigned int SIZE, typename OTYPE>
256 bool VECTORTLP::operator==(const VECTORTLP &v) const {
257  if (std::numeric_limits<TYPE>::is_integer) {
258  return memcmp((void*)&((*this).array[0]), (void*)&(v.array[0]), SIZE * sizeof(TYPE)) == 0;
259  }
260 
261  for (unsigned int i=0; i<SIZE; ++i) {
262  OTYPE tmp = static_cast<OTYPE>((*this)[i]) - static_cast<OTYPE>(v[i]);
263 
264  if (tmp > sqrt(std::numeric_limits<TYPE>::epsilon()) || tmp < -sqrt(std::numeric_limits<TYPE>::epsilon())) {
265  return false;
266  }
267  }
268 
269  return true;
270 }
271 //======================================================
272 template <typename TYPE,unsigned int SIZE, typename OTYPE>
273 TYPE VECTORTLP::dotProduct(const VECTORTLP &v) const {
274  assert (SIZE>0);
275  OTYPE tmpO = static_cast<OTYPE>(VECTORTLP::array[0]) * static_cast<OTYPE>(v[0]);
276 
277  for (unsigned int i=1; i<SIZE; ++i)
278  tmpO += static_cast<OTYPE>((*this)[i]) * static_cast<OTYPE>(v[i]);
279 
280  return static_cast<TYPE>(tmpO);
281 }
282 //======================================================
283 template <typename TYPE,unsigned int SIZE, typename OTYPE>
284 VECTORTLP & VECTORTLP::fill(const TYPE scalaire) {
285  for (unsigned int i=0; i<SIZE; ++i)
286  (*this)[i]=scalaire;
287 
288  return (*this);
289 }
290 //======================================================
291 template <typename TYPE,unsigned int SIZE, typename OTYPE>
292 TYPE VECTORTLP::norm() const {
293  switch(SIZE) {
294  case 1:
295  return VECTORTLP::array[0];
296 
297  case 2:
298  return tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>(x())
299  +
300  tlpsqr<TYPE, OTYPE>(y())
301  );
302 
303  case 3:
304  return tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>(x())
305  +
306  tlpsqr<TYPE, OTYPE>(y())
307  +
308  tlpsqr<TYPE, OTYPE>(z())
309  );
310 
311  default :
312  OTYPE tmp = tlpsqr<TYPE, OTYPE>((*this)[0]);
313 
314  for (unsigned int i=1; i<SIZE; ++i)
315  tmp += tlpsqr<TYPE, OTYPE>((*this)[i]);
316 
317  return(tlpsqrt<TYPE, OTYPE>(tmp));
318  }
319 }
320 //======================================================
321 template <typename TYPE,unsigned int SIZE, typename OTYPE>
322 TYPE VECTORTLP::dist(const VECTOR &c) const {
323  switch(SIZE) {
324  case 1:
325  return static_cast<TYPE>(fabs(x()-c.x()));
326 
327  case 2:
328  return tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>(x()-c.x())
329  +
330  tlpsqr<TYPE, OTYPE>(y()-c.y()));
331 
332  case 3:
333  return tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>(x()-c.x())
334  +
335  tlpsqr<TYPE, OTYPE>(y()-c.y())
336  +
337  tlpsqr<TYPE, OTYPE>(z()-c.z())
338  );
339 
340  default :
341  OTYPE tmp = tlpsqr<TYPE, OTYPE>((*this)[0] - c[0]);
342 
343  for (unsigned int i=1; i<SIZE; ++i)
344  tmp += tlpsqr<TYPE, OTYPE>((*this)[i]-c[i]);
345 
346  return(tlpsqrt<TYPE, OTYPE>(tmp));
347  }
348 }
349 //=======================================================================