]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/gizmo/vtkGizmo.java
47ee62b48bb5758a5d4ea857c8445b1a3c76c54b
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / gizmo / vtkGizmo.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.gizmo;
13
14 import java.util.Collection;
15
16 import javax.vecmath.AxisAngle4d;
17 import javax.vecmath.Tuple3d;
18 import javax.vecmath.Vector3d;
19
20 import org.simantics.g3d.gizmo.Gizmo;
21 import org.simantics.g3d.math.MathTools;
22 import org.simantics.g3d.vtk.common.VtkView;
23
24 import vtk.vtkProp;
25 import vtk.vtkProp3D;
26 import vtk.vtkRenderer;
27
28 public abstract class vtkGizmo implements Gizmo<vtkProp, VtkView> {
29
30         private VtkView view;
31         private Collection<vtkProp> gizmo;
32         
33         private Tuple3d position;
34         private AxisAngle4d orientation;
35         private Tuple3d scale;
36         
37         @Override
38         public void attach(VtkView renderingPart) {
39                 if (view != null)
40                         throw new RuntimeException("Gizmo is attached");
41                 view = renderingPart;
42                 
43                 attachActors();
44         }
45         
46         @Override
47         public void deattach() {
48                 if (view == null)
49                         throw new RuntimeException("Gizmo is not attached");
50                 deattachActors();
51                 view = null;
52         }
53         
54         public boolean isAttached() {
55                 return (view != null);
56         }
57         
58         protected void attachActors() {
59                 view.lock();
60                 gizmo = getGizmo();
61                 vtkRenderer ren1 = view.getRenderer();
62                 for (vtkProp p : gizmo) {
63                         ren1.AddActor(p);
64                 }
65                 if (position != null)
66                         setPosition(position);
67                 if (orientation != null)
68                         setRotation(orientation);
69                 if (scale != null)
70                         setScale(scale);
71                 view.unlock();
72         }
73         
74         protected void deattachActors() {
75                 view.lock();
76                 vtkRenderer ren1 = view.getRenderer();
77                 for (vtkProp p : gizmo) {
78                         ren1.RemoveActor(p);
79                 }
80                 view.unlock();
81         }
82         
83         @Override
84         public boolean isPartOf(vtkProp pickedObject) {
85                 for (vtkProp prop : gizmo) {
86                         if (prop.equals(pickedObject))
87                                 return true;
88                 }
89                 return false;
90         }
91         
92         public void setPosition(Tuple3d position) {
93                 this.position = position;
94                 for (vtkProp p : gizmo) {
95                         ((vtkProp3D)p).SetPosition(position.x, position.y, position.z);
96                 }
97         }
98         
99         public void setRotation(AxisAngle4d q) {
100                 this.orientation = q;
101                 for (vtkProp p : gizmo) {
102                         ((vtkProp3D)p).SetOrientation(0,0,0);
103                         ((vtkProp3D)p).RotateWXYZ(MathTools.radToDeg(q.angle), q.x, q.y, q.z);
104                 }
105         }
106         
107         
108         public void setScale(Tuple3d s) {
109                 this.scale = s;
110                 for (vtkProp p : gizmo) {
111                         ((vtkProp3D)p).SetScale(s.x, s.y, s.z);
112                 }
113         }
114         
115         public void setScale(double s) {
116                 this.scale = new Vector3d(s,s,s);
117                 for (vtkProp p : gizmo) {
118                         ((vtkProp3D)p).SetScale(s, s, s);
119                 }
120         }
121         
122         public abstract Collection<vtkProp> getGizmo();
123         
124 //      public double[] add(double[] color1, double[] color2) {
125 //              double[] result = new double[]{color1[0]+color2[0],color1[1],+color2[1],color1[2]+color2[2]};
126 //              return result;
127 //      }
128 //      
129 //      public double[] add(double[] color1, double[] color2, double[] color3) {
130 //              double[] result = new double[]{color1[0]+color2[0]+color3[0],color1[1],+color2[1]+color3[1],color1[2]+color2[2]+color3[2]};
131 //              return result;
132 //      }
133         
134         public double[] add(double[]... color) {
135                 double result[] = new double[]{0,0,0};
136                 for (double c[] : color) {
137                         for (int i = 0; i < 3; i++)
138                                 result[i] += c[i];
139                 }
140                 
141                 return result;
142         }
143         
144         
145         public VtkView getView() {
146                 return view;
147         }
148         
149         
150         
151 }