-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathComponentAndCladdingWindEDP.cpp
512 lines (391 loc) · 17.5 KB
/
ComponentAndCladdingWindEDP.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* *****************************************************************************
Copyright (c) 2016-2017, The Regents of the University of California (Regents).
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS
PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*************************************************************************** */
// Written: Abiy Melaku
#include <QJsonObject>
#include "ComponentAndCladdingWindEDP.h"
#include <SC_FileEdit.h>
#include <QLabel>
#include <QGroupBox>
#include <QGridLayout>
#include <QLineEdit>
#include <QFileDialog>
#include <QMessageBox>
#include <WindEventSelection.h>
#include <IsolatedBuildingCFD.h>
#include "vtkGenericOpenGLRenderWindow.h"
#include <vtkSmartPointer.h>
#include <vtkGlyph3DMapper.h>
#include <vtkDataObjectToTable.h>
#include <vtkElevationFilter.h>
#include <vtkNew.h>
#include <vtkVoxelGrid.h>
#include <vtkCompositeDataSet.h>
#include <vtkCellCenters.h>
//#include <vtkPoissonDiskSampler.h>
#include <vtkPoints.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkOpenFOAMReader.h>
#include <vtkCleanPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkQtTableView.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkVectorText.h>
#include <vtkStructuredGrid.h>
#include <vtkUnstructuredGrid.h>
#include <vtkGenericDataObjectReader.h>
#include <vtkRectilinearGrid.h>
#include <vtkCellData.h>
#include <vtkDataSetMapper.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkSTLReader.h>
#include <vtkPointData.h>
#include <vtkPointSource.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkInformation.h>
#include <vtkSmartPointer.h>
#include <vtkUnstructuredGrid.h>
#include <QVTKRenderWidget.h>
#include <vtkRegressionTestImage.h>
#include <vtkTestUtilities.h>
#include <vtkSimplePointsReader.h>
#include <WindEventSelection.h>
#include <SimCenterPreferences.h>
#include <QProcess>
#include <QHeaderView>
ComponentAndCladdingWindEDP::ComponentAndCladdingWindEDP(QWidget *parent)
: SimCenterAppWidget(parent)
{
initialized = false;
}
bool ComponentAndCladdingWindEDP::initialize()
{
QVBoxLayout *layout = new QVBoxLayout();
layout->setAlignment(Qt::AlignTop | Qt::AlignLeft); // Align the main layout to the top-left
importComponentGroup = new QGroupBox("Components and Cladding Definition");
importComponentLayout = new QGridLayout();
importComponentGroup->setLayout(importComponentLayout);
QLabel *importJsonLabel = new QLabel("Components Geometry JSON Path:");
//FMK componentDefFilePath = new QLineEdit();
componentDefFilePath = new SC_FileEdit("componentDefFilePath");
//FMK importButton = new QPushButton("Browse");
considerWindDirection = new QCheckBox("Consider Wind Direction");
considerWindDirection->setChecked(true);
considerWindDirection->setToolTip("If checked, rotates the component geometry to align with the building orientation.");
snapToBuilding = new QCheckBox("Snap Components To Building Surface");
snapToBuilding->setChecked(true);
snapToBuilding->setToolTip("If checked, snaps the component geometry to the building surface even if there is a gap.");
showCompGeometryButton = new QPushButton("Map Component Geometry onto Building Surface");
importComponentLayout->addWidget(importJsonLabel, 0, 0);
importComponentLayout->addWidget(componentDefFilePath, 0, 1);
//FMK importComponentLayout->addWidget(importButton, 0, 2);
importComponentLayout->addWidget(considerWindDirection, 1, 0);
importComponentLayout->addWidget(snapToBuilding, 2, 0);
importComponentLayout->addWidget(showCompGeometryButton, 3, 0, 1, 3);
importComponentGroup->setMaximumWidth(800);
importComponentLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft); // Align the group box contents to the top-left
connect(importButton, SIGNAL(clicked()), this, SLOT(onBrowseButtonClicked()));
connect(showCompGeometryButton, SIGNAL(clicked()), this, SLOT(onShowCompGeometryButtonClicked()));
\
layout->addWidget(importComponentGroup);
this->setLayout(layout);
initialized = true;
return true;
}
ComponentAndCladdingWindEDP::~ComponentAndCladdingWindEDP()
{
}
void ComponentAndCladdingWindEDP::clear(void)
{
}
void ComponentAndCladdingWindEDP::onShowCompGeometryButtonClicked()
{
//First check if the component JSON file is properly defined
// FMK QFile jsonFile(componentDefFilePath->text());
QFile jsonFile(componentDefFilePath->getFilename());
if (!jsonFile.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox msgBox;
msgBox.setWindowTitle("WE-UQ Error");
msgBox.setText("The JSON file for component and cladding is is not correctly defined!");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
return;
}
// FMK - double check!!
// Open the JSON file
// QFile file(componentDefFilePath->text());
//if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
// qCritical() << "Could not open file:" << componentDefFilePath->text();
// return;
// }
// Read the file content
QByteArray jsonData = jsonFile.readAll();
jsonFile.close();
// Parse JSON content
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
if (jsonDoc.isNull() || !jsonDoc.isObject()) {
qCritical() << "Invalid JSON format.";
return;
}
QJsonObject rootObj = jsonDoc.object();
QWidget* componentsWidget = new QWidget();
// componentsWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// Setup the QDialog
QDialog* dialog = new QDialog(this);
dialog->setWindowTitle("Component Geometry");
dialog->resize(800, 600); // Set initial size
// Create a horizontal layout for the dialog
QHBoxLayout* dialogLayout = new QHBoxLayout();
dialogLayout->setContentsMargins(0, 0, 0, 0);
dialogLayout->setSpacing(5);
dialogLayout->setAlignment(Qt::AlignTop); // Align the layout to the top
//==================================================
// Setup the VTK window
//==================================================
QVTKRenderWidget* qvtkWidget = new QVTKRenderWidget();
qvtkWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
WindEventSelection* evt = dynamic_cast<WindEventSelection*>(windEventSelection);
IsolatedBuildingCFD* theIso = dynamic_cast<IsolatedBuildingCFD*>(evt->getCurrentEvent());
generateCompGeometry(theIso->caseDir());
//Building mapper
vtkNew<vtkPolyDataMapper>buildingMapper; //mapper
buildingMapper->SetInputData(theIso->getBldgBlock());
buildingMapper->Update();
//Building actor
vtkNew<vtkActor> buildingActor;// Actor in scene
buildingActor->SetMapper(buildingMapper);
buildingActor->GetProperty()->SetColor(0.5, 0.5, 0.5);
buildingActor->GetProperty()->SetRepresentationToSurface();
buildingActor->GetProperty()->SetEdgeVisibility(false);
vtkSmartPointer<vtkSTLReader> componentSTLReader = vtkSmartPointer<vtkSTLReader>::New();
componentSTLReader->SetFileName((theIso->caseDir() + "/constant/geometry/components/components_geometry.stl").toStdString().c_str());
componentSTLReader->Update();
//Component mapper
vtkNew<vtkPolyDataMapper>componentgMapper; //mapper
componentgMapper->SetInputData(componentSTLReader->GetOutput());
componentgMapper->Update();
//Component actor
vtkNew<vtkActor> componentActor;// Actor in scene
componentActor->SetMapper(componentgMapper);
componentActor->GetProperty()->SetColor(0.0, 0.0, 1.0);
componentActor->GetProperty()->SetRepresentationToSurface();
componentActor->GetProperty()->SetEdgeVisibility(false);
//point reader
vtkNew<vtkSimplePointsReader> pointsReader;
pointsReader->SetFileName((theIso->caseDir() + "/constant/geometry/components/probe_points.txt").toStdString().c_str());
pointsReader->Update();
//point mapper
vtkNew<vtkDataSetMapper> pointsMapper; //mapper
pointsMapper->SetInputData(pointsReader->GetOutput());
//Points actor
vtkNew<vtkActor> pointsActor;// Actor in scene
pointsActor->SetMapper(pointsMapper);
pointsActor->GetProperty()->SetColor(0.0, 1.0, 0.0);
pointsActor->GetProperty()->SetPointSize(10);
// Add Actor to renderer
vtkNew<vtkRenderer> renderer; // VTK Renderer
renderer->AddActor(buildingActor);
renderer->AddActor(pointsActor);
renderer->AddActor(componentActor);
renderer->SetBackground(1.0, 1.0, 1.0);
// VTK/Qt wedded
vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
qvtkWidget->setRenderWindow(renderWindow);
qvtkWidget->renderWindow()->AddRenderer(renderer);
renderWindow->BordersOn();
renderer->ResetCamera();
renderWindow->Render();
//=======================================================
//Component Table
//=======================================================
QJsonArray components = rootObj.value("components").toArray();
QStringList headerTitles = {"Name", "Type", "Geometries", "Demand", "Load", "Probe Density" };
int numCols = headerTitles.size();
int numRows = components.size();
QTableWidget* componentsTable = new QTableWidget(numRows, numCols, componentsWidget);
componentsTable->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
componentsTable->setHorizontalHeaderLabels(headerTitles);
for (int i=0; i < numCols; i++)
{
// componentsTable->setColumnWidth(i, componentsTable->size().width()/numCols - 15);
for (int j=0; j < numRows; j++)
{
componentsTable->setItem(j, i, new QTableWidgetItem(""));
}
}
// Parse components array using index-based for loop
for (int i = 0; i < components.size(); ++i)
{
QJsonObject componentObj = components[i].toObject();
// int componentId = componentObj.value("componentId").toInt();
QString componentType = componentObj.value("componentType").toString();
QString componentName = componentObj.value("componentName").toString();
QString loadValue = componentObj.value("loadValue").toString();
QString loadType = componentObj.value("loadType").toString();
QJsonArray geometries = componentObj.value("geometries").toArray();
int samplingDensity = componentObj.value("samplingDensity").toInt();
componentsTable->item(i, 0)->setText(componentName);
componentsTable->item(i, 1)->setText(componentType);
componentsTable->item(i, 2)->setText(QString::number(geometries.size()));
componentsTable->item(i, 3)->setText(loadType);
componentsTable->item(i, 4)->setText(loadValue);
componentsTable->item(i, 5)->setText(QString::number(samplingDensity));
}
// Resize columns to fit text content
componentsTable->resizeColumnsToContents();
dialog->setLayout(dialogLayout);
// Add QVTKRenderWidget and QTableWidget to the layout
dialogLayout->addWidget(qvtkWidget, 1); // VTK widget takes half the space
// dialogLayout->addWidget(componentsTable, 1); // Table takes the other half
dialogLayout->addWidget(componentsTable, 1, Qt::AlignTop);
dialog->exec();
}
void ComponentAndCladdingWindEDP::onBrowseButtonClicked()
{
QString stlFileName = QFileDialog::getOpenFileName(this, tr("Open JSON File"), componentDefFilePath->getFilename(), tr("JSON Files (*.json)"));
QFile jsonFile(stlFileName);
if (jsonFile.open(QFile::ReadOnly | QFile::Text))
{
componentDefFilePath->setFilename(stlFileName);
}
else
{
qDebug() << "Cannot find the path: " << stlFileName;
}
}
bool ComponentAndCladdingWindEDP::outputToJSON(QJsonObject &jsonObject)
{
// just need to send the class type here.. type needed in object in case user screws up
jsonObject["type"]="ComponentAndCladdingWindEDP";
QFile jsonFile(componentDefFilePath->getFilename());
if (!jsonFile.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox msgBox;
msgBox.setWindowTitle("WE-UQ Error");
msgBox.setText("The JSON file for component and cladding is is not correctly defined!");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
false;
}
/* DOUBLE WHAMMY AGAIN!
// Open the JSON file
QFile file(componentDefFilePath->text());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qCritical() <<"Could not open file:" << componentDefFilePath->text();
false;
}
*/
// Read the file content
QByteArray jsonData = jsonFile.readAll();
jsonFile.close();
// Parse JSON content
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
if (jsonDoc.isNull() || !jsonDoc.isObject()) {
qCritical() << "Invalid JSON format.";
false;
}
QJsonObject rootObj = jsonDoc.object();
jsonObject["components"] = rootObj.value("components");
componentDefFilePath->outputToJSON(jsonObject);
//FMK jsonObject["componentDefFilePath"] = componentDefFilePath->text();
return true;
}
bool ComponentAndCladdingWindEDP::inputFromJSON(QJsonObject &jsonObject)
{
Q_UNUSED(jsonObject);
//componentDefFilePath->setText(jsonObject["componentDefFilePath"].toString());
componentDefFilePath->inputFromJSON(jsonObject);
return true;
}
bool ComponentAndCladdingWindEDP::outputAppDataToJSON(QJsonObject &jsonObject) {
//
// per API, need to add name of application to be called in AppLication
// and all data to be used in ApplicationDate
//
jsonObject["Application"] = "ComponentAndCladdingWindEDP";
QJsonObject dataObj;
jsonObject["ApplicationData"] = dataObj;
return true;
}
bool ComponentAndCladdingWindEDP::inputAppDataFromJSON(QJsonObject &jsonObject) {
Q_UNUSED(jsonObject);
return true;
}
bool ComponentAndCladdingWindEDP::copyFiles(QString &dirName) {
Q_UNUSED(dirName);
componentDefFilePath->copyFile(dirName);
return true;
}
bool ComponentAndCladdingWindEDP::isInitialize()
{
return initialized;
}
void ComponentAndCladdingWindEDP::setSelectedEvent(SimCenterAppWidget* event)
{
windEventSelection = event;
}
bool ComponentAndCladdingWindEDP::generateCompGeometry(QString caseDir)
{
//Run python script to create component geometry
QString scriptPath = pyScriptsPath() + "/generate_component_geometry.py";
QString jsonPath = caseDir + QDir::separator() + "constant" + QDir::separator() + "simCenter" + QDir::separator() + "input";
QString comptJsonPath = componentDefFilePath->getFilename();
QString outputPath =caseDir;
QString templatePath = templateDictDir();
QString program = SimCenterPreferences::getInstance()->getPython();
QStringList arguments;
arguments << scriptPath << jsonPath << comptJsonPath << templatePath << outputPath;
QProcess *process = new QProcess(this);
process->start(program, arguments);
process->waitForFinished(-1);
statusMessage(process->readAllStandardOutput() + "\n" + process->readAllStandardError());
process->close();
return true;
}
QString ComponentAndCladdingWindEDP::pyScriptsPath()
{
QString backendAppDir = SimCenterPreferences::getInstance()->getAppDir() + QDir::separator()
+ QString("applications") + QDir::separator() + QString("createEVENT") + QDir::separator()
+ QString("IsolatedBuildingCFD");
return backendAppDir;
}
QString ComponentAndCladdingWindEDP::templateDictDir()
{
QString templateDictsDir = SimCenterPreferences::getInstance()->getAppDir() + QDir::separator()
+ QString("applications") + QDir::separator() + QString("createEVENT") + QDir::separator()
+ QString("IsolatedBuildingCFD") + QDir::separator() + QString("templateOF10Dicts");
return templateDictsDir;
}