#include "Polygon3.h" #include "Mesh.h" #include "AxisAlignedBox3.h" #include "Ray.h" #include "Triangle3.h" Polygon3::Polygon3(): mMaterial(NULL), mParent(NULL), mPiercingRays(0) {} Polygon3::Polygon3(const VertexContainer &vertices): mVertices(vertices), mMaterial(NULL), mParent(NULL), mPiercingRays(0) {} Polygon3::Polygon3(MeshInstance *parent): mMaterial(NULL), mParent(parent), mPiercingRays(0) {} Polygon3::Polygon3(Face *face, Mesh *parentMesh): mMaterial(NULL), mParent(NULL), mPiercingRays(0) { VertexIndexContainer::iterator it = face->mVertexIndices.begin(); for (; it != face->mVertexIndices.end(); ++it) { mVertices.push_back(parentMesh->mVertices[*it]); mMaterial = parentMesh->mMaterial; } } Plane3 Polygon3::GetSupportingPlane() const { return Plane3(mVertices[0], mVertices[1], mVertices[2]); } Vector3 Polygon3::GetNormal() const { return Normalize(CrossProd(mVertices[2] - mVertices[1], mVertices[0] - mVertices[1])); } void Polygon3::Split(const Plane3 &partition, Polygon3 &front, Polygon3 &back, const float epsilon) { Vector3 ptA = mVertices.back(); int sideA = partition.Side(ptA, epsilon); VertexContainer::const_iterator it; Vector3 lastSplit; bool foundSplit = false; //-- find line - plane intersections for (it = mVertices.begin(); it != mVertices.end(); ++ it) { Vector3 ptB = *it; int sideB = partition.Side(ptB, epsilon); // vertices on different sides => split if (sideB > 0) { if (sideA < 0) { //-- plane - line intersection Vector3 splitPt = partition.FindIntersection(ptA, ptB); // test if split point not too close to previous split point if (!foundSplit || (!EpsilonEqualV3(splitPt, lastSplit, epsilon))) { // add vertex to both polygons front.mVertices.push_back(splitPt); back.mVertices.push_back(splitPt); lastSplit = splitPt; foundSplit = true; } } front.mVertices.push_back(ptB); } else if (sideB < 0) { if (sideA > 0) { //-- plane - line intersection Vector3 splitPt = partition.FindIntersection(ptA, ptB); // test if split point not too close to other split point // test if split point not too close to previous split point if (!foundSplit || (!EpsilonEqualV3(splitPt, lastSplit, epsilon))) { // add vertex to both polygons front.mVertices.push_back(splitPt); back.mVertices.push_back(splitPt); lastSplit = splitPt; foundSplit = true; } } back.mVertices.push_back(ptB); } else { // vertex on plane => add vertex to both polygons front.mVertices.push_back(ptB); back.mVertices.push_back(ptB); } ptA = ptB; sideA = sideB; } } float Polygon3::GetArea() const { Vector3 v = CrossProd(mVertices.back(), mVertices.front()); for (int i=0; i < mVertices.size() - 1; ++i) v += CrossProd(mVertices[i], mVertices[i+1]); return 0.5f * fabs(DotProd(GetNormal(), v)); } int Polygon3::Side(const Plane3 &plane, const float epsilon) const { int classification = ClassifyPlane(plane, epsilon); if (classification == BACK_SIDE) return -1; else if (classification == FRONT_SIDE) return 1; // plane splits polygon return 0; } int Polygon3::ClassifyPlane(const Plane3 &plane, const float epsilon) const { VertexContainer::const_iterator it; bool onFrontSide = false; bool onBackSide = false; int count = 0; // find possible line-plane intersections for (it = mVertices.begin(); it != mVertices.end(); ++ it) { const int side = plane.Side(*it, epsilon); if (side > 0) onFrontSide = true; else if (side < 0) onBackSide = true; //TODO: check if split goes through vertex if (onFrontSide && onBackSide) // split { return SPLIT; } // 3 vertices enough to decide coincident else if (((++ count) >= 3) && !onFrontSide && !onBackSide) { return COINCIDENT; } } if (onBackSide) { return BACK_SIDE; } else if (onFrontSide) { return FRONT_SIDE; } return COINCIDENT; // plane and polygon are coincident } /*int Polygon3::ClassifyPlane(const Plane3 &plane) const { return ClassifyPlane(plane, Limits::Small); }*/ Vector3 Polygon3::Center() const { int i; Vector3 sum = mVertices[0]; for (i=1; i < mVertices.size(); i++) sum += mVertices[i]; return sum/(float)i; } void Polygon3::Scale(const float scale) { int i; Vector3 center = Center(); for (i=0; i < mVertices.size(); i++) { mVertices[i] = center + scale*(mVertices[i] - center); } } bool Polygon3::Valid(const float epsilon) const { if (mVertices.size() < 3) return false; #if 1 // check if area exceeds certain size if (AREA_LIMIT > GetArea()) { //Debug << "area too small: " << GetArea() << endl; return false; } #else Vector3 vtx = mVertices.back(); VertexContainer::const_iterator it, it_end = mVertices.end(); for (it = mVertices.begin(); it != it_end; ++it) { if (!(EpsilonEqual(vtx, *it))) { //Debug << "Malformed vertices:\n" << *this << endl; return false; } vtx = *it; } #endif return true; } void Polygon3::IncludeInBox(const PolygonContainer &polys, AxisAlignedBox3 &box) { PolygonContainer::const_iterator it, it_end = polys.end(); for (it = polys.begin(); it != it_end; ++ it) box.Include(*(*it)); } // int_lineseg returns 1 if the given line segment intersects a 2D // ray travelling in the positive X direction. This is used in the // Jordan curve computation for polygon intersection. inline int int_lineseg(float px, float py, float u1, float v1, float u2, float v2) { float t; float ydiff; u1 -= px; u2 -= px; // translate line v1 -= py; v2 -= py; if ((v1 > 0 && v2 > 0) || (v1 < 0 && v2 < 0) || (u1 < 0 && u2 < 0)) return 0; if (u1 > 0 && u2 > 0) return 1; ydiff = v2 - v1; if (fabs(ydiff) < Limits::Small) { // denominator near 0 if (((fabs(v1) > Limits::Small) || (u1 > 0) || (u2 > 0))) return 0; return 1; } t = -v1 / ydiff; // Compute parameter return (u1 + t * (u2 - u1)) > 0; } int Polygon3::CastRay(const Ray &ray, float &t, const float nearestT) { Plane3 plane = GetSupportingPlane(); float dot = DotProd(plane.mNormal, ray.GetDir()); // Watch for near-zero denominator // ONLY single sided polygons!!!!! if (dot > -Limits::Small) // if (fabs(dot) < Limits::Small) return Ray::NO_INTERSECTION; t = (-plane.mD - DotProd(plane.mNormal, ray.GetLoc())) / dot; if (t <= Limits::Small) return Ray::INTERSECTION_OUT_OF_LIMITS; if (t >= nearestT) { return Ray::INTERSECTION_OUT_OF_LIMITS; // no intersection was found } int count = 0; float u, v, u1, v1, u2, v2; int i; int paxis = plane.mNormal.DrivingAxis(); // Project the intersection point onto the coordinate plane // specified by which. ray.Extrap(t).ExtractVerts(&u, &v, paxis); int size = (int)mVertices.size(); mVertices.back().ExtractVerts(&u1, &v1, paxis ); if (0 && size <= 4) { // assume a convex face for (i = 0; i < size; i++) { mVertices[i].ExtractVerts(&u2, &v2, paxis); // line u1, v1, u2, v2 if ((v2 - v1)*(u1 - u) > (u2 - u1)*(v1 - v)) return Ray::NO_INTERSECTION; u1 = u2; v1 = v2; } return Ray::INTERSECTION; } // We're stuck with the Jordan curve computation. Count number // of intersections between the line segments the polygon comprises // with a ray originating at the point of intersection and // travelling in the positive X direction. for (i = 0; i < size; i++) { mVertices[i].ExtractVerts(&u2, &v2, paxis); count += (int_lineseg(u, v, u1, v1, u2, v2) != 0); u1 = u2; v1 = v2; } // We hit polygon if number of intersections is odd. return (count & 1) ? Ray::INTERSECTION : Ray::NO_INTERSECTION; } void Polygon3::InheritRays(Polygon3 &front_piece, Polygon3 &back_piece) const { if (mPiercingRays.empty()) return; RayContainer::const_iterator it, it_end = mPiercingRays.end(); for (it = mPiercingRays.begin(); it != it_end; ++ it) { switch((*it)->GetId()) { case Ray::BACK: back_piece.mPiercingRays.push_back(*it); break; case Ray::FRONT: front_piece.mPiercingRays.push_back(*it); break; case Ray::FRONT_BACK: back_piece.mPiercingRays.push_back(*it); break; case Ray::BACK_FRONT: front_piece.mPiercingRays.push_back(*it); break; default: break; } } } int Polygon3::ClassifyPlane(const PolygonContainer &polys, const Plane3 &plane, const float epsilon) { PolygonContainer::const_iterator it; bool onFrontSide = false; bool onBackSide = false; // find intersections for (it = polys.begin(); it != polys.end(); ++ it) { const int cf = (*it)->ClassifyPlane(plane, epsilon); if (cf == FRONT_SIDE) onFrontSide = true; else if (cf == BACK_SIDE) onBackSide = true; if ((cf == SPLIT) || (cf == COINCIDENT) || (onFrontSide && onBackSide)) { return SPLIT; } } if (onBackSide) { return BACK_SIDE; } else if (onFrontSide) { return FRONT_SIDE; } return SPLIT; } int Polygon3::ParentObjectsSize(const PolygonContainer &polys) { int count = 0; PolygonContainer::const_iterator it, it_end = polys.end(); MeshInstance::NewMail(); for (it = polys.begin(); it != it_end; ++ it) { if ((*it)->mParent && !(*it)->mParent->Mailed()) { ++ count; (*it)->mParent->Mail(); } } return count; } float Polygon3::GetArea(const PolygonContainer &cell) { float area = 0; PolygonContainer::const_iterator pit; for (pit = cell.begin(); pit != cell.end(); ++ pit) { if ((*pit)->mVertices.size() < 3) Debug << "ERROR" << endl; area += (*pit)->GetArea(); } return area; } Polygon3 *Polygon3::CreateReversePolygon() const { Polygon3 *revPoly = new Polygon3(); VertexContainer::const_reverse_iterator rit, rit_end = mVertices.rend(); for(rit = mVertices.rbegin(); rit != rit_end; ++ rit) revPoly->mVertices.push_back(*rit); return revPoly; } void Polygon3::Triangulate(vector &triangles) { int i = 1; int j = 0; int k = (int)mVertices.size() - 1; int count = 0; while (i < k) { triangles.push_back(Triangle3(mVertices[i], mVertices[j], mVertices[k])); if ((count ++) % 2) j = i ++; else j = k --; } } void Polygon3::Triangulate(VertexIndexContainer &indices) { int i = 1; int j = 0; int k = (int)mVertices.size() - 1; int count = 0; while (i < k) { indices.push_back(k); indices.push_back(j); indices.push_back(i); if ((count ++) % 2) j = i ++; else j = k --; } } void Polygon3::AddToMesh(Mesh &mesh) { const int n = (int)mesh.mVertices.size(); //-- add the vertices VertexContainer::const_iterator vit, vit_end = mVertices.end(); for (vit = mVertices.begin(); vit != vit_end; ++ vit) { mesh.mVertices.push_back(*vit); } // one quad => no triangulation necessary if ((int)mVertices.size() == 4) { mesh.AddFace(new Face(n, n + 1, n + 2, n + 3)); } else { VertexIndexContainer indices; Triangulate(indices); // add indices of triangle strip for (int i = 0; i < (int)indices.size(); i += 3) { Face *face = new Face(n + indices[i], n + indices[i + 1], n + indices[i + 2]); mesh.AddFace(face); } } }