123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- #include <assert.h>
- #include "AP_GeodesicGrid.h"
- const struct AP_GeodesicGrid::neighbor_umbrella
- AP_GeodesicGrid::_neighbor_umbrellas[3]{
- {{ 9, 8, 7, 12, 14}, 1, 2, 0, 0, 2},
- {{ 1, 2, 4, 5, 3}, 0, 0, 2, 2, 0},
- {{16, 15, 13, 18, 17}, 2, 2, 0, 2, 1},
- };
- const Matrix3f AP_GeodesicGrid::_inverses[10]{
- {{-0.309017f, 0.500000f, 0.190983f},
- { 0.000000f, 0.000000f, -0.618034f},
- {-0.309017f, -0.500000f, 0.190983f}},
- {{-0.190983f, 0.309017f, -0.500000f},
- {-0.500000f, -0.190983f, 0.309017f},
- { 0.309017f, -0.500000f, -0.190983f}},
- {{-0.618034f, 0.000000f, 0.000000f},
- { 0.190983f, -0.309017f, -0.500000f},
- { 0.190983f, -0.309017f, 0.500000f}},
- {{-0.500000f, 0.190983f, -0.309017f},
- { 0.000000f, -0.618034f, 0.000000f},
- { 0.500000f, 0.190983f, -0.309017f}},
- {{-0.190983f, -0.309017f, -0.500000f},
- {-0.190983f, -0.309017f, 0.500000f},
- { 0.618034f, 0.000000f, 0.000000f}},
- {{-0.309017f, -0.500000f, -0.190983f},
- { 0.190983f, 0.309017f, -0.500000f},
- { 0.500000f, -0.190983f, 0.309017f}},
- {{ 0.309017f, -0.500000f, 0.190983f},
- { 0.000000f, 0.000000f, -0.618034f},
- { 0.309017f, 0.500000f, 0.190983f}},
- {{ 0.190983f, -0.309017f, -0.500000f},
- { 0.500000f, 0.190983f, 0.309017f},
- {-0.309017f, 0.500000f, -0.190983f}},
- {{ 0.500000f, -0.190983f, -0.309017f},
- { 0.000000f, 0.618034f, 0.000000f},
- {-0.500000f, -0.190983f, -0.309017f}},
- {{ 0.309017f, 0.500000f, -0.190983f},
- {-0.500000f, 0.190983f, 0.309017f},
- {-0.190983f, -0.309017f, -0.500000f}},
- };
- const Matrix3f AP_GeodesicGrid::_mid_inverses[10]{
- {{-0.000000f, 1.000000f, -0.618034f},
- { 0.000000f, -1.000000f, -0.618034f},
- {-0.618034f, 0.000000f, 1.000000f}},
- {{-1.000000f, 0.618034f, -0.000000f},
- {-0.000000f, -1.000000f, 0.618034f},
- { 0.618034f, -0.000000f, -1.000000f}},
- {{-0.618034f, -0.000000f, -1.000000f},
- { 1.000000f, -0.618034f, -0.000000f},
- {-0.618034f, 0.000000f, 1.000000f}},
- {{-1.000000f, -0.618034f, -0.000000f},
- { 1.000000f, -0.618034f, 0.000000f},
- {-0.000000f, 1.000000f, -0.618034f}},
- {{-1.000000f, -0.618034f, 0.000000f},
- { 0.618034f, 0.000000f, 1.000000f},
- { 0.618034f, 0.000000f, -1.000000f}},
- {{-0.618034f, -0.000000f, -1.000000f},
- { 1.000000f, 0.618034f, -0.000000f},
- { 0.000000f, -1.000000f, 0.618034f}},
- {{ 0.000000f, -1.000000f, -0.618034f},
- { 0.000000f, 1.000000f, -0.618034f},
- { 0.618034f, -0.000000f, 1.000000f}},
- {{ 1.000000f, -0.618034f, -0.000000f},
- { 0.000000f, 1.000000f, 0.618034f},
- {-0.618034f, 0.000000f, -1.000000f}},
- {{ 1.000000f, 0.618034f, -0.000000f},
- {-1.000000f, 0.618034f, 0.000000f},
- { 0.000000f, -1.000000f, -0.618034f}},
- {{-0.000000f, 1.000000f, 0.618034f},
- {-1.000000f, -0.618034f, -0.000000f},
- { 0.618034f, 0.000000f, -1.000000f}},
- };
- int AP_GeodesicGrid::section(const Vector3f &v, bool inclusive)
- {
- int i = _triangle_index(v, inclusive);
- if (i < 0) {
- return -1;
- }
- int j = _subtriangle_index(i, v, inclusive);
- if (j < 0) {
- return -1;
- }
- return 4 * i + j;
- }
- int AP_GeodesicGrid::_neighbor_umbrella_component(int idx, int comp_idx)
- {
- if (idx < 3) {
- return _neighbor_umbrellas[idx].components[comp_idx];
- }
- return (_neighbor_umbrellas[idx % 3].components[comp_idx] + 10) % 20;
- }
- int AP_GeodesicGrid::_from_neighbor_umbrella(int idx,
- const Vector3f &v,
- const Vector3f &u,
- bool inclusive)
- {
-
- if (is_equal(u.x, u.y)) {
-
- int comp = _neighbor_umbrella_component(idx, 0);
- auto w = _inverses[comp % 10] * v;
- if (comp > 9) {
- w = -w;
- }
- float x0 = w[_neighbor_umbrellas[idx % 3].v0_c0];
- if (is_zero(x0)) {
- if (!inclusive) {
- return -1;
- }
- return comp;
- } else if (x0 < 0) {
- if (!inclusive) {
- return -1;
- }
- return _neighbor_umbrella_component(idx, u.x < u.y ? 3 : 2);
- }
- return comp;
- }
- if (u.y > u.x) {
-
- int comp = _neighbor_umbrella_component(idx, 1);
- auto w = _inverses[comp % 10] * v;
- if (comp > 9) {
- w = -w;
- }
- float x1 = w[_neighbor_umbrellas[idx % 3].v1_c1];
- float x2 = w[_neighbor_umbrellas[idx % 3].v2_c1];
- if (is_zero(x1)) {
- if (!inclusive) {
- return -1;
- }
- return _neighbor_umbrella_component(idx, x1 < 0 ? 2 : 1);
- } else if (x1 < 0) {
- return _neighbor_umbrella_component(idx, 2);
- }
- if (is_zero(x2)) {
- if (!inclusive) {
- return -1;
- }
- return _neighbor_umbrella_component(idx, x2 > 0 ? 1 : 0);
- } else if (x2 < 0) {
- return _neighbor_umbrella_component(idx, 0);
- }
- return comp;
- } else {
-
- int comp = _neighbor_umbrella_component(idx, 4);
- auto w = _inverses[comp % 10] * v;
- if (comp > 9) {
- w = -w;
- }
- float x4 = w[_neighbor_umbrellas[idx % 3].v4_c4];
- float x0 = w[_neighbor_umbrellas[idx % 3].v0_c4];
- if (is_zero(x4)) {
- if (!inclusive) {
- return -1;
- }
- return _neighbor_umbrella_component(idx, x4 < 0 ? 0 : 4);
- } else if (x4 < 0) {
- return _neighbor_umbrella_component(idx, 0);
- }
- if (is_zero(x0)) {
- if (!inclusive) {
- return -1;
- }
- return _neighbor_umbrella_component(idx, x0 > 0 ? 4 : 3);
- } else if (x0 < 0) {
- return _neighbor_umbrella_component(idx, 3);
- }
- return comp;
- }
- }
- int AP_GeodesicGrid::_triangle_index(const Vector3f &v, bool inclusive)
- {
-
- auto w = _inverses[0] * v;
- int zero_count = 0;
- int balance = 0;
- int umbrella = -1;
- if (is_zero(w.x)) {
- zero_count++;
- } else if (w.x > 0) {
- balance++;
- } else {
- balance--;
- }
- if (is_zero(w.y)) {
- zero_count++;
- } else if (w.y > 0) {
- balance++;
- } else {
- balance--;
- }
- if (is_zero(w.z)) {
- zero_count++;
- } else if (w.z > 0) {
- balance++;
- } else {
- balance--;
- }
- switch (balance) {
- case 3:
-
- return 0;
- case -3:
-
- return 10;
- case 2:
-
- return inclusive ? 0 : -1;
- case -2:
-
- return inclusive ? 10 : -1;
- case 1:
-
- if (zero_count == 2) {
- return inclusive ? 0 : -1;
- }
- if (!is_zero(w.x) && w.x < 0) {
- umbrella = 1;
- } else if (!is_zero(w.y) && w.y < 0) {
- umbrella = 2;
- } else {
- umbrella = 0;
- }
- break;
- case -1:
-
- if (zero_count == 2) {
- return inclusive ? 10 : -1;
- }
- if (!is_zero(w.x) && w.x > 0) {
- umbrella = 4;
- } else if (!is_zero(w.y) && w.y > 0) {
- umbrella = 5;
- } else {
- umbrella = 3;
- }
- w = -w;
- break;
- case 0:
-
- if (zero_count == 3) {
- return -1;
- }
- if (!is_zero(w.x) && w.x < 0) {
- umbrella = 1;
- } else if (!is_zero(w.y) && w.y < 0) {
- umbrella = 2;
- } else {
- umbrella = 0;
- }
- break;
- }
- assert(umbrella >= 0);
- switch (umbrella % 3) {
- case 0:
- w.z = -w.z;
- break;
- case 1:
- w(w.y, w.z, -w.x);
- break;
- case 2:
- w(w.z, w.x, -w.y);
- break;
- }
- return _from_neighbor_umbrella(umbrella, v, w, inclusive);
- }
- int AP_GeodesicGrid::_subtriangle_index(const unsigned int triangle_index,
- const Vector3f &v,
- bool inclusive)
- {
-
- auto w = _mid_inverses[triangle_index % 10] * v;
- if (triangle_index > 9) {
- w = -w;
- }
- if ((is_zero(w.x) || is_zero(w.y) || is_zero(w.z)) && !inclusive) {
- return -1;
- }
-
- if (!is_zero(w.x) && w.x < 0) {
- return 3;
- }
- if (!is_zero(w.y) && w.y < 0) {
- return 1;
- }
- if (!is_zero(w.z) && w.z < 0) {
- return 2;
- }
-
- return 0;
- }
|