123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- #include "cdrapp.h"
- #include <algorithm>
- #include <cstdio>
- #include <map>
- #include <vector>
- #include <iostream>
- #include <fstream>
- #include <chrono>
- #include <math.h>
- #define GET_BOUNDING_BOX(box) \
- GetBoundingBox(&(box).x, &(box).y, &(box).w, &(box).h, false)
- #define ZERO_4PC 0, 0, 0, 0
- typedef struct {
- double x;
- double y;
- double w;
- double h;
- } BoundingBox;
- void expand_bounding_boxes(std::vector<BoundingBox>& boxes, double exp) {
- for (auto& box : boxes) {
-
- box.w += 2 * exp;
- box.h += 2 * exp;
-
- box.x -= exp;
- box.y -= exp;
- }
- }
- double get_bounding_box_area(BoundingBox box) { return box.w * box.h; }
- bool compare_bounding_boxes(const std::pair<int, BoundingBox> &a,
- const std::pair<int, BoundingBox> &b) {
- return get_bounding_box_area(a.second) > get_bounding_box_area(b.second);
- }
- int find(std::vector<int>& parent, int x) {
- if (parent[x] != x) {
- parent[x] = find(parent, parent[x]);
- }
- return parent[x];
- }
- void unionSet(std::vector<int>& parent, int x, int y) {
- int xroot = find(parent, x);
- int yroot = find(parent, y);
- parent[xroot] = yroot;
- }
- bool isOverlapped(const BoundingBox &a, const BoundingBox &b) {
- 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;
- }
- bool BBox_DrawRectangle(corel *cdr, double exp) {
- BoundingBox box;
- auto sr = cdr->ActiveSelectionRange;
- auto al = cdr->ActiveLayer;
- if (!sr || !al) return false;
-
- BeginOpt(cdr);
- auto srs = cdr->CreateShapeRange();
-
- for (auto i = 0; i != sr->Count; i++) {
- sr->Shapes->Item[i + 1]->GET_BOUNDING_BOX(box);
- if (fabs(exp) > 0.02 ) { box.w += 2 * exp; box.h += 2 * exp; box.x -= exp; box.y -= exp; }
-
- auto s = al->CreateRectangle2(box.x, box.y, box.w, box.h, ZERO_4PC);
- s->Outline->Color->RGBAssign(0, 255, 0);
- srs->Add(s);
- }
- srs->CreateSelection();
-
- sprintf(infobuf, "提示: 标记画框数量: %d 个\n容差值请使用小键盘输入", srs->Count);
- EndOpt(cdr);
- return true;
- }
- bool AutoMakeSelection(corel *cdr) {
- auto sr = cdr->ActiveSelectionRange;
- if (0 == sr->Count) {
- auto all = cdr->ActiveDocument->ActivePage->Shapes->All();
- all->CreateSelection();
- }
- return true;
- }
- bool Box_AutoGroup(corel *cdr, double exp) {
- BoundingBox box;
- auto sr = cdr->ActiveSelectionRange;
- auto al = cdr->ActiveLayer;
- if (!sr || !al) return false;
- auto start = std::chrono::high_resolution_clock::now();
- BeginOpt(cdr);
- std::vector<BoundingBox> boxes;
- std::vector<int> parent;
-
- for (auto i = 0; i != sr->Count; i++) {
- sr->Shapes->Item[i + 1]->GET_BOUNDING_BOX(box);
- boxes.push_back(box);
- parent.push_back(i);
- }
-
- if (fabs(exp) > 0.02 ) {
- expand_bounding_boxes(boxes, exp);
- }
-
- for (int i = 0; i < boxes.size(); i++) {
- for (int j = i + 1; j < boxes.size(); j++) {
- if (isOverlapped(boxes[i], boxes[j])) {
- unionSet(parent, i, j);
- }
- }
- }
- double runtime[2] = {0,0};
- auto end = std::chrono::high_resolution_clock::now();
- std::chrono::duration<double> duration = end - start;
- runtime[0] = duration.count();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- std::map<int, std::vector<int>> groups;
- for (int i = 0; i < parent.size(); i++) {
- int root = find(parent, i);
- groups[root].push_back(i + 1);
- }
- auto srgp = cdr->CreateShapeRange();
- auto srs = cdr->CreateShapeRange();
- cdr->ActiveDocument->ClearSelection();
-
- for (const auto& group : groups) {
- for (int index : group.second)
- srgp->Add(sr->Shapes->Item[index]);
-
- if(sr->Count >1)
- srs->Add(srgp->Group());
- else
- srs->AddRange(srgp);
- srgp->RemoveAll();
- }
- srs->CreateSelection();
-
- duration = std::chrono::high_resolution_clock::now() - start;
- runtime[1] = duration.count();
- sprintf(infobuf, "选择物件: %d 个, 分组: %.2f秒\n总共群组: %d 组, 总时间: %.2f秒", sr->Count, runtime[0] + 0.01, srs->Count, runtime[1] + 0.02);
- EndOpt(cdr);
- return true;
- }
|