framework2
268 строк · 7.4 Кб
1#include "ofMath.h"
2#include "ofNoise.h"
3#include "ofPolyline.h"
4#include <float.h>
5
6#ifndef TARGET_WIN32
7#include <sys/time.h>
8#endif
9
10//--------------------------------------------------
11int ofNextPow2(int a){
12// from nehe.gamedev.net lesson 43
13int rval=1;
14while(rval<a) rval<<=1;
15return rval;
16}
17
18//--------------------------------------------------
19void ofSeedRandom() {
20
21#ifdef TARGET_WIN32
22srand(GetTickCount());
23#elif !defined(TARGET_EMSCRIPTEN)
24// use XOR'd second, microsecond precision AND pid as seed
25struct timeval tv;
26gettimeofday(&tv, 0);
27long int n = (tv.tv_sec ^ tv.tv_usec) ^ getpid();
28srand(n);
29#else
30struct timeval tv;
31gettimeofday(&tv, 0);
32long int n = (tv.tv_sec ^ tv.tv_usec);
33srand(n);
34#endif
35}
36
37//--------------------------------------------------
38void ofSetRandomSeed(unsigned long new_seed) {
39#ifdef __linux__
40#warning
41#else
42of::random::seed(new_seed);
43#endif
44}
45
46void ofSeedRandom(int val) {
47#ifdef __linux__
48srand((long) val);
49#else
50ofSetRandomSeed(val);
51#endif
52}
53
54//--------------------------------------------------
55float ofRandom(float max) {
56return (max * rand() / float(RAND_MAX)) * (1.0f - std::numeric_limits<float>::epsilon());
57}
58
59//--------------------------------------------------
60float ofRandom(float x, float y) {
61float high = std::max(x, y);
62float low = std::min(x, y);
63return std::max(low, (low + ((high - low) * rand() / float(RAND_MAX))) * (1.0f - std::numeric_limits<float>::epsilon()));
64}
65
66//--------------------------------------------------
67float ofRandomf() {
68return -1.0f + (2.0f * rand() / float(RAND_MAX)) * (1.0f - std::numeric_limits<float>::epsilon());
69}
70
71//--------------------------------------------------
72float ofRandomuf() {
73return (rand() / float(RAND_MAX)) * (1.0f - std::numeric_limits<float>::epsilon());
74}
75
76//---- new to 006
77//from the forums http://www.openframeworks.cc/forum/viewtopic.php?t=1413
78
79//--------------------------------------------------
80float ofNormalize(float value, float min, float max){
81return ofClamp( (value - min) / (max - min), 0, 1);
82}
83
84//check for division by zero???
85//--------------------------------------------------
86float ofMap(float value, float inputMin, float inputMax, float outputMin, float outputMax, bool clamp) {
87
88if (fabs(inputMin - inputMax) < std::numeric_limits<float>::epsilon()){
89return outputMin;
90} else {
91float outVal = ((value - inputMin) / (inputMax - inputMin) * (outputMax - outputMin) + outputMin);
92
93if( clamp ){
94if(outputMax < outputMin){
95if( outVal < outputMax )outVal = outputMax;
96else if( outVal > outputMin )outVal = outputMin;
97}else{
98if( outVal > outputMax )outVal = outputMax;
99else if( outVal < outputMin )outVal = outputMin;
100}
101}
102return outVal;
103}
104
105}
106
107//--------------------------------------------------
108float ofDist(float x1, float y1, float x2, float y2) {
109return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
110}
111
112//--------------------------------------------------
113float ofDist(float x1, float y1, float z1, float x2, float y2, float z2) {
114return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
115}
116
117//--------------------------------------------------
118float ofDistSquared(float x1, float y1, float x2, float y2) {
119return ( (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) );
120}
121
122//--------------------------------------------------
123float ofDistSquared(float x1, float y1, float z1, float x2, float y2, float z2) {
124return ( (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2) );
125}
126
127//--------------------------------------------------
128float ofClamp(float value, float min, float max) {
129return value < min ? min : value > max ? max : value;
130}
131
132//--------------------------------------------------
133int ofSign(float n) {
134if( n > 0 ) return 1;
135else if(n < 0) return -1;
136else return 0;
137}
138
139//--------------------------------------------------
140bool ofInRange(float t, float min, float max) {
141return t>=min && t<=max;
142}
143
144//--------------------------------------------------
145float ofRadToDeg(float radians) {
146return glm::degrees(radians);
147}
148
149//--------------------------------------------------
150float ofDegToRad(float degrees) {
151return glm::radians(degrees);
152}
153
154//--------------------------------------------------
155float ofLerp(float start, float stop, float amt) {
156return start + (stop-start) * amt;
157}
158
159float ofWrap(float value, float from, float to){
160// algorithm from http://stackoverflow.com/a/5852628/599884
161if(from > to){
162std::swap(from, to);
163}
164float cycle = to - from;
165if(ofIsFloatEqual(cycle, 0.0f)){
166return to;
167}
168return value - cycle * floor((value - from) / cycle);
169}
170
171//--------------------------------------------------
172float ofWrapRadians(float angle, float from, float to){
173return ofWrap(angle, from, to);
174}
175
176float ofWrapDegrees(float angle, float from, float to){
177return ofWrap(angle, from, to);
178}
179
180//--------------------------------------------------
181float ofLerpDegrees(float currentAngle, float targetAngle, float pct) {
182return currentAngle + ofAngleDifferenceDegrees(currentAngle,targetAngle) * pct;
183}
184
185//--------------------------------------------------
186float ofLerpRadians(float currentAngle, float targetAngle, float pct) {
187return currentAngle + ofAngleDifferenceRadians(currentAngle,targetAngle) * pct;
188}
189
190//--------------------------------------------------
191float ofNoise(float x){
192return _slang_library_noise1(x)*0.5f + 0.5f;
193}
194
195//--------------------------------------------------
196float ofNoise(float x, float y){
197return _slang_library_noise2(x,y)*0.5f + 0.5f;
198}
199
200//--------------------------------------------------
201float ofNoise(const glm::vec2& p){
202return ofNoise( p.x, p.y );
203}
204
205//--------------------------------------------------
206float ofNoise(float x, float y, float z){
207return _slang_library_noise3(x,y,z)*0.5f + 0.5f;
208}
209
210//--------------------------------------------------
211float ofNoise(const glm::vec3& p){
212return ofNoise( p.x, p.y, p.z );
213}
214
215//--------------------------------------------------
216float ofNoise(float x, float y, float z, float w){
217return _slang_library_noise4(x,y,z,w)*0.5f + 0.5f;
218}
219
220//--------------------------------------------------
221float ofNoise(const glm::vec4& p){
222return ofNoise( p.x, p.y, p.z, p.w );
223}
224
225//--------------------------------------------------
226float ofSignedNoise(float x){
227return _slang_library_noise1(x);
228}
229
230//--------------------------------------------------
231float ofSignedNoise(float x, float y){
232return _slang_library_noise2(x,y);
233}
234
235//--------------------------------------------------
236float ofSignedNoise(const glm::vec2& p){
237return ofSignedNoise( p.x, p.y );
238}
239
240//--------------------------------------------------
241float ofSignedNoise(float x, float y, float z){
242return _slang_library_noise3(x,y,z);
243}
244
245//--------------------------------------------------
246float ofSignedNoise(const glm::vec3& p){
247return ofSignedNoise( p.x, p.y, p.z );
248}
249
250//--------------------------------------------------
251float ofSignedNoise(float x, float y, float z, float w){
252return _slang_library_noise4(x,y,z,w);
253}
254
255//--------------------------------------------------
256float ofSignedNoise(const glm::vec4& p){
257return ofSignedNoise( p.x, p.y, p.z, p.w );
258}
259
260//--------------------------------------------------
261float ofAngleDifferenceDegrees(float currentAngle, float targetAngle) {
262return ofWrapDegrees(targetAngle - currentAngle);
263}
264
265//--------------------------------------------------
266float ofAngleDifferenceRadians(float currentAngle, float targetAngle) {
267return ofWrapRadians(targetAngle - currentAngle);
268}
269
270
271