Integrated Polygon Test

Use calculate traingles and the isInPolygon with theese triangles, this is done because of performance reasons
This commit is contained in:
Sonaion
2019-12-18 16:32:11 +01:00
parent 21ade76563
commit 4953abc791
5 changed files with 150 additions and 22 deletions

View File

@@ -35,26 +35,18 @@ void IntelliShapedImage::calculateVisiblity(){
}
return;
}
QPoint A = polygonData[0];
QColor clr;
for(int y=0; y<imageData.height(); y++){
for(int x=0; x<imageData.width(); x++){
int cutNumeber=0;
for(int i=1; i<static_cast<int>(polygonData.size()-1); i++){
QPoint B = polygonData[static_cast<size_t>(i)];
QPoint C = polygonData[static_cast<size_t>(i+1)];
QPoint P(x,y);
cutNumeber+=IntelliHelper::isInTriangle(A,B,C,P);
}
if(cutNumeber%2==0){
clr = imageData.pixelColor(x,y);
clr.setAlpha(0);
imageData.setPixelColor(x,y,clr);
}else{
clr = imageData.pixelColor(x,y);
QPoint ptr(x,y);
clr = imageData.pixelColor(x,y);
bool isInPolygon = IntelliHelper::isInPolygon(triangles, ptr);
if(isInPolygon){
clr.setAlpha(std::min(255, clr.alpha()));
imageData.setPixelColor(x,y,clr);
}else{
clr.setAlpha(0);
}
imageData.setPixelColor(x,y,clr);
}
}
}
@@ -79,6 +71,7 @@ void IntelliShapedImage::setPolygon(const std::vector<QPoint>& polygonData){
for(auto element:polygonData){
this->polygonData.push_back(QPoint(element.x(), element.y()));
}
triangles = IntelliHelper::calculateTriangles(polygonData);
}
calculateVisiblity();
return;

View File

@@ -2,9 +2,13 @@
#define INTELLISHAPE_H
#include"Image/IntelliRasterImage.h"
#include<vector>
#include"IntelliHelper/IntelliHelper.h"
class IntelliShapedImage : public IntelliRasterImage{
friend IntelliTool;
private:
std::vector<Triangle> triangles;
protected:
std::vector<QPoint> polygonData;

View File

@@ -1,2 +1,123 @@
#include"IntelliHelper.h"
#include<algorithm>
#include<queue>
#include<cmath>
std::vector<Triangle> IntelliHelper::calculateTriangles(std::vector<QPoint> 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<TriangleHelper>& vec){
for(auto element:vec){
if(element.isTip){
return element;
}
}
return vec[0];
};
//get the vertex Index bevor index in relation to the container length
auto getPrev = [](int index, int length){
return (index-1)>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 angle<180.f;
};
std::vector<TriangleHelper> Vertices;
std::vector<Triangle> Triangles;
//set up all vertices and calculate intirior angle
for(int i=0; i<static_cast<int>(polyPoints.size()); i++){
TriangleHelper helper;
int prev = getPrev(i, static_cast<int>(polyPoints.size()));
int post = getPost(i, static_cast<int>(polyPoints.size()));
helper.vertex = polyPoints[static_cast<size_t>(i)];
helper.index = i;
helper.interiorAngle = calculateInner(polyPoints[static_cast<size_t>(i)],
polyPoints[static_cast<size_t>(prev)],
polyPoints[static_cast<size_t>(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<int>(Vertices.size()));
int post = getPost(smallest.index, static_cast<int>(Vertices.size()));
//set triangle and push it
tri.A = Vertices[static_cast<size_t>(prev)].vertex;
tri.B = Vertices[static_cast<size_t>(smallest.index)].vertex;
tri.C = Vertices[static_cast<size_t>(post)].vertex;
Triangles.push_back(tri);
//update Vertice array
Vertices.erase(Vertices.begin()+smallest.index);
for(size_t i=static_cast<size_t>(smallest.index); i<Vertices.size(); i++){
Vertices[i].index-=1;
}
//update post und prev index
post = post-1;
prev = prev<smallest.index?prev:(prev-1);
//calcultae neighboors of prev and post to calculate new interior angles
int prevOfPrev = getPrev(prev, static_cast<int>(Vertices.size()));
int postOfPrev = getPost(prev, static_cast<int>(Vertices.size()));
int prevOfPost = getPrev(post, static_cast<int>(Vertices.size()));
int postOfPost = getPost(post, static_cast<int>(Vertices.size()));
//update vertices with interior angles
//updtae prev
Vertices[static_cast<size_t>(prev)].interiorAngle = calculateInner(Vertices[static_cast<size_t>(prev)].vertex,
Vertices[static_cast<size_t>(prevOfPrev)].vertex,
Vertices[static_cast<size_t>(postOfPrev)].vertex);
Vertices[static_cast<size_t>(prev)].isTip = isTip(Vertices[static_cast<size_t>(prev)].interiorAngle);
//update post
Vertices[static_cast<size_t>(post)].interiorAngle = calculateInner(Vertices[static_cast<size_t>(post)].vertex,
Vertices[static_cast<size_t>(prevOfPost)].vertex,
Vertices[static_cast<size_t>(postOfPost)].vertex);
Vertices[static_cast<size_t>(post)].isTip = isTip(Vertices[static_cast<size_t>(post)].interiorAngle);
}
return Triangles;
}
bool IntelliHelper::isInPolygon(std::vector<Triangle> &triangles, QPoint &point){
for(auto triangle : triangles){
if(IntelliHelper::isInTriangle(triangle.A ,triangle.B, triangle.C, point)){
return true;
}
}
return false;
}

View File

@@ -2,17 +2,20 @@
#define INTELLIHELPER_H
#include<QPoint>
#include<vector>
struct Triangle{
QPoint A,B,C;
};
namespace IntelliHelper {
class IntelliHelper{
public:
static inline float sign(QPoint& p1, QPoint& p2, QPoint& p3){
inline float sign(QPoint& p1, QPoint& p2, QPoint& p3){
return (p1.x()-p3.x())*(p2.y()-p3.y())-(p2.x()-p3.x())*(p1.y()-p3.y());
}
static inline bool isInTriangle(QPoint& A, QPoint& B, QPoint& C, QPoint& P){
inline bool isInTriangle(QPoint& A, QPoint& B, QPoint& C, QPoint& P){
float val1, val2, val3;
bool neg, pos;
@@ -25,6 +28,11 @@ public:
return !(neg && pos);
}
};
std::vector<Triangle> calculateTriangles(std::vector<QPoint> polyPoints);
bool isInPolygon(std::vector<Triangle> &triangles, QPoint &point);
}
#endif

View File

@@ -1,6 +1,8 @@
#include "GUI/IntelliPhotoGui.h"
#include <QApplication>
#include <QDebug>
#include "IntelliHelper/IntelliHelper.h"
#include<vector>
int main(int argc, char *argv[]){
// The main application