Permalink
Browse files

get the qform=>matrix in nifti_sa right (hopefully) and be a bit more…

… verbose there
  • Loading branch information...
1 parent aef9861 commit 6cdc2ee2b9a1143618c031c48f2733d2b52b05f0 @DerOrfa DerOrfa committed Apr 1, 2012
Showing with 30 additions and 13 deletions.
  1. +30 −13 lib/ImageIO/imageFormat_nifti_sa.cpp
@@ -846,9 +846,9 @@ util::Matrix4x4<double> ImageFormat_NiftiSa::getNiftiMatrix( const util::Propert
void ImageFormat_NiftiSa::useSForm( util::PropertyMap &props )
{
// srow_? is the linear map from image space to nifti space (not isis space)
- // [x] [ nifti/srow_x ] [i]
- // [y]=[ nifti/srow_y ] * [j]
- // [z] [ nifti/srow_z ] [k]
+ // [x_nii] [ nifti/srow_x ] [i]
+ // [x_nii]=[ nifti/srow_y ] * [j]
+ // [x_nii] [ nifti/srow_z ] [k]
LOG( Debug, info ) << "Using sform (" << props.propertyValue( "nifti/sform_code" ).toString() << ") " << util::MSubject(
props.propertyValue( "nifti/srow_x" ).toString() + "-" +
@@ -930,17 +930,34 @@ void ImageFormat_NiftiSa::useQForm( util::PropertyMap &props )
}
LOG( Debug, info )
- << "Using qform (" << props.propertyValue( "nifti/qform_code" ).toString()
- << ") quaternion=" << util::fvector4( a, b, c, d ) << " with qfac=" << props.propertyValue( "nifti/qfac" ).toString()
- << ", pixdim=" << props.propertyValue( "nifti/pixdim" ).toString()
- << " and qoffset= " << props.propertyValue( "nifti/qoffset" ).toString();
-
- const util::Matrix4x4<double> M(
- util::fvector4( a * a + b * b - c * c - d * d, 2 * b * c - 2 * a * d, 2 * b * d + 2 * a * c ),
- util::fvector4( 2 * b * c + 2 * a * d, a * a + c * c - b * b - d * d, 2 * c * d - 2 * a * b ),
- util::fvector4( 2 * b * d - 2 * a * c, 2 * c * d + 2 * a * b, a * a + d * d - c * c - b * b )*props.getPropertyAs<float>( "nifti/qfac" )
+ << "Using qform (" << props.propertyValue( "nifti/qform_code" ).toString()
+ << ") quaternion=" << util::fvector4( a, b, c, d ) << " with qfac=" << props.propertyValue( "nifti/qfac" ).toString()
+ << ", pixdim=" << props.propertyValue( "nifti/pixdim" ).toString()
+ << " and qoffset= " << props.propertyValue( "nifti/qoffset" ).toString();
+
+ const double a2 = a * a, b2 = b * b, c2 = c * c, d2 = d * d;
+ const double _2ab = 2 * a * b, _2ac = 2 * a * c, _2ad = 2 * a * d;
+ const double _2bc = 2 * b * c, _2bd = 2 * b * d;
+ const double _2cd = 2 * c * d;
+
+ const double r_11 = a2 + b2 - c2 - d2, r_12 = _2bc - _2ad, r_13 = _2bd + _2ac;
+ const double r_21 = _2bc + _2ad, r_22 = a2 - b2 + c2 - d2, r_23 = _2cd - _2ab;
+ const double r_31 = _2bd - _2ac, r_32 = _2cd + _2ab, r_33 = a2 - b2 - c2 + d2;
+ const int qfac = props.getPropertyAs<float>( "nifti/qfac" );
+
+ const util::Matrix4x4<double> image2nifti(
+ util::fvector4( r_11, r_12, r_13 * qfac ),
+ util::fvector4( r_21, r_22, r_23 * qfac ),
+ util::fvector4( r_31, r_32, r_33 * qfac )
);
- const util::Matrix4x4<double> image2isis = nifti2isis.dot( M );
+
+ LOG( Debug, info )
+ << "The matrix made from the qform is "
+ << util::fvector4( r_11, r_12, r_13 * qfac ) << "-"
+ << util::fvector4( r_21, r_22, r_23 * qfac ) << "-"
+ << util::fvector4( r_31, r_32, r_33 * qfac );
+
+ const util::Matrix4x4<double> image2isis = nifti2isis.dot( image2nifti );
props.setPropertyAs<util::fvector4>( "rowVec", image2isis.transpose().getRow( 0 ) );
props.setPropertyAs<util::fvector4>( "columnVec", image2isis.transpose().getRow( 1 ) );

0 comments on commit 6cdc2ee

Please sign in to comment.