3 Component Vectors

3 Component Vectors — Functions for handling single precision float vectors.

Synopsis

void                cogl_vector3_init                   (CoglVector3 *vector,
                                                         float x,
                                                         float y,
                                                         float z);
void                cogl_vector3_init_zero              (CoglVector3 *vector);
gboolean            cogl_vector3_equal                  (gconstpointer v1,
                                                         gconstpointer v2);
gboolean            cogl_vector3_equal_with_epsilon     (const CoglVector3 *vector0,
                                                         const CoglVector3 *vector1,
                                                         float epsilon);
CoglVector3 *       cogl_vector3_copy                   (const CoglVector3 *vector);
void                cogl_vector3_free                   (CoglVector3 *vector);
void                cogl_vector3_invert                 (CoglVector3 *vector);
void                cogl_vector3_add                    (CoglVector3 *result,
                                                         const CoglVector3 *a,
                                                         const CoglVector3 *b);
void                cogl_vector3_subtract               (CoglVector3 *result,
                                                         const CoglVector3 *a,
                                                         const CoglVector3 *b);
void                cogl_vector3_multiply_scalar        (CoglVector3 *vector,
                                                         float scalar);
void                cogl_vector3_divide_scalar          (CoglVector3 *vector,
                                                         float scalar);
void                cogl_vector3_normalize              (CoglVector3 *vector);
float               cogl_vector3_magnitude              (const CoglVector3 *vector);
void                cogl_vector3_cross_product          (CoglVector3 *result,
                                                         const CoglVector3 *u,
                                                         const CoglVector3 *v);
float               cogl_vector3_dot_product            (const CoglVector3 *a,
                                                         const CoglVector3 *b);
float               cogl_vector3_distance               (const CoglVector3 *a,
                                                         const CoglVector3 *b);

Description

This exposes a utility API that can be used for basic manipulation of 3 component float vectors.

Details

cogl_vector3_init ()

void                cogl_vector3_init                   (CoglVector3 *vector,
                                                         float x,
                                                         float y,
                                                         float z);

Initializes a 3 component, single precision float vector which can then be manipulated with the cogl_vector convenience APIs. Vectors can also be used in places where a "point" is often desired.

vector :

The CoglVector3 you want to initialize

x :

The x component

y :

The y component

z :

The z component

Since 1.4

Stability Level: Unstable


cogl_vector3_init_zero ()

void                cogl_vector3_init_zero              (CoglVector3 *vector);

Initializes a 3 component, single precision float vector with zero for each component.

vector :

The CoglVector3 you want to initialize

Since 1.4

Stability Level: Unstable


cogl_vector3_equal ()

gboolean            cogl_vector3_equal                  (gconstpointer v1,
                                                         gconstpointer v2);

Compares the components of two vectors and returns TRUE if they are the same.

The comparison of the components is done with the '==' operator such that -0 is considered equal to 0, but otherwise there is no fuzziness such as an epsilon to consider vectors that are essentially identical except for some minor precision error differences due to the way they have been manipulated.

v1 :

The first CoglVector3 you want to compare

v2 :

The second CoglVector3 you want to compare

Returns :

TRUE if the vectors are equal else FALSE.

Since 1.4

Stability Level: Unstable


cogl_vector3_equal_with_epsilon ()

gboolean            cogl_vector3_equal_with_epsilon     (const CoglVector3 *vector0,
                                                         const CoglVector3 *vector1,
                                                         float epsilon);

Compares the components of two vectors using the given epsilon and returns TRUE if they are the same, using an internal epsilon for comparing the floats.

Each component is compared against the epsilon value in this way:

1
if (fabsf (vector0->x - vector1->x) < epsilon)

vector0 :

The first CoglVector3 you want to compare

vector1 :

The second CoglVector3 you want to compare

epsilon :

The allowable difference between components to still be considered equal

Returns :

TRUE if the vectors are equal else FALSE.

Since 1.4

Stability Level: Unstable


cogl_vector3_copy ()

CoglVector3 *       cogl_vector3_copy                   (const CoglVector3 *vector);

Allocates a new CoglVector3 structure on the heap initializing the components from the given vector and returns a pointer to the newly allocated vector. You should free the memory using cogl_vector3_free()

vector :

The CoglVector3 you want to copy

Returns :

A newly allocated CoglVector3.

Since 1.4

Stability Level: Unstable


cogl_vector3_free ()

void                cogl_vector3_free                   (CoglVector3 *vector);

Frees a CoglVector3 that was previously allocated with cogl_vector_copy()

vector :

The CoglVector3 you want to free

Since 1.4

Stability Level: Unstable


cogl_vector3_invert ()

void                cogl_vector3_invert                 (CoglVector3 *vector);

Inverts/negates all the components of the given vector.

vector :

The CoglVector3 you want to manipulate

Since 1.4

Stability Level: Unstable


cogl_vector3_add ()

void                cogl_vector3_add                    (CoglVector3 *result,
                                                         const CoglVector3 *a,
                                                         const CoglVector3 *b);

Adds each of the corresponding components in vectors a and b storing the results in result.

result :

Where you want the result written

a :

The first vector operand

b :

The second vector operand

Since 1.4

Stability Level: Unstable


cogl_vector3_subtract ()

void                cogl_vector3_subtract               (CoglVector3 *result,
                                                         const CoglVector3 *a,
                                                         const CoglVector3 *b);

Subtracts each of the corresponding components in vector b from a storing the results in result.

result :

Where you want the result written

a :

The first vector operand

b :

The second vector operand

Since 1.4

Stability Level: Unstable


cogl_vector3_multiply_scalar ()

void                cogl_vector3_multiply_scalar        (CoglVector3 *vector,
                                                         float scalar);

Multiplies each of the vector components by the given scalar.

vector :

The CoglVector3 you want to manipulate

scalar :

The scalar you want to multiply the vector components by

Since 1.4

Stability Level: Unstable


cogl_vector3_divide_scalar ()

void                cogl_vector3_divide_scalar          (CoglVector3 *vector,
                                                         float scalar);

Divides each of the vector components by the given scalar.

vector :

The CoglVector3 you want to manipulate

scalar :

The scalar you want to divide the vector components by

Since 1.4

Stability Level: Unstable


cogl_vector3_normalize ()

void                cogl_vector3_normalize              (CoglVector3 *vector);

Updates the vector so it is a "unit vector" such that the vectors magnitude or length is equal to 1.

vector :

The CoglVector3 you want to manipulate

Since 1.4

Stability Level: Unstable


cogl_vector3_magnitude ()

float               cogl_vector3_magnitude              (const CoglVector3 *vector);

Calculates the scalar magnitude or length of vector.

vector :

The CoglVector3 you want the magnitude for

Returns :

The magnitude of vector.

Since 1.4

Stability Level: Unstable


cogl_vector3_cross_product ()

void                cogl_vector3_cross_product          (CoglVector3 *result,
                                                         const CoglVector3 *u,
                                                         const CoglVector3 *v);

Calculates the cross product between the two vectors u and v.

The cross product is a vector perpendicular to both u and v. This can be useful for calculating the normal of a polygon by creating two vectors in its plane using the polygons vertices and taking their cross product.

If the two vectors are parallel then the cross product is 0.

You can use a right hand rule to determine which direction the perpendicular vector will point: If you place the two vectors tail, to tail and imagine grabbing the perpendicular line that extends through the common tail with your right hand such that you fingers rotate in the direction from u to v then the resulting vector points along your extended thumb.

result :

Where you want the result written

u :

Your first CoglVector3

v :

Your second CoglVector3

Returns :

The cross product between two vectors u and v.

Since 1.4

Stability Level: Unstable


cogl_vector3_dot_product ()

float               cogl_vector3_dot_product            (const CoglVector3 *a,
                                                         const CoglVector3 *b);

Calculates the dot product of the two CoglVector3s. This can be used to determine the magnitude of one vector projected onto another. (for example a surface normal)

For example if you have a polygon with a given normal vector and some other point for which you want to calculate its distance from the polygon, you can create a vector between one of the polygon vertices and that point and use the dot product to calculate the magnitude for that vector but projected onto the normal of the polygon. This way you don't just get the distance from the point to the edge of the polygon you get the distance from the point to the nearest part of the polygon.

Note

If you don't use a unit length normal in the above example then you would then also have to divide the result by the magnitude of the normal

The dot product is calculated as:

1
(a->x * b->x + a->y * b->y + a->z * b->z)

For reference, the dot product can also be calculated from the angle between two vectors as:

1
|a||b|cos𝜃

a :

Your first CoglVector3

b :

Your second CoglVector3

Returns :

The dot product of two vectors.

Since 1.4

Stability Level: Unstable


cogl_vector3_distance ()

float               cogl_vector3_distance               (const CoglVector3 *a,
                                                         const CoglVector3 *b);

If you consider the two given vectors as (x,y,z) points instead then this will compute the distance between those two points.

a :

The first point

b :

The second point

Returns :

The distance between two points given as CoglVector3s

Since 1.4

Stability Level: Unstable