/******************************************************************************* * 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(); } }