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 <QtCore/QObject>
24 #include <QtGui/QGraphicsPolygonItem>
25 #include <QtGui/QGraphicsView>
26 #include <QtGui/QLineEdit>
27 
28 #include <cmath>
29 #include <iostream>
30 
31 #include <SeExpr2/Vec.h>
32 
34 {
36  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) : resolution(resolutionIn), tileSize(tileSizeIn), lengthCutoff(lengthCutoffIn), amplitude(amplitudeIn), windAngle(windAngleIn), windSpeed(windSpeedIn), directionalFactorExponent(directionalFactorExponentIn), directionalReflectionDamping(directionalReflectionDampingIn), flowDirection(flowDirectionIn), sharpen(sharpenIn), time(timeIn), filterWidth(filterWidthIn) {}
37 
39  double tileSize;
40  double lengthCutoff;
41  double amplitude;
42  double windAngle;
43  double windSpeed;
47  double sharpen;
48  double time;
49  double filterWidth;
50 };
51 
52 template<class T>
54 {
55  SeDeepWater() : gravity(9.8) {}
56  virtual ~SeDeepWater() {}
57 
58  void setParams(const SeDeepWaterParams& paramsIn){
59  params = paramsIn;
61  }
62 
63  T sqr(T x) {
64  return x*x;
65  }
66 
67  inline static T kscale(){return 1/100.0;};
68 
69  T toIndex(const T x)
70  {
71  return x/kscale();
72  }
73 
74  T fromIndex(const T index)
75  {
76  return kscale()*index;
77  }
78 
79  inline static T bottom_offset(){return -5;};
80 
81  T fromLog(const T x)
82  {
83  return std::log(x)-bottom_offset();
84  }
85 
86  T toLog(const T z)
87  {
88  return std::exp(z+bottom_offset());
89  }
90 
91  T powerLaw(const T x,const SeDeepWaterParams& params)
92  {
93  return params.amplitude*exp(-1/sqr(x))/pow(x,4+params.directionalFactorExponent); // power law
94  }
95 
96  T rescale(const T x)
97  {
98  return std::pow(x,.1);
99  }
100 
103  const T coefficient = 2*M_PI/params.tileSize;
104 
105  klowindex = toIndex(fromLog(L*coefficient));
106  khighindex = toIndex(fromLog(L*coefficient*gridSize));
107 
108  int sample = 0;
109  T k = 0;
110  while (k<3000000) {
111  k = toLog(fromIndex(sample++));
112  T e = 0;
113  if (k != 0) {
114  e = powerLaw(k,params); // power law
115  e *= exp(-sqr(k/L*params.lengthCutoff)); // damps high frequency waves
116  e = rescale(e);
117  }
118  energy.emplace_back(e);
119  }
120 
121  T x = sqrt(2/(4+params.directionalFactorExponent));
122  kmaxindex = toIndex(fromLog(x));
123  T escale = .95/rescale(powerLaw(x,params)); // power law
124  for (size_t sample=0; sample<energy.size(); sample++) {
125  energy[sample] *= escale;
126  }
127  }
128 
130  T getValue(double param) const {
131  if (energy.empty()) return 0;
132  if (param<0) param = 0;
133  if (param>1) param = 1;
134  int index = param * energy.size()-1;
135  return energy[index];
136  }
137 
138  T getKLow() {
139  T klow = (T)klowindex/energy.size();
140  return klow<0 ? 0 : klow;
141  }
142 
143  T getKHigh() {
144  T khigh = (T)khighindex/energy.size();
145  return khigh>1 ? 1 : khigh;
146  }
147 
149 
151  size_t gridSize;
153  std::vector<T> energy;
157 };
158 
159 /*
160  This class overrides QGraphicsView so we can get resize events
161 */
162 class DeepWaterGraphicsView : public QGraphicsView
163 {
164  Q_OBJECT
165 public:
167  setTransformationAnchor(QGraphicsView::NoAnchor);
168  setResizeAnchor(QGraphicsView::NoAnchor);
169  }
171 
172  virtual void resizeEvent(QResizeEvent *event);
173 
174 signals:
175  void resizeSignal(int width, int height);
176 };
177 
178 class DeepWaterLineEdit : public QLineEdit
179 {
180  Q_OBJECT
181 
182 public:
183  DeepWaterLineEdit(QWidget* parent=0) {}
185 
186 signals:
187  void focusOut();
188 
189 protected:
190  virtual void focusOutEvent(QFocusEvent* e) {
191  QLineEdit::focusOutEvent(e);
192  emit(focusOut());
193  }
194 };
195 
196 /*
197  This class overrides QGraphicsScene so we can handle mouse
198  press, drag and keyboard events
199 */
200 class DeepWaterScene : public QGraphicsScene
201 {
202  Q_OBJECT
203 
205 public:
206  DeepWaterScene();
207  ~DeepWaterScene();
208 
209  void setParams(const SeDeepWaterParams& paramsIn);
210 
211  void drawRect();
212  void drawPoly();
213  void drawGrid();
214 
215  void emitDeepWaterChanged();
216 
217  void rebuildDeepWater();
218 
219  friend class ExprDeepWater;
220 
222 
223 private:
225 
226 public slots:
227  void resolutionChanged(int val);
228  void tileSizeChanged(double val);
229  void lengthCutoffChanged(double val);
230  void amplitudeChanged(double val);
231  void windAngleChanged(double val);
232  void windSpeedChanged(double val);
233  void flowDirectionChanged(QString val);
234  void directionalFactorExponentChanged(double val);
235  void directionalReflectionDampingChanged(double val);
236  void sharpenChanged(double val);
237  void resize(const int width, const int height);
238 
239 signals:
240  void deepWaterChanged();
241 
242 private:
243  int _width;
244  int _height;
245  QGraphicsPolygonItem *_curvePoly;
246  QGraphicsRectItem *_baseRect;
247  QGraphicsRectItem *_gridRect;
248 };
249 
250 class ExprDeepWater : public QWidget
251 {
252  Q_OBJECT
253 public:
254  ExprDeepWater(QWidget* parent = 0);
256 
257  void setParams(const SeDeepWaterParams& params);
259 
260 public slots:
261  void resolutionChanged();
262  void tileSizeChanged();
263  void lengthCutoffChanged();
264  void amplitudeChanged();
265  void windAngleChanged();
266  void windSpeedChanged();
267  void flowDirectionChanged();
270  void sharpenChanged();
271 
272 signals:
273  void resolutionChangedSignal(int val);
274  void tileSizeChangedSignal(double val);
275  void lengthCutoffChangedSignal(double val);
276  void amplitudeChangedSignal(double val);
277  void windAngleChangedSignal(double val);
278  void windSpeedChangedSignal(double val);
279  void flowDirectionChangedSignal(QString val);
282  void sharpenChangedSignal(double val);
283 
284 private:
295 };
DeepWaterLineEdit * _amplitudeEdit
DeepWaterLineEdit * _sharpenEdit
void lengthCutoffChanged(double val)
void windAngleChangedSignal(double val)
T rescale(const T x)
Definition: ExprDeepWater.h:96
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:36
DeepWaterLineEdit * _windSpeedEdit
void amplitudeChanged(double val)
T toLog(const T z)
Definition: ExprDeepWater.h:86
T toIndex(const T x)
Definition: ExprDeepWater.h:69
virtual void resizeEvent(QResizeEvent *event)
SeDeepWaterParams params
QGraphicsRectItem * _baseRect
QGraphicsRectItem * _gridRect
DeepWaterLineEdit * _lengthCutoffEdit
void windSpeedChangedSignal(double val)
void flowDirectionChanged(QString val)
void windAngleChanged(double val)
void setParams(const SeDeepWaterParams &paramsIn)
Definition: ExprDeepWater.h:58
T powerLaw(const T x, const SeDeepWaterParams &params)
Definition: ExprDeepWater.h:91
void deepWaterChanged()
void resolutionChangedSignal(int val)
void flowDirectionChanged()
void generateSpectrum()
virtual ~SeDeepWater()
Definition: ExprDeepWater.h:56
DeepWaterLineEdit * _resolutionEdit
DeepWaterLineEdit * _flowDirectionEdit
void directionalFactorExponentChanged()
QGraphicsPolygonItem * _curvePoly
size_t gridSize
void directionalReflectionDampingChangedSignal(double val)
The result is computed int int< br >< divstyle="margin-left:40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed).&nbsp
T fromIndex(const T index)
Definition: ExprDeepWater.h:74
void windSpeedChanged()
< br > pow($a, 0.5)+$b< br >< br ></div > External variables can also be overridden by local assignment.&nbsp
SeExpr2::Vec3d flowDirection
Definition: ExprDeepWater.h:46
void amplitudeChanged()
double directionalReflectionDamping
Definition: ExprDeepWater.h:45
DeepWaterLineEdit * _windAngleEdit
double directionalFactorExponent
Definition: ExprDeepWater.h:44
static T bottom_offset()
Definition: ExprDeepWater.h:79
void directionalReflectionDampingChanged(double val)
void sharpenChanged(double val)
void windSpeedChanged(double val)
void sharpenChangedSignal(double val)
void tileSizeChanged()
void resize(const int width, const int height)
DeepWaterLineEdit * _directionalReflectionDampingEdit
</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
void resolutionChanged(int val)
ExprDeepWater(QWidget *parent=0)
SeDeepWater< double > T_CURVE
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
void tileSizeChangedSignal(double val)
T_CURVE * _curve
void setParams(const SeDeepWaterParams &params)
void flowDirectionChangedSignal(QString val)
T getValue(double param) const
Evaluates curve and returns full value.
DeepWaterLineEdit * _directionalFactorExponentEdit
void directionalFactorExponentChangedSignal(double val)
virtual void focusOutEvent(QFocusEvent *e)
void resizeSignal(int width, int height)
DeepWaterScene * _scene
void windAngleChanged()
void lengthCutoffChanged()
DeepWaterLineEdit * _tileSizeEdit
void resolutionChanged()
static T kscale()
Definition: ExprDeepWater.h:67
void directionalReflectionDampingChanged()
void tileSizeChanged(double val)
void emitDeepWaterChanged()
SeDeepWaterParams params
DeepWaterLineEdit(QWidget *parent=0)
std::vector< T > energy
void setParams(const SeDeepWaterParams &paramsIn)
void amplitudeChangedSignal(double val)
void lengthCutoffChangedSignal(double val)
T fromLog(const T x)
Definition: ExprDeepWater.h:81
void directionalFactorExponentChanged(double val)