SeExpr

ExprDeepWater.h
Go to the documentation of this file.
1 /*
2 * Copyright Disney Enterprises, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License
6 * and the following modification to it: Section 6 Trademarks.
7 * deleted and replaced with:
8 *
9 * 6. Trademarks. This License does not grant permission to use the
10 * trade names, trademarks, service marks, or product names of the
11 * Licensor and its affiliates, except as required for reproducing
12 * the content of the NOTICE file.
13 *
14 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * @file ExprDeepWater.h
18 */
19 #pragma once
20
21 #include <vector>
22
23 #include <QObject>
24 #include <QGraphicsPolygonItem>
25 #include <QGraphicsView>
26 #include <QLineEdit>
27
28 #include <cmath>
29 #include <iostream>
30
31 #include "../Vec.h"
32
33 struct SeDeepWaterParams {
34 SeDeepWaterParams() {}
35 SeDeepWaterParams(int resolutionIn,
36 double tileSizeIn,
37 double lengthCutoffIn,
38 double amplitudeIn,
39 double windAngleIn,
40 double windSpeedIn,
41 double directionalFactorExponentIn,
42 double directionalReflectionDampingIn,
43 const SeExpr2::Vec3d &flowDirectionIn,
44 double sharpenIn,
45 double timeIn,
46 double filterWidthIn)
47 : resolution(resolutionIn), tileSize(tileSizeIn), lengthCutoff(lengthCutoffIn), amplitude(amplitudeIn),
48 windAngle(windAngleIn), windSpeed(windSpeedIn), directionalFactorExponent(directionalFactorExponentIn),
49 directionalReflectionDamping(directionalReflectionDampingIn), flowDirection(flowDirectionIn),
50 sharpen(sharpenIn), time(timeIn), filterWidth(filterWidthIn) {}
51
52 int resolution;
53 double tileSize;
54 double lengthCutoff;
55 double amplitude;
56 double windAngle;
57 double windSpeed;
58 double directionalFactorExponent;
59 double directionalReflectionDamping;
60 SeExpr2::Vec3d flowDirection;
61 double sharpen;
62 double time;
63 double filterWidth;
64 };
65
66 template <class T>
67 struct SeDeepWater {
68 SeDeepWater() : gravity(9.8) {}
69 virtual ~SeDeepWater() {}
70
71 void setParams(const SeDeepWaterParams &paramsIn) {
72 params = paramsIn;
73 gridSize = 1 << params.resolution;
74 }
75
76 T sqr(T x) { return x * x; }
77
78 inline static T kscale() {
79 return 1 / 100.0;
80 };
81
82 T toIndex(const T x) { return x / kscale(); }
83
84 T fromIndex(const T index) { return kscale() * index; }
85
86 inline static T bottom_offset() {
87 return -5;
88 };
89
90 T fromLog(const T x) { return std::log(x) - bottom_offset(); }
91
92 T toLog(const T z) { return std::exp(z + bottom_offset()); }
93
94 T powerLaw(const T x, const SeDeepWaterParams &params) {
95 return params.amplitude * exp(-1 / sqr(x)) / pow(x, 4 + params.directionalFactorExponent); // power law
96 }
97
98 T rescale(const T x) { return std::pow(x, .1); }
99
100 void generateSpectrum() {
101 const T L = params.windSpeed * params.windSpeed / gravity;
102 const T coefficient = 2 * M_PI / params.tileSize;
103
104 klowindex = toIndex(fromLog(L * coefficient));
105 khighindex = toIndex(fromLog(L * coefficient * gridSize));
106
107 int sample = 0;
108 T k = 0;
109 while (k < 3000000) {
110 k = toLog(fromIndex(sample++));
111 T e = 0;
112 if (k != 0) {
113 e = powerLaw(k, params); // power law
114 e *= exp(-sqr(k / L * params.lengthCutoff)); // damps high frequency waves
115 e = rescale(e);
116 }
117 energy.emplace_back(e);
118 }
119
120 T x = sqrt(2 / (4 + params.directionalFactorExponent));
121 kmaxindex = toIndex(fromLog(x));
122 T escale = .95 / rescale(powerLaw(x, params)); // power law
123 for (size_t sample = 0; sample < energy.size(); sample++) {
124 energy[sample] *= escale;
125 }
126 }
127
129 T getValue(double param) const {
130 if (energy.empty()) return 0;
131 if (param < 0) param = 0;
132 if (param > 1) param = 1;
133 int index = param * energy.size() - 1;
134 return energy[index];
135 }
136
137 T getKLow() {
138 T klow = (T)klowindex / energy.size();
139 return klow < 0 ? 0 : klow;
140 }
141
142 T getKHigh() {
143 T khigh = (T)khighindex / energy.size();
144 return khigh > 1 ? 1 : khigh;
145 }
146
147 bool inGrid() { return kmaxindex > klowindex && kmaxindex < khighindex; }
148
149 T gravity;
150 size_t gridSize;
151 SeDeepWaterParams params;
152 std::vector<T> energy;
153 T klowindex;
154 T khighindex;
155 T kmaxindex;
156 };
157
158 /*
159 This class overrides QGraphicsView so we can get resize events
160 */
161 class DeepWaterGraphicsView : public QGraphicsView {
162 Q_OBJECT
163 public:
164 DeepWaterGraphicsView() {
165 setTransformationAnchor(QGraphicsView::NoAnchor);
166 setResizeAnchor(QGraphicsView::NoAnchor);
167 }
168 ~DeepWaterGraphicsView() {}
169
170 virtual void resizeEvent(QResizeEvent *event);
171
172 signals:
173 void resizeSignal(int width, int height);
174 };
175
176 class DeepWaterLineEdit : public QLineEdit {
177 Q_OBJECT
178
179 public:
180 DeepWaterLineEdit(QWidget *parent = 0) {}
181 ~DeepWaterLineEdit() {}
182
183 signals:
184 void focusOut();
185
186 protected:
187 virtual void focusOutEvent(QFocusEvent *e) {
188 QLineEdit::focusOutEvent(e);
189 emit(focusOut());
190 }
191 };
192
193 /*
194 This class overrides QGraphicsScene so we can handle mouse
195 press, drag and keyboard events
196 */
197 class DeepWaterScene : public QGraphicsScene {
198 Q_OBJECT
199
200 typedef SeDeepWater<double> T_CURVE;
201
202 public:
203 DeepWaterScene();
204 ~DeepWaterScene();
205
206 void setParams(const SeDeepWaterParams &paramsIn);
207
208 void drawRect();
209 void drawPoly();
210 void drawGrid();
211
212 void emitDeepWaterChanged();
213
214 void rebuildDeepWater();
215
216 friend class ExprDeepWater;
217
218 SeDeepWaterParams params;
219
220 private:
221 T_CURVE *_curve;
222
223 public
224 slots:
225 void resolutionChanged(int val);
226 void tileSizeChanged(double val);
227 void lengthCutoffChanged(double val);
228 void amplitudeChanged(double val);
229 void windAngleChanged(double val);
230 void windSpeedChanged(double val);
231 void flowDirectionChanged(QString val);
232 void directionalFactorExponentChanged(double val);
233 void directionalReflectionDampingChanged(double val);
234 void sharpenChanged(double val);
235 void resize(const int width, const int height);
236
237 signals:
238 void deepWaterChanged();
239
240 private:
241 int _width;
242 int _height;
243 QGraphicsPolygonItem *_curvePoly;
244 QGraphicsRectItem *_baseRect;
245 QGraphicsRectItem *_gridRect;
246 };
247
248 class ExprDeepWater : public QWidget {
249 Q_OBJECT
250 public:
251 ExprDeepWater(QWidget *parent = 0);
252 ~ExprDeepWater() {}
253
254 void setParams(const SeDeepWaterParams &params);
255 DeepWaterScene *_scene;
256
257 public
258 slots:
259 void resolutionChanged();
260 void tileSizeChanged();
261 void lengthCutoffChanged();
262 void amplitudeChanged();
263 void windAngleChanged();
264 void windSpeedChanged();
265 void flowDirectionChanged();
266 void directionalFactorExponentChanged();
267 void directionalReflectionDampingChanged();
268 void sharpenChanged();
269
270 signals:
271 void resolutionChangedSignal(int val);
272 void tileSizeChangedSignal(double val);
273 void lengthCutoffChangedSignal(double val);
274 void amplitudeChangedSignal(double val);
275 void windAngleChangedSignal(double val);
276 void windSpeedChangedSignal(double val);
277 void flowDirectionChangedSignal(QString val);
278 void directionalFactorExponentChangedSignal(double val);
279 void directionalReflectionDampingChangedSignal(double val);
280 void sharpenChangedSignal(double val);
281
282 private:
283 DeepWaterLineEdit *_resolutionEdit;
284 DeepWaterLineEdit *_tileSizeEdit;
285 DeepWaterLineEdit *_lengthCutoffEdit;
286 DeepWaterLineEdit *_amplitudeEdit;
287 DeepWaterLineEdit *_windAngleEdit;
288 DeepWaterLineEdit *_windSpeedEdit;
289 DeepWaterLineEdit *_flowDirectionEdit;
290 DeepWaterLineEdit *_directionalFactorExponentEdit;
291 DeepWaterLineEdit *_directionalReflectionDampingEdit;
292 DeepWaterLineEdit *_sharpenEdit;
293 };
DeepWaterScene::setParams
void setParams(const SeDeepWaterParams &paramsIn)
Definition: ExprDeepWater.cpp:108
DeepWaterScene::rebuildDeepWater
void rebuildDeepWater()
Definition: ExprDeepWater.cpp:116
DeepWaterScene::sharpenChanged
void sharpenChanged(double val)
Definition: ExprDeepWater.cpp:103
ExprDeepWater
Definition: ExprDeepWater.h:248
SeDeepWaterParams::SeDeepWaterParams
SeDeepWaterParams()
Definition: ExprDeepWater.h:34
ExprDeepWater::amplitudeChangedSignal
void amplitudeChangedSignal(double val)
index
The result is computed int int< br >< div style="margin-left: 40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed). &nbsp
ExprDeepWater::directionalFactorExponentChangedSignal
void directionalFactorExponentChangedSignal(double val)
SeDeepWaterParams::lengthCutoff
double lengthCutoff
Definition: ExprDeepWater.h:54
ExprDeepWater::resolutionChangedSignal
void resolutionChangedSignal(int val)
ExprDeepWater::lengthCutoffChangedSignal
void lengthCutoffChangedSignal(double val)
DeepWaterGraphicsView::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
Definition: ExprDeepWater.cpp:34
DeepWaterScene::directionalReflectionDampingChanged
void directionalReflectionDampingChanged(double val)
Definition: ExprDeepWater.cpp:98
DeepWaterScene::_width
int _width
Definition: ExprDeepWater.h:241
SeExpr2::Vec< double, 3, false >
DeepWaterScene
Definition: ExprDeepWater.h:197
DeepWaterLineEdit
Definition: ExprDeepWater.h:176
SeDeepWater::kmaxindex
T kmaxindex
Definition: ExprDeepWater.h:155
DeepWaterScene::flowDirectionChanged
void flowDirectionChanged(QString val)
Definition: ExprDeepWater.cpp:85
DeepWaterScene::directionalFactorExponentChanged
void directionalFactorExponentChanged(double val)
Definition: ExprDeepWater.cpp:93
DeepWaterScene::deepWaterChanged
void deepWaterChanged()
SeDeepWaterParams::tileSize
double tileSize
Definition: ExprDeepWater.h:53
ExprDeepWater::flowDirectionChanged
void flowDirectionChanged()
Definition: ExprDeepWater.cpp:439
DeepWaterScene::_curvePoly
QGraphicsPolygonItem * _curvePoly
Definition: ExprDeepWater.h:243
SeDeepWater::energy
std::vector< T > energy
Definition: ExprDeepWater.h:152
SeDeepWater::gravity
T gravity
Definition: ExprDeepWater.h:149
SeDeepWaterParams::resolution
int resolution
Definition: ExprDeepWater.h:52
SeDeepWater::getKLow
T getKLow()
Definition: ExprDeepWater.h:137
ExprDeepWater::_amplitudeEdit
DeepWaterLineEdit * _amplitudeEdit
Definition: ExprDeepWater.h:286
ExprDeepWater::windSpeedChangedSignal
void windSpeedChangedSignal(double val)
SeDeepWater::getKHigh
T getKHigh()
Definition: ExprDeepWater.h:142
ExprDeepWater::_lengthCutoffEdit
DeepWaterLineEdit * _lengthCutoffEdit
Definition: ExprDeepWater.h:285
DeepWaterScene::_baseRect
QGraphicsRectItem * _baseRect
Definition: ExprDeepWater.h:244
DeepWaterScene::T_CURVE
SeDeepWater< double > T_CURVE
Definition: ExprDeepWater.h:200
ExprDeepWater::ExprDeepWater
ExprDeepWater(QWidget *parent=0)
Definition: ExprDeepWater.cpp:165
DeepWaterScene::lengthCutoffChanged
void lengthCutoffChanged(double val)
Definition: ExprDeepWater.cpp:65
DeepWaterScene::tileSizeChanged
void tileSizeChanged(double val)
Definition: ExprDeepWater.cpp:60
ExprDeepWater::windSpeedChanged
void windSpeedChanged()
Definition: ExprDeepWater.cpp:434
SeDeepWaterParams::SeDeepWaterParams
SeDeepWaterParams(int resolutionIn, double tileSizeIn, double lengthCutoffIn, double amplitudeIn, double windAngleIn, double windSpeedIn, double directionalFactorExponentIn, double directionalReflectionDampingIn, const SeExpr2::Vec3d &flowDirectionIn, double sharpenIn, double timeIn, double filterWidthIn)
Definition: ExprDeepWater.h:35
DeepWaterScene::drawGrid
void drawGrid()
Definition: ExprDeepWater.cpp:154
SeDeepWaterParams::flowDirection
SeExpr2::Vec3d flowDirection
Definition: ExprDeepWater.h:60
ExprDeepWater::windAngleChangedSignal
void windAngleChangedSignal(double val)
DeepWaterScene::_height
int _height
Definition: ExprDeepWater.h:242
SeDeepWater::getValue
T getValue(double param) const
Evaluates curve and returns full value.
Definition: ExprDeepWater.h:129
ExprDeepWater::setParams
void setParams(const SeDeepWaterParams &params)
Definition: ExprDeepWater.cpp:456
ExprDeepWater::_directionalFactorExponentEdit
DeepWaterLineEdit * _directionalFactorExponentEdit
Definition: ExprDeepWater.h:290
SeDeepWater::inGrid
bool inGrid()
Definition: ExprDeepWater.h:147
SeDeepWater::params
SeDeepWaterParams params
Definition: ExprDeepWater.h:151
SeDeepWater::fromLog
T fromLog(const T x)
Definition: ExprDeepWater.h:90
ExprDeepWater::amplitudeChanged
void amplitudeChanged()
Definition: ExprDeepWater.cpp:424
ExprDeepWater::_windAngleEdit
DeepWaterLineEdit * _windAngleEdit
Definition: ExprDeepWater.h:287
ExprDeepWater::sharpenChanged
void sharpenChanged()
Definition: ExprDeepWater.cpp:451
SeDeepWaterParams::windSpeed
double windSpeed
Definition: ExprDeepWater.h:57
ExprDeepWater::_directionalReflectionDampingEdit
DeepWaterLineEdit * _directionalReflectionDampingEdit
Definition: ExprDeepWater.h:291
DeepWaterScene::amplitudeChanged
void amplitudeChanged(double val)
Definition: ExprDeepWater.cpp:70
DeepWaterGraphicsView::DeepWaterGraphicsView
DeepWaterGraphicsView()
Definition: ExprDeepWater.h:164
SeDeepWater::gridSize
size_t gridSize
Definition: ExprDeepWater.h:150
ExprDeepWater::tileSizeChanged
void tileSizeChanged()
Definition: ExprDeepWater.cpp:414
SeDeepWaterParams::amplitude
double amplitude
Definition: ExprDeepWater.h:55
DeepWaterGraphicsView::~DeepWaterGraphicsView
~DeepWaterGraphicsView()
Definition: ExprDeepWater.h:168
SeDeepWater::kscale
static T kscale()
Definition: ExprDeepWater.h:78
ExprDeepWater::~ExprDeepWater
~ExprDeepWater()
Definition: ExprDeepWater.h:252
DeepWaterScene::params
SeDeepWaterParams params
Definition: ExprDeepWater.h:218
ExprDeepWater::_resolutionEdit
DeepWaterLineEdit * _resolutionEdit
Definition: ExprDeepWater.h:283
DeepWaterScene::drawPoly
void drawPoly()
Definition: ExprDeepWater.cpp:136
DeepWaterScene::_gridRect
QGraphicsRectItem * _gridRect
Definition: ExprDeepWater.h:245
SeDeepWater::setParams
void setParams(const SeDeepWaterParams &paramsIn)
Definition: ExprDeepWater.h:71
SeDeepWater::toLog
T toLog(const T z)
Definition: ExprDeepWater.h:92
SeDeepWater::powerLaw
T powerLaw(const T x, const SeDeepWaterParams &params)
Definition: ExprDeepWater.h:94
SeDeepWaterParams::filterWidth
double filterWidth
Definition: ExprDeepWater.h:63
SeDeepWater::SeDeepWater
SeDeepWater()
Definition: ExprDeepWater.h:68
DeepWaterLineEdit::focusOutEvent
virtual void focusOutEvent(QFocusEvent *e)
Definition: ExprDeepWater.h:187
SeDeepWaterParams::windAngle
double windAngle
Definition: ExprDeepWater.h:56
DeepWaterScene::~DeepWaterScene
~DeepWaterScene()
Definition: ExprDeepWater.cpp:43
SeDeepWater::bottom_offset
static T bottom_offset()
Definition: ExprDeepWater.h:86
SeDeepWater::khighindex
T khighindex
Definition: ExprDeepWater.h:154
SeDeepWaterParams::sharpen
double sharpen
Definition: ExprDeepWater.h:61
ExprDeepWater::sharpenChangedSignal
void sharpenChangedSignal(double val)
SeDeepWaterParams::directionalFactorExponent
double directionalFactorExponent
Definition: ExprDeepWater.h:58
ExprDeepWater::_flowDirectionEdit
DeepWaterLineEdit * _flowDirectionEdit
Definition: ExprDeepWater.h:289
SeDeepWaterParams
Definition: ExprDeepWater.h:33
DeepWaterScene::windSpeedChanged
void windSpeedChanged(double val)
Definition: ExprDeepWater.cpp:80
DeepWaterGraphicsView
Definition: ExprDeepWater.h:161
ExprDeepWater::directionalFactorExponentChanged
void directionalFactorExponentChanged()
Definition: ExprDeepWater.cpp:441
SeDeepWater::fromIndex
T fromIndex(const T index)
Definition: ExprDeepWater.h:84
DeepWaterLineEdit::~DeepWaterLineEdit
~DeepWaterLineEdit()
Definition: ExprDeepWater.h:181
SeDeepWater::generateSpectrum
void generateSpectrum()
Definition: ExprDeepWater.h:100
ExprDeepWater::windAngleChanged
void windAngleChanged()
Definition: ExprDeepWater.cpp:429
SeDeepWater::sqr
T sqr(T x)
Definition: ExprDeepWater.h:76
ExprDeepWater::lengthCutoffChanged
void lengthCutoffChanged()
Definition: ExprDeepWater.cpp:419
DeepWaterLineEdit::focusOut
void focusOut()
ExprDeepWater::directionalReflectionDampingChangedSignal
void directionalReflectionDampingChangedSignal(double val)
ExprDeepWater::_tileSizeEdit
DeepWaterLineEdit * _tileSizeEdit
Definition: ExprDeepWater.h:284
ExprDeepWater::flowDirectionChangedSignal
void flowDirectionChangedSignal(QString val)
DeepWaterLineEdit::DeepWaterLineEdit
DeepWaterLineEdit(QWidget *parent=0)
Definition: ExprDeepWater.h:180
ExprDeepWater::tileSizeChangedSignal
void tileSizeChangedSignal(double val)
SeDeepWaterParams::directionalReflectionDamping
double directionalReflectionDamping
Definition: ExprDeepWater.h:59
SeDeepWater
Definition: ExprDeepWater.h:67
ExprDeepWater::directionalReflectionDampingChanged
void directionalReflectionDampingChanged()
Definition: ExprDeepWater.cpp:446
DeepWaterScene::_curve
T_CURVE * _curve
Definition: ExprDeepWater.h:221
ExprDeepWater::_windSpeedEdit
DeepWaterLineEdit * _windSpeedEdit
Definition: ExprDeepWater.h:288
DeepWaterGraphicsView::resizeSignal
void resizeSignal(int width, int height)
DeepWaterScene::windAngleChanged
void windAngleChanged(double val)
Definition: ExprDeepWater.cpp:75
ExprDeepWater::_scene
DeepWaterScene * _scene
Definition: ExprDeepWater.h:255
DeepWaterScene::resolutionChanged
void resolutionChanged(int val)
Definition: ExprDeepWater.cpp:55
DeepWaterScene::resize
void resize(const int width, const int height)
Definition: ExprDeepWater.cpp:45
z
This is the same as the prman cellnoise function< br ></div >< br > float< b > float y< br > float< b > float float z
Definition: userdoc.txt:218
SeDeepWaterParams::time
double time
Definition: ExprDeepWater.h:62
DeepWaterScene::drawRect
void drawRect()
Definition: ExprDeepWater.cpp:127
x
</pre >< h3 > A simple variable reference</h3 > This is not a very interesting subclass of expression until we add some additional variables Variables on some applications may be very dynamic In this we only need x
Definition: tutorial.txt:108
DeepWaterScene::DeepWaterScene
DeepWaterScene()
Definition: ExprDeepWater.cpp:38
SeDeepWater::rescale
T rescale(const T x)
Definition: ExprDeepWater.h:98
ExprDeepWater::resolutionChanged
void resolutionChanged()
Definition: ExprDeepWater.cpp:409
pow
< br > pow($a, 0.5)+ $b< br >< br ></div > External variables can also be overridden by local assignment. &nbsp
DeepWaterScene::emitDeepWaterChanged
void emitDeepWaterChanged()
Definition: ExprDeepWater.cpp:124
SeDeepWater::klowindex
T klowindex
Definition: ExprDeepWater.h:153
ExprDeepWater::_sharpenEdit
DeepWaterLineEdit * _sharpenEdit
Definition: ExprDeepWater.h:292
SeDeepWater::~SeDeepWater
virtual ~SeDeepWater()
Definition: ExprDeepWater.h:69
SeDeepWater::toIndex
T toIndex(const T x)
Definition: ExprDeepWater.h:82