1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.vtk.awt;
14 import java.awt.event.InputEvent;
15 import java.awt.event.MouseEvent;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
20 import javax.vecmath.Point2d;
21 import javax.vecmath.Point2i;
22 import javax.vecmath.Point3d;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.ISelectionChangedListener;
26 import org.eclipse.jface.viewers.ISelectionProvider;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.swt.widgets.Display;
30 import org.simantics.g3d.tools.AdaptationUtils;
34 import vtk.vtkRenderWindow;
35 import vtk.vtkRenderer;
37 public class vtkCameraAndSelectorAction extends vtkAwtAction implements ISelectionProvider {
39 protected vtkRenderer ren;
42 protected vtkRenderWindow rw;
43 protected vtkCamera cam;
44 protected int InteractionMode = 1;
46 protected double activeRate = 5.0;
47 protected double passiveRate = 0.01;
48 protected boolean doNotRotate = true;
50 public vtkCameraAndSelectorAction(InteractiveVtkPanel panel) {
52 this.ren = panel.GetRenderer();
53 this.rw = panel.GetRenderWindow();
54 this.cam = ren.GetActiveCamera();
61 public void UnLock() {
65 public void InteractionModeRotate()
67 this.InteractionMode = 1;
70 public void InteractionModeTranslate()
72 this.InteractionMode = 2;
75 public void InteractionModeZoom()
77 this.InteractionMode = 3;
80 public void resetCameraClippingRange() {
82 ren.ResetCameraClippingRange();
86 public void resetCamera() {
92 public void mousePressed(MouseEvent e)
95 if (ren.VisibleActorCount() == 0) return;
96 rw.SetDesiredUpdateRate(activeRate);
99 if ((e.getModifiers()==InputEvent.BUTTON2_MASK) ||
100 (e.getModifiers()==(InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK)))
102 InteractionModeTranslate();
104 else if (e.getModifiers()==InputEvent.BUTTON3_MASK)
106 InteractionModeZoom();
110 InteractionModeRotate();
114 public void mouseReleased(MouseEvent e)
116 rw.SetDesiredUpdateRate(passiveRate);
121 public void mouseDragged(MouseEvent e)
123 if (ren.VisibleActorCount() == 0) return;
127 if (this.InteractionMode == 1)
129 cam.Azimuth(lastX - x);
130 cam.Elevation(y - lastY);
133 cam.OrthogonalizeViewUp();
134 resetCameraClippingRange();
138 if (this.InteractionMode == 2)
142 double APoint[] = new double[3];
146 // get the current focal point and position
147 FPoint = cam.GetFocalPoint();
148 PPoint = cam.GetPosition();
150 // calculate the focal depth since we'll be using it a lot
151 ren.SetWorldPoint(FPoint[0],FPoint[1],FPoint[2],1.0);
152 ren.WorldToDisplay();
153 focalDepth = ren.GetDisplayPoint()[2];
155 APoint[0] = rw.GetSize()[0]/2.0 + (x - lastX);
156 APoint[1] = rw.GetSize()[1]/2.0 - (y - lastY);
157 APoint[2] = focalDepth;
158 ren.SetDisplayPoint(APoint);
159 ren.DisplayToWorld();
160 RPoint = ren.GetWorldPoint();
161 if (RPoint[3] != 0.0)
163 RPoint[0] = RPoint[0]/RPoint[3];
164 RPoint[1] = RPoint[1]/RPoint[3];
165 RPoint[2] = RPoint[2]/RPoint[3];
169 * Compute a translation vector, moving everything 1/2
170 * the distance to the cursor. (Arbitrary scale factor)
173 (FPoint[0]-RPoint[0])/2.0 + FPoint[0],
174 (FPoint[1]-RPoint[1])/2.0 + FPoint[1],
175 (FPoint[2]-RPoint[2])/2.0 + FPoint[2]);
177 (FPoint[0]-RPoint[0])/2.0 + PPoint[0],
178 (FPoint[1]-RPoint[1])/2.0 + PPoint[1],
179 (FPoint[2]-RPoint[2])/2.0 + PPoint[2]);
180 resetCameraClippingRange();
183 if (this.InteractionMode == 3)
186 //double clippingRange[];
188 zoomFactor = Math.pow(1.02,(y - lastY));
189 if (cam.GetParallelProjection() == 1)
191 cam.SetParallelScale(cam.GetParallelScale()/zoomFactor);
195 cam.Dolly(zoomFactor);
196 resetCameraClippingRange();
205 private List<vtkProp> selectActors = new ArrayList<vtkProp>();
206 private List<vtkProp> hoverActor = new ArrayList<vtkProp>();
209 public void mouseClicked(MouseEvent e) {
210 if (!panel.isFocusOwner())
212 if (e.getButton() != MouseEvent.BUTTON1)
216 vtkProp spick[] = panel.pick(e.getX(), e.getY());
217 if (spick != null && spick.length > 0) {
218 for (vtkProp selectActor : spick) {
219 if (!e.isControlDown()) {
220 selectActors.clear();
221 selectActors.add(selectActor);
223 if (selectActors.contains(selectActor))
224 selectActors.remove(selectActor);
226 selectActors.add(selectActor);
229 fireSelectionChanged();
230 } else if (!e.isControlDown()) {
231 selectActors.clear();
232 fireSelectionChanged();
235 // if (e.getClickCount() > 1)
236 // updatePickRay(e.getX(), e.getY());
240 // private void updatePickRay(double x , double y) {
241 // Ray ray = vtkUtil.createMouseRay(panel.GetRenderer(), x, y);
244 // System.out.println(ray.pos + " " + ray.dir);
245 // vtkPoints linePoints = new vtkPoints();
246 // linePoints.InsertPoint(0,ray.pos.x, ray.pos.y, ray.pos.z);
247 // linePoints.InsertPoint(1, ray.pos.x + ray.dir.x, ray.pos.y + ray.dir.y, ray.pos.z + ray.dir.z);
248 // vtkLine aLine = new vtkLine();
249 // aLine.GetPointIds().SetId(0, 0);
250 // aLine.GetPointIds().SetId(1, 1);
251 // vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
252 // aLineGrid.Allocate(1, 1);
253 // aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
254 // aLineGrid.SetPoints(linePoints);
255 // vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
256 // aLineMapper.SetInput(aLineGrid);
257 // vtkActor aLineActor = new vtkActor();
258 // aLineActor.SetMapper(aLineMapper);
259 // aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);
261 // if (rayActor != null) {
262 // panel.GetRenderer().RemoveActor(rayActor);
263 // rayActor.Delete();
265 // rayActor = aLineActor;
266 // panel.GetRenderer().AddActor(rayActor);
268 // linePoints.Delete();
270 // aLineGrid.Delete();
271 // aLineMapper.Delete();
275 // private vtkActor rayActor;
278 public void mouseMoved(MouseEvent e) {
282 if (!panel.isFocusOwner())
284 List<vtkProp> prevHover = new ArrayList<vtkProp>();
285 prevHover.addAll(hoverActor);
287 vtkProp pick[] = panel.pick(e.getX(),e.getY());
289 for (vtkProp p : pick)
293 if (!prevHover.containsAll(hoverActor) || !hoverActor.containsAll(prevHover)) {
299 * Returns mouse position in AWT screen coordinates.
302 public Point2i getMousePositionAWT() {
303 return new Point2i(lastX, lastY);
307 * Returns mouse position in VTK screen coordinates.
310 public Point2d getMousePosition() {
311 return new Point2d(lastX, rw.GetSize()[1]-lastY);
314 public List<vtkProp> getSelectActor() {
318 public List<vtkProp> getHoverActor() {
322 private List<ISelectionChangedListener> selectionListeners = new ArrayList<ISelectionChangedListener>();
325 public void addSelectionChangedListener(ISelectionChangedListener listener) {
326 selectionListeners.add(listener);
330 public ISelection getSelection() {
331 return new StructuredSelection(selectActors);
335 public void removeSelectionChangedListener(
336 ISelectionChangedListener listener) {
337 selectionListeners.remove(listener);
341 public void setSelection(ISelection selection) {
342 setSelection(selection, false);
346 public void setSelection(ISelection selection, boolean fire) {
347 Collection<vtkProp> selectedProps = AdaptationUtils.adaptToCollection(selection, vtkProp.class);
349 selectActors.clear();
350 selectActors.addAll(selectedProps);
352 fireSelectionChanged();
355 private void fireSelectionChanged() {
356 Display.getDefault().asyncExec(new Runnable() {
360 SelectionChangedEvent evt = new SelectionChangedEvent(vtkCameraAndSelectorAction.this, new StructuredSelection(selectActors));
361 for (ISelectionChangedListener l :selectionListeners) {
362 l.selectionChanged(evt);
370 private List<ISelectionChangedListener> hoverListeners = new ArrayList<ISelectionChangedListener>();
373 public void addHoverChangedListener(ISelectionChangedListener listener) {
374 hoverListeners.add(listener);
378 public ISelection getHoverSelection() {
379 return new StructuredSelection(hoverActor);
382 public void removeHoverChangedListener(
383 ISelectionChangedListener listener) {
384 hoverListeners.remove(listener);
387 private void fireHoverChanged() {
388 Display.getDefault().asyncExec(new Runnable() {
391 StructuredSelection sel = null;
392 if (hoverActor == null)
393 sel = new StructuredSelection();
395 sel = new StructuredSelection(hoverActor);
396 SelectionChangedEvent evt = new SelectionChangedEvent(vtkCameraAndSelectorAction.this, sel);
397 for (ISelectionChangedListener l :hoverListeners) {
398 l.selectionChanged(evt);
405 public vtkCamera getCamera() {
409 public Point3d getPosition() {
410 double pos[] = cam.GetPosition();
411 return new Point3d(pos);
414 public Point3d getFocus() {
415 double pos[] = cam.GetFocalPoint();
416 return new Point3d(pos);
419 public void setFocus(Point3d focus) {
420 setFocus(focus.x,focus.y,focus.z);
423 public void setFocus(double x, double y, double z) {
425 cam.SetFocalPoint(x, y, z);
428 cam.OrthogonalizeViewUp();
429 ren.ResetCameraClippingRange();
434 public void setPosition(Point3d position) {
435 setPosition(position.x,position.y,position.z);
438 public void setPosition(double x, double y, double z) {
440 cam.SetPosition(x, y, z);
443 cam.OrthogonalizeViewUp();
444 ren.ResetCameraClippingRange();