17 package com.generalrobotix.ui.view.tdview;
21 import java.awt.event.*;
22 import javax.media.j3d.*;
23 import javax.vecmath.*;
28 import com.sun.j3d.utils.picking.*;
30 class ObjectFittingHandler
extends OperationHandler {
33 public static final int FITTING_FROM = 1;
34 public static final int FITTING_TO = 2;
38 private FittingInfo fittingInfoFrom_;
39 private FittingInfo fittingInfoTo_;
42 private boolean bHaveFirst =
false;
43 private boolean bHaveSecond =
false;
44 private Point3f intersectVW_;
45 private Point3d[] verticesVW_;
49 public ObjectFittingHandler () {
50 fittingInfoFrom_ =
new FittingInfo(
51 new Color3f(1.0
f,0.0
f,0.0
f),
52 new Color3f(0.0
f,1.0
f,0.0
f),
56 fittingInfoTo_ =
new FittingInfo(
57 new Color3f(0.0
f,0.0
f,1.0
f),
58 new Color3f(0.0
f,1.0
f,0.0
f),
65 void setFittingMode(
int mode) {
72 public boolean fit(BehaviorInfo
info) {
74 System.err.println(
"二番目の TG が選択されていません");
78 System.err.println(
"一番目の TG が選択されていません");
87 Vector3f v3fCross =
new Vector3f();
88 Transform3D t3dRotate =
new Transform3D();
89 Vector3f v3fNormal = fittingInfoFrom_.getNormalVector();
90 Vector3f v3fNormalSecond = fittingInfoTo_.getNormalVector();
91 v3fCross.cross(v3fNormal,v3fNormalSecond);
93 if (v3fNormal.angle(v3fNormalSecond) == 0.0f) {
96 if (_isSameSignVector3f(v3fNormal, v3fNormalSecond)) {
99 new Vector3f(
new Point3f(1.0
f,0.0
f,0.0
f));
101 new Vector3f(
new Point3f(0.0
f,1.0
f,0.0
f));
102 if (v3fNormal.x != -1.0d) {
103 v3fCross.cross(vecXAxis,v3fNormal);
104 t3dRotate.set(
new AxisAngle4f(v3fCross,(
float)Math.PI));
106 v3fCross.cross(vecYAxis,v3fNormal);
107 t3dRotate.set(
new AxisAngle4f(v3fCross,(
float)Math.PI));
114 (
float)Math.PI + v3fNormal.angle(v3fNormalSecond)
120 Transform3D t3dCur =
new Transform3D();
122 GrxModelItem model = SceneGraphModifier.getModelFromTG(fittingInfoFrom_.getTransformGroup());
123 if (model ==
null)
return false;
124 TransformGroup tgCur = model.getTransformGroupRoot();
125 tgCur.getTransform(t3dCur);
129 Point3f p3dIntersect = fittingInfoFrom_.getIntersectPoint();
130 Point3f p3dIntersectSecond = fittingInfoTo_.getIntersectPoint();
131 t3dRotate.transform(p3dIntersect);
133 Point3d p3dMove =
new Point3d();
134 p3dMove.x = p3dIntersectSecond.x - p3dIntersect.x;
135 p3dMove.y = p3dIntersectSecond.y - p3dIntersect.y;
136 p3dMove.z = p3dIntersectSecond.z - p3dIntersect.z;
137 Vector3f v3fTranslate =
new Vector3f(p3dMove);
138 Transform3D t3dTranslate =
new Transform3D();
141 Transform3D t3dLocalToVworld =
new Transform3D();
142 Transform3D t3dCurrent =
new Transform3D();
143 tgCur.getLocalToVworld(t3dLocalToVworld);
144 tgCur.getTransform(t3dCurrent);
146 t3dLocalToVworld.mul(t3dCurrent);
147 t3dLocalToVworld.invert();
149 t3dTranslate.set(v3fTranslate);
153 t3dRotate.mul(t3dCur);
154 t3dTranslate.mul(t3dRotate);
156 AxisAngle4f axis =
new AxisAngle4f();
157 Quat4f quat =
new Quat4f();
158 t3dTranslate.get(quat);
159 t3dTranslate.get(v3fTranslate);
162 model.setTransformRoot(t3dTranslate);
165 _transformChanged(info, tgCur);
169 fittingInfoFrom_.removeForDisplay();
170 fittingInfoTo_.removeForDisplay();
175 fittingInfoFrom_.setTransformGroup(
null);
176 fittingInfoTo_.setTransformGroup(
null);
177 ((Grx3DView)info.drawable).showOption();
183 public void processPicking(MouseEvent evt, BehaviorInfo info) {
184 Point mouse = evt.getPoint();
187 info.pickCanvas.setShapeLocation(mouse.x, mouse.y);
189 PickResult pickResult[] = info.pickCanvas.pickAllSorted();
190 if (pickResult ==
null) {
193 TransformGroup tg = (TransformGroup)pickResult[0].getNode(PickResult.TRANSFORM_GROUP);
194 Point3d startPoint = info.pickCanvas.getStartPosition();
195 PickIntersection
intersection = pickResult[0].getClosestIntersection(startPoint);
196 GrxModelItem model = SceneGraphModifier.getModelFromTG(tg);
200 if(info.manager_.focusedItem()==model){
201 if( pickResult.length > 1){
202 tg = (TransformGroup)pickResult[1].getNode(PickResult.TRANSFORM_GROUP );
203 intersection = pickResult[1].getClosestIntersection(startPoint);
204 info.manager_.focusedItem(
null);
210 intersectVW_ =
new Point3f(intersection.getPointCoordinatesVW());
211 verticesVW_ = intersection.getPrimitiveCoordinatesVW();
212 _enableIndicator(tg, info);
213 }
catch (CapabilityNotSetException ex) {
216 ex.printStackTrace();
221 public void processStartDrag(MouseEvent evt, BehaviorInfo info) {}
222 public void processDragOperation(MouseEvent evt, BehaviorInfo info) {}
223 public void processReleased(MouseEvent evt, BehaviorInfo info) {}
224 public boolean processTimerOperation(BehaviorInfo info) {
230 public void disableHandler() {
234 public void setPickTarget(TransformGroup tg, BehaviorInfo info) {}
238 private void _enableIndicator(TransformGroup tg, BehaviorInfo info) {
243 String strFirst =
null;
244 String strSecond =
null;
245 Hashtable<String, Object> tgInfo;
248 if (tg == fittingInfoFrom_.getArrowTransformGroup()) {
250 fittingInfoFrom_.setTransformGroup(
null);
251 fittingInfoFrom_.removeForDisplay();
255 tgInfo = SceneGraphModifier.getHashtableFromTG(tg);
256 if (tgInfo ==
null)
return;
257 GrxModelItem model = SceneGraphModifier.getModelFromTG(tg);
258 strFirst = model.getName();
260 tgInfo = SceneGraphModifier.getHashtableFromTG(
261 fittingInfoTo_.getTransformGroup()
263 model = SceneGraphModifier.getModelFromTG(fittingInfoTo_.getTransformGroup());
265 strSecond = model.getName();
268 if (strFirst.equals(strSecond))
return;
270 fittingInfoFrom_.setPickable(
false);
271 fittingInfoFrom_.setIntersectPoint(intersectVW_);
272 fittingInfoFrom_.setPrimitiveCoordinates(verticesVW_);
273 fittingInfoFrom_.setTransformGroup(tg);
276 fittingInfoFrom_.addForDisplay();
281 if (tg == fittingInfoTo_.getArrowTransformGroup()) {
283 fittingInfoTo_.setTransformGroup(
null);
284 fittingInfoTo_.removeForDisplay();
288 tgInfo = SceneGraphModifier.getHashtableFromTG(tg);
291 model = SceneGraphModifier.getModelFromTG(tg);
292 strSecond = model.getName();
294 tgInfo = SceneGraphModifier.getHashtableFromTG(
295 fittingInfoFrom_.getTransformGroup()
297 model = SceneGraphModifier.getModelFromTG(fittingInfoFrom_.getTransformGroup());
299 strFirst = model.getName();
301 if (strSecond.equals(strFirst))
304 fittingInfoTo_.setPickable(
false);
305 fittingInfoTo_.setIntersectPoint(intersectVW_);
306 fittingInfoTo_.setPrimitiveCoordinates(verticesVW_);
307 fittingInfoTo_.setTransformGroup(tg);
309 fittingInfoTo_.addForDisplay();
316 private void _disableIndicator() {
319 fittingInfoFrom_.removeForDisplay();
320 fittingInfoFrom_.setTransformGroup(
null);
325 fittingInfoTo_.removeForDisplay();
326 fittingInfoTo_.setTransformGroup(
null);
330 private boolean _isSameSign(
float x,
float y) {
331 return (((x >= 0) || (y >= 0)) && ((x <= 0) || (y <= 0)));
334 private boolean _isSameSignVector3f(Vector3f v1, Vector3f v2) {
336 _isSameSign(v1.x, v2.x) &&
337 _isSameSign(v1.y, v2.y) &&
338 _isSameSign(v1.z, v2.z)
342 private void _transformChanged(BehaviorInfo info, TransformGroup tg) {
343 GrxModelItem model = SceneGraphModifier.getModelFromTG(tg);
345 System.out.println(
"no manipulatable.");
348 model.calcForwardKinematics();
349 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