boxAutoGroup.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "cdrapp.h"
  2. #define GET_BOUNDING_BOX(box) \
  3. GetBoundingBox(&(box).x, &(box).y, &(box).w, &(box).h, false)
  4. #define ZERO_4PC 0, 0, 0, 0
  5. typedef struct {
  6. double x; // 左下角 x 坐标
  7. double y; // 左下角 y 坐标
  8. double w; // 宽度
  9. double h; // 高度
  10. } BoundingBox;
  11. // 扩展边界框
  12. void expand_bounding_boxes(std::vector<BoundingBox>& boxes, double exp) {
  13. for (auto& box : boxes) {
  14. // 扩展宽度和高度
  15. box.w += 2 * exp; // 左右各扩展
  16. box.h += 2 * exp; // 上下各扩展
  17. // 调整左下角坐标
  18. box.x -= exp; // 向左扩展
  19. box.y -= exp; // 向下扩展
  20. }
  21. }
  22. double get_bounding_box_area(BoundingBox box) { return box.w * box.h; }
  23. // 自定义比较函数,按 BoundingBox 的面积大小进行排序
  24. bool compare_bounding_boxes(const std::pair<int, BoundingBox> &a,
  25. const std::pair<int, BoundingBox> &b) {
  26. return get_bounding_box_area(a.second) > get_bounding_box_area(b.second);
  27. }
  28. // 查找父节点
  29. int find(std::vector<int>& parent, int x) {
  30. if (parent[x] != x) {
  31. parent[x] = find(parent, parent[x]);
  32. }
  33. return parent[x];
  34. }
  35. // 合并两个集合
  36. void unionSet(std::vector<int>& parent, int x, int y) {
  37. int xroot = find(parent, x);
  38. int yroot = find(parent, y);
  39. parent[xroot] = yroot;
  40. }
  41. // 检查两个矩形是否重叠
  42. bool isOverlapped(const BoundingBox &a, const BoundingBox &b) {
  43. return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
  44. } // 函数使用AABB(Axis-Aligned Bounding Box)碰撞检测
  45. bool BBox_DrawRectangle(corel *cdr, double exp) {
  46. BoundingBox box;
  47. auto sr = cdr->ActiveSelectionRange; // 获得选择范围
  48. auto al = cdr->ActiveLayer; // 获得当前层
  49. if (!sr || !al) return false;
  50. BeginOpt(cdr);
  51. auto srs = cdr->CreateShapeRange();
  52. // CorelDRAW Shapes 物件 Item 编号从1开始
  53. for (auto i = 0; i != sr->Count; i++) {
  54. sr->Shapes->Item[i + 1]->GET_BOUNDING_BOX(box); // 获得Shapes的BoundingBox,赋值到box
  55. if (fabs(exp) > 0.02 ) { box.w += 2 * exp; box.h += 2 * exp; box.x -= exp; box.y -= exp; }
  56. auto s = al->CreateRectangle2(box.x, box.y, box.w, box.h, ZERO_4PC); // 使用BoundingBox box 创建一个矩形
  57. s->Outline->Color->RGBAssign(0, 255, 0);
  58. srs->Add(s);
  59. }
  60. srs->CreateSelection();
  61. sprintf(infobuf, "提示: 标记画框数量: %d 个\n容差值请使用小键盘输入", srs->Count);
  62. EndOpt(cdr);
  63. return true;
  64. }
  65. bool AutoMakeSelection(corel *cdr) {
  66. auto sr = cdr->ActiveSelectionRange;
  67. if (0 == sr->Count) {
  68. auto all = cdr->ActiveDocument->ActivePage->Shapes->All();
  69. all->CreateSelection();
  70. }
  71. return true;
  72. }
  73. // 快速分组重叠的区域, 使用算法"Union-Find" 算法。这个算法可以有效地处理这种并集问题。
  74. // 算法的时间复杂度为 O(n^2),其中 n 是矩形的数量。如果矩形数量较多,可以考虑使用更高效的算法,
  75. // 例如使用四叉树(Quadtree)或者区间树(Interval Tree)等数据结构来加速计算。
  76. bool Box_AutoGroup(corel *cdr, double exp) {
  77. BoundingBox box;
  78. auto sr = cdr->ActiveSelectionRange; // 获得选择范围
  79. auto al = cdr->ActiveLayer; // 获得当前层
  80. if (!sr || !al) return false;
  81. auto start = std::chrono::high_resolution_clock::now(); // 开始时间
  82. BeginOpt(cdr);
  83. std::vector<BoundingBox> boxes;
  84. std::vector<int> parent;
  85. // CorelDRAW Shapes 物件 Item 编号从1开始
  86. for (auto i = 0; i != sr->Count; i++) {
  87. sr->Shapes->Item[i + 1]->GET_BOUNDING_BOX(box);
  88. boxes.push_back(box);
  89. parent.push_back(i);
  90. }
  91. // 扩展边界框,或者收缩边界框
  92. if (fabs(exp) > 0.02 ) {
  93. expand_bounding_boxes(boxes, exp);
  94. }
  95. // 实现 Union-Find 算法来合并重叠的区域
  96. for (int i = 0; i < boxes.size(); i++) {
  97. for (int j = i + 1; j < boxes.size(); j++) {
  98. if (isOverlapped(boxes[i], boxes[j])) {
  99. unionSet(parent, i, j);
  100. }
  101. }
  102. }
  103. double runtime[2] = {0,0};
  104. auto end = std::chrono::high_resolution_clock::now();
  105. std::chrono::duration<double> duration = end - start;
  106. runtime[0] = duration.count();
  107. // 输出分组结果到文件
  108. // std::ofstream output_file("D:\\group.txt");
  109. // if (output_file.is_open()) {
  110. // std::map<int, std::vector<int>> groups;
  111. // for (int i = 0; i < parent.size(); i++) {
  112. // int root = find(parent, i);
  113. // groups[root].push_back(i + 1); // CorelDRAW Shapes 物件 Item 编号从1开始
  114. // }
  115. // for (const auto& group : groups) {
  116. // output_file << "Group: ";
  117. // for (int index : group.second) {
  118. // output_file << index << " ";
  119. // }
  120. // output_file << std::endl;
  121. // }
  122. // auto end = std::chrono::high_resolution_clock::now(); // 结束时间
  123. // // 计算持续时间
  124. // std::chrono::duration<double> duration = end - start;
  125. // output_file << "Execution time: " << duration.count() << " seconds\n";
  126. // output_file.close();
  127. // }
  128. // 输出分组结果
  129. std::map<int, std::vector<int>> groups;
  130. for (int i = 0; i < parent.size(); i++) {
  131. int root = find(parent, i);
  132. groups[root].push_back(i + 1); // CorelDRAW Shapes 物件 Item 编号从1开始
  133. }
  134. auto srgp = cdr->CreateShapeRange();
  135. auto srs = cdr->CreateShapeRange();
  136. cdr->ActiveDocument->ClearSelection();
  137. // 原来 没有取消选择 最初速度
  138. // Execution time: 63.0305 seconds
  139. // srgp->GET_BOUNDING_BOX(box);
  140. // al->CreateRectangle2(box.x, box.y, box.w, box.h, ZERO_4PC); // 使用边界 创建一个矩形
  141. // box边界 转左上和右下坐标 box.x, box.y + box.h, box.x + box.w, box.y
  142. // auto sh = cdr->ActivePage->SelectShapesFromRectangle(box.x, box.y + box.h, box.x + box.w, box.y, false);
  143. // sh->Group();
  144. // 使用 SelectShapesFromRectangle 框选的形状进行群组
  145. // Execution time: 2.44753 seconds
  146. // cdr->ActiveDocument->ClearSelection(); // 使用取消选择
  147. // Execution time: 1.7432 seconds
  148. // srgp->CreateSelection();
  149. // cdr->ActiveSelectionRange->Group();
  150. // Execution time: 1.87662 seconds
  151. // 分组分别进行群组
  152. for (const auto& group : groups) {
  153. for (int index : group.second)
  154. srgp->Add(sr->Shapes->Item[index]);
  155. if(sr->Count >1)
  156. srs->Add(srgp->Group());
  157. else
  158. srs->AddRange(srgp);
  159. srgp->RemoveAll();
  160. }
  161. srs->CreateSelection();
  162. // 计算持续时间
  163. duration = std::chrono::high_resolution_clock::now() - start;
  164. runtime[1] = duration.count();
  165. sprintf(infobuf, "选择物件: %d 个, 分组: %.2f秒\n总共群组: %d 组, 总时间: %.2f秒", sr->Count, runtime[0] + 0.01, srs->Count, runtime[1] + 0.02);
  166. EndOpt(cdr);
  167. return true;
  168. }