-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProcessingEngine.cpp
144 lines (114 loc) · 3.65 KB
/
ProcessingEngine.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
#include "ProcessingEngine.h"
#include <thread>
#include <memory>
#include <QDebug>
#include <QFileInfo>
#include <vtkAlgorithmOutput.h>
#include <vtkOBJReader.h>
#include <vtkPolyDataNormals.h>
#include <vtkProperty.h>
#include <vtkSTLReader.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include "Model.h"
ProcessingEngine::ProcessingEngine()
{
}
const std::shared_ptr<Model> &ProcessingEngine::addModel(const QUrl &modelFilePath)
{
qDebug() << "ProcessingEngine::addModelData()";
QString modelFilePathExtension = QFileInfo(modelFilePath.toString()).suffix().toLower();
vtkSmartPointer<vtkOBJReader> objReader = vtkSmartPointer<vtkOBJReader>::New();
vtkSmartPointer<vtkSTLReader> stlReader = vtkSmartPointer<vtkSTLReader>::New();
vtkSmartPointer<vtkPolyData> inputData;
if (modelFilePathExtension == "obj")
{
// Read OBJ file
objReader->SetFileName(modelFilePath.toString().toStdString().c_str());
objReader->Update();
inputData = objReader->GetOutput();
}
else
{
// Read STL file
stlReader->SetFileName(modelFilePath.toString().toStdString().c_str());
stlReader->Update();
inputData = stlReader->GetOutput();
}
// Preprocess the polydata
vtkSmartPointer<vtkPolyData> preprocessedPolydata = preprocessPolydata(inputData);
// Create Model instance and insert it into the vector
std::shared_ptr<Model> model = std::make_shared<Model>(preprocessedPolydata);
m_models.push_back(model);
return m_models.back();
}
vtkSmartPointer<vtkPolyData> ProcessingEngine::preprocessPolydata(const vtkSmartPointer<vtkPolyData> inputData) const
{
// Center the polygon
double center[3];
inputData->GetCenter(center);
vtkSmartPointer<vtkTransform> translation = vtkSmartPointer<vtkTransform>::New();
translation->Translate(-center[0], -center[1], -center[2]);
vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
transformFilter->SetInputData(inputData);
transformFilter->SetTransform(translation);
transformFilter->Update();
// Normals - For the Gouraud interpolation to work
vtkSmartPointer<vtkPolyDataNormals> normals = vtkSmartPointer<vtkPolyDataNormals>::New();
normals->SetInputData(transformFilter->GetOutput());
normals->ComputePointNormalsOn();
normals->Update();
return normals->GetOutput();
}
void ProcessingEngine::placeModel(Model &model) const
{
qDebug() << "ProcessingEngine::placeModel()";
model.translateToPosition(0, 0);
}
void ProcessingEngine::setModelsRepresentation(const int modelsRepresentationOption) const
{
for (const std::shared_ptr<Model>& model : m_models)
{
model->getModelActor()->GetProperty()->SetRepresentation(modelsRepresentationOption);
}
}
void ProcessingEngine::setModelsOpacity(const double modelsOpacity) const
{
for (const std::shared_ptr<Model>& model : m_models)
{
model->getModelActor()->GetProperty()->SetOpacity(modelsOpacity);
}
}
void ProcessingEngine::setModelsGouraudInterpolation(const bool enableGouraudInterpolation) const
{
for (const std::shared_ptr<Model>& model : m_models)
{
if (enableGouraudInterpolation)
{
model->getModelActor()->GetProperty()->SetInterpolationToGouraud();
}
else
{
model->getModelActor()->GetProperty()->SetInterpolationToFlat();
}
}
}
void ProcessingEngine::updateModelsColor() const
{
for (const std::shared_ptr<Model>& model : m_models)
{
model->updateModelColor();
}
}
std::shared_ptr<Model> ProcessingEngine::getModelFromActor(const vtkSmartPointer<vtkActor> modelActor) const
{
for (const std::shared_ptr<Model> &model : m_models)
{
if (model->getModelActor() == modelActor)
{
return model;
}
}
// Raise exception instead
return nullptr;
}