]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/gizmo/vtkGizmo.java
Eliminated NullPointerException in vtkGizmo.deattachActors().
[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                 if (ren1 != null) {
78                         for (vtkProp p : gizmo) {
79                                 ren1.RemoveActor(p);
80                         }
81                 }
82                 view.unlock();
83         }
84         
85         @Override
86         public boolean isPartOf(vtkProp pickedObject) {
87                 for (vtkProp prop : gizmo) {
88                         if (prop.equals(pickedObject))
89                                 return true;
90                 }
91                 return false;
92         }
93         
94         public void setPosition(Tuple3d position) {
95                 this.position = position;
96                 for (vtkProp p : gizmo) {
97                         ((vtkProp3D)p).SetPosition(position.x, position.y, position.z);
98                 }
99         }
100         
101         public void setRotation(AxisAngle4d q) {
102                 this.orientation = q;
103                 for (vtkProp p : gizmo) {
104                         ((vtkProp3D)p).SetOrientation(0,0,0);
105                         ((vtkProp3D)p).RotateWXYZ(MathTools.radToDeg(q.angle), q.x, q.y, q.z);
106                 }
107         }
108         
109         
110         public void setScale(Tuple3d s) {
111                 this.scale = s;
112                 for (vtkProp p : gizmo) {
113                         ((vtkProp3D)p).SetScale(s.x, s.y, s.z);
114                 }
115         }
116         
117         public void setScale(double s) {
118                 this.scale = new Vector3d(s,s,s);
119                 for (vtkProp p : gizmo) {
120                         ((vtkProp3D)p).SetScale(s, s, s);
121                 }
122         }
123         
124         public abstract Collection<vtkProp> getGizmo();
125         
126 //      public double[] add(double[] color1, double[] color2) {
127 //              double[] result = new double[]{color1[0]+color2[0],color1[1],+color2[1],color1[2]+color2[2]};
128 //              return result;
129 //      }
130 //      
131 //      public double[] add(double[] color1, double[] color2, double[] color3) {
132 //              double[] result = new double[]{color1[0]+color2[0]+color3[0],color1[1],+color2[1]+color3[1],color1[2]+color2[2]+color3[2]};
133 //              return result;
134 //      }
135         
136         public double[] add(double[]... color) {
137                 double result[] = new double[]{0,0,0};
138                 for (double c[] : color) {
139                         for (int i = 0; i < 3; i++)
140                                 result[i] += c[i];
141                 }
142                 
143                 return result;
144         }
145         
146         
147         public VtkView getView() {
148                 return view;
149         }
150         
151         
152         
153 }