Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
Collaboration diagram for 3D Vector operations:

Data Structures

struct  vec_3d_
 3D vector type More...
 

Typedefs

typedef struct vec_3d_ vec_3d
 3D vector type
 

Functions

vec_3d vector_sub (const vec_3d *a, const vec_3d *b)
 Subtract one vector from another.
 
vec_3d vector_add (const vec_3d *a, const vec_3d *b)
 Add one vector to another.
 
float dot_prod (const vec_3d *a, const vec_3d *b)
 Obtain the dot product of two 3D vectors.
 
vec_3d vector_prod (const vec_3d *a, const vec_3d *b)
 Compute the vector product of two 3d vectors.
 
const char * print_vector (const vec_3d *a, const char *name)
 Print formatted vector on stdout.
 
float vector_norm (const vec_3d *a)
 Compute the norm a vector.
 
vec_3d unit_vec (const vec_3d *a)
 Obtain unit vector in the same direction as given vector.
 
mat_3x3 get_cross_matrix (const vec_3d *a)
 The cross product of vectors can be represented as a matrix multiplication operation.
 
double get_angle (const vec_3d *a, const vec_3d *b)
 Obtain the angle between two given vectors.
 

Detailed Description

Function Documentation

◆ dot_prod()

float dot_prod ( const vec_3d a,
const vec_3d b 
)

Obtain the dot product of two 3D vectors.

\[ \vec{a}\cdot\vec{b}=a_xb_x + a_yb_y + a_zb_z \]

Parameters
[in]afirst vector
[in]bsecond vector
Returns
resulting dot product
77{
78 float dot;
79#ifdef LIBQUAT_ARM
80 arm_dot_prod_f32((float *)a, (float *)b, &dot);
81#else
82 dot = a->x * b->x;
83 dot += a->y * b->y;
84 dot += a->z * b->z;
85#endif
86
87 return dot;
88}
float z
Z co-ordinate.
Definition geometry_datatypes.h:25
float x
X co-ordinate.
Definition geometry_datatypes.h:23
float y
Y co-ordinate.
Definition geometry_datatypes.h:24

◆ get_angle()

double get_angle ( const vec_3d a,
const vec_3d b 
)

Obtain the angle between two given vectors.

\[\alpha=acos\left(\frac{\vec{a} \cdot \vec{b}}{\lVert\vec{a}\rVert \cdot \lVert\vec{b}\rVert}\right)\]

Parameters
[in]afirst input vector
[in]bsecond input vector
Returns
angle between \(\vec{a}\) and \(\vec{b}\) in radians

< The norm of vector a

< The norm of vector b

detect possible division by 0 - the angle is not defined in this case

203{
204 double alpha, cos_alpha;
205 float norm_a = vector_norm(a); ///< The norm of vector a
206 float norm_b = vector_norm(b); ///< The norm of vector b
207 if (fabsf(norm_a) < EPSILON || fabsf(norm_b) < EPSILON) /// detect possible division by 0 - the angle is not defined in this case
208 {
209 return NAN;
210 }
211
212 cos_alpha = dot_prod(a, b) / (norm_a * norm_b);
213 alpha = acos(cos_alpha); // delivers the radian
214 return alpha; // in range from -1 to 1
215}
#define EPSILON
Minimum recognizable value.
Definition geometry_datatypes.h:14
float dot_prod(const vec_3d *a, const vec_3d *b)
Obtain the dot product of two 3D vectors.
Definition vectors_3d.c:76
float vector_norm(const vec_3d *a)
Compute the norm a vector.
Definition vectors_3d.c:138
Here is the call graph for this function:

◆ get_cross_matrix()

mat_3x3 get_cross_matrix ( const vec_3d a)

The cross product of vectors can be represented as a matrix multiplication operation.

This function obtains the 3x3 matrix of the cross-product operator from the first vector.

\[\begin{align*} \left(\vec{a}\times\right)\vec{b} &= \tilde{A}_a\vec{b}\\ \tilde{A}_a &= \begin{bmatrix}0&-a_z&a_y\\a_z&0&-a_x\\-a_y&a_x&0\end{bmatrix} \end{align*}\]

Parameters
[in]ainput vector
Returns
the 3x3 matrix for the cross product operator \(\left(\vec{a}\times\right)\)
189{
190 mat_3x3 A = {0., -a->z, a->y, a->z, 0., -a->x, -a->y, a->x, 0.};
191 return A;
192}
A 3x3 Matrix type definition.
Definition geometry_datatypes.h:35

◆ print_vector()

const char * print_vector ( const vec_3d a,
const char *  name 
)

Print formatted vector on stdout.

Parameters
[in]avector to print
[in]namename of the vector
Returns
string representation of vector
123{
124 static char vec_str[100]; // static to ensure the string life extends the
125 // life of function
126
127 snprintf(vec_str, 99, "vec(%s) = (%.3g)i + (%.3g)j + (%.3g)k\n", name, a->x,
128 a->y, a->z);
129 return vec_str;
130}

◆ unit_vec()

vec_3d unit_vec ( const vec_3d a)

Obtain unit vector in the same direction as given vector.

\[\hat{a}=\frac{\vec{a}}{\lVert\vec{a}\rVert}\]

Parameters
[in]ainput vector
Returns
n unit vector in the direction of \(\vec{a}\)
157{
158 vec_3d n = {0};
159
160 float norm = vector_norm(a);
161 if (fabsf(norm) < EPSILON)
162 { // detect possible divide by 0
163 return n;
164 }
165
166 if (norm != 1.F) // perform division only if needed
167 {
168 n.x = a->x / norm;
169 n.y = a->y / norm;
170 n.z = a->z / norm;
171 }
172 return n;
173}
3D vector type
Definition geometry_datatypes.h:22
Here is the call graph for this function:

◆ vector_add()

vec_3d vector_add ( const vec_3d a,
const vec_3d b 
)

Add one vector to another.

\[ \vec{c}=\vec{a}+\vec{b}=\left(a_x+b_x\right)\hat{i}+ \left(a_y+b_y\right)\hat{j}+\left(a_z+b_z\right)\hat{k}\]

Parameters
[in]avector to add to
[in]bvector to add
Returns
resultant vector
54{
55 vec_3d out;
56#ifdef LIBQUAT_ARM
57 arm_add_f32((float *)a, (float *)b, (float *)&out);
58#else
59 out.x = a->x + b->x;
60 out.y = a->y + b->y;
61 out.z = a->z + b->z;
62#endif
63
64 return out;
65}

◆ vector_norm()

float vector_norm ( const vec_3d a)

Compute the norm a vector.

\[\lVert\vec{a}\rVert = \sqrt{\vec{a}\cdot\vec{a}} \]

Parameters
[in]ainput vector
Returns
norm of the given vector
139{
140 float n = dot_prod(a, a);
141#ifdef LIBQUAT_ARM
142 arm_sqrt_f32(*n, n);
143#else
144 n = sqrtf(n);
145#endif
146
147 return n;
148}
Here is the call graph for this function:

◆ vector_prod()

vec_3d vector_prod ( const vec_3d a,
const vec_3d b 
)

Compute the vector product of two 3d vectors.

\[\begin{align*} \vec{a}\times\vec{b} &= \begin{vmatrix} \hat{i} & \hat{j} & \hat{k}\\ a_x & a_y & a_z\\ b_x & b_y & b_z \end{vmatrix}\\ &= \left(a_yb_z-b_ya_z\right)\hat{i} - \left(a_xb_z-b_xa_z\right)\hat{j} + \left(a_xb_y-b_xa_y\right)\hat{k} \end{align*} \]

Parameters
[in]afirst vector \(\vec{a}\)
[in]bsecond vector \(\vec{b}\)
Returns
resultant vector \(\vec{o}=\vec{a}\times\vec{b}\)
106{
107 vec_3d out; // better this way to avoid copying results to input
108 // vectors themselves
109 out.x = a->y * b->z - a->z * b->y;
110 out.y = -a->x * b->z + a->z * b->x;
111 out.z = a->x * b->y - a->y * b->x;
112
113 return out;
114}

◆ vector_sub()

vec_3d vector_sub ( const vec_3d a,
const vec_3d b 
)

Subtract one vector from another.

\[ \vec{c}=\vec{a}-\vec{b}=\left(a_x-b_x\right)\hat{i}+ \left(a_y-b_y\right)\hat{j}+\left(a_z-b_z\right)\hat{k}\]

Parameters
[in]avector to subtract from
[in]bvector to subtract
Returns
resultant vector
32{
33 vec_3d out;
34#ifdef LIBQUAT_ARM
35 arm_sub_f32((float *)a, (float *)b, (float *)&out);
36#else
37 out.x = a->x - b->x;
38 out.y = a->y - b->y;
39 out.z = a->z - b->z;
40#endif
41
42 return out;
43}