void Scene::Extrude (int n, float* x, float* y, float depth, 
		     const Trafo& P, const ColorB* color)
{
  int n1 = n-1, n2 = 2*n-2;
     
  // add front and back polygonal with n vertices
  // the back polygonal has reverse orientation

  Facet &front = AddFacet(n),
        &back  = AddFacet(n);
    
  if (color) {
    front.SetColor(*color);
    back.SetColor(*color);
  }

  // create vertices and link them to facets
  int i;
  for (i = 0; i < n; i++) { 
    front(i) = &AddVertex( P * Vertex(x[i],y[i],0.0) );
    back(i)  = &AddVertex( P * Vertex(x[n1-i],y[n1-i],-depth) );
    if (color) {
      front[i].SetColor(*color);
      back[i].SetColor(*color);
    }
  }

  // create n facets for the sides and link the vertices
  for (i = 0; i < n; i++) { 
    Facet &side = AddFacet(4, front(i), back(n1-i),
			   back((n2-i)%n), front((i+1)%n) );
    if (color) side.SetColor(*color);
  }
}
bool CMesh::LoadBinarySTL(std::string filename)
{
    FILE *fp;

#ifdef WIN32
    fopen_s(&fp, filename.c_str(), "rb"); //secure version. preferred on windows platforms...
#else
    fp = fopen(filename.c_str(), "rb");
#endif


    if(fp == NULL) return false;

    int facenum;
    fseek(fp, STL_LABEL_SIZE, SEEK_SET);
    fread(&facenum, sizeof(int), 1, fp);

    Clear();

    // For each triangle read the normal, the three coords and a short set to zero
    float N[3];
    float P[9];
    short attr;

    for(int i=0;i<facenum;++i) {
        fread(&N,3*sizeof(float),1,fp); //We end up throwing this out and recalculating because... we don't trust it!!!
        fread(&P,3*sizeof(float),3,fp);
        fread(&attr,sizeof(short),1,fp);
        AddFacet(Vec3D<>(P[0], P[1], P[2]), Vec3D<>(P[3], P[4], P[5]), Vec3D<>(P[6], P[7], P[8]));
    }
    fclose(fp);

    CalcFaceNormals();

    return true;
}
void CMesh::AddFacet(const Vec3D<>& v1, const Vec3D<>& v2, const Vec3D<>& v3, bool QuickAdd) //adds a facet, checks vertex list for existing vertices...
{
    AddFacet(v1, v2, v3, CColor(0.5, 0.5, 0.5, 1.0), CColor(0.5, 0.5, 0.5, 1.0), CColor(0.5, 0.5, 0.5, 1.0));
}
bool CMesh::LoadAsciiSTL(std::string filename)
{
    FILE *fp;

#ifdef WIN32
    fopen_s(&fp, filename.c_str(), "r"); //secure version. preferred on windows platforms...
#else
    fp = fopen(filename.c_str(), "r");
#endif

    if(fp == NULL) return false;

    long currentPos = ftell(fp);
    fseek(fp,0L,SEEK_END);
    //	long fileLen = ftell(fp);
    fseek(fp,currentPos,SEEK_SET);

    Clear();

    /* Skip the first line of the file */
    while(getc(fp) != '\n') { }

    float N[3];
    float P[9];
    int cnt=0;
    int lineCnt=0;
    int ret;
    /* Read a single facet from an ASCII .STL file */
    while(!feof(fp)){
        ret=fscanf(fp, "%*s %*s %f %f %f\n", &N[0], &N[1], &N[2]); // --> "facet normal 0 0 0" (We throw this out and recalculate based on vertices)
        if(ret!=3){
            // we could be in the case of a multiple solid object, where after a endfaced instead of another facet we have to skip two lines:
            //     endloop
            //	 endfacet
            //endsolid     <- continue on ret==0 will skip this line
            //solid ascii  <- and this one.
            //   facet normal 0.000000e+000 7.700727e-001 -6.379562e-001
            lineCnt++;
            continue;
        }
        ret=fscanf(fp, "%*s %*s"); // --> "outer loop"
        ret=fscanf(fp, "%*s %f %f %f\n", &P[0],  &P[1],  &P[2]); // --> "vertex x y z"
        if(ret!=3) return false;
        ret=fscanf(fp, "%*s %f %f %f\n", &P[3],  &P[4],  &P[5]); // --> "vertex x y z"
        if(ret!=3) return false;
        ret=fscanf(fp, "%*s %f %f %f\n", &P[6],  &P[7],  &P[8]); // --> "vertex x y z"
        if(ret!=3) return false;
        ret=fscanf(fp, "%*s"); // --> "endloop"
        ret=fscanf(fp, "%*s"); // --> "endfacet"
        lineCnt+=7;
        if(feof(fp)) break;

        AddFacet(Vec3D<>(P[0], P[1], P[2]), Vec3D<>(P[3], P[4], P[5]), Vec3D<>(P[6], P[7], P[8]));

    }
    fclose(fp);

    CalcFaceNormals();

    return true;
}