From 6bcec6fa19e0707190f3d11cf5c9b979ddad2734 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Wed, 27 Mar 2019 21:24:46 -0400 Subject: [PATCH 1/2] Remove operator tokens from simple cell RPN --- src/cell.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/cell.cpp b/src/cell.cpp index c7b92c8294..45817de421 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -336,6 +336,16 @@ CSGCell::CSGCell(pugi::xml_node cell_node) } } + // If this cell is simple, remove all the superfluous operator tokens. + if (simple_) { + std::vector stack; + for (auto token : rpn_) { + if (token < OP_UNION) stack.push_back(token); + } + rpn_ = std::vector(stack.rbegin(), stack.rend()); + rpn_.shrink_to_fit(); + } + // Read the translation vector. if (check_for_node(cell_node, "translation")) { if (fill_ == C_NONE) { @@ -522,19 +532,17 @@ bool CSGCell::contains_simple(Position r, Direction u, int32_t on_surface) const { for (int32_t token : rpn_) { - if (token < OP_UNION) { - // If the token is not an operator, evaluate the sense of particle with - // respect to the surface and see if the token matches the sense. If the - // particle's surface attribute is set and matches the token, that - // overrides the determination based on sense(). - if (token == on_surface) { - } else if (-token == on_surface) { - return false; - } else { - // Note the off-by-one indexing - bool sense = model::surfaces[abs(token)-1]->sense(r, u); - if (sense != (token > 0)) {return false;} - } + // Assume that no tokens are operators. Evaluate the sense of particle with + // respect to the surface and see if the token matches the sense. If the + // particle's surface attribute is set and matches the token, that + // overrides the determination based on sense(). + if (token == on_surface) { + } else if (-token == on_surface) { + return false; + } else { + // Note the off-by-one indexing + bool sense = model::surfaces[abs(token)-1]->sense(r, u); + if (sense != (token > 0)) {return false;} } } return true; From 2dc02c57743ad1ec49868b73e2cc7d4e139b8cf1 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Sun, 31 Mar 2019 07:38:18 -0400 Subject: [PATCH 2/2] Remove unneeded rpn tokens in-place --- src/cell.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/cell.cpp b/src/cell.cpp index 45817de421..1baf7b44eb 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -325,7 +325,6 @@ CSGCell::CSGCell(pugi::xml_node cell_node) // Convert the infix region spec to RPN. rpn_ = generate_rpn(id_, region_); - rpn_.shrink_to_fit(); // Check if this is a simple cell. simple_ = true; @@ -338,13 +337,18 @@ CSGCell::CSGCell(pugi::xml_node cell_node) // If this cell is simple, remove all the superfluous operator tokens. if (simple_) { - std::vector stack; - for (auto token : rpn_) { - if (token < OP_UNION) stack.push_back(token); + size_t i0 = 0; + size_t i1 = 0; + while (i1 < rpn_.size()) { + if (rpn_[i1] < OP_UNION) { + rpn_[i0] = rpn_[i1]; + ++i0; + } + ++i1; } - rpn_ = std::vector(stack.rbegin(), stack.rend()); - rpn_.shrink_to_fit(); + rpn_.resize(i0); } + rpn_.shrink_to_fit(); // Read the translation vector. if (check_for_node(cell_node, "translation")) {