vtkImplicitTextureCoords.cxx 5.68 KB
Newer Older
Will Schroeder's avatar
Will Schroeder committed
1 2
/*=========================================================================

Ken Martin's avatar
Ken Martin committed
3
  Program:   Visualization Toolkit
Ken Martin's avatar
Ken Martin committed
4
  Module:    vtkImplicitTextureCoords.cxx
Will Schroeder's avatar
Will Schroeder committed
5

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.
Ken Martin's avatar
Ken Martin committed
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.
Will Schroeder's avatar
Will Schroeder committed
13 14

=========================================================================*/
Ken Martin's avatar
Ken Martin committed
15
#include "vtkImplicitTextureCoords.h"
16 17

#include "vtkDataSet.h"
18
#include "vtkFloatArray.h"
19
#include "vtkImplicitFunction.h"
20 21
#include "vtkInformation.h"
#include "vtkInformationVector.h"
22
#include "vtkObjectFactory.h"
23
#include "vtkPointData.h"
24

Brad King's avatar
Brad King committed
25
vtkStandardNewMacro(vtkImplicitTextureCoords);
26 27 28
vtkCxxSetObjectMacro(vtkImplicitTextureCoords,SFunction,vtkImplicitFunction);
vtkCxxSetObjectMacro(vtkImplicitTextureCoords,RFunction,vtkImplicitFunction);
vtkCxxSetObjectMacro(vtkImplicitTextureCoords,TFunction,vtkImplicitFunction);
Will Schroeder's avatar
Will Schroeder committed
29

Will Schroeder's avatar
Will Schroeder committed
30 31
// Create object with texture dimension=2 and no r-s-t implicit functions
// defined and FlipTexture turned off.
Ken Martin's avatar
Ken Martin committed
32
vtkImplicitTextureCoords::vtkImplicitTextureCoords()
Will Schroeder's avatar
Will Schroeder committed
33 34 35 36
{
  this->RFunction = NULL;
  this->SFunction = NULL;
  this->TFunction = NULL;
Will Schroeder's avatar
Will Schroeder committed
37 38

  this->FlipTexture = 0;
Will Schroeder's avatar
Will Schroeder committed
39 40
}

41 42 43 44 45 46 47 48
vtkImplicitTextureCoords::~vtkImplicitTextureCoords()
{
  this->SetRFunction(NULL);
  this->SetSFunction(NULL);
  this->SetTFunction(NULL);
}


49 50 51 52
int vtkImplicitTextureCoords::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
Will Schroeder's avatar
Will Schroeder committed
53
{
54 55 56 57
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

58
  // get the input and output
59 60 61 62 63
  vtkDataSet *input = vtkDataSet::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkDataSet *output = vtkDataSet::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

Amy Squillacote's avatar
Amy Squillacote committed
64 65
  vtkIdType ptId, numPts;
  int tcoordDim;
66
  vtkFloatArray *newTCoords;
Ken Martin's avatar
Ken Martin committed
67 68
  double min[3], max[3], scale[3];
  double tCoord[3], tc[3], x[3];
69
  int i;
70

Ken Martin's avatar
Ken Martin committed
71 72
  // Initialize
  //
73
  vtkDebugMacro(<<"Generating texture coordinates from implicit functions...");
Will Schroeder's avatar
Will Schroeder committed
74

75 76 77
  // First, copy the input to the output as a starting point
  output->CopyStructure( input );

78
  if ( ((numPts=input->GetNumberOfPoints()) < 1) )
Will Schroeder's avatar
Will Schroeder committed
79
    {
80
    vtkErrorMacro(<< "No input points!");
81
    return 1;
Will Schroeder's avatar
Will Schroeder committed
82 83 84 85
    }

  if ( this->RFunction == NULL )
    {
86
    vtkErrorMacro(<< "No implicit functions defined!");
87
    return 1;
Will Schroeder's avatar
Will Schroeder committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101
    }

  tcoordDim = 1;
  if ( this->SFunction != NULL )
    {
    tcoordDim++;
    if ( this->TFunction != NULL )
      {
      tcoordDim++;
      }
    }
//
// Allocate
//
102 103
  tCoord[0] = tCoord[1] = tCoord[2] = 0.0;

104
  newTCoords = vtkFloatArray::New();
105
  if ( tcoordDim == 1 ) //force 2D map to be created
Will Schroeder's avatar
Will Schroeder committed
106
    {
107 108
    newTCoords->SetNumberOfComponents(2);
    newTCoords->Allocate(2*numPts);
Will Schroeder's avatar
Will Schroeder committed
109
    }
110
  else
Will Schroeder's avatar
Will Schroeder committed
111
    {
112 113
    newTCoords->SetNumberOfComponents(tcoordDim);
    newTCoords->Allocate(tcoordDim*numPts);
Will Schroeder's avatar
Will Schroeder committed
114
    }
Will Schroeder's avatar
Will Schroeder committed
115
//
116
// Compute implicit function values -> insert as initial texture coordinate
Will Schroeder's avatar
Will Schroeder committed
117
//
118 119
  for (i=0; i<3; i++) //initialize min/max values array
    {
Ken Martin's avatar
Ken Martin committed
120 121
    min[i] = VTK_DOUBLE_MAX;
    max[i] = -VTK_DOUBLE_MAX;
122 123 124
    }
  for (ptId=0; ptId<numPts; ptId++) //compute texture coordinates
    {
125
    input->GetPoint(ptId, x);
126
    tCoord[0] = this->RFunction->FunctionValue(x);
Bill Lorensen's avatar
Bill Lorensen committed
127 128 129 130 131 132 133 134
    if ( this->SFunction )
      {
      tCoord[1] = this->SFunction->FunctionValue(x);
      }
    if ( this->TFunction )
      {
      tCoord[2] = this->TFunction->FunctionValue(x);
      }
135 136 137

    for (i=0; i<tcoordDim; i++)
      {
Bill Lorensen's avatar
Bill Lorensen committed
138
      if (tCoord[i] < min[i])
139 140 141
        {
        min[i] = tCoord[i];
        }
Bill Lorensen's avatar
Bill Lorensen committed
142
      if (tCoord[i] > max[i])
143 144 145
        {
        max[i] = tCoord[i];
        }
146 147
      }

148
    newTCoords->InsertTuple(ptId,tCoord);
149 150
    }
//
151
// Scale and shift texture coordinates into (0,1) range, with 0.0 implicit
152 153 154 155 156 157 158
// function value equal to texture coordinate value of 0.5
//
  for (i=0; i<tcoordDim; i++)
    {
    scale[i] = 1.0;
    if ( max[i] > 0.0 && min[i] < 0.0 ) //have positive & negative numbers
      {
Bill Lorensen's avatar
Bill Lorensen committed
159
      if ( max[i] > (-min[i]) )
160 161 162
        {
        scale[i] = 0.499 / max[i]; //scale into 0.5->1
        }
Bill Lorensen's avatar
Bill Lorensen committed
163
      else
164 165 166
        {
        scale[i] = -0.499 / min[i]; //scale into 0->0.5
        }
167 168 169
      }
    else if ( max[i] > 0.0 ) //have positive numbers only
      {
170
      scale[i] = 0.499 / max[i]; //scale into 0.5->1.0
171 172 173
      }
    else if ( min[i] < 0.0 ) //have negative numbers only
      {
174
      scale[i] = -0.499 / min[i]; //scale into 0.0->0.5
175 176 177
      }
    }

Bill Lorensen's avatar
Bill Lorensen committed
178 179 180 181 182 183 184
  if ( this->FlipTexture )
    {
    for (i=0; i<tcoordDim; i++)
      {
      scale[i] *= (-1.0);
      }
    }
Will Schroeder's avatar
Will Schroeder committed
185 186
  for (ptId=0; ptId<numPts; ptId++)
    {
187
     newTCoords->GetTuple(ptId, tc);
Bill Lorensen's avatar
Bill Lorensen committed
188 189 190 191
    for (i=0; i<tcoordDim; i++)
      {
      tCoord[i] = 0.5 + scale[i] * tc[i];
      }
192
    newTCoords->InsertTuple(ptId,tCoord);
Will Schroeder's avatar
Will Schroeder committed
193 194 195 196
    }
//
// Update self
//
197 198
  output->GetPointData()->CopyTCoordsOff();
  output->GetPointData()->PassData(input->GetPointData());
Will Schroeder's avatar
Will Schroeder committed
199

200
  output->GetPointData()->SetTCoords(newTCoords);
Will Schroeder's avatar
Will Schroeder committed
201
  newTCoords->Delete();
202 203

  return 1;
Will Schroeder's avatar
Will Schroeder committed
204 205
}

206
void vtkImplicitTextureCoords::PrintSelf(ostream& os, vtkIndent indent)
Will Schroeder's avatar
Will Schroeder committed
207
{
Brad King's avatar
Brad King committed
208
  this->Superclass::PrintSelf(os,indent);
Will Schroeder's avatar
Will Schroeder committed
209

210 211
  os << indent << "Flip Texture: " << this->FlipTexture << "\n";

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  if ( this->RFunction != NULL )
    {
    if ( this->SFunction != NULL )
      {
      if ( this->TFunction != NULL )
        {
        os << indent << "R, S, and T Functions defined\n";
        }
      }
    else
      {
      os << indent << "R and S Functions defined\n";
      }
    }
  else
    {
    os << indent << "R Function defined\n";
    }
Will Schroeder's avatar
Will Schroeder committed
230
}