From: Marko Luukkainen Date: Mon, 6 May 2019 10:16:16 +0000 (+0300) Subject: vtk 8.2.0 API changes X-Git-Tag: v1.43.0~265 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F78%2F2878%2F1;p=simantics%2F3d.git vtk 8.2.0 API changes gitlab #2 Change-Id: Ie4041b690cae32cd4f0615233cf03edc013282fc --- diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/gizmo/TranslateGizmo.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/gizmo/TranslateGizmo.java index c747e0b9..49abd1f2 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/gizmo/TranslateGizmo.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/gizmo/TranslateGizmo.java @@ -1,383 +1,383 @@ -/******************************************************************************* - * 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.ArrayList; -import java.util.Collection; -import java.util.List; - -import vtk.vtkActor; -import vtk.vtkDataSetMapper; -import vtk.vtkPoints; -import vtk.vtkPolyData; -import vtk.vtkProp; -import vtk.vtkTriangle; - -public class TranslateGizmo extends vtkGizmo { - - - - private List parts; - - @Override - public Collection getGizmo() { - if (parts == null) { - parts = new ArrayList(); - - - float size = 2.f; - float sizeD2 = 1.f; - float offset = 0.2f; - - double[] colorX = new double[]{0.5,0.0,0.0}; - double[] colorY = new double[]{0.0,0.5,0.0}; - double[] colorZ = new double[]{0.0,0.0,0.5}; - - double[] colorXY = add(colorX, colorY); - double[] colorXZ = add(colorX, colorZ); - double[] colorYZ = add(colorY, colorZ); - double[] colorP = add(colorX,colorY,colorZ); - - vtkActor actorX = new vtkActor(); - vtkActor actorY = new vtkActor(); - vtkActor actorZ = new vtkActor(); - vtkActor actorXY = new vtkActor(); - vtkActor actorXZ = new vtkActor(); - vtkActor actorYZ = new vtkActor(); - vtkActor actorP = new vtkActor(); - - actorX.GetProperty().SetColor(colorX); - actorY.GetProperty().SetColor(colorY); - actorZ.GetProperty().SetColor(colorZ); - actorXY.GetProperty().SetColor(colorXY); - actorXZ.GetProperty().SetColor(colorXZ); - actorYZ.GetProperty().SetColor(colorYZ); - actorP.GetProperty().SetColor(colorP); - - actorX.GetProperty().SetOpacity(0.5); - actorY.GetProperty().SetOpacity(0.5); - actorZ.GetProperty().SetOpacity(0.5); - actorXY.GetProperty().SetOpacity(0.5); - actorXZ.GetProperty().SetOpacity(0.5); - actorYZ.GetProperty().SetOpacity(0.5); - actorP.GetProperty().SetOpacity(0.5); - - actorX.GetProperty().BackfaceCullingOff(); - actorY.GetProperty().BackfaceCullingOff(); - actorZ.GetProperty().BackfaceCullingOff(); - actorXY.GetProperty().BackfaceCullingOff(); - actorXZ.GetProperty().BackfaceCullingOff(); - actorYZ.GetProperty().BackfaceCullingOff(); - actorP.GetProperty().BackfaceCullingOff(); - - actorX.SetPickable(1); - actorY.SetPickable(1); - actorZ.SetPickable(1); - actorXY.SetPickable(1); - actorXZ.SetPickable(1); - actorYZ.SetPickable(1); - actorP.SetPickable(1); - - - actorX.GetProperty().LightingOff(); - actorY.GetProperty().LightingOff(); - actorZ.GetProperty().LightingOff(); - actorXY.GetProperty().LightingOff(); - actorXZ.GetProperty().LightingOff(); - actorYZ.GetProperty().LightingOff(); - actorP.GetProperty().LightingOff(); - - - vtkTriangle triangle = new vtkTriangle(); - - // X - vtkPolyData actorXData = new vtkPolyData(); - actorXData.Allocate(6, 6); - - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 1); - triangle.GetPointIds().SetId(2, 3); - actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 1); - triangle.GetPointIds().SetId(1, 2); - triangle.GetPointIds().SetId(2, 3); - actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 3); - triangle.GetPointIds().SetId(2, 5); - actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 3); - triangle.GetPointIds().SetId(1, 4); - triangle.GetPointIds().SetId(2, 5); - actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - - vtkPoints partPoints = new vtkPoints(); - partPoints.InsertPoint(0, new double[]{size,0.,0.}); - partPoints.InsertPoint(1, new double[]{size-offset,offset,0.}); - partPoints.InsertPoint(2, new double[]{sizeD2-offset,offset,0.}); - partPoints.InsertPoint(3, new double[]{sizeD2,0.,0.}); - partPoints.InsertPoint(4, new double[]{sizeD2-offset,0.,offset}); - partPoints.InsertPoint(5, new double[]{size-offset,0.,offset}); - actorXData.SetPoints(partPoints); - partPoints.Delete(); - - // Y - vtkPolyData actorYData = new vtkPolyData(); - actorYData.Allocate(6, 6); - - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 1); - triangle.GetPointIds().SetId(2, 3); - actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 1); - triangle.GetPointIds().SetId(1, 2); - triangle.GetPointIds().SetId(2, 3); - actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 3); - triangle.GetPointIds().SetId(2, 5); - actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 3); - triangle.GetPointIds().SetId(1, 4); - triangle.GetPointIds().SetId(2, 5); - actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - - partPoints = new vtkPoints(); - partPoints.InsertPoint(0, new double[]{0., size, 0.}); - partPoints.InsertPoint(1, new double[]{offset, size - offset, 0.}); - partPoints.InsertPoint(2, new double[]{offset, sizeD2 - offset, 0.}); - partPoints.InsertPoint(3, new double[]{0., sizeD2, 0.}); - partPoints.InsertPoint(4, new double[]{0., sizeD2 - offset, offset}); - partPoints.InsertPoint(5, new double[]{0., size - offset, offset}); - actorYData.SetPoints(partPoints); - partPoints.Delete(); - - // Z - vtkPolyData actorZData = new vtkPolyData(); - actorZData.Allocate(6, 6); - - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 1); - triangle.GetPointIds().SetId(2, 3); - actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 1); - triangle.GetPointIds().SetId(1, 2); - triangle.GetPointIds().SetId(2, 3); - actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 3); - triangle.GetPointIds().SetId(2, 5); - actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 3); - triangle.GetPointIds().SetId(1, 4); - triangle.GetPointIds().SetId(2, 5); - actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - - partPoints = new vtkPoints(); - partPoints.InsertPoint(0, new double[]{0., 0.,size}); - partPoints.InsertPoint(1, new double[]{offset, 0., size - offset}); - partPoints.InsertPoint(2, new double[]{offset, 0., sizeD2 - offset}); - partPoints.InsertPoint(3, new double[]{0., 0., sizeD2}); - partPoints.InsertPoint(4, new double[]{0., offset, sizeD2 - offset}); - partPoints.InsertPoint(5, new double[]{0., offset, size - offset}); - actorZData.SetPoints(partPoints); - partPoints.Delete(); - - // XY - vtkPolyData actorXYData = new vtkPolyData(); - actorXYData.Allocate(4, 4); - - triangle.GetPointIds().SetId(0, 2); - triangle.GetPointIds().SetId(1, 1); - triangle.GetPointIds().SetId(2, 3); - actorXYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 1); - triangle.GetPointIds().SetId(2, 3); - actorXYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - - partPoints = new vtkPoints(); - partPoints.InsertPoint(0, new double[]{offset, size-offset, 0.f}); - partPoints.InsertPoint(1, new double[]{offset, sizeD2 - offset, 0.f}); - partPoints.InsertPoint(2, new double[]{sizeD2 - offset, offset, 0.f}); - partPoints.InsertPoint(3, new double[]{size-offset, offset, 0.f}); - actorXYData.SetPoints(partPoints); - partPoints.Delete(); - - // XZ - vtkPolyData actorXZData = new vtkPolyData(); - actorXZData.Allocate(4, 4); - - triangle.GetPointIds().SetId(0, 2); - triangle.GetPointIds().SetId(1, 1); - triangle.GetPointIds().SetId(2, 3); - actorXZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 1); - triangle.GetPointIds().SetId(2, 3); - actorXZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - - partPoints = new vtkPoints(); - partPoints.InsertPoint(0, new double[]{offset, 0.f, size-offset}); - partPoints.InsertPoint(1, new double[]{offset, 0.f, sizeD2 - offset}); - partPoints.InsertPoint(2, new double[]{sizeD2 - offset, 0.f, offset}); - partPoints.InsertPoint(3, new double[]{size-offset, 0.f, offset}); - actorXZData.SetPoints(partPoints); - partPoints.Delete(); - - // YZ - vtkPolyData actorYZData = new vtkPolyData(); - actorYZData.Allocate(4, 4); - - triangle.GetPointIds().SetId(0, 2); - triangle.GetPointIds().SetId(1, 1); - triangle.GetPointIds().SetId(2, 3); - actorYZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 1); - triangle.GetPointIds().SetId(2, 3); - actorYZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - - partPoints = new vtkPoints(); - partPoints.InsertPoint(0, new double[]{0.f,offset, size-offset}); - partPoints.InsertPoint(1, new double[]{0.f,offset, sizeD2 - offset}); - partPoints.InsertPoint(2, new double[]{0.f,sizeD2 - offset, offset}); - partPoints.InsertPoint(3, new double[]{0.f,size-offset, offset}); - - actorYZData.SetPoints(partPoints); - partPoints.Delete(); - - vtkPolyData actorPData = new vtkPolyData(); - actorPData.Allocate(10, 10); - - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 1); - triangle.GetPointIds().SetId(2, 2); - actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 2); - triangle.GetPointIds().SetId(2, 3); - actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 3); - triangle.GetPointIds().SetId(2, 4); - actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 4); - triangle.GetPointIds().SetId(2, 5); - actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 5); - triangle.GetPointIds().SetId(2, 6); - actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 6); - triangle.GetPointIds().SetId(2, 7); - actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 7); - triangle.GetPointIds().SetId(2, 8); - actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 8); - triangle.GetPointIds().SetId(2, 9); - actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - triangle.GetPointIds().SetId(0, 0); - triangle.GetPointIds().SetId(1, 9); - triangle.GetPointIds().SetId(2, 1); - actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); - - partPoints = new vtkPoints(); - partPoints.InsertPoint(0, new double[]{0.f, 0.f, 0.f}); - partPoints.InsertPoint(1, new double[]{sizeD2, 0.f, 0.f}); - partPoints.InsertPoint(2, new double[]{sizeD2 - offset, offset, 0.f}); - partPoints.InsertPoint(3, new double[]{offset, sizeD2 - offset, 0.f}); - partPoints.InsertPoint(4, new double[]{0.f, sizeD2, 0.f}); - partPoints.InsertPoint(5, new double[]{0.f, sizeD2 - offset, offset}); - partPoints.InsertPoint(6, new double[]{0.f, offset, sizeD2-offset}); - partPoints.InsertPoint(7, new double[]{0.f, 0.f, sizeD2}); - partPoints.InsertPoint(8, new double[]{offset, 0.f, sizeD2-offset}); - partPoints.InsertPoint(9, new double[]{sizeD2-offset, 0.f, offset}); - actorPData.SetPoints(partPoints); - partPoints.Delete(); - - - vtkDataSetMapper partMapper = new vtkDataSetMapper(); - partMapper.SetInput(actorXData); - partMapper.ScalarVisibilityOn(); - actorX.SetMapper(partMapper); - partMapper.Delete(); - - partMapper = new vtkDataSetMapper(); - partMapper.SetInput(actorYData); - partMapper.ScalarVisibilityOn(); - actorY.SetMapper(partMapper); - partMapper.Delete(); - - partMapper = new vtkDataSetMapper(); - partMapper.SetInput(actorZData); - partMapper.ScalarVisibilityOn(); - actorZ.SetMapper(partMapper); - partMapper.Delete(); - - partMapper = new vtkDataSetMapper(); - partMapper.SetInput(actorXYData); - partMapper.ScalarVisibilityOn(); - actorXY.SetMapper(partMapper); - partMapper.Delete(); - - partMapper = new vtkDataSetMapper(); - partMapper.SetInput(actorXZData); - partMapper.ScalarVisibilityOn(); - actorXZ.SetMapper(partMapper); - partMapper.Delete(); - - partMapper = new vtkDataSetMapper(); - partMapper.SetInput(actorYZData); - partMapper.ScalarVisibilityOn(); - actorYZ.SetMapper(partMapper); - partMapper.Delete(); - - partMapper = new vtkDataSetMapper(); - partMapper.SetInput(actorPData); - partMapper.ScalarVisibilityOn(); - actorP.SetMapper(partMapper); - partMapper.Delete(); - - - actorXData.Delete(); - actorYData.Delete(); - actorZData.Delete(); - actorXYData.Delete(); - actorXZData.Delete(); - actorYZData.Delete(); - actorPData.Delete(); - - parts.add(actorX); - parts.add(actorY); - parts.add(actorZ); - parts.add(actorXY); - parts.add(actorXZ); - parts.add(actorYZ); - parts.add(actorP); - } - return parts; - } - - public int getTranslateAxis(vtkActor actor) { - if (actor == null) - return -1; - return parts.indexOf(actor); - } - - -} +/******************************************************************************* + * 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.ArrayList; +import java.util.Collection; +import java.util.List; + +import vtk.vtkActor; +import vtk.vtkDataSetMapper; +import vtk.vtkPoints; +import vtk.vtkPolyData; +import vtk.vtkProp; +import vtk.vtkTriangle; + +public class TranslateGizmo extends vtkGizmo { + + + + private List parts; + + @Override + public Collection getGizmo() { + if (parts == null) { + parts = new ArrayList(); + + + float size = 2.f; + float sizeD2 = 1.f; + float offset = 0.2f; + + double[] colorX = new double[]{0.5,0.0,0.0}; + double[] colorY = new double[]{0.0,0.5,0.0}; + double[] colorZ = new double[]{0.0,0.0,0.5}; + + double[] colorXY = add(colorX, colorY); + double[] colorXZ = add(colorX, colorZ); + double[] colorYZ = add(colorY, colorZ); + double[] colorP = add(colorX,colorY,colorZ); + + vtkActor actorX = new vtkActor(); + vtkActor actorY = new vtkActor(); + vtkActor actorZ = new vtkActor(); + vtkActor actorXY = new vtkActor(); + vtkActor actorXZ = new vtkActor(); + vtkActor actorYZ = new vtkActor(); + vtkActor actorP = new vtkActor(); + + actorX.GetProperty().SetColor(colorX); + actorY.GetProperty().SetColor(colorY); + actorZ.GetProperty().SetColor(colorZ); + actorXY.GetProperty().SetColor(colorXY); + actorXZ.GetProperty().SetColor(colorXZ); + actorYZ.GetProperty().SetColor(colorYZ); + actorP.GetProperty().SetColor(colorP); + + actorX.GetProperty().SetOpacity(0.5); + actorY.GetProperty().SetOpacity(0.5); + actorZ.GetProperty().SetOpacity(0.5); + actorXY.GetProperty().SetOpacity(0.5); + actorXZ.GetProperty().SetOpacity(0.5); + actorYZ.GetProperty().SetOpacity(0.5); + actorP.GetProperty().SetOpacity(0.5); + + actorX.GetProperty().BackfaceCullingOff(); + actorY.GetProperty().BackfaceCullingOff(); + actorZ.GetProperty().BackfaceCullingOff(); + actorXY.GetProperty().BackfaceCullingOff(); + actorXZ.GetProperty().BackfaceCullingOff(); + actorYZ.GetProperty().BackfaceCullingOff(); + actorP.GetProperty().BackfaceCullingOff(); + + actorX.SetPickable(1); + actorY.SetPickable(1); + actorZ.SetPickable(1); + actorXY.SetPickable(1); + actorXZ.SetPickable(1); + actorYZ.SetPickable(1); + actorP.SetPickable(1); + + + actorX.GetProperty().LightingOff(); + actorY.GetProperty().LightingOff(); + actorZ.GetProperty().LightingOff(); + actorXY.GetProperty().LightingOff(); + actorXZ.GetProperty().LightingOff(); + actorYZ.GetProperty().LightingOff(); + actorP.GetProperty().LightingOff(); + + + vtkTriangle triangle = new vtkTriangle(); + + // X + vtkPolyData actorXData = new vtkPolyData(); + actorXData.Allocate(6, 6); + + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 1); + triangle.GetPointIds().SetId(2, 3); + actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 1); + triangle.GetPointIds().SetId(1, 2); + triangle.GetPointIds().SetId(2, 3); + actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 3); + triangle.GetPointIds().SetId(2, 5); + actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 3); + triangle.GetPointIds().SetId(1, 4); + triangle.GetPointIds().SetId(2, 5); + actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + + vtkPoints partPoints = new vtkPoints(); + partPoints.InsertPoint(0, new double[]{size,0.,0.}); + partPoints.InsertPoint(1, new double[]{size-offset,offset,0.}); + partPoints.InsertPoint(2, new double[]{sizeD2-offset,offset,0.}); + partPoints.InsertPoint(3, new double[]{sizeD2,0.,0.}); + partPoints.InsertPoint(4, new double[]{sizeD2-offset,0.,offset}); + partPoints.InsertPoint(5, new double[]{size-offset,0.,offset}); + actorXData.SetPoints(partPoints); + partPoints.Delete(); + + // Y + vtkPolyData actorYData = new vtkPolyData(); + actorYData.Allocate(6, 6); + + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 1); + triangle.GetPointIds().SetId(2, 3); + actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 1); + triangle.GetPointIds().SetId(1, 2); + triangle.GetPointIds().SetId(2, 3); + actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 3); + triangle.GetPointIds().SetId(2, 5); + actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 3); + triangle.GetPointIds().SetId(1, 4); + triangle.GetPointIds().SetId(2, 5); + actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + + partPoints = new vtkPoints(); + partPoints.InsertPoint(0, new double[]{0., size, 0.}); + partPoints.InsertPoint(1, new double[]{offset, size - offset, 0.}); + partPoints.InsertPoint(2, new double[]{offset, sizeD2 - offset, 0.}); + partPoints.InsertPoint(3, new double[]{0., sizeD2, 0.}); + partPoints.InsertPoint(4, new double[]{0., sizeD2 - offset, offset}); + partPoints.InsertPoint(5, new double[]{0., size - offset, offset}); + actorYData.SetPoints(partPoints); + partPoints.Delete(); + + // Z + vtkPolyData actorZData = new vtkPolyData(); + actorZData.Allocate(6, 6); + + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 1); + triangle.GetPointIds().SetId(2, 3); + actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 1); + triangle.GetPointIds().SetId(1, 2); + triangle.GetPointIds().SetId(2, 3); + actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 3); + triangle.GetPointIds().SetId(2, 5); + actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 3); + triangle.GetPointIds().SetId(1, 4); + triangle.GetPointIds().SetId(2, 5); + actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + + partPoints = new vtkPoints(); + partPoints.InsertPoint(0, new double[]{0., 0.,size}); + partPoints.InsertPoint(1, new double[]{offset, 0., size - offset}); + partPoints.InsertPoint(2, new double[]{offset, 0., sizeD2 - offset}); + partPoints.InsertPoint(3, new double[]{0., 0., sizeD2}); + partPoints.InsertPoint(4, new double[]{0., offset, sizeD2 - offset}); + partPoints.InsertPoint(5, new double[]{0., offset, size - offset}); + actorZData.SetPoints(partPoints); + partPoints.Delete(); + + // XY + vtkPolyData actorXYData = new vtkPolyData(); + actorXYData.Allocate(4, 4); + + triangle.GetPointIds().SetId(0, 2); + triangle.GetPointIds().SetId(1, 1); + triangle.GetPointIds().SetId(2, 3); + actorXYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 1); + triangle.GetPointIds().SetId(2, 3); + actorXYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + + partPoints = new vtkPoints(); + partPoints.InsertPoint(0, new double[]{offset, size-offset, 0.f}); + partPoints.InsertPoint(1, new double[]{offset, sizeD2 - offset, 0.f}); + partPoints.InsertPoint(2, new double[]{sizeD2 - offset, offset, 0.f}); + partPoints.InsertPoint(3, new double[]{size-offset, offset, 0.f}); + actorXYData.SetPoints(partPoints); + partPoints.Delete(); + + // XZ + vtkPolyData actorXZData = new vtkPolyData(); + actorXZData.Allocate(4, 4); + + triangle.GetPointIds().SetId(0, 2); + triangle.GetPointIds().SetId(1, 1); + triangle.GetPointIds().SetId(2, 3); + actorXZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 1); + triangle.GetPointIds().SetId(2, 3); + actorXZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + + partPoints = new vtkPoints(); + partPoints.InsertPoint(0, new double[]{offset, 0.f, size-offset}); + partPoints.InsertPoint(1, new double[]{offset, 0.f, sizeD2 - offset}); + partPoints.InsertPoint(2, new double[]{sizeD2 - offset, 0.f, offset}); + partPoints.InsertPoint(3, new double[]{size-offset, 0.f, offset}); + actorXZData.SetPoints(partPoints); + partPoints.Delete(); + + // YZ + vtkPolyData actorYZData = new vtkPolyData(); + actorYZData.Allocate(4, 4); + + triangle.GetPointIds().SetId(0, 2); + triangle.GetPointIds().SetId(1, 1); + triangle.GetPointIds().SetId(2, 3); + actorYZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 1); + triangle.GetPointIds().SetId(2, 3); + actorYZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + + partPoints = new vtkPoints(); + partPoints.InsertPoint(0, new double[]{0.f,offset, size-offset}); + partPoints.InsertPoint(1, new double[]{0.f,offset, sizeD2 - offset}); + partPoints.InsertPoint(2, new double[]{0.f,sizeD2 - offset, offset}); + partPoints.InsertPoint(3, new double[]{0.f,size-offset, offset}); + + actorYZData.SetPoints(partPoints); + partPoints.Delete(); + + vtkPolyData actorPData = new vtkPolyData(); + actorPData.Allocate(10, 10); + + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 1); + triangle.GetPointIds().SetId(2, 2); + actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 2); + triangle.GetPointIds().SetId(2, 3); + actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 3); + triangle.GetPointIds().SetId(2, 4); + actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 4); + triangle.GetPointIds().SetId(2, 5); + actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 5); + triangle.GetPointIds().SetId(2, 6); + actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 6); + triangle.GetPointIds().SetId(2, 7); + actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 7); + triangle.GetPointIds().SetId(2, 8); + actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 8); + triangle.GetPointIds().SetId(2, 9); + actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + triangle.GetPointIds().SetId(0, 0); + triangle.GetPointIds().SetId(1, 9); + triangle.GetPointIds().SetId(2, 1); + actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); + + partPoints = new vtkPoints(); + partPoints.InsertPoint(0, new double[]{0.f, 0.f, 0.f}); + partPoints.InsertPoint(1, new double[]{sizeD2, 0.f, 0.f}); + partPoints.InsertPoint(2, new double[]{sizeD2 - offset, offset, 0.f}); + partPoints.InsertPoint(3, new double[]{offset, sizeD2 - offset, 0.f}); + partPoints.InsertPoint(4, new double[]{0.f, sizeD2, 0.f}); + partPoints.InsertPoint(5, new double[]{0.f, sizeD2 - offset, offset}); + partPoints.InsertPoint(6, new double[]{0.f, offset, sizeD2-offset}); + partPoints.InsertPoint(7, new double[]{0.f, 0.f, sizeD2}); + partPoints.InsertPoint(8, new double[]{offset, 0.f, sizeD2-offset}); + partPoints.InsertPoint(9, new double[]{sizeD2-offset, 0.f, offset}); + actorPData.SetPoints(partPoints); + partPoints.Delete(); + + + vtkDataSetMapper partMapper = new vtkDataSetMapper(); + partMapper.SetInputData(actorXData); + partMapper.ScalarVisibilityOn(); + actorX.SetMapper(partMapper); + partMapper.Delete(); + + partMapper = new vtkDataSetMapper(); + partMapper.SetInputData(actorYData); + partMapper.ScalarVisibilityOn(); + actorY.SetMapper(partMapper); + partMapper.Delete(); + + partMapper = new vtkDataSetMapper(); + partMapper.SetInputData(actorZData); + partMapper.ScalarVisibilityOn(); + actorZ.SetMapper(partMapper); + partMapper.Delete(); + + partMapper = new vtkDataSetMapper(); + partMapper.SetInputData(actorXYData); + partMapper.ScalarVisibilityOn(); + actorXY.SetMapper(partMapper); + partMapper.Delete(); + + partMapper = new vtkDataSetMapper(); + partMapper.SetInputData(actorXZData); + partMapper.ScalarVisibilityOn(); + actorXZ.SetMapper(partMapper); + partMapper.Delete(); + + partMapper = new vtkDataSetMapper(); + partMapper.SetInputData(actorYZData); + partMapper.ScalarVisibilityOn(); + actorYZ.SetMapper(partMapper); + partMapper.Delete(); + + partMapper = new vtkDataSetMapper(); + partMapper.SetInputData(actorPData); + partMapper.ScalarVisibilityOn(); + actorP.SetMapper(partMapper); + partMapper.Delete(); + + + actorXData.Delete(); + actorYData.Delete(); + actorZData.Delete(); + actorXYData.Delete(); + actorXZData.Delete(); + actorYZData.Delete(); + actorPData.Delete(); + + parts.add(actorX); + parts.add(actorY); + parts.add(actorZ); + parts.add(actorXY); + parts.add(actorXZ); + parts.add(actorYZ); + parts.add(actorP); + } + return parts; + } + + public int getTranslateAxis(vtkActor actor) { + if (actor == null) + return -1; + return parts.indexOf(actor); + } + + +} diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/DualHeadArrowActor.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/DualHeadArrowActor.java index 25274e83..2df9ffb0 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/DualHeadArrowActor.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/DualHeadArrowActor.java @@ -1,308 +1,308 @@ -/******************************************************************************* - * 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.AxisAngle4d; -import javax.vecmath.Matrix4d; -import javax.vecmath.Point3d; -import javax.vecmath.Vector3d; - -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.vtkConeSource; -import vtk.vtkLineSource; -import vtk.vtkLinearTransform; -import vtk.vtkMatrix4x4; -import vtk.vtkPolyDataMapper; -import vtk.vtkProp3D; -import vtk.vtkRenderer; -import vtk.vtkTextActor; -import vtk.vtkTubeFilter; - -public class DualHeadArrowActor extends vtkAssembly implements IvtkVisualObject{ - - private vtkRenderer ren; - private Vector3d axisDir = new Vector3d(1,0,0); - private vtkTextActor tactor; - private vtkActor tubeActor; - private vtkActor coneActor; - private vtkActor coneActor2; - private boolean rendered = false; - - public DualHeadArrowActor(vtkRenderer _ren, String label) { - super(); - ren = _ren; - createAxis(label); - } - - public DualHeadArrowActor(vtkRenderer _ren, String label, Vector3d dir) { - super(); - ren = _ren; - this.axisDir = dir; - if (dir.lengthSquared() < MathTools.NEAR_ZERO) - throw new IllegalArgumentException("Direction vector length must mer larger than zero"); - createAxis(label); - } - - public void createAxis(String label) { - double coneScale = 0.3 * axisDir.length(); - Vector3d coneOffset = new Vector3d(axisDir); - coneOffset.normalize(); - coneOffset.scale(coneScale*0.5); - - vtkLineSource line = new vtkLineSource(); - //line.SetPoint1(0.0,0.0,0.0); - line.SetPoint1(coneOffset.x,coneOffset.y,coneOffset.z); - line.SetPoint2(axisDir.x-coneOffset.x,axisDir.y-coneOffset.y,axisDir.z-coneOffset.z); - - tactor = new vtkTextActor(); - - tactor.SetInput(label); - - tactor.SetTextScaleModeToNone(); - tactor.GetTextProperty().SetColor(0.0, 0.0, 0.0); - tactor.GetTextProperty().ShadowOff(); - tactor.GetTextProperty().ItalicOff(); - tactor.GetTextProperty().BoldOff(); - - tactor.GetTextProperty().Delete(); - - tactor.SetMaximumLineHeight(0.25); - - tactor.SetPickable(0); - - vtkTubeFilter tube = new vtkTubeFilter(); - tube.SetInput(line.GetOutput()); - tube.SetRadius(0.05 * axisDir.length()); - tube.SetNumberOfSides(8); - - vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper(); - tubeMapper.SetInput(tube.GetOutput()); - - tubeActor = new vtkActor(); - tubeActor.SetMapper(tubeMapper); - tubeActor.PickableOff(); - - int coneRes = 12; - - - - - // --- x-Cone - vtkConeSource cone = new vtkConeSource(); - cone.SetResolution(coneRes); - vtkPolyDataMapper coneMapper = new vtkPolyDataMapper(); - coneMapper.SetInput(cone.GetOutput()); - coneActor = new vtkActor(); - coneActor.SetMapper(coneMapper); - coneActor.GetProperty().SetColor(1, 0, 0); - coneActor.SetScale(coneScale, coneScale, coneScale); - coneActor.SetPosition(axisDir.x-coneOffset.x,axisDir.y-coneOffset.y,axisDir.z-coneOffset.z); - coneActor.SetPickable(0); - - AxisAngle4d aa = MathTools.createRotation(new Vector3d(1,0,0), axisDir); - if (aa != null) - coneActor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); - - - coneActor2 = new vtkActor(); - coneActor2.SetMapper(coneMapper); - coneActor2.GetProperty().SetColor(1, 0, 0); - coneActor2.SetScale(coneScale, coneScale, coneScale); - coneActor2.SetPosition(coneOffset.x,coneOffset.y,coneOffset.z); - coneActor2.SetPickable(0); - - aa = MathTools.createRotation(new Vector3d(-1,0,0), axisDir); - if (aa != null) - coneActor2.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); - - this.AddPart(tubeActor); - this.AddPart(coneActor); - this.AddPart(coneActor2); - - tube.GetOutput().Delete(); - cone.GetOutput().Delete(); - line.GetOutput().Delete(); - - tubeMapper.Delete(); - tube.Delete(); - cone.Delete(); - line.Delete(); - - coneMapper.Delete(); - - coneActor.GetProperty().Delete(); - coneActor2.GetProperty().Delete(); - } - - public void addToRenderer() { - if (rendered) - return; - rendered = true; - - ren.AddActor2D(tactor); - - ren.AddActor(this); - } - - public void removeFromRenderer() { - if (!rendered) - return; - rendered = false; - ren.RemoveActor2D(tactor); - ren.RemoveActor(this); - } - - public boolean isRendered() { - return rendered; - } - - public void setAxesVisibility(boolean ison) { - this.SetVisibility(ison ? 1 : 0); - tactor.SetVisibility(ison ? 1 : 0); - } - - public void setLabelVisibility(boolean ison) { - tactor.SetVisibility(ison ? 1 : 0); - } - - double mat[] = new double[16]; - Matrix4d m = new Matrix4d(); - private void updateTextLoc() { - tactor.GetPositionCoordinate().SetCoordinateSystemToWorld(); - - - GetMatrix(mat); - MathTools.set(m,mat); - Point3d p = new Point3d(axisDir.x*0.5, axisDir.y*0.5,axisDir.z*0.5); - m.transform(p); - tactor.GetPositionCoordinate().SetValue(p.x, p.y, p.z); - - tactor.GetPositionCoordinate().Delete(); - } - - @Override - public void SetPickable(int id0) { - super.SetPickable(id0); - tubeActor.SetPickable(id0); - coneActor.SetPickable(id0); - coneActor2.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(); - } - - public void SetColor(double r, double g, double b) { - coneActor.GetProperty().SetColor(r, g, b); - coneActor2.GetProperty().SetColor(r, g, b); - tubeActor.GetProperty().SetColor(r, g, b); - coneActor.GetProperty().Delete(); - coneActor2.GetProperty().Delete(); - tubeActor.GetProperty().Delete(); - } - - public void SetTextColor(double r, double g, double b) { - tactor.GetTextProperty().SetColor(r, g, b); - tactor.GetTextProperty().Delete(); - } - - @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(tactor); - ren.RemoveActor(tubeActor); - ren.RemoveActor(coneActor); - ren.RemoveActor(coneActor2); - tactor.Delete(); - tubeActor.Delete(); - coneActor.Delete(); - coneActor2.Delete(); - super.Delete(); - } - - public void dispose() { - ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { - - @Override - public void run() { - removeFromRenderer(); - Delete(); - } - }); - } - - @Override - public vtkProp3D getVtkProp() { - return this; - } - -} +/******************************************************************************* + * 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.AxisAngle4d; +import javax.vecmath.Matrix4d; +import javax.vecmath.Point3d; +import javax.vecmath.Vector3d; + +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.vtkConeSource; +import vtk.vtkLineSource; +import vtk.vtkLinearTransform; +import vtk.vtkMatrix4x4; +import vtk.vtkPolyDataMapper; +import vtk.vtkProp3D; +import vtk.vtkRenderer; +import vtk.vtkTextActor; +import vtk.vtkTubeFilter; + +public class DualHeadArrowActor extends vtkAssembly implements IvtkVisualObject{ + + private vtkRenderer ren; + private Vector3d axisDir = new Vector3d(1,0,0); + private vtkTextActor tactor; + private vtkActor tubeActor; + private vtkActor coneActor; + private vtkActor coneActor2; + private boolean rendered = false; + + public DualHeadArrowActor(vtkRenderer _ren, String label) { + super(); + ren = _ren; + createAxis(label); + } + + public DualHeadArrowActor(vtkRenderer _ren, String label, Vector3d dir) { + super(); + ren = _ren; + this.axisDir = dir; + if (dir.lengthSquared() < MathTools.NEAR_ZERO) + throw new IllegalArgumentException("Direction vector length must mer larger than zero"); + createAxis(label); + } + + public void createAxis(String label) { + double coneScale = 0.3 * axisDir.length(); + Vector3d coneOffset = new Vector3d(axisDir); + coneOffset.normalize(); + coneOffset.scale(coneScale*0.5); + + vtkLineSource line = new vtkLineSource(); + //line.SetPoint1(0.0,0.0,0.0); + line.SetPoint1(coneOffset.x,coneOffset.y,coneOffset.z); + line.SetPoint2(axisDir.x-coneOffset.x,axisDir.y-coneOffset.y,axisDir.z-coneOffset.z); + + tactor = new vtkTextActor(); + + tactor.SetInput(label); + + tactor.SetTextScaleModeToNone(); + tactor.GetTextProperty().SetColor(0.0, 0.0, 0.0); + tactor.GetTextProperty().ShadowOff(); + tactor.GetTextProperty().ItalicOff(); + tactor.GetTextProperty().BoldOff(); + + tactor.GetTextProperty().Delete(); + + tactor.SetMaximumLineHeight(0.25); + + tactor.SetPickable(0); + + vtkTubeFilter tube = new vtkTubeFilter(); + tube.SetInputData(line.GetOutput()); + tube.SetRadius(0.05 * axisDir.length()); + tube.SetNumberOfSides(8); + + vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper(); + tubeMapper.SetInputData(tube.GetOutput()); + + tubeActor = new vtkActor(); + tubeActor.SetMapper(tubeMapper); + tubeActor.PickableOff(); + + int coneRes = 12; + + + + + // --- x-Cone + vtkConeSource cone = new vtkConeSource(); + cone.SetResolution(coneRes); + vtkPolyDataMapper coneMapper = new vtkPolyDataMapper(); + coneMapper.SetInputData(cone.GetOutput()); + coneActor = new vtkActor(); + coneActor.SetMapper(coneMapper); + coneActor.GetProperty().SetColor(1, 0, 0); + coneActor.SetScale(coneScale, coneScale, coneScale); + coneActor.SetPosition(axisDir.x-coneOffset.x,axisDir.y-coneOffset.y,axisDir.z-coneOffset.z); + coneActor.SetPickable(0); + + AxisAngle4d aa = MathTools.createRotation(new Vector3d(1,0,0), axisDir); + if (aa != null) + coneActor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + + + coneActor2 = new vtkActor(); + coneActor2.SetMapper(coneMapper); + coneActor2.GetProperty().SetColor(1, 0, 0); + coneActor2.SetScale(coneScale, coneScale, coneScale); + coneActor2.SetPosition(coneOffset.x,coneOffset.y,coneOffset.z); + coneActor2.SetPickable(0); + + aa = MathTools.createRotation(new Vector3d(-1,0,0), axisDir); + if (aa != null) + coneActor2.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + + this.AddPart(tubeActor); + this.AddPart(coneActor); + this.AddPart(coneActor2); + + tube.GetOutput().Delete(); + cone.GetOutput().Delete(); + line.GetOutput().Delete(); + + tubeMapper.Delete(); + tube.Delete(); + cone.Delete(); + line.Delete(); + + coneMapper.Delete(); + + coneActor.GetProperty().Delete(); + coneActor2.GetProperty().Delete(); + } + + public void addToRenderer() { + if (rendered) + return; + rendered = true; + + ren.AddActor2D(tactor); + + ren.AddActor(this); + } + + public void removeFromRenderer() { + if (!rendered) + return; + rendered = false; + ren.RemoveActor2D(tactor); + ren.RemoveActor(this); + } + + public boolean isRendered() { + return rendered; + } + + public void setAxesVisibility(boolean ison) { + this.SetVisibility(ison ? 1 : 0); + tactor.SetVisibility(ison ? 1 : 0); + } + + public void setLabelVisibility(boolean ison) { + tactor.SetVisibility(ison ? 1 : 0); + } + + double mat[] = new double[16]; + Matrix4d m = new Matrix4d(); + private void updateTextLoc() { + tactor.GetPositionCoordinate().SetCoordinateSystemToWorld(); + + + GetMatrix(mat); + MathTools.set(m,mat); + Point3d p = new Point3d(axisDir.x*0.5, axisDir.y*0.5,axisDir.z*0.5); + m.transform(p); + tactor.GetPositionCoordinate().SetValue(p.x, p.y, p.z); + + tactor.GetPositionCoordinate().Delete(); + } + + @Override + public void SetPickable(int id0) { + super.SetPickable(id0); + tubeActor.SetPickable(id0); + coneActor.SetPickable(id0); + coneActor2.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(); + } + + public void SetColor(double r, double g, double b) { + coneActor.GetProperty().SetColor(r, g, b); + coneActor2.GetProperty().SetColor(r, g, b); + tubeActor.GetProperty().SetColor(r, g, b); + coneActor.GetProperty().Delete(); + coneActor2.GetProperty().Delete(); + tubeActor.GetProperty().Delete(); + } + + public void SetTextColor(double r, double g, double b) { + tactor.GetTextProperty().SetColor(r, g, b); + tactor.GetTextProperty().Delete(); + } + + @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(tactor); + ren.RemoveActor(tubeActor); + ren.RemoveActor(coneActor); + ren.RemoveActor(coneActor2); + tactor.Delete(); + tubeActor.Delete(); + coneActor.Delete(); + coneActor2.Delete(); + super.Delete(); + } + + public void dispose() { + ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { + + @Override + public void run() { + removeFromRenderer(); + Delete(); + } + }); + } + + @Override + public vtkProp3D getVtkProp() { + return this; + } + +} diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java index 0cfc8246..0466a02e 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java @@ -74,12 +74,12 @@ public class MeshActor extends vtkActor { boolean computeNormals = true; if (computeNormals) { vtkPolyDataNormals normals = new vtkPolyDataNormals(); - normals.SetInput(polyData); + normals.SetInputData(polyData); mapper.SetInputConnection(normals.GetOutputPort()); normals.GetOutputPort().Delete(); normals.Delete(); } else { - mapper.SetInput(polyData); + mapper.SetInputData(polyData); } if (mesh.getColors() != null) { diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axesActor.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axesActor.java index 1b1a6150..7bc9fbbb 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axesActor.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axesActor.java @@ -1,364 +1,364 @@ -/******************************************************************************* - * 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.vtkTextActor; -import vtk.vtkTubeFilter; - -public class axesActor 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 boolean rendered = false; - - public axesActor(vtkRenderer _ren) { - super(); - ren = _ren; - createAxes(); - } - - public axesActor(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); - - 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); - - vtkTubeFilter tube = new vtkTubeFilter(); - tube.SetInput(axes.GetOutput()); - tube.SetRadius(0.05 * axisLength); - tube.SetNumberOfSides(8); - - vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper(); - tubeMapper.SetInput(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.SetInput(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.SetInput(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.SetInput(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); - - - this.AddPart(tubeActor); - this.AddPart(xconeActor); - this.AddPart(yconeActor); - this.AddPart(zconeActor); - - tube.GetOutput().Delete(); - xcone.GetOutput().Delete(); - ycone.GetOutput().Delete(); - zcone.GetOutput().Delete(); - axes.GetOutput().Delete(); - axes.Delete(); - tubeMapper.Delete(); - tube.Delete(); - xcone.Delete(); - ycone.Delete(); - zcone.Delete(); - xconeMapper.Delete(); - yconeMapper.Delete(); - zconeMapper.Delete(); - - xconeActor.GetProperty().Delete(); - yconeActor.GetProperty().Delete(); - zconeActor.GetProperty().Delete(); - } - - 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); - - } - - @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; - } - -} +/******************************************************************************* + * 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.vtkTextActor; +import vtk.vtkTubeFilter; + +public class axesActor 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 boolean rendered = false; + + public axesActor(vtkRenderer _ren) { + super(); + ren = _ren; + createAxes(); + } + + public axesActor(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); + + 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); + + 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); + + + this.AddPart(tubeActor); + this.AddPart(xconeActor); + this.AddPart(yconeActor); + this.AddPart(zconeActor); + + tube.GetOutput().Delete(); + xcone.GetOutput().Delete(); + ycone.GetOutput().Delete(); + zcone.GetOutput().Delete(); + axes.GetOutput().Delete(); + axes.Delete(); + tubeMapper.Delete(); + tube.Delete(); + xcone.Delete(); + ycone.Delete(); + zcone.Delete(); + xconeMapper.Delete(); + yconeMapper.Delete(); + zconeMapper.Delete(); + + xconeActor.GetProperty().Delete(); + yconeActor.GetProperty().Delete(); + zconeActor.GetProperty().Delete(); + } + + 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); + + } + + @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; + } + +} diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axesSphereActor.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axesSphereActor.java index 97725bbc..bf37abf7 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axesSphereActor.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axesSphereActor.java @@ -1,385 +1,385 @@ -/******************************************************************************* - * 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.SetInput(axes.GetOutput()); - tube.SetRadius(0.05 * axisLength); - tube.SetNumberOfSides(8); - - vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper(); - tubeMapper.SetInput(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.SetInput(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.SetInput(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.SetInput(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.SetInput(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; - } - -} +/******************************************************************************* + * 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; + } + +} diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axisActor.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axisActor.java index a9e507ee..a94233cc 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axisActor.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axisActor.java @@ -1,279 +1,279 @@ -/******************************************************************************* - * 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.AxisAngle4d; -import javax.vecmath.Matrix4d; -import javax.vecmath.Point3d; -import javax.vecmath.Vector3d; - -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.vtkConeSource; -import vtk.vtkLineSource; -import vtk.vtkLinearTransform; -import vtk.vtkMatrix4x4; -import vtk.vtkPolyDataMapper; -import vtk.vtkProp3D; -import vtk.vtkRenderer; -import vtk.vtkTextActor; -import vtk.vtkTubeFilter; - -public class axisActor extends vtkAssembly implements IvtkVisualObject{ - - private vtkRenderer ren; - private Vector3d axisDir = new Vector3d(1,0,0); - private vtkTextActor tactor; - private vtkActor tubeActor; - private vtkActor coneActor; - private boolean rendered = false; - - public axisActor(vtkRenderer _ren, String label) { - super(); - ren = _ren; - createAxis(label); - } - - public axisActor(vtkRenderer _ren, String label, Vector3d dir) { - super(); - ren = _ren; - this.axisDir = dir; - createAxis(label); - } - - public void createAxis(String label) { - vtkLineSource line = new vtkLineSource(); - line.SetPoint1(0.0,0.0,0.0); - line.SetPoint2(axisDir.x,axisDir.y,axisDir.z); - - tactor = new vtkTextActor(); - - tactor.SetInput(label); - - tactor.SetTextScaleModeToNone(); - tactor.GetTextProperty().SetColor(0.0, 0.0, 0.0); - tactor.GetTextProperty().ShadowOff(); - tactor.GetTextProperty().ItalicOff(); - tactor.GetTextProperty().BoldOff(); - - tactor.GetTextProperty().Delete(); - - tactor.SetMaximumLineHeight(0.25); - - tactor.SetPickable(0); - - vtkTubeFilter tube = new vtkTubeFilter(); - tube.SetInput(line.GetOutput()); - tube.SetRadius(0.05 * axisDir.length()); - tube.SetNumberOfSides(8); - - vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper(); - tubeMapper.SetInput(tube.GetOutput()); - - tubeActor = new vtkActor(); - tubeActor.SetMapper(tubeMapper); - tubeActor.PickableOff(); - - int coneRes = 12; - double coneScale = 0.3 * axisDir.length(); - - // --- x-Cone - vtkConeSource cone = new vtkConeSource(); - cone.SetResolution(coneRes); - vtkPolyDataMapper coneMapper = new vtkPolyDataMapper(); - coneMapper.SetInput(cone.GetOutput()); - coneActor = new vtkActor(); - coneActor.SetMapper(coneMapper); - coneActor.GetProperty().SetColor(1, 0, 0); - coneActor.SetScale(coneScale, coneScale, coneScale); - coneActor.SetPosition(axisDir.x,axisDir.y,axisDir.z); - coneActor.SetPickable(0); - - AxisAngle4d aa = MathTools.createRotation(new Vector3d(1,0,0), new Vector3d(axisDir)); - if (aa != null) - coneActor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); - - this.AddPart(tubeActor); - this.AddPart(coneActor); - - tube.GetOutput().Delete(); - cone.GetOutput().Delete(); - line.GetOutput().Delete(); - - tubeMapper.Delete(); - tube.Delete(); - cone.Delete(); - line.Delete(); - - coneMapper.Delete(); - - coneActor.GetProperty().Delete(); - } - - public void addToRenderer() { - if (rendered) - return; - rendered = true; - - ren.AddActor2D(tactor); - - ren.AddActor(this); - } - - public void removeFromRenderer() { - if (!rendered) - return; - rendered = false; - ren.RemoveActor2D(tactor); - ren.RemoveActor(this); - } - - public boolean isRendered() { - return rendered; - } - - public void setAxesVisibility(boolean ison) { - this.SetVisibility(ison ? 1 : 0); - tactor.SetVisibility(ison ? 1 : 0); - } - - public void setLabelVisibility(boolean ison) { - tactor.SetVisibility(ison ? 1 : 0); - } - - double mat[] = new double[16]; - Matrix4d m = new Matrix4d(); - Point3d p = new Point3d(); - private void updateTextLoc() { - tactor.GetPositionCoordinate().SetCoordinateSystemToWorld(); - - - GetMatrix(mat); - MathTools.set(m, mat); - p.set(axisDir.x, axisDir.y,axisDir.z); - m.transform(p); - tactor.GetPositionCoordinate().SetValue(p.x, p.y, p.z); - - tactor.GetPositionCoordinate().Delete(); - } - - @Override - public void SetPickable(int id0) { - super.SetPickable(id0); - tubeActor.SetPickable(id0); - coneActor.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(); - } - - public void SetColor(double r, double g, double b) { - coneActor.GetProperty().SetColor(r, g, b); - tubeActor.GetProperty().SetColor(r, g, b); - coneActor.GetProperty().Delete(); - tubeActor.GetProperty().Delete(); - } - - public void SetTextColor(double r, double g, double b) { - tactor.GetTextProperty().SetColor(r, g, b); - tactor.GetTextProperty().Delete(); - } - - @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(tactor); - ren.RemoveActor(tubeActor); - ren.RemoveActor(coneActor); - tactor.Delete(); - tubeActor.Delete(); - coneActor.Delete(); - super.Delete(); - } - - public void dispose() { - ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { - - @Override - public void run() { - removeFromRenderer(); - Delete(); - } - }); - } - - @Override - public vtkProp3D getVtkProp() { - return this; - } - -} +/******************************************************************************* + * 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.AxisAngle4d; +import javax.vecmath.Matrix4d; +import javax.vecmath.Point3d; +import javax.vecmath.Vector3d; + +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.vtkConeSource; +import vtk.vtkLineSource; +import vtk.vtkLinearTransform; +import vtk.vtkMatrix4x4; +import vtk.vtkPolyDataMapper; +import vtk.vtkProp3D; +import vtk.vtkRenderer; +import vtk.vtkTextActor; +import vtk.vtkTubeFilter; + +public class axisActor extends vtkAssembly implements IvtkVisualObject{ + + private vtkRenderer ren; + private Vector3d axisDir = new Vector3d(1,0,0); + private vtkTextActor tactor; + private vtkActor tubeActor; + private vtkActor coneActor; + private boolean rendered = false; + + public axisActor(vtkRenderer _ren, String label) { + super(); + ren = _ren; + createAxis(label); + } + + public axisActor(vtkRenderer _ren, String label, Vector3d dir) { + super(); + ren = _ren; + this.axisDir = dir; + createAxis(label); + } + + public void createAxis(String label) { + vtkLineSource line = new vtkLineSource(); + line.SetPoint1(0.0,0.0,0.0); + line.SetPoint2(axisDir.x,axisDir.y,axisDir.z); + + tactor = new vtkTextActor(); + + tactor.SetInput(label); + + tactor.SetTextScaleModeToNone(); + tactor.GetTextProperty().SetColor(0.0, 0.0, 0.0); + tactor.GetTextProperty().ShadowOff(); + tactor.GetTextProperty().ItalicOff(); + tactor.GetTextProperty().BoldOff(); + + tactor.GetTextProperty().Delete(); + + tactor.SetMaximumLineHeight(0.25); + + tactor.SetPickable(0); + + vtkTubeFilter tube = new vtkTubeFilter(); + tube.SetInputData(line.GetOutput()); + tube.SetRadius(0.05 * axisDir.length()); + 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 * axisDir.length(); + + // --- x-Cone + vtkConeSource cone = new vtkConeSource(); + cone.SetResolution(coneRes); + vtkPolyDataMapper coneMapper = new vtkPolyDataMapper(); + coneMapper.SetInputData(cone.GetOutput()); + coneActor = new vtkActor(); + coneActor.SetMapper(coneMapper); + coneActor.GetProperty().SetColor(1, 0, 0); + coneActor.SetScale(coneScale, coneScale, coneScale); + coneActor.SetPosition(axisDir.x,axisDir.y,axisDir.z); + coneActor.SetPickable(0); + + AxisAngle4d aa = MathTools.createRotation(new Vector3d(1,0,0), new Vector3d(axisDir)); + if (aa != null) + coneActor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + + this.AddPart(tubeActor); + this.AddPart(coneActor); + + tube.GetOutput().Delete(); + cone.GetOutput().Delete(); + line.GetOutput().Delete(); + + tubeMapper.Delete(); + tube.Delete(); + cone.Delete(); + line.Delete(); + + coneMapper.Delete(); + + coneActor.GetProperty().Delete(); + } + + public void addToRenderer() { + if (rendered) + return; + rendered = true; + + ren.AddActor2D(tactor); + + ren.AddActor(this); + } + + public void removeFromRenderer() { + if (!rendered) + return; + rendered = false; + ren.RemoveActor2D(tactor); + ren.RemoveActor(this); + } + + public boolean isRendered() { + return rendered; + } + + public void setAxesVisibility(boolean ison) { + this.SetVisibility(ison ? 1 : 0); + tactor.SetVisibility(ison ? 1 : 0); + } + + public void setLabelVisibility(boolean ison) { + tactor.SetVisibility(ison ? 1 : 0); + } + + double mat[] = new double[16]; + Matrix4d m = new Matrix4d(); + Point3d p = new Point3d(); + private void updateTextLoc() { + tactor.GetPositionCoordinate().SetCoordinateSystemToWorld(); + + + GetMatrix(mat); + MathTools.set(m, mat); + p.set(axisDir.x, axisDir.y,axisDir.z); + m.transform(p); + tactor.GetPositionCoordinate().SetValue(p.x, p.y, p.z); + + tactor.GetPositionCoordinate().Delete(); + } + + @Override + public void SetPickable(int id0) { + super.SetPickable(id0); + tubeActor.SetPickable(id0); + coneActor.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(); + } + + public void SetColor(double r, double g, double b) { + coneActor.GetProperty().SetColor(r, g, b); + tubeActor.GetProperty().SetColor(r, g, b); + coneActor.GetProperty().Delete(); + tubeActor.GetProperty().Delete(); + } + + public void SetTextColor(double r, double g, double b) { + tactor.GetTextProperty().SetColor(r, g, b); + tactor.GetTextProperty().Delete(); + } + + @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(tactor); + ren.RemoveActor(tubeActor); + ren.RemoveActor(coneActor); + tactor.Delete(); + tubeActor.Delete(); + coneActor.Delete(); + super.Delete(); + } + + public void dispose() { + ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { + + @Override + public void run() { + removeFromRenderer(); + Delete(); + } + }); + } + + @Override + public vtkProp3D getVtkProp() { + return this; + } + +} diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/vtkShape.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/vtkShape.java index d3284df2..c992547f 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/vtkShape.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/vtkShape.java @@ -1,195 +1,195 @@ -/******************************************************************************* - * 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.Tuple3d; - -import vtk.vtkActor; -import vtk.vtkDataSetMapper; -import vtk.vtkLine; -import vtk.vtkPoints; -import vtk.vtkPolyLine; -import vtk.vtkUnstructuredGrid; - -public class vtkShape { - - /** - * Creates a grid shaped actor. - * - * @param size number of grid lines - * @param space distance between grid lines - * @param axes bitmask of axes: 1:x, 2:y, 4:z - * @return vtkActor representing a grid. - */ - public static vtkActor createGridActor(int size, double space, int axes) { - int gridCount = 0; - if ((axes & 0x1) > 0) { - gridCount++; - } - if ((axes & 0x2) > 0) { - gridCount++; - } - if ((axes & 0x4) > 0) { - gridCount++; - } - int pointCount = (size+1) * 2 * 2 * gridCount; - vtkPoints linePoints = new vtkPoints(); - linePoints.SetNumberOfPoints(pointCount); - - - double max = space * (double)size * 0.5; - double min = -max; - int base = 0; - if ((axes & 0x1) > 0) { - for (int i = 0; i <= size; i++) { - double s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 ,0.0, s, min); - linePoints.InsertPoint(base + i*2+1,0.0, s, max); - i++; - if (i > size) - break; - s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 ,0.0, s, max); - linePoints.InsertPoint(base + i*2+1,0.0, s, min); - } - base += (size+1)*2; - for (int i = 0; i <= size; i++) { - double s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 , 0.0, max, s); - linePoints.InsertPoint(base + i*2+1, 0.0, min, s); - i++; - if (i > size) - break; - s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 , 0.0, min, s); - linePoints.InsertPoint(base + i*2+1, 0.0, max, s); - } - base += (size+1)*2; - } - if ((axes & 0x4) > 0) { - for (int i = 0; i <= size; i++) { - double s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 ,s, min, 0.0); - linePoints.InsertPoint(base + i*2+1,s, max, 0.0); - i++; - if (i > size) - break; - s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 ,s, max, 0.0); - linePoints.InsertPoint(base + i*2+1,s, min, 0.0); - } - base += (size+1)*2; - for (int i = 0; i <= size; i++) { - double s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 ,max, s, 0.0); - linePoints.InsertPoint(base + i*2+1,min, s, 0.0); - i++; - if (i > size) - break; - s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 ,min, s, 0.0); - linePoints.InsertPoint(base + i*2+1,max, s, 0.0); - } - base += (size+1)*2; - } - if ((axes & 0x2) > 0) { - for (int i = 0; i <= size; i++) { - double s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 ,s, 0.0, min); - linePoints.InsertPoint(base + i*2+1,s, 0.0, max); - i++; - if (i > size) - break; - s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 ,s, 0.0, max); - linePoints.InsertPoint(base + i*2+1,s, 0.0, min); - } - base += (size+1)*2; - for (int i = 0; i <= size; i++) { - double s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 ,max, 0.0, s); - linePoints.InsertPoint(base + i*2+1,min, 0.0, s); - i++; - if (i > size) - break; - s = min + ((double)i) * space; - linePoints.InsertPoint(base + i*2 ,min, 0.0, s); - linePoints.InsertPoint(base + i*2+1,max, 0.0, s); - } - base += (size+1)*2; - } - - - //vtkLine aLine = new vtkLine(); - vtkPolyLine aLine = new vtkPolyLine(); - aLine.GetPointIds().SetNumberOfIds(pointCount); - for (int i = 0; i < pointCount; i++) { - aLine.GetPointIds().SetId(i, i); - } - - - vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid(); - aLineGrid.Allocate(pointCount, pointCount); - aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds()); - aLineGrid.SetPoints(linePoints); - vtkDataSetMapper aLineMapper = new vtkDataSetMapper(); - aLineMapper.SetInput(aLineGrid); - vtkActor aLineActor = new vtkActor(); - aLineActor.SetMapper(aLineMapper); - - linePoints.Delete(); - aLine.GetPointIds().Delete(); - aLine.Delete(); - aLineGrid.Delete(); - aLineMapper.Delete(); - - aLineActor.GetProperty().SetColor(0, 0, 0); - aLineActor.GetProperty().Delete(); - - return aLineActor; - } - - /** - * Creates a line. - * - * @param p1 - * @param p2 - * @return - */ - public static vtkActor createLineActor(Tuple3d p1, Tuple3d p2) { - vtkPoints linePoints = new vtkPoints(); - linePoints.SetNumberOfPoints(2); - linePoints.InsertPoint(0,p1.x, p1.y, p1.z); - linePoints.InsertPoint(1,p2.x, p2.y, p2.z); - vtkLine aLine = new vtkLine(); - aLine.GetPointIds().SetId(0, 0); - aLine.GetPointIds().SetId(1, 1); - vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid(); - aLineGrid.Allocate(1, 1); - aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds()); - aLineGrid.SetPoints(linePoints); - vtkDataSetMapper aLineMapper = new vtkDataSetMapper(); - aLineMapper.SetInput(aLineGrid); - vtkActor aLineActor = new vtkActor(); - aLineActor.SetMapper(aLineMapper); - //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1); - - linePoints.Delete(); - aLine.GetPointIds().Delete(); - aLine.Delete(); - aLineGrid.Delete(); - aLineMapper.Delete(); - - return aLineActor; - } - -} +/******************************************************************************* + * 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.Tuple3d; + +import vtk.vtkActor; +import vtk.vtkDataSetMapper; +import vtk.vtkLine; +import vtk.vtkPoints; +import vtk.vtkPolyLine; +import vtk.vtkUnstructuredGrid; + +public class vtkShape { + + /** + * Creates a grid shaped actor. + * + * @param size number of grid lines + * @param space distance between grid lines + * @param axes bitmask of axes: 1:x, 2:y, 4:z + * @return vtkActor representing a grid. + */ + public static vtkActor createGridActor(int size, double space, int axes) { + int gridCount = 0; + if ((axes & 0x1) > 0) { + gridCount++; + } + if ((axes & 0x2) > 0) { + gridCount++; + } + if ((axes & 0x4) > 0) { + gridCount++; + } + int pointCount = (size+1) * 2 * 2 * gridCount; + vtkPoints linePoints = new vtkPoints(); + linePoints.SetNumberOfPoints(pointCount); + + + double max = space * (double)size * 0.5; + double min = -max; + int base = 0; + if ((axes & 0x1) > 0) { + for (int i = 0; i <= size; i++) { + double s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 ,0.0, s, min); + linePoints.InsertPoint(base + i*2+1,0.0, s, max); + i++; + if (i > size) + break; + s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 ,0.0, s, max); + linePoints.InsertPoint(base + i*2+1,0.0, s, min); + } + base += (size+1)*2; + for (int i = 0; i <= size; i++) { + double s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 , 0.0, max, s); + linePoints.InsertPoint(base + i*2+1, 0.0, min, s); + i++; + if (i > size) + break; + s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 , 0.0, min, s); + linePoints.InsertPoint(base + i*2+1, 0.0, max, s); + } + base += (size+1)*2; + } + if ((axes & 0x4) > 0) { + for (int i = 0; i <= size; i++) { + double s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 ,s, min, 0.0); + linePoints.InsertPoint(base + i*2+1,s, max, 0.0); + i++; + if (i > size) + break; + s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 ,s, max, 0.0); + linePoints.InsertPoint(base + i*2+1,s, min, 0.0); + } + base += (size+1)*2; + for (int i = 0; i <= size; i++) { + double s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 ,max, s, 0.0); + linePoints.InsertPoint(base + i*2+1,min, s, 0.0); + i++; + if (i > size) + break; + s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 ,min, s, 0.0); + linePoints.InsertPoint(base + i*2+1,max, s, 0.0); + } + base += (size+1)*2; + } + if ((axes & 0x2) > 0) { + for (int i = 0; i <= size; i++) { + double s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 ,s, 0.0, min); + linePoints.InsertPoint(base + i*2+1,s, 0.0, max); + i++; + if (i > size) + break; + s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 ,s, 0.0, max); + linePoints.InsertPoint(base + i*2+1,s, 0.0, min); + } + base += (size+1)*2; + for (int i = 0; i <= size; i++) { + double s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 ,max, 0.0, s); + linePoints.InsertPoint(base + i*2+1,min, 0.0, s); + i++; + if (i > size) + break; + s = min + ((double)i) * space; + linePoints.InsertPoint(base + i*2 ,min, 0.0, s); + linePoints.InsertPoint(base + i*2+1,max, 0.0, s); + } + base += (size+1)*2; + } + + + //vtkLine aLine = new vtkLine(); + vtkPolyLine aLine = new vtkPolyLine(); + aLine.GetPointIds().SetNumberOfIds(pointCount); + for (int i = 0; i < pointCount; i++) { + aLine.GetPointIds().SetId(i, i); + } + + + vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid(); + aLineGrid.Allocate(pointCount, pointCount); + aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds()); + aLineGrid.SetPoints(linePoints); + vtkDataSetMapper aLineMapper = new vtkDataSetMapper(); + aLineMapper.SetInputData(aLineGrid); + vtkActor aLineActor = new vtkActor(); + aLineActor.SetMapper(aLineMapper); + + linePoints.Delete(); + aLine.GetPointIds().Delete(); + aLine.Delete(); + aLineGrid.Delete(); + aLineMapper.Delete(); + + aLineActor.GetProperty().SetColor(0, 0, 0); + aLineActor.GetProperty().Delete(); + + return aLineActor; + } + + /** + * Creates a line. + * + * @param p1 + * @param p2 + * @return + */ + public static vtkActor createLineActor(Tuple3d p1, Tuple3d p2) { + vtkPoints linePoints = new vtkPoints(); + linePoints.SetNumberOfPoints(2); + linePoints.InsertPoint(0,p1.x, p1.y, p1.z); + linePoints.InsertPoint(1,p2.x, p2.y, p2.z); + vtkLine aLine = new vtkLine(); + aLine.GetPointIds().SetId(0, 0); + aLine.GetPointIds().SetId(1, 1); + vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid(); + aLineGrid.Allocate(1, 1); + aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds()); + aLineGrid.SetPoints(linePoints); + vtkDataSetMapper aLineMapper = new vtkDataSetMapper(); + aLineMapper.SetInputData(aLineGrid); + vtkActor aLineActor = new vtkActor(); + aLineActor.SetMapper(aLineMapper); + //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1); + + linePoints.Delete(); + aLine.GetPointIds().Delete(); + aLine.Delete(); + aLineGrid.Delete(); + aLineMapper.Delete(); + + return aLineActor; + } + +} diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkEffect.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkEffect.java index 7fa374aa..d4cd7103 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkEffect.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkEffect.java @@ -1,373 +1,373 @@ -/******************************************************************************* - * 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.utils; - -import javax.vecmath.AxisAngle4d; -import javax.vecmath.Vector3d; - -import org.simantics.g3d.math.MathTools; -import org.simantics.g3d.vtk.shape.IvtkVisualObject; - -import vtk.vtkActor; -import vtk.vtkCamera; -import vtk.vtkContourFilter; -import vtk.vtkDataObject; -import vtk.vtkOutlineFilter; -import vtk.vtkOutlineSource; -import vtk.vtkPolyData; -import vtk.vtkPolyDataMapper; -import vtk.vtkPolyDataSilhouette; -import vtk.vtkProp3D; -import vtk.vtkRenderWindow; -import vtk.vtkRenderer; -import vtk.vtkTransform; -import vtk.vtkTransformPolyDataFilter; -import vtk.vtkWindowToImageFilter; - -public class vtkEffect { - - private static vtkRenderer tmpRenderer; - - public static vtkRenderer getRenderer() { - if (tmpRenderer == null) - tmpRenderer = new vtkRenderer(); - return tmpRenderer; - } - public static vtkActor createContour(vtkProp3D tmpProp) { - - vtkRenderer tmpRenderer = getRenderer(); - tmpRenderer.SetBackground(1,1,1); - - - if (tmpProp instanceof IvtkVisualObject) { - ((IvtkVisualObject)tmpProp).addToRenderer(); - } else { - tmpRenderer.AddActor(tmpProp); - } - - if (tmpProp instanceof vtkActor) { - ((vtkActor)tmpProp).GetProperty().SetColor(0, 0, 0); - } - - - double bounds_data[] = tmpProp.GetBounds(); - double center_data[] = tmpProp.GetCenter(); - - tmpRenderer.ResetCamera(); - tmpRenderer.GetActiveCamera().SetParallelProjection(1); - - vtkRenderWindow tmpRenderWindow = new vtkRenderWindow(); - tmpRenderWindow.SetOffScreenRendering(1); - tmpRenderWindow.AddRenderer(tmpRenderer); - tmpRenderWindow.Render(); - - vtkWindowToImageFilter windowToImageFilter = new vtkWindowToImageFilter(); - windowToImageFilter.SetInput(tmpRenderWindow); - windowToImageFilter.SetMagnification(2); - windowToImageFilter.Update(); - - vtkContourFilter contourFilter = new vtkContourFilter(); - contourFilter.SetInputConnection(windowToImageFilter.GetOutputPort()); - - contourFilter.SetValue(0, 255); - contourFilter.Update(); - - vtkPolyData contour = contourFilter.GetOutput(); - - double bounds_contour[] = new double[6]; - double center_contour[] = new double[3]; - double trans_x=0., trans_y=0., trans_z=0., ratio_x=0., ratio_y=0.; - contour.GetBounds(bounds_contour); - - ratio_x = (bounds_data[1]-bounds_data[0])/(bounds_contour[1]-bounds_contour[0]); - ratio_y = (bounds_data[3]-bounds_data[2])/(bounds_contour[3]-bounds_contour[2]); - - vtkTransform transform1 = new vtkTransform(); - transform1.Scale(ratio_x,ratio_y,1); - - vtkTransformPolyDataFilter tFilter1 = new vtkTransformPolyDataFilter(); - tFilter1.SetInput(contour); - tFilter1.SetTransform(transform1); - tFilter1.Update(); - - contour = tFilter1.GetOutput(); - - contour.GetCenter(center_contour); - trans_x = center_data[0]-center_contour[0]; - trans_y = center_data[1]-center_contour[1]; - trans_z = center_data[2]-center_contour[2]; - - vtkTransform transform2 = new vtkTransform(); - transform2.Translate( trans_x, trans_y, trans_z); - - vtkTransformPolyDataFilter tFilter2 = new vtkTransformPolyDataFilter(); - tFilter2.SetInput(contour); - tFilter2.SetTransform(transform2); - tFilter2.Update(); - - contour = tFilter2.GetOutput(); - - vtkPolyDataMapper mapper = new vtkPolyDataMapper(); - mapper.SetInput(contour); - - - vtkActor actor =new vtkActor(); - actor.SetMapper(mapper); - actor.GetProperty().SetLineWidth(2.); - - tmpRenderer.RemoveActor(tmpProp); - - tFilter1.Delete(); - tFilter2.Delete(); - contour.Delete(); - contourFilter.Delete(); - mapper.Delete(); - tmpRenderWindow.Delete(); - transform1.Delete(); - transform2.Delete(); - windowToImageFilter.Delete(); - - return actor; - } - - public static vtkActor createContour(vtkProp3D tmpProp, vtkRenderer ren) { - - vtkRenderer tmpRenderer = getRenderer(); - tmpRenderer.SetBackground(1,1,1); - - - if (tmpProp instanceof IvtkVisualObject) { - ((IvtkVisualObject)tmpProp).addToRenderer(); - } else { - tmpRenderer.AddActor(tmpProp); - } - - if (tmpProp instanceof vtkActor) { - ((vtkActor)tmpProp).GetProperty().SetColor(0, 0, 0); - } - - - //double bounds_data[] = tmpProp.GetBounds(); - double center_data[] = tmpProp.GetCenter(); - - tmpRenderer.ResetCamera(); - - vtkCamera camera = ren.GetActiveCamera(); - - vtkCamera tmpCamera = tmpRenderer.GetActiveCamera(); - tmpCamera.SetParallelProjection(camera.GetParallelProjection()); - - - Vector3d pos = new Vector3d(camera.GetPosition()); - Vector3d foc = new Vector3d(camera.GetFocalPoint()); - Vector3d dir = new Vector3d(); - dir.sub(pos,foc); - double l = dir.length(); -// dir.scale(1.0/l); -// -// -// -// -// dir.scale(2.0); -// Vector3d tmpFoc = new Vector3d(tmpCamera.GetFocalPoint()); -// tmpFoc.add(dir); -// tmpCamera.SetPosition(tmpFoc.x,tmpFoc.y,tmpFoc.z); -// tmpCamera.SetRoll(camera.GetRoll()); - - tmpCamera.DeepCopy(camera); - //tmpCamera.SetModelTransformMatrix(camera.GetModelTransformMatrix()); - tmpCamera.UpdateViewport(tmpRenderer); - - vtkRenderWindow tmpRenderWindow = new vtkRenderWindow(); - tmpRenderWindow.SetOffScreenRendering(1); - tmpRenderWindow.AddRenderer(tmpRenderer); - tmpRenderWindow.Render(); - - vtkWindowToImageFilter windowToImageFilter = new vtkWindowToImageFilter(); - windowToImageFilter.SetInput(tmpRenderWindow); - windowToImageFilter.SetMagnification(2); - windowToImageFilter.Update(); - - vtkContourFilter contourFilter = new vtkContourFilter(); - contourFilter.SetInputConnection(windowToImageFilter.GetOutputPort()); - - contourFilter.SetValue(0, 255); - contourFilter.Update(); - - vtkPolyData contour = contourFilter.GetOutput(); - - - double or[] = camera.GetOrientationWXYZ(); - AxisAngle4d aa = new AxisAngle4d(); - aa.angle = -MathTools.degToRad(or[0]); - aa.x = or[1]; - aa.y = or[2]; - aa.z = or[3]; - - vtkTransform transform0 = new vtkTransform(); - transform0.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); - - vtkTransformPolyDataFilter tFilter0 = new vtkTransformPolyDataFilter(); - tFilter0.SetInput(contour); - tFilter0.SetTransform(transform0); - tFilter0.Update(); - - contour.Delete(); - contour = tFilter0.GetOutput(); - - double bounds_contour[] = new double[6]; - - double trans_x=0., trans_y=0., trans_z=0., ratio_x=0.002, ratio_y=0.002, ratio_z=0.002; - contour.GetBounds(bounds_contour); -// -// ratio_x = (bounds_data[1]-bounds_data[0])/(bounds_contour[1]-bounds_contour[0]); -// ratio_y = (bounds_data[3]-bounds_data[2])/(bounds_contour[3]-bounds_contour[2]); -// ratio_z = (bounds_data[5]-bounds_data[4])/(bounds_contour[5]-bounds_contour[4]); - ratio_x = l*0.0008; - ratio_y = l*0.0008; - ratio_z = l*0.0008; - - - System.out.println(ratio_x + " " + ratio_y); - - -// - vtkTransform transform1 = new vtkTransform(); - transform1.Scale(ratio_x,ratio_y,ratio_z); - - vtkTransformPolyDataFilter tFilter1 = new vtkTransformPolyDataFilter(); - tFilter1.SetInput(contour); - tFilter1.SetTransform(transform1); - tFilter1.Update(); - - contour.Delete(); - contour = tFilter1.GetOutput(); - - double center_contour[] = new double[3]; - contour.GetCenter(center_contour); - trans_x = center_data[0]-center_contour[0]; - trans_y = center_data[1]-center_contour[1]; - trans_z = center_data[2]-center_contour[2]; -// - System.out.println(trans_x + " " + trans_y + " " + trans_z); -// - vtkTransform transform2 = new vtkTransform(); - transform2.Translate( trans_x, trans_y, trans_z); - - vtkTransformPolyDataFilter tFilter2 = new vtkTransformPolyDataFilter(); - tFilter2.SetInput(contour); - tFilter2.SetTransform(transform2); - tFilter2.Update(); - - contour.Delete(); - contour = tFilter2.GetOutput(); - - vtkPolyDataMapper mapper = new vtkPolyDataMapper(); - mapper.SetInput(contour); - - - vtkActor actor =new vtkActor(); - actor.SetMapper(mapper); - actor.GetProperty().SetLineWidth(2.); - actor.GetProperty().SetColor(0,0,1); - actor.GetProperty().Delete(); - - tmpRenderer.RemoveActor(tmpProp); - - tFilter0.Delete(); - tFilter1.Delete(); - tFilter2.Delete(); - contour.Delete(); - contourFilter.Delete(); - mapper.Delete(); - tmpRenderWindow.Delete(); - transform0.Delete(); - transform1.Delete(); - transform2.Delete(); - windowToImageFilter.GetOutputPort().Delete(); - windowToImageFilter.Delete(); - - ren.AddActor(actor); - -// vtkMatrix4x4 m = camera.GetModelTransformMatrix(); -// m.Invert(); -// actor.SetUserMatrix(m); - //m.Delete(); - //actor.SetPosition(trans_x,trans_y, trans_z); - //actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); - // - return actor; - } - - public static vtkActor createSilhouette(vtkRenderer ren, vtkDataObject data) { - - - vtkPolyDataSilhouette silhouette = new vtkPolyDataSilhouette(); - - silhouette.SetInput(data); - silhouette.SetCamera(ren.GetActiveCamera()); - silhouette.SetEnableFeatureAngle(0); - vtkPolyDataMapper mapper = new vtkPolyDataMapper(); - - mapper.SetInputConnection(silhouette.GetOutputPort()); - - vtkActor actor = new vtkActor(); - actor.SetMapper(mapper); - - silhouette.GetOutputPort().Delete(); - silhouette.Delete(); - mapper.Delete(); - - return actor; - } - - public static vtkActor createOutline(vtkDataObject data) { - vtkOutlineFilter filter = new vtkOutlineFilter(); - filter.SetInput(data); - vtkPolyDataMapper mapper = new vtkPolyDataMapper(); - mapper.SetInputConnection(filter.GetOutputPort()); - - vtkActor actor = new vtkActor(); - actor.SetMapper(mapper); - - filter.GetOutputPort().Delete(); - filter.Delete(); - mapper.Delete(); - - return actor; - } - - public static vtkActor createOutline(vtkProp3D prop) { - - double bounds[] = prop.GetBounds(); - - vtkOutlineSource source = new vtkOutlineSource(); - source.SetBounds(bounds); - - vtkOutlineFilter filter = new vtkOutlineFilter(); - filter.SetInput(source.GetOutput()); - vtkPolyDataMapper mapper = new vtkPolyDataMapper(); - mapper.SetInputConnection(filter.GetOutputPort()); - - vtkActor actor = new vtkActor(); - actor.SetMapper(mapper); - - source.GetOutput().Delete(); - source.Delete(); - filter.GetOutputPort().Delete(); - filter.Delete(); - mapper.Delete(); - - actor.SetPickable(0); - return actor; - } -} +/******************************************************************************* + * 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.utils; + +import javax.vecmath.AxisAngle4d; +import javax.vecmath.Vector3d; + +import org.simantics.g3d.math.MathTools; +import org.simantics.g3d.vtk.shape.IvtkVisualObject; + +import vtk.vtkActor; +import vtk.vtkCamera; +import vtk.vtkContourFilter; +import vtk.vtkDataObject; +import vtk.vtkOutlineFilter; +import vtk.vtkOutlineSource; +import vtk.vtkPolyData; +import vtk.vtkPolyDataMapper; +import vtk.vtkPolyDataSilhouette; +import vtk.vtkProp3D; +import vtk.vtkRenderWindow; +import vtk.vtkRenderer; +import vtk.vtkTransform; +import vtk.vtkTransformPolyDataFilter; +import vtk.vtkWindowToImageFilter; + +public class vtkEffect { + + private static vtkRenderer tmpRenderer; + + public static vtkRenderer getRenderer() { + if (tmpRenderer == null) + tmpRenderer = new vtkRenderer(); + return tmpRenderer; + } + public static vtkActor createContour(vtkProp3D tmpProp) { + + vtkRenderer tmpRenderer = getRenderer(); + tmpRenderer.SetBackground(1,1,1); + + + if (tmpProp instanceof IvtkVisualObject) { + ((IvtkVisualObject)tmpProp).addToRenderer(); + } else { + tmpRenderer.AddActor(tmpProp); + } + + if (tmpProp instanceof vtkActor) { + ((vtkActor)tmpProp).GetProperty().SetColor(0, 0, 0); + } + + + double bounds_data[] = tmpProp.GetBounds(); + double center_data[] = tmpProp.GetCenter(); + + tmpRenderer.ResetCamera(); + tmpRenderer.GetActiveCamera().SetParallelProjection(1); + + vtkRenderWindow tmpRenderWindow = new vtkRenderWindow(); + tmpRenderWindow.SetOffScreenRendering(1); + tmpRenderWindow.AddRenderer(tmpRenderer); + tmpRenderWindow.Render(); + + vtkWindowToImageFilter windowToImageFilter = new vtkWindowToImageFilter(); + windowToImageFilter.SetInput(tmpRenderWindow); + //windowToImageFilter.SetMagnification(2); + windowToImageFilter.Update(); + + vtkContourFilter contourFilter = new vtkContourFilter(); + contourFilter.SetInputConnection(windowToImageFilter.GetOutputPort()); + + contourFilter.SetValue(0, 255); + contourFilter.Update(); + + vtkPolyData contour = contourFilter.GetOutput(); + + double bounds_contour[] = new double[6]; + double center_contour[] = new double[3]; + double trans_x=0., trans_y=0., trans_z=0., ratio_x=0., ratio_y=0.; + contour.GetBounds(bounds_contour); + + ratio_x = (bounds_data[1]-bounds_data[0])/(bounds_contour[1]-bounds_contour[0]); + ratio_y = (bounds_data[3]-bounds_data[2])/(bounds_contour[3]-bounds_contour[2]); + + vtkTransform transform1 = new vtkTransform(); + transform1.Scale(ratio_x,ratio_y,1); + + vtkTransformPolyDataFilter tFilter1 = new vtkTransformPolyDataFilter(); + tFilter1.SetInputData(contour); + tFilter1.SetTransform(transform1); + tFilter1.Update(); + + contour = tFilter1.GetOutput(); + + contour.GetCenter(center_contour); + trans_x = center_data[0]-center_contour[0]; + trans_y = center_data[1]-center_contour[1]; + trans_z = center_data[2]-center_contour[2]; + + vtkTransform transform2 = new vtkTransform(); + transform2.Translate( trans_x, trans_y, trans_z); + + vtkTransformPolyDataFilter tFilter2 = new vtkTransformPolyDataFilter(); + tFilter2.SetInputData(contour); + tFilter2.SetTransform(transform2); + tFilter2.Update(); + + contour = tFilter2.GetOutput(); + + vtkPolyDataMapper mapper = new vtkPolyDataMapper(); + mapper.SetInputData(contour); + + + vtkActor actor =new vtkActor(); + actor.SetMapper(mapper); + actor.GetProperty().SetLineWidth(2.); + + tmpRenderer.RemoveActor(tmpProp); + + tFilter1.Delete(); + tFilter2.Delete(); + contour.Delete(); + contourFilter.Delete(); + mapper.Delete(); + tmpRenderWindow.Delete(); + transform1.Delete(); + transform2.Delete(); + windowToImageFilter.Delete(); + + return actor; + } + + public static vtkActor createContour(vtkProp3D tmpProp, vtkRenderer ren) { + + vtkRenderer tmpRenderer = getRenderer(); + tmpRenderer.SetBackground(1,1,1); + + + if (tmpProp instanceof IvtkVisualObject) { + ((IvtkVisualObject)tmpProp).addToRenderer(); + } else { + tmpRenderer.AddActor(tmpProp); + } + + if (tmpProp instanceof vtkActor) { + ((vtkActor)tmpProp).GetProperty().SetColor(0, 0, 0); + } + + + //double bounds_data[] = tmpProp.GetBounds(); + double center_data[] = tmpProp.GetCenter(); + + tmpRenderer.ResetCamera(); + + vtkCamera camera = ren.GetActiveCamera(); + + vtkCamera tmpCamera = tmpRenderer.GetActiveCamera(); + tmpCamera.SetParallelProjection(camera.GetParallelProjection()); + + + Vector3d pos = new Vector3d(camera.GetPosition()); + Vector3d foc = new Vector3d(camera.GetFocalPoint()); + Vector3d dir = new Vector3d(); + dir.sub(pos,foc); + double l = dir.length(); +// dir.scale(1.0/l); +// +// +// +// +// dir.scale(2.0); +// Vector3d tmpFoc = new Vector3d(tmpCamera.GetFocalPoint()); +// tmpFoc.add(dir); +// tmpCamera.SetPosition(tmpFoc.x,tmpFoc.y,tmpFoc.z); +// tmpCamera.SetRoll(camera.GetRoll()); + + tmpCamera.DeepCopy(camera); + //tmpCamera.SetModelTransformMatrix(camera.GetModelTransformMatrix()); + tmpCamera.UpdateViewport(tmpRenderer); + + vtkRenderWindow tmpRenderWindow = new vtkRenderWindow(); + tmpRenderWindow.SetOffScreenRendering(1); + tmpRenderWindow.AddRenderer(tmpRenderer); + tmpRenderWindow.Render(); + + vtkWindowToImageFilter windowToImageFilter = new vtkWindowToImageFilter(); + windowToImageFilter.SetInput(tmpRenderWindow); + //windowToImageFilter.SetMagnification(2); + windowToImageFilter.Update(); + + vtkContourFilter contourFilter = new vtkContourFilter(); + contourFilter.SetInputConnection(windowToImageFilter.GetOutputPort()); + + contourFilter.SetValue(0, 255); + contourFilter.Update(); + + vtkPolyData contour = contourFilter.GetOutput(); + + + double or[] = camera.GetOrientationWXYZ(); + AxisAngle4d aa = new AxisAngle4d(); + aa.angle = -MathTools.degToRad(or[0]); + aa.x = or[1]; + aa.y = or[2]; + aa.z = or[3]; + + vtkTransform transform0 = new vtkTransform(); + transform0.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + + vtkTransformPolyDataFilter tFilter0 = new vtkTransformPolyDataFilter(); + tFilter0.SetInputData(contour); + tFilter0.SetTransform(transform0); + tFilter0.Update(); + + contour.Delete(); + contour = tFilter0.GetOutput(); + + double bounds_contour[] = new double[6]; + + double trans_x=0., trans_y=0., trans_z=0., ratio_x=0.002, ratio_y=0.002, ratio_z=0.002; + contour.GetBounds(bounds_contour); +// +// ratio_x = (bounds_data[1]-bounds_data[0])/(bounds_contour[1]-bounds_contour[0]); +// ratio_y = (bounds_data[3]-bounds_data[2])/(bounds_contour[3]-bounds_contour[2]); +// ratio_z = (bounds_data[5]-bounds_data[4])/(bounds_contour[5]-bounds_contour[4]); + ratio_x = l*0.0008; + ratio_y = l*0.0008; + ratio_z = l*0.0008; + + + System.out.println(ratio_x + " " + ratio_y); + + +// + vtkTransform transform1 = new vtkTransform(); + transform1.Scale(ratio_x,ratio_y,ratio_z); + + vtkTransformPolyDataFilter tFilter1 = new vtkTransformPolyDataFilter(); + tFilter1.SetInputData(contour); + tFilter1.SetTransform(transform1); + tFilter1.Update(); + + contour.Delete(); + contour = tFilter1.GetOutput(); + + double center_contour[] = new double[3]; + contour.GetCenter(center_contour); + trans_x = center_data[0]-center_contour[0]; + trans_y = center_data[1]-center_contour[1]; + trans_z = center_data[2]-center_contour[2]; +// + System.out.println(trans_x + " " + trans_y + " " + trans_z); +// + vtkTransform transform2 = new vtkTransform(); + transform2.Translate( trans_x, trans_y, trans_z); + + vtkTransformPolyDataFilter tFilter2 = new vtkTransformPolyDataFilter(); + tFilter2.SetInputData(contour); + tFilter2.SetTransform(transform2); + tFilter2.Update(); + + contour.Delete(); + contour = tFilter2.GetOutput(); + + vtkPolyDataMapper mapper = new vtkPolyDataMapper(); + mapper.SetInputData(contour); + + + vtkActor actor =new vtkActor(); + actor.SetMapper(mapper); + actor.GetProperty().SetLineWidth(2.); + actor.GetProperty().SetColor(0,0,1); + actor.GetProperty().Delete(); + + tmpRenderer.RemoveActor(tmpProp); + + tFilter0.Delete(); + tFilter1.Delete(); + tFilter2.Delete(); + contour.Delete(); + contourFilter.Delete(); + mapper.Delete(); + tmpRenderWindow.Delete(); + transform0.Delete(); + transform1.Delete(); + transform2.Delete(); + windowToImageFilter.GetOutputPort().Delete(); + windowToImageFilter.Delete(); + + ren.AddActor(actor); + +// vtkMatrix4x4 m = camera.GetModelTransformMatrix(); +// m.Invert(); +// actor.SetUserMatrix(m); + //m.Delete(); + //actor.SetPosition(trans_x,trans_y, trans_z); + //actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + // + return actor; + } + + public static vtkActor createSilhouette(vtkRenderer ren, vtkDataObject data) { + + + vtkPolyDataSilhouette silhouette = new vtkPolyDataSilhouette(); + + silhouette.SetInputData(data); + silhouette.SetCamera(ren.GetActiveCamera()); + silhouette.SetEnableFeatureAngle(0); + vtkPolyDataMapper mapper = new vtkPolyDataMapper(); + + mapper.SetInputConnection(silhouette.GetOutputPort()); + + vtkActor actor = new vtkActor(); + actor.SetMapper(mapper); + + silhouette.GetOutputPort().Delete(); + silhouette.Delete(); + mapper.Delete(); + + return actor; + } + + public static vtkActor createOutline(vtkDataObject data) { + vtkOutlineFilter filter = new vtkOutlineFilter(); + filter.SetInputData(data); + vtkPolyDataMapper mapper = new vtkPolyDataMapper(); + mapper.SetInputConnection(filter.GetOutputPort()); + + vtkActor actor = new vtkActor(); + actor.SetMapper(mapper); + + filter.GetOutputPort().Delete(); + filter.Delete(); + mapper.Delete(); + + return actor; + } + + public static vtkActor createOutline(vtkProp3D prop) { + + double bounds[] = prop.GetBounds(); + + vtkOutlineSource source = new vtkOutlineSource(); + source.SetBounds(bounds); + + vtkOutlineFilter filter = new vtkOutlineFilter(); + filter.SetInputData(source.GetOutput()); + vtkPolyDataMapper mapper = new vtkPolyDataMapper(); + mapper.SetInputConnection(filter.GetOutputPort()); + + vtkActor actor = new vtkActor(); + actor.SetMapper(mapper); + + source.GetOutput().Delete(); + source.Delete(); + filter.GetOutputPort().Delete(); + filter.Delete(); + mapper.Delete(); + + actor.SetPickable(0); + return actor; + } +} diff --git a/org.simantics.opencascade.vtk/src/org/simantics/opencascade/vtk/VTKOCCTool.java b/org.simantics.opencascade.vtk/src/org/simantics/opencascade/vtk/VTKOCCTool.java index 8f5078df..8f8c7b15 100644 --- a/org.simantics.opencascade.vtk/src/org/simantics/opencascade/vtk/VTKOCCTool.java +++ b/org.simantics.opencascade.vtk/src/org/simantics/opencascade/vtk/VTKOCCTool.java @@ -1,720 +1,720 @@ -/******************************************************************************* - * 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.opencascade.vtk; - -import java.util.List; - -import javax.vecmath.Matrix4d; -import javax.vecmath.Point3d; - -import org.jcae.opencascade.jni.BRepMesh_IncrementalMesh; -import org.jcae.opencascade.jni.BRep_Tool; -import org.jcae.opencascade.jni.GP_Trsf; -import org.jcae.opencascade.jni.Poly_Triangulation; -import org.jcae.opencascade.jni.TopAbs_Orientation; -import org.jcae.opencascade.jni.TopAbs_ShapeEnum; -import org.jcae.opencascade.jni.TopExp_Explorer; -import org.jcae.opencascade.jni.TopLoc_Location; -import org.jcae.opencascade.jni.TopoDS_Face; -import org.jcae.opencascade.jni.TopoDS_Shape; -import org.simantics.opencascade.OCCTTool; - -import vtk.vtkActor; -import vtk.vtkAlgorithmOutput; -import vtk.vtkAppendPolyData; -import vtk.vtkAssembly; -import vtk.vtkCleanPolyData; -import vtk.vtkDataSetMapper; -import vtk.vtkFeatureEdges; -import vtk.vtkGlyph3D; -import vtk.vtkIdList; -import vtk.vtkPoints; -import vtk.vtkPolyData; -import vtk.vtkPolyDataMapper; -import vtk.vtkPolyDataNormals; -import vtk.vtkProp; -import vtk.vtkPropCollection; -import vtk.vtkProperty; -import vtk.vtkSphereSource; -import vtk.vtkTriangle; - -public class VTKOCCTool { - public static vtkAssembly vtkTestAssembly() { - vtkAssembly assemblies = new vtkAssembly(); - vtkPolyData partGrid = createTestPartGrid(); - gridToAssembly(assemblies, partGrid); - return assemblies; - } - - public static vtkAssembly vtkOCCShapeToAssembly(TopoDS_Shape shape) { - double deflection = 0.001; - - if (deflection <= 0.0) { - deflection = 0.0005; - System.out.println("Bad value for deflection. Using: " + deflection); - } - - // FIXME : leaks memory! - //BRepTools.clean(shape); - - double mass = OCCTTool.getMass(shape); - - if (mass < 1.0e-12) { - System.out.println("Non 3D-shape detected"); - System.out.println("The cad import features are currently limited to 3D models."); - } - - double length = OCCTTool.getBoundingBoxDiagonal(shape); - deflection *= length; // use relative units - - BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(shape,deflection); - - int faceNumber = 0; - TopExp_Explorer expFace = new TopExp_Explorer(); - - vtkAssembly assemblies = new vtkAssembly(); - for (expFace.init(shape, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) { - TopoDS_Face face = (TopoDS_Face) expFace.current(); - vtkPolyData partGrid = createPartGrid(face); - face.delete(); - if (partGrid == null) - continue; - faceNumber++; - //gridToAssembly(assemblies, partGrid, stlSurfaceData, stlEdgeData); - gridToAssembly(assemblies, partGrid); - - } - expFace.delete(); - mesh.delete(); - - if (faceNumber == 0) { - System.out - .println("Cad import: error: no surface triangulation was generated."); - return null; - } - - return assemblies; - } - - private static void gridToAssembly(vtkAssembly assemblies, vtkPolyData partGrid, vtkAppendPolyData stlSurfaceData, vtkAppendPolyData stlEdgeData) { - - double featureAngle = 30; - - - vtkDataSetMapper partMapper = new vtkDataSetMapper(); - - boolean computeNormals = true; - boolean cleanPart = true; - boolean mergePoints = false; - - vtkCleanPolyData partCleaner = new vtkCleanPolyData(); - if (cleanPart) - { - partCleaner.SetInput(partGrid); - if(mergePoints) { - partCleaner.PointMergingOn(); - } else { - partCleaner.PointMergingOff(); - } - } - - if (computeNormals) - { - vtkPolyDataNormals partNormals = new vtkPolyDataNormals(); - - if (cleanPart) - { - partNormals.SetInputConnection(partCleaner.GetOutputPort()); - } - else partNormals.SetInput(partGrid); - - partNormals.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called - partMapper.SetInputConnection(partNormals.GetOutputPort()); - partNormals.Delete(); - } - else - { - if (cleanPart) partMapper.SetInputConnection(partCleaner.GetOutputPort()); // metoda 2, ne tak pekne, viz http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 - else partMapper.SetInput(partGrid); // metoda 1, ne tak pekne stinovani, viz: http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 - } - partMapper.ScalarVisibilityOn(); - - vtkActor partActor = new vtkActor(); - - partActor.SetPickable(1); - partActor.GetProperty().SetColor(1, 1, 0); - partActor.SetMapper(partMapper); - - // EDGES OF PARTS DETECTION - vtkFeatureEdges partEdges = new vtkFeatureEdges(); - if (cleanPart) partEdges.SetInputConnection(partCleaner.GetOutputPort()); - else partEdges.SetInput(partGrid); - partEdges.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called - partEdges.FeatureEdgesOn(); - partEdges.BoundaryEdgesOn(); - partEdges.NonManifoldEdgesOn(); - partEdges.ManifoldEdgesOn(); - - vtkDataSetMapper partEdgesMapper = new vtkDataSetMapper(); - partEdgesMapper.SetInputConnection(partEdges.GetOutputPort()); - partEdgesMapper.SetResolveCoincidentTopologyToPolygonOffset(); - partEdgesMapper.ScalarVisibilityOff(); - - vtkActor partEdgesActor = new vtkActor(); - partEdgesActor.SetPickable(0); - partEdgesActor.GetProperty().SetColor(1, 0, 1); - partEdgesActor.SetMapper(partEdgesMapper); - - - // Add triangles and edges to STL structures: - //-------------------------------------------- - if (cleanPart) stlSurfaceData.AddInput(partCleaner.GetOutput()); - else stlSurfaceData.AddInput(partGrid); - stlEdgeData.AddInput(partEdges.GetOutput()); - - assemblies.AddPart(partActor); - assemblies.AddPart(partEdgesActor); - - // Clean up: - //---------- - partEdgesActor.Delete(); - partEdgesMapper.Delete(); - partEdges.Delete(); - partActor.Delete(); - partMapper.Delete(); - partGrid.Delete(); - partCleaner.Delete(); - } - -public static void gridToAssembly(vtkAssembly assemblies, vtkPolyData partGrid) { - - double featureAngle = 30; - - - vtkDataSetMapper partMapper = new vtkDataSetMapper(); - - boolean computeNormals = true; - boolean cleanPart = false; - boolean mergePoints = false; - - vtkCleanPolyData partCleaner = new vtkCleanPolyData(); - if (cleanPart) - { - partCleaner.SetInput(partGrid); - if(mergePoints) { - partCleaner.PointMergingOn(); - } else { - partCleaner.PointMergingOff(); - } - } - - if (computeNormals) - { - vtkPolyDataNormals partNormals = new vtkPolyDataNormals(); - - if (cleanPart) - { - vtkAlgorithmOutput out = partCleaner.GetOutputPort(); - partNormals.SetInputConnection(out); - out.Delete(); - } - else partNormals.SetInput(partGrid); - - partNormals.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called - vtkAlgorithmOutput out = partNormals.GetOutputPort(); - partMapper.SetInputConnection(out); - out.Delete(); - partNormals.Delete(); - } - else - { - if (cleanPart) { - vtkAlgorithmOutput out = partCleaner.GetOutputPort(); - partMapper.SetInputConnection(out); // metoda 2, ne tak pekne, viz http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 - out.Delete(); - } - else partMapper.SetInput(partGrid); // metoda 1, ne tak pekne stinovani, viz: http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 - } - partMapper.ScalarVisibilityOn(); - - vtkActor partActor = new vtkActor(); - partActor.SetPickable(1); - vtkProperty prop = partActor.GetProperty(); - prop.SetColor(1, 1, 0); - prop.Delete(); - partActor.SetMapper(partMapper); - - assemblies.AddPart(partActor); - - { - // EDGES OF PARTS DETECTION - vtkFeatureEdges partEdges = new vtkFeatureEdges(); - if (cleanPart) { - vtkAlgorithmOutput out = partCleaner.GetOutputPort(); - partEdges.SetInputConnection(out); - out.Delete(); - } - else partEdges.SetInput(partGrid); - // partEdges.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called - partEdges.FeatureEdgesOn(); - partEdges.BoundaryEdgesOn(); - partEdges.NonManifoldEdgesOn(); - partEdges.ManifoldEdgesOn(); - - vtkDataSetMapper partEdgesMapper = new vtkDataSetMapper(); - vtkAlgorithmOutput out = partEdges.GetOutputPort(); - partEdgesMapper.SetInputConnection(out); - out.Delete(); - partEdgesMapper.SetResolveCoincidentTopologyToPolygonOffset(); - partEdgesMapper.ScalarVisibilityOff(); - - vtkActor partEdgesActor = new vtkActor(); - - prop = partEdgesActor.GetProperty(); - prop.SetColor(0, 0, 0); - prop.SetLineWidth(2.0); - prop.Delete(); - partEdgesActor.SetMapper(partEdgesMapper); - partEdgesActor.PickableOn(); - - assemblies.AddPart(partEdgesActor); - - { - - EdgePointsFilter edgePoints = new EdgePointsFilter(); - - - out = partEdges.GetOutputPort(); - edgePoints.SetInputConnection(out); - out.Delete(); - -// vtkDataSetMapper partEdgePointsMapper = new vtkDataSetMapper(); -// out = edgePoints.GetOutputPort(); -// partEdgePointsMapper.SetInputConnection(out); -// out.Delete(); - -// vtkVertexGlyphFilter glyphFilter = new vtkVertexGlyphFilter(); -// out = edgePoints.GetOutputPort(); -// glyphFilter.SetInputConnection(out); -// glyphFilter.Update(); -// out.Delete(); -// -// vtkPolyDataMapper partEdgePointsMapper = new vtkPolyDataMapper(); -// out = glyphFilter.GetOutputPort(); -// partEdgePointsMapper.SetInputConnection(out); -// out.Delete(); - - vtkSphereSource sphereSource = new vtkSphereSource(); - vtkGlyph3D glyph3D = new vtkGlyph3D(); - out = sphereSource.GetOutputPort(); - glyph3D.SetSourceConnection(out); - out.Delete(); - - out = edgePoints.GetOutputPort(); - glyph3D.SetInputConnection(out); - out.Delete(); - - //glyph3D.ScalingOff(); - glyph3D.SetScaleFactor(0.03); - - glyph3D.Update(); - - vtkPolyDataMapper partEdgePointsMapper = new vtkPolyDataMapper(); - out = glyph3D.GetOutputPort(); - partEdgePointsMapper.SetInputConnection(out); - out.Delete(); - - vtkActor edgePointsActor = new vtkActor(); - - prop = edgePointsActor.GetProperty(); - prop.SetColor(0, 0, 1); - //prop.SetPointSize(10.0); - //prop.SetRepresentationToPoints(); - prop.Delete(); - edgePointsActor.SetMapper(partEdgePointsMapper); - - edgePointsActor.PickableOn(); - assemblies.AddPart(edgePointsActor); - - - edgePointsActor.Delete(); - partEdgePointsMapper.Delete(); - // edgePoints.Delete(); - } - // Clean up: - //---------- - partEdgesActor.Delete(); - partEdgesMapper.Delete(); - partEdges.Delete(); - } - - partActor.Delete(); - partMapper.Delete(); - partGrid.Delete(); - partCleaner.Delete(); -} - - - - public static vtkPolyData createPartGrid ( TopoDS_Face face) - { - TopLoc_Location Location = new TopLoc_Location(); - - Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location); - - if(triangulation == null) { - Location.delete(); - System.out.println("Encountered empty triangulation after face"); - return null; - } - - boolean reverse = face.orientation()==TopAbs_Orientation.REVERSED; - - int[]triangles = triangulation.triangles(); - double[]nodes = triangulation.nodes(); - - int nofTriangles = triangulation.nbTriangles(); - int nofNodes = triangulation.nbNodes(); - - triangulation.delete(); - - if(nofTriangles < 1) { - System.out.println("No triangles for mesh on face"); - Location.delete(); - return null; - } - - if(nofNodes < 1) { - System.out.println("No nodes for mesh on face:"); - Location.delete(); - return null; - } - //System.out.println("v " + nofNodes + " t " +nofTriangles); - vtkPolyData partGrid = new vtkPolyData(); - partGrid.Allocate(nofTriangles, nofTriangles); - - vtkTriangle triangle = new vtkTriangle(); - vtkIdList list = triangle.GetPointIds(); - for(int i = 0; i < nofTriangles; i++) - { - int n0, n1, n2; - if (!reverse) { - n0 = triangles[3 * i]; - n1 = triangles[3 * i + 1]; - n2 = triangles[3 * i + 2]; - } else { - n0 = triangles[3 * i + 2]; - n1 = triangles[3 * i + 1]; - n2 = triangles[3 * i]; - } - - list.SetId(0, n0); - list.SetId(1, n1); - list.SetId(2, n2); - partGrid.InsertNextCell(triangle.GetCellType(), list); - - } - list.Delete(); - triangle.Delete(); - - GP_Trsf transformation = Location.transformation(); - Location.delete(); - - double d_mat[] = new double[16]; - double d_p[] = new double[3]; - transformation.getValues(d_mat); - Matrix4d mat = new Matrix4d(d_mat); - - vtkPoints partPoints = new vtkPoints(); - - for(int i = 0; i < nofNodes; i++) { - // FIXME: GP_Trsf.transform(double[]) leaks memory - - //double xyz[] = new double[]{nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]}; - //transformation.transforms(xyz); - //partPoints.InsertPoint(i, xyz); - Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]); - mat.transform(p); - d_p[0] = p.x; - d_p[1] = p.y; - d_p[2] = p.z; - partPoints.InsertPoint(i, d_p); - } - - transformation.delete(); - - partGrid.SetPoints(partPoints); - partPoints.Delete(); - - return partGrid; - } - - - - public static vtkPolyData createPartGrid ( List meshPoints, List meshTriangles) - { - - - - - - int nofTriangles = meshTriangles.size() / 3; - int nofNodes = meshPoints.size() /3; - - - if(nofTriangles < 1) { - System.out.println("No triangles for mesh on face"); - return null; - } - - if(nofNodes < 1) { - System.out.println("No nodes for mesh on face:"); - return null; - } - //System.out.println("v " + nofNodes + " t " +nofTriangles); - vtkPolyData partGrid = new vtkPolyData(); - partGrid.Allocate(nofTriangles, nofTriangles); - - vtkTriangle triangle = new vtkTriangle(); - vtkIdList list = triangle.GetPointIds(); - for(int i = 0; i < nofTriangles; i++) - { - int n0, n1, n2; - n0 = meshTriangles.get(3 * i); - n1 = meshTriangles.get(3 * i + 1); - n2 = meshTriangles.get(3 * i + 2); - - - list.SetId(0, n0); - list.SetId(1, n1); - list.SetId(2, n2); - partGrid.InsertNextCell(triangle.GetCellType(), list); - - } - list.Delete(); - triangle.Delete(); - - - - double d_p[] = new double[3]; - - - vtkPoints partPoints = new vtkPoints(); - - for(int i = 0; i < nofNodes; i++) { - - d_p[0] = meshPoints.get(3*i); - d_p[1] = meshPoints.get(3*i+1); - d_p[2] = meshPoints.get(3*i+2); - partPoints.InsertPoint(i, d_p); - } - - partGrid.SetPoints(partPoints); - partPoints.Delete(); - - return partGrid; - } - - private static vtkPolyData createTestPartGrid () - { - int size = 64; - - double[] nodes = new double[(size+1)*(size+1)*3]; - int[] triangles = new int[3 * size * size * 2]; - - double es = 1.0; - for (int i = 0; i <= size; i++) { - for (int j = 0; j <= size; j++) { - int index = j * size + i; - index *= 3; - double x = (double)i * es; - double y = (Math.sin((double)i/(double)size) + Math.sin((double)j/(double)size)) * es; - double z = (double)j * es; - nodes[index] = x; - nodes[index+1] = y; - nodes[index+2] = z; - } - } - - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - int index = j * size + i; - index *= 3; - index *= 2; - triangles[index ] = (j * (size+1) + i ); - triangles[index+1] = (j * (size+1) + i+1); - triangles[index+2] = ((j+1) * (size+1) + i ); - triangles[index+3] = ((j+1) * (size+1) + i ); - triangles[index+4] = ((j ) * (size+1) + i +1 ); - triangles[index+5] = ((j+1) * (size+1) + i +1 ); - } - } - - int nofTriangles = triangles.length / 3; - int nofNodes = nodes.length / 3; - - if(nofTriangles < 1) { - - return null; - } - - if(nofNodes < 1) { - - return null; - } - //System.out.println("v " + nofNodes + " t " +nofTriangles); - vtkPolyData partGrid = new vtkPolyData(); - partGrid.Allocate(nofTriangles, nofTriangles); - - vtkTriangle triangle = new vtkTriangle(); - vtkIdList list = triangle.GetPointIds(); - for(int i = 0; i < nofTriangles; i++) - { - int n0, n1, n2; - n0 = triangles[3 * i]; n1 = triangles[3 * i + 1]; n2 = triangles[3 * i + 2]; // triangles(i).Get(n0, n1, n2); - -// if(face.orientation() != TopAbs_Orientation.FORWARD) { -// int tmp = n2; n2 = n1; n1 = tmp; -// } - - list.SetId(0, n0); - list.SetId(1, n1); - list.SetId(2, n2); - partGrid.InsertNextCell(triangle.GetCellType(), list); - - } - list.Delete(); - triangle.Delete(); - - vtkPoints partPoints = new vtkPoints(); - for(int i = 0; i < nofNodes; i++) { - double xyz[] = new double[]{nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]}; - partPoints.InsertPoint(i, xyz); - } - - partGrid.SetPoints(partPoints); - - partPoints.Delete(); - - return partGrid; - } - - public static void test() { - TopoDS_Shape shape = null; - //shape = OccTriangulator.makeCylinder(new double[]{0,0,0}, new double[]{0,1,0}, 1, 1); - for (int t = 0; t < 5000; t++) { - //shape = OccTriangulator.makeCylinder(new double[]{0,0,0}, new double[]{0,1,0}, 1, 1); - int test = 2; - if (test == 0) { - vtkAssembly ass = VTKOCCTool.vtkOCCShapeToAssembly(shape); - vtkPropCollection col; - col = ass.GetParts(); - for (int i = 0; i < col.GetNumberOfItems(); i++) - { - vtkProp prop = (vtkProp) col.GetItemAsObject(i); - //System.out.println(prop.Print()); - prop.Delete(); - } - col.Delete(); - ass.Delete(); - } - else if (test == 1) { - //BRepTools.clean(shape); - - - vtkAssembly ass = new vtkAssembly(); - - double vol = OCCTTool.getBoundingBoxDiagonal(shape); - double d = 0.001 * vol; - BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(shape,d); - TopExp_Explorer expFace = new TopExp_Explorer(); - for (expFace.init(shape, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) { - TopoDS_Face face = (TopoDS_Face) expFace.current(); - { -// TopLoc_Location Location = new TopLoc_Location(); -// Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location); -// if (triangulation != null) { -// -// int[]triangles = triangulation.triangles(); -// double[]nodes = triangulation.nodes(); -// int nofTriangles = triangulation.nbTriangles(); -// int nofNodes = triangulation.nbNodes(); -// -// triangulation.delete(); -// -// GP_Trsf transformation = Location.transformation(); -// double d_mat[] = new double[16]; -// transformation.getValues(d_mat); -// Matrix4d mat = new Matrix4d(d_mat); -// for(int i = 0; i < nofNodes; i++) { -// //double xyz[] = new double[]{nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]}; -// Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]); -// //transformation.transforms(xyz); -// mat.transform(p); -// } -// transformation.delete(); -// } -// -// Location.delete(); - vtkPolyData data = VTKOCCTool.createPartGrid(face); - VTKOCCTool.gridToAssembly(ass, data); - //data.Delete(); - } - face.delete(); - } - expFace.delete(); - mesh.delete(); - - vtkPropCollection col; - col = ass.GetParts(); - for (int i = 0; i < col.GetNumberOfItems(); i++) - { - vtkProp prop = (vtkProp) col.GetItemAsObject(i); - //System.out.println(prop.Print()); - prop.Delete(); - } - col.Delete(); - ass.Delete(); - } else if (test == 2) { - double[] pointStruct = new double[]{0,0,0}, dirStruct = new double[]{0,1,0}; - double radius = 1.0; - double height = 1.0; - - double[] axe = new double[6]; - - System.arraycopy(pointStruct, 0, axe, 0, 3); - System.arraycopy(dirStruct, 0, axe, 3, 3); - org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder cyl = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder(axe, radius, height, 2 * Math.PI); - org.jcae.opencascade.jni.TopoDS_Shape tds = cyl.shape(); - cyl.delete(); - - double vol = OCCTTool.getBoundingBoxDiagonal(tds); - double d = 0.001 * vol; - BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(tds,d); -// TopExp_Explorer expFace = new TopExp_Explorer(); -// for (expFace.init(tds, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) { -// TopoDS_Face face = (TopoDS_Face) expFace.current(); -// { -// -// } -// face.delete(); -// } - mesh.delete(); - - tds.delete(); - } - //shape.delete(); - System.out.println(t); - } - //shape.delete(); - } -} +/******************************************************************************* + * 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.opencascade.vtk; + +import java.util.List; + +import javax.vecmath.Matrix4d; +import javax.vecmath.Point3d; + +import org.jcae.opencascade.jni.BRepMesh_IncrementalMesh; +import org.jcae.opencascade.jni.BRep_Tool; +import org.jcae.opencascade.jni.GP_Trsf; +import org.jcae.opencascade.jni.Poly_Triangulation; +import org.jcae.opencascade.jni.TopAbs_Orientation; +import org.jcae.opencascade.jni.TopAbs_ShapeEnum; +import org.jcae.opencascade.jni.TopExp_Explorer; +import org.jcae.opencascade.jni.TopLoc_Location; +import org.jcae.opencascade.jni.TopoDS_Face; +import org.jcae.opencascade.jni.TopoDS_Shape; +import org.simantics.opencascade.OCCTTool; + +import vtk.vtkActor; +import vtk.vtkAlgorithmOutput; +import vtk.vtkAppendPolyData; +import vtk.vtkAssembly; +import vtk.vtkCleanPolyData; +import vtk.vtkDataSetMapper; +import vtk.vtkFeatureEdges; +import vtk.vtkGlyph3D; +import vtk.vtkIdList; +import vtk.vtkPoints; +import vtk.vtkPolyData; +import vtk.vtkPolyDataMapper; +import vtk.vtkPolyDataNormals; +import vtk.vtkProp; +import vtk.vtkPropCollection; +import vtk.vtkProperty; +import vtk.vtkSphereSource; +import vtk.vtkTriangle; + +public class VTKOCCTool { + public static vtkAssembly vtkTestAssembly() { + vtkAssembly assemblies = new vtkAssembly(); + vtkPolyData partGrid = createTestPartGrid(); + gridToAssembly(assemblies, partGrid); + return assemblies; + } + + public static vtkAssembly vtkOCCShapeToAssembly(TopoDS_Shape shape) { + double deflection = 0.001; + + if (deflection <= 0.0) { + deflection = 0.0005; + System.out.println("Bad value for deflection. Using: " + deflection); + } + + // FIXME : leaks memory! + //BRepTools.clean(shape); + + double mass = OCCTTool.getMass(shape); + + if (mass < 1.0e-12) { + System.out.println("Non 3D-shape detected"); + System.out.println("The cad import features are currently limited to 3D models."); + } + + double length = OCCTTool.getBoundingBoxDiagonal(shape); + deflection *= length; // use relative units + + BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(shape,deflection); + + int faceNumber = 0; + TopExp_Explorer expFace = new TopExp_Explorer(); + + vtkAssembly assemblies = new vtkAssembly(); + for (expFace.init(shape, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) { + TopoDS_Face face = (TopoDS_Face) expFace.current(); + vtkPolyData partGrid = createPartGrid(face); + face.delete(); + if (partGrid == null) + continue; + faceNumber++; + //gridToAssembly(assemblies, partGrid, stlSurfaceData, stlEdgeData); + gridToAssembly(assemblies, partGrid); + + } + expFace.delete(); + mesh.delete(); + + if (faceNumber == 0) { + System.out + .println("Cad import: error: no surface triangulation was generated."); + return null; + } + + return assemblies; + } + + private static void gridToAssembly(vtkAssembly assemblies, vtkPolyData partGrid, vtkAppendPolyData stlSurfaceData, vtkAppendPolyData stlEdgeData) { + + double featureAngle = 30; + + + vtkDataSetMapper partMapper = new vtkDataSetMapper(); + + boolean computeNormals = true; + boolean cleanPart = true; + boolean mergePoints = false; + + vtkCleanPolyData partCleaner = new vtkCleanPolyData(); + if (cleanPart) + { + partCleaner.SetInputData(partGrid); + if(mergePoints) { + partCleaner.PointMergingOn(); + } else { + partCleaner.PointMergingOff(); + } + } + + if (computeNormals) + { + vtkPolyDataNormals partNormals = new vtkPolyDataNormals(); + + if (cleanPart) + { + partNormals.SetInputConnection(partCleaner.GetOutputPort()); + } + else partNormals.SetInputData(partGrid); + + partNormals.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called + partMapper.SetInputConnection(partNormals.GetOutputPort()); + partNormals.Delete(); + } + else + { + if (cleanPart) partMapper.SetInputConnection(partCleaner.GetOutputPort()); // metoda 2, ne tak pekne, viz http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 + else partMapper.SetInputData(partGrid); // metoda 1, ne tak pekne stinovani, viz: http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 + } + partMapper.ScalarVisibilityOn(); + + vtkActor partActor = new vtkActor(); + + partActor.SetPickable(1); + partActor.GetProperty().SetColor(1, 1, 0); + partActor.SetMapper(partMapper); + + // EDGES OF PARTS DETECTION + vtkFeatureEdges partEdges = new vtkFeatureEdges(); + if (cleanPart) partEdges.SetInputConnection(partCleaner.GetOutputPort()); + else partEdges.SetInputData(partGrid); + partEdges.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called + partEdges.FeatureEdgesOn(); + partEdges.BoundaryEdgesOn(); + partEdges.NonManifoldEdgesOn(); + partEdges.ManifoldEdgesOn(); + + vtkDataSetMapper partEdgesMapper = new vtkDataSetMapper(); + partEdgesMapper.SetInputConnection(partEdges.GetOutputPort()); + partEdgesMapper.SetResolveCoincidentTopologyToPolygonOffset(); + partEdgesMapper.ScalarVisibilityOff(); + + vtkActor partEdgesActor = new vtkActor(); + partEdgesActor.SetPickable(0); + partEdgesActor.GetProperty().SetColor(1, 0, 1); + partEdgesActor.SetMapper(partEdgesMapper); + + + // Add triangles and edges to STL structures: + //-------------------------------------------- + if (cleanPart) stlSurfaceData.AddInputData(partCleaner.GetOutput()); + else stlSurfaceData.AddInputData(partGrid); + stlEdgeData.AddInputData(partEdges.GetOutput()); + + assemblies.AddPart(partActor); + assemblies.AddPart(partEdgesActor); + + // Clean up: + //---------- + partEdgesActor.Delete(); + partEdgesMapper.Delete(); + partEdges.Delete(); + partActor.Delete(); + partMapper.Delete(); + partGrid.Delete(); + partCleaner.Delete(); + } + +public static void gridToAssembly(vtkAssembly assemblies, vtkPolyData partGrid) { + + double featureAngle = 30; + + + vtkDataSetMapper partMapper = new vtkDataSetMapper(); + + boolean computeNormals = true; + boolean cleanPart = false; + boolean mergePoints = false; + + vtkCleanPolyData partCleaner = new vtkCleanPolyData(); + if (cleanPart) + { + partCleaner.SetInputData(partGrid); + if(mergePoints) { + partCleaner.PointMergingOn(); + } else { + partCleaner.PointMergingOff(); + } + } + + if (computeNormals) + { + vtkPolyDataNormals partNormals = new vtkPolyDataNormals(); + + if (cleanPart) + { + vtkAlgorithmOutput out = partCleaner.GetOutputPort(); + partNormals.SetInputConnection(out); + out.Delete(); + } + else partNormals.SetInputData(partGrid); + + partNormals.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called + vtkAlgorithmOutput out = partNormals.GetOutputPort(); + partMapper.SetInputConnection(out); + out.Delete(); + partNormals.Delete(); + } + else + { + if (cleanPart) { + vtkAlgorithmOutput out = partCleaner.GetOutputPort(); + partMapper.SetInputConnection(out); // metoda 2, ne tak pekne, viz http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 + out.Delete(); + } + else partMapper.SetInputData(partGrid); // metoda 1, ne tak pekne stinovani, viz: http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 + } + partMapper.ScalarVisibilityOn(); + + vtkActor partActor = new vtkActor(); + partActor.SetPickable(1); + vtkProperty prop = partActor.GetProperty(); + prop.SetColor(1, 1, 0); + prop.Delete(); + partActor.SetMapper(partMapper); + + assemblies.AddPart(partActor); + + { + // EDGES OF PARTS DETECTION + vtkFeatureEdges partEdges = new vtkFeatureEdges(); + if (cleanPart) { + vtkAlgorithmOutput out = partCleaner.GetOutputPort(); + partEdges.SetInputConnection(out); + out.Delete(); + } + else partEdges.SetInputData(partGrid); + // partEdges.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called + partEdges.FeatureEdgesOn(); + partEdges.BoundaryEdgesOn(); + partEdges.NonManifoldEdgesOn(); + partEdges.ManifoldEdgesOn(); + + vtkDataSetMapper partEdgesMapper = new vtkDataSetMapper(); + vtkAlgorithmOutput out = partEdges.GetOutputPort(); + partEdgesMapper.SetInputConnection(out); + out.Delete(); + partEdgesMapper.SetResolveCoincidentTopologyToPolygonOffset(); + partEdgesMapper.ScalarVisibilityOff(); + + vtkActor partEdgesActor = new vtkActor(); + + prop = partEdgesActor.GetProperty(); + prop.SetColor(0, 0, 0); + prop.SetLineWidth(2.0); + prop.Delete(); + partEdgesActor.SetMapper(partEdgesMapper); + partEdgesActor.PickableOn(); + + assemblies.AddPart(partEdgesActor); + + { + + EdgePointsFilter edgePoints = new EdgePointsFilter(); + + + out = partEdges.GetOutputPort(); + edgePoints.SetInputConnection(out); + out.Delete(); + +// vtkDataSetMapper partEdgePointsMapper = new vtkDataSetMapper(); +// out = edgePoints.GetOutputPort(); +// partEdgePointsMapper.SetInputConnection(out); +// out.Delete(); + +// vtkVertexGlyphFilter glyphFilter = new vtkVertexGlyphFilter(); +// out = edgePoints.GetOutputPort(); +// glyphFilter.SetInputConnection(out); +// glyphFilter.Update(); +// out.Delete(); +// +// vtkPolyDataMapper partEdgePointsMapper = new vtkPolyDataMapper(); +// out = glyphFilter.GetOutputPort(); +// partEdgePointsMapper.SetInputConnection(out); +// out.Delete(); + + vtkSphereSource sphereSource = new vtkSphereSource(); + vtkGlyph3D glyph3D = new vtkGlyph3D(); + out = sphereSource.GetOutputPort(); + glyph3D.SetSourceConnection(out); + out.Delete(); + + out = edgePoints.GetOutputPort(); + glyph3D.SetInputConnection(out); + out.Delete(); + + //glyph3D.ScalingOff(); + glyph3D.SetScaleFactor(0.03); + + glyph3D.Update(); + + vtkPolyDataMapper partEdgePointsMapper = new vtkPolyDataMapper(); + out = glyph3D.GetOutputPort(); + partEdgePointsMapper.SetInputConnection(out); + out.Delete(); + + vtkActor edgePointsActor = new vtkActor(); + + prop = edgePointsActor.GetProperty(); + prop.SetColor(0, 0, 1); + //prop.SetPointSize(10.0); + //prop.SetRepresentationToPoints(); + prop.Delete(); + edgePointsActor.SetMapper(partEdgePointsMapper); + + edgePointsActor.PickableOn(); + assemblies.AddPart(edgePointsActor); + + + edgePointsActor.Delete(); + partEdgePointsMapper.Delete(); + // edgePoints.Delete(); + } + // Clean up: + //---------- + partEdgesActor.Delete(); + partEdgesMapper.Delete(); + partEdges.Delete(); + } + + partActor.Delete(); + partMapper.Delete(); + partGrid.Delete(); + partCleaner.Delete(); +} + + + + public static vtkPolyData createPartGrid ( TopoDS_Face face) + { + TopLoc_Location Location = new TopLoc_Location(); + + Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location); + + if(triangulation == null) { + Location.delete(); + System.out.println("Encountered empty triangulation after face"); + return null; + } + + boolean reverse = face.orientation()==TopAbs_Orientation.REVERSED; + + int[]triangles = triangulation.triangles(); + double[]nodes = triangulation.nodes(); + + int nofTriangles = triangulation.nbTriangles(); + int nofNodes = triangulation.nbNodes(); + + triangulation.delete(); + + if(nofTriangles < 1) { + System.out.println("No triangles for mesh on face"); + Location.delete(); + return null; + } + + if(nofNodes < 1) { + System.out.println("No nodes for mesh on face:"); + Location.delete(); + return null; + } + //System.out.println("v " + nofNodes + " t " +nofTriangles); + vtkPolyData partGrid = new vtkPolyData(); + partGrid.Allocate(nofTriangles, nofTriangles); + + vtkTriangle triangle = new vtkTriangle(); + vtkIdList list = triangle.GetPointIds(); + for(int i = 0; i < nofTriangles; i++) + { + int n0, n1, n2; + if (!reverse) { + n0 = triangles[3 * i]; + n1 = triangles[3 * i + 1]; + n2 = triangles[3 * i + 2]; + } else { + n0 = triangles[3 * i + 2]; + n1 = triangles[3 * i + 1]; + n2 = triangles[3 * i]; + } + + list.SetId(0, n0); + list.SetId(1, n1); + list.SetId(2, n2); + partGrid.InsertNextCell(triangle.GetCellType(), list); + + } + list.Delete(); + triangle.Delete(); + + GP_Trsf transformation = Location.transformation(); + Location.delete(); + + double d_mat[] = new double[16]; + double d_p[] = new double[3]; + transformation.getValues(d_mat); + Matrix4d mat = new Matrix4d(d_mat); + + vtkPoints partPoints = new vtkPoints(); + + for(int i = 0; i < nofNodes; i++) { + // FIXME: GP_Trsf.transform(double[]) leaks memory + + //double xyz[] = new double[]{nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]}; + //transformation.transforms(xyz); + //partPoints.InsertPoint(i, xyz); + Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]); + mat.transform(p); + d_p[0] = p.x; + d_p[1] = p.y; + d_p[2] = p.z; + partPoints.InsertPoint(i, d_p); + } + + transformation.delete(); + + partGrid.SetPoints(partPoints); + partPoints.Delete(); + + return partGrid; + } + + + + public static vtkPolyData createPartGrid ( List meshPoints, List meshTriangles) + { + + + + + + int nofTriangles = meshTriangles.size() / 3; + int nofNodes = meshPoints.size() /3; + + + if(nofTriangles < 1) { + System.out.println("No triangles for mesh on face"); + return null; + } + + if(nofNodes < 1) { + System.out.println("No nodes for mesh on face:"); + return null; + } + //System.out.println("v " + nofNodes + " t " +nofTriangles); + vtkPolyData partGrid = new vtkPolyData(); + partGrid.Allocate(nofTriangles, nofTriangles); + + vtkTriangle triangle = new vtkTriangle(); + vtkIdList list = triangle.GetPointIds(); + for(int i = 0; i < nofTriangles; i++) + { + int n0, n1, n2; + n0 = meshTriangles.get(3 * i); + n1 = meshTriangles.get(3 * i + 1); + n2 = meshTriangles.get(3 * i + 2); + + + list.SetId(0, n0); + list.SetId(1, n1); + list.SetId(2, n2); + partGrid.InsertNextCell(triangle.GetCellType(), list); + + } + list.Delete(); + triangle.Delete(); + + + + double d_p[] = new double[3]; + + + vtkPoints partPoints = new vtkPoints(); + + for(int i = 0; i < nofNodes; i++) { + + d_p[0] = meshPoints.get(3*i); + d_p[1] = meshPoints.get(3*i+1); + d_p[2] = meshPoints.get(3*i+2); + partPoints.InsertPoint(i, d_p); + } + + partGrid.SetPoints(partPoints); + partPoints.Delete(); + + return partGrid; + } + + private static vtkPolyData createTestPartGrid () + { + int size = 64; + + double[] nodes = new double[(size+1)*(size+1)*3]; + int[] triangles = new int[3 * size * size * 2]; + + double es = 1.0; + for (int i = 0; i <= size; i++) { + for (int j = 0; j <= size; j++) { + int index = j * size + i; + index *= 3; + double x = (double)i * es; + double y = (Math.sin((double)i/(double)size) + Math.sin((double)j/(double)size)) * es; + double z = (double)j * es; + nodes[index] = x; + nodes[index+1] = y; + nodes[index+2] = z; + } + } + + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + int index = j * size + i; + index *= 3; + index *= 2; + triangles[index ] = (j * (size+1) + i ); + triangles[index+1] = (j * (size+1) + i+1); + triangles[index+2] = ((j+1) * (size+1) + i ); + triangles[index+3] = ((j+1) * (size+1) + i ); + triangles[index+4] = ((j ) * (size+1) + i +1 ); + triangles[index+5] = ((j+1) * (size+1) + i +1 ); + } + } + + int nofTriangles = triangles.length / 3; + int nofNodes = nodes.length / 3; + + if(nofTriangles < 1) { + + return null; + } + + if(nofNodes < 1) { + + return null; + } + //System.out.println("v " + nofNodes + " t " +nofTriangles); + vtkPolyData partGrid = new vtkPolyData(); + partGrid.Allocate(nofTriangles, nofTriangles); + + vtkTriangle triangle = new vtkTriangle(); + vtkIdList list = triangle.GetPointIds(); + for(int i = 0; i < nofTriangles; i++) + { + int n0, n1, n2; + n0 = triangles[3 * i]; n1 = triangles[3 * i + 1]; n2 = triangles[3 * i + 2]; // triangles(i).Get(n0, n1, n2); + +// if(face.orientation() != TopAbs_Orientation.FORWARD) { +// int tmp = n2; n2 = n1; n1 = tmp; +// } + + list.SetId(0, n0); + list.SetId(1, n1); + list.SetId(2, n2); + partGrid.InsertNextCell(triangle.GetCellType(), list); + + } + list.Delete(); + triangle.Delete(); + + vtkPoints partPoints = new vtkPoints(); + for(int i = 0; i < nofNodes; i++) { + double xyz[] = new double[]{nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]}; + partPoints.InsertPoint(i, xyz); + } + + partGrid.SetPoints(partPoints); + + partPoints.Delete(); + + return partGrid; + } + + public static void test() { + TopoDS_Shape shape = null; + //shape = OccTriangulator.makeCylinder(new double[]{0,0,0}, new double[]{0,1,0}, 1, 1); + for (int t = 0; t < 5000; t++) { + //shape = OccTriangulator.makeCylinder(new double[]{0,0,0}, new double[]{0,1,0}, 1, 1); + int test = 2; + if (test == 0) { + vtkAssembly ass = VTKOCCTool.vtkOCCShapeToAssembly(shape); + vtkPropCollection col; + col = ass.GetParts(); + for (int i = 0; i < col.GetNumberOfItems(); i++) + { + vtkProp prop = (vtkProp) col.GetItemAsObject(i); + //System.out.println(prop.Print()); + prop.Delete(); + } + col.Delete(); + ass.Delete(); + } + else if (test == 1) { + //BRepTools.clean(shape); + + + vtkAssembly ass = new vtkAssembly(); + + double vol = OCCTTool.getBoundingBoxDiagonal(shape); + double d = 0.001 * vol; + BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(shape,d); + TopExp_Explorer expFace = new TopExp_Explorer(); + for (expFace.init(shape, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) { + TopoDS_Face face = (TopoDS_Face) expFace.current(); + { +// TopLoc_Location Location = new TopLoc_Location(); +// Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location); +// if (triangulation != null) { +// +// int[]triangles = triangulation.triangles(); +// double[]nodes = triangulation.nodes(); +// int nofTriangles = triangulation.nbTriangles(); +// int nofNodes = triangulation.nbNodes(); +// +// triangulation.delete(); +// +// GP_Trsf transformation = Location.transformation(); +// double d_mat[] = new double[16]; +// transformation.getValues(d_mat); +// Matrix4d mat = new Matrix4d(d_mat); +// for(int i = 0; i < nofNodes; i++) { +// //double xyz[] = new double[]{nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]}; +// Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]); +// //transformation.transforms(xyz); +// mat.transform(p); +// } +// transformation.delete(); +// } +// +// Location.delete(); + vtkPolyData data = VTKOCCTool.createPartGrid(face); + VTKOCCTool.gridToAssembly(ass, data); + //data.Delete(); + } + face.delete(); + } + expFace.delete(); + mesh.delete(); + + vtkPropCollection col; + col = ass.GetParts(); + for (int i = 0; i < col.GetNumberOfItems(); i++) + { + vtkProp prop = (vtkProp) col.GetItemAsObject(i); + //System.out.println(prop.Print()); + prop.Delete(); + } + col.Delete(); + ass.Delete(); + } else if (test == 2) { + double[] pointStruct = new double[]{0,0,0}, dirStruct = new double[]{0,1,0}; + double radius = 1.0; + double height = 1.0; + + double[] axe = new double[6]; + + System.arraycopy(pointStruct, 0, axe, 0, 3); + System.arraycopy(dirStruct, 0, axe, 3, 3); + org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder cyl = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder(axe, radius, height, 2 * Math.PI); + org.jcae.opencascade.jni.TopoDS_Shape tds = cyl.shape(); + cyl.delete(); + + double vol = OCCTTool.getBoundingBoxDiagonal(tds); + double d = 0.001 * vol; + BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(tds,d); +// TopExp_Explorer expFace = new TopExp_Explorer(); +// for (expFace.init(tds, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) { +// TopoDS_Face face = (TopoDS_Face) expFace.current(); +// { +// +// } +// face.delete(); +// } + mesh.delete(); + + tds.delete(); + } + //shape.delete(); + System.out.println(t); + } + //shape.delete(); + } +} diff --git a/org.simantics.opencascade.vtk/src/org/simantics/opencascade/vtk/vtkSolidObject.java b/org.simantics.opencascade.vtk/src/org/simantics/opencascade/vtk/vtkSolidObject.java index 6e1e3dd1..fd2c0bb1 100644 --- a/org.simantics.opencascade.vtk/src/org/simantics/opencascade/vtk/vtkSolidObject.java +++ b/org.simantics.opencascade.vtk/src/org/simantics/opencascade/vtk/vtkSolidObject.java @@ -1,497 +1,497 @@ -/******************************************************************************* - * 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.opencascade.vtk; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.jcae.opencascade.jni.BRepMesh_IncrementalMesh; -import org.jcae.opencascade.jni.TopAbs_ShapeEnum; -import org.jcae.opencascade.jni.TopExp_Explorer; -import org.jcae.opencascade.jni.TopoDS_Face; -import org.jcae.opencascade.jni.TopoDS_Shape; -import org.simantics.opencascade.OCCTTool; -import org.simantics.utils.datastructures.Pair; -import org.simantics.utils.threads.AWTThread; -import org.simantics.utils.threads.ThreadUtils; - -import vtk.vtkActor; -import vtk.vtkAlgorithmOutput; -import vtk.vtkCleanPolyData; -import vtk.vtkDataSetMapper; -import vtk.vtkFeatureEdges; -import vtk.vtkGlyph3D; -import vtk.vtkPanel; -import vtk.vtkPolyData; -import vtk.vtkPolyDataMapper; -import vtk.vtkPolyDataNormals; -import vtk.vtkPolyDataSilhouette; -import vtk.vtkProp3D; -import vtk.vtkProperty; -import vtk.vtkRenderer; -import vtk.vtkSphereSource; - -public class vtkSolidObject { - - public static double deflection = 0.001; - - public static double featureAngle = 30; - public static boolean computeNormals = true; - public static boolean cleanPart = false; - public static boolean mergePoints = false; - - private vtkPanel panel; - private TopoDS_Shape shape; - - private List actors = new ArrayList(2); - - private List solid = new ArrayList(1); - private List edges = new ArrayList(1); - private vtkActor silhouette = null; - - private boolean errors = false; - - public vtkSolidObject(vtkPanel panel,TopoDS_Shape shape) { - this.shape = shape; - this.panel = panel; - } - - public void visualizeSolid(boolean showEdges, boolean showVertices) { - visualizeSolid(true, showEdges, showVertices); - } - - public void visualizeSolid(boolean showFaces, boolean showEdges, boolean showVertices) { - visualizeSolid(showFaces, showEdges, showVertices, false); - } - - public void visualizeSolid(boolean showFaces, boolean showEdges, boolean showVertices, boolean showSilhouette) { - clearActorsAWT(); - errors = false; - Pair res = createSolidMesh(shape); - if (res == null) { - errors = true; - return; - } - vtkPolyData data = res.first; - if (!res.second) - errors = true; - if (showFaces) { - solid.add(createActor(data)); - } - if (showEdges) { - vtkActor edge = createEdgesActor(data); - edges.add(edge); - if (showVertices) { - actors.add(createVerticesActor(edge)); - } - } - if (showSilhouette) { - silhouette = createSilhouette(panel.GetRenderer(), data); - } - actors.addAll(solid); - actors.addAll(edges); - if (silhouette != null) - actors.add(silhouette); - data.Delete(); - showActorsAWT(); - } - - public void visualizeFaces(boolean showEdges, boolean showVertices) { - errors = false; - clearActorsAWT(); - Collection datas = createFaceMeshes(shape); - for (vtkPolyData data : datas) { - if (data == null) { - errors = true; - continue; - } - - solid.add(createActor(data)); - - if (showEdges) { - vtkActor edgesActor = createEdgesActor(data); - edges.add(edgesActor); - if (showVertices) { - actors.add(createVerticesActor(edgesActor)); - } - } - data.Delete(); - } - actors.addAll(solid); - actors.addAll(edges); - - showActorsAWT(); - } - - public boolean hasErrors() { - return errors; - } - - public List getActors() { - assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); - return actors; - } - - public List getSolid() { - assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); - return solid; - } - - public List getEdges() { - assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); - return edges; - } - - public vtkActor getSilhouette() { - assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); - return silhouette; - } - - public void showActorsAWT() { - assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); - vtkRenderer ren = panel.GetRenderer(); - for (vtkProp3D act : actors) { - ren.AddActor(act); - } - } - - public void showActors() { - ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { - - @Override - public void run() { - showActorsAWT(); - } - }); - } - - public void clearActorsAWT() { - assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); - if (actors.size() == 0) - return; - vtkRenderer ren = panel.GetRenderer(); - for (vtkProp3D act : actors) { - if (act.GetVTKId() != 0) { - ren.RemoveActor(act); - act.Delete(); - } - } - actors.clear(); - solid.clear(); - edges.clear(); - } - - private void clearActorsAWT(List actors) { - assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); - - if (actors.size() == 0) - return; - vtkRenderer ren = panel.GetRenderer(); - panel.lock(); - for (vtkProp3D act : actors) { - if (act.GetVTKId() != 0) { - ren.RemoveActor(act); - act.Delete(); - } - } - panel.unlock(); - } - - public void clearActors() { - if (actors.size() == 0) - return; - final List temp = new ArrayList(actors.size()); - temp.addAll(actors); - actors.clear(); - solid.clear(); - edges.clear(); - ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { - - @Override - public void run() { - clearActorsAWT(temp); - } - }); - } - - public void dispose() { - if (shape != null) { - shape.delete(); - shape = null; - } - clearActors(); - } - - public void delete() { - if (shape != null) { - shape.delete(); - shape = null; - } - clearActorsAWT(); - } - - private static double TOLERANCE = 0.01; - - public static Pair createSolidMesh(TopoDS_Shape shape) { - - boolean success = true; - - double volume = OCCTTool.getBoundingBoxDiagonal(shape); - if (volume < TOLERANCE) - return null; - - BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(shape,deflection*volume); - - TopExp_Explorer expFace = new TopExp_Explorer(); - - List meshPoints = new ArrayList(); - List meshTriangles = new ArrayList(); - for (expFace.init(shape, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) { - TopoDS_Face face = (TopoDS_Face) expFace.current(); - if (!OCCTTool.appendToMesh(face, meshPoints, meshTriangles)) - success = false; - face.delete(); - } - if (meshPoints.size() == 0 || meshTriangles.size() == 0) - return null; - - vtkPolyData data = VTKOCCTool.createPartGrid(meshPoints, meshTriangles); - - expFace.delete(); - mesh.delete(); - - return new Pair(data, success); - } - - public static Collection createFaceMeshes(TopoDS_Shape shape) { - - double volume = OCCTTool.getBoundingBoxDiagonal(shape); - Collection faces = new ArrayList(); - - if (volume > TOLERANCE) { - - BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(shape,deflection*volume); - - TopExp_Explorer expFace = new TopExp_Explorer(); - - - for (expFace.init(shape, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) { - TopoDS_Face face = (TopoDS_Face) expFace.current(); - vtkPolyData data = VTKOCCTool.createPartGrid(face); - face.delete(); - faces.add(data); - } - - expFace.delete(); - mesh.delete(); - } - - return faces; - } - - - - public static vtkActor createActor(vtkPolyData partGrid) { - - - vtkDataSetMapper partMapper = new vtkDataSetMapper(); - - vtkCleanPolyData partCleaner = null; - if (cleanPart) - { - partCleaner = new vtkCleanPolyData(); - partCleaner.SetInput(partGrid); - if(mergePoints) { - partCleaner.PointMergingOn(); - } else { - partCleaner.PointMergingOff(); - } - } - - if (computeNormals) - { - vtkPolyDataNormals partNormals = new vtkPolyDataNormals(); - - if (cleanPart) - { - vtkAlgorithmOutput out = partCleaner.GetOutputPort(); - partNormals.SetInputConnection(out); - out.Delete(); - } - else partNormals.SetInput(partGrid); - - partNormals.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called - vtkAlgorithmOutput out = partNormals.GetOutputPort(); - partMapper.SetInputConnection(out); - out.Delete(); - partNormals.Delete(); - } - else - { - if (cleanPart) { - vtkAlgorithmOutput out = partCleaner.GetOutputPort(); - partMapper.SetInputConnection(out); // metoda 2, ne tak pekne, viz http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 - out.Delete(); - } - else partMapper.SetInput(partGrid); // metoda 1, ne tak pekne stinovani, viz: http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 - } - partMapper.ScalarVisibilityOn(); - - vtkActor partActor = new vtkActor(); - // partActor.SetPickable(1); - vtkProperty prop = partActor.GetProperty(); - prop.SetColor(1, 1, 0); - prop.Delete(); - partActor.SetMapper(partMapper); - - partMapper.Delete(); - - if (cleanPart) - partCleaner.Delete(); - - return partActor; - } - - public static vtkActor createEdgesActor(vtkPolyData partGrid) { - vtkCleanPolyData partCleaner = null; - - if (cleanPart) - { - partCleaner = new vtkCleanPolyData(); - partCleaner.SetInput(partGrid); - if(mergePoints) { - partCleaner.PointMergingOn(); - } else { - partCleaner.PointMergingOff(); - } - } - - vtkFeatureEdges partEdges = new vtkFeatureEdges(); - if (cleanPart) { - vtkAlgorithmOutput out = partCleaner.GetOutputPort(); - partEdges.SetInputConnection(out); - out.Delete(); - } - else partEdges.SetInput(partGrid); - // partEdges.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called - partEdges.FeatureEdgesOn(); - partEdges.BoundaryEdgesOn(); - partEdges.NonManifoldEdgesOn(); - partEdges.ManifoldEdgesOn(); - - vtkDataSetMapper partEdgesMapper = new vtkDataSetMapper(); - vtkAlgorithmOutput out = partEdges.GetOutputPort(); - partEdgesMapper.SetInputConnection(out); - out.Delete(); - partEdgesMapper.SetResolveCoincidentTopologyToPolygonOffset(); - partEdgesMapper.ScalarVisibilityOff(); - - vtkActor partEdgesActor = new vtkActor(); - - vtkProperty prop = partEdgesActor.GetProperty(); - prop.SetColor(0, 0, 0); - prop.SetLineWidth(2.0); - prop.Delete(); - partEdgesActor.SetMapper(partEdgesMapper); - - partEdgesMapper.Delete(); - partEdges.Delete(); - - if (cleanPart) - partCleaner.Delete(); - - return partEdgesActor; - } - - public static vtkActor createVerticesActor(vtkActor partEdgesActor) { - vtkDataSetMapper partEdgesMapper = (vtkDataSetMapper) partEdgesActor.GetMapper(); - vtkAlgorithmOutput out = partEdgesMapper.GetInputConnection(0, 0); - vtkFeatureEdges partEdges = (vtkFeatureEdges)out.GetProducer(); - - vtkActor edgePointsActor = createVerticesActor(partEdges); - - partEdges.Delete(); - //out.Delete(); - partEdgesMapper.Delete(); - return edgePointsActor; - } - - public static vtkActor createVerticesActor(vtkFeatureEdges partEdges) { - EdgePointsFilter edgePoints = new EdgePointsFilter(); - - - vtkAlgorithmOutput out = partEdges.GetOutputPort(); - edgePoints.SetInputConnection(out); - out.Delete(); - - vtkSphereSource sphereSource = new vtkSphereSource(); - vtkGlyph3D glyph3D = new vtkGlyph3D(); - out = sphereSource.GetOutputPort(); - glyph3D.SetSourceConnection(out); - out.Delete(); - - out = edgePoints.GetOutputPort(); - glyph3D.SetInputConnection(out); - out.Delete(); - - glyph3D.SetScaleFactor(0.03); - - glyph3D.Update(); - - vtkPolyDataMapper partEdgePointsMapper = new vtkPolyDataMapper(); - out = glyph3D.GetOutputPort(); - partEdgePointsMapper.SetInputConnection(out); - out.Delete(); - - vtkActor edgePointsActor = new vtkActor(); - - vtkProperty prop = edgePointsActor.GetProperty(); - prop.SetColor(0, 0, 1); - prop.Delete(); - edgePointsActor.SetMapper(partEdgePointsMapper); - - edgePointsActor.PickableOn(); - - - partEdgePointsMapper.Delete(); - edgePoints.Delete(); - sphereSource.Delete(); - - return edgePointsActor; - } - - public static vtkActor createSilhouette(vtkRenderer ren, vtkPolyData data) { - - - vtkPolyDataSilhouette silhouette = new vtkPolyDataSilhouette(); - - silhouette.SetInput(data); - silhouette.SetCamera(ren.GetActiveCamera()); - silhouette.SetEnableFeatureAngle(0); - vtkPolyDataMapper mapper = new vtkPolyDataMapper(); - - mapper.SetInputConnection(silhouette.GetOutputPort()); - - vtkActor actor = new vtkActor(); - actor.SetMapper(mapper); - - actor.GetProperty().SetColor(0,0,1); - actor.GetProperty().SetLineWidth(6); - - return actor; - - - - } - -} +/******************************************************************************* + * 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.opencascade.vtk; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.jcae.opencascade.jni.BRepMesh_IncrementalMesh; +import org.jcae.opencascade.jni.TopAbs_ShapeEnum; +import org.jcae.opencascade.jni.TopExp_Explorer; +import org.jcae.opencascade.jni.TopoDS_Face; +import org.jcae.opencascade.jni.TopoDS_Shape; +import org.simantics.opencascade.OCCTTool; +import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.threads.AWTThread; +import org.simantics.utils.threads.ThreadUtils; + +import vtk.vtkActor; +import vtk.vtkAlgorithmOutput; +import vtk.vtkCleanPolyData; +import vtk.vtkDataSetMapper; +import vtk.vtkFeatureEdges; +import vtk.vtkGlyph3D; +import vtk.vtkPanel; +import vtk.vtkPolyData; +import vtk.vtkPolyDataMapper; +import vtk.vtkPolyDataNormals; +import vtk.vtkPolyDataSilhouette; +import vtk.vtkProp3D; +import vtk.vtkProperty; +import vtk.vtkRenderer; +import vtk.vtkSphereSource; + +public class vtkSolidObject { + + public static double deflection = 0.001; + + public static double featureAngle = 30; + public static boolean computeNormals = true; + public static boolean cleanPart = false; + public static boolean mergePoints = false; + + private vtkPanel panel; + private TopoDS_Shape shape; + + private List actors = new ArrayList(2); + + private List solid = new ArrayList(1); + private List edges = new ArrayList(1); + private vtkActor silhouette = null; + + private boolean errors = false; + + public vtkSolidObject(vtkPanel panel,TopoDS_Shape shape) { + this.shape = shape; + this.panel = panel; + } + + public void visualizeSolid(boolean showEdges, boolean showVertices) { + visualizeSolid(true, showEdges, showVertices); + } + + public void visualizeSolid(boolean showFaces, boolean showEdges, boolean showVertices) { + visualizeSolid(showFaces, showEdges, showVertices, false); + } + + public void visualizeSolid(boolean showFaces, boolean showEdges, boolean showVertices, boolean showSilhouette) { + clearActorsAWT(); + errors = false; + Pair res = createSolidMesh(shape); + if (res == null) { + errors = true; + return; + } + vtkPolyData data = res.first; + if (!res.second) + errors = true; + if (showFaces) { + solid.add(createActor(data)); + } + if (showEdges) { + vtkActor edge = createEdgesActor(data); + edges.add(edge); + if (showVertices) { + actors.add(createVerticesActor(edge)); + } + } + if (showSilhouette) { + silhouette = createSilhouette(panel.GetRenderer(), data); + } + actors.addAll(solid); + actors.addAll(edges); + if (silhouette != null) + actors.add(silhouette); + data.Delete(); + showActorsAWT(); + } + + public void visualizeFaces(boolean showEdges, boolean showVertices) { + errors = false; + clearActorsAWT(); + Collection datas = createFaceMeshes(shape); + for (vtkPolyData data : datas) { + if (data == null) { + errors = true; + continue; + } + + solid.add(createActor(data)); + + if (showEdges) { + vtkActor edgesActor = createEdgesActor(data); + edges.add(edgesActor); + if (showVertices) { + actors.add(createVerticesActor(edgesActor)); + } + } + data.Delete(); + } + actors.addAll(solid); + actors.addAll(edges); + + showActorsAWT(); + } + + public boolean hasErrors() { + return errors; + } + + public List getActors() { + assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); + return actors; + } + + public List getSolid() { + assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); + return solid; + } + + public List getEdges() { + assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); + return edges; + } + + public vtkActor getSilhouette() { + assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); + return silhouette; + } + + public void showActorsAWT() { + assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); + vtkRenderer ren = panel.GetRenderer(); + for (vtkProp3D act : actors) { + ren.AddActor(act); + } + } + + public void showActors() { + ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { + + @Override + public void run() { + showActorsAWT(); + } + }); + } + + public void clearActorsAWT() { + assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); + if (actors.size() == 0) + return; + vtkRenderer ren = panel.GetRenderer(); + for (vtkProp3D act : actors) { + if (act.GetVTKId() != 0) { + ren.RemoveActor(act); + act.Delete(); + } + } + actors.clear(); + solid.clear(); + edges.clear(); + } + + private void clearActorsAWT(List actors) { + assert (Thread.currentThread() == AWTThread.getThreadAccess().getThread()); + + if (actors.size() == 0) + return; + vtkRenderer ren = panel.GetRenderer(); + panel.lock(); + for (vtkProp3D act : actors) { + if (act.GetVTKId() != 0) { + ren.RemoveActor(act); + act.Delete(); + } + } + panel.unlock(); + } + + public void clearActors() { + if (actors.size() == 0) + return; + final List temp = new ArrayList(actors.size()); + temp.addAll(actors); + actors.clear(); + solid.clear(); + edges.clear(); + ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { + + @Override + public void run() { + clearActorsAWT(temp); + } + }); + } + + public void dispose() { + if (shape != null) { + shape.delete(); + shape = null; + } + clearActors(); + } + + public void delete() { + if (shape != null) { + shape.delete(); + shape = null; + } + clearActorsAWT(); + } + + private static double TOLERANCE = 0.01; + + public static Pair createSolidMesh(TopoDS_Shape shape) { + + boolean success = true; + + double volume = OCCTTool.getBoundingBoxDiagonal(shape); + if (volume < TOLERANCE) + return null; + + BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(shape,deflection*volume); + + TopExp_Explorer expFace = new TopExp_Explorer(); + + List meshPoints = new ArrayList(); + List meshTriangles = new ArrayList(); + for (expFace.init(shape, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) { + TopoDS_Face face = (TopoDS_Face) expFace.current(); + if (!OCCTTool.appendToMesh(face, meshPoints, meshTriangles)) + success = false; + face.delete(); + } + if (meshPoints.size() == 0 || meshTriangles.size() == 0) + return null; + + vtkPolyData data = VTKOCCTool.createPartGrid(meshPoints, meshTriangles); + + expFace.delete(); + mesh.delete(); + + return new Pair(data, success); + } + + public static Collection createFaceMeshes(TopoDS_Shape shape) { + + double volume = OCCTTool.getBoundingBoxDiagonal(shape); + Collection faces = new ArrayList(); + + if (volume > TOLERANCE) { + + BRepMesh_IncrementalMesh mesh = new BRepMesh_IncrementalMesh(shape,deflection*volume); + + TopExp_Explorer expFace = new TopExp_Explorer(); + + + for (expFace.init(shape, TopAbs_ShapeEnum.FACE); expFace.more(); expFace.next()) { + TopoDS_Face face = (TopoDS_Face) expFace.current(); + vtkPolyData data = VTKOCCTool.createPartGrid(face); + face.delete(); + faces.add(data); + } + + expFace.delete(); + mesh.delete(); + } + + return faces; + } + + + + public static vtkActor createActor(vtkPolyData partGrid) { + + + vtkDataSetMapper partMapper = new vtkDataSetMapper(); + + vtkCleanPolyData partCleaner = null; + if (cleanPart) + { + partCleaner = new vtkCleanPolyData(); + partCleaner.SetInputData(partGrid); + if(mergePoints) { + partCleaner.PointMergingOn(); + } else { + partCleaner.PointMergingOff(); + } + } + + if (computeNormals) + { + vtkPolyDataNormals partNormals = new vtkPolyDataNormals(); + + if (cleanPart) + { + vtkAlgorithmOutput out = partCleaner.GetOutputPort(); + partNormals.SetInputConnection(out); + out.Delete(); + } + else partNormals.SetInputData(partGrid); + + partNormals.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called + vtkAlgorithmOutput out = partNormals.GetOutputPort(); + partMapper.SetInputConnection(out); + out.Delete(); + partNormals.Delete(); + } + else + { + if (cleanPart) { + vtkAlgorithmOutput out = partCleaner.GetOutputPort(); + partMapper.SetInputConnection(out); // metoda 2, ne tak pekne, viz http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 + out.Delete(); + } + else partMapper.SetInputData(partGrid); // metoda 1, ne tak pekne stinovani, viz: http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681 + } + partMapper.ScalarVisibilityOn(); + + vtkActor partActor = new vtkActor(); + // partActor.SetPickable(1); + vtkProperty prop = partActor.GetProperty(); + prop.SetColor(1, 1, 0); + prop.Delete(); + partActor.SetMapper(partMapper); + + partMapper.Delete(); + + if (cleanPart) + partCleaner.Delete(); + + return partActor; + } + + public static vtkActor createEdgesActor(vtkPolyData partGrid) { + vtkCleanPolyData partCleaner = null; + + if (cleanPart) + { + partCleaner = new vtkCleanPolyData(); + partCleaner.SetInputData(partGrid); + if(mergePoints) { + partCleaner.PointMergingOn(); + } else { + partCleaner.PointMergingOff(); + } + } + + vtkFeatureEdges partEdges = new vtkFeatureEdges(); + if (cleanPart) { + vtkAlgorithmOutput out = partCleaner.GetOutputPort(); + partEdges.SetInputConnection(out); + out.Delete(); + } + else partEdges.SetInputData(partGrid); + // partEdges.SetFeatureAngle(featureAngle); // this do not have to be neccesarily called + partEdges.FeatureEdgesOn(); + partEdges.BoundaryEdgesOn(); + partEdges.NonManifoldEdgesOn(); + partEdges.ManifoldEdgesOn(); + + vtkDataSetMapper partEdgesMapper = new vtkDataSetMapper(); + vtkAlgorithmOutput out = partEdges.GetOutputPort(); + partEdgesMapper.SetInputConnection(out); + out.Delete(); + partEdgesMapper.SetResolveCoincidentTopologyToPolygonOffset(); + partEdgesMapper.ScalarVisibilityOff(); + + vtkActor partEdgesActor = new vtkActor(); + + vtkProperty prop = partEdgesActor.GetProperty(); + prop.SetColor(0, 0, 0); + prop.SetLineWidth(2.0); + prop.Delete(); + partEdgesActor.SetMapper(partEdgesMapper); + + partEdgesMapper.Delete(); + partEdges.Delete(); + + if (cleanPart) + partCleaner.Delete(); + + return partEdgesActor; + } + + public static vtkActor createVerticesActor(vtkActor partEdgesActor) { + vtkDataSetMapper partEdgesMapper = (vtkDataSetMapper) partEdgesActor.GetMapper(); + vtkAlgorithmOutput out = partEdgesMapper.GetInputConnection(0, 0); + vtkFeatureEdges partEdges = (vtkFeatureEdges)out.GetProducer(); + + vtkActor edgePointsActor = createVerticesActor(partEdges); + + partEdges.Delete(); + //out.Delete(); + partEdgesMapper.Delete(); + return edgePointsActor; + } + + public static vtkActor createVerticesActor(vtkFeatureEdges partEdges) { + EdgePointsFilter edgePoints = new EdgePointsFilter(); + + + vtkAlgorithmOutput out = partEdges.GetOutputPort(); + edgePoints.SetInputConnection(out); + out.Delete(); + + vtkSphereSource sphereSource = new vtkSphereSource(); + vtkGlyph3D glyph3D = new vtkGlyph3D(); + out = sphereSource.GetOutputPort(); + glyph3D.SetSourceConnection(out); + out.Delete(); + + out = edgePoints.GetOutputPort(); + glyph3D.SetInputConnection(out); + out.Delete(); + + glyph3D.SetScaleFactor(0.03); + + glyph3D.Update(); + + vtkPolyDataMapper partEdgePointsMapper = new vtkPolyDataMapper(); + out = glyph3D.GetOutputPort(); + partEdgePointsMapper.SetInputConnection(out); + out.Delete(); + + vtkActor edgePointsActor = new vtkActor(); + + vtkProperty prop = edgePointsActor.GetProperty(); + prop.SetColor(0, 0, 1); + prop.Delete(); + edgePointsActor.SetMapper(partEdgePointsMapper); + + edgePointsActor.PickableOn(); + + + partEdgePointsMapper.Delete(); + edgePoints.Delete(); + sphereSource.Delete(); + + return edgePointsActor; + } + + public static vtkActor createSilhouette(vtkRenderer ren, vtkPolyData data) { + + + vtkPolyDataSilhouette silhouette = new vtkPolyDataSilhouette(); + + silhouette.SetInputData(data); + silhouette.SetCamera(ren.GetActiveCamera()); + silhouette.SetEnableFeatureAngle(0); + vtkPolyDataMapper mapper = new vtkPolyDataMapper(); + + mapper.SetInputConnection(silhouette.GetOutputPort()); + + vtkActor actor = new vtkActor(); + actor.SetMapper(mapper); + + actor.GetProperty().SetColor(0,0,1); + actor.GetProperty().SetLineWidth(6); + + return actor; + + + + } + +} diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/gizmo/TerminalSelectionGizmo.java b/org.simantics.plant3d/src/org/simantics/plant3d/gizmo/TerminalSelectionGizmo.java index 422e022a..287ce340 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/gizmo/TerminalSelectionGizmo.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/gizmo/TerminalSelectionGizmo.java @@ -195,7 +195,7 @@ public class TerminalSelectionGizmo extends vtkGizmo { plusTex.SetInterpolate(1); vtkPolyDataMapper2D mapper = new vtkPolyDataMapper2D(); - mapper.SetInput(quad); + mapper.SetInputData(quad); nextProp = new vtkTexturedActor2D(); prevProp = new vtkTexturedActor2D();