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