vtkTriangularTCoords.cxx 5.34 KB
Newer Older
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkTriangularTCoords.cxx

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

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

=========================================================================*/
#include "vtkTriangularTCoords.h"
16

17
#include "vtkCellArray.h"
18
#include "vtkFloatArray.h"
19 20
#include "vtkInformation.h"
#include "vtkInformationVector.h"
21
#include "vtkObjectFactory.h"
22
#include "vtkPointData.h"
23
#include "vtkPolyData.h"
24

Brad King's avatar
Brad King committed
25 26 27
#include <math.h>

vtkStandardNewMacro(vtkTriangularTCoords);
28

29 30 31 32
int vtkTriangularTCoords::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
33
{
34 35 36 37
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

38
  // get the input and output
39 40 41 42 43
  vtkPolyData *input = vtkPolyData::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkPolyData *output = vtkPolyData::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

Amy Squillacote's avatar
Amy Squillacote committed
44
  vtkIdType tmp;
45
  int j;
46 47 48
  vtkPoints *inPts;
  vtkPointData *pd;
  vtkCellArray *inPolys,*inStrips;
Amy Squillacote's avatar
Amy Squillacote committed
49
  vtkIdType numNewPts, numNewPolys, polyAllocSize;
50
  vtkFloatArray *newTCoords;
Amy Squillacote's avatar
Amy Squillacote committed
51
  vtkIdType newId, numCells, cellId;
52 53 54
  vtkIdType *pts = 0;
  vtkIdType newIds[3];
  vtkIdType npts = 0;;
55
  int errorLogging = 1;
56
  vtkPoints *newPoints;
57
  vtkCellArray *newPolys;
Ken Martin's avatar
Ken Martin committed
58 59
  double p1[3], p2[3], p3[3];
  double tCoords[6];
60
  vtkPointData *pointData = output->GetPointData();
61 62 63

  // Initialize
  //
64
  vtkDebugMacro(<<"Generating triangular texture coordinates");
65 66 67 68 69 70

  inPts = input->GetPoints();
  pd = input->GetPointData();

  inPolys = input->GetPolys();
  inStrips = input->GetStrips();
71

72
  // Count the number of new points and other primitives that
73 74
  // need to be created.
  //
75 76 77
  numNewPts = input->GetNumberOfVerts();

  numNewPolys = 0;
78
  polyAllocSize = 0;
79 80 81 82 83

  for (inPolys->InitTraversal(); inPolys->GetNextCell(npts,pts); )
    {
    numNewPts += npts;
    numNewPolys++;
84
    polyAllocSize += npts + 1;
85 86 87 88
    }
  for (inStrips->InitTraversal(); inStrips->GetNextCell(npts,pts); )
    {
    numNewPts += (npts-2) * 3;
89
    polyAllocSize += (npts - 2) * 4;
90
    }
91
  numCells = inPolys->GetNumberOfCells() + inStrips->GetNumberOfCells();
92

93 94
  //  Allocate texture data
  //
95 96 97
  newTCoords = vtkFloatArray::New();
  newTCoords->SetNumberOfComponents(2);
  newTCoords->Allocate(2*numNewPts);
98

99 100
  // Allocate
  //
101
  newPoints = vtkPoints::New();
Will Schroeder's avatar
Will Schroeder committed
102
  newPoints->Allocate(numNewPts);
103

104
  newPolys = vtkCellArray::New();
105
  newPolys->Allocate(polyAllocSize);
106 107 108 109

  pointData->CopyTCoordsOff();
  pointData->CopyAllocate(pd);

110 111
  // Texture coordinates are the same for each triangle
  //
112 113 114 115 116 117 118
  tCoords[0]= 0.0;
  tCoords[1]= 0.0;
  tCoords[2]= 1.0;
  tCoords[3]= 0.0;
  tCoords[4]= 0.5;
  tCoords[5]= sqrt(3.0)/2.0;

119
  int abort=0;
Amy Squillacote's avatar
Amy Squillacote committed
120
  vtkIdType progressInterval=numCells/20 + 1;
121
  for (cellId=0, inPolys->InitTraversal();
122
       inPolys->GetNextCell(npts,pts) && !abort; cellId++)
123
    {
124 125
    if ( !(cellId % progressInterval) )
      {
Ken Martin's avatar
Ken Martin committed
126
      this->UpdateProgress((double)cellId/numCells);
127 128
      abort = this->GetAbortExecute();
      }
129

130 131
    if (npts != 3)
      {
132 133
      if (errorLogging) vtkWarningMacro(
        << "No texture coordinates for this cell, it is not a triangle");
134 135 136 137 138 139
      errorLogging = 0;
      continue;
      }
    newPolys->InsertNextCell(npts);
    for (j=0; j<npts; j++)
      {
140
      inPts->GetPoint(pts[j], p1);
141 142 143
      newId = newPoints->InsertNextPoint(p1);
      newPolys->InsertCellPoint(newId);
      pointData->CopyData(pd,pts[j],newId);
144
      newTCoords->InsertNextTuple (&tCoords[2*j]);
145 146
      }
    }
147 148 149

  // Triangle strips
  //
150
  for (inStrips->InitTraversal();
151
       inStrips->GetNextCell(npts,pts) && !abort; cellId++)
152
    {
153 154
    if ( !(cellId % progressInterval) )
      {
Ken Martin's avatar
Ken Martin committed
155
      this->UpdateProgress((double)cellId/numCells);
156 157
      abort = this->GetAbortExecute();
      }
158 159 160

    for (j=0; j<(npts-2); j++)
      {
161 162 163
      inPts->GetPoint(pts[j], p1);
      inPts->GetPoint(pts[j+1], p2);
      inPts->GetPoint(pts[j+2], p3);
164 165 166

      newIds[0] = newPoints->InsertNextPoint(p1);
      pointData->CopyData(pd,pts[j],newIds[0]);
167
      newTCoords->InsertNextTuple (&tCoords[0]);
168 169 170

      newIds[1] = newPoints->InsertNextPoint(p2);
      pointData->CopyData(pd,pts[j+1],newIds[1]);
171
      newTCoords->InsertNextTuple (&tCoords[2]);
172 173 174

      newIds[2] = newPoints->InsertNextPoint(p3);
      pointData->CopyData(pd,pts[j+2],newIds[2]);
175
      newTCoords->InsertNextTuple (&tCoords[4]);
176 177

      // flip orientation for odd tris
178
      if (j%2)
179 180 181 182 183 184 185 186
        {
        tmp = newIds[0];
        newIds[0] = newIds[2];
        newIds[2] = tmp;
        }
      newPolys->InsertNextCell(3,newIds);
      }
    }
187 188 189

  // Update self and release memory
  //
190 191 192 193 194 195 196 197
  output->SetPoints(newPoints);
  newPoints->Delete();

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

  output->GetPointData()->SetTCoords(newTCoords);
  newTCoords->Delete();
198 199

  return 1;
200 201
}

202
void vtkTriangularTCoords::PrintSelf(ostream& os, vtkIndent indent)
203
{
Brad King's avatar
Brad King committed
204
  this->Superclass::PrintSelf(os,indent);
205
}