src/geometry.cpp

changeset 297
bc92f97498f7
parent 264
76a025db4948
child 369
57de8fab2237
equal deleted inserted replaced
296:38f6fad61bad 297:bc92f97498f7
1 #include <glm/gtc/matrix_transform.hpp> 1 #include <glm/gtc/matrix_transform.hpp>
2 #include "src/geometry.h" 2 #include "src/geometry.h"
3 #include "src/basics.h" 3 #include "src/basics.h"
4 #include "src/ring.h"
5 4
6 /** 5 /**
7 * @brief Computes line-plane intersection 6 * @brief Computes line-plane intersection
8 * @param line 7 * @param line
9 * @param plane 8 * @param plane
216 } 215 }
217 216
218 bool isConvex(const std::vector<glm::vec3>& polygon) 217 bool isConvex(const std::vector<glm::vec3>& polygon)
219 { 218 {
220 const std::size_t n = polygon.size(); 219 const std::size_t n = polygon.size();
221 auto polygonRing = iter::ring(polygon, n);
222 std::vector<glm::vec3> crosses; 220 std::vector<glm::vec3> crosses;
223 crosses.resize(n); 221 crosses.resize(n);
224 for (std::size_t i = 0; i < n; i += 1) 222 for (std::size_t i = 0; i < n; i += 1)
225 { 223 {
226 crosses[i] = glm::cross(polygonRing[i - 1] - polygonRing[i], polygonRing[i + 1] - polygonRing[i]); 224 const glm::vec3 v1 = polygon[(i + n - 1) % n];
225 const glm::vec3 v2 = polygon[i];
226 const glm::vec3 v3 = polygon[(i + 1) % n];
227 crosses[i] = glm::cross(v1 - v2, v3 - v2);
227 } 228 }
228 return not std::any_of( 229 return not std::any_of(
229 crosses.begin() + 1, 230 crosses.begin() + 1,
230 crosses.end(), 231 crosses.end(),
231 [&crosses](const glm::vec3& vector) 232 [&crosses](const glm::vec3& vector)

mercurial