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 static double[] Mutiple(vtkTransform transform, double[] value) { vtkMatrix4x4 matrix = transform.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); return(result); }
// For storing the VTK transform generated by a box widget in a C# matrix public void TransferVTKBoxWidgetTransformToMatrix(BoxSpecification box_spec) { vtkMatrix4x4 vtk_matrix = vtkMatrix4x4.New(); vtkTransform vtk_transform = vtkTransform.New(); boxRep.GetTransform(vtk_transform); vtk_transform.GetMatrix(vtk_matrix); for (int ii = 0; ii < 4; ++ii) { for (int jj = 0; jj < 4; ++jj) { // Using elements Set method for property change notification box_spec.SetMatrixElement(ii, jj, vtk_matrix.GetElement(ii, jj)); } } }
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(); }