The Tiny HTM Library
Data Structures | Functions
Spherical coordinates and 3-vectors

Data Structures

struct  htm_v3
 Cartesian coordinates for a point in R3. More...
struct  htm_sc
 Spherical coordinates (in degrees) for a point in S2. More...

Functions

HTM_INLINE enum htm_errcode htm_v3_init (struct htm_v3 *out, double x, double y, double z)
 Stores the given cartesian coordinates in out.
HTM_INLINE enum htm_errcode htm_sc_init (struct htm_sc *out, double lon_deg, double lat_deg)
 Stores the given spherical coordinates in out; coordinates are assumed to be in degrees.
HTM_INLINE void htm_v3_add (struct htm_v3 *out, const struct htm_v3 *v1, const struct htm_v3 *v2)
 Stores the vector sum v1 + v2 in out.
HTM_INLINE void htm_v3_sub (struct htm_v3 *out, const struct htm_v3 *v1, const struct htm_v3 *v2)
 Stores the vector difference v1 - v2 in out.
HTM_INLINE void htm_v3_neg (struct htm_v3 *out, const struct htm_v3 *v)
 Stores the vector v * -1 in out.
HTM_INLINE void htm_v3_mul (struct htm_v3 *out, const struct htm_v3 *v, double s)
 Stores the vector-scalar product v * s in out.
HTM_INLINE void htm_v3_div (struct htm_v3 *out, const struct htm_v3 *v, double s)
 Stores the vector-scalar quotient v / s in out.
HTM_INLINE double htm_v3_dot (const struct htm_v3 *v1, const struct htm_v3 *v2)
 Returns the dot product of the 3-vectors v1 and v2.
HTM_INLINE void htm_v3_cwise_mul (struct htm_v3 *out, const struct htm_v3 *v1, const struct htm_v3 *v2)
 Stores the component-wise product of the 3-vectors v1 and v2 in out.
HTM_INLINE double htm_v3_norm2 (const struct htm_v3 *v)
 Returns the squared norm of the 3-vector v, which must not be NULL.
HTM_INLINE double htm_v3_norm (const struct htm_v3 *v)
 Returns the norm of the 3-vector v, which must not be NULL.
HTM_INLINE void htm_v3_normalize (struct htm_v3 *out, const struct htm_v3 *v)
 Stores a normalized copy of v in out.
HTM_INLINE void htm_v3_rcross (struct htm_v3 *out, const struct htm_v3 *v1, const struct htm_v3 *v2)
 Stores twice the vector cross product of v1 and v2 in out.
HTM_INLINE void htm_v3_cross (struct htm_v3 *out, const struct htm_v3 *v1, const struct htm_v3 *v2)
 Stores the vector cross product of v1 and v2 in out.
enum htm_errcode htm_v3_ne (struct htm_v3 *north, struct htm_v3 *east, const struct htm_v3 *v)
 Computes the N,E basis unit vectors at a position v on the unit sphere.
enum htm_errcode htm_v3_tanrot (double *angle, const struct htm_v3 *v1, const struct htm_v3 *v2, double r)
 Computes the angle by which the normal of the plane defined by the origin, v1 and v2 should be rotated about the axis v1 - v2, such that the resulting plane is tangent to two small circles of radius r centered at v1 and v2.
enum htm_errcode htm_v3_rot (struct htm_v3 *out, const struct htm_v3 *v, const struct htm_v3 *k, double angle_deg)
 Rotates vector v around the axis k by the specified number of degrees.
enum htm_errcode htm_v3_centroid (struct htm_v3 *cen, const struct htm_v3 *points, size_t n)
 Computes the normalized sum of the given list of vectors.
enum htm_errcode htm_sc_tov3 (struct htm_v3 *out, const struct htm_sc *p)
 Converts the spherical coordinate pair p to a unit 3-vector and stores the results in out.
enum htm_errcode htm_v3_tosc (struct htm_sc *out, const struct htm_v3 *v)
 Converts the 3-vector v to spherical coordinates and stores the results in out.

Function Documentation

HTM_INLINE enum htm_errcode htm_sc_init ( struct htm_sc out,
double  lon_deg,
double  lat_deg 
)

Stores the given spherical coordinates in out; coordinates are assumed to be in degrees.

Returns:
  • HTM_ENANINF if any input coordinate is non-finite.
  • HTM_ELAT if lat is not in the [-90, 90] degree range.
  • HTM_ENULLPTR if out is NULL.
  • HTM_OK on success.

Definition at line 71 of file geometry.h.

References HTM_ELAT, HTM_ENANINF, HTM_ENULLPTR, HTM_ISSPECIAL, HTM_OK, htm_sc::lat, and htm_sc::lon.

enum htm_errcode htm_sc_tov3 ( struct htm_v3 out,
const struct htm_sc p 
)

Converts the spherical coordinate pair p to a unit 3-vector and stores the results in out.

Returns:
  • HTM_ENULLPTR if out or p is NULL.
  • HTM_OK on success.

Definition at line 136 of file geometry.c.

References HTM_ENULLPTR, HTM_OK, HTM_RAD_PER_DEG, htm_sc::lat, htm_sc::lon, htm_v3::x, htm_v3::y, and htm_v3::z.

HTM_INLINE void htm_v3_add ( struct htm_v3 out,
const struct htm_v3 v1,
const struct htm_v3 v2 
)

Stores the vector sum v1 + v2 in out.

Arguments must not be NULL pointers, but may alias.

Definition at line 90 of file geometry.h.

References htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2cpoly_hull(), htm_s2cpoly_init(), htm_s2cpoly_pad(), htm_s2ellipse_init(), htm_s2ellipse_init2(), htm_tri_init(), and htm_v3_rot().

enum htm_errcode htm_v3_centroid ( struct htm_v3 cen,
const struct htm_v3 points,
size_t  n 
)

Computes the normalized sum of the given list of vectors.

Returns:
  • HTM_ENULLPTR if cen or points is NULL.
  • HTM_ELEN if n == 0.
  • HTM_OK on success.

Definition at line 115 of file geometry.c.

References HTM_ELEN, HTM_ENULLPTR, HTM_OK, htm_v3_normalize(), htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2cpoly_hull(), and htm_v3_convex().

HTM_INLINE void htm_v3_cross ( struct htm_v3 out,
const struct htm_v3 v1,
const struct htm_v3 v2 
)

Stores the vector cross product of v1 and v2 in out.

Arguments must not be NULL pointers, but may alias.

Definition at line 221 of file geometry.h.

References htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_v3_angsep(), and htm_v3_edgedist2().

HTM_INLINE void htm_v3_cwise_mul ( struct htm_v3 out,
const struct htm_v3 v1,
const struct htm_v3 v2 
)

Stores the component-wise product of the 3-vectors v1 and v2 in out.

Arguments must not be NULL pointers, but may alias.

Definition at line 157 of file geometry.h.

References htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2ellipse_init().

HTM_INLINE void htm_v3_div ( struct htm_v3 out,
const struct htm_v3 v,
double  s 
)

Stores the vector-scalar quotient v / s in out.

Arguments must not be NULL pointers, but may alias.

Definition at line 136 of file geometry.h.

References htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2cpoly_hull(), and htm_v3_convex().

HTM_INLINE double htm_v3_dot ( const struct htm_v3 v1,
const struct htm_v3 v2 
)

Returns the dot product of the 3-vectors v1 and v2.

Arguments must not be NULL pointers, but may alias.

Definition at line 148 of file geometry.h.

References htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2cpoly_area(), htm_s2cpoly_cv3(), htm_s2cpoly_hull(), htm_s2cpoly_init(), htm_s2cpoly_pad(), htm_v3_angsep(), htm_v3_convex(), htm_v3_edgedist2(), htm_v3_id(), and htm_v3_rot().

HTM_INLINE enum htm_errcode htm_v3_init ( struct htm_v3 out,
double  x,
double  y,
double  z 
)

Stores the given cartesian coordinates in out.

Returns:
  • HTM_ENANINF if x, y, or z is non-finite.
  • HTM_ENULLPTR if out is NULL.
  • HTM_OK on success.

Definition at line 45 of file geometry.h.

References HTM_ENANINF, HTM_ENULLPTR, HTM_ISSPECIAL, HTM_OK, htm_v3::x, htm_v3::y, and htm_v3::z.

HTM_INLINE void htm_v3_mul ( struct htm_v3 out,
const struct htm_v3 v,
double  s 
)

Stores the vector-scalar product v * s in out.

Arguments must not be NULL pointers, but may alias.

Definition at line 124 of file geometry.h.

References htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2ellipse_init2(), and htm_v3_rot().

enum htm_errcode htm_v3_ne ( struct htm_v3 north,
struct htm_v3 east,
const struct htm_v3 v 
)

Computes the N,E basis unit vectors at a position v on the unit sphere.

Returns:
  • HTM_ENULLPTR if any argument is NULL.
  • HTM_EZERONORM if v has a norm of 0.
  • HTM_OK on success.

Definition at line 24 of file geometry.c.

References HTM_ENULLPTR, HTM_EZERONORM, HTM_OK, htm_v3_norm2(), htm_v3_normalize(), htm_v3_rcross(), htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2cpoly_box(), htm_s2cpoly_ngon(), and htm_s2ellipse_init2().

HTM_INLINE void htm_v3_neg ( struct htm_v3 out,
const struct htm_v3 v 
)

Stores the vector v * -1 in out.

Arguments must not be NULL pointers, but may alias.

Definition at line 114 of file geometry.h.

References htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2cpoly_init(), and htm_s2cpoly_pad().

HTM_INLINE double htm_v3_norm2 ( const struct htm_v3 v)

Returns the squared norm of the 3-vector v, which must not be NULL.

Equivalent to htm_v3_dot(v, v).

Definition at line 169 of file geometry.h.

References htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2cpoly_box(), htm_s2cpoly_hull(), htm_s2cpoly_ngon(), htm_v3_convex(), htm_v3_edgedist2(), htm_v3_ne(), and htm_v3_norm().

HTM_INLINE void htm_v3_normalize ( struct htm_v3 out,
const struct htm_v3 v 
)

Stores a normalized copy of v in out.

Arguments must not be NULL pointers, but may alias.

Definition at line 184 of file geometry.h.

References htm_v3_norm(), htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2cpoly_box(), htm_s2cpoly_line(), htm_s2cpoly_ngon(), htm_s2cpoly_pad(), htm_s2ellipse_init(), htm_tri_init(), htm_v3_centroid(), and htm_v3_ne().

HTM_INLINE void htm_v3_rcross ( struct htm_v3 out,
const struct htm_v3 v1,
const struct htm_v3 v2 
)

Stores twice the vector cross product of v1 and v2 in out.

Arguments must not be NULL pointers, but may alias.

This is equivalent to the cross product of v2 + v1 and v2 - v1, and is more robust than htm_v3_cross() for nearly identical input unit vectors. This is because when v1 and v2 are nearly identical, the evaluation of v2 - v1 is exact and, for unit v1 and v2, v1 + v2 is perpendicular to v2 - v1.

Definition at line 203 of file geometry.h.

References htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2cpoly_area(), htm_s2cpoly_box(), htm_s2cpoly_hull(), htm_s2cpoly_init(), htm_s2cpoly_line(), htm_s2cpoly_pad(), htm_v3_convex(), htm_v3_id(), htm_v3_ne(), and htm_v3_rot().

enum htm_errcode htm_v3_rot ( struct htm_v3 out,
const struct htm_v3 v,
const struct htm_v3 k,
double  angle_deg 
)

Rotates vector v around the axis k by the specified number of degrees.

Returns:
  • HTM_ENULLPTR if out, v or k is NULL.
  • HTM_EZERONORM if k has a norm of 0.
  • HTM_OK on success.

Definition at line 85 of file geometry.c.

References HTM_ENULLPTR, HTM_EZERONORM, HTM_OK, HTM_RAD_PER_DEG, htm_v3_add(), htm_v3_dot(), htm_v3_mul(), htm_v3_norm(), and htm_v3_rcross().

Referenced by htm_s2cpoly_box(), htm_s2cpoly_line(), and htm_s2cpoly_pad().

HTM_INLINE void htm_v3_sub ( struct htm_v3 out,
const struct htm_v3 v1,
const struct htm_v3 v2 
)

Stores the vector difference v1 - v2 in out.

Arguments must not be NULL pointers, but may alias.

Definition at line 102 of file geometry.h.

References htm_v3::x, htm_v3::y, and htm_v3::z.

Referenced by htm_s2cpoly_line(), htm_s2cpoly_pad(), and htm_s2ellipse_init2().

enum htm_errcode htm_v3_tanrot ( double *  angle,
const struct htm_v3 v1,
const struct htm_v3 v2,
double  r 
)

Computes the angle by which the normal of the plane defined by the origin, v1 and v2 should be rotated about the axis v1 - v2, such that the resulting plane is tangent to two small circles of radius r centered at v1 and v2.

Note that v1 and v2 must be unit vectors!

Returns:
  • HTM_ENULLPTR if angle, v1 or v2 is NULL.
  • HTM_EDEGEN if v1 is too close to v2.
  • HTM_EANG if r is negative or too large.
  • HTM_OK on success.

Definition at line 54 of file geometry.c.

References HTM_DEG_PER_RAD, HTM_EANG, HTM_EDEGEN, HTM_ENULLPTR, HTM_OK, HTM_RAD_PER_DEG, and htm_v3_angsep().

Referenced by htm_s2cpoly_line(), and htm_s2cpoly_pad().

enum htm_errcode htm_v3_tosc ( struct htm_sc out,
const struct htm_v3 v 
)

Converts the 3-vector v to spherical coordinates and stores the results in out.

The vector v is not required to have unit norm.

Returns:
  • HTM_ENULLPTR if out or v is NULL.
  • HTM_OK on success.

Definition at line 152 of file geometry.c.

References htm_clamp(), HTM_DEG_PER_RAD, HTM_ENULLPTR, HTM_OK, htm_sc::lat, htm_sc::lon, htm_v3::x, htm_v3::y, and htm_v3::z.