RandomGridFiller.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // RandomGridFiller.hpp
  3. // Test
  4. //
  5. // Created by 高慕白 on 2024/12/3.
  6. //
  7. #ifndef RandomGridFiller_hpp
  8. #define RandomGridFiller_hpp
  9. #include <vector>
  10. #include <stdio.h>
  11. #include "FillResult.hpp"
  12. #include "contourdata.h"
  13. #include <unordered_map>
  14. #include "BoostGeometryTools.hpp"
  15. using std::unordered_map;
  16. using std::vector;
  17. struct RandomGridFiller {
  18. //输入数据的结构 宝石的外接矩形
  19. struct JewelBox {
  20. inline JewelBox():_cnt(0),_width(0),_height(0),_jewelTypeId(-1){}
  21. int _cnt ;//宝石个数
  22. float _width; //外接矩形宽度(像素)
  23. float _height ;//外接矩形高度(像素)
  24. int _jewelTypeId ;
  25. } ;
  26. int _seed ;
  27. //对外接口
  28. void fill( vector<JewelBox>& jewelsArray , //需要填充的宝石
  29. float bottomLeftX,float bottomLeftY,//盘子有效区域
  30. float topRightX,float topRightY, //盘子有效区域
  31. vector<FillResult>& results //尝试五组只选择一个覆盖度最小的填充结果
  32. );
  33. private:
  34. //潜在的点位
  35. struct DistancePosition {
  36. bool _isTaken ;
  37. float _distance ;
  38. ContourData::Point _point ;
  39. } ;
  40. //格点占用数据
  41. struct GridData {
  42. inline void reset(float blx,float bly,float sx,float sy,int nrows,int ncols){
  43. _bottomLeftX = blx;
  44. _bottomLeftY = bly;
  45. _stepX = sx ;
  46. _stepY = sy ;
  47. _rows.resize(nrows);
  48. for(auto it=_rows.begin();it!=_rows.end();++it)it->resize(ncols,false);
  49. }
  50. inline void clearValues(){for(auto it=_rows.begin();it!=_rows.end();++it)it->resize(it->size(),false); }
  51. vector< vector<bool> > _rows ;
  52. float _bottomLeftX, _bottomLeftY ;//左下角原点坐标
  53. float _stepX,_stepY ;//格点间隔
  54. } ;
  55. // 返回未填进去的宝石数量,目前看没有填进去已经表示本次填充失败了
  56. int randomFillOneRound( vector<JewelBox>& jewelsArray,
  57. float bottomLeftX,float bottomLeftY,//盘子有效区域
  58. float topRightX,float topRightY, //盘子有效区域
  59. vector<ContourData::Point> potPositions, //潜在的点位
  60. vector<FillResult>& results,
  61. float& jewDistanceSum //宝石间距离求和,用于衡量宝石间的分散程度
  62. ,const float gridWid //盘子子网格宽度
  63. ,const float gridhei //盘子子网格高度
  64. ,int& resultTotCov //返回全部宝石格点叠盖度之和
  65. ) ;
  66. //随机排序索引值
  67. vector<int> randIndices(const int count0 );
  68. //假定每个网格覆盖一次宝石其对应的int值加一,下面函数用于统计某个点位(x,y)(盘子内部坐标,与subGrid对齐)周边半径个网格覆盖值的求和。
  69. //最优条件是求和值为0。虽然宝石一般使用矩形描述,这里为了将点坐标覆盖的一个区域换算到 subGrid 格子数方便,使用圆形进行计算。
  70. [[deprecated]]
  71. int sumOfSubGridTakenCounts(const int x,const int y,const int radius,const float gridwid,const float gridhei,const vector<vector<int>>& rowColSubGrid);
  72. //为中心点位x,y,半径radius的覆盖范围的subgrid占用数加1。
  73. [[deprecated]]
  74. void subGridTakenCountsAddOne(const int x,const int y,const int radius,const float gridwid,const float gridhei,vector<vector<int>>& rowColSubGrid);
  75. //多边形盖住的格点数量
  76. int coverageGridCount( BoostGeometryTools::BoostPolygon& box, const GridData& grid ) ;
  77. //全部覆盖求和,excludeIndex 不做比较的索引值
  78. int totalCoverageGridCount(BoostGeometryTools::BoostPolygon& box, const vector<GridData>& grids , int excludeIndex=-1 ) ;
  79. //将覆盖的格点数据赋值
  80. void setGridCoverageByBox( BoostGeometryTools::BoostPolygon& box, GridData& grid ) ;
  81. //按距离从外到内构建随机点位
  82. void generateDistanceRandomSlotPositions(const float centerx,const float centery,const vector<ContourData::Point>& points, vector<DistancePosition>& results ) ;
  83. } ;
  84. #endif /* RandomGridFiller_hpp */