1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.vtk.shape;
14 import javax.vecmath.Tuple3d;
15 import javax.vecmath.Vector3d;
17 import org.simantics.g3d.shape.Color4d;
18 import org.simantics.g3d.shape.Mesh;
21 import vtk.vtkDataSetMapper;
24 import vtk.vtkPolyData;
25 import vtk.vtkPolyDataMapper;
26 import vtk.vtkPolyDataNormals;
27 import vtk.vtkTriangle;
28 import vtk.vtkUnsignedCharArray;
30 public class MeshActor extends vtkActor {
32 public static vtkPolyData createPolyData(Mesh mesh) {
33 vtkPolyData polyData = new vtkPolyData();
34 polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3);
36 vtkTriangle triangle = new vtkTriangle();
37 vtkIdList list = triangle.GetPointIds();
38 for (int i = 0; i < mesh.getIndices().size(); i+=3) {
39 list.SetId(0, mesh.getIndices().get(i));
40 list.SetId(1, mesh.getIndices().get(i+1));
41 list.SetId(2, mesh.getIndices().get(i+2));
42 polyData.InsertNextCell(triangle.GetCellType(), list);
48 vtkPoints points = new vtkPoints();
49 for (int i = 0; i < mesh.getVertices().size(); i++) {
50 Tuple3d p = mesh.getVertices().get(i);
51 points.InsertPoint(i, p.x, p.y, p.z);
54 polyData.SetPoints(points);
57 if (mesh.getColors() != null) {
58 vtkUnsignedCharArray colors = new vtkUnsignedCharArray();
59 colors.SetName("Colors");
60 colors.SetNumberOfComponents(3);
61 colors.SetNumberOfTuples(mesh.getColors().size());
62 for (int i = 0; i < mesh.getColors().size(); i++) {
63 Color4d c = mesh.getColors().get(i);
64 colors.InsertTuple3(i, 255.0* c.x, 255.0 * c.y, 255.0 * c.z);
66 polyData.GetPointData().AddArray(colors);
73 public void setMesh(Mesh mesh) {
75 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
76 vtkPolyData polyData = createPolyData(mesh);
78 boolean computeNormals = true;
80 vtkPolyDataNormals normals = new vtkPolyDataNormals();
81 normals.SetInputData(polyData);
82 mapper.SetInputConnection(normals.GetOutputPort());
83 normals.GetOutputPort().Delete();
86 mapper.SetInputData(polyData);
89 if (mesh.getColors() != null) {
90 mapper.ScalarVisibilityOn();
91 mapper.SetScalarModeToUsePointFieldData();
92 mapper.SelectColorArray("Colors");
97 polyData.GetPointData().Delete();