boxAutoGroup.cpp 6.8 KB

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