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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkSpatialObjectToImageFilter.txx,v $
Language: C++
Date: $Date: 2008-01-02 15:12:17 $
Version: $Revision: 1.21 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/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 notices for more information.
=========================================================================*/
#ifndef _itkSpatialObjectToImageFilter_txx
#define _itkSpatialObjectToImageFilter_txx
#include "itkSpatialObjectToImageFilter.h"
#include <itkImageRegionIteratorWithIndex.h>
namespace itk
{
/** Constructor */
template <class TInputSpatialObject, class TOutputImage>
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::SpatialObjectToImageFilter()
{
this->SetNumberOfRequiredInputs( 1 );
m_ChildrenDepth = 999999;
m_Size.Fill( 0 );
m_Direction.SetIdentity();
for (unsigned int i = 0; i < OutputImageDimension; i++)
{
m_Spacing[i] = 1.0;
m_Origin[i] = 0.;
}
m_InsideValue = 0;
m_OutsideValue = 0;
m_UseObjectValue = false;
}
/** Destructor */
template <class TInputSpatialObject, class TOutputImage>
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::~SpatialObjectToImageFilter()
{
}
/** Set the Input SpatialObject */
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::SetInput( const InputSpatialObjectType *input )
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(0,
const_cast< InputSpatialObjectType * >( input ) );
}
/** Connect one of the operands */
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::SetInput( unsigned int index, const TInputSpatialObject * object )
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(index,
const_cast< TInputSpatialObject *>( object ) );
}
/** Get the input Spatial Object */
template <class TInputSpatialObject, class TOutputImage>
const typename SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::InputSpatialObjectType *
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::GetInput(void)
{
if (this->GetNumberOfInputs() < 1)
{
return 0;
}
return static_cast<const TInputSpatialObject * >
(this->ProcessObject::GetInput(0) );
}
/** Get the input Spatial Object */
template <class TInputSpatialObject, class TOutputImage>
const typename SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::InputSpatialObjectType *
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::GetInput(unsigned int idx)
{
return static_cast< const TInputSpatialObject * >
(this->ProcessObject::GetInput(idx));
}
//----------------------------------------------------------------------------
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::SetSpacing(const SpacingType& spacing )
{
unsigned int i;
for (i=0; i<TOutputImage::ImageDimension; i++)
{
if ( (double)spacing[i] != m_Spacing[i] )
{
break;
}
}
if ( i < TOutputImage::ImageDimension )
{
for (i=0; i<TOutputImage::ImageDimension; i++)
{
m_Spacing[i] = spacing[i];
}
this->Modified();
}
}
//----------------------------------------------------------------------------
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::SetSpacing(const double* spacing)
{
unsigned int i;
for (i=0; i<OutputImageDimension; i++)
{
if ( spacing[i] != m_Spacing[i] )
{
break;
}
}
if ( i < OutputImageDimension )
{
for (i=0; i<OutputImageDimension; i++)
{
m_Spacing[i] = spacing[i];
}
this->Modified();
}
}
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::SetSpacing(const float* spacing)
{
unsigned int i;
for (i=0; i<OutputImageDimension; i++)
{
if ( (double)spacing[i] != m_Spacing[i] )
{
break;
}
}
if ( i < OutputImageDimension )
{
for (i=0; i<OutputImageDimension; i++)
{
m_Spacing[i] = spacing[i];
}
this->Modified();
}
}
template <class TInputSpatialObject, class TOutputImage>
const double *
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::GetSpacing() const
{
return m_Spacing;
}
//----------------------------------------------------------------------------
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::SetOrigin(const PointType& origin)
{
unsigned int i;
for (i=0; i<OutputImageDimension; i++)
{
if ( (double)origin[i] != m_Origin[i] )
{
break;
}
}
if ( i < OutputImageDimension )
{
for (i=0; i<OutputImageDimension; i++)
{
m_Origin[i] = origin[i];
}
this->Modified();
}
}
//----------------------------------------------------------------------------
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::SetOrigin(const double* origin)
{
unsigned int i;
for (i=0; i<OutputImageDimension; i++)
{
if ( origin[i] != m_Origin[i] )
{
break;
}
}
if ( i < OutputImageDimension )
{
for (i=0; i<OutputImageDimension; i++)
{
m_Origin[i] = origin[i];
}
this->Modified();
}
}
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::SetOrigin(const float* origin)
{
unsigned int i;
for (i=0; i<OutputImageDimension; i++)
{
if ( (double)origin[i] != m_Origin[i] )
{
break;
}
}
if ( i < OutputImageDimension )
{
for (i=0; i<OutputImageDimension; i++)
{
m_Origin[i] = origin[i];
}
this->Modified();
}
}
template <class TInputSpatialObject, class TOutputImage>
const double *
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::GetOrigin() const
{
return m_Origin;
}
//----------------------------------------------------------------------------
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::SetDirection( const DirectionType & dir )
{
m_Direction = dir;
this->Modified();
}
template <class TInputSpatialObject, class TOutputImage>
const typename SpatialObjectToImageFilter< TInputSpatialObject, TOutputImage >::DirectionType &
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::GetDirection( void ) const
{
return m_Direction;
}
//----------------------------------------------------------------------------
/** Update */
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::GenerateData(void)
{
unsigned int i;
itkDebugMacro(<< "SpatialObjectToImageFilter::Update() called");
// Get the input and output pointers
const InputSpatialObjectType * InputObject = this->GetInput();
OutputImagePointer OutputImage = this->GetOutput();
// Generate the image
SizeType size;
InputObject->ComputeBoundingBox();
for(i=0;i<ObjectDimension;i++)
{
size[i] = (long unsigned int)(InputObject->GetBoundingBox()->GetMaximum()[i]
- InputObject->GetBoundingBox()->GetMinimum()[i]);
}
typename OutputImageType::IndexType index;
index.Fill(0);
typename OutputImageType::RegionType region;
// If the size of the output has been explicitly specified, the filter
// will set the output size to the explicit size, otherwise the size from the spatial
// object's bounding box will be used as default.
bool specified = false;
for (i = 0; i < OutputImageDimension; i++)
{
if (m_Size[i] != 0)
{
specified = true;
break;
}
}
if (specified)
{
region.SetSize( m_Size );
}
else
{
region.SetSize( size );
}
region.SetIndex( index );
OutputImage->SetLargestPossibleRegion( region); //
OutputImage->SetBufferedRegion( region ); // set the region
OutputImage->SetRequestedRegion( region ); //
// If the spacing has been explicitly specified, the filter
// will set the output spacing to that explicit spacing, otherwise the spacing from
// the spatial object is used as default.
specified = false;
for (i = 0; i < OutputImageDimension; i++)
{
if (m_Spacing[i] != 0)
{
specified = true;
break;
}
}
if (specified)
{
OutputImage->SetSpacing(this->m_Spacing); // set spacing
}
else
{
OutputImage->SetSpacing(InputObject->GetIndexToObjectTransform()->GetScaleComponent()); // set spacing
}
OutputImage->SetOrigin( m_Origin ); // and origin
OutputImage->SetDirection( m_Direction );
OutputImage->Allocate(); // allocate the image
typedef itk::ImageRegionIteratorWithIndex<OutputImageType> myIteratorType;
myIteratorType it(OutputImage, region);
itk::Point<double, ObjectDimension> objectPoint;
itk::Point<double, OutputImageDimension> imagePoint;
while(!it.IsAtEnd())
{
// ValueAt requires the point to be in physical coordinate i.e
OutputImage->TransformIndexToPhysicalPoint(it.GetIndex(), imagePoint);
for(i=0; i<ObjectDimension; i++)
{
objectPoint[i] = imagePoint[i];
}
double val = 0;
InputObject->ValueAt(objectPoint, val, m_ChildrenDepth);
if( m_InsideValue != 0 || m_OutsideValue != 0 )
{
if( val)
{
if(m_UseObjectValue)
{
it.Set(static_cast<ValueType>(val));
}
else
{
it.Set(m_InsideValue);
}
}
else
{
it.Set(m_OutsideValue);
}
}
else
{
it.Set(static_cast<ValueType>(val));
}
++it;
}
itkDebugMacro(<< "SpatialObjectToImageFilter::Update() finished");
} // end update function
template<class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Size : " << m_Size << std::endl;
os << indent << "Children depth : " << m_ChildrenDepth << std::endl;
os << indent << "Inside Value : " << m_InsideValue << std::endl;
os << indent << "Outside Value : " << m_OutsideValue << std::endl;
if(m_UseObjectValue)
{
os << indent << "Using Object Value : ON" << std::endl;
}
else
{
os << indent << "Using Object Value : OFF" << std::endl;
}
}
} // end namespace itk
#endif
|