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 void setMesh(Mesh mesh) {
34 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
36 vtkPolyData polyData = new vtkPolyData();
37 polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3);
39 vtkTriangle triangle = new vtkTriangle();
40 vtkIdList list = triangle.GetPointIds();
41 for (int i = 0; i < mesh.getIndices().size(); i+=3) {
42 list.SetId(0, mesh.getIndices().get(i));
43 list.SetId(1, mesh.getIndices().get(i+1));
44 list.SetId(2, mesh.getIndices().get(i+2));
45 polyData.InsertNextCell(triangle.GetCellType(), list);
51 vtkPoints points = new vtkPoints();
52 for (int i = 0; i < mesh.getVertices().size(); i++) {
53 Tuple3d p = mesh.getVertices().get(i);
54 points.InsertPoint(i, p.x, p.y, p.z);
57 polyData.SetPoints(points);
60 if (mesh.getColors() != null) {
61 vtkUnsignedCharArray colors = new vtkUnsignedCharArray();
62 colors.SetName("Colors");
63 colors.SetNumberOfComponents(3);
64 colors.SetNumberOfTuples(mesh.getColors().size());
65 for (int i = 0; i < mesh.getColors().size(); i++) {
66 Color4d c = mesh.getColors().get(i);
67 colors.InsertTuple3(i, 255.0* c.x, 255.0 * c.y, 255.0 * c.z);
69 polyData.GetPointData().AddArray(colors);
74 boolean computeNormals = true;
76 vtkPolyDataNormals normals = new vtkPolyDataNormals();
77 normals.SetInputData(polyData);
78 mapper.SetInputConnection(normals.GetOutputPort());
79 normals.GetOutputPort().Delete();
82 mapper.SetInputData(polyData);
85 if (mesh.getColors() != null) {
86 mapper.ScalarVisibilityOn();
87 mapper.SetScalarModeToUsePointFieldData();
88 mapper.SelectColorArray("Colors");
93 polyData.GetPointData().Delete();