]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/vtkShape.java
d3284df285983b28707705e411235d69ea5d877c
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / shape / vtkShape.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.g3d.vtk.shape;\r
13 \r
14 import javax.vecmath.Tuple3d;\r
15 \r
16 import vtk.vtkActor;\r
17 import vtk.vtkDataSetMapper;\r
18 import vtk.vtkLine;\r
19 import vtk.vtkPoints;\r
20 import vtk.vtkPolyLine;\r
21 import vtk.vtkUnstructuredGrid;\r
22 \r
23 public class vtkShape {\r
24         \r
25         /**\r
26          * Creates a grid shaped actor.\r
27          * \r
28          * @param size number of grid lines\r
29          * @param space distance between grid lines\r
30          * @param axes bitmask of axes: 1:x, 2:y, 4:z\r
31          * @return vtkActor representing a grid.\r
32          */\r
33         public static vtkActor createGridActor(int size, double space, int axes) {\r
34                 int gridCount = 0;\r
35                 if ((axes & 0x1) > 0) {\r
36                         gridCount++;\r
37                 }\r
38                 if ((axes & 0x2) > 0) {\r
39                         gridCount++;\r
40                 }\r
41                 if ((axes & 0x4) > 0) {\r
42                         gridCount++;\r
43                 }\r
44                 int pointCount = (size+1) * 2 * 2 * gridCount;\r
45                 vtkPoints linePoints = new vtkPoints();\r
46                 linePoints.SetNumberOfPoints(pointCount);\r
47                 \r
48                 \r
49                 double max = space * (double)size * 0.5;\r
50                 double min = -max;\r
51                 int base = 0;\r
52                 if ((axes & 0x1) > 0) {\r
53                         for (int i = 0; i <= size; i++) {\r
54                                 double s = min + ((double)i) * space;\r
55                                 linePoints.InsertPoint(base + i*2  ,0.0, s, min);\r
56                                 linePoints.InsertPoint(base + i*2+1,0.0, s, max);\r
57                                 i++;\r
58                                 if (i > size)\r
59                                         break;\r
60                                 s = min + ((double)i) * space;\r
61                                 linePoints.InsertPoint(base + i*2  ,0.0, s, max);\r
62                                 linePoints.InsertPoint(base + i*2+1,0.0, s, min);\r
63                         }\r
64                         base += (size+1)*2;\r
65                         for (int i = 0; i <= size; i++) {\r
66                                 double s = min + ((double)i) * space;\r
67                                 linePoints.InsertPoint(base + i*2  , 0.0, max, s);\r
68                                 linePoints.InsertPoint(base + i*2+1, 0.0, min, s);\r
69                                 i++;\r
70                                 if (i > size)\r
71                                         break;\r
72                                 s = min + ((double)i) * space;\r
73                                 linePoints.InsertPoint(base + i*2  , 0.0, min, s);\r
74                                 linePoints.InsertPoint(base + i*2+1, 0.0, max, s);\r
75                         }\r
76                         base += (size+1)*2;\r
77                 } \r
78                 if ((axes & 0x4) > 0) {\r
79                         for (int i = 0; i <= size; i++) {\r
80                                 double s = min + ((double)i) * space;\r
81                                 linePoints.InsertPoint(base + i*2  ,s, min, 0.0);\r
82                                 linePoints.InsertPoint(base + i*2+1,s, max, 0.0);\r
83                                 i++;\r
84                                 if (i > size)\r
85                                         break;\r
86                                 s = min + ((double)i) * space;\r
87                                 linePoints.InsertPoint(base + i*2  ,s, max, 0.0);\r
88                                 linePoints.InsertPoint(base + i*2+1,s, min, 0.0);\r
89                         }\r
90                         base += (size+1)*2;\r
91                         for (int i = 0; i <= size; i++) {\r
92                                 double s = min + ((double)i) * space;\r
93                                 linePoints.InsertPoint(base + i*2  ,max, s, 0.0);\r
94                                 linePoints.InsertPoint(base + i*2+1,min, s, 0.0);\r
95                                 i++;\r
96                                 if (i > size)\r
97                                         break;\r
98                                 s = min + ((double)i) * space;\r
99                                 linePoints.InsertPoint(base + i*2  ,min, s, 0.0);\r
100                                 linePoints.InsertPoint(base + i*2+1,max, s, 0.0);\r
101                         }\r
102                         base += (size+1)*2;\r
103                 } \r
104                 if ((axes & 0x2) > 0) {\r
105                         for (int i = 0; i <= size; i++) {\r
106                                 double s = min + ((double)i) * space;\r
107                                 linePoints.InsertPoint(base + i*2  ,s, 0.0, min);\r
108                                 linePoints.InsertPoint(base + i*2+1,s, 0.0, max);\r
109                                 i++;\r
110                                 if (i > size)\r
111                                         break;\r
112                                 s = min + ((double)i) * space;\r
113                                 linePoints.InsertPoint(base + i*2  ,s, 0.0, max);\r
114                                 linePoints.InsertPoint(base + i*2+1,s, 0.0, min);\r
115                         }\r
116                         base += (size+1)*2;\r
117                         for (int i = 0; i <= size; i++) {\r
118                                 double s = min + ((double)i) * space;\r
119                                 linePoints.InsertPoint(base + i*2  ,max, 0.0, s);\r
120                                 linePoints.InsertPoint(base + i*2+1,min, 0.0, s);\r
121                                 i++;\r
122                                 if (i > size)\r
123                                         break;\r
124                                 s = min + ((double)i) * space;\r
125                                 linePoints.InsertPoint(base + i*2  ,min, 0.0, s);\r
126                                 linePoints.InsertPoint(base + i*2+1,max, 0.0, s);\r
127                         }\r
128                         base += (size+1)*2;\r
129                 }\r
130                         \r
131                 \r
132                 //vtkLine aLine = new vtkLine();\r
133                 vtkPolyLine aLine = new vtkPolyLine();\r
134                 aLine.GetPointIds().SetNumberOfIds(pointCount);\r
135                 for (int i = 0; i < pointCount; i++) {\r
136                         aLine.GetPointIds().SetId(i, i);\r
137                 }\r
138                 \r
139                 \r
140                 vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();\r
141                 aLineGrid.Allocate(pointCount, pointCount);\r
142                 aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());\r
143                 aLineGrid.SetPoints(linePoints);\r
144                 vtkDataSetMapper aLineMapper = new vtkDataSetMapper();\r
145                 aLineMapper.SetInput(aLineGrid);\r
146                 vtkActor aLineActor = new vtkActor();\r
147                 aLineActor.SetMapper(aLineMapper);\r
148                 \r
149                 linePoints.Delete();\r
150                 aLine.GetPointIds().Delete();\r
151             aLine.Delete();\r
152             aLineGrid.Delete();\r
153             aLineMapper.Delete();\r
154             \r
155             aLineActor.GetProperty().SetColor(0, 0, 0);\r
156             aLineActor.GetProperty().Delete();\r
157             \r
158             return aLineActor;\r
159         }\r
160         \r
161         /**\r
162          * Creates a line.\r
163          * \r
164          * @param p1\r
165          * @param p2\r
166          * @return\r
167          */\r
168         public static vtkActor createLineActor(Tuple3d p1, Tuple3d p2) {\r
169                 vtkPoints linePoints = new vtkPoints();\r
170                 linePoints.SetNumberOfPoints(2);\r
171                 linePoints.InsertPoint(0,p1.x, p1.y, p1.z);\r
172                 linePoints.InsertPoint(1,p2.x, p2.y, p2.z);\r
173                 vtkLine aLine = new vtkLine();\r
174                 aLine.GetPointIds().SetId(0, 0);\r
175                 aLine.GetPointIds().SetId(1, 1);\r
176                 vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();\r
177                 aLineGrid.Allocate(1, 1);\r
178                 aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());\r
179                 aLineGrid.SetPoints(linePoints);\r
180                 vtkDataSetMapper aLineMapper = new vtkDataSetMapper();\r
181                 aLineMapper.SetInput(aLineGrid);\r
182                 vtkActor aLineActor = new vtkActor();\r
183                 aLineActor.SetMapper(aLineMapper);\r
184                 //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);\r
185             \r
186             linePoints.Delete();\r
187             aLine.GetPointIds().Delete();\r
188             aLine.Delete();\r
189             aLineGrid.Delete();\r
190             aLineMapper.Delete();\r
191             \r
192             return aLineActor;\r
193         }\r
194 \r
195 }\r