/usr/include/InsightToolkit/Review/itkLabelObject.txx is in libinsighttoolkit3-dev 3.20.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkLabelObject.txx
Language: C++
Date: $Date$
Version: $Revision$
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 __itkLabelObject_txx
#define __itkLabelObject_txx
#include "itkLabelObject.h"
#include "itkLabelObjectLineComparator.h"
#include "vnl/vnl_math.h"
#include <algorithm>
namespace itk
{
template < class TLabel, unsigned int VImageDimension >
LabelObject<TLabel, VImageDimension>::LabelObject()
{
m_Label = NumericTraits<LabelType>::Zero;
m_LineContainer.clear();
}
template < class TLabel, unsigned int VImageDimension >
typename LabelObject<TLabel, VImageDimension>::AttributeType
LabelObject<TLabel, VImageDimension>::GetAttributeFromName( const std::string & s )
{
if( s == "Label" )
{
return LABEL;
}
// can't recognize the name
itkGenericExceptionMacro(<< "Unknown attribute: " << s);
}
template < class TLabel, unsigned int VImageDimension >
std::string
LabelObject<TLabel, VImageDimension>
::GetNameFromAttribute( const AttributeType & a )
{
switch( a )
{
case LABEL:
return "Label";
}
// can't recognize the namespace
itkGenericExceptionMacro(<< "Unknown attribute: " << a);
}
/**
* Set/Get the label associated with that object.
*/
template < class TLabel, unsigned int VImageDimension >
const typename LabelObject<TLabel, VImageDimension>::LabelType &
LabelObject<TLabel, VImageDimension>::GetLabel() const
{
return m_Label;
}
template < class TLabel, unsigned int VImageDimension >
void
LabelObject<TLabel, VImageDimension>::SetLabel( const LabelType & label )
{
m_Label = label;
}
/**
* Return true if the object contain the given index and false otherwise.
* Worst case complexity is O(L) where L is the number of lines in the object.
*/
template < class TLabel, unsigned int VImageDimension >
bool
LabelObject<TLabel, VImageDimension>::
HasIndex( const IndexType & idx ) const
{
for( typename LineContainerType::const_iterator it=m_LineContainer.begin();
it != m_LineContainer.end();
it++ )
{
if( it->HasIndex( idx ) )
{
return true;
}
}
return false;
}
/**
* Add an index to the object. If the index is already in the object, the index can
* be found several time in the object.
*/
template < class TLabel, unsigned int VImageDimension >
void
LabelObject<TLabel, VImageDimension>::
AddIndex( const IndexType & idx )
{
if( !m_LineContainer.empty() )
{
// can we use the last line to add that index ?
LineType & lastLine = * m_LineContainer.rbegin();
if( lastLine.IsNextIndex( idx ) )
{
lastLine.SetLength( lastLine.GetLength() + 1 );
return;
}
}
// create a new line
this->AddLine( idx, 1 );
}
/**
* Add a new line to the object, without any check.
*/
template < class TLabel, unsigned int VImageDimension >
void
LabelObject<TLabel, VImageDimension>::
AddLine( const IndexType & idx, const LengthType & length )
{
LineType line( idx, length );
this->AddLine( line );
}
/**
* Add a new line to the object, without any check.
*/
template < class TLabel, unsigned int VImageDimension >
void
LabelObject<TLabel, VImageDimension>::
AddLine( const LineType & line )
{
m_LineContainer.push_back( line );
}
/** Return the line container of this object */
template < class TLabel, unsigned int VImageDimension >
const
typename LabelObject<TLabel, VImageDimension>::LineContainerType &
LabelObject<TLabel, VImageDimension>::GetLineContainer() const
{
return m_LineContainer;
}
template < class TLabel, unsigned int VImageDimension >
typename LabelObject<TLabel, VImageDimension>::LineContainerType &
LabelObject<TLabel, VImageDimension>::GetLineContainer()
{
return m_LineContainer;
}
template < class TLabel, unsigned int VImageDimension >
void
LabelObject<TLabel, VImageDimension>::SetLineContainer( const LineContainerType & lineContainer )
{
m_LineContainer = lineContainer;
}
template < class TLabel, unsigned int VImageDimension >
typename LabelObject<TLabel, VImageDimension>::SizeValueType
LabelObject<TLabel, VImageDimension>::GetNumberOfLines() const
{
return m_LineContainer.size();
}
template < class TLabel, unsigned int VImageDimension >
const
typename LabelObject<TLabel, VImageDimension>::LineType &
LabelObject<TLabel, VImageDimension>::GetLine( SizeValueType i ) const
{
return m_LineContainer[i];
}
template < class TLabel, unsigned int VImageDimension >
typename LabelObject<TLabel, VImageDimension>::LineType &
LabelObject<TLabel, VImageDimension>::GetLine( SizeValueType i )
{
return m_LineContainer[i];
}
template < class TLabel, unsigned int VImageDimension >
typename LabelObject<TLabel, VImageDimension>::SizeValueType
LabelObject<TLabel, VImageDimension>::Size() const
{
int size = 0;
for( typename LineContainerType::const_iterator it=m_LineContainer.begin();
it != m_LineContainer.end();
it++ )
{
size += it->GetLength();
}
return size;
}
template < class TLabel, unsigned int VImageDimension >
bool
LabelObject<TLabel, VImageDimension>::Empty() const
{
return this->m_LineContainer.empty();
}
template < class TLabel, unsigned int VImageDimension >
typename LabelObject<TLabel, VImageDimension>::IndexType
LabelObject<TLabel, VImageDimension>::GetIndex( SizeValueType offset ) const
{
SizeValueType o = offset;
typename LineContainerType::const_iterator it = this->m_LineContainer.begin();
while( it != m_LineContainer.end() )
{
SizeValueType size = it->GetLength();
if( o >= size)
{
o -= size;
}
else
{
IndexType idx = it->GetIndex();
idx[0] += o;
return idx;
}
it++;
}
itkGenericExceptionMacro(<< "Invalid offset: " << offset);
}
/** Copy the attributes of another node to this one */
template < class TLabel, unsigned int VImageDimension >
void
LabelObject<TLabel, VImageDimension>::CopyAttributesFrom( const Self * src )
{
itkAssertOrThrowMacro ( (src != NULL), "Null Pointer" );
m_Label = src->m_Label;
}
/** Copy the lines, the label and the attributes from another node. */
template < class TLabel, unsigned int VImageDimension >
void
LabelObject<TLabel, VImageDimension>::CopyAllFrom( const Self * src )
{
itkAssertOrThrowMacro ( (src != NULL), "Null Pointer" );
m_LineContainer = src->m_LineContainer;
// also copy the attributes
this->CopyAttributesFrom( src );
}
/** Reorder the lines, merge the touching lines and ensure that no
* pixel is covered by two lines
*/
template < class TLabel, unsigned int VImageDimension >
void
LabelObject<TLabel, VImageDimension>::Optimize()
{
if( ! m_LineContainer.empty() )
{
// first copy the lines in another container and clear the current one
LineContainerType lineContainer = m_LineContainer;
m_LineContainer.clear();
// reorder the lines
typename Functor::LabelObjectLineComparator< LineType > comparator;
std::sort( lineContainer.begin(), lineContainer.end(), comparator );
// then check the lines consistancy
// we'll proceed line index by line index
IndexType currentIdx = lineContainer.begin()->GetIndex();
long int currentLength = lineContainer.begin()->GetLength();
typename LineContainerType::const_iterator it = lineContainer.begin();
while( it != lineContainer.end() )
{
const LineType & line = *it;
IndexType idx = line.GetIndex();
unsigned long length = line.GetLength();
// check the index to be sure that we are still in the same line idx
bool sameIdx = true;
for( int i=1; i<ImageDimension; i++ )
{
if( currentIdx[i] != idx[i] )
{
sameIdx = false;
}
}
// try to extend the current line idx, or create a new line
if( sameIdx && currentIdx[0] + currentLength >= idx[0] )
{
// we may expand the line
long int newLength = idx[0] + length - currentIdx[0];
currentLength = vnl_math_max( newLength, currentLength );
}
else
{
// add the previous line to the new line container and use the new line index and size
this->AddLine( currentIdx, currentLength );
currentIdx = idx;
currentLength = length;
}
it++;
}
// complete the last line
this->AddLine( currentIdx, currentLength );
}
}
template < class TLabel, unsigned int VImageDimension >
void
LabelObject<TLabel, VImageDimension>::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf( os, indent );
os << indent << "LineContainer: " << & m_LineContainer << std::endl;
os << indent << "Label: " << static_cast<typename NumericTraits<LabelType>::PrintType>(m_Label) << std::endl;
}
} // end namespace itk
#endif
|