]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java
vtk 8.2.0 API changes
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / shape / MeshActor.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g3d.vtk.shape;
13
14 import javax.vecmath.Tuple3d;
15 import javax.vecmath.Vector3d;
16
17 import org.simantics.g3d.shape.Color4d;
18 import org.simantics.g3d.shape.Mesh;
19
20 import vtk.vtkActor;
21 import vtk.vtkDataSetMapper;
22 import vtk.vtkIdList;
23 import vtk.vtkPoints;
24 import vtk.vtkPolyData;
25 import vtk.vtkPolyDataMapper;
26 import vtk.vtkPolyDataNormals;
27 import vtk.vtkTriangle;
28 import vtk.vtkUnsignedCharArray;
29
30 public class MeshActor extends vtkActor {
31         
32         public void setMesh(Mesh mesh) {
33                 
34                 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
35                 
36                 vtkPolyData polyData = new vtkPolyData();
37                 polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3);
38                 
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);
46         }
47         list.Delete();
48         triangle.Delete();
49                 
50         
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);
55         }
56         
57         polyData.SetPoints(points);
58         points.Delete();
59         
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);
68                 }
69                 polyData.GetPointData().AddArray(colors);
70                 colors.Delete();
71                 
72         }
73         
74         boolean computeNormals = true;
75         if (computeNormals) {
76                 vtkPolyDataNormals normals = new vtkPolyDataNormals();
77                 normals.SetInputData(polyData);
78                 mapper.SetInputConnection(normals.GetOutputPort());
79                 normals.GetOutputPort().Delete();
80                 normals.Delete();
81         } else {
82                 mapper.SetInputData(polyData);
83         }
84         
85         if (mesh.getColors() != null) {
86                  mapper.ScalarVisibilityOn();
87                  mapper.SetScalarModeToUsePointFieldData();
88                  mapper.SelectColorArray("Colors");
89         }
90         
91         SetMapper(mapper);
92         mapper.Delete();
93         polyData.GetPointData().Delete();
94         polyData.Delete();
95         
96         }
97
98 }