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.cpp
18 */
19 #include <iostream>
20 #include <algorithm>
21 
22 #include <QDialog>
23 #include <QHBoxLayout>
24 #include <QLabel>
25 #include <QVBoxLayout>
26 #include <QResizeEvent>
27 #include <QIntValidator>
28 
29 #include <SeExpr2/ExprBuiltins.h>
30 #include <cfloat>
31 
32 #include "ExprDeepWater.h"
33 
resizeEvent(QResizeEvent * event)34 void DeepWaterGraphicsView::resizeEvent(QResizeEvent *event) {
35     emit resizeSignal(event->size().width(), event->size().height());
36 }
37 
DeepWaterScene()38 DeepWaterScene::DeepWaterScene()
39     : _curve(new T_CURVE), _width(320), _height(170), _curvePoly(0), _baseRect(0), _gridRect(0) {
40     resize(_width, _height);
41 }
42 
~DeepWaterScene()43 DeepWaterScene::~DeepWaterScene() { delete _curve; }
44 
resize(const int width,const int height)45 void DeepWaterScene::resize(const int width, const int height) {
46     // width and height already have the 8 px padding factored in
47     _width = width - 16;
48     _height = height - 16;
49     setSceneRect(-9, -7, width, height);
50     drawRect();
51     drawPoly();
52     drawGrid();
53 }
54 
resolutionChanged(int val)55 void DeepWaterScene::resolutionChanged(int val) {
56     params.resolution = val;
57     setParams(params);
58 }
59 
tileSizeChanged(double val)60 void DeepWaterScene::tileSizeChanged(double val) {
61     params.tileSize = val;
62     setParams(params);
63 }
64 
lengthCutoffChanged(double val)65 void DeepWaterScene::lengthCutoffChanged(double val) {
66     params.lengthCutoff = val;
67     setParams(params);
68 }
69 
amplitudeChanged(double val)70 void DeepWaterScene::amplitudeChanged(double val) {
71     params.amplitude = val;
72     setParams(params);
73 }
74 
windAngleChanged(double val)75 void DeepWaterScene::windAngleChanged(double val) {
76     params.windAngle = val;
77     setParams(params);
78 }
79 
windSpeedChanged(double val)80 void DeepWaterScene::windSpeedChanged(double val) {
81     params.windSpeed = val;
82     setParams(params);
83 }
84 
flowDirectionChanged(QString val)85 void DeepWaterScene::flowDirectionChanged(QString val) {
86     QString flowDirection = val.remove(0, 1);
87     flowDirection = flowDirection.remove(flowDirection.size() - 1, 1);
88     QStringList components = flowDirection.split(",");
89     params.flowDirection = SeExpr2::Vec3d(components[0].toDouble(), components[1].toDouble(), components[2].toDouble());
90     setParams(params);
91 }
92 
directionalFactorExponentChanged(double val)93 void DeepWaterScene::directionalFactorExponentChanged(double val) {
94     params.directionalFactorExponent = val;
95     setParams(params);
96 }
97 
directionalReflectionDampingChanged(double val)98 void DeepWaterScene::directionalReflectionDampingChanged(double val) {
99     params.directionalReflectionDamping = val;
100     setParams(params);
101 }
102 
sharpenChanged(double val)103 void DeepWaterScene::sharpenChanged(double val) {
104     params.sharpen = val;
105     setParams(params);
106 }
107 
setParams(const SeDeepWaterParams & paramsIn)108 void DeepWaterScene::setParams(const SeDeepWaterParams &paramsIn) {
109     params = paramsIn;
110     rebuildDeepWater();
111     drawPoly();
112     drawGrid();
113     emitDeepWaterChanged();
114 }
115 
rebuildDeepWater()116 void DeepWaterScene::rebuildDeepWater() {
117     delete _curve;
118     _curve = new T_CURVE;
119     _curve->setParams(params);
120     _curve->generateSpectrum();
121 }
122 
123 // return points in reverse order in order to use same parsing in editor
emitDeepWaterChanged()124 void DeepWaterScene::emitDeepWaterChanged() { emit deepWaterChanged(); }
125 
126 // draws the base gray outline rectangle
drawRect()127 void DeepWaterScene::drawRect() {
128     if (_baseRect == 0) {
129         _baseRect = addRect(0, 0, _width, _height, QPen(Qt::black, 1.0), QBrush(Qt::gray));
130     }
131     _baseRect->setRect(0, 0, _width, _height);
132     _baseRect->setZValue(0);
133 }
134 
135 // draws the poly curve representation
drawPoly()136 void DeepWaterScene::drawPoly() {
137     if (_curvePoly == 0) {
138         _curvePoly = addPolygon(QPolygonF(), QPen(Qt::black, 1.0), QBrush(Qt::darkGray));
139     }
140 
141     QPolygonF poly;
142     poly.append(QPointF(_width, 0));
143     poly.append(QPointF(0, 0));
144     for (int i = 0; i < 1000; i++) {
145         double x = i / 1000.0;
146         poly.append(QPointF(_width * x, _height * _curve->getValue(x)));
147     }
148     poly.append(QPointF(_width, 0));
149     _curvePoly->setPolygon(poly);
150     _curvePoly->setZValue(1);
151 }
152 
153 // draws the base gray outline rectangle
drawGrid()154 void DeepWaterScene::drawGrid() {
155     if (_gridRect == 0) {
156         _gridRect = addRect(0, 0, _width, _height, QPen(Qt::black, 1.0), QBrush(Qt::gray));
157     }
158     _gridRect->setRect(
159         _width * _curve->getKLow(), 0, _width * _curve->getKHigh() - _width * _curve->getKLow(), _height);
160     _gridRect->setBrush(QBrush(_curve->inGrid() ? Qt::green : Qt::cyan));
161     _gridRect->setZValue(2);
162     _gridRect->setOpacity(0.25);
163 }
164 
ExprDeepWater(QWidget * parent)165 ExprDeepWater::ExprDeepWater(QWidget *parent)
166     : QWidget(parent), _scene(0), _resolutionEdit(0), _tileSizeEdit(0), _lengthCutoffEdit(0), _amplitudeEdit(0),
167       _windAngleEdit(0), _windSpeedEdit(0), _flowDirectionEdit(0), _directionalFactorExponentEdit(0),
168       _directionalReflectionDampingEdit(0), _sharpenEdit(0) {
169     QHBoxLayout *mainLayout = new QHBoxLayout();
170     mainLayout->setSpacing(2);
171     mainLayout->setMargin(4);
172 
173     QWidget *edits = new QWidget;
174     QVBoxLayout *editsLayout = new QVBoxLayout;
175     editsLayout->setAlignment(Qt::AlignTop);
176     editsLayout->setSpacing(0);
177     editsLayout->setMargin(0);
178     edits->setLayout(editsLayout);
179 
180     int editWidth = QFontMetrics(font()).boundingRect("[0,0,0]").width() + 8;
181 
182     QWidget *resolution = new QWidget;
183     QHBoxLayout *resolutionLayout = new QHBoxLayout;
184     resolutionLayout->setSpacing(1);
185     resolutionLayout->setMargin(1);
186     resolution->setLayout(resolutionLayout);
187     _resolutionEdit = new DeepWaterLineEdit;
188     _resolutionEdit->setFixedWidth(editWidth);
189     QIntValidator *resolutionValidator = new QIntValidator(_resolutionEdit);
190     resolutionValidator->setBottom(1);
191     _resolutionEdit->setValidator(resolutionValidator);
192     _resolutionEdit->setFixedHeight(20);
193     resolutionLayout->addStretch(50);
194     QLabel *resolutionLabel = new QLabel("R");
195     resolutionLabel->setToolTip("Resolution");
196     resolutionLayout->addWidget(resolutionLabel);
197     resolutionLayout->addWidget(_resolutionEdit);
198 
199     QWidget *tileSize = new QWidget;
200     QHBoxLayout *tileSizeLayout = new QHBoxLayout;
201     tileSizeLayout->setSpacing(1);
202     tileSizeLayout->setMargin(1);
203     tileSize->setLayout(tileSizeLayout);
204     _tileSizeEdit = new DeepWaterLineEdit;
205     _tileSizeEdit->setFixedWidth(editWidth);
206     _tileSizeEdit->setFixedHeight(20);
207     tileSizeLayout->addStretch(50);
208     QLabel *tileSizeLabel = new QLabel("TS");
209     tileSizeLabel->setToolTip("Tile Size");
210     tileSizeLayout->addWidget(tileSizeLabel);
211     tileSizeLayout->addWidget(_tileSizeEdit);
212 
213     QWidget *lengthCutoff = new QWidget;
214     QHBoxLayout *lengthCutoffLayout = new QHBoxLayout;
215     lengthCutoffLayout->setSpacing(1);
216     lengthCutoffLayout->setMargin(1);
217     lengthCutoff->setLayout(lengthCutoffLayout);
218     _lengthCutoffEdit = new DeepWaterLineEdit;
219     _lengthCutoffEdit->setFixedWidth(editWidth);
220     _lengthCutoffEdit->setFixedHeight(20);
221     lengthCutoffLayout->addStretch(50);
222     QLabel *lengthCutoffLabel = new QLabel("LC");
223     lengthCutoffLabel->setToolTip("Length Cutoff");
224     lengthCutoffLayout->addWidget(lengthCutoffLabel);
225     lengthCutoffLayout->addWidget(_lengthCutoffEdit);
226 
227     QWidget *amplitude = new QWidget;
228     QHBoxLayout *amplitudeLayout = new QHBoxLayout;
229     amplitudeLayout->setSpacing(1);
230     amplitudeLayout->setMargin(1);
231     amplitude->setLayout(amplitudeLayout);
232     _amplitudeEdit = new DeepWaterLineEdit;
233     _amplitudeEdit->setFixedWidth(editWidth);
234     _amplitudeEdit->setFixedHeight(20);
235     amplitudeLayout->addStretch(50);
236     QLabel *amplitudeLabel = new QLabel("A");
237     amplitudeLabel->setToolTip("Amplitude");
238     amplitudeLayout->addWidget(amplitudeLabel);
239     amplitudeLayout->addWidget(_amplitudeEdit);
240 
241     QWidget *windAngle = new QWidget;
242     QHBoxLayout *windAngleLayout = new QHBoxLayout;
243     windAngleLayout->setSpacing(1);
244     windAngleLayout->setMargin(1);
245     windAngle->setLayout(windAngleLayout);
246     _windAngleEdit = new DeepWaterLineEdit;
247     _windAngleEdit->setFixedWidth(editWidth);
248     _windAngleEdit->setFixedHeight(20);
249     windAngleLayout->addStretch(50);
250     QLabel *windAngleLabel = new QLabel("WA");
251     windAngleLabel->setToolTip("Wind Angle");
252     windAngleLayout->addWidget(windAngleLabel);
253     windAngleLayout->addWidget(_windAngleEdit);
254 
255     QWidget *windSpeed = new QWidget;
256     QHBoxLayout *windSpeedLayout = new QHBoxLayout;
257     windSpeedLayout->setSpacing(1);
258     windSpeedLayout->setMargin(1);
259     windSpeed->setLayout(windSpeedLayout);
260     _windSpeedEdit = new DeepWaterLineEdit;
261     _windSpeedEdit->setFixedWidth(editWidth);
262     _windSpeedEdit->setFixedHeight(20);
263     windSpeedLayout->addStretch(50);
264     QLabel *windSpeedLabel = new QLabel("WS");
265     windSpeedLabel->setToolTip("Wind Speed");
266     windSpeedLayout->addWidget(windSpeedLabel);
267     windSpeedLayout->addWidget(_windSpeedEdit);
268 
269     QWidget *directionalFactorExponent = new QWidget;
270     QHBoxLayout *directionalFactorExponentLayout = new QHBoxLayout;
271     directionalFactorExponentLayout->setSpacing(1);
272     directionalFactorExponentLayout->setMargin(1);
273     directionalFactorExponent->setLayout(directionalFactorExponentLayout);
274     _directionalFactorExponentEdit = new DeepWaterLineEdit;
275     _directionalFactorExponentEdit->setFixedWidth(editWidth);
276     _directionalFactorExponentEdit->setFixedHeight(20);
277     directionalFactorExponentLayout->addStretch(50);
278     QLabel *directionalFactorExponentLabel = new QLabel("DFE");
279     directionalFactorExponentLabel->setToolTip("Directional Factor Exponent");
280     directionalFactorExponentLayout->addWidget(directionalFactorExponentLabel);
281     directionalFactorExponentLayout->addWidget(_directionalFactorExponentEdit);
282 
283     QWidget *directionalReflectionDamping = new QWidget;
284     QHBoxLayout *directionalReflectionDampingLayout = new QHBoxLayout;
285     directionalReflectionDampingLayout->setSpacing(1);
286     directionalReflectionDampingLayout->setMargin(1);
287     directionalReflectionDamping->setLayout(directionalReflectionDampingLayout);
288     _directionalReflectionDampingEdit = new DeepWaterLineEdit;
289     _directionalReflectionDampingEdit->setFixedWidth(editWidth);
290     _directionalReflectionDampingEdit->setFixedHeight(20);
291     directionalReflectionDampingLayout->addStretch(50);
292     QLabel *directionalReflectionDampingLabel = new QLabel("DRD");
293     directionalReflectionDampingLabel->setToolTip("Directional Reflection Damping");
294     directionalReflectionDampingLayout->addWidget(directionalReflectionDampingLabel);
295     directionalReflectionDampingLayout->addWidget(_directionalReflectionDampingEdit);
296 
297     QWidget *flowDirection = new QWidget;
298     QHBoxLayout *flowDirectionLayout = new QHBoxLayout;
299     flowDirectionLayout->setSpacing(1);
300     flowDirectionLayout->setMargin(1);
301     flowDirection->setLayout(flowDirectionLayout);
302     _flowDirectionEdit = new DeepWaterLineEdit;
303     _flowDirectionEdit->setFixedWidth(editWidth);
304     _flowDirectionEdit->setFixedHeight(20);
305     flowDirectionLayout->addStretch(50);
306     QLabel *flowDirectionLabel = new QLabel("FD");
307     flowDirectionLabel->setToolTip("Flow Direction");
308     flowDirectionLayout->addWidget(flowDirectionLabel);
309     flowDirectionLayout->addWidget(_flowDirectionEdit);
310 
311     QWidget *sharpen = new QWidget;
312     QHBoxLayout *sharpenLayout = new QHBoxLayout;
313     sharpenLayout->setSpacing(1);
314     sharpenLayout->setMargin(1);
315     sharpen->setLayout(sharpenLayout);
316     _sharpenEdit = new DeepWaterLineEdit;
317     _sharpenEdit->setFixedWidth(editWidth);
318     _sharpenEdit->setFixedHeight(20);
319     sharpenLayout->addStretch(50);
320     QLabel *sharpenLabel = new QLabel("S");
321     sharpenLabel->setToolTip("Sharpen");
322     sharpenLayout->addWidget(sharpenLabel);
323     sharpenLayout->addWidget(_sharpenEdit);
324 
325     QFrame *curveFrame = new QFrame;
326     curveFrame->setFrameShape(QFrame::Panel);
327     curveFrame->setFrameShadow(QFrame::Sunken);
328     curveFrame->setLineWidth(1);
329     QHBoxLayout *curveFrameLayout = new QHBoxLayout;
330     curveFrameLayout->setMargin(0);
331     DeepWaterGraphicsView *curveView = new DeepWaterGraphicsView;
332     curveView->setFrameShape(QFrame::Panel);
333     curveView->setFrameShadow(QFrame::Sunken);
334     curveView->setLineWidth(1);
335     curveView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
336     curveView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
337     _scene = new DeepWaterScene;
338     curveView->setScene(_scene);
339     curveView->setTransform(QTransform().scale(1, -1));
340     curveView->setRenderHints(QPainter::Antialiasing);
341     curveFrameLayout->addWidget(curveView);
342     curveFrame->setLayout(curveFrameLayout);
343 
344     editsLayout->addWidget(resolution);
345     editsLayout->addWidget(tileSize);
346     editsLayout->addWidget(lengthCutoff);
347     editsLayout->addWidget(amplitude);
348     editsLayout->addWidget(windSpeed);
349     editsLayout->addWidget(directionalFactorExponent);
350     QFrame *line = new QFrame();
351     line->setToolTip("Parameters below this do not affect spectrum");
352     line->setFrameShape(QFrame::HLine);
353     line->setFrameShadow(QFrame::Sunken);
354     editsLayout->addWidget(line);
355     editsLayout->addWidget(windAngle);
356     editsLayout->addWidget(flowDirection);
357     editsLayout->addWidget(directionalReflectionDamping);
358     editsLayout->addWidget(sharpen);
359 
360     mainLayout->addWidget(edits);
361     mainLayout->addWidget(curveFrame);
362     mainLayout->setStretchFactor(curveFrame, 100);
363     setLayout(mainLayout);
364 
365     // SIGNALS
366 
367     connect(_resolutionEdit, SIGNAL(returnPressed()), this, SLOT(resolutionChanged()));
368     connect(_resolutionEdit, SIGNAL(focusOut()), this, SLOT(resolutionChanged()));
369     connect(this, SIGNAL(resolutionChangedSignal(int)), _scene, SLOT(resolutionChanged(int)));
370     connect(_tileSizeEdit, SIGNAL(returnPressed()), this, SLOT(tileSizeChanged()));
371     connect(_tileSizeEdit, SIGNAL(focusOut()), this, SLOT(tileSizeChanged()));
372     connect(this, SIGNAL(tileSizeChangedSignal(double)), _scene, SLOT(tileSizeChanged(double)));
373     connect(_lengthCutoffEdit, SIGNAL(returnPressed()), this, SLOT(lengthCutoffChanged()));
374     connect(_lengthCutoffEdit, SIGNAL(focusOut()), this, SLOT(lengthCutoffChanged()));
375     connect(this, SIGNAL(lengthCutoffChangedSignal(double)), _scene, SLOT(lengthCutoffChanged(double)));
376     connect(_amplitudeEdit, SIGNAL(returnPressed()), this, SLOT(amplitudeChanged()));
377     connect(_amplitudeEdit, SIGNAL(focusOut()), this, SLOT(amplitudeChanged()));
378     connect(this, SIGNAL(amplitudeChangedSignal(double)), _scene, SLOT(amplitudeChanged(double)));
379     connect(_windAngleEdit, SIGNAL(returnPressed()), this, SLOT(windAngleChanged()));
380     connect(_windAngleEdit, SIGNAL(focusOut()), this, SLOT(windAngleChanged()));
381     connect(this, SIGNAL(windAngleChangedSignal(double)), _scene, SLOT(windAngleChanged(double)));
382     connect(_windSpeedEdit, SIGNAL(returnPressed()), this, SLOT(windSpeedChanged()));
383     connect(_windSpeedEdit, SIGNAL(focusOut()), this, SLOT(windSpeedChanged()));
384     connect(this, SIGNAL(windSpeedChangedSignal(double)), _scene, SLOT(windSpeedChanged(double)));
385     connect(_flowDirectionEdit, SIGNAL(returnPressed()), this, SLOT(flowDirectionChanged()));
386     connect(_flowDirectionEdit, SIGNAL(focusOut()), this, SLOT(flowDirectionChanged()));
387     connect(this, SIGNAL(flowDirectionChangedSignal(QString)), _scene, SLOT(flowDirectionChanged(QString)));
388     connect(_directionalFactorExponentEdit, SIGNAL(returnPressed()), this, SLOT(directionalFactorExponentChanged()));
389     connect(_directionalFactorExponentEdit, SIGNAL(focusOut()), this, SLOT(directionalFactorExponentChanged()));
390     connect(this,
391             SIGNAL(directionalFactorExponentChangedSignal(double)),
392             _scene,
393             SLOT(directionalFactorExponentChanged(double)));
394     connect(
395         _directionalReflectionDampingEdit, SIGNAL(returnPressed()), this, SLOT(directionalReflectionDampingChanged()));
396     connect(_directionalReflectionDampingEdit, SIGNAL(focusOut()), this, SLOT(directionalReflectionDampingChanged()));
397     connect(this,
398             SIGNAL(directionalReflectionDampingChangedSignal(double)),
399             _scene,
400             SLOT(directionalReflectionDampingChanged(double)));
401     connect(_sharpenEdit, SIGNAL(returnPressed()), this, SLOT(sharpenChanged()));
402     connect(_sharpenEdit, SIGNAL(focusOut()), this, SLOT(sharpenChanged()));
403     connect(this, SIGNAL(sharpenChangedSignal(double)), _scene, SLOT(sharpenChanged(double)));
404 
405     // when the widget is resized, resize the curve widget
406     connect(curveView, SIGNAL(resizeSignal(int, int)), _scene, SLOT(resize(int, int)));
407 }
408 
resolutionChanged()409 void ExprDeepWater::resolutionChanged() {
410     int val = QString(_resolutionEdit->text()).toInt();
411     emit resolutionChangedSignal(val);
412 }
413 
tileSizeChanged()414 void ExprDeepWater::tileSizeChanged() {
415     double val = QString(_tileSizeEdit->text()).toDouble();
416     emit tileSizeChangedSignal(val);
417 }
418 
lengthCutoffChanged()419 void ExprDeepWater::lengthCutoffChanged() {
420     double val = QString(_lengthCutoffEdit->text()).toDouble();
421     emit lengthCutoffChangedSignal(val);
422 }
423 
amplitudeChanged()424 void ExprDeepWater::amplitudeChanged() {
425     double val = QString(_amplitudeEdit->text()).toDouble();
426     emit amplitudeChangedSignal(val);
427 }
428 
windAngleChanged()429 void ExprDeepWater::windAngleChanged() {
430     double val = QString(_windAngleEdit->text()).toDouble();
431     emit windAngleChangedSignal(val);
432 }
433 
windSpeedChanged()434 void ExprDeepWater::windSpeedChanged() {
435     double val = QString(_windSpeedEdit->text()).toDouble();
436     emit windSpeedChangedSignal(val);
437 }
438 
flowDirectionChanged()439 void ExprDeepWater::flowDirectionChanged() { emit flowDirectionChangedSignal(_flowDirectionEdit->text()); }
440 
directionalFactorExponentChanged()441 void ExprDeepWater::directionalFactorExponentChanged() {
442     double val = QString(_directionalFactorExponentEdit->text()).toDouble();
443     emit directionalFactorExponentChangedSignal(val);
444 }
445 
directionalReflectionDampingChanged()446 void ExprDeepWater::directionalReflectionDampingChanged() {
447     double val = QString(_directionalReflectionDampingEdit->text()).toDouble();
448     emit directionalReflectionDampingChangedSignal(val);
449 }
450 
sharpenChanged()451 void ExprDeepWater::sharpenChanged() {
452     double val = QString(_sharpenEdit->text()).toDouble();
453     emit sharpenChangedSignal(val);
454 }
455 
setParams(const SeDeepWaterParams & params)456 void ExprDeepWater::setParams(const SeDeepWaterParams &params) {
457     _scene->setParams(params);
458     _resolutionEdit->setText(QString::number(params.resolution));
459     _tileSizeEdit->setText(QString::number(params.tileSize));
460     _lengthCutoffEdit->setText(QString::number(params.lengthCutoff));
461     _amplitudeEdit->setText(QString::number(params.amplitude));
462     _windAngleEdit->setText(QString::number(params.windAngle));
463     _windSpeedEdit->setText(QString::number(params.windSpeed));
464     QString flowDirection = "[";
465     flowDirection += QString::number(params.flowDirection[0]) + "," + QString::number(params.flowDirection[1]) + "," +
466                      QString::number(params.flowDirection[2]) + "]";
467     _flowDirectionEdit->setText(flowDirection);
468     _directionalFactorExponentEdit->setText(QString::number(params.directionalFactorExponent));
469     _directionalReflectionDampingEdit->setText(QString::number(params.directionalReflectionDamping));
470     _sharpenEdit->setText(QString::number(params.sharpen));
471 }
472