]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java
Remove dependencies on log4j
[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
16 import org.simantics.g3d.shape.Color4d;
17 import org.simantics.g3d.shape.Mesh;
18
19 import vtk.vtkActor;
20 import vtk.vtkFloatArray;
21 import vtk.vtkIdList;
22 import vtk.vtkPointData;
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         
33         public static vtkPolyData createPolyData(Mesh mesh) {
34                 vtkPolyData polyData = new vtkPolyData();
35                 polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3);
36                 
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);
44                 }
45                 list.Delete();
46                 triangle.Delete();
47                 
48                 
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);
53                 }
54                 
55                 polyData.SetPoints(points);
56                 points.Delete();
57                 
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);
65                         }
66                         vtkPointData pointData = polyData.GetPointData();
67                         pointData.SetNormals(normals);
68                         normals.Delete();
69                         pointData.Delete();
70                 }
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);
79                         }
80                         polyData.GetPointData().AddArray(colors);
81                         colors.Delete();
82                         
83                 }
84                 return polyData;
85         }
86         
87         public void setMesh(Mesh mesh) {
88                 
89                 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
90                 vtkPolyData polyData = createPolyData(mesh);
91                 
92                 
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();
99                         normals.Delete();
100                 } else {
101                         mapper.SetInputData(polyData);
102                 }
103                 
104                 if (mesh.getColors() != null) {
105                         mapper.ScalarVisibilityOn();
106                         mapper.SetScalarModeToUsePointFieldData();
107                         mapper.SelectColorArray("Colors");
108                 }
109                 
110                 SetMapper(mapper);
111                 mapper.Delete();
112                 polyData.GetPointData().Delete();
113                 polyData.Delete();
114                 
115         }
116         
117         @Override
118         public void Delete() {
119                 // TODO Auto-generated method stub
120                 super.Delete();
121         }
122
123 }