1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.vtk.utils;
14 import javax.vecmath.AxisAngle4d;
15 import javax.vecmath.Vector3d;
17 import org.simantics.g3d.math.MathTools;
18 import org.simantics.g3d.vtk.shape.IvtkVisualObject;
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;
30 import vtk.vtkRenderWindow;
31 import vtk.vtkRenderer;
32 import vtk.vtkTransform;
33 import vtk.vtkTransformPolyDataFilter;
34 import vtk.vtkWindowToImageFilter;
36 public class vtkEffect {
38 private static vtkRenderer tmpRenderer;
40 public static vtkRenderer getRenderer() {
41 if (tmpRenderer == null)
42 tmpRenderer = new vtkRenderer();
45 public static vtkActor createContour(vtkProp3D tmpProp) {
47 vtkRenderer tmpRenderer = getRenderer();
48 tmpRenderer.SetBackground(1,1,1);
51 if (tmpProp instanceof IvtkVisualObject) {
52 ((IvtkVisualObject)tmpProp).addToRenderer();
54 tmpRenderer.AddActor(tmpProp);
57 if (tmpProp instanceof vtkActor) {
58 ((vtkActor)tmpProp).GetProperty().SetColor(0, 0, 0);
62 double bounds_data[] = tmpProp.GetBounds();
63 double center_data[] = tmpProp.GetCenter();
65 tmpRenderer.ResetCamera();
66 tmpRenderer.GetActiveCamera().SetParallelProjection(1);
68 vtkRenderWindow tmpRenderWindow = new vtkRenderWindow();
69 tmpRenderWindow.SetOffScreenRendering(1);
70 tmpRenderWindow.AddRenderer(tmpRenderer);
71 tmpRenderWindow.Render();
73 vtkWindowToImageFilter windowToImageFilter = new vtkWindowToImageFilter();
74 windowToImageFilter.SetInput(tmpRenderWindow);
75 //windowToImageFilter.SetMagnification(2);
76 windowToImageFilter.Update();
78 vtkContourFilter contourFilter = new vtkContourFilter();
79 contourFilter.SetInputConnection(windowToImageFilter.GetOutputPort());
81 contourFilter.SetValue(0, 255);
82 contourFilter.Update();
84 vtkPolyData contour = contourFilter.GetOutput();
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);
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]);
94 vtkTransform transform1 = new vtkTransform();
95 transform1.Scale(ratio_x,ratio_y,1);
97 vtkTransformPolyDataFilter tFilter1 = new vtkTransformPolyDataFilter();
98 tFilter1.SetInputData(contour);
99 tFilter1.SetTransform(transform1);
102 contour = tFilter1.GetOutput();
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];
109 vtkTransform transform2 = new vtkTransform();
110 transform2.Translate( trans_x, trans_y, trans_z);
112 vtkTransformPolyDataFilter tFilter2 = new vtkTransformPolyDataFilter();
113 tFilter2.SetInputData(contour);
114 tFilter2.SetTransform(transform2);
117 contour = tFilter2.GetOutput();
119 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
120 mapper.SetInputData(contour);
123 vtkActor actor =new vtkActor();
124 actor.SetMapper(mapper);
125 actor.GetProperty().SetLineWidth(2.);
127 tmpRenderer.RemoveActor(tmpProp);
132 contourFilter.Delete();
134 tmpRenderWindow.Delete();
137 windowToImageFilter.Delete();
142 public static vtkActor createContour(vtkProp3D tmpProp, vtkRenderer ren) {
144 vtkRenderer tmpRenderer = getRenderer();
145 tmpRenderer.SetBackground(1,1,1);
148 if (tmpProp instanceof IvtkVisualObject) {
149 ((IvtkVisualObject)tmpProp).addToRenderer();
151 tmpRenderer.AddActor(tmpProp);
154 if (tmpProp instanceof vtkActor) {
155 ((vtkActor)tmpProp).GetProperty().SetColor(0, 0, 0);
159 //double bounds_data[] = tmpProp.GetBounds();
160 double center_data[] = tmpProp.GetCenter();
162 tmpRenderer.ResetCamera();
164 vtkCamera camera = ren.GetActiveCamera();
166 vtkCamera tmpCamera = tmpRenderer.GetActiveCamera();
167 tmpCamera.SetParallelProjection(camera.GetParallelProjection());
170 Vector3d pos = new Vector3d(camera.GetPosition());
171 Vector3d foc = new Vector3d(camera.GetFocalPoint());
172 Vector3d dir = new Vector3d();
174 double l = dir.length();
181 // Vector3d tmpFoc = new Vector3d(tmpCamera.GetFocalPoint());
183 // tmpCamera.SetPosition(tmpFoc.x,tmpFoc.y,tmpFoc.z);
184 // tmpCamera.SetRoll(camera.GetRoll());
186 tmpCamera.DeepCopy(camera);
187 //tmpCamera.SetModelTransformMatrix(camera.GetModelTransformMatrix());
188 tmpCamera.UpdateViewport(tmpRenderer);
190 vtkRenderWindow tmpRenderWindow = new vtkRenderWindow();
191 tmpRenderWindow.SetOffScreenRendering(1);
192 tmpRenderWindow.AddRenderer(tmpRenderer);
193 tmpRenderWindow.Render();
195 vtkWindowToImageFilter windowToImageFilter = new vtkWindowToImageFilter();
196 windowToImageFilter.SetInput(tmpRenderWindow);
197 //windowToImageFilter.SetMagnification(2);
198 windowToImageFilter.Update();
200 vtkContourFilter contourFilter = new vtkContourFilter();
201 contourFilter.SetInputConnection(windowToImageFilter.GetOutputPort());
203 contourFilter.SetValue(0, 255);
204 contourFilter.Update();
206 vtkPolyData contour = contourFilter.GetOutput();
209 double or[] = camera.GetOrientationWXYZ();
210 AxisAngle4d aa = new AxisAngle4d();
211 aa.angle = -MathTools.degToRad(or[0]);
216 vtkTransform transform0 = new vtkTransform();
217 transform0.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
219 vtkTransformPolyDataFilter tFilter0 = new vtkTransformPolyDataFilter();
220 tFilter0.SetInputData(contour);
221 tFilter0.SetTransform(transform0);
225 contour = tFilter0.GetOutput();
227 double bounds_contour[] = new double[6];
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);
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]);
240 System.out.println(ratio_x + " " + ratio_y);
244 vtkTransform transform1 = new vtkTransform();
245 transform1.Scale(ratio_x,ratio_y,ratio_z);
247 vtkTransformPolyDataFilter tFilter1 = new vtkTransformPolyDataFilter();
248 tFilter1.SetInputData(contour);
249 tFilter1.SetTransform(transform1);
253 contour = tFilter1.GetOutput();
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];
261 System.out.println(trans_x + " " + trans_y + " " + trans_z);
263 vtkTransform transform2 = new vtkTransform();
264 transform2.Translate( trans_x, trans_y, trans_z);
266 vtkTransformPolyDataFilter tFilter2 = new vtkTransformPolyDataFilter();
267 tFilter2.SetInputData(contour);
268 tFilter2.SetTransform(transform2);
272 contour = tFilter2.GetOutput();
274 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
275 mapper.SetInputData(contour);
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();
284 tmpRenderer.RemoveActor(tmpProp);
290 contourFilter.Delete();
292 tmpRenderWindow.Delete();
296 windowToImageFilter.GetOutputPort().Delete();
297 windowToImageFilter.Delete();
301 // vtkMatrix4x4 m = camera.GetModelTransformMatrix();
303 // actor.SetUserMatrix(m);
305 //actor.SetPosition(trans_x,trans_y, trans_z);
306 //actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
311 public static vtkActor createSilhouette(vtkRenderer ren, vtkDataObject data) {
314 vtkPolyDataSilhouette silhouette = new vtkPolyDataSilhouette();
316 silhouette.SetInputData(data);
317 silhouette.SetCamera(ren.GetActiveCamera());
318 silhouette.SetEnableFeatureAngle(0);
319 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
321 mapper.SetInputConnection(silhouette.GetOutputPort());
323 vtkActor actor = new vtkActor();
324 actor.SetMapper(mapper);
326 silhouette.GetOutputPort().Delete();
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());
339 vtkActor actor = new vtkActor();
340 actor.SetMapper(mapper);
342 filter.GetOutputPort().Delete();
349 public static vtkActor createOutline(vtkProp3D prop) {
351 double bounds[] = prop.GetBounds();
353 vtkOutlineSource source = new vtkOutlineSource();
354 source.SetBounds(bounds);
356 vtkOutlineFilter filter = new vtkOutlineFilter();
357 filter.SetInputData(source.GetOutput());
358 vtkPolyDataMapper mapper = new vtkPolyDataMapper();
359 mapper.SetInputConnection(filter.GetOutputPort());
361 vtkActor actor = new vtkActor();
362 actor.SetMapper(mapper);
364 source.GetOutput().Delete();
366 filter.GetOutputPort().Delete();
370 actor.SetPickable(0);