liba 0.1.15
An algorithm library based on C/C++
Loading...
Searching...
No Matches
point3.h
Go to the documentation of this file.
1
5
6#ifndef LIBA_POINT3_H
7#define LIBA_POINT3_H
8
9#include "a.h"
10#include "vector3.h"
11
17
18/* clang-format off */
20#define A_POINT3_C(x, y, z) {a_real_c(x), a_real_c(y), a_real_c(z)}
21/* clang-format on */
22
24#define a_point3_c(x) a_cast_s(a_point3, x)
25#define a_point3_(_, x) a_cast_s(a_point3 _, x)
26
27typedef struct a_point3 a_point3;
28
29#if defined(__cplusplus)
30namespace a
31{
32typedef struct a_point3 point3;
33} /* namespace a */
34extern "C" {
35#endif /* __cplusplus */
36#if !defined A_HAVE_INLINE || defined(LIBA_POINT3_C)
38#undef A_INTERN
39#define A_INTERN A_PUBLIC extern
41#endif /* A_HAVE_INLINE */
42
46A_INTERN void a_vector3_set(a_vector3 *ctx, a_point3 const *p, a_point3 const *q);
47
51A_INTERN void a_point3_val(a_point3 const *ctx, a_real *x, a_real *y, a_real *z);
55A_INTERN void a_point3_set_val(a_point3 *ctx, a_real x, a_real y, a_real z);
59A_INTERN void a_point3_cyl(a_point3 const *ctx, a_real *rho, a_real *theta, a_real *z);
63A_INTERN void a_point3_set_cyl(a_point3 *ctx, a_real rho, a_real theta, a_real z);
67A_INTERN void a_point3_sph(a_point3 const *ctx, a_real *rho, a_real *theta, a_real *alpha);
71A_INTERN void a_point3_set_sph(a_point3 *ctx, a_real rho, a_real theta, a_real alpha);
72
76A_INTERN void a_point3_add(a_point3 const *lhs, a_vector3 const *rhs, a_point3 *res);
80A_INTERN void a_point3_sub(a_point3 const *lhs, a_vector3 const *rhs, a_point3 *res);
84A_INTERN void a_point3_mul(a_point3 const *lhs, a_real rhs, a_point3 *res);
88A_INTERN void a_point3_div(a_point3 const *lhs, a_real rhs, a_point3 *res);
92A_INTERN void a_point3_pos(a_point3 const *ctx, a_vector3 *res);
96A_INTERN void a_point3_neg(a_point3 const *ctx, a_vector3 *res);
97
108A_EXTERN a_real a_point3_norm(a_point3 const *ctx);
119A_INTERN a_real a_point3_norm2(a_point3 const *ctx);
133A_EXTERN a_real a_point3_dist(a_point3 const *lhs, a_point3 const *rhs);
147A_INTERN a_real a_point3_dist2(a_point3 const *lhs, a_point3 const *rhs);
148
159A_EXTERN a_real a_point3_mindist(a_point3 const *ctx, a_point3 const *i_p, a_size i_n,
160 a_point3 *o_p /*=A_NULL*/, a_size *o_i /*=A_NULL*/);
161
172A_EXTERN a_real a_point3_maxdist(a_point3 const *ctx, a_point3 const *i_p, a_size i_n,
173 a_point3 *o_p /*=A_NULL*/, a_size *o_i /*=A_NULL*/);
174
187A_INTERN void a_point3_lerp(a_point3 const *lhs, a_point3 const *rhs, a_real val, a_point3 *res);
188
199A_EXTERN a_real a_point3_tricir(a_point3 const *p1, a_point3 const *p2, a_point3 const *p3, a_point3 *pc);
241A_EXTERN a_real a_point3_tricir2(a_point3 const *p1, a_point3 const *p2, a_point3 const *p3, a_point3 *pc);
242
254A_EXTERN a_real a_point3_tetsph(a_point3 const *p1, a_point3 const *p2,
255 a_point3 const *p3, a_point3 const *p4,
256 a_point3 *pc);
306A_EXTERN a_real a_point3_tetsph2(a_point3 const *p1, a_point3 const *p2,
307 a_point3 const *p3, a_point3 const *p4,
308 a_point3 *pc);
309
319A_EXTERN int a_point3_cmpxy(a_point3 const *lhs, a_point3 const *rhs);
320
330A_EXTERN int a_point3_cmpyx(a_point3 const *lhs, a_point3 const *rhs);
331
332#if !defined A_HAVE_INLINE || defined(LIBA_POINT3_C)
334#undef A_INTERN
335#define A_INTERN static A_INLINE
337#endif /* A_HAVE_INLINE */
338#if defined(__cplusplus)
339} /* extern "C" */
340A_INLINE void a_vector3::set(a_point3 const &p, a_point3 const &q)
341{
342 a_vector3_set(this, &p, &q);
343}
344#endif /* __cplusplus */
345
350{
351 a_real x, y, z;
352#if defined(__cplusplus)
354 A_INLINE void val(a_real &x_, a_real &y_, a_real &z_) const
355 {
356 a_point3_val(this, &x_, &y_, &z_);
357 }
358
359 A_INLINE void set_val(a_real x_, a_real y_, a_real z_)
360 {
361 a_point3_set_val(this, x_, y_, z_);
362 }
363
364 A_INLINE void cyl(a_real &rho, a_real &theta, a_real &z_) const
365 {
366 a_point3_cyl(this, &rho, &theta, &z_);
367 }
368
369 A_INLINE void set_cyl(a_real rho, a_real theta, a_real z_)
370 {
371 a_point3_set_cyl(this, rho, theta, z_);
372 }
373
374 A_INLINE void sph(a_real &rho, a_real &theta, a_real &alpha) const
375 {
376 a_point3_sph(this, &rho, &theta, &alpha);
377 }
378
379 A_INLINE void set_sph(a_real rho, a_real theta, a_real alpha)
380 {
381 a_point3_set_sph(this, rho, theta, alpha);
382 }
383
384 A_INLINE void add(a_vector3 const &rhs, a_point3 &res) const
385 {
386 a_point3_add(this, &rhs, &res);
387 }
388
389 A_INLINE void sub(a_vector3 const &rhs, a_point3 &res) const
390 {
391 a_point3_sub(this, &rhs, &res);
392 }
393
394 A_INLINE void mul(a_real rhs, a_point3 &res) const
395 {
396 a_point3_mul(this, rhs, &res);
397 }
398
399 A_INLINE void div(a_real rhs, a_point3 &res) const
400 {
401 a_point3_div(this, rhs, &res);
402 }
403
404 A_INLINE void pos(a_vector3 &res) const
405 {
406 a_point3_pos(this, &res);
407 }
408
409 A_INLINE void neg(a_vector3 &res) const
410 {
411 a_point3_neg(this, &res);
412 }
413
414 A_INLINE a_real norm() const { return a_point3_norm(this); }
416 A_INLINE a_real norm2() const { return a_point3_norm2(this); }
418 A_INLINE a_real dist(a_point3 const &rhs) const
419 {
420 return a_point3_dist(this, &rhs);
421 }
422
423 A_INLINE a_real dist2(a_point3 const &rhs) const
424 {
425 return a_point3_dist2(this, &rhs);
426 }
427
428 A_INLINE a_real mindist(a_point3 const *i_p, a_size i_n, a_point3 *o_p = A_NULL, a_size *o_i = A_NULL) const
429 {
430 return a_point3_mindist(this, i_p, i_n, o_p, o_i);
431 }
432
433 A_INLINE a_real maxdist(a_point3 const *i_p, a_size i_n, a_point3 *o_p = A_NULL, a_size *o_i = A_NULL) const
434 {
435 return a_point3_maxdist(this, i_p, i_n, o_p, o_i);
436 }
437
438 A_INLINE void lerp(a_point3 const &rhs, a_real val, a_point3 &res) const
439 {
440 a_point3_lerp(this, &rhs, val, &res);
441 }
442
443 A_INLINE a_real tricir(a_point3 const &p2, a_point3 const &p3, a_point3 &pc) const
444 {
445 return a_point3_tricir(this, &p2, &p3, &pc);
446 }
447
448 A_INLINE a_real tricir2(a_point3 const &p2, a_point3 const &p3, a_point3 &pc) const
449 {
450 return a_point3_tricir2(this, &p2, &p3, &pc);
451 }
452
453 A_INLINE a_real tetsph(a_point3 const &p2, a_point3 const &p3, a_point3 const &p4, a_point3 &pc) const
454 {
455 return a_point3_tetsph(this, &p2, &p3, &p4, &pc);
456 }
457
458 A_INLINE a_real tetsph2(a_point3 const &p2, a_point3 const &p3, a_point3 const &p4, a_point3 &pc) const
459 {
460 return a_point3_tetsph2(this, &p2, &p3, &p4, &pc);
461 }
462
463 friend A_INLINE void operator+=(a_point3 &lhs, a_vector3 const &rhs) { a_point3_add(&lhs, &rhs, &lhs); }
465 friend A_INLINE a_point3 operator+(a_point3 const &lhs, a_vector3 const &rhs)
466 {
467 a_point3 res;
468 a_point3_add(&lhs, &rhs, &res);
469 return res;
470 }
471
472 friend A_INLINE void operator-=(a_point3 &lhs, a_vector3 const &rhs) { a_point3_sub(&lhs, &rhs, &lhs); }
474 friend A_INLINE a_point3 operator-(a_point3 const &lhs, a_vector3 const &rhs)
475 {
476 a_point3 res;
477 a_point3_sub(&lhs, &rhs, &res);
478 return res;
479 }
480
481 friend A_INLINE a_vector3 operator-(a_point3 const &lhs, a_point3 const &rhs)
482 {
483 a_vector3 res;
484 a_vector3_set(&res, &rhs, &lhs);
485 return res;
486 }
487
488 friend A_INLINE void operator*=(a_point3 &lhs, a_real rhs) { a_point3_mul(&lhs, rhs, &lhs); }
490 friend A_INLINE a_point3 operator*(a_real lhs, a_point3 const &rhs)
491 {
492 a_point3 res;
493 a_point3_mul(&rhs, lhs, &res);
494 return res;
495 }
496
497 friend A_INLINE a_point3 operator*(a_point3 const &lhs, a_real rhs)
498 {
499 a_point3 res;
500 a_point3_mul(&lhs, rhs, &res);
501 return res;
502 }
503
504 friend A_INLINE void operator/=(a_point3 &lhs, a_real rhs) { a_point3_div(&lhs, rhs, &lhs); }
506 friend A_INLINE a_point3 operator/(a_point3 const &lhs, a_real rhs)
507 {
508 a_point3 res;
509 a_point3_div(&lhs, rhs, &res);
510 return res;
511 }
512
513 friend A_INLINE a_vector3 operator+(a_point3 const &rhs)
514 {
515 a_vector3 res;
516 a_point3_pos(&rhs, &res);
517 return res;
518 }
519
520 friend A_INLINE a_vector3 operator-(a_point3 const &rhs)
521 {
522 a_vector3 res;
523 a_point3_neg(&rhs, &res);
524 return res;
525 }
526#endif /* __cplusplus */
527};
528
529#if defined(LIBA_POINT3_C)
531#undef A_INTERN
532#define A_INTERN A_INLINE
534#endif /* LIBA_POINT3_C */
535#if defined(A_HAVE_INLINE) || defined(LIBA_POINT3_C)
536
537A_INTERN void a_vector3_set(a_vector3 *ctx, a_point3 const *p, a_point3 const *q)
538{
539 ctx->x = q->x - p->x;
540 ctx->y = q->y - p->y;
541 ctx->z = q->z - p->z;
542}
543A_INTERN void a_point3_val(a_point3 const *ctx, a_real *x, a_real *y, a_real *z)
544{
545 *x = ctx->x;
546 *y = ctx->y;
547 *z = ctx->z;
548}
549A_INTERN void a_point3_set_val(a_point3 *ctx, a_real x, a_real y, a_real z)
550{
551 ctx->x = x;
552 ctx->y = y;
553 ctx->z = z;
554}
555A_INTERN void a_point3_cyl(a_point3 const *ctx, a_real *rho, a_real *theta, a_real *z)
556{
557 a_real_cart2pol(ctx->x, ctx->y, rho, theta);
558 *z = ctx->z;
559}
560A_INTERN void a_point3_set_cyl(a_point3 *ctx, a_real rho, a_real theta, a_real z)
561{
562 a_real_pol2cart(rho, theta, &ctx->x, &ctx->y);
563 ctx->z = z;
564}
565A_INTERN void a_point3_sph(a_point3 const *ctx, a_real *rho, a_real *theta, a_real *alpha)
566{
567 a_real_cart2sph(ctx->x, ctx->y, ctx->z, rho, theta, alpha);
568}
569A_INTERN void a_point3_set_sph(a_point3 *ctx, a_real rho, a_real theta, a_real alpha)
570{
571 a_real_sph2cart(rho, theta, alpha, &ctx->x, &ctx->y, &ctx->z);
572}
573A_INTERN void a_point3_add(a_point3 const *lhs, a_vector3 const *rhs, a_point3 *res)
574{
575 res->x = lhs->x + rhs->x;
576 res->y = lhs->y + rhs->y;
577 res->z = lhs->z + rhs->z;
578}
579A_INTERN void a_point3_sub(a_point3 const *lhs, a_vector3 const *rhs, a_point3 *res)
580{
581 res->x = lhs->x - rhs->x;
582 res->y = lhs->y - rhs->y;
583 res->z = lhs->z - rhs->z;
584}
585A_INTERN void a_point3_mul(a_point3 const *lhs, a_real rhs, a_point3 *res)
586{
587 res->x = lhs->x * rhs;
588 res->y = lhs->y * rhs;
589 res->z = lhs->z * rhs;
590}
591A_INTERN void a_point3_div(a_point3 const *lhs, a_real rhs, a_point3 *res)
592{
593 res->x = lhs->x / rhs;
594 res->y = lhs->y / rhs;
595 res->z = lhs->z / rhs;
596}
597A_INTERN void a_point3_pos(a_point3 const *ctx, a_vector3 *res)
598{
599 res->x = +ctx->x;
600 res->y = +ctx->y;
601 res->z = +ctx->z;
602}
603A_INTERN void a_point3_neg(a_point3 const *ctx, a_vector3 *res)
604{
605 res->x = -ctx->x;
606 res->y = -ctx->y;
607 res->z = -ctx->z;
608}
609
610A_INTERN a_real a_point3_norm2(a_point3 const *ctx)
611{
612 return ctx->x * ctx->x + ctx->y * ctx->y + ctx->z * ctx->z;
613}
614
615A_INTERN a_real a_point3_dist2(a_point3 const *lhs, a_point3 const *rhs)
616{
617 a_real const x = rhs->x - lhs->x;
618 a_real const y = rhs->y - lhs->y;
619 a_real const z = rhs->z - lhs->z;
620 return x * x + y * y + z * z;
621}
622
623A_INTERN void a_point3_lerp(a_point3 const *lhs, a_point3 const *rhs, a_real val, a_point3 *res)
624{
625 res->x = lhs->x + (rhs->x - lhs->x) * val;
626 res->y = lhs->y + (rhs->y - lhs->y) * val;
627 res->z = lhs->z + (rhs->z - lhs->z) * val;
628}
629
630#endif /* A_HAVE_INLINE */
631#if defined(LIBA_POINT3_C)
633#undef A_INTERN
634#define A_INTERN static A_INLINE
636#endif /* LIBA_POINT3_C */
637
639
640#endif /* a/point3.h */
algorithm library
a_real a_point3_tetsph(a_point3 const *p1, a_point3 const *p2, a_point3 const *p3, a_point3 const *p4, a_point3 *pc)
compute the circumcenter and circumradius of a tetrahedron defined by four 3D points.
void a_point3_val(a_point3 const *ctx, a_real *x, a_real *y, a_real *z)
get the cartesian coordinates of a 3D point.
a_real a_point3_tetsph2(a_point3 const *p1, a_point3 const *p2, a_point3 const *p3, a_point3 const *p4, a_point3 *pc)
compute the circumcenter and squared circumradius of a tetrahedron defined by four 3D points.
void a_point3_pos(a_point3 const *ctx, a_vector3 *res)
convert it into a vector from the origin to the point.
a_real a_point3_dist(a_point3 const *lhs, a_point3 const *rhs)
compute the distance between two 3D points.
void a_point3_lerp(a_point3 const *lhs, a_point3 const *rhs, a_real val, a_point3 *res)
compute linear interpolation (LERP) between two 3D points.
void set(a_point3 const &p, a_point3 const &q)
set a 3D vector as the difference from point p to point q.
Definition point3.h:340
a_real a_point3_norm2(a_point3 const *ctx)
compute the squared magnitude of a 3D point.
void a_point3_set_cyl(a_point3 *ctx, a_real rho, a_real theta, a_real z)
set the cylindrical coordinates of a 3D point.
int a_point3_cmpyx(a_point3 const *lhs, a_point3 const *rhs)
compare two 3D points primarily by Y, then by X, and finally by Z.
void a_vector3_set(a_vector3 *ctx, a_point3 const *p, a_point3 const *q)
set a 3D vector as the difference from point p to point q.
void a_point3_add(a_point3 const *lhs, a_vector3 const *rhs, a_point3 *res)
add a 3D vector to a 3D point.
a_real a_point3_mindist(a_point3 const *ctx, a_point3 const *i_p, a_size i_n, a_point3 *o_p, a_size *o_i)
compute the minimum distance from a reference point to a point set.
a_real a_point3_dist2(a_point3 const *lhs, a_point3 const *rhs)
compute the squared distance between two 3D points.
int a_point3_cmpxy(a_point3 const *lhs, a_point3 const *rhs)
compare two 3D points primarily by X, then by Y, and finally by Z.
a_real a_point3_tricir(a_point3 const *p1, a_point3 const *p2, a_point3 const *p3, a_point3 *pc)
compute the circumcenter and circumradius of a triangle defined by three 3D points.
a_real a_point3_maxdist(a_point3 const *ctx, a_point3 const *i_p, a_size i_n, a_point3 *o_p, a_size *o_i)
compute the maximum distance from a reference point to a point set.
void a_point3_set_sph(a_point3 *ctx, a_real rho, a_real theta, a_real alpha)
set the spherical coordinates of a 3D point.
a_real a_point3_norm(a_point3 const *ctx)
compute the magnitude of a 3D point.
void a_point3_set_val(a_point3 *ctx, a_real x, a_real y, a_real z)
set the cartesian coordinates of a 3D point.
a_real a_point3_tricir2(a_point3 const *p1, a_point3 const *p2, a_point3 const *p3, a_point3 *pc)
compute the circumcenter and squared circumradius of a triangle defined by three 3D points.
void a_point3_sph(a_point3 const *ctx, a_real *rho, a_real *theta, a_real *alpha)
get the spherical coordinates of a 3D point.
void a_point3_mul(a_point3 const *lhs, a_real rhs, a_point3 *res)
multiplie a 3D point by a scalar.
void a_point3_cyl(a_point3 const *ctx, a_real *rho, a_real *theta, a_real *z)
get the cylindrical coordinates of a 3D point.
void a_point3_div(a_point3 const *lhs, a_real rhs, a_point3 *res)
divide a 3D point by a scalar.
void a_point3_neg(a_point3 const *ctx, a_vector3 *res)
convert it into a vector from the point to the origin.
void a_point3_sub(a_point3 const *lhs, a_vector3 const *rhs, a_point3 *res)
subtract a 3D vector from a 3D point.
double a_real
compiler built-in floating-point number type
Definition a.h:1012
size_t a_size
unsigned integer type returned by the sizeof operator
Definition a.h:823
instance structure for three-dimensional point
Definition point3.h:350
a_real mindist(a_point3 const *i_p, a_size i_n, a_point3 *o_p=NULL, a_size *o_i=NULL) const
compute the minimum distance from a reference point to a point set.
Definition point3.h:428
a_real tetsph(a_point3 const &p2, a_point3 const &p3, a_point3 const &p4, a_point3 &pc) const
compute the circumcenter and circumradius of a tetrahedron defined by four 3D points.
Definition point3.h:453
a_real maxdist(a_point3 const *i_p, a_size i_n, a_point3 *o_p=NULL, a_size *o_i=NULL) const
compute the maximum distance from a reference point to a point set.
Definition point3.h:433
friend void operator/=(a_point3 &lhs, a_real rhs)
divide a 3D point by a scalar.
Definition point3.h:504
a_real dist2(a_point3 const &rhs) const
compute the squared distance between two 3D points.
Definition point3.h:423
friend a_point3 operator*(a_point3 const &lhs, a_real rhs)
multiplie a 3D point by a scalar.
Definition point3.h:497
friend a_point3 operator/(a_point3 const &lhs, a_real rhs)
divide a 3D point by a scalar.
Definition point3.h:506
void lerp(a_point3 const &rhs, a_real val, a_point3 &res) const
compute linear interpolation (LERP) between two 3D points.
Definition point3.h:438
a_real tricir2(a_point3 const &p2, a_point3 const &p3, a_point3 &pc) const
compute the circumcenter and squared circumradius of a triangle defined by three 3D points.
Definition point3.h:448
a_real norm2() const
compute the squared magnitude of a 3D point.
Definition point3.h:416
friend void operator+=(a_point3 &lhs, a_vector3 const &rhs)
add a 3D vector to a 3D point.
Definition point3.h:463
void sph(a_real &rho, a_real &theta, a_real &alpha) const
get the spherical coordinates of a 3D point.
Definition point3.h:374
friend a_vector3 operator+(a_point3 const &rhs)
convert it into a vector from the origin to the point.
Definition point3.h:513
friend a_point3 operator-(a_point3 const &lhs, a_vector3 const &rhs)
subtract a 3D vector from a 3D point.
Definition point3.h:474
void cyl(a_real &rho, a_real &theta, a_real &z_) const
get the cylindrical coordinates of a 3D point.
Definition point3.h:364
void neg(a_vector3 &res) const
convert it into a vector from the point to the origin.
Definition point3.h:409
void val(a_real &x_, a_real &y_, a_real &z_) const
get the cartesian coordinates of a 3D point.
Definition point3.h:354
void sub(a_vector3 const &rhs, a_point3 &res) const
subtract a 3D vector from a 3D point.
Definition point3.h:389
friend void operator*=(a_point3 &lhs, a_real rhs)
multiplie a 3D point by a scalar.
Definition point3.h:488
void set_val(a_real x_, a_real y_, a_real z_)
set the cartesian coordinates of a 3D point.
Definition point3.h:359
a_real norm() const
compute the magnitude of a 3D point.
Definition point3.h:414
void add(a_vector3 const &rhs, a_point3 &res) const
add a 3D vector to a 3D point.
Definition point3.h:384
void set_sph(a_real rho, a_real theta, a_real alpha)
set the spherical coordinates of a 3D point.
Definition point3.h:379
void pos(a_vector3 &res) const
convert it into a vector from the origin to the point.
Definition point3.h:404
void div(a_real rhs, a_point3 &res) const
divide a 3D point by a scalar.
Definition point3.h:399
void mul(a_real rhs, a_point3 &res) const
multiplie a 3D point by a scalar.
Definition point3.h:394
a_real tricir(a_point3 const &p2, a_point3 const &p3, a_point3 &pc) const
compute the circumcenter and circumradius of a triangle defined by three 3D points.
Definition point3.h:443
friend a_point3 operator+(a_point3 const &lhs, a_vector3 const &rhs)
add a 3D vector to a 3D point.
Definition point3.h:465
void set_cyl(a_real rho, a_real theta, a_real z_)
set the cylindrical coordinates of a 3D point.
Definition point3.h:369
a_real tetsph2(a_point3 const &p2, a_point3 const &p3, a_point3 const &p4, a_point3 &pc) const
compute the circumcenter and squared circumradius of a tetrahedron defined by four 3D points.
Definition point3.h:458
a_real dist(a_point3 const &rhs) const
compute the distance between two 3D points.
Definition point3.h:418
friend a_point3 operator*(a_real lhs, a_point3 const &rhs)
multiplie a 3D point by a scalar.
Definition point3.h:490
friend void operator-=(a_point3 &lhs, a_vector3 const &rhs)
subtract a 3D vector from a 3D point.
Definition point3.h:472
friend a_vector3 operator-(a_point3 const &rhs)
convert it into a vector from the point to the origin.
Definition point3.h:520
friend a_vector3 operator-(a_point3 const &lhs, a_point3 const &rhs)
set a 3D vector as the difference from point p to point q.
Definition point3.h:481
instance structure for three-dimensional vector
Definition vector3.h:415
three-dimensional vector