17 package com.generalrobotix.ui.view.tdview;
20 import java.awt.event.*;
21 import javax.media.j3d.*;
22 import javax.vecmath.*;
28 import com.sun.j3d.utils.picking.*;
30 class JointRotationHandler
extends OperationHandler {
33 private static final int MODE_NONE = 0;
34 private static final int DISK_MODE = 1;
35 private static final int CYLINDER_MODE = 2;
36 private static final float FACTOR = 0.004f;
39 private static final double THRESHOLD = 0.262f;
44 private TransformGroup tgTarget_;
45 private Point prevPoint_ =
new Point();
46 private double angle_;
47 private boolean isPicked_;
48 private Vector3d vectorCylinder_;
52 private Point3d pointTarget_;
56 public void processPicking(MouseEvent evt, BehaviorInfo
info) {
57 prevPoint_.x = evt.getPoint().x;
58 prevPoint_.y = evt.getPoint().y;
63 info.pickCanvas.setShapeLocation(prevPoint_.x, prevPoint_.y);
64 PickResult pickResult[] = info.pickCanvas.pickAllSorted();
65 if (pickResult ==
null) {
68 TransformGroup tg = (TransformGroup)pickResult[0].getNode(
69 PickResult.TRANSFORM_GROUP
71 GrxModelItem model = SceneGraphModifier.getModelFromTG(tg);
75 if(info.manager_.focusedItem()==model){
76 if( pickResult.length > 1)
77 tg = (TransformGroup)pickResult[1].getNode(
78 PickResult.TRANSFORM_GROUP );
83 setPickTarget(tg, info);
84 }
catch (CapabilityNotSetException ex) {
92 public void processStartDrag(MouseEvent evt, BehaviorInfo info) {
99 Transform3D target2vw =
new Transform3D();
100 Transform3D l2vw =
new Transform3D();
101 Transform3D tr =
new Transform3D();
102 tgTarget_.getLocalToVworld(l2vw);
103 tgTarget_.getTransform(tr);
104 target2vw.mul(l2vw, tr);
107 pointTarget_ =
new Point3d();
108 target2vw.transform(pointTarget_);
111 Transform3D vw2view =
new Transform3D();
112 tr =
new Transform3D();
113 l2vw =
new Transform3D();
114 TransformGroup tgView = info.drawable.getTransformGroupRoot();
115 tgView.getLocalToVworld(l2vw);
116 tgView.getTransform(tr);
117 vw2view.mul(l2vw, tr);
122 Point3f point000 =
new Point3f(0,0,0);
125 GrxLinkItem l = SceneGraphModifier.getLinkFromTG(tgTarget_);
126 if(l.jointAxis_==
null)
129 vw2view.mul(target2vw);
130 vw2view.transform(point000);
131 Vector3d vectorView =
new Vector3d(point000);
132 vectorCylinder_ =
new Vector3d(l.jointAxis_);
133 vw2view.transform(vectorCylinder_);
137 double angle = vectorView.angle(vectorCylinder_);
139 if(angle == Double.NaN) {
140 System.err.println(
"無効な値が入りました");
143 if (angle > Math.PI / 2.0) {
146 angle = Math.PI - angle;
149 if (angle < THRESHOLD) {
152 mode_ = CYLINDER_MODE;
156 public void processDragOperation(MouseEvent evt, BehaviorInfo info) {
161 Vector2d mouseMove =
new Vector2d(
162 FACTOR * (evt.getPoint().getX() - prevPoint_.getX()),
163 FACTOR * (evt.getPoint().getY() - prevPoint_.getY())
170 Point2d pointMouseOnPlane =
new Point2d();
172 Point3d pointTemp =
new Point3d(pointTarget_);
174 Canvas3D canvas = info.pickCanvas.getCanvas();
175 Transform3D vw2imagePlate =
new Transform3D();
176 canvas.getVworldToImagePlate(vw2imagePlate);
177 vw2imagePlate.transform(pointTemp);
178 canvas.getPixelLocationFromImagePlate(pointTemp, pointMouseOnPlane);
180 Vector2d prev =
new Vector2d(
181 prevPoint_.getX() - pointMouseOnPlane.x,
182 prevPoint_.getY() - pointMouseOnPlane.y
185 Vector2d current =
new Vector2d(
186 evt.getPoint().getX() - pointMouseOnPlane.x,
187 evt.getPoint().getY() - pointMouseOnPlane.y
192 angle_ = prev.angle(current);
194 Vector3d
cross =
new Vector3d();
195 cross.cross(vectorCylinder_,
new Vector3d(prev.x, prev.y, 0.0));
197 if (mouseMove.dot(
new Vector2d(cross.x, cross.y)) > 0.0) {
219 Vector2d vectorCylinder =
220 new Vector2d(- vectorCylinder_.y, vectorCylinder_.x);
223 vectorCylinder.normalize();
224 angle_ = vectorCylinder.dot(mouseMove);
230 prevPoint_.x = evt.getPoint().x;
231 prevPoint_.y = evt.getPoint().y;
232 _jointAngleChanged(info);
233 ((Grx3DView)info.drawable).showOption();
238 public void processReleased(MouseEvent evt, BehaviorInfo info) {
244 public boolean processTimerOperation(BehaviorInfo info) {
250 public void disableHandler() {
253 public void setPickTarget(TransformGroup tg, BehaviorInfo info) {
254 if (tg != tgTarget_) {
255 GrxLinkItem l = SceneGraphModifier.getLinkFromTG(tg);
256 if (l ==
null)
return;
257 info.manager_.focusedItem(l);
262 private void _jointAngleChanged(BehaviorInfo info) {
264 GrxModelItem model = SceneGraphModifier.getModelFromTG(tgTarget_);
265 GrxLinkItem link = SceneGraphModifier.getLinkFromTG(tgTarget_);
266 if( link.jointType_.equals(
"rotate") || link.jointType_.equals(
"slide") ){
267 link.jointValue(link.jointValue_+angle_);
268 link.setJointValuesWithinLimit();
269 model.updateInitialJointValue(link);
270 model.calcForwardKinematics();
272 }
catch (Exception e) {
#define null
our own NULL pointer
item corresponds to a robot model
Vector3 cross(const Vector3 &v1, const Vector3 &v2)