]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.opencascade.vtk/src/org/simantics/opencascade/vtk/EdgePointsFilter.java
3D framework (Simca 2012)
[simantics/3d.git] / org.simantics.opencascade.vtk / src / org / simantics / opencascade / vtk / EdgePointsFilter.java
1 package org.simantics.opencascade.vtk;\r
2 \r
3 import java.util.HashSet;\r
4 import java.util.List;\r
5 import java.util.Set;\r
6 \r
7 import javax.vecmath.Point3d;\r
8 import javax.vecmath.Vector3d;\r
9 \r
10 import org.simantics.utils.datastructures.MapList;\r
11 \r
12 import vtk.vtkCell;\r
13 import vtk.vtkIdList;\r
14 import vtk.vtkLine;\r
15 import vtk.vtkPoints;\r
16 import vtk.vtkPolyData;\r
17 import vtk.vtkProgrammableFilter;\r
18 import vtk.vtkVertex;\r
19 \r
20 public class EdgePointsFilter extends vtkProgrammableFilter {\r
21         vtkPoints outputPoints;\r
22         \r
23         \r
24         public EdgePointsFilter() {\r
25                 SetExecuteMethod(this, "compute");\r
26         }\r
27         \r
28         public void compute() {\r
29                 vtkPolyData polyDataInput = GetPolyDataInput();\r
30                 vtkPolyData polyDataOutput = GetPolyDataOutput();\r
31                 vtkPoints inputPoints = polyDataInput.GetPoints();\r
32  \r
33             outputPoints = new vtkPoints();\r
34 \r
35                 MapList<Integer, Integer> edgeIndices = new MapList<Integer, Integer>();\r
36                 for (int i = 0; i <polyDataInput.GetNumberOfCells(); i++) {\r
37                         vtkCell cell= polyDataInput.GetCell(i);\r
38                         if (cell.IsA("vtkLine") > 0) {\r
39                                 vtkLine line = (vtkLine)cell;\r
40                                 int i1 = line.GetPointId(0);\r
41                                 int i2 = line.GetPointId(1);\r
42                                 if (!edgeIndices.contains(i1, i2)) {\r
43                                         edgeIndices.add(i1, i2);\r
44                                         edgeIndices.add(i2, i1);\r
45                                 }\r
46                                 line.Delete();\r
47                         }        \r
48                 }\r
49                                 \r
50                 Set<Integer> vertices = new HashSet<Integer>();\r
51                 for (Integer i : edgeIndices.getKeys()) {\r
52                         List<Integer> edges = edgeIndices.getValues(i);\r
53                         if (edges.size() != 2)\r
54                                 vertices.add(i);\r
55                         else {\r
56                                 double tp[] = inputPoints.GetPoint(i);\r
57                                 double ep1[] = inputPoints.GetPoint(edges.get(0));\r
58                                 double ep2[] = inputPoints.GetPoint(edges.get(1));\r
59                                 Point3d t = new Point3d(tp);\r
60                                 Vector3d v1 = new Vector3d(ep1);\r
61                                 Vector3d v2 = new Vector3d(ep2);\r
62                                 v1.sub(t);\r
63                                 v2.sub(t);\r
64                                 double angle = Math.PI - v1.angle(v2);\r
65                                 if (angle > Math.PI/6)\r
66                                         vertices.add(i);\r
67                         }\r
68                 }\r
69                 for (int i : vertices) {\r
70                         outputPoints.InsertNextPoint(inputPoints.GetPoint(i));\r
71                 }\r
72                 polyDataOutput.Allocate(vertices.size(), vertices.size());\r
73                 \r
74                 vtkVertex vertex = new vtkVertex();\r
75                 vtkIdList list = vertex.GetPointIds();\r
76                 for (int i = 0; i < vertices.size(); i++) {\r
77                         list.SetId(0, i);\r
78                         polyDataOutput.InsertNextCell(vertex.GetCellType(), list);\r
79                 }\r
80                 \r
81                 polyDataOutput.SetPoints(outputPoints);\r
82                 \r
83                 \r
84                 list.Delete();\r
85                 vertex.Delete();\r
86                 \r
87                 polyDataOutput.Delete();\r
88                 polyDataInput.Delete();\r
89                 \r
90         }\r
91         \r
92         @Override\r
93         public void Delete() {\r
94                 outputPoints.Delete();\r
95                 super.Delete();\r
96         }\r
97 }\r