]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkEffect.java
Scale tooltip font by display DPI.
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / utils / vtkEffect.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.utils;
13
14 import javax.vecmath.AxisAngle4d;
15 import javax.vecmath.Vector3d;
16
17 import org.simantics.g3d.math.MathTools;
18 import org.simantics.g3d.vtk.shape.IvtkVisualObject;
19
20 import vtk.vtkActor;
21 import vtk.vtkCamera;
22 import vtk.vtkContourFilter;
23 import vtk.vtkDataObject;
24 import vtk.vtkOutlineFilter;
25 import vtk.vtkOutlineSource;
26 import vtk.vtkPolyData;
27 import vtk.vtkPolyDataMapper;
28 import vtk.vtkPolyDataSilhouette;
29 import vtk.vtkProp3D;
30 import vtk.vtkRenderWindow;
31 import vtk.vtkRenderer;
32 import vtk.vtkTransform;
33 import vtk.vtkTransformPolyDataFilter;
34 import vtk.vtkWindowToImageFilter;
35
36 public class vtkEffect {
37
38         private static vtkRenderer tmpRenderer;
39         
40         public static vtkRenderer getRenderer() {
41                 if (tmpRenderer == null)
42                         tmpRenderer = new vtkRenderer();
43                 return tmpRenderer;
44         }
45         public static vtkActor createContour(vtkProp3D tmpProp) {
46                 
47                 vtkRenderer tmpRenderer = getRenderer();
48                 tmpRenderer.SetBackground(1,1,1);
49                 
50                 
51                 if (tmpProp instanceof IvtkVisualObject) {
52                         ((IvtkVisualObject)tmpProp).addToRenderer();
53                 } else {
54                         tmpRenderer.AddActor(tmpProp);
55                 }
56                 
57                 if (tmpProp instanceof vtkActor) {
58                         ((vtkActor)tmpProp).GetProperty().SetColor(0, 0, 0);
59                 }
60                 
61                 
62                 double bounds_data[] = tmpProp.GetBounds();
63                 double center_data[] = tmpProp.GetCenter();
64                 
65                 tmpRenderer.ResetCamera();
66                 tmpRenderer.GetActiveCamera().SetParallelProjection(1);
67                 
68                 vtkRenderWindow tmpRenderWindow = new vtkRenderWindow();
69                 tmpRenderWindow.SetOffScreenRendering(1);
70                 tmpRenderWindow.AddRenderer(tmpRenderer);
71                 tmpRenderWindow.Render();
72                 
73                 vtkWindowToImageFilter windowToImageFilter = new vtkWindowToImageFilter();
74                 windowToImageFilter.SetInput(tmpRenderWindow);
75                 //windowToImageFilter.SetMagnification(2);
76                 windowToImageFilter.Update();
77                 
78                 vtkContourFilter contourFilter = new vtkContourFilter();
79                 contourFilter.SetInputConnection(windowToImageFilter.GetOutputPort());
80                 
81                 contourFilter.SetValue(0, 255);
82                 contourFilter.Update();
83                 
84                 vtkPolyData contour = contourFilter.GetOutput();
85                 
86                 double bounds_contour[] = new double[6]; 
87                 double center_contour[] = new double[3];
88                 double trans_x=0., trans_y=0., trans_z=0., ratio_x=0., ratio_y=0.;
89                 contour.GetBounds(bounds_contour);
90                 
91                 ratio_x = (bounds_data[1]-bounds_data[0])/(bounds_contour[1]-bounds_contour[0]);
92                 ratio_y = (bounds_data[3]-bounds_data[2])/(bounds_contour[3]-bounds_contour[2]);
93                 
94                 vtkTransform transform1 = new vtkTransform();
95                 transform1.Scale(ratio_x,ratio_y,1);
96                 
97                 vtkTransformPolyDataFilter tFilter1 = new vtkTransformPolyDataFilter();
98                 tFilter1.SetInputData(contour);
99                 tFilter1.SetTransform(transform1);
100                 tFilter1.Update();
101                 
102                 contour = tFilter1.GetOutput();
103                 
104                 contour.GetCenter(center_contour);
105                 trans_x = center_data[0]-center_contour[0];
106                 trans_y = center_data[1]-center_contour[1];
107                 trans_z = center_data[2]-center_contour[2];
108
109                 vtkTransform transform2 = new vtkTransform();
110                 transform2.Translate( trans_x, trans_y, trans_z);
111                 
112                 vtkTransformPolyDataFilter tFilter2 = new vtkTransformPolyDataFilter();
113                 tFilter2.SetInputData(contour);
114                 tFilter2.SetTransform(transform2);
115                 tFilter2.Update();
116                 
117                 contour = tFilter2.GetOutput();
118                 
119                 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
120                 mapper.SetInputData(contour);
121                 
122                 
123                 vtkActor actor =new vtkActor();
124                 actor.SetMapper(mapper);
125                 actor.GetProperty().SetLineWidth(2.);
126                 
127                 tmpRenderer.RemoveActor(tmpProp);
128                 
129                 tFilter1.Delete();
130                 tFilter2.Delete();
131                 contour.Delete();
132                 contourFilter.Delete();
133                 mapper.Delete();
134                 tmpRenderWindow.Delete();
135                 transform1.Delete();
136                 transform2.Delete();
137                 windowToImageFilter.Delete();
138                 
139                 return actor;
140         }
141         
142         public static vtkActor createContour(vtkProp3D tmpProp, vtkRenderer ren) {
143                 
144                 vtkRenderer tmpRenderer = getRenderer();
145                 tmpRenderer.SetBackground(1,1,1);
146                 
147                 
148                 if (tmpProp instanceof IvtkVisualObject) {
149                         ((IvtkVisualObject)tmpProp).addToRenderer();
150                 } else {
151                         tmpRenderer.AddActor(tmpProp);
152                 }
153                 
154                 if (tmpProp instanceof vtkActor) {
155                         ((vtkActor)tmpProp).GetProperty().SetColor(0, 0, 0);
156                 }
157                 
158                 
159                 //double bounds_data[] = tmpProp.GetBounds();
160                 double center_data[] = tmpProp.GetCenter();
161                 
162                 tmpRenderer.ResetCamera();
163                 
164                 vtkCamera camera = ren.GetActiveCamera();
165                 
166                 vtkCamera tmpCamera = tmpRenderer.GetActiveCamera(); 
167                 tmpCamera.SetParallelProjection(camera.GetParallelProjection());
168                 
169                 
170                 Vector3d pos = new Vector3d(camera.GetPosition());
171                 Vector3d foc = new Vector3d(camera.GetFocalPoint());
172                 Vector3d dir = new Vector3d();
173                 dir.sub(pos,foc);
174                 double l = dir.length();
175 //              dir.scale(1.0/l);
176 //              
177 //              
178 //
179 //              
180 //              dir.scale(2.0);
181 //              Vector3d tmpFoc = new Vector3d(tmpCamera.GetFocalPoint());
182 //              tmpFoc.add(dir);
183 //              tmpCamera.SetPosition(tmpFoc.x,tmpFoc.y,tmpFoc.z);
184 //              tmpCamera.SetRoll(camera.GetRoll());
185                 
186                 tmpCamera.DeepCopy(camera);
187                 //tmpCamera.SetModelTransformMatrix(camera.GetModelTransformMatrix());
188                 tmpCamera.UpdateViewport(tmpRenderer);
189                 
190                 vtkRenderWindow tmpRenderWindow = new vtkRenderWindow();
191                 tmpRenderWindow.SetOffScreenRendering(1);
192                 tmpRenderWindow.AddRenderer(tmpRenderer);
193                 tmpRenderWindow.Render();
194                 
195                 vtkWindowToImageFilter windowToImageFilter = new vtkWindowToImageFilter();
196                 windowToImageFilter.SetInput(tmpRenderWindow);
197                 //windowToImageFilter.SetMagnification(2);
198                 windowToImageFilter.Update();
199                 
200                 vtkContourFilter contourFilter = new vtkContourFilter();
201                 contourFilter.SetInputConnection(windowToImageFilter.GetOutputPort());
202                 
203                 contourFilter.SetValue(0, 255);
204                 contourFilter.Update();
205                 
206                 vtkPolyData contour = contourFilter.GetOutput();
207                 
208                 
209                 double or[] = camera.GetOrientationWXYZ();
210                 AxisAngle4d aa = new AxisAngle4d();
211                 aa.angle = -MathTools.degToRad(or[0]);
212                 aa.x = or[1];
213                 aa.y = or[2];
214                 aa.z = or[3];
215                 
216                 vtkTransform transform0 = new vtkTransform();
217                 transform0.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
218                 
219                 vtkTransformPolyDataFilter tFilter0 = new vtkTransformPolyDataFilter();
220                 tFilter0.SetInputData(contour);
221                 tFilter0.SetTransform(transform0);
222                 tFilter0.Update();
223                 
224                 contour.Delete();
225                 contour = tFilter0.GetOutput();
226                 
227                 double bounds_contour[] = new double[6]; 
228                 
229                 double trans_x=0., trans_y=0., trans_z=0., ratio_x=0.002, ratio_y=0.002, ratio_z=0.002;
230                 contour.GetBounds(bounds_contour);
231 //       
232 //              ratio_x = (bounds_data[1]-bounds_data[0])/(bounds_contour[1]-bounds_contour[0]);
233 //              ratio_y = (bounds_data[3]-bounds_data[2])/(bounds_contour[3]-bounds_contour[2]);
234 //              ratio_z = (bounds_data[5]-bounds_data[4])/(bounds_contour[5]-bounds_contour[4]);
235                 ratio_x = l*0.0008;
236                 ratio_y = l*0.0008;
237                 ratio_z = l*0.0008;
238                 
239                 
240                 System.out.println(ratio_x + " " + ratio_y);
241                 
242                 
243 //              
244                 vtkTransform transform1 = new vtkTransform();
245                 transform1.Scale(ratio_x,ratio_y,ratio_z);
246                 
247                 vtkTransformPolyDataFilter tFilter1 = new vtkTransformPolyDataFilter();
248                 tFilter1.SetInputData(contour);
249                 tFilter1.SetTransform(transform1);
250                 tFilter1.Update();
251                 
252                 contour.Delete();
253                 contour = tFilter1.GetOutput();
254                 
255                 double center_contour[] = new double[3];
256                 contour.GetCenter(center_contour);
257                 trans_x = center_data[0]-center_contour[0];
258                 trans_y = center_data[1]-center_contour[1];
259                 trans_z = center_data[2]-center_contour[2];
260 //              
261                 System.out.println(trans_x + " " + trans_y + " " + trans_z);
262 //
263                 vtkTransform transform2 = new vtkTransform();
264                 transform2.Translate( trans_x, trans_y, trans_z);
265                 
266                 vtkTransformPolyDataFilter tFilter2 = new vtkTransformPolyDataFilter();
267                 tFilter2.SetInputData(contour);
268                 tFilter2.SetTransform(transform2);
269                 tFilter2.Update();
270                 
271                 contour.Delete();
272                 contour = tFilter2.GetOutput();
273                 
274                 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
275                 mapper.SetInputData(contour);
276                 
277                 
278                 vtkActor actor =new vtkActor();
279                 actor.SetMapper(mapper);
280                 actor.GetProperty().SetLineWidth(2.);
281                 actor.GetProperty().SetColor(0,0,1);
282                 actor.GetProperty().Delete();
283                 
284                 tmpRenderer.RemoveActor(tmpProp);
285                 
286                 tFilter0.Delete();
287                 tFilter1.Delete();
288                 tFilter2.Delete();
289                 contour.Delete();
290                 contourFilter.Delete();
291                 mapper.Delete();
292                 tmpRenderWindow.Delete();
293                 transform0.Delete();
294                 transform1.Delete();
295                 transform2.Delete();
296                 windowToImageFilter.GetOutputPort().Delete();
297                 windowToImageFilter.Delete();
298                 
299                 ren.AddActor(actor);
300                 
301 //              vtkMatrix4x4 m = camera.GetModelTransformMatrix();
302 //              m.Invert();
303 //              actor.SetUserMatrix(m);
304                 //m.Delete();
305                 //actor.SetPosition(trans_x,trans_y, trans_z);
306                 //actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
307                 //
308                 return actor;
309         }
310         
311         public static vtkActor createSilhouette(vtkRenderer ren, vtkDataObject data) {
312                 
313                 
314                 vtkPolyDataSilhouette silhouette = new vtkPolyDataSilhouette();
315                 
316                 silhouette.SetInputData(data);
317                 silhouette.SetCamera(ren.GetActiveCamera());
318                 silhouette.SetEnableFeatureAngle(0);
319                 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
320                 
321                 mapper.SetInputConnection(silhouette.GetOutputPort());
322                 
323                 vtkActor actor = new vtkActor();
324                 actor.SetMapper(mapper);
325                 
326                 silhouette.GetOutputPort().Delete();
327                 silhouette.Delete();
328                 mapper.Delete();
329                 
330                 return actor;
331         }
332         
333         public static vtkActor createOutline(vtkDataObject data) {
334                 vtkOutlineFilter filter = new vtkOutlineFilter();
335                 filter.SetInputData(data);
336                 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
337                 mapper.SetInputConnection(filter.GetOutputPort());
338                 
339                 vtkActor actor = new vtkActor();
340                 actor.SetMapper(mapper);
341                 
342                 filter.GetOutputPort().Delete();
343                 filter.Delete();
344                 mapper.Delete();
345                 
346                 return actor;
347         }
348         
349         public static vtkActor createOutline(vtkProp3D prop) {
350
351                 double bounds[] = prop.GetBounds();
352
353                 vtkOutlineSource source = new vtkOutlineSource();
354                 source.SetBounds(bounds);
355                 
356                 vtkOutlineFilter filter = new vtkOutlineFilter();
357                 filter.SetInputData(source.GetOutput());
358                 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
359                 mapper.SetInputConnection(filter.GetOutputPort());
360                 
361                 vtkActor actor = new vtkActor();
362                 actor.SetMapper(mapper);
363                 
364                 source.GetOutput().Delete();
365                 source.Delete();
366                 filter.GetOutputPort().Delete();
367                 filter.Delete();
368                 mapper.Delete();
369                 
370                 actor.SetPickable(0);
371                 return actor;
372         }
373 }