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 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkConnectedRegionsMeshFilter.txx,v $
Language: C++
Date: $Date: 2008-02-03 04:05:28 $
Version: $Revision: 1.35 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
Portions of this code are covered under the VTK copyright.
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.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 notices for more information.
=========================================================================*/
#ifndef _itkConnectedRegionsMeshFilter_txx
#define _itkConnectedRegionsMeshFilter_txx
#include "itkConnectedRegionsMeshFilter.h"
#include "itkNumericTraits.h"
#include "itkObjectFactory.h"
#include <set>
namespace itk
{
/*
* ------------------------------------------------
*/
template <class TInputMesh, class TOutputMesh>
ConnectedRegionsMeshFilter<TInputMesh, TOutputMesh>
::ConnectedRegionsMeshFilter()
{
m_ExtractionMode = Self::LargestRegion;
m_ClosestPoint.Fill(0);
}
/*
* ------------------------------------------------
*/
template <class TInputMesh, class TOutputMesh>
void
ConnectedRegionsMeshFilter<TInputMesh, TOutputMesh>
::DeleteSeed(unsigned long id)
{
std::vector<unsigned long> tmpVector;
std::vector<unsigned long>::iterator i;
for ( i = m_SeedList.begin(); i != m_SeedList.end(); ++i)
{
if ( *i != id )
{
tmpVector.push_back(*i);
}
}
m_SeedList.clear();
for ( i = tmpVector.begin(); i != tmpVector.end(); ++i)
{
m_SeedList.push_back(*i);
}
}
/*
* ------------------------------------------------
*/
template <class TInputMesh, class TOutputMesh>
void
ConnectedRegionsMeshFilter<TInputMesh, TOutputMesh>
::DeleteSpecifiedRegion(unsigned long id)
{
std::vector<unsigned long> tmpVector;
std::vector<unsigned long>::iterator i;
for ( i = m_RegionList.begin(); i != m_RegionList.end(); ++i)
{
if ( *i != id )
{
tmpVector.push_back(*i);
}
}
m_RegionList.clear();
for ( i = tmpVector.begin(); i != tmpVector.end(); ++i)
{
m_RegionList.push_back(*i);
}
}
/*
* ------------------------------------------------
*/
template <class TInputMesh, class TOutputMesh>
void
ConnectedRegionsMeshFilter<TInputMesh, TOutputMesh>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Extraction Mode: ";
if ( m_ExtractionMode == Self::PointSeededRegions )
{
os << "Point Seeded Regions" << std::endl;
}
else if ( m_ExtractionMode == Self::CellSeededRegions )
{
os << "Cell Seeded Regions" << std::endl;
}
else if ( m_ExtractionMode == Self::SpecifiedRegions )
{
os << "Specified Regions" << std::endl;
}
else if ( m_ExtractionMode == Self::LargestRegion )
{
os << "Largest Region" << std::endl;
}
else if ( m_ExtractionMode == Self::AllRegions )
{
os << "All Regions" << std::endl;
}
else if ( m_ExtractionMode == Self::ClosestPointRegion )
{
os << "Closest Point Region" << std::endl;
}
}
/*
*
*/
template <class TInputMesh, class TOutputMesh>
void
ConnectedRegionsMeshFilter<TInputMesh, TOutputMesh>
::GenerateData()
{
InputMeshPointer input = this->GetInput();
OutputMeshPointer output = this->GetOutput();
InputMeshPointsContainerPointer inPts = input->GetPoints();
InputMeshCellsContainerPointer inCells = input->GetCells();
InputMeshCellDataContainerPointer inCellData = input->GetCellData();
itkDebugMacro(<<"Executing connectivity");
// Check input/allocate storage
unsigned long numCells = input->GetNumberOfCells();
unsigned long numPts = input->GetNumberOfPoints();
if ( numPts < 1 || numCells < 1 )
{
itkDebugMacro(<<"No data to connect!");
return;
}
// Initialize. Keep track of points and cells visited.
input->BuildCellLinks(); //needed to get neighbors
m_RegionSizes.clear();
m_Visited.resize(numCells);
for ( unsigned int i=0; i < numCells; i++ )
{
m_Visited[i] = -1;
}
// Traverse all cells marking those visited. Each new search
// starts a new connected region. Connected region grows
// using a connected wave propagation.
//
m_Wave = new std::vector<unsigned long>;
m_Wave2 = new std::vector<unsigned long>;
m_Wave->reserve(numPts/4);
m_Wave2->reserve(numPts/4);
// variable used to keep track of propagation
m_RegionNumber = 0;
unsigned long maxCellsInRegion=0;
unsigned long largestRegionId = 0;
int tenth = numCells/10 + 1;
int cellId=0;
if ( m_ExtractionMode != PointSeededRegions &&
m_ExtractionMode != CellSeededRegions &&
m_ExtractionMode != ClosestPointRegion )
{ //visit all cells marking with region number
for (CellsContainerConstIterator cell=inCells->Begin();
cell != inCells->End(); ++cell, ++cellId)
{
if ( !(cellId % tenth) )
{
this->UpdateProgress ((float)cellId/numCells);
}
if ( m_Visited[cellId] < 0 )
{
m_NumberOfCellsInRegion = 0;
m_Wave->push_back(cellId);
this->PropagateConnectedWave();
if ( m_NumberOfCellsInRegion > maxCellsInRegion )
{
maxCellsInRegion = m_NumberOfCellsInRegion;
largestRegionId = m_RegionNumber;
}
m_RegionNumber++;
m_RegionSizes.push_back(m_NumberOfCellsInRegion);
m_Wave->clear();
m_Wave2->clear();
}
}
}
//Otherwise, seeds are used to indicate the region
else if ( m_ExtractionMode == PointSeededRegions )
{
m_NumberOfCellsInRegion = 0;
if ( m_ExtractionMode == PointSeededRegions )
{
InputMeshCellLinksContainerPointer cellLinks;
cellLinks = input->GetCellLinks();
InputMeshCellLinksContainer links;
typename std::set<InputMeshCellIdentifier>::iterator citer;
for (std::vector<unsigned long>::iterator i = m_SeedList.begin();
i != m_SeedList.end(); ++i)
{
links = cellLinks->ElementAt(*i);
for (citer = links.begin(); citer != links.end();
++citer)
{
m_Wave->push_back(*citer);
}
}
}
// use the seeds directly
else if ( m_ExtractionMode == CellSeededRegions )
{
for (std::vector<unsigned long>::iterator i = m_SeedList.begin();
i != m_SeedList.end(); ++i)
{
m_Wave->push_back(*i);
}
}
// find the closest point and get the cells using it as the seeds
else if ( m_ExtractionMode == ClosestPointRegion )
{
// find the closest point
double minDist2=NumericTraits<double>::max(), dist2;
InputMeshPointIdentifier minId = 0;
InputMeshPointType x;
for (PointsContainerConstIterator piter=inPts->Begin();
piter != inPts->End(); ++piter)
{
x = piter->Value();
dist2 = x.SquaredEuclideanDistanceTo(m_ClosestPoint);
if ( dist2 < minDist2 )
{
minId = piter.Index();
minDist2 = dist2;
}
}
// get the cells using the closest point and use them as seeds
InputMeshCellLinksContainerPointer cellLinks;
cellLinks = input->GetCellLinks();
InputMeshCellLinksContainer links;
typename std::set<InputMeshCellIdentifier>::iterator citer;
links = cellLinks->ElementAt(minId);
for (citer = links.begin(); citer != links.end();
++citer)
{
m_Wave->push_back(*citer);
}
}//closest point
// now propagate a wave
this->PropagateConnectedWave();
m_RegionSizes[m_RegionNumber] = m_NumberOfCellsInRegion;
}
delete m_Wave;
delete m_Wave2;
m_Wave = m_Wave2 = 0;
itkDebugMacro (<<"Extracted " << m_RegionNumber << " region(s)");
// Pass the point and point data through
output->SetPoints(inPts);
output->SetPointData(input->GetPointData());
// Create output cells
//
InputMeshCellsContainerPointer
outCells(InputMeshCellsContainer::New());
InputMeshCellDataContainerPointer
outCellData(InputMeshCellDataContainer::New());
cellId = 0;
CellsContainerConstIterator cell;
CellDataContainerConstIterator cellData;
bool CellDataPresent = inCellData->size( ) != 0;
InputMeshCellPointer cellCopy; // need an autopointer to duplicate
// a cell
if ( m_ExtractionMode == PointSeededRegions ||
m_ExtractionMode == CellSeededRegions ||
m_ExtractionMode == ClosestPointRegion ||
m_ExtractionMode == AllRegions)
{ // extract any cell that's been visited
for (cell=inCells->Begin(), cellData=inCellData->Begin();
cell != inCells->End();
++cell, ++cellId)
{
if ( m_Visited[cellId] >= 0 )
{
cell->Value()->MakeCopy( cellCopy );
outCells->InsertElement(cellId, cellCopy.GetPointer());
cellCopy.ReleaseOwnership(); // Pass cell ownership to output mesh
if( CellDataPresent )
{
outCellData->InsertElement(cellId, cellData->Value());
++cellData;
}
}
}
}
// if specified regions, add regions
else if ( m_ExtractionMode == SpecifiedRegions )
{
std::vector<unsigned long>::iterator i;
unsigned long regionId;
bool inReg = false;
for (cell=inCells->Begin(), cellData=inCellData->Begin();
cell != inCells->End();
++cell, ++cellId)
{
if ( m_Visited[cellId] >= 0 )
{
regionId=static_cast<unsigned long>(m_Visited[cellId]);
//see if cell is on region
for ( i = m_RegionList.begin(); i != m_RegionList.end(); ++i)
{
if ( *i == regionId )
{
inReg = true;
break;
}
}
if ( inReg )
{
cell->Value()->MakeCopy( cellCopy );
outCells->InsertElement(cellId, cellCopy.GetPointer());
cellCopy.ReleaseOwnership(); // Pass cell ownership to output mesh
if( CellDataPresent )
{
outCellData->InsertElement(cellId, cellData->Value());
++cellData;
}
}
}
}
}
else //we are extracting the largest region
{
for (cell=inCells->Begin(), cellData=inCellData->Begin();
cell != inCells->End();
++cell, ++cellId)
{
if ( m_Visited[cellId] == static_cast<long>(largestRegionId) )
{
cell->Value()->MakeCopy( cellCopy );
outCells->InsertElement(cellId, cellCopy.GetPointer());
cellCopy.ReleaseOwnership(); // Pass cell ownership to output mesh
if( CellDataPresent )
{
outCellData->InsertElement(cellId, cellData->Value());
++cellData;
}
}
}
}
// Set the output and clean up
output->SetCells(outCells);
output->SetCellData(outCellData);
m_Visited.clear();
// Report some statistics
if ( this->GetDebug() )
{
long count=0;
for (std::vector<unsigned long>::const_iterator ii=m_RegionSizes.begin();
ii != m_RegionSizes.end(); ++ii)
{
count += *ii;
}
itkDebugMacro (<< "Total # of cells accounted for: " << count);
itkDebugMacro (<< "Extracted " << output->GetNumberOfCells() << " cells");
}
// This prevents unnecessary re-executions of the pipeline.
output->SetBufferedRegion( output->GetRequestedRegion() );
return;
}
template <class TInputMesh, class TOutputMesh>
void
ConnectedRegionsMeshFilter<TInputMesh, TOutputMesh>
::PropagateConnectedWave()
{
InputMeshPointer input = this->GetInput();
unsigned long cellId;
InputMeshCellPointer cellPtr;
InputMeshPointIdConstIterator piter;
InputMeshCellLinksContainerPointer cellLinks;
cellLinks = input->GetCellLinks();
InputMeshCellLinksContainer links;
std::vector<unsigned long>::iterator i;
std::vector<unsigned long> *tmpWave;
typename std::set<InputMeshCellIdentifier>::iterator citer;
while ( m_Wave->size() > 0 )
{
for (i=m_Wave->begin(); i != m_Wave->end(); ++i)
{
cellId = *i;
if ( m_Visited[cellId] < 0 )
{
m_Visited[cellId] = m_RegionNumber;
m_NumberOfCellsInRegion++;
//now get the cell points, and then cells using these points
input->GetCell(cellId, cellPtr);
for ( piter=cellPtr->PointIdsBegin();
piter != cellPtr->PointIdsEnd(); ++piter)
{
links = cellLinks->ElementAt(*piter);
for (citer = links.begin(); citer != links.end(); ++citer)
{
if ( m_Visited[*citer] < 0 )
{
m_Wave2->push_back(*citer);
}
}
}
}//if visited
}//for all cells in wave
tmpWave = m_Wave;
m_Wave = m_Wave2;
m_Wave2 = tmpWave;
tmpWave->clear();
}//while wave is propagating
return;
}
} // end namespace itk
#endif
|