int srtsgns = (sortaxis.x >= 0 ? 1 : 0) +
(sortaxis.y >= 0 ? 2 : 0) +
(sortaxis.z >= 0 ? 4 : 0);
int inside = (1 << count) - 1;
ObjectArrayList<sStkNPS> stock = new ObjectArrayList<sStkNPS>();
IntArrayList ifree = new IntArrayList();
IntArrayList stack = new IntArrayList();
int[] signs = new int[/*sizeof(unsigned)*8*/4 * 8];
assert (count < (/*sizeof(signs)*/128 / /*sizeof(signs[0])*/ 4));
for (int i = 0; i < count; i++) {
signs[i] = ((normals[i].x >= 0) ? 1 : 0) +
((normals[i].y >= 0) ? 2 : 0) +
((normals[i].z >= 0) ? 4 : 0);
}
//stock.reserve(SIMPLE_STACKSIZE);
//stack.reserve(SIMPLE_STACKSIZE);
//ifree.reserve(SIMPLE_STACKSIZE);
stack.add(allocate(ifree, stock, new sStkNPS(root, 0, root.volume.ProjectMinimum(sortaxis, srtsgns))));
do {
// JAVA NOTE: check
int id = stack.remove(stack.size() - 1);
sStkNPS se = stock.getQuick(id);
ifree.add(id);
if (se.mask != inside) {
boolean out = false;
for (int i = 0, j = 1; (!out) && (i < count); ++i, j <<= 1) {
if (0 == (se.mask & j)) {
int side = se.node.volume.Classify(normals[i], offsets[i], signs[i]);
switch (side) {
case -1:
out = true;
break;
case +1:
se.mask |= j;
break;
}
}
}
if (out) {
continue;
}
}
if (policy.Descent(se.node)) {
if (se.node.isinternal()) {
Node[] pns = new Node[]{se.node.childs[0], se.node.childs[1]};
sStkNPS[] nes = new sStkNPS[]{new sStkNPS(pns[0], se.mask, pns[0].volume.ProjectMinimum(sortaxis, srtsgns)),
new sStkNPS(pns[1], se.mask, pns[1].volume.ProjectMinimum(sortaxis, srtsgns))
};
int q = nes[0].value < nes[1].value ? 1 : 0;
int j = stack.size();
if (fullsort && (j > 0)) {
/* Insert 0 */
j = nearest(stack, stock, nes[q].value, 0, stack.size());
stack.add(0);
//#if DBVT_USE_MEMMOVE
//memmove(&stack[j+1],&stack[j],sizeof(int)*(stack.size()-j-1));
//#else
for (int k = stack.size() - 1; k > j; --k) {
stack.set(k, stack.get(k - 1));
//#endif
}
stack.set(j, allocate(ifree, stock, nes[q]));
/* Insert 1 */
j = nearest(stack, stock, nes[1 - q].value, j, stack.size());
stack.add(0);
//#if DBVT_USE_MEMMOVE
//memmove(&stack[j+1],&stack[j],sizeof(int)*(stack.size()-j-1));
//#else
for (int k = stack.size() - 1; k > j; --k) {
stack.set(k, stack.get(k - 1));
//#endif
}
stack.set(j, allocate(ifree, stock, nes[1 - q]));
}
else {
stack.add(allocate(ifree, stock, nes[q]));
stack.add(allocate(ifree, stock, nes[1 - q]));
}
}
else {
policy.Process(se.node, se.value);
}
}
}
while (stack.size() != 0);
}
}