X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d.vtk%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fvtk%2Fshape%2FMeshActor.java;h=0cfc8246bcc155d43e25edf0dcb550800369669a;hb=58ebeb2baac48f9066c1395a9071f99745574ef9;hp=042142fccdb9af1898a75785db6d00b9b012b19d;hpb=289aaab900078ef56efc8779e4b15830e472149e;p=simantics%2F3d.git 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 042142fc..0cfc8246 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 @@ -1,97 +1,98 @@ -/******************************************************************************* - * 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.Vector3d; - -import org.simantics.g3d.shape.Color4d; -import org.simantics.g3d.shape.Mesh; - -import vtk.vtkActor; -import vtk.vtkDataSetMapper; -import vtk.vtkIdList; -import vtk.vtkPoints; -import vtk.vtkPolyData; -import vtk.vtkPolyDataMapper; -import vtk.vtkPolyDataNormals; -import vtk.vtkTriangle; -import vtk.vtkUnsignedCharArray; - -public class MeshActor extends vtkActor { - - public void setMesh(Mesh mesh) { - - vtkPolyDataMapper mapper = new vtkPolyDataMapper(); - - vtkPolyData polyData = new vtkPolyData(); - polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3); - - vtkTriangle triangle = new vtkTriangle(); - vtkIdList list = triangle.GetPointIds(); - for (int i = 0; i < mesh.getIndices().size(); i+=3) { - list.SetId(0, mesh.getIndices().get(i)); - list.SetId(1, mesh.getIndices().get(i+1)); - list.SetId(2, mesh.getIndices().get(i+2)); - polyData.InsertNextCell(triangle.GetCellType(), list); - } - list.Delete(); - triangle.Delete(); - - - vtkPoints points = new vtkPoints(); - for (int i = 0; i < mesh.getVertices().size(); i++) { - Vector3d p = mesh.getVertices().get(i); - points.InsertPoint(i, p.x, p.y, p.z); - } - - polyData.SetPoints(points); - points.Delete(); - - if (mesh.getColors() != null) { - vtkUnsignedCharArray colors = new vtkUnsignedCharArray(); - colors.SetName("Colors"); - colors.SetNumberOfComponents(3); - colors.SetNumberOfTuples(mesh.getColors().size()); - for (int i = 0; i < mesh.getColors().size(); i++) { - Color4d c = mesh.getColors().get(i); - colors.InsertTuple3(i, 255.0* c.x, 255.0 * c.y, 255.0 * c.z); - } - polyData.GetPointData().AddArray(colors); - colors.Delete(); - - } - - boolean computeNormals = true; - if (computeNormals) { - vtkPolyDataNormals normals = new vtkPolyDataNormals(); - normals.SetInput(polyData); - mapper.SetInputConnection(normals.GetOutputPort()); - normals.GetOutputPort().Delete(); - normals.Delete(); - } else { - mapper.SetInput(polyData); - } - - if (mesh.getColors() != null) { - mapper.ScalarVisibilityOn(); - mapper.SetScalarModeToUsePointFieldData(); - mapper.SelectColorArray("Colors"); - } - - SetMapper(mapper); - mapper.Delete(); - polyData.GetPointData().Delete(); - polyData.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.g3d.vtk.shape; + +import javax.vecmath.Tuple3d; +import javax.vecmath.Vector3d; + +import org.simantics.g3d.shape.Color4d; +import org.simantics.g3d.shape.Mesh; + +import vtk.vtkActor; +import vtk.vtkDataSetMapper; +import vtk.vtkIdList; +import vtk.vtkPoints; +import vtk.vtkPolyData; +import vtk.vtkPolyDataMapper; +import vtk.vtkPolyDataNormals; +import vtk.vtkTriangle; +import vtk.vtkUnsignedCharArray; + +public class MeshActor extends vtkActor { + + public void setMesh(Mesh mesh) { + + vtkPolyDataMapper mapper = new vtkPolyDataMapper(); + + vtkPolyData polyData = new vtkPolyData(); + polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3); + + vtkTriangle triangle = new vtkTriangle(); + vtkIdList list = triangle.GetPointIds(); + for (int i = 0; i < mesh.getIndices().size(); i+=3) { + list.SetId(0, mesh.getIndices().get(i)); + list.SetId(1, mesh.getIndices().get(i+1)); + list.SetId(2, mesh.getIndices().get(i+2)); + polyData.InsertNextCell(triangle.GetCellType(), list); + } + list.Delete(); + triangle.Delete(); + + + vtkPoints points = new vtkPoints(); + for (int i = 0; i < mesh.getVertices().size(); i++) { + Tuple3d p = mesh.getVertices().get(i); + points.InsertPoint(i, p.x, p.y, p.z); + } + + polyData.SetPoints(points); + points.Delete(); + + if (mesh.getColors() != null) { + vtkUnsignedCharArray colors = new vtkUnsignedCharArray(); + colors.SetName("Colors"); + colors.SetNumberOfComponents(3); + colors.SetNumberOfTuples(mesh.getColors().size()); + for (int i = 0; i < mesh.getColors().size(); i++) { + Color4d c = mesh.getColors().get(i); + colors.InsertTuple3(i, 255.0* c.x, 255.0 * c.y, 255.0 * c.z); + } + polyData.GetPointData().AddArray(colors); + colors.Delete(); + + } + + boolean computeNormals = true; + if (computeNormals) { + vtkPolyDataNormals normals = new vtkPolyDataNormals(); + normals.SetInput(polyData); + mapper.SetInputConnection(normals.GetOutputPort()); + normals.GetOutputPort().Delete(); + normals.Delete(); + } else { + mapper.SetInput(polyData); + } + + if (mesh.getColors() != null) { + mapper.ScalarVisibilityOn(); + mapper.SetScalarModeToUsePointFieldData(); + mapper.SelectColorArray("Colors"); + } + + SetMapper(mapper); + mapper.Delete(); + polyData.GetPointData().Delete(); + polyData.Delete(); + + } + +}