glue.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. ISC License
  3. Copyright (c) 2023-2024, Bruce A Henderson
  4. Permission to use, copy, modify, and/or distribute this software for any purpose
  5. with or without fee is hereby granted, provided that the above copyright notice
  6. and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  8. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  11. OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  12. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  13. THIS SOFTWARE.
  14. */
  15. #include "mapbox/earcut.hpp"
  16. #include "clipper2/CPP/Clipper2Lib/include/clipper2/clipper.h"
  17. extern "C" {
  18. #include "brl.mod/blitz.mod/blitz.h"
  19. struct SVec2I {
  20. int x;
  21. int y;
  22. };
  23. struct SVec2F {
  24. float x;
  25. float y;
  26. };
  27. BBArray * bmx_polygon_tri_svec2i(struct SVec2I * p, int size);
  28. BBArray * bmx_polygon_tri_svec2f(struct SVec2F * p, int size);
  29. }
  30. namespace mapbox {
  31. namespace util {
  32. template <>
  33. struct nth<0, struct SVec2I> {
  34. inline static int get(const struct SVec2I &v) {
  35. return v.x;
  36. };
  37. };
  38. template <>
  39. struct nth<1, struct SVec2I> {
  40. inline static int get(const struct SVec2I &v) {
  41. return v.y;
  42. };
  43. };
  44. template <>
  45. struct nth<0, struct SVec2F> {
  46. inline static float get(const struct SVec2F &v) {
  47. return v.x;
  48. };
  49. };
  50. template <>
  51. struct nth<1, struct SVec2F> {
  52. inline static float get(const struct SVec2F &v) {
  53. return v.y;
  54. };
  55. };
  56. } // namespace util
  57. } // namespace mapbox
  58. BBArray * bmx_polygon_tri_svec2i(struct SVec2I * p, int size) {
  59. std::vector<struct SVec2I> points(p, p + size);
  60. std::vector<std::vector<struct SVec2I>> polygon;
  61. polygon.push_back(points);
  62. std::vector<int> indices = mapbox::earcut<int>(polygon);
  63. BBArray *arr = bbArrayNew1DNoInit("i", indices.size());
  64. int *s = (int*)BBARRAYDATA(arr,arr->dims);
  65. std::copy(indices.begin(), indices.end(), s);
  66. return arr;
  67. }
  68. BBArray * bmx_polygon_tri_svec2f(struct SVec2F * p, int size) {
  69. std::vector<struct SVec2F> points(p, p + size);
  70. std::vector<std::vector<struct SVec2F>> polygon;
  71. polygon.push_back(points);
  72. std::vector<int> indices = mapbox::earcut<int>(polygon);
  73. BBArray *arr = bbArrayNew1DNoInit("f", indices.size());
  74. int *s = (int*)BBARRAYDATA(arr,arr->dims);
  75. std::copy(indices.begin(), indices.end(), s);
  76. return arr;
  77. }
  78. using PathI = Clipper2Lib::Path<int>;
  79. using PathF = Clipper2Lib::Path<float>;
  80. using PathsI = std::vector<PathI>;
  81. using PathsF = std::vector<PathF>;