| 1 |
#ifndef hg5059_test_h |
| 2 |
#define hg5059_test_h |
| 3 |
|
| 4 |
#include "itkMacro.h" |
| 5 |
#include "itkArray.h" |
| 6 |
#include "itkVariableLengthVector.h" |
| 7 |
#include "itkVector.h" |
| 8 |
#include "itkFixedArray.h" |
| 9 |
#include "vnl/vnl_vector_fixed.h" |
| 10 |
#include "itkPoint.h" |
| 11 |
#include "itkRGBPixel.h" |
| 12 |
#include "itkMatrix.h" |
| 13 |
#include "itkVariableSizeMatrix.h" |
| 14 |
#include "itkNumericTraits.h" |
| 15 |
#include "itkSize.h" |
| 16 |
#include <vector> |
| 17 |
|
| 18 |
|
| 19 |
/** THIS DECLARATION OF NUMERICTRAITS IS HERE TEMPORARILY |
| 20 |
* |
| 21 |
* It should be moved to the Insight/Code/Common/itkNumericTraits.h file |
| 22 |
* when the new framework is integrated into ITK */ |
| 23 |
#ifdef _WIN64 |
| 24 |
|
| 25 |
namespace itk { |
| 26 |
|
| 27 |
/** \class NumericTraits<unsigned long> |
| 28 |
* \brief Define traits for type unsigned long. |
| 29 |
* \ingroup DataRepresentation |
| 30 |
*/ |
| 31 |
template <> |
| 32 |
class NumericTraits< std::vector<int>::size_type > : public vcl_numeric_limits< std::vector<int>::size_type > { |
| 33 |
public: |
| 34 |
typedef std::vector<int>::size_type SelfType; |
| 35 |
typedef SelfType ValueType; |
| 36 |
typedef SelfType PrintType; |
| 37 |
typedef SelfType AbsType; |
| 38 |
typedef SelfType AccumulateType; |
| 39 |
typedef long double RealType; |
| 40 |
typedef RealType ScalarRealType; |
| 41 |
typedef long double FloatType; |
| 42 |
|
| 43 |
static const SelfType ITKCommon_EXPORT Zero; |
| 44 |
static const SelfType ITKCommon_EXPORT One; |
| 45 |
|
| 46 |
static SelfType NonpositiveMin() { return min(); } |
| 47 |
static bool IsPositive(SelfType val) { return val != Zero; } |
| 48 |
static bool IsNonpositive(SelfType val) { return val == Zero; } |
| 49 |
static bool IsNegative(SelfType) { return false; } |
| 50 |
static bool IsNonnegative(SelfType) {return true; } |
| 51 |
static SelfType ZeroValue() { return Zero; } |
| 52 |
}; |
| 53 |
} |
| 54 |
|
| 55 |
#endif |
| 56 |
|
| 57 |
|
| 58 |
namespace itk |
| 59 |
{ |
| 60 |
namespace Statistics |
| 61 |
{ |
| 62 |
|
| 63 |
/** \class MeasurementVectorTraits |
| 64 |
* \brief |
| 65 |
* \ingroup Statistics |
| 66 |
*/ |
| 67 |
|
| 68 |
|
| 69 |
class MeasurementVectorTraits |
| 70 |
{ |
| 71 |
public: |
| 72 |
|
| 73 |
/** In the old framework, the FrequencyType is set to float. The problem is for |
| 74 |
large histograms the total frequency can be more than 1e+7, than increasing |
| 75 |
the frequency by one does not change the total frequency (because of lack of |
| 76 |
precision). Using double type will also ultimately fall into the same problem. |
| 77 |
Hence in the new statistics framework, InstanceIdentifier/FrequencyTypes are |
| 78 |
set to the the largest possible integer on the machine */ |
| 79 |
typedef std::vector<int>::size_type InstanceIdentifier; |
| 80 |
|
| 81 |
/** Type defined for representing the frequency of measurement vectors */ |
| 82 |
typedef InstanceIdentifier AbsoluteFrequencyType; |
| 83 |
typedef NumericTraits< AbsoluteFrequencyType >::RealType RelativeFrequencyType; |
| 84 |
typedef NumericTraits< AbsoluteFrequencyType >::AccumulateType TotalAbsoluteFrequencyType; |
| 85 |
typedef NumericTraits< RelativeFrequencyType >::AccumulateType TotalRelativeFrequencyType; |
| 86 |
|
| 87 |
typedef unsigned int MeasurementVectorLength; |
| 88 |
|
| 89 |
template<class TValueType, unsigned int VLength> |
| 90 |
static void SetLength( FixedArray< TValueType, VLength > &m, const unsigned int s ) |
| 91 |
{ |
| 92 |
if( s != VLength ) |
| 93 |
{ |
| 94 |
itkGenericExceptionMacro( << "Cannot set the size of a FixedArray of length " |
| 95 |
<< VLength << " to " << s ); |
| 96 |
} |
| 97 |
m.Fill( NumericTraits< TValueType >::Zero ); |
| 98 |
} |
| 99 |
|
| 100 |
template<class TValueType, unsigned int VLength> |
| 101 |
static void SetLength( FixedArray< TValueType, VLength > *m, const unsigned int s ) |
| 102 |
{ |
| 103 |
if( s != VLength ) |
| 104 |
{ |
| 105 |
itkGenericExceptionMacro( << "Cannot set the size of a FixedArray of length " |
| 106 |
<< VLength << " to " << s ); |
| 107 |
} |
| 108 |
m->Fill( NumericTraits< TValueType >::Zero ); |
| 109 |
} |
| 110 |
|
| 111 |
template< class TValueType > |
| 112 |
static void SetLength( Array< TValueType > & m, const unsigned int s ) |
| 113 |
{ |
| 114 |
m.SetSize( s ); |
| 115 |
m.Fill( NumericTraits< TValueType >::Zero ); |
| 116 |
} |
| 117 |
|
| 118 |
template< class TValueType > |
| 119 |
static void SetLength( Array< TValueType > * m, const unsigned int s ) |
| 120 |
{ |
| 121 |
m->SetSize( s ); |
| 122 |
m->Fill( NumericTraits< TValueType >::Zero ); |
| 123 |
} |
| 124 |
|
| 125 |
template< class TValueType > |
| 126 |
static void SetLength( VariableLengthVector< TValueType > & m, const unsigned int s ) |
| 127 |
{ |
| 128 |
m.SetSize( s ); |
| 129 |
m.Fill( NumericTraits< TValueType >::Zero ); |
| 130 |
} |
| 131 |
|
| 132 |
template< class TValueType > |
| 133 |
static void SetLength( VariableLengthVector< TValueType > * m, const unsigned int s ) |
| 134 |
{ |
| 135 |
m->SetSize( s ); |
| 136 |
m->Fill( NumericTraits< TValueType >::Zero ); |
| 137 |
} |
| 138 |
|
| 139 |
template< class TValueType > |
| 140 |
static void SetLength( std::vector< TValueType > & m, const unsigned int s ) |
| 141 |
{ |
| 142 |
m.resize( s ); |
| 143 |
} |
| 144 |
|
| 145 |
template< class TValueType > |
| 146 |
static void SetLength( std::vector< TValueType > * m, const unsigned int s ) |
| 147 |
{ |
| 148 |
m->resize( s ); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
template< class TValueType, unsigned int VLength > |
| 153 |
static MeasurementVectorLength |
| 154 |
GetLength( const FixedArray< TValueType, VLength > &) |
| 155 |
{ return VLength; } |
| 156 |
|
| 157 |
template< class TValueType, unsigned int VLength > |
| 158 |
static MeasurementVectorLength |
| 159 |
GetLength( const FixedArray< TValueType, VLength > *) |
| 160 |
{ return VLength; } |
| 161 |
|
| 162 |
template< class TValueType > |
| 163 |
static MeasurementVectorLength |
| 164 |
GetLength( const Array< TValueType > &m ) |
| 165 |
{return m.GetSize(); } |
| 166 |
|
| 167 |
template< class TValueType > |
| 168 |
static MeasurementVectorLength |
| 169 |
GetLength( const Array< TValueType > *m ) |
| 170 |
{return m->GetSize(); } |
| 171 |
|
| 172 |
template< class TValueType > |
| 173 |
static MeasurementVectorLength |
| 174 |
GetLength( const VariableLengthVector< TValueType > &m ) |
| 175 |
{return m.GetSize(); } |
| 176 |
|
| 177 |
template< class TValueType > |
| 178 |
static MeasurementVectorLength |
| 179 |
GetLength( const VariableLengthVector< TValueType > *m ) |
| 180 |
{return m->GetSize(); } |
| 181 |
|
| 182 |
template< class TValueType > |
| 183 |
static MeasurementVectorLength |
| 184 |
GetLength( const std::vector< TValueType > &m ) |
| 185 |
{return m.size(); } |
| 186 |
|
| 187 |
template< class TValueType > |
| 188 |
static MeasurementVectorLength |
| 189 |
GetLength( const std::vector< TValueType > *m ) |
| 190 |
{return m->size(); } |
| 191 |
|
| 192 |
|
| 193 |
template< class TVectorType > |
| 194 |
static bool IsResizable( const TVectorType & ) |
| 195 |
{ |
| 196 |
// Test whether the vector type is resizable or not |
| 197 |
// |
| 198 |
// If the default constructor creates a vector of |
| 199 |
// length zero, we assume that it is resizable, |
| 200 |
// otherwise that is a pretty useless measurement vector. |
| 201 |
TVectorType m; |
| 202 |
MeasurementVectorLength len = GetLength( m ); |
| 203 |
return ( len == 0 ); |
| 204 |
} |
| 205 |
|
| 206 |
template< class TValueType1, unsigned int VLength, class TValueType2 > |
| 207 |
static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &, |
| 208 |
const Array< TValueType2 > &b, const char *errMsg="Length Mismatch") |
| 209 |
{ |
| 210 |
if( b.Size() == 0 ) |
| 211 |
{ |
| 212 |
return VLength; |
| 213 |
} |
| 214 |
if( b.Size() != 0 ) |
| 215 |
{ |
| 216 |
if (b.Size() != VLength) |
| 217 |
{ |
| 218 |
itkGenericExceptionMacro( << errMsg ); |
| 219 |
return 0; |
| 220 |
} |
| 221 |
} |
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
template< class TValueType1, unsigned int VLength, class TValueType2 > |
| 226 |
static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *, |
| 227 |
const Array< TValueType2 > *b, const char *errMsg="Length Mismatch") |
| 228 |
{ |
| 229 |
if( b->Size() == 0 ) |
| 230 |
{ |
| 231 |
return VLength; |
| 232 |
} |
| 233 |
else if (b->Size() != VLength) |
| 234 |
{ |
| 235 |
itkGenericExceptionMacro( << errMsg ); |
| 236 |
return 0; |
| 237 |
} |
| 238 |
return 0; |
| 239 |
} |
| 240 |
|
| 241 |
template< class TValueType1, unsigned int VLength, class TValueType2 > |
| 242 |
static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &, |
| 243 |
const VariableLengthVector< TValueType2 > &b, const char *errMsg="Length Mismatch") |
| 244 |
{ |
| 245 |
if( b.Size() == 0 ) |
| 246 |
{ |
| 247 |
return VLength; |
| 248 |
} |
| 249 |
if( b.Size() != 0 ) |
| 250 |
{ |
| 251 |
if (b.Size() != VLength) |
| 252 |
{ |
| 253 |
itkGenericExceptionMacro( << errMsg ); |
| 254 |
return 0; |
| 255 |
} |
| 256 |
} |
| 257 |
return 0; |
| 258 |
} |
| 259 |
|
| 260 |
template< class TValueType1, unsigned int VLength, class TValueType2 > |
| 261 |
static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *, |
| 262 |
const VariableLengthVector< TValueType2 > *b, const char *errMsg="Length Mismatch") |
| 263 |
{ |
| 264 |
if( b->Size() == 0 ) |
| 265 |
{ |
| 266 |
return VLength; |
| 267 |
} |
| 268 |
else if (b->Size() != VLength) |
| 269 |
{ |
| 270 |
itkGenericExceptionMacro( << errMsg ); |
| 271 |
return 0; |
| 272 |
} |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
template< class TValueType1, unsigned int VLength, class TValueType2 > |
| 277 |
static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &, |
| 278 |
const std::vector< TValueType2 > &b, const char *errMsg="Length Mismatch") |
| 279 |
{ |
| 280 |
if( b.size() == 0 ) |
| 281 |
{ |
| 282 |
return VLength; |
| 283 |
} |
| 284 |
if( b.size() != 0 ) |
| 285 |
{ |
| 286 |
if (b.size() != VLength) |
| 287 |
{ |
| 288 |
itkGenericExceptionMacro( << errMsg ); |
| 289 |
return 0; |
| 290 |
} |
| 291 |
} |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
template< class TValueType1, unsigned int VLength, class TValueType2 > |
| 296 |
static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *, |
| 297 |
const std::vector< TValueType2 > *b, const char *errMsg="Length Mismatch") |
| 298 |
{ |
| 299 |
if( b->size() == 0 ) |
| 300 |
{ |
| 301 |
return VLength; |
| 302 |
} |
| 303 |
else if (b->size() != VLength) |
| 304 |
{ |
| 305 |
itkGenericExceptionMacro( << errMsg ); |
| 306 |
return 0; |
| 307 |
} |
| 308 |
return 0; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
template< class TValueType1, unsigned int VLength> |
| 313 |
static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > &, |
| 314 |
const MeasurementVectorLength l, const char *errMsg="Length Mismatch") |
| 315 |
{ |
| 316 |
if( l == 0 ) |
| 317 |
{ |
| 318 |
return VLength; |
| 319 |
} |
| 320 |
else if( l != VLength ) |
| 321 |
{ |
| 322 |
itkGenericExceptionMacro( << errMsg ); |
| 323 |
return 0; |
| 324 |
} |
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
template< class TValueType1, unsigned int VLength> |
| 329 |
static MeasurementVectorLength Assert( const FixedArray< TValueType1, VLength > *, |
| 330 |
const MeasurementVectorLength l, const char *errMsg="Length Mismatch") |
| 331 |
{ |
| 332 |
if( l == 0 ) |
| 333 |
{ |
| 334 |
return VLength; |
| 335 |
} |
| 336 |
else if( l != VLength ) |
| 337 |
{ |
| 338 |
itkGenericExceptionMacro( << errMsg ); |
| 339 |
return 0; |
| 340 |
} |
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
template< class TValueType > |
| 345 |
static MeasurementVectorLength Assert( const Array< TValueType > &a, |
| 346 |
const MeasurementVectorLength l, const char *errMsg="Length Mismatch") |
| 347 |
{ |
| 348 |
if( ( ( l != 0 ) && ( a.Size() != l ) ) || ( a.Size() == 0 ) ) |
| 349 |
{ |
| 350 |
itkGenericExceptionMacro( << errMsg ); |
| 351 |
} |
| 352 |
else if( l == 0 ) |
| 353 |
{ |
| 354 |
return a.Size(); |
| 355 |
} |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
template< class TValueType > |
| 360 |
static MeasurementVectorLength Assert( const Array< TValueType > *a, |
| 361 |
const MeasurementVectorLength l, const char *errMsg="Length Mismatch") |
| 362 |
{ |
| 363 |
if( ( ( l != 0 ) && ( a->Size() != l )) || ( a->Size() == 0 ) ) |
| 364 |
{ |
| 365 |
itkGenericExceptionMacro( << errMsg ); |
| 366 |
} |
| 367 |
else if( l == 0 ) |
| 368 |
{ |
| 369 |
return a->Size(); |
| 370 |
} |
| 371 |
return 0; |
| 372 |
} |
| 373 |
|
| 374 |
template< class TValueType > |
| 375 |
static MeasurementVectorLength Assert( const VariableLengthVector< TValueType > &a, |
| 376 |
const MeasurementVectorLength l, const char *errMsg="Length Mismatch") |
| 377 |
{ |
| 378 |
if( ( ( l != 0 ) && ( a.Size() != l ) ) || ( a.Size() == 0 ) ) |
| 379 |
{ |
| 380 |
itkGenericExceptionMacro( << errMsg ); |
| 381 |
} |
| 382 |
else if( l == 0 ) |
| 383 |
{ |
| 384 |
return a.Size(); |
| 385 |
} |
| 386 |
return 0; |
| 387 |
} |
| 388 |
|
| 389 |
template< class TValueType > |
| 390 |
static MeasurementVectorLength Assert( const VariableLengthVector< TValueType > *a, |
| 391 |
const MeasurementVectorLength l, const char *errMsg="Length Mismatch") |
| 392 |
{ |
| 393 |
if( ( ( l != 0 ) && ( a->Size() != l ) ) || ( a->Size() == 0 ) ) |
| 394 |
{ |
| 395 |
itkGenericExceptionMacro( << errMsg ); |
| 396 |
} |
| 397 |
else if( l == 0 ) |
| 398 |
{ |
| 399 |
return a->Size(); |
| 400 |
} |
| 401 |
return 0; |
| 402 |
} |
| 403 |
template< class TValueType > |
| 404 |
static MeasurementVectorLength Assert( const std::vector< TValueType > &a, |
| 405 |
const MeasurementVectorLength l, const char *errMsg="Length Mismatch") |
| 406 |
{ |
| 407 |
if( ( ( l != 0 ) && ( a.size() != l ) ) || ( a.size() == 0 ) ) |
| 408 |
{ |
| 409 |
itkGenericExceptionMacro( << errMsg ); |
| 410 |
} |
| 411 |
else if( l == 0 ) |
| 412 |
{ |
| 413 |
return a.size(); |
| 414 |
} |
| 415 |
return 0; |
| 416 |
} |
| 417 |
|
| 418 |
template< class TValueType > |
| 419 |
static MeasurementVectorLength Assert( const std::vector< TValueType > *a, |
| 420 |
const MeasurementVectorLength l, const char *errMsg="Length Mismatch") |
| 421 |
{ |
| 422 |
if( ( ( l != 0 ) && ( a->size() != l ) ) || ( a->size() == 0 ) ) |
| 423 |
{ |
| 424 |
itkGenericExceptionMacro( << errMsg ); |
| 425 |
} |
| 426 |
else if( l == 0 ) |
| 427 |
{ |
| 428 |
return a->size(); |
| 429 |
} |
| 430 |
return 0; |
| 431 |
} |
| 432 |
|
| 433 |
template< class TArrayType > |
| 434 |
static void Assign( TArrayType & m, const TArrayType & v ) |
| 435 |
{ |
| 436 |
m = v; |
| 437 |
} |
| 438 |
|
| 439 |
template< class TValueType, unsigned int VLength > |
| 440 |
static void Assign( FixedArray< TValueType, VLength > & m, const TValueType & v ) |
| 441 |
{ |
| 442 |
m[0] = v; |
| 443 |
} |
| 444 |
|
| 445 |
}; |
| 446 |
|
| 447 |
/** \class MeasurementVectorTraitsTypes |
| 448 |
* \brief |
| 449 |
* \ingroup Statistics |
| 450 |
*/ |
| 451 |
|
| 452 |
template < class TMeasurementVector > |
| 453 |
class MeasurementVectorTraitsTypes |
| 454 |
{ |
| 455 |
public: |
| 456 |
typedef typename TMeasurementVector::ValueType ValueType; |
| 457 |
}; |
| 458 |
|
| 459 |
|
| 460 |
template< class T > |
| 461 |
class MeasurementVectorTraitsTypes< std::vector< T > > |
| 462 |
{ |
| 463 |
public: |
| 464 |
typedef T ValueType; |
| 465 |
}; |
| 466 |
|
| 467 |
|
| 468 |
/** Traits for generating the MeasurementVectorType that best matches a |
| 469 |
* particular pixel type. */ |
| 470 |
|
| 471 |
template<class TPixelType> |
| 472 |
class MeasurementVectorPixelTraits |
| 473 |
{ |
| 474 |
public: |
| 475 |
/* type of the vector that matches this pixel type */ |
| 476 |
typedef TPixelType MeasurementVectorType; |
| 477 |
}; |
| 478 |
|
| 479 |
|
| 480 |
template<> |
| 481 |
class MeasurementVectorPixelTraits<char> |
| 482 |
{ |
| 483 |
public: |
| 484 |
typedef FixedArray< char, 1 > MeasurementVectorType; |
| 485 |
}; |
| 486 |
|
| 487 |
template<> |
| 488 |
class MeasurementVectorPixelTraits<unsigned char> |
| 489 |
{ |
| 490 |
public: |
| 491 |
typedef FixedArray< unsigned char, 1 > MeasurementVectorType; |
| 492 |
}; |
| 493 |
|
| 494 |
template<> |
| 495 |
class MeasurementVectorPixelTraits<signed char> |
| 496 |
{ |
| 497 |
public: |
| 498 |
typedef FixedArray< signed char, 1 > MeasurementVectorType; |
| 499 |
}; |
| 500 |
|
| 501 |
template<> |
| 502 |
class MeasurementVectorPixelTraits<unsigned short> |
| 503 |
{ |
| 504 |
public: |
| 505 |
typedef FixedArray< unsigned short, 1 > MeasurementVectorType; |
| 506 |
}; |
| 507 |
|
| 508 |
template<> |
| 509 |
class MeasurementVectorPixelTraits<signed short> |
| 510 |
{ |
| 511 |
public: |
| 512 |
typedef FixedArray< signed short, 1 > MeasurementVectorType; |
| 513 |
}; |
| 514 |
|
| 515 |
template<> |
| 516 |
class MeasurementVectorPixelTraits<unsigned int> |
| 517 |
{ |
| 518 |
public: |
| 519 |
typedef FixedArray< unsigned int, 1 > MeasurementVectorType; |
| 520 |
}; |
| 521 |
|
| 522 |
template<> |
| 523 |
class MeasurementVectorPixelTraits<signed int> |
| 524 |
{ |
| 525 |
public: |
| 526 |
typedef FixedArray< signed int, 1 > MeasurementVectorType; |
| 527 |
}; |
| 528 |
|
| 529 |
template<> |
| 530 |
class MeasurementVectorPixelTraits<unsigned long> |
| 531 |
{ |
| 532 |
public: |
| 533 |
typedef FixedArray< unsigned long, 1 > MeasurementVectorType; |
| 534 |
}; |
| 535 |
|
| 536 |
template<> |
| 537 |
class MeasurementVectorPixelTraits<signed long> |
| 538 |
{ |
| 539 |
public: |
| 540 |
typedef FixedArray< signed long, 1 > MeasurementVectorType; |
| 541 |
}; |
| 542 |
|
| 543 |
template<> |
| 544 |
class MeasurementVectorPixelTraits<float> |
| 545 |
{ |
| 546 |
public: |
| 547 |
typedef FixedArray< float, 1 > MeasurementVectorType; |
| 548 |
}; |
| 549 |
|
| 550 |
template<> |
| 551 |
class MeasurementVectorPixelTraits<double> |
| 552 |
{ |
| 553 |
public: |
| 554 |
typedef FixedArray< double, 1 > MeasurementVectorType; |
| 555 |
}; |
| 556 |
|
| 557 |
|
| 558 |
} // namespace Statistics |
| 559 |
} // namespace itk |
| 560 |
|
| 561 |
#endif // __itkMeasurementVectorTraits_h |