framework2

Форк
0
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
//--------------------------------------------------
11
int ofNextPow2(int a){
12
	// from nehe.gamedev.net lesson 43
13
	int rval=1;
14
	while(rval<a) rval<<=1;
15
	return rval;
16
}
17

18
//--------------------------------------------------
19
void ofSeedRandom() {
20

21
	#ifdef TARGET_WIN32
22
		srand(GetTickCount());
23
	#elif !defined(TARGET_EMSCRIPTEN)
24
		// use XOR'd second, microsecond precision AND pid as seed
25
		struct timeval tv;
26
		gettimeofday(&tv, 0);
27
		long int n = (tv.tv_sec ^ tv.tv_usec) ^ getpid();
28
		srand(n);
29
	#else
30
		struct timeval tv;
31
		gettimeofday(&tv, 0);
32
		long int n = (tv.tv_sec ^ tv.tv_usec);
33
		srand(n);
34
	#endif
35
}
36

37
//--------------------------------------------------
38
void ofSetRandomSeed(unsigned long new_seed) {
39
#ifdef __linux__
40
#warning
41
#else
42
    of::random::seed(new_seed);
43
#endif
44
}
45

46
void ofSeedRandom(int val) {
47
#ifdef __linux__
48
    srand((long) val);
49
#else
50
    ofSetRandomSeed(val);
51
#endif
52
}
53

54
//--------------------------------------------------
55
float ofRandom(float max) {
56
	return (max * rand() / float(RAND_MAX)) * (1.0f - std::numeric_limits<float>::epsilon());
57
}
58

59
//--------------------------------------------------
60
float ofRandom(float x, float y) {
61
	float high = std::max(x, y);
62
	float low = std::min(x, y);
63
	return std::max(low, (low + ((high - low) * rand() / float(RAND_MAX))) * (1.0f - std::numeric_limits<float>::epsilon()));
64
}
65

66
//--------------------------------------------------
67
float ofRandomf() {
68
	return -1.0f + (2.0f * rand() / float(RAND_MAX)) * (1.0f - std::numeric_limits<float>::epsilon());
69
}
70

71
//--------------------------------------------------
72
float ofRandomuf() {
73
	return (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
//--------------------------------------------------
80
float ofNormalize(float value, float min, float max){
81
	return ofClamp( (value - min) / (max - min), 0, 1);
82
}
83

84
//check for division by zero???
85
//--------------------------------------------------
86
float ofMap(float value, float inputMin, float inputMax, float outputMin, float outputMax, bool clamp) {
87

88
	if (fabs(inputMin - inputMax) < std::numeric_limits<float>::epsilon()){
89
		return outputMin;
90
	} else {
91
		float outVal = ((value - inputMin) / (inputMax - inputMin) * (outputMax - outputMin) + outputMin);
92
	
93
		if( clamp ){
94
			if(outputMax < outputMin){
95
				if( outVal < outputMax )outVal = outputMax;
96
				else if( outVal > outputMin )outVal = outputMin;
97
			}else{
98
				if( outVal > outputMax )outVal = outputMax;
99
				else if( outVal < outputMin )outVal = outputMin;
100
			}
101
		}
102
		return outVal;
103
	}
104

105
}
106

107
//--------------------------------------------------
108
float ofDist(float x1, float y1, float x2, float y2) {
109
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
110
}
111

112
//--------------------------------------------------
113
float ofDist(float x1, float y1, float z1, float x2, float y2, float z2) {
114
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
115
}
116

117
//--------------------------------------------------
118
float ofDistSquared(float x1, float y1, float x2, float y2) {
119
	return ( (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) );
120
}
121

122
//--------------------------------------------------
123
float ofDistSquared(float x1, float y1, float z1, float x2, float y2, float z2) {
124
	return ( (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2) );
125
}
126

127
//--------------------------------------------------
128
float ofClamp(float value, float min, float max) {
129
	return value < min ? min : value > max ? max : value;
130
}
131

132
//--------------------------------------------------
133
int ofSign(float n) {
134
	if( n > 0 ) return 1;
135
	else if(n < 0) return -1;
136
	else return 0;
137
}
138

139
//--------------------------------------------------
140
bool ofInRange(float t, float min, float max) {
141
	return t>=min && t<=max;
142
}
143

144
//--------------------------------------------------
145
float ofRadToDeg(float radians) {
146
	return glm::degrees(radians);
147
}
148

149
//--------------------------------------------------
150
float ofDegToRad(float degrees) {
151
	return glm::radians(degrees);
152
}
153

154
//--------------------------------------------------
155
float ofLerp(float start, float stop, float amt) {
156
	return start + (stop-start) * amt;
157
}
158

159
float ofWrap(float value, float from, float to){
160
	// algorithm from http://stackoverflow.com/a/5852628/599884
161
	if(from > to){
162
		std::swap(from, to);
163
	}
164
	float cycle = to - from;
165
	if(ofIsFloatEqual(cycle, 0.0f)){
166
		return to;
167
	}
168
	return value - cycle * floor((value - from) / cycle);
169
}
170

171
//--------------------------------------------------
172
float ofWrapRadians(float angle, float from, float to){
173
	return ofWrap(angle, from, to);
174
}
175

176
float ofWrapDegrees(float angle, float from, float to){
177
	return ofWrap(angle, from, to);
178
}
179

180
//--------------------------------------------------
181
float ofLerpDegrees(float currentAngle, float targetAngle, float pct) {
182
    return currentAngle + ofAngleDifferenceDegrees(currentAngle,targetAngle) * pct;
183
}
184

185
//--------------------------------------------------
186
float ofLerpRadians(float currentAngle, float targetAngle, float pct) {
187
	return currentAngle + ofAngleDifferenceRadians(currentAngle,targetAngle) * pct;
188
}
189

190
//--------------------------------------------------
191
float ofNoise(float x){
192
	return _slang_library_noise1(x)*0.5f + 0.5f;
193
}
194

195
//--------------------------------------------------
196
float ofNoise(float x, float y){
197
	return _slang_library_noise2(x,y)*0.5f + 0.5f;
198
}
199

200
//--------------------------------------------------
201
float ofNoise(const glm::vec2& p){
202
	return ofNoise( p.x, p.y );
203
}
204

205
//--------------------------------------------------
206
float ofNoise(float x, float y, float z){
207
	return _slang_library_noise3(x,y,z)*0.5f + 0.5f;
208
}
209

210
//--------------------------------------------------
211
float ofNoise(const glm::vec3& p){
212
	return ofNoise( p.x, p.y, p.z );
213
}
214

215
//--------------------------------------------------
216
float ofNoise(float x, float y, float z, float w){
217
	return _slang_library_noise4(x,y,z,w)*0.5f + 0.5f;
218
}
219

220
//--------------------------------------------------
221
float ofNoise(const glm::vec4& p){
222
	return ofNoise( p.x, p.y, p.z, p.w );
223
}
224

225
//--------------------------------------------------
226
float ofSignedNoise(float x){
227
	return _slang_library_noise1(x);
228
}
229

230
//--------------------------------------------------
231
float ofSignedNoise(float x, float y){
232
	return _slang_library_noise2(x,y);
233
}
234

235
//--------------------------------------------------
236
float ofSignedNoise(const glm::vec2& p){
237
	return ofSignedNoise( p.x, p.y );
238
}
239

240
//--------------------------------------------------
241
float ofSignedNoise(float x, float y, float z){
242
	return _slang_library_noise3(x,y,z);
243
}
244

245
//--------------------------------------------------
246
float ofSignedNoise(const glm::vec3& p){
247
	return ofSignedNoise( p.x, p.y, p.z );
248
}
249

250
//--------------------------------------------------
251
float ofSignedNoise(float x, float y, float z, float w){
252
	return _slang_library_noise4(x,y,z,w);
253
}
254

255
//--------------------------------------------------
256
float ofSignedNoise(const glm::vec4& p){
257
	return ofSignedNoise( p.x, p.y, p.z, p.w );
258
}
259

260
//--------------------------------------------------
261
float ofAngleDifferenceDegrees(float currentAngle, float targetAngle) {
262
	return ofWrapDegrees(targetAngle - currentAngle);
263
}
264

265
//--------------------------------------------------
266
float ofAngleDifferenceRadians(float currentAngle, float targetAngle) {
267
	return  ofWrapRadians(targetAngle - currentAngle);
268
}
269

270

271

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.