| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifndef _TVG_MATH_H_ |
| 24 |
#define _TVG_MATH_H_ |
| 25 |
|
| 26 |
#define _USE_MATH_DEFINES |
| 27 |
|
| 28 |
#include <float.h> |
| 29 |
#include <cmath> |
| 30 |
#include "tvgCommon.h" |
| 31 |
|
| 32 |
namespace tvg |
| 33 |
{ |
| 34 |
|
| 35 |
#define MATH_PI 3.14159265358979323846f |
| 36 |
#define MATH_PI2 1.57079632679489661923f |
| 37 |
#define FLOAT_EPSILON 1.0e-06f //1.192092896e-07f |
| 38 |
#define PATH_KAPPA 0.552284f |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
float atan2(float y, float x); |
| 45 |
|
| 46 |
|
| 47 |
static inline float deg2rad(float degree) |
| 48 |
{ |
| 49 |
return degree * (MATH_PI / 180.0f); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
static inline float rad2deg(float radian) |
| 54 |
{ |
| 55 |
return radian * (180.0f / MATH_PI); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
static inline bool zero(float a) |
| 60 |
{ |
| 61 |
return (fabsf(a) <= FLOAT_EPSILON) ? true : false; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static inline bool equal(float a, float b) |
| 66 |
{ |
| 67 |
return tvg::zero(a - b); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
template <typename T> |
| 72 |
static inline void clamp(T& v, const T& min, const T& max) |
| 73 |
{ |
| 74 |
if (v < min) v = min; |
| 75 |
else if (v > max) v = max; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
void rotate(Matrix* m, float degree); |
| 83 |
bool inverse(const Matrix* m, Matrix* out); |
| 84 |
bool identity(const Matrix* m); |
| 85 |
Matrix operator*(const Matrix& lhs, const Matrix& rhs); |
| 86 |
bool operator==(const Matrix& lhs, const Matrix& rhs); |
| 87 |
|
| 88 |
|
| 89 |
static inline float radian(const Matrix& m) |
| 90 |
{ |
| 91 |
return fabsf(tvg::atan2(m.e21, m.e11)); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
static inline bool rightAngle(const Matrix& m) |
| 96 |
{ |
| 97 |
auto radian = tvg::radian(m); |
| 98 |
if (tvg::zero(radian) || tvg::zero(radian - MATH_PI2) || tvg::zero(radian - MATH_PI)) return true; |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
static inline bool skewed(const Matrix& m) |
| 104 |
{ |
| 105 |
return !tvg::zero(m.e21 + m.e12); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
static inline void identity(Matrix* m) |
| 110 |
{ |
| 111 |
m->e11 = 1.0f; |
| 112 |
m->e12 = 0.0f; |
| 113 |
m->e13 = 0.0f; |
| 114 |
m->e21 = 0.0f; |
| 115 |
m->e22 = 1.0f; |
| 116 |
m->e23 = 0.0f; |
| 117 |
m->e31 = 0.0f; |
| 118 |
m->e32 = 0.0f; |
| 119 |
m->e33 = 1.0f; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
static inline void scale(Matrix* m, float sx, float sy) |
| 124 |
{ |
| 125 |
m->e11 *= sx; |
| 126 |
m->e22 *= sy; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
static inline void scaleR(Matrix* m, float x, float y) |
| 131 |
{ |
| 132 |
if (x != 1.0f) { |
| 133 |
m->e11 *= x; |
| 134 |
m->e21 *= x; |
| 135 |
} |
| 136 |
if (y != 1.0f) { |
| 137 |
m->e22 *= y; |
| 138 |
m->e12 *= y; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
static inline void translate(Matrix* m, float x, float y) |
| 144 |
{ |
| 145 |
m->e13 += x; |
| 146 |
m->e23 += y; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
static inline void translateR(Matrix* m, float x, float y) |
| 151 |
{ |
| 152 |
if (x == 0.0f && y == 0.0f) return; |
| 153 |
m->e13 += (x * m->e11 + y * m->e12); |
| 154 |
m->e23 += (x * m->e21 + y * m->e22); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
static inline bool operator!=(const Matrix& lhs, const Matrix& rhs) |
| 159 |
{ |
| 160 |
return !(lhs == rhs); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
static inline void operator*=(Matrix& lhs, const Matrix& rhs) |
| 165 |
{ |
| 166 |
lhs = lhs * rhs; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
static inline void log(const Matrix& m) |
| 171 |
{ |
| 172 |
TVGLOG("COMMON", "Matrix: [%f %f %f] [%f %f %f] [%f %f %f]", m.e11, m.e12, m.e13, m.e21, m.e22, m.e23, m.e31, m.e32, m.e33); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
void operator*=(Point& pt, const Matrix& m); |
| 181 |
Point operator*(const Point& pt, const Matrix& m); |
| 182 |
Point normal(const Point& p1, const Point& p2); |
| 183 |
|
| 184 |
static inline float cross(const Point& lhs, const Point& rhs) |
| 185 |
{ |
| 186 |
return lhs.x * rhs.y - rhs.x * lhs.y; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
static inline bool zero(const Point& p) |
| 191 |
{ |
| 192 |
return tvg::zero(p.x) && tvg::zero(p.y); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
static inline float length(const Point* a, const Point* b) |
| 197 |
{ |
| 198 |
auto x = b->x - a->x; |
| 199 |
auto y = b->y - a->y; |
| 200 |
|
| 201 |
if (x < 0) x = -x; |
| 202 |
if (y < 0) y = -y; |
| 203 |
|
| 204 |
return (x > y) ? (x + 0.375f * y) : (y + 0.375f * x); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
static inline float length(const Point& a) |
| 209 |
{ |
| 210 |
return sqrtf(a.x * a.x + a.y * a.y); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
static inline bool operator==(const Point& lhs, const Point& rhs) |
| 215 |
{ |
| 216 |
return tvg::equal(lhs.x, rhs.x) && tvg::equal(lhs.y, rhs.y); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
static inline bool operator!=(const Point& lhs, const Point& rhs) |
| 221 |
{ |
| 222 |
return !(lhs == rhs); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
static inline Point operator-(const Point& lhs, const Point& rhs) |
| 227 |
{ |
| 228 |
return {lhs.x - rhs.x, lhs.y - rhs.y}; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
static inline Point operator+(const Point& lhs, const Point& rhs) |
| 233 |
{ |
| 234 |
return {lhs.x + rhs.x, lhs.y + rhs.y}; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
static inline Point operator*(const Point& lhs, float rhs) |
| 239 |
{ |
| 240 |
return {lhs.x * rhs, lhs.y * rhs}; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
static inline Point operator*(const float& lhs, const Point& rhs) |
| 245 |
{ |
| 246 |
return {lhs * rhs.x, lhs * rhs.y}; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
static inline Point operator/(const Point& lhs, const float rhs) |
| 251 |
{ |
| 252 |
return {lhs.x / rhs, lhs.y / rhs}; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
static inline void log(const Point& pt) |
| 257 |
{ |
| 258 |
TVGLOG("COMMON", "Point: [%f %f]", pt.x, pt.y); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
struct Line |
| 267 |
{ |
| 268 |
Point pt1; |
| 269 |
Point pt2; |
| 270 |
|
| 271 |
void split(float at, Line& left, Line& right) const; |
| 272 |
float length() const; |
| 273 |
}; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
struct Bezier |
| 281 |
{ |
| 282 |
Point start; |
| 283 |
Point ctrl1; |
| 284 |
Point ctrl2; |
| 285 |
Point end; |
| 286 |
|
| 287 |
void split(float t, Bezier& left); |
| 288 |
void split(Bezier& left, Bezier& right) const; |
| 289 |
void split(float at, Bezier& left, Bezier& right) const; |
| 290 |
float length() const; |
| 291 |
float lengthApprox() const; |
| 292 |
float at(float at, float length) const; |
| 293 |
float atApprox(float at, float length) const; |
| 294 |
Point at(float t) const; |
| 295 |
float angle(float t) const; |
| 296 |
}; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
template <typename T> |
| 304 |
static inline T lerp(const T &start, const T &end, float t) |
| 305 |
{ |
| 306 |
return static_cast<T>(start + (end - start) * t); |
| 307 |
} |
| 308 |
|
| 309 |
uint8_t lerp(const uint8_t &start, const uint8_t &end, float t); |
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
#endif |