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


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