From 36fb89dead8d79b5b144d3035ea940ae6de9a719 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Fri, 13 Sep 2019 07:58:27 -0500 Subject: [PATCH] Making analagous function for finding the right parenthesis. --- include/openmc/cell.h | 4 ++++ src/cell.cpp | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/include/openmc/cell.h b/include/openmc/cell.h index 2cd98ada1a..f6524c6ee3 100644 --- a/include/openmc/cell.h +++ b/include/openmc/cell.h @@ -218,6 +218,10 @@ protected: static std::vector::iterator find_left_parenthesis(std::vector::iterator start, const std::vector& rpn); + static std::vector::iterator + find_right_parenthesis(std::vector::iterator start, + const std::vector& rpn); + }; //============================================================================== diff --git a/src/cell.cpp b/src/cell.cpp index 7ad48bd326..bc0acbe73d 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -642,6 +642,40 @@ CSGCell::find_left_parenthesis(std::vector::iterator start, return it; } +std::vector::iterator +CSGCell::find_right_parenthesis(std::vector::iterator start, + const std::vector& rpn) { + // start search at zero + int level = 0; + auto it = start; + while (it != rpn.end()) { + // look at two tokens at a time + int32_t one = *it; + int32_t two = *(it + 1); + + // decrement parenthesis level if there are two adjacent surfaces + if (one < OP_UNION && two < OP_UNION) { + level++; + // increment if there are two adjacent operators + } else if (one >= OP_UNION && two >= OP_UNION) { + level--; + } + + // if the level gets to zero, return the position + if (level == 0) { + // move the iterator back one before leaving the loop + // so that all tokens in the parenthesis are included + it++; + break; + } + + // continue loop, one token at a time + it++; + } + return it; +} + + void CSGCell::remove_complement_ops(std::vector& rpn) { auto it = std::find(rpn.begin(), rpn.end(), OP_COMPLEMENT); while (it != rpn.end()) { @@ -687,20 +721,17 @@ BoundingBox CSGCell::bounding_box_complex(std::vector rpn) { // add until last two tokens in the sub-rpn are operators // (indicates a right parenthesis) auto subrpn_start = it - 2; // include current tokens - auto subrpn_end = it; - while (!((*subrpn_end >= OP_UNION) && (*(subrpn_end + 1) >= OP_UNION))) { - subrpn_end++; - } + auto subrpn_end = find_right_parenthesis(subrpn_start, rpn); // create the subrpn, including the first of our current tokens - std::vector subrpn(subrpn_start, ++subrpn_end); + std::vector subrpn(subrpn_start, subrpn_end); // save the last operator, tells us how to combine this region // with our current bounding box int32_t op = *subrpn_end++; it = subrpn_end; - // get bounding box for the subrpn + // get bounding box for the sub-rpn BoundingBox sub_box = bounding_box_complex(subrpn); // combine the sub-rpn bounding box with our current cell box if (op == OP_UNION) {