8 #define M_PI_2 (1.570796326794896619231321691639751442098584699687552910487)
15 static const cart2_t CART2_ZERO = {0, 0};
16 static const cart3_t CART3_ZERO = {0, 0, 0};
20 cart2_t res = {a.x+b.x, a.y+b.y};
26 cart2_t res = {a.x-b.x, a.y-b.y};
32 cart2_t res = {c * v.x, c * v.y};
38 return a.x * b.x + a.y * b.y;
41 static inline double cart2_normsq(
const cart2_t a) {
47 return hypot(v.x, v.y);
54 pol.phi = atan2(cart.y, cart.x);
72 sph.phi = atan2(cart.y, cart.x);
78 sph_t sph = {fabs(z), z < 0 ? M_PI : 0, 0};
104 return a.x * b.x + a.y * b.y + a.z * b.z;
111 .x = a.y * b.z - a.z * b.y,
112 .y = a.z * b.x - a.x * b.z,
113 .z = a.x * b.y - a.y * b.x,
137 sph.theta = sph.r ? acos(cart.z / sph.r) : M_PI_2;
138 sph.phi = atan2(cart.y, cart.x);
146 #ifdef QPMS_VECTORS_NICE_TRANSFORMATIONS
147 (sph.theta == M_PI) ? 0 :
150 cart.x = sph.r * sin_th * cos(sph.phi);
151 cart.y = sph.r * sin_th * sin(sph.phi);
152 cart.z = sph.r * cos(sph.theta);
159 cart.x = pol.r * cos(pol.phi);
160 cart.y = pol.r * sin(pol.phi);
174 cart3_t res = {a.x+b.x, a.y+b.y, a.z+b.z};
180 cart3_t res = {a.x-b.x, a.y-b.y, a.z-b.z};
186 cart3_t res = {c * v.x, c * v.y, c * v.z};
192 cart3_t res = {v.x / c, v.y / c, v.z / c};
201 static inline bool cart3_isclose(
const cart3_t a,
const cart3_t b,
double rtol,
double atol) {
207 ccart3_t res = {c * v.x, c * v.y, c * v.z};
213 ccart3_t res = {a.x+b.x, a.y+b.y, a.z+b.z};
219 ccart3_t res = {a.x-b.x, a.y-b.y, a.z-b.z};
225 return a.x * b.x + a.y * b.y + a.z * b.z;
236 csphvec_t res = {a.rc + b.rc, a.thetac + b.thetac, a.phic + b.phic};
242 csphvec_t res = {a.rc - b.rc, a.thetac - b.thetac, a.phic - b.phic};
248 csphvec_t res = {c * v.rc, c * v.thetac, c * v.phic};
255 return a.rc * b.rc + a.thetac * b.thetac + a.phic * b.phic;
260 sph_t res = {c * s.r, s.theta, s.phi};
266 csph_t res = {c * s.r, s.theta, s.phi};
273 const double st = sin(at.theta);
274 const double ct = cos(at.theta);
275 const double sf = sin(at.phi);
276 const double cf = cos(at.phi);
277 const double rx = st * cf;
278 const double ry = st * sf;
279 const double rz = ct;
280 const double tx = ct * cf;
281 const double ty = ct * sf;
282 const double tz = -st;
283 const double fx = -sf;
284 const double fy = cf;
285 const double fz = 0.;
287 res.x = rx * sphvec.rc + tx * sphvec.thetac + fx * sphvec.phic;
288 res.y = ry * sphvec.rc + ty * sphvec.thetac + fy * sphvec.phic;
289 res.z = rz * sphvec.rc + tz * sphvec.thetac + fz * sphvec.phic;
301 const sph_t atreal = {0 , at.theta, at.phi};
308 const double st = sin(at.theta);
309 const double ct = cos(at.theta);
310 const double sf = sin(at.phi);
311 const double cf = cos(at.phi);
312 const double rx = st * cf;
313 const double ry = st * sf;
314 const double rz = ct;
315 const double tx = ct * cf;
316 const double ty = ct * sf;
317 const double tz = -st;
318 const double fx = -sf;
319 const double fy = cf;
320 const double fz = 0.;
322 res.rc = rx * cartvec.x + ry * cartvec.y + rz * cartvec.z;
323 res.thetac = tx * cartvec.x + ty * cartvec.y + tz * cartvec.z;
324 res.phic = fx * cartvec.x + fy * cartvec.y + fz * cartvec.z;
330 csph_t cs = {s.r, s.theta, s.phi};
336 sph_t rs = {creal(s.r), s.theta, s.phi};
351 cart3_t rcart = {creal(cart.x), creal(cart.y), creal(cart.z)};
352 cart3_t icart = {cimag(cart.x), cimag(cart.y), cimag(cart.z)};
362 sph.theta = sph.r ? acos(cart.z / sph.r) : M_PI_2;
363 sph.phi = atan2(cart.y, cart.x);
371 #ifdef QPMS_VECTORS_NICE_TRANSFORMATIONS
372 (sph.theta == M_PI) ? 0 :
375 cart.x = sph.r * sin_th * cos(sph.phi);
376 cart.y = sph.r * sin_th * sin(sph.phi);
377 cart.z = sph.r * cos(sph.theta);
385 void print_sph(
sph_t);
391 sum->x = sum->y = sum->z = compensation->x = compensation->y = compensation->z = 0;
395 sum->rc = sum->thetac = sum->phic = compensation->rc = compensation->thetac = compensation->phic = 0;
416 return sqrt(creal(a.rc * conj(a.rc) + a.thetac * conj(a.thetac) + a.phic * conj(a.phic)));
419 static inline double csphvec_reldiff_abstol(
const csphvec_t a,
const csphvec_t b,
double tolerance) {
422 if (anorm <= tolerance && bnorm <= tolerance)
return 0;
427 return csphvec_reldiff_abstol(a, b, 0);
513 static const cart3_t CART3_INVALID = {NAN, NAN, NAN};
514 static const cart2_t CART2_INVALID = {NAN, NAN};
515 static const double CART1_INVALID = NAN;
516 static const sph_t SPH_INVALID = {NAN, NAN, NAN};
517 static const pol_t POL_INVALID = {NAN, NAN};
528 " coordinates not allowed");
538 " coordinates not allowed");
553 " coordinates not allowed");
563 " coordinates not allowed");
578 " to 1D not allowed.");
592 const sph_t *s = src;
593 for(
size_t i = 0; i < nmemb; ++i)
599 for(
size_t i = 0; i < nmemb; ++i)
604 const pol_t *s = src;
605 for(
size_t i = 0; i < nmemb; ++i)
611 for(
size_t i = 0; i < nmemb; ++i)
616 const double *s = src;
617 for(
size_t i = 0; i < nmemb; ++i)
630 const sph_t *s = src;
631 for(
size_t i = 0; i < nmemb; ++i)
637 for(
size_t i = 0; i < nmemb; ++i)
642 const pol_t *s = src;
643 for(
size_t i = 0; i < nmemb; ++i)
649 for(
size_t i = 0; i < nmemb; ++i)
654 const double *s = src;
655 for(
size_t i = 0; i < nmemb; ++i)
669 QPMS_PR_ERROR(
"Implicit conversion from 3D to 2D coordinates not allowed");
672 const pol_t *s = src;
673 for(
size_t i = 0; i < nmemb; ++i)
679 for(
size_t i = 0; i < nmemb; ++i)
684 QPMS_PR_ERROR(
"Implicit conversion from 3D to 1D coordinates not allowed");
696 QPMS_PR_ERROR(
"Implicit conversion from 3D to 2D coordinates not allowed");
699 const pol_t *s = src;
700 for(
size_t i = 0; i < nmemb; ++i)
706 for(
size_t i = 0; i < nmemb; ++i)
711 QPMS_PR_ERROR(
"Implicit conversion from 3D to 1D coordinates not allowed");
719 double *d = (
double *) dest;
723 QPMS_PR_ERROR(
"Implicit conversion from 3D to 2D coordinates not allowed");
727 QPMS_PR_ERROR(
"Implicit conversion from 3D to 1D coordinates not allowed");
730 const double *s = src;
731 for(
size_t i = 0; i < nmemb; ++i)
755 for(
size_t i = 0; i < nmemb; ++i)
759 for(
size_t i = 0; i < nmemb; ++i)
763 for(
size_t i = 0; i < nmemb; ++i)
767 for(
size_t i = 0; i < nmemb; ++i)
771 for(
size_t i = 0; i < nmemb; ++i)
783 for(
size_t i = 0; i < nmemb; ++i)
787 for(
size_t i = 0; i < nmemb; ++i)
791 for(
size_t i = 0; i < nmemb; ++i)
795 for(
size_t i = 0; i < nmemb; ++i)
799 for(
size_t i = 0; i < nmemb; ++i)
812 QPMS_PR_ERROR(
"Implicit conversion from 3D to 2D coordinates not allowed");
815 for(
size_t i = 0; i < nmemb; ++i)
819 for(
size_t i = 0; i < nmemb; ++i)
823 QPMS_PR_ERROR(
"Implicit conversion from 3D to 1D coordinates not allowed");
835 QPMS_PR_ERROR(
"Implicit conversion from 3D to 2D coordinates not allowed");
838 for(
size_t i = 0; i < nmemb; ++i)
842 for(
size_t i = 0; i < nmemb; ++i)
846 QPMS_PR_ERROR(
"Implicit conversion from 3D to 1D coordinates not allowed");
854 double *d = (
double *) dest;
858 QPMS_PR_ERROR(
"Implicit conversion from 3D to 2D coordinates not allowed");
862 QPMS_PR_ERROR(
"Implicit conversion from 3D to 1D coordinates not allowed");
865 for(
size_t i = 0; i < nmemb; ++i)
879 a[0] = b.x; a[1] = b.y; a[2] = b.z;
884 cart3_t b = {.x = a[0], .y = a[1], .z = a[1]};
890 a[0] = b.x; a[1] = b.y;
895 cart2_t b = {.x = a[0], .y = a[1]};
900 typedef double matrix3d[3][3];
901 typedef double matrix2d[2][2];
902 typedef complex
double cmatrix3d[3][3];
903 typedef complex
double cmatrix2d[2][2];
QPMS miscellanous internal error handling functions and macros.
#define QPMS_PR_ERROR(msg,...)
Prints a given error message and aborts the program.
Definition: qpms_error.h:197
#define QPMS_WTF
Prints an "unexpected error" message and aborts the program.
Definition: qpms_error.h:181
qpms_coord_system_t
Enum codes for common coordinate systems.
Definition: qpms_types.h:238
@ QPMS_COORDS_CART2
2D cartesian.
Definition: qpms_types.h:244
@ QPMS_COORDS_CART3
Definition: qpms_types.h:245
@ QPMS_COORDS_BITRANGE
Convenience bitmask (not a valid flag!).
Definition: qpms_types.h:247
@ QPMS_COORDS_CART1
1D cartesian (= double).
Definition: qpms_types.h:241
@ QPMS_COORDS_SPH
3D spherical.
Definition: qpms_types.h:243
@ QPMS_COORDS_POL
2D polar.
Definition: qpms_types.h:242
2D cartesian coordinates. See also vectors.h.
Definition: qpms_types.h:202
3D cartesian coordinates. See also vectors.h.
Definition: qpms_types.h:186
3D complex (actually 6D) coordinates. See also vectors.h.
Definition: qpms_types.h:191
Spherical coordinates with complex radial component. See also vectors.h.
Definition: qpms_types.h:212
3D complex vector components in local spherical basis. See also vectors.h.
Definition: qpms_types.h:218
2D polar coordinates. See also vectors.h.
Definition: qpms_types.h:223
Spherical coordinates. See also vectors.h.
Definition: qpms_types.h:207
Union type capable to contain various 1D, 2D and 3D coordinates.
Definition: qpms_types.h:229
double z
1D cartesian coordinate.
Definition: qpms_types.h:230
static double cart3_dot(const cart3_t a, const cart3_t b)
3D vector dot product.
Definition: vectors.h:103
static double csphvec_norm(const csphvec_t a)
Euclidian norm of a vector in geographic coordinates.
Definition: vectors.h:415
static cart3_t cart3_add(const cart3_t a, const cart3_t b)
3D vector addition.
Definition: vectors.h:173
static ccart3_t ccart3_substract(const ccart3_t a, const ccart3_t b)
Complex 3D vector substraction.
Definition: vectors.h:218
static csphvec_t csphvec_substract(const csphvec_t a, const csphvec_t b)
Complex 3D vector (geographic coordinates) substraction.
Definition: vectors.h:241
static ccart3_t csph2ccart(const csph_t sph)
Coordinate transform of csph_t to ccart3_t.
Definition: vectors.h:368
static sph_t sph_scale(double c, const sph_t s)
Spherical coordinate system scaling.
Definition: vectors.h:259
static cart2_t cart2_substract(const cart2_t a, const cart2_t b)
2D vector substraction.
Definition: vectors.h:25
static pol_t anycoord2pol(anycoord_point_t p, qpms_coord_system_t t)
Conversion from anycoord_point_t to explicitly polar coordinates.
Definition: vectors.h:523
static cart2_t cart2_from_double_array(const double a[])
Converts array of doubles to cart2_t.
Definition: vectors.h:894
static csph_t sph2csph(sph_t s)
Convert sph_t to csph_t.
Definition: vectors.h:329
static cart3_t cart3_from_double_array(const double a[])
Converts array of doubles to cart3_t.
Definition: vectors.h:883
static pol_t cart2pol(const cart2_t cart)
2D cartesian to polar coordinates conversion. See Coordinate systems and default conversions.
Definition: vectors.h:51
static void qpms_array_coord_transform(void *dest, qpms_coord_system_t tdest, const void *src, qpms_coord_system_t tsrc, size_t nmemb)
Coordinate conversion of point arrays (something to something).
Definition: vectors.h:584
static sph_t cart22sph(const cart2_t cart)
2D cartesian to spherical coordinates conversion. See Coordinate systems and default conversions.
Definition: vectors.h:68
static csph_t cart2csph(const cart3_t cart)
Real 3D cartesian to spherical (complex r) coordinates conversion. See Coordinate systems and default...
Definition: vectors.h:359
static cart3_t cart3_vectorprod(const cart3_t a, const cart3_t b)
3D vector product a.k.a. cross product.
Definition: vectors.h:109
static csphvec_t csphvec_add(const csphvec_t a, const csphvec_t b)
Complex 3D vector (geographic coordinates) addition.
Definition: vectors.h:235
static sph_t cart2sph(const cart3_t cart)
3D cartesian to spherical coordinates conversion. See Coordinate systems and default conversions.
Definition: vectors.h:134
static cart3_t cart22cart3xy(const cart2_t a)
2D to 3D cartesian coordinates conversion. See Coordinate systems and default conversions.
Definition: vectors.h:89
static sph_t csph2sph(csph_t s)
Convert csph_t to sph_t, discarding the imaginary part of radial component.
Definition: vectors.h:335
static double cart2norm(const cart2_t v)
2D vector euclidian norm.
Definition: vectors.h:46
static csphvec_t ccart2csphvec(const ccart3_t cartvec, const sph_t at)
Coordinate transform of a vector in global cartesian to local geographic system.
Definition: vectors.h:306
static cart3_t anycoord2cart3(anycoord_point_t p, qpms_coord_system_t t)
Conversion from anycoord_point_t to explicitly 3D cartesian coordinates.
Definition: vectors.h:484
static void ccart3_kahaninit(ccart3_t *sum, ccart3_t *compensation)
Kanan sum initialisation for ccart3_t.
Definition: vectors.h:390
static cart3_t pol2cart3_equator(const pol_t pol)
Polar to 3D cartesian coordinates conversion. See Coordinate systems and default conversions.
Definition: vectors.h:165
static cart3_t cart3_scale(const double c, const cart3_t v)
3D vector scaling
Definition: vectors.h:185
static ccart3_t csphvec2ccart(const csphvec_t sphvec, const sph_t at)
Coordinate transform of a vector in local geographic to global cartesian system.
Definition: vectors.h:272
static cart3_t cart3_substract(const cart3_t a, const cart3_t b)
3D vector substraction.
Definition: vectors.h:179
static double cart3_tripleprod(const cart3_t a, const cart3_t b, const cart3_t c)
Scalar triple product .
Definition: vectors.h:119
static void anycoord_arr2something(void *dest, qpms_coord_system_t tdest, const anycoord_point_t *src, qpms_coord_system_t tsrc, size_t nmemb)
Coordinate conversion of point arrays (anycoord_point_t to something).
Definition: vectors.h:746
static sph_t pol2sph_equator(const pol_t pol)
Polar to spherical coordinates conversion. See Coordinate systems and default conversions.
Definition: vectors.h:59
static ccart3_t csphvec2ccart_csph(const csphvec_t sphvec, const csph_t at)
Coordinate transform of a vector in local geographic to global cartesian system.
Definition: vectors.h:300
static void csphvec_kahanadd(csphvec_t *sum, csphvec_t *compensation, const csphvec_t input)
Add element to Kahan sum (csphvec_t).
Definition: vectors.h:407
static cart2_t cart2_scale(const double c, const cart2_t v)
2D vector scaling.
Definition: vectors.h:31
static void cart3_to_double_array(double a[], cart3_t b)
Converts cart3_t to array of doubles.
Definition: vectors.h:878
static double cart2_dot(const cart2_t a, const cart2_t b)
2D vector dot product.
Definition: vectors.h:37
static ccart3_t cart32ccart3(cart3_t c)
Convert cart3_t to ccart3_t.
Definition: vectors.h:229
static ccart3_t ccart3_scale(const complex double c, const ccart3_t v)
Complex 3D vector scaling.
Definition: vectors.h:206
static sph_t cart12sph_zaxis(double z)
1D cartesian to spherical coordinates conversion. See Coordinate systems and default conversions.
Definition: vectors.h:77
static double cart3norm(const cart3_t v)
3D vector euclidian norm.
Definition: vectors.h:129
static complex double csphvec_dotnc(const csphvec_t a, const csphvec_t b)
Complex 3D vector (geographic coordinates) "dot product" without conjugation.
Definition: vectors.h:253
static void csphvec_kahaninit(csphvec_t *sum, csphvec_t *compensation)
Kanan sum initialisation for csphvec_t.
Definition: vectors.h:394
static csph_t sph_cscale(complex double c, const sph_t s)
"Complex spherical" coordinate system scaling.
Definition: vectors.h:265
static double cart3_normsq(const cart3_t a)
3D vector euclidian norm squared.
Definition: vectors.h:124
static csph_t ccart2csph(const ccart3_t cart)
Lossy coordinate transform of ccart3_t to csph_t.
Definition: vectors.h:350
static sph_t anycoord2sph(anycoord_point_t p, qpms_coord_system_t t)
Conversion from anycoord_point_t to explicitly spherical coordinates.
Definition: vectors.h:459
static double anycoord_norm(anycoord_point_t p, qpms_coord_system_t t)
Cartesian norm of anycoord_point_t.
Definition: vectors.h:507
static ccart3_t ccart3_add(const ccart3_t a, const ccart3_t b)
Complex 3D vector adition.
Definition: vectors.h:212
static void cart2_to_double_array(double a[], cart2_t b)
Converts cart2_t to array of doubles.
Definition: vectors.h:889
static complex double ccart3_dotnc(const ccart3_t a, const ccart3_t b)
Complex 3D cartesian vector "dot product" without conjugation.
Definition: vectors.h:224
static cart3_t cart12cart3z(double z)
1D to 3D cartesian coordinates conversion. See Coordinate systems and default conversions.
Definition: vectors.h:83
static double cart3_dist(const cart3_t a, const cart3_t b)
Euclidian distance between two 3D points.
Definition: vectors.h:197
static cart3_t sph2cart(const sph_t sph)
Spherical to 3D cartesian coordinates conversion. See Coordinate systems and default conversions.
Definition: vectors.h:143
static void ccart3_kahanadd(ccart3_t *sum, ccart3_t *compensation, const ccart3_t input)
Add element to Kahan sum (ccart3_t).
Definition: vectors.h:399
static double anycoord2cart1(anycoord_point_t p, qpms_coord_system_t t)
Conversion from anycoord_point_t to explicitly 1D cartesian coordinates.
Definition: vectors.h:573
static cart3_t cart3_divscale(const cart3_t v, const double c)
3D vector division by scalar (N.B. argument order).
Definition: vectors.h:191
static cart2_t cart2_add(const cart2_t a, const cart2_t b)
2D vector addition.
Definition: vectors.h:19
static cart2_t pol2cart(const pol_t pol)
Polar to 2D cartesian coordinates conversion. See Coordinate systems and default conversions.
Definition: vectors.h:157
static cart2_t anycoord2cart2(anycoord_point_t p, qpms_coord_system_t t)
Conversion from anycoord_point_t to explicitly 2D cartesian coordinates.
Definition: vectors.h:548
static csphvec_t csphvec_scale(complex double c, const csphvec_t v)
Complex 3D vector (geographic coordinates) scaling.
Definition: vectors.h:247