638 lines
22 KiB
C
638 lines
22 KiB
C
#ifndef _BGC_VECTOR2_H_INCLUDED_
|
|
#define _BGC_VECTOR2_H_INCLUDED_
|
|
|
|
#include <math.h>
|
|
|
|
#include "./utilities.h"
|
|
#include "./types.h"
|
|
#include "./angle.h"
|
|
|
|
// =================== Reset ==================== //
|
|
|
|
inline void bgc_fp32_vector2_reset(BGC_FP32_Vector2* const vector)
|
|
{
|
|
vector->x = 0.0f;
|
|
vector->y = 0.0f;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_reset(BGC_FP64_Vector2* const vector)
|
|
{
|
|
vector->x = 0.0;
|
|
vector->y = 0.0;
|
|
}
|
|
|
|
// ================== Modulus =================== //
|
|
|
|
inline float bgc_fp32_vector2_get_squared_length(const BGC_FP32_Vector2* const vector)
|
|
{
|
|
return vector->x * vector->x + vector->y * vector->y;
|
|
}
|
|
|
|
inline double bgc_fp64_vector2_get_squared_length(const BGC_FP64_Vector2* const vector)
|
|
{
|
|
return vector->x * vector->x + vector->y * vector->y;
|
|
}
|
|
|
|
inline float bgc_fp32_vector2_get_length(const BGC_FP32_Vector2* const vector)
|
|
{
|
|
return sqrtf(bgc_fp32_vector2_get_squared_length(vector));
|
|
}
|
|
|
|
inline double bgc_fp64_vector2_get_length(const BGC_FP64_Vector2* const vector)
|
|
{
|
|
return sqrt(bgc_fp64_vector2_get_squared_length(vector));
|
|
}
|
|
|
|
// ================= Comparison ================= //
|
|
|
|
inline int bgc_fp32_vector2_is_zero(const BGC_FP32_Vector2* const vector)
|
|
{
|
|
return bgc_fp32_vector2_get_squared_length(vector) <= BGC_FP32_SQUARE_EPSILON;
|
|
}
|
|
|
|
inline int bgc_fp64_vector2_is_zero(const BGC_FP64_Vector2* const vector)
|
|
{
|
|
return bgc_fp64_vector2_get_squared_length(vector) <= BGC_FP64_SQUARE_EPSILON;
|
|
}
|
|
|
|
inline int bgc_fp32_vector2_is_unit(const BGC_FP32_Vector2* const vector)
|
|
{
|
|
return bgc_fp32_is_square_unit(bgc_fp32_vector2_get_squared_length(vector));
|
|
}
|
|
|
|
inline int bgc_fp64_vector2_is_unit(const BGC_FP64_Vector2* const vector)
|
|
{
|
|
return bgc_fp64_is_square_unit(bgc_fp64_vector2_get_squared_length(vector));
|
|
}
|
|
|
|
// ==================== Copy ==================== //
|
|
|
|
inline void bgc_fp32_vector2_copy(BGC_FP32_Vector2* const destination, const BGC_FP32_Vector2* const source)
|
|
{
|
|
destination->x = source->x;
|
|
destination->y = source->y;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_copy(BGC_FP64_Vector2* const destination, const BGC_FP64_Vector2* const source)
|
|
{
|
|
destination->x = source->x;
|
|
destination->y = source->y;
|
|
}
|
|
|
|
// ==================== Swap ==================== //
|
|
|
|
inline void bgc_fp32_vector2_swap(BGC_FP32_Vector2* const vector1, BGC_FP32_Vector2* const vector2)
|
|
{
|
|
const float x = vector2->x;
|
|
const float y = vector2->y;
|
|
|
|
vector2->x = vector1->x;
|
|
vector2->y = vector1->y;
|
|
|
|
vector1->x = x;
|
|
vector1->y = y;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_swap(BGC_FP64_Vector2* const vector1, BGC_FP64_Vector2* const vector2)
|
|
{
|
|
const double x = vector2->x;
|
|
const double y = vector2->y;
|
|
|
|
vector2->x = vector1->x;
|
|
vector2->y = vector1->y;
|
|
|
|
vector1->x = x;
|
|
vector1->y = y;
|
|
}
|
|
|
|
// ================== Convert =================== //
|
|
|
|
inline void bgc_fp32_vector2_convert_to_fp64(BGC_FP64_Vector2* const destination, const BGC_FP32_Vector2* const source)
|
|
{
|
|
destination->x = source->x;
|
|
destination->y = source->y;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_convert_to_fp32(BGC_FP32_Vector2* const destination, const BGC_FP64_Vector2* const source)
|
|
{
|
|
destination->x = (float)source->x;
|
|
destination->y = (float)source->y;
|
|
}
|
|
|
|
// ==================== Add ===================== //
|
|
|
|
inline void bgc_fp32_vector2_add(BGC_FP32_Vector2* const sum, const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2)
|
|
{
|
|
sum->x = vector1->x + vector2->x;
|
|
sum->y = vector1->y + vector2->y;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_add(BGC_FP64_Vector2* const sum, const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2)
|
|
{
|
|
sum->x = vector1->x + vector2->x;
|
|
sum->y = vector1->y + vector2->y;
|
|
}
|
|
|
|
// ================= Add Scaled ================= //
|
|
|
|
inline void bgc_fp32_vector2_add_scaled(BGC_FP32_Vector2* const sum, const BGC_FP32_Vector2* const basic_vector, const BGC_FP32_Vector2* const scalable_vector, const float scale)
|
|
{
|
|
sum->x = basic_vector->x + scalable_vector->x * scale;
|
|
sum->y = basic_vector->y + scalable_vector->y * scale;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_add_scaled(BGC_FP64_Vector2* const sum, const BGC_FP64_Vector2* const basic_vector, const BGC_FP64_Vector2* const scalable_vector, const double scale)
|
|
{
|
|
sum->x = basic_vector->x + scalable_vector->x * scale;
|
|
sum->y = basic_vector->y + scalable_vector->y * scale;
|
|
}
|
|
|
|
// ================== Subtract ================== //
|
|
|
|
inline void bgc_fp32_vector2_subtract(BGC_FP32_Vector2* const difference, const BGC_FP32_Vector2* const minuend, const BGC_FP32_Vector2* const subtrahend)
|
|
{
|
|
difference->x = minuend->x - subtrahend->x;
|
|
difference->y = minuend->y - subtrahend->y;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_subtract(BGC_FP64_Vector2* const difference, const BGC_FP64_Vector2* const minuend, const BGC_FP64_Vector2* const subtrahend)
|
|
{
|
|
difference->x = minuend->x - subtrahend->x;
|
|
difference->y = minuend->y - subtrahend->y;
|
|
}
|
|
|
|
// ============== Subtract Scaled =============== //
|
|
|
|
inline void bgc_fp32_vector2_subtract_scaled(BGC_FP32_Vector2* const difference, const BGC_FP32_Vector2* const basic_vector, const BGC_FP32_Vector2* const scalable_vector, const float scale)
|
|
{
|
|
difference->x = basic_vector->x - scalable_vector->x * scale;
|
|
difference->y = basic_vector->y - scalable_vector->y * scale;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_subtract_scaled(BGC_FP64_Vector2* const difference, const BGC_FP64_Vector2* const basic_vector, const BGC_FP64_Vector2* const scalable_vector, const double scale)
|
|
{
|
|
difference->x = basic_vector->x - scalable_vector->x * scale;
|
|
difference->y = basic_vector->y - scalable_vector->y * scale;
|
|
}
|
|
|
|
// ================== Multiply ================== //
|
|
|
|
inline void bgc_fp32_vector2_multiply_by_real_number(BGC_FP32_Vector2* const product, const BGC_FP32_Vector2* const multiplicand, const float multiplier)
|
|
{
|
|
product->x = multiplicand->x * multiplier;
|
|
product->y = multiplicand->y * multiplier;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_multiply_by_real_number(BGC_FP64_Vector2* const product, const BGC_FP64_Vector2* const multiplicand, const double multiplier)
|
|
{
|
|
product->x = multiplicand->x * multiplier;
|
|
product->y = multiplicand->y * multiplier;
|
|
}
|
|
|
|
// ============ Left Vector Product ============= //
|
|
|
|
inline void bgc_fp32_vector2_multiply_by_matrix2x2(BGC_FP32_Vector2* const product, const BGC_FP32_Vector2* const vector, const BGC_FP32_Matrix2x2* const matrix)
|
|
{
|
|
const float x = vector->x * matrix->r1c1 + vector->y * matrix->r2c1;
|
|
const float y = vector->x * matrix->r1c2 + vector->y * matrix->r2c2;
|
|
|
|
product->x = x;
|
|
product->y = y;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_multiply_by_matrix2x2(BGC_FP64_Vector2* const product, const BGC_FP64_Vector2* const vector, const BGC_FP64_Matrix2x2* const matrix)
|
|
{
|
|
const double x = vector->x * matrix->r1c1 + vector->y * matrix->r2c1;
|
|
const double y = vector->x * matrix->r1c2 + vector->y * matrix->r2c2;
|
|
|
|
product->x = x;
|
|
product->y = y;
|
|
}
|
|
|
|
// ============ Left Vector Product ============= //
|
|
|
|
inline void bgc_fp32_vector2_multiply_by_matrix3x2(BGC_FP32_Vector3* const product, const BGC_FP32_Vector2* const vector, const BGC_FP32_Matrix3x2* const matrix)
|
|
{
|
|
product->x = vector->x * matrix->r1c1 + vector->y * matrix->r2c1;
|
|
product->y = vector->x * matrix->r1c2 + vector->y * matrix->r2c2;
|
|
product->z = vector->x * matrix->r1c3 + vector->y * matrix->r2c3;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_multiply_by_matrix3x2(BGC_FP64_Vector3* const product, const BGC_FP64_Vector2* const vector, const BGC_FP64_Matrix3x2* const matrix)
|
|
{
|
|
product->x = vector->x * matrix->r1c1 + vector->y * matrix->r2c1;
|
|
product->y = vector->x * matrix->r1c2 + vector->y * matrix->r2c2;
|
|
product->z = vector->x * matrix->r1c3 + vector->y * matrix->r2c3;
|
|
}
|
|
|
|
// =================== Divide =================== //
|
|
|
|
inline int bgc_fp32_vector2_divide_by_real_number(BGC_FP32_Vector2* const quotient, const BGC_FP32_Vector2* const dividend, const float divisor)
|
|
{
|
|
if (bgc_fp32_is_zero(divisor) || isnan(divisor)) {
|
|
return BGC_FAILURE;
|
|
}
|
|
|
|
bgc_fp32_vector2_multiply_by_real_number(quotient, dividend, 1.0f / divisor);
|
|
|
|
return BGC_SUCCESS;
|
|
}
|
|
|
|
inline int bgc_fp64_vector2_divide_by_real_number(BGC_FP64_Vector2* const quotient, const BGC_FP64_Vector2* const dividend, const double divisor)
|
|
{
|
|
if (bgc_fp64_is_zero(divisor) || isnan(divisor)) {
|
|
return BGC_FAILURE;
|
|
}
|
|
|
|
bgc_fp64_vector2_multiply_by_real_number(quotient, dividend, 1.0 / divisor);
|
|
|
|
return BGC_SUCCESS;
|
|
}
|
|
|
|
// ================ Mean of Two ================= //
|
|
|
|
inline void bgc_fp32_vector2_get_mean2(BGC_FP32_Vector2* const mean, const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2)
|
|
{
|
|
mean->x = (vector1->x + vector2->x) * 0.5f;
|
|
mean->y = (vector1->y + vector2->y) * 0.5f;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_get_mean2(BGC_FP64_Vector2* const mean, const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2)
|
|
{
|
|
mean->x = (vector1->x + vector2->x) * 0.5;
|
|
mean->y = (vector1->y + vector2->y) * 0.5;
|
|
}
|
|
|
|
// =============== Mean of Three ================ //
|
|
|
|
inline void bgc_fp32_vector2_get_mean3(BGC_FP32_Vector2* const mean, const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2, const BGC_FP32_Vector2* const vector3)
|
|
{
|
|
mean->x = (vector1->x + vector2->x + vector3->x) * BGC_FP32_ONE_THIRD;
|
|
mean->y = (vector1->y + vector2->y + vector3->y) * BGC_FP32_ONE_THIRD;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_get_mean3(BGC_FP64_Vector2* const mean, const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2, const BGC_FP64_Vector2* const vector3)
|
|
{
|
|
mean->x = (vector1->x + vector2->x + vector3->x) * BGC_FP64_ONE_THIRD;
|
|
mean->y = (vector1->y + vector2->y + vector3->y) * BGC_FP64_ONE_THIRD;
|
|
}
|
|
|
|
// =================== Linear =================== //
|
|
|
|
inline void bgc_fp32_vector2_interpolate(BGC_FP32_Vector2* const interpolation, const BGC_FP32_Vector2* const first, const BGC_FP32_Vector2* const second, const float phase)
|
|
{
|
|
const float counter_phase = 1.0f - phase;
|
|
|
|
interpolation->x = first->x * counter_phase + second->x * phase;
|
|
interpolation->y = first->y * counter_phase + second->y * phase;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_interpolate(BGC_FP64_Vector2* const interpolation, const BGC_FP64_Vector2* const first, const BGC_FP64_Vector2* const second, const double phase)
|
|
{
|
|
const double counter_phase = 1.0 - phase;
|
|
|
|
interpolation->x = first->x * counter_phase + second->x * phase;
|
|
interpolation->y = first->y * counter_phase + second->y * phase;
|
|
}
|
|
|
|
// ================== Negative ================== //
|
|
|
|
inline void bgc_fp32_vector2_revert(BGC_FP32_Vector2* const vector)
|
|
{
|
|
vector->x = -vector->x;
|
|
vector->y = -vector->y;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_revert(BGC_FP64_Vector2* const vector)
|
|
{
|
|
vector->x = -vector->x;
|
|
vector->y = -vector->y;
|
|
}
|
|
|
|
inline void bgc_fp32_vector2_get_reverse(BGC_FP32_Vector2* const reverse, const BGC_FP32_Vector2* const vector)
|
|
{
|
|
reverse->x = -vector->x;
|
|
reverse->y = -vector->y;
|
|
}
|
|
|
|
inline void bgc_fp64_vector2_get_reverse(BGC_FP64_Vector2* const reverse, const BGC_FP64_Vector2* const vector)
|
|
{
|
|
reverse->x = -vector->x;
|
|
reverse->y = -vector->y;
|
|
}
|
|
|
|
// ================= Normalize ================== //
|
|
|
|
inline int bgc_fp32_vector2_normalize(BGC_FP32_Vector2* const vector)
|
|
{
|
|
const float square_modulus = bgc_fp32_vector2_get_squared_length(vector);
|
|
|
|
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
|
return BGC_FAILURE;
|
|
}
|
|
|
|
if (bgc_fp32_is_square_unit(square_modulus)) {
|
|
return BGC_SUCCESS;
|
|
}
|
|
|
|
const float multiplier = sqrtf(1.0f / square_modulus);
|
|
|
|
vector->x *= multiplier;
|
|
vector->y *= multiplier;
|
|
|
|
return BGC_SUCCESS;
|
|
}
|
|
|
|
inline int bgc_fp64_vector2_normalize(BGC_FP64_Vector2* const vector)
|
|
{
|
|
const double square_modulus = bgc_fp64_vector2_get_squared_length(vector);
|
|
|
|
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
|
return BGC_FAILURE;
|
|
}
|
|
|
|
if (bgc_fp64_is_square_unit(square_modulus)) {
|
|
return BGC_SUCCESS;
|
|
}
|
|
|
|
const double multiplier = sqrt(1.0 / square_modulus);
|
|
|
|
vector->x *= multiplier;
|
|
vector->y *= multiplier;
|
|
|
|
return BGC_SUCCESS;
|
|
}
|
|
|
|
inline int bgc_fp32_vector2_get_normalized(BGC_FP32_Vector2* const normalized, const BGC_FP32_Vector2* const vector)
|
|
{
|
|
const float square_modulus = bgc_fp32_vector2_get_squared_length(vector);
|
|
|
|
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
|
bgc_fp32_vector2_reset(normalized);
|
|
return BGC_FAILURE;
|
|
}
|
|
|
|
if (bgc_fp32_is_square_unit(square_modulus)) {
|
|
bgc_fp32_vector2_copy(normalized, vector);
|
|
return BGC_SUCCESS;
|
|
}
|
|
|
|
bgc_fp32_vector2_multiply_by_real_number(normalized, vector, sqrtf(1.0f / square_modulus));
|
|
return BGC_SUCCESS;
|
|
}
|
|
|
|
inline int bgc_fp64_vector2_get_normalized(BGC_FP64_Vector2* const normalized, const BGC_FP64_Vector2* const vector)
|
|
{
|
|
const double square_modulus = bgc_fp64_vector2_get_squared_length(vector);
|
|
|
|
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
|
bgc_fp64_vector2_reset(normalized);
|
|
return BGC_FAILURE;
|
|
}
|
|
|
|
if (bgc_fp64_is_square_unit(square_modulus)) {
|
|
bgc_fp64_vector2_copy(normalized, vector);
|
|
return BGC_SUCCESS;
|
|
}
|
|
|
|
bgc_fp64_vector2_multiply_by_real_number(normalized, vector, sqrt(1.0 / square_modulus));
|
|
return BGC_SUCCESS;
|
|
}
|
|
|
|
// ============= Get Scalar Product ============= //
|
|
|
|
inline float bgc_fp32_vector2_get_dot_product(const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2)
|
|
{
|
|
return vector1->x * vector2->x + vector1->y * vector2->y;
|
|
}
|
|
|
|
inline double bgc_fp64_vector2_get_dot_product(const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2)
|
|
{
|
|
return vector1->x * vector2->x + vector1->y * vector2->y;
|
|
}
|
|
|
|
// ============= Get Cross Product ============== //
|
|
|
|
inline float bgc_fp32_vector2_get_cross_product(const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2)
|
|
{
|
|
return vector1->x * vector2->y - vector1->y * vector2->x;
|
|
}
|
|
|
|
inline double bgc_fp64_vector2_get_cross_product(const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2)
|
|
{
|
|
return vector1->x * vector2->y - vector1->y * vector2->x;
|
|
}
|
|
|
|
// ================= Get Angle ================== //
|
|
|
|
float bgc_fp32_vector2_get_angle(const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2, const int angle_unit);
|
|
|
|
double bgc_fp64_vector2_get_angle(const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2, const int angle_unit);
|
|
|
|
// ============= Get Square Distance ============ //
|
|
|
|
inline float bgc_fp32_vector2_get_square_distance(const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2)
|
|
{
|
|
const float dx = vector1->x - vector2->x;
|
|
const float dy = vector1->y - vector2->y;
|
|
|
|
return dx * dx + dy * dy;
|
|
}
|
|
|
|
inline double bgc_fp64_vector2_get_square_distance(const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2)
|
|
{
|
|
const double dx = vector1->x - vector2->x;
|
|
const double dy = vector1->y - vector2->y;
|
|
|
|
return dx * dx + dy * dy;
|
|
}
|
|
|
|
// ================== Distance ================== //
|
|
|
|
inline float bgc_fp32_vector2_get_distance(const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2)
|
|
{
|
|
return sqrtf(bgc_fp32_vector2_get_square_distance(vector1, vector2));
|
|
}
|
|
|
|
inline double bgc_fp64_vector2_get_distance(const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2)
|
|
{
|
|
return sqrt(bgc_fp64_vector2_get_square_distance(vector1, vector2));
|
|
}
|
|
|
|
// ============== Are Close Enough ============== //
|
|
|
|
inline int bgc_fp32_vector2_are_close_enough(const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2, const float distance_limit)
|
|
{
|
|
return bgc_fp32_vector2_get_square_distance(vector1, vector2) <= distance_limit * distance_limit;
|
|
}
|
|
|
|
inline int bgc_fp64_vector2_are_close_enough(const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2, const double distance_limit)
|
|
{
|
|
return bgc_fp64_vector2_get_square_distance(vector1, vector2) <= distance_limit * distance_limit;
|
|
}
|
|
|
|
// ================== Are Close ================= //
|
|
|
|
inline int bgc_fp32_vector2_are_close(const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2)
|
|
{
|
|
const float square_modulus1 = bgc_fp32_vector2_get_squared_length(vector1);
|
|
const float square_modulus2 = bgc_fp32_vector2_get_squared_length(vector2);
|
|
const float square_distance = bgc_fp32_vector2_get_square_distance(vector1, vector2);
|
|
|
|
if (square_modulus1 <= BGC_FP32_EPSILON_EFFECTIVENESS_LIMIT || square_modulus2 <= BGC_FP32_EPSILON_EFFECTIVENESS_LIMIT) {
|
|
return square_distance <= BGC_FP32_SQUARE_EPSILON;
|
|
}
|
|
|
|
return square_distance <= BGC_FP32_SQUARE_EPSILON * square_modulus1 && square_distance <= BGC_FP32_SQUARE_EPSILON * square_modulus2;
|
|
}
|
|
|
|
inline int bgc_fp64_vector2_are_close(const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2)
|
|
{
|
|
const double square_modulus1 = bgc_fp64_vector2_get_squared_length(vector1);
|
|
const double square_modulus2 = bgc_fp64_vector2_get_squared_length(vector2);
|
|
const double square_distance = bgc_fp64_vector2_get_square_distance(vector1, vector2);
|
|
|
|
if (square_modulus1 <= BGC_FP64_EPSILON_EFFECTIVENESS_LIMIT || square_modulus2 <= BGC_FP64_EPSILON_EFFECTIVENESS_LIMIT) {
|
|
return square_distance <= BGC_FP64_SQUARE_EPSILON;
|
|
}
|
|
|
|
return square_distance <= BGC_FP64_SQUARE_EPSILON * square_modulus1 && square_distance <= BGC_FP64_SQUARE_EPSILON * square_modulus2;
|
|
}
|
|
|
|
|
|
// ================== Parallel ================== //
|
|
|
|
inline int bgc_fp32_vector2_are_parallel(const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2)
|
|
{
|
|
const float square_modulus1 = bgc_fp32_vector2_get_squared_length(vector1);
|
|
|
|
if (square_modulus1 <= BGC_FP32_SQUARE_EPSILON) {
|
|
return 1;
|
|
}
|
|
|
|
const float square_modulus2 = bgc_fp32_vector2_get_squared_length(vector2);
|
|
|
|
if (square_modulus2 <= BGC_FP32_SQUARE_EPSILON) {
|
|
return 1;
|
|
}
|
|
|
|
const float cross_product = bgc_fp32_vector2_get_cross_product(vector1, vector2);
|
|
|
|
return cross_product * cross_product <= BGC_FP32_SQUARE_EPSILON * square_modulus1 * square_modulus2;
|
|
}
|
|
|
|
inline int bgc_fp64_vector2_are_parallel(const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2)
|
|
{
|
|
const double square_modulus1 = bgc_fp64_vector2_get_squared_length(vector1);
|
|
|
|
if (square_modulus1 <= BGC_FP64_SQUARE_EPSILON) {
|
|
return 1;
|
|
}
|
|
|
|
const double square_modulus2 = bgc_fp64_vector2_get_squared_length(vector2);
|
|
|
|
if (square_modulus2 <= BGC_FP64_SQUARE_EPSILON) {
|
|
return 1;
|
|
}
|
|
|
|
const double cross_product = bgc_fp64_vector2_get_cross_product(vector1, vector2);
|
|
|
|
return cross_product * cross_product <= BGC_FP64_SQUARE_EPSILON * square_modulus1 * square_modulus2;
|
|
}
|
|
|
|
// ================= Orthogonal ================= //
|
|
|
|
inline int bgc_fp32_vector2_are_orthogonal(const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2)
|
|
{
|
|
const float square_modulus1 = bgc_fp32_vector2_get_squared_length(vector1);
|
|
|
|
if (square_modulus1 <= BGC_FP32_SQUARE_EPSILON) {
|
|
return 1;
|
|
}
|
|
|
|
const float square_modulus2 = bgc_fp32_vector2_get_squared_length(vector2);
|
|
|
|
if (square_modulus2 <= BGC_FP32_SQUARE_EPSILON) {
|
|
return 1;
|
|
}
|
|
|
|
const float scalar_product = bgc_fp32_vector2_get_dot_product(vector1, vector2);
|
|
|
|
return scalar_product * scalar_product <= BGC_FP32_SQUARE_EPSILON * square_modulus1 * square_modulus2;
|
|
}
|
|
|
|
inline int bgc_fp64_vector2_are_orthogonal(const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2)
|
|
{
|
|
const double square_modulus1 = bgc_fp64_vector2_get_squared_length(vector1);
|
|
|
|
if (square_modulus1 <= BGC_FP64_SQUARE_EPSILON) {
|
|
return 1;
|
|
}
|
|
|
|
const double square_modulus2 = bgc_fp64_vector2_get_squared_length(vector2);
|
|
|
|
if (square_modulus2 <= BGC_FP64_SQUARE_EPSILON) {
|
|
return 1;
|
|
}
|
|
|
|
const double scalar_product = bgc_fp64_vector2_get_dot_product(vector1, vector2);
|
|
|
|
return scalar_product * scalar_product <= BGC_FP64_SQUARE_EPSILON * square_modulus1 * square_modulus2;
|
|
}
|
|
|
|
|
|
// ================== Attitude ================== //
|
|
|
|
inline int bgc_fp32_vector2_get_attitude(const BGC_FP32_Vector2* const vector1, const BGC_FP32_Vector2* const vector2)
|
|
{
|
|
const float square_modulus1 = bgc_fp32_vector2_get_squared_length(vector1);
|
|
const float square_modulus2 = bgc_fp32_vector2_get_squared_length(vector2);
|
|
|
|
if (square_modulus1 <= BGC_FP32_SQUARE_EPSILON || square_modulus2 <= BGC_FP32_SQUARE_EPSILON) {
|
|
return BGC_ATTITUDE_ZERO;
|
|
}
|
|
|
|
const float square_limit = BGC_FP32_SQUARE_EPSILON * square_modulus1 * square_modulus2;
|
|
|
|
const float scalar_product = bgc_fp32_vector2_get_dot_product(vector1, vector2);
|
|
|
|
if (scalar_product * scalar_product <= square_limit) {
|
|
return BGC_ATTITUDE_ORTHOGONAL;
|
|
}
|
|
|
|
const float cross_product = bgc_fp32_vector2_get_cross_product(vector1, vector2);
|
|
|
|
if (cross_product * cross_product > square_limit) {
|
|
return BGC_ATTITUDE_ANY;
|
|
}
|
|
|
|
return scalar_product > 0.0f ? BGC_ATTITUDE_CO_DIRECTIONAL : BGC_ATTITUDE_COUNTER_DIRECTIONAL;
|
|
}
|
|
|
|
inline int bgc_fp64_vector2_get_attitude(const BGC_FP64_Vector2* const vector1, const BGC_FP64_Vector2* const vector2)
|
|
{
|
|
const double square_modulus1 = bgc_fp64_vector2_get_squared_length(vector1);
|
|
const double square_modulus2 = bgc_fp64_vector2_get_squared_length(vector2);
|
|
|
|
if (square_modulus1 <= BGC_FP64_SQUARE_EPSILON || square_modulus2 <= BGC_FP64_SQUARE_EPSILON) {
|
|
return BGC_ATTITUDE_ZERO;
|
|
}
|
|
|
|
const double square_limit = BGC_FP64_SQUARE_EPSILON * square_modulus1 * square_modulus2;
|
|
|
|
const double scalar_product = bgc_fp64_vector2_get_dot_product(vector1, vector2);
|
|
|
|
if (scalar_product * scalar_product <= square_limit) {
|
|
return BGC_ATTITUDE_ORTHOGONAL;
|
|
}
|
|
|
|
const double cross_product = bgc_fp64_vector2_get_cross_product(vector1, vector2);
|
|
|
|
if (cross_product * cross_product > square_limit) {
|
|
return BGC_ATTITUDE_ANY;
|
|
}
|
|
|
|
return scalar_product > 0.0 ? BGC_ATTITUDE_CO_DIRECTIONAL : BGC_ATTITUDE_COUNTER_DIRECTIONAL;
|
|
}
|
|
|
|
#endif
|