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;
16 import org.simantics.g3d.shape.Color4d;
17 import org.simantics.g3d.shape.Mesh;
20 import vtk.vtkFloatArray;
22 import vtk.vtkPointData;
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 {
33 public static vtkPolyData createPolyData(Mesh mesh) {
34 vtkPolyData polyData = new vtkPolyData();
35 polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3);
37 vtkTriangle triangle = new vtkTriangle();
38 vtkIdList list = triangle.GetPointIds();
39 for (int i = 0; i < mesh.getIndices().size(); i+=3) {
40 list.SetId(0, mesh.getIndices().get(i));
41 list.SetId(1, mesh.getIndices().get(i+1));
42 list.SetId(2, mesh.getIndices().get(i+2));
43 polyData.InsertNextCell(triangle.GetCellType(), list);
49 vtkPoints points = new vtkPoints();
50 for (int i = 0; i < mesh.getVertices().size(); i++) {
51 Tuple3d p = mesh.getVertices().get(i);
52 points.InsertPoint(i, p.x, p.y, p.z);
55 polyData.SetPoints(points);
58 if (mesh.getNormals() != null) {
59 vtkFloatArray normals = new vtkFloatArray();
60 normals.SetNumberOfComponents(3);
61 normals.SetNumberOfTuples(mesh.getNormals().size());
62 for (int i = 0; i < mesh.getNormals().size(); i++) {
63 Tuple3d p = mesh.getNormals().get(i);
64 normals.InsertTuple3(i, p.x, p.y, p.z);
66 vtkPointData pointData = polyData.GetPointData();
67 pointData.SetNormals(normals);
71 if (mesh.getColors() != null) {
72 vtkUnsignedCharArray colors = new vtkUnsignedCharArray();
73 colors.SetName("Colors");
74 colors.SetNumberOfComponents(3);
75 colors.SetNumberOfTuples(mesh.getColors().size());
76 for (int i = 0; i < mesh.getColors().size(); i++) {
77 Color4d c = mesh.getColors().get(i);
78 colors.InsertTuple3(i, 255.0* c.x, 255.0 * c.y, 255.0 * c.z);
80 polyData.GetPointData().AddArray(colors);
87 public void setMesh(Mesh mesh) {
89 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
90 vtkPolyData polyData = createPolyData(mesh);
93 if (mesh.getNormals() == null) {
94 vtkPolyDataNormals normals = new vtkPolyDataNormals();
95 normals.SetInputData(polyData);
96 normals.ComputePointNormalsOn();
97 mapper.SetInputConnection(normals.GetOutputPort());
98 normals.GetOutputPort().Delete();
101 mapper.SetInputData(polyData);
104 if (mesh.getColors() != null) {
105 mapper.ScalarVisibilityOn();
106 mapper.SetScalarModeToUsePointFieldData();
107 mapper.SelectColorArray("Colors");
112 polyData.GetPointData().Delete();
118 public void Delete() {
119 // TODO Auto-generated method stub