VMTK
Loading...
Searching...
No Matches
vtkvmtkBoundaryLayerGenerator.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkBoundaryLayerGenerator.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:47:48 $
7Version: $Revision: 1.7 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
23#include "vtkvmtkConstants.h"
24#include "vtkUnstructuredGrid.h"
25#include "vtkPointData.h"
26#include "vtkCellData.h"
27#include "vtkPoints.h"
28#include "vtkCellArray.h"
29#include "vtkDoubleArray.h"
30#include "vtkIntArray.h"
31#include "vtkUnsignedCharArray.h"
32#include "vtkMath.h"
33#include "vtkLine.h"
34#include "vtkTriangle.h"
35#include "vtkInformation.h"
36#include "vtkInformationVector.h"
37#include "vtkObjectFactory.h"
38
39#include <iostream>
40
42
44{
45 this->WarpVectorsArrayName = NULL;
46 this->LayerThicknessArrayName = NULL;
47
48 this->WarpVectorsArray = NULL;
49 this->LayerThicknessArray = NULL;
50
52 this->ConstantThickness = 0;
53
54 this->LayerThickness = 1.0;
55 this->LayerThicknessRatio = 1.0; // ratio with respect to the LayerThickness (both constant and local)
57 this->NumberOfSubLayers = 1;
58 this->SubLayerRatio = 1.0; // thickness ratio between successive sublayers (moving from the surface)
59 this->NumberOfSubsteps = 2000;
60 this->Relaxation = 0.01;
61 this->LocalCorrectionFactor = 0.45;
63 this->NumberOfTangledCells = 0;
64
65 this->IncludeSurfaceCells = 0;
66 this->IncludeSidewallCells = 0;
67 this->NegateWarpVectors = 0;
68
69 this->CellEntityIdsArrayName = NULL;
72 this->SidewallCellEntityId = 0;
73 this->VolumeCellEntityId = 0;
74
75 this->InnerSurface = NULL;
76}
77
79{
80 if (this->WarpVectorsArrayName)
81 {
82 delete[] this->WarpVectorsArrayName;
83 this->WarpVectorsArrayName = NULL;
84 }
85
87 {
88 delete[] this->LayerThicknessArrayName;
89 this->LayerThicknessArrayName = NULL;
90 }
91
92 if (this->InnerSurface)
93 {
94 this->InnerSurface->Delete();
95 this->InnerSurface = NULL;
96 }
97
98 if (this->CellEntityIdsArrayName)
99 {
100 delete[] this->CellEntityIdsArrayName;
101 this->CellEntityIdsArrayName = NULL;
102 }
103}
104
106 vtkInformation *vtkNotUsed(request),
107 vtkInformationVector **inputVector,
108 vtkInformationVector *outputVector)
109{
110 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
111 vtkInformation *outInfo = outputVector->GetInformationObject(0);
112
113 vtkUnstructuredGrid *input = vtkUnstructuredGrid::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
114 vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
115
116 vtkPoints* inputPoints = input->GetPoints();
117
118 if (!this->WarpVectorsArrayName)
119 {
120 vtkErrorMacro("WarpVectors array name not specified.");
121 return 1;
122 }
123
124 if (!this->CellEntityIdsArrayName)
125 {
126 vtkErrorMacro("CellEntityIds array name not specified.");
127 return 1;
128 }
129
130 if (!input->GetPointData()->GetArray(this->WarpVectorsArrayName))
131 {
132 vtkErrorMacro(<< "WarpVectors array with name specified does not exist!");
133 return 1;
134 }
135
136 this->WarpVectorsArray = input->GetPointData()->GetArray(this->WarpVectorsArrayName);
137
139 {
140 if (!this->LayerThicknessArrayName)
141 {
142 vtkErrorMacro("LayerThickness array name not specified.");
143 return 1;
144 }
145
146 if (!input->GetPointData()->GetArray(this->LayerThicknessArrayName))
147 {
148 vtkErrorMacro(<< "LayerThickness array with name specified does not exist!");
149 return 1;
150 }
151
152 this->LayerThicknessArray = input->GetPointData()->GetArray(this->LayerThicknessArrayName);
153 }
154
155 vtkIdType i;
156
157 vtkPoints* outputPoints = vtkPoints::New();
158 vtkPoints* warpedPoints = vtkPoints::New();
159
160 vtkCellArray* boundaryLayerCellArray = vtkCellArray::New();
161 vtkIdList* boundaryLayerCellTypes = vtkIdList::New();
162
163 vtkIntArray* cellEntityIdsArray = vtkIntArray::New();
164 cellEntityIdsArray->SetName(this->CellEntityIdsArrayName);
165
166 vtkIntArray* innerSurfaceCellEntityIdsArray = vtkIntArray::New();
167 innerSurfaceCellEntityIdsArray->SetName(this->CellEntityIdsArrayName);
168
169 vtkUnsignedCharArray* checkArray = vtkUnsignedCharArray::New();
170 checkArray->SetNumberOfTuples(input->GetNumberOfCells());
171 checkArray->FillComponent(0,1);
172
173 vtkIdType numberOfInputPoints = inputPoints->GetNumberOfPoints();
174 vtkIdType numberOfInputCells = input->GetNumberOfCells();
175
176 int cellType;
177 cellType = input->GetCellType(0); // TODO: check if all elements are consistent
178 bool warpQuadratic = false;
179 if (cellType == VTK_QUADRATIC_TRIANGLE)
180 {
181 warpQuadratic = true;
182 }
183
184 vtkIdType numberOfLayerPoints = numberOfInputPoints;
185 if (warpQuadratic)
186 {
187 numberOfLayerPoints = 2 * numberOfInputPoints;
188 }
189
190 vtkIdType numberOfOutputPoints = numberOfInputPoints + numberOfLayerPoints * this->NumberOfSubLayers;
191 outputPoints->SetNumberOfPoints(numberOfOutputPoints);
192
193 double point[3];
194 for (i=0; i<numberOfInputPoints; i++)
195 {
196 inputPoints->GetPoint(i,point);
197 outputPoints->SetPoint(i,point);
198 }
199
200 vtkIdType npts;
201 const vtkIdType *pts;
202 vtkIdType *surfacePts;
203
204 if (this->IncludeSurfaceCells)
205 {
206 for (i=0; i<numberOfInputCells; i++)
207 {
208 input->GetCellPoints(i,npts,pts);
209 cellType = input->GetCellType(i);
210 surfacePts = new vtkIdType[npts];
211 switch(cellType)
212 {
213 case VTK_TRIANGLE:
214 boundaryLayerCellTypes->InsertNextId(VTK_TRIANGLE);
215 surfacePts[0] = pts[0];
216 surfacePts[1] = pts[1];
217 surfacePts[2] = pts[2];
218 break;
219 case VTK_QUAD:
220 boundaryLayerCellTypes->InsertNextId(VTK_QUAD);
221 surfacePts[0] = pts[0];
222 surfacePts[1] = pts[1];
223 surfacePts[2] = pts[2];
224 surfacePts[3] = pts[3];
225 break;
226 case VTK_QUADRATIC_TRIANGLE:
227 boundaryLayerCellTypes->InsertNextId(VTK_QUADRATIC_TRIANGLE);
228 surfacePts[0] = pts[0];
229 surfacePts[1] = pts[1];
230 surfacePts[2] = pts[2];
231 surfacePts[3] = pts[3];
232 surfacePts[4] = pts[4];
233 surfacePts[5] = pts[5];
234 break;
235 default:
236 vtkErrorMacro(<<"Unsupported surface element.");
237 return 1;
238 break;
239 }
240 boundaryLayerCellArray->InsertNextCell(npts,surfacePts);
241 cellEntityIdsArray->InsertNextValue(this->InnerSurfaceCellEntityId);
242 delete[] surfacePts;
243 }
244 }
245
246 vtkIdList* edgePointIds = vtkIdList::New();
247 vtkIdList* edgeNeighborCellIds = vtkIdList::New();
248
249 double relaxation = this->Relaxation;
250 int initialNumberOfSubsteps = this->NumberOfSubsteps / 100;
251 int intermediateNumberOfSubsteps = this->NumberOfSubsteps / 10;
252 int finalNumberOfSubsteps = this->NumberOfSubsteps - initialNumberOfSubsteps - intermediateNumberOfSubsteps;
253
254 this->BuildWarpVectors(input);
255 this->IncrementalWarpVectors(input,initialNumberOfSubsteps,relaxation);
256
257 int iteration = 0;
258 int tangled = this->CheckTangle(input,checkArray);
259 if (tangled > 0)
260 {
261 std::cout << "untangle procedure.. " << tangled << " tangled cells" << std::endl;
262 }
263
264 // Each round is a local correction followed by a tenth of the sweep, and the loop may go
265 // round as many times as there are tenths - but a layer that will not untangle does not
266 // untangle in a hundred rounds either, and every round costs a sweep of the surface. So the
267 // loop also stops once a run of rounds has left the count of tangled cells no lower than the
268 // best it has been, and says so, rather than spending the full allowance to hand back the
269 // same tangle.
270 int maximumNumberOfIterations = this->NumberOfSubsteps / 10;
271 int fewestTangled = tangled;
272 int roundsWithoutImprovement = 0;
273 while (tangled > 0 && iteration < maximumNumberOfIterations)
274 {
275 this->LocalUntangle(input,checkArray,this->LocalCorrectionFactor);
276 this->IncrementalWarpVectors(input,intermediateNumberOfSubsteps,relaxation);
277 tangled = this->CheckTangle(input,checkArray);
278 iteration += 1;
279 if (tangled == 0)
280 {
281 std::cout << "untangle iteration " << iteration << ": untangled, sweeping the rest" << std::endl;
282 this->IncrementalWarpVectors(input,finalNumberOfSubsteps,relaxation); //questo se alto tira un po la piega che altrimenti si forma
283 tangled = this->CheckTangle(input,checkArray);
284 }
285 std::cout << "untangle iteration " << iteration << ": " << tangled << " tangled cells" << std::endl;
286 if (tangled < fewestTangled)
287 {
288 fewestTangled = tangled;
289 roundsWithoutImprovement = 0;
290 }
291 else if (++roundsWithoutImprovement >= this->MaximumUntangleRoundsWithoutImprovement)
292 {
293 std::cout << "untangle procedure stopped: " << tangled << " cells are still tangled after "
294 << roundsWithoutImprovement << " rounds without improvement" << std::endl;
295 break;
296 }
297 }
298 this->NumberOfTangledCells = tangled;
299
300 int k;
301 for (k=0; k<this->NumberOfSubLayers; k++)
302 {
303 warpedPoints->Initialize();
304 this->WarpPoints(inputPoints,warpedPoints,k,warpQuadratic);
305
306 for (i=0; i<numberOfLayerPoints; i++)
307 {
308 warpedPoints->GetPoint(i,point);
309 outputPoints->SetPoint(i + numberOfInputPoints + k*numberOfLayerPoints,point);
310 }
311
312 vtkIdType prismNPts, *prismPts;
313 vtkIdType quadNPts, *quadPts;
314 for (i=0; i<numberOfInputCells; i++)
315 {
316 input->GetCellPoints(i,npts,pts);
317 cellType = input->GetCellType(i);
318 if (cellType == VTK_TRIANGLE || cellType == VTK_QUAD)
319 {
320 prismNPts = npts * 2;
321 prismPts = new vtkIdType[prismNPts];
322 quadNPts = 4;
323 quadPts = new vtkIdType[quadNPts];
324 int j;
325 for (j=0; j<npts; j++)
326 {
327 prismPts[j] = pts[j] + k*numberOfLayerPoints;
328 }
329 for (j=0; j<npts; j++)
330 {
331 prismPts[j+npts] = pts[j] + (k+1)*numberOfLayerPoints;
332 }
333 boundaryLayerCellArray->InsertNextCell(prismNPts,prismPts);
334 cellEntityIdsArray->InsertNextValue(this->VolumeCellEntityId);
335
336 if (cellType == VTK_TRIANGLE)
337 {
338 boundaryLayerCellTypes->InsertNextId(VTK_WEDGE);
339 }
340 else if (cellType == VTK_QUAD)
341 {
342 boundaryLayerCellTypes->InsertNextId(VTK_HEXAHEDRON);
343 }
344
345 if (this->IncludeSidewallCells)
346 {
347 for (j=0; j<npts; j++)
348 {
349 vtkIdType jnext = (j+1) % npts;
350 edgePointIds->Initialize();
351 edgePointIds->SetNumberOfIds(2);
352 edgePointIds->SetId(0,pts[j]);
353 edgePointIds->SetId(1,pts[jnext]);
354 input->GetCellNeighbors(i,edgePointIds,edgeNeighborCellIds);
355
356 if (edgeNeighborCellIds->GetNumberOfIds() > 0)
357 {
358 continue;
359 }
360
361 quadPts[0] = prismPts[j];
362 quadPts[1] = prismPts[jnext];
363 quadPts[2] = prismPts[jnext+npts];
364 quadPts[3] = prismPts[j+npts];
365
366 boundaryLayerCellArray->InsertNextCell(quadNPts,quadPts);
367 boundaryLayerCellTypes->InsertNextId(VTK_QUAD);
368 cellEntityIdsArray->InsertNextValue(this->SidewallCellEntityId);
369 }
370 }
371
372 delete[] prismPts;
373 delete[] quadPts;
374 }
375 else if (cellType == VTK_QUADRATIC_TRIANGLE)
376 {
377 prismNPts = npts * 3 - 3;
378 prismPts = new vtkIdType[prismNPts];
379 quadNPts = 8;
380 quadPts = new vtkIdType[quadNPts];
381
382 boundaryLayerCellTypes->InsertNextId(VTK_QUADRATIC_WEDGE);
383
384 prismPts[0] = pts[0] + k*numberOfLayerPoints;
385 prismPts[1] = pts[1] + k*numberOfLayerPoints;
386 prismPts[2] = pts[2] + k*numberOfLayerPoints;
387
388 prismPts[3] = pts[0] + k*numberOfLayerPoints + numberOfLayerPoints;
389 prismPts[4] = pts[1] + k*numberOfLayerPoints + numberOfLayerPoints;
390 prismPts[5] = pts[2] + k*numberOfLayerPoints + numberOfLayerPoints;
391
392 prismPts[6] = pts[3] + k*numberOfLayerPoints;
393 prismPts[7] = pts[4] + k*numberOfLayerPoints;
394 prismPts[8] = pts[5] + k*numberOfLayerPoints;
395
396 prismPts[9] = pts[3] + k*numberOfLayerPoints + numberOfLayerPoints;
397 prismPts[10] = pts[4] + k*numberOfLayerPoints + numberOfLayerPoints;
398 prismPts[11] = pts[5] + k*numberOfLayerPoints + numberOfLayerPoints;
399
400 prismPts[12] = pts[0] + k*numberOfLayerPoints + numberOfLayerPoints/2;
401 prismPts[13] = pts[1] + k*numberOfLayerPoints + numberOfLayerPoints/2;
402 prismPts[14] = pts[2] + k*numberOfLayerPoints + numberOfLayerPoints/2;
403
404 boundaryLayerCellArray->InsertNextCell(prismNPts,prismPts);
405 cellEntityIdsArray->InsertNextValue(this->VolumeCellEntityId);
406
407 if (this->IncludeSidewallCells)
408 {
409 for (int j=0; j<npts/2; j++)
410 {
411 vtkIdType jnext = (j+1) % npts;
412 edgePointIds->Initialize();
413 edgePointIds->SetNumberOfIds(2);
414 edgePointIds->SetId(0,pts[j]);
415 edgePointIds->SetId(1,pts[jnext]);
416 input->GetCellNeighbors(i,edgePointIds,edgeNeighborCellIds);
417
418 if (edgeNeighborCellIds->GetNumberOfIds() > 0)
419 {
420 continue;
421 }
422
423 quadPts[0] = prismPts[j];
424 quadPts[1] = prismPts[jnext];
425 quadPts[2] = prismPts[jnext+npts/2];
426 quadPts[3] = prismPts[j+npts/2];
427
428 quadPts[4] = prismPts[j+npts];
429 quadPts[5] = prismPts[jnext+2*npts];
430 quadPts[6] = prismPts[j+npts+npts/2];
431 quadPts[7] = prismPts[j+2*npts];
432
433 boundaryLayerCellArray->InsertNextCell(quadNPts,quadPts);
434 boundaryLayerCellTypes->InsertNextId(VTK_QUAD);
435 cellEntityIdsArray->InsertNextValue(this->SidewallCellEntityId);
436 }
437 }
438
439 delete[] prismPts;
440 delete[] quadPts;
441 }
442 else
443 {
444 vtkErrorMacro(<<"Unsupported surface element.");
445 return 1;
446 }
447 }
448
449 if (this->IncludeSurfaceCells)
450 {
451 if (k==this->NumberOfSubLayers-1)
452 {
453 for (i=0; i<numberOfInputCells; i++)
454 {
455 input->GetCellPoints(i,npts,pts);
456 cellType = input->GetCellType(i);
457 surfacePts = new vtkIdType[npts];
458 switch(cellType)
459 {
460 case VTK_TRIANGLE:
461 boundaryLayerCellTypes->InsertNextId(VTK_TRIANGLE);
462 surfacePts[0] = pts[0] + k*numberOfLayerPoints + numberOfLayerPoints;
463 surfacePts[1] = pts[1] + k*numberOfLayerPoints + numberOfLayerPoints;
464 surfacePts[2] = pts[2] + k*numberOfLayerPoints + numberOfLayerPoints;
465 break;
466 case VTK_QUAD:
467 boundaryLayerCellTypes->InsertNextId(VTK_QUAD);
468 surfacePts[0] = pts[0] + k*numberOfLayerPoints + numberOfLayerPoints;
469 surfacePts[1] = pts[1] + k*numberOfLayerPoints + numberOfLayerPoints;
470 surfacePts[2] = pts[2] + k*numberOfLayerPoints + numberOfLayerPoints;
471 surfacePts[3] = pts[3] + k*numberOfLayerPoints + numberOfLayerPoints;
472 break;
473 case VTK_QUADRATIC_TRIANGLE:
474 boundaryLayerCellTypes->InsertNextId(VTK_QUADRATIC_TRIANGLE);
475 surfacePts[0] = pts[0] + k*numberOfLayerPoints + numberOfLayerPoints;
476 surfacePts[1] = pts[1] + k*numberOfLayerPoints + numberOfLayerPoints;
477 surfacePts[2] = pts[2] + k*numberOfLayerPoints + numberOfLayerPoints;
478 surfacePts[3] = pts[3] + k*numberOfLayerPoints + numberOfLayerPoints;
479 surfacePts[4] = pts[4] + k*numberOfLayerPoints + numberOfLayerPoints;
480 surfacePts[5] = pts[5] + k*numberOfLayerPoints + numberOfLayerPoints;
481 break;
482 default:
483 vtkErrorMacro(<<"Unsupported surface element.");
484 return 1;
485 break;
486 }
487 boundaryLayerCellArray->InsertNextCell(npts,surfacePts);
488 cellEntityIdsArray->InsertNextValue(this->OuterSurfaceCellEntityId);
489 delete[] surfacePts;
490 }
491 }
492 }
493 }
494
495 output->SetPoints(outputPoints);
496
497 int* boundaryLayerCellTypesInt = new int[boundaryLayerCellTypes->GetNumberOfIds()];
498 for (i=0; i<boundaryLayerCellTypes->GetNumberOfIds(); i++)
499 {
500 boundaryLayerCellTypesInt[i] = boundaryLayerCellTypes->GetId(i);
501 }
502
503 output->SetCells(boundaryLayerCellTypesInt,boundaryLayerCellArray);
504
505 delete[] boundaryLayerCellTypesInt;
506
507 output->GetCellData()->AddArray(cellEntityIdsArray);
508
509 if (this->InnerSurface)
510 {
511 this->InnerSurface->Delete();
512 this->InnerSurface = NULL;
513 }
514
515 this->InnerSurface = vtkUnstructuredGrid::New();
516 this->InnerSurface->DeepCopy(input);
517
518 vtkPoints* innerSurfacePoints = vtkPoints::New();
519 innerSurfacePoints->SetNumberOfPoints(numberOfInputPoints);
520 for (i=0; i<numberOfInputPoints; i++)
521 {
522 output->GetPoint(i + numberOfInputPoints * (this->NumberOfSubLayers),point);
523 innerSurfacePoints->SetPoint(i,point);
524 }
525
526 this->InnerSurface->GetPoints()->DeepCopy(innerSurfacePoints);
527 innerSurfaceCellEntityIdsArray->SetNumberOfTuples(this->InnerSurface->GetNumberOfCells());
528 innerSurfaceCellEntityIdsArray->FillComponent(0,this->InnerSurfaceCellEntityId);
529 this->InnerSurface->GetCellData()->AddArray(innerSurfaceCellEntityIdsArray);
530
531 edgePointIds->Delete();
532 edgeNeighborCellIds->Delete();
533
534 outputPoints->Delete();
535 warpedPoints->Delete();
536 boundaryLayerCellArray->Delete();
537 boundaryLayerCellTypes->Delete();
538 innerSurfacePoints->Delete();
539
540 cellEntityIdsArray->Delete();
541 innerSurfaceCellEntityIdsArray->Delete();
542 checkArray->Delete();
543
544 return 1;
545}
546
548{
549 double warpVector[3];
550 double layerThickness;
551
552 vtkIdType numberOfInputPoints = input->GetNumberOfPoints();
553
554 for (vtkIdType i=0; i<numberOfInputPoints; i++)
555 {
556 this->WarpVectorsArray->GetTuple(i,warpVector);
557 if (this->NegateWarpVectors)
558 {
559 warpVector[0] *= -1.0;
560 warpVector[1] *= -1.0;
561 warpVector[2] *= -1.0;
562 }
563 layerThickness = 0.0;
564 if (this->ConstantThickness)
565 {
566 layerThickness = this->LayerThickness;
567 }
569 {
570 layerThickness = vtkMath::Norm(warpVector);
571 }
572 else
573 {
574 layerThickness = this->LayerThicknessArray->GetComponent(i,0);
575 layerThickness *= this->LayerThicknessRatio;
576 }
577 if (layerThickness > this->MaximumLayerThickness)
578 {
579 layerThickness = this->MaximumLayerThickness;
580 }
581 vtkMath::Normalize(warpVector);
582 warpVector[0] = warpVector[0] * layerThickness;
583 warpVector[1] = warpVector[1] * layerThickness;
584 warpVector[2] = warpVector[2] * layerThickness;
585
586 this->WarpVectorsArray->SetTuple(i,warpVector);
587 }
588}
589
590void vtkvmtkBoundaryLayerGenerator::IncrementalWarpVectors(vtkUnstructuredGrid* input, int numberOfSubsteps, double relaxation)
591{
592 vtkPoints* inputPoints = input->GetPoints();
593 vtkPoints* warpedPoints = vtkPoints::New();
594 vtkIdType numberOfInputPoints = inputPoints->GetNumberOfPoints();
595
596 vtkPoints* basePoints = vtkPoints::New();
597 basePoints->DeepCopy(inputPoints);
598
599 for (int l=0; l<numberOfSubsteps; l++)
600 {
601 this->IncrementalWarpPoints(input, basePoints, warpedPoints, l, numberOfSubsteps, relaxation);
602 basePoints->DeepCopy(warpedPoints);
603 }
604
605 double warpVector[3], basePoint[3], warpedPoint[3];
606 for (vtkIdType j=0; j<numberOfInputPoints; j++)
607 {
608 inputPoints->GetPoint(j,basePoint);
609 warpedPoints->GetPoint(j,warpedPoint);
610 this->WarpVectorsArray->GetTuple(j,warpVector);
611 double layerThickness=vtkMath::Norm(warpVector);
612 warpVector[0] = warpedPoint[0] - basePoint[0];
613 warpVector[1] = warpedPoint[1] - basePoint[1];
614 warpVector[2] = warpedPoint[2] - basePoint[2];
615 vtkMath::Normalize(warpVector);
616 warpVector[0] = warpVector[0] * layerThickness;
617 warpVector[1] = warpVector[1] * layerThickness;
618 warpVector[2] = warpVector[2] * layerThickness;
619 this->WarpVectorsArray->SetTuple(j,warpVector);
620 }
621
622 warpedPoints->Delete();
623 basePoints->Delete();
624}
625
626int vtkvmtkBoundaryLayerGenerator::CheckTangle(vtkUnstructuredGrid* input, vtkUnsignedCharArray* checkArray)
627{
628 vtkIdList* pointList=vtkIdList::New();
629
630 double warpVector1[3], warpVector2[3], warpVector3[3];
631 double basePoint1[3], basePoint2[3], basePoint3[3];
632 double warpedPoint1[3], warpedPoint2[3], warpedPoint3[3];
633 double baseNormal[3], warpedNormal[3];
634
635 int found = 0;
636 for (vtkIdType j=0; j<input->GetNumberOfCells(); j++)
637 {
638 input->GetCellPoints(j,pointList);
639 //points on base triangle
640 input->GetPoint(pointList->GetId(0),basePoint1);
641 input->GetPoint(pointList->GetId(1),basePoint2);
642 input->GetPoint(pointList->GetId(2),basePoint3);
643
644 this->WarpVectorsArray->GetTuple(pointList->GetId(0),warpVector1);
645 this->WarpVectorsArray->GetTuple(pointList->GetId(1),warpVector2);
646 this->WarpVectorsArray->GetTuple(pointList->GetId(2),warpVector3);
647
648 //points on extruded triangle
649 warpedPoint1[0] = basePoint1[0] + warpVector1[0];
650 warpedPoint1[1] = basePoint1[1] + warpVector1[1];
651 warpedPoint1[2] = basePoint1[2] + warpVector1[2];
652 warpedPoint2[0] = basePoint2[0] + warpVector2[0];
653 warpedPoint2[1] = basePoint2[1] + warpVector2[1];
654 warpedPoint2[2] = basePoint2[2] + warpVector2[2];
655 warpedPoint3[0] = basePoint3[0] + warpVector3[0];
656 warpedPoint3[1] = basePoint3[1] + warpVector3[1];
657 warpedPoint3[2] = basePoint3[2] + warpVector3[2];
658
659 //normals on base triangle (baseNormal) and normals on extruded triangle (warpedNormal)
660 vtkTriangle::ComputeNormal(basePoint1,basePoint2,basePoint3,baseNormal);
661 vtkTriangle::ComputeNormal(warpedPoint1,warpedPoint2,warpedPoint3,warpedNormal);
662 double prod = vtkMath::Dot(baseNormal,warpedNormal);
663 double baseArea = vtkTriangle::TriangleArea(basePoint1,basePoint2,basePoint3);
664 double warpedArea = vtkTriangle::TriangleArea(warpedPoint1,warpedPoint2,warpedPoint3);
665 double testArea = warpedArea / baseArea;
666 if (prod < 0 || testArea <= 0.1 )
667 {
668 found = found + 1;
669 checkArray->SetValue(j,1);
670 }
671 else
672 {
673 checkArray->SetValue(j,0);
674 }
675 }
676 pointList->Delete();
677 return found;
678}
679
680void vtkvmtkBoundaryLayerGenerator::LocalUntangle(vtkUnstructuredGrid* input, vtkUnsignedCharArray* checkArray, double alpha)
681{
682 vtkIdList* pointList=vtkIdList::New();
683 vtkIdList* pointList2=vtkIdList::New();
684 vtkIdList* cellList=vtkIdList::New();
685
686 double warpVector1[3], warpVector2[3], warpVector3[3];
687 double w1s[3], w2s[3], w3s[3], n[3];
688 double point1[3], point2[3], point3[3];
689 double c1[3], c2[3], c3[3];
690
691 vtkDoubleArray* correctionArray = vtkDoubleArray::New();
692 correctionArray->SetName("correctionVectors");
693 correctionArray->SetNumberOfComponents(3);
694 correctionArray->SetNumberOfTuples(input->GetNumberOfPoints());
695 correctionArray->FillComponent(0,0.0);
696 correctionArray->FillComponent(1,0.0);
697 correctionArray->FillComponent(2,0.0);
698 input->GetPointData()->AddArray(correctionArray);
699
700 for (vtkIdType j=0; j<input->GetNumberOfCells(); j++)
701 {
702 if (checkArray->GetValue(j)==1)
703 {
704 input->GetCellPoints(j,pointList);
705 this->WarpVectorsArray->GetTuple(pointList->GetId(0),warpVector1);
706 this->WarpVectorsArray->GetTuple(pointList->GetId(1),warpVector2);
707 this->WarpVectorsArray->GetTuple(pointList->GetId(2),warpVector3);
708 //eventually we can do it iteratively (relaxation >1)
709 double relaxation = 1.0;
710 for (int r=0; r<relaxation; r++)
711 {
712 w1s[0] = w1s[1] = w1s[2] = 0.0;
713 w2s[0] = w2s[1] = w2s[2] = 0.0;
714 w3s[0] = w3s[1] = w3s[2] = 0.0;
715 n[0] = n[1] = n[2] = 0.0;
716 input->GetPointCells(pointList->GetId(0),cellList);
717 for (int i=0; i<cellList->GetNumberOfIds();i++)
718 {
719 input->GetCellPoints(cellList->GetId(i),pointList2);
720 input->GetPoint(pointList2->GetId(0),point1);
721 input->GetPoint(pointList2->GetId(1),point2);
722 input->GetPoint(pointList2->GetId(2),point3);
723 vtkTriangle::ComputeNormal(point1,point2,point3,n);
724 w1s[0] += warpVector1[0] - vtkMath::Dot(warpVector1,n) * n[0];
725 w1s[1] += warpVector1[1] - vtkMath::Dot(warpVector1,n) * n[1];
726 w1s[2] += warpVector1[2] - vtkMath::Dot(warpVector1,n) * n[2];
727 }
728 vtkMath::Normalize(w1s);
729 input->GetPointCells(pointList->GetId(1),cellList);
730 for (int i=0; i<cellList->GetNumberOfIds();i++)
731 {
732 input->GetCellPoints(cellList->GetId(i),pointList2);
733 input->GetPoint(pointList2->GetId(0),point1);
734 input->GetPoint(pointList2->GetId(1),point2);
735 input->GetPoint(pointList2->GetId(2),point3);
736 vtkTriangle::ComputeNormal(point1,point2,point3,n);
737 w2s[0] += warpVector2[0] - vtkMath::Dot(warpVector2,n) * n[0];
738 w2s[1] += warpVector2[1] - vtkMath::Dot(warpVector2,n) * n[1];
739 w2s[2] += warpVector2[2] - vtkMath::Dot(warpVector2,n) * n[2];
740 }
741 vtkMath::Normalize(w2s);
742 input->GetPointCells(pointList->GetId(2),cellList);
743 for (int i=0; i<cellList->GetNumberOfIds();i++)
744 {
745 input->GetCellPoints(cellList->GetId(i),pointList2);
746 input->GetPoint(pointList2->GetId(0),point1);
747 input->GetPoint(pointList2->GetId(1),point2);
748 input->GetPoint(pointList2->GetId(2),point3);
749 vtkTriangle::ComputeNormal(point1,point2,point3,n);
750 w3s[0] += warpVector3[0] - vtkMath::Dot(warpVector3,n) * n[0];
751 w3s[1] += warpVector3[1] - vtkMath::Dot(warpVector3,n) * n[1];
752 w3s[2] += warpVector3[2] - vtkMath::Dot(warpVector3,n) * n[2];
753 }
754 vtkMath::Normalize(w3s);
755 correctionArray->GetTuple(pointList->GetId(0),c1);
756 correctionArray->GetTuple(pointList->GetId(1),c2);
757 correctionArray->GetTuple(pointList->GetId(2),c3);
758 c1[0] = c1[0] + alpha * w2s[0] + alpha * w3s[0];
759 c1[1] = c1[1] + alpha * w2s[1] + alpha * w3s[1];
760 c1[2] = c1[2] + alpha * w2s[2] + alpha * w3s[2];
761 c2[0] = c2[0] + alpha * w1s[0] + alpha * w3s[0];
762 c2[1] = c2[1] + alpha * w1s[1] + alpha * w3s[1];
763 c2[2] = c2[2] + alpha * w1s[2] + alpha * w3s[2];
764 c3[0] = c3[0] + alpha * w2s[0] + alpha * w1s[0];
765 c3[1] = c3[1] + alpha * w2s[1] + alpha * w1s[1];
766 c3[2] = c3[2] + alpha * w2s[2] + alpha * w1s[2];
767 correctionArray->SetTuple3(pointList->GetId(0),c1[0],c1[1],c1[2]);
768 correctionArray->SetTuple3(pointList->GetId(1),c2[0],c2[1],c2[2]);
769 correctionArray->SetTuple3(pointList->GetId(2),c3[0],c3[1],c3[2]);
770 }
771 }
772 }
773
774 double warp[3], correction[3];
775 for (vtkIdType i=0; i<input->GetNumberOfPoints();i++)
776 {
777 this->WarpVectorsArray->GetTuple(i,warp);
778 double layerThickness=vtkMath::Norm(warp);
779 correctionArray->GetTuple(i,correction);
780 warp[0] = warp[0] + correction[0];
781 warp[1] = warp[1] + correction[1];
782 warp[2] = warp[2] + correction[2];
783 vtkMath::Normalize(warp);
784 warp[0] = warp[0] * layerThickness;
785 warp[1] = warp[1] * layerThickness;
786 warp[2] = warp[2] * layerThickness;
787 this->WarpVectorsArray->SetTuple(i,warp);
788 }
789
790 correctionArray->Delete();
791 pointList->Delete();
792 pointList2->Delete();
793 cellList->Delete();
794}
795
796void vtkvmtkBoundaryLayerGenerator::WarpPoints(vtkPoints* inputPoints, vtkPoints* warpedPoints, int subLayerId, bool quadratic)
797{
798 double point[3], warpedPoint[3], warpVector[3];
799 double layerThickness, subLayerThicknessRatio, subLayerThickness;
800 double totalLayerZeroSubLayerRatio, subLayerOffsetRatio, subLayerOffset;
801
802 vtkIdType numberOfInputPoints = inputPoints->GetNumberOfPoints();
803
804 totalLayerZeroSubLayerRatio = 0.0;
805 int i;
806 for (i=0; i<this->NumberOfSubLayers; i++)
807 {
808 totalLayerZeroSubLayerRatio += pow(this->SubLayerRatio,this->NumberOfSubLayers-i-1);
809 }
810
811 subLayerOffsetRatio = 0.0;
812 for (i=0; i<subLayerId; i++)
813 {
814 subLayerOffsetRatio += pow(this->SubLayerRatio,this->NumberOfSubLayers-i-1);
815 }
816 subLayerOffsetRatio /= totalLayerZeroSubLayerRatio;
817
818 subLayerThicknessRatio = pow(this->SubLayerRatio,this->NumberOfSubLayers-subLayerId-1) / totalLayerZeroSubLayerRatio;
819
820 if (!quadratic)
821 {
822 warpedPoints->SetNumberOfPoints(numberOfInputPoints);
823 }
824 else
825 {
826 warpedPoints->SetNumberOfPoints(2*numberOfInputPoints);
827 }
828
829 for (i=0; i<numberOfInputPoints; i++)
830 {
831 inputPoints->GetPoint(i,point);
832 this->WarpVectorsArray->GetTuple(i,warpVector);
833
834 layerThickness = vtkMath::Norm(warpVector);
835
836 vtkMath::Normalize(warpVector);
837
838 subLayerOffset = subLayerOffsetRatio * layerThickness;
839 subLayerThickness = subLayerThicknessRatio * layerThickness;
840
841 if (quadratic)
842 {
843 warpedPoint[0] = point[0] + 0.5 * warpVector[0] * (subLayerOffset + subLayerThickness);
844 warpedPoint[1] = point[1] + 0.5 * warpVector[1] * (subLayerOffset + subLayerThickness);
845 warpedPoint[2] = point[2] + 0.5 * warpVector[2] * (subLayerOffset + subLayerThickness);
846 warpedPoints->SetPoint(i,warpedPoint);
847 warpedPoint[0] = point[0] + warpVector[0] * (subLayerOffset + subLayerThickness);
848 warpedPoint[1] = point[1] + warpVector[1] * (subLayerOffset + subLayerThickness);
849 warpedPoint[2] = point[2] + warpVector[2] * (subLayerOffset + subLayerThickness);
850 warpedPoints->SetPoint(i+numberOfInputPoints,warpedPoint);
851 }
852 else
853 {
854 warpedPoint[0] = point[0] + warpVector[0] * (subLayerOffset + subLayerThickness);
855 warpedPoint[1] = point[1] + warpVector[1] * (subLayerOffset + subLayerThickness);
856 warpedPoint[2] = point[2] + warpVector[2] * (subLayerOffset + subLayerThickness);
857 warpedPoints->SetPoint(i,warpedPoint);
858 }
859 }
860}
861
862void vtkvmtkBoundaryLayerGenerator::IncrementalWarpPoints(vtkUnstructuredGrid* input, vtkPoints* basePoints, vtkPoints* warpedPoints, int substep, int numberOfSubsteps, double relaxation)
863{
864 double point[3], warpedPoint[3], warpVector[3];
865 double layerThickness;
866
867 vtkIdType numberOfInputPoints = basePoints->GetNumberOfPoints();
868
869 warpedPoints->SetNumberOfPoints(numberOfInputPoints);
870
871 for (vtkIdType i=0; i<numberOfInputPoints; i++)
872 {
873 basePoints->GetPoint(i,point);
874 this->WarpVectorsArray->GetTuple(i,warpVector);
875
876 layerThickness = vtkMath::Norm(warpVector);
877 vtkMath::Normalize(warpVector);
878
879 layerThickness /= numberOfSubsteps;
880
881 warpedPoint[0] = point[0] + warpVector[0] * layerThickness;
882 warpedPoint[1] = point[1] + warpVector[1] * layerThickness;
883 warpedPoint[2] = point[2] + warpVector[2] * layerThickness;
884 warpedPoints->SetPoint(i,warpedPoint);
885 }
886
887 vtkIdType npts;
888 const vtkIdType *pts;
889 vtkIdList* cellIds = vtkIdList::New();
890 vtkIdType cellId;
891
892 vtkIdList* neighborIds = vtkIdList::New();
893 vtkIdList* edgePointIds = vtkIdList::New();
894 vtkIdList* edgeNeighborCellIds = vtkIdList::New();
895
896 for (vtkIdType j=0; j<numberOfInputPoints; j++)
897 {
898 input->GetPointCells(j,cellIds);
899
900 neighborIds->Initialize();
901
902 vtkIdType numberOfNeighborCells = cellIds->GetNumberOfIds();
903 for (int k=0; k<numberOfNeighborCells; k++)
904 {
905 cellId = cellIds->GetId(k);
906 input->GetCellPoints(cellId,npts,pts);
907
908 neighborIds->InsertUniqueId(pts[0]);
909 neighborIds->InsertUniqueId(pts[1]);
910 neighborIds->InsertUniqueId(pts[2]);
911 }
912 neighborIds->DeleteId(j);
913
914 int numberOfNeighbors = neighborIds->GetNumberOfIds();
915
916 bool onEdge = false;
917 edgePointIds->Initialize();
918 edgeNeighborCellIds->Initialize();
919 edgePointIds->InsertId(0,j);
920 for (int k=0; k<numberOfNeighbors; k++)
921 {
922 edgePointIds->InsertId(1,neighborIds->GetId(k));
923 input->GetCellNeighbors(-1,edgePointIds,edgeNeighborCellIds);
924 if (edgeNeighborCellIds->GetNumberOfIds() < 2)
925 {
926 onEdge = true;
927 break;
928 }
929 }
930
931 if (onEdge)
932 {
933 continue;
934 }
935
936 double barycenter[3];
937 barycenter[0] = barycenter[1] = barycenter[2] = 0.0;
938 double neighborPoint[3];
939 for (int k=0; k<numberOfNeighbors; k++)
940 {
941 warpedPoints->GetPoint(neighborIds->GetId(k),neighborPoint);
942 barycenter[0] += neighborPoint[0];
943 barycenter[1] += neighborPoint[1];
944 barycenter[2] += neighborPoint[2];
945 }
946 barycenter[0] /= numberOfNeighbors;
947 barycenter[1] /= numberOfNeighbors;
948 barycenter[2] /= numberOfNeighbors;
949
950 warpedPoints->GetPoint(j,warpedPoint);
951
952 // TODO: find out if the current surface is intersecting the original
953 // input surface (not the input surface at this iteration) and in that
954 // case (before it gets too close) stop the warp
955
956 warpedPoint[0] += relaxation * (barycenter[0] - warpedPoint[0]);
957 warpedPoint[1] += relaxation * (barycenter[1] - warpedPoint[1]);
958 warpedPoint[2] += relaxation * (barycenter[2] - warpedPoint[2]);
959
960 warpedPoints->SetPoint(j,warpedPoint);
961 }
962
963 cellIds->Delete();
964 neighborIds->Delete();
965 edgePointIds->Delete();
966 edgeNeighborCellIds->Delete();
967}
968
969void vtkvmtkBoundaryLayerGenerator::PrintSelf(std::ostream& os, vtkIndent indent)
970{
971 this->Superclass::PrintSelf(os,indent);
972}
Generates boundary layers of prismatic elements by warping a surface mesh.
void WarpPoints(vtkPoints *inputPoints, vtkPoints *warpedPoints, int subLayerId, bool quadratic)
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void LocalUntangle(vtkUnstructuredGrid *input, vtkUnsignedCharArray *checkArray, double alpha)
int CheckTangle(vtkUnstructuredGrid *input, vtkUnsignedCharArray *checkArray)
void PrintSelf(std::ostream &os, vtkIndent indent) override
void BuildWarpVectors(vtkUnstructuredGrid *input)
void IncrementalWarpPoints(vtkUnstructuredGrid *input, vtkPoints *basePoints, vtkPoints *warpedPoints, int substep, int numberOfSubsteps, double relaxation)
void IncrementalWarpVectors(vtkUnstructuredGrid *input, int numberOfSubsteps, double relaxation)
vtkStandardNewMacro(vtkvmtkBoundaryLayerGenerator)
#define VTK_VMTK_LARGE_DOUBLE