private void vtkPolyDataConnectivityFilter_LargestRegion()
        {
            // Small sphere
            vtkSphereSource sphereSource1 = vtkSphereSource.New();

            sphereSource1.Update();

            // Large sphere
            vtkSphereSource sphereSource2 = vtkSphereSource.New();

            sphereSource2.SetRadius(10);
            sphereSource2.SetCenter(25, 0, 0);
            sphereSource2.SetThetaResolution(10);
            sphereSource2.SetPhiResolution(10);
            sphereSource2.Update();

            vtkAppendPolyData appendFilter = vtkAppendPolyData.New();

            appendFilter.AddInputConnection(sphereSource1.GetOutputPort());
            appendFilter.AddInputConnection(sphereSource2.GetOutputPort());
            appendFilter.Update();

            vtkPolyDataConnectivityFilter connectivityFilter = vtkPolyDataConnectivityFilter.New();

            connectivityFilter.SetInputConnection(appendFilter.GetOutputPort());
            connectivityFilter.SetExtractionModeToLargestRegion();
            connectivityFilter.Update();

            // Create a mapper and actor for original data
            vtkPolyDataMapper originalMapper = vtkPolyDataMapper.New();

            originalMapper.SetInputConnection(appendFilter.GetOutputPort());
            originalMapper.Update();

            vtkActor originalActor = vtkActor.New();

            originalActor.SetMapper(originalMapper);

            // Create a mapper and actor for extracted data
            vtkPolyDataMapper extractedMapper = vtkPolyDataMapper.New();

            extractedMapper.SetInputConnection(connectivityFilter.GetOutputPort());
            extractedMapper.Update();

            vtkActor extractedActor = vtkActor.New();

            extractedActor.GetProperty().SetColor(1, 0, 0);
            extractedActor.SetMapper(extractedMapper);
            // 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(originalActor);
            renderer.AddActor(extractedActor);
        }
        private void ExtractLargestIsoSurface(string filePath, double threshold, bool extractLargest)
        {
            // Load data
            vtkStructuredPointsReader reader = vtkStructuredPointsReader.New();

            reader.SetFileName(filePath);

            // Create a 3D model using marching cubes
            vtkMarchingCubes mc = vtkMarchingCubes.New();

            mc.SetInputConnection(reader.GetOutputPort());
            mc.ComputeNormalsOn();
            mc.ComputeGradientsOn();
            mc.SetValue(0, threshold); // second value acts as threshold

            // To remain largest region
            vtkPolyDataConnectivityFilter confilter = vtkPolyDataConnectivityFilter.New();

            confilter.SetInputConnection(mc.GetOutputPort());
            confilter.SetExtractionModeToLargestRegion();

            // Create a mapper
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();

            if (extractLargest)
            {
                mapper.SetInputConnection(confilter.GetOutputPort());
            }
            else
            {
                mapper.SetInputConnection(mc.GetOutputPort());
            }

            mapper.ScalarVisibilityOff(); // utilize actor's property I set

            // Visualize
            vtkActor actor = vtkActor.New();

            actor.GetProperty().SetColor(1, 1, 1);
            actor.SetMapper(mapper);

            // get a reference to the renderwindow of our renderWindowControl1
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            // renderer
            vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();

            // add our actor to the renderer
            renderer.AddActor(actor);
        }