From 5f52cd19b81b35aed14c058a818a64e9e6ceb922 Mon Sep 17 00:00:00 2001 From: Ravi Prakash Singh Date: Thu, 10 Feb 2022 12:39:51 +0530 Subject: [PATCH] Refactored vec3 and vec4 --- include/hpml/vec3/template_definitions.h | 726 ++++++++++---------- include/hpml/vec3/template_instantiations.h | 168 +++-- include/hpml/vec4/template_definitions.h | 163 ++++- 3 files changed, 624 insertions(+), 433 deletions(-) diff --git a/include/hpml/vec3/template_definitions.h b/include/hpml/vec3/template_definitions.h index d68c6d8..2d3ac20 100644 --- a/include/hpml/vec3/template_definitions.h +++ b/include/hpml/vec3/template_definitions.h @@ -1,174 +1,253 @@ -#ifndef __HPML_VEC3_TEMPLATE_DEFINITION_H__ -#define __HPML_VEC3_TEMPLATE_DEFINITION_H__ -#include +#pragma once + +#if defined(IMPLEMENTATION) +#include +#include +#endif + #include #include #include -#include -#include +#include #include -/*Begin: Template Definitions*/ -/*template signatures*/ +/*DATA*/ +/*vec3_t(T) struct*/ #define vec3_t(T) c_template(vec3_t, T) -#define vec3(T) c_template(vec3, T) -#define vec3_add(T) c_template(vec3_add, T) -#define vec3_sub(T) c_template(vec3_sub, T) -#define vec3_mul(T) c_template(vec3_mul, T) -#define vec3_div(T) c_template(vec3_div, T) -#define vec3_is_null(T) c_template(vec3_is_null, T) -#define vec3_is_equal(T) c_template(vec3_is_equal, T) -#define vec3_null(T) c_template(vec3_null, T) -#define vec3_zero(T) vec3_null(T) -#define vec3_print(T) c_template(vec3_print, T) -#define vec3_dot(T) c_template(vec3_dot, T) -#define vec3_cross(T) c_template(vec3_cross, T) -#define vec3_magnitude(T) c_template(vec3_magnitude, T) -#define vec3_negate(T) c_template(vec3_negate, T) -#define vec3_normalize(T) c_template(vec3_normalize, T) -#define vec3_rotate(T) c_template(vec3_rotate, T) NOT_IMPLEMENTED -#define vec3_angle(T) c_template(vec3_angle, T) -#define vec3_abs(T) c_template(vec3_abs, T) -#define vec3_up(T) c_template(vec3_up, T) -#define vec3_down(T) c_template(vec3_down, T) -#define vec3_left(T) c_template(vec3_left, T) -#define vec3_right(T) c_template(vec3_right, T) -#define vec3_forward(T) c_template(vec3_forward, T) -#define vec3_backward(T) c_template(vec3_backward, T) -#define vec3_project(T) c_template(vec3_project, T) -#define vec3_lerp(T) c_template(vec3_lerp, T) -#define vec3_slerp(T) c_template(vec3_slerp, T) -#define vec3_scale(T) c_template(vec3_scale, T) //vec3_transform(T)(vec3_t(T) v, mat33_t(T) transform) -#define vec3_transform(T) c_template(vec3_transform, T) NOT_IMPLEMENTED -#define vec3_internal_division(T) c_template(vec3_internal_division, T) -#define vec3_external_division(T) c_template(vec3_external_division, T) -#define vec3_direction_ratios(T) c_template(vec3_direction_ratios, T) NOT_IMPLEMENTED -#define vec3_box(T) c_template(vec3_box, T) NOT_IMPLEMENTED - -#define instantiate_vec3_struct(T) \ -typedef struct vec3_t(T)\ +#define instantiate_vec3_struct(T)\ +typedef struct\ {\ T x;\ T y;\ T z;\ } vec3_t(T) -#define instantiate_declaration_vec3_is_equal(T) HPML_API bool vec3_is_equal(T)(vec3_t(T) v1, vec3_t(T v2) v2) -#define instantiate_declaration_vec3_slerp(T) HPML_API vec3_t(T) vec3_slerp(T)(vec3_t(T) v1, vec3_t(T) v2, float slerp_value) -#define instantiate_declaration_vec3_add(T) HPML_API vec3_t(T) vec3_add(T)(vec3_t(T) v1, vec3_t(T) v2) -#define instantiate_declaration_vec3_sub(T) HPML_API vec3_t(T) vec3_sub(T)(vec3_t(T) v1, vec3_t(T) v2) -#define instantiate_declaration_vec3_mul(T) HPML_API vec3_t(T) vec3_mul(T)(vec3_t(T) v1, vec3_t(T) v2) -#define instantiate_declaration_vec3_div(T) HPML_API vec3_t(T) vec3_div(T)(vec3_t(T) v1, vec3_t(T) v2) -#define instantiate_declaration_vec3_is_null(T) HPML_API bool vec3_is_null(T)(vec3_t(T) v) -#define instantiate_declaration_vec3_null(T) HPML_API vec3_t(T) vec3_null(T)() -#define instantiate_declaration_vec3_zero(T) HPML_API instantiate_declaration_vec3_null(T) HPML_API -#define instantiate_declaration_vec3_print(T) HPML_API void vec3_print(T)(vec3_t(T) v) +/*CONSTRUCTOR*/ +/*vec3*/ +#define vec3(T) c_template(vec3, T) #define instantiate_declaration_vec3(T) HPML_API vec3_t(T) vec3(T)(T x, T y, T z) -#define instantiate_declaration_vec3_dot(T) HPML_API float vec3_dot(T)(vec3_t(T) v1, vec3_t(T) v2) -#define instantiate_declaration_vec3_magnitude(T) HPML_API float vec3_magnitude(T)(vec3_t(T) v) -#define instantiate_declaration_vec3_negate(T) HPML_API vec3_t(T) vec3_negate(T)(vec3_t(T) v) -#define instantiate_declaration_vec3_normalize(T) HPML_API vec3_t(T) vec3_normalize(T)(vec3_t(T) v) -#define instantiate_declaration_vec3_rotate(T) HPML_API vec3_t(T) vec3_rotate(T)(vec3_t(T) v, vec3_t(T) axis, float angle) -#define instantiate_declaration_vec3_angle(T) HPML_API float vec3_angle(T)(vec3_t(T) v1, vec3_t(T) v2) -#define instantiate_declaration_vec3_abs(T) HPML_API vec3_t(T) vec3_abs(T)(vec3_t(T) v) +#define instantiate_implementation_vec3(T) HPML_API vec3_t(T) vec3(T)(T x, T y, T z) { return (vec3_t(T)) { x, y, z }; } + +/* vec3_zero: Creates a vector3 object in memory having x = 0, and y = 0 + * returns: vec3_t(T) vector having x = 0, y = 0 + */ +#define vec3_zero(T) c_template(vec3_zero, T) +#define instantiate_declaration_vec3_zero(T) HPML_API vec3_t(T) vec3_zero(T)() +#define instantiate_implementation_vec3_zero(T) HPML_API vec3_t(T) vec3_zero(T)() { return (vec3_t(T)) { 0, 0, 0 }; } + +/* vec3_up: Returns the up direction vector (vector4) + * returns: vec3_t(T) up direction, i.e. Vector4.up [+ve y axis direction] + */ +#define vec3_up(T) c_template(vec3_up, T) #define instantiate_declaration_vec3_up(T) HPML_API vec3_t(T) vec3_up(T)() +#define instantiate_implementation_vec3_up(T) HPML_API vec3_t(T) vec3_up(T)() { return (vec3_t(T)) { 0, 1, 0 }; } + +/* vec3_down: Returns the down direction vector (vector4) + * returns: vec3_t(T) down direction, i.e. Vector4.down [-ve y axis direction] + */ +#define vec3_down(T) c_template(vec3_down, T) #define instantiate_declaration_vec3_down(T) HPML_API vec3_t(T) vec3_down(T)() -#define instantiate_declaration_vec3_left(T) HPML_API vec3_t(T) vec3_left(T)() +#define instantiate_implementation_vec3_down(T) HPML_API vec3_t(T) vec3_down(T)() { return (vec3_t(T)) { 0, -1, 0 }; } + +/* vec3_right: Returns the right direction vector (vector4) + * returns: vec3_t(T) right direction, i.e. Vector4.right [+ve x axis direction] + */ +#define vec3_right(T) c_template(vec3_right, T) #define instantiate_declaration_vec3_right(T) HPML_API vec3_t(T) vec3_right(T)() +#define instantiate_implementation_vec3_right(T) HPML_API vec3_t(T) vec3_right(T)() { return (vec3_t(T)) { 1, 0, 0 }; } + +/*vec3_left*/ +#define vec3_left(T) c_template(vec3_left, T) +#define instantiate_declaration_vec3_left(T) HPML_API vec3_t(T) vec3_left(T)() +#define instantiate_implementation_vec3_left(T) HPML_API vec3_t(T) vec3_left(T)() { return (vec3_t(T)) { -1, 0, 0 }; } + +/* vec3_forward: Returns the forward direction vector (vector4) + * returns: vec3_t(T) forward direction, i.e. Vector4.forward [+ve z axis direction] + */ +#define vec3_forward(T) c_template(vec3_forward, T) #define instantiate_declaration_vec3_forward(T) HPML_API vec3_t(T) vec3_forward(T)() -#define instantiate_declaration_vec3_backward(T) HPML_API vec3_t(T) vec3_backward(T)() -#define instantiate_declaration_vec3_project(T) HPML_API vec3_t(T) vec3_project(T)(T)(vec3_t(T) v1, vec3_t(T) v2) -#define instantiate_declaration_vec3_lerp(T) HPML_API vec3_t(T) vec3_lerp(T)(vec3_t(T) v1, vec3_t(T) v2, float interpolation_value) -#define instantiate_declaration_vec3_internal_division(T) HPML_API vec3_t(T) vec3_internal_division(T)(vec3_t(T) v1, vec3_t(T) v2, float m, float n) -#define instantiate_declaration_vec3_external_division(T) HPML_API vec3_t(T) vec3_external_division(T)(vec3_t(T) v1, vec3_t(T) v2, float m, float n) -#define instantiate_declaration_vec3_scale(T) HPML_API vec3_t(T) vec3_scale(T)(vec3_t(T) v, float scalar_value) -#define instantiate_declaration_vec3_cross(T) HPML_API vec3_t(T) vec3_cross(T)(vec3_t(T) from, vec3_t(T) to) -#define instantiate_declaration_vec3_box(T) HPML_API float vec3_box(T)(vec3_t(T) from, vec3_t(T) to, vec3_t(T) height) +#define instantiate_implementation_vec3_forward(T) HPML_API vec3_t(T) vec3_forward(T)() { return (vec3_t(T)) { 0, 0, 1 }; } -/* vec3_is_equal(T): Checks whether the two vectors are equal or not - * vec3_t(T) v1: First vector to be checked against - * vec3_t(T) v2: Second vector to be checked against - * returns: bool true if both vectors are approximetly equal, otherwise false +/* vec3_back: Return the backward direction vector (vector4) + * returns: vec3_t(T) backward direction, i.e. Vector4.back [-ve z axis direction] + */ +#define vec3_back(T) c_template(vec3_back, T) +#define instantiate_declaration_vec3_back(T) HPML_API vec3_t(T) vec3_back(T)() +#define instantiate_implementation_vec3_back(T) HPML_API vec3_t(T) vec3_back(T)() { return (vec3_t(T)) { 0, 0, 1 }; } + +/*vec3_one*/ +#define vec3_one(T) c_template(vec3_one, T) +#define instantiate_declaration_vec3_one(T) HPML_API vec3_t(T) vec3_one(T)() +#define instantiate_implementation_vec3_one(T) HPML_API vec3_t(T) vec3_one(T)() { return (vec3_t(T)) { 1, 1, 1 }; } + + +/*ARITHMETIC*/ + +/* vec3_add: Adds the rest of the vectors to the first vector in the variable arguments list + * u32 count: number of vectors involved in the addition + * ... : variable number of vectors + * returns: vec3_t(T) resulting vector3 vector */ -#define instantiate_implementation_vec3_is_equal(T)\ -HPML_API bool vec3_is_equal(T)(vec3_t(T) v1, vec3_t(T) v2)\ +#define __vec3_add(T) c_template(__vec3_add, T) +#define vec3_add(T) c_template(vec3_add, T) +#define instantiate_declaration_vec3_add(T)\ +HPML_API vec3_t(T) __vec3_add(T)(vec3_t(T) v1, vec3_t(T) v2);\ +HPML_API vec3_t(T) vec3_add(T)(u32 count, ...) +#define instantiate_implementation_vec3_add(T)\ +HPML_API vec3_t(T) __vec3_add(T)(vec3_t(T) v1, vec3_t(T) v2) { return (vec3_t(T)) { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; }\ +HPML_API vec3_t(T) vec3_add(T)(u32 count, ...)\ {\ - return (v1.x == v2.x) && (v1.y == v2.y) && (v1.z == v2.z);\ + va_list args;\ + va_start(args, count);\ + vec3_t(T) result = va_arg(args, vec3_t(T));\ + --count;\ + while(count > 0)\ + {\ + result = __vec3_add(T)(result, va_arg(args, vec3_t(T)));\ + --count;\ + }\ + va_end(args);\ + return result;\ } -/* vec3_slerp(T): Calculates spherical interpolation value in-between the vector v1 and v2 - * vec3_t(T) v1: First vector [from] - * vec3_t(T) v2: Second vector [to] - * T slerp_value: interpolation parameter - * returns: vec3_t(T) spherically interpolated value +/* vec3_sub: Subtracts the rest of the vectors from the first vector in the variable arguments list + * u32 count: number of vectors involved in the subtraction + * ... : variable number of vectors + * returns: vec3_t(T) resulting vector3 vector */ -#define instantiate_implementation_vec3_slerp(T)\ -HPML_API vec3_t(T) vec3_slerp(T)(vec3_t(T) v1, vec3_t(T) v2, float slerp_value)\ +#define __vec3_sub(T) c_template(__vec3_sub, T) +#define vec3_sub(T) c_template(vec3_sub, T) +#define instantiate_declaration_vec3_sub(T)\ +HPML_API vec3_t(T) __vec3_sub(T)(vec3_t(T) v1, vec3_t(T) v2);\ +HPML_API vec3_t(T) vec3_sub(T)(u32 count, ...) +#define instantiate_implementation_vec3_sub(T)\ +HPML_API vec3_t(T) __vec3_sub(T)(vec3_t(T) v1, vec3_t(T) v2) { return (vec3_t(T)) { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z }; }\ +HPML_API vec3_t(T) vec3_sub(T)(u32 count, ...)\ {\ - float angle = vec3_angle(T)(v1, v2);\ - EXCEPTION_BLOCK(\ - if((angle == 0) || (angle == 3.145926f) || (angle == 2 * 3.145926f))\ - THROW_EXCEPTION(DIVIDE_BY_ZERO);\ - )\ - float inv_sin_angle = 1 / sin(angle);\ - float temp = slerp_value * angle;\ - return vec3_add(vec3_scale(T)(v1, sin(angle - temp) * inv_sin_angle), vec3_scale(v2, sin(temp)));\ + va_list args;\ + va_start(args, count);\ + vec3_t(T) result = va_arg(args, vec3_t(T));\ + --count;\ + while(count > 0)\ + {\ + result = __vec3_sub(T)(result, va_arg(args, vec3_t(T)));\ + --count;\ + }\ + va_end(args);\ + return result;\ } -/* vec3_box: Calculates box product [Value of parallelpiped] - * vec3_t(T) from: from vector( - * vec3_t(T) to: to vector - * returns: float box product of from, to, and height vectors +/* vec3_mul: Multiplies the first vector by the rest of the vectors in the variable arguments list + * u32 count: number of vectors involved in the multiplication + * ... : variable number of vectors + * returns: vec3_t(T) resulting vector3 vector */ -#define instantiate_implementation_vec3_box(T)\ -HPML_API float vec3_box(T)(vec3_t(T) from, vec3_t(T) to, vec3_t(T) height)\ +#define __vec3_mul(T) c_template(__vec3_mul, T) +#define vec3_mul(T) c_template(vec3_mul, T) +#define instantiate_declaration_vec3_mul(T)\ +HPML_API vec3_t(T) __vec3_mul(T)(vec3_t(T) v1, vec3_t(T) v2);\ +HPML_API vec3_t(T) vec3_mul(T)(u32 count, ...) +#define instantiate_implementation_vec3_mul(T)\ +HPML_API vec3_t(T) __vec3_mul(T)(vec3_t(T) v1, vec3_t(T) v2) { return (vec3_t(T)) { v1.x * v2.x, v1.y * v2.y, v1.z * v2.z }; }\ +HPML_API vec3_t(T) vec3_mul(T)(u32 count, ...)\ {\ - return vec3_dot(T)(height, vec3_cross(T)(from, to));\ + va_list args;\ + va_start(args, count);\ + vec3_t(T) result = va_arg(args, vec3_t(T));\ + --count;\ + while(count > 0)\ + {\ + result = __vec3_mul(T)(result, va_arg(args, vec3_t(T)));\ + --count;\ + }\ + va_end(args);\ + return result;\ } -/* vec3_cross: Calculates cross product of 'from' (vector3) to 'to' (vector3) - * vec3_t(T) from: from vector - * vec3_t(T) to: to vector - * returns: vec3_t(T) cross product, pseudo vector +/* vec3_div: Divides the first vector by the rest of the vectors in the variable arguments list + * u32 count: number of vectors involved in the division + * ... : variable number of vectors + * returns: vec3_t(T) resulting vector3 vector + * exceptions: throws DIVIDE_BY_ZERO exception if any of the vectors are null except the first vector in the variable arguments list */ -#define instantiate_implementation_vec3_cross(T)\ -HPML_API vec3_t(T) vec3_cross(T)(vec3_t(T) from, vec3_t(T) to)\ +#define __vec3_div(T) c_template(__vec3_div, T) +#define vec3_div(T) c_template(vec3_div, T) +#define instantiate_declaration_vec3_div(T)\ +HPML_API vec3_t(T) __vec3_div(T)(vec3_t(T) v1, vec3_t(T) v2);\ +HPML_API vec3_t(T) vec3_div(T)(u32 count, ...) +#define instantiate_implementation_vec3_div(T)\ +HPML_API vec3_t(T) __vec3_div(T)(vec3_t(T) v1, vec3_t(T) v2)\ {\ - vec3_t(T) v;\ - v.x = from.y * to.z - from.z * to.y;\ - v.y = from.x * to.z - from.z * to.x;\ - v.z = from.x * to.y - from.y * to.x;\ - return v;\ + EXCEPTION_BLOCK\ + (\ + if(vec3_is_null(T)(v2))\ + THROW_EXCEPTION(DIVIDE_BY_ZERO);\ + )\ + return (vec3_t(T)) { v1.x / v2.x, v1.y / v2.y, v1.z / v2.z };\ +}\ +HPML_API vec3_t(T) vec3_div(T)(u32 count, ...)\ +{\ + va_list args;\ + va_start(args, count);\ + vec3_t(T) result = va_arg(args, vec3_t(T));\ + --count;\ + while(count > 0)\ + {\ + result = __vec3_div(T)(result, va_arg(args, vec3_t(T)));\ + --count;\ + }\ + va_end(args);\ + return result;\ } -/* vec3_scale: Calculates a scaled vector by amount 'scalar_value' - * vec3_t(T) v: vector3 vector to be scaled - * float scalar_value: scalar value to be multiplied with components of v vector - * returns: vec3_t(T) scaled vector +/*INTERPOLATION*/ + +/* vec3_lerp: Calculates an interpolated vector from vector v1 to vector v2, having interpolation value 'interpolation_value' + * vec3_t(T) v1: from vector + * vec3_t(T) v2: to vector + * float interpolation_value: interpolation value [Range (0, 1)], inclusive of 0 and 1 + * returns: vec3_t(T) interpolated vector */ -#define instantiate_implementation_vec3_scale(T)\ -HPML_API vec3_t(T) vec3_scale(T)(vec3_t(T) v, float scalar_value)\ +#define vec3_lerp(T) c_template(vec3_lerp, T) +#define instantiate_declaration_vec3_lerp(T) HPML_API vec3_t(T) vec3_lerp(T)(vec3_t(T) from, vec3_t(T) to, float t) +#define instantiate_implementation_vec3_lerp(T) HPML_API vec3_t(T) vec3_lerp(T)(vec3_t(T) from, vec3_t(T) to, float t)\ {\ - vec3_t(T) scaled_v = { v.x * scalar_value, v.y * scalar_value, v.z * scalar_value };\ - return scaled_v;\ + float _t = 1 - t;\ + return (vec3_t(T)) { from.x * _t + to.x * t, from.y * _t + to.y * t, from.z * _t + to.z * t };\ } -/* vec3_internal_division: Calculates internal division vector - * vec3_t(T) v1: first or left vector - * vec3_t(T) v2: second or right vector - * float m: left value of the ratio or numerator - * float n: right value of the ratio or denominator - * NOTE: if m + n = 0 then it will return first vector +/* vec3_slerp(T): Calculates spherical interpolation value in-between the vector v1 and v2 + * vec3_t(T) v1: First vector [from] + * vec3_t(T) v2: Second vector [to] + * T slerp_value: interpolation parameter + * returns: vec3_t(T) spherically interpolated value */ -#define instantiate_implementation_vec3_internal_division(T)\ -HPML_API vec3_t(T) vec3_internal_division(T)(vec3_t(T) v1, vec3_t(T) v2, float m, float n)\ +#define vec3_slerp(T) c_template(vec3_slerp, T) +#define instantiate_declaration_vec3_slerp(T) HPML_API vec3_t(T) vec3_slerp(T)(vec3_t(T) from, vec3_t(T) to, float t) +#define instantiate_implementation_vec3_slerp(T) HPML_API vec3_t(T) vec3_slerp(T)(vec3_t(T) from, vec3_t(T) to, float t)\ {\ - float ratio_sum = m + n;\ - if(ratio_sum == 0)\ - return v1;\ - return vec3_add(T)(vec3_scale(T)(v1, n), vec3_scale(T)(v2, m)) / ratio_sum;\ + float angle = vec3_angle(T)(from, to);\ + EXCEPTION_BLOCK\ + (\ + if((angle == 0) || (angle == 3.145926f) || (angle == 2 * 3.145926f))\ + THROW_EXCEPTION(DIVIDE_BY_ZERO);\ + )\ + float inv_sin_angle = 1 / sin(angle);\ + float temp = t * angle;\ + return vec3_add(T)(2, vec3_scale(T)(from, sin(angle - temp) * inv_sin_angle), vec3_scale(T)(to, sin(temp)));\ +} + +/*ALGEBRA*/ + +/*vec3_direction_ratios*/ +#define vec3_direction_ratios(T) c_template(vec3_direction_ratios, T) +#define instantiate_declaration_vec3_direction_ratios(T) HPML_API vec3_t(T) vec3_direction_ratios(T)(vec3_t(T)) +#define instantiate_implementation_vec3_direction_ratios(T) HPML_API vec3_t(T) vec3_direction_ratios(T)(vec3_t(T) v)\ +{\ + float inv_mag = 1 / vec3_magnitude(T)(v);\ + return vec3_scale(T)(v, inv_mag);\ } /* vec3_external_division: Calculates external division vector @@ -178,296 +257,243 @@ HPML_API vec3_t(T) vec3_internal_division(T)(vec3_t(T) v1, vec3_t(T) v2, float m * float n: right value of the ratio or denominator * NOTE: if m - n = 0 then it will return second vector */ +#define vec3_external_division(T) c_template(vec3_external_division, T) +#define instantiate_declaration_vec3_external_division(T) HPML_API vec3_t(T) vec3_internal_division(T)(vec3_t) #define instantiate_implementation_vec3_external_division(T)\ HPML_API vec3_t(T) vec3_external_division(T)(vec3_t(T) v1, vec3_t(T) v2, float m, float n)\ {\ float ratio_sum = m - n;\ if(ratio_sum == 0)\ return v2;\ - return vec3_sub(T)(vec3_scale(T)(v1, n), vec3_scale(T)(v2, m)) / (m - n);\ + return vec3_sub(T)(vec3_scale(T)(v1, n), vec3_scale(T)(v2, m)) / ratio_sum;\ } -/* vec3_lerp: Calculates an interpolated vector from vector v1 to vector v2, having interpolation value 'interpolation_value' - * vec3_t(T) v1: from vector - * vec3_t(T) v2: to vector - * float interpolation_value: interpolation value [Range (0, 1)], inclusive of 0 and 1 - * returns: vec3_t(T) interpolated vector +/* vec3_internal_division: Calculates internal division vector + * vec3_t(T) v1: first or left vector + * vec3_t(T) v2: second or right vector + * float m: left value of the ratio or numerator + * float n: right value of the ratio or denominator + * NOTE: if m + n = 0 then it will return second vector */ -#define instantiate_implementation_vec3_lerp(T)\ -HPML_API vec3_t(T) vec3_lerp(T)(vec3_t(T) v1, vec3_t(T) v2, float interpolation_value)\ +#define vec3_internal_division(T) c_template(vec3_internal_division, T) +#define instantiate_declaration_vec3_internal_division(T) HPML_API vec3_t(T) vec3_internal_division(T)(vec3_t) +#define instantiate_implementation_vec3_internal_division(T)\ +HPML_API vec3_t(T) vec3_internal_division(T)(vec3_t(T) v1, vec3_t(T) v2, float m, float n)\ {\ - return vec3_add(T)(vec3_scale(T)(v1, 1 - interpolation_value), vec3_scale(T)(v2, interpolation_value));\ + float ratio_sum = m + n;\ + if(ratio_sum == 0)\ + return v2;\ + return vec3_add(T)(vec3_scale(T)(v1, n), vec3_scale(T)(v2, m)) / ratio_sum;\ } -/* vec3_project: Calculates a projection vector of v1 in the direction of v2 vector - * vec3_t(T) v1: vector3 vector which is projected on v1 vector - * vec3_t(T) v2: direction vector on which v1 vector is projected - * returns: vec3_t(T) projected vector in the direction of vector v1 +/* vec3_scale: Calculates a scaled vector by amount 'scalar_value' + * vec3_t(T) v: vector3 vector to be scaled + * float scalar_value: scalar value to be multiplied with components of v vector + * returns: vec3_t(T) scaled vector */ -#define instantiate_implementation_vec3_project(T)\ -HPML_API vec3_t(T) vec3_project(T)(vec3_t(T) v1, vec3_t(T) v2)\ -{\ - vec3_t(T) unit_dir = vec3_normalize(T)(v2);\ - float projection_value = vec3_dot(T)(v1, unit_dir);\ - return vec3_scale(T)(unit_dir, projection_value);\ -} +#define vec3_scale(T) c_template(vec3_scale, T) +#define instantiate_declaration_vec3_scale(T) HPML_API vec3_t(T) vec3_scale(T)(vec3_t(T) v, T value) +#define instantiate_implementation_vec3_scale(T) HPML_API vec3_t(T) vec3_scale(T)(vec3_t(T) v, T value) { return (vec3_t(T)) { v.x * value, v.y * value, v.z * value }; } -/* vec3_forward: Returns the forward direction vector (vector3) - * returns: vec3_t(T) forward direction, i.e. Vector3.forward [+ve z axis direction] +/* vec3_magnitude: Calculates the magnitude of vector3 + * vec3_t(T) v: vector of which magnitude to be calculated + * returns: float magnitude of vector3 v */ -#define instantiate_implementation_vec3_forward(T)\ -HPML_API vec3_t(T) vec3_forward(T)()\ -{\ - vec3_t(T) v = { 0, 0, 1 };\ - return v;\ -} +#define vec3_magnitude(T) c_template(vec3_magnitude, T) +#define instantiate_declaration_vec3_magnitude(T) HPML_API float vec3_magnitude(T)(vec3_t(T)) +#define instantiate_implementation_vec3_magnitude(T) HPML_API float vec3_magnitude(T)(vec3_t(T) v) { return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); } -/* vec3_backward: Return the backward direction vector (vector3) - * returns: vec3_t(T) backward direction, i.e. Vector3.backward [-ve z axis direction] +/* vec3_sqrmagnitude: Calculates the squared magnitude of vector3 + * vec3_t(T) v: vector of which squared magnitude to be calculated + * returns: float squared magnitude of vector3 v */ -#define instantiate_implementation_vec3_backward(T)\ -HPML_API vec3_t(T) vec3_backward(T)()\ -{\ - vec3_t(T) v = { 0, 0, -1 };\ - return v;\ -} +#define vec3_sqrmagnitude(T) c_template(vec3_sqrmagnitude, T) +#define instantiate_declaration_vec3_sqrmagnitude(T) HPML_API float vec3_sqrmagnitude(T)(vec3_t(T)) +#define instantiate_implementation_vec3_sqrmagnitude(T) HPML_API float vec3_sqrmagnitude(T)(vec3_t(T) v) { return (v.x * v.x + v.y * v.y + v.z * v.z); } -/* vec3_right: Returns the right direction vector (vector3) - * returns: vec3_t(T) right direction, i.e. Vector3.right [+ve x axis direction] +/* vec3_normalize: Normalizes a vector3 + * vec3_t(T) v: vector to be normalized + * returns: vec3_t(T) normalized vector */ -#define instantiate_implementation_vec3_right(T)\ -HPML_API vec3_t(T) vec3_right(T)()\ +#define vec3_normalize(T) c_template(vec3_normalize, T) +#define instantiate_declaration_vec3_normalize(T) HPML_API vec3_t(T) vec3_normalize(T)(vec3_t(T)) +#define instantiate_implementation_vec3_normalize(T) HPML_API vec3_t(T) vec3_normalize(T)(vec3_t(T) v)\ {\ - vec3_t(T) v = { 1, 0, 0 };\ - return v;\ + float inv_mag = 1 / vec3_magnitude(T)(v);\ + return (vec3_t(T)) { v.x * inv_mag, v.y * inv_mag, v.z * inv_mag };\ } -/* vec3_left: Returns the left direction vector (vector3) - * returns: vec3_t(T) left direction, i.e. Vector3.left [-ve x axis direction] +/* vec3_angle: Calculates the angle between two vector3 vectors + * vec3_t(T) v1: first vector involved in the angle calculation + * vec3_t(T) v2: second vector involved in the angle calculation + * returns: float +ve angle always */ -#define instantiate_implementation_vec3_left(T)\ -HPML_API vec3_t(T) vec3_left(T)()\ +#define vec3_angle(T) c_template(vec3_angle, T) +#define instantiate_declaration_vec3_angle(T) HPML_API float vec3_angle(T)(vec3_t(T) v1, vec3_t(T) v2) +#define instantiate_implementation_vec3_angle(T) HPML_API float vec3_angle(T)(vec3_t(T) v1, vec3_t(T) v2)\ {\ - vec3_t(T) v = { -1, 0, 0 };\ - return v;\ + float m1 = vec3_magnitude(T)(v1);\ + float m2 = vec3_magnitude(T)(v2);\ +EXCEPTION_BLOCK\ +(\ + if((m1 == 0) || (m2 == 0))\ + throw_exception(DIVIDE_BY_ZERO);\ +)\ + return acos(vec3_dot(T)(v1, v2) / (m1 * m2));\ } -/* vec3_down: Returns the down direction vector (vector3) - * returns: vec3_t(T) down direction, i.e. Vector3.down [-ve y axis direction] - */ -#define instantiate_implementation_vec3_down(T)\ -HPML_API vec3_t(T) vec3_down(T)()\ +/*vec3_unit_angle*/ +#define vec3_unit_angle(T) c_template(vec3_unit_angle, T) +#define instantiate_declaration_vec3_unit_angle(T) HPML_API float vec3_unit_angle(T)(vec3_t(T) v1, vec3_t(T) v2) +#define instantiate_implementation_vec3_unit_angle(T) HPML_API float vec3_unit_angle(T)(vec3_t(T) v1, vec3_t(T) v2)\ {\ - vec3_t(T) v = { 0, -1, 0 };\ - return v;\ + return acos(vec3_dot(T)(v1, v2));\ } -/* vec3_up: Returns the up direction vector (vector3) - * returns: vec3_t(T) up direction, i.e. Vector3.up [+ve y axis direction] - */ -#define instantiate_implementation_vec3_up(T)\ -HPML_API vec3_t(T) vec3_up(T)()\ +/*vec3_negate*/ +#define vec3_negate(T) c_template(vec3_negate, T) +#define instantiate_declaration_vec3_negate(T) HPML_API vec3_t(T) vec3_negate(T)(vec3_t(T)) +#define instantiate_implementation_vec3_negate(T) HPML_API vec3_t(T) vec3_negate(T)(vec3_t(T) v) { return (vec3_t(T)) { -v.x, -v.y, -v.z }; } + +/*vec3_rotate_x*/ +#define vec3_rotate_x(T) c_template(vec3_rotate_x, T) +#define instantiate_declaration_vec3_rotate_x(T) HPML_API vec3_t(T) vec3_rotate_x(T)(vec3_t(T) v, float angle) +#define instantiate_implementation_vec3_rotate_x(T) HPML_API vec3_t(T) vec3_rotate_x(T)(vec3_t(T) v, float angle)\ {\ - vec3_t(T) v = { 0, 1, 0 };\ - return v;\ + float c = cos(angle);\ + float s = sin(angle);\ + return (vec3_t(T)) { v.x, v.y * c - s * v.z, v.y * s + v.z * c };\ } -/* vec3_abs: Calculates the absolute value of each component of the vector3 v - * vec3_t(T) v: input vector3 vector - * returns: vec3_t(T) vector having absolute value components - */ -#define instantiate_implementation_vec3_abs(T)\ -HPML_API vec3_t(T) vec3_abs(T)(vec3_t(T) v)\ +/*vec3_rotate_y*/ +#define vec3_rotate_y(T) c_template(vec3_rotate_y, T) +#define instantiate_declaration_vec3_rotate_y(T) HPML_API vec3_t(T) vec3_rotate_y(T)(vec3_t(T) v, float angle) +#define instantiate_implementation_vec3_rotate_y(T) HPML_API vec3_t(T) vec3_rotate_y(T)(vec3_t(T) v, float angle)\ {\ - vec3_t(T) v;\ - v.x = abs(v.x);\ - v.y = abs(v.y);\ - v.z = abs(v.z);\ - return v;\ + float c = cos(angle);\ + float s = sin(angle);\ + return (vec3_t(T)) { v.x * c + v.z * s, v.y, -v.x * s + v.z * c };\ } -/* vec3_angle: Calculates the angle between two vector3 vectors - * vec3_t(T) v1: first vector involved in the angle calculation - * vec3_t(T) v2: second vector involved in the angle calculation - * returns: float +ve angle always - */ -#define instantiate_implementation_vec3_angle(T)\ -HPML_API float vec3_angle(T)(vec3_t(T) v1, vec3_t(T) v2)\ +/*vec3_rotate_z*/ +#define vec3_rotate_z(T) c_template(vec3_rotate_z, T) +#define instantiate_declaration_vec3_rotate_z(T) HPML_API vec3_t(T) vec3_rotate_z(T)(vec3_t(T) v, float angle) +#define instantiate_implementation_vec3_rotate_z(T) HPML_API vec3_t(T) vec3_rotate_z(T)(vec3_t(T) v, float angle)\ {\ - if(!(vec3_is_null(T)(v1) && vec3_is_null(T)(v2)))\ - return 0.0f;\ - return acos(vec3_dot(T)(v1, v2) / (vec3_magnitude(T)(v1) * vec3_magnitude(T)(v2)));\ + float c = cos(angle);\ + float s = sin(angle);\ + return (vec3_t(T)) { v.x * c - v.y * s, v.x * s + c * v.y, v.z };\ } -/* vec3_rotate: Rotates a vector3 by angle 'angle' - * vec3_t(T) v: vector to be rotated - * float angle: angle by which to rotate, +ve angle means anticlockwise rotation, -ve angle means clockwise rotation - * returns: vec3_t(T) rotated vector - */ -#define instantiate_implementation_vec3_rotate(T)\ -HPML_API vec3_t(T) vec3_rotate(T)(vec3_t(T) v, vec3_t(T) axis, float angle)\ +/*vec3_rotate*/ +#define vec3_rotate(T) c_template(vec3_rotate, T) +#define instantiate_declaration_vec3_rotate(T) HPML_API vec3_t(T) vec3_rotate(T)(vec3_t(T) v, float x, float y, float z) +#define instantiate_implementation_vec3_rotate(T) HPML_API vec3_t(T) vec3_rotate(T)(vec3_t(T) v, float x, float y, float z)\ {\ - vec3_t(T) rot_v;\ - float cos_angle = cos(angle);\ - float sin_angle = sin(angle);\ - rot_v.x = v.x * cos_angle - v.y * sin_angle;\ - rot_v.y = v.x * sin_angle + v.y * cos_angle;\ - return rot_v;\ + return vec3_rotate_z(T)(vec3_rotate_y(T)(vec3_rotate_x(T)(v, x), y), z);\ } -/* vec3_normalize: Normalizes a vector3 - * vec3_t(T) v: vector to be normalized - * returns: vec3_t(T) normalized vector - */ -#define instantiate_implementation_vec3_normalize(T)\ -HPML_API vec3_t(T) vec3_normalize(T)(vec3_t(T) v)\ +/*vec3_axis_rotate*/ +/* + let axis = a; + let vector = v; + let angle = r; + + k = v - (v.a)a + j = a X k + f = k * cos(r) + j * sin(r) + result = f + (v.a)a + result = k * cos(r) + j * sin(r) + (v.a)a + result = (v - (v.a)a) * cos(r) + (a x k) * sin(r) + (v.a)a + result = (v - (v.a)a) * cos(r) + a x (v - (v.a)a) * sin(r) + (v.a)a + result = (v - (v.a)a) * cos(r) + a x v * sin(r) + (v.a)a + result = (v.a)a * (1 - cos(r)) + a x v * sin(r) + v * cos(r) +*/ +#define vec3_axis_rotate(T) c_template(vec3_axis_rotate, T) +#define instantiate_declaration_vec3_axis_rotate(T) HPML_API vec3_t(T) vec3_axis_rotate(T)(vec3_t(T) v, float axisX, float axisY, float axisZ, float angle) +#define instantiate_implementation_vec3_axis_rotate(T) HPML_API vec3_t(T) vec3_axis_rotate(T)(vec3_t(T) v, float axisX, float axisY, float axisZ, float angle)\ +{\ + vec3_t(T) p = vec3_unit_project(T)(v, (vec3_t(T)) { axisX, axisY, axisZ });\ + vec3_t(T) k = vec3_sub(T)(2, v, p);\ + vec3_t(T) j = vec3_cross(T)((vec3_t(T)) { axisX, axisY, axisZ }, k);\ + return vec3_add(T)(3 , vec3_scale(T)(k, cos(angle)), vec3_scale(T)(j, sin(angle)), p);\ + } + + +/*vec3_project*/ +#define vec3_project(T) c_template(vec3_project, T) +#define instantiate_declaration_vec3_project(T) HPML_API vec3_t(T) vec3_project(T)(vec3_t(T) v, vec3_t(T) a) +#define instantiate_implementation_vec3_project(T) HPML_API vec3_t(T) vec3_project(T)(vec3_t(T) v, vec3_t(T) a)\ {\ - if(vec3_is_null(T)(v))\ - return v;\ - float inv_mag = (float)1 / vec3_magnitude(T)(v);\ - vec3_t(T) unit_v;\ - unit_v.x = v.x * inv_mag;\ - unit_v.y = v.y * inv_mag;\ - unit_v.z = v.z * inv_mag;\ - return unit_v;\ + vec3_t(T) na = vec3_normalize(T)(v);\ + return vec3_scale(T)(na, vec3_dot(T)(v, na));\ } -/* vec3_negate: Negates a vector3 - * vec3_t(T) v: vector to be negated - * returns: vec3_t(T) negated vector - */ -#define instantiate_implementation_vec3_negate(T)\ -HPML_API vec3_t(T) vec3_negate(T)(vec3_t(T) v)\ +/*vec3_unit_project*/ +#define vec3_unit_project(T) c_template(vec3_unit_project, T) +#define instantiate_declaration_vec3_unit_project(T) HPML_API vec3_t(T) vec3_unit_project(T)(vec3_t(T) v, vec3_t(T) a) +#define instantiate_implementation_vec3_unit_project(T) HPML_API vec3_t(T) vec3_unit_project(T)(vec3_t(T) v, vec3_t(T) a)\ {\ - vec3_t(T) neg_v;\ - neg_v.x = -v.x;\ - neg_v.y = -v.y;\ - neg_v.z = -v.z;\ - return neg_v;\ + return vec3_scale(T)(a, vec3_dot(T)(v, a));\ } -/* vec3_magnitude: Calculates the magnitude of vector3 - * vec3_t(T) v: vector of which magnitude to be calculated - * returns: float magnitude of vector3 v - */ -#define instantiate_implementation_vec3_magnitude(T)\ -HPML_API float vec3_magnitude(T)(vec3_t(T) v)\ +/*vec3_box*/ +#define vec3_box(T) c_template(vec3_box, T) +#define instantiate_declaration_vec3_box(T) HPML_API float vec3_box(T)(vec3_t(T) from, vec3_t(T) to, vec3_t(T) height) +#define instantiate_implementation_vec3_box(T) HPML_API float vec3_box(T)(vec3_t(T) from, vec3_t(T) to, vec3_t(T) height)\ {\ - return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);\ + return vec3_dot(T)(height, vec3_cross(T)(from, to));\ } -/* vec_dot: Calculates the dot product of two vector3's - * vec3_t(T) v1: first vector involved in the dot product calculation - * vec3_t(T) v2: second vector involved in the dot product calculation - * returns: float dot product of v1 and v2 - */ -#define instantiate_implementation_vec3_dot(T)\ -HPML_API float vec3_dot(T)(vec3_t(T) v1, vec3_t(T) v2)\ +/*vec3_dot*/ +#define vec3_dot(T) c_template(vec3_dot, T) +#define instantiate_declaration_vec3_dot(T) HPML_API T vec3_dot(T)(vec3_t(T) v1, vec3_t(T) v2) +#define instantiate_implementation_vec3_dot(T) HPML_API T vec3_dot(T)(vec3_t(T) v1, vec3_t(T) v2)\ {\ return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;\ } -/* vec3: Creates vector3 object in memory taking two arguments - * x: x-component - * y: y-component - * returns: vec_t(T) vector having x, y - */ -#define instantiate_implementation_vec3(T)\ -HPML_API vec3_t(T) vec3(T)(T x, T y, T z)\ +/*vec3_cross*/ +#define vec3_cross(T) c_template(vec3_cross, T) +#define instantiate_declaration_vec3_cross(T) HPML_API vec3_t(T) vec3_cross(T)(vec3_t(T) from, vec3_t(T) to) +#define instantiate_implementation_vec3_cross(T) HPML_API vec3_t(T) vec3_cross(T)(vec3_t(T) from, vec3_t(T) to)\ {\ - vec3_t(T) v = { x, y, z };\ + vec3_t(T) v = {\ + from.y * to.z - from.z * to.y,\ + from.x * to.z - from.z * to.x,\ + from.x * to.y - from.y * to.x,\ + };\ return v;\ } -/* See: vec3_null - */ -#define instantiate_implementation_vec3_zero(T) instantiate_implementation_vec3_null(T) - -/* vec3_null: Creates a vector3 object in memory having x = 0, and y = 0 - * returns: vec3_t(T) vector having x = 0, y = 0 - */ -#define instantiate_implementation_vec3_null(T)\ -HPML_API vec3_t(T) vec3_null(T)()\ -{\ - vec3_t(T) null_vector = { 0, 0, 0 };\ - return null_vector;\ -} +/*MISC*/ /* vec3_is_null: Checks if passed vector3 is null or not * vec3_t(T) v: vector3 to be checked for * returns: true if vector3 is null vector, otherwise false [boolean] */ -#define instantiate_implementation_vec3_is_null(T)\ -HPML_API bool vec3_is_null(T)(vec3_t(T) v)\ +#define vec3_is_null(T) c_template(vec3_is_null, T) +#define instantiate_declaration_vec3_is_null(T) HPML_API bool vec3_is_null(T)(vec3_t(T) v) +#define instantiate_implementation_vec3_is_null(T) HPML_API bool vec3_is_null(T)(vec3_t(T) v)\ {\ return (v.x == 0) && (v.y == 0) && (v.z == 0);\ } -/* vec3_add: Adds two vector3 vectors component wise, i.e. v1 + v2 - * vec3_t(T) v1: first vector involved in addition calculation - * vec3_t(T) v2: second vector involved in addition calculation - * returns: vec3_t(T) resulting vector3 vector - */ -#define instantiate_implementation_vec3_add(T)\ -HPML_API vec3_t(T) vec3_add(T)(vec3_t(T) v1, vec3_t(T) v2)\ -{\ - vec3_t(T) result;\ - result.x = v1.x + v2.x;\ - result.y = v1.y + v2.y;\ - result.z = v1.z + v2.z;\ - return result;\ -} - -/* vec3_sub: Subtracts second vector (v2) from first vector (v1) component wise, i.e. v1 - v2 - * vec3_t(T) v1: first vector involved in subtraction calculation - * vec3_t(T) v2: second vector involved in subtraction calculation - * returns: vec3_t(T) resulting vector3 vector - */ -#define instantiate_implementation_vec3_sub(T)\ -HPML_API vec3_t(T) vec3_sub(T)(vec3_t(T) v1, vec3_t(T) v2)\ -{\ - vec3_t(T) result;\ - result.x = v1.x - v2.x;\ - result.y = v1.y - v2.y;\ - result.z = v1.z - v2.z;\ - return result;\ -} - -/* vec3_mul: Multiplies two vector3 vectors v1 and v2 component wise - * vec3_t(T) v1: first vector involved in multiplication calculation - * vec3_t(T) v2: second vector involved in multiplication calculation - * returns: vec3_t(T) resulting vector3 vector +/* vec3_is_equal(T): Checks whether the two vectors are equal or not + * vec3_t(T) v1: First vector to be checked against + * vec3_t(T) v2: Second vector to be checked against + * returns: bool true if both vectors are approximetly equal, otherwise false */ -#define instantiate_implementation_vec3_mul(T)\ -HPML_API vec3_t(T) vec3_mul(T)(vec3_t(T) v1, vec3_t(T) v2)\ +#define vec3_is_equal(T) c_template(vec3_is_equal, T) +#define instantiate_declaration_vec3_is_equal(T) HPML_API bool vec3_is_equal(T)(vec3_t(T) v1, vec3_t(T) v2) +#define instantiate_implementation_vec3_is_equal(T) HPML_API bool vec3_is_equal(T)(vec3_t(T) v1, vec3_t(T) v2)\ {\ - vec3_t(T) result;\ - result.x = v1.x * v2.x;\ - result.y = v1.y * v2.y;\ - result.z = v1.z * v2.z;\ - return result;\ + return (v1.x == v2.x) && (v1.y == v2.y) && (v1.z == v2.z);\ } -/* vec3_div: Divides first vector (v1) by second vector (v2), i.e. v1 / v2 - * vec3_t(T) v1: first vector involved in division calculation - * vec3_t(T) v2: second vector involved in division calculation - * returns: vec3_t(T) resulting vector3 vector - * exceptions: throws DIVIDE_BY_ZERO exception if v2 is a null vector - */ -#define instantiate_implementation_vec3_div(T)\ -HPML_API vec3_t(T) vec3_div(T)(vec3_t(T) v1, vec3_t(T) v2)\ -{\ - EXCEPTION_BLOCK\ - (\ - if(vec3_is_null(T)(v2))\ - THROW_EXCEPTION(DIVIDE_BY_ZERO);\ - )\ - vec3_t(T) result;\ - result.x = v1.x / v2.x;\ - result.y = v1.y / v2.y;\ - result.z = v1.z / v2.z;\ - return result;\ -} -/*End: Template Definitions*/ +/*DEBUGGING*/ -#endif \ No newline at end of file +/*vec3_print*/ +#define vec3_print(T) c_template(vec3_print, T) +#define instantiate_declaration_vec3_print(T) HPML_API void vec3_print(T)(vec3_t(T) v) +#define instantiate_implementation_vec3_print(T) /*specialized*/ diff --git a/include/hpml/vec3/template_instantiations.h b/include/hpml/vec3/template_instantiations.h index d4138d0..c764211 100644 --- a/include/hpml/vec3/template_instantiations.h +++ b/include/hpml/vec3/template_instantiations.h @@ -1,120 +1,168 @@ -#ifndef __HPML_VEC3_TEMPLATE_INSTANTIATIONS_H__ -#define __HPML_VEC3_TEMPLATE_INSTANTIATIONS_H__ +#pragma once #include #ifdef __cplusplus extern "C" { #endif - + /*Begin: Template Instantiations*/ instantiate_vec3_struct(int); +instantiate_declaration_vec3(int); +instantiate_declaration_vec3_zero(int); +instantiate_declaration_vec3_one(int); instantiate_declaration_vec3_add(int); instantiate_declaration_vec3_sub(int); instantiate_declaration_vec3_mul(int); instantiate_declaration_vec3_div(int); -instantiate_declaration_vec3_is_null(int); -instantiate_declaration_vec3_null(int); -instantiate_declaration_vec3_print(int); -instantiate_declaration_vec3(int); //constructor -instantiate_declaration_vec3_dot(int); -instantiate_declaration_vec3_magnitude(int); -instantiate_declaration_vec3_normalize(int); +instantiate_declaration_vec3_lerp(int); +instantiate_declaration_vec3_slerp(int); instantiate_declaration_vec3_rotate(int); -instantiate_declaration_vec3_scale(int); -instantiate_declaration_vec3_cross(int); +instantiate_declaration_vec3_negate(int); instantiate_declaration_vec3_up(int); instantiate_declaration_vec3_down(int); instantiate_declaration_vec3_left(int); instantiate_declaration_vec3_right(int); instantiate_declaration_vec3_forward(int); -instantiate_declaration_vec3_backward(int); +instantiate_declaration_vec3_back(int); +instantiate_declaration_vec3_is_null(int); +instantiate_declaration_vec3_is_equal(int); + +instantiate_declaration_vec3_print(int); +instantiate_declaration_vec3_normalize(int); +instantiate_declaration_vec3_magnitude(int); +instantiate_declaration_vec3_sqrmagnitude(int); +instantiate_declaration_vec3_angle(int); +instantiate_declaration_vec3_unit_angle(int); +instantiate_declaration_vec3_scale(int); +instantiate_declaration_vec3_dot(int); +instantiate_declaration_vec3_cross(int); +instantiate_declaration_vec3_rotate_x(int); +instantiate_declaration_vec3_rotate_y(int); +instantiate_declaration_vec3_rotate_z(int); +instantiate_declaration_vec3_axis_rotate(int); instantiate_declaration_vec3_box(int); -instantiate_declaration_vec3_negate(int); +instantiate_declaration_vec3_project(int); +instantiate_declaration_vec3_unit_project(int); instantiate_vec3_struct(float); +instantiate_declaration_vec3(float); +instantiate_declaration_vec3_zero(float); +instantiate_declaration_vec3_one(float); instantiate_declaration_vec3_add(float); instantiate_declaration_vec3_sub(float); instantiate_declaration_vec3_mul(float); instantiate_declaration_vec3_div(float); -instantiate_declaration_vec3_is_null(float); -instantiate_declaration_vec3_null(float); -instantiate_declaration_vec3_print(float); -instantiate_declaration_vec3(float); //constructor -instantiate_declaration_vec3_dot(float); -instantiate_declaration_vec3_magnitude(float); -instantiate_declaration_vec3_normalize(float); +instantiate_declaration_vec3_lerp(float); +instantiate_declaration_vec3_slerp(float); instantiate_declaration_vec3_rotate(float); -instantiate_declaration_vec3_scale(float); -instantiate_declaration_vec3_cross(float); +instantiate_declaration_vec3_negate(float); instantiate_declaration_vec3_up(float); instantiate_declaration_vec3_down(float); instantiate_declaration_vec3_left(float); instantiate_declaration_vec3_right(float); instantiate_declaration_vec3_forward(float); -instantiate_declaration_vec3_backward(float); +instantiate_declaration_vec3_back(float); +instantiate_declaration_vec3_is_null(float); +instantiate_declaration_vec3_is_equal(float); + +instantiate_declaration_vec3_print(float); +instantiate_declaration_vec3_normalize(float); +instantiate_declaration_vec3_magnitude(float); +instantiate_declaration_vec3_sqrmagnitude(float); +instantiate_declaration_vec3_angle(float); +instantiate_declaration_vec3_unit_angle(float); +instantiate_declaration_vec3_scale(float); +instantiate_declaration_vec3_dot(float); +instantiate_declaration_vec3_cross(float); +instantiate_declaration_vec3_rotate_x(float); +instantiate_declaration_vec3_rotate_y(float); +instantiate_declaration_vec3_rotate_z(float); +instantiate_declaration_vec3_axis_rotate(float); instantiate_declaration_vec3_box(float); -instantiate_declaration_vec3_negate(float); +instantiate_declaration_vec3_project(float); +instantiate_declaration_vec3_unit_project(float); #if defined(IMPLEMENTATION) -instantiate_implementation_vec3_add(int); +instantiate_implementation_vec3(int); +instantiate_implementation_vec3_zero(int); +instantiate_implementation_vec3_one(int); +instantiate_implementation_vec3_add(int); instantiate_implementation_vec3_sub(int); instantiate_implementation_vec3_mul(int); instantiate_implementation_vec3_div(int); -instantiate_implementation_vec3_null(int); -instantiate_implementation_vec3_is_null(int); -instantiate_implementation_vec3(int); -instantiate_implementation_vec3_dot(int); -instantiate_implementation_vec3_magnitude(int); -instantiate_implementation_vec3_normalize(int); +instantiate_implementation_vec3_lerp(int); +instantiate_implementation_vec3_slerp(int); instantiate_implementation_vec3_rotate(int); -instantiate_implementation_vec3_scale(int); -instantiate_implementation_vec3_cross(int); +instantiate_implementation_vec3_negate(int); instantiate_implementation_vec3_up(int); instantiate_implementation_vec3_down(int); instantiate_implementation_vec3_left(int); instantiate_implementation_vec3_right(int); instantiate_implementation_vec3_forward(int); -instantiate_implementation_vec3_backward(int); -instantiate_implementation_vec3_box(int); -instantiate_implementation_vec3_negate(int); +instantiate_implementation_vec3_back(int); +instantiate_implementation_vec3_is_null(int); +instantiate_implementation_vec3_is_equal(int); -void vec3_print(int)(vec3_t(int) v) -{ - printf("vec3_t(int): { %d, %d, %d }\n", v.x, v.y, v.z); -} +HPML_API void vec3_print(int)(vec3_t(int) v) { printf("vec3_t(int): { %d, %d }\n", v.x, v.y); } +instantiate_implementation_vec3_normalize(int); +instantiate_implementation_vec3_magnitude(int); +instantiate_implementation_vec3_sqrmagnitude(int); +instantiate_implementation_vec3_angle(int); +instantiate_implementation_vec3_unit_angle(int); +instantiate_implementation_vec3_scale(int); +instantiate_implementation_vec3_dot(int); +instantiate_implementation_vec3_cross(int); +instantiate_implementation_vec3_rotate_x(int); +instantiate_implementation_vec3_rotate_y(int); +instantiate_implementation_vec3_rotate_z(int); +instantiate_implementation_vec3_axis_rotate(int); +instantiate_implementation_vec3_box(int); +instantiate_implementation_vec3_project(int); +instantiate_implementation_vec3_unit_project(int); -instantiate_implementation_vec3_add(float); +instantiate_implementation_vec3(float); +instantiate_implementation_vec3_zero(float); +instantiate_implementation_vec3_one(float); +instantiate_implementation_vec3_add(float); instantiate_implementation_vec3_sub(float); instantiate_implementation_vec3_mul(float); instantiate_implementation_vec3_div(float); -instantiate_implementation_vec3_null(float); -instantiate_implementation_vec3_is_null(float); -instantiate_implementation_vec3(float); -instantiate_implementation_vec3_dot(float); -instantiate_implementation_vec3_magnitude(float); -instantiate_implementation_vec3_normalize(float); +instantiate_implementation_vec3_lerp(float); +instantiate_implementation_vec3_slerp(float); instantiate_implementation_vec3_rotate(float); -instantiate_implementation_vec3_scale(float); -instantiate_implementation_vec3_cross(float); +instantiate_implementation_vec3_negate(float); instantiate_implementation_vec3_up(float); instantiate_implementation_vec3_down(float); instantiate_implementation_vec3_left(float); instantiate_implementation_vec3_right(float); instantiate_implementation_vec3_forward(float); -instantiate_implementation_vec3_backward(float); +instantiate_implementation_vec3_back(float); +instantiate_implementation_vec3_is_null(float); +instantiate_implementation_vec3_is_equal(float); + +HPML_API void vec3_print(float)(vec3_t(float) v) { printf("vec3_t(float): { %f, %f, %f }\n", v.x, v.y, v.z); } +instantiate_implementation_vec3_normalize(float); +instantiate_implementation_vec3_magnitude(float); +instantiate_implementation_vec3_sqrmagnitude(float); +instantiate_implementation_vec3_angle(float); +instantiate_implementation_vec3_unit_angle(float); +instantiate_implementation_vec3_scale(float); +instantiate_implementation_vec3_dot(float); +instantiate_implementation_vec3_cross(float); +instantiate_implementation_vec3_rotate_x(float); +instantiate_implementation_vec3_rotate_y(float); +instantiate_implementation_vec3_rotate_z(float); +instantiate_implementation_vec3_axis_rotate(float); instantiate_implementation_vec3_box(float); -instantiate_implementation_vec3_negate(float); -void vec3_print(float)(vec3_t(float) v) -{ - printf("vec3_t(float): { %f, %f, %f }\n", v.x, v.y, v.z); -} -#endif /*IMPLEMENTATION*/ +instantiate_implementation_vec3_project(float); +instantiate_implementation_vec3_unit_project(float); /*End: Template Instantiations*/ +#endif /*IMPLEMENTATION*/ + + #ifdef __cplusplus } -#endif /*__cplusplus*/ - -#endif /*__HPML_VEC3_TEMPLATE_INSTANTIATIONS_H__*/ \ No newline at end of file +#endif/*__cplusplus*/ diff --git a/include/hpml/vec4/template_definitions.h b/include/hpml/vec4/template_definitions.h index 8e6cea1..be3f3d1 100644 --- a/include/hpml/vec4/template_definitions.h +++ b/include/hpml/vec4/template_definitions.h @@ -35,22 +35,30 @@ typedef struct\ #define instantiate_declaration_vec4(T) HPML_API vec4_t(T) vec4(T)(T x, T y, T z, T w) #define instantiate_implementation_vec4(T) HPML_API vec4_t(T) vec4(T)(T x, T y, T z, T w) { return (vec4_t(T)) { x, y, z, w }; } -/*vec4_zero*/ +/* vec4_zero: Creates a vector3 object in memory having x = 0, and y = 0 + * returns: vec4_t(T) vector having x = 0, y = 0 + */ #define vec4_zero(T) c_template(vec4_zero, T) #define instantiate_declaration_vec4_zero(T) HPML_API vec4_t(T) vec4_zero(T)() #define instantiate_implementation_vec4_zero(T) HPML_API vec4_t(T) vec4_zero(T)() { return (vec4_t(T)) { 0, 0, 0, 0 }; } -/*vec4_up*/ +/* vec4_up: Returns the up direction vector (vector4) + * returns: vec4_t(T) up direction, i.e. Vector4.up [+ve y axis direction] + */ #define vec4_up(T) c_template(vec4_up, T) #define instantiate_declaration_vec4_up(T) HPML_API vec4_t(T) vec4_up(T)() #define instantiate_implementation_vec4_up(T) HPML_API vec4_t(T) vec4_up(T)() { return (vec4_t(T)) { 0, 1, 0, 0 }; } -/*vec4_down*/ +/* vec4_down: Returns the down direction vector (vector4) + * returns: vec4_t(T) down direction, i.e. Vector4.down [-ve y axis direction] + */ #define vec4_down(T) c_template(vec4_down, T) #define instantiate_declaration_vec4_down(T) HPML_API vec4_t(T) vec4_down(T)() #define instantiate_implementation_vec4_down(T) HPML_API vec4_t(T) vec4_down(T)() { return (vec4_t(T)) { 0, -1, 0, 0 }; } -/*vec4_right*/ +/* vec4_right: Returns the right direction vector (vector4) + * returns: vec4_t(T) right direction, i.e. Vector4.right [+ve x axis direction] + */ #define vec4_right(T) c_template(vec4_right, T) #define instantiate_declaration_vec4_right(T) HPML_API vec4_t(T) vec4_right(T)() #define instantiate_implementation_vec4_right(T) HPML_API vec4_t(T) vec4_right(T)() { return (vec4_t(T)) { 1, 0, 0, 0 }; } @@ -60,12 +68,16 @@ typedef struct\ #define instantiate_declaration_vec4_left(T) HPML_API vec4_t(T) vec4_left(T)() #define instantiate_implementation_vec4_left(T) HPML_API vec4_t(T) vec4_left(T)() { return (vec4_t(T)) { -1, 0, 0, 0 }; } -/*vec4_forward*/ +/* vec4_forward: Returns the forward direction vector (vector4) + * returns: vec4_t(T) forward direction, i.e. Vector4.forward [+ve z axis direction] + */ #define vec4_forward(T) c_template(vec4_forward, T) #define instantiate_declaration_vec4_forward(T) HPML_API vec4_t(T) vec4_forward(T)() #define instantiate_implementation_vec4_forward(T) HPML_API vec4_t(T) vec4_forward(T)() { return (vec4_t(T)) { 0, 0, 1, 0 }; } -/*vec4_back*/ +/* vec4_back: Return the backward direction vector (vector4) + * returns: vec4_t(T) backward direction, i.e. Vector4.back [-ve z axis direction] + */ #define vec4_back(T) c_template(vec4_back, T) #define instantiate_declaration_vec4_back(T) HPML_API vec4_t(T) vec4_back(T)() #define instantiate_implementation_vec4_back(T) HPML_API vec4_t(T) vec4_back(T)() { return (vec4_t(T)) { 0, 0, 1, 0 }; } @@ -77,7 +89,12 @@ typedef struct\ /*ARITHMETIC*/ -/*vec4_add*/ + +/* vec4_add: Adds the rest of the vectors to the first vector in the variable arguments list + * u32 count: number of vectors involved in the addition + * ... : variable number of vectors + * returns: vec4_t(T) resulting vector3 vector + */ #define __vec4_add(T) c_template(__vec4_add, T) #define vec4_add(T) c_template(vec4_add, T) #define instantiate_declaration_vec4_add(T)\ @@ -99,7 +116,11 @@ HPML_API vec4_t(T) vec4_add(T)(u32 count, ...)\ return result;\ } -/*vec4_sub*/ +/* vec4_sub: Subtracts the rest of the vectors from the first vector in the variable arguments list + * u32 count: number of vectors involved in the subtraction + * ... : variable number of vectors + * returns: vec4_t(T) resulting vector3 vector + */ #define __vec4_sub(T) c_template(__vec4_sub, T) #define vec4_sub(T) c_template(vec4_sub, T) #define instantiate_declaration_vec4_sub(T)\ @@ -121,7 +142,11 @@ HPML_API vec4_t(T) vec4_sub(T)(u32 count, ...)\ return result;\ } -/*vec4_mul*/ +/* vec4_mul: Multiplies the first vector by the rest of the vectors in the variable arguments list + * u32 count: number of vectors involved in the multiplication + * ... : variable number of vectors + * returns: vec4_t(T) resulting vector3 vector + */ #define __vec4_mul(T) c_template(__vec4_mul, T) #define vec4_mul(T) c_template(vec4_mul, T) #define instantiate_declaration_vec4_mul(T)\ @@ -143,17 +168,30 @@ HPML_API vec4_t(T) vec4_mul(T)(u32 count, ...)\ return result;\ } -/*vec4_div*/ +/* vec4_div: Divides the first vector by the rest of the vectors in the variable arguments list + * u32 count: number of vectors involved in the division + * ... : variable number of vectors + * returns: vec4_t(T) resulting vector3 vector + * exceptions: throws DIVIDE_BY_ZERO exception if any of the vectors are null except the first vector in the variable arguments list + */ #define __vec4_div(T) c_template(__vec4_div, T) #define vec4_div(T) c_template(vec4_div, T) #define instantiate_declaration_vec4_div(T)\ HPML_API vec4_t(T) __vec4_div(T)(vec4_t(T) v1, vec4_t(T) v2);\ HPML_API vec4_t(T) vec4_div(T)(u32 count, ...) #define instantiate_implementation_vec4_div(T)\ -HPML_API vec4_t(T) __vec4_div(T)(vec4_t(T) v1, vec4_t(T) v2) { return (vec4_t(T)) { v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w }; }\ +HPML_API vec4_t(T) __vec4_div(T)(vec4_t(T) v1, vec4_t(T) v2)\ +{\ + EXCEPTION_BLOCK\ + (\ + if(vec4_is_null(T)(v2))\ + THROW_EXCEPTION(DIVIDE_BY_ZERO);\ + )\ + return (vec4_t(T)) { v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w };\ +}\ HPML_API vec4_t(T) vec4_div(T)(u32 count, ...)\ {\ - vec4_t(T) result = vec4_zero(T)();\ + vec4_t(T) result = vec4_one(T)();\ va_list args;\ va_start(args, count);\ while(count > 0)\ @@ -166,7 +204,13 @@ HPML_API vec4_t(T) vec4_div(T)(u32 count, ...)\ } /*INTERPOLATION*/ -/*vec4_lerp*/ + +/* vec4_lerp: Calculates an interpolated vector from vector v1 to vector v2, having interpolation value 'interpolation_value' + * vec4_t(T) v1: from vector + * vec4_t(T) v2: to vector + * float interpolation_value: interpolation value [Range (0, 1)], inclusive of 0 and 1 + * returns: vec4_t(T) interpolated vector + */ #define vec4_lerp(T) c_template(vec4_lerp, T) #define instantiate_declaration_vec4_lerp(T) HPML_API vec4_t(T) vec4_lerp(T)(vec4_t(T) from, vec4_t(T) to, float t) #define instantiate_implementation_vec4_lerp(T) HPML_API vec4_t(T) vec4_lerp(T)(vec4_t(T) from, vec4_t(T) to, float t)\ @@ -175,7 +219,12 @@ HPML_API vec4_t(T) vec4_div(T)(u32 count, ...)\ return (vec4_t(T)) { from.x * _t + to.x * t, from.y * _t + to.y * t, from.z * _t + to.z * t, from.w * _t + to.w * t };\ } -/*vec4_slerp*/ +/* vec4_slerp(T): Calculates spherical interpolation value in-between the vector v1 and v2 + * vec4_t(T) v1: First vector [from] + * vec4_t(T) v2: Second vector [to] + * T slerp_value: interpolation parameter + * returns: vec4_t(T) spherically interpolated value + */ #define vec4_slerp(T) c_template(vec4_slerp, T) #define instantiate_declaration_vec4_slerp(T) HPML_API vec4_t(T) vec4_slerp(T)(vec4_t(T) from, vec4_t(T) to, float t) #define instantiate_implementation_vec4_slerp(T) HPML_API vec4_t(T) vec4_slerp(T)(vec4_t(T) from, vec4_t(T) to, float t)\ @@ -193,22 +242,80 @@ HPML_API vec4_t(T) vec4_div(T)(u32 count, ...)\ /*ALGEBRA*/ -/*vec4_scale*/ +/*vec4_direction_ratios*/ +#define vec4_direction_ratios(T) c_template(vec4_direction_ratios, T) +#define instantiate_declaration_vec4_direction_ratios(T) HPML_API vec4_t(T) vec4_direction_ratios(T)(vec4_t(T)) +#define instantiate_implementation_vec4_direction_ratios(T) HPML_API vec4_t(T) vec4_direction_ratios(T)(vec4_t(T) v)\ +{\ + float inv_mag = 1 / vec4_magnitude(T)(v);\ + return vec4_scale(T)(v, inv_mag);\ +} + +/* vec4_external_division: Calculates external division vector + * vec4_t(T) v1: first or left vector + * vec4_t(T) v2: second or right vector + * float m: left value of the ratio or numerator + * float n: right value of the ratio or denominator + * NOTE: if m - n = 0 then it will return second vector + */ +#define vec4_external_division(T) c_template(vec4_external_division, T) +#define instantiate_declaration_vec4_external_division(T) HPML_API vec4_t(T) vec4_internal_division(T)(vec4_t) +#define instantiate_implementation_vec4_external_division(T)\ +HPML_API vec4_t(T) vec4_external_division(T)(vec4_t(T) v1, vec4_t(T) v2, float m, float n)\ +{\ + float ratio_sum = m - n;\ + if(ratio_sum == 0)\ + return v2;\ + return vec4_sub(T)(vec4_scale(T)(v1, n), vec4_scale(T)(v2, m)) / ratio_sum;\ +} + +/* vec4_internal_division: Calculates internal division vector + * vec4_t(T) v1: first or left vector + * vec4_t(T) v2: second or right vector + * float m: left value of the ratio or numerator + * float n: right value of the ratio or denominator + * NOTE: if m + n = 0 then it will return second vector + */ +#define vec4_internal_division(T) c_template(vec4_internal_division, T) +#define instantiate_declaration_vec4_internal_division(T) HPML_API vec4_t(T) vec4_internal_division(T)(vec4_t) +#define instantiate_implementation_vec4_internal_division(T)\ +HPML_API vec4_t(T) vec4_internal_division(T)(vec4_t(T) v1, vec4_t(T) v2, float m, float n)\ +{\ + float ratio_sum = m + n;\ + if(ratio_sum == 0)\ + return v2;\ + return vec4_add(T)(vec4_scale(T)(v1, n), vec4_scale(T)(v2, m)) / ratio_sum;\ +} + +/* vec4_scale: Calculates a scaled vector by amount 'scalar_value' + * vec4_t(T) v: vector3 vector to be scaled + * float scalar_value: scalar value to be multiplied with components of v vector + * returns: vec4_t(T) scaled vector + */ #define vec4_scale(T) c_template(vec4_scale, T) #define instantiate_declaration_vec4_scale(T) HPML_API vec4_t(T) vec4_scale(T)(vec4_t(T) v, T value) #define instantiate_implementation_vec4_scale(T) HPML_API vec4_t(T) vec4_scale(T)(vec4_t(T) v, T value) { return (vec4_t(T)) { v.x * value, v.y * value, v.z * value, v.w * value }; } -/*vec4_magnitude*/ +/* vec4_magnitude: Calculates the magnitude of vector3 + * vec4_t(T) v: vector of which magnitude to be calculated + * returns: float magnitude of vector4 v + */ #define vec4_magnitude(T) c_template(vec4_magnitude, T) #define instantiate_declaration_vec4_magnitude(T) HPML_API float vec4_magnitude(T)(vec4_t(T)) #define instantiate_implementation_vec4_magnitude(T) HPML_API float vec4_magnitude(T)(vec4_t(T) v) { return sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); } -/*vec4_sqrmagnitude*/ +/* vec4_magnitude: Calculates the squared magnitude of vector3 + * vec4_t(T) v: vector of which squared magnitude to be calculated + * returns: float squared magnitude of vector4 v + */ #define vec4_sqrmagnitude(T) c_template(vec4_sqrmagnitude, T) #define instantiate_declaration_vec4_sqrmagnitude(T) HPML_API float vec4_sqrmagnitude(T)(vec4_t(T)) #define instantiate_implementation_vec4_sqrmagnitude(T) HPML_API float vec4_sqrmagnitude(T)(vec4_t(T) v) { return sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); } -/*vec4_normalize*/ +/* vec4_normalize: Normalizes a vector3 + * vec4_t(T) v: vector to be normalized + * returns: vec4_t(T) normalized vector + */ #define vec4_normalize(T) c_template(vec4_normalize, T) #define instantiate_declaration_vec4_normalize(T) HPML_API vec4_t(T) vec4_normalize(T)(vec4_t(T)) #define instantiate_implementation_vec4_normalize(T) HPML_API vec4_t(T) vec4_normalize(T)(vec4_t(T) v)\ @@ -217,11 +324,11 @@ HPML_API vec4_t(T) vec4_div(T)(u32 count, ...)\ return (vec4_t(T)) { v.x * inv_mag, v.y * inv_mag, v.z * inv_mag, v.w * inv_mag };\ } -/*vec4_angle*/ -/* -v1 dot v2 = mag(v1) * mag(v2) * cos(angle) -angle = acos((v1 dot v2) / mag(v1)mag(v2)) -*/ +/* vec4_angle: Calculates the angle between two vector3 vectors + * vec4_t(T) v1: first vector involved in the angle calculation + * vec4_t(T) v2: second vector involved in the angle calculation + * returns: float +ve angle always + */ #define vec4_angle(T) c_template(vec4_angle, T) #define instantiate_declaration_vec4_angle(T) HPML_API float vec4_angle(T)(vec4_t(T) v1, vec4_t(T) v2) #define instantiate_implementation_vec4_angle(T) HPML_API float vec4_angle(T)(vec4_t(T) v1, vec4_t(T) v2)\ @@ -361,6 +468,11 @@ EXCEPTION_BLOCK\ } /*MISC*/ + +/* vec4_is_null: Checks if passed vector3 is null or not + * vec4_t(T) v: vector3 to be checked for + * returns: true if vector3 is null vector, otherwise false [boolean] + */ #define vec4_is_null(T) c_template(vec4_is_null, T) #define instantiate_declaration_vec4_is_null(T) HPML_API bool vec4_is_null(T)(vec4_t(T) v) #define instantiate_implementation_vec4_is_null(T) HPML_API bool vec4_is_null(T)(vec4_t(T) v)\ @@ -368,6 +480,11 @@ EXCEPTION_BLOCK\ return (v.x == 0) && (v.y == 0) && (v.z == 0) && (v.w == 0);\ } +/* vec4_is_equal(T): Checks whether the two vectors are equal or not + * vec4_t(T) v1: First vector to be checked against + * vec4_t(T) v2: Second vector to be checked against + * returns: bool true if both vectors are approximetly equal, otherwise false + */ #define vec4_is_equal(T) c_template(vec4_is_equal, T) #define instantiate_declaration_vec4_is_equal(T) HPML_API bool vec4_is_equal(T)(vec4_t(T) v1, vec4_t(T) v2) #define instantiate_implementation_vec4_is_equal(T) HPML_API bool vec4_is_equal(T)(vec4_t(T) v1, vec4_t(T) v2)\