AP_GeodesicGrid.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * This file is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * This comment section explains the basic idea behind the implementation.
  19. *
  20. * Vectors difference notation
  21. * ===========================
  22. * Let v and w be vectors. For readability purposes, unless explicitly
  23. * otherwise noted, the notation vw will be used to represent w - v.
  24. *
  25. * Relationship between a vector and a triangle in 3d space
  26. * ========================================================
  27. * Vector in the area of a triangle
  28. * --------------------------------
  29. * Let T = (a, b, c) be a triangle, where a, b and c are also vectors and
  30. * linearly independent. A vector inside that triangle can be written as one of
  31. * its vertices plus the sum of the positively scaled vectors from that vertex
  32. * to the other ones. Taking a as the first vertex, a vector p in the area
  33. * formed by T can be written as:
  34. *
  35. * p = a + w_ab * ab + w_ac * ac
  36. *
  37. * It's fairly easy to see that if p is in the area formed by T, then w_ab >= 0
  38. * and w_ac >= 0. That vector p can also be written as:
  39. *
  40. * p = b + w_ba * ba + w_bc * bc
  41. *
  42. * It's easy to check that the triangle formed by (a + w_ab * ab, b + w_ba *
  43. * ba, p) is similar to T and, with the correct algebraic manipulations, we can
  44. * come to the conclusion that:
  45. *
  46. * w_ba = 1 - w_ab - w_ac
  47. *
  48. * Since we know that w_ba >= 0, then w_ab + w_ac <= 1. Thus:
  49. *
  50. * ----------------------------------------------------------
  51. * | p = a + w_ab * ab + w_ac * ac is in the area of T iff: |
  52. * | w_ab >= 0 and w_ac >= 0 and w_ab + w_ac <= 1 |
  53. * ----------------------------------------------------------
  54. *
  55. * Proving backwards shouldn't be difficult.
  56. *
  57. * Vector p can also be written as:
  58. *
  59. * p = (1 - w_ab - w_ba) * a + w_ab * b + w_ba * c
  60. *
  61. *
  62. * Vector that crosses a triangle
  63. * ------------------------------
  64. * Let T be the same triangle discussed above and let v be a vector such that:
  65. *
  66. * v = x * a + y * b + z * c
  67. * where x >= 0, y >= 0, z >= 0, and x + y + z > 0.
  68. *
  69. * It's geometrically easy to see that v crosses the triangle T. But that can
  70. * also be verified analytically.
  71. *
  72. * The vector v crosses the triangle T iff there's a positive alpha such that
  73. * alpha * v is in the area formed by T, so we need to prove that such value
  74. * exists. To find alpha, we solve the equation alpha * v = p, which will lead
  75. * us to the system, for the variables alpha, w_ab and w_ac:
  76. *
  77. * alpha * x = 1 - w_ab - w_ac
  78. * alpha * y = w_ab
  79. * alpha * z = w_ac,
  80. * where w_ab >= 0 and w_ac >= 0 and w_ab + w_ac <= 1
  81. *
  82. * That will lead to alpha = 1 / (x + y + z), w_ab = y / (x + y + b) and
  83. * w_ac = z / (x + y + z) and the following holds:
  84. * - alpha does exist because x + y + z > 0.
  85. * - w_ab >= 0 and w_ac >= 0 because y >= 0 and z >= 0 and x + y + z > 0.
  86. * - 0 <= 1 - w_ab - w_ac <= 1 because 0 <= (y + z) / (x + y + z) <= 1.
  87. *
  88. * Thus:
  89. *
  90. * ----------------------------------------------------------
  91. * | v = x * a + y * b + z * c crosses T = (a, b, c), where |
  92. * | a, b and c are linearly independent, iff: |
  93. * | x >= 0, y >= 0, z >= 0 and x + y + z > 0 |
  94. * ----------------------------------------------------------
  95. *
  96. * Moreover:
  97. * - if one of the coefficients is zero, then v crosses the edge formed by the
  98. * vertices multiplied by the non-zero coefficients.
  99. * - if two of the coefficients are zero, then v crosses the vertex multiplied
  100. * by the non-zero coefficient.
  101. */
  102. #include <assert.h>
  103. #include "AP_GeodesicGrid.h"
  104. /* This was generated with
  105. * libraries/AP_Math/tools/geodesic_grid/geodesic_grid.py */
  106. const struct AP_GeodesicGrid::neighbor_umbrella
  107. AP_GeodesicGrid::_neighbor_umbrellas[3]{
  108. {{ 9, 8, 7, 12, 14}, 1, 2, 0, 0, 2},
  109. {{ 1, 2, 4, 5, 3}, 0, 0, 2, 2, 0},
  110. {{16, 15, 13, 18, 17}, 2, 2, 0, 2, 1},
  111. };
  112. /* This was generated with
  113. * libraries/AP_Math/tools/geodesic_grid/geodesic_grid.py */
  114. const Matrix3f AP_GeodesicGrid::_inverses[10]{
  115. {{-0.309017f, 0.500000f, 0.190983f},
  116. { 0.000000f, 0.000000f, -0.618034f},
  117. {-0.309017f, -0.500000f, 0.190983f}},
  118. {{-0.190983f, 0.309017f, -0.500000f},
  119. {-0.500000f, -0.190983f, 0.309017f},
  120. { 0.309017f, -0.500000f, -0.190983f}},
  121. {{-0.618034f, 0.000000f, 0.000000f},
  122. { 0.190983f, -0.309017f, -0.500000f},
  123. { 0.190983f, -0.309017f, 0.500000f}},
  124. {{-0.500000f, 0.190983f, -0.309017f},
  125. { 0.000000f, -0.618034f, 0.000000f},
  126. { 0.500000f, 0.190983f, -0.309017f}},
  127. {{-0.190983f, -0.309017f, -0.500000f},
  128. {-0.190983f, -0.309017f, 0.500000f},
  129. { 0.618034f, 0.000000f, 0.000000f}},
  130. {{-0.309017f, -0.500000f, -0.190983f},
  131. { 0.190983f, 0.309017f, -0.500000f},
  132. { 0.500000f, -0.190983f, 0.309017f}},
  133. {{ 0.309017f, -0.500000f, 0.190983f},
  134. { 0.000000f, 0.000000f, -0.618034f},
  135. { 0.309017f, 0.500000f, 0.190983f}},
  136. {{ 0.190983f, -0.309017f, -0.500000f},
  137. { 0.500000f, 0.190983f, 0.309017f},
  138. {-0.309017f, 0.500000f, -0.190983f}},
  139. {{ 0.500000f, -0.190983f, -0.309017f},
  140. { 0.000000f, 0.618034f, 0.000000f},
  141. {-0.500000f, -0.190983f, -0.309017f}},
  142. {{ 0.309017f, 0.500000f, -0.190983f},
  143. {-0.500000f, 0.190983f, 0.309017f},
  144. {-0.190983f, -0.309017f, -0.500000f}},
  145. };
  146. /* This was generated with
  147. * libraries/AP_Math/tools/geodesic_grid/geodesic_grid.py */
  148. const Matrix3f AP_GeodesicGrid::_mid_inverses[10]{
  149. {{-0.000000f, 1.000000f, -0.618034f},
  150. { 0.000000f, -1.000000f, -0.618034f},
  151. {-0.618034f, 0.000000f, 1.000000f}},
  152. {{-1.000000f, 0.618034f, -0.000000f},
  153. {-0.000000f, -1.000000f, 0.618034f},
  154. { 0.618034f, -0.000000f, -1.000000f}},
  155. {{-0.618034f, -0.000000f, -1.000000f},
  156. { 1.000000f, -0.618034f, -0.000000f},
  157. {-0.618034f, 0.000000f, 1.000000f}},
  158. {{-1.000000f, -0.618034f, -0.000000f},
  159. { 1.000000f, -0.618034f, 0.000000f},
  160. {-0.000000f, 1.000000f, -0.618034f}},
  161. {{-1.000000f, -0.618034f, 0.000000f},
  162. { 0.618034f, 0.000000f, 1.000000f},
  163. { 0.618034f, 0.000000f, -1.000000f}},
  164. {{-0.618034f, -0.000000f, -1.000000f},
  165. { 1.000000f, 0.618034f, -0.000000f},
  166. { 0.000000f, -1.000000f, 0.618034f}},
  167. {{ 0.000000f, -1.000000f, -0.618034f},
  168. { 0.000000f, 1.000000f, -0.618034f},
  169. { 0.618034f, -0.000000f, 1.000000f}},
  170. {{ 1.000000f, -0.618034f, -0.000000f},
  171. { 0.000000f, 1.000000f, 0.618034f},
  172. {-0.618034f, 0.000000f, -1.000000f}},
  173. {{ 1.000000f, 0.618034f, -0.000000f},
  174. {-1.000000f, 0.618034f, 0.000000f},
  175. { 0.000000f, -1.000000f, -0.618034f}},
  176. {{-0.000000f, 1.000000f, 0.618034f},
  177. {-1.000000f, -0.618034f, -0.000000f},
  178. { 0.618034f, 0.000000f, -1.000000f}},
  179. };
  180. int AP_GeodesicGrid::section(const Vector3f &v, bool inclusive)
  181. {
  182. int i = _triangle_index(v, inclusive);
  183. if (i < 0) {
  184. return -1;
  185. }
  186. int j = _subtriangle_index(i, v, inclusive);
  187. if (j < 0) {
  188. return -1;
  189. }
  190. return 4 * i + j;
  191. }
  192. int AP_GeodesicGrid::_neighbor_umbrella_component(int idx, int comp_idx)
  193. {
  194. if (idx < 3) {
  195. return _neighbor_umbrellas[idx].components[comp_idx];
  196. }
  197. return (_neighbor_umbrellas[idx % 3].components[comp_idx] + 10) % 20;
  198. }
  199. int AP_GeodesicGrid::_from_neighbor_umbrella(int idx,
  200. const Vector3f &v,
  201. const Vector3f &u,
  202. bool inclusive)
  203. {
  204. /* The following comparisons between the umbrella's first and second
  205. * vertices' coefficients work for this algorithm because all vertices'
  206. * vectors are of the same length. */
  207. if (is_equal(u.x, u.y)) {
  208. /* If the coefficients of the first and second vertices are equal, then
  209. * v crosses the first component or the edge formed by the umbrella's
  210. * pivot and forth vertex. */
  211. int comp = _neighbor_umbrella_component(idx, 0);
  212. auto w = _inverses[comp % 10] * v;
  213. if (comp > 9) {
  214. w = -w;
  215. }
  216. float x0 = w[_neighbor_umbrellas[idx % 3].v0_c0];
  217. if (is_zero(x0)) {
  218. if (!inclusive) {
  219. return -1;
  220. }
  221. return comp;
  222. } else if (x0 < 0) {
  223. if (!inclusive) {
  224. return -1;
  225. }
  226. return _neighbor_umbrella_component(idx, u.x < u.y ? 3 : 2);
  227. }
  228. return comp;
  229. }
  230. if (u.y > u.x) {
  231. /* If the coefficient of the second vertex is greater than the first
  232. * one's, then v crosses the first, second or third component. */
  233. int comp = _neighbor_umbrella_component(idx, 1);
  234. auto w = _inverses[comp % 10] * v;
  235. if (comp > 9) {
  236. w = -w;
  237. }
  238. float x1 = w[_neighbor_umbrellas[idx % 3].v1_c1];
  239. float x2 = w[_neighbor_umbrellas[idx % 3].v2_c1];
  240. if (is_zero(x1)) {
  241. if (!inclusive) {
  242. return -1;
  243. }
  244. return _neighbor_umbrella_component(idx, x1 < 0 ? 2 : 1);
  245. } else if (x1 < 0) {
  246. return _neighbor_umbrella_component(idx, 2);
  247. }
  248. if (is_zero(x2)) {
  249. if (!inclusive) {
  250. return -1;
  251. }
  252. return _neighbor_umbrella_component(idx, x2 > 0 ? 1 : 0);
  253. } else if (x2 < 0) {
  254. return _neighbor_umbrella_component(idx, 0);
  255. }
  256. return comp;
  257. } else {
  258. /* If the coefficient of the second vertex is lesser than the first
  259. * one's, then v crosses the first, fourth or fifth component. */
  260. int comp = _neighbor_umbrella_component(idx, 4);
  261. auto w = _inverses[comp % 10] * v;
  262. if (comp > 9) {
  263. w = -w;
  264. }
  265. float x4 = w[_neighbor_umbrellas[idx % 3].v4_c4];
  266. float x0 = w[_neighbor_umbrellas[idx % 3].v0_c4];
  267. if (is_zero(x4)) {
  268. if (!inclusive) {
  269. return -1;
  270. }
  271. return _neighbor_umbrella_component(idx, x4 < 0 ? 0 : 4);
  272. } else if (x4 < 0) {
  273. return _neighbor_umbrella_component(idx, 0);
  274. }
  275. if (is_zero(x0)) {
  276. if (!inclusive) {
  277. return -1;
  278. }
  279. return _neighbor_umbrella_component(idx, x0 > 0 ? 4 : 3);
  280. } else if (x0 < 0) {
  281. return _neighbor_umbrella_component(idx, 3);
  282. }
  283. return comp;
  284. }
  285. }
  286. int AP_GeodesicGrid::_triangle_index(const Vector3f &v, bool inclusive)
  287. {
  288. /* w holds the coordinates of v with respect to the basis comprised by the
  289. * vectors of T_i */
  290. auto w = _inverses[0] * v;
  291. int zero_count = 0;
  292. int balance = 0;
  293. int umbrella = -1;
  294. if (is_zero(w.x)) {
  295. zero_count++;
  296. } else if (w.x > 0) {
  297. balance++;
  298. } else {
  299. balance--;
  300. }
  301. if (is_zero(w.y)) {
  302. zero_count++;
  303. } else if (w.y > 0) {
  304. balance++;
  305. } else {
  306. balance--;
  307. }
  308. if (is_zero(w.z)) {
  309. zero_count++;
  310. } else if (w.z > 0) {
  311. balance++;
  312. } else {
  313. balance--;
  314. }
  315. switch (balance) {
  316. case 3:
  317. /* All coefficients are positive, thus return the first triangle. */
  318. return 0;
  319. case -3:
  320. /* All coefficients are negative, which means that the coefficients for
  321. * -w are positive, thus return the first triangle's opposite. */
  322. return 10;
  323. case 2:
  324. /* Two coefficients are positive and one is zero, thus v crosses one of
  325. * the edges of the first triangle. */
  326. return inclusive ? 0 : -1;
  327. case -2:
  328. /* Analogous to the previous case, but for the opposite of the first
  329. * triangle. */
  330. return inclusive ? 10 : -1;
  331. case 1:
  332. /* There are two possible cases when balance is 1:
  333. *
  334. * 1) Two coefficients are zero, which means v crosses one of the
  335. * vertices of the first triangle.
  336. *
  337. * 2) Two coefficients are positive and one is negative. Let a and b be
  338. * vertices with positive coefficients and c the one with the negative
  339. * coefficient. That means that v crosses the triangle formed by a, b
  340. * and -c. The vector -c happens to be the 3-th vertex, with respect to
  341. * (a, b), of the first triangle's neighbor umbrella with respect to a
  342. * and b. Thus, v crosses one of the components of that umbrella. */
  343. if (zero_count == 2) {
  344. return inclusive ? 0 : -1;
  345. }
  346. if (!is_zero(w.x) && w.x < 0) {
  347. umbrella = 1;
  348. } else if (!is_zero(w.y) && w.y < 0) {
  349. umbrella = 2;
  350. } else {
  351. umbrella = 0;
  352. }
  353. break;
  354. case -1:
  355. /* Analogous to the previous case, but for the opposite of the first
  356. * triangle. */
  357. if (zero_count == 2) {
  358. return inclusive ? 10 : -1;
  359. }
  360. if (!is_zero(w.x) && w.x > 0) {
  361. umbrella = 4;
  362. } else if (!is_zero(w.y) && w.y > 0) {
  363. umbrella = 5;
  364. } else {
  365. umbrella = 3;
  366. }
  367. w = -w;
  368. break;
  369. case 0:
  370. /* There are two possible cases when balance is 1:
  371. *
  372. * 1) The vector v is the null vector, which doesn't cross any section.
  373. *
  374. * 2) One coefficient is zero, another is positive and yet another is
  375. * negative. Let a, b and c be the respective vertices for those
  376. * coefficients, then the statements in case (2) for when balance is 1
  377. * are also valid here.
  378. */
  379. if (zero_count == 3) {
  380. return -1;
  381. }
  382. if (!is_zero(w.x) && w.x < 0) {
  383. umbrella = 1;
  384. } else if (!is_zero(w.y) && w.y < 0) {
  385. umbrella = 2;
  386. } else {
  387. umbrella = 0;
  388. }
  389. break;
  390. }
  391. assert(umbrella >= 0);
  392. switch (umbrella % 3) {
  393. case 0:
  394. w.z = -w.z;
  395. break;
  396. case 1:
  397. w(w.y, w.z, -w.x);
  398. break;
  399. case 2:
  400. w(w.z, w.x, -w.y);
  401. break;
  402. }
  403. return _from_neighbor_umbrella(umbrella, v, w, inclusive);
  404. }
  405. int AP_GeodesicGrid::_subtriangle_index(const unsigned int triangle_index,
  406. const Vector3f &v,
  407. bool inclusive)
  408. {
  409. /* w holds the coordinates of v with respect to the basis comprised by the
  410. * vectors of the middle triangle of T_i where i is triangle_index */
  411. auto w = _mid_inverses[triangle_index % 10] * v;
  412. if (triangle_index > 9) {
  413. w = -w;
  414. }
  415. if ((is_zero(w.x) || is_zero(w.y) || is_zero(w.z)) && !inclusive) {
  416. return -1;
  417. }
  418. /* At this point, we know that v crosses the icosahedron triangle pointed
  419. * by triangle_index. Thus, we can geometrically see that if v doesn't
  420. * cross its middle triangle, then one of the coefficients will be negative
  421. * and the other ones positive. Let a and b be the non-negative
  422. * coefficients and c the negative one. In that case, v will cross the
  423. * triangle with vertices (a, b, -c). Since we know that v crosses the
  424. * icosahedron triangle and the only sub-triangle that contains the set of
  425. * points (seen as vectors) that cross the triangle (a, b, -c) is the
  426. * middle triangle's neighbor with respect to a and b, then that
  427. * sub-triangle is the one crossed by v. */
  428. if (!is_zero(w.x) && w.x < 0) {
  429. return 3;
  430. }
  431. if (!is_zero(w.y) && w.y < 0) {
  432. return 1;
  433. }
  434. if (!is_zero(w.z) && w.z < 0) {
  435. return 2;
  436. }
  437. /* If x >= 0 and y >= 0 and z >= 0, then v crosses the middle triangle. */
  438. return 0;
  439. }