#include <cgx.h>
#include <time.h>
#include <sys/utsname.h>

#define     VERSION         1.5
#define     TEST            0
#define     MAX_FLOAT       1.e32


/* special cases */

/* temporary conversion from old to new bias definition */
int OLD_BIAS_DEF=0;
/* generate sets from pressure-loads */
int MAKE_SETS_DEF=0;


void generalinfo()
{
  printf("  --------------------------------------------------------------------  \n");
  printf("                           CALCULIX                                     \n");
  printf("                    - GRAPHICAL INTERFACE -                             \n");
  printf("      Version %.1lf                                                        \n", VERSION);
  printf("                                                                        \n");
  printf("                                                                        \n");
  printf("      A 3-dimensional pre- and post-processor for finite elements       \n");
  printf("               Copyright (C) 1996, 2002 Klaus Wittig                    \n");
  printf("                                                                        \n");
  printf("      This program is free software; you can redistribute it and/or     \n");
  printf("      modify it under the terms of the GNU General Public License as    \n");
  printf("      published by the Free Software Foundation; version 2 of           \n");
  printf("      the License.                                                      \n");
  printf("                                                                        \n");
  printf("      This program is distributed in the hope that it will be useful,   \n");
  printf("      but WITHOUT ANY WARRANTY; without even the implied warranty of    \n"); 
  printf("      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      \n");
  printf("      GNU General Public License for more details.                      \n");
  printf("                                                                        \n");
  printf("      You should have received a copy of the GNU General Public License \n");
  printf("      along with this program; if not, write to the Free Software       \n");
  printf("      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         \n");
  printf("  --------------------------------------------------------------------  \n");
  printf("\n usage: cgx [parameter] filename [ccxfile]                            \n\n");
  printf(" Parameters:                                                            \n");
  printf("  -a  auto-mode, geometry file derived from a cad-file must be provided \n");
  printf("  -b  build-mode, geometry file must be provided                        \n");
  printf("  -c  read an solver input file (ccx)                                   \n");
  printf("  -duns2d  read duns result files (2D)                                  \n");
  printf("  -duns3d  read duns result files (3D)                                  \n");
  printf("  -foam    read openFoam result files                                   \n");
  printf("  -isaac2d [-pref<val> -tref<val> -R<val>] read isaac result files (2D) \n");
  printf("  -isaac3d [-pref<val> -tref<val> -R<val>] read isaac result files (3D) \n");
  printf("  -stl  read stl triangles                                              \n");
  printf("  -v  (default) read a result file in frd-format and optional a solver  \n");
  printf("      input file (ccx) which provides the sets and loads used in the    \n");
  printf("      calculation.                                                      \n");
  printf("\n special purpose options:                                               \n");
  printf("  -oldbias      convert pre-1.5 bias                                    \n");
  printf("  -mksets       make node-sets from *DLOAD-values (setname: \"_<value>\" ) \n");
}

/* 
Necessary system-routines and libs:
    glut
    openGL
    system
    sort
    rm
*/
/* 
Necessary stand alone programs:
- for postscript Hardcopys 
    xlsclients, xwd, convert
- for bitmap Hardcopys 
    xlsclients, xwd
*/
/* 
Optional stand alone programs:
- for 2D plots
    gnuplot
- for online-help
    netscape
*/
/* 
Known problems:
- to speed up the search of an entity-index a "sum" of the ascii value of the
  entity-name is  calculated. This value must be lower as MAX_SUM_ASCI which
  should be the case if the names use only 8 ascii values. But it is not
  tracked because of speed and it might be too high for some names, especially
  the program generated names are not watched.
  This could lead to a segmentation fault.
  The reallocation of this fields should be implemented later.
*/
/* 
Known bugs:
*/
/* 
TODO:
- *NSET, *ELSET should also accept pre-defined sets
- line-, surf-, body-splitter
- trim lines (arc or sharp corner )
- proj nodes, points onto a set of embedded nurbs
- "big" node and element numbers should be possible, hash-table has to be implemented
- to be included in the menu:
  glPointSize ();
  glLineWidth ();
  font size
*/


/* Display-lists */
GLuint list_model_edges, list_surf_edges, list_elem_edges ;
GLuint list_elem_light, list_elem_load;           
GLuint list_surf_light, list_surf_load, list_animate_light;
GLuint list_elem_elstress, list_animate[MAX_LOADCASES];
GLuint list_vector[MAX_LOADCASES];


Summen    anz[1]; 
SumGeo    anzGeo[1];
SumAsci   sumAsci[1];

SpecialSet specialset[1];

Nodes     *node=NULL;
Datasets *lcase=NULL;
NodeBlocks *nBlock;
Alias     *alias=NULL;
Sets      *set=NULL;
Psets     *pset=NULL;
Points    *point=NULL;
Lines     *line=NULL;
Lcmb      *lcmb=NULL;
Gsur      *surf=NULL;
Gbod      *body=NULL;
Nurbl     *nurbl=NULL;
Nurbs     *nurbs=NULL;


double     *value_n=NULL;
Elements  *e_enqire=NULL;     /* elem-array indexed by elem-number */
double     *vp=NULL;
Nodes     *n_anim=NULL;
Scale     scale[1];
Faces     *face;
Edges     *edge=NULL;             /* model-edges           */

Meshp meshp={ALPHA,BETA,NADAPT};   /* mesh parameters for tr3u elements. Used in mesh2d */

int       num_onod;          /* nr-of-original-nodes (nodes from input-file) */
int       num_olc;          /* nr-of-original-loadcases (lc from input-file) */



#if INX_MODE
int       *colNr;
#endif
#if TEX_MODE
double     *colNr;
#endif
GLfloat   *contur_tex=NULL;

/* for CFD-meshing */
double pref=1.e5, tref=288., R_GAS=287.1;

struct utsname  cursys[1];
int             bitplanes;            /*  colorbuffer depth */

Display       *dpy;
int           dpycells;
Colormap      cmap;
XColor        *xcolor;
unsigned long *pixels_return;
unsigned int  npixels;
double         priv_cmap[256][3];


/* Main-Prog and GLUT-Window Management */
unsigned char  buffer[MAX_LINE_LENGTH];                          /* string buffer */
unsigned char  datin[MAX_LINE_LENGTH];                          /* Input-data-file */
unsigned char  browser[MAX_LINE_LENGTH]=BROWSER;  /* html-browser */
unsigned char  psviewer[MAX_LINE_LENGTH]=PSVIEWER;  /* postscript viewer */
unsigned char  helpfile[2][MAX_LINE_LENGTH]=HELPFILE;  /* help-file */
unsigned char  xbuffer[MAX_LINE_LENGTH];
int   width_ini={INI_SCREEN}, height_ini={INI_SCREEN}; /* Grafik-Fensterbreite/hoehe */
int   width_menu={INI_MENU_WIDTH}, height_menu={INI_MENU_HEIGHT};
int   width_w0, height_w0;
int   width_w1, height_w1;
int   open_cgxs;              /* No. of cgx-sessions on that screen */
int   w0, w1, w2;                           /* window identifier  */
int   activWindow={-1};                      /* das aktuelle Fenster */
int   animList=0;                           /* die aktuelle Displayliste fuer animation */

int   basCol[3]={0,1,2};       /* color indexes due to basic colormap: 0=bl. 1=w 2=gy */
int   foregrndcol=0, backgrndcol=1;          /* default fore- and background color */
double foregrndcol_rgb[4]={0.,0.,0.,1.};
double backgrndcol_rgb[4]={1.,1.,1.,1.};
GLfloat entity_r[SET_COLS]={0., 1., 1., 0., 0., 1., 1., 1. };  /* predefined colors of entities */
GLfloat entity_g[SET_COLS]={0., 1., .0, 1., 0., 1., 0., .5 };
GLfloat entity_b[SET_COLS]={0., 1., .0, 0., 1., 0., 1., .5 };

int   submenu_load, submenu_view, mainmenu;        /* menu identifier */
int   submenu_scala, submenu_animate, submenu_help;
int   submenu_orientation, submenu_hardcopy;
int   subsubmenu_entity;
int   subsubmenu_animTune, subsubmenu_animSteps;
int   subsubmenu_animPeriod;
int   subsubmenu_load[100];

GLfloat lmodel_twoside[] = { GL_TRUE };
GLfloat lmodel_oneside[] = { GL_FALSE };
double dx ,dy;                                      /* Mauskoordinaten im bereich +-1*/
int   xpixels ,ypixels;                            /* Mauskoordinaten in pixel, links unten 0,0 */
double beginx, beginy;                              /* letzter mousepoint */
double trackbsize={0.8};                            /* TRACKBALLSIZE */
double curquat[4];                                  /* Matrix aus Trackball */
double lastquat[4];                                 /* letzte Matrix aus Trackball*/
GLint   gl_max_eval_order;                         /* max order of NURBS */
GLdouble R[4][4];                                   /* Rotationsmatrix */
GLdouble dR[4][4];                                  /* dR= R-Rmem fuer center */
GLdouble Rmem[4][4];
double v[4];                                        /* drehkorrekturen fuer centerPkt */
double vmem[4];                                     /* kor. bis auswahl eines neuen drehpkts */
int   MouseMode;                                   /* status maustasten */
GLdouble dx_cur={PICK_LENGTH}, dy_cur={PICK_LENGTH};   /* pick-cursor Area */

unsigned char  frameFlag={1};                   /* mit (1) oder ohne Rahmen um das Grafikfenster */
unsigned char  filenamFlag={1};                 /* mit (1) oder ohne filename im Menufenster */
unsigned char  textFlag={1};                    /* mit (1) oder ohne text im Menufenster */
unsigned char  printFlag=0;                     /* printf on/off on=1 (kommando 'msg' 'on'|'off' )*/
unsigned char  scalaFlag={1};                   /* mit (1) oder ohne scala und wertetexte */ 
unsigned char  sequenceFlag=0;                  /* 1: play a sequence of LC */
unsigned char  vectorFlag=0;                    /* 0: scalar plot, 1: vector plot */
unsigned char  addDispFlag=0;                   /* 0: original node-coordinates, 1: node-coordinates+displacements */
unsigned char  v_dim=0;                         /* 1: scalar plot, 2: a 2D vector plot, 3: a 3D vectorplot */
unsigned char  lightFlag=0;                     /* sequence with illumination */
unsigned char  movieFlag=0;                     /* >0: save sequence of gif pictures */
int seqLC[MAX_LOADCASES], seq_nlc=0;
int   seq_steps=1;                            /* anzahl der displaylisten einer sequenz */
int v_LC[MAX_LOADCASES];                         /* vector-plot loadcases */
int   lcase_animList={-1};                       /* additional lcase for the values of the animation */

unsigned char  surfFlag={1};                     /* zeichne nur Oberflaechenelemente (1), sonst (0)*/
unsigned char  modelEdgeFlag={1};                /* zeichne mit Ecken (1), sonst (0)*/
unsigned char  elemEdgeFlag={0};                 /* zeichne mit Surface Ecken (1), sonst (0)*/
unsigned char  drawMode={0};                     /* actual draw-function (Load=1, Light=2, Animate=3, Preprocessor=4, Vector=5)*/
unsigned char  stopFlag=0;                      /* stop/start animation */
unsigned char  zoomFlag={0};                    /* (1) zoom Modus */
unsigned char  centerFlag={0};                  /* (1) search centerPnt */
int   centerNode=0;                    /* Nr of center Node, 0:no centernode */
unsigned char  cutFlag={0};                     /* (1) move Model in Z Direction through the cuting plane */
unsigned char  cullFlag={1};                    /* (1) enable Culling */
unsigned char  pickfunc[MAX_LINE_LENGTH];       /* pick-function like "qenq" "qadd" "qrem" ..   */
unsigned char  pickFlag={0};                    /* 1 if picking is active */
unsigned char  delPntFlag={0};                  /* 1: deleted points exists */
unsigned char  delLineFlag={0};                 /* 1: deleted lines exists */
unsigned char  delLcmbFlag={0};                 /* 1: deleted lcmbs exists */
unsigned char  delSurfFlag={0};                 /* 1: deleted surfs exists */
unsigned char  delBodyFlag={0};                 /* 1: deleted bodys exists */
unsigned char  delNursFlag={0};                 /* 1: deleted Nurbs exists */
unsigned char  delSetFlag={0};                  /* 1: deleted sets exists */
unsigned char  fillSurfFlag={0};                /* 1: generate triangles for surface-rendering and projection */
int   hcpyFlag={0};                    /* triggers createHardcopy if !=0 */

unsigned char  mode[2]={'i'};                        /* pickmode */
double minvalue, maxvalue;                          /* Wertebereich */
int   steps={21};                                  /* Schrittweite der Farbscala */
int   offset, maxIndex;                            /* offset+steps-1 = maxIndex */
int   anim_steps={8};                              /* Animationen pro Schwingung */
double anim_faktor={1.};                            /* Scalierung der Amplitude */
int   anim_alfa[MAX_ANIM_STEPS];                   /* amplitude der Animation fuer Bildbeschriftung */
int   time_per_period={MILLISECONDS_PER_PERIOD}; /* fuer Animation */
int   frameNr={0};                                 /* zaehlt alle animierten Bilder */
int   halfperiod={0};                             /* 1:Animation nur der posit. Halbperiod */
int   hcpy={0};                                    /* hcpy=1 Hardcopy angefordert */
double dtx={0.}, dty={0.}, dtz={0.}, drx, dry, drz, ds={0.5};            /* Verschiebungen */
double centerPnt[3];                                /* Rotationszentrum */
int   entity_Nr={1};                                       /* aktive entity (component) */
int   entity_v[3];                                         /* components of a vector-entity */
double v_factor;                                    /* scaling-factor for the vectors in the vector-plot */
double v_scale={1.};                                     /* additional scaling-factor for the vectors */
int       pre_lc={0};                                      /* pre-selected Dataset, active after entity is selected */
int       lc={0};                                          /* aktive Dataset */
int       elemMat[MAX_MATERIALS]={1,1};      /*  Material Numbers, Number of Materials stored in elemMat[0]  */
unsigned char  picture_text[MAX_LINE_LENGTH]= {" "};               /* Text on window base line */

double     gtol={GTOL};                                    /* geometric tolerance for merging */
int     ddiv={DEF_LINE_DIV};
double     dbias=1;
int       neqn={0};            /* start-number of equations in ansys, MID in nastran */

/* nr of hardcopies */
int psNr, xwdNr, gifNr, pngNr;

/* track the open sets */
OpenSets openSets[1];

/* saved  plot, plus commands */
unsigned char **plusstr=NULL, plotstr[MAX_LINE_LENGTH];
int anz_plusstr=0;


int askGLError(unsigned char buffer[MAX_LINE_LENGTH])
{
  if (glGetError() == (GLenum)GL_NO_ERROR) return(1);
  else if (glGetError() == (GLenum)GL_INVALID_ENUM) printf ("in:%s GL_INVALID_ENUM\n",buffer);
  else if (glGetError() == (GLenum)GL_INVALID_VALUE) printf ("in:%s GL_INVALID_VALUE \n",buffer);
  else if (glGetError() == (GLenum)GL_INVALID_OPERATION) printf("in:%s GL_INVALID_OPERATION\n",buffer);
  else if (glGetError() == (GLenum)GL_STACK_OVERFLOW) printf ("in:%s  GL_STACK_OVERFLOW\n",buffer);
  else if (glGetError() == (GLenum)GL_STACK_UNDERFLOW) printf ("in:%s  GL_STACK_UNDERFLOW\n",buffer);
  else if (glGetError() == (GLenum)GL_OUT_OF_MEMORY) printf ("in:%s  GL_OUT_OF_MEMORY\n",buffer);
  else {printf("glGetError detects unknown error %d in:%s\n", glGetError(), buffer); return (-1);}
  return (0);
}

void allocUtilityData(void)
{
  /* Colors, animierte Nodes, Wertepuffer reallocieren  */

#if INX_MODE
  if ( (colNr = (int *)realloc((int *)colNr, (anz->nmax+1) * sizeof(int))) == NULL )
    printf("\n\n ERROR: realloc failed colNr\n\n") ;
  else
    if(printFlag) printf (" colNr reallocated \n");
#endif
#if TEX_MODE
  if ( (colNr = (double *)realloc((double *)colNr, (anz->nmax+1) * sizeof(double))) == NULL )
    printf("\n\n ERROR: realloc failed colNr\n\n") ;
  else
    if(printFlag) printf (" colNr reallocated \n");
#endif

  if ( (n_anim = (Nodes *)realloc((Nodes *)n_anim, (anz->nmax+1) * sizeof(Nodes))) == NULL )
    printf("\n\n ERROR: malloc failed n_anim\n");
  else
    if(printFlag) printf (" n_anim allocated \n");
  if ( (value_n = (double *)realloc((double *)value_n, (anz->nmax+1) * sizeof(double))) == NULL )
    printf("\n\n ERROR: malloc failed value_n\n");
  else
    if(printFlag) printf (" value_n allocated \n");
}


#if INX_MODE
void nodalDataset( int entity_Nr, int lc, Summen *anz, Scale *scale, Nodes *node
                  , Datasets *lcase, double *value_n, int *colNr, int offset, int maxIndex, int steps )
#endif 
#if TEX_MODE
void nodalDataset( int entity_Nr, int lc, Summen *anz, Scale *scale, Nodes *node
                  , Datasets *lcase, double *value_n, double *colNr )
#endif 
{
  int i, n, n1, n2;

  /* check if the specified lcase-component is allocated */
  if (lcase[lc].dat[entity_Nr-1] == NULL )
    printf("ERROR: Component not available\a\n");
  else 
  {
    for ( i=0; i<anz->n; i++ ) if(!node[node[i].nr].pflag)
    {
      value_n[node[i].nr]  = lcase[lc].dat[entity_Nr-1][node[i].nr];
    }
  }
  scale->vmax=lcase[lc].max[entity_Nr-1];
  scale->vmin=lcase[lc].min[entity_Nr-1];
  scale->nmax=lcase[lc].nmax[entity_Nr-1];
  scale->nmin=lcase[lc].nmin[entity_Nr-1];

  /* create values for the additional nodes in case some elements are HE26 */
  /* Warning: All additional Nodes must belong to He26!                    */
  for ( i=0; i<anz->e; i++ )
  {
    if(e_enqire[e_enqire[i].nr].type==4)
    {
      for (n=0; n<3; n++)  /* create new nodes in center of areas */
      {
        value_n[e_enqire[e_enqire[i].nr].nod[20+n]] = -0.25* (
          value_n[e_enqire[e_enqire[i].nr].nod[0+n]]+value_n[e_enqire[e_enqire[i].nr].nod[1+n]]    +
          value_n[e_enqire[e_enqire[i].nr].nod[5+n]]+value_n[e_enqire[e_enqire[i].nr].nod[4+n]] )  + 0.5*(
          value_n[e_enqire[e_enqire[i].nr].nod[8+n]]+value_n[e_enqire[e_enqire[i].nr].nod[13+n]]   +
          value_n[e_enqire[e_enqire[i].nr].nod[16+n]]+value_n[e_enqire[e_enqire[i].nr].nod[12+n]]) ;
      }
      /* create  new node in center of area4 */
        value_n[e_enqire[e_enqire[i].nr].nod[23]] = -0.25* (
          value_n[e_enqire[e_enqire[i].nr].nod[3]]+value_n[e_enqire[e_enqire[i].nr].nod[0]]    +
          value_n[e_enqire[e_enqire[i].nr].nod[4]]+value_n[e_enqire[e_enqire[i].nr].nod[7]] )  + 0.5*(
          value_n[e_enqire[e_enqire[i].nr].nod[11]]+value_n[e_enqire[e_enqire[i].nr].nod[12]]   +
          value_n[e_enqire[e_enqire[i].nr].nod[19]]+value_n[e_enqire[e_enqire[i].nr].nod[15]]) ;
      for (n=0; n<2; n++)  /* create last 2 new nodes in center of areas */
      {
        n1=n*4;
        n2=n*8;
        value_n[e_enqire[e_enqire[i].nr].nod[24+n]] = -0.25* (
          value_n[e_enqire[e_enqire[i].nr].nod[0+n1]]+value_n[e_enqire[e_enqire[i].nr].nod[1+n1]]    +
          value_n[e_enqire[e_enqire[i].nr].nod[2+n1]]+value_n[e_enqire[e_enqire[i].nr].nod[3+n1]] )  + 0.5*(
          value_n[e_enqire[e_enqire[i].nr].nod[8+n2]]+value_n[e_enqire[e_enqire[i].nr].nod[9+n2]]   +
          value_n[e_enqire[e_enqire[i].nr].nod[10+n2]]+value_n[e_enqire[e_enqire[i].nr].nod[11+n2]]) ;
      }
    }
    else if(e_enqire[e_enqire[i].nr].type==5)
    {
      for (n=0; n<2; n++)  /* create new nodes in center of areas */
      {
        value_n[e_enqire[e_enqire[i].nr].nod[15+n]] = -0.25* (
          value_n[e_enqire[e_enqire[i].nr].nod[0+n]]+value_n[e_enqire[e_enqire[i].nr].nod[1+n]]    +
          value_n[e_enqire[e_enqire[i].nr].nod[4+n]]+value_n[e_enqire[e_enqire[i].nr].nod[3+n]] )  + 0.5*(
          value_n[e_enqire[e_enqire[i].nr].nod[6+n]]+value_n[e_enqire[e_enqire[i].nr].nod[10+n]]   +
          value_n[e_enqire[e_enqire[i].nr].nod[12+n]]+value_n[e_enqire[e_enqire[i].nr].nod[ 9+n]]) ;
      }
      /* create  new node in center of area3 */
        value_n[e_enqire[e_enqire[i].nr].nod[17]] = -0.25* (
          value_n[e_enqire[e_enqire[i].nr].nod[2]]+value_n[e_enqire[e_enqire[i].nr].nod[0]]    +
          value_n[e_enqire[e_enqire[i].nr].nod[3]]+value_n[e_enqire[e_enqire[i].nr].nod[5]] )  + 0.5*(
          value_n[e_enqire[e_enqire[i].nr].nod[ 8]]+value_n[e_enqire[e_enqire[i].nr].nod[ 9]]   +
          value_n[e_enqire[e_enqire[i].nr].nod[14]]+value_n[e_enqire[e_enqire[i].nr].nod[11]]) ;
      /* create  new node in center of area4 */
        value_n[e_enqire[e_enqire[i].nr].nod[18]] = -0.25* (
          value_n[e_enqire[e_enqire[i].nr].nod[0]]+value_n[e_enqire[e_enqire[i].nr].nod[2]]    +
          value_n[e_enqire[e_enqire[i].nr].nod[1]]+value_n[e_enqire[e_enqire[i].nr].nod[0]] )  + 0.5*(
          value_n[e_enqire[e_enqire[i].nr].nod[ 8]]+value_n[e_enqire[e_enqire[i].nr].nod[ 7]]   +
          value_n[e_enqire[e_enqire[i].nr].nod[ 6]]+value_n[e_enqire[e_enqire[i].nr].nod[ 0]]) ;
      /* create  new node in center of area5 */
        value_n[e_enqire[e_enqire[i].nr].nod[19]] = -0.25* (
          value_n[e_enqire[e_enqire[i].nr].nod[3]]+value_n[e_enqire[e_enqire[i].nr].nod[4]]    +
          value_n[e_enqire[e_enqire[i].nr].nod[5]]+value_n[e_enqire[e_enqire[i].nr].nod[3]] )  + 0.5*(
          value_n[e_enqire[e_enqire[i].nr].nod[12]]+value_n[e_enqire[e_enqire[i].nr].nod[13]]   +
          value_n[e_enqire[e_enqire[i].nr].nod[14]]+value_n[e_enqire[e_enqire[i].nr].nod[ 3]]) ;
    }
    else if(e_enqire[e_enqire[i].nr].type==10)
    {
      value_n[e_enqire[e_enqire[i].nr].nod[8]] = -0.25* (
      value_n[e_enqire[e_enqire[i].nr].nod[0]]+value_n[e_enqire[e_enqire[i].nr].nod[1]] +
      value_n[e_enqire[e_enqire[i].nr].nod[3]]+value_n[e_enqire[e_enqire[i].nr].nod[2]])+0.5*(
      value_n[e_enqire[e_enqire[i].nr].nod[4]]+value_n[e_enqire[e_enqire[i].nr].nod[6]] +
      value_n[e_enqire[e_enqire[i].nr].nod[7]]+value_n[e_enqire[e_enqire[i].nr].nod[5]]);
    }
  }

  /* ------------  Darstellung scalieren -------------------  */

  if (scale->bmin==scale->bmax)
  {
    scale->bmin = scale->vmin;
    scale->bmax = scale->vmax;
  }


  for (i=0; i<anz->n; i++ )
  {
#if INX_MODE
    /* ColorIndex bestimmen colNr = (0->1)*(steps) + Offset */
    if ( value_n[node[i].nr] <= scale->bmin )
    {
      colNr[node[i].nr] = offset;
    }
    else if ( value_n[node[i].nr] >= scale->bmax )
    {
      colNr[node[i].nr] = maxIndex;
    }
    else
    {
      colNr[node[i].nr] = (value_n[node[i].nr]-scale->bmin)/(scale->bmax-scale->bmin)*steps  +offset;
      if ( colNr[node[i].nr] > maxIndex )  colNr[node[i].nr] = maxIndex;
      if ( colNr[node[i].nr] < offset )  colNr[node[i].nr] = offset;
    }
#endif
#if TEX_MODE
    if ( value_n[node[i].nr] <= scale->bmin )
    {
      colNr[node[i].nr] = 0.;
    }
    else if ( value_n[node[i].nr] >= scale->bmax )
    {
      colNr[node[i].nr] = (double)steps/(double)TEX_PIXELS;
    }
    else
    {
      colNr[node[i].nr] = (value_n[node[i].nr]-scale->bmin)/(scale->bmax-scale->bmin) *steps/TEX_PIXELS;
    }
#endif
  }
}


void elementDataset( int entity_Nr, int lc, Summen *anz, Scale *scale, Datasets *lcase, int offset, int maxIndex, int steps )
{
  int   i;
  double **vp;

  if ( (vp = (double **)malloc( (anz->emax+1) * sizeof(double))) == NULL )
    printf("\n\n ERROR: malloc failed vp\n");
  for (i=0; i<(anz->emax+1); i++)
    if ( (vp[i] = (double *)malloc( (2) * sizeof(double))) == NULL )
      printf("\n\n ERROR: malloc failed vp[%d]\n", i);

  /* check if the specified lcase-component is allocated */

/*
  if (lcase[lc].edat[entity_Nr-1] == NULL )
    printf("ERROR: Component not available\a\n");
  else 
  {
    for (i=0; i<num_etype[11]; i++ )
    {
      for (j=0; j<2; j++ )
      {
        vp[i][j]  = lcase[lc].edat[entity_Nr-1][cbeam[i].elem_nr][j];
      }
    }
  }
  scale->vmax=lcase[lc].max[entity_Nr-1];
  scale->vmin=lcase[lc].min[entity_Nr-1];

  for (i=0; i<num_etype[11]; i++ )
  {
    for (j=0; j<2; j++ )
    {
      if (scale->bmin==0)
      {
        scale->bmin = scale->vmin;
      }
      if (scale->bmax==0)
      {
        scale->bmax = scale->vmax;
      }
#if INX_MODE
      if ( vp[i][j] <= scale->bmin )
      {
        cbeam[i].ncol[j][0] = offset;
      }
      if ( vp[i][j] >= scale->bmax )
      {
        cbeam[i].ncol[j][0] = maxIndex;
      }
      if ( (vp[i][j] > scale->bmin) && (vp[i][j] < scale->bmax) )
      {
        cbeam[i].ncol[j][0] = (vp[i][j]-scale->bmin)/(scale->bmax-scale->bmin)*steps  +offset;
        if ( cbeam[i].ncol[j][0] > maxIndex )  cbeam[i].ncol[j][0]= maxIndex;
        if ( cbeam[i].ncol[j][0] < offset )  cbeam[i].ncol[j][0]= offset;
      }
      printf ("v:%lf col:%lf  \n", vp[i][j], cbeam[i].ncol[j][0] );
#endif
#if TEX_MODE
      if ( vp[i][j] <= scale->bmin )
      {
        cbeam[i].ncol[j][0] = 0.;
      }
      if ( vp[i][j] >= scale->bmax )
      {
        cbeam[i].ncol[j][0] = 1.;
      }
      if ( (vp[i][j] > scale->bmin) && (vp[i][j] < scale->bmax) )
      {
        cbeam[i].ncol[j][0] = (vp[i][j]-scale->bmin)/(scale->bmax-scale->bmin);
      }
      printf ("v:%lf col:%lf  \n", vp[i][j], cbeam[i].ncol[j][0] );
#endif
    }
  }
*/

  for (i=0; i<(anz->emax+1); i++) free(vp[i]);
  free(vp);
}


void createHardcopy( int selection )
{
  unsigned char buffer[MAX_LINE_LENGTH], winid[MAX_LINE_LENGTH], name[MAX_LINE_LENGTH];
  int  sb,length;
  FILE *handle;

  if(selection==0)
  {
    /* generate movie from single gif files */
    sprintf( buffer, "make 0. %lf",(double)gifNr);
    pre_movie(buffer);
    gifNr=0;
  }
  else
  {
    /* X-Hardcopy mit xwd         */
    sprintf( buffer, "rm -f $HOME/CalculiX.tmp >/dev/null");
    system (buffer);
    sprintf( buffer, "xlsclients -l  > $HOME/CalculiX.tmp");
    system (buffer);
    handle=fopen(xbuffer,"r+");
    if (handle==NULL)
    { printf ("\nThe output file \"%s\" could not be opened.\n\n", xbuffer); return;}
    sb=0;
    do
    {
      length = frecord( handle, buffer);
      if( compare ("Window",buffer,6) == 6)
      {
        stos( buffer,8,16,winid);
      }
      stos( buffer,10,32,name);
      sprintf( buffer, "Calculix Graphix No.%d  ", open_cgxs );
      if ( compare (buffer,name,22) == 22) { sb=1; break; }
    } while ( length >-1);
    sprintf( buffer, "rm -f $HOME/CalculiX.tmp >/dev/null ");
    system (buffer);
    if (sb)
    {
      if(selection==1)
      {
        psNr++;
        printf("create hcpy_%d.ps\n ",psNr);
        sprintf( buffer, "xwd -id %s  -nobdrs -out hcpy_%d.xwd  ",winid,psNr);
        system (buffer);
        /* on some systems PS has to be changed to PS2 */
        sprintf( buffer, "convert -density %dx%d -page +49+196 -gamma %lf hcpy_%d.xwd PS:hcpy_%d.ps   ", (int)((double)(PS_DENSITY*width_w0)/(double)(INI_SCREEN+INI_MENU_WIDTH)),(int)((double)(PS_DENSITY*width_w0)/(double)(INI_SCREEN+INI_MENU_WIDTH)) , GAMMA, psNr, psNr);
        system (buffer);
        sprintf( buffer, "rm -f hcpy_%d.xwd >/dev/null",psNr);
        system (buffer);
        printf ("ready\n");
      }
      if(selection==2)
      {
        xwdNr++;
        printf("create hcpy_%d.xwd\n ",xwdNr);
        sprintf( buffer, "xwd -id %s  -nobdrs -out hcpy_%d.xwd  ",winid,xwdNr);
        system (buffer);
        printf ("ready\n");
      }
      if(selection==3)
      {
        /* movie gif files */
        sprintf( buffer, "xwd -id %s  -nobdrs -out _.xwd",winid);
        system (buffer);
        sprintf( buffer, "convert _.xwd _%d.gif",gifNr++);
        system (buffer);
      }
      if(selection==4)
      {
        printf("create hcpy_%d.gif\n ",gifNr);
        sprintf( buffer, "xwd -id %s  -nobdrs -out _.xwd",winid);
        system (buffer);
        sprintf( buffer, "convert _.xwd hcpy_%d.gif",gifNr++);
        system (buffer);
        sprintf( buffer, "rm -f  _.xwd >/dev/null");
        system (buffer);
      }
      if(selection==5)
      {
        printf("create hcpy_%d.png\n ",pngNr);
        sprintf( buffer, "xwd -id %s  -nobdrs -out _.xwd",winid);
        system (buffer);
        sprintf( buffer, "convert _.xwd hcpy_%d.png",pngNr++);
        system (buffer);
        sprintf( buffer, "rm -f  _.xwd >/dev/null");
        system (buffer);
      }
    } else printf ("ERROR: could not create Hardcopy\a\n\n");
    glutSetWindow( w0);
    fclose (handle);
  }
}


void entryfunktion( int state )
{
  if (state==GLUT_ENTERED)
  {
    activWindow=w1;
    glutPostRedisplay();
      glutSetWindow( w2);
      glutPostRedisplay();
    glutSetWindow(activWindow );
  }
  else
  {
    activWindow=w0;
  }
}


void WindowState( int state )
{
  if (state==GLUT_NOT_VISIBLE)
    activWindow=w0;
}


void Mouse( int x, int y )
{
  xpixels=x; ypixels=y-height_w1;
  dx= (width_w1/2.-(double)x)/width_w1*-2.;
  dy= (height_w1/2.-(double)y)/height_w1*2.;
  if (MouseMode == 3)
  {
    trackball( 0, trackbsize, curquat, beginx, beginy, dx, dy);
    add_quats(curquat, lastquat, lastquat);
    build_rotmatrix( R, lastquat );
  }
  if (MouseMode == 1)
  {
    dtx+= (dx-beginx);
    dty+= (dy-beginy);
  }
  if (MouseMode == 2)
  {
    if (cutFlag)
    {
      dtz+= (dy-beginy);
    }
    else
    {
    dtx*=ds;
    dty*=ds;
    ds+= (beginy-dy)*ds;
    dtx/=ds;
    dty/=ds;
    }
  }
  beginx = dx;
  beginy = dy;
  if ( activWindow == w1 )
  {
    glutPostRedisplay();
      glutSetWindow( w2);
      glutPostRedisplay();
    glutSetWindow(activWindow );
  }
}


void MouseState( int button, int state, int x, int y )
{
  xpixels=x; ypixels=y-height_w1;
  dx= (width_w1/2.-(double)x)/width_w1*-2.;
  dy= (height_w1/2.-(double)y)/height_w1*2.;
  beginx = dx;
  beginy = dy;
  MouseMode = 0;

  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  {
    MouseMode = 3;
    if (zoomFlag) qzoom();
    if (centerFlag) qcenter(x, y);
  }

  if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
  { MouseMode = 2; }

  if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
  { MouseMode = 1; }

  /* update the elem-edges in case the view was zoomed */
  if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP)
  {
    /* printf("GLUT_MIDDLE_BUTTON && state == GLUT_UP\n"); */
    if (elemEdgeFlag)
    {
      if (surfFlag) drawDispListEdges(list_surf_edges, basCol[0], 1., 'f' );
      else          drawDispListEdges(list_elem_edges, basCol[0], 1., 'e' );
    }
  }


  if(( MouseMode==1 )&&( activWindow==w0 )) 
  {
    stopFlag=1;
    MouseMode=4;
    glutSetWindow( w1);
    glutPostRedisplay();
    glutSetWindow( activWindow );
    if((movieFlag>0)&&(stopFlag))
    {
      movieFlag=0;
      printf("movie stopped, please wait for ready (might take a while)\n");
      sprintf( buffer, "rm -f _.xwd >/dev/null");
      system (buffer);
      createHardcopy(0);
      printf("movie.gif ready\n");
    }
    else animList++;
  }
  if(( MouseMode==2 )&&( activWindow==w0 )) 
  {
    stopFlag=!stopFlag;
    if(animList>0) animList--;
  }
}


void menu( int selection )
{
  int  i;

  switch (selection) {
  case 1:
    /* nodes in definierten wertebereich scalieren wg. beleuchtung!  */
    descalNodes( anz->n, node, scale);
    descalPoints( anzGeo->p, point, scale);
    descalSurfs( anzGeo->s, surf, scale);
    getScaleValues( anz->n, anzGeo->p, point, node, scale);
    scalPoints ( anzGeo->p, point, scale );
    scalNodes ( anz->n, node, scale );
    scalSurfs( anzGeo->s, surf, scale);
    dtx=0.; dty=0.; dtz=0.; ds=0.5;
    for (i=0; i<4; i++) vmem[i]=0; /* reset all kompensations (center()) */
    /* recalculate the line-shapes */
    for (i=0; i<anzGeo->l; i++) rep(i);
    for (i=0; i<anzGeo->nurl; i++) repNurl(i);
    for (i=0; i<anzGeo->nurs; i++) repNurs(i);
    redraw(); glutPostRedisplay();
    break;
  case 2:
    zoomFlag=!zoomFlag;
    break;
  case 3:
    centerFlag=!centerFlag;
    break;
  case 4:
    exit(0);
    break;
  }
}


void orientModel( int selection )
{
  switch (selection) {
  case 1:
    /* Trackballfunktion inizialisieren, Blickrichtung +x (rot_x)*/
    rot_x(1);
    break;
  case 2:
    /* Trackballfunktion inizialisieren, Blickrichtung -x (rot_x)*/
    rot_x(-1);
    break;
  case 3:
    /* Trackballfunktion inizialisieren, Blickrichtung +y (rot_y)*/
    rot_y(1);
    break;
  case 4:
    /* Trackballfunktion inizialisieren, Blickrichtung -y (rot_y)*/
    rot_y(1);
    rot_r(90.);
    rot_r(90.);
    break;
  case 5:
    /* Trackballfunktion inizialisieren, Blickrichtung +z (rot_z)*/
    rot_z(1);
    break;
  case 6:
    /* Trackballfunktion inizialisieren, Blickrichtung -z (rot_z)*/
    rot_z(-1);
    break;
  }
}


void markHardcopy( int selection )
{

  if(selection==3)
  {
    movieFlag=1;
    printf(" start recording movie\n");
    printf("   stop recording with right mouse key while in menu area of the window\n");
    sprintf( buffer, "rm -f _*.gif >/dev/null ");
    system (buffer);
    sprintf( buffer, "rm -f movie.gif >/dev/null ");
    system (buffer);
    stopFlag=0;
  }
  else
  {
    hcpyFlag=selection;
    printf("Please wait\n");
    redraw();
    glutPostRedisplay();
  }
}


void showHelp( int selection )
{
  unsigned char buffer[MAX_LINE_LENGTH];

  switch (selection) {
  case 1:
    help();
    break;
  case 2:
    sprintf( buffer, "%s %s& ", browser, helpfile[0]);
    printf("starting help: %s %s\n", browser, helpfile[0]);
    system (buffer);
    break;
  case 3:
    sprintf( buffer, "%s %s& ", browser, helpfile[1]);
    printf("starting help: %s %s\n", browser, helpfile[1]);
    system (buffer);
    break;
  }
}


void updateDispLists()
{
#if TEST
  printf ("in updateDispLists drawMode=%d \n",drawMode );
#endif 
  if(activWindow==-1) return;

  glutSetWindow( w1);
  /* update the element and face normals */

  if (drawMode==1)
  {
    if (surfFlag)
    {
      if (lcase[lc].irtype == 3) printf("ERROR: Element-results are not supported so far!\n");
      else drawDispList( list_surf_load, 'f', node );
    }
    else        
    {
      if (lcase[lc].irtype == 3) printf("ERROR: Element-results are not supported so far!\n");
      else drawDispList( list_elem_load, 'e', node );
    }
  }
  if (drawMode==2)
  {
    if (surfFlag)   drawDispList( list_surf_light, 'f', node );
    if (!surfFlag)  drawDispList( list_elem_light, 'e', node );
  }

  if (modelEdgeFlag) drawModelEdges( list_model_edges, basCol[0], 2., anz->g, node, edge );
  if (surfFlag) drawDispListEdges(list_surf_edges, basCol[0], 1., 'f' );
  else          drawDispListEdges(list_elem_edges, basCol[0], 1., 'e' );
#if TEST
  printf (" end updateDispLists drawMode=%d \n",drawMode );
#endif 
}


void ConfigureAndShowWindow_Light()
{
  int j;

  if(sequenceFlag)
  {
    ConfigureAndShowWindow_Sequence();
    return;
  }

  glutSetWindow( w1 );
#if INX_MODE
  defineColIndexes_light();
#endif 

  if (drawMode==2)
  {
    if(surfFlag)  plot("fi all");
    else          plot("ei all");
    drawMode=2;
    glutSetWindow( w1 );
    glutDisplayFunc ( DrawGraficLight );
    DrawGraficLight();
  }
  else if (drawMode==3)
  {
    /* change the color to "illuminated" */
    for (j=0; j<anzGeo->psets; j++ )
    {
      if (pset[j].type[0]=='f') pset[j].col=-1;
      if (pset[j].type[0]=='e') pset[j].col=-1;
      pset[j].type[1]='i';
    }
    drawMode=3;
    lc=pre_lc;
    calcAnimation( list_animate_light, list_animate, anim_steps, anim_faktor, anim_alfa, halfperiod, centerNode, anz, n_anim, node, e_enqire, lcase, lc, scale, surfFlag );
    glutDisplayFunc ( DrawGraficAnimate );
    DrawGraficAnimate();
    DrawGraficAnimate();
  }
  updateDispLists();
  activWindow = w1;
  glShadeModel ( GL_FLAT );
  glutPostRedisplay();
  glutSetWindow( w0);
  if (drawMode==2) { glutDisplayFunc ( DrawMenuLight ); DrawMenuLight();  }
  if (drawMode==3) { glutDisplayFunc ( DrawMenuAnimate ); DrawMenuAnimate();  }
  glutSetWindow( w2);
  DrawAxes();
}


void ConfigureAndShowWindow_Plot( void )
{
  int i;

  for(i=0; i<anzGeo->psets; i++)
  {
   if (pset[i].type[1]=='v')
   {
    glutSetWindow( w0);
    glutDisplayFunc ( DrawMenuLoad );
    DrawMenuLoad();
    glutSetWindow( w1 );
    glShadeModel ( GL_SMOOTH );

#if INX_MODE
    defineColIndexes_load();
    nodalDataset( entity_Nr, lc, anz, scale, node, lcase, value_n, colNr, offset, maxIndex, steps );
#endif 
#if TEX_MODE
    defineColTextur_load();
    nodalDataset( entity_Nr, lc, anz, scale, node, lcase, value_n, colNr );
#endif
    break;
   }
  }
  if (i==anzGeo->psets)
  {
    glutSetWindow( w0);
    glutDisplayFunc ( DrawMenuSet );
    DrawMenuSet();
    glutSetWindow( w2);
    DrawAxes();
  }

  glutSetWindow( w1);
  glutDisplayFunc ( DrawPickedItems );
  DrawPickedItems();

}


void ConfigureAndShowWindow_Load( void )
{
  if(sequenceFlag)
  {
    ConfigureAndShowWindow_Sequence();
    return;
  }

  glutSetWindow( w1 );
  glShadeModel ( GL_SMOOTH );

#if INX_MODE
  defineColIndexes_load();
#endif
#if TEX_MODE
  defineColTextur_load();
#endif

  if (lcase[lc].irtype == 3) /* element data */
  {
    printf ("\nDataset:%d name= %s", lc+1, lcase[lc].name);
    printf (" entity:%s\n", lcase[lc].compName[entity_Nr-1] );
    elementDataset( entity_Nr, lc, anz, scale, lcase, offset, maxIndex, steps );
    printf (" maxvalue:%e\n minvalue:%e \n", scale->vmax, scale->vmin);
  }
  else
  {
    printf ("\nDataset:%d name= %s", lc+1, lcase[lc].name);
    printf (" entity:%s\n", lcase[lc].compName[entity_Nr-1] );
#if INX_MODE
    nodalDataset( entity_Nr, lc, anz, scale, node, lcase, value_n
    , colNr, offset, maxIndex, steps );
#endif 
#if TEX_MODE
    nodalDataset( entity_Nr, lc, anz, scale, node, lcase, value_n
    , colNr );
#endif
    printf (" maxvalue:%e at node:%d\n minvalue:%e at node:%d\n", scale->vmax, scale->nmax, scale->vmin, scale->nmin);
  }
  drawMode=1;
  glutSetWindow( w1);
  updateDispLists();
  glutDisplayFunc ( DrawGraficLoad );
  DrawGraficLoad();
  glutSetWindow( w0);
  glutDisplayFunc ( DrawMenuLoad );
  DrawMenuLoad();
  glutSetWindow( w2);
  DrawAxes();
}


void ConfigureAndShowWindow_Sequence( void )
{
  int i,j,l=0;
  double max=MINVALUE, min=MAXVALUE;

  /* store the loadcases for which vectors should be displayed */
  j=0; for(i=seqLC[0]; i<seqLC[2]+1; i+=seqLC[1]-seqLC[0]) v_LC[j++]=i;

  lc=seqLC[0];
  drawModelEdges( list_model_edges, basCol[0], 2., anz->g, node, edge );
  if (surfFlag) drawDispListEdges(list_surf_edges, basCol[0], 1., 'f' );
  else          drawDispListEdges(list_elem_edges, basCol[0], 1., 'e' );

  if((compare(lcase[seqLC[0]].name, "DISP", 4)==4)&&(entity_Nr==4)&&(lcase[lc].ictype[0]!= 12)) /* light */
  {
    /* change the element and face mode to 'value' */
    for(i=0; i<anzGeo->psets; i++)
      if((pset[i].type[0]=='e')||(pset[i].type[0]=='f'))
      { pset[i].type[1]=0; pset[i].col=-1; }
    drawMode=2;
    lightFlag=1;
    glutSetWindow( w1 );
#if INX_MODE
    defineColIndexes_light();
#endif 

    for(i=seqLC[0]; i<seqLC[2]+1; i+=seqLC[1]-seqLC[0])
    {
      if(lcase_animList<0)
      {
        /* create an additional lcase after for the vector length of all steps */
        strcpy(lcase[anz->l].name,"seqLC");
        lcase[anz->l].ncomps = (seqLC[2]-seqLC[0])/(seqLC[1]-seqLC[0])+1;
        lcase[anz->l].irtype = 1;
    
        if ( (lcase[anz->l].nmax = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].nmin = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].max = (float *)malloc( lcase[anz->l].ncomps * sizeof(float))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].min = (float *)malloc( lcase[anz->l].ncomps * sizeof(float))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].dat = (float **)malloc( lcase[anz->l].ncomps * sizeof(float *))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].compName = (unsigned char **)malloc( lcase[anz->l].ncomps * sizeof(unsigned char *))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].icname = (unsigned char **)malloc( lcase[anz->l].ncomps * sizeof(unsigned char *))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        for(j=0; j<lcase[anz->l].ncomps; j++)
        {
          if ( (lcase[anz->l].dat[j] = (float *)malloc( (anz->nmax+1) * sizeof(float))) == NULL )
            printf("\n\n ERROR: malloc failure\n\n" );	               
          if ( (lcase[anz->l].compName[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
             printf("\n\n ERROR: malloc failed\n\n" );
          if ( (lcase[anz->l].icname[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
             printf("\n\n ERROR: malloc failed\n\n" );
          lcase[anz->l].max[j]=-MAX_FLOAT;
          lcase[anz->l].min[j]=MAX_FLOAT;
        }
        if ( (lcase[anz->l].ictype = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].icind1 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].icind2 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].iexist = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
    
        for(j=0; j<lcase[anz->l].ncomps; j++)
        {
          lcase[anz->l].ictype[j] = 2;
          lcase[anz->l].icind1[j] = j+1;
          lcase[anz->l].icind2[j] = 0;
          lcase[anz->l].iexist[j] = 0;
          sprintf(lcase[anz->l].compName[j], "step%d",j);
        }
        lcase_animList=anz->l;
      }
      else
      {
        /* realloc the additional lcase after for the vector length of all steps */
        for(j=0; j<lcase[lcase_animList].ncomps; j++)
        {
          free(lcase[lcase_animList].dat[j]);
          free(lcase[lcase_animList].compName[j]);
          free(lcase[lcase_animList].icname[j]);
        }
  
        strcpy(lcase[lcase_animList].name,"seqLC");
        lcase[lcase_animList].ncomps = (seqLC[2]-seqLC[0])/(seqLC[1]-seqLC[0])+1;
        lcase[lcase_animList].irtype = 1;
    
        if ( (lcase[lcase_animList].nmax = (int *)realloc( lcase[lcase_animList].nmax, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].nmin = (int *)realloc( lcase[lcase_animList].nmin, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].max = (float *)realloc( lcase[lcase_animList].max, lcase[lcase_animList].ncomps * sizeof(float))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].min = (float *)realloc( lcase[lcase_animList].min, lcase[lcase_animList].ncomps * sizeof(float))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].dat = (float **)realloc( lcase[lcase_animList].dat, lcase[lcase_animList].ncomps * sizeof(float *))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].compName = (unsigned char **)realloc( lcase[lcase_animList].compName, lcase[lcase_animList].ncomps * sizeof(unsigned char *))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].icname = (unsigned char **)realloc( lcase[lcase_animList].icname, lcase[lcase_animList].ncomps * sizeof(unsigned char *))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        for(j=0; j<lcase[lcase_animList].ncomps; j++)
        {
          if ( (lcase[lcase_animList].dat[j] = (float *)malloc( (anz->nmax+1) * sizeof(float))) == NULL )
            printf("\n\n ERROR: malloc failure\n\n" );	               
          if ( (lcase[lcase_animList].compName[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
             printf("\n\n ERROR: malloc failed\n\n" );
          if ( (lcase[lcase_animList].icname[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
             printf("\n\n ERROR: malloc failed\n\n" );
          lcase[lcase_animList].max[j]=-MAX_FLOAT;
          lcase[lcase_animList].min[j]=MAX_FLOAT;
        }
        if ( (lcase[lcase_animList].ictype = (int *)realloc( lcase[lcase_animList].ictype, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].icind1 = (int *)realloc( lcase[lcase_animList].icind1, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].icind2 = (int *)realloc( lcase[lcase_animList].icind2, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].iexist = (int *)realloc( lcase[lcase_animList].iexist, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
    
        for(j=0; j<lcase[lcase_animList].ncomps; j++)
        {
          lcase[lcase_animList].ictype[j] = 2;
          lcase[lcase_animList].icind1[j] = j+1;
          lcase[lcase_animList].icind2[j] = 0;
          lcase[lcase_animList].iexist[j] = 0;
          sprintf(lcase[lcase_animList].compName[j], "step%d",j);
        }
      }

      l++;
    }
    calcSequence( list_animate_light, list_animate, seqLC, seq_steps, anim_faktor, halfperiod, centerNode, anz, n_anim, node, e_enqire, lcase, lc, scale );

    glutDisplayFunc ( DrawGraficSequence );
    DrawGraficSequence();
    glShadeModel ( GL_FLAT );
    glutSetWindow( w0);
    glutDisplayFunc ( DrawMenuAnimate );
    DrawMenuAnimate();
    glutSetWindow( w2);
    DrawAxes();

    activWindow = w1;
  }
  else /* load */
  {
    drawMode=1;
    lightFlag=0;
    glutSetWindow( w1 );
    glShadeModel ( GL_SMOOTH );
#if INX_MODE
    defineColIndexes_load();
#endif 
#if TEX_MODE
    defineColTextur_load();
#endif

    printf(" selected Datasets:\n");
    for(i=seqLC[0]; i<seqLC[2]+1; i+=seqLC[1]-seqLC[0])
    {
      if(lcase_animList<0)
      {
        /* create an additional lcase after for the vector length of all steps */
        strcpy(lcase[anz->l].name,"seqLC");
        lcase[anz->l].ncomps = (seqLC[2]-seqLC[0])/(seqLC[1]-seqLC[0])+1;
        lcase[anz->l].irtype = 1;
    
        if ( (lcase[anz->l].nmax = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].nmin = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].max = (float *)malloc( lcase[anz->l].ncomps * sizeof(float))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].min = (float *)malloc( lcase[anz->l].ncomps * sizeof(float))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].dat = (float **)malloc( lcase[anz->l].ncomps * sizeof(float *))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].compName = (unsigned char **)malloc( lcase[anz->l].ncomps * sizeof(unsigned char *))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].icname = (unsigned char **)malloc( lcase[anz->l].ncomps * sizeof(unsigned char *))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        for(j=0; j<lcase[anz->l].ncomps; j++)
        {
          if ( (lcase[anz->l].dat[j] = (float *)malloc( (anz->nmax+1) * sizeof(float))) == NULL )
            printf("\n\n ERROR: malloc failure\n\n" );	               
          if ( (lcase[anz->l].compName[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
             printf("\n\n ERROR: malloc failed\n\n" );
          if ( (lcase[anz->l].icname[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
             printf("\n\n ERROR: malloc failed\n\n" );
          lcase[anz->l].max[j]=-MAX_FLOAT;
          lcase[anz->l].min[j]=MAX_FLOAT;
        }
        if ( (lcase[anz->l].ictype = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].icind1 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].icind2 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
        if ( (lcase[anz->l].iexist = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: malloc failure\n\n" );
    
        for(j=0; j<lcase[anz->l].ncomps; j++)
        {
          lcase[anz->l].ictype[j] = 2;
          lcase[anz->l].icind1[j] = j+1;
          lcase[anz->l].icind2[j] = 0;
          lcase[anz->l].iexist[j] = 0;
          sprintf(lcase[anz->l].compName[j], "step%d",j);
        }
        lcase_animList=anz->l;
      }
      else
      {
        /* realloc the additional lcase after for the vector length of all steps */
        for(j=0; j<lcase[lcase_animList].ncomps; j++)
        {
          free(lcase[lcase_animList].dat[j]);
          free(lcase[lcase_animList].compName[j]);
          free(lcase[lcase_animList].icname[j]);
        }
  
        strcpy(lcase[lcase_animList].name,"seqLC");
        lcase[lcase_animList].ncomps = (seqLC[2]-seqLC[0])/(seqLC[1]-seqLC[0])+1;
        lcase[lcase_animList].irtype = 1;
    
        if ( (lcase[lcase_animList].nmax = (int *)realloc( lcase[lcase_animList].nmax, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].nmin = (int *)realloc( lcase[lcase_animList].nmin, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].max = (float *)realloc( lcase[lcase_animList].max, lcase[lcase_animList].ncomps * sizeof(float))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].min = (float *)realloc( lcase[lcase_animList].min, lcase[lcase_animList].ncomps * sizeof(float))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].dat = (float **)realloc( lcase[lcase_animList].dat, lcase[lcase_animList].ncomps * sizeof(float *))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].compName = (unsigned char **)realloc( lcase[lcase_animList].compName, lcase[lcase_animList].ncomps * sizeof(unsigned char *))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].icname = (unsigned char **)realloc( lcase[lcase_animList].icname, lcase[lcase_animList].ncomps * sizeof(unsigned char *))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        for(j=0; j<lcase[lcase_animList].ncomps; j++)
        {
          if ( (lcase[lcase_animList].dat[j] = (float *)malloc( (anz->nmax+1) * sizeof(float))) == NULL )
            printf("\n\n ERROR: malloc failure\n\n" );	               
          if ( (lcase[lcase_animList].compName[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
             printf("\n\n ERROR: malloc failed\n\n" );
          if ( (lcase[lcase_animList].icname[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
             printf("\n\n ERROR: malloc failed\n\n" );
          lcase[lcase_animList].max[j]=-MAX_FLOAT;
          lcase[lcase_animList].min[j]=MAX_FLOAT;
        }
        if ( (lcase[lcase_animList].ictype = (int *)realloc( lcase[lcase_animList].ictype, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].icind1 = (int *)realloc( lcase[lcase_animList].icind1, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].icind2 = (int *)realloc( lcase[lcase_animList].icind2, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
        if ( (lcase[lcase_animList].iexist = (int *)realloc( lcase[lcase_animList].iexist, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
          printf("\n\n ERROR: realloc failure\n\n" );
    
        for(j=0; j<lcase[lcase_animList].ncomps; j++)
        {
          lcase[lcase_animList].ictype[j] = 2;
          lcase[lcase_animList].icind1[j] = j+1;
          lcase[lcase_animList].icind2[j] = 0;
          lcase[lcase_animList].iexist[j] = 0;
          sprintf(lcase[lcase_animList].compName[j], "step%d",j);
        }
      }
      if (lcase[i].irtype == 3)
      {
        printf ("\nDataset:%d name= %s", i+1, lcase[i].name);
        printf (" entity:%s\n", lcase[i].compName[entity_Nr-1] );
        elementDataset( entity_Nr, i, anz, scale, lcase, offset, maxIndex, steps );
        printf (" maxvalue:%e\n minvalue:%e \n", scale->vmax, scale->vmin);
      }
      else
      { 
        printf ("\nDataset:%d name= %s", i+1, lcase[i].name);
        printf (" entity:%s\n", lcase[i].compName[entity_Nr-1] );
#if INX_MODE
        nodalDataset( entity_Nr, i, anz, scale, node, lcase, value_n
        , colNr, offset, maxIndex, steps );
#endif 
#if TEX_MODE
        nodalDataset( entity_Nr, i, anz, scale, node, lcase, value_n
        , colNr );
#endif 
        printf (" maxvalue:%e at node:%d\n minvalue:%e at node:%d\n", scale->vmax, scale->nmax, scale->vmin, scale->nmin);
        for(j=1; j<=anz->nmax; j++) lcase[lcase_animList].dat[l][j]=value_n[j];
      }
      list_animate[l]=list_animate_light+l;
      if( max <scale->vmax ) max=scale->vmax;
      if( min >scale->vmin ) min=scale->vmin;
      if (surfFlag)   drawDispList( list_animate[l], 'f', node );
      if (!surfFlag)  drawDispList( list_animate[l], 'e', node );

      l++;
    }
    glutDisplayFunc ( DrawGraficSequence );
    DrawGraficSequence();
    glutSetWindow( w0);
    glutDisplayFunc ( DrawMenuLoad );
    DrawMenuLoad();
    glutSetWindow( w2);
    DrawAxes();
  }
  for(l=0; l<lcase[lcase_animList].ncomps; l++)
  {
      lcase[lcase_animList].dat[l][0]=lcase[v_LC[l]].value;
      lcase[lcase_animList].value=lcase[v_LC[l]].value;
      strcpy(lcase[lcase_animList].dataset_text,lcase[v_LC[l]].dataset_text);
  }
  scale->vmax=max;
  scale->vmin=min;
  if(scale->bmin ==scale->bmax )
  {
    scale->bmax=max;
    scale->bmin=min;
  }
}


void ConfigureAndShowWindow_Vector( void )
{
  int i,j,k,l=0;
  int lc_max=0, lc_min=0;
  double max, min;

  drawModelEdges( list_model_edges, basCol[0], 2., anz->g, node, edge );
  if (surfFlag) drawDispListEdges(list_surf_edges, basCol[0], 1., 'f' );
  else          drawDispListEdges(list_elem_edges, basCol[0], 1., 'e' );

  lightFlag=0;
  glutSetWindow( w1 );
  glShadeModel ( GL_SMOOTH );
#if INX_MODE
  defineColIndexes_load();
#endif 
#if TEX_MODE
  defineColTextur_load();
#endif

  /* store the loadcases for which vectors should be displayed */
  j=0; for(i=seqLC[0]; i<seqLC[2]+1; i+=seqLC[1]-seqLC[0]) v_LC[j++]=i;

  /* determine the maximum and minimum length of the vectors based on all lc of the sequence */
  scale->vmax=-MAXVALUE;
  scale->vmin=-MAXVALUE;
  for(i=seqLC[0]; i<seqLC[2]+1; i+=seqLC[1]-seqLC[0])
  {
    max=min=0.;
    for(j=0; j<v_dim; j++)
    {
      max+=lcase[i].max[entity_v[j]]*lcase[i].max[entity_v[j]];    
      min+=lcase[i].min[entity_v[j]]*lcase[i].min[entity_v[j]];
    }    
    max=sqrt(max);
    min=sqrt(min);

    if(max>scale->vmax) { scale->vmax=max; lc_max=i; }
    if(min>scale->vmin) { scale->vmin=min; lc_min=i; }
  }
  scale->vmax=0.;
  scale->vmin=0.;
  for(j=0; j<v_dim; j++)
  {
    scale->vmax+=lcase[lc_max].max[entity_v[j]]*lcase[lc_max].max[entity_v[j]];    
    scale->vmin+=lcase[lc_min].min[entity_v[j]]*lcase[lc_min].min[entity_v[j]];
  }
  scale->vmax=sqrt(scale->vmax);
  scale->vmin=sqrt(scale->vmin);
    
  if(scale->vmax*scale->vmax>scale->vmin*scale->vmin) v_factor=1./scale->vmax;
  else v_factor=1./scale->vmin;

  if(lcase_animList<0)
  {
    /* create an additional lcase for the vector length of all steps */
    if ( (lcase = (Datasets *)realloc((Datasets *)lcase, (anz->l+1) * sizeof(Datasets))) == NULL )
      printf("\n\n ERROR: realloc failed, lcase\n\n") ;

    strcpy(lcase[anz->l].name,"seqLC");
    lcase[anz->l].ncomps = (seqLC[2]-seqLC[0])/(seqLC[1]-seqLC[0])+1;
    lcase[anz->l].irtype = 1;

    if ( (lcase[anz->l].nmax = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase[anz->l].nmin = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase[anz->l].max = (float *)malloc( lcase[anz->l].ncomps * sizeof(float))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase[anz->l].min = (float *)malloc( lcase[anz->l].ncomps * sizeof(float))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase[anz->l].dat = (float **)malloc( lcase[anz->l].ncomps * sizeof(float *))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase[anz->l].compName = (unsigned char **)malloc( lcase[anz->l].ncomps * sizeof(unsigned char *))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase[anz->l].icname = (unsigned char **)malloc( lcase[anz->l].ncomps * sizeof(unsigned char *))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    for(j=0; j<lcase[anz->l].ncomps; j++)
    {
      if ( (lcase[anz->l].dat[j] = (float *)malloc( (anz->nmax+1) * sizeof(float))) == NULL )
        printf("\n\n ERROR: malloc failure\n\n" );	               
      if ( (lcase[anz->l].compName[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
         printf("\n\n ERROR: malloc failed\n\n" );
      if ( (lcase[anz->l].icname[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
         printf("\n\n ERROR: malloc failed\n\n" );
      lcase[anz->l].max[j]=-MAX_FLOAT;
      lcase[anz->l].min[j]=MAX_FLOAT;
    }
    if ( (lcase[anz->l].ictype = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase[anz->l].icind1 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase[anz->l].icind2 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase[anz->l].iexist = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );

    for(j=0; j<lcase[anz->l].ncomps; j++)
    {
      lcase[anz->l].ictype[j] = 2;
      lcase[anz->l].icind1[j] = j+1;
      lcase[anz->l].icind2[j] = 0;
      lcase[anz->l].iexist[j] = 0;
      sprintf(lcase[anz->l].compName[j], "vector%d",j);
    }
    lcase_animList=anz->l;
  }
  else
  {
    /* realloc the additional lcase for the vector length of all steps */
    for(j=0; j<lcase[lcase_animList].ncomps; j++)
    {
      free(lcase[lcase_animList].dat[j]);
      free(lcase[lcase_animList].compName[j]);
      free(lcase[lcase_animList].icname[j]);
    }

    strcpy(lcase[lcase_animList].name,"seqLC");
    lcase[lcase_animList].ncomps = (seqLC[2]-seqLC[0])/(seqLC[1]-seqLC[0])+1;
    lcase[lcase_animList].irtype = 1;

    if ( (lcase[lcase_animList].nmax = (int *)realloc( lcase[lcase_animList].nmax, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: realloc failure\n\n" );
    if ( (lcase[lcase_animList].nmin = (int *)realloc( lcase[lcase_animList].nmin, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: realloc failure\n\n" );
    if ( (lcase[lcase_animList].max = (float *)realloc( lcase[lcase_animList].max, lcase[lcase_animList].ncomps * sizeof(float))) == NULL )
      printf("\n\n ERROR: realloc failure\n\n" );
    if ( (lcase[lcase_animList].min = (float *)realloc( lcase[lcase_animList].min, lcase[lcase_animList].ncomps * sizeof(float))) == NULL )
      printf("\n\n ERROR: realloc failure\n\n" );
    if ( (lcase[lcase_animList].dat = (float **)realloc( lcase[lcase_animList].dat, lcase[lcase_animList].ncomps * sizeof(float *))) == NULL )
      printf("\n\n ERROR: realloc failure\n\n" );
    if ( (lcase[lcase_animList].compName = (unsigned char **)realloc( lcase[lcase_animList].compName, lcase[lcase_animList].ncomps * sizeof(unsigned char *))) == NULL )
      printf("\n\n ERROR: realloc failure\n\n" );
    if ( (lcase[lcase_animList].icname = (unsigned char **)realloc( lcase[lcase_animList].icname, lcase[lcase_animList].ncomps * sizeof(unsigned char *))) == NULL )
      printf("\n\n ERROR: realloc failure\n\n" );
    for(j=0; j<lcase[lcase_animList].ncomps; j++)
    {
      if ( (lcase[lcase_animList].dat[j] = (float *)malloc( (anz->nmax+1) * sizeof(float))) == NULL )
        printf("\n\n ERROR: malloc failure\n\n" );	               
      if ( (lcase[lcase_animList].compName[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
         printf("\n\n ERROR: malloc failed\n\n" );
      if ( (lcase[lcase_animList].icname[j] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
         printf("\n\n ERROR: malloc failed\n\n" );
      lcase[lcase_animList].max[j]=-MAX_FLOAT;
      lcase[lcase_animList].min[j]=MAX_FLOAT;
    }
    if ( (lcase[lcase_animList].ictype = (int *)realloc( lcase[lcase_animList].ictype, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: realloc failure\n\n" );
    if ( (lcase[lcase_animList].icind1 = (int *)realloc( lcase[lcase_animList].icind1, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: realloc failure\n\n" );
    if ( (lcase[lcase_animList].icind2 = (int *)realloc( lcase[lcase_animList].icind2, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: realloc failure\n\n" );
    if ( (lcase[lcase_animList].iexist = (int *)realloc( lcase[lcase_animList].iexist, lcase[lcase_animList].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: realloc failure\n\n" );

    for(j=0; j<lcase[lcase_animList].ncomps; j++)
    {
      lcase[lcase_animList].ictype[j] = 2;
      lcase[lcase_animList].icind1[j] = j+1;
      lcase[lcase_animList].icind2[j] = 0;
      lcase[lcase_animList].iexist[j] = 0;
      sprintf(lcase[lcase_animList].compName[j], "vector%d",j);
    }
  }

  /* calculate the length (value of all vectors for picking */
  l=0;
  for(i=seqLC[0]; i<seqLC[2]+1; i+=seqLC[1]-seqLC[0])
  {
    /* select the vector length (color plot) */
    if (lcase[i].irtype == 3) { printf("Element results are not supported\n"); return; }
    else
    {
      for(j=0; j<anz->n; j++)
      {
        lcase[lcase_animList].dat[l][node[j].nr]=0.;
        if(v_dim==4)
        {
          lcase[lcase_animList].dat[l][node[j].nr]=lcase[i].dat[entity_v[3]][node[j].nr];
        }
        else
        {
          for(k=0; k<v_dim; k++)
            lcase[lcase_animList].dat[l][node[j].nr]+=lcase[i].dat[entity_v[k]][node[j].nr]*lcase[i].dat[entity_v[k]][node[j].nr];
          lcase[lcase_animList].dat[l][node[j].nr]=sqrt(lcase[lcase_animList].dat[l][node[j].nr]);
        }

      }
    }
    l++;
  }

  /* max and min for plotting */
  scale->vmax=-MAX_FLOAT;
  scale->vmin=MAX_FLOAT;
  for(k=0; k<lcase[lcase_animList].ncomps; k++)
  {
    for(j=0; j<anz->n; j++)
    {
      if(lcase[lcase_animList].dat[k][node[j].nr] > lcase[lcase_animList].max[k])
      {
        lcase[lcase_animList].max[k]=lcase[lcase_animList].dat[k][node[j].nr];
        lcase[lcase_animList].nmax[k]=node[j].nr;
      }
      if(lcase[lcase_animList].dat[k][node[j].nr] < lcase[lcase_animList].min[k])
      {
        lcase[lcase_animList].min[k]=lcase[lcase_animList].dat[k][node[j].nr];
        lcase[lcase_animList].nmin[k]=node[j].nr;
      }
      if(scale->vmax<lcase[lcase_animList].max[k])
      { scale->vmax=lcase[lcase_animList].max[k]; scale->nmax=lcase[lcase_animList].nmax[k]; }
      if(scale->vmin>lcase[lcase_animList].min[k])
      { scale->vmin=lcase[lcase_animList].min[k]; scale->nmin=lcase[lcase_animList].nmin[k]; }
    }
    printf (" maxval:%e at node:%d minval:%e at node:%d\n", lcase[lcase_animList].max[k], lcase[lcase_animList].nmax[k], lcase[lcase_animList].min[k], lcase[lcase_animList].nmin[k]);
  }

  /* set some general variables for plotting */
  lc=lcase_animList;
  entity_Nr=1;
  if(scale->bmin ==scale->bmax )
  {
    scale->bmin = scale->vmin;
    scale->bmax = scale->vmax;
  }

  /* prepare the color values */
  for(k=0; k<lcase[lcase_animList].ncomps; k++)
  {
#if INX_MODE
    nodalDataset( k+1, lcase_animList, anz, scale, node, lcase, value_n
    , colNr, offset, maxIndex, steps );
#endif 
#if TEX_MODE
    nodalDataset( k+1, lcase_animList, anz, scale, node, lcase, value_n
    , colNr );
#endif 

    lcase[lcase_animList].dat[k][0]=lcase[v_LC[k]].value;
    lcase[lcase_animList].value=lcase[v_LC[k]].value;
    strcpy(lcase[lcase_animList].dataset_text,lcase[v_LC[k]].dataset_text);

    glutSetWindow( w1);
    list_animate[k]=list_animate_light+k;
    if (surfFlag)   drawDispList( list_animate[k], 'f', node );
    if (!surfFlag)  drawDispList( list_animate[k], 'e', node );
  }
  /* ATTENTION: k must be kept for the next display-list */

  /* if the amplitude was specified in addition, reduce the number of components to 3 */
  if(v_dim>3) v_dim=3;

  glutSetWindow( w1 );
  glShadeModel ( GL_SMOOTH );
  defineColTextur_load();
  glutDisplayFunc ( DrawGraficSequence );
  DrawGraficSequence();
  glutSetWindow( w0);
  glutDisplayFunc ( DrawMenuLoad );
  DrawMenuLoad();
  glutSetWindow( w2);
  DrawAxes();
}


int minus( unsigned char *record )
{
  int   setNr;
  int  length, i, flag=0;
  unsigned char typ[MAX_LINE_LENGTH];
  unsigned char setnam[MAX_LINE_LENGTH];

  length = sword( record, typ );
  length = 1+length + sword( &record[length], setnam );
  if   (typ[0]=='m') typ[0]='e';

  operateAlias( setnam, "se" );
  setNr=getSetNr(setnam);
  if (setNr<0)
  {
    printf (" minus: set:%s does not exist\n", setnam);
    return (-1);
  }

  /* look for the pset to remove */
  for(i=0; i<anzGeo->psets; i++)
  {
    if(pset[i].nr==setNr)
    {
      if(pset[i].type[0]==typ[0])
      {
        printf (" set:%s type:%s removed\n", setnam, pset[i].type);
        strcpy( pset[i].type, " ");
      }
    }
    updateDispLists(); 
    flag=1;
  }
  if(flag==0) printf (" set:%s is not on the screen\n", setnam);

  return(1);
}


int plot( unsigned char *record )
{
  int  setNr;
  int  length;
  unsigned char typ[MAX_LINE_LENGTH];
  unsigned char setnam[MAX_LINE_LENGTH];
  unsigned char col[MAX_LINE_LENGTH];

  /* store the commands if no window is present */
  if(w1==w0)
  {
    strcpy(plotstr, record);
    return(-1);
  }

  col[0]=0;
  length = sword( record, typ );
  length = 1+length + sword( &record[length], setnam );
  length = 1+length + sword( &record[length], col );
  if (typ[0]=='m') typ[0]='e';
  if (typ[1]=='i') col[0]='i';

  operateAlias( setnam, "se" );
  setNr=getSetNr(setnam);
  if (setNr<0)
  {
    printf (" plot: set:%s does not exist\n", setnam);
    return (-1);
  }

  if ((pset = (Psets *)realloc( (Psets *)pset, (1)*sizeof(Psets)) ) == NULL )
  {
    printf(" ERROR: realloc failure in plot, pset not installed\n\n");
    return(-1);
  }
  anzGeo->psets=1;

  if      (col[0]=='w') pset[0].col=1;
  else if (col[0]=='k') pset[0].col=0;
  else if (col[0]=='r') pset[0].col=2;
  else if (col[0]=='g') pset[0].col=3;
  else if (col[0]=='b') pset[0].col=4;
  else if (col[0]=='y') pset[0].col=5;
  else if (col[0]=='m') pset[0].col=6;
  else if (col[0]=='i') pset[0].col=-1;
  else if   (typ[0]=='n') pset[0].col=2;
  else if   (typ[0]=='e') pset[0].col=3;
  else if   (typ[0]=='f') pset[0].col=5;
  else if   (typ[0]=='p') pset[0].col=4;
  else if   (typ[0]=='l') pset[0].col=foregrndcol;
  else if   (typ[0]=='s') pset[0].col=6;
  else if   (typ[0]=='b') pset[0].col=3;
  else if   (typ[0]=='L') pset[0].col=foregrndcol;
  else                    pset[0].col=5;
  pset[0].nr= setNr;
  strcpy( pset[0].type, typ);

  drawMode=4;
  if((typ[0]=='e')||(typ[0]=='f'))
  {
    if(typ[0]=='e') surfFlag=0;
#if INX_MODE
    defineColIndexes_light();
#endif 
    updateDispLists();
  }

  glutSetWindow( w1);
  glutDisplayFunc ( DrawPickedItems );
  DrawPickedItems();
  DrawPickedItems();
  if (typ[1]=='v')
  {
    glutSetWindow( w0);
    glutDisplayFunc ( DrawMenuLoad );
    DrawMenuLoad();
    DrawMenuLoad();
    glutSetWindow( w1 );
    glShadeModel ( GL_SMOOTH );

#if INX_MODE
    defineColIndexes_load();
    nodalDataset( entity_Nr, lc, anz, scale, node, lcase, value_n
    , colNr, offset, maxIndex, steps );
#endif 
#if TEX_MODE
    defineColTextur_load();
    nodalDataset( entity_Nr, lc, anz, scale, node, lcase, value_n
    , colNr );
#endif
  }
  else
  {
    glutSetWindow( w0);
    glutDisplayFunc ( DrawMenuSet );
    DrawMenuSet();
    DrawMenuSet();
  }
  return(1);
}

int plus( unsigned char *record )
{
  int  setNr;
  int  length;
  unsigned char typ[MAX_LINE_LENGTH];
  unsigned char setnam[MAX_LINE_LENGTH];
  unsigned char col[MAX_LINE_LENGTH];

  if(w1==w0)
  {
    if((plusstr=(unsigned char **)realloc((unsigned char **)plusstr, (anz_plusstr+1)*sizeof(unsigned char *)))==NULL)
      printf("\n\n ERROR: realloc failed: plusstr\n\n") ;
    if( (plusstr[anz_plusstr]= (unsigned char *)malloc( MAX_LINE_LENGTH*sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed: plusstr\n\n") ;
    strcpy(plusstr[anz_plusstr++], record);
    return(-1);
  }

  col[0]=0;
  length = sword( record, typ );
  length = 1+length + sword( &record[length], setnam );
  length = 1+length + sword( &record[length], col );
  if (typ[0]=='m') typ[0]='e';
  if (typ[1]=='i') col[0]='i';

  operateAlias( setnam, "se" );
  setNr=getSetNr(setnam);

  if (setNr<0)
  {
    printf (" plot: set:%s does not exist\n", setnam);
    return (-1);
  }
  
  if ((pset = (Psets *)realloc( (Psets *)pset, (anzGeo->psets+1)*sizeof(Psets)) ) == NULL )
  {
    printf(" ERROR: realloc failure in plot, pset not installed\n\n");
    return(-1);
  }

  if      (col[0]=='w') pset[anzGeo->psets].col=1;
  else if (col[0]=='k') pset[anzGeo->psets].col=0;
  else if (col[0]=='r') pset[anzGeo->psets].col=2;
  else if (col[0]=='g') pset[anzGeo->psets].col=3;
  else if (col[0]=='b') pset[anzGeo->psets].col=4;
  else if (col[0]=='y') pset[anzGeo->psets].col=5;
  else if (col[0]=='m') pset[anzGeo->psets].col=6;
  else if (col[0]=='i') pset[anzGeo->psets].col=-1;
  else if   (typ[0]=='n') pset[anzGeo->psets].col=2;
  else if   (typ[0]=='e') pset[anzGeo->psets].col=3;
  else if   (typ[0]=='f') pset[anzGeo->psets].col=5;
  else if   (typ[0]=='p') pset[anzGeo->psets].col=4;
  else if   (typ[0]=='l') pset[anzGeo->psets].col=foregrndcol;
  else if   (typ[0]=='s') pset[anzGeo->psets].col=6;
  else if   (typ[0]=='b') pset[anzGeo->psets].col=3;
  else if   (typ[0]=='L') pset[anzGeo->psets].col=foregrndcol;
  else                    pset[anzGeo->psets].col=5;
  pset[anzGeo->psets].nr= setNr;
  strcpy( pset[anzGeo->psets].type, typ);
  anzGeo->psets++;

  drawMode=4;
  if((typ[0]=='e')||(typ[0]=='f'))
  {
    if(typ[0]=='e') surfFlag=0;
#if INX_MODE
    defineColIndexes_light();
#endif 
    updateDispLists();
  }
  ConfigureAndShowWindow_Plot();
  return(1);
}


void changeAnimation( int selection )
{
  /* more options in separate submenus, not listed here */

  drawMode=3;
  switch (selection) {
  case 1:
      ConfigureAndShowWindow_Light();
    break;
  case 2:
      halfperiod=!halfperiod;
      ConfigureAndShowWindow_Light();
    break;
  case 3:
      seq_nlc=0;
      sequenceFlag=!sequenceFlag;
      if(sequenceFlag) printf(" choose 1st, 2nd and last Dataset and at last the entity\n");
      else printf(" sequence off\n");
    break;
  }
}

void newTimePerPeriod( int selection )
{
  switch (selection) {
  case 1:
      time_per_period=1;
    break;
  case 2:
      time_per_period=1000;
    break;
  case 3:
      time_per_period=1200;
    break;
  case 4:
      time_per_period=1500;
    break;
  case 5:
      time_per_period=2000;
    break;
  case 6:
      time_per_period=5000;
    break;
  }
  redraw();
}

void tuneAnimation( int selection )
{
  switch (selection) {
  case 0:
    anim_faktor=1.;
    break;
  case 1:
    anim_faktor*= 10.;
    break;
  case 2:
    anim_faktor*= 5.;
    break;
  case 3:
    anim_faktor*= 2;
    break;
  case 4:
    anim_faktor/= 2;
    break;
  case 5:
    anim_faktor/= 5.;
    break;
  case 6:
    anim_faktor/= 10.;
    break;
  }
  if(addDispFlag==1)
  {
    /* remove the displacements and use the scaled ones */
    node=addDispToCoordinates(node);
    node=addDispToCoordinates(node);
  }  
  redraw();
}

void stepsAnimation( int selection )
{
/* ------- glDeleteLists funktioniert nicht wenn nicht konsequent pointer uebergeben werden! --- */
/*  glDeleteLists( (GLuint)list_animate_light, (GLsizei)anim_steps );   */
/*  printf ("list_animate_light deleted %d \n",list_animate_light);     */
  anim_steps=selection;
  redraw();
}

void redraw(void)
{
  if (drawMode==1)
  {
    ConfigureAndShowWindow_Load();
  }
  if (drawMode==2)
  {
    ConfigureAndShowWindow_Light();
  }
  if (drawMode==3)
  {
    ConfigureAndShowWindow_Light();
  }
  if (drawMode==4)
  {
    ConfigureAndShowWindow_Plot();
  }
  if (drawMode==5)
  {
    ConfigureAndShowWindow_Vector();
  }
}

void selectEntity( int selection )
{
  int i, e[4];

  /* change the element and face mode to 'value' */
  for(i=0; i<anzGeo->psets; i++)
    if((pset[i].type[0]=='e')||(pset[i].type[0]=='f')) pset[i].type[1]='v';

  entity_Nr=selection;
  lc=pre_lc;
  v_scale=1.;

  if(sequenceFlag)
  {
    if (seq_nlc!=3) 
    {
      printf(" ERROR: only %d Datasets yet selected, select more \a\n",seq_nlc+1); 
      return;
    }
    if((vectorFlag)&&(lcase[lc].ictype[entity_Nr-1]==2))
    {
      /* search all necessary entities */
      if (lcase[seqLC[0]].icind1[entity_Nr-1]==1)      { e[0]=entity_Nr; e[1]=entity_Nr+1; e[2]=entity_Nr+2; e[3]=-1;}         
      else if (lcase[seqLC[0]].icind1[entity_Nr-1]==2) { e[0]=entity_Nr-1; e[1]=entity_Nr; e[2]=entity_Nr+1; e[3]=-1;}         
      else if (lcase[seqLC[0]].icind1[entity_Nr-1]==3) { e[0]=entity_Nr-2; e[1]=entity_Nr-1; e[2]=entity_Nr; e[3]=-1;}         
      else if (lcase[seqLC[0]].icind1[entity_Nr-1]==0) { e[0]=entity_Nr-3; e[1]=entity_Nr-2; e[2]=entity_Nr-1; e[3]=entity_Nr;}
      else
      {
        /* vector-components not located before the vector-value */
        scale->bmin=scale->bmax=0;
        seq_steps=1;
        drawMode=1;
        ConfigureAndShowWindow_Load();
        return;
      }
      sprintf(buffer,"%d %d %d e", seqLC[0]+1, seqLC[1]+1, seqLC[2]+1);
      for(i=0; i<3; i++)
      {
        if((lcase[seqLC[0]].icind1[e[i]-1]==i+1)&&(lcase[seqLC[0]].ncomps>=e[i]))
          sprintf( &buffer[strlen(buffer)]," %d",e[i]);
        else break;
      }
      if(e[3]>-1) sprintf( &buffer[strlen(buffer)]," %d",e[3]);
      selectData(buffer);
      return;
    }
  }
  else
  {
    if((vectorFlag)&&(lcase[lc].ictype[entity_Nr-1]==2))
    {
      /* search all necessary entities */
      if (lcase[lc].icind1[entity_Nr-1]==1)      { e[0]=entity_Nr; e[1]=entity_Nr+1; e[2]=entity_Nr+2; e[3]=-1;}
      else if (lcase[lc].icind1[entity_Nr-1]==2) { e[0]=entity_Nr-1; e[1]=entity_Nr; e[2]=entity_Nr+1; e[3]=-1;}
      else if (lcase[lc].icind1[entity_Nr-1]==3) { e[0]=entity_Nr-2; e[1]=entity_Nr-1; e[2]=entity_Nr; e[3]=-1;}
      else if (lcase[lc].icind1[entity_Nr-1]==0) { e[0]=entity_Nr-3; e[1]=entity_Nr-2; e[2]=entity_Nr-1; e[3]=entity_Nr;}
      else
      {
        /* components not located before the length */
        scale->bmin=scale->bmax=0;
        drawMode=1;
        ConfigureAndShowWindow_Load();
        return;
      }
      sprintf(buffer,"%d e",lc+1 );
      for(i=0; i<3; i++)
      {
        if((lcase[lc].icind1[e[i]-1]==i+1)&&(lcase[lc].ncomps>=e[i]))
          sprintf( &buffer[strlen(buffer)]," %d",e[i]);
        else break;
      }
      if(e[3]>-1) sprintf( &buffer[strlen(buffer)]," %d",e[3]);
      selectData(buffer);
      return;
    }
    else
      scale->bmin=scale->bmax=0;
  }
  drawMode=1;
  ConfigureAndShowWindow_Load();
}



/* Dataset creation */
void createDatasetEntries(void)
{
  int  i,j,k, screen_height, menus, lc_per_menu, lc_per_basmenu;

  glutSetWindow( w0);
  if(anz->l)
  {
    glutDestroyMenu( submenu_load );
    submenu_load = glutCreateMenu( selectDataset );

    /* calculate how much lines of Datasets can be displayed on the screen */
    screen_height=glutGet(GLUT_SCREEN_HEIGHT);
    if (!screen_height)
    { screen_height=800; printf(" screen not known, assume screen_height of 800 pixels\n"); } 
    lc_per_menu=screen_height/GLUT_MENU_POINT_HEIGHT ;
    menus=anz->l/lc_per_menu;

    /* check if the remaining lc's fit in the remaining space of the basmenu */
    if(lc_per_menu-menus<anz->l%lc_per_menu) menus++;
    lc_per_basmenu=anz->l-menus*lc_per_menu;

#if TEST
    printf ("lc_per_menu:%d lc_per_basmenu:%d menus:%d\n",lc_per_menu, lc_per_basmenu, menus );
#endif  

    /* define the additional submenus */
    i=lc_per_basmenu; if(i<0) i=0;
    for (j=0; j<menus; j++ )
    {
      subsubmenu_load[j] = glutCreateMenu( selectDataset );
      for (k=0; k<lc_per_menu; k++ )
      {
        if (i>=anz->l) break;
        sprintf (buffer,"%d %.8s %lf %s", i+1, lcase[i].name, lcase[i].value, lcase[i].dataset_text );
        glutAddMenuEntry( buffer, i);
        i++;
      }
    }
  
    /* define the main-lc-menu */
    glutSetMenu( submenu_load );
    for (i=0; i<lc_per_basmenu; i++ )
    {
        sprintf (buffer,"%d %.8s %lf %s", i+1, lcase[i].name, lcase[i].value, lcase[i].dataset_text );
        glutAddMenuEntry( buffer, i);
    }
    for (i=0; i<menus; i++) 
    {
        glutAddSubMenu( "-MORE- ", subsubmenu_load[i] );
    }
    glutAddSubMenu( "-Entity- ", subsubmenu_entity );
    selectDataset(lc);


    glutDestroyMenu( mainmenu );
    mainmenu = glutCreateMenu( menu );
    if(anz->l>0) glutAddSubMenu  ( "Datasets   ", submenu_load );
    glutAddSubMenu  ( "Viewing     ", submenu_view );
    glutAddSubMenu  ( "Animate     ", submenu_animate );
    glutAddMenuEntry( "Frame       ", 1);
    glutAddMenuEntry( "Zoom        ", 2);
    glutAddMenuEntry( "Center      ", 3);
    glutAddSubMenu  ( "Orientation ", submenu_orientation );
#ifndef WIN32
    glutAddSubMenu  ( "Hardcopy    ", submenu_hardcopy);
#endif
    glutAddSubMenu  ( "Help        ", submenu_help);
    glutAddMenuEntry( " -QUIT-     ", 4);
    glutAttachMenu(GLUT_LEFT_BUTTON);
  }
  else
  {
    glutDestroyMenu( mainmenu );
    mainmenu = glutCreateMenu( menu );
    glutAddSubMenu  ( "Viewing     ", submenu_view );
    glutAddSubMenu  ( "Animate     ", submenu_animate );
    glutAddMenuEntry( "Frame       ", 1);
    glutAddMenuEntry( "Zoom        ", 2);
    glutAddMenuEntry( "Center      ", 3);
    glutAddSubMenu  ( "Orientation ", submenu_orientation );
    glutAddSubMenu  ( "Hardcopy    ", submenu_hardcopy);
    glutAddSubMenu  ( "Help        ", submenu_help);
    glutAddMenuEntry( " -QUIT-     ", 4);
    glutAttachMenu(GLUT_LEFT_BUTTON);
  }
  glutSetWindow( activWindow);
}



void selectDataset( int selection )
{
  int i, entries;

  pre_lc= selection;
  if(sequenceFlag)
  {
    if (seq_nlc>2) {seq_nlc=0; seq_steps=1;}
    seqLC[seq_nlc]=pre_lc;
    if ((seq_nlc>0)&&(lcase[pre_lc].ncomps!=lcase[seqLC[0]].ncomps))
    {
      printf(" ERROR: selected Dataset is of different type than the 1st selected, please reselect\a\n");
      return;
    }
    if ((seq_nlc>0)&&((seqLC[1]-seqLC[0])==0))
    {
      printf(" ERROR: selected Dataset is equal to the first selected, please reselect\a\n");
      return;
    }
    if (seq_nlc>1)
    {
      seq_steps=1+(seqLC[2]-seqLC[0])/(seqLC[1]-seqLC[0]);
      printf(" selection finished, select entity\n");
    }
    seq_nlc++;
  }
  else { seq_nlc=0; seq_steps=1; }/* no sequence until a new entity is selected and all data are prepared */

  if(seq_nlc>1)     return;
  glutSetWindow( w0);
  glutSetMenu( submenu_load );
  glutSetMenu( subsubmenu_entity );
  entries=glutGet(GLUT_MENU_NUM_ITEMS);
  for (i=entries; i>0; i--) glutRemoveMenuItem(i);
  for (i=1; i<=lcase[pre_lc].ncomps; i++)
  {
    sprintf (buffer,"%d %s", i, lcase[pre_lc].compName[i-1] );
    glutAddMenuEntry( buffer, i);
  }
}


Nodes *addDispToCoordinates( Nodes *nodes)
{
  int i,n,n1,n2,lc;
  static Nodes *n_anim=NULL, *orig_nodes;

  double amplitude;

  lc=pre_lc;

  if(!addDispFlag)
  {
    if (lcase[lc].ictype[0]== 2)
    {
      addDispFlag=!addDispFlag;

      /* add displacements to the node-coords and return a pointer to the changed nodes */
      orig_nodes=nodes;

      /* create new nodes */
      if ( (n_anim = (Nodes *)realloc((Nodes *)n_anim, (anz->nmax+1) * sizeof(Nodes))) == NULL )
        printf("\n\n ERROR: malloc failed n_anim\n");
      else
        if(printFlag) printf (" n_anim allocated \n");

      amplitude= anim_faktor/scale->w;
      for ( i=0; i<anz->n; i++ )
      {
	n_anim[i].nr = node[i].nr;
        if(!node[node[i].nr].pflag)
        {
          n_anim[node[i].nr].nx = node[node[i].nr].nx + lcase[lc].dat[0][node[i].nr] * amplitude;
          n_anim[node[i].nr].ny = node[node[i].nr].ny + lcase[lc].dat[1][node[i].nr] * amplitude;
          n_anim[node[i].nr].nz = node[node[i].nr].nz + lcase[lc].dat[2][node[i].nr] * amplitude;
	}
      }

    for ( i=0; i<anz->e; i++ )
    {
      if(e_enqire[e_enqire[i].nr].type==4)
      {
        for (n=0; n<3; n++)  /* create new nodes in center of areas */
        {
        n_anim[e_enqire[e_enqire[i].nr].nod[20+n]].nx = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 0+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[1+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 1+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[5+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 5+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[4+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 4+n]] * amplitude  )  +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[8+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 8+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[13+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[13+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[16+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[16+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[12+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[12+n]] * amplitude    ) ;

        n_anim[e_enqire[e_enqire[i].nr].nod[20+n]].ny = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 0+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[1+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 1+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[5+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 5+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[4+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 4+n]] * amplitude  )  +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[8+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 8+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[13+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[13+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[16+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[16+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[12+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[12+n]] * amplitude    ) ;

        n_anim[e_enqire[e_enqire[i].nr].nod[20+n]].nz = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 0+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[1+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 1+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[5+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 5+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[4+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 4+n]] * amplitude )  +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[8+n]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 8+n]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[13+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[13+n]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[16+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[16+n]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[12+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[12+n]] * amplitude   ) ;
        }

        /* create  new node in center of area4 */
        n_anim[e_enqire[e_enqire[i].nr].nod[23  ]].nx = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[3]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[0]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[4]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 4]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[7]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 7]] * amplitude ) +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[11]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[11]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[12]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[12]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[19]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[19]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[15]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[15]] * amplitude   )  ;

        n_anim[e_enqire[e_enqire[i].nr].nod[23  ]].ny = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[3]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[0]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[4]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 4]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[7]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 7]] * amplitude)  +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[11]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[11]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[12]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[12]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[19]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[19]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[15]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[15]] * amplitude   ) ;

        n_anim[e_enqire[e_enqire[i].nr].nod[23  ]].nz = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[3]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[0]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[4]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 4]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[7]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 7]] * amplitude)  +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[11]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[11]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[12]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[12]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[19]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[19]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[15]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[15]] * amplitude   ) ;
        for (n=0; n<2; n++)  /* create last 2 new nodes in center of areas */
        {
          n1=n*4;
          n2=n*8;
        n_anim[e_enqire[e_enqire[i].nr].nod[24+n]].nx = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0+n1]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 0+n1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[1+n1]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 1+n1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[2+n1]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 2+n1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[3+n1]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 3+n1]] * amplitude)  +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[8+n2]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 8+n2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[9+n2]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 9+n2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[10+n2]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[10+n2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[11+n2]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[11+n2]] * amplitude   ) ;

        n_anim[e_enqire[e_enqire[i].nr].nod[24+n]].ny = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0+n1]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 0+n1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[1+n1]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 1+n1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[2+n1]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 2+n1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[3+n1]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 3+n1]] * amplitude)  +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[8+n2]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 8+n2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[9+n2]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 9+n2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[10+n2]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[10+n2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[11+n2]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[11+n2]] * amplitude   ) ;

        n_anim[e_enqire[e_enqire[i].nr].nod[24+n]].nz = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0+n1]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 0+n1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[1+n1]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 1+n1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[2+n1]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 2+n1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[3+n1]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 3+n1]] * amplitude)  +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[8+n2]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 8+n2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[9+n2]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 9+n2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[10+n2]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[10+n2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[11+n2]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[11+n2]] * amplitude   ) ;
        }
      }
      if(e_enqire[e_enqire[i].nr].type==5)
      {
        for (n=0; n<2; n++)  /* create new nodes in center of areas */
        {
        n_anim[e_enqire[e_enqire[i].nr].nod[15+n]].nx = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 0+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[1+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 1+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[4+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 4+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[3+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 3+n]] * amplitude  )  +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[6+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 6+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[10+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[10+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[12+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[12+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[ 9+n]].nx+ lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 9+n]] * amplitude    ) ;

        n_anim[e_enqire[e_enqire[i].nr].nod[15+n]].ny = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 0+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[1+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 1+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[4+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 4+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[3+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 3+n]] * amplitude  )  +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[6+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 6+n]] * amplitude     +
          node[e_enqire[e_enqire[i].nr].nod[10+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[10+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[12+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[12+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[ 9+n]].ny+ lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 9+n]] * amplitude    ) ;

        n_anim[e_enqire[e_enqire[i].nr].nod[15+n]].nz = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 0+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[1+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 1+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[4+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 4+n]] * amplitude    +
          node[e_enqire[e_enqire[i].nr].nod[3+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 3+n]] * amplitude )  +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[6+n]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 6+n]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[10+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[10+n]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[12+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[12+n]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 9+n]].nz+ lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 9+n]] * amplitude   ) ;
        }

        /* create  new node in center of area3 */
        n_anim[e_enqire[e_enqire[i].nr].nod[17  ]].nx = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[2]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[0]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[3]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[5]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 5]] * amplitude ) +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[ 8]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 9]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 9]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 8]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[14]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[14]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[11]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[11]] * amplitude   )  ;

        n_anim[e_enqire[e_enqire[i].nr].nod[17  ]].ny = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[2]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[0]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[3]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[5]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 5]] * amplitude ) +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[ 8]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 9]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 9]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 8]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[14]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[14]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[11]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[11]] * amplitude   )  ;

        n_anim[e_enqire[e_enqire[i].nr].nod[17  ]].nz = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[2]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[0]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[3]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[5]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 5]] * amplitude ) +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[ 8]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 9]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 9]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 8]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[14]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[14]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[11]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[11]] * amplitude   )  ;

        /* create  new node in center of area4 */
        n_anim[e_enqire[e_enqire[i].nr].nod[18  ]].nx = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[2]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[1]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[0]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude ) +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[ 8]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 8]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 7]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 7]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 6]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 6]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 0]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   )  ;

        n_anim[e_enqire[e_enqire[i].nr].nod[18  ]].ny = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[2]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[1]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[0]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude ) +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[ 8]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 8]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 7]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 7]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 6]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 6]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 0]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   )  ;

        n_anim[e_enqire[e_enqire[i].nr].nod[18  ]].nz = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[0]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[2]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 2]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[1]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 1]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[0]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude ) +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[ 8]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 8]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 7]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 7]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 6]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 6]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 0]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 0]] * amplitude   )  ;

        /* create  new node in center of area5 */
        n_anim[e_enqire[e_enqire[i].nr].nod[19  ]].nx = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[3]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[4]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 4]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[5]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 5]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[3]].nx  + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude ) +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[12]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[12]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[13]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[13]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[14]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[14]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 3]].nx + lcase[lc].dat[0][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   )  ;

        n_anim[e_enqire[e_enqire[i].nr].nod[19  ]].ny = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[3]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[4]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 4]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[5]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 5]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[3]].ny  + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude ) +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[12]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[12]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[13]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[13]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[14]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[14]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 3]].ny + lcase[lc].dat[1][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   )  ;

        n_anim[e_enqire[e_enqire[i].nr].nod[19  ]].nz = -0.25* (
          node[e_enqire[e_enqire[i].nr].nod[3]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[4]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 4]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[5]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 5]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[3]].nz  + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude ) +
          0.5*(
          node[e_enqire[e_enqire[i].nr].nod[12]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[12]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[13]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[13]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[14]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[14]] * amplitude   +
          node[e_enqire[e_enqire[i].nr].nod[ 3]].nz + lcase[lc].dat[2][e_enqire[e_enqire[i].nr].nod[ 3]] * amplitude   )  ;
      }
    }
      return(n_anim);
    }
    else
    {
      printf ("\n ERROR: choose a Dataset with displacements before\a\n");
      printf ("\n  Remark: Before you use this option select the Dataset and the Entity you want to display and then the related Dataset of the displacements (but no entity).\n");
    }
  }
  else
  {
     /* return the original pointer to the nodes and delete the manipulated nodes */
     addDispFlag=!addDispFlag;
     return(orig_nodes);
  }

  return(node);
}


void selectView( int selection )
{
  int i;
  static GLint ipuf[2];
  static unsigned char  mem_elemEdgeFlag, mem_surfFlag;

  glutSetWindow( w1);
  switch (selection) {
  case 1:
      if(getSetNr("-qcut")) zap("-qcut");
      sequenceFlag=0;
      drawMode=2;
      ConfigureAndShowWindow_Light();
    break;
  case 2:
    /* show bad elements */
    i=calcBadElements("all");
    if (i>0)
    {
      printf("found %d bad elements in set:all\n", i);
      sprintf(buffer, "e %s i",specialset->njby);
      plot(buffer);
      /* elemEdgeFlag=1;
      sequenceFlag=0;
      drawMode=2; */
    }
    else
    {
      printf("No bad elements in set:all\n");
    }
    if(drawMode==1) ConfigureAndShowWindow_Load();
    else if(drawMode<4) ConfigureAndShowWindow_Light();
    break;
  case 3:
    glPointSize (1);
    glGetIntegerv( GL_POLYGON_MODE, ipuf );
    if ( ipuf[1] != GL_FILL )
      glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
    break;
  case 4:
    glPointSize (1);
    glGetIntegerv( GL_POLYGON_MODE, ipuf );
    if ( ipuf[1] != GL_LINE )
      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE );
    break;
  case 5:
    glPointSize (1);
    glGetIntegerv( GL_POLYGON_MODE, ipuf );
    if ( ipuf[1] != GL_POINT )
      glPolygonMode( GL_FRONT_AND_BACK, GL_POINT );
    break;
  case 6:
    glGetIntegerv( GL_CULL_FACE_MODE, ipuf );
    if ( ipuf[0] == GL_BACK )
      {
      glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
      glCullFace ( GL_FRONT );
      }
    else
      {
      glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_oneside);
      glCullFace ( GL_BACK );
      }
    break;
  case 7:
      modelEdgeFlag=!modelEdgeFlag;
      if (modelEdgeFlag) /* copy from updateDispLists() */
        drawModelEdges( list_model_edges, basCol[0], 2., anz->g, node, edge );
    break;
  case 8:
      elemEdgeFlag=!elemEdgeFlag;
      if (elemEdgeFlag)
      {
        if (surfFlag) drawDispListEdges(list_surf_edges, basCol[0], 1., 'f' );
        else          drawDispListEdges(list_elem_edges, basCol[0], 1., 'e' );
      }
    break;
  case 9:
      /* change between element and face mode */
      surfFlag=!surfFlag;
      if(drawMode!=4)
      {
        if(surfFlag)
        {
          for(i=0; i<anzGeo->psets; i++ ) if(pset[i].type[0]=='e') pset[i].type[0]='f';
        }
        else
        {
          for(i=0; i<anzGeo->psets; i++ ) if(pset[i].type[0]=='f') pset[i].type[0]='e';
        }
      }
      redraw();
    break;
  case 10:
      cutFlag=!cutFlag;
      if( cutFlag)
      {
        glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
        mem_surfFlag=surfFlag;
        mem_elemEdgeFlag=elemEdgeFlag;
        glDisable ( GL_CULL_FACE );
        cullFlag=0;
        surfFlag=0;
        elemEdgeFlag=1;
        printf ("press middle Mouse Button to move in Z Direction\n");
      }
      else
      {
        surfFlag=mem_surfFlag;
        elemEdgeFlag=mem_elemEdgeFlag;
        cullFlag=1;
        glGetIntegerv( GL_CULL_FACE_MODE, ipuf );
        if ( ipuf[0] == GL_BACK )
        {
          glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_oneside);
        }
        else
        {
          glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
        }
        printf ("press middle Mouse Button to Zoom in and out\n");
      }
      askGLError("cutFlag, GL_CULL_FACE");
      redraw();
    break;
  case 11:
    if(foregrndcol) { foregrndcol=0; foregrndcol_rgb[0]=foregrndcol_rgb[1]=foregrndcol_rgb[2]=foregrndcol_rgb[3]=0.; }
    else            { foregrndcol=1; foregrndcol_rgb[0]=foregrndcol_rgb[1]=foregrndcol_rgb[2]=foregrndcol_rgb[3]=1.; }
    if(backgrndcol) { backgrndcol=0; backgrndcol_rgb[0]=backgrndcol_rgb[1]=backgrndcol_rgb[2]=backgrndcol_rgb[3]=0.; }
    else            { backgrndcol=1; backgrndcol_rgb[0]=backgrndcol_rgb[1]=backgrndcol_rgb[2]=backgrndcol_rgb[3]=1.; }
    for (i=0; i<anzGeo->psets; i++ )
    {
      if(pset[i].col==0) pset[i].col=1;
      else if(pset[i].col==1) pset[i].col=0;
    }
    updateDispLists();
    break;
  case 12:
    /* create a vector-plot */
    /* if one entity is choosen, check if it belongs to a vector (icind) */
    /* and choose also the other components */
    vectorFlag=!vectorFlag;
    if(vectorFlag) printf("\n Select one component or the value of a vector. The data must be marked as a vector or use the 'lc' command.\n");
   break;
  case 13:
    /* add/remove the actual displacements to all node-coordinates multiplied by anim_faktor */
    node=addDispToCoordinates(node);
    if(addDispFlag) printf("\n displacements of dataset:%d added. Scale them with 'scal d <value>'\n",pre_lc+1); 
    redraw();
    break;
  }
  glutSetWindow( w0);
  glutPostRedisplay();
}


void selectData( unsigned char *record)
{
  int i,j, dim, length, lc[3], e[3], nlc, ne=0;
  unsigned char data[8][MAX_LINE_LENGTH];

  /* change the element and face mode to 'value' */
  for(i=0; i<anzGeo->psets; i++)
    if((pset[i].type[0]=='e')||(pset[i].type[0]=='f')) pset[i].type[1]='v';

  length = sscanf( record,"%s %s %s %s %s %s %s %s", data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);

  if(anz->l<1) { printf(" ERROR: no Datasets available\n"); return; }

  /* search for 'e', after 'e' appear the entities (up to 3 for a vector-plot) */
  dim=nlc=ne=0;
  for(i=0; i<length; i++)
  {
    if(dim) e[ne++]=atoi(data[i]);
    else if(data[i][0]=='a') { e[ne++]=data[i][0]; dim=1; break; }
    else if(data[i][0]=='e') dim=length-i-1;
    else if(!dim)lc[nlc++]=atoi(data[i]);
  }
  /* check the selection */
  for(i=0; i<nlc; i++)
  {
    if( lc[i]>anz->l)
    { printf ("ERROR: Dataset %d not known, max Dataset=%d\a\n", lc[i], anz->l ); return; }
    for(j=0; j<ne; j++)
    {
      if ( ( e[j]=='a' ) || ( (e[j]<=lcase[lc[i]-1].ncomps) && (e[j]>0) ) ) ;
      else { printf("ERROR: entity not known %d\a\n", e[j]); return; }
    }
  } 
  if(!dim)
  {
    printf("WARNING: No entity specified, animation not created\n");
    dim=1;
    if(nlc==1)
    {
      drawMode=1;
      selectDataset(lc[0]-1);
    }
    /* store the sequence of Datasets */
    else
    {
      sequenceFlag=1;
      drawMode=3;
      seq_nlc=nlc;
      for(i=0; i<seq_nlc; i++)
      {
        if((i>0)&&(lcase[lc[i]-1].ncomps!=lcase[lc[i-1]-1].ncomps))
        {
          printf(" ERROR: selected Dataset is of different type than the 1st selected, please reselect\a\n");
          return;
        }
        else if((i>0)&&((lc[i]-lc[i-1])==0))
        {
          printf(" ERROR: selected Dataset is equal to the first selected, please reselect\a\n");
          return;
        }
        else seqLC[i]=lc[i]-1;
      }
      seq_steps=1+(seqLC[2]-seqLC[0])/(seqLC[1]-seqLC[0]);
    }
    return;
  } 

  /* if nlc > 1 generate a sequence */
  /* if dim > 1 generate a vector plot */
  if(dim>1) vectorFlag=1;

  sequenceFlag=0;
  v_dim=0;
    selectDataset(lc[0]-1);
  if(nlc==1)
  { 
    drawMode=1;
    if( e[0]=='a' )
    {
      drawMode=3;
      ConfigureAndShowWindow_Light();
    }

    /* prepare an vector- or an scalar-plot */
    else if(ne>1)
    {
      drawMode=5;
      v_dim=ne;
      seq_steps=1;
      seq_nlc=1;
      seqLC[0]=seqLC[2]=lc[0]-1;
      seqLC[1]=lc[0];
      for(i=0; i<ne; i++) entity_v[i]=e[i]-1;
      scale->bmin=scale->bmax=0;
      ConfigureAndShowWindow_Vector();
    }
    else
    {
      selectEntity(e[0]);
    }
  }
  else
  {
    /* store the sequence of Datasets */
    sequenceFlag=1;
    drawMode=3;
    seq_nlc=nlc;
    for(i=0; i<seq_nlc; i++)
    {
      if((i>0)&&(lcase[lc[i]-1].ncomps!=lcase[lc[i-1]-1].ncomps))
      {
        printf(" ERROR: selected Dataset is of different type than the 1st selected, please reselect\a\n");
        return;
      }
      else if((i>0)&&((lc[i]-lc[i-1])==0))
      {
        printf(" ERROR: selected Dataset is equal to the first selected, please reselect\a\n");
        return;
      }
      else seqLC[i]=lc[i]-1;
    }
    seq_steps=1+(seqLC[2]-seqLC[0])/(seqLC[1]-seqLC[0]);

    /* prepare an vector- or a scalar-plot */
    if(ne>1)
    {
      drawMode=5;
      v_dim=ne;
      for(i=0; i<ne; i++) entity_v[i]=e[i]-1;
      scale->bmin=scale->bmax=0;
      ConfigureAndShowWindow_Vector();
    }
    else
    {
      if( e[0]=='a' )
      {
        entity_Nr=4;
        drawMode=3;
        ConfigureAndShowWindow_Light();
      }
      else
      {
        entity_Nr=e[0];
        ConfigureAndShowWindow_Load();
      }
    }
  }
}



void Keyboard( unsigned char gkey, int x, int y )
{
  int  i,j, pos, new_elems=0;
  static unsigned char  keystroke[MAX_LINE_LENGTH], prognam[MAX_LINE_LENGTH];
  
  if ( parser( gkey, keystroke ) )
  {
    /* parser */
    pos = sword( keystroke, prognam);
    for(j=0;j<strlen(prognam); j++) prognam[j]=toupper(prognam[j]);

    if(movieFlag>0)
    {
      sprintf(picture_text, "COMMAND: %s", keystroke);
      i=strlen(picture_text);
      for(; i<80; i++) picture_text[i]=' ';
      picture_text[80]=0;
      i=glutGetWindow();
      glutSetWindow( w0);
      glutPostRedisplay();
      glutSetWindow(i );
    }
      
    if ( compare(prognam, "HELP", 4) == 4 ) help();
    else if ( compare(prognam, "QADD", 4) == 4 ) qadd(&keystroke[pos+1]);
    else if ( compare(prognam, "QALI", 4) == 4 ) qali();
    else if ( compare(prognam, "QBIA", 4) == 4 ) qbia();
    else if ( compare(prognam, "QBOD", 4) == 4 ) qbod(&keystroke[pos+1]);
    else if ( compare(prognam, "QCNT", 4) == 4 ) qcnt();
    else if ( compare(prognam, "QCUT", 4) == 4 ) qcut();
    else if ( compare(prognam, "QDEL", 4) == 4 ) qdel();
    else if ( compare(prognam, "QDIV", 4) == 4 ) qdiv();
    else if ( compare(prognam, "QDIS", 4) == 4 ) qdis();
    else if ( compare(prognam, "QELE", 4) == 4 ) qele();
    else if ( compare(prognam, "QENQ", 4) == 4 ) qenq();
    else if ( compare(prognam, "QFLP", 4) == 4 ) qflp();
    else if ( compare(prognam, "QLIN", 4) == 4 ) qlin(&keystroke[pos+1]);
    else if ( compare(prognam, "QPNT", 4) == 4 ) qpnt(&keystroke[pos+1]);
    else if ( compare(prognam, "QNOD", 4) == 4 ) qnod();
    else if ( compare(prognam, "QREM", 4) == 4 ) qrem(&keystroke[pos+1]);
    else if ( compare(prognam, "QSUR", 4) == 4 ) qsur(&keystroke[pos+1]);
    else if ( compare(prognam, "QSEQ", 4) == 4 ) qseq();
    else if ( compare(prognam, "QSPL", 4) == 4 ) qspl();
    else
    {
      commandoInterpreter( prognam, keystroke, pos, 0, 0, 0, 0 );
      if ( compare(prognam, "ELEM", 4) == 4 ) new_elems=1;
    }
    for (j=0; j<MAX_LINE_LENGTH; j++) prognam[j]=keystroke[j]=(unsigned char)NULL;

    if(new_elems)
    {
      new_elems=0;

      /* das neue netz muss noch zur beleuchteten ansicht aufbereitet werden  */
      num_olc       = anz->l;
      makeSurfaces();
      allocUtilityData();
      updateDispLists();       
    }
  }
}



void moveModel()
{
  glOrtho( -ds, ds, -ds, ds, -Z_DEPTH, Z_DEPTH ); /* nach glLoadIdentity () !! */
  
  v[0]= centerPnt[0] ;            /* nodes sind scaliert, sonst scalieren mit scalNodes() */
  v[1]= centerPnt[1] ;
  v[2]= centerPnt[2] ;
  v[3]=1.;
  m_sub( &dR[0][0], &R[0][0], &Rmem[0][0] );
  v_matmult( v, &dR[0][0] );
  glTranslated ( -v[0]-vmem[0], -v[1]-vmem[1], -v[2]-vmem[2] ); /* verschiebung durch verdrehung im Vorfeld */
                                            /* rueckgaengig machen */
  glTranslated ( dtx*ds, dty*ds, dtz );
  glMultMatrixd( &R[0][0] );
}



void DrawGraficLoad( void )
{
  static int flipflop;
  double xc, yc;

#if INX_MODE
  glClearIndex ( basCol[backgrndcol] );
#endif
#if TEX_MODE
  glClearColor ( backgrndcol_rgb[0], backgrndcol_rgb[1], backgrndcol_rgb[2], backgrndcol_rgb[3] );
#endif
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity ();
  moveModel();

#if TEX_MODE
  /* enable all colors for the TEX_MODE */
  glColor3d( 1,1,1);
  // glEnable(GL_TEXTURE_1D);
#endif
  if (lcase[lc].irtype == 3)
  {
    glCallList( list_elem_elstress );
  }
  else
  {
    if (surfFlag)   glCallList( list_surf_load );
    else            glCallList( list_elem_load );
  }
#if TEX_MODE
  // glDisable(GL_TEXTURE_1D);
#endif

  if (modelEdgeFlag)     glCallList( list_model_edges );
  if (elemEdgeFlag)
  {
    if (surfFlag) glCallList( list_surf_edges );
    else          glCallList( list_elem_edges );
  }
  if (pickFlag)
  {
    /* draw currend picking-Area  */
    xc= dx_cur/(double)width_w1;
    yc= dy_cur/(double)height_w1;
    glLoadIdentity ();
    glOrtho( -1., 1, -1, 1, -1., 1. ); /* nach glLoadIdentity () !! */
#if INX_MODE
    glIndexi  ( basCol[foregrndcol] );
#endif
#if TEX_MODE
    glColor3dv( foregrndcol_rgb );
#endif
    glBegin ( GL_LINE_LOOP );
      glVertex3d ( dx-xc, dy+yc, 1. );
      glVertex3d ( dx+xc, dy+yc, 1. );
      glVertex3d ( dx+xc, dy-yc, 1. );
      glVertex3d ( dx-xc, dy-yc, 1. );
    glEnd();
  }

  glutSwapBuffers();

  if((movieFlag>0)&&(!stopFlag))
  { if(flipflop) createHardcopy(3); flipflop=!flipflop; }
  if(hcpyFlag)
  {
    if ( activWindow==w1 )  glutPostRedisplay();
    if(flipflop) { createHardcopy(hcpyFlag); hcpyFlag=0; } flipflop=!flipflop;
  }
}


void DrawGraficLight( void )
{
  static int flipflop;
  double xc, yc;

  /* glScalef rausgenommen um Flaechennormalen nicht zu verlaengern  (Light!) */
  /* stattdessen werden der Viewport skaliert (glOrtho) */

#if INX_MODE
  glClearIndex ( basCol[backgrndcol] );
#endif
#if TEX_MODE
  glClearColor ( backgrndcol_rgb[0], backgrndcol_rgb[1], backgrndcol_rgb[2], backgrndcol_rgb[3] );
#endif
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity ();
  moveModel();

  if (surfFlag)    glCallList( list_surf_light );
  else             glCallList( list_elem_light );

  if (modelEdgeFlag) glCallList( list_model_edges );
  if (elemEdgeFlag)
  {
     if (surfFlag) glCallList( list_surf_edges );
     else          glCallList( list_elem_edges );
  }
  if (pickFlag)
  {
    /* draw currend picking-Area  */
    xc= dx_cur/(double)width_w1;
    yc= dy_cur/(double)height_w1;
    glLoadIdentity ();
    glOrtho( -1., 1, -1, 1, -1., 1. ); /* nach glLoadIdentity () !! */
#if INX_MODE
    glIndexi  ( basCol[foregrndcol] );
#endif
#if TEX_MODE
    glColor3dv( foregrndcol_rgb );
#endif
    glBegin ( GL_LINE_LOOP );
      glVertex3d ( dx-xc, dy+yc, 1. );
      glVertex3d ( dx+xc, dy+yc, 1. );
      glVertex3d ( dx+xc, dy-yc, 1. );
      glVertex3d ( dx-xc, dy-yc, 1. );
    glEnd();
  }

  glutSwapBuffers();

  if((movieFlag>0)&&(!stopFlag))
  { if(flipflop) createHardcopy(3); flipflop=!flipflop; }
  if(hcpyFlag)
  {
    if ( activWindow==w1 )  glutPostRedisplay();
    if(flipflop) { createHardcopy(hcpyFlag); hcpyFlag=0; } flipflop=!flipflop;
  }
}


void DrawGraficAnimate( void )
{
  static unsigned char buffer[MAX_LINE_LENGTH];
  static int t0, t1, flipflop;
  static double freqb, freqb_soll;

#if TEST
  frameNr++;
  /* Zeit stoppen  */
  if (frameNr==1) stopClock( frameNr );
  if (frameNr==100) {stopClock( frameNr ); frameNr=0;}
#endif

  /* glScalef rausgenommen um Flaechennormalen nicht zu verlaengern  (Light!) */
  /* stattdessen werden der Viewport skaliert (glOrtho) */

#if INX_MODE
  glClearIndex ( basCol[backgrndcol] );
#endif
#if TEX_MODE
  glClearColor ( backgrndcol_rgb[0], backgrndcol_rgb[1], backgrndcol_rgb[2], backgrndcol_rgb[3] );
#endif
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity ();
  moveModel();

  // glEnable ( GL_LIGHTING );
  if ( animList>=anim_steps) animList=0;
  glCallList( list_animate[animList] );
  // glDisable( GL_LIGHTING );
  if (modelEdgeFlag)     glCallList( list_model_edges );
  if (elemEdgeFlag)
  {
     if (surfFlag) glCallList( list_surf_edges );
     else          glCallList( list_elem_edges );
  }
  
  glLoadIdentity ();
  sprintf (buffer,"%4d%%Amplitude     ", anim_alfa[animList]);
#if INX_MODE
  glIndexi  ( basCol[foregrndcol] );
#endif
#if TEX_MODE
  glColor3dv( foregrndcol_rgb );
#endif
  text( -0.96, 0.96, 0.,buffer );
  glutSwapBuffers();
  if ( activWindow==w1 )  glutPostRedisplay();

  if (!stopFlag) animList++;

  /* because of a sgi-problem only one side of the graphics-buffer can be used for hcpy */
  /* therefore each frame is displayed twice if a movie is recorded */
  if((movieFlag>0)&&(!stopFlag))
  { if(!flipflop) animList--; else createHardcopy(3); flipflop=!flipflop; }
  if(hcpyFlag)
  {
    if(flipflop) { createHardcopy(hcpyFlag); hcpyFlag=0; } flipflop=!flipflop;
  }

  /* real-time wait */
  if(!movieFlag)
  {
    t0=t1;
    do {
      t1=glutGet(GLUT_ELAPSED_TIME);
      freqb = 1000 / ((t1-t0)+1) ;
      freqb_soll= anim_steps*1000 / time_per_period;
    } while ( freqb > freqb_soll );
  }
}


void DrawGraficSequence( void )
{
  static int i,j;
  static unsigned char buffer[MAX_LINE_LENGTH];
  static int t0, t1, flipflop;
  static double freqb, freqb_soll;
  unsigned char key;

#if TEST
  frameNr++;
  /* Zeit stoppen  */
  if (frameNr==1) stopClock( frameNr );
  if (frameNr==100) {stopClock( frameNr ); frameNr=0;}
#endif

  /* glScalef rausgenommen um Flaechennormalen nicht zu verlaengern  (Light!) */
  /* stattdessen werden der Viewport skaliert (glOrtho) */

#if INX_MODE
  glClearIndex ( basCol[backgrndcol] );
#endif
#if TEX_MODE
  glClearColor ( backgrndcol_rgb[0], backgrndcol_rgb[1], backgrndcol_rgb[2], backgrndcol_rgb[3] );
#endif
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity ();
  moveModel();

  if (lightFlag); 
#if TEX_MODE
  else
  {
    /* enable all colors for the TEX_MODE */
    glColor3d( 1,1,1);
    glEnable(GL_TEXTURE_1D);
  }
#endif
  if ( animList>=seq_steps) animList=0;
  glCallList( list_animate[animList] );
  if (lightFlag); 
#if TEX_MODE
  else
  {
    glDisable(GL_TEXTURE_1D);
  }
#endif
  if (compare(pickfunc, "qenq", 4) == 4)
  {
    if (lcase[lc].irtype != 3) for(i=1; i<=anz->nmax; i++) value_n[i]=lcase[lcase_animList].dat[animList][i];
  }
  if (modelEdgeFlag)     glCallList( list_model_edges );
  if (elemEdgeFlag)
  {
     if (surfFlag) glCallList( list_surf_edges );
     else          glCallList( list_elem_edges );
  }

  /* immediate draw the vectors. no display-list used because vector-length */
  /* should be updated immediately */
  if ((vectorFlag)&&(v_dim>1))
  {
    if(surfFlag) key='f';
    else         key='e';
    for (j=0; j<anzGeo->psets; j++ )
    {
      if (pset[j].type[0]==key)
      { 
        if(key=='f') drawFaces_vector( v_LC[animList], v_dim, entity_v, v_factor*ds*0.1*v_scale, set[pset[j].nr].anz_f, set[pset[j].nr].face, node, face);  
        if(key=='e') drawElements_vector( v_LC[animList], v_dim, entity_v, v_factor*ds*0.1*v_scale, set[pset[j].nr].anz_e, set[pset[j].nr].elem, node, e_enqire);
      }
    }
  }
  
  glLoadIdentity ();
  sprintf (buffer,"Frame:%d Time:%e", animList+1, lcase[lcase_animList].dat[animList][0]);
#if INX_MODE
  glIndexi  ( basCol[foregrndcol] );
#endif
#if TEX_MODE
  glColor3dv( foregrndcol_rgb );
#endif
  text( -0.96, 0.96, 0.,buffer );

  glutSwapBuffers();
  if ( activWindow==w1 )  glutPostRedisplay();

  if (!stopFlag) animList++;

  /* because of a sgi-problem only one side of the graphics-buffer can be used for hcpy */
  /* therefore each frame is displayed twice if a movie is recorded */
  if((movieFlag>0)&&(!stopFlag))
  { if(!flipflop) animList--; else createHardcopy(3); flipflop=!flipflop; }
  if(hcpyFlag)
  {
    if(flipflop) { createHardcopy(hcpyFlag); hcpyFlag=0; } flipflop=!flipflop;
  }

  /* real-time wait */
  if(!movieFlag)
  {
    t0=t1;
    do {
    t1=glutGet(GLUT_ELAPSED_TIME);
    freqb = 1000 / ((t1-t0)+1) ;
    freqb_soll= anim_steps*1000 / time_per_period;
    } while ( freqb > freqb_soll );
  }
}


void DrawPickedItems()
{
  static int flipflop;
  double xc, yc;


#if INX_MODE
  glClearIndex ( basCol[backgrndcol] );
#endif
#if TEX_MODE
  glClearColor ( backgrndcol_rgb[0], backgrndcol_rgb[1], backgrndcol_rgb[2], backgrndcol_rgb[3] );
#endif
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  if (pickFlag)
  {
    /* draw currend picking-Area  */
    xc= dx_cur/(double)width_w1;
    yc= dy_cur/(double)height_w1;
    glLoadIdentity ();
    glOrtho( -1., 1, -1, 1, -1., 1. ); /* nach glLoadIdentity () !! */
#if INX_MODE
    glIndexi  ( basCol[foregrndcol] );
#endif
#if TEX_MODE
    glColor3dv( foregrndcol_rgb );
#endif
    glBegin ( GL_LINE_LOOP );
      glVertex3d ( dx-xc, dy+yc, 1. );
      glVertex3d ( dx+xc, dy+yc, 1. );
      glVertex3d ( dx+xc, dy-yc, 1. );
      glVertex3d ( dx-xc, dy-yc, 1. );
    glEnd();
  }

  /* draw set-entities */
  glLoadIdentity ();
  moveModel();

  drawSets(!PICK);
  if (modelEdgeFlag) glCallList( list_model_edges );
  
  glutSwapBuffers();

  if((movieFlag>0)&&(!stopFlag))
  { if(flipflop) createHardcopy(3); flipflop=!flipflop; }
  if(hcpyFlag)
  {
    if ( activWindow==w1 )  glutPostRedisplay();
    if(flipflop) { createHardcopy(hcpyFlag); hcpyFlag=0; } flipflop=!flipflop;
  }
}


void DrawAxes()
{
  static unsigned char buffer[MAX_LINE_LENGTH];

#if INX_MODE
  glClearIndex ( basCol[backgrndcol] );
#endif
#if TEX_MODE
  glClearColor ( backgrndcol_rgb[0], backgrndcol_rgb[1], backgrndcol_rgb[2], backgrndcol_rgb[3] );
#endif
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLoadIdentity ();
  glOrtho( -1., 1., -1., 1., -1., 1. ); /* nach glLoadIdentity () !! */

    v[0]= centerPnt[0] ;            /* nodes sind scaliert, sonst scalieren mit scalNodes() */
    v[1]= centerPnt[1] ;
    v[2]= centerPnt[2] ;
    v[3]=1.;
    m_sub( &dR[0][0], &R[0][0], &Rmem[0][0] );
    v_matmult( v, &dR[0][0] );
    glMultMatrixd( &R[0][0] );

#if INX_MODE
    glIndexi  ( basCol[foregrndcol] );
#endif
#if TEX_MODE
    glColor3dv( foregrndcol_rgb );
#endif
    glBegin ( GL_LINE_STRIP );
     glVertex3d(0.,.5,0.);
     glVertex3d(0.,0.,0.);
     glVertex3d(.5,0.,0.);
    glEnd();
    glBegin ( GL_LINES );
     glVertex3d(0.,0.,0.);
     glVertex3d(0.,0.,.5);
    glEnd();
  
    text( .5, 0., 0., "X " );
    text( 0., .5, 0., "Y " );
    text( 0., 0., .5, "Z " );

  if(drawMode!=4)
  {
    glLoadIdentity ();
    if(surfFlag) sprintf (buffer,"s");
    else         sprintf (buffer,"v");
#if INX_MODE
    glIndexi  ( basCol[foregrndcol] );
#endif
#if TEX_MODE
    glColor3dv( foregrndcol_rgb );
#endif
    text( -1, -1, 0.,buffer );
  }    
  glutSwapBuffers();
}

void iniDrawMenu()
{
#if INX_MODE
  glClearIndex ( basCol[backgrndcol] );
#endif
#if TEX_MODE
  glClearColor ( backgrndcol_rgb[0], backgrndcol_rgb[1], backgrndcol_rgb[2], backgrndcol_rgb[3] ); 
#endif
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLoadIdentity ();
  glOrtho( -1., 1., -1., 1., -1., 1. ); /* nach glLoadIdentity () !! */

#if INX_MODE
  glIndexi  ( basCol[foregrndcol] );
#endif
#if TEX_MODE
  glColor3dv( foregrndcol_rgb );
#endif
  if (frameFlag)
  {
    glBegin ( GL_LINE_LOOP );
     glVertex3d(-1+(width_menu*19/20-1)*2./width_w0, 1-(height_menu/10-1)*2./height_w0, 0. );
     glVertex3d(-1+(width_menu*19/20-1)*2./width_w0, 1-(height_menu/10+height_w1+1)*2./height_w0, 0. );
     glVertex3d(-1+(width_menu*19/20+width_w1+1)*2./width_w0,1-(height_menu/10+height_w1+1)*2./height_w0,0.);
     glVertex3d(-1+(width_menu*19/20+width_w1+1)*2./width_w0, 1-(height_menu/10-1)*2./height_w0, 0. );
    glEnd();
  }
  if (filenamFlag)
    text( -0.01176*(width_ini+width_menu)/width_w0*strlen( datin ), -0.91, 0., datin );
  if (textFlag)
    text( -0.01176*(width_ini+width_menu)/width_w0*strlen( picture_text ), -0.97, 0., picture_text );
}

void DrawMenuLoad( void )
{
  unsigned char buffer[MAX_LINE_LENGTH];
#if INX_MODE
  defineColIndexes_load();
#endif
#if TEX_MODE
  defineColTextur_load();
#endif

  iniDrawMenu();
  if (scalaFlag)
  {
    if((!sequenceFlag)&&(lc > -1))
    {
      sprintf (buffer,"LC%d:%s", lc+1,lcase[lc].name);
      text( -0.99, 0.95, 0.,buffer);
      sprintf (buffer,"Tim:%lf", lcase[lc].value);
      text( -0.99, 0.9, 0.,buffer);
    }
    sprintf (buffer,"entity:%s", lcase[lc].compName[entity_Nr-1]);
    text( -0.99, 0.85, 0., buffer );
    sprintf (buffer,"max: %5.2e  ", scale->vmax);
    text( -0.99, 0.76, 0., buffer );
    sprintf (buffer,"min: %5.2e  ", scale->vmin);
    text( -0.99, 0.72, 0., buffer );
#if INX_MODE
    scala_indx( -0.93, -0.9, steps, scale->bmin, scale->bmax, offset, basCol[foregrndcol]);
#endif
#if TEX_MODE
    scala_tex( -0.93, -0.9, steps, scale->bmin, scale->bmax, (double)steps/(double)TEX_PIXELS, foregrndcol_rgb);
#endif
  }
  glutSwapBuffers();
}


void DrawMenuLight( void )
{
  iniDrawMenu();
  glutSwapBuffers();
}


void DrawMenuAnimate( void )
{
  unsigned char buffer[MAX_LINE_LENGTH];

  iniDrawMenu();
  if ((!sequenceFlag)&&(lc > -1))
  {
    sprintf (buffer,"LC%d:%s", lc+1,lcase[lc].name);
    text( -0.99, 0.95, 0.,buffer);
    sprintf (buffer,"Tim:%lf", lcase[lc].value);
    text( -0.99, 0.9, 0.,buffer);
  }
  text( -0.95, 0.5, 0., "Animated" );
  if (halfperiod)
  {
    text( -0.99, 0.8, 0., "tune-value:" );
    sprintf (buffer,"%3.1f", anim_faktor);
    text( -0.99, 0.76, 0., buffer );
  }
  glutSwapBuffers();
}


void DrawMenuSet( void )
{
#if INX_MODE
  defineColIndexes_set();
#endif
  iniDrawMenu();
  glutSwapBuffers();
}


void reshape( int width, int height )
{
   glutSetWindow( w0 );
/*     width_menu=width/5;   */ /* muss wg. bitmapfonts konstant bleiben! */
/*     height_menu=height/10;  */
     width_w0=width;
     width_w1=width - width_menu;
     height_w0=width_w1+height_menu;
     height_w1=width_w1;
     glutReshapeWindow( width_w0, height_w0);
     glViewport(0, 0, (GLint)width_w0, (GLint)height_w0 );
   glutSetWindow( w1 );
     glutPositionWindow( width_menu*19/20, height_menu/10);
     glutReshapeWindow( width_w1, height_w1);
     glViewport( 0, 0, (GLint)width_w1, (GLint)height_w1);
   glutSetWindow( w2 );
     glutPositionWindow( 0, height_w1*0.9);
     glutReshapeWindow( width_w1/10, height_w1/10);
     glViewport( 0, 0, (GLint)width_w1/10, (GLint)height_w1/10);
}

#define DIFF     0.5
#define MAT_DIFF 0.6

#define AMB      1.0
#define MAT_SPEC 0.0

void initLightAndMaterial_rgb( void )
{
    static float lmodel_ambient[] = { 0.5, 0.5, 0.5, 1.0 };

  /* lichtanteile rgba definieren  */
    static float ambient0[] = { AMB, AMB, AMB, 1.0 }; /* ungerichtet */
    static float diffuse0[] = { DIFF, DIFF, DIFF, 1.0 }; /* gerichtetes licht*/

  /* Materialeigenschaften definieren */
    static float mat_shininess[] = { 128. };             /* 0->128 Blankheit */
    static float mat_specular[] = { MAT_SPEC,MAT_SPEC,MAT_SPEC, 1.0 }; /* ungerichtete reflektion*/
    static float mat_diffuse[] = { MAT_DIFF,MAT_DIFF,MAT_DIFF, 1.0 };  /* gerichtete reflektion*/

  /* Position der Lichtquellen xyzw, mit w=0.0 entfernung unendlich def.*/
    static float position0[] = { .0, .0, -1., 0.0 };

    /*    glMatrixMode(GL_PROJECTION); fuert hier zu bildfehlern (Z-Buffer) */

    /* Definieren und Positionieren der Lampen */
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);          /* allseitiges Licht */
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);          /* gerichtetes Licht, ungerichtete reflexion */
    glLightfv(GL_LIGHT0, GL_SPECULAR, diffuse0);         /* gerichtetes Licht, gerichtete reflexion */
    glLightfv(GL_LIGHT0, GL_POSITION, position0);

    /* Reflexionseigenschaften des Materials aufbringen */
    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
    askGLError("initLightAndMaterial_rgb,GL_SHININESS");
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,  mat_specular);

    /* Beschreibung des Beleuchtungsmodells */
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); /* globales licht ohne quelle */
    glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_oneside);
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE); /* GL_FALSE: infinite distance */

    glEnable(GL_LIGHT0);
    glDisable (GL_COLOR_MATERIAL);      /* improves performance (~2%) */
}

#define DIFF_I     1.
void initLightAndMaterial_index( void )
{
  /* lichtanteile rgba definieren  */
  static float diffuse0[] = { DIFF_I,DIFF_I,DIFF_I, 1.0 };   /* gerichtetes licht, ungerichtete reflexion */
  static GLint shininess = { 128 };              /* 0->128 Blankheit (geht nicht!)*/

  /* Position der Lichtquellen xyzw, mit w=0.0 entfernung unendlich def.*/
  static float position0[] = { .0, .0, -1., 0.0 };

    glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, shininess );
    askGLError("initLightAndMaterial_index,GL_SHININESS");

    /* Definieren und Positionieren der Lampen */
    /*    glLightfv(GL_LIGHT0, GL_AMBIENT, ----- ); does not work in Index-mode */
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
    askGLError("initLightAndMaterial_index,GL_DIFFUSE");
    /*    glLightfv(GL_LIGHT0, GL_SPECULAR, specular0); has no effect */
    glLightfv(GL_LIGHT0, GL_POSITION, position0);
    askGLError("initLightAndMaterial_index,GL_POSITION");

    /* Beschreibung des Beleuchtungsmodells */
    glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_oneside);
    askGLError("initLightAndMaterial_index,GL_LIGHT_MODEL_TWO_SIDE");
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);       /* GL_FALSE: infinite distance */
    askGLError("initLightAndMaterial_index,GL_LIGHT_MODEL_LOCAL_VIEWER");

    glEnable(GL_LIGHT0);
}


int main ( int argc, char **argv )
{
  int i,j,k;
  unsigned char buffer[MAX_LINE_LENGTH], inpformat, automode=0;
  int  length;
  unsigned char winid[20], wname[20];
  static unsigned char ccxfile[MAX_LINE_LENGTH];
  FILE *handle;


  lcase=NULL;
  node=NULL;
  e_enqire=NULL;
  anz->n=0;
  anz->e=0;
  anz->f=0;
  anz->g=0;
  anz->l=0;
  anz->nmax=0;
  anz->nmin=MAXVALUE;
  anz->emax=0;
  anz->emin=MAXVALUE;

  anzGeo->p=0;
  anzGeo->l=0;
  anzGeo->c=0;
  anzGeo->s=0;
  anzGeo->b=0;
  anzGeo->nurl=0;
  anzGeo->nurs=0;
  anzGeo->sets=0;
  anzGeo->psets=0;

  openSets->nr=0;
  openSets->set=NULL;

  strcpy(specialset->flpb,FLPB);
  strcpy(specialset->njby,NJBY);
  strcpy(specialset->copy,COPY);
  strcpy(specialset->mesh,MESH);
  strcpy(specialset->nomesh,NOMESH);
  strcpy(specialset->zap,ZAP);
  strcpy(specialset->mpc,MPC);
  strcpy(specialset->nompc,NOMPC);
  strcpy(specialset->noel,NOEL);
  strcpy(specialset->ori,ORI);
  strcpy(specialset->dep,DEP);
  strcpy(specialset->ind,IND);
  strcpy(specialset->tmp,TMP);
  strcpy(specialset->highl,HIGHLIGHT);
  strcpy(specialset->uori,UORI);
  strcpy(specialset->bnur,BNUR);
  strcpy(specialset->bsur,BSUR);

  scale->x=scale->y=scale->z=0.; scale->w=1.;
  scale->xmax=1.; scale->xmin=-1.;
  scale->ymax=1.; scale->ymin=-1.;
  scale->zmax=1.; scale->zmin=-1.;

  seto( "all" );

  /* get the current system type  */
  uname (cursys);
  printf ("on a %s machine, ", cursys->sysname);
  printf ("nodename %s, ", cursys->nodename);
  printf ("release %s, ", cursys->release);
  printf ("version %s, ", cursys->version);
  printf ("machine %s \n", cursys->machine);

  if (getenv("CGXDOCS")!=NULL) {
  	sprintf(helpfile[0],"%s",getenv("CGXDOCS"));
  }
  if (getenv("CCXDOCS")!=NULL) {
  	sprintf(helpfile[1],"%s",getenv("CCXDOCS"));
  }
  if (getenv("BROWSER")!=NULL) {
  	sprintf(browser,"%s",getenv("BROWSER"));
  }
  if (getenv("CGXTERMVIEWER")!=NULL) {
  	sprintf(psviewer,"%s",getenv("CGXTERMVIEWER"));
  }

  if (argc < 2)
  {
    generalinfo();
    exit (-1);
  }
  else
  {
    inpformat=0;
    i=0;
    do{ i++;} while((i<argc)&&(argv[i][0]=='-'));
    printf("parameters:%d arguments:%d\n", argc, i);
    for (j=1; j<i; j++)
    {
      if( compare( argv[j], "-a", 2 ) == 2 )
      { 
        inpformat='b';
        automode=1;
        if(argc==i) strcpy( datin, "dummy.fbd"); 
        else strcpy( datin, argv[i]);
      }
      if( compare( argv[j], "-b", 2 ) == 2 )
      { 
        inpformat='b';
        if(argc==i) strcpy( datin, "dummy.fbd"); 
        else strcpy( datin, argv[i]);
      }
      else if( compare( argv[j], "-oldbias", 7 ) == 7 )  { OLD_BIAS_DEF=1; }
      else if( compare( argv[j], "-mksets", 6 ) == 6 )  { MAKE_SETS_DEF=1; }
      else if( compare( argv[j], "-c", 2 ) == 2 ) 
      { 
        inpformat='c';
        if(argc==i) { printf("ERROR: no file specified\n"); exit(-1); } 
        else strcpy( datin, argv[i]); 
      } 
      else if( compare( argv[j], "-isaac2d", 8 ) == 8 ) 
      { 
        inpformat='i';
        strcpy( datin, argv[argc-1]);
      } 
      else if( compare( argv[j], "-isaac3d", 8 ) == 8 ) 
      { 
        inpformat='j';
        strcpy( datin, argv[argc-1]);
      } 
      else if( compare( argv[j], "-pref", 5 ) == 5 )  { pref=atof(&argv[j][5]); }
      else if( compare( argv[j], "-tref", 5 ) == 5 )  { tref=atof(&argv[j][5]); }
      else if( compare( argv[j], "-R", 2 ) == 2 )     { R_GAS=atof(&argv[j][2]); }
      else if( compare( argv[j], "-duns2d", 7 ) == 7 ) 
      { 
        inpformat='d';
        if(argc==i) { printf("ERROR: no duns-file specified\n"); exit(-1); } 
        else strcpy( datin, argv[i]); 
      } 
      else if( compare( argv[j], "-duns3d", 7 ) == 7 ) 
      { 
        inpformat='e';
        if(argc==i) { printf("ERROR: no duns-file specified\n"); exit(-1); } 
        else strcpy( datin, argv[i]); 
      } 
      else if( compare( argv[j], "-foam", 5 ) == 5 ) 
      { 
        inpformat='f';
        if(argc==i) { printf("ERROR: no foam-file specified\n"); exit(-1); } 
        else strcpy( datin, argv[i]); 
      } 
      else if( compare( argv[j], "-stl", 4 ) == 4 ) 
      { 
        inpformat='s';
        if(argc==i) { printf("ERROR: no stl-file specified\n"); exit(-1); } 
        else strcpy( datin, argv[i]); 
      } 

      if( compare( argv[j], "--v", 3 ) == 3 ) { printf("Version %lf\n", VERSION);  exit (-1); } 
    }
    if (inpformat==0)
    {
      inpformat='v';
      if(argc==i) { printf("ERROR: no file specified\n"); exit(-1); }
      else if(argc==i+2) strcpy( ccxfile, argv[i+1]); 
      strcpy( datin, argv[i]); 
    }
  }

  if(inpformat=='b')
  {
    if ( (lcase = (Datasets *)malloc( (anz->l+1) * sizeof(Datasets))) == NULL )
      printf("\n\n ERROR: malloc failed lcase\n\n") ;
    if ( (node = (Nodes *)malloc( (anz->n+1) * sizeof(Nodes))) == NULL )
      printf("\n\n ERROR: malloc failed node\n\n") ;
    if ( (e_enqire = (Elements *)malloc( (anz->e+1) * sizeof(Elements))) == NULL )
      printf("\n\n ERROR: malloc failed elem\n\n") ;

    /* read the geometry of the model */
    readfbd( datin, 0);
  }
  else
  {
    if(inpformat=='c') iniMeshData( datin, "ccx" );
    if(inpformat=='d') iniMeshData( datin, "duns2d" );
    if(inpformat=='i') iniMeshData( datin, "isaac2d" );
    if(inpformat=='j') iniMeshData( datin, "isaac3d" );
    if(inpformat=='e') iniMeshData( datin, "duns3d" );
    if(inpformat=='f') iniMeshData( datin, "foam" );
    if(inpformat=='s') iniMeshData( datin, "stl" );
    if(inpformat=='v') iniMeshData( datin, "frd" );
    getScaleValues( anz->n, anzGeo->p, point, node, scale);
    scalNodes ( anz->n, node, scale );
    calcDatasets( num_olc, anz, node, lcase );
  }

  
  if(anz->n>0)
  {
    if ( (set[0].node = (int *)realloc((int *)set[0].node, (anz->n+1) * sizeof(int))) == NULL )
      printf(" ERROR: malloc failed in set[%d]:%s\n\n", 0, set[0].name);
    set[0].anz_n=0;
    for (i=0; i<anz->n; i++) if(!node[node[i].nr].pflag)
    {
      set[0].node[i]= node[i].nr;
      set[0].anz_n++;
    }
    qsort( set[0].node, set[0].anz_n, sizeof(int), (void *)compareInt );
  }
  if(anz->e>0)
  {
    set[0].anz_e=anz->e;
    if ( (set[0].elem = (int *)realloc((int *)set[0].elem, (anz->e+1) * sizeof(int))) == NULL )
      printf(" ERROR: malloc failed in set[%d]:%s\n\n", 0, set[0].name);
    for (i=0; i<set[0].anz_e; i++) set[0].elem[i]= e_enqire[i].nr;
    qsort( set[0].elem, set[0].anz_e, sizeof(int), (void *)compareInt );
  }


  /* Trackballfunktion inizialisieren  */
  trackball( 1, trackbsize, lastquat, -0.2,-0.7, 0.2, 0.7 );
  build_rotmatrix( R, lastquat );

  width_w0  = width_ini  + width_menu;
  height_w0 = height_ini + height_menu;
  width_w1=width_w0 - width_menu;
  height_w1=width_w1;

  /* X-related actions */
  /* determine the identifiers of all open CGX's and choose a new one */
  /* this is necessary for later identification of the win-id (hcpy)  */
#ifdef WIN32
  sprintf (buffer, "Calculix Graphix");
#else
  sprintf( buffer, "rm -f $HOME/CalculiX.tmp >/dev/null ");
  system (buffer);
  sprintf( buffer, "xlsclients -l  > $HOME/CalculiX.tmp    ");
  system (buffer);
  sprintf(xbuffer,"%s",getenv("HOME"));
  i=strlen(xbuffer);
  strcpy(&xbuffer[i],"/CalculiX.tmp");
  handle=fopen(xbuffer,"r+");
  if (handle==NULL)
  { printf ("\nERROR: The output file \"%s\" could not be opened.\n\n", xbuffer); exit(1);}
  open_cgxs=0;
  do
  {
    length = frecord( handle, buffer);
    if( buffer[length]== (unsigned char)EOF)  break;
    if( compare ("Window",buffer,6) == 6) stos( buffer,8,16,winid);
    stos( buffer,10,30,wname);
    sprintf( buffer, "Calculix Graphix No.  " );
    if ( compare (buffer,wname,16) == 16)
    {
      for (j=0; j<20; j++) if (wname[j]=='.') break;
      if( atoi(&wname[j+1])> open_cgxs) open_cgxs=atoi(&wname[j+1]) ;
    }
  }while ( length >-1);
  sprintf( buffer, "rm -f $HOME/CalculiX.tmp >/dev/null ");
  system (buffer);
  sprintf (buffer, "Calculix Graphix No.%d ", ++open_cgxs );
#endif

  /* Mutter-Fenster */
  glutInitWindowSize ( width_w0, height_w0 );
#if INX_MODE
  glutInitDisplayMode ( GLUT_INDEX | GLUT_DOUBLE );
#endif
#if TEX_MODE
  /* problems with xwd on sgi without GLUT_DEPTH */
  glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
#endif
  glutInit ( &argc, argv );
  activWindow= w0 = glutCreateWindow ( buffer );
  glDisable ( GL_DEPTH_TEST );
  glMatrixMode ( GL_MODELVIEW );
  glutReshapeFunc ( reshape );
  glutMouseFunc ( MouseState );
  glutKeyboardFunc ( Keyboard );
  glutVisibilityFunc ( WindowState );
  /* kein DITHERing (speed, Hoehenlinien)  */
  glDisable(GL_DITHER);
#if INX_MODE
  /* get the actual colormap and define the color-indexes for the main-window */
  getColormap();
  maxIndex= offset+steps-1;
  glShadeModel ( GL_FLAT );
#endif

  subsubmenu_animTune = glutCreateMenu( tuneAnimation );
  glutAddMenuEntry(" RESET TO 1.", 0);
  glutAddMenuEntry(" tune * 10", 1);
  glutAddMenuEntry(" tune *  5", 2);
  glutAddMenuEntry(" tune *  2", 3);
  glutAddMenuEntry(" tune /  2", 4);
  glutAddMenuEntry(" tune /  5", 5);
  glutAddMenuEntry(" tune / 10", 6);

  subsubmenu_animSteps = glutCreateMenu( stepsAnimation );
  glutAddMenuEntry("  4 Steps", 4);
  glutAddMenuEntry("  8 Steps", 8);
  glutAddMenuEntry(" 12 Steps", 12);
  glutAddMenuEntry(" 24 Steps", 24);
  glutAddMenuEntry(" 36 Steps", 36);
  glutAddMenuEntry(" 72 Steps", 72);

  subsubmenu_animPeriod = glutCreateMenu( newTimePerPeriod );
  glutAddMenuEntry(" Fastest     ", 1);
  glutAddMenuEntry(" 1,0 seconds ", 2);
  glutAddMenuEntry(" 1,2 seconds ", 3);
  glutAddMenuEntry(" 1,5 seconds ", 4);
  glutAddMenuEntry(" 2,0 seconds ", 5);
  glutAddMenuEntry(" 5,0 seconds ", 6);

  submenu_view = glutCreateMenu( selectView );
  glutAddMenuEntry("Show All Elements With Light", 1);
  glutAddMenuEntry("Show Bad Elements", 2);
  glutAddMenuEntry(" FILL  ", 3);
  glutAddMenuEntry(" LINES ", 4);
  glutAddMenuEntry(" DOTS ", 5);
  glutAddMenuEntry("Toggle Culling Back/Front", 6);
  glutAddMenuEntry("Toggle Model Edges", 7);
  glutAddMenuEntry("Toggle Element Edges", 8);
  glutAddMenuEntry("Toggle Surfaces/Volumes", 9);
  glutAddMenuEntry("Toggle Move-Z/Zoom", 10);
  glutAddMenuEntry("Toggle Background Color", 11);
  glutAddMenuEntry("Toggle Vector-Plot", 12);
  glutAddMenuEntry("Toggle Add-Displacement", 13);

  submenu_animate = glutCreateMenu( changeAnimation );
  glutAddMenuEntry("Start", 1);
  glutAddSubMenu  ("Tune-Value ", subsubmenu_animTune );
  glutAddSubMenu  ("Steps per Period", subsubmenu_animSteps );
  glutAddSubMenu  ("Time per Period ", subsubmenu_animPeriod );
  glutAddMenuEntry("Toggle Real Displacements", 2);
  glutAddMenuEntry("Toggle Dataset Sequence", 3);

  submenu_orientation = glutCreateMenu( orientModel );
  glutAddMenuEntry( "+x View     ", 1);
  glutAddMenuEntry( "-x View     ", 2);
  glutAddMenuEntry( "+y View     ", 3);
  glutAddMenuEntry( "-y View     ", 4);
  glutAddMenuEntry( "+z View     ", 5);
  glutAddMenuEntry( "-z View     ", 6);

#ifndef WIN32
  submenu_hardcopy = glutCreateMenu( markHardcopy );
  glutAddMenuEntry( "Ps-Hardcopy ", 1);
  glutAddMenuEntry( "Xwd-Hardcopy", 2);
  glutAddMenuEntry( "Start Recording Gif-Movie", 3);
#endif

  submenu_help = glutCreateMenu( showHelp );
  glutAddMenuEntry( "Quick Reference (cgx)", 1);
  glutAddMenuEntry( "Html Manual (cgx)", 2);
  glutAddMenuEntry( "Html Manual (ccx)", 3);

  subsubmenu_entity = glutCreateMenu( selectEntity );
  submenu_load = glutCreateMenu( selectDataset );
  mainmenu = glutCreateMenu( menu );
  createDatasetEntries();

  /* Displaylisten der zu beleuchdenden Struktur aufbauen */
  /*
  list_elem_light = glGenLists((GLsizei)1);
  list_surf_light = glGenLists((GLsizei)1);

  list_elem_load = glGenLists((GLsizei)1);
  list_elem_elstress = glGenLists((GLsizei)1);
  list_surf_load = glGenLists((GLsizei)1);
  list_model_edges = glGenLists((GLsizei)1);
  list_surf_edges = glGenLists((GLsizei)1);
  list_elem_edges = glGenLists((GLsizei)1);
  list_animate_light = glGenLists( (GLsizei)MAX_LOADCASES );
*/
  /* list_animate_light sollten als absolut letzte angelegt werden  */
  list_elem_light = glGenLists((GLuint)1);
  list_surf_light = glGenLists((GLuint)1);

  list_elem_load = glGenLists((GLuint)1);
  list_elem_elstress = glGenLists((GLuint)1);
  list_surf_load = glGenLists((GLuint)1);
  list_model_edges = glGenLists((GLuint)1);
  list_surf_edges = glGenLists((GLuint)1);
  list_elem_edges = glGenLists((GLuint)1);
  list_animate_light = glGenLists( (GLuint)MAX_LOADCASES );
  printf ("display lists created (0 to %d) \n", list_animate_light);

  /* Grafik-Fenster */
#if INX_MODE
  glutInitDisplayMode ( GLUT_INDEX | GLUT_DOUBLE | GLUT_DEPTH );
#endif
#if TEX_MODE
  glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
#endif
  activWindow= w1 = glutCreateSubWindow ( w0, width_menu*19/20, height_menu/10, width_ini, height_ini );
  glEnable ( GL_DEPTH_TEST );
  glDepthFunc(GL_LEQUAL);
  glFrontFace ( GL_CCW );
  glGetIntegerv (GL_MAX_EVAL_ORDER, &gl_max_eval_order);
  printf("GL_MAX_EVAL_ORDER:%d\n", gl_max_eval_order);
  /* Eventhandling Grafikfenster */
  glutMouseFunc ( MouseState );
  glutMotionFunc ( Mouse );
  glutKeyboardFunc ( Keyboard );
  glutEntryFunc ( entryfunktion );
  glutPassiveMotionFunc ( Mouse );
  glDisable(GL_DITHER);
  glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_oneside);
  glCullFace ( GL_BACK );
#if INX_MODE
  /* glutCopyColormap( w0 ); Geht auf cp0309 mit mesa nicht! */
  /* deshalb autonom belegen:  */
  for (i=0; i<256; i++)
  {
    glutSetColor( i, priv_cmap[i][0], priv_cmap[i][1], priv_cmap[i][2] );
  }
  initLightAndMaterial_index();
#endif
#if TEX_MODE
  initLightAndMaterial_rgb();
#endif

  drawMode=2;

  /* Axenkreuz-Fenster-index im w1 fenster  */
#if INX_MODE
  glutInitDisplayMode ( GLUT_INDEX | GLUT_DOUBLE  );
#endif
#if TEX_MODE
  glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH  );
#endif
  activWindow= w2 = glutCreateSubWindow ( w1, 0, height_ini*0.9, width_ini/10, height_ini/10 );
  glutDisplayFunc ( DrawAxes );
  glDisable(GL_DITHER);
  
  if(inpformat=='b')
  {
#if INX_MODE
    defineColIndexes_light();
#endif
#if WIN32
    ConfigureAndShowWindow_Light();
#endif

    /* if a cad file was read (if NURL exist) then prepare the visualization */
    if(automode==1)
    {
      k=pre_seta(specialset->zap, "i", 0 );
      for (i=0; i<anzGeo->l; i++) seta(k,"l",i);
      completeSet( specialset->zap, "d") ;
      sprintf( buffer,"p %s", specialset->zap);
      pre_merge( buffer);

      /* complete the default-set with the unoriented surfaces (-UORI) by the edge-points */
      /* of all lines and merge this edge-points */
      completeSet( specialset->uori, "e") ;
      sprintf( buffer,"p %s %lf nolock", specialset->uori, gtol*GTOL_EDGES);
      pre_merge( buffer);

      /* in case NURLs are read then calculate a suitable div for the related spline */
      for (i=0; i<set[k].anz_l; i++)
      {
        calcLineDiv(line, set[k].line[i], point, GTOL_COS_A, gtol*GTOL_NODE_DIST/scale->w, gtol*GTOL_NODE_DIST/scale->w*ELEM_LENGTH_RATIO);
      } 
      delSet(specialset->zap );

      glutSetWindow( w1 ); /* has to be before repSurf() to use the correct resolution */
      /* glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside); */
      /* glCullFace ( GL_FRONT ); */
      /* set "all" to avoid substitute surfs */
      for (i=0; i<set[0].anz_s; i++) repSurf(set[0].surf[i]);
      plot("si all    \n"); 
      updateDispLists();
    }
    else
    {
      if(plotstr[0]==0)
      {
        plot("p all    \n");
        plus("l all    \n"); 
        plus("s all    \n"); 
        plus("b all    \n");
      }
      else
      {
        plot(plotstr);
        for(i=0; i<anz_plusstr; i++) plus(plusstr[i]);
      }
    }
  }
  else ConfigureAndShowWindow_Light();

#if TEST
  printHash();
#endif

  sprintf(buffer, "%s inp", ccxfile );
  if(ccxfile[0]>0) pre_read(buffer);
  glutMainLoop ();
  return(1);
}



void printHash()
{
  int i;
  FILE *handle[7];

  handle[0] = fopen ("hash.p", "w");
  if ( handle[0]== NULL )
  {
    printf ("\nThe file could not be opened.\n\n"); 
    return;
  }
  for (i=0; i<MAX_SUM_ASCI; i++)
  {
    fprintf(handle[0], " sumASCI: %d indexes: %d\n", i, sumAsci->anzp[i]);
  }  
  handle[1] = fopen ("hash.l", "w");
  if ( handle[1]== NULL )
  {
    printf ("\nThe file could not be opened.\n\n"); 
    return;
  }
  for (i=0; i<MAX_SUM_ASCI; i++)
  {
    fprintf(handle[1], " sumASCI: %d indexes: %d\n", i, sumAsci->anzl[i]);
  }  
  handle[2] = fopen ("hash.c", "w");
  if ( handle[2]== NULL )
  {
    printf ("\nThe file could not be opened.\n\n"); 
    return;
  }
  for (i=0; i<MAX_SUM_ASCI; i++)
  {
    fprintf(handle[2], " sumASCI: %d indexes: %d\n", i, sumAsci->anzc[i]);
  }  
  handle[3] = fopen ("hash.s", "w");
  if ( handle[3]== NULL )
  {
    printf ("\nThe file could not be opened.\n\n"); 
    return;
  }
  for (i=0; i<MAX_SUM_ASCI; i++)
  {
    fprintf(handle[3], " sumASCI: %d indexes: %d\n", i, sumAsci->anzs[i]);
  }  
  handle[4] = fopen ("hash.b", "w");
  if ( handle[4]== NULL )
  {
    printf ("\nThe file could not be opened.\n\n"); 
    return;
  }
  for (i=0; i<MAX_SUM_ASCI; i++)
  {
    fprintf(handle[4], " sumASCI: %d indexes: %d\n", i, sumAsci->anzb[i]);
  }  
  handle[5] = fopen ("hash.a", "w");
  if ( handle[5]== NULL )
  {
    printf ("\nThe file could not be opened.\n\n"); 
    return;
  }
  for (i=0; i<MAX_SUM_ASCI; i++)
  {
    fprintf(handle[5], " sumASCI: %d indexes: %d\n", i, sumAsci->anza[i]);
  }  
  handle[6] = fopen ("hash.S", "w");
  if ( handle[6]== NULL )
  {
    printf ("\nThe file could not be opened.\n\n"); 
    return;
  }
  for (i=0; i<MAX_SUM_ASCI; i++)
  {
    fprintf(handle[6], " sumASCI: %d indexes: %d\n", i, sumAsci->anzS[i]);
  }  
}




