Sun position
Sun position algorithm
vectors3d.h
Go to the documentation of this file.
1 #ifndef VECTORS3D_H
2 #define VECTORS3D_H
3 /*============================================================================*/
4 /*!\file
5  * \brief vectors3d.h - Three dimensional geometry, vectors and matrices
6  *
7  * \author David Hoadley
8  *
9  * \details
10  * Data types and operations for handling three-dimensional positions
11  * and performing operations upon them, such as adding or subtracting
12  * vectors, coordinate rotation, conversion to and from polar coords.
13  *
14  *==============================================================================
15  */
16 /*
17  * Copyright (c) 2020, David Hoadley <vcrumble@westnet.com.au>
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions are met:
22  *
23  * * Redistributions of source code must retain the above copyright notice, this
24  * list of conditions and the following disclaimer.
25  * * Redistributions in binary form must reproduce the above copyright notice,
26  * this list of conditions and the following disclaimer in the documentation
27  * and/or other materials provided with the distribution.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
33  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /*
47  * Global #defines and typedefs
48  */
49 /*! 3x3 matrix. See the note \ref page-why-struct-array (at the end of file
50  * vectors3.c) for the reason that this has been implemented in this fashion */
51 typedef struct {
52  double a[3][3];
53 } V3D_Matrix;
54 
55 /*! 3x1 vector. See the note \ref page-why-struct-array (at the end of file
56  * vectors3.c) for the reason that this has been implemented in this fashion */
57 typedef struct {
58  double a[3];
59 } V3D_Vector;
60 
61 /*! Enumeration to be used by the v3d_createRotationMatrix() function */
62 typedef enum /* V3D_AxisNames_tag */ {
63  Xaxis,
64  Yaxis,
65  Zaxis
67 
68 
69 /*
70  * Global functions available to be called by other modules
71  */
72 /* Most of the functions below return vector or matrix results in their
73  first argument, but also return a pointer to that same variable as the
74  function result. */
75 
76 /* Vector arithmetic */
78  const V3D_Vector *srcV1,
79  const V3D_Vector *srcV2);
80 V3D_Vector *v3d_addToV(V3D_Vector *modV1, const V3D_Vector *srcV2);
82  const V3D_Vector *srcV1,
83  const V3D_Vector *srcV2);
84 V3D_Vector *v3d_subFromV(V3D_Vector *modV1, const V3D_Vector *srcV2);
85 double v3d_dotProductV(const V3D_Vector *srcV1, const V3D_Vector *srcV2);
87  const V3D_Vector *srcV1,
88  const V3D_Vector *srcV2);
89 /* Two special versions of v3d_addToV() for unit vectors */
90 V3D_Vector *v3d_addToUV(V3D_Vector *modV1, const V3D_Vector *srcV2);
91 V3D_Vector *v3d_addToUVfast(V3D_Vector *modV1, const V3D_Vector *srcV2);
92 
93 double v3d_magVSq(const V3D_Vector *srcV);
94 double v3d_magV(const V3D_Vector *srcV);
95 
96 /* Polar <==> Rectangular coordinate conversions */
98  double alpha_rad,
99  double delta_rad);
100 void v3d_rectToPolar(double *alpha_rad,
101  double *delta_rad,
102  const V3D_Vector *srcV);
103 #if 0
104 /* deprecated older versions */
105 void v3d_polar2Rect(double alpha_rad, double delta_rad, V3D_Vector *R);
106 void v3d_rect2Polar(const V3D_Vector *R,
107  bool alphaNonNegative,
108  double *alpha_rad,
109  double *delta_rad);
110 #endif
111 
112 /* Matrices for coordinate rotation, and matrix operations */
114  V3D_AxisNames axis,
115  double angle_rad);
117  const V3D_Matrix *srcM,
118  const V3D_Vector *srcV);
120  const V3D_Matrix *srcM,
121  const V3D_Vector *srcV);
123  const V3D_Matrix *srcM1,
124  const V3D_Matrix *srcM2);
125 
126 
127 #ifdef __cplusplus
128 }
129 #endif
130 
131 #endif /*VECTORS3D_H*/
v3d_magVSq
double v3d_magVSq(const V3D_Vector *srcV)
Return the square of the magnitude of the specified vector.
Definition: vectors3d.c:258
v3d_multMxM
V3D_Matrix * v3d_multMxM(V3D_Matrix *destM, const V3D_Matrix *srcM1, const V3D_Matrix *srcM2)
Multiply 3x3 matrix by 3x3 matrix to give a new 3x3 matrix, as per [destM] = [srcM1]*[srcM2].
Definition: vectors3d.c:517
v3d_polarToRect
V3D_Vector * v3d_polarToRect(V3D_Vector *destV, double alpha_rad, double delta_rad)
Converts polar (curvilinear) coordinates to equivalent rectangular (Cartesian) coordinates.
Definition: vectors3d.c:280
v3d_magV
double v3d_magV(const V3D_Vector *srcV)
Return the magnitude of the specified vector.
Definition: vectors3d.c:271
V3D_AxisNames
V3D_AxisNames
Enumeration to be used by the v3d_createRotationMatrix() function.
Definition: vectors3d.h:62
v3d_rectToPolar
void v3d_rectToPolar(double *alpha_rad, double *delta_rad, const V3D_Vector *srcV)
Converts rectangular (Cartesian) coordinates to the equivalent polar (curvilinear) ones.
Definition: vectors3d.c:311
v3d_addToUV
V3D_Vector * v3d_addToUV(V3D_Vector *modV1, const V3D_Vector *srcV2)
Modify a unit length rectangular position vector modV1 by adding a correction vector srcV2 to it and ...
Definition: vectors3d.c:193
v3d_multMtransxV
V3D_Vector * v3d_multMtransxV(V3D_Vector *destV, const V3D_Matrix *srcM, const V3D_Vector *srcV)
Multiply 3x1 vector by the transpose of the 3x3 matrix to give a new 3x1 vector, as per equation [des...
Definition: vectors3d.c:475
v3d_crossProductV
V3D_Vector * v3d_crossProductV(V3D_Vector *destV, const V3D_Vector *srcV1, const V3D_Vector *srcV2)
Return the cross product of the two vectors: [destV] = [srcV1] x [srcV2].
Definition: vectors3d.c:167
v3d_subFromV
V3D_Vector * v3d_subFromV(V3D_Vector *modV1, const V3D_Vector *srcV2)
Modify first vector by subtracting a second one: [modV1] -= [srcV2].
Definition: vectors3d.c:129
V3D_Matrix
3x3 matrix.
Definition: vectors3d.h:51
v3d_addToV
V3D_Vector * v3d_addToV(V3D_Vector *modV1, const V3D_Vector *srcV2)
Modify first vector by adding a second one: [modV1] += [srcV2].
Definition: vectors3d.c:87
v3d_subtractV
V3D_Vector * v3d_subtractV(V3D_Vector *destV, const V3D_Vector *srcV1, const V3D_Vector *srcV2)
Vector subtraction: [destV] = [srcV1] - [srcV2].
Definition: vectors3d.c:106
V3D_Vector
3x1 vector.
Definition: vectors3d.h:57
v3d_dotProductV
double v3d_dotProductV(const V3D_Vector *srcV1, const V3D_Vector *srcV2)
Return the dot product of the two vectors: res = [srcV1] ยท [srcV2] or res = ||srcV1|| * ||srcV2|| * c...
Definition: vectors3d.c:148
v3d_addV
V3D_Vector * v3d_addV(V3D_Vector *destV, const V3D_Vector *srcV1, const V3D_Vector *srcV2)
Add two vectors: [destV] = [srcV1] + [srcV2].
Definition: vectors3d.c:64
v3d_createRotationMatrix
V3D_Matrix * v3d_createRotationMatrix(V3D_Matrix *destM, V3D_AxisNames axis, double angle_rad)
Creates a matrix to rotate a coordinate system about an axis.
Definition: vectors3d.c:377
v3d_addToUVfast
V3D_Vector * v3d_addToUVfast(V3D_Vector *modV1, const V3D_Vector *srcV2)
Modify a unit length rectangular position vector modV1 by adding a small correction vector srcV2 to i...
Definition: vectors3d.c:223
v3d_multMxV
V3D_Vector * v3d_multMxV(V3D_Vector *destV, const V3D_Matrix *srcM, const V3D_Vector *srcV)
Multiply 3x3 matrix by 3x1 vector to give a new 3x1 vector, as per equation [destV] = [srcM] * [srcV]...
Definition: vectors3d.c:439