File: vtkDensifyPolyData.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 (565 lines) | stat: -rw-r--r-- 17,016 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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*=========================================================================

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

#include "vtkCellArray.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkDoubleArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPolyData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkMath.h"
#include <vector>

//----------------------------------------------------------------------------
class vtkDensifyPolyDataInternals
{
public:

  // Constructor with params :
  //  - vertices of the triangle to be decimated,
  //  - number of vertices
  //  - ptIds of these vertices
  //  - current running count of the max pointIds, so the new points added
  //    can be assigned ptIds starting from this value.
  //  - Number of subdivisions
  vtkDensifyPolyDataInternals( double    * verts,
                                    vtkIdType   numVerts,
                                    vtkIdType * vertIds,
                                    vtkIdType  & numPoints,
                                    unsigned int nSubdivisions)
  {
    this->NumPoints = this->StartPointId = this->CurrentPointId = numPoints;
    Polygon p( verts, numVerts, vertIds );
    this->Polygons.push_back( p );

    // The actual work: subdivision of the supplied polygon is done here.
    for (unsigned int i = 0; i < nSubdivisions; i++)
    {
      this->Polygons = this->Subdivide( this->Polygons );
    }

    this->PolygonsIterator = this->Polygons.begin();
    numPoints = this->NumPoints;
  }

  // Internal class to represent an nSided polygon.
  class Polygon
  {
  public:
    // Construct a polygon.
    //  nPts:        number of vertices in the polygon
    //  p:           Array of vertices, organized as p0[0], p0[1], p0[2],
    //               p1[0], ..
    //  ptIds:       The pointids of the vertices.
    //  parentPtIds: PointIds of the parent polygon (if one exists), ie the
    //               polygon this is intended to represent a subdivision of.
    //
    Polygon( double *p, vtkIdType nPts, vtkIdType *ptIds,
             vtkIdType nParentPoints = 0, vtkIdType *parentPtIds = NULL)
    {
      this->Verts   = new double[3*nPts];
      this->VertIds = new vtkIdType[nPts];
      this->NumVerts = nPts;
      for (vtkIdType i = 0; i < nPts; i++ )
      {
        this->Verts[3*i]       = p[3*i];
        this->Verts[3*i + 1]   = p[3*i + 1];
        this->Verts[3*i + 2]   = p[3*i + 2];
        this->VertIds[i]       = ptIds[i];
      }

      if (nParentPoints && parentPtIds)
      {
        this->ParentVertIds = new vtkIdType[nParentPoints];
        for (vtkIdType i = 0; i < nPts; i++ )
        {
          this->ParentVertIds[i] = parentPtIds[i];
        }
        this->NumParentVerts = nParentPoints;
      }
      else
      {
        this->NumParentVerts = 0;
        this->ParentVertIds  = NULL;
      }
    }

    // Default constructor
    Polygon()
    {
      this->NumVerts = this->NumParentVerts = 0;
      this->VertIds = this->ParentVertIds = NULL;
      this->Verts = NULL;
    }

    ~Polygon()
    {
      this->Clear();
    }

    void Clear()
    {
      delete [] this->Verts;
      this->Verts=0;
      delete [] this->VertIds;
      this->VertIds=0;
      delete [] this->ParentVertIds;
      this->ParentVertIds=0;
    }

    void DeepCopy( const Polygon & p )
    {
      // Copy the vertices.
      this->NumVerts = p.NumVerts;
      if (p.Verts)
      {
        this->Verts = new double [3 * p.NumVerts];
        for (vtkIdType i = 0; i < this->NumVerts; i++ )
        {
          this->Verts[3*i]       = p.Verts[3*i];
          this->Verts[3*i + 1]   = p.Verts[3*i + 1];
          this->Verts[3*i + 2]   = p.Verts[3*i + 2];
        }
      }
      else
      {
          this->Verts=0; // can happen if called from the copy constructor.
      }

      // Copy the vertex ids.
      if (p.VertIds)
      {
        this->VertIds = new vtkIdType [p.NumVerts];
        for (vtkIdType i = 0; i < this->NumVerts; i++ )
        {
          this->VertIds[i] = p.VertIds[i];
        }
      }
      else
      {
          this->VertIds=0; // can happen if called from the copy constructor.
      }

      // Copy the parent vertex ids, if any.
      this->NumParentVerts = p.NumParentVerts;
      if (p.ParentVertIds)
      {
        this->ParentVertIds = new vtkIdType [p.NumParentVerts];
        for (vtkIdType i = 0; i < this->NumParentVerts; i++ )
        {
          this->ParentVertIds[i] = p.ParentVertIds[i];
        }
      }
      else
      {
        this->ParentVertIds = NULL;
        this->NumParentVerts = 0;
      }
    }

    // Overload method to deep-copy internal data instead of just copying over
    // the pointers.
    Polygon (const Polygon & p)
    {
      this->DeepCopy(p);
    }

    // Overload method to deep-copy internal data instead of just copying over
    // the pointers.
    Polygon &operator=(const Polygon &p)
    {
      // start afresh
      this->Clear();

      this->DeepCopy(p);
      return *this;
    }

    void GetCentroid( double centroid[3] )
    {
      centroid[0] = centroid[1] = centroid[2] = 0.0;
      for (vtkIdType i = 0; i < this->NumVerts; i++)
      {
        centroid[0] += this->Verts[3*i];
        centroid[1] += this->Verts[3*i+1];
        centroid[2] += this->Verts[3*i+2];
      }
      centroid[0] /= static_cast<double>(this->NumVerts);
      centroid[1] /= static_cast<double>(this->NumVerts);
      centroid[2] /= static_cast<double>(this->NumVerts);
    }

    // Get a point with a specific pointId. Returns false if not found
    bool GetPointWithId( vtkIdType id, double p[3] )
    {
      for (vtkIdType i=0; i < this->NumVerts; i++)
      {
        if (this->VertIds[i] == id)
        {
          p[0] = this->Verts[3*i];
          p[1] = this->Verts[3*i+1];
          p[2] = this->Verts[3*i+2];
          return true;
        }
      }
      return false;
    }

    double          * Verts;
    vtkIdType       * VertIds;
    vtkIdType         NumVerts;
    vtkIdType       * ParentVertIds;
    vtkIdType         NumParentVerts;
  };

  // A container of polygons.
  typedef std::vector< Polygon > PolygonsType;

  // After subdivision, use this method to get the next point.
  // Returns the pointId of the point. Returns -1 if no more points.
  vtkIdType GetNextPoint( double p[3], vtkIdList * parentPointIds = NULL )
  {
    vtkIdType id = -1;
    if (this->CurrentPointId < this->NumPoints)
    {
      for (PolygonsType::iterator it = this->Polygons.begin();
           it != this->Polygons.end(); ++it )
      {
        if ((*it).GetPointWithId( this->CurrentPointId, p ) )
        {
          id = this->CurrentPointId;
          if (parentPointIds)
          {
            parentPointIds->Reset();
            for (vtkIdType i = 0; i < it->NumParentVerts; i++)
            {
              parentPointIds->InsertNextId( it->ParentVertIds[i] );
            }
          }
        }
      }
    }
    this->CurrentPointId++;
    return id;
  }

  // After subdivision, methods to get the next cell (polygon) point Ids.
  // Returns false if no more cells.
  vtkIdType * GetNextCell(
      vtkIdType & numVerts /* number of verts in the returned ids */ )
  {
    vtkIdType *vertIds = NULL;
    numVerts = 0;
    if (this->PolygonsIterator != this->Polygons.end())
    {
      vertIds = this->PolygonsIterator->VertIds;
      numVerts = this->PolygonsIterator->NumVerts;
      ++this->PolygonsIterator;
    }
    return vertIds;
  }

  // Subdivide a triangle. Returns a container containing 3 new triangles.
  PolygonsType Subdivide( Polygon & t )
  {
    PolygonsType polygons;

    // Can't subdivide a polygon with less than 3 vertices ! It will be passed
    // through to the output.
    if (t.NumVerts < 3)
    {
      polygons.push_back(t);
      return polygons;
    }

    // Subdivide the polygon by fanning out triangles from the centroid of the
    // polygon over to each of the vertices of the polygon.
    double centroid[3];
    t.GetCentroid( centroid );

    for (vtkIdType i = 0; i < t.NumVerts; i++)
    {
      vtkIdType id1 = i, id2 = (i+1)%(t.NumVerts), id3 = this->NumPoints;

      // verts for the new triangle.
      double verts[9] =
        {  t.Verts[3*id1], t.Verts[3*id1+1], t.Verts[3*id1+2],
           t.Verts[3*id2], t.Verts[3*id2+1], t.Verts[3*id2+2],
           centroid[0],    centroid[1],      centroid[2]       };
      vtkIdType vertIds[3] = { t.VertIds[id1], t.VertIds[id2], id3 };
      polygons.push_back(
          Polygon( verts, 3, vertIds, t.NumVerts, t.VertIds ) );
    }

    this->NumPoints++;
    return polygons;
  }

  // Subdivide each polygon in a container of polygons once.
  PolygonsType Subdivide( PolygonsType & polygons )
  {
    PolygonsType newPolygons;
    for (PolygonsType::iterator it = polygons.begin();
         it != polygons.end(); ++it)
    {
      PolygonsType output = this->Subdivide( *it );
      for (PolygonsType::iterator it2 = output.begin();
           it2 != output.end(); ++it2 )
      {
        newPolygons.push_back( *it2 );
      }
    }
    return newPolygons;
  }

private:
  PolygonsType            Polygons;
  vtkIdType               NumPoints;
  vtkIdType               StartPointId;
  vtkIdType               CurrentPointId;
  PolygonsType::iterator  PolygonsIterator;
};


//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkDensifyPolyData);

//----------------------------------------------------------------------------
vtkDensifyPolyData::vtkDensifyPolyData()
{
  this->NumberOfSubdivisions = 1;
  this->SetNumberOfInputPorts(1);
}

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

//----------------------------------------------------------------------------
int vtkDensifyPolyData::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 ouptut
  vtkPolyData *input = vtkPolyData::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkPolyData *output = vtkPolyData::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

  vtkCellArray *inputPolys = input->GetPolys();
  vtkPoints * inputPoints = input->GetPoints();

  if (!inputPolys || !inputPoints)
  {
    vtkWarningMacro(
      "vtkDensifyPolyData has no points/cells to linearly interpolate.");
    return 0;
  }

  vtkIdType npts = 0, *ptIds = 0;

  input->BuildLinks();

  const vtkIdType inputNumCells = input->GetNumberOfCells();
  const vtkIdType inputNumPoints = input->GetNumberOfPoints();
  vtkCellArray *outputPolys = vtkCellArray::New();

  // Deep copy the input points. We will then add more points this during
  // subdivision.
  vtkPoints * outputPoints = vtkPoints::New();
  outputPoints->DeepCopy( inputPoints );

  // Will be at least that big.. in reality much larger..
  outputPolys->Allocate( outputPolys->EstimateSize( inputNumCells, 3 ) );

  // Copy pointdata structure from input. There will be at least as many
  // points as in the input.
  vtkPointData * inputPD  = input->GetPointData();
  vtkCellData  * inputCD  = input->GetCellData();
  vtkPointData * outputPD = output->GetPointData();
  vtkCellData  * outputCD = output->GetCellData();
  outputPD->DeepCopy(inputPD);

  // Copy celldata structure from input. There will be at least as many cells
  // in the output as in the input.
  outputCD->CopyStructure(inputCD);
  outputCD->CopyAllocate(outputCD, inputNumCells);

  double q[3];
  vtkIdType outputNumPoints = inputNumPoints, cellId = 0, ptId;

  vtkIdList * parentPointIds = vtkIdList::New();

  // var containing number of cells in the output
  vtkIdType outputNumCells = 0;

  for (inputPolys->InitTraversal();
       inputPolys->GetNextCell(npts, ptIds); cellId++)
  { // for every cell

    // Make sure that the polygon is a planar polygon.
    int cellType = input->GetCellType(cellId);
    if ( cellType != VTK_POLYGON &&
         cellType != VTK_QUAD &&
         cellType != VTK_TRIANGLE )
    {
      // Only triangles are subdivided, the others are simply passed through
      // to the output.
      vtkIdType newCellId = outputPolys->InsertNextCell( npts, ptIds );
      ++outputNumCells;
      outputCD->CopyAllocate( outputCD, outputNumCells );
      outputCD->CopyData( inputCD, cellId, newCellId );
      continue;
    }

    double triangleOrQuadPoints[4*3]; // points of the cell (triangle or quad)
    double *p;

    if(cellType==VTK_POLYGON)
    {
      p=new double[npts*3];
    }
    else
    {
      p=triangleOrQuadPoints;
    }

    for (vtkIdType j=0; j < npts; j++)
    {
      inputPoints->GetPoint(ptIds[j], p+(3*j));
    }

    // Check constraints..
    unsigned int nSubdivisions = VTK_UNSIGNED_INT_MAX;

    if (this->NumberOfSubdivisions > 0)
    {
      if(nSubdivisions > this->NumberOfSubdivisions)
      {
        nSubdivisions=this->NumberOfSubdivisions;
      }
    }

    if (nSubdivisions == 0 || nSubdivisions == VTK_UNSIGNED_INT_MAX)
    {
      // No need to subdivide.. just keep the same cell..
      vtkIdType newCellId = outputPolys->InsertNextCell( npts, ptIds );
      ++outputNumCells;
      outputCD->CopyAllocate( outputCD, outputNumCells );
      outputCD->CopyData( inputCD, cellId, newCellId );
    }
    else
    {
      // Subdivide the triangle.
      // The number of new cells formed due to the subdivision of this triangle
      //  = nPts * pow(3, nSubdivisions-1)
      outputNumCells += (npts * static_cast< vtkIdType >(
                           pow(3.0,static_cast<int>(nSubdivisions-1))));

      // Ensure that we have enough space to hold the new cell data. (This does
      // not actually resize the array at every step of the iteration.) It will
      // end up resizing when
      // outputNumCells = { 2*inputNumCells, 4*inputNumCells, 8*inputNumCells,
      //                    16*inputNumCells, .... }.
      outputCD->CopyAllocate( outputCD, outputNumCells );

      vtkDensifyPolyDataInternals
        polygons( p, npts, ptIds, outputNumPoints, nSubdivisions );

      // Insert points and cells generated by subdividing this polygon
      // nSubdivisions times. Generate the point data and the cell data
      // for the new cell points and cells.

      outputPD->CopyAllocate( outputPD, outputNumPoints);
      while ((ptId = polygons.GetNextPoint(q, parentPointIds)) != -1)
      {

        // Interpolation weights for interpolating point data at the
        // subdivided polygon
        vtkIdType nParentVerts = parentPointIds->GetNumberOfIds();
        double *interpolationWeights = new double [nParentVerts];
        double weight = 1.0 / static_cast<double>(nParentVerts);
        for (vtkIdType i = 0; i < nParentVerts; i++)
        {
          interpolationWeights[i] = weight;
        }

        outputPoints->InsertNextPoint( q );
        outputPD->InterpolatePoint( inputPD, ptId,
                                    parentPointIds, interpolationWeights );

        delete [] interpolationWeights;
      }

      vtkIdType numNewCellVerts;
      while (vtkIdType * newCellVertIds = polygons.GetNextCell(numNewCellVerts))
      {
        vtkIdType newCellId = outputPolys->
          InsertNextCell( numNewCellVerts, newCellVertIds );
        outputCD->CopyData( inputCD, cellId, newCellId );
      }
    } // else
    if(cellType==VTK_POLYGON)
    {
      delete[] p;
    }
  } // for every cell


  output->SetPoints( outputPoints );
  output->SetPolys( outputPolys );

  outputPolys->Delete();
  outputPoints->Delete();
  parentPointIds->Delete();

  return 1;
}

//----------------------------------------------------------------------------
int vtkDensifyPolyData::FillInputPortInformation(
                       int port, vtkInformation* info)
{
  if (port == 0)
  {
    info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
  }
  else
  {
    return 0;
  }
  return 1;
}

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

  os << indent << "Number of Subdivisions: "
     << this->NumberOfSubdivisions << endl;
}