#include "Polygon3.h" #include "Mesh.h" Polygon3::Polygon3() {} Polygon3::Polygon3(const VertexContainer &vertices): mVertices(vertices) {} Polygon3::Polygon3(Face *face, Mesh *parent) { VertexIndexContainer::const_iterator it; for (it = face->mVertexIndices.begin(); it != face->mVertexIndices.end(); ++ it) { mVertices.push_back(parent->mVertices[*it]); } } Plane3 Polygon3::GetSupportingPlane() { return Plane3(mVertices[0], mVertices[1], mVertices[2]); } void Polygon3::DeletePolygons(PolygonQueue *polys) { // don't need to store polygon information = delete polygons while(!polys->empty()) { Polygon3 *poly = polys->front(); polys->pop(); DEL_PTR(poly); } } void Polygon3::Split(Plane3 *partition, Polygon3 *front, Polygon3 *back, int &splits) { splits = 0; Vector3 ptA = mVertices[mVertices.size() - 1];; int sideA = partition->Side(ptA); VertexContainer::const_iterator it; // find line - plane intersections for (it = mVertices.begin(); it != mVertices.end(); ++ it) { Vector3 ptB = (*it); int sideB = partition->Side(ptB); // vertices on different sides => split if ((sideA != 0) && (sideB != 0) && (sideA != sideB)) { Vector3 v = ptB - ptA; // line from A to B float dv = DotProd(partition->mNormal, v); float t = 0; if (dv) { t = - partition->Distance(ptA) / dv; } ++ splits; } if (sideB >= 0) { back->mVertices.push_back(ptB); } else if (sideB <= 0) { front->mVertices.push_back(ptB); } ptA = ptB; sideA = sideB; } } int Polygon3::Side(Plane3 *plane) { VertexContainer::const_iterator it; bool onFrontSide = false; bool onBackSide = false; // find line - plane intersections for (it = mVertices.begin(); it != mVertices.end(); ++ it) { int side = plane->Side(*it); if (side > 0) { onFrontSide = true; } else if (side < 0) { onBackSide = true; } if (onFrontSide && onBackSide) // split { return SPLIT; } } if (onBackSide) { return BACK_SIDE; } else if (onFrontSide) { return FRONT_SIDE; } return COINCIDENT; }