// Cutmesh - Copyright (C) 2010-2018 T. Mouton, E. Bechet 
//
// See the LICENSE file for license information and contributions.
// bugs and problems to <thibaud.mouton@gmail.com>.

#include "topo.h"

#ifndef CUTMESH_ENABLE_IBREP


void Topo::ImportIbrep(IBrep *ib)
{
}

#else

#include "IBrep.h"

void Topo::ImportIbrep(IBrep *ib)
{
  _brm = new BRepModel;
  // lecture des sommets
  int nbvx=ib->GetNumVertices();
  tprintf("--> %4d topovertex\n", nbvx);
  int i=0;
  for (IBrep::iterator_vertices it = ib->begin_vertices(); it!=ib->end_vertices() ; ++it)
  {
    std::vector<int> surf;
    int idvx=it->num;
    std::cout << " IVertex #" << idvx << " Name:" <<  it->Name() << std::endl;  
    double x=it->XYZ()[0], y=it->XYZ()[1], z=it->XYZ()[2];
//    std::cout << "vertex " << idvx << " " << x << " " << y << " " << z << std::endl;
    int nbf=it->GetNumFaces();
    assert(nbf>=3);
    // TODO Check if this is correct (take any 3 of the faces incident to the vertex. There may be more !)
    for (int j = 0; j < 3; j++)
    {
      int idsurf=it->GetFace(j)->num;  
//      std::cout << "connecte a la face " << idsurf ;
      idsurf = get_entity_manager()->get_lookup_face_id(idsurf);
//      std::cout << "(" << idsurf << ")" << std::endl;
      surf.push_back(idsurf);
    }
    std::sort(surf.begin(), surf.end());
    BRepEntity *be = new BRepEntity(idvx);
    for (int j = 0; j < 3; j++)
      be->add_surf(surf[j]);
    _brm->add_vertex(be);
    LVertex *v = new LVertex(x, y, z);
    _brep_vertex_coord.insert(std::pair<BRepEntity*, LVertex*>(be, v));
  }

  // lecture des edges
  int nbedge=ib->GetNumEdges();
  tprintf("--> %4d topoedges\n", nbedge);
  for (IBrep::iterator_edges it = ib->begin_edges(); it!=ib->end_edges(); ++it)
  {
    int idedge=it->num;
    std::cout << " IEdge #" << idedge << " Name:" <<  it->Name() << std::endl;  
    std::pair<int, int> idsurf;
    int nco=it->GetNumCoEdges();
    assert(nco==2); // manifold surface (one edge -> 2 faces connectes)
    int s[2];
    for (int f=0;f<nco;++f)
    {
      IBrep::iterator_coedges coe=it->GetCoEdge(f);
      s[f]=coe->GetFace()->num;
    }
    idsurf.first=s[0];
    idsurf.second=s[1];
    idsurf.first = get_entity_manager()->get_lookup_face_id(idsurf.first);
    idsurf.second = get_entity_manager()->get_lookup_face_id(idsurf.second);

    if (idsurf.first > idsurf.second)
    {
      int tmp = idsurf.first;
      idsurf.first = idsurf.second;
      idsurf.second = tmp;
    }
    BRepEntity *be = new BRepEntity(idedge);
    be->add_surf(idsurf.first);
    be->add_surf(idsurf.second);
    _brm->add_edge(be);
    std::pair<int, int> idendv;
    IBrep::iterator_covertices fcov=it->GetFromCoVertex();
    IBrep::iterator_covertices tcov=it->GetToCoVertex();
    idendv.first=fcov->GetVertex()->num;
    idendv.second=tcov->GetVertex()->num;
    be->add_adj(idendv.first);
    be->add_adj(idendv.second);
  }

  // lecture des faces
  int nbface=ib->GetNumFaces();
  tprintf("--> %4d topofaces\n", nbface);
  for (IBrep::iterator_faces it = ib->begin_faces(); it!=ib->end_faces() ; ++it)
  {
    int idface=it->num;
    std::cout << " IFace #" << idface << " Name:" <<  it->Name() << std::endl;  
    idface = get_entity_manager()->get_lookup_face_id(idface);
    BRepEntity *be = new BRepEntity(it->num);

    int nloops= it->GetNumLoops();
    for (int i=0;i<nloops;++i)
    {
      ILoopPtr lo;
      it->GetLoop(i,lo);
      for (int j=0;j<lo.size();++j)
      {
        int idedgeface=lo[j]->GetEdge()->num;
        be->add_adj(idedgeface);
      }
    }
    be->add_surf(idface);
    _brm->add_face(be);
  }
// lecture volumes
  for (IBrep::iterator_volumes it = ib->begin_volumes(); it!=ib->end_volumes() ; ++it)
  {
    int idvol=it->num;
    std::cout << " IVolume #" << idvol << " Name:" <<  it->Name() << std::endl;  

    BRepEntity *be = new BRepEntity(it->num);

    int nshells= it->GetNumShells();
    for (int i=0;i<nshells;++i)
    {
      IShellPtr sh;
      it->GetShell(i,sh);
      for (int j=0;j<sh.size();++j)
      {
        int idfacevol=sh[j]->GetFace()->num;
        be->add_adj(idfacevol);
      }
    }
    /// to be continued
//    be->add_surf(idface);
    _brm->add_volume(be);
  }
}

#endif
