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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAdaptiveSubdivisionFilter.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 "vtkAdaptiveSubdivisionFilter.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkTriangle.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkMergePoints.h"
vtkStandardNewMacro(vtkAdaptiveSubdivisionFilter);
vtkCxxSetObjectMacro(vtkAdaptiveSubdivisionFilter,Locator,vtkIncrementalPointLocator);
//----------------------------------------------------------------------------
// Construct object
vtkAdaptiveSubdivisionFilter::vtkAdaptiveSubdivisionFilter()
{
this->MaximumEdgeLength = 1.0;
this->MaximumTriangleArea = 1.0;
this->MaximumNumberOfTriangles = VTK_ID_MAX;
this->MaximumNumberOfPasses = VTK_ID_MAX;
this->Locator = NULL;
this->OutputPointsPrecision = DEFAULT_PRECISION;
}
//----------------------------------------------------------------------------
// Construct object with number of subdivisions set to 1.
vtkAdaptiveSubdivisionFilter::~vtkAdaptiveSubdivisionFilter()
{
this->SetLocator(NULL);
}
//-----------------------------------------------------------------------------
void vtkAdaptiveSubdivisionFilter::CreateDefaultLocator()
{
if ( this->Locator == NULL )
{
this->Locator = vtkMergePoints::New();
this->Locator->Register(this);
this->Locator->Delete();
}
}
//-----------------------------------------------------------------------------
// Overload standard modified time function.
vtkMTimeType vtkAdaptiveSubdivisionFilter::GetMTime()
{
vtkMTimeType mTime=this->Superclass::GetMTime();
vtkMTimeType time;
if (this->Locator)
{
time = this->Locator->GetMTime();
mTime = ( time > mTime ? time : mTime );
}
return mTime;
}
// Helper functions and structures
namespace {
// There are eight possible subdivision cases (each of the three edges may
// or may not be subdivided). Case 0 just outputs the original triangle;
// the other cases output between 2 and four triangles. Note that when
// three triangles are generated, then the diagonal of the quadrilateral
// produced can go one of two ways. The tetCases is set up so that the two
// triangles forming the quad are the last two triangles and can be
// adjusted as necessary.
int CASE_MASK[3] = {1,2,4};
vtkIdType tessCases[16][13] = {
{1, 0,1,2, 0,0,0, 0,0,0, 0,0,0}, //case 0
{2, 0,3,2, 3,1,2, 0,0,0, 0,0,0}, //case 1
{2, 0,1,4, 4,2,0, 0,0,0, 0,0,0}, //case 2
{3, 3,1,4, 3,4,2, 2,0,3, 0,0,0}, //case 3
{2, 0,1,5, 5,1,2, 0,0,0, 0,0,0}, //case 4
{3, 0,3,5, 5,3,1, 1,2,5, 0,0,0}, //case 5
{3, 5,4,2, 0,1,4, 4,5,0, 0,0,0}, //case 6
{4, 0,3,5, 3,1,4, 5,3,4, 5,4,2}, //case 7
{1, 0,1,2, 0,0,0, 0,0,0, 0,0,0}, //case 0a
{2, 0,3,2, 3,1,2, 0,0,0, 0,0,0}, //case 1a
{2, 0,1,4, 4,2,0, 0,0,0, 0,0,0}, //case 2a
{3, 3,1,4, 0,3,4, 4,2,0, 0,0,0}, //case 3a
{2, 0,1,5, 5,1,2, 0,0,0, 0,0,0}, //case 4a
{3, 0,3,5, 3,1,2, 2,5,3, 0,0,0}, //case 5a
{3, 4,2,5, 5,0,1, 1,4,5, 0,0,0}, //case 6a
{4, 0,3,5, 3,1,4, 5,3,4, 5,4,2}, //case 7a
};
// This method assumes that the diagonal of the quadrilateral formed by
// triangles 2 & 3 may be "swapped" to produce a better triangulation. It
// assumes a lot about the ordering of the connectivity array (subTess).
vtkIdType *SelectTessellation(unsigned char subCase, vtkIdType *ptIds,
vtkPoints *newPts)
{
// If no choice in triangulation return the table entry
if ( tessCases[subCase][0] != 3 )
{
return tessCases[subCase];
}
// Else select best triangulation based on diagonal length
vtkIdType *subTess = tessCases[subCase];
double x0[3], x1[3], x2[3], x3[3];
newPts->GetPoint(ptIds[subTess[4]], x0);
newPts->GetPoint(ptIds[subTess[6]], x1);
newPts->GetPoint(ptIds[subTess[5]], x2);
newPts->GetPoint(ptIds[subTess[8]], x3);
if ( vtkMath::Distance2BetweenPoints(x0,x1) <=
vtkMath::Distance2BetweenPoints(x2,x3) )
{
return tessCases[subCase];
}
else
{
return tessCases[subCase + 8]; //alternate triangulation
}
}
}//anonymous namespace
//----------------------------------------------------------------------------
// This uses a very simple, serial implementation that makes repeated passes
// over the triangles using a swap buffer approach.
int vtkAdaptiveSubdivisionFilter::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 and check its validity
vtkPolyData *input = vtkPolyData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkIdType numPts = input->GetNumberOfPoints();
vtkCellArray *inTris = input->GetPolys();
vtkIdType numTris = inTris->GetNumberOfCells();
if (numPts < 1 || numTris < 1)
{
vtkDebugMacro(<<"No data to subdivide!");
return 1;
}
vtkPointData *inPointData = input->GetPointData();
vtkCellData *inCellData = input->GetCellData();
// This is a quick check that all cells are a triangle. It is not foolproof
// however.... it may be necessary to tighten this up at some point.
vtkIdType connLen = inTris->GetNumberOfConnectivityEntries();
if ( (connLen / 4) != numTris )
{
vtkDebugMacro(<<"Filter operates only on triangles!");
return 1;
}
// Need a locator
if ( ! this->Locator )
{
this->CreateDefaultLocator();
}
// The first thing is to take the existing points and push them into the
// incremental point locator. We know that we are going to use the original
// points. Note that points are only created and are not swapped as each
// pass is invoked.
vtkPoints *inPts = input->GetPoints();
vtkPoints *newPts = vtkPoints::New();
vtkPointData *swapPointData, *newPointData = vtkPointData::New();
newPointData->CopyAllocate(inPointData);
// set precision for the points in the output
if ( this->OutputPointsPrecision == vtkAlgorithm::DEFAULT_PRECISION )
{
newPts->SetDataType(inPts->GetDataType());
}
else if (this->OutputPointsPrecision == vtkAlgorithm::SINGLE_PRECISION)
{
newPts->SetDataType(VTK_FLOAT);
}
else if(this->OutputPointsPrecision == vtkAlgorithm::DOUBLE_PRECISION)
{
newPts->SetDataType(VTK_DOUBLE);
}
this->Locator->InitPointInsertion (newPts,
input->GetBounds(),
input->GetNumberOfPoints());
// Load in the already existing points. Also load in the point data
// associated with the existing points.
for (vtkIdType ptId=0; ptId < numPts; ++ptId)
{
this->Locator->InsertNextPoint(inPts->GetPoint(ptId));
newPointData->CopyData(inPointData, ptId, ptId);
}
// This is a multipass algorithm. From a list of triangles, check each
// against the edge length and area criteria. If necessary, break the
// triangle (using a case table) into smaller triangles by inserting one or
// more points on edges (the edge is broken at its midpoint). The new
// triangles are placed into a new list which serves as the starting point
// for the next pass. An important note: triangles are split independently
// without neighbor "links" (i.e.,cell links) and new points are merged
// into the locator. Since the algorithm treats edges on triangles in an
// identical way, the end result is that triangle neighbors remain
// compatible (due to conincident point merging).
vtkIdType *currTris = inTris->GetPointer();
vtkCellArray *swapTris, *newTris = vtkCellArray::New();
newTris->Allocate(inTris->EstimateSize(2*numTris,3), numTris);
vtkCellData *swapCellData, *newCellData = vtkCellData::New();
newCellData->CopyAllocate(inCellData);
int i;
double area, eLengths[3];
double maxLen2=this->MaximumEdgeLength*this->MaximumEdgeLength;
double maxArea=this->MaximumTriangleArea;
double x[6][3]; //three vertices plus potential mid-edge points
vtkIdType *tri, triId, newId;
vtkIdType passNum;
vtkIdType totalTriangles=0;
bool changesMade;
for ( passNum=0, changesMade=true;
passNum < this->MaximumNumberOfPasses && totalTriangles < this->MaximumNumberOfTriangles && changesMade;
++passNum )
{
changesMade = false;
for (triId=0; triId < numTris; ++triId)
{
tri = currTris + 4*triId + 1; //get point ids defining triangle
newPts->GetPoint(tri[0],x[0]);
newPts->GetPoint(tri[1],x[1]);
newPts->GetPoint(tri[2],x[2]);
eLengths[0] = vtkMath::Distance2BetweenPoints(x[0],x[1]);
eLengths[1] = vtkMath::Distance2BetweenPoints(x[1],x[2]);
eLengths[2] = vtkMath::Distance2BetweenPoints(x[2],x[0]);
area = vtkTriangle::TriangleArea(x[0],x[1],x[2]);
// Various subdivision cases are possible
unsigned char subCase=0;
if ( area > maxArea )
{
subCase = 7;
}
else
{
for (i=0; i<3; ++i)
{
if ( eLengths[i] > maxLen2 )
{
subCase |= CASE_MASK[i];
}
}
}//determine edges to divide
// If not just outputting original triangle then changes are made
if (subCase > 0 )
{
changesMade = true;
}
// Now create new points and triangles dividing edges as appropriate.
double xNew[3];
vtkIdType ptIds[6];
ptIds[0] = tri[0]; ptIds[1] = tri[1]; ptIds[2] = tri[2];
for (i=0; i<3; ++i)
{
if ( subCase & CASE_MASK[i] ) //ith edge needs subdivision
{
xNew[0] = 0.5*(x[i][0] + x[(i+1)%3][0]);
xNew[1] = 0.5*(x[i][1] + x[(i+1)%3][1]);
xNew[2] = 0.5*(x[i][2] + x[(i+1)%3][2]);
if ( (ptIds[3+i] = this->Locator->IsInsertedPoint(xNew)) < 0 )
{
ptIds[3+i] = this->Locator->InsertNextPoint(xNew);
newPointData->InterpolateEdge(inPointData, ptIds[3+i], tri[i], tri[(i+1)%3], 0.5);
}
}
}
// The tessellation may vary based on geometric concerns (selecting best
// diagonal during triangulation of quadrilateral)
vtkIdType newTIds[3], *subTess;
subTess = SelectTessellation(subCase,ptIds,newPts);
vtkIdType numTessTris = *subTess++;
for (i=0; i<numTessTris; ++i, subTess+=3)
{
newTIds[0] = ptIds[subTess[0]];
newTIds[1] = ptIds[subTess[1]];
newTIds[2] = ptIds[subTess[2]];
newId = newTris->InsertNextCell(3,newTIds);
newCellData->CopyData(inCellData, triId, newId);
if ( ++totalTriangles >= this->MaximumNumberOfTriangles )
{
break;
}
}
}//for all triangles in this pass
// Prepare for the next pass, which means swapping input and output.
// Remember that the initial pass uses the filter input; subsequent passes
// cannot modify the input to a new cell array must be created to support
// the swapping.
if ( passNum == 0 )
{
inTris = vtkCellArray::New();
inCellData = vtkCellData::New();
inCellData->CopyAllocate(newCellData);
inPointData = vtkPointData::New();
inPointData->CopyAllocate(newPointData);
}
// Prepare for new triangles
swapTris = newTris;
newTris = inTris;
inTris = swapTris;
currTris = inTris->GetPointer();
numTris = inTris->GetNumberOfCells();
newTris->Reset();
newTris->Allocate(inTris->EstimateSize(2*newTris->GetNumberOfCells(),3),
newTris->GetNumberOfCells());
// Prepare for new cell data
swapCellData = newCellData;
newCellData = inCellData;
inCellData = swapCellData;
// Prepare for new point data
numPts = newPts->GetNumberOfPoints();
swapPointData = newPointData;
newPointData = inPointData;
inPointData = swapPointData;
for (vtkIdType ptId=0; ptId < numPts; ++ptId)
{
newPointData->CopyData(inPointData, ptId, ptId);
}
}//for another pass
// Configure output and clean up
output->SetPoints(newPts);
newPts->Delete();
output->GetPointData()->ShallowCopy(inPointData);
newPointData->Delete();
output->SetPolys(inTris);
inTris->Delete();
newTris->Delete();
output->GetCellData()->ShallowCopy(inCellData);
inCellData->Delete();
inPointData->Delete();
newCellData->Delete();
return 1;
}
//----------------------------------------------------------------------------
void vtkAdaptiveSubdivisionFilter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Maximum Edge Length: " << this->MaximumEdgeLength << "\n";
os << indent << "Maximum Triangle Area: " << this->MaximumTriangleArea << "\n";
os << indent << "Maximum Number Of Triangles: " << this->MaximumNumberOfTriangles << "\n";
os << indent << "Maximum Number Of Passes: " << this->MaximumNumberOfPasses << "\n";
if ( this->Locator )
{
os << indent << "Locator: " << this->Locator << "\n";
}
else
{
os << indent << "Locator: (none)\n";
}
os << indent << "Precision of the output points: "
<< this->OutputPointsPrecision << "\n";
}
|