SceneGraphModifier.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
17 package com.generalrobotix.ui.view.tdview;
18 
19 import java.util.*;
20 
21 import javax.vecmath.*;
22 import javax.media.j3d.*;
23 
26 import com.sun.j3d.utils.picking.PickTool;
27 
28 public class SceneGraphModifier {
29  //--------------------------------------------------------------------
30  // 定数
31  public static final int CREATE_BOUNDS = 0;
32  public static final int RESIZE_BOUNDS = 1;
33  private static final int SHADING_MODE = 2;
34 
35  public static final int SHADING = 0;
36  public static final int WIRE_FRAME = 1;
37 
38  //private static String USERDATA_SWITCH = "this is bounding box switch.";
39 
40  //--------------------------------------------------------------------
41  // インスタンス変数
42  private static SceneGraphModifier this_;
43  private float[] lower_ = new float[3]; // 頂点のMax値
44  private float[] upper_ = new float[3]; // 頂点のMin値
45  public boolean init_;
46  private int polygonMode_;
47  private int cullFace_;
48  public int mode_;
49 
50  //--------------------------------------------------------------------
51  // コンストラクタ(Singleton pattern)
52  private SceneGraphModifier() {}
53 
54  //--------------------------------------------------------------------
55  // スタティックメソッド
56  public static SceneGraphModifier getInstance() {
57  if (this_ == null) {
58  this_ = new SceneGraphModifier();
59  }
60  return this_;
61  }
62 
68  public static Hashtable<String, Object> getHashtableFromTG(TransformGroup tg) {
69  if (tg == null)
70  return null;
71 
72  Object userData = tg.getUserData();
73  if (userData instanceof Hashtable)
74  return (Hashtable<String, Object>)userData;
75 
76  return null;
77  }
78 
84  public static GrxLinkItem getLinkFromTG(TransformGroup tg){
85  Hashtable<String, Object> htable = getHashtableFromTG(tg);
86  if (htable != null){
87  GrxLinkItem link = (GrxLinkItem)htable.get("linkInfo");
88  return link;
89  }
90  return null;
91  }
92 
98  public static GrxModelItem getModelFromTG(TransformGroup tg){
99  Hashtable<String, Object> htable = getHashtableFromTG(tg);
100  if (htable != null){
101  GrxModelItem model = (GrxModelItem)htable.get("object");
102  return model;
103  }
104  return null;
105  }
106  //--------------------------------------------------------------------
107  // 公開メソッド
108 /*
109  public void changeShadingMode(SimulationElementNode node, int mode) {
110  mode_ = SHADING_MODE;
111 
112  switch (mode) {
113  case SHADING:
114  polygonMode_ = PolygonAttributes.POLYGON_FILL;
115  cullFace_ = PolygonAttributes.CULL_BACK;
116  break;
117  case WIRE_FRAME:
118  polygonMode_ = PolygonAttributes.POLYGON_LINE;
119  cullFace_ = PolygonAttributes.CULL_BACK;
120  break;
121  }
122 
123  // ノードのRootのTransformGroupを取得
124  TransformGroup tg = node.getTransformGroupRoot();
125 
126  for (int i = 0; i < tg.numChildren(); i++) {
127  try {
128  _calcUpperLower(tg.getChild(i), new Transform3D());
129  } catch (CapabilityNotSetException ex) {
130  ex.printStackTrace();
131  }
132  }
133  }
134 */
135  //--------------------------------------------------------------------
136  // プライベートメソッド
137  private void _setSegmentCapabilities(TransformGroup tg) {
138  tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
139  tg.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
140  tg.setCapability(TransformGroup.ALLOW_LOCAL_TO_VWORLD_READ);
141  }
142 
148  public void _calcUpperLower(Node node, Transform3D t3dParent) {
149  if (init_) {
150  for (int i = 0; i < 3; i ++) {
151  upper_[i] = 0.0f;
152  lower_[i] = 0.0f;
153  }
154  }
155  if (node instanceof Group) {
156  // added for GrxUI
157  if (node instanceof BranchGroup) {
158  BranchGroup bg = (BranchGroup)node;
159  for (int i = 0; i < bg.numChildren(); i++) {
160  _calcUpperLower(bg.getChild(i), t3dParent);
161  }
162  } else
163  // added for GrxUI
164  if (node instanceof TransformGroup) {
165  Transform3D t3dLocal = new Transform3D();
166  TransformGroup tg = (TransformGroup)node;
167  if (mode_ == CREATE_BOUNDS) {
169  }
170  tg.getTransform(t3dLocal);
171  Transform3D t3dWorld = new Transform3D(t3dParent);
172  t3dWorld.mul(t3dLocal);
173  for (int i = 0; i < tg.numChildren(); i++) {
174  _calcUpperLower(tg.getChild(i), t3dWorld);
175  }
176  }else{
177  boolean flag = true;
178  if ((node instanceof Switch)) {
179  //Object obj = node.getUserData();
180  //if(!(obj instanceof String && USERDATA_SWITCH.equals(obj)))
181  //{
182  flag = false;
183  //}
184  }
185  if(flag){
186  Group group = (Group)node;
187  if (mode_ == CREATE_BOUNDS) {
188  group.setCapability(Group.ALLOW_CHILDREN_READ);
189  }
190  for (int i = 0; i < group.numChildren(); i++) {
191  _calcUpperLower(group.getChild(i), t3dParent);
192  }
193  }
194  }
195  } else if (node instanceof Link) {
196  Link lk = (Link) node;
197  if (mode_ == CREATE_BOUNDS) {
198  lk.setCapability(Link.ALLOW_SHARED_GROUP_READ);
199  }
200  SharedGroup sg = lk.getSharedGroup();
201  if (mode_ == CREATE_BOUNDS) {
202  sg.setCapability(SharedGroup.ALLOW_CHILDREN_READ);
203  }
204  Group group = (Group) sg;
205  for (int i = 0; i < group.numChildren(); i++) {
206  _calcUpperLower(group.getChild(i), t3dParent);
207  }
208  } else if (node instanceof Shape3D) {
209  Shape3D shape = (Shape3D) node;
210  Appearance appearance = shape.getAppearance();
211 
212  if (mode_ == CREATE_BOUNDS) {
213  shape.setCapability(Node.ENABLE_PICK_REPORTING);
214  shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
215  PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
216 
217  // for shading change
218  shape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
219  shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
220 
221  if (appearance != null){
222  appearance.setCapability(
223  Appearance.ALLOW_POLYGON_ATTRIBUTES_READ
224  );
225  appearance.setCapability(
226  Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE
227  );
228  }
229  }
230 
231  if (mode_ == SHADING_MODE) {
232  //PolygonAttributes attributes = appearance.getPolygonAttributes();
233  PolygonAttributes attributes = new PolygonAttributes();
234  if (attributes == null) {
235  System.out.println("can't get PolygonAttributes");
236  return;
237  }
238  attributes.setPolygonMode(polygonMode_);
239  attributes.setCullFace(cullFace_);
240  //System.out.println("koko" + attributes + appearance);
241  if(appearance!=null){
242  appearance.setPolygonAttributes(attributes);
243  }
244  return;
245  }
246 
247  // 三角形の頂点を求める
248  Geometry geometry = shape.getGeometry();
249 
250  if (mode_ == CREATE_BOUNDS) {
251  geometry.setCapability(GeometryArray.ALLOW_COORDINATE_READ);
252  geometry.setCapability(GeometryArray.ALLOW_COUNT_READ);
253  }
254 
255  if (geometry instanceof GeometryArray) { // GeometryArray
256  GeometryArray ga = (GeometryArray)geometry;
257  for (int i = 0; i < ga.getVertexCount(); i++) {
258  float[] point = new float[3]; // 頂点
259  ga.getCoordinate(i, point); // 頂点座標取得
260  Point3f point3f = new Point3f(point);
261  t3dParent.transform(point3f);
262  point[0] = point3f.x;
263  point[1] = point3f.y;
264  point[2] = point3f.z;
265  _updateUpperLower(point);
266  }
267  }
268  }
269  }
270 
271  private void _updateUpperLower(float[] point) {
272  if (init_) {
273  for (int i = 0; i < 3; i++) {
274  upper_[i] = point[i];
275  lower_[i] = point[i];
276  }
277  init_ = false;
278  } else {
279  for (int i = 0; i < 3; i++) {
280  if (point[i] > upper_[i]) upper_[i] = point[i];
281  else if (point[i] < lower_[i]) lower_[i] = point[i];
282  }
283  }
284  }
285 
290  public Point3f[] _makePoints() {
291  return _makePoints(upper_, lower_);
292  }
293 
294  public static Point3f[] _makePoints(float x, float y, float z){
295  float[] upper = new float[]{x/2,y/2,z/2};
296  float[] lower = new float[]{-x/2,-y/2,-z/2};
297  return _makePoints(upper, lower);
298  }
299 
306  public static Point3f[] _makePoints(float[] upper, float[] lower) {
307  Point3f[] points = new Point3f[24];
308  points[0] = new Point3f(upper[0], upper[1], upper[2]); // A
309  points[1] = new Point3f(lower[0], upper[1], upper[2]); // B
310  points[2] = new Point3f(lower[0], lower[1], upper[2]); // C
311  points[3] = new Point3f(upper[0], lower[1], upper[2]); // D
312  points[4] = new Point3f(upper[0], lower[1], lower[2]); // H
313  points[5] = new Point3f(lower[0], lower[1], lower[2]); // G
314  points[6] = new Point3f(lower[0], upper[1], lower[2]); // F
315  points[7] = new Point3f(upper[0], upper[1], lower[2]); // E
316  points[8] = new Point3f(upper[0], upper[1], upper[2]); // A
317  points[9] = new Point3f(upper[0], lower[1], upper[2]); // D
318  points[10] = new Point3f(upper[0], lower[1], lower[2]); // H
319  points[11] = new Point3f(upper[0], upper[1], lower[2]); // E
320  points[12] = new Point3f(lower[0], upper[1], lower[2]); // F
321  points[13] = new Point3f(lower[0], lower[1], lower[2]); // G
322  points[14] = new Point3f(lower[0], lower[1], upper[2]); // C
323  points[15] = new Point3f(lower[0], upper[1], upper[2]); // B
324  points[16] = new Point3f(upper[0], lower[1], upper[2]); // C
325  points[17] = new Point3f(lower[0], lower[1], upper[2]); // D
326  points[18] = new Point3f(lower[0], lower[1], lower[2]); // H
327  points[19] = new Point3f(upper[0], lower[1], lower[2]); // G
328  points[20] = new Point3f(upper[0], upper[1], lower[2]); // A
329  points[21] = new Point3f(lower[0], upper[1], lower[2]); // B
330  points[22] = new Point3f(lower[0], upper[1], upper[2]); // F
331  points[23] = new Point3f(upper[0], upper[1], upper[2]); // E
332  return points;
333  }
334 
335  private static Vector3f[] _makeNormals() {
336  Vector3f[] normal = new Vector3f[24];
337  normal[0] = new Vector3f(0.0f, 0.0f, 1.0f);
338  normal[1] = normal[0];
339  normal[2] = normal[0];
340  normal[3] = normal[0];
341  normal[4] = new Vector3f(0.0f, 0.0f, -1.0f);
342  normal[5] = normal[4];
343  normal[6] = normal[4];
344  normal[7] = normal[4];
345  normal[8] = new Vector3f(1.0f, 0.0f, 0.0f);
346  normal[9] = normal[8];
347  normal[10] = normal[8];
348  normal[11] = normal[8];
349  normal[12] = new Vector3f(-1.0f, 0.0f, 0.0f);
350  normal[13] = normal[12];
351  normal[14] = normal[12];
352  normal[15] = normal[12];
353  normal[16] = new Vector3f(0.0f, -1.0f, 0.0f);
354  normal[17] = normal[16];
355  normal[18] = normal[16];
356  normal[19] = normal[16];
357  normal[20] = new Vector3f(0.0f, 1.0f, 0.0f);
358  normal[21] = normal[20];
359  normal[22] = normal[20];
360  normal[23] = normal[20];
361  return normal;
362  }
363 
364  public static Shape3D _makeCube(Color3f color, Point3f[] points){
365  QuadArray quads = new QuadArray(
366  points.length,
367  QuadArray.COLOR_3 | QuadArray.COORDINATES | QuadArray.NORMALS
368  );
369  quads.setCapability(QuadArray.ALLOW_COORDINATE_READ);
370  quads.setCapability(QuadArray.ALLOW_COORDINATE_WRITE);
371  quads.setCoordinates(0, points);
372  Vector3f[] normals = _makeNormals();
373  Color3f[] colors = new Color3f[points.length];
374  for (int i = 0; i < points.length; i++) {
375  colors[i] = color;
376  }
377  quads.setNormals(0, normals);
378  quads.setColors(0, colors);
379  PolygonAttributes attr = new PolygonAttributes();
380  attr.setPolygonMode(PolygonAttributes.POLYGON_LINE);
381  attr.setCullFace(PolygonAttributes.CULL_NONE);
382  Appearance appearance = new Appearance();
383  appearance.setPolygonAttributes(attr);
384  Shape3D shape = new Shape3D(quads, appearance);
385  shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
386  shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
387  shape.setCapability(Node.ENABLE_PICK_REPORTING);
388  PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
389  return shape;
390  }
391 
392  public Shape3D _makeBoundingBox(Color3f color) {
393  Point3f[] points = _makePoints();
394  return _makeCube(color, points);
395  }
396 
402  public Point3f[] makeAxisPoints(Vector3d jointAxis){
403  Point3f[] points = new Point3f[2];
404 
405  float upperlen = (float)(
406  upper_[0]*jointAxis.x+
407  upper_[1]*jointAxis.y+
408  upper_[2]*jointAxis.z);
409  float lowerlen = (float)(
410  lower_[0]*jointAxis.x+
411  lower_[1]*jointAxis.y+
412  lower_[2]*jointAxis.z);
413  float offset = 0.05f;
414  if(upperlen < lowerlen)
415  offset = -0.05f;
416 
417  points[0] = new Point3f(
418  (upperlen + offset) * (float)jointAxis.x,
419  (upperlen + offset) * (float)jointAxis.y,
420  (upperlen + offset) * (float)jointAxis.z
421  ); // A
422 
423  points[1] = new Point3f(
424  (lowerlen - offset) * (float)jointAxis.x,
425  (lowerlen - offset) * (float)jointAxis.y,
426  (lowerlen - offset) * (float)jointAxis.z
427  ); // B
428  return points;
429  }
433  public Shape3D _makeAxisLine(Vector3d jointAxis) {
434  Point3f[] points = makeAxisPoints(jointAxis);
435 
436  LineArray lines = new LineArray(
437  points.length,
438  LineArray.COLOR_3 | LineArray.COORDINATES | LineArray.NORMALS
439  );
440  lines.setCapability(LineArray.ALLOW_COORDINATE_WRITE);
441  lines.setCoordinates(0, points); // 座標
442  Color3f[] colors = new Color3f[points.length];
443  Vector3f[] normals = new Vector3f[points.length];
444  Color3f color = new Color3f(1.0f, 0.0f, 1.0f);
445  Vector3f normal = new Vector3f(0.0f, 0.0f, 1.0f);
446  for (int i = 0; i < points.length; i++) {
447  colors[i] = color;
448  normals[i] = normal;
449  }
450  lines.setNormals(0, normals); // 法線
451  lines.setColors(0, colors); // 色
452  lines.setCapability(GeometryArray.ALLOW_COUNT_READ);
453  lines.setCapability(GeometryArray.ALLOW_FORMAT_READ);
454  lines.setCapability(GeometryArray.ALLOW_COORDINATE_READ);
455  LineAttributes lineAttr = new LineAttributes();
456  lineAttr.setLineWidth(4.0f);
457  Appearance appearance = new Appearance();
458  appearance.setLineAttributes(lineAttr);
459  Shape3D shape = new Shape3D(lines, appearance);
460  shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
461  shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
462  //shape.setCapability(Node.ENABLE_PICK_REPORTING);
463  return shape;
464  }
465 
469  public static Switch _makeSwitchNode(Shape3D shape) {
470  Switch switchNode = _makeSwitchNode();
471  switchNode.addChild(shape);
472  return switchNode;
473  }
474 
475  public static Switch _makeSwitchNode() {
476  Switch switchNode = new Switch();
477  switchNode.setCapability(Switch.ALLOW_CHILDREN_EXTEND);
478  switchNode.setCapability(Switch.ALLOW_CHILDREN_READ);
479  switchNode.setCapability(Switch.ALLOW_CHILDREN_WRITE);
480  switchNode.setCapability(Switch.ALLOW_SWITCH_READ);
481  switchNode.setCapability(Switch.ALLOW_SWITCH_WRITE);
482  //switchNode.setUserData(USERDATA_SWITCH);
483  //switchNode.setWhichChild(Switch.CHILD_ALL);
484  return switchNode;
485  }
486 }
Point3f[] makeAxisPoints(Vector3d jointAxis)
make both end points of axis line
* x
Definition: IceUtils.h:98
#define null
our own NULL pointer
Definition: IceTypes.h:57
static GrxModelItem getModelFromTG(TransformGroup tg)
get GrxModelItem associated with TransformGroup
static Point3f[] _makePoints(float x, float y, float z)
item corresponds to a robot model
static Point3f[] _makePoints(float[] upper, float[] lower)
make array of vertices of cube
png_uint_32 i
Definition: png.h:2735
static Hashtable< String, Object > getHashtableFromTG(TransformGroup tg)
get hash table assigned to TransformGroup as user data
static Shape3D _makeCube(Color3f color, Point3f[] points)
static GrxLinkItem getLinkFromTG(TransformGroup tg)
get GrxLinkItem associated with TransformGroup
* y
Definition: IceUtils.h:97
png_infop png_uint_32 flag
Definition: png.h:2159
void _calcUpperLower(Node node, Transform3D t3dParent)


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:25