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

#include "gmafConfig.h"

#include "DataStructure/gmafPolyhedron.h"
#include "gmafQuadMap.h"
#include "gmafQuad.h"
#include "gmafQuadVertex.h"

#include "DataStructure/gmafFacetIterator.h"
#include "DataStructure/gmafVertexIterator.h"
#include "gmafFacetAroundVertexIterator.h"


gmafQuadMap::gmafQuadMap(gmafPolyhedron* p) 
  : gmafDataStructure("QuadMap") {
  mpPolyhedron = p;

  // create the list of the quads
  for (gmafFacetIterator fitr(mpPolyhedron); fitr.valid(); fitr.next()) {
    gmafQuad* curq = new gmafQuad(&(fitr.current()));
    mQuadList.push_back(curq);
  }

  // create the list of the verteices -- including ordinary and 
  // extraordinary vertices (use interator to simulate the 
  // extraordinary-only list)
  for (gmafVertexIterator vitr(mpPolyhedron); vitr.valid(); vitr.next()) {
    gmafQuadVertex* curqv = new gmafQuadVertex();
    Point p3d = (vitr.current()).point();
    //add the neighbors of this vertex
    for (gmafFacetAroundVertexIterator favitr(&(vitr.current())); 
	 favitr.valid(); favitr.next()) {
      int qidx = (favitr.current()).getIdx();
      int c = mQuadList[qidx]->whichCorner(p3d);
      assert(c != -1);
      curqv->addNeighbor(qidx, c);
    }
    mVertexList.push_back(curqv);
  }
}


gmafQuadMap::~gmafQuadMap() {
  for (int i = 0; i < mVertexList.size(); i++)
    delete mVertexList[i];
  for (int i = 0; i < mQuadList.size(); i++)
    delete mQuadList[i];
}
