public static double[] Multiply(vtkLandmarkTransform landMatrix, double[] value) { vtkMatrix4x4 matrix = landMatrix.GetMatrix(); int size = Marshal.SizeOf(typeof(double)) * value.Length; IntPtr pre = Marshal.AllocHGlobal(size); Marshal.Copy(value, 0, pre, value.Length); double[] result = matrix.MultiplyDoublePoint(pre); Marshal.FreeHGlobal(pre); return(result); }
static void AlignFrames(Frame sourceFrame, Frame targetFrame, ref vtkTransform transform) { // This function takes two frames and finds the matrix M between them. vtkLandmarkTransform landmarkTransform = vtkLandmarkTransform.New(); // Setup source points vtkPoints sourcePoints = vtkPoints.New(); sourcePoints.InsertNextPoint( sourceFrame.Origin[0], sourceFrame.Origin[1], sourceFrame.Origin[2]); float[] sourceX = new float[3]; float[] sourceY = new float[3]; float[] sourceZ = new float[3]; Add(sourceFrame.Origin, sourceFrame.XDirection, ref sourceX); Add(sourceFrame.Origin, sourceFrame.YDirection, ref sourceY); Add(sourceFrame.Origin, sourceFrame.ZDirection, ref sourceZ); sourcePoints.InsertNextPoint(sourceX[0], sourceX[1], sourceX[2]); sourcePoints.InsertNextPoint(sourceY[0], sourceY[1], sourceY[2]); sourcePoints.InsertNextPoint(sourceZ[0], sourceZ[1], sourceZ[2]); // Setup target points vtkPoints targetPoints = vtkPoints.New(); targetPoints.InsertNextPoint(targetFrame.Origin[0], targetFrame.Origin[1], targetFrame.Origin[2]); float[] targetX = new float[3]; float[] targetY = new float[3]; float[] targetZ = new float[3]; Add(targetFrame.Origin, targetFrame.XDirection, ref targetX); Add(targetFrame.Origin, targetFrame.YDirection, ref targetY); Add(targetFrame.Origin, targetFrame.ZDirection, ref targetZ); targetPoints.InsertNextPoint(targetX[0], targetX[1], targetX[2]); targetPoints.InsertNextPoint(targetY[0], targetY[1], targetY[2]); targetPoints.InsertNextPoint(targetZ[0], targetZ[1], targetZ[2]); landmarkTransform.SetSourceLandmarks(sourcePoints); landmarkTransform.SetTargetLandmarks(targetPoints); landmarkTransform.SetModeToRigidBody(); landmarkTransform.Update(); vtkMatrix4x4 M = landmarkTransform.GetMatrix(); transform.SetMatrix(M); }
public static vtkMatrix4x4 GetTranform(double[] soucePoint1, double[] soucePoint2, double[] soucePoint3, double[] targetPoint1, double[] targetPoint2, double[] targetPoint3) { vtkPoints sourcePoints = new vtkPoints(); sourcePoints.InsertNextPoint(soucePoint1[0], soucePoint1[1], soucePoint1[2]); sourcePoints.InsertNextPoint(soucePoint2[0], soucePoint2[1], soucePoint2[2]); sourcePoints.InsertNextPoint(soucePoint3[0], soucePoint3[1], soucePoint3[2]); vtkPoints targetPoints = new vtkPoints(); targetPoints.InsertNextPoint(targetPoint1[0], targetPoint1[1], targetPoint1[2]); targetPoints.InsertNextPoint(targetPoint2[0], targetPoint2[1], targetPoint2[2]); targetPoints.InsertNextPoint(targetPoint3[0], targetPoint3[1], targetPoint3[2]); vtkLandmarkTransform landmark = new vtkLandmarkTransform(); landmark.SetSourceLandmarks(sourcePoints); landmark.SetTargetLandmarks(targetPoints); landmark.Update(); return(landmark.GetMatrix()); }