oILAB
Loading...
Searching...
No Matches
BicrystalActor.cpp
Go to the documentation of this file.
1/* This file is part of MODEL, the Mechanics Of Defect Evolution Library.
2 *
3 * Copyright (C) 2011 by Giacomo Po <gpo@ucla.edu>.
4 *
5 * model is distributed without any warranty under the
6 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
7 */
8
9#ifndef model_BicrystalActor_cpp_
10#define model_BicrystalActor_cpp_
11
12#include <iostream>
13#include <deque>
14#include <string>
15
16
17#include <vtkVersion.h>
18#include <vtkSmartPointer.h>
19#include <vtkPolyData.h>
20#include <vtkPolyDataMapper.h>
21#include <vtkActor.h>
22#include <vtkMath.h>
23#include <vtkProperty.h>
24#include <vtkTubeFilter.h>
25#include <vtkPolyLine.h>
26#include <vtkSphereSource.h>
27#include <vtkArrowSource.h>
28#include <vtkGlyph3D.h>
29#include <vtkDoubleArray.h>
30#include <vtkPointData.h>
31#include <vtkLabeledDataMapper.h>
32#include <vtkFloatArray.h>
33#include <vtkTextProperty.h>
34#include <vtkActor2D.h>
35#include <vtkProperty2D.h>
36#include <vtkRenderer.h>
37
38#include "../../include/IO/TerminalColors.h"
39#include "../../include/Visualization/BicrystalActor.h"
40
41namespace oILAB {
42
43/**********************************************************************/
44BicrystalActor::BicrystalActor(vtkGenericOpenGLRenderWindow *const renWin,
45 vtkRenderer *const rndr)
46 : /* init */ renderWindow(renWin)
47 /* init */,
48 renderer(rndr)
49 /* init */,
50 mainLayout(new QGridLayout(this))
51 /* init */,
52 showA(new QCheckBox(this))
53 /* init */,
54 showB(new QCheckBox(this))
55
56 // /* init */,showNodeLabels(new QCheckBox(this))
57 // /* init */,showVelocities(new QCheckBox(this))
58 // /* init */,velocityScaleEdit(new QLineEdit("1"))
59 /* init */,
60 aPolyData(vtkSmartPointer<vtkPolyData>::New())
61 /* init */,
62 aGlyphs(vtkSmartPointer<vtkGlyph3D>::New())
63 /* init */,
64 aMapper(vtkSmartPointer<vtkPolyDataMapper>::New())
65 /* init */,
66 aActor(vtkSmartPointer<vtkActor>::New())
67 /* init */,
68 bPolyData(vtkSmartPointer<vtkPolyData>::New())
69 /* init */,
70 bGlyphs(vtkSmartPointer<vtkGlyph3D>::New())
71 /* init */,
72 bMapper(vtkSmartPointer<vtkPolyDataMapper>::New())
73 /* init */,
74 bActor(vtkSmartPointer<vtkActor>::New())
75
76// /* init */,labelPolyData(vtkSmartPointer<vtkPolyData>::New())
77// /* init */,labelMapper(vtkSmartPointer<vtkLabeledDataMapper>::New())
78// /* init */,labelActor(vtkSmartPointer<vtkActor2D>::New())
79// /* init */,velocityPolyData(vtkSmartPointer<vtkPolyData>::New())
80// /* init */,velocityGlyphs(vtkSmartPointer<vtkGlyph3D>::New())
81// /* init */,velocityMapper(vtkSmartPointer<vtkPolyDataMapper>::New())
82// /* init */,velocityActor(vtkSmartPointer<vtkActor>::New())
83// /* init
84// */,singleNodeLabelPolyData(vtkSmartPointer<vtkPolyData>::New())
85// /* init
86// */,singleNodeLabelMapper(vtkSmartPointer<vtkLabeledDataMapper>::New())
87// /* init */,singleNodeLabelActor(vtkSmartPointer<vtkActor2D>::New())
88// /* init */,singleNodeID(0)
89// /* init */,nodeClr{{100,100,100},{0,255,255},{255,0,255},{1,1,1}}
90{
91
92 showA->setText("lattice A");
93 showA->setChecked(true);
94 aActor->SetVisibility(true);
95 showB->setText("lattice B");
96 showB->setChecked(true);
97 bActor->SetVisibility(true);
98
99 // showNodeLabels->setText("node labels");
100 // showNodeLabels->setChecked(false);
101 // labelActor->SetVisibility(false);
102 //
103 // showVelocities->setText("velocities");
104 // showVelocities->setChecked(false);
105 // velocityActor->SetVisibility(false);
106 // velocityScaleEdit->setEnabled(false);
107
108 mainLayout->addWidget(showA, 0, 0, 1, 1);
109 mainLayout->addWidget(showB, 1, 0, 1, 1);
110
111 // mainLayout->addWidget(showNodeLabels,1,0,1,1);
112 // mainLayout->addWidget(showVelocities,2,0,1,1);
113 // mainLayout->addWidget(velocityScaleEdit,2,1,1,1);
114 this->setLayout(mainLayout);
115
116 connect(showA, SIGNAL(stateChanged(int)), this, SLOT(modify()));
117 connect(showB, SIGNAL(stateChanged(int)), this, SLOT(modify()));
118
119 // connect(showNodeLabels,SIGNAL(stateChanged(int)), this,
120 // SLOT(modify()));
121 // connect(showVelocities,SIGNAL(stateChanged(int)), this,
122 // SLOT(modify()));
123 // connect(velocityScaleEdit,SIGNAL(returnPressed()), this,
124 // SLOT(modify()));
125
126 aGlyphs->SetInputData(aPolyData);
127 aGlyphs->SetSourceConnection(
128 vtkSmartPointer<vtkSphereSource>::New()->GetOutputPort());
129 aGlyphs->ScalingOn();
130 aGlyphs->SetScaleModeToScaleByVector();
131 aGlyphs->SetScaleFactor(0.25);
132 aGlyphs->SetColorModeToColorByScalar();
133 aGlyphs->Update();
134 aMapper->SetInputConnection(aGlyphs->GetOutputPort());
135 aActor->SetMapper(aMapper);
136 aActor->GetProperty()->SetColor(1.0, 0.0, 0.0); //(R,G,B)
137 renderer->AddActor(aActor);
138
139 bGlyphs->SetInputData(bPolyData);
140 bGlyphs->SetSourceConnection(
141 vtkSmartPointer<vtkSphereSource>::New()->GetOutputPort());
142 bGlyphs->ScalingOn();
143 bGlyphs->SetScaleModeToScaleByVector();
144 bGlyphs->SetScaleFactor(0.25);
145 bGlyphs->SetColorModeToColorByScalar();
146 bGlyphs->Update();
147 bMapper->SetInputConnection(bGlyphs->GetOutputPort());
148 bActor->SetMapper(bMapper);
149 bActor->GetProperty()->SetColor(0.0, 0.0, 1.0); //(R,G,B)
150 renderer->AddActor(bActor);
151
152 // Labels
153 // labelMapper->SetInputData(labelPolyData);
154 // labelMapper->SetLabelModeToLabelScalars();
155 // labelMapper->SetLabelFormat("%1.0f");
156 // labelMapper->GetLabelTextProperty()->SetFontSize(20);
157 // labelActor->SetMapper(labelMapper);
158 // labelActor->GetProperty()->SetColor(0.0, 0.0, 0.0); //(R,G,B)
159 //
160 // // Velocities
161 // velocityGlyphs->SetInputData(velocityPolyData);
162 // velocityGlyphs->SetSourceConnection(vtkSmartPointer<vtkArrowSource>::New()->GetOutputPort());
163 // velocityGlyphs->ScalingOn();
164 // velocityGlyphs->SetScaleModeToScaleByVector();
165 // velocityGlyphs->OrientOn();
166 // velocityGlyphs->ClampingOff();
167 // velocityGlyphs->SetVectorModeToUseVector();
168 // velocityGlyphs->SetIndexModeToOff();
169 // velocityMapper->SetInputConnection(velocityGlyphs->GetOutputPort());
170 // velocityMapper->ScalarVisibilityOff();
171 // velocityActor->SetMapper(velocityMapper);
172 // velocityActor->GetProperty()->SetColor(1.0, 0.0, 1.0); //(R,G,B)
173 //
174 // // Single node Label
175 // singleNodeLabelMapper->SetInputData(singleNodeLabelPolyData);
176 // singleNodeLabelMapper->SetLabelModeToLabelScalars();
177 // singleNodeLabelMapper->SetLabelFormat("%1.0f");
178 // singleNodeLabelMapper->GetLabelTextProperty()->SetFontSize(20);
179 // singleNodeLabelActor->SetMapper(singleNodeLabelMapper);
180 // singleNodeLabelActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
181 // //(R,G,B) singleNodeLabelActor->VisibilityOff();
182
183 // renderer->AddActor(velocityActor);
184 // renderer->AddActor(labelActor);
185 // renderer->AddActor(singleNodeLabelActor);
186
187 }
188
189
190
191 /**********************************************************************/
192 void BicrystalActor::updateConfiguration(const std::shared_ptr<BiCrystal<3>>& bc)
193 {// https://stackoverflow.com/questions/6878263/remove-individual-points-from-vtkpoints
194 std::cout<<"Updating BiCrystal..."<<std::flush;
195 const auto t0= std::chrono::system_clock::now();
196//
197 vtkSmartPointer<vtkPoints> aPoints(vtkSmartPointer<vtkPoints>::New());
198 vtkSmartPointer<vtkPoints> bPoints(vtkSmartPointer<vtkPoints>::New());
199
200 const int N=5;
201 for(int i=-N;i<N+1;++i)
202 {
203 for(int j=-N;j<N+1;++j)
204 {
205 for(int k=-N;k<N+1;++k)
206 {
207 const Eigen::Matrix<double,3,1> Pa(bc->A.latticeBasis*(Eigen::Matrix<double,3,1>()<<i,j,k).finished());
208 aPoints->InsertNextPoint(Pa.data());
209 const Eigen::Matrix<double,3,1> Pb(bc->B.latticeBasis*(Eigen::Matrix<double,3,1>()<<i,j,k).finished());
210 bPoints->InsertNextPoint(Pb.data());
211
212 }
213 }
214 }
215
216
217// vtkSmartPointer<vtkUnsignedCharArray> nodeColors(vtkSmartPointer<vtkUnsignedCharArray>::New());
218// nodeColors->SetNumberOfComponents(3);
219//
220// vtkSmartPointer<vtkDoubleArray> nodeLabels(vtkSmartPointer<vtkDoubleArray>::New());
221// nodeLabels->SetNumberOfComponents(1);
222// nodeLabels->SetName("node IDs");
223//
224// vtkSmartPointer<vtkPoints> singleNodePoint(vtkSmartPointer<vtkPoints>::New());
225// vtkSmartPointer<vtkDoubleArray> singleNodenodeLabels(vtkSmartPointer<vtkDoubleArray>::New());
226//
227// vtkSmartPointer<vtkDoubleArray> velocityVectors(vtkSmartPointer<vtkDoubleArray>::New());
228// velocityVectors->SetNumberOfComponents(3);
229// velocityVectors->SetName("nodeVelocity");
230//
231// for(const auto& node : configIO.nodes())
232// {
233// nodePoints->InsertNextPoint(node.P.data());
234// nodeLabels->InsertNextTuple1(node.sID);
235// velocityVectors->InsertNextTuple(node.V.data()); // arrow vector
236// nodeColors->InsertNextTypedTuple(node.meshLocation>2? this->nodeClr[3] : this->nodeClr[node.meshLocation]);
237//
238// // Single node
239// if(node.sID==singleNodeID)
240// {
241// singleNodePoint->InsertNextPoint(node.P.data());
242// singleNodenodeLabels->InsertNextTuple1(node.sID);
243// }
244// }
245//
246 aPolyData->SetPoints(aPoints);
247 aPolyData->Modified();
248 bPolyData->SetPoints(bPoints);
249 bPolyData->Modified();
250
251//
252// labelPolyData->SetPoints(nodePoints);
253// labelPolyData->GetPointData()->SetScalars(nodeLabels);
254// labelPolyData->Modified();
255//
256// singleNodeLabelPolyData->SetPoints(singleNodePoint);
257// singleNodeLabelPolyData->GetPointData()->SetScalars(singleNodenodeLabels);
258// singleNodeLabelPolyData->Modified();
259//
260// velocityPolyData->SetPoints(nodePoints);
261// velocityPolyData->GetPointData()->SetVectors(velocityVectors);
262// velocityPolyData->Modified();
263 renderer->ResetCamera();
264 renderWindow->Render();
265 std::cout<<magentaColor<<" ["<<(std::chrono::duration<double>(std::chrono::system_clock::now()-t0)).count()<<" sec]"<<defaultColor<<std::endl;
266 }
267
268 /**********************************************************************/
270 {
271
272 aActor->SetVisibility(showA->isChecked());
273 bActor->SetVisibility(showB->isChecked());
274
275// labelActor->SetVisibility(showNodeLabels->isChecked());
276// velocityActor->SetVisibility(showVelocities->isChecked());
277// velocityScaleEdit->setEnabled(showVelocities->isChecked());
278// const double vScaling(std::atof(velocityScaleEdit->text() .toStdString().c_str()));
279// velocityGlyphs->SetScaleFactor(vScaling);
280
281// velocityActor->SetScale(vScaling);
282// aGlyphs->SetScaleFactor(2.0*this->tubeRadius*1.2);
283//
284// if(this->showVelocities)
285// {
286// velocityActor->VisibilityOn();
287// }
288// else
289// {
290// velocityActor->VisibilityOff();
291// }
292//
293// if(this->showNodeIDs)
294// {
295// labelActor->VisibilityOn();
296//
297// }
298// else
299// {
300// labelActor->VisibilityOff();
301// }
302//
303// velocityGlyphs->SetScaleFactor(this->velocityFactor);
304//
305//
306// if(this->showSingleNode)
307// {
308// // HERE WE SHOULD CHANGE THE NODE POSITION BASED ON NODE ID
309// // OTHERWISE THE SELECTED NODE WILL BE VISIBLE ONLY UPON LOADING A NEW FRAME
310// std::cout<<"RELOAD FRAME TO SHOW SELECTED NODE"<<std::endl;
311// singleNodeLabelActor->VisibilityOn();
312// }
313// else
314// {
315// singleNodeLabelActor->VisibilityOff();
316// }
317//
318// if(this->showA)
319// {
320// aActor->VisibilityOn();
321// }
322// else
323// {
324// aActor->VisibilityOff();
325// }
326
327 renderWindow->Render();
328 }
329
330 } // namespace oILAB
331#endif
static std::string magentaColor
static std::string defaultColor
BicrystalActor(vtkGenericOpenGLRenderWindow *const, vtkRenderer *const)
vtkGenericOpenGLRenderWindow *const renderWindow
QGridLayout * mainLayout
vtkSmartPointer< vtkPolyDataMapper > bMapper
vtkSmartPointer< vtkGlyph3D > aGlyphs
vtkRenderer *const renderer
void updateConfiguration(const std::shared_ptr< BiCrystal< 3 > > &bc)
vtkSmartPointer< vtkPolyDataMapper > aMapper
vtkSmartPointer< vtkActor > aActor
vtkSmartPointer< vtkPolyData > aPolyData
vtkSmartPointer< vtkPolyData > bPolyData
vtkSmartPointer< vtkGlyph3D > bGlyphs
vtkSmartPointer< vtkActor > bActor