/* * Copyright (c) 2018 Jean-Philippe Bruyère * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the * Software, and to permit persons to whom the Software is furnished to do so, subject * to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #ifndef VKVG_VECTORS_H #define VKVG_VECTORS_H #include "vkvg_internal.h" #include typedef union { struct { float x; float y; }; }vec2; #ifdef __SSE2__ typedef union { __m128d raw; struct { double x; double y; }; }vec2d; #else typedef struct { double x; double y; }vec2d; #endif typedef struct { float x; float y; float z; }vec3; typedef struct { union { float x; float r; }; union { float y; float g; }; union { float z; float width; float b; }; union { float w; float height; float a; }; }vec4; typedef struct { uint16_t x; uint16_t y; uint16_t z; uint16_t w; }vec4i16; typedef struct { int16_t x; int16_t y; }vec2i16; // compute length of float vector 2d vkvg_inline float vec2_length(vec2 v){ return sqrtf (v.x*v.x + v.y*v.y); } // compute normal direction vector from line defined by 2 points in double precision vkvg_inline vec2d vec2d_line_norm(vec2d a, vec2d b) { vec2d d = {b.x - a.x, b.y - a.y}; double md = sqrt (d.x*d.x + d.y*d.y); d.x/=md; d.y/=md; return d; } // compute normal direction vector from line defined by 2 points vkvg_inline vec2 vec2_line_norm(vec2 a, vec2 b) { vec2 d = {b.x - a.x, b.y - a.y}; float md = sqrtf (d.x*d.x + d.y*d.y); d.x/=md; d.y/=md; return d; } // compute length of double vector 2d vkvg_inline double vec2d_length(vec2d v){ return sqrt (v.x*v.x + v.y*v.y); } // normalize float vector vkvg_inline vec2 vec2_norm(vec2 a) { float m = sqrtf (a.x*a.x + a.y*a.y); return (vec2){a.x/m, a.y/m}; } // devide 2d vector by scalar vkvg_inline vec2 vec2_div(vec2 a, float m){ return (vec2){a.x/m,a.y/m}; } // multiply 2d vector by scalar vkvg_inline vec2 vec2_mult(vec2 a, float m){ return (vec2){a.x*m,a.y*m}; } // compute sum of two double precision vectors vkvg_inline vec2d vec2d_add (vec2d a, vec2d b){ #ifdef __SSE2__ return (vec2d)_mm_add_pd (a.raw, b.raw); #else return (vec2d){a.x + b.x, a.y + b.y}; #endif } // compute subbstraction of two double precision vectors vkvg_inline vec2d vec2d_sub (vec2d a, vec2d b){ #ifdef __SSE2__ return (vec2d)_mm_sub_pd (a.raw, b.raw); #else return (vec2d){a.x - b.x, a.y - b.y}; #endif } // multiply 2d vector by scalar vkvg_inline vec2d vec2d_mult(vec2d a, double m){ #ifdef __SSE2__ return (vec2d)_mm_mul_pd (a.raw, _mm_set_pd1 (m)); #else return (vec2d){a.x*m,a.y*m}; #endif } vkvg_inline vec2d vec2d_div(vec2d a, double m){ #ifdef __SSE2__ return (vec2d)_mm_div_pd (a.raw, _mm_set_pd1 (m)); #else return (vec2d){a.x/m,a.y/m}; #endif } // normalize double vector vkvg_inline vec2d vec2d_norm(vec2d a) { double m = sqrt (a.x*a.x + a.y*a.y); return (vec2d)vec2d_div (a, m); //return (vec2d){a.x/m, a.y/m}; } // compute perpendicular vector vkvg_inline vec2d vec2d_perp (vec2d a){ return (vec2d){a.y, -a.x}; } // compute perpendicular vector vkvg_inline vec2 vec2_perp (vec2 a){ return (vec2){a.y, -a.x}; } // convert double precision vector to single precision vkvg_inline vec2 vec2d_to_vec2(vec2d vd){ return (vec2){(float)vd.x,(float)vd.y}; } // compute sum of two single precision vectors vkvg_inline vec2 vec2_add (vec2 a, vec2 b){ return (vec2){a.x + b.x, a.y + b.y}; } // compute subbstraction of two single precision vectors vkvg_inline vec2 vec2_sub (vec2 a, vec2 b){ return (vec2){a.x - b.x, a.y - b.y}; } // test equality of two single precision vectors vkvg_inline bool vec2_equ (vec2 a, vec2 b){ return (EQUF(a.x,b.x)&EQUF(a.y,b.y)); } vkvg_inline bool vec2d_equ (vec2d a, vec2d b){ return (EQU(a.x,b.x)&EQU(a.y,b.y)); } // compute opposite of single precision vector vkvg_inline void vec2_inv (vec2* v){ v->x = -v->x; v->y = -v->y; } // test if one component of float vector is nan vkvg_inline bool vec2_isnan (vec2 v){ return (bool)(isnanf (v.x) || isnanf (v.y)); } // test if one component of double vector is nan vkvg_inline bool vec2d_isnan (vec2d v){ return (bool)(isnan (v.x) || isnan (v.y)); } vkvg_inline bool vec4_equ (vec4 a, vec4 b){ return (EQUF(a.x,b.x)&EQUF(a.y,b.y)&EQUF(a.z,b.z)&EQUF(a.w,b.w)); } #endif