Geophysical Inversion and Modelling Library v1.5.4
Loading...
Searching...
No Matches
pos.h
1/******************************************************************************
2 * Copyright (C) 2006-2024 by the GIMLi development team *
3 * Carsten Rücker carsten@resistivity.net *
4 * *
5 * Licensed under the Apache License, Version 2.0 (the "License"); *
6 * you may not use this file except in compliance with the License. *
7 * You may obtain a copy of the License at *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 * See the License for the specific language governing permissions and *
15 * limitations under the License. *
16 * *
17 ******************************************************************************/
18
19#ifndef _GIMLI_POS__H
20#define _GIMLI_POS__H
21
22#include "gimli.h"
23#include "vector.h"
24
25namespace GIMLI{
26
27std::ostream & operator << (std::ostream & str, const Pos & pos);
28std::istream & operator >> (std::istream & is, Pos & pos);
29
30
31DLLEXPORT RVector3 center(const R3Vector & vPos);
32DLLEXPORT R3Vector normalise(const R3Vector & vPos);
33
34DLLEXPORT double jacobianDetXY(const RVector3 & p1, const RVector3 & p2, const RVector3 & p3);
35DLLEXPORT double angle(const RVector3 & p1, const RVector3 & p2, const RVector3 & p3);
36
37DLLEXPORT bool xVari(const R3Vector & electrodeList);
38DLLEXPORT bool yVari(const R3Vector & electrodeList);
39DLLEXPORT bool zVari(const R3Vector & electrodeList);
40
42DLLEXPORT RVector x(const R3Vector & rv);
44DLLEXPORT RVector y(const R3Vector & rv);
46DLLEXPORT RVector z(const R3Vector & rv);
47
48DLLEXPORT RVector absR3(const R3Vector & vPos);
49
50DLLEXPORT void swapXY(R3Vector & rv);
51DLLEXPORT void swapXZ(R3Vector & rv);
52DLLEXPORT void swapYZ(R3Vector & rv);
53
54DLLEXPORT std::vector < RVector3 > loadRVector3(const std::string & fileName);
55DLLEXPORT void saveRVector3(const std::vector < RVector3 > l, const std::string & fileName);
56
59DLLEXPORT RVector toArray(const R3Vector & vec);
60
62DLLEXPORT RMatrix toMatrix(const R3Vector & vec);
63
65DLLEXPORT R3Vector stdVectorRVector3ToR3Vector(const std::vector < RVector3 > & rv);
66
68DLLEXPORT std::vector < RVector3 > R3VectorTostdVectorRVector3(const R3Vector & rv);
69
71
72
73class DLLEXPORT Pos {
74public:
75
76 // static const RVector3 ZERO(0.0, 0.0, 0.0);
77// static const RVector3 UNIT_X;
78// static const RVector3 UNIT_Y;
79// static const RVector3 UNIT_Z;
80// static const RVector3 NEGATIVE_UNIT_X;
81// static const RVector3 NEGATIVE_UNIT_Y;
82// static const RVector3 NEGATIVE_UNIT_Z;
83// static const RVector3 UNIT_SCALE;
84
86 Pos() : valid_(true) { assign(0.0, 0.0, 0.0); }
87
89 Pos(bool valid) : valid_(valid) { assign(0.0, 0.0, 0.0); }
90
91 Pos(double x, double y) : valid_(true) { assign(x, y, 0.0); }
92 Pos(double x, double y, double z) : valid_(true) { assign(x, y, z); }
93
94 Pos(const Pos & pos) { copy_(pos); }
95
97 Pos & operator = (const Pos & pos){
98 if (this != & pos){ copy_(pos); } return *this; }
99
101 Pos & operator = (const Vector < double > & v){
102 if (v.size() > 2) {
103 mat_[0] = v[0];
104 mat_[1] = v[1];
105 mat_[2] = v[2];
106 } else {
107 throwLengthError(WHERE_AM_I + " v.size() < 2 " + str(v.size()));
108 }
109 return *this;
110
111 }
112
113 inline double & operator [] (Index i) { return mat_[i]; }
114
115 inline const double & operator [] (Index i) const { return mat_[i]; }
116
117 #define DEFINE_UNARY_MOD_OPERATOR__(OP) \
118 inline Pos & operator OP##= (const double & b){ mat_[0] OP##= b; mat_[1] OP##= b; mat_[2] OP##= b; return *this; }\
119 inline Pos & operator OP##= (const Pos & b){ mat_[0] OP##= b[0]; mat_[1] OP##= b[1]; mat_[2] OP##= b[2]; return *this; }\
120
121 DEFINE_UNARY_MOD_OPERATOR__(+)
122 DEFINE_UNARY_MOD_OPERATOR__(-)
123 DEFINE_UNARY_MOD_OPERATOR__(/)
124 DEFINE_UNARY_MOD_OPERATOR__(*)
125
126 #undef DEFINE_UNARY_MOD_OPERATOR__
127
128 Pos operator - () const { return Pos(-mat_[0], -mat_[1], -mat_[2]); }
129
130 inline void setValid(bool valid) { valid_ = valid; }
131 inline bool valid() const { return valid_; }
132
133 inline void assign(const double & x, const double & y, const double & z) {
134 mat_[0] = x; mat_[1] = y; mat_[2] = z;
135 // x_ = x; y_ = y; z_ = z;
136 }
137
138 inline const double & x() const { return mat_[0]; }
139 inline const double & y() const { return mat_[1]; }
140 inline const double & z() const { return mat_[2]; }
141 inline void setX(double x) { mat_[0] = x; }
142 inline void setY(double y) { mat_[1] = y; }
143 inline void setZ(double z) { mat_[2] = z; }
144
146 inline void setVal(const double & val, Index i) {
147 if (i < 3) {
148 mat_[i] = val;
149 } else {
150 throwRangeError(WHERE_AM_I, i, 0, 3);
151 }
152 }
153
155 inline const double & getVal(Index i) const {
156 if (i < 3) {
157 return mat_[i];
158 } else {
159 throwRangeError(WHERE_AM_I, i, 0, 3);
160 }
161 return mat_[0];
162 }
163
165 inline Pos & round(double tol){
166 mat_[0] = rint(mat_[0] / tol) * tol;
167 mat_[1] = rint(mat_[1] / tol) * tol;
168 mat_[2] = rint(mat_[2] / tol) * tol;
169 return *this;
170 }
171
173 inline double distSquared(const Pos & p) const {
174 return ((mat_[0] - p[0]) * (mat_[0] - p[0]) +
175 (mat_[1] - p[1]) * (mat_[1] - p[1]) +
176 (mat_[2] - p[2]) * (mat_[2] - p[2]));
177 }
178
180 inline double dist(const Pos & p) const { return std::sqrt(distSquared(p)); }
181
183 inline double distance(const Pos & p) const { return dist(p); }
184
186 inline double distSquared() const {
187 return mat_[0] * mat_[0] + mat_[1] * mat_[1] + mat_[2] * mat_[2];
188 }
189
190 inline double abs() const { return length(); }
191
193 inline double length() const { return std::sqrt(distSquared()); }
194
196 double angle(const Pos & p) const;
197
199 double angle(const RVector3 & p1, const RVector3 & p3) const;
200
201 inline double dot(const Pos & p) const {
202 return mat_[0] * p[0] + mat_[1] * p[1] + mat_[2] * p[2];
203 }
204
205 inline double sum() const {
206 return mat_[0] + mat_[1] + mat_[2];
207 }
208
209 Pos norm(const Pos & p1, const Pos & p2) const;
210
212 Pos norm() const {
213 Pos p(*this);
214 p.normalize();
215 return p;
216 }
217
220 double t = this->abs();
221 if (t > TOLERANCE) *this /= t;
222 return *this;
223 }
224
226 Pos & normalise(){ return normalize(); }
227
228 Pos cross(const Pos & p) const;
229
230 Pos normXY(const Pos & p) const;
231
232 template < class Matrix > Pos & transform(const Matrix & wm){
233 double x = mat_[0], y = mat_[1], z = mat_[2];
234
235 mat_[0] = x * wm[0][0] + y * wm[0][1] + z * wm[0][2];
236 mat_[1] = x * wm[1][0] + y * wm[1][1] + z * wm[1][2];
237 mat_[2] = x * wm[2][0] + y * wm[2][1] + z * wm[2][2];
238 return *this;
239 }
240
241 inline Pos & rotateX(double phi){
242 double mat[3][3] ={{ 1.0, 0.0, 0.0},
243 { 0.0, std::cos(phi), -std::sin(phi)},
244 { 0.0, std::sin(phi), std::cos(phi)} };
245 return this->transform(mat);
246 }
247 inline Pos & rotateY(double phi){
248 double mat[3][3] = {{std::cos(phi), 0.0, std::sin(phi)},
249 {0.0, 1.0, 0.0},
250 {-std::sin(phi), 0.0, std::cos(phi)}};
251
252 return this->transform(mat);
253 }
254 inline Pos & rotateZ(double phi){
255 double mat[3][3] = {{std::cos(phi), -std::sin(phi), 0.0},
256 {std::sin(phi), std::cos(phi), 0.0},
257 { 0.0, 0.0, 1.0}};
258 return this->transform(mat);
259 }
260
261 inline Pos & rotate(const RVector3 & r){
262 return this->rotateX(r[0]).rotateY(r[1]).rotateZ(r[2]);
263 }
264 inline Pos & rotate(double phiX, double phiY, double phiZ){
265 return this->rotateX(phiX).rotateY(phiY).rotateZ(phiZ);
266 }
267
268 inline Pos & scale(const RVector3 & s){ return (*this) *= s;}
269
270 inline Pos & translate(const RVector3 & t){ return (*this) += t;}
271
272 inline Pos & translate(double x, double y=0.0, double z=0.0){
273 mat_[0] += x; mat_[1] += y; mat_[2] += z; return *this; }
274
275
276 RVector vec() const {
277 RVector tmp(3);
278 tmp[0] = mat_[0];
279 tmp[1] = mat_[1];
280 tmp[2] = mat_[2];
281 return tmp;
282 }
283
284 Index hash() const {
285 return GIMLI::hash(mat_[0], mat_[1], mat_[2], valid_);
286 }
287
289 void swap(Index i, Index j){
290 ASSERT_RANGE(i, 0, 3);
291 ASSERT_RANGE(j, 0, 3);
292 if (i != j){
293 double tmp = mat_[i];
294 mat_[i] = mat_[j];
295 mat_[j] = tmp;
296 }
297 }
298protected:
299
300 inline void copy_(const Pos & pos) {
301 valid_ = pos.valid(); assign(pos[0], pos[1], pos[2]);
302 }
303
304 bool valid_;
305
306 double mat_[3];
307
308};
309
310// template < class double > const RVector3 RVector3::ZERO(0.0, 0.0, 0.0);
311// template < class double > const RVector3 RVector3::UNIT_X(1.0, 0.0, 0.0);
312// template < class double > const RVector3 RVector3::UNIT_Y(0.0, 1.0, 0.0);
313// template < class double > const RVector3 RVector3::UNIT_Z(0.0, 0.0, 1.0);
314// template < class double > const RVector3 RVector3::NEGATIVE_UNIT_X(-1.0, 0.0, 0.0);
315// template < class double > const RVector3 RVector3::NEGATIVE_UNIT_Y(0.0, -1.0, 0.0);
316// template < class double > const RVector3 RVector3::NEGATIVE_UNIT_Z(0.0, 0.0, -1.0);
317// template < class double > const RVector3 RVector3::UNIT_SCALE(1.0, 1.0, 1.0);
318
319inline bool operator == (const RVector3 & a , const RVector3 & b){
320 if (a.valid() != b.valid()) return false;
321 if (a.distSquared(b) < TOLERANCE) return true; else return false;
322}
323inline bool operator != (const RVector3 & a , const RVector3 & b){
324 return !(a == b);
325}
326inline bool operator < (const RVector3 & a , const RVector3 & b){
327 return a.distSquared() < b.distSquared();
328}
329inline bool operator <= (const RVector3 & a , const RVector3 & b){
330 return a.distSquared() <= b.distSquared();
331}
332inline bool operator > (const RVector3 & a , const RVector3 & b){
333 return a.distSquared() > b.distSquared();
334}
335inline bool operator >= (const RVector3 & a , const RVector3 & b){
336 return a.distSquared() >= b.distSquared();
337}
338inline RVector3 RINT(const RVector3 & a) {
339 std::cout << WHERE_AM_I << std::endl;
340 return RVector3();
341}
342
343#define DEFINE_POS_BIN_OPERATOR__(OP) \
344inline Pos operator OP (const Pos & a, const Pos & b){ \
345 Pos tmp(a); return tmp OP##= b; } \
346inline Pos operator OP (const Pos & a, const double & b){ \
347 Pos tmp(a); return tmp OP##= b; } \
348inline Pos operator OP (const double & a, const Pos & b){ \
349 Pos tmp(a, a, a); return tmp OP##= b; } \
350\
351
352DEFINE_POS_BIN_OPERATOR__(+)
353DEFINE_POS_BIN_OPERATOR__(*)
354DEFINE_POS_BIN_OPERATOR__(/)
355DEFINE_POS_BIN_OPERATOR__(-)
356
357
358inline std::ostream & operator << (std::ostream & str, const Pos & pos){
359 if (pos.valid()){
360 str << pos[0] << "\t" << pos[1] << "\t" << pos[2];
361 } else {
362 str << " pos is not valid";
363 }
364 return str;
365}
366
367inline std::istream & operator >> (std::istream & is, Pos & pos){
368 THROW_TO_IMPL
369 return is;
370}
371
373inline bool posLesserXrY(const RVector3 & a, const RVector3 & b){
374 if (abs(a[0] - b[0]) < TOLERANCE) {
375 if (abs(a[1] - b[1]) < TOLERANCE) {
376 return a[2] < b[2];
377 } else {
378 return a[1] > b[1];
379 }
380 } else return a[0] < b[0];
381}
382
384inline bool posLesserXYrZ(const RVector3 & a, const RVector3 & b){
385 if (abs(a[0] - b[0]) < TOLERANCE) {
386 if (abs(a[1] - b[1]) < TOLERANCE) {
387 return a[2] > b[2];
388 } else {
389 return a[1] < b[1];
390 }
391 } else return a[0] < b[0];
392}
393
394inline bool posLesserX(const RVector3 & a, const RVector3 & b){
395 if (abs(a[0] - b[0]) < TOLERANCE) {
396 if (abs(a[1] - b[1]) < TOLERANCE) {
397 return a[2] < b[2];
398 } else {
399 return a[1] < b[1];
400 }
401 } else return a[0] < b[0];
402}
403
404} // namespace GIMLI;
405
406#ifndef PYGIMLI_CAST
407namespace std {
408 template<> struct hash<GIMLI::Pos> {
409 GIMLI::Index operator()(const GIMLI::Pos & p) const noexcept {
410 return p.hash();
411 }
412 };
413 template<> struct hash< GIMLI::PosVector > {
414 GIMLI::Index operator()(const GIMLI::PosVector & p) const noexcept {
415 return p.hash();
416 }
417 };
418}
419#endif // PYGIMLI_CAST
420#endif // _GIMLI_POS__H
Simple row-based dense matrix based on Vector.
Definition matrix.h:324
3 dimensional vector
Definition pos.h:73
const double & getVal(Index i) const
Definition pos.h:155
Pos & normalise()
Definition pos.h:226
double distSquared() const
Definition pos.h:186
Pos(bool valid)
Definition pos.h:89
void setVal(const double &val, Index i)
Definition pos.h:146
double distance(const Pos &p) const
Definition pos.h:183
double distSquared(const Pos &p) const
Definition pos.h:173
double abs() const
Definition pos.h:190
void swap(Index i, Index j)
Definition pos.h:289
Pos norm() const
Definition pos.h:212
double length() const
Definition pos.h:193
Pos & normalize()
Definition pos.h:219
double dist(const Pos &p) const
Definition pos.h:180
Pos()
Definition pos.h:86
Pos & round(double tol)
Definition pos.h:165
GIMLi main namespace for the Geophyiscal Inversion and Modelling Library.
Definition baseentity.h:24
RMatrix toMatrix(const R3Vector &vec)
Definition pos.cpp:149
RVector y(const R3Vector &rv)
Definition pos.cpp:114
RVector x(const R3Vector &rv)
Definition pos.cpp:107
RVector toArray(const R3Vector &vec)
Definition pos.cpp:139
RVector z(const R3Vector &rv)
Definition pos.cpp:120
bool posLesserXrY(const RVector3 &a, const RVector3 &b)
Definition pos.h:373
bool operator<(const PolynomialElement< ValueType > &a, const PolynomialElement< ValueType > &b)
Definition polynomial.h:48
R3Vector stdVectorRVector3ToR3Vector(const std::vector< Pos > &rv)
Definition pos.cpp:157
std::vector< Pos > R3VectorTostdVectorRVector3(const R3Vector &rv)
Definition pos.cpp:163
bool posLesserXYrZ(const RVector3 &a, const RVector3 &b)
Definition pos.h:384