File: TestNamedComponents.cxx

package info (click to toggle)
vtk 5.8.0-13
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 130,524 kB
  • sloc: cpp: 1,129,256; ansic: 708,203; tcl: 48,526; python: 20,875; xml: 6,779; yacc: 4,208; perl: 3,121; java: 2,788; lex: 931; sh: 660; asm: 471; makefile: 299
file content (179 lines) | stat: -rw-r--r-- 5,439 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestNamedComponents.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkCellData.h"
#include "vtkIdTypeArray.h"
#include "vtkIntArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkSmartPointer.h"
#include "vtkThreshold.h"
#include "vtkUnstructuredGrid.h"
#include "vtkArrayCalculator.h"

int TestNamedComponents(int , char *[])
{
  int rval = 0;

  vtkIdType numPoints = 20;
  vtkIdType numVerts = 5;
  vtkIdType numLines = 8;
  vtkIdType numTriangles = 3;
  vtkIdType numStrips = 2;
  vtkIdType numCells = numVerts+numLines+numTriangles+numStrips;
  vtkIdType i;


  vtkIdTypeArray* pointCoords = vtkIdTypeArray::New();
  const char pcName[] = "point coords";
  pointCoords->SetName(pcName);
  pointCoords->SetNumberOfComponents(3); // num points + point ids
  pointCoords->SetNumberOfTuples(numPoints);    
  pointCoords->SetComponentName(0,"XLOC");
  pointCoords->SetComponentName(1,"YLOC");
  pointCoords->SetComponentName(2,"ZLOC");

  vtkPoints* points = vtkPoints::New();
  points->SetNumberOfPoints(numPoints);
  for(i=0;i<numPoints;i++)
    {
    double loc[3] = {i, i*i, 0};
    points->InsertPoint(i, loc);
    pointCoords->InsertTuple(i,loc);
    }
  vtkSmartPointer<vtkPolyData> poly = vtkSmartPointer<vtkPolyData>::New();
  poly->Allocate(numCells, numCells);
  poly->SetPoints(points);
  poly->GetPointData()->AddArray( pointCoords );
  pointCoords->Delete();
  points->Delete();

  for(i=0;i<numVerts;i++)
    {
    poly->InsertNextCell(VTK_VERTEX, 1, &i);
    }

  for(i=0;i<numLines;i++)
    {
    vtkIdType pts[2] = {i, i+1};
    poly->InsertNextCell(VTK_LINE, 2, pts);
    }

  for(i=0;i<numTriangles;i++)
    {
    vtkIdType pts[3] = {0, i+1, i+2};
    poly->InsertNextCell(VTK_TRIANGLE, 3, pts);
    }

  for(i=0;i<numStrips;i++)
    {
    vtkIdType pts[3] = {0, i+1, i+2};
    poly->InsertNextCell(VTK_TRIANGLE_STRIP, 3, pts);
    }

  vtkIntArray* cellIndex = vtkIntArray::New();
  const char ctName[] = "scalars";
  cellIndex->SetName(ctName);
  cellIndex->SetNumberOfComponents(1);
  cellIndex->SetNumberOfTuples(numCells);
  cellIndex->SetComponentName(0,"index");
  for(i=0;i<numCells;i++)
    {
    cellIndex->SetValue(i, i);
    }
  poly->GetCellData()->SetScalars( cellIndex );
  cellIndex->Delete();

  vtkIdTypeArray* cellPoints = vtkIdTypeArray::New();
  const char cpName[] = "cell points";
  cellPoints->SetName(cpName);
  cellPoints->SetNumberOfComponents(4); // num points + point ids
  cellPoints->SetNumberOfTuples(numCells);
  
  cellPoints->SetComponentName(0,"NumberOfPoints");
  cellPoints->SetComponentName(1,"X_ID");
  cellPoints->SetComponentName(2,"Y_ID");
  cellPoints->SetComponentName(3,"Z_ID");

  for(i=0;i<numCells;i++)
    {
    vtkIdType npts, *pts;
    poly->GetCellPoints(i, npts, pts);
    vtkIdType data[4] = {npts, pts[0], 0, 0};
    for(vtkIdType j=1;j<npts;j++)
      {
      data[j+1] = pts[j];
      }
    cellPoints->SetTupleValue(i, data);
    }
  
  poly->GetCellData()->AddArray(cellPoints);
  cellPoints->Delete();
 
  poly->BuildCells();
  
  vtkSmartPointer<vtkThreshold> thresh = vtkSmartPointer<vtkThreshold>::New();  
  thresh->SetInput(poly);
  thresh->SetInputArrayToProcess(0, 0, 0,
                                   vtkDataObject::FIELD_ASSOCIATION_CELLS,
                                   vtkDataSetAttributes::SCALARS);
  
  thresh->ThresholdBetween(0, 10);
  thresh->Update();
  
  vtkSmartPointer<vtkUnstructuredGrid> out = thresh->GetOutput();
  
  if ( out == NULL )
    {
    vtkGenericWarningMacro("threshold failed.");
    return 1;
    }

  // the arrays should have been changed so get them again...
  cellIndex = vtkIntArray::SafeDownCast(out->GetCellData()->GetArray(ctName));
  cellPoints = vtkIdTypeArray::SafeDownCast(out->GetCellData()->GetArray(cpName));

  //confirm component names are intact
  if ( strcmp(cellIndex->GetComponentName(0),"index") != 0 )
    {
    vtkGenericWarningMacro("threshold failed to mantain component name on cell scalars.");
    return 1;
    }
  
  if ( strcmp(cellPoints->GetComponentName(0),"NumberOfPoints")  != 0 ||
       strcmp(cellPoints->GetComponentName(1),"X_ID")  != 0 ||
       strcmp(cellPoints->GetComponentName(2),"Y_ID")  != 0 ||
       strcmp(cellPoints->GetComponentName(3),"Z_ID")  != 0)
    {
    vtkGenericWarningMacro("threshold failed to mantain component names on point property.");
    return 1;
    }

  //Test component names with the calculator
  vtkSmartPointer<vtkArrayCalculator> calc = vtkSmartPointer<vtkArrayCalculator>::New();  
  calc->SetInput( poly );
  calc->SetAttributeModeToUsePointData();
  // Add coordinate scalar and vector variables
  calc->AddCoordinateScalarVariable( "coordsX", 0 );    
  calc->AddScalarVariable("point coords_YLOC","point coords",1 );
  calc->SetFunction("coordsX + point coords_YLOC");
  calc->SetResultArrayName("Result");
  calc->Update();

  
  return rval;
}