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.swt;
14 import java.awt.event.InputEvent;
15 import java.awt.event.MouseEvent;
16 import java.awt.event.MouseWheelEvent;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionChangedListener;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.swt.widgets.Display;
27 import org.simantics.g3d.tools.AdaptationUtils;
31 import vtk.vtkRenderWindow;
32 import vtk.vtkRenderer;
34 public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelectionProvider {
36 protected vtkRenderer ren;
39 protected vtkRenderWindow rw;
40 protected vtkCamera cam;
41 protected int InteractionMode = 1;
43 protected double activeRate = 5.0;
44 protected double passiveRate = 0.01;
45 protected boolean doNotRotate = true;
46 private double[] upDirection = new double[] { 0, 1, 0 };
48 public vtkCameraAndSelectorAction(InteractiveVtkComposite panel) {
50 this.ren = panel.getRenderer();
51 this.rw = panel.getRenderWindow();
52 this.cam = ren.GetActiveCamera();
55 public void setUpDirection(double[] upDirection) {
56 this.upDirection = upDirection;
63 public void UnLock() {
67 public void InteractionModeRotate() {
68 this.InteractionMode = 1;
71 public void InteractionModeTranslate() {
72 this.InteractionMode = 2;
75 public void InteractionModeZoom() {
76 this.InteractionMode = 3;
79 public void resetCameraClippingRange() {
81 ren.ResetCameraClippingRange();
85 public void resetCamera() {
91 public boolean mousePressed(MouseEvent e) {
93 if (ren.VisibleActorCount() == 0)
95 rw.SetDesiredUpdateRate(activeRate);
98 if ((e.getModifiers() == InputEvent.BUTTON2_MASK)
99 || (e.getModifiers() == (InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK))) {
100 InteractionModeTranslate();
101 } else if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
102 InteractionModeZoom();
104 InteractionModeRotate();
109 public boolean mouseReleased(MouseEvent e) {
110 rw.SetDesiredUpdateRate(passiveRate);
114 public boolean mouseDragged(MouseEvent e) {
115 if (ren.VisibleActorCount() == 0)
120 if (this.InteractionMode == 1) {
121 cam.Azimuth(lastX - x);
122 cam.Elevation(y - lastY);
124 cam.SetViewUp(upDirection);
125 cam.OrthogonalizeViewUp();
126 resetCameraClippingRange();
127 // panel.UpdateLight();
130 if (this.InteractionMode == 2) {
136 // get the current focal point and position
137 FPoint = cam.GetFocalPoint();
138 PPoint = cam.GetPosition();
140 // calculate the focal depth since we'll be using it a lot
141 ren.SetWorldPoint(FPoint[0], FPoint[1], FPoint[2], 1.0);
142 ren.WorldToDisplay();
143 APoint = ren.GetDisplayPoint();
145 // int[] size = rw.GetSize();
146 APoint[0] -= x - lastX;
147 APoint[1] += y - lastY;
148 ren.SetDisplayPoint(APoint);
149 ren.DisplayToWorld();
150 RPoint = ren.GetWorldPoint();
152 if (RPoint[3] != 0.0) {
153 RPoint[0] = RPoint[0] / RPoint[3];
154 RPoint[1] = RPoint[1] / RPoint[3];
155 RPoint[2] = RPoint[2] / RPoint[3];
159 * Compute a translation vector, moving everything 1/2 the distance to the
160 * cursor. (Arbitrary scale factor)
162 cam.SetFocalPoint(RPoint);
163 cam.SetPosition((RPoint[0] - FPoint[0]) + PPoint[0], (RPoint[1] - FPoint[1]) + PPoint[1],
164 (RPoint[2] - FPoint[2]) + PPoint[2]);
165 resetCameraClippingRange();
168 if (this.InteractionMode == 3) {
170 // double clippingRange[];
172 zoomFactor = Math.pow(1.02, (y - lastY));
173 if (cam.GetParallelProjection() == 1) {
174 cam.SetParallelScale(cam.GetParallelScale() / zoomFactor);
175 resetCameraClippingRange();
177 cam.Dolly(zoomFactor);
178 resetCameraClippingRange();
188 public boolean mouseWheelMoved(MouseWheelEvent e) {
190 // double clippingRange[];
191 zoomFactor = Math.pow(1.02, (e.getWheelRotation()));
192 if (cam.GetParallelProjection() == 1) {
193 cam.SetParallelScale(cam.GetParallelScale() / zoomFactor);
195 cam.Dolly(zoomFactor);
196 resetCameraClippingRange();
202 protected List<vtkProp> selectActors = new ArrayList<vtkProp>();
203 protected List<vtkProp> hoverActor = new ArrayList<vtkProp>();
206 public boolean mouseClicked(MouseEvent e) {
207 if (!panel.getComponent().isFocusControl())
209 if (e.getButton() != MouseEvent.BUTTON1)
211 vtkProp spick[] = panel.pick(e.getX(), e.getY());
212 if (spick != null && spick.length > 0) {
213 for (vtkProp selectActor : spick) {
214 if (!e.isControlDown()) {
215 selectActors.clear();
216 selectActors.add(selectActor);
218 if (selectActors.contains(selectActor))
219 selectActors.remove(selectActor);
221 selectActors.add(selectActor);
224 fireSelectionChanged();
225 } else if (!e.isControlDown()) {
226 selectActors.clear();
227 fireSelectionChanged();
230 // if (e.getClickCount() > 1)
231 // updatePickRay(e.getX(), e.getY());
235 // private void updatePickRay(double x , double y) {
236 // Ray ray = vtkUtil.createMouseRay(panel.GetRenderer(), x, y);
239 // System.out.println(ray.pos + " " + ray.dir);
240 // vtkPoints linePoints = new vtkPoints();
241 // linePoints.InsertPoint(0,ray.pos.x, ray.pos.y, ray.pos.z);
242 // linePoints.InsertPoint(1, ray.pos.x + ray.dir.x, ray.pos.y + ray.dir.y,
243 // ray.pos.z + ray.dir.z);
244 // vtkLine aLine = new vtkLine();
245 // aLine.GetPointIds().SetId(0, 0);
246 // aLine.GetPointIds().SetId(1, 1);
247 // vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
248 // aLineGrid.Allocate(1, 1);
249 // aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
250 // aLineGrid.SetPoints(linePoints);
251 // vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
252 // aLineMapper.SetInput(aLineGrid);
253 // vtkActor aLineActor = new vtkActor();
254 // aLineActor.SetMapper(aLineMapper);
255 // aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);
257 // if (rayActor != null) {
258 // panel.GetRenderer().RemoveActor(rayActor);
259 // rayActor.Delete();
261 // rayActor = aLineActor;
262 // panel.GetRenderer().AddActor(rayActor);
264 // linePoints.Delete();
266 // aLineGrid.Delete();
267 // aLineMapper.Delete();
271 // private vtkActor rayActor;
274 public boolean mouseMoved(MouseEvent e) {
278 if (!panel.getComponent().isFocusControl())
280 List<vtkProp> prevHover = new ArrayList<vtkProp>();
281 prevHover.addAll(hoverActor);
283 vtkProp pick[] = panel.pick(e.getX(), e.getY());
285 for (vtkProp p : pick)
289 if (!prevHover.containsAll(hoverActor) || !hoverActor.containsAll(prevHover)) {
295 public List<vtkProp> getSelectActor() {
299 public List<vtkProp> getHoverActor() {
303 private List<ISelectionChangedListener> selectionListeners = new ArrayList<ISelectionChangedListener>();
306 public void addSelectionChangedListener(ISelectionChangedListener listener) {
307 selectionListeners.add(listener);
311 public ISelection getSelection() {
312 return new StructuredSelection(selectActors);
316 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
317 selectionListeners.remove(listener);
321 public void setSelection(ISelection selection) {
322 setSelection(selection, false);
326 public void setSelection(ISelection selection, boolean fire) {
327 Collection<vtkProp> selectedProps = AdaptationUtils.adaptToCollection(selection, vtkProp.class);
329 selectActors.clear();
330 selectActors.addAll(selectedProps);
332 fireSelectionChanged();
335 protected void fireSelectionChanged() {
336 Display.getDefault().asyncExec(new Runnable() {
340 SelectionChangedEvent evt = new SelectionChangedEvent(vtkCameraAndSelectorAction.this,
341 new StructuredSelection(selectActors));
342 for (ISelectionChangedListener l : selectionListeners) {
343 l.selectionChanged(evt);
350 private List<ISelectionChangedListener> hoverListeners = new ArrayList<ISelectionChangedListener>();
352 public void addHoverChangedListener(ISelectionChangedListener listener) {
353 hoverListeners.add(listener);
356 public ISelection getHoverSelection() {
357 return new StructuredSelection(hoverActor);
360 public void removeHoverChangedListener(ISelectionChangedListener listener) {
361 hoverListeners.remove(listener);
364 private void fireHoverChanged() {
365 Display.getDefault().asyncExec(new Runnable() {
368 StructuredSelection sel = null;
369 if (hoverActor == null)
370 sel = new StructuredSelection();
372 sel = new StructuredSelection(hoverActor);
373 SelectionChangedEvent evt = new SelectionChangedEvent(vtkCameraAndSelectorAction.this, sel);
374 for (ISelectionChangedListener l : hoverListeners) {
375 l.selectionChanged(evt);
382 public void focus(double x, double y, double z) {
384 cam.SetFocalPoint(x, y, z);
386 cam.SetViewUp(upDirection);
387 cam.OrthogonalizeViewUp();
388 resetCameraClippingRange();
389 // panel.UpdateLight();