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