libQuat
Loading...
Searching...
No Matches
Collaboration diagram for 3D Vector operations:

Classes

struct  vec_3d_
 

Typedefs

typedef struct vec_3d_ vec_3d
 

Functions

LIBQUAT_DLL_EXPORTED float dot_prod (const vec_3d *a, const vec_3d *b)
 
LIBQUAT_DLL_EXPORTED vec_3d vector_sub (const vec_3d *a, const vec_3d *b)
 
LIBQUAT_DLL_EXPORTED vec_3d vector_add (const vec_3d *a, const vec_3d *b)
 
LIBQUAT_DLL_EXPORTED vec_3d vector_prod (const vec_3d *a, const vec_3d *b)
 
LIBQUAT_DLL_EXPORTED LIBQUAT_DLL_EXPORTED const char * print_vector (const vec_3d *a, const char *name)
 
LIBQUAT_DLL_EXPORTED float vector_norm (const vec_3d *a)
 
LIBQUAT_DLL_EXPORTED vec_3d unit_vec (const vec_3d *a)
 
LIBQUAT_DLL_EXPORTED mat_3x3 get_cross_matrix (const vec_3d *a)
 
LIBQUAT_DLL_EXPORTED vec_3d operator- (const vec_3d &a, const vec_3d &b)
 
LIBQUAT_DLL_EXPORTED vec_3d operator- (const vec_3d &a)
 
LIBQUAT_DLL_EXPORTED vec_3d operator+ (const vec_3d &a, const vec_3d &b)
 
LIBQUAT_DLL_EXPORTED float operator* (const vec_3d &a, const vec_3d &b)
 
LIBQUAT_DLL_EXPORTED vec_3d operator* (const float a, const vec_3d &b)
 
LIBQUAT_DLL_EXPORTED vec_3d operator* (const vec_3d &a, const float b)
 
LIBQUAT_DLL_EXPORTED vec_3d operator/ (const vec_3d &a, const float b)
 
LIBQUAT_DLL_EXPORTED vec_3d operator^ (const vec_3d &a, const vec_3d &b)
 
LIBQUAT_DLL_EXPORTED float operator~ (const vec_3d &a)
 
LIBQUAT_DLL_EXPORTED std::ostream & operator<< (std::ostream &out, vec_3d const &v)
 
LIBQUAT_DLL_EXPORTED std::ostream & operator<< (std::ostream &out, quaternion const &v)
 

Detailed Description

Typedef Documentation

◆ vec_3d

typedef struct vec_3d_ vec_3d

3D vector type

Function Documentation

◆ dot_prod()

LIBQUAT_DLL_EXPORTED 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
84{ return *a * *b; }

◆ get_cross_matrix()

LIBQUAT_DLL_EXPORTED 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)\)
162 {
163 mat_3x3 A = {0., -a->z, a->y, a->z, 0., -a->x, -a->y, a->x, 0.};
164 return A;
165}
Definition: libquat.h:76
float z
Definition: libquat.h:67
float x
Definition: libquat.h:65
float y
Definition: libquat.h:66

◆ operator*() [1/3]

LIBQUAT_DLL_EXPORTED vec_3d operator* ( const float  a,
const vec_3d &  b 
)

Scalar multiplication of a 3D vector.

\[ a\cdot\vec{b}=a\cdot b_x\hat{i} + a\cdot b_y \hat{j} + a\cdot b_z\hat{k} \]

Parameters
[in]apre-multiplying scalar
[in]bsecond vector
Returns
resulting dot product
66 {
67 vec_3d result = {0.f};
68 result.x = a * b.x;
69 result.y = a * b.y;
70 result.z = a * b.z;
71
72 return result;
73}
Definition: libquat.h:64

◆ operator*() [2/3]

LIBQUAT_DLL_EXPORTED vec_3d operator* ( const vec_3d &  a,
const float  b 
)

Scalar multiplication of a 3D vector.

\[ \vec{a}\cdot b=a_x\cdot b\hat{i} + a_y\cdot b\hat{j} + a_z\cdot b\hat{k} \]

Parameters
[in]afirst vector
[in]bpost-multiplying scalar
Returns
resulting dot product
75 {
76 vec_3d result = {0.f};
77 result.x = a.x * b;
78 result.y = a.y * b;
79 result.z = a.z * b;
80
81 return result;
82}

◆ operator*() [3/3]

LIBQUAT_DLL_EXPORTED float operator* ( 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
53 {
54 float result = 0.f;
55#ifdef LIBQUAT_ARM
56 arm_dot_prod_f32((float *)a, (float *)b, result);
57#else
58 result = a.x * b.x;
59 result += a.y * b.y;
60 result += a.z * b.z;
61#endif
62
63 return result;
64}

◆ operator+()

LIBQUAT_DLL_EXPORTED vec_3d operator+ ( 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]afirst vector to add
[in]bsecond vector to add
Returns
resultant vector
38 {
39 vec_3d result = {0.f};
40#ifdef LIBQUAT_ARM
41 arm_sub_f32((float *)&a, (float *)&b, (float *)&result);
42#else
43 result.x = a.x + b.x;
44 result.y = a.y + b.y;
45 result.z = a.z + b.z;
46#endif
47
48 return result;
49}

◆ operator-() [1/2]

LIBQUAT_DLL_EXPORTED vec_3d operator- ( const vec_3d &  a)

Unary negation operator.

\[ -\vec{a}=(-a_x)\hat{i}+ (-a_y)\hat{j} + (-a_z)\hat{k} \]

Parameters
[in]avector to negate
Returns
resultant vector
18 {
19 vec_3d ret = {-a.x, -a.y, -a.z};
20 return ret;
21}

◆ operator-() [2/2]

LIBQUAT_DLL_EXPORTED vec_3d operator- ( 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
23 {
24 vec_3d result = {0.f};
25#ifdef LIBQUAT_ARM
26 arm_sub_f32((float *)&a, (float *)&b, (float *)&result);
27#else
28 result.x = a.x - b.x;
29 result.y = a.y - b.y;
30 result.z = a.z - b.z;
31#endif
32
33 return result;
34}

◆ operator/()

LIBQUAT_DLL_EXPORTED vec_3d operator/ ( const vec_3d &  a,
const float  b 
)

Scalar division of a 3D vector.

Parameters
[in]avector to divide
[in]bscalar divisor
Returns
resulting quotient
86 {
87 vec_3d temp = {0.f};
88
89 temp.x = in_quat.x / scalar;
90 temp.y = in_quat.y / scalar;
91 temp.z = in_quat.z / scalar;
92
93 return temp;
94}

◆ operator<<() [1/2]

LIBQUAT_DLL_EXPORTED std::ostream & operator<< ( std::ostream &  out,
quaternion const &  v 
)

operator to print a vec_3d instance.

Parameters
outstd::ostream stream to print output to
vvector to print
Returns
formatted stream

◆ operator<<() [2/2]

LIBQUAT_DLL_EXPORTED std::ostream & operator<< ( std::ostream &  out,
vec_3d const &  v 
)

operator to print a vec_3d instance.

Parameters
outstd::ostream stream to print output to
vvector to print
Returns
formatted stream
108 {
109 const int width = 10;
110 const char separator = ' ';
111
112 out << "<";
113 out << std::left << std::setw(width) << std::setfill(separator) << v.x;
114 out << "," << std::left << std::setw(width) << std::setfill(separator)
115 << v.y;
116 out << "," << std::left << std::setw(width) << std::setfill(separator)
117 << v.z;
118 out << ">";
119
120 return out;
121}
T left(T... args)
T setfill(T... args)
T setw(T... args)
Here is the call graph for this function:

◆ operator^()

LIBQUAT_DLL_EXPORTED vec_3d operator^ ( 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}\)
96 {
97 vec_3d out{0.f}; // better this way to avoid copying results to input
98 // vectors themselves
99 out.x = a.y * b.z - a.z * b.y;
100 out.y = -a.x * b.z + a.z * b.x;
101 out.z = a.x * b.y - a.y * b.x;
102
103 return out;
104}

◆ operator~()

LIBQUAT_DLL_EXPORTED float operator~ ( const vec_3d &  a)

Compute the norm a vector.

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

Parameters
[in]ainput vector
Returns
n norm of the given vector
130 {
131 float result = a * a;
132#ifdef LIBQUAT_ARM
133 arm_sqrt_f32(&result, result);
134#else
135 result = sqrtf(result);
136#endif
137
138 return result;
139}

◆ print_vector()

LIBQUAT_DLL_EXPORTED LIBQUAT_DLL_EXPORTED 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
formatted vector for printing
123 {
124 static char vec_str[100]; // NOLINT
125 snprintf(vec_str, 99, // NOLINT
126 "vec(%s) = (%.3g)i + (%.3g)j + (%.3g)k\n", name, a->x, a->y, a->z);
127 return vec_str;
128}
T snprintf(T... args)

◆ unit_vec()

LIBQUAT_DLL_EXPORTED 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
unit vector in the direction of \(\vec{a}\)
143 {
144 float norm = ~(*a);
145 vec_3d result = {0.f};
146
147 if (fabsf(norm) < LIBQUAT_EPSILON) {
148 std::cerr << __func__ << ": Division by zero.";
149 return result;
150 }
151
152 if (norm != 1.F) // perform division only if needed
153 {
154 result.x = a->x / norm;
155 result.y = a->y / norm;
156 result.z = a->z / norm;
157 }
158
159 return result;
160}
#define LIBQUAT_EPSILON
Definition: libquat.h:24

◆ vector_add()

LIBQUAT_DLL_EXPORTED 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
51{ return *a + *b; }

◆ vector_norm()

LIBQUAT_DLL_EXPORTED 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
141{ return ~(*a); }

◆ vector_prod()

LIBQUAT_DLL_EXPORTED 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{ return *a ^ *b; }

◆ vector_sub()

LIBQUAT_DLL_EXPORTED 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
36{ return *a - *b; }