MeshKit  1.0
AF2Polygon2D.cpp
Go to the documentation of this file.
00001 #include "meshkit/AF2Polygon2D.hpp"
00002 #include "meshkit/Error.hpp"
00003 
00004 AF2Polygon2D::AF2Polygon2D(
00005     std::list<const AF2Point2D*> const & polygonVertices) :
00006     numVertices(polygonVertices.size())
00007 {
00008   typedef std::list<const AF2Point2D*>::const_iterator ItrType;
00009 
00010   // check for the exceptional case of a null pointer in the list of vertices
00011   for (ItrType itr = polygonVertices.begin();
00012       itr != polygonVertices.end(); ++itr)
00013   {
00014     if (*itr == NULL)
00015     {
00016       MeshKit::Error badArg(MeshKit::MK_BAD_INPUT);
00017       badArg.set_string(
00018           "polygonVertices may not contain a null pointer in AF2Polygon2D.");
00019       throw badArg;
00020     }
00021   }
00022 
00023   // allocate memory and copy the vertex pointers
00024   vertices = new const AF2Point2D*[numVertices];
00025   int indx = 0;
00026   for (ItrType itr = polygonVertices.begin();
00027       itr != polygonVertices.end(); ++itr)
00028   {
00029     vertices[indx] = *itr;
00030     ++indx;
00031   }
00032 }
00033 
00034 AF2Polygon2D::~AF2Polygon2D()
00035 {
00036   delete[] vertices;
00037 }
00038 
00039 AF2Polygon2D::AF2Polygon2D(const AF2Polygon2D & toCopy) :
00040     numVertices(toCopy.numVertices)
00041 {
00042   vertices = new const AF2Point2D*[numVertices];
00043   for (unsigned int indx = 0; indx < numVertices; ++indx)
00044   {
00045     vertices[indx] = toCopy.vertices[indx];
00046   }
00047 }
00048 
00049 AF2Polygon2D& AF2Polygon2D::operator=(const AF2Polygon2D & rhs)
00050 {
00051   // directly copy the number of vertices
00052   numVertices = rhs.numVertices;
00053 
00054   // allocate new array in temporary location
00055   // and copy the pointers into the temporary array
00056   const AF2Point2D** tempVertices = new const AF2Point2D*[numVertices];
00057   for (unsigned int indx = 0; indx < numVertices; ++indx)
00058   {
00059     tempVertices[indx] = rhs.vertices[indx];
00060   }
00061 
00062   // delete what used to be held by the array of vertices
00063   // (may be null if default constructor was used)
00064   delete[] vertices;
00065 
00066   // transfer ownership from the temporary array to a member of this object
00067   vertices = tempVertices;
00068   tempVertices = NULL; // not necessary, but to be explicit
00069 
00070   return *this;
00071 }
00072 
00073 unsigned int AF2Polygon2D::getNumVertices() const
00074 {
00075   return numVertices;
00076 }
00077 
00078 const AF2Point2D* AF2Polygon2D::getVertex(unsigned int vtxNum) const
00079 {
00080   if (vtxNum >= numVertices)
00081   {
00082     MeshKit::Error badArg(MeshKit::MK_BAD_INPUT);
00083     badArg.set_string("The vertex index is too large.");
00084     throw badArg;
00085   }
00086   return vertices[vtxNum];
00087 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines