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