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.*;
33 class ObjectRotationHandler
extends OperationHandler {
34 private static final float ROTATION_FACTOR = 0.006f;
36 private TransformGroup tgTarget_;
37 private Vector3f norm_;
38 private Vector3f axis_;
39 private Vector3f dir_ =
new Vector3f();
40 private Point prevPoint_ =
new Point();
41 private boolean isPicked_;
45 public void processPicking(MouseEvent evt, BehaviorInfo
info) {
46 prevPoint_.x = evt.getPoint().x;
47 prevPoint_.y = evt.getPoint().y;
53 info.pickCanvas.setShapeLocation(prevPoint_.x, prevPoint_.y);
54 PickResult pickResult = info.pickCanvas.pickClosest();
55 if (pickResult ==
null)
58 (TransformGroup)pickResult.getNode(PickResult.TRANSFORM_GROUP);
59 GrxModelItem model = SceneGraphModifier.getModelFromTG(tg);
63 tg = model.getTransformGroupRoot();
67 if (_enableBoundingBox(tg, info)) {
69 Point3d startPoint = info.pickCanvas.getStartPosition();
70 PickIntersection
intersection = pickResult.getClosestIntersection(startPoint);
71 norm_ =
new Vector3f(intersection.getPointNormal());
73 }
catch (CapabilityNotSetException ex) {
77 _disableBoundingBox();
81 public void processStartDrag(MouseEvent evt, BehaviorInfo info) {
85 Transform3D tr =
new Transform3D();
86 Transform3D l2vw =
new Transform3D();
87 Transform3D trTarget2View =
new Transform3D();
89 tgTarget_.getLocalToVworld(l2vw);
90 tgTarget_.getTransform(tr);
91 trTarget2View.mul(l2vw, tr);
93 TransformGroup tgView = info.drawable.getTransformGroupRoot();
94 tgView.getLocalToVworld(l2vw);
95 tgView.getTransform(tr);
99 l2vw.mul(trTarget2View);
100 trTarget2View.set(l2vw);
103 float fdx = (float)(evt.getPoint().getX() - prevPoint_.getX());
104 float fdy = (float)(evt.getPoint().getY() - prevPoint_.getY());
105 Vector3f mouse =
new Vector3f(fdx, - fdy, 0.0
f);
106 Vector3f normal =
new Vector3f();
108 trTarget2View.transform(norm_, normal);
110 float inner = normal.dot(mouse);
114 trTarget2View.invert();
115 trTarget2View.transform(mouse);
118 axis_ = _createAxisVector(mouse);
121 System.out.println(
"axis is null");
123 _disableBoundingBox();
127 dir_.cross(axis_, norm_);
133 public void processDragOperation(MouseEvent evt, BehaviorInfo info) {
139 Transform3D tr =
new Transform3D();
140 Transform3D l2vw =
new Transform3D();
141 Transform3D trTarget2View =
new Transform3D();
143 tgTarget_.getLocalToVworld(l2vw);
144 tgTarget_.getTransform(tr);
145 trTarget2View.mul(l2vw, tr);
147 TransformGroup tgView = info.drawable.getTransformGroupRoot();
148 tgView.getLocalToVworld(l2vw);
149 tgView.getTransform(tr);
153 l2vw.mul(trTarget2View);
154 trTarget2View.set(l2vw);
157 float fdx = (float)(evt.getPoint().getX() - prevPoint_.getX());
158 float fdy = (float)(evt.getPoint().getY() - prevPoint_.getY());
159 Vector3f mouse =
new Vector3f(fdx, - fdy, 0.0
f);
160 Vector3f normal =
new Vector3f();
162 trTarget2View.transform(norm_, normal);
164 float inner = normal.dot(mouse);
168 trTarget2View.invert();
169 trTarget2View.transform(mouse);
171 float angle = ROTATION_FACTOR * mouse.dot(dir_);
172 tgTarget_.getTransform(l2vw);
173 tr.set(
new AxisAngle4f(axis_, angle));
175 GrxLinkItem link = SceneGraphModifier.getLinkFromTG(tgTarget_);
176 link.setTransform(l2vw);
177 _transformChanged(info);
179 prevPoint_.x = evt.getPoint().x;
180 prevPoint_.y = evt.getPoint().y;
182 ((Grx3DView)info.drawable).showOption();
187 public void processReleased(MouseEvent evt, BehaviorInfo info) {
193 public boolean processTimerOperation(BehaviorInfo info) {
199 public void disableHandler() {
200 _disableBoundingBox();
203 public void setPickTarget(TransformGroup tg, BehaviorInfo info) {
204 if (tg != tgTarget_) {
205 _enableBoundingBox(tg, info);
211 private void _disableBoundingBox() {
212 if (tgTarget_ !=
null) {
218 private boolean _enableBoundingBox(TransformGroup tg, BehaviorInfo info) {
219 GrxModelItem model = SceneGraphModifier.getModelFromTG(tg);
224 info.manager_.focusedItem(model);
228 private Vector3f _createAxisVector(Vector3f mouse) {
229 if (1.0
f - Math.abs(norm_.x) < 0.000001) {
230 if (Math.abs(mouse.y) < Math.abs(mouse.z)) {
231 if (mouse.y > 0.0f) {
232 return new Vector3f(0.0
f, 1.0
f, 0.0
f);
234 return new Vector3f(0.0
f, -1.0
f, 0.0
f);
237 if (mouse.z > 0.0f) {
238 return new Vector3f(0.0
f, 0.0
f, 1.0
f);
240 return new Vector3f(0.0
f, 0.0
f, -1.0
f);
243 }
else if (1.0
f - Math.abs(norm_.y) < 0.000001) {
244 if (Math.abs(mouse.x) < Math.abs(mouse.z)) {
245 if (mouse.x > 0.0f) {
246 return new Vector3f(1.0
f, 0.0
f, 0.0
f);
248 return new Vector3f(-1.0
f, 0.0
f, 0.0
f);
251 if (mouse.z > 0.0f) {
252 return new Vector3f(0.0
f, 0.0
f, 1.0
f);
254 return new Vector3f(0.0
f, 0.0
f, -1.0
f);
257 }
else if (1.0
f - Math.abs(norm_.z) < 0.000001) {
258 if (Math.abs(mouse.x) < Math.abs(mouse.y)) {
259 if (mouse.x > 0.0f) {
260 return new Vector3f(1.0
f, 0.0
f, 0.0
f);
262 return new Vector3f(-1.0
f, 0.0
f, 0.0
f);
265 if (mouse.y > 0.0f) {
266 return new Vector3f(0.0
f, 1.0
f, 0.0
f);
268 return new Vector3f(0.0
f, -1.0
f, 0.0
f);
273 System.out.println(
"norm=" + norm_);
277 private void _transformChanged(BehaviorInfo info) {
278 GrxModelItem model = SceneGraphModifier.getModelFromTG(tgTarget_);
280 System.out.println(
"can't get manipulatable.");
284 model.calcForwardKinematics();
285 model.updateInitialTransformRoot();
int intersection(const fLineVec &lv1, const fLineVec &lv2, fVec3 &c1, fVec3 &c2, double &d, double eps)
#define null
our own NULL pointer
item corresponds to a robot model