Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
NetworkNodeActor.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_NetworkNodeActor_cpp_
10#define model_NetworkNodeActor_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 <IDreader.h>
38#include <MeshPlane.h>
39#include <NetworkNodeActor.h>
40#include <DislocationNetwork.h>
41#include <vtkRenderer.h>
42
43#include <DDqtControls.h>
44
45namespace model
46{
47
48
49 /**********************************************************************/
50 NetworkNodeActor::NetworkNodeActor(vtkGenericOpenGLRenderWindow* const renWin,vtkRenderer* const renderer) :
51 /* init */ renderWindow(renWin)
52 /* init */,mainLayout(new QGridLayout(this))
53 /* init */,showNodes(new QCheckBox(this))
54 /* init */,sliderNodeRadius(new QSlider(this))
55 /* init */,showNodeLabels(new QCheckBox(this))
56 /* init */,showSpecificNodeLabel(new QCheckBox(this))
57 /* init */,showSpecificNodeLabelEdit(new QLineEdit("0"))
58 /* init */,showVelocities(new QCheckBox(this))
59 /* init */,velocityScaleEdit(new QLineEdit("1"))
60 /* init */,nodePolyData(vtkSmartPointer<vtkPolyData>::New())
61 /* init */,nodeGlyphs(vtkSmartPointer<vtkGlyph3D>::New())
62 /* init */,nodeMapper(vtkSmartPointer<vtkPolyDataMapper>::New())
63 /* init */,nodeActor(vtkSmartPointer<vtkActor>::New())
64 /* init */,labelPolyData(vtkSmartPointer<vtkPolyData>::New())
65 /* init */,labelMapper(vtkSmartPointer<vtkLabeledDataMapper>::New())
66 /* init */,labelActor(vtkSmartPointer<vtkActor2D>::New())
67 /* init */,velocityPolyData(vtkSmartPointer<vtkPolyData>::New())
68 /* init */,velocityGlyphs(vtkSmartPointer<vtkGlyph3D>::New())
69 /* init */,velocityMapper(vtkSmartPointer<vtkPolyDataMapper>::New())
70 /* init */,velocityActor(vtkSmartPointer<vtkActor>::New())
71 /* init */,specificNodeLabelPolyData(vtkSmartPointer<vtkPolyData>::New())
72 /* init */,specificNodeLabelMapper(vtkSmartPointer<vtkLabeledDataMapper>::New())
73 /* init */,specificNodeLabelActor(vtkSmartPointer<vtkActor2D>::New())
74// /* init */,singleNodeID(0)
75 /* init */,nodeClr{{100,100,100},{0,255,255},{255,0,255},{1,1,1}}
76 {
77
78 const int initialRadius(12);
79
80 showNodes->setText("nodes");
81 showNodes->setChecked(false);
82 nodeActor->SetVisibility(false);
83 sliderNodeRadius->setEnabled(false);
84 sliderNodeRadius->setMinimum(0);
85 sliderNodeRadius->setMaximum(30);
86 sliderNodeRadius->setValue(initialRadius);
87 sliderNodeRadius->setOrientation(Qt::Horizontal);
88
89
90 showSpecificNodeLabel->setText("selected nodes");
91 showSpecificNodeLabel->setChecked(false);
92 specificNodeLabelActor->SetVisibility(false);
93
94 showNodeLabels->setText("node labels");
95 showNodeLabels->setChecked(false);
96 labelActor->SetVisibility(false);
97
98 showVelocities->setText("velocities");
99 showVelocities->setChecked(false);
100 velocityActor->SetVisibility(false);
101 velocityScaleEdit->setEnabled(false);
102
103 auto *nodesBox = controls::box("Nodes", this, "Show or hide network-node markers.", showNodes);
104 controls::field(controls::grid(nodesBox), 0, "Radius", sliderNodeRadius,
105 "Marker radius in simulation length units b. Changes apply immediately.");
106 auto *labelsBox = controls::box("Node labels", this, "Show IDs for all network nodes.", showNodeLabels);
107 auto *selectedBox = controls::box("Selected node labels", this, "Show IDs only for the specified nodes.",
108 showSpecificNodeLabel);
109 controls::field(controls::grid(selectedBox), 0, "Node IDs", showSpecificNodeLabelEdit,
110 "Enter space-separated node IDs. Press Enter to update the labels.");
111 auto *velocityBox =
112 controls::box("Velocities", this, "Show velocity arrows at network nodes.", showVelocities);
113 controls::field(controls::grid(velocityBox), 0, "Scale", velocityScaleEdit,
114 "Multiply each velocity vector by this display scale. Press Enter to apply.");
115 mainLayout->addWidget(nodesBox, 0, 0);
116 mainLayout->addWidget(labelsBox, 1, 0);
117 mainLayout->addWidget(selectedBox, 2, 0);
118 mainLayout->addWidget(velocityBox, 3, 0);
119 mainLayout->setRowStretch(4, 1);
120 this->setLayout(mainLayout);
121
122 connect(showNodes,SIGNAL(stateChanged(int)), this, SLOT(modify()));
123 connect(showNodeLabels,SIGNAL(stateChanged(int)), this, SLOT(modify()));
124 connect(showVelocities,SIGNAL(stateChanged(int)), this, SLOT(modify()));
125 connect(velocityScaleEdit,SIGNAL(returnPressed()), this, SLOT(modify()));
126 connect(showSpecificNodeLabel,SIGNAL(stateChanged(int)), this, SLOT(modify()));
127 connect(showSpecificNodeLabelEdit,SIGNAL(returnPressed()), this, SLOT(modify()));
128 connect(sliderNodeRadius,SIGNAL(valueChanged(int)), this, SLOT(modify()));
129
130
131 nodeGlyphs->SetInputData(nodePolyData);
132 nodeGlyphs->SetSourceConnection(vtkSmartPointer<vtkSphereSource>::New()->GetOutputPort());
133 nodeGlyphs->ScalingOn();
134 nodeGlyphs->SetScaleModeToScaleByVector();
135 nodeGlyphs->SetScaleFactor(initialRadius);
136 nodeGlyphs->SetColorModeToColorByScalar();
137 nodeGlyphs->Update();
138 nodeMapper->SetInputConnection(nodeGlyphs->GetOutputPort());
139 nodeActor->SetMapper(nodeMapper);
140
141 // Labels
142 labelMapper->SetInputData(labelPolyData);
143 labelMapper->SetLabelModeToLabelScalars();
144 labelMapper->SetLabelFormat("%1.0f");
145 labelMapper->GetLabelTextProperty()->SetFontSize(20);
146 labelActor->SetMapper(labelMapper);
147 labelActor->GetProperty()->SetColor(0.0, 0.0, 0.0); //(R,G,B)
148
149 // Velocities
150 velocityGlyphs->SetInputData(velocityPolyData);
151 velocityGlyphs->SetSourceConnection(vtkSmartPointer<vtkArrowSource>::New()->GetOutputPort());
152 velocityGlyphs->ScalingOn();
153 velocityGlyphs->SetScaleModeToScaleByVector();
154 velocityGlyphs->OrientOn();
155 velocityGlyphs->ClampingOff();
156 velocityGlyphs->SetVectorModeToUseVector();
157 velocityGlyphs->SetIndexModeToOff();
158 velocityMapper->SetInputConnection(velocityGlyphs->GetOutputPort());
159 velocityMapper->ScalarVisibilityOff();
160 velocityActor->SetMapper(velocityMapper);
161 velocityActor->GetProperty()->SetColor(1.0, 0.0, 1.0); //(R,G,B)
162
163 // Single node Label
164 specificNodeLabelMapper->SetInputData(specificNodeLabelPolyData);
165 specificNodeLabelMapper->SetLabelModeToLabelScalars();
166 specificNodeLabelMapper->SetLabelFormat("%1.0f");
167 specificNodeLabelMapper->GetLabelTextProperty()->SetFontSize(20);
168 specificNodeLabelActor->SetMapper(specificNodeLabelMapper);
169 specificNodeLabelActor->GetProperty()->SetColor(1.0, 0.0, 0.0); //(R,G,B)
170// specificNodeLabelActor->VisibilityOff();
171
172 renderer->AddActor(nodeActor);
173 renderer->AddActor(velocityActor);
174 renderer->AddActor(labelActor);
175 renderer->AddActor(specificNodeLabelActor);
176
177 }
178
179
180
181 /**********************************************************************/
183 {// https://stackoverflow.com/questions/6878263/remove-individual-points-from-vtkpoints
184 std::cout<<"Updating nodes..."<<std::flush;
185 const auto t0= std::chrono::system_clock::now();
186
187 vtkSmartPointer<vtkPoints> nodePoints(vtkSmartPointer<vtkPoints>::New());
188 vtkSmartPointer<vtkUnsignedCharArray> nodeColors(vtkSmartPointer<vtkUnsignedCharArray>::New());
189 nodeColors->SetNumberOfComponents(3);
190
191 vtkSmartPointer<vtkDoubleArray> nodeLabels(vtkSmartPointer<vtkDoubleArray>::New());
192 nodeLabels->SetNumberOfComponents(1);
193 nodeLabels->SetName("node IDs");
194
195 vtkSmartPointer<vtkPoints> specificNodePoints(vtkSmartPointer<vtkPoints>::New());
196 vtkSmartPointer<vtkDoubleArray> specificNodeLabels(vtkSmartPointer<vtkDoubleArray>::New());
197 specificNodeLabels->SetNumberOfComponents(1);
198
199
200 vtkSmartPointer<vtkDoubleArray> velocityVectors(vtkSmartPointer<vtkDoubleArray>::New());
201 velocityVectors->SetNumberOfComponents(3);
202 velocityVectors->SetName("nodeVelocity");
203
204 std::set<size_t> singleNodes;
205 if(showSpecificNodeLabel->isChecked())
206 {
207 try
208 {
209 std::stringstream ss(showSpecificNodeLabelEdit->text() .toStdString());
210 size_t val;
211 while(ss>>val)
212 {
213 singleNodes.insert(val);
214 }
215 }
216 catch(std::exception& e)
217 {
218 showSpecificNodeLabel->setText(QString::fromStdString(e.what()));
219 }
220
221 }
222
223// std::cout<<"Single nodes:"<<std::endl;
224// for(const auto& sn : singleNodes)
225// {
226// std::cout<<sn<<std::endl;
227// }
228
229 for(const auto& node : configIO.nodes())
230 {
231 if(singleNodes.find(node.sID)!=singleNodes.end())
232 {// current node is among selected
233 specificNodePoints->InsertNextPoint(node.P.data());
234 specificNodeLabels->InsertNextTuple1(node.sID);
235 }
236
237
238
239 nodePoints->InsertNextPoint(node.P.data());
240 nodeLabels->InsertNextTuple1(node.sID);
241 velocityVectors->InsertNextTuple(node.V.data()); // arrow vector
242 // Old outputs contain geometric-only labels. Prefer the current
243 // network classification, reconstructed with the active face pairs.
244 int location=node.meshLocation;
245 if(network)
246 {
247 const auto found=network->networkNodes().find(node.sID);
248 if(found!=network->networkNodes().end())
249 {
250 if(const auto liveNode=found->second.lock())
251 {
252 location=liveNode->meshLocation();
253 }
254 }
255 }
256 const int colorIndex=location>=0 && location<=2?location:3;
257 nodeColors->InsertNextTypedTuple(this->nodeClr[colorIndex]);
258
259// // Single node
260// if(node.sID==singleNodeID)
261// {
262// singleNodePoint->InsertNextPoint(node.P.data());
263// singleNodenodeLabels->InsertNextTuple1(node.sID);
264// }
265 }
266
267 nodePolyData->SetPoints(nodePoints);
268 nodePolyData->GetPointData()->SetScalars(nodeColors);
269 nodePolyData->Modified();
270
271 labelPolyData->SetPoints(nodePoints);
272 labelPolyData->GetPointData()->SetScalars(nodeLabels);
273 labelPolyData->Modified();
274
275 specificNodeLabelPolyData->SetPoints(specificNodePoints);
276 specificNodeLabelPolyData->GetPointData()->SetScalars(specificNodeLabels);
277 specificNodeLabelPolyData->Modified();
278
279 velocityPolyData->SetPoints(nodePoints);
280 velocityPolyData->GetPointData()->SetVectors(velocityVectors);
281 velocityPolyData->Modified();
282 std::cout<<magentaColor<<" ["<<(std::chrono::duration<double>(std::chrono::system_clock::now()-t0)).count()<<" sec]"<<defaultColor<<std::endl;
283 }
284
285 /**********************************************************************/
286 void NetworkNodeActor::modify()
287 {
288
289 nodeGlyphs->SetScaleFactor(sliderNodeRadius->value());
290 sliderNodeRadius->setEnabled(showNodes->isChecked());
291 nodeActor->SetVisibility(showNodes->isChecked());
292 labelActor->SetVisibility(showNodeLabels->isChecked());
293 specificNodeLabelActor->SetVisibility(showSpecificNodeLabel->isChecked());
294 velocityActor->SetVisibility(showVelocities->isChecked());
295 velocityScaleEdit->setEnabled(showVelocities->isChecked());
296 const double vScaling(std::atof(velocityScaleEdit->text() .toStdString().c_str()));
297 velocityGlyphs->SetScaleFactor(vScaling);
298
299// velocityActor->SetScale(vScaling);
300// nodeGlyphs->SetScaleFactor(2.0*this->tubeRadius*1.2);
301//
302// if(this->showVelocities)
303// {
304// velocityActor->VisibilityOn();
305// }
306// else
307// {
308// velocityActor->VisibilityOff();
309// }
310//
311// if(this->showNodeIDs)
312// {
313// labelActor->VisibilityOn();
314//
315// }
316// else
317// {
318// labelActor->VisibilityOff();
319// }
320//
321// velocityGlyphs->SetScaleFactor(this->velocityFactor);
322//
323//
324// if(this->showSingleNode)
325// {
326// // HERE WE SHOULD CHANGE THE NODE POSITION BASED ON NODE ID
327// // OTHERWISE THE SELECTED NODE WILL BE VISIBLE ONLY UPON LOADING A NEW FRAME
328// std::cout<<"RELOAD FRAME TO SHOW SELECTED NODE"<<std::endl;
329// singleNodeLabelActor->VisibilityOn();
330// }
331// else
332// {
333// singleNodeLabelActor->VisibilityOff();
334// }
335//
336// if(this->showNodes)
337// {
338// nodeActor->VisibilityOn();
339// }
340// else
341// {
342// nodeActor->VisibilityOff();
343// }
344
345 renderWindow->Render();
346 }
347
348
349} // namespace model
350#endif
const NetworkNodeContainerType & networkNodes() const
size_t InstanceCount< Derived >::count
static std::string magentaColor
static std::string defaultColor
vtkSmartPointer< vtkPolyData > labelPolyData
vtkSmartPointer< vtkPolyData > specificNodeLabelPolyData
vtkSmartPointer< vtkPolyData > nodePolyData
vtkSmartPointer< vtkPolyData > velocityPolyData
vtkSmartPointer< vtkActor2D > labelActor
vtkSmartPointer< vtkActor2D > specificNodeLabelActor
void updateConfiguration(const DefectiveCrystalConfigIO< 3 > &configIO, const DislocationNetwork< 3 > *network=nullptr)
vtkSmartPointer< vtkGlyph3D > velocityGlyphs
vtkSmartPointer< vtkPolyDataMapper > nodeMapper
vtkSmartPointer< vtkGlyph3D > nodeGlyphs
vtkSmartPointer< vtkActor > nodeActor
vtkSmartPointer< vtkLabeledDataMapper > labelMapper
unsigned char nodeClr[4][3]
NetworkNodeActor(vtkGenericOpenGLRenderWindow *const, vtkRenderer *const)
vtkSmartPointer< vtkPolyDataMapper > velocityMapper
vtkSmartPointer< vtkActor > velocityActor
vtkSmartPointer< vtkLabeledDataMapper > specificNodeLabelMapper
REAL t0
newsubseg ss[0]
int e