Exemple #1
0
        protected static float MeasureUnboxedVoxels(VoxelField volume)
        {
            int unboxedVoxels = 0;
            int totalvoxels   = 0;

            for (Int32 x = 0; x < volume.VoxelSize.X; ++x)
            {
                for (Int32 y = 0; y < volume.VoxelSize.Y; ++y)
                {
                    for (Int32 z = 0; z < volume.VoxelSize.Z; ++z)
                    {
                        byte value = volume.GetVoxel(x, y, z);
                        if (value == 1)
                        {
                            unboxedVoxels++;
                        }
                        if (value != 0)
                        {
                            totalvoxels++;
                        }
                    }
                }
            }

            return(unboxedVoxels / (float)totalvoxels);
        }
Exemple #2
0
        protected static bool TestRangeForFreeSpace(VoxelField volume, Vector3i start, Vector3i end)
        {
            // 1. Ignore directions that take us outside the bounds of the voxel volume.
            if (start.X < 0 || start.Y < 0 || start.Z < 0)
            {
                return(false);
            }

            // 2. Ensure that we can safely expand into the area, expand as long as the voxel isn't empty.
            //    Note: it is ok to expand into a space already occupied.
            for (Int32 z = start.Z; z <= end.Z; ++z)
            {
                for (Int32 y = start.Y; y <= end.Y; ++y)
                {
                    for (Int32 x = start.X; x <= end.X; ++x)
                    {
                        byte value = volume.GetVoxel(x, y, z);
                        // If a voxel has a '1' then it's unused volume space, if it is '0' it is empty,
                        // and if anything else, it has a box in it already.
                        if (value != 1)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Exemple #3
0
        protected static Vector3i FindHighestDensityVoxel(VoxelField volume)
        {
            float    denestDistance = float.MinValue;
            Vector3i densestVoxel   = new Vector3i(0, 0, 0);

            Object syncroot = new Object();

            Parallel.For(0, volume.VoxelSize.Z, z =>
            {
                for (Int32 y = 0; y < volume.VoxelSize.Y; ++y)
                {
                    for (Int32 x = 0; x < volume.VoxelSize.X; ++x)
                    {
                        byte value = volume.GetVoxel(x, y, z);

                        // Ignore empty voxels and already boxed voxels
                        if (value != 1)
                        {
                            continue;
                        }

                        float closestExtDist = FindShortestDistanceToAbnormalVoxel(volume, x, y, z);

                        if (closestExtDist > denestDistance)
                        {
                            lock (syncroot)
                            {
                                if (closestExtDist > denestDistance)
                                {
                                    denestDistance = closestExtDist;
                                    densestVoxel   = new Vector3i(x, y, z);
                                }
                            }
                        }
                    }
                }
            });

            return(densestVoxel);
        }
Exemple #4
0
        protected static float FindDistanceToAbnormalVoxelInRange(
            VoxelField volume,
            Vector3i voxel,
            int startX, int startY, int startZ,
            int endX, int endY, int endZ)
        {
            // 1. Ignore directions that take us outside the bounds of the voxel volume.
            if (startX < 0 || startY < 0 || startZ < 0)
            {
                return(float.MaxValue);
            }

            float closestValueDist = float.MaxValue;

            // 2. Find the value along the axis
            for (Int32 z = startZ; z <= endZ; ++z)
            {
                for (Int32 y = startY; y <= endY; ++y)
                {
                    for (Int32 x = startX; x <= endX; ++x)
                    {
                        byte value = volume.GetVoxel(x, y, z);
                        if (value != 1)
                        {
                            float dist = (float)(voxel - new Vector3i(x, y, z)).LengthSquared;
                            if (dist < closestValueDist)
                            {
                                closestValueDist = dist;
                            }
                        }
                    }
                }
            }

            return(closestValueDist);
        }