File: vtkIconGlyphFilter.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 125,776 kB
  • sloc: cpp: 1,539,582; ansic: 106,521; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 122; objc: 83
file content (249 lines) | stat: -rw-r--r-- 7,957 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkIconGlyphFilter.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 "vtkIconGlyphFilter.h"

#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkFloatArray.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPointLocator.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"

vtkStandardNewMacro(vtkIconGlyphFilter);

//-----------------------------------------------------------------------------
vtkIconGlyphFilter::vtkIconGlyphFilter()
{
  this->IconSize[0] = 1;
  this->IconSize[1] = 1;
  this->IconSheetSize[0] = 1;
  this->IconSheetSize[1] = 1;
  this->DisplaySize[0] = 25;
  this->DisplaySize[1] = 25;
  this->Gravity = VTK_ICON_GRAVITY_CENTER_CENTER;
  this->UseIconSize = true;
  this->PassScalars = false;
  this->IconScaling = VTK_ICON_SCALING_OFF;
  this->Offset[0] = this->Offset[1] = 0;

  this->SetInputArrayToProcess(0, 0, 0,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, vtkDataSetAttributes::SCALARS);
}

//-----------------------------------------------------------------------------
vtkIconGlyphFilter::~vtkIconGlyphFilter()
{
}

//-----------------------------------------------------------------------------
void vtkIconGlyphFilter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "Icon Size: " << this->IconSize[0] << " " << this->IconSize[1] << endl;
  os << indent << "Icon Sheet Size: " << this->IconSheetSize[0] << " " << this->IconSheetSize[1] << endl;
  os << indent << "Display Size: " << this->DisplaySize[0] << " " << this->DisplaySize[1] << endl;
  os << indent << "Offset: " << this->Offset[0] << " " << this->Offset[1] << endl;
  os << indent << "Gravity: " << this->Gravity << "\n";
  os << indent << "Use Icon Size: "
     << (this->UseIconSize ? " On" : " Off") << endl;
  os << indent << "Pass Scalars: "
     << (this->PassScalars ? " On" : " Off") << endl;
  os << indent << "Icon Scaling: " << this->IconScaling << endl;
}

//-----------------------------------------------------------------------------
int vtkIconGlyphFilter::RequestData(vtkInformation *vtkNotUsed(request),
                                  vtkInformationVector **inputVector,
                                  vtkInformationVector *outputVector)
{
  // Get the information object.
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  // Get the data objects.
  vtkPointSet *input = vtkPointSet::SafeDownCast(
                                     inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkPolyData *output = vtkPolyData::SafeDownCast(
                                    outInfo->Get(vtkDataObject::DATA_OBJECT()));

  int numPoints = input->GetNumberOfPoints();

  if (numPoints <= 0)
  {
    // nothing to do...
    return 1;
  }

  vtkIntArray* scalars = vtkArrayDownCast<vtkIntArray>(
    this->GetInputArrayToProcess(0, inputVector));
  if (!scalars)
  {
    vtkErrorMacro("Input Scalars must be specified to index into the icon sheet.");
    return 0;
  }

  // Optional scaling may be going on
  vtkDataArray *scalingArray = NULL;
  if ( this->IconScaling == VTK_ICON_SCALING_USE_SCALING_ARRAY )
  {
    scalingArray = input->GetPointData()->GetArray("IconScale");
  }

  double point[3], textureCoord[2];
  point[2] = 0;
  double sheetXDim = this->IconSheetSize[0]/this->IconSize[0];
  double sheetYDim = this->IconSheetSize[1]/this->IconSize[1];
  int iconIndex = 0;
  int j, k;

  vtkPoints * outPoints = vtkPoints::New();
  outPoints->Allocate(4 * numPoints);

  vtkCellArray * outCells = vtkCellArray::New();
  outCells->Allocate(outCells->EstimateSize(numPoints, 4));

  vtkFloatArray *outTCoords = vtkFloatArray::New();
  outTCoords->SetNumberOfComponents(2);
  outTCoords->Allocate(8*numPoints);

  // Copy point data to cell data
  vtkPointData *inPD = input->GetPointData();
  vtkCellData  *outCD = output->GetCellData();

  double size[2];
  size[0] = this->DisplaySize[0];
  size[1] = this->DisplaySize[1];
  if(this->UseIconSize)
  {
    size[0] = this->IconSize[0];
    size[1] = this->IconSize[1];
  }

  vtkIdType ptId;
  double sf = 1.0;
  for(ptId = 0; ptId < numPoints; ++ptId)
  {
    iconIndex = scalars->GetValue(ptId);

    if(iconIndex >= 0)
    {
      this->IconConvertIndex(iconIndex, j, k);

      textureCoord[0] = j/sheetXDim;
      textureCoord[1] = k/sheetYDim;
      outTCoords->InsertTuple(ptId * 4, textureCoord);

      textureCoord[0] = (j + 1.0)/sheetXDim;
      textureCoord[1] = k/sheetYDim;
      outTCoords->InsertTuple(ptId * 4 + 1, textureCoord);

      textureCoord[0] = (j + 1.0)/sheetXDim;
      textureCoord[1] = (k + 1.0)/sheetYDim;
      outTCoords->InsertTuple(ptId * 4 + 2, textureCoord);

      textureCoord[0] = j/sheetXDim;
      textureCoord[1] = (k + 1.0)/sheetYDim;
      outTCoords->InsertTuple(ptId * 4 + 3, textureCoord);
    }

    input->GetPoint(ptId, point);
    point[0] += this->Offset[0];
    point[1] += this->Offset[1];

    if ( scalingArray )
    {
      sf = scalingArray->GetTuple1(ptId);
    }

    switch(this->Gravity)
    {
      case VTK_ICON_GRAVITY_CENTER_CENTER:
        break;
      case VTK_ICON_GRAVITY_TOP_RIGHT:
        point[0] = point[0] + 0.5 * sf * size[0];
        point[1] = point[1] + 0.5 * sf * size[1];
        break;
      case VTK_ICON_GRAVITY_TOP_CENTER:
        point[1] = point[1] + 0.5 * sf * size[1];
        break;
      case VTK_ICON_GRAVITY_TOP_LEFT:
        point[0] = point[0] - 0.5 * sf * size[0];
        point[1] = point[1] + 0.5 * sf * size[1];
        break;
      case VTK_ICON_GRAVITY_CENTER_RIGHT:
        point[0] = point[0] + 0.5 * sf * size[0];
        break;
      case VTK_ICON_GRAVITY_CENTER_LEFT:
        point[0] = point[0] - 0.5 * sf * size[0];
        break;
      case VTK_ICON_GRAVITY_BOTTOM_RIGHT:
        point[0] = point[0] + 0.5 * sf * size[0];
        point[1] = point[1] - 0.5 * sf * size[1];
        break;
      case VTK_ICON_GRAVITY_BOTTOM_CENTER:
        point[1] = point[1] - 0.5 * sf * size[1];
        break;
      case VTK_ICON_GRAVITY_BOTTOM_LEFT:
        point[0] = point[0] - 0.5 * sf * size[0];
        point[1] = point[1] - 0.5 * sf * size[1];
        break;
    }

    outPoints->InsertNextPoint(point[0] - 0.5 * sf * size[0], point[1] - 0.5 * sf * size[1], point[2]);
    outPoints->InsertNextPoint(point[0] + 0.5 * sf * size[0], point[1] - 0.5 * sf * size[1], point[2]);
    outPoints->InsertNextPoint(point[0] + 0.5 * sf * size[0], point[1] + 0.5 * sf * size[1], point[2]);
    outPoints->InsertNextPoint(point[0] - 0.5 * sf * size[0], point[1] + 0.5 * sf * size[1], point[2]);

    outCells->InsertNextCell(4);
    outCells->InsertCellPoint(ptId * 4);
    outCells->InsertCellPoint(ptId * 4 + 1);
    outCells->InsertCellPoint(ptId * 4 + 2);
    outCells->InsertCellPoint(ptId * 4 + 3);
  }

  output->SetPoints(outPoints);
  outPoints->Delete();

  outTCoords->SetName("TextureCoordinates");
  output->GetPointData()->SetTCoords(outTCoords);
  outTCoords->Delete();

  output->SetPolys(outCells);
  outCells->Delete();

  // Pass the input point data to the cell data because for every point we
  // generate a quad cell.
  if ( this->PassScalars )
  {
    outCD->CopyScalarsOn();
  }
  else
  {
    outCD->CopyScalarsOff();
  }
  outCD->PassData(inPD);

  return 1;
}