/// <summary> /// Gets a rotation matrix that, when multiplied by a column matrix representing a /// position vector in patient coordinates, will rotate the position vector /// into a coordinate system matching that of the image plane. /// </summary> /// <returns>The rotation matrix, or null if the <see cref="Frame"/>'s position information is invalid.</returns> private Matrix3D GetRotationMatrix() { if (_rotationMatrix == null) { if (ImageOrientationPatient.IsNull) return null; var normal = GetNormalVector(); if (normal == null || normal.IsNull) return null; _rotationMatrix = new Matrix3D(); _rotationMatrix.SetRow(0, (float) ImageOrientationPatient.RowX, (float) ImageOrientationPatient.RowY, (float) ImageOrientationPatient.RowZ); _rotationMatrix.SetRow(1, (float) ImageOrientationPatient.ColumnX, (float) ImageOrientationPatient.ColumnY, (float) ImageOrientationPatient.ColumnZ); _rotationMatrix.SetRow(2, normal); } return _rotationMatrix; }
private static void Rotate(Matrix3D transform, float angle, float x, float y, float z) { const double rads = Math.PI/180; var c0 = Math.Cos(rads*angle); var c1 = 1 - c0; var xc1 = x*c1; var yc1 = y*c1; var zc1 = z*c1; var s = Math.Sin(rads*angle); var xs = x*s; var ys = y*s; var zs = z*s; var r = new Matrix3D(new[,] { {(float) (x*xc1 + c0), (float) (x*yc1 - zs), (float) (x*zc1 + ys)}, {(float) (y*xc1 + zs), (float) (y*yc1 + c0), (float) (y*zc1 - xs)}, {(float) (z*xc1 - ys), (float) (z*yc1 + xs), (float) (z*zc1 + c0)} }); var m = r*transform; transform.SetRow(0, m[0, 0], m[0, 1], m[0, 2]); transform.SetRow(1, m[1, 0], m[1, 1], m[1, 2]); transform.SetRow(2, m[2, 0], m[2, 1], m[2, 2]); }