goat3d

diff libs/vmath/vector.h @ 27:4deb0b12fe14

wtf... corrupted heap?
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Sep 2013 08:20:19 +0300
parents
children 9ba3e2fb8a33
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/vmath/vector.h	Sun Sep 29 08:20:19 2013 +0300
     1.3 @@ -0,0 +1,292 @@
     1.4 +/*
     1.5 +libvmath - a vector math library
     1.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
     1.7 +
     1.8 +This program is free software: you can redistribute it and/or modify
     1.9 +it under the terms of the GNU Lesser General Public License as published
    1.10 +by the Free Software Foundation, either version 3 of the License, or
    1.11 +(at your option) any later version.
    1.12 +
    1.13 +This program is distributed in the hope that it will be useful,
    1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +GNU Lesser General Public License for more details.
    1.17 +
    1.18 +You should have received a copy of the GNU Lesser General Public License
    1.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 +*/
    1.21 +
    1.22 +#ifndef VMATH_VECTOR_H_
    1.23 +#define VMATH_VECTOR_H_
    1.24 +
    1.25 +#include <stdio.h>
    1.26 +#include "vmath_types.h"
    1.27 +
    1.28 +#ifdef __cplusplus
    1.29 +extern "C" {
    1.30 +#endif	/* __cplusplus */
    1.31 +
    1.32 +/* C 2D vector functions */
    1.33 +static inline vec2_t v2_cons(scalar_t x, scalar_t y);
    1.34 +static inline void v2_print(FILE *fp, vec2_t v);
    1.35 +
    1.36 +static inline vec2_t v2_add(vec2_t v1, vec2_t v2);
    1.37 +static inline vec2_t v2_sub(vec2_t v1, vec2_t v2);
    1.38 +static inline vec2_t v2_scale(vec2_t v, scalar_t s);
    1.39 +static inline scalar_t v2_dot(vec2_t v1, vec2_t v2);
    1.40 +static inline scalar_t v2_length(vec2_t v);
    1.41 +static inline scalar_t v2_length_sq(vec2_t v);
    1.42 +static inline vec2_t v2_normalize(vec2_t v);
    1.43 +
    1.44 +static inline vec2_t v2_lerp(vec2_t v1, vec2_t v2, scalar_t t);
    1.45 +
    1.46 +/* C 3D vector functions */
    1.47 +static inline vec3_t v3_cons(scalar_t x, scalar_t y, scalar_t z);
    1.48 +static inline void v3_print(FILE *fp, vec3_t v);
    1.49 +
    1.50 +static inline vec3_t v3_add(vec3_t v1, vec3_t v2);
    1.51 +static inline vec3_t v3_sub(vec3_t v1, vec3_t v2);
    1.52 +static inline vec3_t v3_neg(vec3_t v);
    1.53 +static inline vec3_t v3_mul(vec3_t v1, vec3_t v2);
    1.54 +static inline vec3_t v3_scale(vec3_t v1, scalar_t s);
    1.55 +static inline scalar_t v3_dot(vec3_t v1, vec3_t v2);
    1.56 +static inline vec3_t v3_cross(vec3_t v1, vec3_t v2);
    1.57 +static inline scalar_t v3_length(vec3_t v);
    1.58 +static inline scalar_t v3_length_sq(vec3_t v);
    1.59 +static inline vec3_t v3_normalize(vec3_t v);
    1.60 +static inline vec3_t v3_transform(vec3_t v, mat4_t m);
    1.61 +
    1.62 +static inline vec3_t v3_rotate(vec3_t v, scalar_t x, scalar_t y, scalar_t z);
    1.63 +static inline vec3_t v3_rotate_axis(vec3_t v, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
    1.64 +static inline vec3_t v3_rotate_quat(vec3_t v, quat_t q);
    1.65 +
    1.66 +static inline vec3_t v3_reflect(vec3_t v, vec3_t n);
    1.67 +
    1.68 +static inline vec3_t v3_lerp(vec3_t v1, vec3_t v2, scalar_t t);
    1.69 +
    1.70 +/* C 4D vector functions */
    1.71 +static inline vec4_t v4_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t w);
    1.72 +static inline void v4_print(FILE *fp, vec4_t v);
    1.73 +
    1.74 +static inline vec4_t v4_add(vec4_t v1, vec4_t v2);
    1.75 +static inline vec4_t v4_sub(vec4_t v1, vec4_t v2);
    1.76 +static inline vec4_t v4_neg(vec4_t v);
    1.77 +static inline vec4_t v4_mul(vec4_t v1, vec4_t v2);
    1.78 +static inline vec4_t v4_scale(vec4_t v, scalar_t s);
    1.79 +static inline scalar_t v4_dot(vec4_t v1, vec4_t v2);
    1.80 +static inline scalar_t v4_length(vec4_t v);
    1.81 +static inline scalar_t v4_length_sq(vec4_t v);
    1.82 +static inline vec4_t v4_normalize(vec4_t v);
    1.83 +static inline vec4_t v4_transform(vec4_t v, mat4_t m);
    1.84 +
    1.85 +#ifdef __cplusplus
    1.86 +}	/* extern "C" */
    1.87 +
    1.88 +/* when included from C++ source files, also define the vector classes */
    1.89 +#include <iostream>
    1.90 +
    1.91 +/** 2D Vector */
    1.92 +class Vector2 {
    1.93 +public:
    1.94 +	scalar_t x, y;
    1.95 +
    1.96 +	Vector2(scalar_t x = 0.0, scalar_t y = 0.0);
    1.97 +	Vector2(const vec2_t &vec);
    1.98 +	Vector2(const Vector3 &vec);
    1.99 +	Vector2(const Vector4 &vec);
   1.100 +
   1.101 +	inline scalar_t &operator [](int elem);
   1.102 +	inline const scalar_t &operator [](int elem) const;
   1.103 +
   1.104 +	inline scalar_t length() const;
   1.105 +	inline scalar_t length_sq() const;
   1.106 +	void normalize();
   1.107 +	Vector2 normalized() const;
   1.108 +
   1.109 +	void transform(const Matrix3x3 &mat);
   1.110 +	Vector2 transformed(const Matrix3x3 &mat) const;
   1.111 +
   1.112 +	void rotate(scalar_t angle);
   1.113 +	Vector2 rotated(scalar_t angle) const;
   1.114 +
   1.115 +	Vector2 reflection(const Vector2 &normal) const;
   1.116 +	Vector2 refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const;
   1.117 +};
   1.118 +
   1.119 +/* unary operations */
   1.120 +inline Vector2 operator -(const Vector2 &vec);
   1.121 +
   1.122 +/* binary vector (op) vector operations */
   1.123 +inline scalar_t dot_product(const Vector2 &v1, const Vector2 &v2);
   1.124 +
   1.125 +inline Vector2 operator +(const Vector2 &v1, const Vector2 &v2);
   1.126 +inline Vector2 operator -(const Vector2 &v1, const Vector2 &v2);
   1.127 +inline Vector2 operator *(const Vector2 &v1, const Vector2 &v2);
   1.128 +inline Vector2 operator /(const Vector2 &v1, const Vector2 &v2);
   1.129 +inline bool operator ==(const Vector2 &v1, const Vector2 &v2);
   1.130 +
   1.131 +inline void operator +=(Vector2 &v1, const Vector2 &v2);
   1.132 +inline void operator -=(Vector2 &v1, const Vector2 &v2);
   1.133 +inline void operator *=(Vector2 &v1, const Vector2 &v2);
   1.134 +inline void operator /=(Vector2 &v1, const Vector2 &v2);
   1.135 +
   1.136 +/* binary vector (op) scalar and scalar (op) vector operations */
   1.137 +inline Vector2 operator +(const Vector2 &vec, scalar_t scalar);
   1.138 +inline Vector2 operator +(scalar_t scalar, const Vector2 &vec);
   1.139 +inline Vector2 operator -(const Vector2 &vec, scalar_t scalar);
   1.140 +inline Vector2 operator *(const Vector2 &vec, scalar_t scalar);
   1.141 +inline Vector2 operator *(scalar_t scalar, const Vector2 &vec);
   1.142 +inline Vector2 operator /(const Vector2 &vec, scalar_t scalar);
   1.143 +
   1.144 +inline void operator +=(Vector2 &vec, scalar_t scalar);
   1.145 +inline void operator -=(Vector2 &vec, scalar_t scalar);
   1.146 +inline void operator *=(Vector2 &vec, scalar_t scalar);
   1.147 +inline void operator /=(Vector2 &vec, scalar_t scalar);
   1.148 +
   1.149 +std::ostream &operator <<(std::ostream &out, const Vector2 &vec);
   1.150 +
   1.151 +inline Vector2 lerp(const Vector2 &a, const Vector2 &b, scalar_t t);
   1.152 +inline Vector2 catmull_rom_spline(const Vector2 &v0, const Vector2 &v1,
   1.153 +		const Vector2 &v2, const Vector2 &v3, scalar_t t);
   1.154 +
   1.155 +/* 3D Vector */
   1.156 +class Vector3 {
   1.157 +public:
   1.158 +	scalar_t x, y, z;
   1.159 +
   1.160 +	Vector3(scalar_t x = 0.0, scalar_t y = 0.0, scalar_t z = 0.0);
   1.161 +	Vector3(const vec3_t &vec);
   1.162 +	Vector3(const Vector2 &vec);
   1.163 +	Vector3(const Vector4 &vec);
   1.164 +	Vector3(const SphVector &sph);
   1.165 +
   1.166 +	Vector3 &operator =(const SphVector &sph);
   1.167 +
   1.168 +	inline scalar_t &operator [](int elem);
   1.169 +	inline const scalar_t &operator [](int elem) const;
   1.170 +
   1.171 +	inline scalar_t length() const;
   1.172 +	inline scalar_t length_sq() const;
   1.173 +	void normalize();
   1.174 +	Vector3 normalized() const;
   1.175 +
   1.176 +	void transform(const Matrix3x3 &mat);
   1.177 +	Vector3 transformed(const Matrix3x3 &mat) const;
   1.178 +	void transform(const Matrix4x4 &mat);
   1.179 +	Vector3 transformed(const Matrix4x4 &mat) const;
   1.180 +	void transform(const Quaternion &quat);
   1.181 +	Vector3 transformed(const Quaternion &quat) const;
   1.182 +
   1.183 +	void rotate(const Vector3 &euler);
   1.184 +	Vector3 rotated(const Vector3 &euler) const;
   1.185 +
   1.186 +	Vector3 reflection(const Vector3 &normal) const;
   1.187 +	Vector3 refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const;
   1.188 +	Vector3 refraction(const Vector3 &normal, scalar_t ior) const;
   1.189 +};
   1.190 +
   1.191 +/* unary operations */
   1.192 +inline Vector3 operator -(const Vector3 &vec);
   1.193 +
   1.194 +/* binary vector (op) vector operations */
   1.195 +inline scalar_t dot_product(const Vector3 &v1, const Vector3 &v2);
   1.196 +inline Vector3 cross_product(const Vector3 &v1, const Vector3 &v2);
   1.197 +
   1.198 +inline Vector3 operator +(const Vector3 &v1, const Vector3 &v2);
   1.199 +inline Vector3 operator -(const Vector3 &v1, const Vector3 &v2);
   1.200 +inline Vector3 operator *(const Vector3 &v1, const Vector3 &v2);
   1.201 +inline Vector3 operator /(const Vector3 &v1, const Vector3 &v2);
   1.202 +inline bool operator ==(const Vector3 &v1, const Vector3 &v2);
   1.203 +
   1.204 +inline void operator +=(Vector3 &v1, const Vector3 &v2);
   1.205 +inline void operator -=(Vector3 &v1, const Vector3 &v2);
   1.206 +inline void operator *=(Vector3 &v1, const Vector3 &v2);
   1.207 +inline void operator /=(Vector3 &v1, const Vector3 &v2);
   1.208 +
   1.209 +/* binary vector (op) scalar and scalar (op) vector operations */
   1.210 +inline Vector3 operator +(const Vector3 &vec, scalar_t scalar);
   1.211 +inline Vector3 operator +(scalar_t scalar, const Vector3 &vec);
   1.212 +inline Vector3 operator -(const Vector3 &vec, scalar_t scalar);
   1.213 +inline Vector3 operator *(const Vector3 &vec, scalar_t scalar);
   1.214 +inline Vector3 operator *(scalar_t scalar, const Vector3 &vec);
   1.215 +inline Vector3 operator /(const Vector3 &vec, scalar_t scalar);
   1.216 +
   1.217 +inline void operator +=(Vector3 &vec, scalar_t scalar);
   1.218 +inline void operator -=(Vector3 &vec, scalar_t scalar);
   1.219 +inline void operator *=(Vector3 &vec, scalar_t scalar);
   1.220 +inline void operator /=(Vector3 &vec, scalar_t scalar);
   1.221 +
   1.222 +std::ostream &operator <<(std::ostream &out, const Vector3 &vec);
   1.223 +
   1.224 +inline Vector3 lerp(const Vector3 &a, const Vector3 &b, scalar_t t);
   1.225 +inline Vector3 catmull_rom_spline(const Vector3 &v0, const Vector3 &v1,
   1.226 +		const Vector3 &v2, const Vector3 &v3, scalar_t t);
   1.227 +
   1.228 +/* 4D Vector */
   1.229 +class Vector4 {
   1.230 +public:
   1.231 +	scalar_t x, y, z, w;
   1.232 +
   1.233 +	Vector4(scalar_t x = 0.0, scalar_t y = 0.0, scalar_t z = 0.0, scalar_t w = 0.0);
   1.234 +	Vector4(const vec4_t &vec);
   1.235 +	Vector4(const Vector2 &vec);
   1.236 +	Vector4(const Vector3 &vec);
   1.237 +
   1.238 +	inline scalar_t &operator [](int elem);
   1.239 +	inline const scalar_t &operator [](int elem) const;
   1.240 +
   1.241 +	inline scalar_t length() const;
   1.242 +	inline scalar_t length_sq() const;
   1.243 +	void normalize();
   1.244 +	Vector4 normalized() const;
   1.245 +
   1.246 +	void transform(const Matrix4x4 &mat);
   1.247 +	Vector4 transformed(const Matrix4x4 &mat) const;
   1.248 +
   1.249 +	Vector4 reflection(const Vector4 &normal) const;
   1.250 +	Vector4 refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const;
   1.251 +};
   1.252 +
   1.253 +
   1.254 +/* unary operations */
   1.255 +inline Vector4 operator -(const Vector4 &vec);
   1.256 +
   1.257 +/* binary vector (op) vector operations */
   1.258 +inline scalar_t dot_product(const Vector4 &v1, const Vector4 &v2);
   1.259 +inline Vector4 cross_product(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3);
   1.260 +
   1.261 +inline Vector4 operator +(const Vector4 &v1, const Vector4 &v2);
   1.262 +inline Vector4 operator -(const Vector4 &v1, const Vector4 &v2);
   1.263 +inline Vector4 operator *(const Vector4 &v1, const Vector4 &v2);
   1.264 +inline Vector4 operator /(const Vector4 &v1, const Vector4 &v2);
   1.265 +inline bool operator ==(const Vector4 &v1, const Vector4 &v2);
   1.266 +
   1.267 +inline void operator +=(Vector4 &v1, const Vector4 &v2);
   1.268 +inline void operator -=(Vector4 &v1, const Vector4 &v2);
   1.269 +inline void operator *=(Vector4 &v1, const Vector4 &v2);
   1.270 +inline void operator /=(Vector4 &v1, const Vector4 &v2);
   1.271 +
   1.272 +/* binary vector (op) scalar and scalar (op) vector operations */
   1.273 +inline Vector4 operator +(const Vector4 &vec, scalar_t scalar);
   1.274 +inline Vector4 operator +(scalar_t scalar, const Vector4 &vec);
   1.275 +inline Vector4 operator -(const Vector4 &vec, scalar_t scalar);
   1.276 +inline Vector4 operator *(const Vector4 &vec, scalar_t scalar);
   1.277 +inline Vector4 operator *(scalar_t scalar, const Vector4 &vec);
   1.278 +inline Vector4 operator /(const Vector4 &vec, scalar_t scalar);
   1.279 +
   1.280 +inline void operator +=(Vector4 &vec, scalar_t scalar);
   1.281 +inline void operator -=(Vector4 &vec, scalar_t scalar);
   1.282 +inline void operator *=(Vector4 &vec, scalar_t scalar);
   1.283 +inline void operator /=(Vector4 &vec, scalar_t scalar);
   1.284 +
   1.285 +std::ostream &operator <<(std::ostream &out, const Vector4 &vec);
   1.286 +
   1.287 +inline Vector4 lerp(const Vector4 &v0, const Vector4 &v1, scalar_t t);
   1.288 +inline Vector4 catmull_rom_spline(const Vector4 &v0, const Vector4 &v1,
   1.289 +		const Vector4 &v2, const Vector4 &v3, scalar_t t);
   1.290 +
   1.291 +#endif	/* __cplusplus */
   1.292 +
   1.293 +#include "vector.inl"
   1.294 +
   1.295 +#endif	/* VMATH_VECTOR_H_ */