17 package com.generalrobotix.ui.view.tdview;
21 import javax.vecmath.*;
22 import javax.media.j3d.*;
26 import com.sun.j3d.utils.picking.PickTool;
43 private float[]
lower_ =
new float[3];
44 private float[]
upper_ =
new float[3];
72 Object userData = tg.getUserData();
73 if (userData instanceof Hashtable)
74 return (Hashtable<String, Object>)userData;
138 tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
139 tg.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
140 tg.setCapability(TransformGroup.ALLOW_LOCAL_TO_VWORLD_READ);
150 for (
int i = 0;
i < 3;
i ++) {
155 if (node instanceof Group) {
157 if (node instanceof BranchGroup) {
158 BranchGroup bg = (BranchGroup)node;
159 for (
int i = 0;
i < bg.numChildren();
i++) {
164 if (node instanceof TransformGroup) {
165 Transform3D t3dLocal =
new Transform3D();
166 TransformGroup tg = (TransformGroup)node;
167 if (mode_ == CREATE_BOUNDS) {
170 tg.getTransform(t3dLocal);
171 Transform3D t3dWorld =
new Transform3D(t3dParent);
172 t3dWorld.mul(t3dLocal);
173 for (
int i = 0;
i < tg.numChildren();
i++) {
178 if ((node instanceof Switch)) {
186 Group group = (Group)node;
187 if (mode_ == CREATE_BOUNDS) {
188 group.setCapability(Group.ALLOW_CHILDREN_READ);
190 for (
int i = 0;
i < group.numChildren();
i++) {
195 }
else if (node instanceof Link) {
196 Link lk = (Link) node;
197 if (mode_ == CREATE_BOUNDS) {
198 lk.setCapability(Link.ALLOW_SHARED_GROUP_READ);
200 SharedGroup sg = lk.getSharedGroup();
201 if (mode_ == CREATE_BOUNDS) {
202 sg.setCapability(SharedGroup.ALLOW_CHILDREN_READ);
204 Group group = (Group) sg;
205 for (
int i = 0;
i < group.numChildren();
i++) {
208 }
else if (node instanceof Shape3D) {
209 Shape3D shape = (Shape3D) node;
210 Appearance appearance = shape.getAppearance();
212 if (mode_ == CREATE_BOUNDS) {
213 shape.setCapability(Node.ENABLE_PICK_REPORTING);
214 shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
215 PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
218 shape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
219 shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
221 if (appearance !=
null){
222 appearance.setCapability(
223 Appearance.ALLOW_POLYGON_ATTRIBUTES_READ
225 appearance.setCapability(
226 Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE
231 if (mode_ == SHADING_MODE) {
233 PolygonAttributes attributes =
new PolygonAttributes();
234 if (attributes ==
null) {
235 System.out.println(
"can't get PolygonAttributes");
238 attributes.setPolygonMode(polygonMode_);
239 attributes.setCullFace(cullFace_);
241 if(appearance!=
null){
242 appearance.setPolygonAttributes(attributes);
248 Geometry geometry = shape.getGeometry();
250 if (mode_ == CREATE_BOUNDS) {
251 geometry.setCapability(GeometryArray.ALLOW_COORDINATE_READ);
252 geometry.setCapability(GeometryArray.ALLOW_COUNT_READ);
255 if (geometry instanceof GeometryArray) {
256 GeometryArray ga = (GeometryArray)geometry;
257 for (
int i = 0;
i < ga.getVertexCount();
i++) {
258 float[] point =
new float[3];
259 ga.getCoordinate(
i, point);
260 Point3f point3f =
new Point3f(point);
261 t3dParent.transform(point3f);
262 point[0] = point3f.x;
263 point[1] = point3f.y;
264 point[2] = point3f.z;
273 for (
int i = 0;
i < 3;
i++) {
274 upper_[
i] = point[
i];
275 lower_[
i] = point[
i];
279 for (
int i = 0;
i < 3;
i++) {
280 if (point[
i] > upper_[
i]) upper_[
i] = point[
i];
281 else if (point[i] < lower_[i]) lower_[
i] = point[
i];
295 float[] upper =
new float[]{x/2,y/2,z/2};
296 float[] lower =
new float[]{-x/2,-y/2,-z/2};
306 public static Point3f[]
_makePoints(
float[] upper,
float[] lower) {
307 Point3f[] points =
new Point3f[24];
308 points[0] =
new Point3f(upper[0], upper[1], upper[2]);
309 points[1] =
new Point3f(lower[0], upper[1], upper[2]);
310 points[2] =
new Point3f(lower[0], lower[1], upper[2]);
311 points[3] =
new Point3f(upper[0], lower[1], upper[2]);
312 points[4] =
new Point3f(upper[0], lower[1], lower[2]);
313 points[5] =
new Point3f(lower[0], lower[1], lower[2]);
314 points[6] =
new Point3f(lower[0], upper[1], lower[2]);
315 points[7] =
new Point3f(upper[0], upper[1], lower[2]);
316 points[8] =
new Point3f(upper[0], upper[1], upper[2]);
317 points[9] =
new Point3f(upper[0], lower[1], upper[2]);
318 points[10] =
new Point3f(upper[0], lower[1], lower[2]);
319 points[11] =
new Point3f(upper[0], upper[1], lower[2]);
320 points[12] =
new Point3f(lower[0], upper[1], lower[2]);
321 points[13] =
new Point3f(lower[0], lower[1], lower[2]);
322 points[14] =
new Point3f(lower[0], lower[1], upper[2]);
323 points[15] =
new Point3f(lower[0], upper[1], upper[2]);
324 points[16] =
new Point3f(upper[0], lower[1], upper[2]);
325 points[17] =
new Point3f(lower[0], lower[1], upper[2]);
326 points[18] =
new Point3f(lower[0], lower[1], lower[2]);
327 points[19] =
new Point3f(upper[0], lower[1], lower[2]);
328 points[20] =
new Point3f(upper[0], upper[1], lower[2]);
329 points[21] =
new Point3f(lower[0], upper[1], lower[2]);
330 points[22] =
new Point3f(lower[0], upper[1], upper[2]);
331 points[23] =
new Point3f(upper[0], upper[1], upper[2]);
336 Vector3f[] normal =
new Vector3f[24];
337 normal[0] =
new Vector3f(0.0
f, 0.0
f, 1.0
f);
338 normal[1] = normal[0];
339 normal[2] = normal[0];
340 normal[3] = normal[0];
341 normal[4] =
new Vector3f(0.0
f, 0.0
f, -1.0
f);
342 normal[5] = normal[4];
343 normal[6] = normal[4];
344 normal[7] = normal[4];
345 normal[8] =
new Vector3f(1.0
f, 0.0
f, 0.0
f);
346 normal[9] = normal[8];
347 normal[10] = normal[8];
348 normal[11] = normal[8];
349 normal[12] =
new Vector3f(-1.0
f, 0.0
f, 0.0
f);
350 normal[13] = normal[12];
351 normal[14] = normal[12];
352 normal[15] = normal[12];
353 normal[16] =
new Vector3f(0.0
f, -1.0
f, 0.0
f);
354 normal[17] = normal[16];
355 normal[18] = normal[16];
356 normal[19] = normal[16];
357 normal[20] =
new Vector3f(0.0
f, 1.0
f, 0.0
f);
358 normal[21] = normal[20];
359 normal[22] = normal[20];
360 normal[23] = normal[20];
364 public static Shape3D
_makeCube(Color3f color, Point3f[] points){
365 QuadArray quads =
new QuadArray(
367 QuadArray.COLOR_3 | QuadArray.COORDINATES | QuadArray.NORMALS
369 quads.setCapability(QuadArray.ALLOW_COORDINATE_READ);
370 quads.setCapability(QuadArray.ALLOW_COORDINATE_WRITE);
371 quads.setCoordinates(0, points);
373 Color3f[] colors =
new Color3f[points.length];
374 for (
int i = 0;
i < points.length;
i++) {
377 quads.setNormals(0, normals);
378 quads.setColors(0, colors);
379 PolygonAttributes attr =
new PolygonAttributes();
380 attr.setPolygonMode(PolygonAttributes.POLYGON_LINE);
381 attr.setCullFace(PolygonAttributes.CULL_NONE);
382 Appearance appearance =
new Appearance();
383 appearance.setPolygonAttributes(attr);
384 Shape3D shape =
new Shape3D(quads, appearance);
385 shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
386 shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
387 shape.setCapability(Node.ENABLE_PICK_REPORTING);
388 PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
403 Point3f[] points =
new Point3f[2];
405 float upperlen = (float)(
406 upper_[0]*jointAxis.x+
407 upper_[1]*jointAxis.y+
408 upper_[2]*jointAxis.z);
409 float lowerlen = (float)(
410 lower_[0]*jointAxis.x+
411 lower_[1]*jointAxis.y+
412 lower_[2]*jointAxis.z);
413 float offset = 0.05f;
414 if(upperlen < lowerlen)
417 points[0] =
new Point3f(
418 (upperlen + offset) * (
float)jointAxis.x,
419 (upperlen + offset) * (
float)jointAxis.y,
420 (upperlen + offset) * (
float)jointAxis.z
423 points[1] =
new Point3f(
424 (lowerlen - offset) * (
float)jointAxis.x,
425 (lowerlen - offset) * (
float)jointAxis.y,
426 (lowerlen - offset) * (
float)jointAxis.z
436 LineArray lines =
new LineArray(
438 LineArray.COLOR_3 | LineArray.COORDINATES | LineArray.NORMALS
440 lines.setCapability(LineArray.ALLOW_COORDINATE_WRITE);
441 lines.setCoordinates(0, points);
442 Color3f[] colors =
new Color3f[points.length];
443 Vector3f[] normals =
new Vector3f[points.length];
444 Color3f color =
new Color3f(1.0
f, 0.0
f, 1.0
f);
445 Vector3f normal =
new Vector3f(0.0
f, 0.0
f, 1.0
f);
446 for (
int i = 0;
i < points.length;
i++) {
450 lines.setNormals(0, normals);
451 lines.setColors(0, colors);
452 lines.setCapability(GeometryArray.ALLOW_COUNT_READ);
453 lines.setCapability(GeometryArray.ALLOW_FORMAT_READ);
454 lines.setCapability(GeometryArray.ALLOW_COORDINATE_READ);
455 LineAttributes lineAttr =
new LineAttributes();
456 lineAttr.setLineWidth(4.0
f);
457 Appearance appearance =
new Appearance();
458 appearance.setLineAttributes(lineAttr);
459 Shape3D shape =
new Shape3D(lines, appearance);
460 shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
461 shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
471 switchNode.addChild(shape);
476 Switch switchNode =
new Switch();
477 switchNode.setCapability(Switch.ALLOW_CHILDREN_EXTEND);
478 switchNode.setCapability(Switch.ALLOW_CHILDREN_READ);
479 switchNode.setCapability(Switch.ALLOW_CHILDREN_WRITE);
480 switchNode.setCapability(Switch.ALLOW_SWITCH_READ);
481 switchNode.setCapability(Switch.ALLOW_SWITCH_WRITE);
Shape3D _makeAxisLine(Vector3d jointAxis)
Point3f [] makeAxisPoints(Vector3d jointAxis)
make both end points of axis line
Shape3D _makeBoundingBox(Color3f color)
#define null
our own NULL pointer
static GrxModelItem getModelFromTG(TransformGroup tg)
get GrxModelItem associated with TransformGroup
static Point3f [] _makePoints(float x, float y, float z)
item corresponds to a robot model
static Vector3f [] _makeNormals()
static Point3f [] _makePoints(float[] upper, float[] lower)
make array of vertices of cube
static Switch _makeSwitchNode()
static Hashtable< String, Object > getHashtableFromTG(TransformGroup tg)
get hash table assigned to TransformGroup as user data
static Shape3D _makeCube(Color3f color, Point3f[] points)
void _setSegmentCapabilities(TransformGroup tg)
void _updateUpperLower(float[] point)
static SceneGraphModifier getInstance()
static SceneGraphModifier this_
static final int RESIZE_BOUNDS
static Switch _makeSwitchNode(Shape3D shape)
static GrxLinkItem getLinkFromTG(TransformGroup tg)
get GrxLinkItem associated with TransformGroup
static final int WIRE_FRAME
static final int SHADING_MODE
png_infop png_uint_32 flag
void _calcUpperLower(Node node, Transform3D t3dParent)
static final int CREATE_BOUNDS