Tulip  5.7.4
Large graphs analysis and drawing
Vector.cxx
1 /*
2  *
3  * This file is part of Tulip (https://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
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 #include <cstring>
21 
22 #define VECTORTLP tlp::Vector<TYPE, SIZE, OTYPE, DTYPE>
23 
24 //======================================================
25 TEMPLATEVECTOR
26 VECTORTLP &VECTORTLP::operator*=(const TYPE scalaire) {
27  for (size_t i = 0; i < SIZE; ++i)
28  (*this)[i] *= scalaire;
29 
30  return (*this);
31 }
32 //======================================================
33 TEMPLATEVECTOR
34 VECTORTLP &VECTORTLP::operator*=(const VECTORTLP &vecto) {
35  for (size_t i = 0; i < SIZE; ++i)
36  (*this)[i] *= vecto[i];
37 
38  return (*this);
39 }
40 //======================================================
41 TEMPLATEVECTOR
42 VECTORTLP &VECTORTLP::operator/=(const TYPE scalaire) {
43  assert(scalaire != static_cast<TYPE>(0));
44 
45  for (size_t i = 0; i < SIZE; ++i)
46  (*this)[i] /= scalaire;
47 
48  return (*this);
49 }
50 //======================================================
51 TEMPLATEVECTOR
52 VECTORTLP &VECTORTLP::operator/=(const VECTORTLP &vecto) {
53  for (size_t i = 0; i < SIZE; ++i) {
54  assert(vecto[i] != static_cast<TYPE>(0));
55  (*this)[i] /= vecto[i];
56  }
57 
58  return (*this);
59 }
60 //======================================================
61 TEMPLATEVECTOR
62 VECTORTLP &VECTORTLP::operator+=(const TYPE scalaire) {
63  for (size_t i = 0; i < SIZE; ++i)
64  (*this)[i] += scalaire;
65 
66  return (*this);
67 }
68 //======================================================
69 TEMPLATEVECTOR
70 VECTORTLP &VECTORTLP::operator+=(const VECTORTLP &vecto) {
71  for (size_t i = 0; i < SIZE; ++i)
72  (*this)[i] += vecto[i];
73 
74  return (*this);
75 }
76 //======================================================
77 TEMPLATEVECTOR
78 VECTORTLP &VECTORTLP::operator-=(const TYPE scalaire) {
79  for (size_t i = 0; i < SIZE; ++i)
80  (*this)[i] -= scalaire;
81 
82  return (*this);
83 }
84 //======================================================
85 TEMPLATEVECTOR
86 VECTORTLP &VECTORTLP::operator-=(const VECTORTLP &vecto) {
87  for (size_t i = 0; i < SIZE; ++i)
88  (*this)[i] -= vecto[i];
89 
90  return (*this);
91 }
92 //======================================================
93 TEMPLATEVECTOR
94 VECTORTLP &VECTORTLP::operator^=(const VECTORTLP &v) {
95  (*this) = (*this) ^ v;
96  return (*this);
97 }
98 //======================================================
99 TEMPLATEVECTOR
100 VECTORTLP tlp::operator+(const VECTORTLP &u, const VECTORTLP &v) {
101  return VECTORTLP(u) += v;
102 }
103 //======================================================
104 TEMPLATEVECTOR
105 VECTORTLP tlp::operator+(const VECTORTLP &u, const TYPE scalaire) {
106  return VECTORTLP(u) += scalaire;
107 }
108 //======================================================
109 TEMPLATEVECTOR
110 VECTORTLP tlp::operator-(const VECTORTLP &u, const VECTORTLP &v) {
111  return VECTORTLP(u) -= v;
112 }
113 //======================================================
114 TEMPLATEVECTOR
115 VECTORTLP tlp::operator-(const VECTORTLP &u, const TYPE scalaire) {
116  return VECTORTLP(u) -= scalaire;
117 }
118 //======================================================
119 TEMPLATEVECTOR
120 VECTORTLP tlp::operator*(const VECTORTLP &u, const VECTORTLP &v) {
121  return VECTORTLP(u) *= v;
122 }
123 //======================================================
124 TEMPLATEVECTOR
125 VECTORTLP tlp::operator*(const VECTORTLP &u, const TYPE scalaire) {
126  return VECTORTLP(u) *= scalaire;
127 }
128 //======================================================
129 TEMPLATEVECTOR
130 VECTORTLP tlp::operator*(const TYPE scalaire, const VECTORTLP &u) {
131  return VECTORTLP(u) *= scalaire;
132 }
133 //======================================================
134 TEMPLATEVECTOR
135 VECTORTLP tlp::operator/(const VECTORTLP &u, const VECTORTLP &v) {
136  return VECTORTLP(u) /= v;
137 }
138 //======================================================
139 TEMPLATEVECTOR
140 VECTORTLP tlp::operator/(const VECTORTLP &u, const TYPE scalaire) {
141  return VECTORTLP(u) /= scalaire;
142 }
143 //======================================================
144 TEMPLATEVECTOR
145 VECTORTLP tlp::operator^(const VECTORTLP &u, const VECTORTLP &v) {
146 
147  switch (SIZE) {
148  case 3:
149  return VECTORTLP(static_cast<TYPE>(static_cast<OTYPE>(u.y()) * static_cast<OTYPE>(v.z()) -
150  static_cast<OTYPE>(u.z()) * static_cast<OTYPE>(v.y())),
151  static_cast<TYPE>(static_cast<OTYPE>(u.z()) * static_cast<OTYPE>(v.x()) -
152  static_cast<OTYPE>(u.x()) * static_cast<OTYPE>(v.z())),
153  static_cast<TYPE>(static_cast<OTYPE>(u.x()) * static_cast<OTYPE>(v.y()) -
154  static_cast<OTYPE>(u.y()) * static_cast<OTYPE>(v.x())));
155  break;
156 
157  default:
158  tlp::warning() << "cross product not implemented for dimension :" << SIZE << std::endl;
159  VECTORTLP result;
160  return result;
161  break;
162  }
163 }
164 //======================================================
165 TEMPLATEVECTOR
166 VECTORTLP tlp::operator-(const VECTORTLP &u) {
167  return VECTORTLP(u) *= static_cast<TYPE>(-1);
168 }
169 //======================================================
170 TEMPLATEVECTOR
171 bool VECTORTLP::operator>(const VECTORTLP &vecto) const {
172  return vecto < (*this);
173 }
174 //======================================================
175 TEMPLATEVECTOR
176 bool VECTORTLP::operator<(const VECTORTLP &v) const {
177  if (std::numeric_limits<TYPE>::is_integer) {
178  return memcmp(this->data(), v.data(), SIZE * sizeof(TYPE)) < 0;
179  }
180 
181  for (size_t i = 0; i < SIZE; ++i) {
182  OTYPE tmp = static_cast<OTYPE>((*this)[i]) - static_cast<OTYPE>(v[i]);
183 
184  if (tmp > sqrt(std::numeric_limits<TYPE>::epsilon()) ||
185  tmp < -sqrt(std::numeric_limits<TYPE>::epsilon())) {
186  if (tmp > 0)
187  return false;
188 
189  if (tmp < 0)
190  return true;
191  }
192  }
193 
194  return false;
195 }
196 //======================================================
197 TEMPLATEVECTOR
198 bool VECTORTLP::operator!=(const VECTORTLP &vecto) const {
199  return (!((*this) == vecto));
200 }
201 //======================================================
202 TEMPLATEVECTOR
203 bool VECTORTLP::operator==(const VECTORTLP &v) const {
204  if (std::numeric_limits<TYPE>::is_integer) {
205  return memcmp(this->data(), v.data(), SIZE * sizeof(TYPE)) == 0;
206  }
207 
208  for (size_t i = 0; i < SIZE; ++i) {
209  OTYPE tmp = static_cast<OTYPE>((*this)[i]) - static_cast<OTYPE>(v[i]);
210 
211  if (tmp > sqrt(std::numeric_limits<TYPE>::epsilon()) ||
212  tmp < -sqrt(std::numeric_limits<TYPE>::epsilon())) {
213  return false;
214  }
215  }
216 
217  return true;
218 }
219 //======================================================
220 TEMPLATEVECTOR
221 TYPE VECTORTLP::dotProduct(const VECTORTLP &v) const {
222  assert(SIZE > 0);
223  OTYPE tmpO = static_cast<OTYPE>((*this)[0]) * static_cast<OTYPE>(v[0]);
224 
225  for (size_t i = 1; i < SIZE; ++i)
226  tmpO += static_cast<OTYPE>((*this)[i]) * static_cast<OTYPE>(v[i]);
227 
228  return static_cast<TYPE>(tmpO);
229 }
230 //======================================================
231 TEMPLATEVECTOR
232 VECTORTLP &VECTORTLP::fill(const TYPE scalaire) {
233  for (size_t i = 0; i < SIZE; ++i)
234  (*this)[i] = scalaire;
235 
236  return (*this);
237 }
238 //======================================================
239 TEMPLATEVECTOR
240 TYPE VECTORTLP::norm() const {
241  switch (SIZE) {
242  case 1:
243  return (*this)[0];
244 
245  case 2:
246  return tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>(x()) + tlpsqr<TYPE, OTYPE>(y()));
247 
248  case 3:
249  return tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>(x()) + tlpsqr<TYPE, OTYPE>(y()) +
250  tlpsqr<TYPE, OTYPE>(z()));
251 
252  default:
253  OTYPE tmp = tlpsqr<TYPE, OTYPE>((*this)[0]);
254 
255  for (size_t i = 1; i < SIZE; ++i)
256  tmp += tlpsqr<TYPE, OTYPE>((*this)[i]);
257 
258  return (tlpsqrt<TYPE, OTYPE>(tmp));
259  }
260 }
261 //======================================================
262 TEMPLATEVECTOR
263 DTYPE VECTORTLP::dist(const VECTOR &c) const {
264  switch (SIZE) {
265  case 1:
266  return static_cast<TYPE>(fabs(x() - c.x()));
267 
268  case 2:
269  return tlpsqrt<DTYPE, OTYPE>(tlpsqr<DTYPE, OTYPE>(x() - c.x()) +
270  tlpsqr<DTYPE, OTYPE>(y() - c.y()));
271 
272  case 3:
273  return tlpsqrt<DTYPE, OTYPE>(tlpsqr<DTYPE, OTYPE>(x() - c.x()) +
274  tlpsqr<DTYPE, OTYPE>(y() - c.y()) +
275  tlpsqr<DTYPE, OTYPE>(z() - c.z()));
276 
277  default:
278  OTYPE tmp = tlpsqr<DTYPE, OTYPE>((*this)[0] - c[0]);
279 
280  for (size_t i = 1; i < SIZE; ++i)
281  tmp += tlpsqr<DTYPE, OTYPE>((*this)[i] - c[i]);
282 
283  return (tlpsqrt<DTYPE, OTYPE>(tmp));
284  }
285 }
286 
287 #undef VECTORTLP