BoostGeometryTools.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // BoostGeometryTools.cpp
  3. // Test
  4. //
  5. // Created by 高慕白 on 2024/12/3.
  6. //
  7. #include "BoostGeometryTools.hpp"
  8. #define BOOSTGEOMETRYTOOLS_BOX_PT_CNT 5
  9. bool BoostGeometryTools::isAIntersectsB(BoostGeometryTools::BoostPolygon& polyA, BoostGeometryTools::BoostPolygon& polyB)
  10. {
  11. return boost::geometry::intersects(polyA, polyB) ;
  12. }
  13. bool BoostGeometryTools::isAWithinB(BoostGeometryTools::BoostPolygon& polyA, BoostGeometryTools::BoostPolygon& polyB)
  14. {
  15. return boost::geometry::within(polyA,polyB) ;
  16. }
  17. BoostGeometryTools::BoostPolygon BoostGeometryTools::makeBox( float lowerLeftX,float lowerLeftY,float wid,float hei )
  18. {
  19. float xarr[BOOSTGEOMETRYTOOLS_BOX_PT_CNT] = { lowerLeftX, lowerLeftX+wid, lowerLeftX+wid , lowerLeftX , lowerLeftX } ;
  20. float yarr[BOOSTGEOMETRYTOOLS_BOX_PT_CNT] = { lowerLeftY , lowerLeftY , lowerLeftY+hei , lowerLeftY+hei , lowerLeftY } ;
  21. BoostPolygon poly ;
  22. for(int ip = 0 ; ip < BOOSTGEOMETRYTOOLS_BOX_PT_CNT;++ip ) {
  23. poly.outer().push_back( {xarr[ip],yarr[ip]} ) ;
  24. }
  25. return poly ;
  26. }
  27. BoostGeometryTools::BoostPolygon BoostGeometryTools::makeRotatedBox( float lowerLeftX,float lowerLeftY,float wid,float hei,float rotdeg)
  28. {
  29. const float DEG2RAD = 3.141592/180.0;
  30. float rad = DEG2RAD*rotdeg ;
  31. float xarr[BOOSTGEOMETRYTOOLS_BOX_PT_CNT] = { lowerLeftX, lowerLeftX+wid, lowerLeftX+wid , lowerLeftX , lowerLeftX } ;
  32. float yarr[BOOSTGEOMETRYTOOLS_BOX_PT_CNT] = { lowerLeftY , lowerLeftY , lowerLeftY+hei , lowerLeftY+hei , lowerLeftY } ;
  33. BoostPolygon poly ;
  34. for(int ip = 0 ; ip < BOOSTGEOMETRYTOOLS_BOX_PT_CNT;++ip ) {
  35. float x1 = 0 ;
  36. float y1 = 0 ;
  37. rotatePoint(xarr[ip], yarr[ip], rad, x1, y1) ;
  38. poly.outer().push_back( {x1,y1} ) ;
  39. }
  40. return poly ;
  41. }
  42. void BoostGeometryTools::rotatePoint( float x0, float y0, float rotRad, float& x1,float& y1) {
  43. float cosval = cosf(rotRad) ;
  44. float sinval = sinf(rotRad) ;
  45. x1 = x0*cosval - y0*sinval ;
  46. y1 = x0*sinval + y0*cosval ;
  47. }
  48. BoostGeometryTools::BoostPolygon BoostGeometryTools::makeRotateNTranslateBox( float lowerLeftX,float lowerLeftY,float wid,float hei,float rotdeg,float dx,float dy)
  49. {
  50. const float DEG2RAD = 3.141592/180.0;
  51. float rad = DEG2RAD*rotdeg ;
  52. float xarr[BOOSTGEOMETRYTOOLS_BOX_PT_CNT] = { lowerLeftX, lowerLeftX+wid, lowerLeftX+wid , lowerLeftX , lowerLeftX } ;
  53. float yarr[BOOSTGEOMETRYTOOLS_BOX_PT_CNT] = { lowerLeftY , lowerLeftY , lowerLeftY+hei , lowerLeftY+hei , lowerLeftY } ;
  54. BoostPolygon poly ;
  55. for(int ip = 0 ; ip < BOOSTGEOMETRYTOOLS_BOX_PT_CNT;++ip ) {
  56. float x1 = 0 ;
  57. float y1 = 0 ;
  58. rotatePoint(xarr[ip], yarr[ip], rad, x1, y1) ;
  59. poly.outer().push_back( {x1+dx,y1+dy} ) ;
  60. }
  61. return poly ;
  62. }
  63. BoostGeometryTools::BoostPolygon BoostGeometryTools::translatePolygon(BoostGeometryTools::BoostPolygon& poly,float dx,float dy)
  64. {
  65. BoostPolygon newPoly = poly ;
  66. for(auto it = newPoly.outer().begin(); it!=newPoly.outer().end();++it ) {
  67. it->set<0>( it->get<0>() + dx ) ;
  68. it->set<1>( it->get<1>() + dy ) ;
  69. }
  70. return newPoly ;
  71. }
  72. BoostGeometryTools::BoostPolygon BoostGeometryTools::convex_hull(BoostGeometryTools::BoostPolygon& poly)
  73. {
  74. BoostGeometryTools::BoostPolygon hull ;
  75. boost::geometry::convex_hull(poly, hull) ;
  76. return hull ;
  77. }
  78. bool BoostGeometryTools::pointIntersetsBox(BoostGeometryTools::BoostPoint& ptA,BoostGeometryTools::BoostPolygon& polyB)
  79. {
  80. return boost::geometry::intersects(ptA,polyB) ;
  81. }