-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindSim.cpp
More file actions
163 lines (137 loc) · 4.47 KB
/
windSim.cpp
File metadata and controls
163 lines (137 loc) · 4.47 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <getopt.h>
#include "float3.h"
using namespace std;
static constexpr int WIND_UPDATE_RATE = 15 * 1;
float minWindStrength = 0.0f;
float maxWindStrength = 20.0f;
bool verbose = false;
uint16_t iterations = 0;
unsigned int windDirTimer = 0;
float curWindStrength = 0.0f;
float3 curWindVec;
float3 oldWindVec;
float3 newWindVec;
float3 curWindDir;
//helper functions
float smoothstep(const float edge0, const float edge1, const float value)
{
const float v = std::clamp(value, edge0, edge1);
const float x = (v - edge0) / (edge1 - edge0);
const float t = std::clamp(x, 0.0f, 1.0f);
return (t * t * (3.0f - 2.0f * t));
}
template<class T, typename T2> constexpr T mix(const T v1, const T v2, const T2 a) { return (v1 + (v2 - v1) * a); }
void reset(void)
{
windDirTimer = 0;
curWindStrength = 0.0f;
curWindDir = RgtVector;
curWindVec = ZeroVector;
newWindVec = ZeroVector;
oldWindVec = ZeroVector;
}
// function to update wind speed
void updateWind()
{
if(windDirTimer == 0)
{
oldWindVec = curWindVec;
newWindVec = oldWindVec;
float newStrength = 0.0f;
do
{
newWindVec.x -= (rand() % 2 - 0.5f) * maxWindStrength;
newWindVec.z -= (rand() % 2 - 0.5f) * maxWindStrength;
newStrength = newWindVec.Length();
} while (newStrength == 0.0f);
newWindVec /= newStrength;
newWindVec *= (newStrength = std::clamp(newStrength, minWindStrength, maxWindStrength));
}
else
{
const float mod = smoothstep(0.0f, 1.0f, windDirTimer / float(WIND_UPDATE_RATE));
// blend between old & new wind directions
curWindVec = mix(oldWindVec, newWindVec, mod);
curWindStrength = curWindVec.LengthNormalize();
curWindDir = curWindVec;
curWindVec = curWindDir * (curWindStrength = std::clamp(curWindStrength, minWindStrength, maxWindStrength));
}
windDirTimer = (windDirTimer + 1) % (WIND_UPDATE_RATE + 1);
}
int main(int argc, char* argv[]) {
//use getopt to read in command line arguments
// options are -m minWindStrength -M maxWindStrength -s seed -i iterations
// default values are minWindStrength = 0.0, maxWindStrength = 20.0, seed = time(NULL), iterations = 5000
// if no arguments are provided, default values are used
option longopt[] = {
{"minWindStrength", required_argument, NULL, 'm'},
{"maxWindStrength", required_argument, NULL, 'M'},
{"seed", required_argument, NULL, 's'},
{"iterations", required_argument, NULL, 'i'},
{"verbose", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};
//set default values
minWindStrength = 0.0f;
maxWindStrength = 20.0f;
srand((unsigned) time(NULL));
iterations = 5000;
// read in command line arguments
int opt;
while((opt = getopt_long(argc, argv, "m:M:s:i:v", longopt, 0)) != -1)
{
if(opt == -1)
break;
switch(opt)
{
case 'm':
minWindStrength = atof(optarg);
break;
case 'M':
maxWindStrength = atof(optarg);
break;
case 's':
srand(atoi(optarg));
break;
case 'i':
iterations = atoi(optarg);
break;
case 'v':
verbose = true;
break;
case '?':
printf("Unknown option '%c' was specified.\n", optopt);
break;
case ':':
printf("Option '%c' requires an argument.\n", optopt);
break;
default:
break;
}
}
//print all options to stderr
if(verbose == true)
{
std::cerr << "minWindStrength: " << minWindStrength << endl;
std::cerr << "maxWindStrength: " << maxWindStrength << endl;
std::cerr << "first random number: " << rand() << endl;
std::cerr << "iterations: " << iterations << endl;
}
reset();
std::cout << "x" << endl;
for (size_t i = 0; i < 100; i++)
{
updateWind();
}
for(int i = 0; i < iterations; i++)
{
updateWind();
// std::cout << "Wind Direction: " << curWindDir.x << ", " << curWindDir.y << ", " << curWindDir.z << endl;
// std::cout << i << ", " << curWindStrength << endl;
std::cout << curWindStrength << endl;
}
return 0;
}