OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
ias_math_compute_unit_vector.c
Go to the documentation of this file.
1 /*******************************************************************************
2 NAME: ias_math_compute_unit_vector
3 
4 PURPOSE: Compute a vector's unit vector
5 
6 RETURNS: SUCCESS or ERROR
7 
8 *******************************************************************************/
9 #include <math.h>
10 #include "ias_math.h"
11 #include "ias_logging.h"
12 
14 (
15  const IAS_VECTOR *vec, /* I: Input vector */
16  IAS_VECTOR *unit_vector /* O: Unit vector of the input vector */
17 )
18 {
19  double magnitude; /* Magnitude of the input vector */
20 
21  /* Calculate the norm */
22  magnitude = ias_math_compute_vector_length(vec);
23 
24  if (magnitude == 0.0)
25  {
26  IAS_LOG_ERROR("Input vector magnitude is zero");
27  return ERROR;
28  }
29 
30  /* Calculate the unit vector */
31  unit_vector->x = vec->x / magnitude;
32  unit_vector->y = vec->y / magnitude;
33  unit_vector->z = vec->z / magnitude;
34 
35  return SUCCESS;
36 }
#define SUCCESS
Definition: ObpgReadGrid.h:15
#define IAS_LOG_ERROR(format,...)
Definition: ias_logging.h:96
int ias_math_compute_unit_vector(const IAS_VECTOR *vec, IAS_VECTOR *unit_vector)
double ias_math_compute_vector_length(const IAS_VECTOR *vec)
#define ERROR
Definition: ancil.h:24