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
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
53 double tileSize;
55 double amplitude;
56 double windAngle;
57 double windSpeed;
61 double sharpen;
62 double time;
64};
65
66template <class T>
68 SeDeepWater() : gravity(9.8) {}
69 virtual ~SeDeepWater() {}
70
71 void setParams(const SeDeepWaterParams &paramsIn) {
72 params = paramsIn;
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
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));
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
138 T klow = (T)klowindex / energy.size();
139 return klow < 0 ? 0 : klow;
140 }
141
143 T khigh = (T)khighindex / energy.size();
144 return khigh > 1 ? 1 : khigh;
145 }
146
148
150 size_t gridSize;
152 std::vector<T> energy;
156};
157
158/*
159 This class overrides QGraphicsView so we can get resize events
160*/
161class DeepWaterGraphicsView : public QGraphicsView {
162 Q_OBJECT
163 public:
165 setTransformationAnchor(QGraphicsView::NoAnchor);
166 setResizeAnchor(QGraphicsView::NoAnchor);
167 }
169
170 virtual void resizeEvent(QResizeEvent *event);
171
172signals:
173 void resizeSignal(int width, int height);
174};
175
176class DeepWaterLineEdit : public QLineEdit {
177 Q_OBJECT
178
179 public:
180 DeepWaterLineEdit(QWidget *parent = 0) {}
182
183signals:
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*/
197class DeepWaterScene : public QGraphicsScene {
198 Q_OBJECT
199
201
202 public:
205
206 void setParams(const SeDeepWaterParams &paramsIn);
207
208 void drawRect();
209 void drawPoly();
210 void drawGrid();
211
213
214 void rebuildDeepWater();
215
216 friend class ExprDeepWater;
217
219
220 private:
222
223 public
224slots:
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);
234 void sharpenChanged(double val);
235 void resize(const int width, const int height);
236
237signals:
239
240 private:
243 QGraphicsPolygonItem *_curvePoly;
244 QGraphicsRectItem *_baseRect;
245 QGraphicsRectItem *_gridRect;
246};
247
248class ExprDeepWater : public QWidget {
249 Q_OBJECT
250 public:
251 ExprDeepWater(QWidget *parent = 0);
253
254 void setParams(const SeDeepWaterParams &params);
256
257 public
258slots:
259 void resolutionChanged();
260 void tileSizeChanged();
261 void lengthCutoffChanged();
262 void amplitudeChanged();
263 void windAngleChanged();
264 void windSpeedChanged();
268 void sharpenChanged();
269
270signals:
272 void tileSizeChangedSignal(double val);
274 void amplitudeChangedSignal(double val);
275 void windAngleChangedSignal(double val);
276 void windSpeedChangedSignal(double val);
277 void flowDirectionChangedSignal(QString val);
280 void sharpenChangedSignal(double val);
281
282 private:
293};
virtual void resizeEvent(QResizeEvent *event)
void resizeSignal(int width, int height)
virtual void focusOutEvent(QFocusEvent *e)
DeepWaterLineEdit(QWidget *parent=0)
SeDeepWater< double > T_CURVE
T_CURVE * _curve
SeDeepWaterParams params
void windSpeedChanged(double val)
void tileSizeChanged(double val)
QGraphicsPolygonItem * _curvePoly
void deepWaterChanged()
void setParams(const SeDeepWaterParams &paramsIn)
void lengthCutoffChanged(double val)
void resize(const int width, const int height)
void resolutionChanged(int val)
void sharpenChanged(double val)
void emitDeepWaterChanged()
void amplitudeChanged(double val)
void directionalFactorExponentChanged(double val)
void directionalReflectionDampingChanged(double val)
void windAngleChanged(double val)
QGraphicsRectItem * _gridRect
void flowDirectionChanged(QString val)
QGraphicsRectItem * _baseRect
void lengthCutoffChanged()
void directionalFactorExponentChanged()
void amplitudeChangedSignal(double val)
DeepWaterScene * _scene
void flowDirectionChanged()
void windAngleChangedSignal(double val)
DeepWaterLineEdit * _resolutionEdit
void flowDirectionChangedSignal(QString val)
void directionalReflectionDampingChanged()
DeepWaterLineEdit * _directionalReflectionDampingEdit
void amplitudeChanged()
void directionalFactorExponentChangedSignal(double val)
void tileSizeChangedSignal(double val)
void lengthCutoffChangedSignal(double val)
DeepWaterLineEdit * _sharpenEdit
void windAngleChanged()
DeepWaterLineEdit * _tileSizeEdit
DeepWaterLineEdit * _lengthCutoffEdit
void directionalReflectionDampingChangedSignal(double val)
void windSpeedChangedSignal(double val)
DeepWaterLineEdit * _windSpeedEdit
void setParams(const SeDeepWaterParams &params)
DeepWaterLineEdit * _windAngleEdit
void resolutionChangedSignal(int val)
void sharpenChangedSignal(double val)
DeepWaterLineEdit * _directionalFactorExponentEdit
void windSpeedChanged()
DeepWaterLineEdit * _amplitudeEdit
DeepWaterLineEdit * _flowDirectionEdit
void resolutionChanged()
void tileSizeChanged()
SeExpr2::Vec3d flowDirection
Definition: ExprDeepWater.h:60
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
double directionalFactorExponent
Definition: ExprDeepWater.h:58
double directionalReflectionDamping
Definition: ExprDeepWater.h:59
static T kscale()
Definition: ExprDeepWater.h:78
T fromIndex(const T index)
Definition: ExprDeepWater.h:84
T powerLaw(const T x, const SeDeepWaterParams &params)
Definition: ExprDeepWater.h:94
T rescale(const T x)
Definition: ExprDeepWater.h:98
T getValue(double param) const
Evaluates curve and returns full value.
virtual ~SeDeepWater()
Definition: ExprDeepWater.h:69
std::vector< T > energy
SeDeepWaterParams params
T fromLog(const T x)
Definition: ExprDeepWater.h:90
void generateSpectrum()
void setParams(const SeDeepWaterParams &paramsIn)
Definition: ExprDeepWater.h:71
size_t gridSize
T toIndex(const T x)
Definition: ExprDeepWater.h:82
static T bottom_offset()
Definition: ExprDeepWater.h:86
T toLog(const T z)
Definition: ExprDeepWater.h:92
</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
< br > pow($a, 0.5)+ $b< br >< br ></div > External variables can also be overridden by local assignment. &nbsp
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
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