Mathématiques, géométrie dans l'espace, patron d'un tétraèdre

Olivski - 3 déc. 2012 à 22:15
 Olivski - 18 déc. 2012 à 18:07
Bonjour. Je suis professeur de mathématiques et j'essaye de m'initier au java pour faire des illustrations dans mes cours. Mais voilà, je n'y connais pas grand chose en Java3D et voici par exemple ce que j'essaye de faire : Je veux faire un patron d'un tétraèdre, mais, voilà, comme dans mon code ci dessous, il y'a un coté qui fait ce que je veux mais pas les deux autres...car je ne comprends pas bien comment est-ce que l'on défini une rotation en java3D. Voici mon code, est-ce que quelqu'un pourrait m'aider.
En vous remerciant du temps que vous m'accorderez,
Arnaud.






import java.applet.Applet;
import java.awt.*;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import javax.vecmath.AxisAngle4f;



public class patron_tetraedre extends Applet {

public patron_tetraedre() {
this.setLayout(new BorderLayout());

// Etape 3 :
// Creation du Canvas 3D
Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());

this.add(canvas3D, BorderLayout.CENTER);

// Etape 4 :
// Creation d'un objet SimpleUniverse
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

// Etape 5 :
// Positionnement du point d'observation pour avoir une vue correcte de la
// scene 3D
simpleU.getViewingPlatform().setNominalViewingTransform();

// Etape 6 :
// Creation de la scene 3D qui contient tous les objets 3D que l'on veut visualiser
BranchGroup scene = createSceneGraph(simpleU);

// Etape 7 :
// Compilation de la scene 3D
// scene.compile();

// Etape 8:
// Attachement de la scene 3D a l'objet SimpleUniverse
simpleU.addBranchGraph(scene);
}

/**
* Creation de la scene 3D qui contient tous les objets 3D
* @return scene 3D
*/
public BranchGroup createSceneGraph(SimpleUniverse su) {
// Creation de l'objet parent qui contiendra tous les autres objets 3D
BranchGroup parent = new BranchGroup();

// Creation du groupe de transformation sur lequel vont s'appliquer les
// comportements




/******************************************DEBUT ANIMATION************************************************/


/*LE SOUCIS EST CI DESSOUS*/


// Les 3 lignes ci dessous ne marchent pas...
Transform3D rotate = new Transform3D();
AxisAngle4f Axe2=new AxisAngle4f(-1.5f,(float)((Math.sqrt(3f))/2f),0f,(float)(Math.PI)/3f);
rotate.set(Axe2);

Transform3D rotation = new Transform3D();
Transform3D translation =new Transform3D();
rotation.rotX(Math.PI/6f);
// translation.set(new Vector3f(1.0f,0.0f,0.0f));
// translation.mul(rotation);


TransformGroup objSpin1 = new TransformGroup();
objSpin1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D rotation1 = new Transform3D();
rotation1.set(new Vector3f( 1f,(float)(Math.sqrt(3f)),0f));

Alpha rotationAlpha1 = new Alpha(-1, 15000);

RotationInterpolator rotatorX1 =
new RotationInterpolator(rotationAlpha1, objSpin1, rotate,
0.0f, (float) ((2.0f)*Math.PI)/3.0f);
BoundingSphere bounds1 = new BoundingSphere();
rotatorX1.setSchedulingBounds(bounds1);
objSpin1.addChild(rotatorX1);

/*FIN DU SOUCIS*/


TransformGroup objSpin2 = new TransformGroup();

objSpin2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);


// Interressant car combine une translation avec une translation !!!
// Transform3D xAxis = new Transform3D();
// Transform3D Axe = new Transform3D();
// Axe.setTranslation(new Vector3f (0f,0f,0f));

// Les 3 lignes ci dessous ne marchent pas...
// Transform3D rotate = new Transform3D();
// AxisAngle4f Axe2=new AxisAngle4f(0.2f,0f,0f,(float)(Math.PI)/3f);
//rotation.set(Axe2);


Transform3D rotation2 = new Transform3D();
rotation2.set(new Vector3f( -0.5f,(float)((2.0f)*(Math.sqrt(3f))),0f));

Alpha rotationAlpha2 = new Alpha(-1, 15000);

RotationInterpolator rotatorX2 =
new RotationInterpolator(rotationAlpha2, objSpin2, rotation2,
0.0f, (float) ((2.0f)*Math.PI)/3.0f);
BoundingSphere bounds2 = new BoundingSphere();
rotatorX2.setSchedulingBounds(bounds2);
objSpin2.addChild(rotatorX2);


// Creation du groupe de transformation a partir de l'objet SimpleUniverse
// On recupere en fait la position de la camera qui sera modifiee a
// la souris grace au TransformGroup tg
TransformGroup tg = su.getViewingPlatform().getViewPlatformTransform();

// Creation comportement navigation (rotation) a la souris
MouseRotate mouseRotate = new MouseRotate(MouseBehavior.INVERT_INPUT);
mouseRotate.setFactor(0.005);
mouseRotate.setTransformGroup(tg);

// Champ d'action de la souris (rotation)
mouseRotate.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000));

// Ajout du comportement rotation a la souris a l'objet parent de la
// scene 3D
parent.addChild(mouseRotate);

// Creation comportement navigation (translation) a la souris
MouseTranslate mouseTranslate =
new MouseTranslate(MouseBehavior.INVERT_INPUT);
mouseTranslate.setFactor(0.005);
mouseTranslate.setTransformGroup(tg);

// Champ d'action de la souris (translation)
mouseTranslate.setSchedulingBounds(
new BoundingSphere(new Point3d(), 1000));

// Ajout du comportement translation a la souris a l'objet parent de la
// scene 3D
parent.addChild(mouseTranslate);

// Creation comportement navigation (zoom) a la souris
MouseZoom mouseZoom = new MouseZoom(MouseBehavior.INVERT_INPUT);
mouseZoom.setFactor(0.005);
mouseZoom.setTransformGroup(tg);

// Champ d'action de la souris (zoom)
mouseZoom.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000));

// Ajout du comportement zoom a la souris a l'objet parent de la
// scene 3D
parent.addChild(mouseZoom);



/******************************************FIN ANIMATION************************************************/



//Arriere plan en blanc
Background background = new Background(0.5f, 0.5f, 0.5f);
background.setApplicationBounds(new BoundingBox());
parent.addChild(background);

parent.addChild(new Triangle_base());

objSpin1.addChild(new Triangle_un());
parent.addChild(objSpin1);

objSpin2.addChild(new Triangle_deux());
parent.addChild(objSpin2);

parent.addChild(new Triangle_trois());

parent.addChild(new axe_X( Color.green));
parent.addChild(new axe_Y( Color.blue));
parent.addChild(new axe_Z( Color.yellow));

return parent;
}


class axe_X extends Shape3D {
public axe_X( Color color) {
Point3f points[] = new Point3f[6];
Color3f colors[] = new Color3f[6];
points[0] = new Point3f(-1f, 0f, 0f);
colors[0] = new Color3f(color);
points[1] = new Point3f(1f,0f,0f);
colors[1] = new Color3f(color);
points[2]= new Point3f(0.5f,0f,0f);
colors[2] = new Color3f(color);
points[3]= new Point3f(0.45f,0.1f,0f);
colors[3] = new Color3f(color);
points[4]= new Point3f(0.5f,0f,0f);
colors[4] = new Color3f(color);
points[5]= new Point3f(0.45f,-0.1f,0f);
colors[5] = new Color3f(color);
LineArray lineArray = new LineArray(6, LineArray.COORDINATES |
LineArray.COLOR_3);
lineArray.setCoordinates(0, points);
lineArray.setColors(0, colors);

this.setGeometry(lineArray);
} // fin constructeur
}


class axe_Y extends Shape3D {
public axe_Y( Color color) {
Point3f points[] = new Point3f[6];
Color3f colors[] = new Color3f[6];
points[0] = new Point3f(0f, -1f, 0f);
colors[0] = new Color3f(color);
points[1] = new Point3f(0f,1f,0f);
colors[1] = new Color3f(color);
points[2]= new Point3f(0f,0.5f,0f);
colors[2] = new Color3f(color);
points[3]= new Point3f(0.1f,0.45f,0f);
colors[3] = new Color3f(color);
points[4]= new Point3f(0.0f,0.5f,0f);
colors[4] = new Color3f(color);
points[5]= new Point3f(-0.1f,0.45f,0f);
colors[5] = new Color3f(color);
LineArray lineArray = new LineArray(6, LineArray.COORDINATES |
LineArray.COLOR_3);
lineArray.setCoordinates(0, points);
lineArray.setColors(0, colors);

this.setGeometry(lineArray);
} // fin constructeur
}


class axe_Z extends Shape3D {
public axe_Z( Color color) {
Point3f points[] = new Point3f[6];
Color3f colors[] = new Color3f[6];
points[0] = new Point3f(0f, 0f, -1f);
colors[0] = new Color3f(color);
points[1] = new Point3f(0f,0f,1f);
colors[1] = new Color3f(color);
points[2]= new Point3f(0f,0f,0.5f);
colors[2] = new Color3f(color);
points[3]= new Point3f(0.1f,0f,0.45f);
colors[3] = new Color3f(color);
points[4]= new Point3f(0.0f,0f,0.5f);
colors[4] = new Color3f(color);
points[5]= new Point3f(-0.1f,0f,0.45f);
colors[5] = new Color3f(color);
LineArray lineArray = new LineArray(6, LineArray.COORDINATES |
LineArray.COLOR_3);
lineArray.setCoordinates(0, points);
lineArray.setColors(0, colors);

this.setGeometry(lineArray);
} // fin constructeur
}


class Triangle_base extends Shape3D {
public Triangle_base() {
Point3f face1_s3 = new Point3f(1.0f, 0f, 0f);
Point3f face1_s2 = new Point3f( -0.5f,(float) ((Math.sqrt(3.0f))/(2.0f)), 0f);
Point3f face1_s1 = new Point3f( -0.5f,(float) (-(Math.sqrt(3.0f))/(2.0f)), 0f);

Color3f color1 = new Color3f(Color.yellow);
;
TriangleArray quadArray = new TriangleArray(6 ,
TriangleArray.COORDINATES | TriangleArray.COLOR_3);

quadArray.setCoordinates(0, new Point3f[] {

face1_s3, face1_s2, face1_s1,
face1_s1, face1_s2, face1_s3,
});
quadArray.setColors(0, new Color3f[] {
color1, color1, color1, color1
});
this.setGeometry(quadArray);
}

} //fin Carre_base

class Triangle_un extends Shape3D {
public Triangle_un() {
Point3f face1_s3 = new Point3f(1.0f, 0f, 0f);
Point3f face1_s2 = new Point3f( 1f,(float) (Math.sqrt(3.0f)), 0f);
Point3f face1_s1 = new Point3f( -0.5f,(float) ((Math.sqrt(3.0f)/(2.f))), 0f);

Color3f color1 = new Color3f(Color.blue);
;
TriangleArray quadArray = new TriangleArray(6 ,
TriangleArray.COORDINATES | TriangleArray.COLOR_3);

quadArray.setCoordinates(0, new Point3f[] {

face1_s3, face1_s2, face1_s1,
face1_s1, face1_s2, face1_s3,
});
quadArray.setColors(0, new Color3f[] {
color1, color1, color1, color1
});
this.setGeometry(quadArray);
}

} //fin triangle_un
class Triangle_deux extends Shape3D {
public Triangle_deux() {
Point3f face1_s3 = new Point3f(-2.0f, 0f, 0f);
Point3f face1_s2 = new Point3f( -0.5f,-(float) ((Math.sqrt(3.0f)/(2.f))), 0f);
Point3f face1_s1 = new Point3f( -0.5f,(float) ((Math.sqrt(3.0f)/(2.f))), 0f);

Color3f color1 = new Color3f(Color.blue);
;
TriangleArray quadArray = new TriangleArray(6 ,
TriangleArray.COORDINATES | TriangleArray.COLOR_3);

quadArray.setCoordinates(0, new Point3f[] {

face1_s3, face1_s2, face1_s1,
face1_s1, face1_s2, face1_s3,
});
quadArray.setColors(0, new Color3f[] {
color1, color1, color1, color1
});
this.setGeometry(quadArray);
}

}
class Triangle_trois extends Shape3D {
public Triangle_trois() {
Point3f face1_s3 = new Point3f(1.0f, 0f, 0f);
Point3f face1_s2 = new Point3f( -0.5f,-(float) ((Math.sqrt(3.0f)/(2.f))), 0f);
Point3f face1_s1 = new Point3f( 1.0f,(float) (-Math.sqrt(3.0f)), 0f);

Color3f color1 = new Color3f(Color.blue);
;
TriangleArray quadArray = new TriangleArray(6 ,
TriangleArray.COORDINATES | TriangleArray.COLOR_3);

quadArray.setCoordinates(0, new Point3f[] {

face1_s3, face1_s2, face1_s1,
face1_s1, face1_s2, face1_s3,
});
quadArray.setColors(0, new Color3f[] {
color1, color1, color1, color1
});
this.setGeometry(quadArray);
}

} //fin Carre_base
/**
* Etape 9 :
* Methode main() nous permettant d'utiliser cette classe comme une applet
* ou une application.
* @param args arguments de la ligne de commande
*/
public static void main(String[] args) {
Frame frame = new MainFrame(new patron_tetraedre(), 256, 256);
}
}

1 réponse

Up.
Désolé, mais je suis toujours bloqué, et je n'arrive pas à mettre la main sur de bon livres pour programmer en java3D. Si, quelqu'un sait comment résoudre mon problème, je lui en serait très remerciant.

PS: Le code ci-dessus fonctionne, c'est juste qu'un des coté du tétraèdre ne se déplace pas comme il faut, car je ne sais pas comment définir une rotation dans l'espace selon un axe quelconque en java3D.


Bonne soirée, Arnaud
0
Rejoignez-nous