Directory: | ./ |
---|---|
File: | Geometry/point.hpp |
Date: | 2024-04-14 07:32:34 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 119 | 124 | 96.0% |
Branches: | 10 | 16 | 62.5% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // ______ ______ _ _ _____ ______ | ||
2 | // | ____| ____| | (_)/ ____| | ____| | ||
3 | // | |__ | |__ | | _| (___ ___| |__ | ||
4 | // | __| | __| | | | |\___ \ / __| __| | ||
5 | // | | | |____| |____| |____) | (__| |____ | ||
6 | // |_| |______|______|_|_____/ \___|______| | ||
7 | // Finite Elements for Life Sciences and Engineering | ||
8 | // | ||
9 | // License: LGL2.1 License | ||
10 | // FELiScE default license: LICENSE in root folder | ||
11 | // | ||
12 | // Main authors: J-F. Gerbeau | ||
13 | // | ||
14 | |||
15 | #ifndef _FEL_POINT_HPP | ||
16 | #define _FEL_POINT_HPP | ||
17 | |||
18 | // System includes | ||
19 | #include <cstdlib> | ||
20 | #include <iostream> | ||
21 | #include <vector> | ||
22 | #include <atomic> | ||
23 | #include <array> | ||
24 | |||
25 | // External includes | ||
26 | |||
27 | // Project includes | ||
28 | #include "Core/shared_pointers.hpp" | ||
29 | #include "Core/felisceTools.hpp" | ||
30 | |||
31 | namespace felisce | ||
32 | { | ||
33 | ///@name felisce Globals | ||
34 | ///@{ | ||
35 | |||
36 | ///@} | ||
37 | ///@name Type Definitions | ||
38 | ///@{ | ||
39 | |||
40 | ///@} | ||
41 | ///@name Enum's | ||
42 | ///@{ | ||
43 | |||
44 | ///@} | ||
45 | ///@name Functions | ||
46 | ///@{ | ||
47 | |||
48 | ///@} | ||
49 | ///@name felisce Classes | ||
50 | ///@{ | ||
51 | |||
52 | class Point | ||
53 | { | ||
54 | public: | ||
55 | ///@name Type Definitions | ||
56 | ///@{ | ||
57 | |||
58 | /// Pointer definition of Point | ||
59 | FELISCE_CLASS_INTRUSIVE_POINTER_DEFINITION(Point); | ||
60 | |||
61 | /// Definition of the size type | ||
62 | typedef std::size_t SizeType; | ||
63 | |||
64 | /// Definition of the index type | ||
65 | typedef std::size_t IndexType; | ||
66 | |||
67 | /// Toleranc definition | ||
68 | static constexpr double absToll = 1.e-10; | ||
69 | |||
70 | ///@} | ||
71 | ///@name Life Cycle | ||
72 | ///@{ | ||
73 | |||
74 | // default constructor | ||
75 | Point() = default; | ||
76 | |||
77 | // construction from a scalar | ||
78 | 1207 | Point(double val) { | |
79 | 1207 | m_coor[0] = val; | |
80 | 1207 | m_coor[1] = val; | |
81 | 1207 | m_coor[2] = val; | |
82 | 1207 | } | |
83 | |||
84 | // construction from an array | ||
85 | 24423 | Point(const std::array<double, 3>& coord) : m_coor(coord) {} | |
86 | |||
87 | // construction from a C array | ||
88 | 2 | Point(const double* coord) | |
89 | 2 | { | |
90 | 2 | m_coor[0] = coord[0]; | |
91 | 2 | m_coor[1] = coord[1]; | |
92 | 2 | m_coor[2] = coord[2]; | |
93 | 2 | } | |
94 | |||
95 | // construction from a std::vector | ||
96 | 2 | Point(const std::vector<double>& coord) | |
97 | 2 | { | |
98 | 2 | FEL_ASSERT_EQUAL(coord.size(), 3); | |
99 | 2 | m_coor[0] = coord[0]; | |
100 | 2 | m_coor[1] = coord[1]; | |
101 | 2 | m_coor[2] = coord[2]; | |
102 | 2 | } | |
103 | |||
104 | // construction from three scalars | ||
105 | 10748966 | Point(const double xval, const double yval, const double zval) | |
106 | 10748966 | { | |
107 | 10748966 | m_coor[0] = xval; | |
108 | 10748966 | m_coor[1] = yval; | |
109 | 10748966 | m_coor[2] = zval; | |
110 | 10748966 | } | |
111 | |||
112 | // copy constructor | ||
113 | 7370226 | Point(const Point& P) : m_coor(P.m_coor) {} | |
114 | |||
115 | ///@} | ||
116 | ///@name Operators | ||
117 | ///@{ | ||
118 | |||
119 | // Assignment. | ||
120 | 4359315 | Point& operator=(const Point& P) { | |
121 | 4359315 | m_coor = P.m_coor; | |
122 | |||
123 | 4359315 | return *this; | |
124 | } | ||
125 | |||
126 | 217033 | inline double operator [](const IndexType i) const { | |
127 | 217033 | return m_coor[i]; | |
128 | } | ||
129 | |||
130 | 45269721 | inline double& operator [] (const IndexType i) { | |
131 | 45269721 | return m_coor[i]; | |
132 | } | ||
133 | |||
134 | 325 | inline bool operator!=(const Point& rOther) const { | |
135 | 325 | return ! this->operator==(rOther); | |
136 | } | ||
137 | |||
138 | 974367 | inline void operator/=(const double Value) { | |
139 | 974367 | m_coor[0] /= Value; | |
140 | 974367 | m_coor[1] /= Value; | |
141 | 974367 | m_coor[2] /= Value; | |
142 | 974367 | } | |
143 | |||
144 | 39501 | inline void operator*=(const double Value) { | |
145 | 39501 | m_coor[0] *= Value; | |
146 | 39501 | m_coor[1] *= Value; | |
147 | 39501 | m_coor[2] *= Value; | |
148 | 39501 | } | |
149 | |||
150 | 25512 | inline void operator+=(const Point& rOther) { | |
151 | 25512 | m_coor[0] += rOther.x(); | |
152 | 25512 | m_coor[1] += rOther.y(); | |
153 | 25512 | m_coor[2] += rOther.z(); | |
154 | 25512 | } | |
155 | |||
156 | 343508 | inline void operator-=(const Point& rOther) { | |
157 | 343508 | m_coor[0] -= rOther.x(); | |
158 | 343508 | m_coor[1] -= rOther.y(); | |
159 | 343508 | m_coor[2] -= rOther.z(); | |
160 | 343508 | } | |
161 | |||
162 | 330 | inline bool operator==(const Point& rOther) const { | |
163 | return | ||
164 |
1/2✓ Branch 3 taken 328 times.
✗ Branch 4 not taken.
|
658 | Tools::equalTol(m_coor[0],rOther.x(),absToll) && |
165 |
3/4✓ Branch 0 taken 328 times.
✓ Branch 1 taken 2 times.
✓ Branch 5 taken 328 times.
✗ Branch 6 not taken.
|
658 | Tools::equalTol(m_coor[1],rOther.y(),absToll) && |
166 | 658 | Tools::equalTol(m_coor[2],rOther.z(),absToll); | |
167 | } | ||
168 | |||
169 | ✗ | friend std::ostream & operator<<(std::ostream& os, const Point& mc) { | |
170 | ✗ | return os <<"P: x = " << mc.x() << " y = " << mc.y() << " z = " << mc.z(); | |
171 | } | ||
172 | |||
173 | ///@} | ||
174 | ///@name Operations | ||
175 | ///@{ | ||
176 | |||
177 | /** | ||
178 | * @brief This method creates a clone of the current point | ||
179 | * @return A new pointer with exactly the same coordinates | ||
180 | */ | ||
181 | 1 | Pointer Clone() | |
182 | { | ||
183 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | auto p_new_point = felisce::make_intrusive<Point>(this->coor()); |
184 | 1 | return p_new_point; | |
185 | } | ||
186 | |||
187 | // Method to compute the norm of the coordinates | ||
188 | 1023810 | inline double norm() const { | |
189 | 1023810 | double norm = 0.; | |
190 |
2/2✓ Branch 0 taken 3071430 times.
✓ Branch 1 taken 1023810 times.
|
4095240 | for(std::size_t icoor=0; icoor<3; ++icoor) { |
191 | 3071430 | norm += std::pow(m_coor[icoor], 2); | |
192 | } | ||
193 | 1023810 | return std::sqrt(norm); | |
194 | } | ||
195 | |||
196 | // coordinate getters | ||
197 | 51306902 | inline double x() const { | |
198 | 51306902 | return m_coor[0]; | |
199 | } | ||
200 | 48748976 | inline double y() const { | |
201 | 48748976 | return m_coor[1]; | |
202 | } | ||
203 | 24429456 | inline double z() const { | |
204 | 24429456 | return m_coor[2]; | |
205 | } | ||
206 | ✗ | inline const double* coor() const { | |
207 | ✗ | return m_coor.data(); | |
208 | } | ||
209 | |||
210 | // coordinate setters | ||
211 | 23768245 | inline double& x() { | |
212 | 23768245 | return m_coor[0]; | |
213 | } | ||
214 | 23773809 | inline double& y() { | |
215 | 23773809 | return m_coor[1]; | |
216 | } | ||
217 | 19057724 | inline double& z() { | |
218 | 19057724 | return m_coor[2]; | |
219 | } | ||
220 | 2185688 | inline double coor(const IndexType i) const { | |
221 | 2185688 | return m_coor[i]; | |
222 | } | ||
223 | 850314490 | inline double& coor(const IndexType i) { | |
224 | 850314490 | return m_coor[i]; | |
225 | } | ||
226 | |||
227 | const std::array<double, 3>& getCoor() const; | ||
228 | |||
229 | std::array<double, 3>& getCoor(); | ||
230 | |||
231 | void getCoor(std::vector<double>& coord); | ||
232 | |||
233 | void setCoor(const std::vector<double>& coord); | ||
234 | |||
235 | void setCoor(const std::array<double,3>& coord); | ||
236 | |||
237 | void setCoor(const double* coord); | ||
238 | |||
239 | //distance between this point and another point | ||
240 | 434 | double dist(const Point& b) const { | |
241 | 434 | double d(0); | |
242 |
2/2✓ Branch 0 taken 1302 times.
✓ Branch 1 taken 434 times.
|
1736 | for (IndexType i = 0; i<3; ++i) { |
243 | 1302 | d+=(m_coor[i]-b.coor(i))*(m_coor[i]-b.coor(i)); | |
244 | } | ||
245 | 434 | return std::sqrt(d); | |
246 | } | ||
247 | |||
248 | 4322807 | void clear() | |
249 | { | ||
250 | 4322807 | this->x() = 0.0; | |
251 | 4322807 | this->y() = 0.0; | |
252 | 4322807 | this->z() = 0.0; | |
253 | 4322807 | } | |
254 | |||
255 | ///@} | ||
256 | ///@name Access | ||
257 | ///@{ | ||
258 | |||
259 | 2 | inline double* coor() { | |
260 | 2 | return m_coor.data(); | |
261 | } | ||
262 | |||
263 | ///@} | ||
264 | ///@name Inquiry | ||
265 | ///@{ | ||
266 | |||
267 | ///@} | ||
268 | ///@name Input and output | ||
269 | ///@{ | ||
270 | |||
271 | // info | ||
272 | void print(const IndexType verbose, std::ostream& c = std::cout) const; | ||
273 | void printWithTab(std::ostream& outstr) const; | ||
274 | |||
275 | ///@} | ||
276 | ///@name Friends | ||
277 | ///@{ | ||
278 | |||
279 | ///@} | ||
280 | protected: | ||
281 | ///@name Protected static Member Variables | ||
282 | ///@{ | ||
283 | |||
284 | ///@} | ||
285 | ///@name Protected member Variables | ||
286 | ///@{ | ||
287 | |||
288 | std::array<double, 3> m_coor; | ||
289 | |||
290 | ///@} | ||
291 | ///@name Protected Operators | ||
292 | ///@{ | ||
293 | |||
294 | ///@} | ||
295 | ///@name Protected Operations | ||
296 | ///@{ | ||
297 | |||
298 | ///@} | ||
299 | ///@name Protected Access | ||
300 | ///@{ | ||
301 | |||
302 | ///@} | ||
303 | ///@name Protected Inquiry | ||
304 | ///@{ | ||
305 | |||
306 | ///@} | ||
307 | ///@name Protected LifeCycle | ||
308 | ///@{ | ||
309 | |||
310 | ///@} | ||
311 | private: | ||
312 | ///@name Private static Member Variables | ||
313 | ///@{ | ||
314 | |||
315 | ///@} | ||
316 | ///@name Private member Variables | ||
317 | ///@{ | ||
318 | |||
319 | //this block is needed for refcounting | ||
320 | mutable std::atomic<int> mReferenceCounter; | ||
321 | |||
322 | ///@} | ||
323 | ///@name Private Operators | ||
324 | ///@{ | ||
325 | |||
326 | 1 | friend void intrusive_ptr_add_ref(const Point* x) | |
327 | { | ||
328 | 1 | x->mReferenceCounter.fetch_add(1, std::memory_order_relaxed); | |
329 | 1 | } | |
330 | |||
331 | 1 | friend void intrusive_ptr_release(const Point* x) | |
332 | { | ||
333 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
2 | if (x->mReferenceCounter.fetch_sub(1, std::memory_order_release) == 1) { |
334 | std::atomic_thread_fence(std::memory_order_acquire); | ||
335 | ✗ | delete x; | |
336 | } | ||
337 | 1 | } | |
338 | |||
339 | ///@} | ||
340 | ///@name Private Operations | ||
341 | ///@{ | ||
342 | |||
343 | ///@} | ||
344 | ///@name Private Access | ||
345 | ///@{ | ||
346 | |||
347 | ///@} | ||
348 | ///@name Private Inquiry | ||
349 | ///@{ | ||
350 | |||
351 | ///@} | ||
352 | ///@name Private LifeCycle | ||
353 | ///@{ | ||
354 | |||
355 | ///@} | ||
356 | }; | ||
357 | ///@} | ||
358 | ///@name Type Definitions | ||
359 | ///@{ | ||
360 | |||
361 | ///@} | ||
362 | ///@name Operators | ||
363 | ///@{ | ||
364 | 7135 | inline Point operator*(const double value, const Point& point) | |
365 | { | ||
366 | 7135 | Point pt(point); | |
367 | 7135 | pt *= value; | |
368 | 7135 | return pt; | |
369 | } | ||
370 | |||
371 | 36 | inline Point operator*(const Point& point, const double value) | |
372 | { | ||
373 | 36 | Point pt(point); | |
374 | 36 | pt *= value; | |
375 | 36 | return pt; | |
376 | } | ||
377 | |||
378 | inline Point operator/(const Point& point, const double value) | ||
379 | { | ||
380 | Point pt(point); | ||
381 | pt /= value; | ||
382 | return pt; | ||
383 | } | ||
384 | |||
385 | 1089 | inline Point operator+(const Point& lPoint, const Point& rPoint) | |
386 | { | ||
387 | 1089 | Point pt(lPoint); | |
388 | 1089 | pt += rPoint; | |
389 | 1089 | return pt; | |
390 | } | ||
391 | |||
392 | 336977 | inline Point operator-(const Point& lPoint, const Point& rPoint) | |
393 | { | ||
394 | 336977 | Point pt(lPoint); | |
395 | 336977 | pt -= rPoint; | |
396 | 336977 | return pt; | |
397 | } | ||
398 | |||
399 | 26923 | inline double operator*(const Point& lPoint, const Point& rPoint) | |
400 | { | ||
401 | 26923 | return lPoint[0]*rPoint[0]+lPoint[1]*rPoint[1]+lPoint[2]*rPoint[2]; | |
402 | } | ||
403 | ///@} | ||
404 | } /* namespace felisce.*/ | ||
405 | |||
406 | #endif /* _FEL_POINT_HPP defined */ | ||
407 | |||
408 |