ObjectTranslationHandler.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.awt.*;
20 import java.awt.event.*;
21 import javax.media.j3d.*;
22 import javax.vecmath.*;
23 
24 
28 import com.sun.j3d.utils.picking.*;
29 
30 class ObjectTranslationHandler extends OperationHandler {
31  private static final float TRANSLATION_FACTOR = 0.002f;
32 
33  private TransformGroup tgTarget_;
34  private Vector3f norm_;
35  private Point prevPoint_ = new Point();
36  private boolean isPicked_;
37 
38  public void processPicking(MouseEvent evt, BehaviorInfo info) {
39  prevPoint_.x = evt.getPoint().x;
40  prevPoint_.y = evt.getPoint().y;
41 
42  norm_ = null;
43  isPicked_ = false;
44 
45  try {
46  info.pickCanvas.setShapeLocation(prevPoint_.x, prevPoint_.y);
47  PickResult pickResult = info.pickCanvas.pickClosest();
48  if (pickResult == null)
49  return;
50  TransformGroup tg =
51  (TransformGroup)pickResult.getNode(PickResult.TRANSFORM_GROUP);
52  GrxModelItem model = SceneGraphModifier.getModelFromTG(tg);
53  if (model == null)
54  return;
55  else
56  tg = model.getTransformGroupRoot();
57  if (tg == null)
58  return;
59  if (_enableBoundingBox(tg, info)) {
60  isPicked_ = true;
61  Point3d startPoint = info.pickCanvas.getStartPosition();
62  PickIntersection intersection = pickResult.getClosestIntersection(startPoint);
63  norm_ = new Vector3f(intersection.getPointNormal());
64  }
65 
66  } catch (CapabilityNotSetException ex) {
67  // もう出ることはないと思うが、読み込むモデルによっては
68  // 出るかもしれないので、スタックトレースは表示する。
69  ex.printStackTrace();
70  _disableBoundingBox();
71  }
72  }
73 
74  public void processStartDrag(MouseEvent evt, BehaviorInfo info) {
75  if (isPicked_) {
76  evt.consume();
77  }
78  }
79 
80  public void processDragOperation(MouseEvent evt, BehaviorInfo info) {
81  // tgViewからみたマウスの軌跡ベクトルmouseを求める。
82  // mouse-(norm_,mouse)norm_ がオブジェクトの移動ベクトル
83  if (isPicked_) {
84  if (norm_ != null) {
85  // ターゲットの座標系から視点座標系への変換を求める。
86  Transform3D tr = new Transform3D();
87  Transform3D l2vw = new Transform3D();
88  Transform3D trTarget2View = new Transform3D();
89 
90  tgTarget_.getLocalToVworld(l2vw);
91  tgTarget_.getTransform(tr);
92  trTarget2View.mul(l2vw, tr);
93 
94  TransformGroup tgView = info.drawable.getTransformGroupRoot();
95  tgView.getLocalToVworld(l2vw);
96  tgView.getTransform(tr);
97  l2vw.mul(tr);
98  l2vw.invert();
99 
100  l2vw.mul(trTarget2View);
101  trTarget2View.set(l2vw);
102 
103  // マウスの動きをターゲットの座標系の動きに変換
104  float fdx =
105  TRANSLATION_FACTOR *
106  (float)(evt.getPoint().getX() - prevPoint_.getX());
107  float fdy =
108  - TRANSLATION_FACTOR *
109  (float)(evt.getPoint().getY() - prevPoint_.getY());
110  Vector3f mouse = new Vector3f(fdx, fdy, 0.0f);
111  Vector3f normal = new Vector3f();
112 
113  trTarget2View.transform(norm_, normal);
114 
115  float inner = normal.dot(mouse);
116  normal.scale(inner);
117  mouse.sub(normal);
118 
119  trTarget2View.invert();
120  trTarget2View.transform(mouse);
121 
122  tr.set(mouse);
123  tgTarget_.getTransform(l2vw);
124  l2vw.mul(tr);
125  GrxLinkItem link = SceneGraphModifier.getLinkFromTG(tgTarget_);
126  link.setTransform(l2vw);
127  _transformChanged(info);
128 
129  prevPoint_.x = evt.getPoint().x;
130  prevPoint_.y = evt.getPoint().y;
131  }
132  ((Grx3DView)info.drawable).showOption();
133  evt.consume();
134  }
135  }
136 
137  public void processReleased(MouseEvent evt, BehaviorInfo info) {
138  if (isPicked_) {
139  evt.consume();
140  }
141  }
142 
143  public boolean processTimerOperation(BehaviorInfo info) {
144  return true;
145  }
146 
147  //--------------------------------------------------------------------
148  // OperationHandlerの実装
149  public void disableHandler() {
150  _disableBoundingBox();
151  }
152 
153  public void setPickTarget(TransformGroup tg, BehaviorInfo info) {
154  if (tg != tgTarget_) {
155  _enableBoundingBox(tg, info);
156  }
157  }
158 
159  //--------------------------------------------------------------------
160  // プライベートメソッド
161  private void _disableBoundingBox() {
162  if (tgTarget_ != null) {
163  tgTarget_ = null;
164  norm_ = null;
165  }
166  }
167 
168  private boolean _enableBoundingBox(TransformGroup tg, BehaviorInfo info) {
169  GrxModelItem model = SceneGraphModifier.getModelFromTG(tg);
170  if (model == null)
171  return false;
172 
173  tgTarget_ = tg;
174  info.manager_.focusedItem(model);
175  return true;
176  }
177 
178  private void _transformChanged(BehaviorInfo info) {
179  GrxModelItem model = SceneGraphModifier.getModelFromTG(tgTarget_);
180  if (model == null) {
181  System.out.println("no manipulatable.");
182  return;
183  }
184  model.calcForwardKinematics();
185  model.updateInitialTransformRoot();
186  }
187 }
int intersection(const fLineVec &lv1, const fLineVec &lv2, fVec3 &c1, fVec3 &c2, double &d, double eps)
Definition: fLineVec.cpp:36
#define null
our own NULL pointer
Definition: IceTypes.h:57
item corresponds to a robot model
backing_store_ptr info
Definition: jmemsys.h:181


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:24