@@ -205,7 +205,7 @@ void add_precedence(std::vector<int32_t>& infix)
205205// ! This function uses the shunting-yard algorithm.
206206// ==============================================================================
207207
208- vector<int32_t > generate_rpn (int32_t cell_id, vector<int32_t > infix)
208+ vector<int32_t > generate_postfix (int32_t cell_id, vector<int32_t > infix)
209209{
210210 vector<int32_t > rpn;
211211 vector<int32_t > stack;
@@ -619,15 +619,14 @@ CSGCell::CSGCell(pugi::xml_node cell_node)
619619 }
620620 region_.shrink_to_fit ();
621621
622- region_infix_ = region_;
623622 // If this cell is simple, remove all the superfluous operator tokens.
624623 if (simple_) {
625- for (auto it = region_infix_ .begin (); it != region_infix_ .end (); it++) {
624+ for (auto it = region_ .begin (); it != region_ .end (); it++) {
626625 if (*it == OP_INTERSECTION || *it > OP_COMPLEMENT ) {
627- region_infix_ .erase (it);
626+ region_ .erase (it);
628627 }
629628 }
630- region_infix_ .shrink_to_fit ();
629+ region_ .shrink_to_fit ();
631630 }
632631
633632 // Read the translation vector.
@@ -672,7 +671,7 @@ std::pair<double, int32_t> CSGCell::distance(
672671 double min_dist {INFTY };
673672 int32_t i_surf {std::numeric_limits<int32_t >::max ()};
674673
675- for (int32_t token : region_infix_ ) {
674+ for (int32_t token : region_ ) {
676675 // Ignore this token if it corresponds to an operator rather than a region.
677676 if (token >= OP_UNION )
678677 continue ;
@@ -705,19 +704,18 @@ void CSGCell::to_hdf5_inner(hid_t group_id) const
705704 if (!region_.empty ()) {
706705 std::stringstream region_spec {};
707706 for (int32_t token : region_) {
708- if (token == OP_LEFT_PAREN ) {
709- region_spec << " (" ;
710- } else if (token == OP_RIGHT_PAREN ) {
711- region_spec << " )" ;
712- } else if (token == OP_COMPLEMENT ) {
713- region_spec << " ~" ;
714- } else if (token == OP_INTERSECTION ) {
715- } else if (token == OP_UNION ) {
716- region_spec << " |" ;
717- } else {
707+ if (token < OP_UNION ) {
718708 // Note the off-by-one indexing
719709 auto surf_id = model::surfaces[abs (token) - 1 ]->id_ ;
720710 region_spec << " " << ((token > 0 ) ? surf_id : -surf_id);
711+ } else if (token > OP_COMPLEMENT ) {
712+ if (token == OP_LEFT_PAREN ) {
713+ region_spec << " (" ;
714+ } else {
715+ region_spec << " )" ;
716+ }
717+ } else if (token == OP_UNION ) {
718+ region_spec << " |" ;
721719 }
722720 }
723721 write_string (group_id, " region" , region_spec.str (), false );
@@ -727,7 +725,7 @@ void CSGCell::to_hdf5_inner(hid_t group_id) const
727725BoundingBox CSGCell::bounding_box_simple () const
728726{
729727 BoundingBox bbox;
730- for (int32_t token : region_infix_ ) {
728+ for (int32_t token : region_ ) {
731729 bbox &= model::surfaces[abs (token) - 1 ]->bounding_box (token > 0 );
732730 }
733731 return bbox;
@@ -813,12 +811,12 @@ void CSGCell::remove_complement_ops(vector<int32_t>& infix)
813811 }
814812}
815813
816- BoundingBox CSGCell::bounding_box_complex (vector<int32_t > rpn )
814+ BoundingBox CSGCell::bounding_box_complex (vector<int32_t > postfix )
817815{
818- vector<BoundingBox> stack (rpn .size ());
816+ vector<BoundingBox> stack (postfix .size ());
819817 int i_stack = -1 ;
820818
821- for (auto & token : rpn ) {
819+ for (auto & token : postfix ) {
822820 if (token == OP_UNION ) {
823821 stack[i_stack - 1 ] = stack[i_stack - 1 ] | stack[i_stack];
824822 i_stack--;
@@ -837,14 +835,19 @@ BoundingBox CSGCell::bounding_box_complex(vector<int32_t> rpn)
837835
838836BoundingBox CSGCell::bounding_box () const
839837{
840- return simple_ ? bounding_box_simple () : bounding_box_complex (region_infix_);
838+ if (simple_) {
839+ return bounding_box_simple ();
840+ } else {
841+ auto postfix = generate_postfix (this ->id_ , this ->region_ );
842+ return bounding_box_complex (postfix);
843+ }
841844}
842845
843846// ==============================================================================
844847
845848bool CSGCell::contains_simple (Position r, Direction u, int32_t on_surface) const
846849{
847- for (int32_t token : region_infix_ ) {
850+ for (int32_t token : region_ ) {
848851 // Assume that no tokens are operators. Evaluate the sense of particle with
849852 // respect to the surface and see if the token matches the sense. If the
850853 // particle's surface attribute is set and matches the token, that
@@ -872,7 +875,7 @@ bool CSGCell::contains_complex(
872875 int total_depth = 0 ;
873876
874877 // For each token
875- for (auto it = region_infix_ .begin (); it != region_infix_ .end (); it++) {
878+ for (auto it = region_ .begin (); it != region_ .end (); it++) {
876879 int32_t token = *it;
877880
878881 // If the token is a surface evaluate the sense
0 commit comments