#ifndef _BGC_ANGLE_H_ #define _BGC_ANGLE_H_ #include #include "utilities.h" #define BGC_FP32_PI 3.1415926536f #define BGC_FP32_TWO_PI 6.2831853072f #define BGC_FP32_HALF_OF_PI 1.5707963268f #define BGC_FP32_ONE_THIRD_OF_PI 1.0471975512f #define BGC_FP32_ONE_FOURTH_OF_PI 0.7853981634f #define BGC_FP32_ONE_SIXTH_OF_PI 0.5235987756f #define BGC_FP32_DEGREES_IN_RADIAN 57.295779513f #define BGC_FP32_TURNS_IN_RADIAN 0.1591549431f #define BGC_FP32_RADIANS_IN_DEGREE 1.745329252E-2f #define BGC_FP32_TURNS_IN_DEGREE 2.7777777778E-3f #define BGC_FP64_PI 3.14159265358979324 #define BGC_FP64_TWO_PI 6.28318530717958648 #define BGC_FP64_HALF_OF_PI 1.57079632679489662 #define BGC_FP64_ONE_THIRD_OF_PI 1.04719755119659775 #define BGC_FP64_ONE_FOURTH_OF_PI 0.78539816339744831 #define BGC_FP64_ONE_SIXTH_OF_PI 0.523598775598298873 #define BGC_FP64_DEGREES_IN_RADIAN 57.2957795130823209 #define BGC_FP64_TURNS_IN_RADIAN 0.159154943091895336 #define BGC_FP64_RADIANS_IN_DEGREE 1.74532925199432958E-2 #define BGC_FP64_TURNS_IN_DEGREE 2.77777777777777778E-3 #define BGC_ANGLE_UNIT_RADIANS 1 #define BGC_ANGLE_UNIT_DEGREES 2 #define BGC_ANGLE_UNIT_TURNS 3 /** * The measure of an angle with a range of: * [0, 360) degrees, [0, 2xPI) radians, [0, 1) turns, [0, 400) gradians */ #define BGC_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 */ #define BGC_ANGLE_RANGE_SIGNED 2 // !================= Radians ==================! // // ========= Convert radians to degrees ========= // inline float bgc_fp32_radians_to_degrees(const float radians) { return radians * BGC_FP32_DEGREES_IN_RADIAN; } inline double bgc_fp64_radians_to_degrees(const double radians) { return radians * BGC_FP64_DEGREES_IN_RADIAN; } // ========== Convert radians to turns ========== // inline float bgc_fp32_radians_to_turns(const float radians) { return radians * BGC_FP32_TURNS_IN_RADIAN; } inline double bgc_fp64_radians_to_turns(const double radians) { return radians * BGC_FP64_TURNS_IN_RADIAN; } // ========= Convert radians to any unit ======== // inline float bgc_fp32_radians_to_units(const float radians, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return radians * BGC_FP32_DEGREES_IN_RADIAN; } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return radians * BGC_FP32_TURNS_IN_RADIAN; } return radians; } inline double bgc_fp64_radians_to_units(const double radians, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return radians * BGC_FP64_DEGREES_IN_RADIAN; } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return radians * BGC_FP64_TURNS_IN_RADIAN; } return radians; } // ============ Normalize radians ============= // inline float bgc_fp32_normalize_radians(const float radians, const int angle_range) { if (angle_range == BGC_ANGLE_RANGE_UNSIGNED) { if (0.0f <= radians && radians < BGC_FP32_TWO_PI) { return radians; } } else { if (-BGC_FP32_PI < radians && radians <= BGC_FP32_PI) { return radians; } } float turns = radians * BGC_FP32_TURNS_IN_RADIAN; turns -= floorf(turns); if (angle_range == BGC_ANGLE_RANGE_SIGNED && turns > 0.5f) { turns -= 1.0f; } return turns * BGC_FP32_TWO_PI; } inline double bgc_fp64_normalize_radians(const double radians, const int angle_range) { if (angle_range == BGC_ANGLE_RANGE_UNSIGNED) { if (0.0 <= radians && radians < BGC_FP64_TWO_PI) { return radians; } } else { if (-BGC_FP64_PI < radians && radians <= BGC_FP64_PI) { return radians; } } double turns = radians * BGC_FP64_TURNS_IN_RADIAN; turns -= floor(turns); if (angle_range == BGC_ANGLE_RANGE_SIGNED && turns > 0.5) { turns -= 1.0; } return turns * BGC_FP64_TWO_PI; } // !================= Degrees ==================! // // ========= Convert degrees to radians ========= // inline float bgc_fp32_degrees_to_radians(const float degrees) { return degrees * BGC_FP32_RADIANS_IN_DEGREE; } inline double bgc_fp64_degrees_to_radians(const double degrees) { return degrees * BGC_FP64_RADIANS_IN_DEGREE; } // ========== Convert degrees to turns ========== // inline float bgc_fp32_degrees_to_turns(const float radians) { return radians * BGC_FP32_TURNS_IN_DEGREE; } inline double bgc_fp64_degrees_to_turns(const double radians) { return radians * BGC_FP64_TURNS_IN_DEGREE; } // ========= Convert degreess to any unit ======== // inline float bgc_fp32_degrees_to_units(const float degrees, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_RADIANS) { return degrees * BGC_FP32_RADIANS_IN_DEGREE; } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return degrees * BGC_FP32_TURNS_IN_DEGREE; } return degrees; } inline double bgc_fp64_degrees_to_units(const double degrees, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_RADIANS) { return degrees * BGC_FP64_RADIANS_IN_DEGREE; } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return degrees * BGC_FP64_TURNS_IN_DEGREE; } return degrees; } // ============ Normalize degrees ============= // inline float bgc_fp32_normalize_degrees(const float degrees, const int angle_range) { if (angle_range == BGC_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 * BGC_FP32_TURNS_IN_DEGREE; turns -= floorf(turns); if (angle_range == BGC_ANGLE_RANGE_SIGNED && turns > 0.5f) { turns -= 1.0f; } return turns * 360.0f; } inline double bgc_fp64_degrees_normalize(const double degrees, const int angle_range) { if (angle_range == BGC_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 * BGC_FP64_TURNS_IN_DEGREE; turns -= floor(turns); if (angle_range == BGC_ANGLE_RANGE_SIGNED && turns > 0.5) { turns -= 1.0; } return turns * 360.0; } // !================== Turns ===================! // // ========== Convert turns to radians ========== // inline float bgc_fp32_turns_to_radians(const float turns) { return turns * BGC_FP32_TWO_PI; } inline double bgc_fp64_turns_to_radians(const double turns) { return turns * BGC_FP64_TWO_PI; } // ========== Convert turns to degrees ========== // inline float bgc_fp32_turns_to_degrees(const float turns) { return turns * 360.0f; } inline double bgc_fp64_turns_to_degrees(const double turns) { return turns * 360.0; } // ========= Convert turns to any unit ======== // inline float bgc_fp32_turns_to_units(const float turns, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_RADIANS) { return turns * BGC_FP32_TWO_PI; } if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return turns * 360.0f; } return turns; } inline double bgc_fp64_turns_to_units(const double turns, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_RADIANS) { return turns * BGC_FP64_TWO_PI; } if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return turns * 360.0; } return turns; } // ============= Normalize turns ============== // inline float bgc_fp32_normalize_turns(const float turns, const int angle_range) { if (angle_range == BGC_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 (angle_range == BGC_ANGLE_RANGE_SIGNED && rest > 0.5f) { return rest - 1.0f; } return rest; } inline double bgc_fp64_normalize_turns(const double turns, const int angle_range) { if (angle_range == BGC_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 (angle_range == BGC_ANGLE_RANGE_SIGNED && rest > 0.5) { return rest - 1.0; } return rest; } // !================== Angle ===================! // // ========= Convert any unit to radians ======== // inline float bgc_fp32_angle_to_radians(const float angle, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return angle * BGC_FP32_RADIANS_IN_DEGREE; } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return angle * BGC_FP32_TWO_PI; } return angle; } inline double bgc_fp64_angle_to_radians(const double angle, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return angle * BGC_FP64_RADIANS_IN_DEGREE; } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return angle * BGC_FP64_TWO_PI; } return angle; } // ========= Convert any unit to degreess ======== // inline float bgc_fp32_angle_to_degrees(const float angle, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_RADIANS) { return angle * BGC_FP32_DEGREES_IN_RADIAN; } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return angle * 360.0f; } return angle; } inline double bgc_fp64_angle_to_degrees(const double angle, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_RADIANS) { return angle * BGC_FP64_DEGREES_IN_RADIAN; } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return angle * 360.0; } return angle; } // ========= Convert any unit to turns ======== // inline float bgc_fp32_angle_to_turns(const float angle, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_RADIANS) { return angle * BGC_FP32_TURNS_IN_RADIAN; } if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return angle * BGC_FP32_TURNS_IN_DEGREE; } return angle; } inline double bgc_fp64_angle_to_turns(const double angle, const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_RADIANS) { return angle * BGC_FP64_TURNS_IN_RADIAN; } if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return angle * BGC_FP64_TURNS_IN_DEGREE; } return angle; } // ============= Get Full Circle ============== // inline float bgc_fp32_full_circle(const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return 360.0f; } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return 1.0f; } return BGC_FP32_TWO_PI; } inline double bgc_fp64_full_circle(const int angle_unit) { if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return 360.0; } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return 1.0; } return BGC_FP64_TWO_PI; } // ================ Normalize ================= // inline float bgc_fp32_normalize_angle(const float angle, const int angle_unit, const int angle_range) { if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return bgc_fp32_normalize_degrees(angle, angle_range); } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return bgc_fp32_normalize_turns(angle, angle_range); } return bgc_fp32_normalize_radians(angle, angle_range); } inline double bgc_fp64_normalize_angle(const double angle, const int angle_unit, const int angle_range) { if (angle_unit == BGC_ANGLE_UNIT_DEGREES) { return bgc_fp64_degrees_normalize(angle, angle_range); } if (angle_unit == BGC_ANGLE_UNIT_TURNS) { return bgc_fp64_normalize_turns(angle, angle_range); } return bgc_fp64_normalize_radians(angle, angle_range); } #endif