557 lines
12 KiB
C
557 lines
12 KiB
C
#ifndef _BASIC_GEOMETRY_ANGLE_H_
|
|
#define _BASIC_GEOMETRY_ANGLE_H_
|
|
|
|
#include <math.h>
|
|
#include "basis.h"
|
|
|
|
#define FP32_PI 3.1415926536f
|
|
#define FP32_TWO_PI 6.2831853072f
|
|
#define FP32_HALF_OF_PI 1.5707963268f
|
|
#define FP32_THIRD_OF_PI 1.0471975512f
|
|
#define FP32_FOURTH_OF_PI 0.7853981634f
|
|
#define FP32_SIXTH_OF_PI 0.5235987756f
|
|
|
|
#define FP32_DEGREES_IN_RADIAN 57.295779513f
|
|
#define FP32_TURNS_IN_RADIAN 0.1591549431f
|
|
#define FP32_RADIANS_IN_DEGREE 1.745329252E-2f
|
|
#define FP32_TURNS_IN_DEGREE 2.7777777778E-3f
|
|
|
|
#define FP64_PI 3.14159265358979324
|
|
#define FP64_TWO_PI 6.28318530717958648
|
|
#define FP64_HALF_OF_PI 1.57079632679489662
|
|
#define FP64_THIRD_OF_PI 1.04719755119659775
|
|
#define FP64_FOURTH_OF_PI 0.78539816339744831
|
|
#define FP64_SIXTH_OF_PI 0.523598775598298873
|
|
|
|
#define FP64_DEGREES_IN_RADIAN 57.2957795130823209
|
|
#define FP64_TURNS_IN_RADIAN 0.159154943091895336
|
|
#define FP64_RADIANS_IN_DEGREE 1.74532925199432958E-2
|
|
#define FP64_TURNS_IN_DEGREE 2.77777777777777778E-3
|
|
|
|
typedef enum {
|
|
BG_ANGLE_UNIT_RADIANS = 1,
|
|
BG_ANGLE_UNIT_DEGREES = 2,
|
|
BG_ANGLE_UNIT_TURNS = 3
|
|
} angle_unit_t;
|
|
|
|
typedef enum {
|
|
/**
|
|
* The measure of an angle with a range of:
|
|
* [0, 360) degrees, [0, 2xPI) radians, [0, 1) turns, [0, 400) gradians
|
|
*/
|
|
BG_ANGLE_RANGE_UNSIGNED = 1,
|
|
|
|
/**
|
|
* The measure of an angle with a range of:
|
|
* (-180, 180] degrees, (-PI, PI] radians, (-0.5, 0.5] turns, (-200, 200] gradians
|
|
*/
|
|
BG_ANGLE_RANGE_SIGNED = 2
|
|
} angle_range_t;
|
|
|
|
// !================= Radians ==================! //
|
|
|
|
// ========= Convert radians to degrees ========= //
|
|
|
|
inline float fp32_radians_to_degrees(const float radians)
|
|
{
|
|
return radians * FP32_DEGREES_IN_RADIAN;
|
|
}
|
|
|
|
inline double fp64_radians_to_degrees(const double radians)
|
|
{
|
|
return radians * FP64_DEGREES_IN_RADIAN;
|
|
}
|
|
|
|
// ========== Convert radians to turns ========== //
|
|
|
|
inline float fp32_radians_to_turns(const float radians)
|
|
{
|
|
return radians * FP32_TURNS_IN_RADIAN;
|
|
}
|
|
|
|
inline double fp64_radians_to_turns(const double radians)
|
|
{
|
|
return radians * FP64_TURNS_IN_RADIAN;
|
|
}
|
|
|
|
// ========= Convert radians to any unit ======== //
|
|
|
|
inline float fp32_radians_to_units(const float radians, const angle_unit_t to_unit)
|
|
{
|
|
if (to_unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return radians * FP32_DEGREES_IN_RADIAN;
|
|
}
|
|
|
|
if (to_unit == BG_ANGLE_UNIT_TURNS) {
|
|
return radians * FP32_TURNS_IN_RADIAN;
|
|
}
|
|
|
|
return radians;
|
|
}
|
|
|
|
inline double fp64_radians_to_units(const double radians, const angle_unit_t to_unit)
|
|
{
|
|
if (to_unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return radians * FP64_DEGREES_IN_RADIAN;
|
|
}
|
|
|
|
if (to_unit == BG_ANGLE_UNIT_TURNS) {
|
|
return radians * FP64_TURNS_IN_RADIAN;
|
|
}
|
|
|
|
return radians;
|
|
}
|
|
|
|
// ============ Normalize radians ============= //
|
|
|
|
inline float fp32_radians_normalize(const float radians, const angle_range_t range)
|
|
{
|
|
if (range == BG_ANGLE_RANGE_UNSIGNED) {
|
|
if (0.0f <= radians && radians < FP32_TWO_PI) {
|
|
return radians;
|
|
}
|
|
}
|
|
else {
|
|
if (-FP32_PI < radians && radians <= FP32_PI) {
|
|
return radians;
|
|
}
|
|
}
|
|
|
|
float turns = radians * FP32_TURNS_IN_RADIAN;
|
|
|
|
turns -= floorf(turns);
|
|
|
|
if (range == BG_ANGLE_RANGE_SIGNED && turns > 0.5f) {
|
|
turns -= 1.0f;
|
|
}
|
|
|
|
return turns * FP32_TWO_PI;
|
|
}
|
|
|
|
inline double fp64_radians_normalize(const double radians, const angle_range_t range)
|
|
{
|
|
if (range == BG_ANGLE_RANGE_UNSIGNED) {
|
|
if (0.0 <= radians && radians < FP64_TWO_PI) {
|
|
return radians;
|
|
}
|
|
}
|
|
else {
|
|
if (-FP64_PI < radians && radians <= FP64_PI) {
|
|
return radians;
|
|
}
|
|
}
|
|
|
|
double turns = radians * FP64_TURNS_IN_RADIAN;
|
|
|
|
turns -= floor(turns);
|
|
|
|
if (range == BG_ANGLE_RANGE_SIGNED && turns > 0.5) {
|
|
turns -= 1.0;
|
|
}
|
|
|
|
return turns * FP64_TWO_PI;
|
|
}
|
|
|
|
// !================= Degrees ==================! //
|
|
|
|
// ========= Convert degrees to radians ========= //
|
|
|
|
inline float fp32_degrees_to_radians(const float degrees)
|
|
{
|
|
return degrees * FP32_RADIANS_IN_DEGREE;
|
|
}
|
|
|
|
inline double fp64_degrees_to_radians(const double degrees)
|
|
{
|
|
return degrees * FP64_RADIANS_IN_DEGREE;
|
|
}
|
|
|
|
// ========== Convert degrees to turns ========== //
|
|
|
|
inline float fp32_degrees_to_turns(const float radians)
|
|
{
|
|
return radians * FP32_TURNS_IN_DEGREE;
|
|
}
|
|
|
|
inline double fp64_degrees_to_turns(const double radians)
|
|
{
|
|
return radians * FP64_TURNS_IN_DEGREE;
|
|
}
|
|
|
|
// ========= Convert degreess to any unit ======== //
|
|
|
|
inline float fp32_degrees_to_units(const float degrees, const angle_unit_t to_unit)
|
|
{
|
|
if (to_unit == BG_ANGLE_UNIT_RADIANS) {
|
|
return degrees * FP32_RADIANS_IN_DEGREE;
|
|
}
|
|
|
|
if (to_unit == BG_ANGLE_UNIT_TURNS) {
|
|
return degrees * FP32_TURNS_IN_DEGREE;
|
|
}
|
|
|
|
return degrees;
|
|
}
|
|
|
|
inline double fp64_degrees_to_units(const double degrees, const angle_unit_t to_unit)
|
|
{
|
|
if (to_unit == BG_ANGLE_UNIT_RADIANS) {
|
|
return degrees * FP64_RADIANS_IN_DEGREE;
|
|
}
|
|
|
|
if (to_unit == BG_ANGLE_UNIT_TURNS) {
|
|
return degrees * FP64_TURNS_IN_DEGREE;
|
|
}
|
|
|
|
return degrees;
|
|
}
|
|
|
|
// ============ Normalize degrees ============= //
|
|
|
|
inline float fp32_degrees_normalize(const float degrees, const angle_range_t range)
|
|
{
|
|
if (range == BG_ANGLE_RANGE_UNSIGNED) {
|
|
if (0.0f <= degrees && degrees < 360.0f) {
|
|
return degrees;
|
|
}
|
|
}
|
|
else {
|
|
if (-180.0f < degrees && degrees <= 180.0f) {
|
|
return degrees;
|
|
}
|
|
}
|
|
|
|
float turns = degrees * FP32_TURNS_IN_DEGREE;
|
|
|
|
turns -= floorf(turns);
|
|
|
|
if (range == BG_ANGLE_RANGE_SIGNED && turns > 0.5f) {
|
|
turns -= 1.0f;
|
|
}
|
|
|
|
return turns * 360.0f;
|
|
}
|
|
|
|
inline double fp64_degrees_normalize(const double degrees, const angle_range_t range)
|
|
{
|
|
if (range == BG_ANGLE_RANGE_UNSIGNED) {
|
|
if (0.0 <= degrees && degrees < 360.0) {
|
|
return degrees;
|
|
}
|
|
}
|
|
else {
|
|
if (-180.0 < degrees && degrees <= 180.0) {
|
|
return degrees;
|
|
}
|
|
}
|
|
|
|
double turns = degrees * FP64_TURNS_IN_DEGREE;
|
|
|
|
turns -= floor(turns);
|
|
|
|
if (range == BG_ANGLE_RANGE_SIGNED && turns > 0.5) {
|
|
turns -= 1.0;
|
|
}
|
|
|
|
return turns * 360.0;
|
|
}
|
|
|
|
// !================== Turns ===================! //
|
|
|
|
// ========== Convert turns to radians ========== //
|
|
|
|
inline float fp32_turns_to_radians(const float turns)
|
|
{
|
|
return turns * FP32_TWO_PI;
|
|
}
|
|
|
|
inline double fp64_turns_to_radians(const double turns)
|
|
{
|
|
return turns * FP64_TWO_PI;
|
|
}
|
|
|
|
// ========== Convert turns to degrees ========== //
|
|
|
|
inline float fp32_turns_to_degrees(const float turns)
|
|
{
|
|
return turns * 360.0f;
|
|
}
|
|
|
|
inline double fp64_turns_to_degrees(const double turns)
|
|
{
|
|
return turns * 360.0;
|
|
}
|
|
|
|
// ========= Convert turns to any unit ======== //
|
|
|
|
inline float fp32_turns_to_units(const float turns, const angle_unit_t to_unit)
|
|
{
|
|
if (to_unit == BG_ANGLE_UNIT_RADIANS) {
|
|
return turns * FP32_TWO_PI;
|
|
}
|
|
|
|
if (to_unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return turns * 360.0f;
|
|
}
|
|
|
|
return turns;
|
|
}
|
|
|
|
inline double fp64_turns_to_units(const double turns, const angle_unit_t to_unit)
|
|
{
|
|
if (to_unit == BG_ANGLE_UNIT_RADIANS) {
|
|
return turns * FP64_TWO_PI;
|
|
}
|
|
|
|
if (to_unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return turns * 360.0;
|
|
}
|
|
|
|
return turns;
|
|
}
|
|
|
|
// ============= Normalize turns ============== //
|
|
|
|
inline float fp32_turns_normalize(const float turns, const angle_range_t range)
|
|
{
|
|
if (range == BG_ANGLE_RANGE_UNSIGNED) {
|
|
if (0.0f <= turns && turns < 1.0f) {
|
|
return turns;
|
|
}
|
|
}
|
|
else {
|
|
if (-0.5f < turns && turns <= 0.5f) {
|
|
return turns;
|
|
}
|
|
}
|
|
|
|
float rest = turns - floorf(turns);
|
|
|
|
if (range == BG_ANGLE_RANGE_SIGNED && rest > 0.5f) {
|
|
return rest - 1.0f;
|
|
}
|
|
|
|
return rest;
|
|
}
|
|
|
|
inline double fp64_turns_normalize(const double turns, const angle_range_t range)
|
|
{
|
|
if (range == BG_ANGLE_RANGE_UNSIGNED) {
|
|
if (0.0 <= turns && turns < 1.0) {
|
|
return turns;
|
|
}
|
|
}
|
|
else {
|
|
if (-0.5 < turns && turns <= 0.5) {
|
|
return turns;
|
|
}
|
|
}
|
|
|
|
double rest = turns - floor(turns);
|
|
|
|
if (range == BG_ANGLE_RANGE_SIGNED && rest > 0.5) {
|
|
return rest - 1.0;
|
|
}
|
|
|
|
return rest;
|
|
}
|
|
|
|
// !================== Angle ===================! //
|
|
|
|
// ========= Convert any unit to radians ======== //
|
|
|
|
inline float fp32_angle_to_radians(const float angle, const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return angle * FP32_RADIANS_IN_DEGREE;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return angle * FP32_TWO_PI;
|
|
}
|
|
|
|
return angle;
|
|
}
|
|
|
|
inline double fp64_angle_to_radians(const double angle, const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return angle * FP64_RADIANS_IN_DEGREE;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return angle * FP64_TWO_PI;
|
|
}
|
|
|
|
return angle;
|
|
}
|
|
|
|
// ========= Convert any unit to degreess ======== //
|
|
|
|
inline float fp32_angle_to_degrees(const float angle, const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_RADIANS) {
|
|
return angle * FP32_DEGREES_IN_RADIAN;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return angle * 360.0f;
|
|
}
|
|
|
|
return angle;
|
|
}
|
|
|
|
inline double fp64_angle_to_degrees(const double angle, const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_RADIANS) {
|
|
return angle * FP64_DEGREES_IN_RADIAN;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return angle * 360.0;
|
|
}
|
|
|
|
return angle;
|
|
}
|
|
|
|
// ========= Convert any unit to turns ======== //
|
|
|
|
inline float fp32_angle_to_turns(const float angle, const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_RADIANS) {
|
|
return angle * FP32_TURNS_IN_RADIAN;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return angle * FP32_TURNS_IN_DEGREE;
|
|
}
|
|
|
|
return angle;
|
|
}
|
|
|
|
inline double fp64_angle_to_turns(const double angle, const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_RADIANS) {
|
|
return angle * FP64_TURNS_IN_RADIAN;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return angle * FP64_TURNS_IN_DEGREE;
|
|
}
|
|
|
|
return angle;
|
|
}
|
|
|
|
// ============= Get Full Circle ============== //
|
|
|
|
inline float fp32_angle_get_full_circle(const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return 360.0f;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return 1.0f;
|
|
}
|
|
|
|
return FP32_TWO_PI;
|
|
}
|
|
|
|
inline double fp64_angle_get_full_circle(const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return 360.0;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return 1.0;
|
|
}
|
|
|
|
return FP64_TWO_PI;
|
|
}
|
|
|
|
// ============= Get Half Circle ============== //
|
|
|
|
inline float fp32_angle_get_half_circle(const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return 180.0f;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return 0.5f;
|
|
}
|
|
|
|
return FP32_PI;
|
|
}
|
|
|
|
inline double fp64_angle_get_half_circle(const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return 180.0;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return 0.5;
|
|
}
|
|
|
|
return FP64_PI;
|
|
}
|
|
|
|
// ============= Get Half Circle ============== //
|
|
|
|
inline float fp32_angle_get_quater_circle(const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return 90.0f;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return 0.25f;
|
|
}
|
|
|
|
return FP32_HALF_OF_PI;
|
|
}
|
|
|
|
inline double fp64_angle_get_quater_circle(const angle_unit_t unit)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return 90.0;
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return 0.25;
|
|
}
|
|
|
|
return FP64_HALF_OF_PI;
|
|
}
|
|
|
|
// ================ Normalize ================= //
|
|
|
|
inline float fp32_angle_normalize(const float angle, const angle_unit_t unit, const angle_range_t range)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return fp32_degrees_normalize(angle, range);
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return fp32_turns_normalize(angle, range);
|
|
}
|
|
|
|
return fp32_radians_normalize(angle, range);
|
|
}
|
|
|
|
inline double fp64_angle_normalize(const double angle, const angle_unit_t unit, const angle_range_t range)
|
|
{
|
|
if (unit == BG_ANGLE_UNIT_DEGREES) {
|
|
return fp64_degrees_normalize(angle, range);
|
|
}
|
|
|
|
if (unit == BG_ANGLE_UNIT_TURNS) {
|
|
return fp64_turns_normalize(angle, range);
|
|
}
|
|
|
|
return fp64_radians_normalize(angle, range);
|
|
}
|
|
|
|
#endif
|