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 org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.ISelectionChangedListener;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.jface.viewers.SelectionChangedEvent;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.swt.widgets.Display;
26 import org.simantics.g3d.tools.AdaptationUtils;
30 import vtk.vtkRenderWindow;
31 import vtk.vtkRenderer;
33 public class vtkCameraAndSelectorAction extends vtkAwtAction implements ISelectionProvider {
35 protected vtkRenderer ren;
38 protected vtkRenderWindow rw;
39 protected vtkCamera cam;
40 protected int InteractionMode = 1;
42 protected double activeRate = 5.0;
43 protected double passiveRate = 0.01;
44 protected boolean doNotRotate = true;
46 public vtkCameraAndSelectorAction(InteractiveVtkPanel panel) {
48 this.ren = panel.GetRenderer();
49 this.rw = panel.GetRenderWindow();
50 this.cam = ren.GetActiveCamera();
57 public void UnLock() {
61 public void InteractionModeRotate()
63 this.InteractionMode = 1;
66 public void InteractionModeTranslate()
68 this.InteractionMode = 2;
71 public void InteractionModeZoom()
73 this.InteractionMode = 3;
76 public void resetCameraClippingRange() {
78 ren.ResetCameraClippingRange();
82 public void resetCamera() {
88 public void mousePressed(MouseEvent e)
91 if (ren.VisibleActorCount() == 0) return;
92 rw.SetDesiredUpdateRate(activeRate);
95 if ((e.getModifiers()==InputEvent.BUTTON2_MASK) ||
96 (e.getModifiers()==(InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK)))
98 InteractionModeTranslate();
100 else if (e.getModifiers()==InputEvent.BUTTON3_MASK)
102 InteractionModeZoom();
106 InteractionModeRotate();
110 public void mouseReleased(MouseEvent e)
112 rw.SetDesiredUpdateRate(passiveRate);
117 public void mouseDragged(MouseEvent e)
119 if (ren.VisibleActorCount() == 0) return;
123 if (this.InteractionMode == 1)
125 cam.Azimuth(lastX - x);
126 cam.Elevation(y - lastY);
129 cam.OrthogonalizeViewUp();
130 resetCameraClippingRange();
134 if (this.InteractionMode == 2)
138 double APoint[] = new double[3];
142 // get the current focal point and position
143 FPoint = cam.GetFocalPoint();
144 PPoint = cam.GetPosition();
146 // calculate the focal depth since we'll be using it a lot
147 ren.SetWorldPoint(FPoint[0],FPoint[1],FPoint[2],1.0);
148 ren.WorldToDisplay();
149 focalDepth = ren.GetDisplayPoint()[2];
151 APoint[0] = rw.GetSize()[0]/2.0 + (x - lastX);
152 APoint[1] = rw.GetSize()[1]/2.0 - (y - lastY);
153 APoint[2] = focalDepth;
154 ren.SetDisplayPoint(APoint);
155 ren.DisplayToWorld();
156 RPoint = ren.GetWorldPoint();
157 if (RPoint[3] != 0.0)
159 RPoint[0] = RPoint[0]/RPoint[3];
160 RPoint[1] = RPoint[1]/RPoint[3];
161 RPoint[2] = RPoint[2]/RPoint[3];
165 * Compute a translation vector, moving everything 1/2
166 * the distance to the cursor. (Arbitrary scale factor)
169 (FPoint[0]-RPoint[0])/2.0 + FPoint[0],
170 (FPoint[1]-RPoint[1])/2.0 + FPoint[1],
171 (FPoint[2]-RPoint[2])/2.0 + FPoint[2]);
173 (FPoint[0]-RPoint[0])/2.0 + PPoint[0],
174 (FPoint[1]-RPoint[1])/2.0 + PPoint[1],
175 (FPoint[2]-RPoint[2])/2.0 + PPoint[2]);
176 resetCameraClippingRange();
179 if (this.InteractionMode == 3)
182 //double clippingRange[];
184 zoomFactor = Math.pow(1.02,(y - lastY));
185 if (cam.GetParallelProjection() == 1)
187 cam.SetParallelScale(cam.GetParallelScale()/zoomFactor);
191 cam.Dolly(zoomFactor);
192 resetCameraClippingRange();
201 private List<vtkProp> selectActors = new ArrayList<vtkProp>();
202 private List<vtkProp> hoverActor = new ArrayList<vtkProp>();
205 public void mouseClicked(MouseEvent e) {
206 if (!panel.isFocusOwner())
208 if (e.getButton() != MouseEvent.BUTTON1)
210 vtkProp spick[] = panel.pick(e.getX(), e.getY());
211 if (spick != null && spick.length > 0) {
212 for (vtkProp selectActor : spick) {
213 if (!e.isControlDown()) {
214 selectActors.clear();
215 selectActors.add(selectActor);
217 if (selectActors.contains(selectActor))
218 selectActors.remove(selectActor);
220 selectActors.add(selectActor);
223 fireSelectionChanged();
224 } else if (!e.isControlDown()) {
225 selectActors.clear();
226 fireSelectionChanged();
229 // if (e.getClickCount() > 1)
230 // updatePickRay(e.getX(), e.getY());
234 // private void updatePickRay(double x , double y) {
235 // Ray ray = vtkUtil.createMouseRay(panel.GetRenderer(), x, y);
238 // System.out.println(ray.pos + " " + ray.dir);
239 // vtkPoints linePoints = new vtkPoints();
240 // linePoints.InsertPoint(0,ray.pos.x, ray.pos.y, ray.pos.z);
241 // linePoints.InsertPoint(1, ray.pos.x + ray.dir.x, ray.pos.y + ray.dir.y, ray.pos.z + ray.dir.z);
242 // vtkLine aLine = new vtkLine();
243 // aLine.GetPointIds().SetId(0, 0);
244 // aLine.GetPointIds().SetId(1, 1);
245 // vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
246 // aLineGrid.Allocate(1, 1);
247 // aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
248 // aLineGrid.SetPoints(linePoints);
249 // vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
250 // aLineMapper.SetInput(aLineGrid);
251 // vtkActor aLineActor = new vtkActor();
252 // aLineActor.SetMapper(aLineMapper);
253 // aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);
255 // if (rayActor != null) {
256 // panel.GetRenderer().RemoveActor(rayActor);
257 // rayActor.Delete();
259 // rayActor = aLineActor;
260 // panel.GetRenderer().AddActor(rayActor);
262 // linePoints.Delete();
264 // aLineGrid.Delete();
265 // aLineMapper.Delete();
269 // private vtkActor rayActor;
272 public void mouseMoved(MouseEvent e) {
276 if (!panel.isFocusOwner())
278 List<vtkProp> prevHover = new ArrayList<vtkProp>();
279 prevHover.addAll(hoverActor);
281 vtkProp pick[] = panel.pick(e.getX(),e.getY());
283 for (vtkProp p : pick)
287 if (!prevHover.containsAll(hoverActor) || !hoverActor.containsAll(prevHover)) {
292 public List<vtkProp> getSelectActor() {
296 public List<vtkProp> getHoverActor() {
300 private List<ISelectionChangedListener> selectionListeners = new ArrayList<ISelectionChangedListener>();
303 public void addSelectionChangedListener(ISelectionChangedListener listener) {
304 selectionListeners.add(listener);
308 public ISelection getSelection() {
309 return new StructuredSelection(selectActors);
313 public void removeSelectionChangedListener(
314 ISelectionChangedListener listener) {
315 selectionListeners.remove(listener);
319 public void setSelection(ISelection selection) {
320 setSelection(selection, false);
324 public void setSelection(ISelection selection, boolean fire) {
325 Collection<vtkProp> selectedProps = AdaptationUtils.adaptToCollection(selection, vtkProp.class);
327 selectActors.clear();
328 selectActors.addAll(selectedProps);
330 fireSelectionChanged();
333 private void fireSelectionChanged() {
334 Display.getDefault().asyncExec(new Runnable() {
338 SelectionChangedEvent evt = new SelectionChangedEvent(vtkCameraAndSelectorAction.this, new StructuredSelection(selectActors));
339 for (ISelectionChangedListener l :selectionListeners) {
340 l.selectionChanged(evt);
348 private List<ISelectionChangedListener> hoverListeners = new ArrayList<ISelectionChangedListener>();
351 public void addHoverChangedListener(ISelectionChangedListener listener) {
352 hoverListeners.add(listener);
356 public ISelection getHoverSelection() {
357 return new StructuredSelection(hoverActor);
360 public void removeHoverChangedListener(
361 ISelectionChangedListener listener) {
362 hoverListeners.remove(listener);
365 private void fireHoverChanged() {
366 Display.getDefault().asyncExec(new Runnable() {
369 StructuredSelection sel = null;
370 if (hoverActor == null)
371 sel = new StructuredSelection();
373 sel = new StructuredSelection(hoverActor);
374 SelectionChangedEvent evt = new SelectionChangedEvent(vtkCameraAndSelectorAction.this, sel);
375 for (ISelectionChangedListener l :hoverListeners) {
376 l.selectionChanged(evt);
383 public void focus(double x, double y, double z) {
385 cam.SetFocalPoint(x, y, z);
388 cam.OrthogonalizeViewUp();
389 resetCameraClippingRange();