Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Java 3D / January 2004

Tip: Looking for answers? Try searching our database.

vector problem

Thread view: 
Samba - 30 Dec 2003 00:26 GMT
Hello, i got a problem dealing with a vector, i create a vector in which i
put tables of objects
(x, y et z) of "param" type (two attributes t and cst), i calculate the
values of this table in  calculEquationDroite(...), but when i want to get
the values of t and cst of each x, y, z of my vector, i get an exception "in
thread main", and i don't understand why

Here is my code, if somenone understands my mistake, thanks:

//I add the x, y z
... equationsDroites.addElement(calculEquationDroite(c1[0], c1[1], c1[2],
c2[0],c2[1], c2[2]));
     equationsDroites.addElement(calculEquationDroite(c1[0], c1[1], c1[2],
c3[0],c3[1], c3[2]));
     equationsDroites.addElement(calculEquationDroite(c2[0], c2[1], c2[2],
c3[0],c3[1], c3[2]));

     //bug?
     for (int w = 0; w<equationsDroites.size(); w++){
       param[] r = (param[])equationsDroites.get(w);
       //System.out.println("Equation n? " + (w+1));
       System.out.println("x = " + r[0].t + " t" + "+ " + r[0].cst);
       System.out.println("y = " + r[1].t + " t" + "+ " + r[1].cst);
       System.out.println("z = " + r[2].t + " t" + "+ " + r[2].cst);
       }

//calculation of x, y, z
 public param[] calculEquationDroite(double x1, double y1, double z1,
double x2, double y2, double z2){
   // (x-x1)/(x2-x1) = (y-y1)/(y2-y1) = (z-z1)/(z2-z1)
   // Equation param?trique de la droite entre les points p1 et p2
   // x = t(x2-x1) + x1
   // y = t(y2-y1) + y1
   // z = t(z2-z1) + z1
   //tableau de resultat
   param x = new param();
   param y = new param();
   param z = new param();
   param[] result = new param[3];
   x.t = x2-x1;
   x.cst = x1;
   y.t = y2-y1;
   y.cst = y1;
   z.t = z2-z1;
   z.cst = z1;
   result[0] = x;
   result[1] = y;
   result[2] = z;

   return result;
 }

class param{
 public double t;
 public double cst;

 param(){
   t = 0;
   cst = 0;
 }
}
Samba - 30 Dec 2003 09:56 GMT
Here is the whole code , the line param[] r = (param[])iter.next(); still
returns an exception in thread main.

package tc2;
import java.io.*;
import java.util.*;
import java.util.regex.*;

// essai ouverture fichier .vrml
public class essai {

 public essai() {
   try{
     //Ouverture fichier vrml
     FileInputStream fichier = new FileInputStream("D:/Mes documents
D/Cours/DEA 2003-2004/PROJET RV/Images 3D/bearing.vrml");
     InputStreamReader entree = new InputStreamReader(fichier);
     BufferedReader b = new BufferedReader(entree);
     String chaine;
     Coord coord = new Coord();
     Itrnoe itrnoe = new Itrnoe();

     //Construction des tableaux de coordonn?es de points et des sommets
des faces
     while ((chaine = b.readLine())!= null) {

       //R?cup?ration des coordonn?es des points et construction du tableau
Coord
       if (chaine.equals("# Coordonnees des points")){
         // pour ?viter que la derni?re ligne soit prise en compte
         chaine = b.readLine();
         coord.addPoint(0.0,0.0,0.0);

         while (!chaine.equals("]}}}}")){
           String[] sa = chaine.split("\\s+");
           coord.addPoint(new Double(sa[1]).doubleValue(), new
Double(sa[2]).doubleValue(),new Double(sa[3]).doubleValue());
           chaine = b.readLine();
         }
       }//Fin construction du tableau des points

       //R?cup?ration des faces des triangles et construction du tableau
Itrnoe
       if (chaine.equals("# Les faces")){
         // pour ?viter que la derni?re ligne soit prise en compte
         chaine = b.readLine();

         while (!chaine.equals("]ccw FALSE solid FALSE}}}")){
           String[] sa = chaine.split("\\s+");
           itrnoe.addFace(new Double(sa[1]).doubleValue(), new
Double(sa[2]).doubleValue(),new Double(sa[3]).doubleValue());
           chaine = b.readLine();
         }
       }

     }//Fin construction des tableaux Coord et Itrnoe

     //Essai de construction d'un vecteur des triangles s?lectionn?s
     Vector trianglesSelectionnes = new Vector();
     for (int k = 0; k<itrnoe.vec.size();k++){
       double[] test = itrnoe.getElement(k);
       //Affichage des sommets de ce triangle
       for (int j = 0; j<test.length; j++)
         System.out.println(test[j]);
       System.out.println("\n");
       //r?cup?ration des trois points de ce triangle
       int i = (int)test[0];
       double[] c1 = coord.getElement(i-1);
       i = (int)test[1];
       double[] c2 = coord.getElement(i-1);
       i = (int)test[2];
       double[] c3 = coord.getElement(i-1);

       if (appartientIntervalle(0.9, c1[0], c2[0])||
           appartientIntervalle(0.9, c1[0], c3[0])||
           appartientIntervalle(0.9, c2[0], c3[0])){
         System.out.println("Triangle " +k +" selectionne\n");
         trianglesSelectionnes.addElement(itrnoe.getElement(k));
       }
     }

     //Affichage du vecteur des triangles selectionnes
     System.out.println("Les sommets des " + trianglesSelectionnes.size()+"
triangles selectionnes sont :");
     for (int m = 0; m<trianglesSelectionnes.size(); m++){
       double[] t = (double[])trianglesSelectionnes.get(m);

       for (int j = 0; j<t.length; j++)
         System.out.print(t[j] +" ");

       System.out.println("\n");
     }

     // calcul des equations de droites du premier triangle
     // on met les equations de droites dans un tableau
     param[] tab = new param[3];
     double[] ind = (double[])trianglesSelectionnes.get(0);
     System.out.println("les sommets de ce 1er triangle et leurs
coordonnees:");
     for (int j = 0; j<ind.length; j++)
       System.out.print(ind[j] +" ");
     System.out.println("c1");
     int i = (int)ind[0];
     double[] c1 = coord.getElement(i-1);
     for (int j = 0; j<c1.length; j++)
       System.out.print(c1[j] +" ");
     System.out.println("c2");
     i = (int)ind[1];
     double[] c2 = coord.getElement(i-1);
     for (int j = 0; j<c2.length; j++)
       System.out.print(c2[j] +" ");
     System.out.println("c3");
     i = (int)ind[2];
     double[] c3 = coord.getElement(i-1);
     for (int j = 0; j<c3.length; j++)
       System.out.print(c3[j] +" ");
     // on calcul les equations des trois droites, on met le tout dans un
vecteur
     Vector equationsDroites = new Vector();
     int e1[] = {1,2,3};
   int e2[] = {2,3,4};int e3[] = {3,4,5};
     equationsDroites.addElement(e1);
     equationsDroites.addElement(e2);
     equationsDroites.addElement(e3);

     for (int u = 0; u<equationsDroites.size(); u++){
       System.out.println("boucle " +u);
       int[] temp = (int[])equationsDroites.get(u);
       System.out.println(temp[0]);
       System.out.println(temp[1]);
       System.out.println(temp[2]);
     }

     //bugg!!!!!

     //for (int w = 0; w<equationsDroites.size(); w++){
     for (Iterator iter = equationsDroites.iterator(); iter.hasNext();){

   // the problem seems to be that line!!
       param[] r = (param[])iter.next();

       System.out.println("x = " + r[0].t + " t" + "+ " + r[0].cst);
       System.out.println("y = " + r[1].t + " t" + "+ " + r[1].cst);
       System.out.println("z = " + r[2].t + " t" + "+ " + r[2].cst);
     }
            //}
     fichier.close();
     }catch(FileNotFoundException e){}
     catch (IOException ex){}
 }

 public boolean appartientIntervalle(double x, double v1, double v2){
   if (v1<v2){
     if ((x>=v1)&&(x<=v2))
       return true;
     else return false;
   }
   else {
     if ((x>=v2)&&(x<=v1))
       return true;
     else return false;
   }
 }

 public param[] calculEquationDroite(double x1, double y1, double z1,
double x2, double y2, double z2){
   // (x-x1)/(x2-x1) = (y-y1)/(y2-y1) = (z-z1)/(z2-z1)
   // Equation param?trique de la droite entre les points p1 et p2
   // x = t(x2-x1) + x1
   // y = t(y2-y1) + y1
   // z = t(z2-z1) + z1
   //tableau de resultat
   param[] result = new param[3];
   result[0] = new param (x2-x1, x1);
   result[1] = new param (y2-y1, y1);
   result[2] = new param (z2-z1, z1);

   return result;
 }

 public static void main(String args[]){
   essai e = new essai();
 }
}

class param{
 public double t;
 public double cst;

 param(double t , double cst){
   this.t = t;
   this.cst = cst;
 }
}
Remi Koutcherawy - 11 Jan 2004 14:08 GMT
Your error seems be in the type of elements you store in your vector.

int e1[] = {1,2,3};
equationsDroites.addElement(e1);
int[] temp = (int[])equationsDroites.get(u);
is OK.

But...
param[] r = (param[])iter.next();
Is deemed to a ClassCast exception as equationsDroites
contains only int[] and not param[]

May I suggest you to add the 3 following lines before your issue, to see :

  param[] p1 = {new param(1,2),new param(3,4),new param(5,6)};
  equationsDroites.clear();
  equationsDroites.addElement(p1);

  //bugg!!!!!

  //for (int w = 0; w<equationsDroites.size(); w++){
  for (Iterator iter = equationsDroites.iterator(); iter.hasNext();) {
  // the problem seems to be that line!!
   param[] r = (param[])iter.next();

   System.out.println("x = " + r[0].t + " t" + "+ " + r[0].cst);
   System.out.println("y = " + r[1].t + " t" + "+ " + r[1].cst);
   System.out.println("z = " + r[2].t + " t" + "+ " + r[2].cst);
}

This shall redeem something like :
x = 1.0 t+ 2.0
y = 3.0 t+ 4.0
z = 5.0 t+ 6.0

HTH.
Rémi
adhirmehta41@redifmail.com - 24 Jan 2004 05:51 GMT
Hi
u can solve the problem using right approach. instead of adding
param[](array) directly, first add the param one by one into List
class. and then add that list to the vector.u will not find any
problem.

> Your error seems be in the type of elements you store in your vector.
>
[quoted text clipped - 33 lines]
> HTH.
> Rémi


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.