Making analagous function for finding the right parenthesis.

This commit is contained in:
Patrick Shriwise 2019-09-13 07:58:27 -05:00
parent 25df513a77
commit 36fb89dead
2 changed files with 41 additions and 6 deletions

View file

@ -218,6 +218,10 @@ protected:
static std::vector<int32_t>::iterator
find_left_parenthesis(std::vector<int32_t>::iterator start,
const std::vector<int32_t>& rpn);
static std::vector<int32_t>::iterator
find_right_parenthesis(std::vector<int32_t>::iterator start,
const std::vector<int32_t>& rpn);
};
//==============================================================================

View file

@ -642,6 +642,40 @@ CSGCell::find_left_parenthesis(std::vector<int32_t>::iterator start,
return it;
}
std::vector<int32_t>::iterator
CSGCell::find_right_parenthesis(std::vector<int32_t>::iterator start,
const std::vector<int32_t>& 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<int32_t>& 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<int32_t> 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<int32_t> subrpn(subrpn_start, ++subrpn_end);
std::vector<int32_t> 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) {