MeshKit  1.0
AF2RuleApplication.cpp
Go to the documentation of this file.
00001 #include "meshkit/AF2RuleApplication.hpp"
00002 #include "meshkit/Error.hpp"
00003 
00004 AF2RuleApplication::AF2RuleApplication(
00005     std::list<const AF2Point2D*> const & newPointsList,
00006     std::list<const AF2Polygon2D*> const & newFacesList) :
00007     numNewPoints(newPointsList.size()), numNewFaces(newFacesList.size())
00008 {
00009   // type definitions for two iterators that the constructor will use
00010   typedef std::list<const AF2Point2D*>::const_iterator ConstPntItrType;
00011   typedef std::list<const AF2Polygon2D*>::const_iterator ConstPolyItrType;
00012 
00013   // check whether there is a null-valued pointer in the list of new points
00014   for (ConstPntItrType itr = newPointsList.begin();
00015       itr != newPointsList.end(); ++itr)
00016   {
00017     if (*itr == NULL)
00018     {
00019       MeshKit::Error badArg(MeshKit::MK_BAD_INPUT);
00020       badArg.set_string("newPointsList may not contain a null pointer in AF2RuleApplication.");
00021       throw badArg;
00022     }
00023   }
00024 
00025   // check whether there is a null-valued pointer in the list of faces
00026   for (ConstPolyItrType itr = newFacesList.begin();
00027       itr != newFacesList.end(); ++itr)
00028   {
00029     if (*itr == NULL)
00030     {
00031       MeshKit::Error badArg(MeshKit::MK_BAD_INPUT);
00032       badArg.set_string(
00033           "newFacesList may not contain a null pointer in AF2RuleApplication.");
00034       throw badArg;
00035     }
00036   }
00037 
00038   // map from new points passed in to the copies of the new points
00039   std::map<const AF2Point2D*, const AF2Point2D*> pointPtrCopyMap;
00040 
00041   // now that the constructor has checked things that will cause it
00042   // to throw its own exceptions, proceed with allocating memory, etc.
00043   newPoints = new const AF2Point2D*[numNewPoints];
00044   int indx = 0;
00045   for (ConstPntItrType itr = newPointsList.begin();
00046       itr != newPointsList.end(); ++itr)
00047   {
00048     newPoints[indx] = new AF2Point2D(*(*itr));
00049     pointPtrCopyMap[*itr] = newPoints[indx];
00050     ++indx;
00051   }
00052 
00053   newFaces = new const AF2Polygon2D*[numNewFaces];
00054   indx = 0;
00055   for (ConstPolyItrType itr = newFacesList.begin();
00056       itr != newFacesList.end(); ++itr)
00057   {
00058     mapCopyPolygon(*(*itr), newFaces[indx], pointPtrCopyMap);
00059     ++indx;
00060   }
00061 }
00062 
00063 AF2RuleApplication::~AF2RuleApplication()
00064 {
00065   for (unsigned int plygnIndx = 0; plygnIndx < numNewFaces; ++plygnIndx)
00066   {
00067     delete newFaces[plygnIndx];
00068   }
00069   delete[] newFaces;
00070 
00071   for (unsigned int pntIndx = 0; pntIndx < numNewPoints; ++pntIndx)
00072   {
00073     delete newPoints[pntIndx];
00074   }
00075   delete[] newPoints;
00076 }
00077 
00078 AF2RuleApplication::AF2RuleApplication(const AF2RuleApplication & toCopy) :
00079     numNewPoints(toCopy.numNewPoints), numNewFaces(toCopy.numNewFaces)
00080 {
00081   std::map<const AF2Point2D*, const AF2Point2D*> pointPtrCopyMap;
00082 
00083   newPoints = new const AF2Point2D*[numNewPoints];
00084   for (unsigned int pntIndx = 0; pntIndx < numNewPoints; ++pntIndx)
00085   {
00086     newPoints[pntIndx] = new AF2Point2D(*(toCopy.newPoints[pntIndx]));
00087     pointPtrCopyMap[toCopy.newPoints[pntIndx]] = newPoints[pntIndx];
00088   }
00089 
00090   newFaces = new const AF2Polygon2D*[numNewFaces];
00091   for (unsigned int plygnIndx = 0; plygnIndx < numNewFaces; ++plygnIndx)
00092   {
00093     mapCopyPolygon(*(toCopy.newFaces[plygnIndx]),
00094         newFaces[plygnIndx], pointPtrCopyMap);
00095   }
00096 }
00097 
00098 AF2RuleApplication& AF2RuleApplication::operator=(
00099     const AF2RuleApplication & rhs)
00100 {
00101   // copy constructor functionality, but copy to
00102   // other parts of memory rather than to this
00103   std::map<const AF2Point2D*, const AF2Point2D*> pointPtrCopyMap;
00104 
00105   const AF2Point2D** otherNewPoints = new const AF2Point2D*[rhs.numNewPoints];
00106   for (unsigned int pntIndx = 0; pntIndx < rhs.numNewPoints; ++pntIndx)
00107   {
00108     otherNewPoints[pntIndx] = new AF2Point2D(*(rhs.newPoints[pntIndx]));
00109     pointPtrCopyMap[rhs.newPoints[pntIndx]] = otherNewPoints[pntIndx];
00110   }
00111 
00112   const AF2Polygon2D** otherNewFaces = new const AF2Polygon2D*[rhs.numNewFaces];
00113   for (unsigned int plygnIndx = 0; plygnIndx < rhs.numNewFaces; ++plygnIndx)
00114   {
00115     mapCopyPolygon(*(rhs.newFaces[plygnIndx]),
00116         otherNewFaces[plygnIndx], pointPtrCopyMap);
00117   }
00118 
00119   // destructor functionality to clean up current members of this
00120   for (unsigned int plygnIndx = 0; plygnIndx < numNewFaces; ++plygnIndx)
00121   {
00122     delete newFaces[plygnIndx];
00123   }
00124   delete[] newFaces;
00125 
00126   for (unsigned int pntIndx = 0; pntIndx < numNewPoints; ++pntIndx)
00127   {
00128     delete newPoints[pntIndx];
00129   }
00130   delete[] newPoints;
00131 
00132   // transfer ownership from other parts of memory to this object
00133   numNewPoints = rhs.numNewPoints;
00134   newPoints = otherNewPoints;
00135   otherNewPoints = NULL; // not necessary, but to be explicit
00136   numNewFaces = rhs.numNewFaces;
00137   newFaces = otherNewFaces;
00138   otherNewFaces = NULL; // not necessary, but to be explicit
00139 
00140   // return reference to this
00141   return *this;
00142 }
00143 
00144 unsigned int AF2RuleApplication::getNumNewFaces() const
00145 {
00146   return numNewFaces;
00147 }
00148 
00149 const AF2Polygon2D* AF2RuleApplication::getNewFace(
00150     unsigned int newFaceIndex) const
00151 {
00152   if (newFaceIndex >= numNewFaces)
00153   {
00154     MeshKit::Error badArg(MeshKit::MK_BAD_INPUT);
00155     badArg.set_string("newFaceIndex is out of range");
00156     throw badArg;
00157   }
00158   return newFaces[newFaceIndex];
00159 }
00160 
00161 unsigned int AF2RuleApplication::getNumNewPoints() const
00162 {
00163   return numNewPoints;
00164 }
00165 
00166 const AF2Point2D* AF2RuleApplication::getNewPoint(
00167     unsigned int newPointIndex) const
00168 {
00169   if (newPointIndex >= numNewPoints)
00170   {
00171     MeshKit::Error badArg(MeshKit::MK_BAD_INPUT);
00172     badArg.set_string("newPointIndex is out of range");
00173     throw badArg;
00174   }
00175   return newPoints[newPointIndex];
00176 }
00177 
00178 void AF2RuleApplication::mapCopyPolygon(const AF2Polygon2D & sourcePolygon,
00179     const AF2Polygon2D* & targetPolygon,
00180     const std::map<const AF2Point2D*, const AF2Point2D*> & vertexMap)
00181 {
00182   std::list<const AF2Point2D*> mapCopyVertices;
00183   for (unsigned int vIndx = 0; vIndx < sourcePolygon.getNumVertices(); ++vIndx)
00184   {
00185     const AF2Point2D* sourcePolyVertex = sourcePolygon.getVertex(vIndx);
00186     typedef std::map<const AF2Point2D*, const AF2Point2D*>::const_iterator
00187         ConstItrType;
00188     ConstItrType mappedVertex = vertexMap.find(sourcePolyVertex);
00189     if (mappedVertex == vertexMap.end())
00190     {
00191       // The vertex is not a new vertex from rule application.
00192       // Instead it is from the neighborhood and does not need to be mapped
00193       mapCopyVertices.push_back(sourcePolyVertex);
00194     }
00195     else
00196     {
00197       // The vertex is a vertex from the rule application and
00198       // this code maps it to the appropriate pointer in the object
00199       // that is being constructed.
00200       mapCopyVertices.push_back(mappedVertex->second);
00201     }
00202   }
00203 
00204   targetPolygon = new AF2Polygon2D(mapCopyVertices);
00205 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines