//
// File: shape.cc
//
// (C) 2000-2008 Helmut Cantzler
//
// Licensed under the terms of the Lesser General Public License.
//
#include <set>
#include <algorithm>
#include <string.h>
#include "shape.h"
Shape::Shape(list<Triangle*> *tri, list<Vertex*> *ver, const char *textName)
{
triangles = tri; edges = NULL; vertices = ver;
textureId=-1;
setTextureName(textName);
}
Shape::Shape(list<Triangle*> *tri, const char *textName)
{
triangles = tri; edges = NULL; vertices = new list<Vertex*>;
textureId=-1;
setTextureName(textName);
//create the vertex list from the triangle list
set<Vertex*> vertexSet;
list<Triangle*>::iterator it;
int i;
for (it=triangles->begin(); it != triangles->end(); it++)
for (i = 0; i < 3; i++)
// avoid double vertices
if (vertexSet.find( (*it)->vertices[i] ) == vertexSet.end())
{
vertices->push_back((*it)->vertices[i]);
vertexSet.insert((*it)->vertices[i]);
}
}
Shape::Shape(list<Edge*> *edg, list<Vertex*> *ver)
{
triangles = NULL; edges = edg; vertices = ver;
textureId=-1;
setTextureName(NULL);
}
Shape::Shape(list<Vertex*> *ver)
{
triangles = NULL; edges = NULL; vertices = ver;
textureId=-1;
setTextureName(NULL);
}
Shape::~Shape()
{
delete vertices;
delete edges;
delete triangles;
delete textureName;
}
int Shape::numberOfVertices(void) const
{
return vertices != NULL ? (int) vertices->size() : 0;
}
int Shape::numberOfEdges(void) const
{
return edges != NULL ? (int) edges->size() : 0;
}
int Shape::numberOfTriangles(void) const
{
return triangles != NULL ? (int) triangles->size() : 0;
}
int Shape::contains(list<Vertex*> *ver) const
{
list<Vertex*>::iterator iv;
for (iv=ver->begin(); iv != ver->end(); iv++)
if ( find( vertices->begin(), vertices->end(), *iv ) == vertices->end() )
return FALSE;
return TRUE;
}
void Shape::setTextureName(const char *textName)
{
if (textName == NULL || strlen(textName) == 0)
textureName = NULL;
else
{
textureName = new char[strlen(textName)+1];
strcpy(textureName, textName);
}
}
void Shape::removeVertex(Vertex *ver)
{
if (vertices != NULL)
vertices->remove(ver);
}
void Shape::changeVertex(const Vertex *oldVer, Vertex *newVer)
{
if (vertices != NULL)
{
list<Vertex*>::iterator iv;
iv = find( vertices->begin(), vertices->end(), oldVer );
if (iv != vertices->end())
*iv = newVer;
}
}