22 #ifndef QDENGINE_XMATH_H 23 #define QDENGINE_XMATH_H 25 #include "common/scummsys.h" 48 #define M_PI 3.14159265358979323846f 50 #define M_PI_2 1.57079632679489661923f 52 #define M_PI_4 0.785398163397448309616f 54 const double DBL_EPS = 1.e-15;
55 const double DBL_INF = 1.e+100;
56 const double DBL_COMPARE_TOLERANCE = 1.e-10;
58 const float FLT_EPS = 1.192092896e-07f;
59 const float FLT_INF = 1.e+30f;
60 const float FLT_COMPARE_TOLERANCE = 1.e-5f;
62 const int INT_INF = 0x7fffffff;
64 inline float invSqrtFast(
float x) {
66 float xhalf = 0.5f * x;
70 i = 0x5f375a86 - (i >> 1);
72 x = x * (1.5f - xhalf * x * x);
76 inline float cycle(
float f,
float size) {
77 return fmod(fmod(f, size) + size, size);
80 inline float getDist(
float v0,
float v1,
float size) {
81 float d = fmod(v0 - v1, size);
82 float ad = (float)fabs(d);
84 if (ad <= dd)
return d;
85 return d < 0 ? d + size : d - size;
88 inline float getDeltaAngle(
float to,
float from) {
89 return getDist(to, from, 2 * M_PI);
92 inline float cycleAngle(
float a) {
93 return cycle(a, 2 * M_PI);
104 inline T sqr(
const T &x) {
108 #define G2R(x) ((x)*M_PI/180.f) 109 #define R2G(x) ((x)*180.f/M_PI) 121 inline Vect2f() { x = y = 0.0; }
122 inline Vect2f(
float x_,
float y_) {
127 typedef float float2[2];
128 inline Vect2f(
const float2 &v) {
136 inline Vect2f &
set(
float x_,
float y_) {
141 inline Vect2f operator - ()
const {
145 inline int xi()
const {
148 inline int yi()
const {
152 inline const float &operator[](
int i)
const {
155 inline float &operator[](
int i) {
179 inline Vect2f &operator *= (
float f) {
184 inline Vect2f &operator /= (
float f) {
185 if (f != 0.f) f = 1 / f;
193 return Vect2f(*
this) += v;
196 return Vect2f(*
this) -= v;
199 return Vect2f(*
this) *= v;
202 return Vect2f(*
this) /= v;
204 inline Vect2f operator * (
float f)
const {
205 return Vect2f(*
this) *= f;
207 inline Vect2f operator / (
float f)
const {
208 return Vect2f(*
this) /= f;
211 inline bool eq(
const Vect2f &v,
float delta = FLT_COMPARE_TOLERANCE)
const {
212 return fabsf(v.x - x) < delta && fabsf(v.y - y) < delta;
215 inline float dot(
const Vect2f &v)
const {
216 return x * v.x + y * v.y;
218 inline friend float dot(
const Vect2f &u,
const Vect2f &v) {
222 inline float operator % (
const Vect2f &v)
const {
223 return x * v.y - y * v.x;
226 inline Vect2f &scaleAdd(
const Vect2f &u,
float lambda) {
234 inline float norm()
const {
235 return sqrtf(x * x + y * y);
237 inline float norm2()
const {
238 return x * x + y * y;
240 inline Vect2f &normalize(
float norma) {
241 float f = norma * invSqrtFast(x * x + y * y);
246 inline float distance(
const Vect2f &v)
const {
247 return sqrtf(distance2(v));
249 inline float distance2(
const Vect2f &v)
const {
250 float dx = x - v.x, dy = y - v.y;
251 return dx * dx + dy * dy;
254 inline void swap(
Vect2f &v) {
272 inline Vect2i() { x = y = 0; }
273 inline Vect2i(
int x_,
int y_) {
277 inline Vect2i(
float x_,
float y_) {
288 inline void set(
int x_,
int y_) {
292 inline void set(
float x_,
float y_) {
296 inline Vect2i operator - ()
const {
300 inline const int &operator[](
int i)
const {
303 inline int &operator[](
int i) {
329 return Vect2i(*
this) += v;
332 return Vect2i(*
this) -= v;
335 return Vect2i(*
this) *= v;
338 inline Vect2i &operator *= (
int f) {
343 inline Vect2i operator * (
int f)
const {
344 return Vect2i(*
this) *= f;
347 inline Vect2i &operator >>= (
int n) {
352 inline Vect2i operator >> (
int n)
const {
353 return Vect2i(*
this) >>= n;
356 inline Vect2i &operator *= (
float f) {
361 inline Vect2i &operator /= (
float f) {
362 return *
this *= 1.f / f;
364 inline Vect2i operator * (
float f)
const {
365 return Vect2i(*
this) *= f;
367 inline Vect2i operator / (
float f)
const {
368 return Vect2i(*
this) /= f;
371 inline int dot(
const Vect2i &v)
const {
372 return x * v.x + y * v.y;
374 inline friend int dot(
const Vect2i &u,
const Vect2i &v) {
378 inline int operator % (
const Vect2i &v)
const {
379 return x * v.y - y * v.x;
382 inline int norm()
const {
383 return round(sqrtf(
float(x * x + y * y)));
385 inline int norm2()
const {
386 return x * x + y * y;
389 inline void normalize(
int norma) {
390 float f = (float)norma * invSqrtFast((
float)(x * x + y * y));
394 inline int distance2(
const Vect2i &v)
const {
395 return sqr(x - v.x) + sqr(y - v.y);
398 inline int operator == (
const Vect2i &v)
const {
399 return x == v.x && y == v.y;
401 inline int operator != (
const Vect2i &v)
const {
402 return x != v.x || y != v.y;
405 inline void swap(
Vect2i &v) {
423 inline Vect2s() { x = y = 0; }
424 inline Vect2s(
int x_,
int y_) {
438 inline void set(
int x_,
int y_) {
442 inline Vect2s operator - ()
const {
446 inline const int16 &operator[](
int i)
const {
449 inline int16 &operator[](
int i) {
468 inline Vect2s &operator *= (
float f) {
473 inline Vect2s &operator /= (
float f) {
474 if (f != 0.f) f = 1 / f;
481 return Vect2s(x - v.x, y - v.y);
484 return Vect2s(x + v.x, y + v.y);
487 return Vect2s(x * v.x, y * v.y);
489 inline Vect2s operator * (
float f)
const {
490 Vect2s tmp(round(x * f), round(y * f));
493 inline Vect2s operator / (
float f)
const {
494 if (f != 0.f) f = 1 / f;
496 Vect2s tmp(round(x * f), round(y * f));
500 inline int operator == (
const Vect2s &v)
const {
501 return x == v.x && y == v.y;
504 inline int norm()
const {
505 return round(sqrtf((
float)(x * x + y * y)));
507 inline int norm2()
const {
508 return x * x + y * y;
510 inline int distance(
const Vect2s &v)
const {
511 int dx = v.x - x, dy = v.y - y;
512 return round(sqrtf((
float)(dx * dx + dy * dy)));
514 inline void normalize(
int norma) {
515 float f = (float)norma * invSqrtFast((
float)((int)x * x + (
int)y * y));
520 inline void swap(
Vect2s &v) {
536 typedef float float3[3];
542 inline Vect3f() { x = y = z = 0.0;}
543 inline Vect3f(
float x_,
float y_,
float z_) {
554 inline Vect3f(
const float3 &v) {
560 inline operator const Vect2f &()
const {
561 return *
reinterpret_cast<const Vect2f *
>(
this);
566 inline Vect3f &
set(
float x_,
float y_,
float z_) {
573 inline Vect3f &setSpherical(
float psi,
float theta,
float radius);
576 inline const float &operator[](
int i)
const {
579 inline float &operator[](
int i) {
584 inline const float &operator()(
int i)
const {
585 return *(&x + i - 1);
587 inline float &operator()(
int i) {
588 return *(&x + i - 1);
592 inline int xi()
const {
595 inline int yi()
const {
598 inline int zi()
const {
603 inline Vect3f operator- ()
const;
608 inline bool eq(
const Vect3f &v,
float delta = FLT_COMPARE_TOLERANCE)
const;
623 return u.add(*
this, v);
627 return u.sub(*
this, v);
643 return u.mult(*
this, v);
647 return u.div(*
this, v);
659 return u.cross(*
this, v);
663 inline float dot(
const Vect3f &other)
const;
664 inline friend float dot(
const Vect3f &u,
const Vect3f &v) {
672 inline Vect3f &operator*= (
float s) {
675 inline Vect3f &operator/= (
float s) {
678 inline Vect3f operator* (
float s)
const {
680 return u.scale(*
this, s);
682 inline Vect3f operator/ (
float s)
const {
684 return u.scale(*
this, 1 / s);
686 inline friend Vect3f operator* (
float s,
const Vect3f &v) {
688 return u.scale(v, s);
692 inline Vect3f &normalize(
float r = 1.0f);
693 inline Vect3f &normalize(
const Vect3f &v,
float r = 1.0f);
696 inline float norm()
const;
697 inline float norm2()
const;
698 inline float distance(
const Vect3f &other)
const;
699 inline float distance2(
const Vect3f &other)
const;
701 inline float psi()
const;
702 inline float theta()
const;
704 inline float min()
const;
705 inline float max()
const;
706 inline float minAbs()
const;
707 inline float maxAbs()
const;
708 inline float sumAbs()
const;
719 inline void swap(
Vect3f &other);
734 v_normal.scale(axis, dot(axis, v) / ((axis).norm2()));
735 v_tangent.sub(v, v_normal);
753 inline Vect2i::Vect2i(
const Vect2s &v) {
757 inline Vect2f::Vect2f(
const Vect2i &v) {
761 inline Vect2f::Vect2f(
const Vect2s &v) {
767 float lambda2 = 1.0f - lambda;
769 x = lambda2 * u.x + lambda * v.x;
770 y = lambda2 * u.y + lambda * v.y;
784 bool Vect3f::eq(
const Vect3f &other,
float delta)
const {
785 return fabs(x - other.x) < delta &&
786 fabs(y - other.y) < delta &&
787 fabs(z - other.z) < delta;
790 Vect3f Vect3f::operator- ()
const {
791 return Vect3f(-x, -y, -z);
795 float Vect3f::sumAbs()
const {
796 return (
float)(fabs(x) + fabs(y) + fabs(z));
801 float Vect3f::psi()
const {
802 return (
float)atan2(y, x);
804 float Vect3f::theta()
const {
805 return (
float)acos(z / (norm() + FLT_EPS));
807 Vect3f &Vect3f::setSpherical(
float psi,
float theta,
float radius) {
808 x = radius * (float)sin(theta);
809 y = x * (float)sin(psi);
810 x = x * (float)cos(psi);
811 z = radius * (float)cos(theta);
815 float Vect3f::dot(
const Vect3f &other)
const {
816 return x * other.x + y * other.y + z * other.z;
820 float Vect3f::norm()
const {
821 return (
float)sqrt(x * x + y * y + z * z);
825 float Vect3f::norm2()
const {
826 return (x * x + y * y + z * z);
830 float Vect3f::distance(
const Vect3f &other)
const {
838 float Vect3f::distance2(
const Vect3f &other)
const {
846 float Vect3f::min()
const {
847 return (x <= y) ? ((x <= z) ? x : z) : ((y <= z) ? y : z);
851 float Vect3f::max()
const {
852 return (x >= y) ? ((x >= z) ? x : z) : ((y >= z) ? y : z);
856 float Vect3f::minAbs()
const {
862 return (ax <= ay) ? ((ax <= az) ? ax : az) : ((ay <= az) ? ay : az);
866 float Vect3f::maxAbs()
const {
872 return (ax >= ay) ? ((ax >= az) ? ax : az) : ((ay >= az) ? ay : az);
876 void Vect3f::swap(
Vect3f &other) {
886 float s = r * invSqrtFast(v.x * v.x + v.y * v.y + v.z * v.z);
894 Vect3f &Vect3f::normalize(
float r) {
895 float s = r * invSqrtFast(x * x + y * y + z * z);
910 Vect3f &Vect3f::negate() {
990 Vect3f &Vect3f::scale(
float s) {
1000 x = u.y * v.z - u.z * v.y;
1001 y = u.z * v.x - u.x * v.z;
1002 z = u.x * v.y - u.y * v.x;
1012 x = v.y * z - v.z * oy;
1013 y = v.z * ox - v.x * z;
1014 z = v.x * oy - v.y * ox;
1024 x = oy * v.z - z * v.y;
1025 y = z * v.x - ox * v.z;
1026 z = ox * v.y - oy * v.x;
1032 x = u.y * v.z - u.z * v.y + w.x;
1033 y = u.z * v.x - u.x * v.z + w.y;
1034 z = u.x * v.y - u.y * v.x + w.z;
1040 x += u.y * v.z - u.z * v.y;
1041 y += u.z * v.x - u.x * v.z;
1042 z += u.x * v.y - u.y * v.x;
1048 x = v.x + lambda * u.x;
1049 y = v.y + lambda * u.y;
1050 z = v.z + lambda * u.z;
1055 Vect3f &Vect3f::scaleAdd(
const Vect3f &u,
float lambda) {
1064 float lambda2 = 1.0f - lambda;
1066 x = lambda2 * u.x + lambda * v.x;
1067 y = lambda2 * u.y + lambda * v.y;
1068 z = lambda2 * u.z + lambda * v.z;
1074 #endif // QDENGINE_XMATH_H
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Базовый класс для игровых ресурсов.
Definition: console.h:28