]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.opencascade.vtk/src/org/simantics/opencascade/vtk/EdgePointsFilter.java
Copyrights
[simantics/3d.git] / org.simantics.opencascade.vtk / src / org / simantics / opencascade / vtk / EdgePointsFilter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.opencascade.vtk;\r
13 \r
14 import java.util.HashSet;\r
15 import java.util.List;\r
16 import java.util.Set;\r
17 \r
18 import javax.vecmath.Point3d;\r
19 import javax.vecmath.Vector3d;\r
20 \r
21 import org.simantics.utils.datastructures.MapList;\r
22 \r
23 import vtk.vtkCell;\r
24 import vtk.vtkIdList;\r
25 import vtk.vtkLine;\r
26 import vtk.vtkPoints;\r
27 import vtk.vtkPolyData;\r
28 import vtk.vtkProgrammableFilter;\r
29 import vtk.vtkVertex;\r
30 \r
31 public class EdgePointsFilter extends vtkProgrammableFilter {\r
32         vtkPoints outputPoints;\r
33         \r
34         \r
35         public EdgePointsFilter() {\r
36                 SetExecuteMethod(this, "compute");\r
37         }\r
38         \r
39         public void compute() {\r
40                 vtkPolyData polyDataInput = GetPolyDataInput();\r
41                 vtkPolyData polyDataOutput = GetPolyDataOutput();\r
42                 vtkPoints inputPoints = polyDataInput.GetPoints();\r
43  \r
44             outputPoints = new vtkPoints();\r
45 \r
46                 MapList<Integer, Integer> edgeIndices = new MapList<Integer, Integer>();\r
47                 for (int i = 0; i <polyDataInput.GetNumberOfCells(); i++) {\r
48                         vtkCell cell= polyDataInput.GetCell(i);\r
49                         if (cell.IsA("vtkLine") > 0) {\r
50                                 vtkLine line = (vtkLine)cell;\r
51                                 int i1 = line.GetPointId(0);\r
52                                 int i2 = line.GetPointId(1);\r
53                                 if (!edgeIndices.contains(i1, i2)) {\r
54                                         edgeIndices.add(i1, i2);\r
55                                         edgeIndices.add(i2, i1);\r
56                                 }\r
57                                 line.Delete();\r
58                         }        \r
59                 }\r
60                                 \r
61                 Set<Integer> vertices = new HashSet<Integer>();\r
62                 for (Integer i : edgeIndices.getKeys()) {\r
63                         List<Integer> edges = edgeIndices.getValues(i);\r
64                         if (edges.size() != 2)\r
65                                 vertices.add(i);\r
66                         else {\r
67                                 double tp[] = inputPoints.GetPoint(i);\r
68                                 double ep1[] = inputPoints.GetPoint(edges.get(0));\r
69                                 double ep2[] = inputPoints.GetPoint(edges.get(1));\r
70                                 Point3d t = new Point3d(tp);\r
71                                 Vector3d v1 = new Vector3d(ep1);\r
72                                 Vector3d v2 = new Vector3d(ep2);\r
73                                 v1.sub(t);\r
74                                 v2.sub(t);\r
75                                 double angle = Math.PI - v1.angle(v2);\r
76                                 if (angle > Math.PI/6)\r
77                                         vertices.add(i);\r
78                         }\r
79                 }\r
80                 for (int i : vertices) {\r
81                         outputPoints.InsertNextPoint(inputPoints.GetPoint(i));\r
82                 }\r
83                 polyDataOutput.Allocate(vertices.size(), vertices.size());\r
84                 \r
85                 vtkVertex vertex = new vtkVertex();\r
86                 vtkIdList list = vertex.GetPointIds();\r
87                 for (int i = 0; i < vertices.size(); i++) {\r
88                         list.SetId(0, i);\r
89                         polyDataOutput.InsertNextCell(vertex.GetCellType(), list);\r
90                 }\r
91                 \r
92                 polyDataOutput.SetPoints(outputPoints);\r
93                 \r
94                 \r
95                 list.Delete();\r
96                 vertex.Delete();\r
97                 \r
98                 polyDataOutput.Delete();\r
99                 polyDataInput.Delete();\r
100                 \r
101         }\r
102         \r
103         @Override\r
104         public void Delete() {\r
105                 outputPoints.Delete();\r
106                 super.Delete();\r
107         }\r
108 }\r