Misc fix + more tests (#1511)

This commit is contained in:
iam 2022-11-09 17:46:53 -05:00 committed by GitHub
parent 88dd901438
commit d496fcfe4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* This class performs ray tracing and iterates along blocks on a line
@ -43,6 +44,8 @@ public class BlockIterator implements Iterator<Point> {
this.start = start.add(0, yOffset, 0);
this.end = start.add(0, yOffset, 0).add(direction.normalize().mul(maxDistance)).apply(Vec.Operator.FLOOR);
if (this.direction.isZero()) this.foundEnd = true;
signums[0] = (short) Math.signum(direction.x());
signums[1] = (short) Math.signum(direction.y());
signums[2] = (short) Math.signum(direction.z());
@ -152,6 +155,8 @@ public class BlockIterator implements Iterator<Point> {
@Override
public Point next() {
if (foundEnd) throw new NoSuchElementException();
// If we have entries in the extra points queue, return those first
var res = extraPoints.isEmpty() ? updateClosest() : extraPoints.poll();
// If we have reached the end, set the flag

View File

@ -74,6 +74,33 @@ public class BlockIteratorTest {
assertFalse(iterator.hasNext());
}
@Test
public void testZeroVelocity() {
Vec s = new Vec(0, 0, 0);
Vec e = new Vec(0, 0, 0);
BlockIterator iterator = new BlockIterator(s, e, 0, 4);
assertFalse(iterator.hasNext());
}
@Test
public void testExactEnd() {
Vec s = new Vec(0.5, 0, 0.5);
Vec e = new Vec(0, 1, 0);
BlockIterator iterator = new BlockIterator(s, e, 0, 1);
assertEquals(new Vec(0, 0, 0), iterator.next());
assertEquals(new Vec(0, 1, 0), iterator.next());
assertFalse(iterator.hasNext());
}
@Test
public void testSameEnd() {
Vec s = new Vec(0.5, 0, 0.5);
Vec e = new Vec(0, 1, 0);
BlockIterator iterator = new BlockIterator(s, e, 0, 0.5);
assertEquals(new Vec(0, 0, 0), iterator.next());
assertFalse(iterator.hasNext());
}
@Test
public void test3dExtraCollection() {
Vec s = new Vec(0.1, 0.1, 0.1);