<commit_before>﻿// ***********************************************************************
// Assembly         : TessellationAndVoxelizationGeometryLibrary
// Author           : Design Engineering Lab
// Created          : 02-27-2015
//
// Last Modified By : <NAME>
// Last Modified On : 09-21-2017
// ***********************************************************************
// <copyright file="VoxelizedSolid.cs" company="Design Engineering Lab">
//     Copyright ©  2017
// </copyright>
// <summary></summary>
// ***********************************************************************

using StarMathLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace TVGL.Voxelization
{
    /// <summary>
    /// Class VoxelizedSolid.
    /// </summary>
    public partial class VoxelizedSolid
    {
        #region Functions to make voxel full, empty or partial
        /// <summary>
        /// Changes the voxel to empty.
        /// </summary>
        /// <param name="voxel">The voxel.</param>
        /// <param name="removeDescendants">if set to <c>true</c> [remove descendants].</param>
        /// <param name="checkParentEmpty">if set to <c>true</c> [check parent empty].</param>
        public void ChangeVoxelToEmpty(long voxel, bool removeDescendants, bool checkParentEmpty)
        {
            Constants.GetAllFlags(voxel, out var level, out var role, out var btmIsInside);
            if (role == VoxelRoleTypes.Empty) return;
            if (level == 0)
                // no need to do anything with the HigherLevelVoxels as deleting this Voxel, deletes
                // the only reference to these higher voxels.
                lock (voxelDictionaryLevel0)
                    voxelDictionaryLevel0.Remove(voxel);
            else
            {
                var voxel0 = voxelDictionaryLevel0.GetVoxel(voxel);
                lock (voxel0.InnerVoxels[level - 1]) //remove the node here
                {
                    voxel0.InnerVoxels[level - 1].Remove(voxel);
                    var parent = GetParentVoxel(voxel);
                    // then check to see if the parent should be empty as well
                    if (checkParentEmpty)
                        if (voxel0.InnerVoxels[level - 1].Count == 0 ||
                            voxel0.InnerVoxels[level - 1].CountDescendants(parent, level - 1) == 0)
                            ChangeVoxelToEmpty(parent, false, true);
                }
                // finally, any descendants of voxel need to be removed
                if (role == VoxelRoleTypes.Partial && removeDescendants)
                {
                    for (int i = level; i < numberOfLevels - 1; i++)
                    {
                        // by starting at level (and not doing an i-1), we are starting at the next lower level
                        lock (voxel0.InnerVoxels[i]) voxel0.InnerVoxels[i].RemoveDescendants(voxel, level);
                    }
                }
            }
        }

        private long ChangeEmptyVoxelToFull(long ID, int level, bool checkParentFull)
        {
            if (level == 0)
            {   //adding a new level-0 voxel is fairly straightforward
                var voxelBin = new VoxelBinClass(ID, VoxelRoleTypes.Full, this);
                lock (voxelDictionaryLevel0)
                    voxelDictionaryLevel0.AddOrReplace(voxelBin);
                voxelBin.ID = Constants.ClearFlagsFromID(ID) + Constants.MakeFlags(level, VoxelRoleTypes.Full);
                return voxelBin.ID;
            }
            // for the lower levels, first get or make the level-0 voxel (next7 lines)
            var thisIDwoFlags = Constants.ClearFlagsFromID(ID);
            var id0 = MakeParentVoxelID(thisIDwoFlags, 0);<commit_msg>Sometimes voxel0 is null, causing this piece of code to throw an exception. Checking if it's not null fixes that.<commit_after>﻿// ***********************************************************************
// Assembly         : TessellationAndVoxelizationGeometryLibrary
// Author           : Design Engineering Lab
// Created          : 02-27-2015
//
// Last Modified By : <NAME>
// Last Modified On : 09-21-2017
// ***********************************************************************
// <copyright file="VoxelizedSolid.cs" company="Design Engineering Lab">
//     Copyright ©  2017
// </copyright>
// <summary></summary>
// ***********************************************************************

using StarMathLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace TVGL.Voxelization
{
    /// <summary>
    /// Class VoxelizedSolid.
    /// </summary>
    public partial class VoxelizedSolid
    {
        #region Functions to make voxel full, empty or partial
        /// <summary>
        /// Changes the voxel to empty.
        /// </summary>
        /// <param name="voxel">The voxel.</param>
        /// <param name="removeDescendants">if set to <c>true</c> [remove descendants].</param>
        /// <param name="checkParentEmpty">if set to <c>true</c> [check parent empty].</param>
        public void ChangeVoxelToEmpty(long voxel, bool removeDescendants, bool checkParentEmpty)
        {
            Constants.GetAllFlags(voxel, out var level, out var role, out var btmIsInside);
            if (role == VoxelRoleTypes.Empty) return;
            if (level == 0)
                // no need to do anything with the HigherLevelVoxels as deleting this Voxel, deletes
                // the only reference to these higher voxels.
                lock (voxelDictionaryLevel0)
                    voxelDictionaryLevel0.Remove(voxel);
            else
            {
                var voxel0 = voxelDictionaryLevel0.GetVoxel(voxel);
                if (voxel0 != null)
                    lock (voxel0.InnerVoxels[level - 1]) //remove the node here
                    {
                        voxel0.InnerVoxels[level - 1].Remove(voxel);
                        var parent = GetParentVoxel(voxel);
                        // then check to see if the parent should be empty as well
                        if (checkParentEmpty)
                            if (voxel0.InnerVoxels[level - 1].Count == 0 ||
                                voxel0.InnerVoxels[level - 1].CountDescendants(parent, level - 1) == 0)
                                ChangeVoxelToEmpty(parent, false, true);
                    }
                // finally, any descendants of voxel need to be removed
                if (role == VoxelRoleTypes.Partial && removeDescendants)
                {
                    for (int i = level; i < numberOfLevels - 1; i++)
                    {
                        // by starting at level (and not doing an i-1), we are starting at the next lower level
                        lock (voxel0.InnerVoxels[i]) voxel0.InnerVoxels[i].RemoveDescendants(voxel, level);
                    }
                }
            }
        }

        private long ChangeEmptyVoxelToFull(long ID, int level, bool checkParentFull)
        {
            if (level == 0)
            {   //adding a new level-0 voxel is fairly straightforward
                var voxelBin = new VoxelBinClass(ID, VoxelRoleTypes.Full, this);
                lock (voxelDictionaryLevel0)
                    voxelDictionaryLevel0.AddOrReplace(voxelBin);
                voxelBin.ID = Constants.ClearFlagsFromID(ID) + Constants.MakeFlags(level, VoxelRoleTypes.Full);
                return voxelBin.ID;
            }
            // for the lower levels, first get or make the level-0 voxel (next7 lines)
            var thisIDwoFlags = Constants.ClearFlagsFromID(ID);
            var id0 = MakeParentVoxelID(thisIDwoFlags, 0);
{'fraction_non_alphanumeric': 0.1453473468341451, 'fraction_numerical': 0.012560881825173032, 'mean_word_length': 1.8793357933579335, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3114888', 'n_tokens_mistral': 2208, 'n_tokens_neox': 1990, 'n_words': 559}