[vtkusers] Getting Original 3d Point co-ordinate on mouse hover

mahendra mahendra123awale at gmail.com
Thu Apr 4 04:42:23 EDT 2013


Thank you very much Alex for guiding me on the right track. Finally I could
able to get the required data. I have attached the Java code here. I hope it
will help others user as well..

//===============================================================
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingConstants;
import javax.swing.ToolTipManager;
import javax.swing.border.BevelBorder;
import vtk.vtkActor;
import vtk.vtkCellArray;
import vtk.vtkInteractorStyleTrackballCamera;
import vtk.vtkPointPicker;
import vtk.vtkPoints;
import vtk.vtkPolyData;
import vtk.vtkPolyDataMapper;
import vtk.vtkRenderWindowPanel;
import vtk.vtkUnsignedCharArray;


/**
 *
 * @author mahendra
 */
public class pointPicked extends JFrame {

    final vtkPointPicker pointPicker = new vtkPointPicker();

        static {
        System.loadLibrary("vtkCommonJava");
        System.loadLibrary("vtkFilteringJava");
        System.loadLibrary("vtkIOJava");
        System.loadLibrary("vtkImagingJava");
        System.loadLibrary("vtkGraphicsJava");
        System.loadLibrary("vtkRenderingJava");
    }
    
    pointPicked() throws FileNotFoundException, IOException {

        //READ xyz File
        BufferedReader br = new BufferedReader(new
FileReader("/pathToFile//test.txt"));
        vtkPoints points = new vtkPoints();
        vtkCellArray vtkCellArray = new vtkCellArray();
        vtkUnsignedCharArray Colors = new vtkUnsignedCharArray();
        Colors.SetNumberOfComponents(3);
        Colors.SetName("Colors");

        String str;
        while ((str = br.readLine()) != null) {
            String s[] = str.split(" ");
            int id = points.InsertNextPoint(Float.valueOf(s[0]),
Float.valueOf(s[1]), Float.valueOf(s[2]));
            vtkCellArray.InsertNextCell(1);
            vtkCellArray.InsertCellPoint(id);

            int r = Integer.valueOf(s[3]);
            int g = Integer.valueOf(s[4]);
            int b = Integer.valueOf(s[5]);
            Colors.InsertNextTuple3(r, g, b);
        }

        br.close();
        final vtkPolyData polydata = new vtkPolyData();
        polydata.SetPoints(points);
        polydata.GetPointData().SetScalars(Colors);
        polydata.SetVerts(vtkCellArray);
        points.Delete();
        vtkCellArray.Delete();

        vtkPolyDataMapper mapper = new vtkPolyDataMapper();
        mapper.SetInput(polydata);

        // 
        // Create an actor to represent the cone. The actor orchestrates
rendering of
        // the mapper's graphics primitives. An actor also refers to
properties via a
        // vtkProperty instance, and includes an internal transformation
matrix. We
        // set this actor's mapper to be coneMapper which we created above.
        //
        vtkActor myActor = new vtkActor();
        myActor.SetMapper(mapper);
        myActor.GetProperty().SetPointSize(8);

        // Create a render window panel to display the sphere
        final vtkRenderWindowPanel renderWindowPanel = new
vtkRenderWindowPanel();
        renderWindowPanel.setPreferredSize(new Dimension(600, 600));
        renderWindowPanel.setInteractorStyle(new
vtkInteractorStyleTrackballCamera());

        add(renderWindowPanel, BorderLayout.CENTER);
        renderWindowPanel.GetRenderer().AddActor(myActor);

        //Create a status bar
        JPanel statusBar = new JPanel(new BorderLayout());
        final JLabel label = new JLabel(" ", SwingConstants.RIGHT);
        statusBar.add(label, BorderLayout.EAST);
        statusBar.setBorder(new BevelBorder(BevelBorder.LOWERED));

        add(statusBar, BorderLayout.SOUTH);
        
       
//======================================================================
        // Show the point on the sphere the mouse is hovering over in the
status bar
        renderWindowPanel.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                // The call to Pick needs to be surrounded by lock and
unlock to prevent crashes.
                
                renderWindowPanel.lock();                
                int pickSucceeded = pointPicker.Pick(e.getX(),
renderWindowPanel.getHeight() - e.getY() - 1,
                        0.0, renderWindowPanel.GetRenderer());
                renderWindowPanel.unlock();
                
                
                if (pickSucceeded == 1) {
                    double[] p = pointPicker.GetPickPosition();
                    label.setText(pointPicker.GetPointId()+ " Position: " +
p[0] + ", " + p[1] + ", " + p[2]);
                } else {
                    label.setText(" ");
                }
            }
        });
       
//======================================================================
        
        
    }
    
     public static void main(String[] args) {
        try {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
                   
ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);

                    JFrame frame=null;
                    try {
                        frame = new pointPicked();
                    } catch (FileNotFoundException ex) {
                       
Logger.getLogger(pointPicked.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                       
Logger.getLogger(pointPicked.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        } catch (Exception e) {
        }
    }
}

//============================================================
test.txt contains

572 576 581 166 255 0
572 576 582 166 255 0
569 636 579 140 255 0
512 530 641 64 255 0
412 486 517 0 255 38
710 488 403 255 51 0
791 673 581 255 0 0
503 530 554 64 255 0
832 565 546 255 0 102
464 450 527 38 255 0

//===========================================================

Thanks



--
View this message in context: http://vtk.1045678.n5.nabble.com/Getting-Original-3d-Point-co-ordinate-on-mouse-hover-tp5719771p5719825.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list