Tulip 5.7.1
Large graphs analysis and drawing
Loading...
Searching...
No Matches
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//======================================================
25TEMPLATEVECTOR
26VECTORTLP &VECTORTLP::operator*=(const TYPE scalaire) {
27 for (size_t i = 0; i < SIZE; ++i)
28 (*this)[i] *= scalaire;
29
30 return (*this);
31}
32//======================================================
33TEMPLATEVECTOR
34VECTORTLP &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//======================================================
41TEMPLATEVECTOR
42VECTORTLP &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//======================================================
51TEMPLATEVECTOR
52VECTORTLP &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//======================================================
61TEMPLATEVECTOR
62VECTORTLP &VECTORTLP::operator+=(const TYPE scalaire) {
63 for (size_t i = 0; i < SIZE; ++i)
64 (*this)[i] += scalaire;
65
66 return (*this);
67}
68//======================================================
69TEMPLATEVECTOR
70VECTORTLP &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//======================================================
77TEMPLATEVECTOR
78VECTORTLP &VECTORTLP::operator-=(const TYPE scalaire) {
79 for (size_t i = 0; i < SIZE; ++i)
80 (*this)[i] -= scalaire;
81
82 return (*this);
83}
84//======================================================
85TEMPLATEVECTOR
86VECTORTLP &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//======================================================
93TEMPLATEVECTOR
94VECTORTLP &VECTORTLP::operator^=(const VECTORTLP &v) {
95 (*this) = (*this) ^ v;
96 return (*this);
97}
98//======================================================
99TEMPLATEVECTOR
100VECTORTLP tlp::operator+(const VECTORTLP &u, const VECTORTLP &v) {
101 return VECTORTLP(u) += v;
102}
103//======================================================
104TEMPLATEVECTOR
105VECTORTLP tlp::operator+(const VECTORTLP &u, const TYPE scalaire) {
106 return VECTORTLP(u) += scalaire;
107}
108//======================================================
109TEMPLATEVECTOR
110VECTORTLP tlp::operator-(const VECTORTLP &u, const VECTORTLP &v) {
111 return VECTORTLP(u) -= v;
112}
113//======================================================
114TEMPLATEVECTOR
115VECTORTLP tlp::operator-(const VECTORTLP &u, const TYPE scalaire) {
116 return VECTORTLP(u) -= scalaire;
117}
118//======================================================
119TEMPLATEVECTOR
120VECTORTLP tlp::operator*(const VECTORTLP &u, const VECTORTLP &v) {
121 return VECTORTLP(u) *= v;
122}
123//======================================================
124TEMPLATEVECTOR
125VECTORTLP tlp::operator*(const VECTORTLP &u, const TYPE scalaire) {
126 return VECTORTLP(u) *= scalaire;
127}
128//======================================================
129TEMPLATEVECTOR
130VECTORTLP tlp::operator*(const TYPE scalaire, const VECTORTLP &u) {
131 return VECTORTLP(u) *= scalaire;
132}
133//======================================================
134TEMPLATEVECTOR
135VECTORTLP tlp::operator/(const VECTORTLP &u, const VECTORTLP &v) {
136 return VECTORTLP(u) /= v;
137}
138//======================================================
139TEMPLATEVECTOR
140VECTORTLP tlp::operator/(const VECTORTLP &u, const TYPE scalaire) {
141 return VECTORTLP(u) /= scalaire;
142}
143//======================================================
144TEMPLATEVECTOR
145VECTORTLP 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//======================================================
165TEMPLATEVECTOR
166VECTORTLP tlp::operator-(const VECTORTLP &u) {
167 return VECTORTLP(u) *= static_cast<TYPE>(-1);
168}
169//======================================================
170TEMPLATEVECTOR
171bool VECTORTLP::operator>(const VECTORTLP &vecto) const {
172 return vecto < (*this);
173}
174//======================================================
175TEMPLATEVECTOR
176bool 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//======================================================
197TEMPLATEVECTOR
198bool VECTORTLP::operator!=(const VECTORTLP &vecto) const {
199 return (!((*this) == vecto));
200}
201//======================================================
202TEMPLATEVECTOR
203bool 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//======================================================
220TEMPLATEVECTOR
221TYPE 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//======================================================
231TEMPLATEVECTOR
232VECTORTLP &VECTORTLP::fill(const TYPE scalaire) {
233 for (size_t i = 0; i < SIZE; ++i)
234 (*this)[i] = scalaire;
235
236 return (*this);
237}
238//======================================================
239TEMPLATEVECTOR
240TYPE 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//======================================================
262TEMPLATEVECTOR
263DTYPE 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