00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 package com.generalrobotix.ui.item;
00019
00020 import java.awt.image.BufferedImage;
00021 import java.awt.image.DataBufferByte;
00022 import java.awt.image.DataBufferInt;
00023 import java.awt.image.DataBufferUShort;
00024 import java.util.List;
00025
00026 import javax.media.j3d.Appearance;
00027 import javax.media.j3d.BranchGroup;
00028 import javax.media.j3d.Geometry;
00029 import javax.media.j3d.GeometryArray;
00030 import javax.media.j3d.ImageComponent;
00031 import javax.media.j3d.ImageComponent2D;
00032 import javax.media.j3d.Material;
00033 import javax.media.j3d.Node;
00034 import javax.media.j3d.PolygonAttributes;
00035 import javax.media.j3d.QuadArray;
00036 import javax.media.j3d.Shape3D;
00037 import javax.media.j3d.Switch;
00038 import javax.media.j3d.Texture;
00039 import javax.media.j3d.Texture2D;
00040 import javax.media.j3d.TextureAttributes;
00041 import javax.media.j3d.Transform3D;
00042 import javax.media.j3d.TransformGroup;
00043 import javax.media.j3d.TransparencyAttributes;
00044 import javax.vecmath.Color3f;
00045 import javax.vecmath.Matrix3d;
00046 import javax.vecmath.Matrix4d;
00047 import javax.vecmath.Point2f;
00048 import javax.vecmath.Point3f;
00049 import javax.vecmath.Vector3d;
00050 import javax.vecmath.Vector3f;
00051
00052 import jp.go.aist.hrp.simulator.AppearanceInfo;
00053 import jp.go.aist.hrp.simulator.MaterialInfo;
00054 import jp.go.aist.hrp.simulator.SceneInfo;
00055 import jp.go.aist.hrp.simulator.ShapeInfo;
00056 import jp.go.aist.hrp.simulator.ShapePrimitiveType;
00057 import jp.go.aist.hrp.simulator.TextureInfo;
00058 import jp.go.aist.hrp.simulator.TransformedShapeIndex;
00059
00060 import org.eclipse.jface.action.Action;
00061 import org.eclipse.jface.dialogs.InputDialog;
00062 import org.eclipse.jface.dialogs.MessageDialog;
00063 import org.eclipse.osgi.util.NLS;
00064
00065 import com.generalrobotix.ui.GrxPluginManager;
00066 import com.generalrobotix.ui.util.AxisAngle4d;
00067 import com.generalrobotix.ui.util.MessageBundle;
00068 import com.generalrobotix.ui.view.tdview.SceneGraphModifier;
00069 import com.sun.j3d.utils.geometry.Box;
00070 import com.sun.j3d.utils.geometry.Cone;
00071 import com.sun.j3d.utils.geometry.Cylinder;
00072 import com.sun.j3d.utils.geometry.GeometryInfo;
00073 import com.sun.j3d.utils.geometry.NormalGenerator;
00074 import com.sun.j3d.utils.geometry.Primitive;
00075 import com.sun.j3d.utils.geometry.Sphere;
00076 import com.sun.j3d.utils.image.TextureLoader;
00077 import com.sun.j3d.utils.picking.PickTool;
00078
00079 @SuppressWarnings("serial")
00083 public class GrxShapeItem extends GrxShapeTransformItem{
00084 public boolean isInline_ = false;
00085 public ShapeInfo[] shapes_;
00086 public AppearanceInfo[] appearances_;
00087 public MaterialInfo[] materials_;
00088 public TextureInfo[] textures_;
00089
00090 private int primitiveFlag = Primitive.GEOMETRY_NOT_SHARED | Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS;
00091 private Appearance appearance_=null;
00092 private BranchGroup bg_;
00093
00104 public GrxShapeItem(String name, GrxPluginManager manager, GrxModelItem model){
00105 super(name, manager, model);
00106 bg_ = new BranchGroup();
00107 bg_.setCapability(BranchGroup.ALLOW_DETACH);
00108 bg_.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
00109 bg_.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
00110
00111 }
00112
00113 public void loadShape(Matrix4d shapeT, int index, Matrix4d segmentT) {
00114 GrxModelItem model = model();
00115
00116 shapes_ = new ShapeInfo[1];
00117 appearances_ = new AppearanceInfo[1];
00118 materials_ = new MaterialInfo[1];
00119 textures_ = new TextureInfo[1];
00120
00121 Matrix4d invSegmentT = new Matrix4d();
00122 invSegmentT.invert(segmentT);
00123 Matrix4d shapeT0 = new Matrix4d();
00124 shapeT0.mul(invSegmentT, shapeT);
00125 Transform3D t3d = new Transform3D(shapeT0);
00126
00127 setShapeInfofromModel((short) index, 0);
00128 Shape3D shape3d = createShape3D(shapes_[0], appearances_[0], materials_[0], textures_[0]);
00129 bg_.addChild(shape3d);
00130 tg_.addChild(bg_);
00131 setPrimitiveProperty(model.shapes[index]);
00132
00133 tg_.setTransform(t3d);
00134 setURL(model.shapes[index].url);
00135
00136 initialize(t3d);
00137
00138 }
00139
00140 public void createnewPrimitiveShape(int type){
00141 shapes_ = new ShapeInfo[1];
00142 shapes_[0] = new ShapeInfo();
00143 appearances_ = new AppearanceInfo[1];
00144 appearances_[0] = new AppearanceInfo();
00145 materials_ = new MaterialInfo[1];
00146 materials_[0] = new MaterialInfo();
00147 textures_ = new TextureInfo[1];
00148 textures_[0] = null;
00149
00150 Appearance appearance = createAppearance();
00151 materials_[0].diffuseColor = new float[3];
00152 materials_[0].diffuseColor[0] = materials_[0].diffuseColor[1] = materials_[0].diffuseColor[2] =0.8f;
00153 materials_[0].emissiveColor = new float[3];
00154 materials_[0].emissiveColor[0] = materials_[0].emissiveColor[1] = materials_[0].emissiveColor[2] =0.0f;
00155 materials_[0].specularColor = new float[3];
00156 materials_[0].specularColor[0] = materials_[0].specularColor[1] = materials_[0].specularColor[2] =0.0f;
00157 materials_[0].ambientIntensity = 0.2f;
00158 materials_[0].shininess = 0.2f;
00159 materials_[0].transparency = 0.0f;
00160 setMaterial(appearance, materials_[0]);
00161 appearance_ = appearance;
00162
00163 Primitive primitive=null;
00164 switch(type){
00165 case ShapePrimitiveType._SP_BOX :
00166 float[] size=new float[3];
00167 size[0] = size[1] = size[2] = 1.0f;
00168 primitive = new Box(size[0]/2, size[1]/2, size[2]/2, primitiveFlag, appearance);
00169 shapes_[0].primitiveType = ShapePrimitiveType.SP_BOX;
00170 shapes_[0].primitiveParameters = size;
00171 setFltAry("size", size);
00172 break;
00173 case ShapePrimitiveType._SP_CONE :
00174 float bottomRadius = 1.0f;
00175 float height = 2.0f;
00176 primitive = new Cone(bottomRadius, height, primitiveFlag, appearance);
00177 shapes_[0].primitiveType = ShapePrimitiveType.SP_CONE;
00178 float[] param = new float[4];
00179 param[0] = bottomRadius;
00180 param[1] = height;
00181 param[2] = 1;
00182 param[3] = 1;
00183 shapes_[0].primitiveParameters = param;
00184 setFlt("bottomRadius", bottomRadius);
00185 setFlt("height", height);
00186 setProperty("side","true");
00187 setProperty("bottom","true");
00188 break;
00189 case ShapePrimitiveType._SP_CYLINDER :
00190 float radius = 1.0f;
00191 height = 2.0f;
00192 primitive = new Cylinder(radius, height, primitiveFlag, appearance);
00193 shapes_[0].primitiveType = ShapePrimitiveType.SP_CYLINDER;
00194 param = new float[5];
00195 param[0] = radius;
00196 param[1] = height;
00197 param[2] = 1;
00198 param[3] = 1;
00199 param[4] = 1;
00200 shapes_[0].primitiveParameters = param;
00201 setFlt("radius", radius);
00202 setFlt("height", height);
00203 setProperty("side","true");
00204 setProperty("bottom","true");
00205 setProperty("top","true");
00206 break;
00207 case ShapePrimitiveType._SP_SPHERE :
00208 radius = 1.0f;
00209 primitive = new Sphere(radius, primitiveFlag, appearance);
00210 shapes_[0].primitiveType = ShapePrimitiveType.SP_SPHERE;
00211 param = new float[1];
00212 param[0] = radius;
00213 shapes_[0].primitiveParameters = param;
00214 setFlt("radius", radius);
00215 break;
00216 default :
00217 break;
00218 }
00219 bg_.addChild(primitive);
00220 Transform3D t3d = new Transform3D();
00221 tg_.setTransform(t3d);
00222 tg_.addChild(bg_);
00223
00224 initialize(t3d);
00225 setFltAry("diffuseColor", materials_[0].diffuseColor);
00226 }
00227
00228 public void loadInlineShape(Matrix4d[] shapeT, Matrix4d inlinedT, int[] index, Matrix4d segmentT){
00229 isInline_ = true;
00230 int n = index.length;
00231 shapes_ = new ShapeInfo[n];
00232 appearances_ = new AppearanceInfo[n];
00233 materials_ = new MaterialInfo[n];
00234 textures_ = new TextureInfo[n];
00235 for(int i=0; i<n; i++){
00236 setShapeInfofromModel((short) index[i], i);
00237 }
00238
00239 setShape(shapeT, inlinedT, n, segmentT);
00240 setURL(model().shapes[index[0]].url);
00241 }
00242
00243 private void setShape(Matrix4d[] shapeT, Matrix4d inlinedT, int n, Matrix4d segmentT){
00244 Matrix4d invertIT = new Matrix4d();
00245 invertIT.invert(inlinedT);
00246 for(int i=0; i<n; i++){
00247 TransformGroup tfg = new TransformGroup();
00248 Matrix4d shapeT0 = new Matrix4d();
00249 shapeT0.mul(invertIT, shapeT[i]);
00250 Transform3D transform3d = new Transform3D(shapeT0);
00251 tfg.setTransform(transform3d);
00252
00253 Shape3D linkShape3D = createShape3D(shapes_[i], appearances_[i], materials_[i], textures_[i]);
00254 tfg.addChild(linkShape3D);
00255 tg_.addChild(tfg);
00256 }
00257 Matrix4d invSegmentT = new Matrix4d();
00258 invSegmentT.invert(segmentT);
00259 Matrix4d inlinedT0 = new Matrix4d();
00260 inlinedT0.mul(invSegmentT, inlinedT);
00261 Transform3D t3d = new Transform3D(inlinedT0);
00262 tg_.setTransform(t3d);
00263
00264 initialize(t3d);
00265 }
00266
00267 public void loadnewInlineShape(SceneInfo sInfo){
00268 isInline_ = true;
00269 ShapeInfo[] shapes = sInfo.shapes();
00270 AppearanceInfo[] appearances = sInfo.appearances();
00271 MaterialInfo[] materials = sInfo.materials();
00272 TextureInfo[] textures = sInfo.textures();
00273
00274 TransformedShapeIndex[] tsi = sInfo.shapeIndices();
00275 int n = tsi.length;
00276 shapes_ = new ShapeInfo[n];
00277 appearances_ = new AppearanceInfo[n];
00278 materials_ = new MaterialInfo[n];
00279 textures_ = new TextureInfo[n];
00280 Matrix4d[] shapeT = new Matrix4d[n];
00281 for(int k=0; k<n; k++){
00282 ShapeInfo shapeInfo = shapes[tsi[k].shapeIndex];
00283 AppearanceInfo appearanceInfo = null;
00284 MaterialInfo materialInfo = null;
00285 TextureInfo textureInfo = null;
00286 if (shapeInfo.appearanceIndex >= 0){
00287 appearanceInfo = appearances[shapeInfo.appearanceIndex];
00288 if (appearanceInfo.materialIndex >= 0){
00289 materialInfo = materials[appearanceInfo.materialIndex];
00290 }
00291 if (appearanceInfo.textureIndex >= 0){
00292 textureInfo = textures[appearanceInfo.textureIndex];
00293 }
00294 }
00295 shapes_[k] = shapeInfo;
00296 appearances_[k] = appearanceInfo;
00297 materials_[k] = materialInfo;
00298 textures_[k] = textureInfo;
00299 double[] m = tsi[k].transformMatrix;
00300 shapeT[k] = new Matrix4d(m[0],m[1],m[2],m[3],m[4],m[5],m[6],m[7],m[8],m[9],m[10],m[11],0,0,0,1);
00301 }
00302 setShape(shapeT, new Matrix4d(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1), n, new Matrix4d(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1));
00303 }
00304
00305 protected void initialize(Transform3D t3d){
00306 Vector3d trans = new Vector3d();
00307 Matrix3d rotat = new Matrix3d();
00308 t3d.get(rotat, trans);
00309
00310 double [] pos = new double[3];
00311 trans.get(pos);
00312 translation(pos);
00313 AxisAngle4d a4d = new AxisAngle4d();
00314 rotat.normalize();
00315 a4d.setMatrix(rotat);
00316 double [] rot = new double[4];
00317 a4d.get(rot);
00318 rotation(rot);
00319
00320 resizeBoundingBox();
00321 getMenu().clear();
00322
00323 Action item;
00324
00325
00326 item = new Action(){
00327 public String getText(){
00328 return MessageBundle.get("GrxShapeItem.menu.rename");
00329 }
00330 public void run(){
00331 InputDialog dialog = new InputDialog( null, getText(),
00332 MessageBundle.get("GrxShapeItem.dialog.message.newName"), getName(),null);
00333 if ( dialog.open() == InputDialog.OK && dialog.getValue() != null)
00334 rename( dialog.getValue() );
00335 }
00336 };
00337 setMenuItem(item);
00338
00339
00340 item = new Action(){
00341 public String getText(){
00342 return MessageBundle.get("GrxShapeItem.menu.delete");
00343 }
00344 public void run(){
00345 String mes = MessageBundle.get("GrxShapeItem.dialog.message.delete");
00346 mes = NLS.bind(mes, new String[]{getName()});
00347
00348 if( MessageDialog.openQuestion( null, MessageBundle.get("GrxShapeItem.dialog.title.delete"),
00349 mes) ){
00350 delete();
00351 }
00352
00353 }
00354 };
00355 setMenuItem(item);
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381 setIcon("segment.png");
00382 }
00383
00384 protected Appearance createAppearance(){
00385 Appearance appearance = new Appearance();
00386 appearance.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
00387 appearance.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_READ);
00388 appearance.setCapability(Appearance.ALLOW_MATERIAL_READ);
00389 appearance.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
00390
00391 PolygonAttributes pa = new PolygonAttributes();
00392 pa.setCapability(PolygonAttributes.ALLOW_MODE_READ);
00393 pa.setCapability(PolygonAttributes.ALLOW_MODE_WRITE);
00394 pa.setPolygonMode(PolygonAttributes.POLYGON_FILL);
00395 pa.setCullFace(PolygonAttributes.CULL_NONE);
00396 pa.setBackFaceNormalFlip(true);
00397 appearance.setPolygonAttributes(pa);
00398
00399 return appearance;
00400 }
00401
00402 private void setShapeInfofromModel(short shapeIndex, int id){
00403 ShapeInfo shapeInfo = model().shapes[shapeIndex];
00404 AppearanceInfo appearanceInfo = null;
00405 MaterialInfo materialInfo = null;
00406 TextureInfo textureInfo = null;
00407 if (shapeInfo.appearanceIndex >= 0){
00408 appearanceInfo = model().appearances[shapeInfo.appearanceIndex];
00409 if (appearanceInfo.materialIndex >= 0){
00410 materialInfo = model().materials[appearanceInfo.materialIndex];
00411 }
00412 if (appearanceInfo.textureIndex >= 0){
00413 textureInfo = model().textures[appearanceInfo.textureIndex];
00414 }
00415 }
00416 shapes_[id] = shapeInfo;
00417 appearances_[id] = appearanceInfo;
00418 materials_[id] = materialInfo;
00419 textures_[id] = textureInfo;
00420 }
00421
00430 @SuppressWarnings("deprecation")
00431 private Shape3D createShape3D
00432 (ShapeInfo shapeInfo, AppearanceInfo appearanceInfo, MaterialInfo materialInfo, TextureInfo textureInfo){
00433
00434 GeometryInfo geometryInfo = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
00435
00436
00437 int numVertices = shapeInfo.vertices.length / 3;
00438 Point3f[] vertices = new Point3f[numVertices];
00439 for(int i=0; i < numVertices; ++i){
00440 vertices[i] = new Point3f(shapeInfo.vertices[i*3], shapeInfo.vertices[i*3+1], shapeInfo.vertices[i*3+2]);
00441 }
00442 geometryInfo.setCoordinates(vertices);
00443 geometryInfo.setCoordinateIndices(shapeInfo.triangles);
00444
00445 Appearance appearance = createAppearance();
00446 if (appearanceInfo != null){
00447 setColors(geometryInfo, shapeInfo, appearanceInfo);
00448 setNormals(geometryInfo, shapeInfo, appearanceInfo);
00449 if(appearanceInfo.solid){
00450 PolygonAttributes pa = appearance.getPolygonAttributes();
00451 pa.setCullFace(PolygonAttributes.CULL_BACK);
00452 appearance.setPolygonAttributes(pa);
00453 }
00454
00455 if(materialInfo != null)
00456 setMaterial( appearance, materialInfo);
00457
00458 if(textureInfo != null){
00459 setTexture( appearance, textureInfo);
00460
00461
00462 int numTexCoordinate = appearanceInfo.textureCoordinate.length / 2;
00463 Point2f[] texCoordinate = new Point2f[numTexCoordinate];
00464 for(int i=0, j=0; i<numTexCoordinate; i++)
00465 texCoordinate[i] = new Point2f( appearanceInfo.textureCoordinate[j++], appearanceInfo.textureCoordinate[j++] );
00466
00467 geometryInfo.setTextureCoordinates(texCoordinate);
00468 geometryInfo.setTextureCoordinateIndices(appearanceInfo.textureCoordIndices);
00469 }
00470 }
00471
00472 Shape3D shape3D = new Shape3D(geometryInfo.getGeometryArray());
00473 shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
00474 shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
00475 shape3D.setCapability(GeometryArray.ALLOW_COORDINATE_READ);
00476 shape3D.setCapability(GeometryArray.ALLOW_COUNT_READ);
00477 PickTool.setCapabilities(shape3D, PickTool.INTERSECT_FULL);
00478 appearance_ = appearance;
00479 shape3D.setAppearance(appearance);
00480
00481 return shape3D;
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 protected void setMaterial(Appearance appearance, MaterialInfo materialInfo){
00538 if(materialInfo.transparency > 0.0f){
00539 TransparencyAttributes ta = new TransparencyAttributes(TransparencyAttributes.NICEST, materialInfo.transparency);
00540 ta.setCapability(TransparencyAttributes.ALLOW_MODE_READ);
00541 ta.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
00542 ta.setCapability(TransparencyAttributes.ALLOW_VALUE_READ);
00543 ta.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
00544 appearance.setTransparencyAttributes(ta);
00545 }
00546 if(materialInfo != null){
00547 appearance.setMaterial(createMaterial(materialInfo));
00548 }
00549 }
00550
00551 protected void setTexture( Appearance appearance, TextureInfo textureInfo ){
00552 TextureInfoLocal texInfo = new TextureInfoLocal(textureInfo);
00553 if((texInfo.width != 0) && (texInfo.height != 0)){
00554 ImageComponent2D icomp2d = texInfo.readImage;
00555 Texture2D texture2d=null;
00556 switch (texInfo.numComponents) {
00557 case 1:
00558 texture2d = new Texture2D(Texture.BASE_LEVEL, Texture.LUMINANCE, texInfo.width, texInfo.height);
00559 break;
00560 case 2:
00561 texture2d = new Texture2D(Texture.BASE_LEVEL, Texture.LUMINANCE_ALPHA, texInfo.width, texInfo.height);
00562 appearance.setTransparencyAttributes( new TransparencyAttributes(TransparencyAttributes.BLENDED, 1.0f));
00563 break;
00564 case 3:
00565 texture2d = new Texture2D(Texture.BASE_LEVEL, Texture.RGB, texInfo.width, texInfo.height);
00566 break;
00567 case 4:
00568 texture2d = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, texInfo.width, texInfo.height);
00569 appearance.setTransparencyAttributes( new TransparencyAttributes(TransparencyAttributes.BLENDED, 1.0f));
00570 break;
00571 }
00572 texture2d.setImage(0, icomp2d);
00573 appearance.setTexture(texture2d);
00574 }else{
00575 if(!texInfo.url.equals("")){
00576 try{
00577 TextureLoader tloader = new TextureLoader(texInfo.url, null);
00578 Texture texture = tloader.getTexture();
00579 appearance.setTexture(texture);
00580 }catch(Exception ex){
00581 System.out.println(ex+" "+texInfo.url);
00582 }
00583 }
00584 }
00585 TextureAttributes texAttrBase = new TextureAttributes();
00586 texAttrBase.setTextureMode(TextureAttributes.REPLACE);
00587 appearance.setTextureAttributes(texAttrBase);
00588 }
00589
00590
00591 protected Material createMaterial(MaterialInfo materialInfo){
00592
00593 Material material = new Material();
00594
00595 float[] dColor = materialInfo.diffuseColor;
00596 material.setDiffuseColor(new Color3f(dColor[0], dColor[1], dColor[2]));
00597
00598 float[] sColor = materialInfo.specularColor;
00599 material.setSpecularColor(new Color3f(sColor[0], sColor[1], sColor[2]));
00600
00601 float[] eColor = materialInfo.emissiveColor;
00602 material.setEmissiveColor(new Color3f(eColor[0], eColor[1], eColor[2]));
00603
00604 float r = materialInfo.ambientIntensity;
00605 material.setAmbientColor(new Color3f(r * dColor[0], r * dColor[1], r * dColor[2]));
00606
00607 float shininess = materialInfo.shininess * 127.0f + 1.0f;
00608 material.setShininess(shininess);
00609
00610 material.setCapability(Material.ALLOW_COMPONENT_READ);
00611 material.setCapability(Material.ALLOW_COMPONENT_WRITE);
00612
00613 return material;
00614 }
00615
00616 private void setColors(GeometryInfo geometryInfo, ShapeInfo shapeInfo, AppearanceInfo appearanceInfo) {
00617
00618 int numColors = appearanceInfo.colors.length / 3;
00619
00620 if(numColors > 0){
00621 float[] orgColors = appearanceInfo.colors;
00622 Color3f[] colors = new Color3f[numColors];
00623 for(int i=0; i < numColors; ++i){
00624 colors[i] = new Color3f(orgColors[i*3], orgColors[i*3+1], orgColors[i*3+2]);
00625 }
00626 geometryInfo.setColors(colors);
00627
00628 int[] orgColorIndices = appearanceInfo.colorIndices;
00629 int numOrgColorIndices = orgColorIndices.length;
00630 int numTriangles = shapeInfo.triangles.length / 3;
00631 int[] colorIndices = new int[numTriangles * 3];
00632
00633 if(numOrgColorIndices > 0){
00634 if(appearanceInfo.colorPerVertex){
00635 colorIndices = orgColorIndices;
00636 } else {
00637 int pos = 0;
00638 for(int i=0; i < numTriangles; ++i){
00639 int colorIndex = orgColorIndices[i];
00640 for(int j=0; j < 3; ++j){
00641 colorIndices[pos++] = colorIndex;
00642 }
00643 }
00644 }
00645 } else {
00646 if(appearanceInfo.colorPerVertex){
00647 for(int i=0; i < colorIndices.length; ++i){
00648 colorIndices[i] = shapeInfo.triangles[i];
00649 }
00650 } else {
00651 int pos = 0;
00652 for(int i=0; i < numTriangles; ++i){
00653 for(int j=0; j < 3; ++j){
00654 colorIndices[pos++] = i;
00655 }
00656 }
00657
00658 }
00659 }
00660 geometryInfo.setColorIndices(colorIndices);
00661 }
00662 }
00663
00664
00665 private void setNormals(GeometryInfo geometryInfo, ShapeInfo shapeInfo, AppearanceInfo appearanceInfo) {
00666
00667 int numNormals = appearanceInfo.normals.length / 3;
00668
00669 if(numNormals == 0){
00670 NormalGenerator ng = new NormalGenerator(appearanceInfo.creaseAngle);
00671 ng.generateNormals(geometryInfo);
00672
00673 } else {
00674
00675 float[] orgNormals = appearanceInfo.normals;
00676 Vector3f[] normals = new Vector3f[numNormals];
00677 for(int i=0; i < numNormals; ++i){
00678 normals[i] = new Vector3f(orgNormals[i*3], orgNormals[i*3+1], orgNormals[i*3+2]);
00679 }
00680 geometryInfo.setNormals(normals);
00681
00682 int[] orgNormalIndices = appearanceInfo.normalIndices;
00683 int numOrgNormalIndices = orgNormalIndices.length;
00684 int numTriangles = shapeInfo.triangles.length / 3;
00685 int[] normalIndices = new int[numTriangles * 3];
00686
00687 if(numOrgNormalIndices > 0){
00688 if(appearanceInfo.normalPerVertex){
00689 normalIndices = orgNormalIndices;
00690 } else {
00691 int pos = 0;
00692 for(int i=0; i < numTriangles; ++i){
00693 int normalIndex = orgNormalIndices[i];
00694 for(int j=0; j < 3; ++j){
00695 normalIndices[pos++] = normalIndex;
00696 }
00697 }
00698 }
00699 } else {
00700 if(appearanceInfo.normalPerVertex){
00701 for(int i=0; i < normalIndices.length; ++i){
00702 normalIndices[i] = shapeInfo.triangles[i];
00703 }
00704 } else {
00705 int pos = 0;
00706 for(int i=0; i < numTriangles; ++i){
00707 for(int j=0; j < 3; ++j){
00708 normalIndices[pos++] = i;
00709 }
00710 }
00711
00712 }
00713 }
00714
00715 geometryInfo.setNormalIndices(normalIndices);
00716 }
00717 }
00718
00719
00726 public boolean propertyChanged(String property, String value) {
00727 if (super.propertyChanged(property, value)){
00728 }else if(property.equals("translation")){
00729 translation(value);
00730 }else if(property.equals("rotation")){
00731 rotation(value);
00732 }else if(property.equals("size")){
00733 size(value);
00734 }else if(property.equals("bottomRadius") || property.equals("height") || property.equals("radius") ||
00735 property.equals("side") || property.equals("bottom") || property.equals("top")){
00736 if(shapes_[0].primitiveType==ShapePrimitiveType.SP_CONE){
00737 coneSize(property, value);
00738 }else if(shapes_[0].primitiveType==ShapePrimitiveType.SP_CYLINDER){
00739 cylinderSize(property, value);
00740 }else{
00741 sphereSize(value);
00742 }
00743 }else if(property.equals("diffuseColor")){
00744 diffuseColor(value);
00745 }else
00746 return false;
00747 return true;
00748 }
00749
00750 private void size(float[] newSize){
00751 if (newSize != null && newSize.length == 3){
00752 setFltAry("size", newSize);
00753 tg_.removeChild(bg_);
00754 bg_ = new BranchGroup();
00755 bg_.setCapability(BranchGroup.ALLOW_DETACH);
00756 bg_.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
00757 bg_.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
00758 Primitive primitive = new Box(newSize[0]/2, newSize[1]/2, newSize[2]/2, primitiveFlag, appearance_);
00759 bg_.addChild(primitive);
00760 tg_.addChild(bg_);
00761 resizeBoundingBox();
00762 if (model_ != null) model_.notifyModified();
00763 shapes_[0].primitiveParameters = newSize;
00764 }
00765 }
00766
00767 public void size(String size){
00768 float[] newSize = getFltAry(size);
00769 size(newSize);
00770 }
00771
00772 public void coneSize(String property, String size){
00773 float bottomRadius = getFlt("bottomRadius", 0.0f);
00774 float height = getFlt("height", 0.0f);
00775 boolean side = isTrue("side");
00776 boolean bottom = isTrue("bottom");
00777 if(property.equals("bottomRadius")){
00778 float newsize = getFlt(size);
00779 setFlt(property, newsize );
00780 bottomRadius = newsize;
00781 }else if(property.equals("height")){
00782 float newsize = getFlt(size);
00783 setFlt(property, newsize );
00784 height = newsize;
00785 }else if(property.equals("side")){
00786 boolean flg = size.equals("true");
00787 setBool(property, flg);
00788 side = flg;
00789 }else if(property.equals("bottom")){
00790 boolean flg = size.equals("true");
00791 setBool(property, flg);
00792 bottom = flg;
00793 }
00794 tg_.removeChild(bg_);
00795 bg_ = new BranchGroup();
00796 bg_.setCapability(BranchGroup.ALLOW_DETACH);
00797 bg_.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
00798 bg_.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
00799 Primitive primitive = new Cone( bottomRadius, height, primitiveFlag, appearance_);
00800 if(!bottom)
00801 primitive.removeChild(primitive.getShape(Cone.CAP));
00802 if(!side)
00803 primitive.removeChild(primitive.getShape(Cone.BODY));
00804 bg_.addChild(primitive);
00805 tg_.addChild(bg_);
00806 resizeBoundingBox();
00807 if (model_ != null) model_.notifyModified();
00808 float[] param = new float[4];
00809 param[0] = bottomRadius;
00810 param[1] = height;
00811 param[2] = bottom ? 1: 0;
00812 param[3] = side? 1 : 0;
00813 shapes_[0].primitiveParameters = param;
00814 }
00815
00816 public void cylinderSize(String property, String size){
00817 float radius = getFlt("radius", 0.0f);
00818 float height = getFlt("height", 0.0f);
00819 boolean side = isTrue("side");
00820 boolean bottom = isTrue("bottom");
00821 boolean top = isTrue("top");
00822 if(property.equals("radius")){
00823 float newsize = getFlt(size);
00824 setFlt(property, newsize );
00825 radius = newsize;
00826 }else if(property.equals("height")){
00827 float newsize = getFlt(size);
00828 setFlt(property, newsize );
00829 height = newsize;
00830 }else if(property.equals("side")){
00831 boolean flg = size.equals("true");
00832 setBool(property, flg);
00833 side = flg;
00834 }else if(property.equals("bottom")){
00835 boolean flg = size.equals("true");
00836 setBool(property, flg);
00837 bottom = flg;
00838 }else if(property.equals("top")){
00839 boolean flg = size.equals("true");
00840 setBool(property, flg);
00841 top = flg;
00842 }
00843
00844 tg_.removeChild(bg_);
00845 bg_ = new BranchGroup();
00846 bg_.setCapability(BranchGroup.ALLOW_DETACH);
00847 bg_.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
00848 bg_.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
00849 Primitive primitive = new Cylinder( radius, height, primitiveFlag, appearance_);
00850 bg_.addChild(primitive);
00851 if(!bottom)
00852 primitive.removeChild(primitive.getShape(Cylinder.BOTTOM));
00853 if(!top)
00854 primitive.removeChild(primitive.getShape(Cylinder.TOP));
00855 if(!side)
00856 primitive.removeChild(primitive.getShape(Cylinder.BODY));
00857 tg_.addChild(bg_);
00858 resizeBoundingBox();
00859 if (model_ != null) model_.notifyModified();
00860 float[] param = new float[5];
00861 param[0] = radius;
00862 param[1] = height;
00863 param[2] = top ? 1 : 0;
00864 param[3] = bottom? 1 : 0;
00865 param[4] = side? 1 : 0;
00866 shapes_[0].primitiveParameters = param;
00867 }
00868
00869 private void sphereSize(float[] newSize){
00870 if (newSize != null && newSize.length == 1){
00871 setFlt("radius", newSize[0]);
00872 tg_.removeChild(bg_);
00873 bg_ = new BranchGroup();
00874 bg_.setCapability(BranchGroup.ALLOW_DETACH);
00875 bg_.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
00876 bg_.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
00877 Primitive primitive = new Sphere(newSize[0], primitiveFlag, appearance_);
00878 bg_.addChild(primitive);
00879 tg_.addChild(bg_);
00880 resizeBoundingBox();
00881 if (model_ != null) model_.notifyModified();
00882 shapes_[0].primitiveParameters = newSize;
00883 }
00884 }
00885
00886 public void sphereSize(String size){
00887 float[] newSize = getFltAry(size);
00888 sphereSize(newSize);
00889 }
00890
00891 void diffuseColor(float[] newValue){
00892 if (newValue != null && newValue.length == 3){
00893 setFltAry("diffuseColor", newValue);
00894 materials_[0].diffuseColor = newValue;
00895 setMaterial(appearance_, materials_[0]);
00896 if (model_ != null) model_.notifyModified();
00897 }
00898 }
00899
00900 void diffuseColor(String value){
00901 float[] newValue = getFltAry(value);
00902 diffuseColor(newValue);
00903 }
00904
00905 public class TextureInfoLocal
00906 {
00907 public short numComponents;
00908 public short width;
00909 public short height;
00910 public boolean repeatS;
00911 public boolean repeatT;
00912 public ImageComponent2D readImage;
00913 String url;
00914
00915 public TextureInfoLocal(TextureInfo texinfo) {
00916 width = texinfo.width;
00917 height = texinfo.height;
00918 numComponents = texinfo.numComponents;
00919
00920 repeatS = texinfo.repeatS;
00921 repeatT = texinfo.repeatT;
00922 url = texinfo.url;
00923
00924 if((width == 0) || (height == 0)){
00925 numComponents = 3;
00926 repeatS = false;
00927 repeatT = false;
00928 width = 0;
00929 height = 0;
00930 return;
00931 }
00932
00933
00934 short w=1;
00935 do{
00936 w *=2;
00937 }while(w<=width);
00938 short width_new = (short)(w);
00939 if(width_new-width > width-width_new/2)
00940 width_new /=2;
00941 w=1;
00942 do{
00943 w *=2;
00944 }while(w<=height);
00945 short height_new = (short)(w);
00946 if(height_new-height > height-height_new/2)
00947 height_new /=2;
00948
00949
00950 BufferedImage bimageRead=null;
00951 switch(numComponents){
00952 case 1:
00953 bimageRead = new BufferedImage(width_new, height_new, BufferedImage.TYPE_BYTE_GRAY);
00954 byte[] bytepixels = ( ( DataBufferByte)bimageRead.getRaster().getDataBuffer() ).getData();
00955 for(int i=0; i<height_new; i++){
00956 for(int j=0; j<width_new; j++){
00957 int k = (int)(i*height/height_new)*width+(int)(j*width/width_new);
00958 bytepixels[i*width_new+j] = texinfo.image[k];
00959 }
00960 }
00961 break;
00962 case 2:
00963 bimageRead = new BufferedImage(width_new, height_new, BufferedImage.TYPE_USHORT_GRAY);
00964 short[] shortpixels = ( ( DataBufferUShort)bimageRead.getRaster().getDataBuffer() ).getData();
00965 for(int i=0; i<height_new; i++){
00966 for(int j=0; j<width_new; j++){
00967 int k = (int)(i*height/height_new)*width*2+(int)(j*width/width_new)*2;
00968 short l = texinfo.image[k];
00969 short a = texinfo.image[k+1];
00970 shortpixels[i*width_new+j] = (short)((l&0xff) << 8 | (a&0xff)) ;
00971 }
00972 }
00973 break;
00974 case 3:
00975 bimageRead = new BufferedImage(width_new, height_new, BufferedImage.TYPE_INT_RGB);
00976 int[] intpixels = ( (DataBufferInt)bimageRead.getRaster().getDataBuffer() ).getData();
00977 for(int i=0; i<height_new; i++){
00978 for(int j=0; j<width_new; j++){
00979 int k = (int)(i*height/height_new)*width*3+(int)(j*width/width_new)*3;
00980 short r = texinfo.image[k];
00981 short g = texinfo.image[k+1];
00982 short b = texinfo.image[k+2];
00983 intpixels[i*width_new+j] = (r&0xff) << 16 | (g&0xff) << 8 | (b&0xff);
00984 }
00985 }
00986 break;
00987 case 4:
00988 bimageRead = new BufferedImage(width_new, height_new, BufferedImage.TYPE_INT_ARGB);
00989 intpixels = ( (DataBufferInt)bimageRead.getRaster().getDataBuffer() ).getData();
00990 for(int i=0; i<height_new; i++){
00991 for(int j=0; j<width_new; j++){
00992 int k = (int)(i*height/height_new)*width*4+(int)(j*width/width_new)*4;
00993 short r = texinfo.image[k];
00994 short g = texinfo.image[k+1];
00995 short b = texinfo.image[k+2];
00996 short a = texinfo.image[k+3];
00997 intpixels[i*width_new+j] = (a&0xff) << 24 | (r&0xff) << 16 | (g&0xff) << 8 | (b&0xff);
00998 }
00999 }
01000 break;
01001 }
01002 height = height_new;
01003 width = width_new;
01004
01005 switch(numComponents){
01006 case 1:
01007 readImage = new ImageComponent2D(ImageComponent.FORMAT_CHANNEL8, bimageRead);
01008 break;
01009 case 2:
01010 readImage = new ImageComponent2D(ImageComponent.FORMAT_LUM8_ALPHA8, bimageRead);
01011 break;
01012 case 3:
01013 readImage = new ImageComponent2D(ImageComponent.FORMAT_RGB, bimageRead);
01014 break;
01015 case 4:
01016 readImage = new ImageComponent2D(ImageComponent.FORMAT_RGBA, bimageRead);
01017 break;
01018 }
01019
01020 }
01021 }
01022
01027 public GrxShapeItem clone(){
01028 GrxShapeItem ret = (GrxShapeItem)super.clone();
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072 return ret;
01073 }
01074
01075 void setColor(java.awt.Color color, Node node){
01076 if (node instanceof Shape3D){
01077 Color3f c3f = new Color3f(color);
01078 Shape3D s3d = (Shape3D)node;
01079 Appearance app = s3d.getAppearance();
01080 if (app != null){
01081 Material ma = app.getMaterial();
01082 if (ma != null){
01083 ma.setAmbientColor(c3f);
01084 }
01085 }
01086 }else if (node instanceof Primitive){
01087 Primitive prim = (Primitive)node;
01088 for (int j=0; j<prim.numChildren(); j++){
01089 setColor(color, (Shape3D)prim.getChild(j));
01090 }
01091 }else if(node instanceof TransformGroup ){
01092 TransformGroup tfg = (TransformGroup)node;
01093 for(int j=0; j<tfg.numChildren(); j++){
01094 setColor(color, tfg.getChild(j));
01095 }
01096 }else if(node instanceof BranchGroup ){
01097 BranchGroup bg = (BranchGroup)node;
01098 for(int j=0; j<bg.numChildren(); j++){
01099 setColor(color, bg.getChild(j));
01100 }
01101 }
01102 }
01103
01104 void setColor(java.awt.Color color){
01105 setColor(color, tg_);
01106 }
01107
01108 void restoreColor(Node node, int id){
01109 if(node instanceof Shape3D){
01110 Shape3D s3d = (Shape3D)node;
01111 Appearance app = s3d.getAppearance();
01112 setMaterial(app, materials_[id]);
01113 }else if(node instanceof Primitive){
01114 Primitive prim = (Primitive)node;
01115 for (int j=0; j<prim.numChildren(); j++){
01116 restoreColor((Shape3D)prim.getChild(j), id);
01117 }
01118 }else if(node instanceof TransformGroup ){
01119 TransformGroup tfg = (TransformGroup)node;
01120 for(int j=0; j<tfg.numChildren(); j++){
01121 restoreColor(tfg.getChild(j), id);
01122 }
01123 }else if(node instanceof BranchGroup ){
01124 BranchGroup bg = (BranchGroup)node;
01125 for(int j=0; j<bg.numChildren(); j++){
01126 restoreColor(bg.getChild(j), id);
01127 }
01128 }
01129 }
01130
01131 void restoreColor(){
01132 restoreColor(tg_, 0);
01133 }
01134
01135 private void setPrimitiveProperty(ShapeInfo shapeInfo){
01136 switch(shapeInfo.primitiveType.value()){
01137 case ShapePrimitiveType._SP_BOX :
01138 setFltAry("size", shapeInfo.primitiveParameters);
01139 setFltAry("diffuseColor", materials_[0].diffuseColor);
01140 break;
01141 case ShapePrimitiveType._SP_CONE :
01142 setFlt("bottomRadius", shapeInfo.primitiveParameters[0]);
01143 setFlt("height", shapeInfo.primitiveParameters[1]);
01144 setProperty("bottom",shapeInfo.primitiveParameters[2]==0?"false":"true");
01145 setProperty("side",shapeInfo.primitiveParameters[3]==0?"false":"true");
01146 setFltAry("diffuseColor", materials_[0].diffuseColor);
01147 break;
01148 case ShapePrimitiveType._SP_CYLINDER :
01149 setFlt("radius", shapeInfo.primitiveParameters[0]);
01150 setFlt("height", shapeInfo.primitiveParameters[1]);
01151 setProperty("top",shapeInfo.primitiveParameters[2]==0?"false":"true");
01152 setProperty("bottom",shapeInfo.primitiveParameters[3]==0?"false":"true");
01153 setProperty("side",shapeInfo.primitiveParameters[4]==0?"false":"true");
01154 setFltAry("diffuseColor", materials_[0].diffuseColor);
01155 break;
01156 case ShapePrimitiveType._SP_SPHERE :
01157 setFlt("radius", shapeInfo.primitiveParameters[0]);
01158 setFltAry("diffuseColor", materials_[0].diffuseColor);
01159 break;
01160 default :
01161 break;
01162 }
01163 }
01164
01165 @Override
01166 public ValueEditType GetValueEditType(String key) {
01167
01168 if(shapes_.length == 1 && shapes_[0].primitiveType == ShapePrimitiveType.SP_CONE &&
01169 (key.equals("side") || key.equals("bottom")))
01170 {
01171 return new ValueEditCombo(booleanComboItem_);
01172 }
01173 else if(shapes_.length == 1 && shapes_[0].primitiveType == ShapePrimitiveType.SP_CYLINDER &&
01174 (key.equals("side") || key.equals("bottom") || key.equals("top")))
01175 {
01176 return new ValueEditCombo(booleanComboItem_);
01177 }
01178 return super.GetValueEditType(key);
01179 }
01180 }