Classe de base: tableau de float de taille arbitraire.

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Classe de stockage d'un vecteur de float             */
/* de longueur arbitraire                               */

public class Vecteur {
  public float [] c;

  public Vecteur(int n) {
    c = new float[n];
  }

  public Vecteur(float v) {
    c = new float[1];
    c[0] = v;
  }
  
  public Vecteur(float v1,float v2) {
    c = new float[2];
    c[0] = v1;
    c[1] = v2;
  }
  
  public Vecteur(float v1,float v2,float v3) {
    c = new float[3];
    c[0] = v1;
    c[1] = v2;
    c[2] = v3;
  }
  
  public Vecteur(float v1,float v2,float v3,float v4) {
    c = new float[4];
    c[0] = v1;
    c[1] = v2;
    c[2] = v3;
    c[3] = v4;
  }
  
  public Vecteur(float [] v) {
    c = new float[v.length];
    for ( int i = 0 ; i < v.length ; i++ )
      c[i] = v[i];
  }
  
  public Vecteur(Vecteur v) {
    c = new float[v.c.length];
    for ( int i = 0 ; i < v.c.length ; i++ )
      c[i] = v.c[i];
  }
  
  public String toString() {
    String s = "[";
    for ( int i = 0 ; i < c.length ; i++ )
      s += (c[i]+",");
    s += "]";
    return(s);
  }
}

Vecteur.java

Classe tableau de 4 float.

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Une classe de stockage d'un vecteur de 4 float       */

public class Vecteur4 extends Vecteur {

  public Vecteur4() {
    super(4);
  }

  public Vecteur4(float v1,float v2,float v3,float v4) {
    super(v1,v2,v3,v4);
  }

  public Vecteur4(Vecteur4 v4) {
    super(v4);
  }
}

Vecteur4.java

Horizontale.gif (2348 octets)

Classe de base: tableau de float à deux dimensions de taille arbitraire selon les deux dimensions.

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Classe de stockage d'une matrice rectangulaire       */
/* de float de nombre de lignes et de colonnes          */
/* arbitraires                                          */

public class Matrice {
  public float [][] c;

  public Matrice(int lignes,int colonnes) {
    c = new float[lignes][colonnes];
  }

  public Matrice(float [][] v) {
    c = new float[v.length][v[0].length];
    for ( int i = 0 ; i < v.length ; i++ )
      for ( int j = 0 ; j < v[i].length ; j++ )
    c[i][j] = v[i][j];
  }

  public Matrice(Matrice m) {
    c = new float[m.c.length][m.c[0].length];
    for ( int i = 0 ; i < m.c.length ; i++ )
      for ( int j = 0 ; j < m.c[i].length ; j++ )
    c[i][j] = m.c[i][j];
  }
  
  public Vecteur multiplication(Vecteur v) {
    if ( v.c.length == c[0].length ) {
      Vecteur r = new Vecteur(c.length);
      for ( int i = 0 ; i < c.length ; i++ )
        for ( int j = 0 ; j < c[0].length ; j++ )
          r.c[i] += c[i][j]*v.c[j];
      return(r); }
    return(null);
  }
  
  public Matrice multiplication(Matrice m) {
    if ( m.c.length == c[0].length ) {
      Matrice r = new Matrice(c.length,m.c[0].length);
      for ( int i = 0 ; i < c.length ; i++ )
        for ( int j = 0 ; j < m.c[0].length ; j++ )
          for ( int k = 0 ; k < m.c.length ; k++ )
            r.c[i][j] += c[i][k]*m.c[k][j];
      return(r); }
    return(null);
  }
  
  public String toString() {
    String s = "[";
    for ( int j = 0 ; j < c.length ; j++ ) {
      s += "[";
      for ( int i = 0 ; i < c[j].length ; i++ )
        s += (c[j][i]+",");
      s += "]\n"; }
    s += "]";
    return(s);
  }
}

Matrice.java

Classe matrice de 4x4 float

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Une classe de stockage d'une matrice de 4x4 float    */

public class Matrice44 extends Matrice {

  public Matrice44() {
    super(4,4);
  }

  public Matrice44(float [][] t) {
    super(t);
  }

  public Matrice44(Matrice44 m) {
    super(m.c);
  }
}

Matrice44.java

Horizontale.gif (2348 octets)

Une première classe CoodonneesHomogenes est dérivée de la classe Vecteur4 pour représenter des coordonnées homogènes 3D.

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Une classe coordonnees homogenes en trois dimensions */

public class CoordonneesHomogenes extends Vecteur4 {

  public CoordonneesHomogenes() {
    super();
  }

  public CoordonneesHomogenes(float x,float y,float z,float t) {
    super(x,y,z,t);
  }

  public CoordonneesHomogenes(CoordonneesHomogenes ch) {
    super(ch.c[0],ch.c[1],ch.c[2],ch.c[3]);
  }
  
  public void setX(float x) {
    c[0] = x;
  }
  
  public float getX() {
    return(c[0]);
  }
  
  public void setY(float y) {
    c[1] = y;
  }
  
  public float getY() {
    return(c[1]);
  }
  
  public void setZ(float z) {
    c[2] = z;
  }
  
  public float getZ() {
    return(c[2]);
  }
  
  public void setT(float t) {
    c[3] = t;
  }
  
  public float getT() {
    return(c[3]);
  }
}

CoordonneesHomogenes.java

La classe Position est dérivée de la classe CoordonneesHomogenes pour représenter des positions dans un espace 3D (4ème coordonnée initialisée à 1.0F).

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Une classe position en coordonnees homogenes         */

public class Position extends CoordonneesHomogenes {

  public Position() {
    super(0.0F,0.0F,0.0F,1.0F);
  }

  public Position(float x,float y,float z) {
    super(x,y,z,1.0F);
  }

  public Position(Position p) {
    super(p);
  }
}

Horizontale.gif (2348 octets)

Position.java

La classe Direction est dérivée de la classe CoordonneesHomogenes pour représenter des directions dans un espace 3D (4ème coordonnée initialisée à 0.0F).

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Une classe direction en coordonnees homogenes        */

public class Direction extends CoordonneesHomogenes {

  public Direction() {
    super(0.0F,0.0F,1.0F,0.0F);
  }

  public Direction(float x,float y,float z) {
    super(x,y,z,0.0F);
  }

  public Direction(Direction p) {
    super(p);
  }

  public Direction(Position pi,
                   Position pf) throws ArithmeticException {
    float dx = pf.c[0]-pi.c[0];
    float dy = pf.c[1]-pi.c[1];
    float dz = pf.c[2]-pi.c[2];
    float d =(float) Math.sqrt(dx*dx+dy*dy+dz*dz);
    if ( d == 0.0F ) {
      throw new ArithmeticException(); }
    c = new float[4];
    c[0] = dx/d;
    c[1] = dy/d;
    c[2] = dz/d;
    c[3] = 0.0F;
  }
}

Direction.java

Horizontale.gif (2348 octets)

La classe Transformation est dérivée de la classe Matrice44 pour représenter des transformations géométriques quelconques en coordonnées homogènes dans un espace 3D.
Le constructeur sans paramètre dérive directement de celui de la classe Matrice44 qui crée une matrice identité.
Trois méthodes sont développées pour:

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Une classe transformation geometrique 3D             */
/* en coordonnees homogenes                             */

public class Transformation extends Matrice44 {

  public Transformation() {
    super();
    for ( int i = 0 ; i < 4 ; i++ )
      c[i][i] = 1.0F;
  }
  
  public Transformation(float [][] t) {
    super(t);
  }
  
  public Transformation(Transformation t) {
    super(t);
  }
  
  public Position multiplication(Position p) {
    Vecteur v = multiplication((Vecteur) p);
    return(new Position(v.c[0],v.c[1],v.c[2]));
  }
  
  public Direction multiplication(Direction d) {
    Vecteur v = multiplication((Vecteur) d);
    return(new Direction(v.c[0],v.c[1],v.c[2]));
  }  
  
  public Transformation multiplication(Transformation t) {
    Matrice m = multiplication((Matrice) t);
    return(new Transformation(m.c));
  }  
}

Transformation.java

Horizontale.gif (2348 octets)

La classe Identite est dérivée de la classe Transformation.

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Une classe transformation geometrique identite       */

public class Identite extends Transformation {

  public Identite() {
    super();
  }
}

Identite.java

La classe Translation est dérivée de la classe Transformation.

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Une classe transformation geometrique translation    */

public class Translation extends Transformation {

  public Translation() {
    super();
  }

  public Translation(float x,float y,float z) {
    super();
    c[0][3] = x;
    c[1][3] = y;
    c[2][3] = z;
  }

  public Translation(Direction d) {
    super();
    c[0][3] = d.getX();
    c[1][3] = d.getY();
    c[2][3] = d.getZ();
  }

  public Translation(Translation t) {
    super(t);
  }
}

Translation.java

La classe Rotation est dérivée de la classe Transformation.
Elle comporte un constructeur pour la création d'une rotation autour d'un axe défini par une direction arbitraire passant par l'origine.

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Une classe transformation geometrique rotation       */

public class Rotation extends Transformation {

  public Rotation() {
    super();
  }

  public Rotation(float a,
                  float dx,float dy,float dz) throws ArithmeticException {
    super();
    remplissage(a,dx,dy,dz);
  }

  public Rotation(float a,
                  Direction d) throws ArithmeticException {
    super();
    remplissage(a,d.getX(),d.getY(),d.getZ());
  }

  public Rotation(Rotation rt) {
    super(rt);
  }

  private void remplissage(float a,
                           float x,float y,float z) throws ArithmeticException {
    a = a/180.0F*(float) Math.PI;
    float c =(float) Math.cos(a);
    float s =(float) Math.sin(a);
    float d =(float) Math.sqrt(x*x+y*y+z*z);
    if ( d == 0 ) {
      throw new ArithmeticException(); }
    x /= d;
    y /= d;
    z /= d;
    float x2 = x*x;
    float y2 = y*y;
    float z2 = z*z;
    float xs = x*s;
    float ys = y*s;
    float zs = z*s;
    float mc = 1.0F - c;
    float xymc = x*y*mc;
    float xzmc = x*z*mc;
    float yzmc = y*z*mc;
    this.c[0][0] = x2+c*(1.0F-x2);
    this.c[0][1] = xymc-zs;
    this.c[0][2] = xzmc+ys;
    this.c[1][0] = xymc+zs;
    this.c[1][1] = y2+c*(1.0F-y2);
    this.c[1][2] = yzmc-xs;
    this.c[2][0] = xzmc-ys;
    this.c[2][1] = yzmc+xs;
    this.c[2][2] = z2+c*(1.0F-z2);
  }
}

Rotation.java

La classe Scale est dérivée de la classe Transformation.

/* Auteur: Nicolas JANEY                                */
/* nicolas.janey@univ-fcomte.fr                         */
/* Octobre 2007                                         */
/* Une classe transformation geometrique scale          */

public class Scale extends Transformation {

  public Scale() {
    super();
  }

  public Scale(float rx,float ry,float rz) {
    super();
    c[0][0] = rx;
    c[1][1] = ry;
    c[2][2] = rz;
  }

  public Scale(Scale sc) {
    super(sc);
  }
}

Scale.java

Aide de cette hiérarchie de classes au format Javadoc

RETOUR