/* --------------------------------------------------------------------  */
/*                          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>

/*
Creates "ctri3" based on a set of elements eset. The element-faces to be used are identified by a set of nodes setNr.
Each tri3 knows the element and face it belongs to.

        ctri3[sum_tri3].elem_nr=  e, real elemnr
        ctri3[sum_tri3].group  =  elem-face-nr
        ctri3[sum_tri3].mat    =  elem-mat (unused)
        ctri3[sum_tri3].nod[0] = elem[e].nod[];
        ctri3[sum_tri3].nod[1] = elem[e].nod[];
        ctri3[sum_tri3].nod[2] = elem[e].nod[];

Creates "tri3_index[nodenr][i]" which holds the ctri3-indexes who use nodenr in common (-1<i<ntri_nodes[nodenr]).

Creates "ntri_nodes[nodenr]" which stores the amound of ctri3 who use nodenr.

returns -1 or "sum_tri3"
*/


int makeTriFromElems(int setNr, int eset, int nmax, Sets *set, Elements *elem,  CTri3 **pctri3, int ***ptri3_index, int **pntri_nodes)
{
  int i, ie, e, j, n, s;
  int sum_tri3;
  int       ec[21];                  /* */

  CTri3 *ctri3;
  int   **tri3_index;
  int   *ntri_nodes;
  
  ctri3=*pctri3;
  tri3_index=*ptri3_index;
  ntri_nodes=*pntri_nodes;

  if((tri3_index =(int **)realloc((int **)tri3_index, (nmax+1) *sizeof(int *)))==NULL)
  {
    errMsg("\n\n ERROR: realloc failed for tri3_index\n" );
    return(-1);
  }
  if((ntri_nodes =(int *)malloc((nmax+2) *sizeof(int)))==NULL)
  {
    errMsg("\n\n ERROR: malloc failed for ntri_nodes\n" );
    return(-1);
  }
  if ( (ctri3 = (CTri3 *)malloc( 1 * sizeof(CTri3))) == NULL )
  {
    errMsg("\nERROR: realloc failed in makeTriFromElems() \n\n");
    return(-1);
  }

  for (i=0; i<=nmax; i++)
  {
    tri3_index[i]=NULL;
    ntri_nodes[i]=0;
  }
  sum_tri3=0;

  for (ie=0; ie<set[eset].anz_e; ie++)
  {
    e=set[eset].elem[ie];
    j=0;

    /* ----- Tri3 --------------- */
    if (elem[e].type==7)
    {
      for (n=0; n<3; n++)
      {
        for (i=0; i<set[setNr].anz_n; i++)
        {
          if (elem[e].nod[n]==set[setNr].node[i])
          {
            ec[j]=n;
            j++;
          }
        }
      }
      if(j==3)
      {
        /* merke dir alle am node haengenden flaechen (flaeche: sum_tri3)*/
        for (i=0; i<3; i++)
        {
         if((tri3_index[ elem[e].nod[ec[i]] ] 
          = (int *)realloc((int *)tri3_index[ elem[e].nod[ec[i]] ]
          , (ntri_nodes[elem[e].nod[ec[i]]]+1) *sizeof(int)))==NULL)
	  {
            errMsg("\n\n ERROR: realloc failed for tri3_index\n" );
            return(-1);
          }

          tri3_index[ elem[e].nod[ec[i]] ][ ntri_nodes[elem[e].nod[ec[i]]] ]=sum_tri3;
          ntri_nodes[ elem[e].nod[ec[i]] ]++;
        }
        if ( (ctri3 = (CTri3 *)realloc( (CTri3 *)ctri3, (sum_tri3+2) * sizeof(CTri3))) == NULL )
        {
          errMsg("\nERROR: realloc failed in makeTriFromElems() \n\n");
          return(-1);
        }
        ctri3[sum_tri3].elem_nr= e;  /* elem-indx im hilfsfeld elem */
        ctri3[sum_tri3].group  = 1;
        ctri3[sum_tri3].mat    = elem[e].mat;
        ctri3[sum_tri3].nod[0]=elem[e].nod[0];
        ctri3[sum_tri3].nod[1]=elem[e].nod[1];
        ctri3[sum_tri3].nod[2]=elem[e].nod[2];
        sum_tri3++;
      }
    }

    /* ----- Tri6 --------------- */
    else if (elem[e].type==8)
    {
      for (n=0; n<6; n++)
      {
        for (i=0; i<set[setNr].anz_n; i++)
        {
          if (elem[e].nod[n]==set[setNr].node[i])
          {
            ec[j]=n;
            j++;
          }
        }
      }
      if(j==6)
      {
        /* merke dir alle am node haengenden flaechen (flaeche: sum_tri3, sum_tri3+1)*/
        for (n=0; n<4; n++)
        {
          for (i=0; i<6; i++)
          {
            if((tri3_index[ elem[e].nod[ec[i]] ] 
            = (int *)realloc((int *)tri3_index[ elem[e].nod[ec[i]] ]
            , (ntri_nodes[elem[e].nod[ec[i]]]+1) *sizeof(int)))==NULL)
	    {
              errMsg("\n\n ERROR: realloc failed for tri3_index\n" );
              return(-1);
            }

            tri3_index[ elem[e].nod[ec[i]] ][ ntri_nodes[elem[e].nod[ec[i]]] ]=sum_tri3+n;
            ntri_nodes[ elem[e].nod[ec[i]] ]++;
          }
        }
        sum_tri3+=4;
        if ( (ctri3 = (CTri3 *)realloc( (CTri3 *)ctri3, sum_tri3 * sizeof(CTri3))) == NULL )
        {
          errMsg("\nERROR: realloc failed in makeTriFromElems() \n\n");
          return(-1);
        }

        ctri3[sum_tri3-4].elem_nr= e;
        ctri3[sum_tri3-4].group  = 1;
        ctri3[sum_tri3-4].mat    = elem[e].mat;
        ctri3[sum_tri3-4].nod[0]=elem[e].nod[0];
        ctri3[sum_tri3-4].nod[1]=elem[e].nod[5];
        ctri3[sum_tri3-4].nod[2]=elem[e].nod[3];

        ctri3[sum_tri3-3].elem_nr= e;
        ctri3[sum_tri3-3].group  = 1;
        ctri3[sum_tri3-3].mat    = elem[e].mat;
        ctri3[sum_tri3-3].nod[0]=elem[e].nod[3];
        ctri3[sum_tri3-3].nod[1]=elem[e].nod[5];
        ctri3[sum_tri3-3].nod[2]=elem[e].nod[4];

        ctri3[sum_tri3-2].elem_nr= e;
        ctri3[sum_tri3-2].group  = 1;
        ctri3[sum_tri3-2].mat    = elem[e].mat;
        ctri3[sum_tri3-2].nod[0]=elem[e].nod[5];
        ctri3[sum_tri3-2].nod[1]=elem[e].nod[2];
        ctri3[sum_tri3-2].nod[2]=elem[e].nod[4];

        ctri3[sum_tri3-1].elem_nr= e;
        ctri3[sum_tri3-1].group  = 1;
        ctri3[sum_tri3-1].mat    = elem[e].mat;
        ctri3[sum_tri3-1].nod[0]=elem[e].nod[3];
        ctri3[sum_tri3-1].nod[1]=elem[e].nod[4];
        ctri3[sum_tri3-1].nod[2]=elem[e].nod[1];
      }
    }

    /* ----- Qu4 --------------- */
    else if (elem[e].type==9)
    {
     for (n=0; n<4; n++)
      {
        for (i=0; i<set[setNr].anz_n; i++)
        {
          if (elem[e].nod[n]==set[setNr].node[i])
          {
            ec[j]=n;
            j++;
          }
        }
      }
     if(j==4)
      {
        /* merke dir alle am node haengenden flaechen (flaeche: sum_tri3, sum_tri3+1)*/
        for (n=0; n<2; n++)
        {
          for (i=0; i<4; i++)
          {
            if((tri3_index[ elem[e].nod[ec[i]] ] 
            = (int *)realloc((int *)tri3_index[ elem[e].nod[ec[i]] ]
            , (ntri_nodes[elem[e].nod[ec[i]]]+1) *sizeof(int)))==NULL)
	    {
              errMsg("\n\n ERROR: realloc failed for tri3_index\n" );
              return(-1);
            }

            tri3_index[ elem[e].nod[ec[i]] ][ ntri_nodes[elem[e].nod[ec[i]]] ]=sum_tri3+n;
            ntri_nodes[ elem[e].nod[ec[i]] ]++;
          }
        }
        sum_tri3+=2;
        if ( (ctri3 = (CTri3 *)realloc( (CTri3 *)ctri3, sum_tri3 * sizeof(CTri3))) == NULL )
        {
          errMsg("\nERROR: realloc failed in makeTriFromElems() \n\n");
          return(-1);
        }

        ctri3[sum_tri3-2].elem_nr= e;
        ctri3[sum_tri3-2].group  = 1;
        ctri3[sum_tri3-2].mat    = elem[e].mat;
        ctri3[sum_tri3-2].nod[0]=elem[e].nod[0];
        ctri3[sum_tri3-2].nod[1]=elem[e].nod[1];
        ctri3[sum_tri3-2].nod[2]=elem[e].nod[2];

        ctri3[sum_tri3-1].elem_nr= e;
        ctri3[sum_tri3-1].group  = 1;
        ctri3[sum_tri3-1].mat    = elem[e].mat;
        ctri3[sum_tri3-1].nod[0]=elem[e].nod[0];
        ctri3[sum_tri3-1].nod[1]=elem[e].nod[2];
        ctri3[sum_tri3-1].nod[2]=elem[e].nod[3];
      }
    }


    /* ----- Qu8 --------------- */
    else if (elem[e].type==10)
    {
      for (n=0; n<8; n++)
      {
        for (i=0; i<set[setNr].anz_n; i++)
        {
          if (elem[e].nod[n]==set[setNr].node[i])
          {
            ec[j]=n;
            j++;
          }
        }
      }
      if(j==8)
      {
        /* merke dir alle am node haengenden flaechen (flaeche: sum_tri3, sum_tri3+1)*/
        for (n=0; n<6; n++)
        {
          for (i=0; i<8; i++)
          {
            if((tri3_index[ elem[e].nod[ec[i]] ] 
            = (int *)realloc((int *)tri3_index[ elem[e].nod[ec[i]] ]
            , (ntri_nodes[elem[e].nod[ec[i]]]+1) *sizeof(int)))==NULL)
	    {
              errMsg("\n\n ERROR: realloc failed for tri3_index\n" );
              return(-1);
            }

            tri3_index[ elem[e].nod[ec[i]] ][ ntri_nodes[elem[e].nod[ec[i]]] ]=sum_tri3+n;
            ntri_nodes[ elem[e].nod[ec[i]] ]++;
          }
        }
        sum_tri3+=6;
        if ( (ctri3 = (CTri3 *)realloc( (CTri3 *)ctri3, sum_tri3 * sizeof(CTri3))) == NULL )
        {
          errMsg("\nERROR: realloc failed in makeTriFromElems() \n\n");
          return(-1);
        }

        ctri3[sum_tri3-6].elem_nr= e;
        ctri3[sum_tri3-6].group  = 1;
        ctri3[sum_tri3-6].mat    = elem[e].mat;
        ctri3[sum_tri3-6].nod[0]=elem[e].nod[4];
        ctri3[sum_tri3-6].nod[1]=elem[e].nod[0];
        ctri3[sum_tri3-6].nod[2]=elem[e].nod[7];

        ctri3[sum_tri3-5].elem_nr= e;
        ctri3[sum_tri3-5].group  = 1;
        ctri3[sum_tri3-5].mat    = elem[e].mat;
        ctri3[sum_tri3-5].nod[0]=elem[e].nod[7];
        ctri3[sum_tri3-5].nod[1]=elem[e].nod[3];
        ctri3[sum_tri3-5].nod[2]=elem[e].nod[6];

        ctri3[sum_tri3-4].elem_nr= e;
        ctri3[sum_tri3-4].group  = 1;
        ctri3[sum_tri3-4].mat    = elem[e].mat;
        ctri3[sum_tri3-4].nod[0]=elem[e].nod[6];
        ctri3[sum_tri3-4].nod[1]=elem[e].nod[2];
        ctri3[sum_tri3-4].nod[2]=elem[e].nod[5];

        ctri3[sum_tri3-3].elem_nr= e;
        ctri3[sum_tri3-3].group  = 1;
        ctri3[sum_tri3-3].mat    = elem[e].mat;
        ctri3[sum_tri3-3].nod[0]=elem[e].nod[5];
        ctri3[sum_tri3-3].nod[1]=elem[e].nod[1];
        ctri3[sum_tri3-3].nod[2]=elem[e].nod[4];

        ctri3[sum_tri3-2].elem_nr= e;
        ctri3[sum_tri3-2].group  = 1;
        ctri3[sum_tri3-2].mat    = elem[e].mat;
        ctri3[sum_tri3-2].nod[0]=elem[e].nod[4];
        ctri3[sum_tri3-2].nod[1]=elem[e].nod[7];
        ctri3[sum_tri3-2].nod[2]=elem[e].nod[6];

        ctri3[sum_tri3-1].elem_nr= e;
        ctri3[sum_tri3-1].group  = 1;
        ctri3[sum_tri3-1].mat    = elem[e].mat;
        ctri3[sum_tri3-1].nod[0]=elem[e].nod[6];
        ctri3[sum_tri3-1].nod[1]=elem[e].nod[5];
        ctri3[sum_tri3-1].nod[2]=elem[e].nod[4];
      }
    }

    /* ----- Te4 --------------- */
    else if (elem[e].type==3)
    {
      /* go over all element faces and look if all nodes are also in the node-set */
      for (s=0; s<4; s++)
      {
        if(s==0)      { ec[0]=0; ec[1]=2; ec[2]=1;  }
        else if(s==1) { ec[0]=0; ec[1]=1; ec[2]=3;  }
        else if(s==2) { ec[0]=1; ec[1]=2; ec[2]=3;  }
        else if(s==3) { ec[0]=2; ec[1]=0; ec[2]=3;  }

        j=0;
        for (n=0; n<3; n++)
        {
          for (i=0; i<set[setNr].anz_n; i++)
          {
            if (elem[e].nod[ec[n]]==set[setNr].node[i]) j++;
          }
        }
        if(j==3)
        {
          /* merke dir alle am node haengenden flaechen (flaeche: sum_tri3, sum_tri3+1)*/
          for (i=0; i<1; i++)
          {
            for (n=0; n<3; n++)
            {
              if((tri3_index[ elem[e].nod[ec[n]] ] 
              = (int *)realloc((int *)tri3_index[ elem[e].nod[ec[n]] ]
              , (ntri_nodes[elem[e].nod[ec[n]]]+1) *sizeof(int)))==NULL)
    	      {
                errMsg("\n\n ERROR: realloc failed for tri3_index\n" );
                return(-1);
              }
    
              tri3_index[ elem[e].nod[ec[n]] ][ ntri_nodes[elem[e].nod[ec[n]]] ]=sum_tri3+i;
              ntri_nodes[ elem[e].nod[ec[n]] ]++;
            }
          }
          if ( (ctri3 = (CTri3 *)realloc( (CTri3 *)ctri3, (sum_tri3+1) * sizeof(CTri3))) == NULL )
          {
            errMsg("\nERROR: realloc failed in makeTriFromElems() \n\n");
            return(-1);
          }

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[0]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[1]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[2]];
          sum_tri3++;
        }
      }
    }

    /* ----- Te10 --------------- */
    else if (elem[e].type==6)
    {
      /* go over all element faces and look if all nodes are also in the node-set */
      for (s=0; s<4; s++)
      {
        if(s==0)      { ec[0]=0; ec[1]=4; ec[2]=1; ec[3]=8; ec[4]=3; ec[5]=7;  }
        else if(s==1) { ec[0]=1; ec[1]=5; ec[2]=2; ec[3]=9; ec[4]=3; ec[5]=8;  }
        else if(s==2) { ec[0]=2; ec[1]=6; ec[2]=0; ec[3]=7; ec[4]=3; ec[5]=9;  }
        else if(s==3) { ec[0]=0; ec[1]=6; ec[2]=2; ec[3]=5; ec[4]=1; ec[5]=4;  }

        j=0;
        for (n=0; n<6; n++)
        {
          for (i=0; i<set[setNr].anz_n; i++)
          {
            if (elem[e].nod[ec[n]]==set[setNr].node[i]) j++;
          }
        }
        if(j==6)
        {
          /* merke dir alle am node haengenden flaechen (flaeche: sum_tri3, sum_tri3+1)*/
          for (i=0; i<4; i++)
          {
            for (n=0; n<6; n++)
            {
              if((tri3_index[ elem[e].nod[ec[n]] ] 
              = (int *)realloc((int *)tri3_index[ elem[e].nod[ec[n]] ]
              , (ntri_nodes[elem[e].nod[ec[n]]]+1) *sizeof(int)))==NULL)
    	      {
                errMsg("\n\n ERROR: realloc failed for tri3_index\n" );
                return(-1);
              }
    
              tri3_index[ elem[e].nod[ec[n]] ][ ntri_nodes[elem[e].nod[ec[n]]] ]=sum_tri3+i;
              ntri_nodes[ elem[e].nod[ec[n]] ]++;
            }
          }
          if ( (ctri3 = (CTri3 *)realloc( (CTri3 *)ctri3, (sum_tri3+4) * sizeof(CTri3))) == NULL )
          {
            errMsg("\nERROR: realloc failed in makeTriFromElems() \n\n");
            return(-1);
          }

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[0]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[1]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[5]];
          sum_tri3++;

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[5]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[1]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[3]];
          sum_tri3++;

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[3]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[4]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[5]];
          sum_tri3++;

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[1]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[2]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[3]];
          sum_tri3++;
        }
      }
    }

    /* ----- HE8 --------------- */
    else if (elem[e].type==1)
    {
      /* go over all element faces and look if all nodes are also in the node-set */
      for (s=0; s<6; s++)
      {
        if(s==0)      { ec[0]=3; ec[1]=2; ec[2]=1; ec[3]=0; }
        else if(s==1) { ec[0]=0; ec[1]=1; ec[2]=5; ec[3]=4; }
        else if(s==2) { ec[0]=1; ec[1]=2; ec[2]=6; ec[3]=5; }
        else if(s==3) { ec[0]=2; ec[1]=3; ec[2]=7; ec[3]=6; }
        else if(s==4) { ec[0]=3; ec[1]=0; ec[2]=4; ec[3]=7; }
        else if(s==5) { ec[0]=4; ec[1]=5; ec[2]=6; ec[3]=7; }

        j=0;
        for (n=0; n<4; n++)
        {
          for (i=0; i<set[setNr].anz_n; i++)
          {
            if (elem[e].nod[ec[n]]==set[setNr].node[i]) j++;
          }
        }
        if(j==4)
        {
          /* merke dir alle am node haengenden flaechen (flaeche: sum_tri3, sum_tri3+1)*/
          for (i=0; i<2; i++)
          {
            for (n=0; n<4; n++)
            {
              if((tri3_index[ elem[e].nod[ec[n]] ] 
              = (int *)realloc((int *)tri3_index[ elem[e].nod[ec[n]] ]
              , (ntri_nodes[elem[e].nod[ec[n]]]+1) *sizeof(int)))==NULL)
    	      {
                errMsg("\n\n ERROR: realloc failed for tri3_index\n" );
                return(-1);
              }
    
              tri3_index[ elem[e].nod[ec[n]] ][ ntri_nodes[elem[e].nod[ec[n]]] ]=sum_tri3+i;
              ntri_nodes[ elem[e].nod[ec[n]] ]++;
            }
          }
          if ( (ctri3 = (CTri3 *)realloc( (CTri3 *)ctri3, (sum_tri3+2) * sizeof(CTri3))) == NULL )
          {
            errMsg("\nERROR: realloc failed in makeTriFromElems() \n\n");
            return(-1);
          }

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[0]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[1]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[2]];
          sum_tri3++;

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[0]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[2]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[3]];
          sum_tri3++;
        }
      }
    }


    /* ----- HE20 --------------- */
    else if (elem[e].type==4)
    {
      /* go over all element faces and look if all nodes are also in the node-set */
      for (s=0; s<6; s++)
      {
        if(s==0)      { ec[0]= 0; ec[1]=11; ec[2]= 3; ec[3]=10; ec[4]= 2; ec[5]= 9; ec[6]= 1; ec[7]= 8; }
        else if(s==1) { ec[0]= 0; ec[1]= 8; ec[2]= 1; ec[3]=13; ec[4]= 5; ec[5]=16; ec[6]= 4; ec[7]=12; }
        else if(s==2) { ec[0]= 1; ec[1]= 9; ec[2]= 2; ec[3]=14; ec[4]= 6; ec[5]=17; ec[6]= 5; ec[7]=13; }
        else if(s==3) { ec[0]= 2; ec[1]=10; ec[2]= 3; ec[3]=15; ec[4]= 7; ec[5]=18; ec[6]= 6; ec[7]=14; }
        else if(s==4) { ec[0]= 3; ec[1]=11; ec[2]= 0; ec[3]=12; ec[4]= 4; ec[5]=19; ec[6]= 7; ec[7]=15; }
        else if(s==5) { ec[0]= 4; ec[1]=16; ec[2]= 5; ec[3]=17; ec[4]= 6; ec[5]=18; ec[6]= 7; ec[7]=19; }

        j=0;
        for (n=0; n<8; n++)
        {
          for (i=0; i<set[setNr].anz_n; i++)
          {
            if (elem[e].nod[ec[n]]==set[setNr].node[i]) j++;
          }
        }
        if(j==8)
        {
          /* merke dir alle am node haengenden flaechen (flaeche: sum_tri3, sum_tri3+1)*/
          for (i=0; i<6; i++)
          {
            for (n=0; n<8; n++)
            {
              if((tri3_index[ elem[e].nod[ec[n]] ] 
              = (int *)realloc((int *)tri3_index[ elem[e].nod[ec[n]] ]
              , (ntri_nodes[elem[e].nod[ec[n]]]+1) *sizeof(int)))==NULL)
    	      {
                errMsg("\n\n ERROR: realloc failed for tri3_index\n" );
                return(-1);
              }
    
              tri3_index[ elem[e].nod[ec[n]] ][ ntri_nodes[elem[e].nod[ec[n]]] ]=sum_tri3+i;
              ntri_nodes[ elem[e].nod[ec[n]] ]++;
            }
          }
          if ( (ctri3 = (CTri3 *)realloc( (CTri3 *)ctri3, (sum_tri3+6) * sizeof(CTri3))) == NULL )
          {
            errMsg("\nERROR: realloc failed in makeTriFromElems() \n\n");
            return(-1);
          }

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[0]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[1]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[7]];
          sum_tri3++;

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[7]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[1]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[5]];
          sum_tri3++;

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[5]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[6]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[7]];
          sum_tri3++;

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[4]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[5]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[3]];
          sum_tri3++;

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[3]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[5]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[1]];
          sum_tri3++;

          ctri3[sum_tri3].elem_nr= e;
          ctri3[sum_tri3].group  = 1+s;
          ctri3[sum_tri3].mat    = elem[e].mat;
          ctri3[sum_tri3].nod[0]=elem[e].nod[ec[1]];
          ctri3[sum_tri3].nod[1]=elem[e].nod[ec[2]];
          ctri3[sum_tri3].nod[2]=elem[e].nod[ec[3]];
          sum_tri3++;
        }
      }
    }
    else
    {
      errMsg ("ERROR: independent element-type:%d is not supported\n\n",elem[e].type );
    }
  }

  *pctri3=ctri3;
  *ptri3_index=tri3_index;
  *pntri_nodes=ntri_nodes;
  return(sum_tri3);
}
