Advertisement
Guest User

Sparse Boxel Octree

a guest
May 7th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.59 KB | None | 0 0
  1. #ifndef clSparseBoxelOctree_h__
  2. #define clSparseBoxelOctree_h__
  3.  
  4. #include "clBitList.h"
  5. #include "clHashMap.h"
  6. #include "clTri3I32.h"
  7. #include "clTriangle3.h"
  8. #include "clFileStream.h"
  9. #include "clKeyValuePair.h"
  10. #include "clHTTPFileStream.h"
  11. #include "clSparseBoxelOctreeHelper.h"
  12. #include "clTextureStreamingService.h"
  13. #include "clSparseBoxelOctreeVisitor.h"
  14. #include "clSparseBoxelOctreeMomento.h"
  15. #include "clSparseBoxelOctreeRemoteData.h"
  16. #include "clSparseBoxelOctreeCompression.h"
  17.  
  18. class clImage;
  19. struct clVoxel;
  20. struct clBoxel;
  21. struct clTriangle;
  22. class clDepthImage;
  23. template<typename T> class clFrustum;
  24.  
  25. class clSparseBoxelOctree
  26. {
  27. public:
  28.   class Region
  29.   {
  30.   public:
  31.     i64 m_lastUsed = 0;
  32.     clBitList m_occupancy;
  33.     bool m_modified = false;
  34.     clList<clBoxel> m_cliff;
  35.     clList<clVoxel> m_voxelCache;
  36.     clList<clBoxel> m_boxelCache;
  37.     clList<clTri3I32> m_triangleCache;
  38.   };
  39.   static const i64 regionChildCount = 8;
  40.   static const i64 regionResolution = 64;
  41.  
  42.   static const clString compressionModeDescriptions[4];
  43.  
  44.   clSparseBoxelOctree(const clPath &projectFolder, const clString &remoteDatasetURL = "");
  45.   ~clSparseBoxelOctree();
  46.  
  47.   void Save();
  48.   void Clear();
  49.   void UnCache();
  50.  
  51.   i64 ActiveLayerCount() const;
  52.   i64 UniqueVoxelCount() const;
  53.   i64 SourceVoxelCount() const;
  54.   clPath CurrentProjectPath() const;
  55.   ui8 ActiveDataCompressionMode() const;
  56.  
  57.   clImage GeneratePreview(const i32 &resolution, const float zoom, float camYaw = clDegreesToRadians(45), float camPitch = clDegreesToRadians(45), clDepthImage *pDepthImage = nullptr);
  58.   clImage GeneratePreview(const i32 &resolution = 64);
  59.  
  60.   clAABB3<i32> CalculateEncompassingBoundsApproximate(const ui8 &bitsOfResolution);
  61.  
  62.   void GenerateRemoteStreamable(const clPath &outputLSD);
  63.  
  64.   void CrushFileStream();
  65.  
  66.   void Undo();
  67.   void Redo();
  68.   bool CanUndo();
  69.   bool CanRedo();
  70.   void CommitAction();
  71.  
  72.   void ResolveVoxelCaches();
  73.   bool HasCachedData(const Region &region);
  74.   i64 SetTexture(const clImage &texture);
  75.   void GenerateBlendedColors(ui8 minLayer = 0);
  76.  
  77.   void GetBoxels(clWriteStream *pWriter);
  78.   void GetVoxels(clWriteStream *pWriter);
  79.   void GetBoxels(clWriteStream *pWriter, const ui8 &numLayers);
  80.   void GetBoxels(clWriteStream *pWriter, const clAABB3<i32> &area);
  81.   void GetBoxels(clWriteStream *pWriter, const clFrustum<float> &frustum);
  82.   void GetBoxels(clList<clBoxel> *pBoxels, const clVec3I &regionPosition, const ui8 &regionLayer, bool *pRegionWasBuiltFromCache = nullptr);
  83.  
  84.   void SetVoxel(const clVoxel &voxel, const bool &saveUndoData = false);
  85.   void SetBoxel(const clBoxel &boxel, const bool &saveUndoData = false);
  86.   //void GetTriangles(clWriteStream *pWriter, const clFrustum<float> &frustum);
  87.   i64 RemoveVoxels(const clFrustum<float> &frustum, clImage *pMask = nullptr, const bool &saveUndoData = false);
  88.   //clList<clVoxel> GetVoxels(const clVec3I &regionPosition, const ui8 &regionLayer, bool *pRegionWasBuiltFromCache = nullptr);
  89.   void SetTriangle(const clVector3<clVec3I> &position, const clVector3<ui32> &color, const clVector3<clVec2> &textureCoordinates = { {}, {}, {} }, const i64 &texture = -1);
  90.   void Visit(clBoxelVisitor visitor, i32 neighbourhoodRadius = 0, clAABB3<i32> area = clAABB3<i32>::Largest(), const bool &visitEmptyvoxels = false, const bool &visitEmptyNeighbourhoods = false, const i64 &stride = 1);
  91.  
  92. private:
  93.   void RegionCliffTriangleRecursiveIntersection(clBitList &occupancy, clList<clBoxel>* pBoxels, const clTri3I32 &triangle, const clVec3 &triNorm, const TriBoxIntersection &triBoxIntersection, const clAABB3<i32> &triangleBounds, const clVec3I &regionPosition, const ui8 &regionLayer, const clVec3I &subRegionPosition = clVec3I::Zero(), const i64 &subRegionLayer = clLog2Fast((ui32)regionResolution));
  94.   void RegionCliffTriangle(clBitList &occupancy, clList<clBoxel> *pBoxels, const clTri3I32 &triangle, const TriBoxIntersection &triBoxIntersection, const clVec3I &regionPosition, const ui8 &regionLayer);
  95.   void RegionCliffFromCache(clList<clBoxel> *pBoxels, Region *pRegion, const clVec3I &regionPosition, const ui8 &regionLayer, const bool &applyPreferedSpliting, bool *pRegionWasBuiltFromCache);
  96.   void CliffRegion(clList<clBoxel> *pBoxels, const clVec3I &regionPosition, const ui8 &regionLayer, const bool &applyPreferedSpliting = true, bool *pRegionWasBuiltFromCache = nullptr);
  97.   i64 RemoveVoxelsRecursiveIntersectFrustum(const clFrustum<float> &frustum, clVec3I position, ui8 layer, clImage *pMask = nullptr, const bool &saveUndoData = false);
  98.   void AddToLayers(const clTri3I32 &triangle, const TriBoxIntersection &triBoxIntersection, i64 startingLayer = -1, const clVec3I &regionPos = clVec3I::Zero());
  99.   void GetBoxelsRecursive(clWriteStream *pWriter, const clVec3I &regionPosition, const ui8 &regionLayer, const clAABB3<i32> &area);
  100.   void GetBoxelsRecursive(clWriteStream *pWriter, clVec3I position, ui8 layer, bool parentExists = true, bool voxelMode = false);
  101.   void GetBoxelsRecursiveIntersectFrustum(const clFrustum<float> &frustum, clWriteStream *pWriter, clVec3I position, ui8 layer);
  102.   void GenerateBlendedColorsRecursive(clVec3I regionPosition = clVec3I::Zero(), ui8 minLayer = 0, ui8 regionLayer = 255);
  103.   Region* CacheRegion(const clVec3I &regionPosition, const ui8 &regionLayer, const bool &create = false);
  104.   void GetVoxelPreviewRecursive(clWriteStream *pWriter, clVec3I position, ui8 layer, ui8 stopLayer);
  105.   void RemoveFromLayers(const clBoxel &boxel, i64 startingLayer = 0, const bool &saveUndoData = false);
  106.   ui32 SampleTriangle(const clTri3I32 &triangle, const clVec3 &weights, const ui8 &regionLayer);
  107.   void AddToLayers(const clVoxel &voxel, i64 startingLayer = -1, const bool &saveUndoData = false);
  108.   void AddToLayers(const clBoxel &boxel, i64 startingLayer = -1, const bool &saveUndoData = false);
  109.   void SplitRegionCache(Region *pRegion, const clVec3I &regionPosition, const ui8 &regionLayer);
  110.   void UnloadRegion(Region *pRegion, const clVec3I &regionPosition, const ui8 &regionLayer);
  111.   clPath RegionFileName(const clVec3I &regionPosition, const ui8 &regionLayer);
  112.   void UncacheRegion(const clVec3I &regionPosition, const ui8 &regionLayer);
  113.   bool AddToRegionCliff(Region *pRegion, clBoxel regionBoxel);
  114.   void SplitCachesRecursive(clVec3I position, ui8 layer);
  115.   void UncacheLeastRecentlyUsedRegion();
  116.   bool WriteCompressionHeader();
  117.   bool ReadCompressionHeader();
  118.   void ExpandOctree();
  119.  
  120.   bool WriteHeader();
  121.   bool ReadHeader();
  122.  
  123.   // IO
  124.   void SaveRegion(Region *pRegion, const clVec3I &regionPosition, const ui8 &regionLayer);
  125.   Region *LoadRegion(const clVec3I &regionPosition, const ui8 &regionLayer);
  126.  
  127.   // Compression
  128.   void SerializeRegion(clWriteStream *pStream, Region *pRegion, const ui8 &compressionMode, const clVec3I &regionMinPos = clVec3I::Zero());
  129.   void DeserializeRegion(clReadStream *pStream, Region *pRegion, const ui8 &compressionMode, const clVec3I &regionMinPos = clVec3I::Zero());
  130.  
  131.   // Remote
  132.   bool ReadRemoteHeader();
  133.   bool WriteRemoteHeader();
  134.   bool DownloadRemoteHeader();
  135.  
  136.   // Memento
  137.   bool ReadActionHeader();
  138.   bool WriteActionHeader();
  139.   void CompleteAction(); // Call this to push Undo/ReDo Action
  140.   bool MoveBackAnAction(); // Undo
  141.   bool MoveForwardAnAction(); // Redo
  142.   void DoMomento(const clVoxelMemento &momento);
  143.   void UndoMomento(const clVoxelMemento &momento);
  144.   void ExtendAction(const clList<clVoxelMemento> &changes); // Call this to build up Undo/ReDo Action
  145.  
  146.   // Geometry Data //
  147.   clFileStream m_geometryStream;
  148.   clHashMap<clVec4I, i64> m_regionIndex;
  149.  
  150.   // Action Data //
  151.   clFileStream m_actionStream;
  152.   i64 m_currentAction = 0;
  153.   i64 m_activeActionExtensionCount = 0;
  154.   clList<clVec3I64> m_actionsHeader; // X = Stream Location, Y = Stream Length, Z = Action Count
  155.  
  156.   // Header Data //
  157.   ui8 m_layerCount;
  158.   i64 m_sourceVoxelCount;
  159.   i64 m_uniqueVoxelCount;
  160.   i64 m_sourceTriangleCount;
  161.  
  162.   // Directory Data //
  163.   clPath m_projectFolderName;
  164.  
  165.   // Remote Data //
  166.   clString m_remoteDatasetURL;
  167.   i64 m_tileServiceLocation;
  168.   i64 m_textureServiceLocation;
  169.   clHTTPFileReader *m_pNetReader = nullptr;
  170.   clHashMap<clVec4I, i64> m_regionLengths;
  171.  
  172.   // Texture Data //
  173.   clTextureStreamingService *m_pTextures;
  174.  
  175.   // Geometry Data //
  176.   ui8 m_fileSystemMode = 1; // 0-Flat, 1-PartialCombined, 2-NetworkBacked, 3-FullyCombined (not yet implemented)
  177.   ui8 m_compressionMode = 0; // 0-Raw, 1-LZ4 (Very Fast), 2-ZSTD (Fast), 3-ZPAQ (Very Slow)
  178.   i64 m_currentAccessRound = 0;
  179.   i64 m_modifiedRegionCount = 0;
  180.   clHashMap<clVec4I, Region*> m_regionsCachedByPosition;
  181.   clList<clKeyValuePair<clVec3I, Region*>> m_regionsCachedByLayer;
  182. };
  183.  
  184. #endif // clSparseBoxelOctree_h__
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement