GrxTransformItem.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  */
00010 
00011 
00012 package com.generalrobotix.ui.item;
00013 
00014 import java.util.List;
00015 import java.util.Vector;
00016 
00017 import org.eclipse.jface.dialogs.MessageDialog;
00018 
00019 import javax.media.j3d.BadTransformException;
00020 import javax.media.j3d.BranchGroup;
00021 import javax.media.j3d.Geometry;
00022 import javax.media.j3d.QuadArray;
00023 import javax.media.j3d.Shape3D;
00024 import javax.media.j3d.Switch;
00025 import javax.media.j3d.Transform3D;
00026 import javax.media.j3d.TransformGroup;
00027 import com.generalrobotix.ui.util.AxisAngle4d;
00028 
00029 import javax.vecmath.Color3f;
00030 import javax.vecmath.Matrix3d;
00031 import javax.vecmath.Matrix4d;
00032 import javax.vecmath.Point3f;
00033 import javax.vecmath.Vector3d;
00034 
00035 import jp.go.aist.hrp.simulator.ModelLoader;
00036 import jp.go.aist.hrp.simulator.ModelLoaderHelper;
00037 import jp.go.aist.hrp.simulator.ModelLoaderPackage.ModelLoaderException;
00038 import jp.go.aist.hrp.simulator.SceneInfo;
00039 import jp.go.aist.hrp.simulator.ShapePrimitiveType;
00040 
00041 import com.generalrobotix.ui.grxui.GrxUIPerspectiveFactory;
00042 import com.generalrobotix.ui.GrxBaseItem;
00043 import com.generalrobotix.ui.GrxPluginManager;
00044 import com.generalrobotix.ui.util.GrxCorbaUtil;
00045 import com.generalrobotix.ui.util.GrxShapeUtil;
00046 import com.generalrobotix.ui.util.MessageBundle;
00047 import com.generalrobotix.ui.view.tdview.SceneGraphModifier;
00048 
00052 @SuppressWarnings("serial")
00053 public class GrxTransformItem extends GrxBaseItem {
00054         public TransformGroup tg_;      
00055         public BranchGroup bg_;
00056         
00057         public GrxTransformItem parent_;
00058         public Vector<GrxTransformItem> children_;
00059         
00060         private Switch switchAxes_;
00061         private Switch switchBb_;
00062         protected GrxModelItem model_;
00063 
00069     public GrxTransformItem(String name, GrxPluginManager manager, GrxModelItem model) {
00070         super(name, manager);
00071         model_ = model;
00072         
00073         tg_ = new TransformGroup();
00074         tg_.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
00075         tg_.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
00076         tg_.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
00077         tg_.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
00078         tg_.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
00079 
00080         switchAxes_ = GrxShapeUtil.createAxes();
00081         tg_.addChild(switchAxes_);
00082         
00083         bg_ = new BranchGroup();
00084         bg_.setCapability(BranchGroup.ALLOW_DETACH);
00085         bg_.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
00086         bg_.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
00087         
00088         bg_.addChild(tg_);
00089 
00090         createBoundingBox();
00091         
00092         children_ = new Vector<GrxTransformItem>();   
00093 
00094     }
00095 
00100     public void addChild(GrxTransformItem child){
00101         children_.add(child);
00102         child.parent_ = this;
00103         tg_.addChild(child.bg_);
00104         resizeBoundingBox();
00105     }
00106 
00111     public void removeChild(GrxTransformItem child){
00112         children_.remove(child);
00113         child.bg_.detach();
00114         resizeBoundingBox();
00115     }
00116 
00120     public void delete() {
00121         try{
00122         // delete children first
00123         while (children_.size() > 0){
00124                 children_.get(0).delete();
00125         }
00126 
00127         if (parent_ != null){
00128                 parent_.removeChild(this);
00129                 // I don't know why the following line is required.
00130                 // But without the line, this transform is moved to the origin.
00131                         model_.calcForwardKinematics();
00132         }
00133         }catch(Exception ex){
00134                 ex.printStackTrace();
00135         }
00136                 manager_.itemChange(this, GrxPluginManager.REMOVE_ITEM);
00137     }
00138 
00143         public GrxModelItem model() {
00144                 return model_;
00145         }
00146         
00147         public void setFocused(boolean b){
00148                 super.setFocused(b);
00149                 switchAxes_.setWhichChild(b? Switch.CHILD_ALL:Switch.CHILD_NONE);
00150                 switchBb_.setWhichChild(b? Switch.CHILD_ALL:Switch.CHILD_NONE);
00151         }
00152         
00153         private void createBoundingBox(){
00154                  SceneGraphModifier modifier = SceneGraphModifier.getInstance();
00155                  modifier.init_ = true;
00156              modifier.mode_ = SceneGraphModifier.CREATE_BOUNDS;
00157              Color3f color = new Color3f(1.0f, 0.0f, 0.0f);
00158              switchBb_ =  SceneGraphModifier._makeSwitchNode(modifier._makeBoundingBox(color));
00159              tg_.addChild(switchBb_);
00160         }
00161         
00162         protected void resizeBoundingBox(){
00163         Transform3D trorg = new Transform3D();
00164                 tg_.getTransform(trorg);
00165         try{
00166                         Transform3D tr = new Transform3D(); 
00167                         tg_.setTransform(tr);
00168                         
00169                 SceneGraphModifier modifier = SceneGraphModifier.getInstance();
00170          
00171                 modifier.init_ = true;
00172                 modifier.mode_ = SceneGraphModifier.RESIZE_BOUNDS;
00173                 modifier._calcUpperLower(tg_, tr);
00174                 
00175                 Shape3D shapeNode = (Shape3D)switchBb_.getChild(0);
00176                 Geometry gm = (Geometry)shapeNode.getGeometry(0);
00177         
00178                 Point3f[] p3fW = modifier._makePoints();
00179                 if (gm instanceof QuadArray) {
00180                         QuadArray qa = (QuadArray) gm;
00181                         qa.setCoordinates(0, p3fW);
00182                         }
00183         
00184             }catch(Exception ex){
00185                         ex.printStackTrace();
00186 
00187             }
00188             tg_.setTransform(trorg);
00189     }
00190         
00191         public void gatherSensors(String type, List<GrxSensorItem> sensors){
00192                 if (this instanceof GrxSensorItem){
00193                         GrxSensorItem sensor = (GrxSensorItem)this;
00194                         if (sensor.type_.equals(type)){
00195                                 sensors.add(sensor);
00196                         }
00197                 }
00198                 for (int i=0; i<children_.size(); i++){
00199                         children_.get(i).gatherSensors(type, sensors);
00200                 }
00201         }
00202         
00203         // (Vector3)v = (Matrix4d)tg_*(Vector3d)v 
00204         public Vector3d transformV3  (Vector3d v){
00205                 Transform3D t3d = new Transform3D();
00206                 Vector3d p = new Vector3d();
00207                 Vector3d _v = new Vector3d(v);
00208         tg_.getTransform(t3d);
00209         t3d.transform(v, _v);
00210         t3d.get(p);
00211         _v.add(p);
00212         return _v;
00213         }
00214         
00215         public Switch getBBSwitch(){
00216                 resizeBoundingBox();
00217                 return switchBb_;
00218         }
00219 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:54