⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 itkversorrigid3dtransformtest.cxx

📁 DTMK软件开发包,此为开源软件,是一款很好的医学图像开发资源.
💻 CXX
📖 第 1 页 / 共 2 页
字号:
  }


  /**  Exercise the SetCenter method  */
  {
  bool Ok = true;

    TransformType::Pointer  transform = TransformType::New();

    itk::Vector<double,3> axis(1);

    const double angle = (atan(1.0)/45.0)*30.0; // turn 30 degrees

    transform->SetRotation( axis, angle );

    TransformType::InputPointType  center;
    center[0] = 31;
    center[1] = 62;
    center[2] = 93;
    
    transform->SetCenter( center );

    TransformType::OutputPointType transformedPoint;
    transformedPoint = transform->TransformPoint( center );

    for(unsigned int i=0; i<3; i++)
      {
        if( fabs( center[i] - transformedPoint[i] ) > epsilon )
        {
          Ok = false;
          break;    
        }
      }

    if( !Ok )
      { 
      std::cerr << "The center point was not invariant to rotation " << std::endl;
      return EXIT_FAILURE;
      }
    else
      {
      std::cout << "Ok center is invariant to rotation." << std::endl;
      }

    const unsigned int np = transform->GetNumberOfParameters();

    ParametersType parameters( np ); // Number of parameters

    VersorType versor;

    parameters[0] = versor.GetX();   // Rotation axis * sin(t/2)
    parameters[1] = versor.GetY();
    parameters[2] = versor.GetZ();
    parameters[3] = 8.0;             // Translation
    parameters[4] = 7.0;
    parameters[5] = 6.0;

    transform->SetParameters( parameters );

    ParametersType parameters2 = transform->GetParameters();

    const double tolerance = 1e-8;

    for(unsigned int p=0; p<np; p++)
      {
      if( fabs( parameters[p] - parameters2[p] ) > tolerance )
        {
        std::cerr << "Output parameter does not match input " << std::endl;
        return EXIT_FAILURE;
        }
      }
     std::cout << "Input/Output parameter check Passed !"  << std::endl;

     // Try the GetJacobian method
     TransformType::InputPointType  aPoint;
     aPoint[0] = 10.0;
     aPoint[1] = 20.0;
     aPoint[2] = -10.0;
     JacobianType   jacobian = transform->GetJacobian( aPoint );
     std::cout << "Jacobian: "  << std::endl;
     std::cout << jacobian << std::endl;

     // copy the read one just for getting the right matrix size
     JacobianType   TheoreticalJacobian = jacobian;
     
     TheoreticalJacobian[0][0] =    0.0;
     TheoreticalJacobian[1][0] =  206.0;
     TheoreticalJacobian[2][0] =  -84.0;

     TheoreticalJacobian[0][1] = -206.0;
     TheoreticalJacobian[1][1] =    0.0;
     TheoreticalJacobian[2][1] =   42.0;

     TheoreticalJacobian[0][2] =   84.0;
     TheoreticalJacobian[1][2] =  -42.0;
     TheoreticalJacobian[2][2] =    0.0;

     TheoreticalJacobian[0][3] = 1.0;
     TheoreticalJacobian[1][3] = 0.0;
     TheoreticalJacobian[2][3] = 0.0;

     TheoreticalJacobian[0][4] = 0.0;
     TheoreticalJacobian[1][4] = 1.0;
     TheoreticalJacobian[2][4] = 0.0;

     TheoreticalJacobian[0][5] = 0.0;
     TheoreticalJacobian[1][5] = 0.0;
     TheoreticalJacobian[2][5] = 1.0;

     for(unsigned int ii=0; ii < 3; ii++)
       {
       for(unsigned int jj=0; jj < 6; jj++)
         {
         if( vnl_math_abs( TheoreticalJacobian[ii][jj] - jacobian[ii][jj] ) > 1e-5 )
           {
           std::cerr << "Jacobian components differ from expected values ";
           std::cerr << std::endl << std::endl;
           std::cerr << "Expected Jacobian = " << std::endl;
           std::cerr << TheoreticalJacobian << std::endl << std::endl;
           std::cerr << "Computed Jacobian = " << std::endl;
           std::cerr << jacobian << std::endl << std::endl;
           std::cerr << std::endl << "Test FAILED ! " << std::endl;
           return EXIT_FAILURE;
           }
         }
       }
  }

  {
  std::cout << " Exercise the SetIdentity() method " << std::endl; 
  TransformType::Pointer  transform = TransformType::New();

  itk::Vector<double,3> axis(1);

  const double angle = (atan(1.0)/45.0)*30.0; // turn 30 degrees

  transform->SetRotation( axis, angle );

  TransformType::InputPointType  center;
  center[0] = 31;
  center[1] = 62;
  center[2] = 93;
  
  transform->SetCenter( center );

  transform->SetIdentity();

  const unsigned int np = transform->GetNumberOfParameters();

  ParametersType parameters( np ); // Number of parameters

  VersorType versor;

  parameters[0] = versor.GetX();   // Rotation axis * sin(t/2)
  parameters[1] = versor.GetY();
  parameters[2] = versor.GetZ();
  parameters[3] = 0.0;             // Translation
  parameters[4] = 0.0;
  parameters[5] = 0.0;


  ParametersType parameters2 = transform->GetParameters();

  const double tolerance = 1e-8;

  for(unsigned int p=0; p<np; p++)
    {
    if( fabs( parameters[p] - parameters2[p] ) > tolerance )
      {
      std::cerr << "Output parameter does not match input " << std::endl;
      return EXIT_FAILURE;
      }
    }
  std::cout << "Input/Output parameter check Passed !"  << std::endl;
  }

  {
     // Testing SetMatrix()
     std::cout << "Testing SetMatrix() ... ";
     unsigned int par;
     bool Ok;

     typedef TransformType::MatrixType MatrixType;
     MatrixType matrix;

     TransformType::Pointer t = TransformType::New();
      
     // attempt to set an non-orthogonal matrix
     par = 0;
     for( unsigned int row = 0; row < 3; row++ )
        {
        for( unsigned int col = 0; col < 3; col++ )
          {
          matrix[row][col] = static_cast<double>( par + 1 );
          ++par;
          }
        }

     Ok = false;
     try
      {
      t->SetMatrix( matrix );
      }
     catch ( itk::ExceptionObject & itkNotUsed(err) )
      {
      Ok = true;
      }
     catch( ... )
      {
      std::cout << "Caught unknown exception" << std::endl;
      }

     if( !Ok )
      {
      std::cerr << "Error: expected to catch an exception when attempting";
      std::cerr << " to set an non-orthogonal matrix." << std::endl;
      return EXIT_FAILURE;
      }

      t = TransformType::New();

      // attempt to set an orthogonal matrix
      matrix.GetVnlMatrix().set_identity();

      double a = 1.0 / 180.0 * vnl_math::pi;
      matrix[0][0] =        cos( a );
      matrix[0][1] = -1.0 * sin( a );
      matrix[1][0] =        sin( a ); 
      matrix[1][1] =        cos( a );

     Ok = true;
     try
      {
      t->SetMatrix( matrix );
      }
     catch ( itk::ExceptionObject & err )
      {
      std::cout << err << std::endl;
      Ok = false;
      }
     catch( ... )
      {
      std::cout << "Caught unknown exception" << std::endl;
      Ok = false;
      }

     if( !Ok )
      {
      std::cerr << "Error: caught unexpected exception" << std::endl;
      return EXIT_FAILURE;
      }

   // Check the computed parameters

    typedef TransformType::ParametersType ParametersType;
    ParametersType e( t->GetNumberOfParameters() );
    e.Fill( 0.0 );
    e[2] = sin(0.5 * a);

    t = TransformType::New();
    t->SetParameters( e );

    TransformType::Pointer t2 = TransformType::New();
    t2->SetMatrix( t->GetMatrix() );

    ParametersType p = t2->GetParameters();

    for( unsigned int k = 0; k < e.GetSize(); k++ )
      {
      if( fabs( e[k] - p[k] ) > epsilon )
        {
        std::cout << " [ FAILED ] " << std::endl;
        std::cout << "Expected parameters: " << e << std::endl;
        std::cout << "but got: " << p << std::endl;
        return EXIT_FAILURE; 
        }
      }

    std::cout << "[ PASSED ]" << std::endl;
    }


  
  std::cout << std::endl << "Test PASSED ! " << std::endl;
  return EXIT_SUCCESS;

}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -