/* --------------------------------------------------------------------  */
/*                          CALCULIX                                     */
/*                   - GRAPHICAL INTERFACE -                             */
/*                                                                       */
/*     A 3-dimensional pre- and post-processor for finite elements       */
/*              Copyright (C) 1996 Klaus Wittig                          */
/*                                                                       */
/*     This program is free software; you can redistribute it and/or     */
/*     modify it under the terms of the GNU General Public License as    */
/*     published by the Free Software Foundation; version 2 of           */
/*     the License.                                                      */
/*                                                                       */
/*     This program is distributed in the hope that it will be useful,   */
/*     but WITHOUT ANY WARRANTY; without even the implied warranty of    */ 
/*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
/*     GNU General Public License for more details.                      */
/*                                                                       */
/*     You should have received a copy of the GNU General Public License */
/*     along with this program; if not, write to the Free Software       */
/*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         */
/* --------------------------------------------------------------------  */

#include <cgx.h>

void descalPoints ( int anz_p, Points *point, Scale *scale )
{
  register int  i;
  for (i=0; i<anz_p; i++ )
    {
    /*  zuruek scalieren  */
    point[i].px = (point[i].px* scale->w+scale->x);
    point[i].py = (point[i].py* scale->w+scale->y);
    point[i].pz = (point[i].pz* scale->w+scale->z);
    }
}


void scalPoints ( int anz_p,  Points *point, Scale *scale )
{
  register int  i;
  for (i=0; i<anz_p; i++ )
    {
    /* scalieren  */
    point[i].px = (point[i].px-scale->x)/scale->w;
    point[i].py = (point[i].py-scale->y)/scale->w;
    point[i].pz = (point[i].pz-scale->z)/scale->w;
    }
}

void descalSurfs ( int anz_s,  Gsur *surf, Scale *scale )
{
  register int  i,j,n,k;
  for (i=0; i<anz_s; i++ )
  {
    n=0;
    while((surf[i].npgn-n))
    {
      n++; /* jump over the polygon token (ie.GL_POLYGON_TOKEN) */
      j=surf[i].pgn[n++];
      n+=3;
      for(k=0; k<j; k++)
      {
        surf[i].pgn[n]=(surf[i].pgn[n]*scale->w)+scale->x; 
        surf[i].pgn[n+1]=(surf[i].pgn[n+1]*scale->w)+scale->y; 
        surf[i].pgn[n+2]=(surf[i].pgn[n+2]*scale->w)+scale->z;
        n+=3; 
      }
    }
  }
}

void scalSurfs ( int anz_s,  Gsur *surf, Scale *scale )
{
  register int  i,j,n,k;
  for (i=0; i<anz_s; i++ )
  {
    /* printf("surf:%s w:%lf xyz: %lf %lf %lf\n",surf[i].name,scale->w,scale->x,scale->y,scale->z); */
    n=0;
    while((surf[i].npgn-n))
    {
      n++; /* jump over the polygon token (ie.GL_POLYGON_TOKEN) */
      j=surf[i].pgn[n++];
      n+=3;
      for(k=0; k<j; k++)
      {
        /* printf(" %lf %lf %lf  -> ", surf[i].pgn[n],surf[i].pgn[n+1],surf[i].pgn[n+2]); */
        surf[i].pgn[n]=(surf[i].pgn[n]-scale->x)/scale->w; 
        surf[i].pgn[n+1]=(surf[i].pgn[n+1]-scale->y)/scale->w; 
        surf[i].pgn[n+2]=(surf[i].pgn[n+2]-scale->z)/scale->w; 
        n+=3;
        /* printf(" %lf %lf %lf\n", surf[i].pgn[n-3],surf[i].pgn[n-2],surf[i].pgn[n-1]); */ 
      }
    }
  }
}


void getScaleValues( int anz_n, int anz_p, Points *point, Nodes *node, Scale *scale)
{
  register int  i;

  /* -----------  MAX und MIN Werte feststellen -----------------  */

  scale->xmax=-10000000.; scale->xmin=10000000.;
  scale->ymax=-10000000.; scale->ymin=10000000.;
  scale->zmax=-10000000.; scale->zmin=10000000.;

  for (i=0; i<anz_n; i++ )
    {
    if (node[node[i].nr].nx > scale->xmax) scale->xmax=node[node[i].nr].nx;
    if (node[node[i].nr].nx < scale->xmin) scale->xmin=node[node[i].nr].nx;
    if (node[node[i].nr].ny > scale->ymax) scale->ymax=node[node[i].nr].ny;
    if (node[node[i].nr].ny < scale->ymin) scale->ymin=node[node[i].nr].ny;
    if (node[node[i].nr].nz > scale->zmax) scale->zmax=node[node[i].nr].nz;
    if (node[node[i].nr].nz < scale->zmin) scale->zmin=node[node[i].nr].nz;
    }

  for (i=0; i<anz_p; i++ )
    {
    if (point[i].px > scale->xmax) scale->xmax=point[i].px;
    if (point[i].px < scale->xmin) scale->xmin=point[i].px;
    if (point[i].py > scale->ymax) scale->ymax=point[i].py;
    if (point[i].py < scale->ymin) scale->ymin=point[i].py;
    if (point[i].pz > scale->zmax) scale->zmax=point[i].pz;
    if (point[i].pz < scale->zmin) scale->zmin=point[i].pz;
    }

  /* ------------  DATEN scalieren -------------------  */

  scale->x=(scale->xmax+scale->xmin)/2.;
  scale->y=(scale->ymax+scale->ymin)/2.;
  scale->z=(scale->zmax+scale->zmin)/2.;
  scale->xmax=scale->xmax-scale->x;
  scale->ymax=scale->ymax-scale->y;
  scale->zmax=scale->zmax-scale->z;
  scale->xmin=scale->xmin-scale->x;
  scale->ymin=scale->ymin-scale->y;
  scale->zmin=scale->zmin-scale->z;
  if (scale->xmax < (-scale->xmin)) scale->xmax=(-scale->xmin);
  if (scale->ymax < (-scale->ymin)) scale->ymax=(-scale->ymin);
  if (scale->zmax < (-scale->zmin)) scale->zmax=(-scale->zmin);
  scale->w=scale->xmax;
  if (scale->w < scale->ymax){ scale->w=scale->ymax;}
  if (scale->w < scale->zmax){ scale->w=scale->zmax;}

  scale->w/=0.4; /* nochmal scaliert */
  if (scale->w==0.) scale->w=1.;
}




