public void SetToEmpty() { CellType = MyVoxelCellType.EMPTY; m_voxelContentSum = 0; CheckCellType(); }
public void SetToFull() { CellType = MyVoxelCellType.FULL; m_voxelContentSum = MyVoxelConstants.VOXEL_CELL_CONTENT_SUM_TOTAL; CheckCellType(); }
public MyVoxelContentCell() { // Default cell is FULL CellType = MyVoxelCellType.FULL; // Sums all voxel values. Default is summ of all full voxel in cell, so be subtracting we can switch cell from MIXED to EMPTY. m_voxelContentSum = MyVoxelConstants.VOXEL_CELL_CONTENT_SUM_TOTAL; }
// Set voxel content for the whole cell. public void SetAllVoxelContents(byte[] buffer) { // quantize the buffer and compute sum m_voxelContentSum = 0; for (int i = 0; i < buffer.Length; i++) { buffer[i] = MyVoxelContentCellContent.QuantizedValue(buffer[i]); m_voxelContentSum += buffer[i]; } // mixed-->empty/full: deallocate // empty/full-->mixed: allocate // mixed: fill with values from buffer if (m_voxelContentSum == 0) { if (CellType == MyVoxelCellType.MIXED) { Deallocate(); } CellType = MyVoxelCellType.EMPTY; } else if (m_voxelContentSum == MyVoxelConstants.VOXEL_CELL_CONTENT_SUM_TOTAL) { if (CellType == MyVoxelCellType.MIXED) { Deallocate(); } CellType = MyVoxelCellType.FULL; } else { if (CellType == MyVoxelCellType.FULL || CellType == MyVoxelCellType.EMPTY) { m_cellContent = MyVoxelContentCellContents.Allocate(); } if (m_cellContent != null) { m_cellContent.Value.SetAddVoxelContents(buffer); } CellType = MyVoxelCellType.MIXED; } }
// This method helps us to maintain correct cell type even after removing or adding voxels from cell // If all voxels were removed from this cell, we change its type to from MIXED to EMPTY. // If voxels were added, we change its type to from EMPTY to MIXED. // If voxels were added to full, we change its type to FULL. void CheckCellType() { // Voxel cell content sum isn't in allowed range. Probably increased or descreased too much. System.Diagnostics.Debug.Assert((m_voxelContentSum >= 0) && (m_voxelContentSum <= MyVoxelConstants.VOXEL_CELL_CONTENT_SUM_TOTAL)); if (m_voxelContentSum == 0) { CellType = MyVoxelCellType.EMPTY; } else if (m_voxelContentSum == MyVoxelConstants.VOXEL_CELL_CONTENT_SUM_TOTAL) { CellType = MyVoxelCellType.FULL; } else { CellType = MyVoxelCellType.MIXED; } // If cell changed from MIXED to EMPTY or FULL, we will release it's cell content because it's not needed any more if ((CellType == MyVoxelCellType.EMPTY) || (CellType == MyVoxelCellType.FULL)) { Deallocate(); } }
/// <summary> /// Check the given cell type against a possible cell contents state (full, empty, mixed) /// </summary> /// <param name="cell">MyVoxelContentCell object to check</param> /// <param name="type">A cell state to check against (MyVoxelCellType enum)</param> /// <returns>whether the cell has the given type or not.</returns> private bool CheckCellType(ref MyVoxelContentCell cell, MyVoxelCellType type) { if (cell == null) return type == MyVoxelCellType.FULL; return cell.CellType == type; }
// Set voxel content for the whole cell. public void SetAllVoxelContents(byte[] buffer) { // quantize the buffer and compute sum m_voxelContentSum = 0; for (int i = 0; i < buffer.Length; i++) { buffer[i] = MyVoxelContentCellContent.QuantizedValue(buffer[i]); m_voxelContentSum += buffer[i]; } // mixed-->empty/full: deallocate // empty/full-->mixed: allocate // mixed: fill with values from buffer if (m_voxelContentSum == 0) { if (CellType == MyVoxelCellType.MIXED) Deallocate(); CellType = MyVoxelCellType.EMPTY; } else if (m_voxelContentSum == MyVoxelConstants.VOXEL_CELL_CONTENT_SUM_TOTAL) { if (CellType == MyVoxelCellType.MIXED) Deallocate(); CellType = MyVoxelCellType.FULL; } else { if (CellType == MyVoxelCellType.FULL || CellType == MyVoxelCellType.EMPTY) m_cellContent = MyVoxelContentCellContents.Allocate(); if (m_cellContent != null) { m_cellContent.Value.SetAddVoxelContents(buffer); } CellType = MyVoxelCellType.MIXED; } }