예제 #1
0
        private void LoadImageToRenderer(vtk.vtkRenderer renderer)
        {
            try
            {
                string backgroundImageFileName = ApplicationOptions.Instance().ViewOptions.BackgroundFileName;
                if (!string.IsNullOrEmpty(backgroundImageFileName))
                {
                    string backgroundImagePath = System.IO.Path.Combine(CFolderUtility.DataFolder(), ApplicationOptions.Instance().ViewOptions.BackgroundFileName);
                    //string backgroundImagePath = @"C:\sky4.jpg";
                    vtk.vtkJPEGReader jpegReader = new vtk.vtkJPEGReader();
                    if (jpegReader.CanReadFile(backgroundImagePath) != 0)
                    {
                        jpegReader.SetFileName(backgroundImagePath);
                        jpegReader.Update();

                        vtk.vtkImageData imageData = jpegReader.GetOutput();

                        vtk.vtkImageActor actor = new vtk.vtkImageActor();
                        actor.SetInput(imageData);

                        renderer.AddActor(actor);

                        double[] origin = imageData.GetOrigin();
                        double[] spacing = imageData.GetSpacing();
                        int[] extent = imageData.GetExtent();

                        vtk.vtkCamera camera = renderer.GetActiveCamera();
                        camera.ParallelProjectionOn();
                        double xc = origin[0] + 0.5 * (extent[0] + extent[1]) * spacing[0];
                        double yc = origin[1] + 0.5 * (extent[2] + extent[3]) * spacing[1];
                        //double xd = (extent[1] - extent[0] + 1)*spacing[0];
                        double yd = (extent[3] - extent[2] + 1) * spacing[1];
                        double d = camera.GetDistance();
                        camera.SetParallelScale(0.5 * yd);
                        camera.SetFocalPoint(xc, yc, 0.0);
                        camera.SetPosition(xc, yc, d);
                    }
                    jpegReader.Dispose();
                }
            }
            catch (Exception ex)
            {
                string errMsg = ex.Message + "\n" + ex.StackTrace;
                vtk.vtkOutputWindow.GetInstance().DisplayErrorText(errMsg);
            }
        }
예제 #2
0
        private void UpdateRendererOutline(ref vtk.vtkActor2D outlineRendererActor, ref vtk.vtkPolyData polyData, vtk.vtkRenderer renderer)
        {
            try
            {

                if (outlineRendererActor == null)
                {
                    outlineRendererActor = new vtk.vtkActor2D();

                    polyData = new vtk.vtkPolyData();
                    polyData.Allocate(5, 0);

                    vtk.vtkIdList idList = new vtk.vtkIdList();
                    idList.SetNumberOfIds(5);

                    vtk.vtkPoints points = new vtk.vtkPoints();
                    idList.InsertId(0, points.InsertNextPoint(1, 1, 0));
                    idList.InsertId(1, points.InsertNextPoint(2, 1, 0));
                    idList.InsertId(2, points.InsertNextPoint(2, 2, 0));
                    idList.InsertId(3, points.InsertNextPoint(1, 2, 0));
                    idList.InsertId(4, idList.GetId(0));

                    polyData.SetPoints(points);
                    polyData.InsertNextCell(4, idList);

                    vtk.vtkCoordinate coordinate = new vtk.vtkCoordinate();
                    coordinate.SetCoordinateSystemToDisplay();
                    vtk.vtkPolyDataMapper2D mapper = new vtk.vtkPolyDataMapper2D();
                    mapper.SetInput(polyData);
                    mapper.SetTransformCoordinate(coordinate);

                    outlineRendererActor.SetMapper(mapper);
                    outlineRendererActor.SetPosition(0, 0);
                    outlineRendererActor.SetPosition2(1, 1);

                    renderer.AddActor(outlineRendererActor);
                }

                double[] vp = renderer.GetViewport();
                renderer.NormalizedDisplayToDisplay(ref vp[0], ref vp[1]);
                renderer.NormalizedDisplayToDisplay(ref vp[2], ref vp[3]);

                polyData.GetPoints().SetPoint(0, vp[0], vp[1], 0);
                polyData.GetPoints().SetPoint(1, vp[2], vp[1], 0);
                polyData.GetPoints().SetPoint(2, vp[2], vp[3], 0);
                polyData.GetPoints().SetPoint(3, vp[0], vp[3], 0);

                // Change Outline color and width
                double[] normalOutlineColor = ApplicationOptions.Instance().RendererLayoutOptions.NormalOutlineColor;
                float normalOutlineWidth = ApplicationOptions.Instance().RendererLayoutOptions.NormalOutlineLineWidth;

                if (renderer == IApp.theApp.RendererManager.ActiveRenderer)
                {
                    normalOutlineColor = ApplicationOptions.Instance().RendererLayoutOptions.ActiveOutlineColor;
                    normalOutlineWidth = ApplicationOptions.Instance().RendererLayoutOptions.ActiveOutlineLineWidth;
                }

                outlineRendererActor.GetProperty().SetColor(normalOutlineColor);
                outlineRendererActor.GetProperty().SetLineWidth(normalOutlineWidth);
            }
            catch (Exception ex)
            {
                string errMsg = ex.Message + "\n" + ex.StackTrace;
                vtk.vtkOutputWindow.GetInstance().DisplayErrorText(errMsg);
            }
        }