src/geometry.cpp

changeset 123
e3fe3617b631
parent 122
b54b350dff5d
child 168
24590af32ad6
equal deleted inserted replaced
122:b54b350dff5d 123:e3fe3617b631
1 #include <glm/gtc/matrix_transform.hpp> 1 #include <glm/gtc/matrix_transform.hpp>
2 #include "geometry.h" 2 #include "geometry.h"
3 #include "main.h"
3 #include "ring.h" 4 #include "ring.h"
4 #include "maths.h"
5 5
6 /** 6 /**
7 * @brief Computes line-plane intersection 7 * @brief Computes line-plane intersection
8 * @param line 8 * @param line
9 * @param plane 9 * @param plane
197 glm::vec2{rectangle.right(), rectangle.top()}, 197 glm::vec2{rectangle.right(), rectangle.top()},
198 glm::vec2{rectangle.right(), rectangle.bottom()} 198 glm::vec2{rectangle.right(), rectangle.bottom()}
199 }; 199 };
200 } 200 }
201 201
202 bool geom::convex(const std::vector<glm::vec3>& polygon) 202 bool geom::isConvex(const std::vector<glm::vec3>& polygon)
203 { 203 {
204 const int n = polygon.size(); 204 const int n = polygon.size();
205 auto polygonRing = iter::ring(polygon, n); 205 auto polygonRing = iter::ring(polygon, n);
206 std::vector<glm::vec3> crosses; 206 std::vector<glm::vec3> crosses;
207 crosses.resize(n); 207 crosses.resize(n);
208 for (int i = 0; i < n; i += 1) 208 for (int i = 0; i < n; i += 1)
209 { 209 {
210 crosses[i] = glm::cross(polygonRing[i - 1] - polygonRing[i], polygonRing[i + 1] - polygonRing[i]); 210 crosses[i] = glm::cross(polygonRing[i - 1] - polygonRing[i], polygonRing[i + 1] - polygonRing[i]);
211 } 211 }
212 for (int i = 0; i < n - 1; i += 1) 212 return not std::any_of(
213 { 213 crosses.begin() + 1,
214 const float dotp = glm::dot(crosses[0], crosses[i + 1]); 214 crosses.end(),
215 if (dotp < 0 or qFuzzyIsNull(dotp)) 215 [&crosses](const glm::vec3& vector)
216 { 216 {
217 return false; 217 return glm::dot(crosses[0], vector) < 1e-6;
218 } 218 });
219 } 219 }
220 return true;
221 }

mercurial