]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java
Create vtkPolyData without MeshActor instance
[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 static vtkPolyData createPolyData(Mesh mesh) {
33                 vtkPolyData polyData = new vtkPolyData();
34                 polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3);
35                 
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);
43         }
44         list.Delete();
45         triangle.Delete();
46                 
47         
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);
52         }
53         
54         polyData.SetPoints(points);
55         points.Delete();
56         
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);
65                 }
66                 polyData.GetPointData().AddArray(colors);
67                 colors.Delete();
68                 
69         }
70         return polyData;
71         }
72         
73         public void setMesh(Mesh mesh) {
74                 
75                 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
76                 vtkPolyData polyData = createPolyData(mesh);
77                 
78         boolean computeNormals = true;
79         if (computeNormals) {
80                 vtkPolyDataNormals normals = new vtkPolyDataNormals();
81                 normals.SetInputData(polyData);
82                 mapper.SetInputConnection(normals.GetOutputPort());
83                 normals.GetOutputPort().Delete();
84                 normals.Delete();
85         } else {
86                 mapper.SetInputData(polyData);
87         }
88         
89         if (mesh.getColors() != null) {
90                  mapper.ScalarVisibilityOn();
91                  mapper.SetScalarModeToUsePointFieldData();
92                  mapper.SelectColorArray("Colors");
93         }
94         
95         SetMapper(mapper);
96         mapper.Delete();
97         polyData.GetPointData().Delete();
98         polyData.Delete();
99         
100         }
101
102 }