Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
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. More...
 
vec_3d vector_add (const vec_3d *a, const vec_3d *b)
 Add one vector to another. More...
 
float dot_prod (const vec_3d *a, const vec_3d *b)
 Obtain the dot product of two 3D vectors. More...
 
vec_3d vector_prod (const vec_3d *a, const vec_3d *b)
 Compute the vector product of two 3d vectors. More...
 
const char * print_vector (const vec_3d *a, const char *name)
 Print formatted vector on stdout. More...
 
float vector_norm (const vec_3d *a)
 Compute the norm a vector. More...
 
vec_3d unit_vec (const vec_3d *a)
 Obtain unit vector in the same direction as given vector. More...
 
mat_3x3 get_cross_matrix (const vec_3d *a)
 The cross product of vectors can be represented as a matrix multiplication operation. More...
 

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 }

◆ 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 }

◆ 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 }
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 }
EPSILON
#define EPSILON
Minimum recognizable value.
Definition: geometry_datatypes.h:14
vec_3d_::x
float x
X co-ordinate.
Definition: geometry_datatypes.h:23
vector_norm
float vector_norm(const vec_3d *a)
Compute the norm a vector.
Definition: vectors_3d.c:138
vec_3d_
3D vector type
Definition: geometry_datatypes.h:22
vec_3d_::z
float z
Z co-ordinate.
Definition: geometry_datatypes.h:25
dot_prod
float dot_prod(const vec_3d *a, const vec_3d *b)
Obtain the dot product of two 3D vectors.
Definition: vectors_3d.c:76
mat_3x3_
A 3x3 Matrix type definition.
Definition: geometry_datatypes.h:35
vec_3d_::y
float y
Y co-ordinate.
Definition: geometry_datatypes.h:24