vtkImageSeedConnectivity.cxx 8.26 KB
Newer Older
Charles Law's avatar
Charles Law committed
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkImageSeedConnectivity.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Will Schroeder's avatar
Will Schroeder committed
9

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
Charles Law's avatar
Charles Law committed
13 14 15

=========================================================================*/
#include "vtkImageSeedConnectivity.h"
16 17 18

#include "vtkImageConnector.h"
#include "vtkImageData.h"
Amy Squillacote's avatar
Amy Squillacote committed
19 20 21 22
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
23

Brad King's avatar
Brad King committed
24
vtkStandardNewMacro(vtkImageSeedConnectivity);
Charles Law's avatar
Charles Law committed
25 26 27 28 29 30 31 32 33

//----------------------------------------------------------------------------
vtkImageSeedConnectivity::vtkImageSeedConnectivity()
{
  this->InputConnectValue = 255;
  this->OutputConnectedValue = 255;
  this->OutputUnconnectedValue = 0;
  this->Seeds = NULL;
  this->Connector = vtkImageConnector::New();
Ken Martin's avatar
Ken Martin committed
34
  this->Dimensionality = 3;
Charles Law's avatar
Charles Law committed
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
}

//----------------------------------------------------------------------------
vtkImageSeedConnectivity::~vtkImageSeedConnectivity()
{
  this->Connector->Delete();
  this->RemoveAllSeeds();
}

//----------------------------------------------------------------------------
void vtkImageSeedConnectivity::RemoveAllSeeds()
{
  vtkImageConnectorSeed *temp;
  while (this->Seeds)
    {
    temp = this->Seeds;
    this->Seeds = temp->Next;
    delete temp;
    }
}

//----------------------------------------------------------------------------
void vtkImageSeedConnectivity::AddSeed(int num, int *index)
{
  int idx, newIndex[3];
  vtkImageConnectorSeed *seed;
61

Charles Law's avatar
Charles Law committed
62 63 64
  if (num > 3)
    {
    num = 3;
65
    }
Charles Law's avatar
Charles Law committed
66 67 68 69 70 71 72 73 74 75 76
  for (idx = 0; idx < num; ++idx)
    {
    newIndex[idx] = index[idx];
    }
  for (idx = num; idx < 3; ++idx)
    {
    newIndex[idx] = 0;
    }
  seed = this->Connector->NewSeed(newIndex, NULL);
  seed->Next = this->Seeds;
  this->Seeds = seed;
77
  this->Modified();
Charles Law's avatar
Charles Law committed
78
}
Amy Squillacote's avatar
Amy Squillacote committed
79

Charles Law's avatar
Charles Law committed
80 81 82 83 84 85 86 87 88 89
//----------------------------------------------------------------------------
void vtkImageSeedConnectivity::AddSeed(int i0, int i1, int i2)
{
  int index[3];

  index[0] = i0;
  index[1] = i1;
  index[2] = i2;
  this->AddSeed(3, index);
}
Amy Squillacote's avatar
Amy Squillacote committed
90

Charles Law's avatar
Charles Law committed
91 92 93 94 95 96 97 98 99
//----------------------------------------------------------------------------
void vtkImageSeedConnectivity::AddSeed(int i0, int i1)
{
  int index[2];

  index[0] = i0;
  index[1] = i1;
  this->AddSeed(2, index);
}
Charles Law's avatar
Charles Law committed
100

101
//----------------------------------------------------------------------------
102
int vtkImageSeedConnectivity::RequestUpdateExtent(
Amy Squillacote's avatar
Amy Squillacote committed
103 104 105
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *vtkNotUsed(outputVector))
106
{
Amy Squillacote's avatar
Amy Squillacote committed
107 108 109 110
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
              inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()),
              6);
111 112

  return 1;
113
}
Charles Law's avatar
Charles Law committed
114 115

//----------------------------------------------------------------------------
116
int vtkImageSeedConnectivity::RequestData(
Amy Squillacote's avatar
Amy Squillacote committed
117 118 119
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
Charles Law's avatar
Charles Law committed
120
{
Amy Squillacote's avatar
Amy Squillacote committed
121 122 123 124 125 126 127 128
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  vtkImageData *inData = vtkImageData::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkImageData *outData = vtkImageData::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

Charles Law's avatar
Charles Law committed
129 130
  vtkImageConnectorSeed *seed;
  int idx0, idx1, idx2;
131 132
  vtkIdType inInc0, inInc1, inInc2;
  vtkIdType outInc0, outInc1, outInc2;
Charles Law's avatar
Charles Law committed
133 134 135
  int min0, max0, min1, max1, min2, max2;
  unsigned char *inPtr0, *inPtr1, *inPtr2;
  unsigned char *outPtr0, *outPtr1, *outPtr2;
Charles Law's avatar
Charles Law committed
136
  unsigned char temp1, temp2;
Charles Law's avatar
Charles Law committed
137 138
  int temp;

Amy Squillacote's avatar
Amy Squillacote committed
139 140
  outData->SetExtent(
    outInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()));
141
  outData->AllocateScalars(outInfo);
Amy Squillacote's avatar
Amy Squillacote committed
142

Ken Martin's avatar
Ken Martin committed
143 144
  if (inData->GetScalarType() != VTK_UNSIGNED_CHAR ||
      outData->GetScalarType() != VTK_UNSIGNED_CHAR)
Charles Law's avatar
Charles Law committed
145 146
    {
    vtkErrorMacro("Execute: Both input and output must have scalar type UnsignedChar");
147
    return 1;
Charles Law's avatar
Charles Law committed
148 149 150
    }

  // Pick an intermediate value (In some cases, we could eliminate the last threshold.)
Charles Law's avatar
Charles Law committed
151
  temp1 = 1;
152
  while (temp1 == this->InputConnectValue ||
153 154
         temp1 == this->OutputUnconnectedValue ||
         temp1 == this->OutputConnectedValue)
Charles Law's avatar
Charles Law committed
155 156 157 158
    {
    ++temp1;
    }
  temp2 = temp1 + 1;
159
  while (temp2 == this->InputConnectValue ||
160 161
         temp2 == this->OutputUnconnectedValue ||
         temp2 == this->OutputConnectedValue)
Charles Law's avatar
Charles Law committed
162 163 164
    {
    ++temp2;
    }
165

Charles Law's avatar
Charles Law committed
166 167
  //-------
  // threshold to eliminate unknown values ( only intermediate and 0)
Ken Martin's avatar
Ken Martin committed
168
  inData->GetIncrements(inInc0, inInc1, inInc2);
169
  this->GetOutput()->GetExtent(min0, max0, min1, max1, min2, max2);
Ken Martin's avatar
Ken Martin committed
170
  outData->GetIncrements(outInc0, outInc1, outInc2);
171 172 173 174
  inPtr2 = static_cast<unsigned char *>(
    inData->GetScalarPointer(min0,min1,min2));
  outPtr2 = static_cast<unsigned char *>(
    outData->GetScalarPointer(min0,min1,min2));
Charles Law's avatar
Charles Law committed
175 176 177 178 179 180 181 182 183 184 185 186
  for (idx2 = min2; idx2 <= max2; ++idx2)
    {
    inPtr1 = inPtr2;
    outPtr1 = outPtr2;
    for (idx1 = min1; idx1 <= max1; ++idx1)
      {
      inPtr0 = inPtr1;
      outPtr0 = outPtr1;
      for (idx0 = min0; idx0 <= max0; ++idx0)
        {
        if (*inPtr0 == this->InputConnectValue)
          {
Charles Law's avatar
Charles Law committed
187
          *outPtr0 = temp1;
Charles Law's avatar
Charles Law committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201
          }
        else
          {
          *outPtr0 = 0;
          }
        inPtr0 += inInc0;
        outPtr0 += outInc0;
        }
      inPtr1 += inInc1;
      outPtr1 += outInc1;
      }
    inPtr2 += inInc2;
    outPtr2 += outInc2;
    }
202

203 204 205
  this->UpdateProgress(0.2);
  if (this->AbortExecute)
    {
206
    return 1;
207
    }
208

Charles Law's avatar
Charles Law committed
209 210 211 212 213 214 215
  //-------
  // find actual seeds in this image. (only scan along the first axis for now)
  this->Connector->RemoveAllSeeds();
  seed = this->Seeds;
  while (seed)
    {
    temp = seed->Index[0];
Ken Martin's avatar
Ken Martin committed
216
    // make sure z value of seed is acceptable
Jim Miller's avatar
Style  
Jim Miller committed
217 218 219 220 221 222 223 224
    if (seed->Index[2] < min2)
      {
      seed->Index[2] = min2;
      }
    if (seed->Index[2] > max2)
      {
      seed->Index[2] = max2;
      }
225 226
    outPtr0 = static_cast<unsigned char *>(
      outData->GetScalarPointer(seed->Index));
Charles Law's avatar
Charles Law committed
227 228
    for (idx0 = temp; idx0 <= max0; ++idx0)
      {
Charles Law's avatar
Charles Law committed
229
      if (*outPtr0 == temp1)
Charles Law's avatar
Charles Law committed
230 231 232 233 234 235 236 237 238 239 240
        { // we found our seed
        seed->Index[0] = idx0;
        this->Connector->AddSeed(this->Connector->NewSeed(seed->Index, outPtr0));
        seed->Index[0] = temp;
        break;
        }
      outPtr0 += outInc0;
      }
    seed = seed->Next;
    }

241 242 243
  this->UpdateProgress(0.5);
  if (this->AbortExecute)
    {
244
    return 1;
245 246
    }

Charles Law's avatar
Charles Law committed
247 248
  //-------
  // connect
Charles Law's avatar
Charles Law committed
249 250
  this->Connector->SetUnconnectedValue(temp1);
  this->Connector->SetConnectedValue(temp2);
251
  this->Connector->MarkData(outData, this->Dimensionality,
252
                            this->GetOutput()->GetExtent());
Charles Law's avatar
Charles Law committed
253

254 255 256
  this->UpdateProgress(0.9);
  if (this->AbortExecute)
    {
257
    return 1;
258 259
    }

Charles Law's avatar
Charles Law committed
260 261
  //-------
  // Threshold to convert intermediate values into OutputUnconnectedValues
262 263
  outPtr2 = static_cast<unsigned char *>(
    outData->GetScalarPointer(min0,min1,min2));
Charles Law's avatar
Charles Law committed
264 265 266 267 268 269 270 271
  for (idx2 = min2; idx2 <= max2; ++idx2)
    {
    outPtr1 = outPtr2;
    for (idx1 = min1; idx1 <= max1; ++idx1)
      {
      outPtr0 = outPtr1;
      for (idx0 = min0; idx0 <= max0; ++idx0)
        {
Charles Law's avatar
Charles Law committed
272 273 274 275
        if (*outPtr0 == temp2)
          {
          *outPtr0 = this->OutputConnectedValue;
          }
276
        else
Charles Law's avatar
Charles Law committed
277 278 279 280 281 282 283 284 285
          {
          *outPtr0 = this->OutputUnconnectedValue;
          }
        outPtr0 += outInc0;
        }
      outPtr1 += outInc1;
      }
     outPtr2 += outInc2;
    }
286 287

  return 1;
Charles Law's avatar
Charles Law committed
288 289
}

290
void vtkImageSeedConnectivity::PrintSelf(ostream& os, vtkIndent indent)
291
{
Brad King's avatar
Brad King committed
292
  this->Superclass::PrintSelf(os,indent);
Charles Law's avatar
Charles Law committed
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307
  if ( this->Connector )
    {
    os << indent << "Connector: " << this->Connector << "\n";
    }
  else
    {
    os << indent << "Connector: (none)\n";
    }

  os << indent << "Dimensionality: " << this->Dimensionality << "\n";
  os << indent << "InputConnectValue: " << this->InputConnectValue << "\n";
  os << indent << "OutputConnectedValue: " << this->OutputConnectedValue << "\n";
  os << indent << "OutputUnconnectedValue: " << this->OutputUnconnectedValue << "\n";
}