Class OctreeVolume

java.lang.Object
eu.svjatoslav.sixth.e3d.renderer.octree.OctreeVolume

public class OctreeVolume extends Object
Sparse voxel octree for 3D volume storage and ray tracing.

The octree represents a 3D volume with three cell types:

  • UNUSED - Empty cell, not yet allocated
  • SOLID - Contains color and illumination data
  • CLUSTER - Contains pointers to 8 child cells (for subdivision)

Cell data is stored in parallel arrays (cell1 through cell8) for memory efficiency. Each array stores different aspects of cell data.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int[]
    Cell data array 1: stores cell state and first child pointer.
    int[]
    Cell data array 2: stores color values.
    int[]
    Cell data array 3: stores illumination values.
    int[]
    Cell data array 4: stores child pointer 4.
    int[]
    Cell data array 5: stores child pointer 5.
    int[]
    Cell data array 6: stores child pointer 6.
    int[]
    Cell data array 7: stores child pointer 7.
    int[]
    Cell data array 8: stores child pointer 8.
    int
    Pointer to the next unused cell in the allocation buffer.
    int
    Size of the root (master) cell in world units.
    static final int
    Return value indicating no intersection during ray tracing.
    int
    Number of currently allocated cells.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new octree volume with default buffer size (1.5M cells) and master cell size of 256*64 units.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    breakSolidCell(int pointer)
    Subdivides a solid cell into 8 child cells, each with the same color and illumination.
    void
    clearCell(int pointer)
    Clears the cell.
    void
    deleteCell(int cellPointer)
    Marks a cell as deleted and returns it to the unused pool.
    int
    doesIntersect(int cubeX, int cubeY, int cubeZ, int cubeSize, Ray r)
    Tests whether a ray intersects with a cubic region.
    void
    Fills a 3D rectangular region with solid cells of the given color.
    int
    getCellColor(int pointer)
    Returns the color value stored in a solid cell.
    int
    getCellIllumination(int pointer)
    Returns the illumination value stored in a solid cell.
    int
    Scans cells arrays and returns pointer to found unused cell.
    void
    initWorld(int bufferLength, int masterCellSize)
    Initializes the octree storage arrays with the specified buffer size and root cell size.
    boolean
    isCellSolid(int pointer)
    Checks if the cell at the given pointer is a solid (leaf) cell.
    int
    makeNewCell(int color, int illumination)
    Allocates a new solid cell with the given color and illumination.
    void
    markCellAsSolid(int pointer)
    Mark cell as solid.
    void
    putCell(int x, int y, int z, Color color)
    Stores a voxel at the given world coordinates with the specified color.
    void
    setCellColor(int pointer, int color)
    Sets the color value for the cell at the given pointer.
    void
    setCellIllumination(int pointer, int illumination)
    Sets the illumination value for the cell at the given pointer.
    int
    traceCell(int cellX, int cellY, int cellZ, int cellSize, int pointer, Ray ray)
    Traces a ray through the octree to find an intersecting solid cell.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TRACE_NO_HIT

      public static final int TRACE_NO_HIT
      Return value indicating no intersection during ray tracing.
      See Also:
    • cell1

      public int[] cell1
      Cell data array 1: stores cell state and first child pointer.
    • cell2

      public int[] cell2
      Cell data array 2: stores color values.
    • cell3

      public int[] cell3
      Cell data array 3: stores illumination values.
    • cell4

      public int[] cell4
      Cell data array 4: stores child pointer 4.
    • cell5

      public int[] cell5
      Cell data array 5: stores child pointer 5.
    • cell6

      public int[] cell6
      Cell data array 6: stores child pointer 6.
    • cell7

      public int[] cell7
      Cell data array 7: stores child pointer 7.
    • cell8

      public int[] cell8
      Cell data array 8: stores child pointer 8.
    • cellAllocationPointer

      public int cellAllocationPointer
      Pointer to the next unused cell in the allocation buffer.
    • usedCellsCount

      public int usedCellsCount
      Number of currently allocated cells.
    • masterCellSize

      public int masterCellSize
      Size of the root (master) cell in world units.
  • Constructor Details

    • OctreeVolume

      public OctreeVolume()
      Creates a new octree volume with default buffer size (1.5M cells) and master cell size of 256*64 units.
  • Method Details

    • breakSolidCell

      public void breakSolidCell(int pointer)
      Subdivides a solid cell into 8 child cells, each with the same color and illumination.
      Parameters:
      pointer - the cell to break up
    • clearCell

      public void clearCell(int pointer)
      Clears the cell.
      Parameters:
      pointer - Pointer to the cell.
    • deleteCell

      public void deleteCell(int cellPointer)
      Marks a cell as deleted and returns it to the unused pool.
      Parameters:
      cellPointer - the cell to delete
    • doesIntersect

      public int doesIntersect(int cubeX, int cubeY, int cubeZ, int cubeSize, Ray r)
      Tests whether a ray intersects with a cubic region.
      Parameters:
      cubeX - the X center of the cube
      cubeY - the Y center of the cube
      cubeZ - the Z center of the cube
      cubeSize - the half-size of the cube
      r - the ray to test
      Returns:
      intersection type code, or 0 if no intersection
    • fillRectangle

      public void fillRectangle(IntegerPoint p1, IntegerPoint p2, Color color)
      Fills a 3D rectangular region with solid cells of the given color.
      Parameters:
      p1 - one corner of the rectangle
      p2 - the opposite corner of the rectangle
      color - the color to fill with
    • getCellColor

      public int getCellColor(int pointer)
      Returns the color value stored in a solid cell.
      Parameters:
      pointer - the cell pointer
      Returns:
      the packed RGB color value
    • getCellIllumination

      public int getCellIllumination(int pointer)
      Returns the illumination value stored in a solid cell.
      Parameters:
      pointer - the cell pointer
      Returns:
      the packed RGB illumination value
    • initWorld

      public void initWorld(int bufferLength, int masterCellSize)
      Initializes the octree storage arrays with the specified buffer size and root cell size.
      Parameters:
      bufferLength - the number of cells to allocate space for
      masterCellSize - the size of the root cell in world units
    • isCellSolid

      public boolean isCellSolid(int pointer)
      Checks if the cell at the given pointer is a solid (leaf) cell.
      Parameters:
      pointer - the cell pointer to check
      Returns:
      true if the cell is solid
    • getNewCellPointer

      public int getNewCellPointer()
      Scans cells arrays and returns pointer to found unused cell.
      Returns:
      pointer to found unused cell
    • makeNewCell

      public int makeNewCell(int color, int illumination)
      Allocates a new solid cell with the given color and illumination.
      Parameters:
      color - the color value for the new cell
      illumination - the illumination value for the new cell
      Returns:
      the pointer to the newly allocated cell
    • markCellAsSolid

      public void markCellAsSolid(int pointer)
      Mark cell as solid.
      Parameters:
      pointer - pointer to cell
    • putCell

      public void putCell(int x, int y, int z, Color color)
      Stores a voxel at the given world coordinates with the specified color.
      Parameters:
      x - the X coordinate
      y - the Y coordinate
      z - the Z coordinate
      color - the color of the voxel
    • setCellColor

      public void setCellColor(int pointer, int color)
      Sets the color value for the cell at the given pointer.
      Parameters:
      pointer - the cell pointer
      color - the color value to set
    • setCellIllumination

      public void setCellIllumination(int pointer, int illumination)
      Sets the illumination value for the cell at the given pointer.
      Parameters:
      pointer - the cell pointer
      illumination - the illumination value to set
    • traceCell

      public int traceCell(int cellX, int cellY, int cellZ, int cellSize, int pointer, Ray ray)
      Traces a ray through the octree to find an intersecting solid cell.
      Parameters:
      cellX - the X coordinate of the current cell center
      cellY - the Y coordinate of the current cell center
      cellZ - the Z coordinate of the current cell center
      cellSize - the size of the current cell
      pointer - the pointer to the current cell
      ray - the ray to trace
      Returns:
      pointer to intersecting cell or TRACE_NO_HIT if no intersection