Hello Marco,
I looked at your bool_eval() prototype implementation and made some minor
adjustments:
- moved NOT, GUARD, XNOP handlers from the 3rd switch to the 2nd switch
statement to reduce the amount of tests in those cases.
- renamed some variables.
- removed a temporary variable.
- changed the stack pointer assignment expressions.
- changed 'idx' variable type.

Code below.

===
HIDDEN int
bool_eval(struct bit_tree *treep, struct partition *partp, struct resource
*resp)
/* Tree to evaluate */
/* Partition to evaluate */
/* XOR true (and overlap) return */
/* resource pointer for this CPU */
{
    static int stack[BOOL_STACKSIZE*MAX_PSW];
    int *sp;
    int ret;
    uint uop;
    int idx;

    sp = &stack[BOOL_STACKSIZE*resp->re_cpu];
    *sp++ = INT_MAX;
    idx = 0;
    for (;;) {
    for (;;) {
        uop = treep[idx].val & 7;

        switch (uop) {
        case UOP_SOLID:
            {
            /* Tree Leaf */
            register const uint st_bit = treep[idx].val >> 3;
            register struct seg **segpp;
            ret = 0;
            for (BU_PTBL_FOR(segpp, (struct seg **), &partp->pt_seglist)) {
                if ((*segpp)->seg_stp->st_bit == st_bit) {
                ret = 1;
                break;
                }
            }
            }
            break;
        case UOP_UNION:
        case UOP_INTERSECT:
        case UOP_SUBTRACT:
        case UOP_XOR:
            *sp++ = idx;
            idx++;
            continue;
        default:
            bu_log("bool_eval:  bad sp op [%d]\n", uop);
            return BOOL_TRUE;    /* screw up output */
        }
        break;
    }

    for (;;) {
        idx = *--sp;

        switch (idx) {
        case INT_MAX:
            return ret;        /* top of tree again */
        case -1:
            /* Read values from tree_not */
            /* Special operation for subtraction */
            ret = !ret;
            continue;
        case -2:
            /* Read values from tree_guard */
            /*
             * Special operation for XOR.  lhs was true.  If rhs
             * subtree was true, an overlap condition exists (both
             * sides of the XOR are BOOL_TRUE).  Return error
             * condition.  If subtree is false, then return BOOL_TRUE
             * (from lhs).
             */
            if (ret) {
            /* sped temp val: rhs */
            return -1;    /* GUARD error */
            }
            ret = BOOL_TRUE;
            sp--;            /* pop temp val */
            continue;
        case -3:
            /* Read values from tree_xnop */
            /*
             * Special NOP for XOR.  lhs was false.  If rhs is true,
             * take note of its regionp.
             */
            sp--;            /* pop temp val */
            continue;
        default:
            break;
        }

        uop = treep[idx].val & 7;

        /*
         * Here, each operation will look at the operation just completed
         * (the left branch of the tree generally), and rewrite the top of
         * the sp and/or branch accordingly.
         */
        switch (uop) {
        case UOP_SOLID:
            bu_log("bool_eval:  pop SOLID?\n");
            return BOOL_TRUE;        /* screw up output */
        case UOP_UNION:
            if (ret) continue;        /* BOOL_TRUE, we are done */
            /* lhs was false, rewrite as rhs tree */
            idx = treep[idx].val >> 3;
            break;
        case UOP_INTERSECT:
            if (!ret) {
            ret = BOOL_FALSE;
            continue;
            }
            /* lhs was true, rewrite as rhs tree */
            idx = treep[idx].val >> 3;
            break;
        case UOP_SUBTRACT:
            if (!ret) continue;    /* BOOL_FALSE, we are done */
            /* lhs was true, rewrite as NOT of rhs tree */
            /* We introduce the special NOT operator here */
            *sp++ = -1;
            idx = treep[idx].val >> 3;
            break;
        case UOP_XOR:
            if (ret) {
            /* lhs was true, rhs better not be, or we have an
             * overlap condition.  Rewrite as guard node followed
             * by rhs.
             */
            idx = treep[idx].val >> 3;
            *sp++ = idx;        /* temp val for guard node */
            *sp++ = -2;
            } else {
            /* lhs was false, rewrite as xnop node and result of
             * rhs.
             */
            idx = treep[idx].val >> 3;
            *sp++ = idx;        /* temp val for xnop */
            *sp++ = -3;
            }
            break;
        default:
            bu_log("bool_eval:  bad pop op [%d]\n", uop);
            return BOOL_TRUE;        /* screw up output */
        }
        break;
    }
    }
    /* NOTREACHED */
}
===

-- 
Vasco Alexandre da Silva Costa
PhD in Computer Engineering (Computer Graphics)
Instituto Superior T├ęcnico/University of Lisbon, Portugal
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
BRL-CAD Developer mailing list
brlcad-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/brlcad-devel

Reply via email to