/usr/include/ITK-4.5/itkLabelObject.hxx is in libinsighttoolkit4-dev 4.5.0-3.
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 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 | /*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef __itkLabelObject_hxx
#define __itkLabelObject_hxx
#include "itkLabelObject.h"
#include "itkLabelObjectLineComparator.h"
#include "vnl/vnl_math.h"
#include <algorithm>
namespace itk
{
template< typename TLabel, unsigned int VImageDimension >
LabelObject< TLabel, VImageDimension >::LabelObject()
{
m_Label = NumericTraits< LabelType >::Zero;
m_LineContainer.clear();
}
template< typename 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< typename 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< typename TLabel, unsigned int VImageDimension >
const typename LabelObject< TLabel, VImageDimension >::LabelType &
LabelObject< TLabel, VImageDimension >::GetLabel() const
{
return m_Label;
}
template< typename 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< typename TLabel, unsigned int VImageDimension >
bool
LabelObject< TLabel, VImageDimension >::HasIndex(const IndexType & idx) const
{
typedef typename LineContainerType::const_iterator LineContainerConstIteratorType;
LineContainerConstIteratorType end = m_LineContainer.end();
for ( LineContainerConstIteratorType it = m_LineContainer.begin();
it != end;
++it )
{
if ( it->HasIndex(idx) )
{
return true;
}
}
return false;
}
template< typename TLabel, unsigned int VImageDimension >
bool
LabelObject< TLabel, VImageDimension >::RemoveIndex(const IndexType & idx)
{
typename LineContainerType::iterator it = m_LineContainer.begin();
while( it != m_LineContainer.end() )
{
if( it->HasIndex( idx ) )
{
IndexType orgLineIndex = it->GetIndex();
LengthType orgLineLength = it->GetLength();
if( orgLineLength == 1 )
{
// remove the line and exit
m_LineContainer.erase( it );
return true;
}
if( orgLineIndex == idx )
{
// shift the index to the right and decrease the length by one
++orgLineIndex[0];
it->SetIndex( orgLineIndex );
it->SetLength( orgLineLength - 1 );
return true;
}
else if( orgLineIndex[0] + static_cast< IndexValueType >( orgLineLength ) - 1 == idx[0] )
{
// decrease the length by one
it->SetLength( orgLineLength - 1 );
return true;
}
else
{
// we have to split the line in two parts
it->SetLength( idx[0] - orgLineIndex[0] );
IndexType newIdx = idx;
++newIdx[0];
LengthType newLength = orgLineLength - it->GetLength() - 1;
m_LineContainer.push_back( LineType( newIdx, newLength ) );
return true;
}
}
++it;
}
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< typename 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< typename 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< typename TLabel, unsigned int VImageDimension >
void
LabelObject< TLabel, VImageDimension >::AddLine(const LineType & line)
{
m_LineContainer.push_back(line);
}
template< typename TLabel, unsigned int VImageDimension >
typename LabelObject< TLabel, VImageDimension >::SizeValueType
LabelObject< TLabel, VImageDimension >::GetNumberOfLines() const
{
return m_LineContainer.size();
}
template< typename TLabel, unsigned int VImageDimension >
const
typename LabelObject< TLabel, VImageDimension >::LineType &
LabelObject< TLabel, VImageDimension >::GetLine(SizeValueType i) const
{
return m_LineContainer[i];
}
template< typename TLabel, unsigned int VImageDimension >
typename LabelObject< TLabel, VImageDimension >::LineType &
LabelObject< TLabel, VImageDimension >::GetLine(SizeValueType i)
{
return m_LineContainer[i];
}
template< typename 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< typename TLabel, unsigned int VImageDimension >
bool
LabelObject< TLabel, VImageDimension >::Empty() const
{
return this->m_LineContainer.empty();
}
template< typename 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< typename 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< typename 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< typename 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();
LengthType currentLength = lineContainer.begin()->GetLength();
typename LineContainerType::const_iterator it = lineContainer.begin();
while ( it != lineContainer.end() )
{
const LineType & line = *it;
IndexType idx = line.GetIndex();
LengthType length = line.GetLength();
// check the index to be sure that we are still in the same line idx
bool sameIdx = true;
for ( unsigned 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] + (OffsetValueType)currentLength >= idx[0] )
{
// we may expand the line
LengthType newLength = idx[0] + (OffsetValueType)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< typename TLabel, unsigned int VImageDimension >
void
LabelObject< TLabel, VImageDimension >
::Shift( OffsetType offset )
{
for( typename LineContainerType::iterator it = m_LineContainer.begin();
it != m_LineContainer.end();
it++ )
{
LineType & line = *it;
line.SetIndex( line.GetIndex() + offset );
}
}
template< typename TLabel, unsigned int VImageDimension >
void
LabelObject< TLabel, VImageDimension >
::Clear()
{
m_LineContainer.clear();
}
template< typename 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
|