///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 1995-1996 Virtual Design All Rights Reserved.
//
// Permission to use,  copy,  modify,  and  distribute  this  software and its
// documentation for NON-COMMERCIAL purposes and without fee is hereby granted.
//
// VIRTUAL DESIGN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
// OF THE SOFTWARE, EITHER EXPRESS  OR  IMPLIED,  INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
// OR NON-INFRINGEMENT. VIRTUAL DESIGN SHALL  NOT  BE LIABLE FOR ANY DAMAGES
// SUFFERED BY  LICENSEE AS A RESULT OF USING,  MODIFYING OR DISTRIBUTING THIS
// SOFTWARE OR ITS DERIVATIVES.
//
// THIS SOFTWARE  IS   NOT  DESIGNED  OR  INTENDED FOR USE OR RESALE AS ONLINE
// OR NOT ONLINE CONTROL  EQUIPMENT IN HAZARDOUS  ENVIRONMENTS REQUIRING  FAIL-
// SAFE PERFORMANCE,  SUCH  AS  IN   THE   OPERATION   OF  NUCLEAR  FACILITIES,
// AIRCRAFT   NAVIGATION  OR  COMMUNICATION  SYSTEMS,  AIR   TRAFFIC   CONTROL,
// DIRECT LIFE  SUPPORT  MACHINES, OR WEAPONS SYSTEMS, IN  WHICH  THE  FAILURE
// OF  THE SOFTWARE COULD  LEAD DIRECTLY  TO DEATH, PERSONAL INJURY, OR SEVERE
// PHYSICAL OR ENVIRONMENTAL  DAMAGE ("HIGH RISK ACTIVITIES").  VIRTUAL DESIGN
// SPECIFICALLY DISCLAIMS  ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH
// RISK ACTIVITIES.
///////////////////////////////////////////////////////////////////////////////

public class modtrans
 {
 // Les variables de définition.
 public static final int SUCCESS                          = 0 ;
 public static final int MOD_TRAN_ERROR_MEMORY            = 1 ;
 public static final int MOD_TRAN_ERROR_MSG               = 2 ;
 public static final int MOD_TRAN_ERROR_BAD_DATA_CARDINAL = 3 ;
 public static final int MOD_TRAN_ERROR_BAD_TRANS_PAR     = 4 ;

 // les données.
  public int GenerateVertex( Sommet3DH [] p_profil,
			                    Maillage3DH  p_data  ,
			                    GenerateType p_type  ,
			                    Vecteur3DH   p_trans ,
			                    int          p_pas   )
   {

   int   l_nbnewsommet ,
         l_borneprofilhaut = 0 ,
         l_borneprofilbas  = p_profil.length-1,
         l_i , l_j ,
         l_compt = 0 , l_depart = 0 ;
   float l_tx = p_trans.compox() ,
	      l_ty = p_trans.compoy() ,
	      l_tz = p_trans.compoz() ;

   // On verifis les données.
   // -----------------------
   if ( p_profil.length == 0 ) return MOD_TRAN_ERROR_BAD_DATA_CARDINAL;


   if ( p_pas == 0 ||
        (l_tx ==0 && l_ty==0 && l_tz==0))
        return MOD_TRAN_ERROR_BAD_TRANS_PAR;

   // On calcul le nombre de nouveaux sommets
   // ----------------------------------------

   // le profil est il un domaine?
   if ( p_profil[0].m_x == p_profil[ l_borneprofilbas ].m_x &&
        p_profil[0].m_y == p_profil[ l_borneprofilbas ].m_y &&
        p_profil[0].m_z == p_profil[ l_borneprofilbas ].m_z )
      {
      p_type.m_pvert = GenerateType.VERT_CLOSE ;
      l_borneprofilbas -- ;
      }
   else p_type.m_pvert = GenerateType.VERT_OPEN ;


   // On alloue la table  des sommets.
   // --------------------------------
   l_nbnewsommet = ((p_type.m_pvert == GenerateType.VERT_OPEN)?
                   (p_profil.length): (p_profil.length-1)) * p_pas ;

   try
    {
    p_data.m_sommets = new Sommet3DH[l_nbnewsommet ] ;
    }
   catch ( OutOfMemoryError p_oome )
    {
    System.out.println( "Erreur d'allocation des sommets. Modeleur." );
    return MOD_TRAN_ERROR_MEMORY ;
    }

    // On commence par copier le profil initial.
    // -----------------------------------------
    l_compt = 0 ;
    for ( l_j = l_borneprofilhaut ; l_j <= l_borneprofilbas ; l_j ++ )
      {
      p_data.m_sommets[l_compt] = new Sommet3DH(p_profil[l_j]) ;

      l_compt ++ ;
      }


    // on initialise la matrice de translation.
    // ----------------------------------------
    Matrice44 l_mat = new Matrice44() ;
    l_mat.setIdentity();
    l_mat.setTranslateX(l_tx);
    l_mat.setTranslateY(l_ty);
    l_mat.setTranslateZ(l_tz);

    // On fait une boucle sur les profils restants.
    // --------------------------------------------
    l_depart = 0 ;
    for ( l_i = 1 ; l_i < p_pas ; l_i ++ )
      {
      for ( l_j = l_borneprofilhaut ; l_j <= l_borneprofilbas ; l_j ++ )
	       {
	       p_data.m_sommets[l_compt]= new Sommet3DH(p_data.m_sommets[l_depart]);

          // translation.
          p_data.m_sommets[l_compt].multiplyByMatrix(l_mat);

	       l_compt ++  ;
	       l_depart++  ;
	       }
      }

    // Mise à jour des renseignements sur la forme générée.
    // ----------------------------------------------------
    p_type.m_ponaxis = 0                     ;
    p_type.m_phor    = GenerateType.HOR_OPEN ;
    p_type.m_type    = GenerateType.SURFACE  ;

    return SUCCESS ;
    }

 }
