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
|
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: vtkImageConnector.cxx,v $
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 "vtkImageConnector.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
vtkCxxRevisionMacro(vtkImageConnector, "$Revision: 1.20 $");
vtkStandardNewMacro(vtkImageConnector);
//----------------------------------------------------------------------------
vtkImageConnector::vtkImageConnector()
{
this->Seeds = NULL;
this->LastSeed = NULL;
this->ConnectedValue = 255;
this->UnconnectedValue = 128;
}
//----------------------------------------------------------------------------
vtkImageConnector::~vtkImageConnector()
{
this->RemoveAllSeeds();
}
//----------------------------------------------------------------------------
void vtkImageConnector::RemoveAllSeeds()
{
vtkImageConnectorSeed *temp;
while (this->Seeds)
{
temp = this->Seeds;
this->Seeds = temp->Next;
delete temp;
}
this->LastSeed = NULL;
}
//----------------------------------------------------------------------------
vtkImageConnectorSeed *vtkImageConnector::NewSeed(int index[3], void *ptr)
{
vtkImageConnectorSeed *seed = vtkImageConnectorSeed::New();
int idx;
for (idx = 0; idx < 3; ++idx)
{
seed->Index[idx] = index[idx];
}
seed->Pointer = ptr;
seed->Next = NULL;
return seed;
}
//----------------------------------------------------------------------------
// Add a new seed to the end of the seed list.
void vtkImageConnector::AddSeedToEnd(vtkImageConnectorSeed *seed)
{
// Add the seed to the end of the list
if (this->LastSeed == NULL)
{ // no seeds yet
this->LastSeed = this->Seeds = seed;
}
else
{
this->LastSeed->Next = seed;
this->LastSeed = seed;
}
}
//----------------------------------------------------------------------------
// Add a new seed to the start of the seed list.
void vtkImageConnector::AddSeed(vtkImageConnectorSeed *seed)
{
seed->Next = this->Seeds;
this->Seeds = seed;
if ( ! this->LastSeed)
{
this->LastSeed = seed;
}
}
//----------------------------------------------------------------------------
// Removes a seed from the start of the seed list, and returns the seed.
vtkImageConnectorSeed *vtkImageConnector::PopSeed()
{
vtkImageConnectorSeed *seed;
seed = this->Seeds;
this->Seeds = seed->Next;
if (this->Seeds == NULL)
{
this->LastSeed = NULL;
}
return seed;
}
//----------------------------------------------------------------------------
// Input a data of 0's and "UnconnectedValue"s. Seeds of this object are
// used to find connected pixels.
// All pixels connected to seeds are set to ConnectedValue.
// The data has to be unsigned char.
void vtkImageConnector::MarkData(vtkImageData *data, int numberOfAxes, int extent[6])
{
vtkIdType *incs, *pIncs;
int *pExtent;
vtkImageConnectorSeed *seed;
unsigned char *ptr;
int newIndex[3], *pIndex, idx;
long count = 0;
incs = data->GetIncrements();
while (this->Seeds)
{
++count;
seed = this->PopSeed();
// just in case the seed has not been marked visited.
*((unsigned char *)(seed->Pointer)) = this->ConnectedValue;
// Add neighbors
newIndex[0] = seed->Index[0];
newIndex[1] = seed->Index[1];
newIndex[2] = seed->Index[2];
pExtent = extent;
pIncs = incs;
pIndex = newIndex;
for (idx = 0; idx < numberOfAxes; ++idx)
{
// check pixel below
if (*pExtent < *pIndex)
{
ptr = (unsigned char *)(seed->Pointer) - *pIncs;
if (*ptr == this->UnconnectedValue)
{ // add a new seed
--(*pIndex);
*ptr = this->ConnectedValue;
this->AddSeedToEnd(this->NewSeed(newIndex, ptr));
++(*pIndex);
}
}
++pExtent;
// check above pixel
if (*pExtent > *pIndex)
{
ptr = (unsigned char *)(seed->Pointer) + *pIncs;
if (*ptr == this->UnconnectedValue)
{ // add a new seed
++(*pIndex);
*ptr = this->ConnectedValue;
this->AddSeedToEnd(this->NewSeed(newIndex, ptr));
--(*pIndex);
}
}
++pExtent;
// move to next axis
++pIncs;
++pIndex;
}
// Delete seed
delete seed;
}
vtkDebugMacro("Marked " << count << " pixels");
}
void vtkImageConnector::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "ConnectedValue: " << this->ConnectedValue << "\n";
os << indent << "UnconnectedValue: " << this->UnconnectedValue << "\n";
}
|