#include "IntelliHelper.h" #include #include #include #define pi 3.14159265359 std::vector IntelliHelper::calculateTriangles(std::vector polyPoints){ // helper for managing the triangle vertices and their state struct TriangleHelper { QPoint vertex; float interiorAngle; int index; bool isTip; }; // calculates the inner angle of 'point' auto calculateInner = [](QPoint& point, QPoint& prev, QPoint& post){ QPoint AP(point.x()-prev.x(), point.y()-prev.y()); QPoint BP(point.x()-post.x(), point.y()-post.y()); float topSclar = AP.x()*BP.x()+AP.y()*BP.y(); float absolute = sqrt(pow(AP.x(),2.)+pow(AP.y(),2.))*sqrt(pow(BP.x(),2.)+pow(BP.y(),2.)); return acos(topSclar/absolute); }; // gets the first element of vec for which element.isTip == true holds auto getTip= [](const std::vector& vec){ size_t min = 0; for(size_t i=0; i=0 ? (index-1) : (length-1); }; // get the vertex Index after index in relation to the container lenght auto getPost = [](int index, int length){ return (index+1)%length; }; // return if the vertex is a tip auto isTip = [](float angle){ return static_cast(angle)<(pi/2.); }; std::vector Vertices; std::vector Triangles; // set up all vertices and calculate intirior angle for(int i=0; i(polyPoints.size()); i++) { TriangleHelper helper; int prev = getPrev(i, static_cast(polyPoints.size())); int post = getPost(i, static_cast(polyPoints.size())); helper.vertex = polyPoints[static_cast(i)]; helper.index = i; helper.interiorAngle = calculateInner(polyPoints[static_cast(i)], polyPoints[static_cast(prev)], polyPoints[static_cast(post)]); helper.isTip = isTip(helper.interiorAngle); Vertices.push_back(helper); } // search triangles based on the intirior angles of each vertey while(Triangles.size() != polyPoints.size()-2) { Triangle tri; TriangleHelper smallest = getTip(Vertices); int prev = getPrev(smallest.index, static_cast(Vertices.size())); int post = getPost(smallest.index, static_cast(Vertices.size())); // set triangle and push it tri.A = Vertices[static_cast(prev)].vertex; tri.B = Vertices[static_cast(smallest.index)].vertex; tri.C = Vertices[static_cast(post)].vertex; Triangles.push_back(tri); // update Vertice array Vertices.erase(Vertices.begin()+smallest.index); for(size_t i=static_cast(smallest.index); i(Vertices.size())); int postOfPrev = getPost(prev, static_cast(Vertices.size())); int prevOfPost = getPrev(post, static_cast(Vertices.size())); int postOfPost = getPost(post, static_cast(Vertices.size())); // update vertices with interior angles // updtae prev Vertices[static_cast(prev)].interiorAngle = calculateInner(Vertices[static_cast(prev)].vertex, Vertices[static_cast(prevOfPrev)].vertex, Vertices[static_cast(postOfPrev)].vertex); Vertices[static_cast(prev)].isTip = isTip(Vertices[static_cast(prev)].interiorAngle); // update post Vertices[static_cast(post)].interiorAngle = calculateInner(Vertices[static_cast(post)].vertex, Vertices[static_cast(prevOfPost)].vertex, Vertices[static_cast(postOfPost)].vertex); Vertices[static_cast(post)].isTip = isTip(Vertices[static_cast(post)].interiorAngle); } return Triangles; } bool IntelliHelper::isInPolygon(std::vector &triangles, QPoint &point){ for(auto triangle : triangles) { if(IntelliHelper::isInTriangle(triangle, point)) { return true; } } return false; }