/// <summary> /// Rotates volume along given axis. /// </summary> /// <param name="volume"></param> /// <param name="angles"></param> public static void RotateData(ref vtkImageData volume, double[] angles) { // Rotation along image center double[] center = volume.GetExtent().Divide(2); // Dimensions of rotated image int[] outExtent = volume.GetExtent().Multiply(1.1).Round().ToInt32(); // Rotation parameters var rotate = new vtkTransform(); rotate.Translate(center[1], center[3], center[5]); rotate.RotateX(angles[0]); rotate.RotateY(angles[1]); rotate.RotateZ(angles[2]); // z angle should be 0 rotate.Translate(-center[1], -center[3], -center[5]); // Perform rotation var slice = new vtkImageReslice(); slice.SetInput(volume); slice.SetResliceTransform(rotate); slice.SetInterpolationModeToCubic(); slice.SetOutputSpacing(volume.GetSpacing()[0], volume.GetSpacing()[1], volume.GetSpacing()[2]); slice.SetOutputOrigin(volume.GetOrigin()[0], volume.GetOrigin()[1], volume.GetOrigin()[2]); slice.SetOutputExtent(outExtent[0], outExtent[1], outExtent[2], outExtent[3], outExtent[4], outExtent[5]); }
/// <summary> /// 进行2D转换 /// </summary> /// <param name="polyData"></param> /// <param name="info"></param> /// <returns></returns> private static vtkPolyData Extrude2DSection(vtkPolyData polyData, ModelingBaseInfo info) { vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); vtkTransformFilter transFilter = vtkTransformFilter.New(); vtkTransform transform = vtkTransform.New(); transform.RotateX(-90); // 旋转到xoy面上 transform.RotateY(info.Angle); //顺时针为正逆时针为负 // transform.Translate(info.BaseCoordinate.X, info.BaseCoordinate.Y, info.BaseCoordinate.Z);//先旋转后平移 transform.Translate(info.BaseCoordinate.X, info.BaseCoordinate.Z, info.BaseCoordinate.Y); //先旋转后平移 transFilter.SetTransform(transform); transFilter.SetInput(polyData); return(transFilter.GetPolyDataOutput()); }
/// <summary> /// 进行二维结构的转换 /// </summary> /// <param name="polyData"></param> /// <param name="info"></param> /// <returns></returns> private static vtkActor FormActor2D(vtkPolyData polyData, ModelingBaseInfo info) {// vtkActor actor = vtkActor.New(); vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); vtkTransformFilter transFilter = vtkTransformFilter.New(); vtkTransform transform = vtkTransform.New(); transform.RotateX(-90); // 旋转到xoy面上 transform.RotateY(info.Angle); //顺时针为正逆时针为负 transform.Translate(info.BaseCoordinate.X, info.BaseCoordinate.Y, info.BaseCoordinate.Z); //先旋转后平移 // transform.Translate(info.BaseCoordinate.X, info.BaseCoordinate.Z,info.BaseCoordinate.Y );//先旋转后平移 transFilter.SetTransform(transform); transFilter.SetInput(polyData); mapper.SetInputConnection(transFilter.GetOutputPort()); actor.SetMapper(mapper); return(actor); }
public void ReadImageIntoObject(RenderWindowControl renderWindowControl, nvmImageModel camera) { vtkRenderWindow renderWindow = renderWindowControl.RenderWindow; vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); string filePath = Path.Combine(tempDirectory, $"{camera.fileName}"); vtkJPEGReader reader = vtkJPEGReader.New(); reader.SetFileName(filePath); reader.Update(); // Treba poriesit ako nasmerovat obrazky bez pokazenia textury var vectoris = Vector3.Transform(new Vector3(0, 0, 1), camera.quaternion); vtkPlaneSource planeSource = vtkPlaneSource.New(); vtkTexture texture = new vtkTexture(); texture.SetInputConnection(reader.GetOutputPort()); vtkTransform transform = new vtkTransform(); transform.RotateX(180); texture.SetTransform(transform); vtkTextureMapToPlane plane = new vtkTextureMapToPlane(); plane.SetInputConnection(planeSource.GetOutputPort()); planeSource.SetCenter(camera.cameraCenter.X, camera.cameraCenter.Y, camera.cameraCenter.Z); vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection(plane.GetOutputPort()); vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); actor.SetTexture(texture); renderer.SetBackground(0.2, 0.3, 0.4); renderer.AddActor(actor); }
/// <summary> /// 进行三维结构的转换 /// </summary> /// <param name="degree"></param> /// <param name="polyData"></param> /// <param name="info"></param> /// <returns></returns> private static vtkPolyData Extrude3DSection(float degree, vtkPolyData polyData, ModelingBaseInfo info) { //进行拉伸以构造的三维实体 vtkRotationalExtrusionFilter extrude = vtkRotationalExtrusionFilter.New(); extrude.SetInput(polyData); extrude.SetResolution(80); extrude.SetAngle(degree);//旋转 extrude.CappingOn(); vtkPolyDataNormals normals = vtkPolyDataNormals.New(); normals.SetInput(extrude.GetOutput()); normals.SetFeatureAngle(100); //进行转换 vtkTransform transform = vtkTransform.New(); transform.RotateX(info.Angle); //逆时针为正,顺时针为负 transform.Translate(info.BaseCoordinate.X, info.BaseCoordinate.Y, info.BaseCoordinate.Z); //先旋转后平移 vtkTransformFilter transFilter = vtkTransformFilter.New(); transFilter.SetTransform(transform); transFilter.SetInput(normals.GetOutput()); //vtkTriangleFilter filter = vtkTriangleFilter.New(); //filter.SetInput(transFilter.GetOutput()); //return filter.GetPolyDataInput(0); vtkCleanPolyData clearPolydata = vtkCleanPolyData.New();//清除重合的点和片 clearPolydata.SetInput(transFilter.GetOutput()); return(clearPolydata.GetOutput()); //return transFilter.GetPolyDataOutput(); }
public void SetRotation() { vtkTransform.RotateX(rotateX); vtkTransform.RotateY(rotateY); vtkTransform.RotateZ(rotateZ); }
private void WeightedTransformFilter() { // Use a sphere as a basis of the shape vtkSphereSource sphere = vtkSphereSource.New(); sphere.SetPhiResolution(40); sphere.SetThetaResolution(40); sphere.Update(); vtkPolyData sphereData = sphere.GetOutput(); // Create a data array to hold the weighting coefficients vtkFloatArray tfarray = vtkFloatArray.New(); int npoints = sphereData.GetNumberOfPoints(); tfarray.SetNumberOfComponents(2); tfarray.SetNumberOfTuples(npoints); // Parameterize the sphere along the z axis, and fill the weights // with (1.0-a, a) to linearly interpolate across the shape IntPtr pPoint = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * 3); double[] point = new double[3]; for (int i = 0; i < npoints; i++) { sphereData.GetPoint(i, pPoint); Marshal.Copy(pPoint, point, 0, 3); double x = point[0]; double y = point[1]; double z = point[2]; double zn = z + 0.5; double zn1 = 1.0 - zn; if (zn > 1.0) { zn = 1.0; } if (zn1 < 0.0) { zn1 = 0.0; } tfarray.SetComponent(i, 0, zn1); tfarray.SetComponent(i, 1, zn); } Marshal.FreeHGlobal(pPoint); // Create field data to hold the array, and bind it to the sphere vtkFieldData fd = vtkFieldData.New(); tfarray.SetName("weights"); sphereData.GetPointData().AddArray(tfarray); // Use an ordinary transform to stretch the shape vtkTransform stretch = vtkTransform.New(); stretch.Scale(1, 1, 3.2); vtkTransformFilter stretchFilter = vtkTransformFilter.New(); stretchFilter.SetInputConnection(sphereData.GetProducerPort()); stretchFilter.SetTransform(stretch); // Now, for the weighted transform stuff vtkWeightedTransformFilter weightedTrans = vtkWeightedTransformFilter.New(); // Create two transforms to interpolate between vtkTransform identity = vtkTransform.New(); identity.Identity(); vtkTransform rotated = vtkTransform.New(); double rotatedAngle = 45; rotated.RotateX(rotatedAngle); weightedTrans.SetNumberOfTransforms(2); weightedTrans.SetTransform(identity, 0); weightedTrans.SetTransform(rotated, 1); // which data array should the filter use ? weightedTrans.SetWeightArray("weights"); weightedTrans.SetInputConnection(stretchFilter.GetOutputPort()); vtkPolyDataMapper weightedTransMapper = vtkPolyDataMapper.New(); weightedTransMapper.SetInputConnection(weightedTrans.GetOutputPort()); vtkActor weightedTransActor = vtkActor.New(); weightedTransActor.SetMapper(weightedTransMapper); weightedTransActor.GetProperty().SetDiffuseColor(0.8, 0.8, 0.1); weightedTransActor.GetProperty().SetRepresentationToSurface(); // get a reference to the renderwindow of our renderWindowControl1 vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow; // renderer vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); // set background color renderer.SetBackground(0.2, 0.3, 0.4); // add our actor to the renderer renderer.AddActor(weightedTransActor); renderer.ResetCamera(); renderer.GetActiveCamera().Azimuth(90); renderer.GetActiveCamera().Dolly(1); }