/****************************************************************************
** GMAF - The Geometry Modeling Algorithms Framework
**
** Created : 03112000
**
** Copyright (C) 2000 by Le-Jeng Shiue.  All rights reserved.
**
****************************************************************************/
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <assert.h>

#include "gmafDataStructureManager.h"

#include "DataStructure/gmafDataStructure.h"

#include "DataStructure/gmafPolyhedron.h"

#include "Algorithm/CCSubdivision/gmafQuadMap.h"
#include "Algorithm/PCCM/gmafPCCMQuadMap.h"

gmafDataStructureManager::gmafDataStructureManager() 
  : mpMesh(NULL), mpQuadMap(NULL), mpPCCMQuadMap(NULL) {
  mCenterX = mCenterY = mCenterZ = 0;
  mMinX = mMaxX = mMinY = mMaxY = mMinZ = mMaxZ = 0;
}

gmafDataStructureManager::~gmafDataStructureManager() {
  cleanAll();
}

gmafPolyhedron* gmafDataStructureManager::importMesh(std::ifstream& mesh) {
  cleanAll();
  mpMesh = new gmafPolyhedron();
  mpMesh->import(mesh);
  //mpMesh->createVisualObject();

  Point c = mpMesh->getCenter();
  mCenterX = c.x();
  mCenterY = c.y();
  mCenterZ = c.z();

  CGAL::Bbox_3* bbox = mpMesh->getBbox();
  mMinX = bbox->xmin();
  mMaxX = bbox->xmax();
  mMinY = bbox->ymin();
  mMaxY = bbox->ymax();
  mMinZ = bbox->zmin();
  mMaxZ = bbox->zmax();

  return mpMesh;
}

gmafQuadMap* gmafDataStructureManager::createQuadMap() {
  assert(mpMesh != NULL);
  delete mpQuadMap;
  mpQuadMap = new gmafQuadMap(mpMesh);
  return mpQuadMap;
}

gmafPCCMQuadMap* gmafDataStructureManager::createPCCMQuadMap() {
  assert(mpQuadMap != NULL);
  delete mpPCCMQuadMap;
  mpPCCMQuadMap = new gmafPCCMQuadMap(mpQuadMap);
  return mpPCCMQuadMap;
}

void gmafDataStructureManager::cleanAll() {
  delete mpMesh; mpMesh = NULL;
  delete mpQuadMap; mpQuadMap = NULL;
  delete mpPCCMQuadMap; mpPCCMQuadMap = NULL;
}
