C Attitude Math Library
Quaternion, Euler, and DCM math utilities for attitude control.
Loading...
Searching...
No Matches
vector3.c File Reference
#include "attitude/vector3.h"
#include <math.h>

Go to the source code of this file.

Functions

void vector3_add (const double a[3], const double b[3], double out[3])
 Add two 3D vectors component-wise.
 
void vector3_cross (const double a[3], const double b[3], double out[3])
 Compute the cross product of two vectors.
 
double vector3_dot (const double a[3], const double b[3])
 Compute the dot product of two vectors.
 
double vector3_mag (const double v[3])
 
void vector3_normalize (double v[3])
 Normalise a 3D vector in-place.
 
int vector3_normalize_safe (double v[3])
 
void vector3_sub (const double a[3], const double b[3], double out[3])
 Subtract two 3D vectors component-wise.
 

Function Documentation

◆ vector3_add()

void vector3_add ( const double  a[3],
const double  b[3],
double  out[3] 
)

Add two 3D vectors component-wise.

Parameters
aFirst operand.
bSecond operand.
outOutput \(a + b\).

Definition at line 4 of file vector3.c.

4 {
5 out[0] = a[0] + b[0];
6 out[1] = a[1] + b[1];
7 out[2] = a[2] + b[2];
8}

◆ vector3_cross()

void vector3_cross ( const double  a[3],
const double  b[3],
double  out[3] 
)

Compute the cross product of two vectors.

Parameters
aFirst vector.
bSecond vector.
outOutput \(a \times b\).

Definition at line 20 of file vector3.c.

20 {
21 out[0] = a[1]*b[2] - a[2]*b[1];
22 out[1] = a[2]*b[0] - a[0]*b[2];
23 out[2] = a[0]*b[1] - a[1]*b[0];
24}

◆ vector3_dot()

double vector3_dot ( const double  a[3],
const double  b[3] 
)

Compute the dot product of two vectors.

Parameters
aFirst vector.
bSecond vector.
Returns
Dot product \(a \cdot b\).

Definition at line 16 of file vector3.c.

16 {
17 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
18}

◆ vector3_mag()

double vector3_mag ( const double  v[3])

Definition at line 33 of file vector3.c.

33 {
34 return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
35}

◆ vector3_normalize()

void vector3_normalize ( double  v[3])

Normalise a 3D vector in-place.

Parameters
vVector to normalise.

Definition at line 26 of file vector3.c.

26 {
27 double mag = sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
28 if (mag > 0) {
29 v[0]/=mag; v[1]/=mag; v[2]/=mag;
30 }
31}

◆ vector3_normalize_safe()

int vector3_normalize_safe ( double  v[3])

Definition at line 37 of file vector3.c.

37 {
38 double mag = vector3_mag(v);
39 if (mag < 1e-14) {
40 // Too small to normalize, return failure
41 return 0;
42 }
43 v[0] /= mag; v[1] /= mag; v[2] /= mag;
44 return 1;
45}
double vector3_mag(const double v[3])
Definition vector3.c:33

◆ vector3_sub()

void vector3_sub ( const double  a[3],
const double  b[3],
double  out[3] 
)

Subtract two 3D vectors component-wise.

Parameters
aMinuend.
bSubtrahend.
outOutput \(a - b\).

Definition at line 10 of file vector3.c.

10 {
11 out[0] = a[0] - b[0];
12 out[1] = a[1] - b[1];
13 out[2] = a[2] - b[2];
14}