IntelliPhoto  0.4
IntelliHelper.cpp
Go to the documentation of this file.
1 #include"IntelliHelper.h"
2 #include<algorithm>
3 #include<queue>
4 #include<cmath>
5 
6 
7 std::vector<Triangle> IntelliHelper::calculateTriangles(std::vector<QPoint> polyPoints){
8  //helper for managing the triangle vertices and their state
9  struct TriangleHelper{
10  QPoint vertex;
11  float interiorAngle;
12  int index;
13  bool isTip;
14  };
15 
16  //calculates the inner angle of 'point'
17  auto calculateInner = [](QPoint& point, QPoint& prev, QPoint& post){
18  QPoint AP(point.x()-prev.x(), point.y()-prev.y());
19  QPoint BP(point.x()-post.x(), point.y()-post.y());
20 
21  float topSclar = AP.x()*BP.x()+AP.y()*BP.y();
22  float absolute = sqrt(pow(AP.x(),2.)+pow(AP.y(),2.))*sqrt(pow(BP.x(),2.)+pow(BP.y(),2.));
23  return acos(topSclar/absolute);
24  };
25 
26  //gets the first element of vec for which element.isTip == true holds
27  auto getTip= [](const std::vector<TriangleHelper>& vec){
28  for(auto element:vec){
29  if(element.isTip){
30  return element;
31  }
32  }
33  return vec[0];
34  };
35 
36  //get the vertex Index bevor index in relation to the container length
37  auto getPrev = [](int index, int length){
38  return (index-1)>0?(index-1):(length-1);
39  };
40 
41  //get the vertex Index after index in relation to the container lenght
42  auto getPost = [](int index, int length){
43  return (index+1)%length;
44  };
45 
46  //return if the vertex is a tip
47  auto isTip = [](float angle){
48  return angle<180.f;
49  };
50 
51  std::vector<TriangleHelper> Vertices;
52  std::vector<Triangle> Triangles;
53 
54  //set up all vertices and calculate intirior angle
55  for(int i=0; i<static_cast<int>(polyPoints.size()); i++){
56  TriangleHelper helper;
57  int prev = getPrev(i, static_cast<int>(polyPoints.size()));
58  int post = getPost(i, static_cast<int>(polyPoints.size()));
59 
60  helper.vertex = polyPoints[static_cast<size_t>(i)];
61  helper.index = i;
62 
63  helper.interiorAngle = calculateInner(polyPoints[static_cast<size_t>(i)],
64  polyPoints[static_cast<size_t>(prev)],
65  polyPoints[static_cast<size_t>(post)]);
66  helper.isTip = isTip(helper.interiorAngle);
67  Vertices.push_back(helper);
68  }
69 
70  //search triangles based on the intirior angles of each vertey
71  while(Triangles.size() != polyPoints.size()-2){
72  Triangle tri;
73  TriangleHelper smallest = getTip(Vertices);
74  int prev = getPrev(smallest.index, static_cast<int>(Vertices.size()));
75  int post = getPost(smallest.index, static_cast<int>(Vertices.size()));
76 
77  //set triangle and push it
78  tri.A = Vertices[static_cast<size_t>(prev)].vertex;
79  tri.B = Vertices[static_cast<size_t>(smallest.index)].vertex;
80  tri.C = Vertices[static_cast<size_t>(post)].vertex;
81  Triangles.push_back(tri);
82 
83  //update Vertice array
84  Vertices.erase(Vertices.begin()+smallest.index);
85  for(size_t i=static_cast<size_t>(smallest.index); i<Vertices.size(); i++){
86  Vertices[i].index-=1;
87  }
88 
89  //update post und prev index
90  post = post-1;
91  prev = prev<smallest.index?prev:(prev-1);
92 
93  //calcultae neighboors of prev and post to calculate new interior angles
94  int prevOfPrev = getPrev(prev, static_cast<int>(Vertices.size()));
95  int postOfPrev = getPost(prev, static_cast<int>(Vertices.size()));
96 
97  int prevOfPost = getPrev(post, static_cast<int>(Vertices.size()));
98  int postOfPost = getPost(post, static_cast<int>(Vertices.size()));
99 
100  //update vertices with interior angles
101  //updtae prev
102  Vertices[static_cast<size_t>(prev)].interiorAngle = calculateInner(Vertices[static_cast<size_t>(prev)].vertex,
103  Vertices[static_cast<size_t>(prevOfPrev)].vertex,
104  Vertices[static_cast<size_t>(postOfPrev)].vertex);
105  Vertices[static_cast<size_t>(prev)].isTip = isTip(Vertices[static_cast<size_t>(prev)].interiorAngle);
106  //update post
107  Vertices[static_cast<size_t>(post)].interiorAngle = calculateInner(Vertices[static_cast<size_t>(post)].vertex,
108  Vertices[static_cast<size_t>(prevOfPost)].vertex,
109  Vertices[static_cast<size_t>(postOfPost)].vertex);
110  Vertices[static_cast<size_t>(post)].isTip = isTip(Vertices[static_cast<size_t>(post)].interiorAngle);
111 
112  }
113  return Triangles;
114 }
115 
116 bool IntelliHelper::isInPolygon(std::vector<Triangle> &triangles, QPoint &point){
117  for(auto triangle : triangles){
118  if(IntelliHelper::isInTriangle(triangle, point)){
119  return true;
120  }
121  }
122  return false;
123 }
IntelliHelper::isInTriangle
bool isInTriangle(Triangle &tri, QPoint &P)
A function to check if a given point is in a triangle.
Definition: IntelliHelper.h:34
IntelliHelper.h
Triangle::B
QPoint B
Definition: IntelliHelper.h:11
Triangle::C
QPoint C
Definition: IntelliHelper.h:11
Triangle
The Triangle struct holds the 3 vertices of a triangle.
Definition: IntelliHelper.h:10
IntelliHelper::isInPolygon
bool isInPolygon(std::vector< Triangle > &triangles, QPoint &point)
A function to check if a point lies in a polygon by checking its spanning triangles.
Definition: IntelliHelper.cpp:116
Triangle::A
QPoint A
Definition: IntelliHelper.h:11
IntelliHelper::calculateTriangles
std::vector< Triangle > calculateTriangles(std::vector< QPoint > polyPoints)
A function to split a polygon in its spanning traingles by using Meisters Theorem of graph theory by ...
Definition: IntelliHelper.cpp:7