-
Notifications
You must be signed in to change notification settings - Fork 5
/
poMath.h
146 lines (114 loc) · 3.62 KB
/
poMath.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2012 Potion Design. All rights reserved.
* This file is part of pocode.
*
* pocode is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* pocode is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with pocode. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#define _USE_MATH_DEFINES
#include <math.h>
#include <ctime>
#include <algorithm>
#include "poPoint.h"
#include "poRect.h"
#include "poColor.h"
#include "poMatrixSet.h"
inline bool compare(float f1, float f2, float range=1.0e-6f) {
return ::fabs(f1-f2) < range;
}
inline float deg2rad(float deg) {
return deg / 180.f * M_PI;
}
inline float rad2deg(float rad) {
return rad / M_PI * 180.f;
}
inline float sin_deg(float deg) {
return ::sinf(deg2rad(deg));
}
inline float cos_deg(float deg) {
return ::cosf(deg2rad(deg));
}
inline void poRandSeed() {
::srand((unsigned int)::time(NULL));
}
inline void poRandSeed(int seed) {
::srand(seed);
}
inline float poRand() {
return rand() / (float)RAND_MAX;
}
inline float poRand(float low, float high) {
return poRand() * (high-low) + low;
}
inline poPoint randomPointInRect(poRect r) {
return poPoint(poRand(r.x,r.x+r.width),
poRand(r.y,r.y+r.height));
}
inline poColor randomColor(float alpha=1.f) {
return poColor(poRand(), poRand(), poRand(), alpha);
}
inline void capToRange(float *f, float low, float high) {
*f = std::max(*f, low);
*f = std::min(*f, high);
}
inline void capToRange(poPoint *pt, float low, float high) {
capToRange(&pt->x, low, high);
capToRange(&pt->y, low, high);
capToRange(&pt->z, low, high);
}
// will round -.5 to -1
inline float roundSym(float f) {
return (f > 0.f) ? floor(f + 0.5f) : ceil(f - 0.5f);
}
inline poPoint roundSym(poPoint p) {
return poPoint(roundSym(p.x), roundSym(p.y), roundSym(p.z));
}
inline float round(float f) {
return floor(f + 0.5f);
}
inline poPoint round(poPoint p) {
return poPoint(round(p.x), round(p.y), round(p.z));
}
inline poPoint floor(poPoint p) {
return poPoint(floor(p.x), floor(p.y), floor(p.z));
}
template <typename T>
inline T poClamp(T min, T max, T val) {
return std::max(std::min(val, max), min);
}
inline float poMapf(float min1, float max1, float v, float min2, float max2) {
float tmp = (v-min1) / (max1-min1);
return tmp * (max2-min2) + min2;
}
inline float poDist(float x1, float y1, float z1, float x2, float y2, float z2) {
return sqrtf(powf((x2-x1), 2) + powf((y2-y1), 2) + powf((z2-z1), 2));
}
inline float poDist(poPoint p1, poPoint p2) {
return poDist(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z);
}
struct poRay {
poPoint origin, dir;
poRay(poPoint o, poPoint d)
: origin(o), dir(normalize(d)) {}
};
// angle ABC
float angleBetweenPoints(poPoint a, poPoint b, poPoint c);
// returns false if parallel, p1 = intersection or p1 and p2 = closest point on each
bool rayIntersection(poRay r1, poRay r2, poPoint *p1, poPoint *p2);
bool pointInTriangle( poPoint &P, poPoint &A, poPoint &B, poPoint &C );
bool pointInTriangle3D( poPoint P, poMatrixSet& M, poPoint A, poPoint B, poPoint C );
bool pointInRect3D( poPoint P, poMatrixSet& M, poRect R );
poPoint projectOntoLine(poPoint p, poPoint a, poPoint b);
float distanceToLine(poPoint p, poPoint a, poPoint b);