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.Point3d;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.ISelectionProvider;
25 import org.eclipse.jface.viewers.SelectionChangedEvent;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.swt.widgets.Display;
28 import org.simantics.g3d.tools.AdaptationUtils;
32 import vtk.vtkRenderWindow;
33 import vtk.vtkRenderer;
35 public class vtkCameraAndSelectorAction extends vtkAwtAction implements ISelectionProvider {
37 protected vtkRenderer ren;
40 protected vtkRenderWindow rw;
41 protected vtkCamera cam;
42 protected int InteractionMode = 1;
44 protected double activeRate = 5.0;
45 protected double passiveRate = 0.01;
46 protected boolean doNotRotate = true;
48 public vtkCameraAndSelectorAction(InteractiveVtkPanel panel) {
50 this.ren = panel.GetRenderer();
51 this.rw = panel.GetRenderWindow();
52 this.cam = ren.GetActiveCamera();
59 public void UnLock() {
63 public void InteractionModeRotate()
65 this.InteractionMode = 1;
68 public void InteractionModeTranslate()
70 this.InteractionMode = 2;
73 public void InteractionModeZoom()
75 this.InteractionMode = 3;
78 public void resetCameraClippingRange() {
80 ren.ResetCameraClippingRange();
84 public void resetCamera() {
90 public void mousePressed(MouseEvent e)
93 if (ren.VisibleActorCount() == 0) return;
94 rw.SetDesiredUpdateRate(activeRate);
97 if ((e.getModifiers()==InputEvent.BUTTON2_MASK) ||
98 (e.getModifiers()==(InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK)))
100 InteractionModeTranslate();
102 else if (e.getModifiers()==InputEvent.BUTTON3_MASK)
104 InteractionModeZoom();
108 InteractionModeRotate();
112 public void mouseReleased(MouseEvent e)
114 rw.SetDesiredUpdateRate(passiveRate);
119 public void mouseDragged(MouseEvent e)
121 if (ren.VisibleActorCount() == 0) return;
125 if (this.InteractionMode == 1)
127 cam.Azimuth(lastX - x);
128 cam.Elevation(y - lastY);
131 cam.OrthogonalizeViewUp();
132 resetCameraClippingRange();
136 if (this.InteractionMode == 2)
140 double APoint[] = new double[3];
144 // get the current focal point and position
145 FPoint = cam.GetFocalPoint();
146 PPoint = cam.GetPosition();
148 // calculate the focal depth since we'll be using it a lot
149 ren.SetWorldPoint(FPoint[0],FPoint[1],FPoint[2],1.0);
150 ren.WorldToDisplay();
151 focalDepth = ren.GetDisplayPoint()[2];
153 APoint[0] = rw.GetSize()[0]/2.0 + (x - lastX);
154 APoint[1] = rw.GetSize()[1]/2.0 - (y - lastY);
155 APoint[2] = focalDepth;
156 ren.SetDisplayPoint(APoint);
157 ren.DisplayToWorld();
158 RPoint = ren.GetWorldPoint();
159 if (RPoint[3] != 0.0)
161 RPoint[0] = RPoint[0]/RPoint[3];
162 RPoint[1] = RPoint[1]/RPoint[3];
163 RPoint[2] = RPoint[2]/RPoint[3];
167 * Compute a translation vector, moving everything 1/2
168 * the distance to the cursor. (Arbitrary scale factor)
171 (FPoint[0]-RPoint[0])/2.0 + FPoint[0],
172 (FPoint[1]-RPoint[1])/2.0 + FPoint[1],
173 (FPoint[2]-RPoint[2])/2.0 + FPoint[2]);
175 (FPoint[0]-RPoint[0])/2.0 + PPoint[0],
176 (FPoint[1]-RPoint[1])/2.0 + PPoint[1],
177 (FPoint[2]-RPoint[2])/2.0 + PPoint[2]);
178 resetCameraClippingRange();
181 if (this.InteractionMode == 3)
184 //double clippingRange[];
186 zoomFactor = Math.pow(1.02,(y - lastY));
187 if (cam.GetParallelProjection() == 1)
189 cam.SetParallelScale(cam.GetParallelScale()/zoomFactor);
193 cam.Dolly(zoomFactor);
194 resetCameraClippingRange();
203 private List<vtkProp> selectActors = new ArrayList<vtkProp>();
204 private List<vtkProp> hoverActor = new ArrayList<vtkProp>();
207 public void mouseClicked(MouseEvent e) {
208 if (!panel.isFocusOwner())
210 if (e.getButton() != MouseEvent.BUTTON1)
212 vtkProp spick[] = panel.pick(e.getX(), e.getY());
213 if (spick != null && spick.length > 0) {
214 for (vtkProp selectActor : spick) {
215 if (!e.isControlDown()) {
216 selectActors.clear();
217 selectActors.add(selectActor);
219 if (selectActors.contains(selectActor))
220 selectActors.remove(selectActor);
222 selectActors.add(selectActor);
225 fireSelectionChanged();
226 } else if (!e.isControlDown()) {
227 selectActors.clear();
228 fireSelectionChanged();
231 // if (e.getClickCount() > 1)
232 // updatePickRay(e.getX(), e.getY());
236 // private void updatePickRay(double x , double y) {
237 // Ray ray = vtkUtil.createMouseRay(panel.GetRenderer(), x, y);
240 // System.out.println(ray.pos + " " + ray.dir);
241 // vtkPoints linePoints = new vtkPoints();
242 // linePoints.InsertPoint(0,ray.pos.x, ray.pos.y, ray.pos.z);
243 // linePoints.InsertPoint(1, ray.pos.x + ray.dir.x, ray.pos.y + ray.dir.y, 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 void mouseMoved(MouseEvent e) {
278 if (!panel.isFocusOwner())
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)) {
294 public List<vtkProp> getSelectActor() {
298 public List<vtkProp> getHoverActor() {
302 private List<ISelectionChangedListener> selectionListeners = new ArrayList<ISelectionChangedListener>();
305 public void addSelectionChangedListener(ISelectionChangedListener listener) {
306 selectionListeners.add(listener);
310 public ISelection getSelection() {
311 return new StructuredSelection(selectActors);
315 public void removeSelectionChangedListener(
316 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 private void fireSelectionChanged() {
336 Display.getDefault().asyncExec(new Runnable() {
340 SelectionChangedEvent evt = new SelectionChangedEvent(vtkCameraAndSelectorAction.this, new StructuredSelection(selectActors));
341 for (ISelectionChangedListener l :selectionListeners) {
342 l.selectionChanged(evt);
350 private List<ISelectionChangedListener> hoverListeners = new ArrayList<ISelectionChangedListener>();
353 public void addHoverChangedListener(ISelectionChangedListener listener) {
354 hoverListeners.add(listener);
358 public ISelection getHoverSelection() {
359 return new StructuredSelection(hoverActor);
362 public void removeHoverChangedListener(
363 ISelectionChangedListener listener) {
364 hoverListeners.remove(listener);
367 private void fireHoverChanged() {
368 Display.getDefault().asyncExec(new Runnable() {
371 StructuredSelection sel = null;
372 if (hoverActor == null)
373 sel = new StructuredSelection();
375 sel = new StructuredSelection(hoverActor);
376 SelectionChangedEvent evt = new SelectionChangedEvent(vtkCameraAndSelectorAction.this, sel);
377 for (ISelectionChangedListener l :hoverListeners) {
378 l.selectionChanged(evt);
385 public vtkCamera getCamera() {
389 public Point3d getPosition() {
390 double pos[] = cam.GetPosition();
391 return new Point3d(pos);
394 public Point3d getFocus() {
395 double pos[] = cam.GetFocalPoint();
396 return new Point3d(pos);
399 public void setFocus(Point3d focus) {
400 setFocus(focus.x,focus.y,focus.z);
403 public void setFocus(double x, double y, double z) {
405 cam.SetFocalPoint(x, y, z);
408 cam.OrthogonalizeViewUp();
409 ren.ResetCameraClippingRange();
414 public void setPosition(Point3d position) {
415 setPosition(position.x,position.y,position.z);
418 public void setPosition(double x, double y, double z) {
420 cam.SetPosition(x, y, z);
423 cam.OrthogonalizeViewUp();
424 ren.ResetCameraClippingRange();