WalkViewHandler.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 import com.sun.j3d.utils.picking.*;
00024 
00025 class WalkViewHandler extends ViewHandler {
00026     //--------------------------------------------------------------------
00027     // 定数
00028     private static final float ROTATION_FACTOR = 0.02f;
00029     private static final float TRANSLATION_FACTOR = 0.005f;
00030     private static final float ZOOM_FACTOR = 0.0005f;
00031     private static final float PAN_FACTOR = 0.0001f;
00032 
00033     //--------------------------------------------------------------------
00034     // インスタンス変数
00035     private Point3f pickPoint_;
00036     private Point prevPoint_;
00037     private float zoom_;
00038     private float pan_;
00039 
00040     WalkViewHandler() {
00041         pickPoint_ = new Point3f();
00042     }
00043 
00044     //--------------------------------------------------------------------
00045     // BehaviorHandlerの実装
00046     public void processPicking(MouseEvent evt, BehaviorInfo info) {
00047         prevPoint_ = new Point(evt.getPoint());
00048 
00049         info.pickCanvas.setShapeLocation(prevPoint_.x, prevPoint_.y);
00050         try {
00051             PickResult pickResult = info.pickCanvas.pickClosest();
00052             if (pickResult != null) {
00053                 Point3d startPoint = info.pickCanvas.getStartPosition();
00054                 PickIntersection intersection =
00055                     pickResult.getClosestIntersection(startPoint);
00056                 pickPoint_ = new Point3f(intersection.getPointCoordinatesVW());
00057             }
00058         } catch (CapabilityNotSetException ex) {
00059             return;
00060         }
00061     }
00062 
00063     public void processStartDrag(MouseEvent evt, BehaviorInfo info) {
00064         if (mode_[getMouseButtonMode(evt)] == ZOOM_MODE) {
00065             info.setTimerEnabled(true);
00066             System.out.println("timer start");
00067         }
00068     }
00069 
00070     public void processDragOperation(MouseEvent evt, BehaviorInfo info) {
00071         switch (mode_[getMouseButtonMode(evt)]) {
00072         case ROTATION_MODE:
00073             _rotation(evt, info);
00074             prevPoint_ = new Point(evt.getPoint());
00075             break;
00076         case TRANSLATION_MODE:
00077             _translation(evt, info);
00078             prevPoint_ = new Point(evt.getPoint());
00079             break;
00080         case ZOOM_MODE:
00081             _zoom(evt, info);
00082             break;
00083         }
00084     }
00085 
00086     public void processReleased(MouseEvent evt, BehaviorInfo info) {}
00087 
00088     public boolean processTimerOperation(BehaviorInfo info) {
00089         Transform3D tr = new Transform3D();
00090         Transform3D trView = new Transform3D();
00091         TransformGroup tgView = info.drawable.getTransformGroupRoot();
00092         tgView.getTransform(trView);
00093 
00094         tr.setRotation(
00095             new AxisAngle4f(
00096                 new Vector3f(0.0f, 1.0f, 0.0f),
00097                 pan_
00098             )
00099         );
00100         tr.setTranslation(new Vector3f(0.0f, 0.0f, zoom_));
00101 
00102         trView.mul(tr);
00103         info.drawable.setTransform(trView);
00104         return true;
00105     }
00106 
00107     //--------------------------------------------------------------------
00108     // プライベートメソッド
00109     private void _rotation(MouseEvent evt, BehaviorInfo info) {
00110         double dx =
00111             ROTATION_FACTOR * (prevPoint_.getX() - evt.getPoint().getX());
00112         double dy = 
00113             ROTATION_FACTOR * (prevPoint_.getY() - evt.getPoint().getY());
00114 
00115         // グローバル座標から視点座標への変換をもとめる。
00116         TransformGroup tgView = info.drawable.getTransformGroupRoot();
00117         Transform3D trView = new Transform3D();   // 視点の変換
00118         tgView.getTransform(trView);
00119 
00120         // グローバル座標系から視点座標系への変換
00121         Transform3D vw2view = new Transform3D();
00122         tgView.getLocalToVworld(vw2view);
00123         vw2view.mul(trView);
00124         vw2view.invert();
00125 
00126         // 視点から見たターゲットポイント
00127         Point3d pointTarget = new Point3d(pickPoint_);
00128         vw2view.transform(pointTarget);
00129 
00130         // 視点座標系をターゲットポイントまで移動
00131         Vector3d ray = new Vector3d(pointTarget);  // 視線ベクトル
00132         Transform3D trMove = new Transform3D();
00133         trMove.set(ray);
00134         trView.mul(trMove);
00135 
00136         // 視点座標系を回転
00137         Transform3D tr = new Transform3D();
00138         Vector3d zAxis = new Vector3d(0.0, 0.0, 1.0);
00139         vw2view.transform(zAxis);
00140         tr.set(new AxisAngle4d(zAxis, dx));
00141         trView.mul(tr);
00142 
00143         Vector3d xAxis = new Vector3d(1.0, 0.0, 0.0);
00144         tr.set(new AxisAngle4d(xAxis, dy));
00145         trView.mul(tr);
00146 
00147         // 視点座標系をターゲットポイントから離す
00148         trView.mulInverse(trMove);
00149 
00150         info.drawable.setTransform(trView);
00151     }
00152 
00153     private void _translation(MouseEvent evt, BehaviorInfo info) {
00154         float fdx =
00155             TRANSLATION_FACTOR *
00156                 (float)(evt.getPoint().getX() - prevPoint_.getX());
00157         float fdy =
00158             - TRANSLATION_FACTOR *
00159                 (float)(evt.getPoint().getY() - prevPoint_.getY());
00160 
00161         Transform3D tr = new Transform3D();
00162         Transform3D trView = new Transform3D();
00163         TransformGroup tgView = info.drawable.getTransformGroupRoot();
00164         tgView.getTransform(trView);
00165         tr.set(new Vector3f(fdx, fdy, 0.0f));
00166         trView.mul(tr);
00167         info.drawable.setTransform(trView);
00168     }
00169 
00170     private void _zoom(MouseEvent evt, BehaviorInfo info) {
00171         pan_ =
00172             - PAN_FACTOR * (float)(evt.getPoint().getX() - prevPoint_.getX());
00173         zoom_ =
00174             - ZOOM_FACTOR * (float)(evt.getPoint().getY() - prevPoint_.getY());
00175     }
00176 }


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