replace DhSectionPos getCenter() with getCenterBlockPos()

This commit is contained in:
James Seibel
2023-09-14 20:07:25 -05:00
parent 96a39bce69
commit 4d863adac5
7 changed files with 78 additions and 46 deletions
@@ -44,7 +44,6 @@ import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
public class FullDataFileHandler implements IFullDataSourceProvider
{
@@ -67,9 +67,11 @@ public class DhBlockPos2D
// calculations //
//==============//
public double dist(DhBlockPos2D other) { return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.z - other.z, 2)); }
public double dist(DhBlockPos2D other) { return this.dist(other.x, other.z); }
public double dist(int x, int z) { return Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.z - z, 2)); }
public long distSquared(DhBlockPos2D other) { return MathUtil.pow2((long) this.x - other.x) + MathUtil.pow2((long) this.z - other.z); }
public long distSquared(DhBlockPos2D other) { return this.distSquared(other.x, other.z); }
public long distSquared(int x, int z) { return MathUtil.pow2((long) this.x - x) + MathUtil.pow2((long) this.z - z); }
@@ -143,31 +143,6 @@ public class DhSectionPos
// getters //
//=========//
/** Returns the center for detail level 0 */
public DhLodPos getCenter() { return this.getCenter((byte) 0); } // TODO why does this use detail level 0 instead of this object's detail level?
public DhLodPos getCenter(byte returnDetailLevel)
{
LodUtil.assertTrue(returnDetailLevel <= this.sectionDetailLevel, "returnDetailLevel must be less than sectionDetail");
if (returnDetailLevel == this.sectionDetailLevel)
{
return new DhLodPos(this.sectionDetailLevel, this.sectionX, this.sectionZ);
}
byte detailLevelOffset = (byte) (this.sectionDetailLevel - returnDetailLevel);
// we can't get the center of the position at block level, only attempt to get the position offset for detail levels above 0 // TODO should this also apply to detail level 1 or is it fine?
int positionOffset = 0;
if (this.sectionDetailLevel != 1 || returnDetailLevel != 0)
{
positionOffset = BitShiftUtil.powerOfTwo(detailLevelOffset - 1);
}
return new DhLodPos(returnDetailLevel,
(this.sectionX * BitShiftUtil.powerOfTwo(detailLevelOffset)) + positionOffset,
(this.sectionZ * BitShiftUtil.powerOfTwo(detailLevelOffset)) + positionOffset);
}
/** @return the corner with the smallest X and Z coordinate */
public DhLodPos getCorner() { return this.getCorner((byte) (this.sectionDetailLevel - 1)); }
/** @return the corner with the smallest X and Z coordinate */
@@ -202,6 +177,32 @@ public class DhSectionPos
public int getBlockWidth() { return BitShiftUtil.powerOfTwo(this.sectionDetailLevel); }
public DhBlockPos2D getCenterBlockPos() { return new DhBlockPos2D(this.getCenterBlockPosX(), this.getCenterBlockPosZ()); }
public int getCenterBlockPosX() { return this.getCenterBlockPos(true); }
public int getCenterBlockPosZ() { return this.getCenterBlockPos(false); }
private int getCenterBlockPos(boolean returnX)
{
int centerBlockPos = returnX ? this.sectionX : this.sectionZ;
if (this.sectionDetailLevel == 0)
{
// already at block detail level, no conversion necessary
return centerBlockPos;
}
// we can't get the center of the position at block level, only attempt to get the position offset for detail levels above 0
int positionOffset = 0;
if (this.sectionDetailLevel != 1)
{
positionOffset = BitShiftUtil.powerOfTwo(this.sectionDetailLevel - 1);
}
return (centerBlockPos * BitShiftUtil.powerOfTwo(this.sectionDetailLevel)) + positionOffset;
}
//==================//
// parent child pos //
//==================//
@@ -320,7 +320,7 @@ public class LodQuadTree extends QuadTree<LodRenderSection> implements AutoClose
* @param sectionPos section position
* @return detail level of this section pos
*/
public byte calculateExpectedDetailLevel(DhBlockPos2D playerPos, DhSectionPos sectionPos) { return this.getDetailLevelFromDistance(playerPos.dist(sectionPos.getCenter().getCenterBlockPos())); }
public byte calculateExpectedDetailLevel(DhBlockPos2D playerPos, DhSectionPos sectionPos) { return this.getDetailLevelFromDistance(playerPos.dist(sectionPos.getCenterBlockPosX(), sectionPos.getCenterBlockPosZ())); }
private byte getDetailLevelFromDistance(double distance)
{
// special case, never drop the quality
@@ -118,13 +118,13 @@ public class RenderBufferHandler
}
}
Pos2D cPos = lodQuadTree.getCenterBlockPos().toPos2D();
Pos2D cPos = this.lodQuadTree.getCenterBlockPos().toPos2D();
// Now that we have the axis directions, we can sort the render list
Comparator<LoadedRenderBuffer> farToNearComparator = (loadedBufferA, loadedBufferB) ->
{
Pos2D aPos = loadedBufferA.pos.getCenter().getCenterBlockPos().toPos2D();
Pos2D bPos = loadedBufferB.pos.getCenter().getCenterBlockPos().toPos2D();
Pos2D aPos = loadedBufferA.pos.getCenterBlockPos().toPos2D();
Pos2D bPos = loadedBufferB.pos.getCenterBlockPos().toPos2D();
if (true)
{
int aManhattanDistance = aPos.manhattanDist(cPos);
@@ -179,7 +179,7 @@ public class QuadNode<T>
if (!this.sectionPos.contains(inputSectionPos))
{
LOGGER.error((replaceValue ? "set " : "get ") + inputSectionPos + " center block: " + inputSectionPos.getCenter().getCornerBlockPos() + ", this pos: " + this.sectionPos + " this center block: " + this.sectionPos.getCenter().getCornerBlockPos());
LOGGER.error((replaceValue ? "set " : "get ") + inputSectionPos + " center block: " + inputSectionPos.getCenterBlockPos() + ", this pos: " + this.sectionPos + " this center block: " + this.sectionPos.getCenterBlockPos());
throw new IllegalArgumentException("Input section pos " + inputSectionPos + " outside of this quadNode's pos: " + this.sectionPos + ", this node's blockPos: " + this.sectionPos.convertNewToDetailLevel(LodUtil.BLOCK_DETAIL_LEVEL) + " block width: " + this.sectionPos.getBlockWidth() + " input detail level: " + inputSectionPos.convertNewToDetailLevel(LodUtil.BLOCK_DETAIL_LEVEL) + " width: " + inputSectionPos.getBlockWidth());
}
+43 -13
View File
@@ -130,17 +130,17 @@ public class DhSectionPosTest
public void GetCenterTest()
{
DhSectionPos node = new DhSectionPos((byte) 1, 2303, 0);
DhLodPos centerNode = node.getCenter();
DhLodPos expectedCenterNode = new DhLodPos((byte) 0, 4606, 0);
Assert.assertEquals("", expectedCenterNode, centerNode);
DhBlockPos2D centerBlockPos = node.getCenterBlockPos();
DhBlockPos2D expectedCenterNode = new DhBlockPos2D(4606, 0);
Assert.assertEquals("", expectedCenterNode, centerBlockPos);
node = new DhSectionPos((byte) 10, 0, 0); // 1024 blocks wide
centerNode = node.getCenter();
expectedCenterNode = new DhLodPos((byte) 0, 1024 / 2, 1024 / 2);
Assert.assertEquals("", expectedCenterNode, centerNode);
centerBlockPos = node.getCenterBlockPos();
expectedCenterNode = new DhBlockPos2D(1024 / 2, 1024 / 2);
Assert.assertEquals("", expectedCenterNode, centerBlockPos);
}
@Test
@@ -150,11 +150,11 @@ public class DhSectionPosTest
DhSectionPos inputPos = new DhSectionPos((byte) 0, 4606, 0); // width 1 block
Assert.assertTrue(parentNode.contains(inputPos));
DhLodPos parentCenter = parentNode.getCenter();
DhLodPos inputCenter = inputPos.getCenter();
DhBlockPos2D parentCenter = parentNode.getCenterBlockPos();
DhBlockPos2D inputCenter = inputPos.getCenterBlockPos();
Assert.assertEquals(new DhLodPos((byte) 0, 4606, 2), parentCenter);
Assert.assertEquals(new DhLodPos((byte) 0, 4606, 0), inputCenter);
Assert.assertEquals(new DhBlockPos2D(4606, 2), parentCenter);
Assert.assertEquals(new DhBlockPos2D(4606, 0), inputCenter);
}
@@ -273,7 +273,6 @@ public class DhSectionPosTest
Assert.assertEquals(2, sectionPos.getWidthCountForLowerDetailedSection(returnDetailLevel));
// 2 -> 1
returnDetailLevel = 1;
originSectionPos = originSectionPos.convertNewToDetailLevel((byte) 2);
@@ -283,7 +282,6 @@ public class DhSectionPosTest
Assert.assertEquals(2, sectionPos.getWidthCountForLowerDetailedSection(returnDetailLevel));
// Block -> 0
returnDetailLevel = 0;
originSectionPos = originSectionPos.convertNewToDetailLevel(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL);
@@ -329,4 +327,36 @@ public class DhSectionPosTest
Assert.assertEquals(32768, sectionPos.getBlockWidth());
}
@Test
public void GetCenterBlockPos()
{
DhSectionPos originSectionPos = new DhSectionPos((byte) 0,0,0);
DhSectionPos sectionPos = new DhSectionPos((byte) 0,-10000,5000);
Assert.assertEquals(new DhBlockPos2D(0, 0), originSectionPos.getCenterBlockPos());
Assert.assertEquals(new DhBlockPos2D(-10000, 5000), sectionPos.getCenterBlockPos());
originSectionPos = originSectionPos.convertNewToDetailLevel((byte) 1);
Assert.assertEquals(new DhBlockPos2D(0, 0), originSectionPos.getCenterBlockPos());
sectionPos = sectionPos.convertNewToDetailLevel((byte) 1);
Assert.assertEquals(new DhBlockPos2D(-10000, 5000), sectionPos.getCenterBlockPos());
originSectionPos = originSectionPos.convertNewToDetailLevel(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL);
Assert.assertEquals(new DhBlockPos2D(32, 32), originSectionPos.getCenterBlockPos());
sectionPos = sectionPos.convertNewToDetailLevel(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL);
Assert.assertEquals(new DhBlockPos2D(-10016, 5024), sectionPos.getCenterBlockPos());
originSectionPos = originSectionPos.convertNewToDetailLevel(DhSectionPos.SECTION_REGION_DETAIL_LEVEL);
Assert.assertEquals(new DhBlockPos2D(16384, 16384), originSectionPos.getCenterBlockPos());
sectionPos = sectionPos.convertNewToDetailLevel(DhSectionPos.SECTION_REGION_DETAIL_LEVEL);
Assert.assertEquals(new DhBlockPos2D(-16384, 16384), sectionPos.getCenterBlockPos());
}
}