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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSplineFilter.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 "vtkSplineFilter.h"
#include "vtkCardinalSpline.h"
#include "vtkCell.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkFloatArray.h"
#include "vtkMath.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
vtkStandardNewMacro(vtkSplineFilter);
vtkCxxSetObjectMacro(vtkSplineFilter,Spline,vtkSpline);
vtkSplineFilter::vtkSplineFilter()
{
this->Subdivide = VTK_SUBDIVIDE_SPECIFIED;
this->MaximumNumberOfSubdivisions = VTK_INT_MAX;
this->NumberOfSubdivisions = 100;
this->Length = 0.1;
this->GenerateTCoords = VTK_TCOORDS_FROM_NORMALIZED_LENGTH;
this->TextureLength = 1.0;
this->Spline = vtkCardinalSpline::New();
this->TCoordMap = vtkFloatArray::New();
}
vtkSplineFilter::~vtkSplineFilter()
{
if (this->Spline)
{
this->Spline->Delete();
this->Spline = 0;
}
if (this->TCoordMap)
{
this->TCoordMap->Delete();
this->TCoordMap = 0;
}
}
int vtkSplineFilter::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 *pd=input->GetPointData();
vtkPointData *outPD=output->GetPointData();
vtkCellData *cd=input->GetCellData();
vtkCellData *outCD=output->GetCellData();
vtkCellArray *inLines;
vtkPoints *inPts;
vtkIdType numLines;
vtkCellArray *newLines;
vtkIdType numNewPts, numNewCells;
vtkPoints *newPts;
vtkIdType npts=0, *pts=NULL;
vtkIdType offset=0;
vtkFloatArray *newTCoords=NULL;
int abort=0;
vtkIdType inCellId, numGenPts;
int genTCoords = VTK_TCOORDS_OFF;
// Check input and initialize
//
vtkDebugMacro(<<"Splining polylines");
if ( !(inPts=input->GetPoints()) || inPts->GetNumberOfPoints() < 1 ||
!(inLines = input->GetLines()) ||
(numLines = inLines->GetNumberOfCells()) < 1 )
{
return 1;
}
if ( this->Spline == NULL )
{
vtkWarningMacro(<< "Need to specify a spline!");
return 1;
}
// Create the geometry and topology
numNewPts = this->NumberOfSubdivisions * numLines;
newPts = vtkPoints::New();
newPts->Allocate(numNewPts);
newLines = vtkCellArray::New();
newLines->Allocate(newLines->EstimateSize(1,numNewPts));
// Point data
if ( (this->GenerateTCoords == VTK_TCOORDS_FROM_SCALARS &&
pd->GetScalars() != NULL) ||
(this->GenerateTCoords == VTK_TCOORDS_FROM_LENGTH ||
this->GenerateTCoords == VTK_TCOORDS_FROM_NORMALIZED_LENGTH) )
{
genTCoords = this->GenerateTCoords;
newTCoords = vtkFloatArray::New();
newTCoords->SetNumberOfComponents(2);
newTCoords->Allocate(numNewPts);
newTCoords->SetName("TCoords");
outPD->CopyTCoordsOff();
}
outPD->InterpolateAllocate(pd,numNewPts);
this->TCoordMap->Allocate(VTK_CELL_SIZE);
// Copy cell data
numNewCells = numLines;
outCD->CopyNormalsOff();
outCD->CopyAllocate(cd,numNewCells);
// Set up the splines
this->XSpline = this->Spline->NewInstance();
this->XSpline->DeepCopy(this->Spline);
this->YSpline = this->Spline->NewInstance();
this->YSpline->DeepCopy(this->Spline);
this->ZSpline = this->Spline->NewInstance();
this->ZSpline->DeepCopy(this->Spline);
// Create points along each polyline.
//
for (inCellId=0, inLines->InitTraversal();
inLines->GetNextCell(npts,pts) && !abort; inCellId++)
{
this->UpdateProgress(static_cast<double>(inCellId)/numLines);
abort = this->GetAbortExecute();
if (npts < 2)
{
vtkWarningMacro(<< "Less than two points in line!");
continue; //skip tubing this polyline
}
// Generate the points around the polyline. The strip is not created
// if the polyline is bad.
//
this->TCoordMap->Reset();
numGenPts = this->GeneratePoints(offset, npts, pts, inPts, newPts,
pd, outPD, genTCoords, newTCoords);
if ( ! numGenPts )
{
//vtkWarningMacro(<< "Could not generate points!");
continue; //skip splining
}
// Generate the polyline
//
this->GenerateLine(offset,numGenPts,inCellId,cd,outCD,newLines);
// Compute the new offset for the next polyline
offset += numGenPts;
}//for all polylines
// Update ourselves
//
this->TCoordMap->Initialize();
this->XSpline->Delete();
this->YSpline->Delete();
this->ZSpline->Delete();
output->SetPoints(newPts);
newPts->Delete();
output->SetLines(newLines);
newLines->Delete();
if ( newTCoords )
{
outPD->SetTCoords(newTCoords);
newTCoords->Delete();
}
output->Squeeze();
return 1;
}
int vtkSplineFilter::GeneratePoints(vtkIdType offset, vtkIdType npts,
vtkIdType *pts, vtkPoints *inPts,
vtkPoints *newPts, vtkPointData *pd,
vtkPointData *outPD, int genTCoords,
vtkFloatArray *newTCoords)
{
vtkIdType i;
// Initialize the splines
this->XSpline->RemoveAllPoints();
this->YSpline->RemoveAllPoints();
this->ZSpline->RemoveAllPoints();
// Compute the length of the resulting spline
double xPrev[3], x[3], length=0.0, len, t, tc, dist;
inPts->GetPoint(pts[0],xPrev);
for (i=1; i < npts; i++)
{
inPts->GetPoint(pts[i],x);
len = sqrt(vtkMath::Distance2BetweenPoints(x,xPrev));
length += len;
xPrev[0]=x[0]; xPrev[1]=x[1]; xPrev[2]=x[2];
}
if ( length <= 0.0 )
{
return 0; //failure
}
// Now we insert points into the splines with the parametric coordinate
// based on (polyline) length. We keep track of the parametric coordinates
// of the points for later point interpolation.
inPts->GetPoint(pts[0],xPrev);
for (len=0,i=0; i < npts; i++)
{
inPts->GetPoint(pts[i],x);
dist = sqrt(vtkMath::Distance2BetweenPoints(x,xPrev));
if (i > 0 && dist == 0)
{
continue;
}
len += dist;
t = len/length;
this->TCoordMap->InsertValue(i,t);
this->XSpline->AddPoint(t,x[0]);
this->YSpline->AddPoint(t,x[1]);
this->ZSpline->AddPoint(t,x[2]);
xPrev[0]=x[0]; xPrev[1]=x[1]; xPrev[2]=x[2];
}
// Compute the number of subdivisions
vtkIdType numDivs, numNewPts;
if ( this->Subdivide == VTK_SUBDIVIDE_SPECIFIED )
{
numDivs = this->NumberOfSubdivisions;
}
else
{
numDivs = static_cast<int>(length / this->Length);
}
numDivs = ( numDivs < 1 ? 1 : (numDivs > this->MaximumNumberOfSubdivisions ?
this->MaximumNumberOfSubdivisions : numDivs));
// Now compute the new points
numNewPts = numDivs + 1;
vtkIdType idx;
double s, s0=0.0;
if ( genTCoords == VTK_TCOORDS_FROM_SCALARS )
{
s0=pd->GetScalars()->GetTuple1(pts[0]);
}
double tLo = this->TCoordMap->GetValue(0);
double tHi = this->TCoordMap->GetValue(1);
for (idx=0, i=0; i < numNewPts; i++)
{
t = static_cast<double>(i) / numDivs;
x[0] = this->XSpline->Evaluate(t);
x[1] = this->YSpline->Evaluate(t);
x[2] = this->ZSpline->Evaluate(t);
newPts->InsertPoint(offset+i,x);
// interpolate point data
while ( t > tHi && idx < (npts-2))
{
idx++;
tLo = this->TCoordMap->GetValue(idx);
tHi = this->TCoordMap->GetValue(idx+1);
}
tc = (t - tLo) / (tHi - tLo);
outPD->InterpolateEdge(pd,offset+i,pts[idx],pts[idx+1],tc);
// generate texture coordinates if desired
if ( genTCoords != VTK_TCOORDS_OFF )
{
if ( genTCoords == VTK_TCOORDS_FROM_NORMALIZED_LENGTH )
{
tc = t;
}
else if ( genTCoords == VTK_TCOORDS_FROM_LENGTH )
{
tc = t * length / this->TextureLength;
}
else if ( genTCoords == VTK_TCOORDS_FROM_SCALARS )
{
s = outPD->GetScalars()->GetTuple1(offset+i); //data just interpolated
tc = (s - s0) / this->TextureLength;
}
newTCoords->InsertTuple2(offset+i,tc,0.0);
} //if generating tcoords
} //for all new points
return numNewPts;
}
void vtkSplineFilter::GenerateLine(vtkIdType offset, vtkIdType npts,
vtkIdType inCellId,
vtkCellData *cd, vtkCellData *outCD,
vtkCellArray *newLines)
{
vtkIdType i, outCellId;
outCellId = newLines->InsertNextCell(npts);
outCD->CopyData(cd,inCellId,outCellId);
for (i=0; i < npts; i++)
{
newLines->InsertCellPoint(offset+i);
}
}
const char *vtkSplineFilter::GetSubdivideAsString()
{
if ( this->Subdivide == VTK_SUBDIVIDE_SPECIFIED )
{
return "Specified by Number of Subdivisions";
}
else
{
return "Specified by Length";
}
}
// Return the method of generating the texture coordinates.
const char *vtkSplineFilter::GetGenerateTCoordsAsString(void)
{
if ( this->GenerateTCoords == VTK_TCOORDS_OFF )
{
return "GenerateTCoordsOff";
}
else if ( this->GenerateTCoords == VTK_TCOORDS_FROM_SCALARS )
{
return "GenerateTCoordsFromScalar";
}
else if ( this->GenerateTCoords == VTK_TCOORDS_FROM_LENGTH )
{
return "GenerateTCoordsFromLength";
}
else
{
return "GenerateTCoordsFromNormalizedLength";
}
}
void vtkSplineFilter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Subdivide: :" << this->GetSubdivideAsString() << "\n";
os << indent << "Maximum Number of Subdivisions: "
<< this->MaximumNumberOfSubdivisions << "\n";
os << indent << "Number of Subdivisions: "
<< this->NumberOfSubdivisions << "\n";
os << indent << "Length: " << this->Length << "\n";
os << indent << "Spline: " << this->Spline << "\n";
os << indent << "Generate TCoords: "
<< this->GetGenerateTCoordsAsString() << endl;
os << indent << "Texture Length: " << this->TextureLength << endl;
}
|