17 package com.generalrobotix.ui.view.tdview;
18 import com.sun.j3d.utils.picking.*;
19 import com.sun.j3d.utils.geometry.Primitive;
20 import com.sun.j3d.utils.geometry.Cone;
21 import com.sun.j3d.utils.geometry.Cylinder;
22 import com.sun.j3d.utils.geometry.Sphere;
24 import javax.media.j3d.*;
25 import javax.vecmath.*;
35 Point3f p3fIntersectVW;
39 Point3d[] p3dPrimitivePoints;
42 TransformGroup tgFittingTarget;
43 TransformGroup tgFittingTargetBefore;
47 BranchGroup bgAddTop =
new BranchGroup();
50 TransformGroup tgArrowTranslate =
new TransformGroup();
51 TransformGroup tgArrowRotate =
new TransformGroup();
71 bgAddTop.setCapability(BranchGroup.ALLOW_DETACH);
72 tgArrowTranslate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
73 tgArrowTranslate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
74 tgArrowTranslate.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
75 tgArrowTranslate.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
76 tgArrowTranslate.setCapability(
77 TransformGroup.ALLOW_LOCAL_TO_VWORLD_READ
79 tgArrowRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
80 tgArrowRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
81 float fScaleFactor = 0.1f;
82 Appearance spherelook =
new Appearance();
83 Color3f black =
new Color3f(0.0
f, 0.0
f, 0.0
f);
84 Color3f white =
new Color3f(1.0
f, 1.0
f, 1.0
f);
85 spherelook.setMaterial(
86 new Material(c3fSphere, black, c3fSphere, white, 50.0
f)
91 sph[
i] =
new Sphere(0.25
f * fScaleFactor, spherelook);
92 sph[
i].setPickable(
false);
93 sphTrans[
i] =
new TransformGroup ();
94 sphTrans[
i].setCapability (TransformGroup.ALLOW_TRANSFORM_READ);
95 sphTrans[
i].setCapability (TransformGroup.ALLOW_TRANSFORM_WRITE);
98 bgAddTop.addChild (sphTrans[
i]);
99 sphTrans[
i].addChild (sph[i]);
103 Appearance arrowlook =
new Appearance();
104 arrowlook.setMaterial(
105 new Material(c3fArrow, black, c3fArrow, white, 50.0
f)
112 Cone.GENERATE_NORMALS,
115 cone.setCapability(Primitive.ENABLE_GEOMETRY_PICKING);
116 for (
int i = Cone.BODY;
i <= Cone.CAP;
i ++) {
117 PickTool.setCapabilities(cone.getShape(
i), PickTool.INTERSECT_FULL);
119 cone.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
120 cone.setCapability(Primitive.ALLOW_PICKABLE_READ);
121 cone.setCapability(Primitive.ALLOW_PICKABLE_WRITE);
123 new Cylinder(0.2
f * fScaleFactor, 2.0
f * fScaleFactor, arrowlook);
124 cylinder.setCapability(Primitive.ENABLE_GEOMETRY_PICKING);
125 cylinder.setCapability(Primitive.ALLOW_PICKABLE_READ);
126 cylinder.setCapability(Primitive.ALLOW_PICKABLE_WRITE);
127 for(
int i = Cylinder.BODY;
i <= Cylinder.BOTTOM;
i ++) {
128 PickTool.setCapabilities(
129 cylinder.getShape(
i),
130 PickTool.INTERSECT_FULL
133 cylinder.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
135 TransformGroup tgCone =
new TransformGroup();
136 Transform3D t3dCone =
new Transform3D();
137 TransformGroup tgCylinder =
new TransformGroup();
138 Transform3D t3dCylinder =
new Transform3D();
139 t3dCone.setTranslation(
new Vector3f(0.0
f,1.5
f * fScaleFactor,0.0
f));
140 tgCone.setTransform(t3dCone);
141 tgCone.addChild(cone);
143 t3dCylinder.setTranslation(
144 new Vector3f(0.0
f, 2.0
f * fScaleFactor, 0.0
f)
146 t3dCylinder.setRotation(
147 new AxisAngle4f(
new Vector3f(1.0
f, 0.0
f, 0.0
f), (
float)Math.PI)
150 t3dCylinder.setTranslation(
151 new Vector3f(0.0
f, fScaleFactor, 0.0
f)
154 tgCylinder.addChild(tgCone);
155 tgCylinder.setTransform(t3dCylinder);
156 tgCylinder.addChild(cylinder);
157 tgArrowRotate.addChild(tgCylinder);
158 tgArrowTranslate.addChild(tgArrowRotate);
159 bgAddTop.addChild(tgArrowTranslate);
167 if(tgFittingTarget ==
null) {
168 System.err.println(
"物体を表示する対象の TG が設定されていません");
172 v3fNormalVW =
new Vector3f();
173 if (p3dPrimitivePoints.length >= 3) {
174 Point3d pt1 =
new Point3d();
175 Point3d pt2 =
new Point3d();
176 pt1.sub(p3dPrimitivePoints[1],p3dPrimitivePoints[0]);
177 pt2.sub(p3dPrimitivePoints[2],p3dPrimitivePoints[1]);
178 Vector3f vecOne =
new Vector3f(pt1);
179 Vector3f vecTwo =
new Vector3f(pt2);
181 v3fNormalVW.cross(vecOne,vecTwo);
184 v3fNormalVW.x * v3fNormalVW.x +
185 v3fNormalVW.y * v3fNormalVW.y +
186 v3fNormalVW.z * v3fNormalVW.z
188 v3fNormalVW.x /= dScale;
189 v3fNormalVW.y /= dScale;
190 v3fNormalVW.z /= dScale;
194 Transform3D t3dSph =
new Transform3D();
195 Transform3D t3dLocalToVworld =
new Transform3D();
196 Transform3D t3dCurrent =
new Transform3D();
197 this.tgFittingTarget.getLocalToVworld(t3dLocalToVworld);
198 this.tgFittingTarget.getTransform(t3dCurrent);
200 t3dLocalToVworld.mul(t3dCurrent);
201 t3dLocalToVworld.invert();
203 int iCoorsNum = p3dPrimitivePoints.length;
206 if(iCoorsNum <= iCount) {
207 t3dSph.set(
new Vector3f(p3dPrimitivePoints[iCoorsNum - 1]));
209 t3dLocalToVworld.transform(this.p3dPrimitivePoints[iCount]);
210 t3dSph.set(
new Vector3f(this.p3dPrimitivePoints[iCount]));
213 sphTrans[iCount].setTransform(t3dSph);
217 v3fNormal =
new Vector3f(this.v3fNormalVW);
218 t3dLocalToVworld.transform(this.v3fNormal);
219 p3fIntersect =
new Point3f(this.p3fIntersectVW);
220 t3dLocalToVworld.transform(this.p3fIntersect);
222 Transform3D t3dTranslate =
new Transform3D();
223 Transform3D t3dRotate =
new Transform3D();
224 t3dTranslate.set(
new Vector3f(p3fIntersect));
225 tgArrowTranslate.setTransform(t3dTranslate);
228 Vector3f v3fLine =
new Vector3f(0.0
f,1.0
f,0.0
f);
229 Vector3f v3fLineOther =
new Vector3f(0.0
f,0.0
f,1.0
f);
230 Vector3f v3fCross =
new Vector3f();
232 if (v3fNormal.y != -1.0f) {
233 v3fCross.cross(v3fLine,v3fNormal);
235 v3fCross.cross(v3fLineOther,v3fNormal);
237 t3dRotate.set(
new AxisAngle4f(v3fCross,v3fLine.angle(v3fNormal)));
238 tgArrowRotate.setTransform(t3dRotate);
240 if (bgAddTop.isLive()) {
243 for(
int i= 0;
i < tgFittingTargetBefore.numChildren();
i ++) {
244 if (bgAddTop == tgFittingTargetBefore.getChild(
i)) {
245 tgFittingTargetBefore.removeChild(
i);
250 tgFittingTarget.addChild(bgAddTop);
251 tgFittingTargetBefore = tgFittingTarget;
259 for (
int i = 0;
i < tgFittingTargetBefore.numChildren();
i ++) {
260 if(bgAddTop == tgFittingTargetBefore.getChild(
i)) {
261 tgFittingTargetBefore.removeChild(
i);
272 this.cone.setPickable(bPick);
273 this.cylinder.setPickable(bPick);
282 this.p3fIntersectVW =
new Point3f(p3fIntersect);
291 return this.p3fIntersectVW;
300 return this.v3fNormalVW;
309 this.p3dPrimitivePoints = ptCoordinates;
318 this.tgFittingTarget = tgFittingTarget;
327 return this.tgFittingTarget;
336 return this.tgArrowTranslate;
void setPickable(boolean bPick)
Vector3f getNormalVector()
#define null
our own NULL pointer
TransformGroup getTransformGroup()
static final int iNumberOfSphere
Point3f getIntersectPoint()
TransformGroup getArrowTransformGroup()
FittingInfo(Color3f c3fArrow, Color3f c3fSphere, boolean bInvertArrow)
void setTransformGroup(TransformGroup tgFittingTarget)
void setIntersectPoint(Point3f p3fIntersect)
void setPrimitiveCoordinates(Point3d[] ptCoordinates)