/******************************************************************************* * 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.shape; import javax.vecmath.Matrix4d; import javax.vecmath.Point3d; import org.simantics.g3d.math.MathTools; import org.simantics.utils.threads.AWTThread; import org.simantics.utils.threads.ThreadUtils; import vtk.vtkActor; import vtk.vtkAssembly; import vtk.vtkAxes; import vtk.vtkConeSource; import vtk.vtkLinearTransform; import vtk.vtkMatrix4x4; import vtk.vtkPolyDataMapper; import vtk.vtkProp3D; import vtk.vtkRenderer; import vtk.vtkSphereSource; import vtk.vtkTextActor; import vtk.vtkTubeFilter; public class axesSphereActor extends vtkAssembly implements IvtkVisualObject{ private vtkRenderer ren; private double axisLength = 0.8; private vtkTextActor xactor, yactor, zactor; private vtkActor tubeActor; private vtkActor xconeActor; private vtkActor yconeActor; private vtkActor zconeActor; private vtkActor oBallActor; private boolean rendered = false; public axesSphereActor(vtkRenderer _ren) { super(); ren = _ren; createAxes(); } public axesSphereActor(vtkRenderer _ren, double axisLength) { super(); ren = _ren; this.axisLength = axisLength; createAxes(); } public void createAxes() { vtkAxes axes = new vtkAxes(); axes.SetOrigin(0, 0, 0); axes.SetScaleFactor(axisLength); vtkTubeFilter tube = new vtkTubeFilter(); tube.SetInputData(axes.GetOutput()); tube.SetRadius(0.05 * axisLength); tube.SetNumberOfSides(8); vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper(); tubeMapper.SetInputData(tube.GetOutput()); tubeActor = new vtkActor(); tubeActor.SetMapper(tubeMapper); tubeActor.PickableOff(); int coneRes = 12; double coneScale = 0.3 * axisLength; // --- x-Cone vtkConeSource xcone = new vtkConeSource(); xcone.SetResolution(coneRes); vtkPolyDataMapper xconeMapper = new vtkPolyDataMapper(); xconeMapper.SetInputData(xcone.GetOutput()); xconeActor = new vtkActor(); xconeActor.SetMapper(xconeMapper); xconeActor.GetProperty().SetColor(1, 0, 0); xconeActor.SetScale(coneScale, coneScale, coneScale); xconeActor.SetPosition(axisLength, 0.0, 0.0); xconeActor.SetPickable(0); // --- y-Cone vtkConeSource ycone = new vtkConeSource(); ycone.SetResolution(coneRes); vtkPolyDataMapper yconeMapper = new vtkPolyDataMapper(); yconeMapper.SetInputData(ycone.GetOutput()); yconeActor = new vtkActor(); yconeActor.SetMapper(yconeMapper); yconeActor.GetProperty().SetColor(1, 1, 0); yconeActor.RotateZ(90); yconeActor.SetScale(coneScale, coneScale, coneScale); yconeActor.SetPosition(0.0, axisLength, 0.0); yconeActor.SetPickable(0); // --- z-Cone vtkConeSource zcone = new vtkConeSource(); zcone.SetResolution(coneRes); vtkPolyDataMapper zconeMapper = new vtkPolyDataMapper(); zconeMapper.SetInputData(zcone.GetOutput()); zconeActor = new vtkActor(); zconeActor.SetMapper(zconeMapper); zconeActor.GetProperty().SetColor(0, 1, 0); zconeActor.RotateY(-90); zconeActor.SetScale(coneScale, coneScale, coneScale); zconeActor.SetPosition(0.0, 0.0, axisLength); zconeActor.SetPickable(0); vtkSphereSource ball = new vtkSphereSource(); ball.SetRadius(axisLength * 0.3); ball.SetPhiResolution(6); ball.SetThetaResolution(8); vtkPolyDataMapper ballMapper = new vtkPolyDataMapper(); ballMapper.SetInputData(ball.GetOutput()); oBallActor = new vtkActor(); oBallActor.SetMapper(ballMapper); oBallActor.GetProperty().SetColor(0, 0, 1); oBallActor.SetPickable(0); this.AddPart(tubeActor); this.AddPart(xconeActor); this.AddPart(yconeActor); this.AddPart(zconeActor); this.AddPart(oBallActor); tube.GetOutput().Delete(); xcone.GetOutput().Delete(); ycone.GetOutput().Delete(); zcone.GetOutput().Delete(); axes.GetOutput().Delete(); ball.GetOutput().Delete(); axes.Delete(); tube.Delete(); xcone.Delete(); ycone.Delete(); zcone.Delete(); ball.Delete(); tubeMapper.Delete(); xconeMapper.Delete(); yconeMapper.Delete(); zconeMapper.Delete(); ballMapper.Delete(); xconeActor.GetProperty().Delete(); yconeActor.GetProperty().Delete(); zconeActor.GetProperty().Delete(); oBallActor.GetProperty().Delete(); xactor = new vtkTextActor(); yactor = new vtkTextActor(); zactor = new vtkTextActor(); xactor.SetInput("X"); yactor.SetInput("Y"); zactor.SetInput("Z"); // xactor.SetTextScaleModeToViewport(); // yactor.SetTextScaleModeToViewport(); // zactor.SetTextScaleModeToViewport(); xactor.SetTextScaleModeToNone(); yactor.SetTextScaleModeToNone(); zactor.SetTextScaleModeToNone(); xactor.GetTextProperty().SetColor(0.0, 0.0, 0.0); xactor.GetTextProperty().ShadowOff(); xactor.GetTextProperty().ItalicOff(); xactor.GetTextProperty().BoldOff(); yactor.GetTextProperty().SetColor(0.0, 0.0, 0.0); yactor.GetTextProperty().ShadowOff(); yactor.GetTextProperty().ItalicOff(); yactor.GetTextProperty().BoldOff(); zactor.GetTextProperty().SetColor(0.0, 0.0, 0.0); zactor.GetTextProperty().ShadowOff(); zactor.GetTextProperty().ItalicOff(); zactor.GetTextProperty().BoldOff(); xactor.GetTextProperty().Delete(); yactor.GetTextProperty().Delete(); zactor.GetTextProperty().Delete(); xactor.SetMaximumLineHeight(0.25); yactor.SetMaximumLineHeight(0.25); zactor.SetMaximumLineHeight(0.25); xactor.SetPickable(0); yactor.SetPickable(0); zactor.SetPickable(0); } public void addToRenderer() { if (rendered) return; rendered = true; ren.AddActor2D(xactor); ren.AddActor2D(yactor); ren.AddActor2D(zactor); ren.AddActor(this); } public void removeFromRenderer() { if (!rendered) return; rendered = false; ren.RemoveActor2D(xactor); ren.RemoveActor2D(yactor); ren.RemoveActor2D(zactor); ren.RemoveActor(this); } public boolean isRendered() { return rendered; } public void setAxesVisibility(boolean ison) { this.SetVisibility(ison ? 1 : 0); xactor.SetVisibility(ison ? 1 : 0); yactor.SetVisibility(ison ? 1 : 0); zactor.SetVisibility(ison ? 1 : 0); } private boolean labelVisible = true; public void setLabelVisibility(boolean ison) { xactor.SetVisibility(ison ? 1 : 0); yactor.SetVisibility(ison ? 1 : 0); zactor.SetVisibility(ison ? 1 : 0); labelVisible = ison; if (labelVisible) updateTextLoc(); } Matrix4d m = new Matrix4d(); double mat[] = new double[16]; Point3d x = new Point3d(); Point3d y = new Point3d(); Point3d z = new Point3d(); private void updateTextLoc() { if (!labelVisible) return; xactor.GetPositionCoordinate().SetCoordinateSystemToWorld(); yactor.GetPositionCoordinate().SetCoordinateSystemToWorld(); zactor.GetPositionCoordinate().SetCoordinateSystemToWorld(); GetMatrix(mat); MathTools.set(m, mat); x.set(axisLength, 0,0); y.set(0,axisLength, 0); z.set(0,0,axisLength); m.transform(x); m.transform(y); m.transform(z); xactor.GetPositionCoordinate().SetValue(x.x, x.y, x.z); yactor.GetPositionCoordinate().SetValue(y.x, y.y, y.z); zactor.GetPositionCoordinate().SetValue(z.x, z.y, z.z); xactor.GetPositionCoordinate().Delete(); yactor.GetPositionCoordinate().Delete(); zactor.GetPositionCoordinate().Delete(); } @Override public void SetPickable(int id0) { super.SetPickable(id0); tubeActor.SetPickable(id0); xconeActor.SetPickable(id0); yconeActor.SetPickable(id0); zconeActor.SetPickable(id0); oBallActor.SetPickable(id0); } @Override public void SetOrientation(double id0, double id1, double id2) { super.SetOrientation(id0, id1, id2); updateTextLoc(); } @Override public void RotateWXYZ(double id0, double id1, double id2, double id3) { super.RotateWXYZ(id0, id1, id2, id3); updateTextLoc(); } @Override public void SetPosition(double[] id0) { super.SetPosition(id0); updateTextLoc(); } @Override public void SetPosition(double id0, double id1, double id2) { super.SetPosition(id0, id1, id2); updateTextLoc(); } @Override public void SetOrientation(double[] id0) { super.SetOrientation(id0); updateTextLoc(); } @Override public void SetScale(double id0) { super.SetScale(id0); updateTextLoc(); } @Override public void SetScale(double id0, double id1, double id2) { super.SetScale(id0, id1, id2); updateTextLoc(); } @Override public void SetScale(double[] id0) { super.SetScale(id0); updateTextLoc(); } @Override public void SetUserMatrix(vtkMatrix4x4 id0) { super.SetUserMatrix(id0); updateTextLoc(); } @Override public void SetUserTransform(vtkLinearTransform id0) { super.SetUserTransform(id0); updateTextLoc(); } @Override public void Delete() { ren.RemoveActor(xactor); ren.RemoveActor(yactor); ren.RemoveActor(zactor); ren.RemoveActor(tubeActor); ren.RemoveActor(xconeActor); ren.RemoveActor(yconeActor); ren.RemoveActor(xconeActor); xactor.Delete(); yactor.Delete(); zactor.Delete(); tubeActor.Delete(); xconeActor.Delete(); yconeActor.Delete(); zconeActor.Delete(); super.Delete(); } public void dispose() { ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { @Override public void run() { removeFromRenderer(); Delete(); } }); } @Override public vtkProp3D getVtkProp() { return this; } }