/******************************************************************************* * Copyright (c) 2012, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.g3d.vtk.gizmo; import java.util.Collection; import javax.vecmath.AxisAngle4d; import javax.vecmath.Tuple3d; import javax.vecmath.Vector3d; import org.simantics.g3d.gizmo.Gizmo; import org.simantics.g3d.math.MathTools; import vtk.vtkProp; import vtk.vtkProp3D; import vtk.vtkRenderer; public abstract class vtkGizmo implements Gizmo { private vtkRenderer ren1; private Collection gizmo; private Tuple3d position; private AxisAngle4d orientation; private Tuple3d scale; @Override public void attach(Object renderingPart) { if (ren1 != null) throw new RuntimeException("Gizmo is attached"); ren1 = (vtkRenderer)renderingPart; attachActors(); } @Override public void deattach() { if (ren1 == null) throw new RuntimeException("Gizmo is not attached"); deattachActors(); ren1 = null; } public boolean isAttached() { return (ren1 != null); } protected void attachActors() { gizmo = getGizmo(); for (vtkProp p : gizmo) { ren1.AddActor(p); } if (position != null) setPosition(position); if (orientation != null) setRotation(orientation); if (scale != null) setScale(scale); } protected void deattachActors() { for (vtkProp p : gizmo) { ren1.RemoveActor(p); } } @Override public boolean isPartOf(vtkProp pickedObject) { for (vtkProp prop : gizmo) { if (prop.equals(pickedObject)) return true; } return false; } public void setPosition(Tuple3d position) { this.position = position; for (vtkProp p : gizmo) { ((vtkProp3D)p).SetPosition(position.x, position.y, position.z); } } public void setRotation(AxisAngle4d q) { this.orientation = q; for (vtkProp p : gizmo) { ((vtkProp3D)p).SetOrientation(0,0,0); ((vtkProp3D)p).RotateWXYZ(MathTools.radToDeg(q.angle), q.x, q.y, q.z); } } public void setScale(Tuple3d s) { this.scale = s; for (vtkProp p : gizmo) { ((vtkProp3D)p).SetScale(s.x, s.y, s.z); } } public void setScale(double s) { this.scale = new Vector3d(s,s,s); for (vtkProp p : gizmo) { ((vtkProp3D)p).SetScale(s, s, s); } } public abstract Collection getGizmo(); // public double[] add(double[] color1, double[] color2) { // double[] result = new double[]{color1[0]+color2[0],color1[1],+color2[1],color1[2]+color2[2]}; // return result; // } // // public double[] add(double[] color1, double[] color2, double[] color3) { // double[] result = new double[]{color1[0]+color2[0]+color3[0],color1[1],+color2[1]+color3[1],color1[2]+color2[2]+color3[2]}; // return result; // } public double[] add(double[]... color) { double result[] = new double[]{0,0,0}; for (double c[] : color) { for (int i = 0; i < 3; i++) result[i] += c[i]; } return result; } public vtkRenderer getRenderer() { return ren1; } }