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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkDiscreteMarchingCubes.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 "vtkDiscreteMarchingCubes.h"
#include "vtkCellArray.h"
#include "vtkCharArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkLongArray.h"
#include "vtkMarchingCubesTriangleCases.h"
#include "vtkMath.h"
#include "vtkMergePoints.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkCellData.h"
#include "vtkShortArray.h"
#include "vtkStructuredPoints.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnsignedIntArray.h"
#include "vtkUnsignedLongArray.h"
#include "vtkUnsignedShortArray.h"
#include "vtkStreamingDemandDrivenPipeline.h"
vtkStandardNewMacro(vtkDiscreteMarchingCubes);
// Description:
// Construct object with initial range (0,1) and single contour value
// of 0.0. ComputeNormals is off, ComputeGradients is off, ComputeScalars is on and ComputeAdjacentScalars is off.
vtkDiscreteMarchingCubes::vtkDiscreteMarchingCubes()
{
this->ComputeNormals = 0;
this->ComputeGradients = 0;
this->ComputeScalars = 1;
this->ComputeAdjacentScalars = 0;
}
vtkDiscreteMarchingCubes::~vtkDiscreteMarchingCubes()
{
}
//
// Contouring filter specialized for volumes and "short int" data values.
//
template <class T>
void vtkDiscreteMarchingCubesComputeGradient(
vtkDiscreteMarchingCubes *self,T *scalars, int dims[3],
double origin[3], double spacing[3],
vtkIncrementalPointLocator *locator,
vtkDataArray *newCellScalars,
vtkDataArray *newPointScalars,
vtkCellArray *newPolys, double *values,
int numValues)
{
double s[8], value;
int i, j, k;
vtkIdType sliceSize, rowSize;
static int CASE_MASK[8] = {1,2,4,8,16,32,64,128};
vtkMarchingCubesTriangleCases *triCase, *triCases;
EDGE_LIST *edge;
int contNum, ii, index, *vert;
vtkIdType jOffset, kOffset, idx;
vtkIdType ptIds[3];
int extent[6];
int ComputeScalars = newCellScalars != NULL;
int ComputeAdjacentScalars = newPointScalars != NULL;
double t, *x1, *x2, x[3], min, max;
double pts[8][3], xp, yp, zp;
static int edges[12][2] = { {0,1}, {1,2}, {3,2}, {0,3},
{4,5}, {5,6}, {7,6}, {4,7},
{0,4}, {1,5}, {3,7}, {2,6}};
vtkInformation *inInfo = self->GetExecutive()->GetInputInformation(0, 0);
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),extent);
triCases = vtkMarchingCubesTriangleCases::GetCases();
//
// Get min/max contour values
//
if ( numValues < 1 )
{
return;
}
for ( min=max=values[0], i=1; i < numValues; i++)
{
if ( values[i] < min )
{
min = values[i];
}
if ( values[i] > max )
{
max = values[i];
}
}
//
// Traverse all voxel cells, generating triangles
// using marching cubes algorithm.
//
rowSize = dims[0];
sliceSize = rowSize*dims[1];
for ( k=0; k < (dims[2]-1); k++)
{
self->UpdateProgress(static_cast<double>(k)/(dims[2] - 1));
if (self->GetAbortExecute())
{
break;
}
kOffset = k*sliceSize;
pts[0][2] = origin[2] + (k+extent[4])*spacing[2];
zp = pts[0][2] + spacing[2];
for ( j=0; j < (dims[1]-1); j++)
{
jOffset = j*rowSize;
pts[0][1] = origin[1] + (j+extent[2])*spacing[1];
yp = pts[0][1] + spacing[1];
for ( i=0; i < (dims[0]-1); i++)
{
//get scalar values
idx = i + jOffset + kOffset;
s[0] = scalars[idx];
s[1] = scalars[idx+1];
s[2] = scalars[idx+1 + dims[0]];
s[3] = scalars[idx + dims[0]];
s[4] = scalars[idx + sliceSize];
s[5] = scalars[idx+1 + sliceSize];
s[6] = scalars[idx+1 + dims[0] + sliceSize];
s[7] = scalars[idx + dims[0] + sliceSize];
if ( (s[0] < min && s[1] < min && s[2] < min && s[3] < min &&
s[4] < min && s[5] < min && s[6] < min && s[7] < min) ||
(s[0] > max && s[1] > max && s[2] > max && s[3] > max &&
s[4] > max && s[5] > max && s[6] > max && s[7] > max) )
{
continue; // no contours possible
}
//create voxel points
pts[0][0] = origin[0] + (i+extent[0])*spacing[0];
xp = pts[0][0] + spacing[0];
pts[1][0] = xp;
pts[1][1] = pts[0][1];
pts[1][2] = pts[0][2];
pts[2][0] = xp;
pts[2][1] = yp;
pts[2][2] = pts[0][2];
pts[3][0] = pts[0][0];
pts[3][1] = yp;
pts[3][2] = pts[0][2];
pts[4][0] = pts[0][0];
pts[4][1] = pts[0][1];
pts[4][2] = zp;
pts[5][0] = xp;
pts[5][1] = pts[0][1];
pts[5][2] = zp;
pts[6][0] = xp;
pts[6][1] = yp;
pts[6][2] = zp;
pts[7][0] = pts[0][0];
pts[7][1] = yp;
pts[7][2] = zp;
for (contNum=0; contNum < numValues; contNum++)
{
value = values[contNum];
// Build the case table
for ( ii=0, index = 0; ii < 8; ii++)
{
// for discrete marching cubes, we are looking for an
// exact match of a scalar at a vertex to a value
if ( s[ii] == value )
{
index |= CASE_MASK[ii];
}
}
if ( index == 0 || index == 255 ) //no surface
{
continue;
}
triCase = triCases+ index;
edge = triCase->edges;
for ( ; edge[0] > -1; edge += 3 )
{
for (ii=0; ii<3; ii++) //insert triangle
{
vert = edges[edge[ii]];
// for discrete marching cubes, the interpolation point
// is always 0.5.
t = 0.5;
x1 = pts[vert[0]];
x2 = pts[vert[1]];
x[0] = x1[0] + t * (x2[0] - x1[0]);
x[1] = x1[1] + t * (x2[1] - x1[1]);
x[2] = x1[2] + t * (x2[2] - x1[2]);
// add point
if ( locator->InsertUniquePoint(x, ptIds[ii]) )
{
if (ComputeAdjacentScalars)
{
// check which vert holds the neighbour value
if (s[vert[0]] == value)
{
newPointScalars->InsertTuple(ptIds[ii],&s[vert[1]]);
}
else
{
newPointScalars->InsertTuple(ptIds[ii],&s[vert[0]]);
}
}
}
}
// check for degenerate triangle
if ( ptIds[0] != ptIds[1] &&
ptIds[0] != ptIds[2] &&
ptIds[1] != ptIds[2] )
{
newPolys->InsertNextCell(3,ptIds);
// Note that DiscreteMarchingCubes stores the scalar
// data in the cells. It does not use the point data
// since cells from different labeled segments may use
// the same point.
if (ComputeScalars)
{
newCellScalars->InsertNextTuple(&value);
}
}
}//for each triangle
}//for all contours
}//for i
}//for j
}//for k
}
//
// Contouring filter specialized for volumes and "short int" data values.
//
int vtkDiscreteMarchingCubes::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkPoints *newPts;
vtkCellArray *newPolys;
vtkFloatArray *newCellScalars;
vtkFloatArray *newPointScalars;
vtkImageData *input = vtkImageData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPointData *pd;
vtkDataArray *inScalars;
int dims[3], extent[6];
vtkIdType estimatedSize;
double spacing[3], origin[3];
double bounds[6];
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
int numContours=this->ContourValues->GetNumberOfContours();
double *values=this->ContourValues->GetValues();
vtkDebugMacro(<< "Executing marching cubes");
// initialize and check input
pd=input->GetPointData();
if (pd ==NULL)
{
vtkErrorMacro(<<"PointData is NULL");
return 1;
}
inScalars=pd->GetScalars();
if ( inScalars == NULL )
{
vtkErrorMacro(<<"Scalars must be defined for contouring");
return 1;
}
if ( input->GetDataDimension() != 3 )
{
vtkErrorMacro(<<"Cannot contour data of dimension != 3");
return 1;
}
input->GetDimensions(dims);
input->GetOrigin(origin);
input->GetSpacing(spacing);
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), extent);
// estimate the number of points from the volume dimensions
estimatedSize = dims[0];
estimatedSize *= dims[1]; // The "*=" ensures coercion to vtkIdType,
estimatedSize *= dims[2]; // which might be wider than "int"
estimatedSize = static_cast<vtkIdType>(
pow(static_cast<double>(estimatedSize), .75));
estimatedSize = estimatedSize / 1024 * 1024; //multiple of 1024
if (estimatedSize < 1024)
{
estimatedSize = 1024;
}
vtkDebugMacro(<< "Estimated allocation size is " << estimatedSize);
newPts = vtkPoints::New();
newPts->Allocate(estimatedSize,estimatedSize/2);
// compute bounds for merging points
for ( int i=0; i<3; i++)
{
bounds[2*i] = origin[i] + extent[2*i] * spacing[i];
bounds[2*i+1] = origin[i] + extent[2*i+1] * spacing[i];
}
if ( this->Locator == NULL )
{
this->CreateDefaultLocator();
}
this->Locator->InitPointInsertion (newPts, bounds, estimatedSize);
newPolys = vtkCellArray::New();
newPolys->Allocate(newPolys->EstimateSize(estimatedSize,3));
if (this->ComputeScalars)
{
newCellScalars = vtkFloatArray::New();
newCellScalars->Allocate(estimatedSize,3);
}
else
{
newCellScalars = NULL;
}
if (this->ComputeAdjacentScalars)
{
newPointScalars = vtkFloatArray::New();
newPointScalars->Allocate(estimatedSize,estimatedSize/2);
}
else
{
newPointScalars = NULL;
}
if (inScalars->GetNumberOfComponents() == 1 )
{
void* scalars = inScalars->GetVoidPointer(0);
switch (inScalars->GetDataType())
{
vtkTemplateMacro(
vtkDiscreteMarchingCubesComputeGradient(this,
static_cast<VTK_TT*>(scalars),
dims, origin, spacing,
this->Locator, newCellScalars,
newPointScalars,
newPolys, values, numContours)
);
} //switch
}
else //multiple components - have to convert
{
vtkIdType dataSize = dims[0];
dataSize *= dims[1]; // The "*=" ensures coercion to vtkIdType,
dataSize *= dims[2]; // which might be wider than "int".
vtkDoubleArray *image=vtkDoubleArray::New();
image->SetNumberOfComponents(inScalars->GetNumberOfComponents());
image->SetNumberOfTuples(image->GetNumberOfComponents()*dataSize);
inScalars->GetTuples(0,dataSize,image);
double *scalars = image->GetPointer(0);
vtkDiscreteMarchingCubesComputeGradient(this,
scalars,
dims,
origin,
spacing,
this->Locator,
newCellScalars,
newPointScalars,
newPolys,
values,
numContours);
image->Delete();
}
vtkDebugMacro(<<"Created: "
<< newPts->GetNumberOfPoints() << " points, "
<< newPolys->GetNumberOfCells() << " triangles");
//
// Update ourselves. Because we don't know up front how many triangles
// we've created, take care to reclaim memory.
//
output->SetPoints(newPts);
newPts->Delete();
output->SetPolys(newPolys);
newPolys->Delete();
if (newCellScalars)
{
output->GetCellData()->SetScalars(newCellScalars);
newCellScalars->Delete();
}
if (newPointScalars)
{
int idx = output->GetPointData()->AddArray(newPointScalars);
output->GetPointData()->SetActiveAttribute(idx, vtkDataSetAttributes::SCALARS);
newPointScalars->Delete();
}
output->Squeeze();
if (this->Locator)
{
this->Locator->Initialize(); //free storage
}
return 1;
}
|