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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkMetaArrowConverterTest.cxx
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.
=========================================================================*/
#include "itkMetaArrowConverter.h"
#include "itkGroupSpatialObject.h"
#include <iostream>
/**
* This is a test file for the itkMetaArrowConverter class.
*
*
* This test creates a sample ArrowSpatialObject and a sample MetaArrow and
* Converts between the two, testing for conversion completeness. At the
* moment, this means testing to make sure length and color are properly
* converted, but it may also include testing direction and position. The
* test also runs the read and write methods to test them.
*
* Notes: Parent cannot be converted from MetaObject to SpatialObject since
* MetaObject only holds a parent id rather than a parent object.
* Only the ParentID can be properly converted.
*/
int itkMetaArrowConverterTest(int ac, char* av[])
{
// check number of arguments
if (ac != 2)
{
std::cout << "Must specify output path as argument" << std::endl;
return EXIT_FAILURE;
}
// typedefs
const unsigned int Dimensions = 3;
typedef itk::ArrowSpatialObject<Dimensions> SpatialObjectType;
typedef itk::GroupSpatialObject<Dimensions> SpatialObjectParentType;
typedef itk::MetaArrowConverter<Dimensions> ConverterType;
// instantiate new converter and object (I don't think I'm supposed to do it like this in ITK!!!)
ConverterType* converter = new ConverterType();
//
// create the test data
//
// direction
SpatialObjectType::VectorType direction;
direction[0] = 0;
direction[1] = 1;
direction[2] = 2;
double mDirection[3];
mDirection[0] = 0;
mDirection[1] = 1;
mDirection[2] = 2;
// position
SpatialObjectType::PointType position;
position[0] = -1;
position[1] = -2;
position[2] = -3;
double mPosition[3];
mPosition[0] = -1;
mPosition[1] = -2;
mPosition[2] = -3;
// length
double length = 2.3;
// color
float color[4];
color[0] = 1;
color[1] = .5;
color[2] = .25;
color[3] = 1;
// set up itkArrow
SpatialObjectType::Pointer itkArrow = SpatialObjectType::New();
itkArrow->SetDirection(direction);
itkArrow->SetPosition(position);
itkArrow->SetLength(length);
itkArrow->GetProperty()->SetRed(color[0]);
itkArrow->GetProperty()->SetGreen(color[1]);
itkArrow->GetProperty()->SetBlue(color[2]);
itkArrow->GetProperty()->SetAlpha(color[3]);
SpatialObjectParentType::Pointer itkParent = SpatialObjectParentType::New();
itkParent->SetId(1);
itkParent->AddSpatialObject(itkArrow);
// set up metaArrow
MetaArrow* metaArrow = new MetaArrow(Dimensions);
metaArrow->Length((float)length);
metaArrow->Position((const double*)mPosition);
metaArrow->Direction((const double*)mDirection);
metaArrow->Color((const float*)color);
metaArrow->ParentID(itkParent->GetId());
// precision limit for comparing floats and doubles
double precisionLimit = .000001;
//
// test itk to metaArrow
//
MetaArrow* newMetaArrow = converter->ArrowSpatialObjectToMetaArrow(itkArrow);
// check length
double metaLength = newMetaArrow->Length();
//if (metaLength != (float)length)
if (vcl_fabs(metaLength - length) > precisionLimit)
{
std::cout << "Conversion to MetaArrow failed to convert length [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] SpatialObject -> MetaObject: length" << std::endl;
// check color
const float* newMetaColor = newMetaArrow->Color();
if (newMetaColor[0] != color[0] || newMetaColor[1] != color[1] ||
newMetaColor[2] != color[2] || newMetaColor[3] != color[3])
{
std::cout << "Conversion to MetaArrow failed to convert color [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] SpatialObject -> MetaObject: color" << std::endl;
// check parent id
if (newMetaArrow->ParentID() != itkArrow->GetParent()->GetId())
{
std::cout << "Conversion to MetaArrow failed to convert parent [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] SpatialObject -> MetaObject: parent id" << std::endl;
// check position
const double* metaPosition = newMetaArrow->Position();
if (vcl_fabs(metaPosition[0] - position[0]) > precisionLimit ||
vcl_fabs(metaPosition[1] - position[1]) > precisionLimit ||
vcl_fabs(metaPosition[2] - position[2]) > precisionLimit)
{
std::cout << "Conversion to MetaArrow failed to convert position [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] SpatialObject -> MetaObject: position" << std::endl;
// check direction (note: need to normalize before comparing)
SpatialObjectType::VectorType directionNorm = direction;
directionNorm.Normalize();
const double* newMetaDirection = newMetaArrow->Direction();
SpatialObjectType::VectorType newMetaDirectionNorm;
newMetaDirectionNorm[0] = newMetaDirection[0];
newMetaDirectionNorm[1] = newMetaDirection[1];
newMetaDirectionNorm[2] = newMetaDirection[2];
// normalize if the vector isn't all zeros
if (newMetaDirection[0] != 0 || newMetaDirection[1] != 0 || newMetaDirection[2] != 0)
{
newMetaDirectionNorm.Normalize();
}
if (vcl_fabs(newMetaDirectionNorm[0] - directionNorm[0]) > precisionLimit
|| vcl_fabs(newMetaDirectionNorm[1] - directionNorm[1]) > precisionLimit
|| vcl_fabs(newMetaDirectionNorm[2] - directionNorm[2]) > precisionLimit)
{
std::cout << "Conversion to SpatialObject failed to convert direction [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] SpatialObject -> MetaObject: direction" << std::endl;
//
// test metaArrow to itk
//
SpatialObjectType::Pointer newItkArrow = converter->MetaArrowToArrowSpatialObject(metaArrow);
// check length
if (vcl_fabs(newItkArrow->GetLength() - metaArrow->Length()) > precisionLimit)
{
std::cout << "Conversion to SpatialObject failed to convert length [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] MetaObject -> SpatialObject: length" << std::endl;
// check color
if (newItkArrow->GetProperty()->GetRed() != color[0] ||
newItkArrow->GetProperty()->GetGreen() != color[1] ||
newItkArrow->GetProperty()->GetBlue() != color[2] ||
newItkArrow->GetProperty()->GetAlpha() != color[3])
{
std::cout << "Conversion to SpatialObject failed to convert color [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] MetaObject -> SpatialObject: color" << std::endl;
// check parent id
if (newItkArrow->GetParentId() != itkParent->GetId())
{
std::cout << "Conversion to SpatialObject failed to convert parent id [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] MetaObject -> SpatialObject: parent id" << std::endl;
// check position
SpatialObjectType::PointType itkPosition = newItkArrow->GetPosition();
if (vcl_fabs(itkPosition[0] - mPosition[0]) > precisionLimit ||
vcl_fabs(itkPosition[1] - mPosition[1]) > precisionLimit ||
vcl_fabs(itkPosition[2] - mPosition[2]) > precisionLimit)
{
std::cout << "Conversion to SpatialObject failed to convert position [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] MetaObject -> SpatialObject: position" << std::endl;
// check direction (note: need to normalize before comparing)
SpatialObjectType::VectorType itkDirectionNorm = newItkArrow->GetDirection();
itkDirectionNorm.Normalize();
SpatialObjectType::VectorType mDirectionNorm = newItkArrow->GetDirection();
mDirectionNorm[0] = mDirection[0];
mDirectionNorm[1] = mDirection[1];
mDirectionNorm[2] = mDirection[2];
if (mDirection[0] != 0 || mDirection[1] != 0 || mDirection[2] != 0)
{
mDirectionNorm.Normalize();
}
if (vcl_fabs(itkDirectionNorm[0] - mDirectionNorm[0]) > precisionLimit
|| vcl_fabs(itkDirectionNorm[1] - mDirectionNorm[1]) > precisionLimit
|| vcl_fabs(itkDirectionNorm[2] - mDirectionNorm[2]) > precisionLimit)
{
std::cout << "Conversion to SpatialObject failed to convert direction [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] MetaObject -> SpatialObject: direction" << std::endl;
//
// test writing
//
if (!converter->WriteMeta(itkArrow, av[1]))
{
std::cout << "Didn't write properly [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] SpatialObject write as MetaObject" << std::endl;
//
// test reading
//
SpatialObjectType::Pointer reLoad = converter->ReadMeta(av[1]);
// check length
if (vcl_fabs(reLoad->GetLength() - length) > precisionLimit)
{
std::cout << "Didn't read length properly [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] Reading: length" << std::endl;
// check color
if (reLoad->GetProperty()->GetRed() != color[0] ||
reLoad->GetProperty()->GetGreen() != color[1] ||
reLoad->GetProperty()->GetBlue() != color[2] ||
reLoad->GetProperty()->GetAlpha() != color[3])
{
std::cout << "Didn't read color properly [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] Reading: color" << std::endl;
// check parent id
if (reLoad->GetParentId() != itkParent->GetId())
{
std::cout << "Didn't read parent id properly [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] Reading: parent id" << std::endl;
// check position
itkPosition = reLoad->GetPosition();
if (vcl_fabs(itkPosition[0] - mPosition[0]) > precisionLimit ||
vcl_fabs(itkPosition[1] - mPosition[1]) > precisionLimit ||
vcl_fabs(itkPosition[2] - mPosition[2]) > precisionLimit)
{
std::cout << "Didn't read position properly [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] Reading: position" << std::endl;
// check direction (note: need to normalize before comparing)
SpatialObjectType::VectorType reLoadDirectionNorm = reLoad->GetDirection();
if (reLoadDirectionNorm[0] != 0 || reLoadDirectionNorm[1] != 0 || reLoadDirectionNorm[2] != 0)
{
reLoadDirectionNorm.Normalize();
}
if (vcl_fabs(reLoadDirectionNorm[0] - directionNorm[0]) > precisionLimit
|| vcl_fabs(reLoadDirectionNorm[1] - directionNorm[1]) > precisionLimit
|| vcl_fabs(reLoadDirectionNorm[2] - directionNorm[2]) > precisionLimit)
{
std::cout << "Didn't read direction properly [FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED] Reading: direction" << std::endl;
// All tests executed successfully
return EXIT_SUCCESS;
}
|