1 #include "createspecialmapping.h"
3 #include <vtkCharArray.h>
4 #include "vtkCellArray.h"
5 #include "vtkCellData.h"
6 #include "vtkCellLocator.h"
7 #include "vtkFloatArray.h"
9 #include "vtkInformation.h"
10 #include "vtkInformationVector.h"
11 #include "vtkObjectFactory.h"
12 #include "vtkPointData.h"
13 #include "vtkPolyData.h"
14 #include "vtkPolygon.h"
15 #include "vtkStreamingDemandDrivenPipeline.h"
16 #include "vtkTriangleFilter.h"
18 #include <QTextStream>
20 #include "smoothingutilities.h"
22 #include "swaptriangles.h"
23 #include "laplacesmoother.h"
28 // Special structure for marking vertices
29 typedef struct _vtkMeshVertex
32 vtkIdList
*edges
; // connected edges (list of connected point ids)
33 } vtkMeshVertex
, *vtkMeshVertexPtr
;
35 CreateSpecialMapping::CreateSpecialMapping()
39 int CreateSpecialMapping::Process()
43 for(i_iter
=0;i_iter
<NumberOfIterations
;i_iter
++)//TODO:Optimize this loop
45 cout
<<"===ITERATION NB "<<i_iter
<<"/"<<NumberOfIterations
<<"==="<<endl
;
47 m_total_N_newpoints
=0;
50 getAllSurfaceCells(m_AllCells
,m_grid
);
51 getSurfaceCells(m_bcs
, m_SelectedCells
, m_grid
);
52 cout
<<"m_AllCells.size()="<<m_AllCells
.size()<<endl
;
54 EG_VTKDCC(vtkIntArray
, cell_code
, m_grid
, "cell_code");
55 EG_VTKDCN(vtkDoubleArray
, node_meshdensity
, m_grid
, "node_meshdensity");
57 m_SelectedNodes
.clear();
58 getSurfaceNodes(m_bcs
,m_SelectedNodes
,m_grid
);
59 getNodesFromCells(m_AllCells
, nodes
, m_grid
);
63 cout
<<"m_AllCells.size()="<<m_AllCells
.size()<<endl
;
66 EG_VTKDCN(vtkCharArray
, node_type
, m_grid
, "node_type");
68 //Phase A : Calculate current mesh density
69 cout
<<"===Phase A==="<<endl
;
71 foreach(vtkIdType node
,m_SelectedNodes
)
73 VertexMeshDensity nodeVMD
= getVMD(node
,node_type
->GetValue(node
));
74 int idx
=VMDvector
.indexOf(nodeVMD
);
75 if(DebugLevel
>3) cout
<<"idx="<<idx
<<endl
;
76 if(idx
!=-1)//specified
78 node_meshdensity
->SetValue(node
, VMDvector
[idx
].density
);
82 double L
=CurrentVertexAvgDist(node
,n2n
,m_grid
);
84 node_meshdensity
->SetValue(node
, D
);
88 //Phase B : define desired mesh density
89 cout
<<"===Phase B==="<<endl
;
90 double diff
=Convergence_meshdensity
+1;
91 if(DebugLevel
>3) cout
<<"before loop: diff="<<diff
<<endl
;
96 if(DebugLevel
>2) cout
<<"--->diff="<<diff
<<endl
;
98 foreach(vtkIdType node
,m_SelectedNodes
)
100 if(DebugLevel
>2) cout
<<"======>"<<endl
;
101 VertexMeshDensity nodeVMD
= getVMD(node
,node_type
->GetValue(node
));
102 int idx
=VMDvector
.indexOf(nodeVMD
);
103 if(DebugLevel
>2) cout
<<"------>idx="<<idx
<<endl
;
104 if(idx
!=-1)//specified
106 node_meshdensity
->SetValue(node
, VMDvector
[idx
].density
);
110 double D
=DesiredMeshDensity(node
,n2n
,m_grid
);
113 cout
<<"------>FIRST:"<<endl
;
114 cout
<<"------>D="<<D
<<endl
;
115 cout
<<"------>node_meshdensity->GetValue("<<node
<<")="<<node_meshdensity
->GetValue(node
)<<endl
;
116 cout
<<"------>D-node_meshdensity->GetValue("<<node
<<")="<<D
-node_meshdensity
->GetValue(node
)<<endl
;
117 cout
<<"------>diff=abs(D-node_meshdensity->GetValue("<<node
<<"))="<<abs(D
-node_meshdensity
->GetValue(node
))<<endl
;
119 diff
=abs(D
-node_meshdensity
->GetValue(node
));
124 cout
<<"------>NOT FIRST:"<<endl
;
125 cout
<<"------>D="<<D
<<endl
;
126 cout
<<"------>node_meshdensity->GetValue("<<node
<<")="<<node_meshdensity
->GetValue(node
)<<endl
;
127 cout
<<"------>D-node_meshdensity->GetValue("<<node
<<")="<<D
-node_meshdensity
->GetValue(node
)<<endl
;
128 cout
<<"------>diff=abs(D-node_meshdensity->GetValue("<<node
<<"))="<<abs(D
-node_meshdensity
->GetValue(node
))<<endl
;
129 cout
<<"------>diff="<<diff
<<endl
;
130 cout
<<"------>max(abs(D-node_meshdensity->GetValue("<<node
<<")),diff)="<<max(abs(D
-node_meshdensity
->GetValue(node
)),diff
)<<endl
;
132 diff
=max(abs(D
-node_meshdensity
->GetValue(node
)),diff
);
134 node_meshdensity
->SetValue(node
, D
);
136 if(DebugLevel
>2) cout
<<"======>"<<endl
;
139 } while(diff
>Convergence_meshdensity
&& !first
&& iter
<maxiter
);// if first=true, it means no new mesh density has been defined (all densities specified)
140 cout
<<"iter="<<iter
<<endl
;
141 if(iter
>=maxiter
) cout
<<"WARNING: Desired convergence factor has not been reached!"<<endl
;
143 //Phase C: Prepare edge_map
144 cout
<<"===Phase C==="<<endl
;
147 foreach(vtkIdType node1
,m_SelectedNodes
)
149 // cout<<"node1="<<node1<<endl;
150 foreach(vtkIdType node2
,n2n
[node1
])
152 if(edge_map
[OrderedPair(node1
,node2
)]==0) { //this edge hasn't been numbered yet
153 edge_map
[OrderedPair(node1
,node2
)]=edgeId
;edgeId
++;
157 cout
<<"edge_map.size()="<<edge_map
.size()<<endl
;
159 //Phase D: edit points
160 cout
<<"===Phase D==="<<endl
;
170 /* if(insert_FP) insert_FP_all();
171 if(insert_EP) insert_EP_all();
172 if(remove_FP) remove_FP_all();
173 if(remove_EP) remove_EP_all();*/
176 if(insert_FP
) insert_FP_all();
177 if(insert_EP
) insert_EP_all();
178 if(remove_FP
) remove_FP_all_2();
179 if(remove_EP
) remove_EP_all_2();
181 //Phase E : Delaunay swap
182 QSet
<int> bcs_complement
=complementary_bcs(m_bcs
,m_grid
,cells
);
183 cout
<<"m_bcs="<<m_bcs
<<endl
;
184 cout
<<"bcs_complement="<<bcs_complement
<<endl
;
187 swap
.setGrid(m_grid
);
188 swap
.setBoundaryCodes(bcs_complement
);
191 //Phase F : translate points to smooth grid
195 //laplacian smoothing with projection
196 //Roland smoothing with projection
198 //laplacian smoothing with projection
200 Lap
.SetInput(m_bcs
,m_grid
);
201 Lap
.SetNumberOfIterations(N_SmoothIterations
);
204 cout
<<"===Summary==="<<endl
;
205 cout
<<"N_inserted_FP="<<N_inserted_FP
<<endl
;
206 cout
<<"N_inserted_EP="<<N_inserted_EP
<<endl
;
207 cout
<<"N_removed_FP="<<N_removed_FP
<<endl
;
208 cout
<<"N_removed_EP="<<N_removed_EP
<<endl
;
210 cout
<<"N_points="<<N_points
<<endl
;
211 cout
<<"N_cells="<<N_cells
<<endl
;
212 cout
<<"m_total_N_newpoints="<<m_total_N_newpoints
<<endl
;
213 cout
<<"m_total_N_newcells="<<m_total_N_newcells
<<endl
;
214 cout
<<"============"<<endl
;
216 if(m_total_N_newpoints
==0 && m_total_N_newcells
==0) break;
220 cout
<<"i_iter/NumberOfIterations="<<i_iter
<<"/"<<NumberOfIterations
<<endl
;
226 VertexMeshDensity
CreateSpecialMapping::getVMD(vtkIdType node
, char VertexType
)
228 VertexMeshDensity VMD
;
231 VMD
.CurrentNode
=node
;
232 EG_VTKDCC(vtkIntArray
, cell_code
, m_grid
, "cell_code");
233 /* createNodeMapping(nodes, _nodes, m_grid);
234 createNodeToCell(m_AllCells, nodes, _nodes, n2c, m_grid);*/
237 foreach(vtkIdType C
, n2c
[node
])
239 bc
.insert(cell_code
->GetValue(C
));
241 VMD
.BClist
.resize(bc
.size());
242 qCopy(bc
.begin(),bc
.end(),VMD
.BClist
.begin());
243 qSort(VMD
.BClist
.begin(),VMD
.BClist
.end());
247 int CreateSpecialMapping::insert_FP_counter()
249 cout
<<"===insert_FP_counter() START==="<<endl
;
250 foreach(vtkIdType id_cell
, m_SelectedCells
)
252 if( !marked_cells
[id_cell
] && insert_fieldpoint(id_cell
) )
254 cout
<<"inserting a field point "<<id_cell
<<endl
;
256 marked_cells
[id_cell
]=true;
261 cout
<<"===insert_FP_counter() END==="<<endl
;
265 int CreateSpecialMapping::insert_EP_counter()
267 cout
<<"===insert_EP_counter() START==="<<endl
;
268 StencilVector
.clear();
269 QMapIterator
< pair
<vtkIdType
,vtkIdType
>, vtkIdType
> edge_map_iter(edge_map
);
270 //rewind the iterator
271 edge_map_iter
.toFront ();
273 while (edge_map_iter
.hasNext()) {
274 edge_map_iter
.next();
275 vtkIdType node1
=edge_map_iter
.key().first
;
276 vtkIdType node2
=edge_map_iter
.key().second
;
277 if(DebugLevel
>10) cout
<< "--->(" << node1
<< "," << node2
<< ")" << ": " << edge_map_iter
.value() << endl
;
278 QSet
<int> stencil_cells_set
;
279 QVector
<int> stencil_cells_vector
;
280 stencil_cells_set
=n2c
[node1
];
281 stencil_cells_set
.intersect(n2c
[node2
]);
282 if(DebugLevel
>10) cout
<<"stencil_cells_set="<<stencil_cells_set
<<endl
;
284 stencil_cells_vector
.resize(stencil_cells_set
.size());
285 qCopy(stencil_cells_set
.begin(),stencil_cells_set
.end(),stencil_cells_vector
.begin());
286 if(DebugLevel
>10) cout
<<"stencil_cells_vector="<<stencil_cells_vector
<<endl
;
288 vtkIdType id_cell
=stencil_cells_vector
[0];
289 int SideToSplit
= getSide(id_cell
,m_grid
,node1
,node2
);
290 if(DebugLevel
>10) cout
<<"SideToSplit="<<SideToSplit
<<endl
;
291 if(DebugLevel
>10) cout
<<"c2c[id_cell][SideToSplit]="<<c2c
[id_cell
][SideToSplit
]<<endl
;
292 if(DebugLevel
>10) for(int i
=0;i
<3;i
++) cout
<<"c2c[id_cell]["<<i
<<"]="<<c2c
[id_cell
][i
]<<endl
;
293 stencil_t S
=getStencil(id_cell
,SideToSplit
);
295 bool stencil_marked
=false;
296 foreach(vtkIdType C
,stencil_cells_vector
)
298 if(marked_cells
[C
]) stencil_marked
=true;
300 if(DebugLevel
>10) cout
<<"stencil_marked="<<stencil_marked
<<endl
;
301 if(DebugLevel
>10) cout
<<"insert_edgepoint(node1,node2)="<<insert_edgepoint(node1
,node2
)<<endl
;
303 if( !stencil_marked
&& insert_edgepoint(node1
,node2
) )
305 if(DebugLevel
>1) cout
<<"inserting an edge point "<< "(" << node1
<< "," << node2
<< ")" << ": " << edge_map_iter
.value() << endl
;
307 foreach(vtkIdType C
,stencil_cells_vector
) marked_cells
[C
]=true;
308 StencilVector
.push_back(S
);
310 if(stencil_cells_vector
.size()==2)//2 cells around the edge
315 else//1 cell around the edge
321 if(DebugLevel
>10) cout
<<"--->end of edge processing"<<endl
;
323 cout
<<"===insert_EP_counter() END==="<<endl
;
327 int CreateSpecialMapping::remove_FP_counter()
329 cout
<<"===remove_FP_counter() START==="<<endl
;
331 EG_VTKDCN(vtkCharArray
, node_type
, m_grid
, "node_type");
332 foreach(vtkIdType node
,m_SelectedNodes
)
334 if(node_type
->GetValue(node
)==VTK_SIMPLE_VERTEX
)
337 foreach(vtkIdType C
,n2c
[node
])
339 if(marked_cells
[C
]) marked
=true;
342 QSet
<vtkIdType
> DeadCells
;
343 QSet
<vtkIdType
> MutatedCells
;
344 QSet
<vtkIdType
> MutilatedCells
;
345 if( !marked
&& remove_fieldpoint(node
) && FindSnapPoint(m_grid
,node
,DeadCells
,MutatedCells
,MutilatedCells
)!=-1)
347 if(DebugLevel
>1) cout
<<"removing field point "<<node
<<endl
;
350 foreach(vtkIdType C
,n2c
[node
]) marked_cells
[C
]=true;
356 cout
<<"===remove_FP_counter() END==="<<endl
;
360 int CreateSpecialMapping::remove_EP_counter()
362 cout
<<"===remove_EP_counter() START==="<<endl
;
364 EG_VTKDCN(vtkCharArray
, node_type
, m_grid
, "node_type");
365 foreach(vtkIdType node
,m_SelectedNodes
)
367 if(node_type
->GetValue(node
)==VTK_BOUNDARY_EDGE_VERTEX
)
370 foreach(vtkIdType C
,n2c
[node
])
372 if(marked_cells
[C
]) marked
=true;
374 QSet
<vtkIdType
> DeadCells
;
375 QSet
<vtkIdType
> MutatedCells
;
376 QSet
<vtkIdType
> MutilatedCells
;
377 if( !marked
&& remove_edgepoint(node
) && FindSnapPoint(m_grid
,node
,DeadCells
,MutatedCells
,MutilatedCells
)!=-1)
379 cout
<<"removing edge point "<<node
<<endl
;
382 foreach(vtkIdType C
,n2c
[node
]) marked_cells
[C
]=true;
383 if(n2n
[node
].size()==4)//4 cells around the edge
388 else//2 cells around the edge
396 cout
<<"===remove_EP_counter() END==="<<endl
;
400 int CreateSpecialMapping::insert_FP_actor(vtkUnstructuredGrid
* grid_tmp
)
402 cout
<<"===insert_FP_actor START==="<<endl
;
404 EG_VTKDCC(vtkIntArray
, cell_code_tmp
, grid_tmp
, "cell_code");
405 foreach(vtkIdType id_cell
, m_SelectedCells
)
407 /* if(marked_cells[id_cell]) cout<<"--->marked_cells["<<id_cell<<"]=TRUE"<<endl;
408 else cout<<"--->marked_cells["<<id_cell<<"]=FALSE"<<endl;*/
410 if( !marked_cells
[id_cell
] && insert_fieldpoint(id_cell
) )
412 cout
<<"inserting a field point "<<id_cell
<<endl
;
413 vtkIdType newBC
=cell_code_tmp
->GetValue(id_cell
);
414 cout
<<"id_cell="<<id_cell
<<" newBC="<<newBC
<<endl
;
416 vtkIdType N_pts
, *pts
;
417 m_grid
->GetCellPoints(id_cell
, N_pts
, pts
);
420 int N_neighbours
=N_pts
;
421 cout
<<"N_neighbours="<<N_neighbours
<<endl
;
423 vtkIdType pts_triangle
[4][3];
424 for(int i
=0;i
<N_neighbours
;i
++)
426 m_grid
->GetPoints()->GetPoint(pts
[i
], corner
[i
].data());
429 C
=(1/(double)N_neighbours
)*C
;
430 addPoint(grid_tmp
,m_newNodeId
,C
.data());
431 vtkIdType intmidpoint
=m_newNodeId
;
434 for(int i
=0;i
<N_neighbours
;i
++)
436 pts_triangle
[i
][0]=pts
[i
];
437 pts_triangle
[i
][1]=pts
[(i
+1)%N_neighbours
];
438 pts_triangle
[i
][2]=intmidpoint
;
441 grid_tmp
->ReplaceCell(id_cell
, 3, pts_triangle
[0]);
442 cell_code_tmp
->SetValue(id_cell
, newBC
);
446 vtkIdType newCellId
= grid_tmp
->InsertNextCell(VTK_TRIANGLE
,3,pts_triangle
[i
]);
447 cell_code_tmp
->SetValue(newCellId
, newBC
);
453 cout
<<"===insert_FP_actor END==="<<endl
;
457 int CreateSpecialMapping::insert_EP_actor(vtkUnstructuredGrid
* grid_tmp
)
459 cout
<<"===insert_EP_actor START==="<<endl
;
461 EG_VTKDCC(vtkIntArray
, cell_code_tmp
, grid_tmp
, "cell_code");
462 foreach(stencil_t S
,StencilVector
)
464 if(DebugLevel
>10) cout
<<"S="<<S
<<endl
;
466 grid_tmp
->GetPoint(S
.p
[1],A
.data());
467 grid_tmp
->GetPoint(S
.p
[3],B
.data());
469 addPoint(grid_tmp
,m_newNodeId
,M
.data());
471 vtkIdType pts_triangle
[4][3];
473 if(S
.valid
){//there is a neighbour cell
474 if(DebugLevel
>10) cout
<<"marked_cells["<<S
.id_cell1
<<"]=true;"<<endl
;
475 if(DebugLevel
>10) cout
<<"marked_cells["<<S
.id_cell2
<<"]=true;"<<endl
;
476 marked_cells
[S
.id_cell1
]=true;
477 marked_cells
[S
.id_cell2
]=true;
481 pts_triangle
[i
][0]=S
.p
[i
];
482 pts_triangle
[i
][1]=S
.p
[(i
+1)%4];
483 pts_triangle
[i
][2]=m_newNodeId
;
486 int bc1
=cell_code_tmp
->GetValue(S
.id_cell1
);
487 int bc2
=cell_code_tmp
->GetValue(S
.id_cell2
);
489 grid_tmp
->ReplaceCell(S
.id_cell1
, 3, pts_triangle
[0]);
490 cell_code_tmp
->SetValue(S
.id_cell1
, bc1
);
492 grid_tmp
->ReplaceCell(S
.id_cell2
, 3, pts_triangle
[1]);
493 cell_code_tmp
->SetValue(S
.id_cell2
, bc2
);
496 newCellId
= grid_tmp
->InsertNextCell(VTK_TRIANGLE
,3,pts_triangle
[2]);
497 cell_code_tmp
->SetValue(newCellId
, bc2
);
498 newCellId
= grid_tmp
->InsertNextCell(VTK_TRIANGLE
,3,pts_triangle
[3]);
499 cell_code_tmp
->SetValue(newCellId
, bc1
);
501 else{//there is no neighbour cell
502 if(DebugLevel
>10) cout
<<"marked_cells["<<S
.id_cell1
<<"]=true;"<<endl
;
503 marked_cells
[S
.id_cell1
]=true;
505 pts_triangle
[0][0]=S
.p
[0];
506 pts_triangle
[0][1]=S
.p
[1];
507 pts_triangle
[0][2]=m_newNodeId
;
508 pts_triangle
[3][0]=S
.p
[3];
509 pts_triangle
[3][1]=S
.p
[0];
510 pts_triangle
[3][2]=m_newNodeId
;
512 int bc1
=cell_code_tmp
->GetValue(S
.id_cell1
);
514 grid_tmp
->ReplaceCell(S
.id_cell1
, 3, pts_triangle
[0]);
515 cell_code_tmp
->SetValue(S
.id_cell1
, bc1
);
518 newCellId
= grid_tmp
->InsertNextCell(VTK_TRIANGLE
,3,pts_triangle
[3]);
519 cell_code_tmp
->SetValue(newCellId
, bc1
);
524 cout
<<"===insert_EP_actor END==="<<endl
;
528 int CreateSpecialMapping::remove_FP_actor(vtkUnstructuredGrid
* grid_tmp
)
530 cout
<<"===remove_FP_actor START==="<<endl
;
532 foreach(vtkIdType node
,m_SelectedNodes
)
539 foreach(vtkIdType C
,n2c
[node
])
541 if(marked_cells
[C
]) marked
=true;
543 if( !marked
&& remove_fieldpoint(node
) )
545 if(DebugLevel
>1) cout
<<"removing field point "<<node
<<endl
;
546 foreach(vtkIdType C
,n2c
[node
]) marked_cells
[C
]=true;
547 //TODO: Special copy function, leaving out nodes to remove
550 cout
<<"===remove_FP_actor END==="<<endl
;
554 int CreateSpecialMapping::remove_EP_actor(vtkUnstructuredGrid
* grid_tmp
)
556 cout
<<"===remove_EP_actor START==="<<endl
;
558 foreach(vtkIdType node
,m_SelectedNodes
)
561 foreach(vtkIdType C
,n2c
[node
])
563 if(marked_cells
[C
]) marked
=true;
565 if( !marked
&& remove_edgepoint(node
) )
567 cout
<<"removing edge point "<<node
<<endl
;
568 foreach(vtkIdType C
,n2c
[node
]) marked_cells
[C
]=true;
569 if(n2n
[node
].size()==4)//4 cells around the edge
573 else//2 cells around the edge
579 cout
<<"===remove_EP_actor END==="<<endl
;
583 int CreateSpecialMapping::insert_FP_all()
585 cout
<<"===insert_FP_all START==="<<endl
;
587 getAllSurfaceCells(m_AllCells
,m_grid
);
588 getSurfaceCells(m_bcs
, m_SelectedCells
, m_grid
);
589 EG_VTKDCC(vtkIntArray
, cell_code
, m_grid
, "cell_code");
590 EG_VTKDCN(vtkDoubleArray
, node_meshdensity
, m_grid
, "node_meshdensity");
591 m_SelectedNodes
.clear();
592 getSurfaceNodes(m_bcs
,m_SelectedNodes
,m_grid
);
593 getNodesFromCells(m_AllCells
, nodes
, m_grid
);
595 setCells(m_AllCells
);
596 cout
<<"m_AllCells.size()="<<m_AllCells
.size()<<endl
;
600 N_points
=m_grid
->GetNumberOfPoints();
601 N_cells
=m_grid
->GetNumberOfCells();
605 marked_cells
.clear();
606 marked_nodes
.clear();
610 //unmark cells (TODO: optimize)
611 marked_cells
.clear();
613 N_points
=m_grid
->GetNumberOfPoints();
614 N_cells
=m_grid
->GetNumberOfCells();
615 cout
<<"N_points="<<N_points
<<endl
;
616 cout
<<"N_cells="<<N_cells
<<endl
;
617 cout
<<"N_newpoints="<<N_newpoints
<<endl
;
618 cout
<<"N_newcells="<<N_newcells
<<endl
;
619 EG_VTKSP(vtkUnstructuredGrid
,grid_tmp
);
620 allocateGrid(grid_tmp
,N_cells
+N_newcells
,N_points
+N_newpoints
);
621 m_total_N_newpoints
+=N_newpoints
; m_total_N_newcells
+=N_newcells
;
623 makeCopyNoAlloc(m_grid
, grid_tmp
);
624 //initialize new node counter
625 m_newNodeId
=N_points
;
627 insert_FP_actor(grid_tmp
);
629 makeCopy(grid_tmp
,m_grid
);
630 cout
<<"===insert_FP_all END==="<<endl
;
634 int CreateSpecialMapping::insert_EP_all()
636 cout
<<"===insert_EP_all START==="<<endl
;
638 getAllSurfaceCells(m_AllCells
,m_grid
);
639 getSurfaceCells(m_bcs
, m_SelectedCells
, m_grid
);
640 EG_VTKDCC(vtkIntArray
, cell_code
, m_grid
, "cell_code");
641 EG_VTKDCN(vtkDoubleArray
, node_meshdensity
, m_grid
, "node_meshdensity");
642 m_SelectedNodes
.clear();
643 getSurfaceNodes(m_bcs
,m_SelectedNodes
,m_grid
);
644 getNodesFromCells(m_AllCells
, nodes
, m_grid
);
646 setCells(m_AllCells
);
647 cout
<<"m_AllCells.size()="<<m_AllCells
.size()<<endl
;
651 N_points
=m_grid
->GetNumberOfPoints();
652 N_cells
=m_grid
->GetNumberOfCells();
656 marked_cells
.clear();
657 marked_nodes
.clear();
661 //unmark cells (TODO: optimize)
662 marked_cells
.clear();
664 N_points
=m_grid
->GetNumberOfPoints();
665 N_cells
=m_grid
->GetNumberOfCells();
666 cout
<<"N_points="<<N_points
<<endl
;
667 cout
<<"N_cells="<<N_cells
<<endl
;
668 cout
<<"N_newpoints="<<N_newpoints
<<endl
;
669 cout
<<"N_newcells="<<N_newcells
<<endl
;
670 EG_VTKSP(vtkUnstructuredGrid
,grid_tmp
);
671 allocateGrid(grid_tmp
,N_cells
+N_newcells
,N_points
+N_newpoints
);
672 m_total_N_newpoints
+=N_newpoints
; m_total_N_newcells
+=N_newcells
;
674 makeCopyNoAlloc(m_grid
, grid_tmp
);
675 //initialize new node counter
676 m_newNodeId
=N_points
;
678 insert_EP_actor(grid_tmp
);
680 makeCopy(grid_tmp
,m_grid
);
682 cout
<<"===insert_EP_all END==="<<endl
;
686 int CreateSpecialMapping::remove_FP_all()
688 cout
<<"===remove_FP_all START==="<<endl
;
690 getAllSurfaceCells(m_AllCells
,m_grid
);
691 getSurfaceCells(m_bcs
, m_SelectedCells
, m_grid
);
692 EG_VTKDCC(vtkIntArray
, cell_code
, m_grid
, "cell_code");
693 EG_VTKDCN(vtkDoubleArray
, node_meshdensity
, m_grid
, "node_meshdensity");
694 m_SelectedNodes
.clear();
695 getSurfaceNodes(m_bcs
,m_SelectedNodes
,m_grid
);
696 getNodesFromCells(m_AllCells
, nodes
, m_grid
);
698 setCells(m_AllCells
);
699 cout
<<"m_AllCells.size()="<<m_AllCells
.size()<<endl
;
703 N_points
=m_grid
->GetNumberOfPoints();
704 N_cells
=m_grid
->GetNumberOfCells();
708 hitlist
.resize(N_points
);
709 offset
.resize(N_points
);
711 marked_cells
.clear();
712 marked_nodes
.clear();
716 //unmark cells (TODO: optimize)
717 marked_cells
.clear();
719 N_points
=m_grid
->GetNumberOfPoints();
720 N_cells
=m_grid
->GetNumberOfCells();
721 cout
<<"N_points="<<N_points
<<endl
;
722 cout
<<"N_cells="<<N_cells
<<endl
;
723 cout
<<"N_newpoints="<<N_newpoints
<<endl
;
724 cout
<<"N_newcells="<<N_newcells
<<endl
;
725 EG_VTKSP(vtkUnstructuredGrid
,grid_tmp
);
726 allocateGrid(grid_tmp
,N_cells
+N_newcells
,N_points
+N_newpoints
);
727 m_total_N_newpoints
+=N_newpoints
; m_total_N_newcells
+=N_newcells
;
729 makeCopyNoAlloc(m_grid
, grid_tmp
);
730 //initialize new node counter
731 m_newNodeId
=N_points
;
733 remove_FP_actor(grid_tmp
);
735 makeCopy(grid_tmp
,m_grid
);
737 cout
<<"===remove_FP_all END==="<<endl
;
741 int CreateSpecialMapping::remove_EP_all()
743 cout
<<"===remove_EP_all START==="<<endl
;
745 getAllSurfaceCells(m_AllCells
,m_grid
);
746 getSurfaceCells(m_bcs
, m_SelectedCells
, m_grid
);
747 EG_VTKDCC(vtkIntArray
, cell_code
, m_grid
, "cell_code");
748 EG_VTKDCN(vtkDoubleArray
, node_meshdensity
, m_grid
, "node_meshdensity");
749 m_SelectedNodes
.clear();
750 getSurfaceNodes(m_bcs
,m_SelectedNodes
,m_grid
);
751 getNodesFromCells(m_AllCells
, nodes
, m_grid
);
753 setCells(m_AllCells
);
754 cout
<<"m_AllCells.size()="<<m_AllCells
.size()<<endl
;
758 N_points
=m_grid
->GetNumberOfPoints();
759 N_cells
=m_grid
->GetNumberOfCells();
763 hitlist
.resize(N_points
);
764 offset
.resize(N_points
);
766 marked_cells
.clear();
767 marked_nodes
.clear();
771 //unmark cells (TODO: optimize)
772 marked_cells
.clear();
774 N_points
=m_grid
->GetNumberOfPoints();
775 N_cells
=m_grid
->GetNumberOfCells();
776 cout
<<"N_points="<<N_points
<<endl
;
777 cout
<<"N_cells="<<N_cells
<<endl
;
778 cout
<<"N_newpoints="<<N_newpoints
<<endl
;
779 cout
<<"N_newcells="<<N_newcells
<<endl
;
780 EG_VTKSP(vtkUnstructuredGrid
,grid_tmp
);
781 allocateGrid(grid_tmp
,N_cells
+N_newcells
,N_points
+N_newpoints
);
782 m_total_N_newpoints
+=N_newpoints
; m_total_N_newcells
+=N_newcells
;
784 makeCopyNoAlloc(m_grid
, grid_tmp
);
785 //initialize new node counter
786 m_newNodeId
=N_points
;
788 remove_EP_actor(grid_tmp
);
790 makeCopy(grid_tmp
,m_grid
);
792 cout
<<"===remove_EP_all END==="<<endl
;
796 int CreateSpecialMapping::FullEdit()
798 cout
<<"===FullEdit START==="<<endl
;
805 N_points
=m_grid
->GetNumberOfPoints();
806 N_cells
=m_grid
->GetNumberOfCells();
810 hitlist
.resize(N_points
);
811 offset
.resize(N_points
);
813 marked_cells
.clear();
814 marked_nodes
.clear();
816 if(insert_FP
) insert_FP_counter();
817 if(insert_EP
) insert_EP_counter();
818 if(remove_FP
) remove_FP_counter();
819 if(remove_EP
) remove_EP_counter();
821 cout
<<"================="<<endl
;
822 cout
<<"hitlist="<<hitlist
<<endl
;
823 cout
<<"================="<<endl
;
825 //unmark cells (TODO: optimize)
826 marked_cells
.clear();
828 N_points
=m_grid
->GetNumberOfPoints();
829 N_cells
=m_grid
->GetNumberOfCells();
830 cout
<<"N_points="<<N_points
<<endl
;
831 cout
<<"N_cells="<<N_cells
<<endl
;
832 cout
<<"N_newpoints="<<N_newpoints
<<endl
;
833 cout
<<"N_newcells="<<N_newcells
<<endl
;
834 EG_VTKSP(vtkUnstructuredGrid
,grid_tmp
);
835 allocateGrid(grid_tmp
,N_cells
+N_newcells
,N_points
+N_newpoints
);
836 m_total_N_newpoints
+=N_newpoints
; m_total_N_newcells
+=N_newcells
;
838 makeCopyNoAlloc(m_grid
, grid_tmp
);//TODO: This will not work if the size of the grid is reduced!
839 //initialize new node counter
840 m_newNodeId
=N_points
;
842 if(insert_FP
) insert_FP_actor(grid_tmp
);
843 if(insert_EP
) insert_EP_actor(grid_tmp
);
845 cout
<<"================="<<endl
;
846 cout
<<"hitlist="<<hitlist
<<endl
;
847 cout
<<"================="<<endl
;
848 if(remove_FP
) remove_FP_actor(grid_tmp
);
849 if(remove_EP
) remove_EP_actor(grid_tmp
);
851 makeCopy(grid_tmp
,m_grid
);
854 cout
<<"===FullEdit END==="<<endl
;
857 int CreateSpecialMapping::UpdateMeshDensity()
859 cout
<<"===UpdateMeshDensity START==="<<endl
;
863 getAllSurfaceCells(m_AllCells
,m_grid
);
864 getSurfaceCells(m_bcs
, m_SelectedCells
, m_grid
);
865 EG_VTKDCC(vtkIntArray
, cell_code
, m_grid
, "cell_code");
866 EG_VTKDCN(vtkDoubleArray
, node_meshdensity
, m_grid
, "node_meshdensity");
867 m_SelectedNodes
.clear();
868 getSurfaceNodes(m_bcs
,m_SelectedNodes
,m_grid
);
869 getNodesFromCells(m_AllCells
, nodes
, m_grid
);
871 setCells(m_AllCells
);
873 if(DebugLevel
>5) cout
<<"m_AllCells.size()="<<m_AllCells
.size()<<endl
;
874 if(DebugLevel
>5) cout
<<"m_SelectedNodes.size()="<<m_SelectedNodes
.size()<<endl
;
876 EG_VTKDCN(vtkDoubleArray
, node_meshdensity_current
, m_grid
, "node_meshdensity_current");
877 foreach(vtkIdType node
,m_SelectedNodes
)
879 double L
=CurrentVertexAvgDist(node
,n2n
,m_grid
);
881 node_meshdensity_current
->SetValue(node
, D
);
883 cout
<<"===UpdateMeshDensity END==="<<endl
;
887 int CreateSpecialMapping::UpdateNodeType()
889 // cout<<"===UpdateNodeType START==="<<endl;
892 getAllSurfaceCells(m_AllCells
,m_grid
);
893 getSurfaceCells(m_bcs
, m_SelectedCells
, m_grid
);
894 if(DebugLevel
>5) cout
<<"m_AllCells.size()="<<m_AllCells
.size()<<endl
;
896 EG_VTKSP(vtkPolyData
, pdata
);
897 // addToPolyData(m_SelectedCells, pdata, m_grid);
898 addToPolyData(m_AllCells
, pdata
, m_grid
);
901 vtkPolyData
*source
= 0;
903 vtkIdType numPts
, numCells
, i
, numPolys
, numStrips
;
908 double x
[3], y
[3], deltaX
[3], xNew
[3], conv
, maxDist
, dist
, factor
;
909 double x1
[3], x2
[3], x3
[3], l1
[3], l2
[3];
910 double CosFeatureAngle
; //Cosine of angle between adjacent polys
911 double CosEdgeAngle
; // Cosine of angle between adjacent edges
912 double closestPt
[3], dist2
, *w
= NULL
;
913 int iterationNumber
, abortExecute
;
914 vtkIdType numSimple
=0, numBEdges
=0, numFixed
=0, numFEdges
=0;
915 vtkPolyData
*inMesh
, *Mesh
;
917 vtkTriangleFilter
*toTris
=NULL
;
918 vtkCellArray
*inVerts
, *inLines
, *inPolys
, *inStrips
;
920 vtkMeshVertexPtr Verts
;
921 vtkCellLocator
*cellLocator
=NULL
;
925 numPts
=input
->GetNumberOfPoints();
926 numCells
=input
->GetNumberOfCells();
927 if (numPts
< 1 || numCells
< 1)
929 cout
<<"No data to smooth!"<<endl
;
934 cos((double) vtkMath::DegreesToRadians() * this->FeatureAngle
);
935 CosEdgeAngle
= cos((double) vtkMath::DegreesToRadians() * this->EdgeAngle
);
938 cout
<<"Smoothing " << numPts
<< " vertices, " << numCells
940 << "\tConvergence= " << this->Convergence
<< "\n"
941 << "\tIterations= " << this->NumberOfIterations
<< "\n"
942 << "\tRelaxation Factor= " << this->RelaxationFactor
<< "\n"
943 << "\tEdge Angle= " << this->EdgeAngle
<< "\n"
944 << "\tBoundary Smoothing " << (this->BoundarySmoothing
? "On\n" : "Off\n")
945 << "\tFeature Edge Smoothing " << (this->FeatureEdgeSmoothing
? "On\n" : "Off\n")
946 << "\tError Scalars " << (this->GenerateErrorScalars
? "On\n" : "Off\n")
947 << "\tError Vectors " << (this->GenerateErrorVectors
? "On\n" : "Off\n")<<endl
;
949 // Peform topological analysis. What we're gonna do is build a connectivity
950 // array of connected vertices. The outcome will be one of three
951 // classifications for a vertex: VTK_SIMPLE_VERTEX, VTK_FIXED_VERTEX. or
952 // VTK_EDGE_VERTEX. Simple vertices are smoothed using all connected
953 // vertices. FIXED vertices are never smoothed. Edge vertices are smoothed
954 // using a subset of the attached vertices.
956 if(DebugLevel
>5) cout
<<"===>Analyze topology==="<<endl
;
957 if(DebugLevel
>5) cout
<<"Analyzing topology..."<<endl
;
958 if(DebugLevel
>5) cout
<<"0:numPts="<<numPts
<<endl
;
959 Verts
= new vtkMeshVertex
[numPts
];
960 for (i
=0; i
<numPts
; i
++)
962 if(DebugLevel
>5) cout
<<"0:VTK_SIMPLE_VERTEX"<<endl
;
963 Verts
[i
].type
= VTK_SIMPLE_VERTEX
; //can smooth
964 Verts
[i
].edges
= NULL
;
967 inPts
= input
->GetPoints();
968 conv
= this->Convergence
* input
->GetLength();
970 // check vertices first. Vertices are never smoothed_--------------
971 for (inVerts
=input
->GetVerts(), inVerts
->InitTraversal();
972 inVerts
->GetNextCell(npts
,pts
); )
974 for (j
=0; j
<npts
; j
++)
976 if(DebugLevel
>5) cout
<<"pts[j]="<<pts
[j
]<<"->vertices:VTK_FIXED_VERTEX 0"<<endl
;
977 Verts
[pts
[j
]].type
= VTK_FIXED_VERTEX
;
981 if(DebugLevel
>5) cout
<<"==check lines=="<<endl
;
982 // now check lines. Only manifold lines can be smoothed------------
983 for (inLines
=input
->GetLines(), inLines
->InitTraversal();
984 inLines
->GetNextCell(npts
,pts
); )
986 for (j
=0; j
<npts
; j
++)
988 if(DebugLevel
>5) cout
<<"pts[j]="<<pts
[j
]<<"->lines"<<endl
;
989 if ( Verts
[pts
[j
]].type
== VTK_SIMPLE_VERTEX
)
991 if ( j
== (npts
-1) ) //end-of-line marked FIXED
993 if(DebugLevel
>5) cout
<<"pts[j]="<<pts
[j
]<<"2:VTK_FIXED_VERTEX 1"<<endl
;
994 Verts
[pts
[j
]].type
= VTK_FIXED_VERTEX
;
996 else if ( j
== 0 ) //beginning-of-line marked FIXED
998 if(DebugLevel
>5) cout
<<"pts[j]="<<pts
[j
]<<"3:VTK_FIXED_VERTEX 2"<<endl
;
999 Verts
[pts
[0]].type
= VTK_FIXED_VERTEX
;
1000 inPts
->GetPoint(pts
[0],x2
);
1001 inPts
->GetPoint(pts
[1],x3
);
1003 else //is edge vertex (unless already edge vertex!)
1005 if(DebugLevel
>5) cout
<<"pts[j]="<<pts
[j
]<<"4:VTK_FEATURE_EDGE_VERTEX"<<endl
;
1006 Verts
[pts
[j
]].type
= VTK_FEATURE_EDGE_VERTEX
;
1007 Verts
[pts
[j
]].edges
= vtkIdList::New();
1008 Verts
[pts
[j
]].edges
->SetNumberOfIds(2);
1009 Verts
[pts
[j
]].edges
->SetId(0,pts
[j
-1]);
1010 Verts
[pts
[j
]].edges
->SetId(1,pts
[j
+1]);
1012 } //if simple vertex
1014 else if ( Verts
[pts
[j
]].type
== VTK_FEATURE_EDGE_VERTEX
)
1015 { //multiply connected, becomes fixed!
1016 if(DebugLevel
>5) cout
<<"pts[j]="<<pts
[j
]<<"5:VTK_FIXED_VERTEX 3"<<endl
;
1017 Verts
[pts
[j
]].type
= VTK_FIXED_VERTEX
;
1018 Verts
[pts
[j
]].edges
->Delete();
1019 Verts
[pts
[j
]].edges
= NULL
;
1022 } //for all points in this line
1025 if(DebugLevel
>5) cout
<<"==polygons and triangle strips=="<<endl
;
1026 // now polygons and triangle strips-------------------------------
1027 inPolys
=input
->GetPolys();
1028 numPolys
= inPolys
->GetNumberOfCells();
1029 inStrips
=input
->GetStrips();
1030 numStrips
= inStrips
->GetNumberOfCells();
1032 if(DebugLevel
>5) cout
<<"numPolys="<<numPolys
<<endl
;
1033 if(DebugLevel
>5) cout
<<"numStrips="<<numStrips
<<endl
;
1036 if ( numPolys
> 0 || numStrips
> 0 )
1037 { //build cell structure
1038 vtkCellArray
*polys
;
1040 int numNei
, nei
, edge
;
1041 vtkIdType numNeiPts
;
1043 double normal
[3], neiNormal
[3];
1044 vtkIdList
*neighbors
;
1046 neighbors
= vtkIdList::New();
1047 neighbors
->Allocate(VTK_CELL_SIZE
);
1049 inMesh
= vtkPolyData::New();
1050 inMesh
->SetPoints(inPts
);
1051 inMesh
->SetPolys(inPolys
);
1054 if ( (numStrips
= inStrips
->GetNumberOfCells()) > 0 )
1055 { // convert data to triangles
1056 inMesh
->SetStrips(inStrips
);
1057 toTris
= vtkTriangleFilter::New();
1058 toTris
->SetInput(inMesh
);
1060 Mesh
= toTris
->GetOutput();
1063 Mesh
->BuildLinks(); //to do neighborhood searching
1064 polys
= Mesh
->GetPolys();
1066 for (cellId
=0, polys
->InitTraversal(); polys
->GetNextCell(npts
,pts
);
1069 if(DebugLevel
>5) cout
<<"->cellId="<<cellId
<<endl
;
1070 for (i
=0; i
< npts
; i
++)
1072 if(DebugLevel
>5) cout
<<"-->i="<<i
<<endl
;
1074 p2
= pts
[(i
+1)%npts
];
1076 if ( Verts
[p1
].edges
== NULL
)
1078 Verts
[p1
].edges
= vtkIdList::New();
1079 Verts
[p1
].edges
->Allocate(16,6);
1081 if ( Verts
[p2
].edges
== NULL
)
1083 Verts
[p2
].edges
= vtkIdList::New();
1084 Verts
[p2
].edges
->Allocate(16,6);
1087 Mesh
->GetCellEdgeNeighbors(cellId
,p1
,p2
,neighbors
);
1088 numNei
= neighbors
->GetNumberOfIds();
1089 if(DebugLevel
>5) cout
<<"-->numNei="<<numNei
<<endl
;
1091 edge
= VTK_SIMPLE_VERTEX
;
1094 edge
= VTK_BOUNDARY_EDGE_VERTEX
;
1097 else if ( numNei
>= 2 )
1099 // check to make sure that this edge hasn't been marked already
1100 for (j
=0; j
< numNei
; j
++)
1102 if ( neighbors
->GetId(j
) < cellId
)
1109 edge
= VTK_FEATURE_EDGE_VERTEX
;
1113 else if ( numNei
== 1 && (nei
=neighbors
->GetId(0)) > cellId
)
1115 vtkPolygon::ComputeNormal(inPts
,npts
,pts
,normal
);
1116 Mesh
->GetCellPoints(nei
,numNeiPts
,neiPts
);
1117 vtkPolygon::ComputeNormal(inPts
,numNeiPts
,neiPts
,neiNormal
);
1119 if ( this->FeatureEdgeSmoothing
&&
1120 vtkMath::Dot(normal
,neiNormal
) <= CosFeatureAngle
)
1122 edge
= VTK_FEATURE_EDGE_VERTEX
;
1125 else // a visited edge; skip rest of analysis
1130 if ( edge
&& Verts
[p1
].type
== VTK_SIMPLE_VERTEX
)
1132 Verts
[p1
].edges
->Reset();
1133 Verts
[p1
].edges
->InsertNextId(p2
);
1134 Verts
[p1
].type
= edge
;
1136 else if ( (edge
&& Verts
[p1
].type
== VTK_BOUNDARY_EDGE_VERTEX
) ||
1137 (edge
&& Verts
[p1
].type
== VTK_FEATURE_EDGE_VERTEX
) ||
1138 (!edge
&& Verts
[p1
].type
== VTK_SIMPLE_VERTEX
) )
1140 Verts
[p1
].edges
->InsertNextId(p2
);
1141 if ( Verts
[p1
].type
&& edge
== VTK_BOUNDARY_EDGE_VERTEX
)
1143 Verts
[p1
].type
= VTK_BOUNDARY_EDGE_VERTEX
;
1147 if ( edge
&& Verts
[p2
].type
== VTK_SIMPLE_VERTEX
)
1149 Verts
[p2
].edges
->Reset();
1150 Verts
[p2
].edges
->InsertNextId(p1
);
1151 Verts
[p2
].type
= edge
;
1153 else if ( (edge
&& Verts
[p2
].type
== VTK_BOUNDARY_EDGE_VERTEX
) ||
1154 (edge
&& Verts
[p2
].type
== VTK_FEATURE_EDGE_VERTEX
) ||
1155 (!edge
&& Verts
[p2
].type
== VTK_SIMPLE_VERTEX
) )
1157 Verts
[p2
].edges
->InsertNextId(p1
);
1158 if ( Verts
[p2
].type
&& edge
== VTK_BOUNDARY_EDGE_VERTEX
)
1160 Verts
[p2
].type
= VTK_BOUNDARY_EDGE_VERTEX
;
1167 if (toTris
) {toTris
->Delete();}
1169 neighbors
->Delete();
1170 }//if strips or polys
1172 //post-process edge vertices to make sure we can smooth them
1173 for (i
=0; i
<numPts
; i
++)
1175 if ( Verts
[i
].type
== VTK_SIMPLE_VERTEX
)
1180 else if ( Verts
[i
].type
== VTK_FIXED_VERTEX
)
1185 else if ( Verts
[i
].type
== VTK_FEATURE_EDGE_VERTEX
||
1186 Verts
[i
].type
== VTK_BOUNDARY_EDGE_VERTEX
)
1187 { //see how many edges; if two, what the angle is
1189 if ( !this->BoundarySmoothing
&&
1190 Verts
[i
].type
== VTK_BOUNDARY_EDGE_VERTEX
)
1192 if(DebugLevel
>5) cout
<<"Verts[i].type = VTK_FIXED_VERTEX; 4"<<endl
;
1193 Verts
[i
].type
= VTK_FIXED_VERTEX
;
1197 else if ( (npts
= Verts
[i
].edges
->GetNumberOfIds()) != 2 )
1199 if(DebugLevel
>5) cout
<<"Verts["<<i
<<"].type = VTK_FIXED_VERTEX; 5"<<endl
;
1200 Verts
[i
].type
= VTK_FIXED_VERTEX
;
1204 else //check angle between edges
1206 inPts
->GetPoint(Verts
[i
].edges
->GetId(0),x1
);
1207 inPts
->GetPoint(i
,x2
);
1208 inPts
->GetPoint(Verts
[i
].edges
->GetId(1),x3
);
1212 l1
[k
] = x2
[k
] - x1
[k
];
1213 l2
[k
] = x3
[k
] - x2
[k
];
1215 if ( vtkMath::Normalize(l1
) >= 0.0 &&
1216 vtkMath::Normalize(l2
) >= 0.0 &&
1217 vtkMath::Dot(l1
,l2
) < CosEdgeAngle
)
1219 if(DebugLevel
>5) cout
<<"Verts["<<i
<<"].type = VTK_FIXED_VERTEX; 6"<<endl
;
1220 Verts
[i
].type
= VTK_FIXED_VERTEX
;
1225 if ( Verts
[i
].type
== VTK_FEATURE_EDGE_VERTEX
)
1239 cout
<<"Found\n\t" << numSimple
<< " simple vertices\n\t"
1240 << numFEdges
<< " feature edge vertices\n\t"
1241 << numBEdges
<< " boundary edge vertices\n\t"
1242 << numFixed
<< " fixed vertices\n\t"<<endl
;
1243 cout
<<"1:numPts="<<numPts
<<endl
;
1246 for (i
=0; i
<numPts
; i
++)
1248 if(DebugLevel
>5) cout
<<"Verts["<<i
<<"].type="<<VertexType2Str(Verts
[i
].type
)<<endl
;
1249 if(Verts
[i
].edges
!= NULL
&& (npts
= Verts
[i
].edges
->GetNumberOfIds()) > 0)
1251 for (j
=0; j
<npts
; j
++)
1253 if(DebugLevel
>5) cout
<<"Verts["<<i
<<"].edges->GetId("<<j
<<")="<<Verts
[i
].edges
->GetId(j
)<<endl
;
1254 }//for all connected points
1258 //Copy node type info from Verts
1259 EG_VTKDCN(vtkCharArray
, node_type
, m_grid
, "node_type");
1260 m_SelectedNodes
.clear();
1261 getSurfaceNodes(m_bcs
,m_SelectedNodes
,m_grid
);
1262 if(DebugLevel
>5) cout
<<"m_SelectedNodes.size()="<<m_SelectedNodes
.size()<<endl
;
1263 foreach(vtkIdType node
,m_SelectedNodes
)
1265 if(DebugLevel
>5) cout
<<"Verts["<<node
<<"].type="<<VertexType2Str(Verts
[node
].type
)<<endl
;
1266 node_type
->SetValue(node
,Verts
[node
].type
);
1269 //free up connectivity storage
1270 for (int i
=0; i
<numPts
; i
++)
1272 if ( Verts
[i
].edges
!= NULL
)
1274 Verts
[i
].edges
->Delete();
1275 Verts
[i
].edges
= NULL
;
1283 // vtkIdType CreateSpecialMapping::FindSnapPoint(vtkUnstructuredGrid *src, vtkIdType DeadNode)
1284 vtkIdType
CreateSpecialMapping::FindSnapPoint(vtkUnstructuredGrid
*src
, vtkIdType DeadNode
,QSet
<vtkIdType
> & DeadCells
,QSet
<vtkIdType
> & MutatedCells
,QSet
<vtkIdType
> & MutilatedCells
)
1286 getAllSurfaceCells(m_AllCells
,src
);
1287 getSurfaceCells(m_bcs
, m_SelectedCells
, src
);
1288 m_SelectedNodes
.clear();
1289 getSurfaceNodes(m_bcs
,m_SelectedNodes
,src
);
1290 getNodesFromCells(m_AllCells
, nodes
, src
);
1292 setCells(m_AllCells
);
1296 EG_VTKDCN(vtkCharArray
, node_type
, src
, "node_type");
1297 if(node_type
->GetValue(DeadNode
)==VTK_FIXED_VERTEX
)
1299 cout
<<"Sorry, unable to remove fixed vertex."<<endl
;
1304 N_points
=src
->GetNumberOfPoints();
1305 N_cells
=src
->GetNumberOfCells();
1309 vtkIdType SnapPoint
=-1;
1311 foreach(vtkIdType PSP
, n2n
[DeadNode
])
1313 bool IsValidSnapPoint
=true;
1315 if(DebugLevel
>10) cout
<<"====>PSP="<<PSP
<<endl
;
1317 if(NumberOfCommonPoints(DeadNode
,PSP
,IsTetra
)>2)//common point check
1319 if(DebugLevel
>10) cout
<<"Sorry, but you are not allowed to move point "<<DeadNode
<<" to point "<<PSP
<<"."<<endl
;
1320 IsValidSnapPoint
=false;
1322 if(IsTetra
)//tetra check
1324 if(DebugLevel
>10) cout
<<"Sorry, but you are not allowed to move point "<<DeadNode
<<" to point "<<PSP
<<"."<<endl
;
1325 IsValidSnapPoint
=false;
1328 //count number of points and cells to remove + analyse cell transformations
1332 MutatedCells
.clear();
1333 MutilatedCells
.clear();
1334 foreach(vtkIdType C
, n2c
[DeadNode
])//loop through potentially dead cells
1336 //get points around cell
1337 vtkIdType N_pts
, *pts
;
1338 src
->GetCellPoints(C
, N_pts
, pts
);
1340 bool ContainsSnapPoint
=false;
1341 bool invincible
=false;
1342 for(int i
=0;i
<N_pts
;i
++)
1344 if(DebugLevel
>10) cout
<<"pts["<<i
<<"]="<<pts
[i
]<<" and PSP="<<PSP
<<endl
;
1345 if(pts
[i
]==PSP
) {ContainsSnapPoint
=true;}
1346 if(pts
[i
]!=DeadNode
&& pts
[i
]!=PSP
&& n2c
[pts
[i
]].size()<=1) invincible
=true;
1348 if(ContainsSnapPoint
)
1350 if(N_pts
==3)//dead cell
1352 if(invincible
)//Check that empty lines aren't left behind when a cell is killed
1354 if(DebugLevel
>10) cout
<<"Sorry, but you are not allowed to move point "<<DeadNode
<<" to point "<<PSP
<<"."<<endl
;
1355 IsValidSnapPoint
=false;
1359 DeadCells
.insert(C
);
1361 if(DebugLevel
>10) cout
<<"cell "<<C
<<" has been pwned!"<<endl
;
1366 cout
<<"RED ALERT: Xenomorph detected!"<<endl
;
1372 vtkIdType src_N_pts
, *src_pts
;
1373 src
->GetCellPoints(C
, src_N_pts
, src_pts
);
1377 cout
<<"RED ALERT: Xenomorph detected!"<<endl
;
1381 vtkIdType OldTriangle
[3];
1382 vtkIdType NewTriangle
[3];
1384 for(int i
=0;i
<src_N_pts
;i
++)
1386 OldTriangle
[i
]=src_pts
[i
];
1387 NewTriangle
[i
]=( (src_pts
[i
]==DeadNode
) ? PSP
: src_pts
[i
] );
1389 vec3_t Old_N
= triNormal(src
, OldTriangle
[0], OldTriangle
[1], OldTriangle
[2]);
1390 vec3_t New_N
= triNormal(src
, NewTriangle
[0], NewTriangle
[1], NewTriangle
[2]);
1391 double OldArea
=Old_N
.abs();
1392 double NewArea
=New_N
.abs();
1393 double scal
=Old_N
*New_N
;
1394 double cross
=(Old_N
.cross(New_N
)).abs();//double-cross on Nar Shadaa B-)
1397 cout
<<"OldArea="<<OldArea
<<endl
;
1398 cout
<<"NewArea="<<NewArea
<<endl
;
1399 cout
<<"scal="<<scal
<<endl
;
1400 cout
<<"cross="<<cross
<<endl
;
1403 if(Old_N
*New_N
<Old_N
*Old_N
*1./100.)//area + inversion check
1405 if(DebugLevel
>10) cout
<<"Sorry, but you are not allowed to move point "<<DeadNode
<<" to point "<<PSP
<<"."<<endl
;
1406 IsValidSnapPoint
=false;
1410 MutatedCells
.insert(C
);
1411 if(DebugLevel
>10) cout
<<"cell "<<C
<<" has been infected!"<<endl
;
1415 if(N_cells
+N_newcells
<=0)//survivor check
1417 if(DebugLevel
>10) cout
<<"Sorry, but you are not allowed to move point "<<DeadNode
<<" to point "<<PSP
<<"."<<endl
;
1418 IsValidSnapPoint
=false;
1420 if(node_type
->GetValue(DeadNode
)==VTK_BOUNDARY_EDGE_VERTEX
&& node_type
->GetValue(PSP
)==VTK_SIMPLE_VERTEX
)
1422 if(DebugLevel
>10) cout
<<"Sorry, but you are not allowed to move point "<<DeadNode
<<" to point "<<PSP
<<"."<<endl
;
1423 IsValidSnapPoint
=false;
1426 if(IsValidSnapPoint
) {SnapPoint
=PSP
; break;}
1427 }//end of loop through potential SnapPoints
1431 cout
<<"AT FINDSNAPPOINT EXIT"<<endl
;
1432 cout
<<"N_points="<<N_points
<<endl
;
1433 cout
<<"N_cells="<<N_cells
<<endl
;
1434 cout
<<"N_newpoints="<<N_newpoints
<<endl
;
1435 cout
<<"N_newcells="<<N_newcells
<<endl
;
1440 bool CreateSpecialMapping::DeletePoint_2(vtkUnstructuredGrid
*src
, vtkIdType DeadNode
)
1442 getAllSurfaceCells(m_AllCells
,src
);
1443 getSurfaceCells(m_bcs
, m_SelectedCells
, src
);
1444 m_SelectedNodes
.clear();
1445 getSurfaceNodes(m_bcs
,m_SelectedNodes
,src
);
1446 getNodesFromCells(m_AllCells
, nodes
, src
);
1448 setCells(m_AllCells
);
1451 N_points
=src
->GetNumberOfPoints();
1452 N_cells
=src
->GetNumberOfCells();
1456 if(DeadNode
<0 || DeadNode
>=N_points
)
1458 cout
<<"Warning: Point out of range: DeadNode="<<DeadNode
<<" N_points="<<N_points
<<endl
;
1462 QSet
<vtkIdType
> DeadCells
;
1463 QSet
<vtkIdType
> MutatedCells
;
1464 QSet
<vtkIdType
> MutilatedCells
;
1467 cout
<<"BEFORE FINDSNAPPOINT"<<endl
;
1468 cout
<<"N_points="<<N_points
<<endl
;
1469 cout
<<"N_cells="<<N_cells
<<endl
;
1470 cout
<<"N_newpoints="<<N_newpoints
<<endl
;
1471 cout
<<"N_newcells="<<N_newcells
<<endl
;
1473 vtkIdType SnapPoint
=FindSnapPoint(src
,DeadNode
,DeadCells
,MutatedCells
,MutilatedCells
);
1477 if(DebugLevel
>10) cout
<<"===>SNAPPOINT="<<SnapPoint
<<endl
;
1478 if(SnapPoint
<0) {cout
<<"Sorry no possible SnapPoint found."<<endl
; return(false);}
1482 cout
<<"BEFORE ALLOCATION"<<endl
;
1483 cout
<<"N_points="<<N_points
<<endl
;
1484 cout
<<"N_cells="<<N_cells
<<endl
;
1485 cout
<<"N_newpoints="<<N_newpoints
<<endl
;
1486 cout
<<"N_newcells="<<N_newcells
<<endl
;
1488 N_points
=m_grid
->GetNumberOfPoints();
1489 N_cells
=m_grid
->GetNumberOfCells();
1490 cout
<<"N_points="<<N_points
<<endl
;
1491 cout
<<"N_cells="<<N_cells
<<endl
;
1492 cout
<<"N_newpoints="<<N_newpoints
<<endl
;
1493 cout
<<"N_newcells="<<N_newcells
<<endl
;
1494 EG_VTKSP(vtkUnstructuredGrid
,dst
);
1495 allocateGrid(dst
,N_cells
+N_newcells
,N_points
+N_newpoints
);
1496 m_total_N_newpoints
+=N_newpoints
; m_total_N_newcells
+=N_newcells
;
1498 //vector used to redefine the new point IDs
1499 QVector
<vtkIdType
> OffSet(N_points
);
1501 //copy undead points
1502 vtkIdType dst_id_node
=0;
1503 for (vtkIdType src_id_node
= 0; src_id_node
< N_points
; src_id_node
++) {//loop through src points
1504 if(src_id_node
!=DeadNode
)//if the node isn't dead, copy it
1507 src
->GetPoints()->GetPoint(src_id_node
, x
.data());
1508 dst
->GetPoints()->SetPoint(dst_id_node
, x
.data());
1509 copyNodeData(src
, src_id_node
, dst
, dst_id_node
);
1510 OffSet
[src_id_node
]=src_id_node
-dst_id_node
;
1515 cout
<<"DeadCells="<<DeadCells
<<endl
;
1516 cout
<<"MutatedCells="<<MutatedCells
<<endl
;
1517 cout
<<"MutilatedCells="<<MutilatedCells
<<endl
;
1520 for (vtkIdType id_cell
= 0; id_cell
< src
->GetNumberOfCells(); ++id_cell
) {//loop through src cells
1521 if(!DeadCells
.contains(id_cell
))//if the cell isn't dead
1523 vtkIdType src_N_pts
, *src_pts
;
1524 vtkIdType dst_N_pts
, *dst_pts
;
1525 src
->GetCellPoints(id_cell
, src_N_pts
, src_pts
);
1527 vtkIdType type_cell
= src
->GetCellType(id_cell
);
1528 if(DebugLevel
>10) cout
<<"-->id_cell="<<id_cell
<<endl
;
1529 if(DebugLevel
>10) for(int i
=0;i
<src_N_pts
;i
++) cout
<<"src_pts["<<i
<<"]="<<src_pts
[i
]<<endl
;
1530 // src->GetCellPoints(id_cell, dst_N_pts, dst_pts);
1531 dst_N_pts
=src_N_pts
;
1532 dst_pts
=new vtkIdType
[dst_N_pts
];
1533 if(MutatedCells
.contains(id_cell
))//mutated cell
1535 if(DebugLevel
>10) cout
<<"processing mutated cell "<<id_cell
<<endl
;
1536 for(int i
=0;i
<src_N_pts
;i
++)
1538 if(src_pts
[i
]==DeadNode
) {
1540 cout
<<"SnapPoint="<<SnapPoint
<<endl
;
1541 cout
<<"OffSet[SnapPoint]="<<OffSet
[SnapPoint
]<<endl
;
1542 cout
<<"src_pts["<<i
<<"]="<<src_pts
[i
]<<endl
;
1544 dst_pts
[i
]=SnapPoint
-OffSet
[SnapPoint
];
1546 else dst_pts
[i
]=src_pts
[i
]-OffSet
[src_pts
[i
]];
1548 if(DebugLevel
>10) cout
<<"--->dst_pts:"<<endl
;
1549 if(DebugLevel
>10) for(int i
=0;i
<dst_N_pts
;i
++) cout
<<"dst_pts["<<i
<<"]="<<dst_pts
[i
]<<endl
;
1552 else if(MutilatedCells
.contains(id_cell
))//mutilated cell
1554 if(DebugLevel
>10) cout
<<"processing mutilated cell "<<id_cell
<<endl
;
1556 if(type_cell
==VTK_QUAD
) {
1557 type_cell
=VTK_TRIANGLE
;
1560 else {cout
<<"FATAL ERROR: Unknown mutilated cell detected! It is not a quad! Potential xenomorph infestation!"<<endl
;EG_BUG
;}
1563 for(int i
=0;i
<src_N_pts
;i
++)
1565 if(src_pts
[i
]==SnapPoint
) { dst_pts
[j
]=SnapPoint
-OffSet
[SnapPoint
];j
++; }//SnapPoint
1566 else if(src_pts
[i
]!=DeadNode
) { dst_pts
[j
]=src_pts
[i
]-OffSet
[src_pts
[i
]];j
++; }//pre-snap/dead + post-snap/dead
1567 //do nothing in case of DeadNode
1572 if(DebugLevel
>10) cout
<<"processing normal cell "<<id_cell
<<endl
;
1573 for(int i
=0;i
<src_N_pts
;i
++)
1575 dst_pts
[i
]=src_pts
[i
]-OffSet
[src_pts
[i
]];
1579 vtkIdType id_new_cell
= dst
->InsertNextCell(type_cell
, dst_N_pts
, dst_pts
);
1580 copyCellData(src
, id_cell
, dst
, id_new_cell
);
1582 cout
<<"===Copying cell "<<id_cell
<<" to "<<id_new_cell
<<"==="<<endl
;
1583 cout
<<"src_pts:"<<endl
;
1584 for(int i
=0;i
<src_N_pts
;i
++) cout
<<"src_pts["<<i
<<"]="<<src_pts
[i
]<<endl
;
1585 cout
<<"dst_pts:"<<endl
;
1586 for(int i
=0;i
<dst_N_pts
;i
++) cout
<<"dst_pts["<<i
<<"]="<<dst_pts
[i
]<<endl
;
1587 cout
<<"OffSet="<<OffSet
<<endl
;
1588 cout
<<"===Copying cell end==="<<endl
;
1593 // cout_grid(cout,dst,true,true,true,true);
1598 int CreateSpecialMapping::remove_EP_all_2()
1600 cout
<<"===remove_EP_all_2 START==="<<endl
;
1601 getAllSurfaceCells(m_AllCells
,m_grid
);
1602 getSurfaceCells(m_bcs
, m_SelectedCells
, m_grid
);
1603 EG_VTKDCC(vtkIntArray
, cell_code
, m_grid
, "cell_code");
1604 EG_VTKDCN(vtkDoubleArray
, node_meshdensity
, m_grid
, "node_meshdensity");
1605 m_SelectedNodes
.clear();
1606 getSurfaceNodes(m_bcs
,m_SelectedNodes
,m_grid
);
1607 getNodesFromCells(m_AllCells
, nodes
, m_grid
);
1609 setCells(m_AllCells
);
1610 cout
<<"m_AllCells.size()="<<m_AllCells
.size()<<endl
;
1614 N_points
=m_grid
->GetNumberOfPoints();
1615 N_cells
=m_grid
->GetNumberOfCells();
1621 hitlist
.resize(N_points
);
1622 offset
.resize(N_points
);
1624 marked_cells
.clear();
1625 marked_nodes
.clear();
1627 remove_EP_counter();
1628 cout
<<"================="<<endl
;
1629 cout
<<"hitlist="<<hitlist
<<endl
;
1630 cout
<<"================="<<endl
;
1634 for(int i
=0;i
<hitlist
.size();i
++)
1638 cout
<<"Deleting point "<<i
<<" currently known as "<<i
-kills
<<endl
;
1639 if(DeletePoint_2(m_grid
,i
-kills
))
1642 cout
<<"Kill successful"<<endl
;
1646 cout
<<"Kill failed"<<endl
;
1651 cout
<<"Killed: "<<kills
<<"/"<<contracts
<<endl
;
1652 if(kills
!=contracts
) {cout
<<"MISSION FAILED"<<endl
;EG_BUG
;}
1653 cout
<<"===remove_EP_all_2 END==="<<endl
;
1657 int CreateSpecialMapping::remove_FP_all_2()
1659 cout
<<"===remove_FP_all_2 START==="<<endl
;
1660 getAllSurfaceCells(m_AllCells
,m_grid
);
1661 getSurfaceCells(m_bcs
, m_SelectedCells
, m_grid
);
1662 EG_VTKDCC(vtkIntArray
, cell_code
, m_grid
, "cell_code");
1663 EG_VTKDCN(vtkDoubleArray
, node_meshdensity
, m_grid
, "node_meshdensity");
1664 m_SelectedNodes
.clear();
1665 getSurfaceNodes(m_bcs
,m_SelectedNodes
,m_grid
);
1666 getNodesFromCells(m_AllCells
, nodes
, m_grid
);
1668 setCells(m_AllCells
);
1669 cout
<<"m_AllCells.size()="<<m_AllCells
.size()<<endl
;
1673 N_points
=m_grid
->GetNumberOfPoints();
1674 N_cells
=m_grid
->GetNumberOfCells();
1680 hitlist
.resize(N_points
);
1681 offset
.resize(N_points
);
1683 marked_cells
.clear();
1684 marked_nodes
.clear();
1686 remove_FP_counter();
1687 cout
<<"================="<<endl
;
1688 cout
<<"hitlist="<<hitlist
<<endl
;
1689 cout
<<"================="<<endl
;
1693 for(int i
=0;i
<hitlist
.size();i
++)
1697 cout
<<"Deleting point "<<i
<<" currently known as "<<i
-kills
<<endl
;
1698 if(DeletePoint_2(m_grid
,i
-kills
))
1701 cout
<<"Kill successful"<<endl
;
1705 cout
<<"Kill failed"<<endl
;
1710 cout
<<"Killed: "<<kills
<<"/"<<contracts
<<endl
;
1711 if(kills
!=contracts
) {cout
<<"MISSION FAILED"<<endl
;EG_BUG
;}
1712 cout
<<"===remove_FP_all_2 END==="<<endl
;