]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axesSphereActor2.java
Copyrights
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / shape / axesSphereActor2.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g3d.vtk.shape;\r
13 \r
14 import javax.vecmath.AxisAngle4d;\r
15 import javax.vecmath.Matrix4d;\r
16 import javax.vecmath.Point3d;\r
17 import javax.vecmath.Vector3d;\r
18 \r
19 import org.simantics.g3d.math.MathTools;\r
20 import org.simantics.g3d.shape.Color4d;\r
21 import org.simantics.g3d.shape.Cone;\r
22 import org.simantics.g3d.shape.Cylinder;\r
23 import org.simantics.g3d.shape.Mesh;\r
24 import org.simantics.g3d.shape.Sphere;\r
25 \r
26 import vtk.vtkLinearTransform;\r
27 import vtk.vtkMatrix4x4;\r
28 import vtk.vtkRenderer;\r
29 import vtk.vtkTextActor;\r
30 \r
31 public class axesSphereActor2 extends MeshActor{\r
32         \r
33         private double axisLength;\r
34         private vtkRenderer ren;\r
35         private vtkTextActor xactor, yactor, zactor;\r
36         \r
37         public axesSphereActor2(vtkRenderer ren, double size) {\r
38                 int res = 16;\r
39                 axisLength = size;\r
40                 this.ren = ren;\r
41                 Mesh cone_x = Cone.create(size*0.3, res);\r
42                 Mesh cone_y = Cone.create(size*0.3, res);\r
43                 Mesh cone_z = Cone.create(size*0.3, res);\r
44                 cone_x.rotate(MathTools.getQuat(new AxisAngle4d(0,0,-1,Math.PI*0.5)));\r
45                 cone_z.rotate(MathTools.getQuat(new AxisAngle4d(1,0,0,Math.PI*0.5)));\r
46                 cone_x.translate(new Vector3d(size,0,0));\r
47                 cone_y.translate(new Vector3d(0,size,0));\r
48                 cone_z.translate(new Vector3d(0,0,size));\r
49                 \r
50                 Mesh tube_x = Cylinder.create(MathTools.ORIGIN, new Vector3d(size,0,0), size*0.1, res);\r
51                 Mesh tube_y = Cylinder.create(MathTools.ORIGIN, new Vector3d(0,size,0), size*0.1, res);\r
52                 Mesh tube_z = Cylinder.create(MathTools.ORIGIN, new Vector3d(0,0,size), size*0.1, res);\r
53                 \r
54                 Mesh sphere = Sphere.create(size*0.3, res, res*2/3);\r
55                 \r
56                 Color4d x_col = new Color4d(1,0,0,1);\r
57                 Color4d y_col = new Color4d(1,1,0,1);\r
58                 Color4d z_col = new Color4d(0,1,0,1);\r
59                 Color4d o_col = new Color4d(0,0,1,1);\r
60                 \r
61                 cone_x.setColor(x_col);\r
62                 tube_x.setColor(x_col);\r
63                 cone_y.setColor(y_col);\r
64                 tube_y.setColor(y_col);\r
65                 cone_z.setColor(z_col);\r
66                 tube_z.setColor(z_col);\r
67                 sphere.setColor(o_col);\r
68                 \r
69                 sphere.add(cone_x);\r
70                 sphere.add(tube_x);\r
71                 sphere.add(cone_y);\r
72                 sphere.add(tube_y);\r
73                 sphere.add(cone_z);\r
74                 sphere.add(tube_z);\r
75                 \r
76                 setMesh(sphere);\r
77                 \r
78                 \r
79                 \r
80                 xactor = new vtkTextActor();\r
81                 yactor = new vtkTextActor();\r
82                 zactor = new vtkTextActor();\r
83 \r
84                 xactor.SetInput("X");\r
85                 yactor.SetInput("Y");\r
86                 zactor.SetInput("Z");\r
87                 \r
88                 xactor.SetTextScaleModeToNone();\r
89                 yactor.SetTextScaleModeToNone();\r
90                 zactor.SetTextScaleModeToNone();\r
91                 \r
92                 xactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);\r
93                 xactor.GetTextProperty().ShadowOff();\r
94                 xactor.GetTextProperty().ItalicOff();\r
95                 xactor.GetTextProperty().BoldOff();\r
96 \r
97                 yactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);\r
98                 yactor.GetTextProperty().ShadowOff();\r
99                 yactor.GetTextProperty().ItalicOff();\r
100                 yactor.GetTextProperty().BoldOff();\r
101 \r
102                 zactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);\r
103                 zactor.GetTextProperty().ShadowOff();\r
104                 zactor.GetTextProperty().ItalicOff();\r
105                 zactor.GetTextProperty().BoldOff();\r
106                 \r
107                 xactor.GetTextProperty().Delete();\r
108                 yactor.GetTextProperty().Delete();\r
109                 zactor.GetTextProperty().Delete();\r
110 \r
111                 xactor.SetMaximumLineHeight(0.25);\r
112                 yactor.SetMaximumLineHeight(0.25);\r
113                 zactor.SetMaximumLineHeight(0.25);\r
114                 \r
115                 xactor.SetPickable(0);\r
116                 yactor.SetPickable(0);\r
117                 zactor.SetPickable(0);\r
118         }\r
119         \r
120         private boolean labelVisible = false;\r
121         public void setLabelVisibility(boolean ison) {\r
122                 if (ison == labelVisible)\r
123                         return;\r
124                 labelVisible = ison;\r
125                 if (labelVisible) {\r
126                         ren.AddActor2D(xactor);\r
127                         ren.AddActor2D(yactor);\r
128                         ren.AddActor2D(zactor);\r
129                         updateTextLoc();\r
130                 } else {\r
131                         ren.RemoveActor2D(xactor);\r
132                         ren.RemoveActor2D(yactor);\r
133                         ren.RemoveActor2D(zactor);\r
134                 }\r
135         \r
136         }\r
137         \r
138         Matrix4d m = new Matrix4d();\r
139         double mat[] = new double[16];\r
140         Point3d x = new Point3d();\r
141         Point3d y = new Point3d();\r
142         Point3d z = new Point3d();\r
143         \r
144         private void updateTextLoc() {\r
145                 if (!labelVisible)\r
146                         return;\r
147                 xactor.GetPositionCoordinate().SetCoordinateSystemToWorld();\r
148                 yactor.GetPositionCoordinate().SetCoordinateSystemToWorld();\r
149                 zactor.GetPositionCoordinate().SetCoordinateSystemToWorld();\r
150 \r
151                 GetMatrix(mat);\r
152                 MathTools.set(m, mat);\r
153                 x.set(axisLength, 0,0);\r
154                 y.set(0,axisLength, 0);\r
155                 z.set(0,0,axisLength);\r
156                 \r
157                 m.transform(x);\r
158                 m.transform(y);\r
159                 m.transform(z);\r
160                 xactor.GetPositionCoordinate().SetValue(x.x, x.y, x.z);\r
161                 yactor.GetPositionCoordinate().SetValue(y.x, y.y, y.z);\r
162                 zactor.GetPositionCoordinate().SetValue(z.x, z.y, z.z);\r
163                 \r
164                 xactor.GetPositionCoordinate().Delete();\r
165                 yactor.GetPositionCoordinate().Delete();\r
166                 zactor.GetPositionCoordinate().Delete();\r
167         }\r
168         \r
169         @Override\r
170         public void SetOrientation(double id0, double id1, double id2) {\r
171                 super.SetOrientation(id0, id1, id2);\r
172                 updateTextLoc();\r
173         }\r
174         \r
175         @Override\r
176         public void RotateWXYZ(double id0, double id1, double id2, double id3) {\r
177                 super.RotateWXYZ(id0, id1, id2, id3);\r
178                 updateTextLoc();\r
179         }\r
180         \r
181         @Override\r
182         public void SetPosition(double[] id0) {\r
183                 super.SetPosition(id0);\r
184                 updateTextLoc();\r
185         }\r
186         \r
187         @Override\r
188         public void SetPosition(double id0, double id1, double id2) {\r
189                 super.SetPosition(id0, id1, id2);\r
190                 updateTextLoc();\r
191         }\r
192         \r
193         @Override\r
194         public void SetOrientation(double[] id0) {\r
195                 super.SetOrientation(id0);\r
196                 updateTextLoc();\r
197         }\r
198         \r
199         @Override\r
200         public void SetScale(double id0) {\r
201                 super.SetScale(id0);\r
202                 updateTextLoc();\r
203         }\r
204         \r
205         @Override\r
206         public void SetScale(double id0, double id1, double id2) {\r
207                 super.SetScale(id0, id1, id2);\r
208                 updateTextLoc();\r
209         }\r
210         \r
211         @Override\r
212         public void SetScale(double[] id0) {\r
213                 super.SetScale(id0);\r
214                 updateTextLoc();\r
215         }\r
216         \r
217         @Override\r
218         public void SetUserMatrix(vtkMatrix4x4 id0) {\r
219                 super.SetUserMatrix(id0);\r
220                 updateTextLoc();\r
221         }\r
222         \r
223         @Override\r
224         public void SetUserTransform(vtkLinearTransform id0) {\r
225                 super.SetUserTransform(id0);\r
226                 updateTextLoc();\r
227         }\r
228         \r
229         @Override\r
230         public void Delete() {\r
231                 ren.RemoveActor(xactor);\r
232                 ren.RemoveActor(yactor);\r
233                 ren.RemoveActor(zactor);\r
234                 xactor.Delete();\r
235                 yactor.Delete();\r
236                 zactor.Delete();\r
237                 super.Delete();\r
238         }\r
239 }\r