[vtkusers] How to do Curved MPR Using VTKProbeFilter

jruiz jonathan.ruiz3 at gmail.com
Tue Apr 17 19:38:42 EDT 2012


Thanks a lot for this post it was very helpfull for me as I'm trying to build
and MPR viwer based on a curved surface instead of ortogonal planes, the
final objective is to get a panoramic dental view from a dicom image. 

I'm using java to do it and so far I was able to get your example and
traslate it to JAVA, I'm going to paste the whole code for the record at the
end of this message,  maybe some one find it usefull in some way.

Well, now I got this curved surface "textured" with the desired image but
I'd need it in a viewport just like the MPR viewver example do with coronal
and sagital views, any advice to reach it will be welcome.

Thanks! 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.File;
import vtk.*;
import gdcm.*;



/**
 *
 * @author jruiz
 */
public class JavaApplication9 {
     static {
        // VTK
        System.loadLibrary("vtkCommonJava");
        System.loadLibrary("vtkFilteringJava");
        System.loadLibrary("vtkIOJava");
        System.loadLibrary("vtkImagingJava");
        System.loadLibrary("vtkGraphicsJava");
        System.loadLibrary("vtkRenderingJava");
        System.loadLibrary("vtkVolumeRenderingJava"); //
vtkSmartVolumeMapper
        System.loadLibrary("vtkWidgetsJava"); // vtkBoxWidget
        // VTK-GDCM
        System.loadLibrary("vtkgdcmJava");
        System.loadLibrary("gdcmjni");
    }
    static FilenamesType fns = new FilenamesType();
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{
    
        // Read the volume data
        /*
        vtkSmartPointer< vtkreader2Factory > imageFactory = 
        vtkSmartPointer< vtkreader2Factory >::New();
        vtkreader2 *reader = 
        imageFactory->Createreader2( argv[1] );  
        reader->SetFileName( argv[1] );
        reader->Update();
        * 
        */
        String dirname = "/home/jruiz/Proyectos/dicom_example_1";
        if( !PosixEmulation.FileIsDirectory( dirname ) )
        {
        return;
        }

        File dir = new File(dirname);
        visitAllFiles(dir);

        IPPSorter ipp = new IPPSorter();
        ipp.SetComputeZSpacing( true );
        ipp.SetZSpacingTolerance( 1e-3 );
        boolean b = ipp.Sort( fns );
        if(!b)
        {
            throw new Exception("Could not scan");
        }
        double ippzspacing = ipp.GetZSpacing();

        FilenamesType sorted = ipp.GetFilenames();
        vtkStringArray files = new vtkStringArray();
        long nfiles = sorted.size();
        
        for (int i = 0; i < nfiles; i++) {
        String f = sorted.get(i);
        files.InsertNextValue( f );
        }
        vtkGDCMImageReader reader = new vtkGDCMImageReader();
        reader.SetFileNames( files );
        reader.Update(); // get spacing value
        double[] spacing = reader.GetOutput().GetSpacing();

        vtkImageChangeInformation change = new vtkImageChangeInformation();
        change.SetInputConnection( reader.GetOutputPort() );
        change.SetOutputSpacing( spacing[0], spacing[1], ippzspacing );
        
        // Read the Polyline
        vtkPolyDataReader polyLineReader = new  vtkPolyDataReader();        
        polyLineReader.SetFileName("/tmp/polyline.vtk");
        polyLineReader.Update();

        vtkSplineFilter spline =  new vtkSplineFilter();
        spline.SetInputConnection(polyLineReader.GetOutputPort());
        spline.SetNumberOfSubdivisions(10);
        spline.SetSubdivideToSpecified();
        
        
         // Sweep the line to form a surface
        double [] direction = new double [3];
        direction[0] = 0.0;
        direction[1] = 0.0;
        direction[2] = 1.0;
        double distance = 164;
        spline.Update();
        vtkPolyData surface = SweepLine(spline.GetOutput(), direction,
distance, 10);
        
        // Probe the volume with the extruded surface
        vtkProbeFilter sampleVolume = new vtkProbeFilter();
        sampleVolume.SetInputConnection(1, reader.GetOutputPort());
        sampleVolume.SetInput(0, surface);

        // Compute a simple window/level based on scalar range
        vtkWindowLevelLookupTable wlLut = new vtkWindowLevelLookupTable();
        double range = reader.GetOutput().GetScalarRange()[1] -
reader.GetOutput().GetScalarRange()[0];
        double level = (reader.GetOutput().GetScalarRange()[1] +
reader.GetOutput().GetScalarRange()[0]) / 2.0;
        wlLut.SetWindow(range);
        wlLut.SetLevel(level);

        // Create a mapper and actor.
        vtkDataSetMapper mapper = new vtkDataSetMapper();
        mapper.SetInputConnection(sampleVolume.GetOutputPort());
        mapper.SetLookupTable(wlLut);
        mapper.SetScalarRange(0, 255);

        vtkActor actor =new vtkActor();
        actor.SetMapper(mapper);

        // Create a renderer, render window, and interactor
        vtkRenderer renderer = new vtkRenderer();
        vtkRenderWindow renderWindow = new vtkRenderWindow();
        renderWindow.AddRenderer(renderer);
        vtkRenderWindowInteractor renderWindowInteractor = new
vtkRenderWindowInteractor();
        renderWindowInteractor.SetRenderWindow(renderWindow);

        // Add the actors to the scene
        renderer.AddActor(actor);
        renderer.SetBackground(.2, .3, .4);

        // Set the camera for viewing medical images
        renderer.GetActiveCamera().SetViewUp(0,0,1);
        renderer.GetActiveCamera().SetPosition(0,0,0);
        renderer.GetActiveCamera().SetFocalPoint(0,1,0);
        renderer.ResetCamera();

        // Render and interact
        renderWindow.Render();
        renderWindowInteractor.Start();

        reader.Delete();
    }
    
    public static vtkPolyData SweepLine(vtkPolyData line, double []
direction, double distance, int cols) 
    {
        int rows = line.GetNumberOfPoints();
        double spacing = distance / cols;
        vtkPolyData surface = new vtkPolyData();

        // Generate the points
        cols++;
        int numberOfPoints = rows * cols;
        int numberOfPolys = (rows - 1) * (cols - 1);
        vtkPoints points =new vtkPoints();
        points.Allocate(numberOfPoints, 1000);
        vtkCellArray polys = new vtkCellArray();
        polys.Allocate(numberOfPolys * 4, 1000);        
        double [] x = new double[3];
        int cnt = 0;
        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < cols; col++)
            {
                double [] p = new double [3];
                line.GetPoint(row, p);
                x[0] = p[0] + direction[0] * col * spacing;
                x[1] = p[1] + direction[1] * col * spacing;
                x[2] = p[2] + direction[2] * col * spacing;
                points.InsertPoint(cnt++, x);
            }
        }
        
        // Generate the quads
       
        
        for (int row = 0; row < rows - 1; row++)
        {
            for (int col = 0; col < cols - 1; col++)
            {
                vtkIdList pts = new vtkIdList() ;
                pts.InsertNextId( col + row * (cols));
                pts.InsertNextId( pts.GetId(0) + 1);
                pts.InsertNextId( pts.GetId(0) + cols + 1);
                pts.InsertNextId( pts.GetId(0) + cols);
                polys.InsertNextCell(pts);
            }
        }
        surface.SetPoints(points);
        surface.SetPolys(polys);

        return surface;  
    }
    // Process only files under dir
    public static void visitAllFiles(File dir)
        {
        if (dir.isDirectory())
        {
        String[] children = dir.list();
        for (int i=0; i<children.length; i++)
            {
            visitAllFiles(new File(dir, children[i]));
            }
        }
        else
        {
            process(dir.getPath());
        }
    }
    public static void process(String path)
    {
        fns.add( path );
    }
}


see http://vtk.org/Wiki/VTK/Examples/Cxx/Visualization/CurvedReformation for
detailed information

--
View this message in context: http://vtk.1045678.n5.nabble.com/How-to-do-Curved-MPR-Using-VTKProbeFilter-tp5482396p5647874.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list