public void UpdatePosition_OLD(double[] endPoint, double[] startPoint) { double[] normalizedX = new double[3]; double[] normalizedY = new double[3]; double[] normalizedZ = new double[3]; // The X axis is a vector from start to end Subtract(endPoint, startPoint, normalizedX); double length = vtkMath.Norm(VTKUtil.ConvertIntPtr(normalizedX)); vtkMath.Normalize(VTKUtil.ConvertIntPtr(normalizedX)); // The Z axis is an arbitrary vecotr cross X double[] arbitrary = new double[3]; arbitrary[0] = vtkMath.Random(-10, 10); arbitrary[1] = vtkMath.Random(-10, 10); arbitrary[2] = vtkMath.Random(-10, 10); // TODO FIX ME vtkMath.Cross(VTKUtil.ConvertIntPtr(normalizedX), VTKUtil.ConvertIntPtr(arbitrary), VTKUtil.ConvertIntPtr(normalizedZ)); vtkMath.Normalize(VTKUtil.ConvertIntPtr(normalizedZ)); // The Y axis is Z cross X // TODO FIX ME vtkMath.Cross(VTKUtil.ConvertIntPtr(normalizedZ), VTKUtil.ConvertIntPtr(normalizedX), VTKUtil.ConvertIntPtr(normalizedY)); vtkMatrix4x4 matrix = vtkMatrix4x4.New(); // Create the direction cosine matrix matrix.Identity(); for (int i = 0; i < 3; i++) { matrix.SetElement(i, 0, normalizedX[i]); matrix.SetElement(i, 1, normalizedY[i]); matrix.SetElement(i, 2, normalizedZ[i]); } // Apply the transforms vtkTransform transform = vtkTransform.New(); transform.Translate(VTKUtil.ConvertIntPtr(startPoint)); transform.Concatenate(matrix); //length = 500; transform.Scale(length, length, length); // Transform the polydata //vtkTransformPolyDataFilter transformPD = new vtkTransformPolyDataFilter(); //transformPD.SetTransform(transform); //transformPD.SetInputConnection(_arrowSource.GetOutputPort()); _arrowSource.Update(); _mapper.SetInput(_arrowSource.GetOutput()); _arrowActor.SetUserMatrix(transform.GetMatrix()); _arrowActor.SetMapper(_mapper); }
public void ResetMatrix() { vtkMatrix4x4 matrix = actor.GetMatrix(); matrix.Zero(); matrix.SetElement(0, 0, 1); matrix.SetElement(1, 1, 1); matrix.SetElement(2, 2, 1); matrix.SetElement(3, 3, 1); }
public static void SetElements(this vtkMatrix4x4 vtkMatrix, Matrix matrix) { for (var i = 0; i < 16; i++) { var row = (i & 0x0C) >> 2; var column = i & 0x03; vtkMatrix.SetElement(row, column, matrix[row, column]); } }
/// <summary> /// Converts a <see cref="Matrix"/> to a <see cref="vtkMatrix4x4"/>. /// </summary> /// <remarks> /// The <see cref="vtkMatrix4x4"/> matrix is equivalent to <see cref="Matrix"/> transposed! /// This is due to the fact that vtkMatrix4x4 uses (x,y) addresses whereas Matrix /// uses (row,column). /// </remarks> /// <param name="matrix">The source <see cref="Matrix"/>.</param> /// <returns>The equivalent <see cref="vtkMatrix4x4"/>.</returns> public static vtkMatrix4x4 ConvertToVtkMatrix(Matrix matrix) { vtkMatrix4x4 vtkMatrix = new vtkMatrix4x4(); for (int row = 0; row < 4; row++) for (int column = 0; column < 4; column++) vtkMatrix.SetElement(column, row, matrix[row, column]); return vtkMatrix; }
// For reading C# matrices stored in scenario into VTK transforms public void TransferMatrixToVTKTransform(double[][] matrix, vtkTransform transform) { vtkMatrix4x4 vtk_matrix = vtkMatrix4x4.New(); for (int ii = 0; ii < 4; ++ii) { for (int jj = 0; jj < 4; ++jj) { vtk_matrix.SetElement(ii, jj, matrix[ii][jj]); } } transform.SetMatrix(vtk_matrix); }
/// <summary> /// Converts a <see cref="Matrix"/> to a <see cref="vtkMatrix4x4"/>. /// </summary> /// <remarks> /// The <see cref="vtkMatrix4x4"/> matrix is equivalent to <see cref="Matrix"/> transposed! /// This is due to the fact that vtkMatrix4x4 uses (x,y) addresses whereas Matrix /// uses (row,column). /// </remarks> /// <param name="matrix">The source <see cref="Matrix"/>.</param> /// <returns>The equivalent <see cref="vtkMatrix4x4"/>.</returns> public static vtkMatrix4x4 ConvertToVtkMatrix(Matrix matrix) { vtkMatrix4x4 vtkMatrix = new vtkMatrix4x4(); for (int row = 0; row < 4; row++) { for (int column = 0; column < 4; column++) { vtkMatrix.SetElement(column, row, matrix[row, column]); } } return(vtkMatrix); }
public static vtkMatrix4x4 Convert(double[] elems) { vtkMatrix4x4 matrix = new vtkMatrix4x4(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { matrix.SetElement(i, j, elems[i * 4 + j]); Console.Write(elems[i * 4 + j]); } Console.WriteLine(); } return(matrix); }
/// <summary> /// Gets the pixel data representing a thick slice (a.k.a. slab) of the volume. /// </summary> private static byte[] GetSlabPixelData(IVolumeReference volumeReference, Matrix resliceAxes, Vector3D stackOrientation, int rows, int columns, int subsamples, float rowSpacing, float columnSpacing, float sliceThickness, VolumeInterpolationMode interpolation, VolumeProjectionMode projection) { if (subsamples == 0) { return(GetSlicePixelData(volumeReference, resliceAxes, rows, columns, rowSpacing, columnSpacing, interpolation)); } var subsampleSpacing = sliceThickness / (subsamples - 1); using (var reslicer = new vtkImageReslice()) using (var resliceAxesMatrix = new vtkMatrix4x4()) using (var executive = reslicer.GetExecutive()) using (var volume = volumeReference.Volume.CreateVtkVolumeHandle()) { // update the reslice axes matrix with the values from the slicing orientation resliceAxesMatrix.SetElements(resliceAxes); // determine offset for start of slab (we centre the slab on the requested slice position, as DICOM defines "image position (patient)" to be centre of the thick slice) var slabOffset = volumeReference.RotateToVolumeOrientation(-sliceThickness / 2f * stackOrientation) + new Vector3D(resliceAxes[0, 3], resliceAxes[1, 3], resliceAxes[2, 3]); resliceAxesMatrix.SetElement(0, 3, slabOffset.X); resliceAxesMatrix.SetElement(1, 3, slabOffset.Y); resliceAxesMatrix.SetElement(2, 3, slabOffset.Z); // register for errors on the reslicer reslicer.RegisterVtkErrorEvents(); // set input to the volume reslicer.SetInput(volume.vtkImageData); reslicer.SetInformationInput(volume.vtkImageData); // instruct reslicer to output a 3D slab volume reslicer.SetOutputDimensionality(3); // use the volume's padding value for all pixels that are outside the volume reslicer.SetBackgroundLevel(volumeReference.PaddingValue); // ensure the slicer outputs at our desired spacing reslicer.SetOutputSpacing(columnSpacing, rowSpacing, subsampleSpacing); // set the reslice axes reslicer.SetResliceAxes(resliceAxesMatrix); // clamp the output based on the slice extent reslicer.SetOutputExtent(0, columns - 1, 0, rows - 1, 0, subsamples - 1); // set the output origin to the slice through-point (output image will be centered on this location) // VTK output origin is derived from the center image being 0,0 reslicer.SetOutputOrigin(-columns * columnSpacing / 2, -rows * rowSpacing / 2, 0); // select the requested interpolation mode switch (interpolation) { case VolumeInterpolationMode.NearestNeighbor: reslicer.SetInterpolationModeToNearestNeighbor(); break; case VolumeInterpolationMode.Linear: reslicer.SetInterpolationModeToLinear(); break; case VolumeInterpolationMode.Cubic: reslicer.SetInterpolationModeToCubic(); break; } // select the requested slab projection mode Action <IntPtr, byte[], int, int, int, bool> slabAggregator; switch (projection) { case VolumeProjectionMode.Maximum: slabAggregator = SlabProjection.AggregateSlabMaximumIntensity; break; case VolumeProjectionMode.Minimum: slabAggregator = SlabProjection.AggregateSlabMinimumIntensity; break; case VolumeProjectionMode.Average: default: slabAggregator = SlabProjection.AggregateSlabAverageIntensity; break; } // register for errors on the reslicer executive executive.RegisterVtkErrorEvents(); executive.Update(); // get the slice output using (var imageData = reslicer.GetOutput()) { var pixelData = SlabVtkImageData(imageData, slabAggregator, volumeReference.BitsPerVoxel, volumeReference.Signed); imageData.ReleaseData(); return(pixelData); } } }
/// <summary> /// Gets the pixel data representing a thick slice (a.k.a. slab) of the volume. /// </summary> private static byte[] GetSlabPixelData(IVolumeReference volumeReference, Matrix resliceAxes, Vector3D stackOrientation, int rows, int columns, int subsamples, float rowSpacing, float columnSpacing, float sliceThickness, VolumeInterpolationMode interpolation, VolumeProjectionMode projection) { if (subsamples <= 1) return GetSlicePixelData(volumeReference, resliceAxes, rows, columns, rowSpacing, columnSpacing, interpolation); var subsampleSpacing = sliceThickness/(subsamples - 1); using (var reslicer = new vtkImageReslice()) using (var resliceAxesMatrix = new vtkMatrix4x4()) using (var executive = reslicer.GetExecutive()) using (var volume = volumeReference.Volume.CreateVtkVolumeHandle()) { // update the reslice axes matrix with the values from the slicing orientation resliceAxesMatrix.SetElements(resliceAxes); // determine offset for start of slab (we centre the slab on the requested slice position, as DICOM defines "image position (patient)" to be centre of the thick slice) var slabOffset = volumeReference.RotateToVolumeOrientation(-sliceThickness/2f*stackOrientation) + new Vector3D(resliceAxes[0, 3], resliceAxes[1, 3], resliceAxes[2, 3]); resliceAxesMatrix.SetElement(0, 3, slabOffset.X); resliceAxesMatrix.SetElement(1, 3, slabOffset.Y); resliceAxesMatrix.SetElement(2, 3, slabOffset.Z); // register for errors on the reslicer reslicer.RegisterVtkErrorEvents(); // set input to the volume reslicer.SetInput(volume.vtkImageData); reslicer.SetInformationInput(volume.vtkImageData); // instruct reslicer to output a 3D slab volume reslicer.SetOutputDimensionality(3); // use the volume's padding value for all pixels that are outside the volume reslicer.SetBackgroundLevel(volumeReference.PaddingValue); // ensure the slicer outputs at our desired spacing reslicer.SetOutputSpacing(columnSpacing, rowSpacing, subsampleSpacing); // set the reslice axes reslicer.SetResliceAxes(resliceAxesMatrix); // clamp the output based on the slice extent reslicer.SetOutputExtent(0, columns - 1, 0, rows - 1, 0, subsamples - 1); // set the output origin to the slice through-point (output image will be centered on this location) // VTK output origin is derived from the center image being 0,0 reslicer.SetOutputOrigin(-columns*columnSpacing/2, -rows*rowSpacing/2, 0); // select the requested interpolation mode switch (interpolation) { case VolumeInterpolationMode.NearestNeighbor: reslicer.SetInterpolationModeToNearestNeighbor(); break; case VolumeInterpolationMode.Linear: reslicer.SetInterpolationModeToLinear(); break; case VolumeInterpolationMode.Cubic: reslicer.SetInterpolationModeToCubic(); break; } // select the requested slab projection mode Action<IntPtr, byte[], int, int, int, bool> slabAggregator; switch (projection) { case VolumeProjectionMode.Maximum: slabAggregator = SlabProjection.AggregateSlabMaximumIntensity; break; case VolumeProjectionMode.Minimum: slabAggregator = SlabProjection.AggregateSlabMinimumIntensity; break; case VolumeProjectionMode.Average: default: slabAggregator = SlabProjection.AggregateSlabAverageIntensity; break; } // register for errors on the reslicer executive executive.RegisterVtkErrorEvents(); executive.Update(); // get the slice output using (var imageData = reslicer.GetOutput()) { var pixelData = SlabVtkImageData(imageData, slabAggregator, volumeReference.BitsPerVoxel, volumeReference.Signed); imageData.ReleaseData(); return pixelData; } } }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVMatrixToTransform(String [] argv) { //Prefix Content is: "" // This example demonstrates how to use a matrix in place of a transfrom[] // via vtkMatrixToLinearTransform and vtkMatrixToHomogeneousTransform.[] // create a rendering window[] renWin = vtkRenderWindow.New(); renWin.SetSize((int)600,(int)300); // set up first set of polydata[] p1 = new vtkPlaneSource(); p1.SetOrigin((double)0.5,(double)0.508,(double)-0.5); p1.SetPoint1((double)-0.5,(double)0.508,(double)-0.5); p1.SetPoint2((double)0.5,(double)0.508,(double)0.5); p1.SetXResolution((int)5); p1.SetYResolution((int)5); p2 = new vtkPlaneSource(); p2.SetOrigin((double)-0.508,(double)0.5,(double)-0.5); p2.SetPoint1((double)-0.508,(double)-0.5,(double)-0.5); p2.SetPoint2((double)-0.508,(double)0.5,(double)0.5); p2.SetXResolution((int)5); p2.SetYResolution((int)5); p3 = new vtkPlaneSource(); p3.SetOrigin((double)-0.5,(double)-0.508,(double)-0.5); p3.SetPoint1((double)0.5,(double)-0.508,(double)-0.5); p3.SetPoint2((double)-0.5,(double)-0.508,(double)0.5); p3.SetXResolution((int)5); p3.SetYResolution((int)5); p4 = new vtkPlaneSource(); p4.SetOrigin((double)0.508,(double)-0.5,(double)-0.5); p4.SetPoint1((double)0.508,(double)0.5,(double)-0.5); p4.SetPoint2((double)0.508,(double)-0.5,(double)0.5); p4.SetXResolution((int)5); p4.SetYResolution((int)5); p5 = new vtkPlaneSource(); p5.SetOrigin((double)0.5,(double)0.5,(double)-0.508); p5.SetPoint1((double)0.5,(double)-0.5,(double)-0.508); p5.SetPoint2((double)-0.5,(double)0.5,(double)-0.508); p5.SetXResolution((int)5); p5.SetYResolution((int)5); p6 = new vtkPlaneSource(); p6.SetOrigin((double)0.5,(double)0.5,(double)0.508); p6.SetPoint1((double)-0.5,(double)0.5,(double)0.508); p6.SetPoint2((double)0.5,(double)-0.5,(double)0.508); p6.SetXResolution((int)5); p6.SetYResolution((int)5); // append together[] ap = new vtkAppendPolyData(); ap.AddInputConnection(p1.GetOutputPort()); ap.AddInputConnection(p2.GetOutputPort()); ap.AddInputConnection(p3.GetOutputPort()); ap.AddInputConnection(p4.GetOutputPort()); ap.AddInputConnection(p5.GetOutputPort()); ap.AddInputConnection(p6.GetOutputPort()); //--------------------------[] // linear transform matrix[] t1 = new vtkMatrixToLinearTransform(); m1 = new vtkMatrix4x4(); t1.SetInput((vtkMatrix4x4)m1); m1.SetElement((int)0,(int)0,(double)1.127631); m1.SetElement((int)0,(int)1,(double)0.205212); m1.SetElement((int)0,(int)2,(double)-0.355438); m1.SetElement((int)1,(int)0,(double)0.000000); m1.SetElement((int)1,(int)1,(double)0.692820); m1.SetElement((int)1,(int)2,(double)0.400000); m1.SetElement((int)2,(int)0,(double)0.200000); m1.SetElement((int)2,(int)1,(double)-0.469846); m1.SetElement((int)2,(int)2,(double)0.813798); f11 = new vtkTransformPolyDataFilter(); f11.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f11.SetTransform((vtkAbstractTransform)t1); m11 = new vtkDataSetMapper(); m11.SetInputConnection((vtkAlgorithmOutput)f11.GetOutputPort()); a11 = new vtkActor(); a11.SetMapper((vtkMapper)m11); a11.GetProperty().SetColor((double)1,(double)0,(double)0); a11.GetProperty().SetRepresentationToWireframe(); ren11 = vtkRenderer.New(); ren11.SetViewport((double)0.0,(double)0.5,(double)0.25,(double)1.0); ren11.ResetCamera((double)-0.5,(double)0.5,(double)-0.5,(double)0.5,(double)-1,(double)1); ren11.AddActor((vtkProp)a11); renWin.AddRenderer((vtkRenderer)ren11); // inverse identity transform[] f12 = new vtkTransformPolyDataFilter(); f12.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f12.SetTransform((vtkAbstractTransform)t1.GetInverse()); m12 = new vtkDataSetMapper(); m12.SetInputConnection((vtkAlgorithmOutput)f12.GetOutputPort()); a12 = new vtkActor(); a12.SetMapper((vtkMapper)m12); a12.GetProperty().SetColor((double)0.9,(double)0.9,(double)0); a12.GetProperty().SetRepresentationToWireframe(); ren12 = vtkRenderer.New(); ren12.SetViewport((double)0.0,(double)0.0,(double)0.25,(double)0.5); ren12.ResetCamera((double)-0.5,(double)0.5,(double)-0.5,(double)0.5,(double)-1,(double)1); ren12.AddActor((vtkProp)a12); renWin.AddRenderer((vtkRenderer)ren12); //--------------------------[] // perspective transform matrix[] m2 = new vtkMatrix4x4(); m2.SetElement((int)3,(int)0,(double)-0.11); m2.SetElement((int)3,(int)1,(double)0.3); m2.SetElement((int)3,(int)2,(double)0.2); t2 = new vtkMatrixToHomogeneousTransform(); t2.SetInput((vtkMatrix4x4)m2); f21 = new vtkTransformPolyDataFilter(); f21.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f21.SetTransform((vtkAbstractTransform)t2); m21 = new vtkDataSetMapper(); m21.SetInputConnection((vtkAlgorithmOutput)f21.GetOutputPort()); a21 = new vtkActor(); a21.SetMapper((vtkMapper)m21); a21.GetProperty().SetColor((double)1,(double)0,(double)0); a21.GetProperty().SetRepresentationToWireframe(); ren21 = vtkRenderer.New(); ren21.SetViewport((double)0.25,(double)0.5,(double)0.50,(double)1.0); ren21.ResetCamera((double)-0.5,(double)0.5,(double)-0.5,(double)0.5,(double)-1,(double)1); ren21.AddActor((vtkProp)a21); renWin.AddRenderer((vtkRenderer)ren21); // inverse linear transform[] f22 = new vtkTransformPolyDataFilter(); f22.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f22.SetTransform((vtkAbstractTransform)t2.GetInverse()); m22 = new vtkDataSetMapper(); m22.SetInputConnection((vtkAlgorithmOutput)f22.GetOutputPort()); a22 = new vtkActor(); a22.SetMapper((vtkMapper)m22); a22.GetProperty().SetColor((double)0.9,(double)0.9,(double)0); a22.GetProperty().SetRepresentationToWireframe(); ren22 = vtkRenderer.New(); ren22.SetViewport((double)0.25,(double)0.0,(double)0.50,(double)0.5); ren22.ResetCamera((double)-0.5,(double)0.5,(double)-0.5,(double)0.5,(double)-1,(double)1); ren22.AddActor((vtkProp)a22); renWin.AddRenderer((vtkRenderer)ren22); //--------------------------[] // linear concatenation - should end up with identity here[] t3 = new vtkTransform(); t3.Concatenate((vtkLinearTransform)t1); t3.Concatenate((vtkLinearTransform)t1.GetInverse()); f31 = new vtkTransformPolyDataFilter(); f31.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f31.SetTransform((vtkAbstractTransform)t3); m31 = new vtkDataSetMapper(); m31.SetInputConnection((vtkAlgorithmOutput)f31.GetOutputPort()); a31 = new vtkActor(); a31.SetMapper((vtkMapper)m31); a31.GetProperty().SetColor((double)1,(double)0,(double)0); a31.GetProperty().SetRepresentationToWireframe(); ren31 = vtkRenderer.New(); ren31.SetViewport((double)0.50,(double)0.5,(double)0.75,(double)1.0); ren31.ResetCamera((double)-0.5,(double)0.5,(double)-0.5,(double)0.5,(double)-1,(double)1); ren31.AddActor((vtkProp)a31); renWin.AddRenderer((vtkRenderer)ren31); // inverse linear transform[] f32 = new vtkTransformPolyDataFilter(); f32.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f32.SetTransform((vtkAbstractTransform)t3.GetInverse()); m32 = new vtkDataSetMapper(); m32.SetInputConnection((vtkAlgorithmOutput)f32.GetOutputPort()); a32 = new vtkActor(); a32.SetMapper((vtkMapper)m32); a32.GetProperty().SetColor((double)0.9,(double)0.9,(double)0); a32.GetProperty().SetRepresentationToWireframe(); ren32 = vtkRenderer.New(); ren32.SetViewport((double)0.5,(double)0.0,(double)0.75,(double)0.5); ren32.ResetCamera((double)-0.5,(double)0.5,(double)-0.5,(double)0.5,(double)-1,(double)1); ren32.AddActor((vtkProp)a32); renWin.AddRenderer((vtkRenderer)ren32); //--------------------------[] // perspective transform concatenation[] t4 = new vtkPerspectiveTransform(); t4.Concatenate((vtkHomogeneousTransform)t1); t4.Concatenate((vtkHomogeneousTransform)t2); t4.Concatenate((vtkHomogeneousTransform)t3); f41 = new vtkTransformPolyDataFilter(); f41.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f41.SetTransform((vtkAbstractTransform)t4); m41 = new vtkDataSetMapper(); m41.SetInputConnection((vtkAlgorithmOutput)f41.GetOutputPort()); a41 = new vtkActor(); a41.SetMapper((vtkMapper)m41); a41.GetProperty().SetColor((double)1,(double)0,(double)0); a41.GetProperty().SetRepresentationToWireframe(); ren41 = vtkRenderer.New(); ren41.SetViewport((double)0.75,(double)0.5,(double)1.0,(double)1.0); ren41.ResetCamera((double)-0.5,(double)0.5,(double)-0.5,(double)0.5,(double)-1,(double)1); ren41.AddActor((vtkProp)a41); renWin.AddRenderer((vtkRenderer)ren41); // inverse of transform concatenation[] f42 = new vtkTransformPolyDataFilter(); f42.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f42.SetTransform((vtkAbstractTransform)t4.GetInverse()); m42 = new vtkDataSetMapper(); m42.SetInputConnection((vtkAlgorithmOutput)f42.GetOutputPort()); a42 = new vtkActor(); a42.SetMapper((vtkMapper)m42); a42.GetProperty().SetColor((double)0.9,(double)0.9,(double)0); a42.GetProperty().SetRepresentationToWireframe(); ren42 = vtkRenderer.New(); ren42.SetViewport((double)0.75,(double)0.0,(double)1.0,(double)0.5); ren42.ResetCamera((double)-0.5,(double)0.5,(double)-0.5,(double)0.5,(double)-1,(double)1); ren42.AddActor((vtkProp)a42); renWin.AddRenderer((vtkRenderer)ren42); renWin.Render(); //deleteAllVTKObjects(); }
private void OrientedArrow() { //Create an arrow. vtkArrowSource arrowSource = vtkArrowSource.New(); // Generate a random start and end point vtkMath.RandomSeed(8775070); double[] startPoint = new double[] { vtkMath.Random(-10, 10), vtkMath.Random(-10, 10), vtkMath.Random(-10, 10) }; double[] endPoint = new double[] { vtkMath.Random(-10, 10), vtkMath.Random(-10, 10), vtkMath.Random(-10, 10) }; // Compute a basis double[] normalizedX = new double[3]; double[] normalizedY = new double[3]; double[] normalizedZ = new double[3]; // The X axis is a vector from start to end myMath.Subtract(endPoint, startPoint, ref normalizedX); double length = myMath.Norm(normalizedX); myMath.Normalize(ref normalizedX); // The Z axis is an arbitrary vector cross X double[] arbitrary = new double[] { vtkMath.Random(-10, 10), vtkMath.Random(-10, 10), vtkMath.Random(-10, 10) }; myMath.Cross(normalizedX, arbitrary, ref normalizedZ); myMath.Normalize(ref normalizedZ); // The Y axis is Z cross X myMath.Cross(normalizedZ, normalizedX, ref normalizedY); vtkMatrix4x4 matrix = vtkMatrix4x4.New(); // Create the direction cosine matrix matrix.Identity(); for (int i = 0; i < 3; i++) { matrix.SetElement(i, 0, normalizedX[i]); matrix.SetElement(i, 1, normalizedY[i]); matrix.SetElement(i, 2, normalizedZ[i]); } // Apply the transforms vtkTransform transform = vtkTransform.New(); transform.Translate(startPoint[0], startPoint[1], startPoint[2]); transform.Concatenate(matrix); transform.Scale(length, length, length); //Create a mapper and actor for the arrow vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); vtkActor actor = vtkActor.New(); #if USER_MATRIX mapper.SetInputConnection(arrowSource.GetOutputPort()); actor.SetUserMatrix(transform.GetMatrix()); #else // Transform the polydata vtkTransformPolyDataFilter transformPD = vtkTransformPolyDataFilter.New(); transformPD.SetTransform(transform); transformPD.SetInputConnection(arrowSource.GetOutputPort()); mapper.SetInputConnection(transformPD.GetOutputPort()); #endif actor.SetMapper(mapper); // Create spheres for start and end point vtkSphereSource sphereStartSource = vtkSphereSource.New(); sphereStartSource.SetCenter(startPoint[0], startPoint[1], startPoint[2]); vtkPolyDataMapper sphereStartMapper = vtkPolyDataMapper.New(); sphereStartMapper.SetInputConnection(sphereStartSource.GetOutputPort()); vtkActor sphereStart = vtkActor.New(); sphereStart.SetMapper(sphereStartMapper); sphereStart.GetProperty().SetColor(1.0, 1.0, .3); vtkSphereSource sphereEndSource = vtkSphereSource.New(); sphereEndSource.SetCenter(endPoint[0], endPoint[1], endPoint[2]); vtkPolyDataMapper sphereEndMapper = vtkPolyDataMapper.New(); sphereEndMapper.SetInputConnection(sphereEndSource.GetOutputPort()); vtkActor sphereEnd = vtkActor.New(); sphereEnd.SetMapper(sphereEndMapper); sphereEnd.GetProperty().SetColor(1.0, .3, .3); vtkRenderWindow renderWindow = myRenderWindowControl.RenderWindow; vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); renderer.SetBackground(0.2, 0.3, 0.4); renderer.AddActor(actor); renderer.AddActor(sphereStart); renderer.AddActor(sphereEnd); renderer.ResetCamera(); }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVMatrixToTransform(String [] argv) { //Prefix Content is: "" // This example demonstrates how to use a matrix in place of a transfrom[] // via vtkMatrixToLinearTransform and vtkMatrixToHomogeneousTransform.[] // create a rendering window[] renWin = vtkRenderWindow.New(); renWin.SetSize((int)600, (int)300); // set up first set of polydata[] p1 = new vtkPlaneSource(); p1.SetOrigin((double)0.5, (double)0.508, (double)-0.5); p1.SetPoint1((double)-0.5, (double)0.508, (double)-0.5); p1.SetPoint2((double)0.5, (double)0.508, (double)0.5); p1.SetXResolution((int)5); p1.SetYResolution((int)5); p2 = new vtkPlaneSource(); p2.SetOrigin((double)-0.508, (double)0.5, (double)-0.5); p2.SetPoint1((double)-0.508, (double)-0.5, (double)-0.5); p2.SetPoint2((double)-0.508, (double)0.5, (double)0.5); p2.SetXResolution((int)5); p2.SetYResolution((int)5); p3 = new vtkPlaneSource(); p3.SetOrigin((double)-0.5, (double)-0.508, (double)-0.5); p3.SetPoint1((double)0.5, (double)-0.508, (double)-0.5); p3.SetPoint2((double)-0.5, (double)-0.508, (double)0.5); p3.SetXResolution((int)5); p3.SetYResolution((int)5); p4 = new vtkPlaneSource(); p4.SetOrigin((double)0.508, (double)-0.5, (double)-0.5); p4.SetPoint1((double)0.508, (double)0.5, (double)-0.5); p4.SetPoint2((double)0.508, (double)-0.5, (double)0.5); p4.SetXResolution((int)5); p4.SetYResolution((int)5); p5 = new vtkPlaneSource(); p5.SetOrigin((double)0.5, (double)0.5, (double)-0.508); p5.SetPoint1((double)0.5, (double)-0.5, (double)-0.508); p5.SetPoint2((double)-0.5, (double)0.5, (double)-0.508); p5.SetXResolution((int)5); p5.SetYResolution((int)5); p6 = new vtkPlaneSource(); p6.SetOrigin((double)0.5, (double)0.5, (double)0.508); p6.SetPoint1((double)-0.5, (double)0.5, (double)0.508); p6.SetPoint2((double)0.5, (double)-0.5, (double)0.508); p6.SetXResolution((int)5); p6.SetYResolution((int)5); // append together[] ap = new vtkAppendPolyData(); ap.AddInputConnection(p1.GetOutputPort()); ap.AddInputConnection(p2.GetOutputPort()); ap.AddInputConnection(p3.GetOutputPort()); ap.AddInputConnection(p4.GetOutputPort()); ap.AddInputConnection(p5.GetOutputPort()); ap.AddInputConnection(p6.GetOutputPort()); //--------------------------[] // linear transform matrix[] t1 = new vtkMatrixToLinearTransform(); m1 = new vtkMatrix4x4(); t1.SetInput((vtkMatrix4x4)m1); m1.SetElement((int)0, (int)0, (double)1.127631); m1.SetElement((int)0, (int)1, (double)0.205212); m1.SetElement((int)0, (int)2, (double)-0.355438); m1.SetElement((int)1, (int)0, (double)0.000000); m1.SetElement((int)1, (int)1, (double)0.692820); m1.SetElement((int)1, (int)2, (double)0.400000); m1.SetElement((int)2, (int)0, (double)0.200000); m1.SetElement((int)2, (int)1, (double)-0.469846); m1.SetElement((int)2, (int)2, (double)0.813798); f11 = new vtkTransformPolyDataFilter(); f11.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f11.SetTransform((vtkAbstractTransform)t1); m11 = new vtkDataSetMapper(); m11.SetInputConnection((vtkAlgorithmOutput)f11.GetOutputPort()); a11 = new vtkActor(); a11.SetMapper((vtkMapper)m11); a11.GetProperty().SetColor((double)1, (double)0, (double)0); a11.GetProperty().SetRepresentationToWireframe(); ren11 = vtkRenderer.New(); ren11.SetViewport((double)0.0, (double)0.5, (double)0.25, (double)1.0); ren11.ResetCamera((double)-0.5, (double)0.5, (double)-0.5, (double)0.5, (double)-1, (double)1); ren11.AddActor((vtkProp)a11); renWin.AddRenderer((vtkRenderer)ren11); // inverse identity transform[] f12 = new vtkTransformPolyDataFilter(); f12.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f12.SetTransform((vtkAbstractTransform)t1.GetInverse()); m12 = new vtkDataSetMapper(); m12.SetInputConnection((vtkAlgorithmOutput)f12.GetOutputPort()); a12 = new vtkActor(); a12.SetMapper((vtkMapper)m12); a12.GetProperty().SetColor((double)0.9, (double)0.9, (double)0); a12.GetProperty().SetRepresentationToWireframe(); ren12 = vtkRenderer.New(); ren12.SetViewport((double)0.0, (double)0.0, (double)0.25, (double)0.5); ren12.ResetCamera((double)-0.5, (double)0.5, (double)-0.5, (double)0.5, (double)-1, (double)1); ren12.AddActor((vtkProp)a12); renWin.AddRenderer((vtkRenderer)ren12); //--------------------------[] // perspective transform matrix[] m2 = new vtkMatrix4x4(); m2.SetElement((int)3, (int)0, (double)-0.11); m2.SetElement((int)3, (int)1, (double)0.3); m2.SetElement((int)3, (int)2, (double)0.2); t2 = new vtkMatrixToHomogeneousTransform(); t2.SetInput((vtkMatrix4x4)m2); f21 = new vtkTransformPolyDataFilter(); f21.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f21.SetTransform((vtkAbstractTransform)t2); m21 = new vtkDataSetMapper(); m21.SetInputConnection((vtkAlgorithmOutput)f21.GetOutputPort()); a21 = new vtkActor(); a21.SetMapper((vtkMapper)m21); a21.GetProperty().SetColor((double)1, (double)0, (double)0); a21.GetProperty().SetRepresentationToWireframe(); ren21 = vtkRenderer.New(); ren21.SetViewport((double)0.25, (double)0.5, (double)0.50, (double)1.0); ren21.ResetCamera((double)-0.5, (double)0.5, (double)-0.5, (double)0.5, (double)-1, (double)1); ren21.AddActor((vtkProp)a21); renWin.AddRenderer((vtkRenderer)ren21); // inverse linear transform[] f22 = new vtkTransformPolyDataFilter(); f22.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f22.SetTransform((vtkAbstractTransform)t2.GetInverse()); m22 = new vtkDataSetMapper(); m22.SetInputConnection((vtkAlgorithmOutput)f22.GetOutputPort()); a22 = new vtkActor(); a22.SetMapper((vtkMapper)m22); a22.GetProperty().SetColor((double)0.9, (double)0.9, (double)0); a22.GetProperty().SetRepresentationToWireframe(); ren22 = vtkRenderer.New(); ren22.SetViewport((double)0.25, (double)0.0, (double)0.50, (double)0.5); ren22.ResetCamera((double)-0.5, (double)0.5, (double)-0.5, (double)0.5, (double)-1, (double)1); ren22.AddActor((vtkProp)a22); renWin.AddRenderer((vtkRenderer)ren22); //--------------------------[] // linear concatenation - should end up with identity here[] t3 = new vtkTransform(); t3.Concatenate((vtkLinearTransform)t1); t3.Concatenate((vtkLinearTransform)t1.GetInverse()); f31 = new vtkTransformPolyDataFilter(); f31.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f31.SetTransform((vtkAbstractTransform)t3); m31 = new vtkDataSetMapper(); m31.SetInputConnection((vtkAlgorithmOutput)f31.GetOutputPort()); a31 = new vtkActor(); a31.SetMapper((vtkMapper)m31); a31.GetProperty().SetColor((double)1, (double)0, (double)0); a31.GetProperty().SetRepresentationToWireframe(); ren31 = vtkRenderer.New(); ren31.SetViewport((double)0.50, (double)0.5, (double)0.75, (double)1.0); ren31.ResetCamera((double)-0.5, (double)0.5, (double)-0.5, (double)0.5, (double)-1, (double)1); ren31.AddActor((vtkProp)a31); renWin.AddRenderer((vtkRenderer)ren31); // inverse linear transform[] f32 = new vtkTransformPolyDataFilter(); f32.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f32.SetTransform((vtkAbstractTransform)t3.GetInverse()); m32 = new vtkDataSetMapper(); m32.SetInputConnection((vtkAlgorithmOutput)f32.GetOutputPort()); a32 = new vtkActor(); a32.SetMapper((vtkMapper)m32); a32.GetProperty().SetColor((double)0.9, (double)0.9, (double)0); a32.GetProperty().SetRepresentationToWireframe(); ren32 = vtkRenderer.New(); ren32.SetViewport((double)0.5, (double)0.0, (double)0.75, (double)0.5); ren32.ResetCamera((double)-0.5, (double)0.5, (double)-0.5, (double)0.5, (double)-1, (double)1); ren32.AddActor((vtkProp)a32); renWin.AddRenderer((vtkRenderer)ren32); //--------------------------[] // perspective transform concatenation[] t4 = new vtkPerspectiveTransform(); t4.Concatenate((vtkHomogeneousTransform)t1); t4.Concatenate((vtkHomogeneousTransform)t2); t4.Concatenate((vtkHomogeneousTransform)t3); f41 = new vtkTransformPolyDataFilter(); f41.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f41.SetTransform((vtkAbstractTransform)t4); m41 = new vtkDataSetMapper(); m41.SetInputConnection((vtkAlgorithmOutput)f41.GetOutputPort()); a41 = new vtkActor(); a41.SetMapper((vtkMapper)m41); a41.GetProperty().SetColor((double)1, (double)0, (double)0); a41.GetProperty().SetRepresentationToWireframe(); ren41 = vtkRenderer.New(); ren41.SetViewport((double)0.75, (double)0.5, (double)1.0, (double)1.0); ren41.ResetCamera((double)-0.5, (double)0.5, (double)-0.5, (double)0.5, (double)-1, (double)1); ren41.AddActor((vtkProp)a41); renWin.AddRenderer((vtkRenderer)ren41); // inverse of transform concatenation[] f42 = new vtkTransformPolyDataFilter(); f42.SetInputConnection((vtkAlgorithmOutput)ap.GetOutputPort()); f42.SetTransform((vtkAbstractTransform)t4.GetInverse()); m42 = new vtkDataSetMapper(); m42.SetInputConnection((vtkAlgorithmOutput)f42.GetOutputPort()); a42 = new vtkActor(); a42.SetMapper((vtkMapper)m42); a42.GetProperty().SetColor((double)0.9, (double)0.9, (double)0); a42.GetProperty().SetRepresentationToWireframe(); ren42 = vtkRenderer.New(); ren42.SetViewport((double)0.75, (double)0.0, (double)1.0, (double)0.5); ren42.ResetCamera((double)-0.5, (double)0.5, (double)-0.5, (double)0.5, (double)-1, (double)1); ren42.AddActor((vtkProp)a42); renWin.AddRenderer((vtkRenderer)ren42); renWin.Render(); //deleteAllVTKObjects(); }