File: vtkArcPlotter.cxx

package info (click to toggle)
paraview 4.0.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 526,572 kB
  • sloc: cpp: 2,284,430; ansic: 816,374; python: 239,936; xml: 70,162; tcl: 48,295; fortran: 39,116; yacc: 5,466; java: 3,518; perl: 3,107; lex: 1,620; sh: 1,555; makefile: 932; asm: 471; pascal: 228
file content (459 lines) | stat: -rw-r--r-- 11,824 bytes parent folder | download | duplicates (9)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*=========================================================================

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

#include "vtkCamera.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkMath.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPlane.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"

vtkStandardNewMacro(vtkArcPlotter);

vtkCxxSetObjectMacro(vtkArcPlotter,Camera,vtkCamera);

vtkArcPlotter::vtkArcPlotter()
{
  this->Camera = NULL;
  this->PlotMode = VTK_PLOT_SCALARS;
  this->PlotComponent = (-1); //plot all components
  this->Radius = 0.5;
  this->Height = 0.5;
  this->Offset = 0.0;

  this->DefaultNormal[0] = this->DefaultNormal[1] = 0.0;
  this->DefaultNormal[2] = 1.0;
  this->UseDefaultNormal = 0;

  this->FieldDataArray = 0;

  this->DataRange = NULL;
  this->Tuple = NULL;
  this->ActiveComponent = 0;
  this->NumberOfComponents = 0;
}

vtkArcPlotter::~vtkArcPlotter()
{
  if ( this->DataRange )
    {
    delete [] this->DataRange;
    delete [] this->Tuple;
    }
  if ( this->Camera )
    {
    this->Camera->UnRegister (this);
    this->Camera = NULL;
    }
}

int vtkArcPlotter::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
{
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  // get the input and output
  vtkPolyData *input = vtkPolyData::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkPolyData *output = vtkPolyData::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

  vtkPointData *inPD;
  vtkPoints *inPts;
  vtkCellArray *inLines;
  int j;
  vtkIdType numPts, i;
  double x[3], normal[3], point[3], aveNormal[3];
  int id;
  vtkIdType *pts = 0;
  vtkIdType npts = 0;
  double x1[3], x2[3], x21[3], n[3];
  vtkFloatArray *lineNormals;
  vtkPoints *newPts;
  vtkCellArray *newLines;
  double *range, offset;
  int plotNum, compNum;
  vtkPoints *projPts;

  inPD = input->GetPointData();

  // Initialize
  //
  vtkDebugMacro(<<"Plotting along arc");

  if ( !(inPts=input->GetPoints()) ||
  (numPts=inPts->GetNumberOfPoints()) < 1 ||
  !(inLines=input->GetLines()) || inLines->GetNumberOfCells() < 1 )
    {
    vtkErrorMacro(<< "No input data!");
    return 0;
    }

  // Process attribute data to determine ranges, number of components, etc.
  if ( this->ProcessComponents(numPts, inPD) <= 0 )
    {
    return 0;
    }

  // Allocate points for projection
  //
  // Determine the projection plane. Project to a plane if camera is available
  // and defulat normal is not desired.
  if ( this->Camera && ! this->UseDefaultNormal )
    {
    double xProj[3];
    projPts = vtkPoints::New();
    projPts->SetNumberOfPoints(numPts);
    this->Camera->GetViewPlaneNormal(normal);
    this->Camera->GetFocalPoint(point);
    vtkMath::Normalize(normal);
    for ( i=0; i < numPts; i++ )
      {
      inPts->GetPoint(i, x);
      vtkPlane::ProjectPoint(x, point, normal, xProj);
      projPts->SetPoint(i,xProj);
      }
    }
  else
    {
    normal[0] = this->DefaultNormal[0];
    normal[1] = this->DefaultNormal[1];
    normal[2] = this->DefaultNormal[2];
    vtkMath::Normalize(normal);
    projPts = inPts; //use existing points
    }

  // For each polyline, compute a normal that lies in the
  // projection plane and is roughly perpendicular to the projected
  // polyline. Then generate the arc.
  //
  newPts = vtkPoints::New();
  newPts->Allocate(numPts,numPts);
  lineNormals = vtkFloatArray::New();
  lineNormals->SetNumberOfComponents(3);

  newLines = vtkCellArray::New();
  newLines->Allocate(inLines->GetSize());

  for (inLines->InitTraversal(); inLines->GetNextCell(npts,pts); )
    {
    lineNormals->SetNumberOfTuples(npts);
    if ( !this->Camera || this->UseDefaultNormal )
      {//use default normal
      for (i=0; i < npts; i++)
        {
        lineNormals->SetTuple(i,normal);
        }
      }
    else //generate normals
      {
      // Compute normals on each line segment perpendicular to view normal
      for (i=0; i < (npts-1); i++)
        {
        projPts->GetPoint(pts[i], x1);
        projPts->GetPoint(pts[i+1], x2);
        for (j=0; j<3; j++)
          {
          x21[j] = x2[j] - x1[j];
          }
        vtkMath::Cross(normal,x21,n);
        vtkMath::Normalize(n);
        lineNormals->SetTuple(i,n);
        }
      lineNormals->SetTuple(npts-1,n);
      }

    // Now average the normal calculation to get smoother results
    //
    vtkIdType window = npts / 100;
    if ( window < 5 )
      {
      window = 5;
      }
    // Start by computing an initial average normal
    aveNormal[0] = aveNormal[1] = aveNormal[2] = 0.0;
    for (i=0; i < npts && i < window; i++)
      {
      lineNormals->GetTuple(i,n);
      aveNormal[0] += n[0]; aveNormal[1] += n[1]; aveNormal[2] += n[2];
      }

    for (i=0; i < npts; i++)
      {
      if ( (i+window) < npts )
        {
        lineNormals->GetTuple(i+window,n);
        aveNormal[0] += n[0]; aveNormal[1] += n[1]; aveNormal[2] += n[2];
        }
      if ( (i-window) >= 0 )
        {
        lineNormals->GetTuple(i-window,n);
        aveNormal[0] -= n[0]; aveNormal[1] -= n[1]; aveNormal[2] -= n[2];
        }
      n[0] = aveNormal[0]; n[1] = aveNormal[1]; n[2] = aveNormal[2];
      vtkMath::Normalize(n);
      lineNormals->SetTuple(i, n);
      }
    this->UpdateProgress(0.50);

    // For each component, create an offset plot.
    for (plotNum=0, compNum=this->StartComp; compNum <= this->EndComp;
         compNum++, plotNum++)
      {
      offset = this->Radius + plotNum*this->Offset;
      range = this->DataRange + 2*compNum;

      newLines->InsertNextCell(npts);

      // Continue average normal computation using sliding window
      for (i=0; i < npts; i++)
        {
        this->Data->GetTuple(pts[i], this->Tuple);
        lineNormals->GetTuple(i,n);
        id = this->OffsetPoint(pts[i], inPts, n, newPts,
                               offset, range, this->Tuple[compNum]);
        newLines->InsertCellPoint(id);
        }
      } //for all components
    } //for all polylines
  this->UpdateProgress(0.90);

  lineNormals->Delete();
  if ( projPts != inPts )
    {
    projPts->Delete();
    }

  // Update output
  output->SetPoints(newPts);
  newPts->Delete();
  output->SetLines(newLines);
  newLines->Delete();

  return 1;
}

int vtkArcPlotter::ProcessComponents(vtkIdType numPts, vtkPointData *pd)
{
  vtkIdType i;
  int j;
  double *range;

  this->Data = NULL;
  switch (this->PlotMode)
    {
    case VTK_PLOT_SCALARS:
      if ( pd->GetScalars() )
        {
        this->Data = pd->GetScalars();
        }
      break;
    case VTK_PLOT_VECTORS:
      if ( pd->GetVectors() )
        {
        this->Data = pd->GetVectors();
        }
      break;
    case VTK_PLOT_NORMALS:
      if ( pd->GetNormals() )
        {
        this->Data = pd->GetNormals();
        }
      break;
    case VTK_PLOT_TCOORDS:
      if ( pd->GetTCoords() )
        {
        this->Data = pd->GetTCoords();
        }
      break;
    case VTK_PLOT_TENSORS:
      if ( pd->GetTensors() )
        {
        this->Data = pd->GetTensors();
        }
      break;
    case VTK_PLOT_FIELD_DATA:
      int arrayNum = (this->FieldDataArray < pd->GetNumberOfArrays() ?
                      this->FieldDataArray : pd->GetNumberOfArrays() - 1);
      this->Data = pd->GetArray(arrayNum);
      break;
    }

  // Determine the number of components
  if ( this->Data )
    {
    this->NumberOfComponents = this->Data->GetNumberOfComponents();
    if ( this->PlotComponent >= 0 )
      {
      this->ActiveComponent = (this->PlotComponent < this->NumberOfComponents ?
                           this->PlotComponent : this->NumberOfComponents - 1);
      this->StartComp = this->EndComp = this->ActiveComponent;
      }
    else
      {
      this->StartComp = 0;
      this->EndComp = this->NumberOfComponents - 1;
      }
    }
  else
    {
    vtkErrorMacro(<<"Need input data to plot");
    return 0;
    }

  // Get the range of the components (for scaling the plot later)
  if ( this->DataRange )
    {
    delete [] this->DataRange;
    delete [] this->Tuple;
    }

  this->DataRange = new double [2*this->NumberOfComponents];
  this->Tuple = new double [this->NumberOfComponents];

  for (i=this->StartComp; i <= this->EndComp; i++)
    {
    range = this->DataRange + 2*i;
    range[0] =  VTK_FLOAT_MAX;
    range[1] =  -VTK_FLOAT_MAX;
    }

  for (i=0; i<numPts; i++)
    {
    this->Data->GetTuple(i, this->Tuple);

    for (j=this->StartComp; j <= this->EndComp; j++)
      {
      range = this->DataRange + 2*j;
      if ( this->Tuple[j] < range[0] )
        {
        range[0] = this->Tuple[j];
        }
      if ( this->Tuple[j] > range[1] )
        {
        range[1] = this->Tuple[j];
        }
      }
    }

  return this->NumberOfComponents;
}


int  vtkArcPlotter::OffsetPoint(vtkIdType ptId, vtkPoints *inPts, double n[3],
                                vtkPoints *newPts, double offset,
                                double *range, double v)
{
  double x[3], xNew[3];
  int i;
  double median = (range[0] + range[1])/2.0;
  double denom = range[1] - range[0];

  inPts->GetPoint(ptId, x);
  for (i=0; i<3; i++)
    {
    xNew[i] = x[i] + n[i] * (offset + ((v - median)/denom)*this->Height);
    }

  return newPts->InsertNextPoint(xNew);
}

unsigned long vtkArcPlotter::GetMTime()
{
  unsigned long mTime=this->Superclass::GetMTime();
  unsigned long cameraMTime;

  if ( this->Camera && ! this->UseDefaultNormal )
    {
    cameraMTime = this->Camera->GetMTime();
    return (cameraMTime > mTime ? cameraMTime : mTime);
    }
  else
    {
    return mTime;
    }
}

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

  if ( this->Camera )
    {
    os << indent << "Camera:\n";
    this->Camera->PrintSelf(os,indent.GetNextIndent());
    }
  else
    {
    os << indent << "Camera: (none)\n";
    }

  os << indent << "Plot Mode: ";
  if ( this->PlotMode == VTK_PLOT_SCALARS )
    {
    os << "Plot Scalars\n";
    }
  else if ( this->PlotMode == VTK_PLOT_VECTORS )
    {
    os << "Plot Vectors\n";
    }
  else if ( this->PlotMode == VTK_PLOT_NORMALS )
    {
    os << "Plot Normals\n";
    }
  else if ( this->PlotMode == VTK_PLOT_TCOORDS )
    {
    os << "Plot TCoords\n";
    }
  else if ( this->PlotMode == VTK_PLOT_TENSORS )
    {
    os << "Plot Tensors\n";
    }
  else
    {
    os << "Plot Field Data\n";
    }

  os << indent << "Plot Component: ";
  if ( this->PlotComponent < 0 )
    {
    os << "(All Components)\n";
    }
  else
    {
    os << this->PlotComponent << "\n";
    }

  os << indent << "Field Data Array: " << this->FieldDataArray << "\n";

  os << indent << "Use Default Normal: "
     << (this->UseDefaultNormal ? "On\n" : "Off\n");
  os << indent << "Default Normal: " << "( " << this->DefaultNormal[0]
     << ", " << this->DefaultNormal[1] << ", " << this->DefaultNormal[2]
     << " )\n";

  os << indent << "Radius: " << this->Radius << "\n";
  os << indent << "Height: " << this->Height << "\n";
  os << indent << "Offset: " << this->Offset << "\n";
}