Skip to content

Commit 2602edc

Browse files
iscgarruevs
authored andcommitted
generate: add minor optimisation to request and constraint pruning
For request pruning, iterate over requests rather than over entities, and for constraint pruning look up in the requests list where possible, as it should be much shorter. While at it, combine them into a single operation. This saves us one potential recursion for every group, since there's no reason to wait for the next recursion to do constraint pruning when requests were pruned.
1 parent 9c3f489 commit 2602edc

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

src/generate.cpp

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,46 +86,53 @@ bool SolveSpaceUI::PruneGroups(hGroup hg) {
8686
return true;
8787
}
8888

89-
bool SolveSpaceUI::PruneRequests(hGroup hg) {
90-
const int requests = SK.request.n;
91-
for(Entity &e : SK.entity) {
92-
if(e.group != hg) {
93-
continue;
89+
bool SolveSpaceUI::PruneRequestsAndConstraints(hGroup hg) {
90+
auto entityRequestExists = [](hEntity he, bool checkEntity = false) {
91+
if(he == Entity::NO_ENTITY) {
92+
return true;
93+
}
94+
95+
if(he.isFromRequest()) {
96+
if(SK.request.FindByIdNoOops(he.request()) != nullptr) {
97+
return true;
98+
}
99+
} else if(!checkEntity || SK.entity.FindByIdNoOops(he) != nullptr) {
100+
return true;
94101
}
95102

96-
if(!e.h.isFromRequest()) {
103+
return false;
104+
};
105+
106+
const int requests = SK.request.n;
107+
for(Request &r : SK.request) {
108+
if(r.group != hg) {
97109
continue;
98110
}
99111

100-
if(EntityExists(e.workplane)) {
112+
if(entityRequestExists(r.workplane)) {
101113
continue;
102114
}
103115

104-
Request *r = SK.GetRequest(e.h.request());
105-
r->tag = 1;
116+
r.tag = 1;
106117
}
107118
SK.request.RemoveTagged();
108119
deleted.requests += requests - SK.request.n;
109120

110-
return requests > SK.request.n;
111-
}
112-
113-
bool SolveSpaceUI::PruneConstraints(hGroup hg) {
114121
const int constraints = SK.constraint.n;
115122
for(Constraint &c : SK.constraint) {
116-
if(c.group != hg) continue;
117-
118-
if(EntityExists(c.workplane) &&
119-
EntityExists(c.ptA) &&
120-
EntityExists(c.ptB) &&
121-
EntityExists(c.entityA) &&
122-
EntityExists(c.entityB) &&
123-
EntityExists(c.entityC) &&
124-
EntityExists(c.entityD)) {
123+
if(c.group != hg)
124+
continue;
125+
126+
if(entityRequestExists(c.workplane, true) &&
127+
entityRequestExists(c.ptA, true) &&
128+
entityRequestExists(c.ptB, true) &&
129+
entityRequestExists(c.entityA, true) &&
130+
entityRequestExists(c.entityB, true) &&
131+
entityRequestExists(c.entityC, true) &&
132+
entityRequestExists(c.entityD, true)) {
125133
continue;
126134
}
127135

128-
(deleted.constraints)++;
129136
if(c.type != Constraint::Type::POINTS_COINCIDENT &&
130137
c.type != Constraint::Type::HORIZONTAL &&
131138
c.type != Constraint::Type::VERTICAL) {
@@ -134,10 +141,10 @@ bool SolveSpaceUI::PruneConstraints(hGroup hg) {
134141

135142
c.tag = 1;
136143
}
137-
138144
SK.constraint.RemoveTagged();
145+
deleted.constraints += constraints - SK.constraint.n;
139146

140-
return constraints > SK.constraint.n;
147+
return (requests > SK.request.n) || (constraints > SK.constraint.n);
141148
}
142149

143150
void SolveSpaceUI::GenerateAll(Generate type, bool andFindFree, bool genForBBox) {
@@ -215,8 +222,7 @@ void SolveSpaceUI::GenerateAll(Generate type, bool andFindFree, bool genForBBox)
215222
// Remove any requests or constraints that refer to a nonexistent
216223
// group; can check those immediately, since we know what the list
217224
// of groups should be.
218-
while(PruneOrphans())
219-
;
225+
PruneOrphans();
220226

221227
// Don't lose our numerical guesses when we regenerate.
222228
IdList<Param,hParam> prev = {};
@@ -254,7 +260,7 @@ void SolveSpaceUI::GenerateAll(Generate type, bool andFindFree, bool genForBBox)
254260

255261
// The requests and constraints depend on stuff in this or the
256262
// previous group, so check them after generating.
257-
if(PruneRequests(hg) || PruneConstraints(hg))
263+
if(PruneRequestsAndConstraints(hg))
258264
goto pruned;
259265

260266
// Use the previous values for params that we've seen before, as

src/solvespace.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,7 @@ class SolveSpaceUI {
765765
bool EntityExists(hEntity he);
766766
bool GroupsInOrder(hGroup before, hGroup after);
767767
bool PruneGroups(hGroup hg);
768-
bool PruneRequests(hGroup hg);
769-
bool PruneConstraints(hGroup hg);
768+
bool PruneRequestsAndConstraints(hGroup hg);
770769
static void ShowNakedEdges(bool reportOnlyWhenNotOkay);
771770

772771
enum class Generate : uint32_t {

0 commit comments

Comments
 (0)