ObjectTranslationHandler.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00017 package com.generalrobotix.ui.view.tdview;
00018 
00019 import java.awt.*;
00020 import java.awt.event.*;
00021 import javax.media.j3d.*;
00022 import javax.vecmath.*;
00023 
00024 
00025 import com.generalrobotix.ui.item.GrxLinkItem;
00026 import com.generalrobotix.ui.item.GrxModelItem;
00027 import com.generalrobotix.ui.view.Grx3DView;
00028 import com.sun.j3d.utils.picking.*;
00029 
00030 class ObjectTranslationHandler extends OperationHandler {
00031     private static final float TRANSLATION_FACTOR = 0.002f;
00032 
00033     private TransformGroup tgTarget_;
00034     private Vector3f norm_;
00035     private Point prevPoint_ = new Point();
00036     private boolean isPicked_;
00037 
00038     public void processPicking(MouseEvent evt, BehaviorInfo info) {
00039         prevPoint_.x = evt.getPoint().x;
00040         prevPoint_.y = evt.getPoint().y;
00041 
00042         norm_ = null;
00043         isPicked_ = false;
00044 
00045         try {
00046             info.pickCanvas.setShapeLocation(prevPoint_.x, prevPoint_.y);
00047             PickResult pickResult = info.pickCanvas.pickClosest();
00048             if (pickResult == null)
00049                 return;
00050             TransformGroup tg =
00051                 (TransformGroup)pickResult.getNode(PickResult.TRANSFORM_GROUP);
00052             GrxModelItem model = SceneGraphModifier.getModelFromTG(tg);
00053             if (model == null) 
00054                 return;
00055             else
00056                 tg = model.getTransformGroupRoot();
00057             if (tg == null)
00058                 return;
00059             if (_enableBoundingBox(tg, info)) {
00060                 isPicked_ = true;
00061                 Point3d startPoint = info.pickCanvas.getStartPosition();
00062                 PickIntersection intersection = pickResult.getClosestIntersection(startPoint);
00063                 norm_ = new Vector3f(intersection.getPointNormal());
00064             } 
00065             
00066         } catch (CapabilityNotSetException ex) {
00067             // もう出ることはないと思うが、読み込むモデルによっては
00068             // 出るかもしれないので、スタックトレースは表示する。
00069             ex.printStackTrace();
00070             _disableBoundingBox();
00071         }
00072     }
00073 
00074     public void processStartDrag(MouseEvent evt, BehaviorInfo info) {
00075         if (isPicked_) {
00076             evt.consume();
00077         }
00078     }
00079 
00080     public void processDragOperation(MouseEvent evt, BehaviorInfo info) {
00081         // tgViewからみたマウスの軌跡ベクトルmouseを求める。
00082         // mouse-(norm_,mouse)norm_ がオブジェクトの移動ベクトル
00083         if (isPicked_) {
00084             if (norm_ != null) {
00085                 // ターゲットの座標系から視点座標系への変換を求める。
00086                 Transform3D tr = new Transform3D();
00087                 Transform3D l2vw = new Transform3D();
00088                 Transform3D trTarget2View = new Transform3D();
00089          
00090                 tgTarget_.getLocalToVworld(l2vw);
00091                 tgTarget_.getTransform(tr);
00092                 trTarget2View.mul(l2vw, tr);
00093          
00094                 TransformGroup tgView = info.drawable.getTransformGroupRoot();
00095                 tgView.getLocalToVworld(l2vw);
00096                 tgView.getTransform(tr);
00097                 l2vw.mul(tr);
00098                 l2vw.invert();
00099          
00100                 l2vw.mul(trTarget2View);
00101                 trTarget2View.set(l2vw);
00102          
00103                 // マウスの動きをターゲットの座標系の動きに変換
00104                 float fdx =
00105                     TRANSLATION_FACTOR *
00106                         (float)(evt.getPoint().getX() - prevPoint_.getX());
00107                 float fdy =
00108                     - TRANSLATION_FACTOR *
00109                         (float)(evt.getPoint().getY() - prevPoint_.getY());
00110                 Vector3f mouse = new Vector3f(fdx, fdy, 0.0f);
00111                 Vector3f normal = new Vector3f();
00112          
00113                 trTarget2View.transform(norm_, normal);
00114          
00115                 float inner = normal.dot(mouse);
00116                 normal.scale(inner);
00117                 mouse.sub(normal);
00118          
00119                 trTarget2View.invert();
00120                 trTarget2View.transform(mouse);
00121          
00122                 tr.set(mouse);
00123                 tgTarget_.getTransform(l2vw);
00124                 l2vw.mul(tr);
00125                 GrxLinkItem link = SceneGraphModifier.getLinkFromTG(tgTarget_);
00126                 link.setTransform(l2vw);
00127                 _transformChanged(info);
00128          
00129                 prevPoint_.x = evt.getPoint().x;
00130                 prevPoint_.y = evt.getPoint().y;
00131             }
00132             ((Grx3DView)info.drawable).showOption();
00133             evt.consume();
00134         }
00135     }
00136 
00137     public void processReleased(MouseEvent evt, BehaviorInfo info) {
00138         if (isPicked_) {
00139             evt.consume();
00140         }
00141     }
00142 
00143     public boolean processTimerOperation(BehaviorInfo info) {
00144         return true;
00145     }
00146 
00147     //--------------------------------------------------------------------
00148     // OperationHandlerの実装 
00149     public void disableHandler() {
00150         _disableBoundingBox();
00151     }
00152 
00153     public void setPickTarget(TransformGroup tg, BehaviorInfo info) {
00154         if (tg != tgTarget_) {
00155             _enableBoundingBox(tg, info);
00156         }
00157     }
00158 
00159     //--------------------------------------------------------------------
00160     // プライベートメソッド
00161     private void _disableBoundingBox() {
00162         if (tgTarget_ != null) {
00163             tgTarget_ = null;
00164             norm_ = null;
00165         }
00166     }
00167 
00168     private boolean _enableBoundingBox(TransformGroup tg, BehaviorInfo info) {
00169         GrxModelItem model = SceneGraphModifier.getModelFromTG(tg);
00170         if (model == null) 
00171                 return false;
00172 
00173         tgTarget_ = tg;
00174         info.manager_.focusedItem(model);
00175         return true;
00176     }
00177 
00178     private void _transformChanged(BehaviorInfo info) {
00179         GrxModelItem model = SceneGraphModifier.getModelFromTG(tgTarget_);
00180         if (model == null) {
00181             System.out.println("no manipulatable.");
00182             return;
00183         }
00184         model.calcForwardKinematics();
00185         model.updateInitialTransformRoot();
00186     }
00187 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:17