-package org.simantics.g3d.vtk.shape;\r
-\r
-import javax.vecmath.Vector3d;\r
-\r
-import org.simantics.g3d.shape.Color4d;\r
-import org.simantics.g3d.shape.Mesh;\r
-\r
-import vtk.vtkActor;\r
-import vtk.vtkDataSetMapper;\r
-import vtk.vtkIdList;\r
-import vtk.vtkPoints;\r
-import vtk.vtkPolyData;\r
-import vtk.vtkPolyDataMapper;\r
-import vtk.vtkPolyDataNormals;\r
-import vtk.vtkTriangle;\r
-import vtk.vtkUnsignedCharArray;\r
-\r
-public class MeshActor extends vtkActor {\r
- \r
- public void setMesh(Mesh mesh) {\r
- \r
- vtkPolyDataMapper mapper = new vtkPolyDataMapper();\r
- \r
- vtkPolyData polyData = new vtkPolyData();\r
- polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3);\r
- \r
- vtkTriangle triangle = new vtkTriangle();\r
- vtkIdList list = triangle.GetPointIds();\r
- for (int i = 0; i < mesh.getIndices().size(); i+=3) {\r
- list.SetId(0, mesh.getIndices().get(i));\r
- list.SetId(1, mesh.getIndices().get(i+1));\r
- list.SetId(2, mesh.getIndices().get(i+2));\r
- polyData.InsertNextCell(triangle.GetCellType(), list);\r
- }\r
- list.Delete();\r
- triangle.Delete();\r
- \r
- \r
- vtkPoints points = new vtkPoints();\r
- for (int i = 0; i < mesh.getVertices().size(); i++) {\r
- Vector3d p = mesh.getVertices().get(i);\r
- points.InsertPoint(i, p.x, p.y, p.z);\r
- }\r
- \r
- polyData.SetPoints(points);\r
- points.Delete();\r
- \r
- if (mesh.getColors() != null) {\r
- vtkUnsignedCharArray colors = new vtkUnsignedCharArray();\r
- colors.SetName("Colors");\r
- colors.SetNumberOfComponents(3);\r
- colors.SetNumberOfTuples(mesh.getColors().size());\r
- for (int i = 0; i < mesh.getColors().size(); i++) {\r
- Color4d c = mesh.getColors().get(i);\r
- colors.InsertTuple3(i, 255.0* c.x, 255.0 * c.y, 255.0 * c.z);\r
- }\r
- polyData.GetPointData().AddArray(colors);\r
- colors.Delete();\r
- \r
- }\r
- \r
- boolean computeNormals = true;\r
- if (computeNormals) {\r
- vtkPolyDataNormals normals = new vtkPolyDataNormals();\r
- normals.SetInput(polyData);\r
- mapper.SetInputConnection(normals.GetOutputPort());\r
- normals.GetOutputPort().Delete();\r
- normals.Delete();\r
- } else {\r
- mapper.SetInput(polyData);\r
- }\r
- \r
- if (mesh.getColors() != null) {\r
- mapper.ScalarVisibilityOn();\r
- mapper.SetScalarModeToUsePointFieldData();\r
- mapper.SelectColorArray("Colors");\r
- }\r
- \r
- SetMapper(mapper);\r
- mapper.Delete();\r
- polyData.GetPointData().Delete();\r
- polyData.Delete();\r
- \r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.vtk.shape;
+
+import javax.vecmath.Tuple3d;
+
+import org.simantics.g3d.shape.Color4d;
+import org.simantics.g3d.shape.Mesh;
+
+import vtk.vtkActor;
+import vtk.vtkFloatArray;
+import vtk.vtkIdList;
+import vtk.vtkPointData;
+import vtk.vtkPoints;
+import vtk.vtkPolyData;
+import vtk.vtkPolyDataMapper;
+import vtk.vtkPolyDataNormals;
+import vtk.vtkTriangle;
+import vtk.vtkUnsignedCharArray;
+
+public class MeshActor extends vtkActor {
+
+
+ public static vtkPolyData createPolyData(Mesh mesh) {
+ vtkPolyData polyData = new vtkPolyData();
+ polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3);
+
+ vtkTriangle triangle = new vtkTriangle();
+ vtkIdList list = triangle.GetPointIds();
+ for (int i = 0; i < mesh.getIndices().size(); i+=3) {
+ list.SetId(0, mesh.getIndices().get(i));
+ list.SetId(1, mesh.getIndices().get(i+1));
+ list.SetId(2, mesh.getIndices().get(i+2));
+ polyData.InsertNextCell(triangle.GetCellType(), list);
+ }
+ list.Delete();
+ triangle.Delete();
+
+
+ vtkPoints points = new vtkPoints();
+ for (int i = 0; i < mesh.getVertices().size(); i++) {
+ Tuple3d p = mesh.getVertices().get(i);
+ points.InsertPoint(i, p.x, p.y, p.z);
+ }
+
+ polyData.SetPoints(points);
+ points.Delete();
+
+ if (mesh.getNormals() != null) {
+ vtkFloatArray normals = new vtkFloatArray();
+ normals.SetNumberOfComponents(3);
+ normals.SetNumberOfTuples(mesh.getNormals().size());
+ for (int i = 0; i < mesh.getNormals().size(); i++) {
+ Tuple3d p = mesh.getNormals().get(i);
+ normals.InsertTuple3(i, p.x, p.y, p.z);
+ }
+ vtkPointData pointData = polyData.GetPointData();
+ pointData.SetNormals(normals);
+ normals.Delete();
+ pointData.Delete();
+ }
+ if (mesh.getColors() != null) {
+ vtkUnsignedCharArray colors = new vtkUnsignedCharArray();
+ colors.SetName("Colors");
+ colors.SetNumberOfComponents(3);
+ colors.SetNumberOfTuples(mesh.getColors().size());
+ for (int i = 0; i < mesh.getColors().size(); i++) {
+ Color4d c = mesh.getColors().get(i);
+ colors.InsertTuple3(i, 255.0* c.x, 255.0 * c.y, 255.0 * c.z);
+ }
+ polyData.GetPointData().AddArray(colors);
+ colors.Delete();
+
+ }
+ return polyData;
+ }
+
+ public void setMesh(Mesh mesh) {
+
+ vtkPolyDataMapper mapper = new vtkPolyDataMapper();
+ vtkPolyData polyData = createPolyData(mesh);
+
+
+ if (mesh.getNormals() == null) {
+ vtkPolyDataNormals normals = new vtkPolyDataNormals();
+ normals.SetInputData(polyData);
+ normals.ComputePointNormalsOn();
+ mapper.SetInputConnection(normals.GetOutputPort());
+ normals.GetOutputPort().Delete();
+ normals.Delete();
+ } else {
+ mapper.SetInputData(polyData);
+ }
+
+ if (mesh.getColors() != null) {
+ mapper.ScalarVisibilityOn();
+ mapper.SetScalarModeToUsePointFieldData();
+ mapper.SelectColorArray("Colors");
+ }
+
+ SetMapper(mapper);
+ mapper.Delete();
+ polyData.GetPointData().Delete();
+ polyData.Delete();
+
+ }
+
+ @Override
+ public void Delete() {
+ // TODO Auto-generated method stub
+ super.Delete();
+ }
+
+}