< Summary

Information
Class: MorphoReader.Voxel
Assembly: MorphoReader
File(s): D:\a\Morpho\Morpho\project\Morpho\MorphoReader\Voxel.cs
Line coverage
25%
Covered lines: 26
Uncovered lines: 77
Coverable lines: 103
Total lines: 252
Line coverage: 25.2%
Branch coverage
8%
Covered branches: 2
Total branches: 24
Branch coverage: 8.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

D:\a\Morpho\Morpho\project\Morpho\MorphoReader\Voxel.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using MorphoGeometry;
 4using Morpho25.Utility;
 5
 6namespace MorphoReader
 7{
 8    /// <summary>
 9    /// Facade class.
 10    /// </summary>
 11    public class Voxel
 12    {
 13        const int NULL_VALUE = -999;
 14
 15        /// <summary>
 16        /// Face.
 17        /// </summary>
 018        public Face Face { get; }
 19        /// <summary>
 20        /// Pixel.
 21        /// </summary>
 8914622        public Pixel Pixel { get; }
 23        /// <summary>
 24        /// Value of X.
 25        /// </summary>
 8914626        public double ValueX { get; set; }
 27        /// <summary>
 28        /// Value of Y.
 29        /// </summary>
 8914630        public double ValueY { get; set; }
 31        /// <summary>
 32        /// Value of Z.
 33        /// </summary>
 18136634        public double ValueZ { get; set; }
 35        /// <summary>
 36        /// Create a new facade.
 37        /// </summary>
 38        /// <param name="face"></param>
 8914639        public Voxel(Face face)
 8914640        {
 8914641            Face = face;
 8914642            ValueX = NULL_VALUE;
 8914643            ValueY = NULL_VALUE;
 8914644            ValueZ = NULL_VALUE;
 8914645        }
 46        /// <summary>
 47        /// Create a new facade.
 48        /// </summary>
 49        /// <param name="pixel">Pixel.</param>
 50        /// <param name="face">Face.</param>
 51        public Voxel(Pixel pixel, Face face)
 8914652            : this(face)
 8914653        {
 8914654            Pixel = pixel;
 8914655        }
 56        /// <summary>
 57        /// Is direction X?
 58        /// </summary>
 59        /// <returns>Yes or no.</returns>
 60        public bool IsXdirection()
 061        {
 062            return ValueX != NULL_VALUE;
 063        }
 64        /// <summary>
 65        /// Is direction Y?
 66        /// </summary>
 67        /// <returns>Yes or no.</returns>
 68        public bool IsYdirection()
 069        {
 070            return ValueY != NULL_VALUE;
 071        }
 72        /// <summary>
 73        /// Is direction Z?
 74        /// </summary>
 75        /// <returns>Yes or no.</returns>
 76        public bool IsZdirection()
 077        {
 078            return ValueZ != NULL_VALUE;
 079        }
 80        /// <summary>
 81        /// Get values in X.
 82        /// </summary>
 83        /// <param name="voxels">Voxels.</param>
 84        /// <returns>Collection of values.</returns>
 85        public static List<double> GetValueXFromVoxels(List<Voxel> voxels)
 086        {
 087            return voxels.Select(_ => _.ValueX)
 088                   .ToList();
 089        }
 90        /// <summary>
 91        /// Get values in Y.
 92        /// </summary>
 93        /// <param name="voxels">Voxels.</param>
 94        /// <returns>Collection of values.</returns>
 95        public static List<double> GetValueYFromVoxels(List<Voxel> voxels)
 096        {
 097            return voxels.Select(_ => _.ValueY)
 098                   .ToList();
 099        }
 100        /// <summary>
 101        /// Get values in Z.
 102        /// </summary>
 103        /// <param name="voxels">Voxels.</param>
 104        /// <returns>Collection of values.</returns>
 105        public static List<double> GetValueZFromVoxels(List<Voxel> voxels)
 1106        {
 3075107            return voxels.Select(_ => _.ValueZ)
 1108                   .ToList();
 1109        }
 110        /// <summary>
 111        /// Get faces from facades.
 112        /// </summary>
 113        /// <param name="voxels">Voxels.</param>
 114        /// <returns>Collection of faces.</returns>
 115        public static List<Face> GetFacesFromVoxels(List<Voxel> voxels)
 0116        {
 0117            return voxels.Select(_ => _.Face)
 0118                          .ToList();
 0119        }
 120
 121
 122        #region Query
 123        /// <summary>
 124        /// Get facades by direction.
 125        /// </summary>
 126        /// <param name="voxels">Voxels.</param>
 127        /// <param name="direction">Direction.</param>
 128        /// <returns>Collection of facades.</returns>
 129        public static List<Voxel> GetVoxelsByDirection(List<Voxel> voxels,
 130            Direction direction = Direction.Z)
 0131        {
 132
 0133            if (direction == Direction.X)
 0134            {
 0135                return voxels.Where(_ => _.IsXdirection())
 0136                    .ToList();
 137            }
 0138            else if (direction == Direction.Y)
 0139            {
 0140                return voxels.Where(_ => _.IsYdirection())
 0141                    .ToList();
 142            }
 143            else
 0144            {
 0145                return voxels.Where(_ => _.IsZdirection())
 0146                    .ToList();
 147            }
 0148        }
 149
 150        /// <summary>
 151        /// Get Slice of facades by pixel index and direction.
 152        /// </summary>
 153        /// <param name="voxels">Voxels.</param>
 154        /// <param name="index">Index.</param>
 155        /// <param name="direction">Direction.</param>
 156        /// <returns>Collection of facades.</returns>
 157        public static List<Voxel> GetSliceByPixelCoordinate(List<Voxel> voxels,
 158            int index, Direction direction = Direction.Z)
 1159        {
 160
 1161            if (direction == Direction.X)
 0162            {
 0163                return voxels.Where(_ => _.Pixel.I == index)
 0164                    .ToList();
 165            }
 1166            else if (direction == Direction.Y)
 0167            {
 0168                return voxels.Where(_ => _.Pixel.J == index)
 0169                    .ToList();
 170            }
 171            else
 1172            {
 89147173                return voxels.Where(_ => _.Pixel.K == index)
 1174                    .ToList();
 175            }
 1176        }
 177
 178        /// <summary>
 179        /// Get facades by mask of values.
 180        /// </summary>
 181        /// <param name="voxels">Voxels.</param>
 182        /// <param name="values">Collection of index to use.</param>
 183        /// <param name="gridHeight">Height of the grid.</param>
 184        /// <returns>Collection of facades.</returns>
 185        public static List<Voxel> GetVoxelsFilterByZmask(List<Voxel> voxels,
 186            List<int> values, int gridHeight)
 0187        {
 0188            List<Voxel> result = new List<Voxel>();
 189
 0190            values = DuplicateIndex(values, gridHeight);
 191
 0192            for (int i = 0; i < voxels.Count; i++)
 0193            {
 0194                if (voxels[i].Pixel.K == values[i])
 0195                {
 0196                    result.Add(voxels[i]);
 0197                }
 0198            }
 199
 0200            result = result.OrderBy(f => f.Pixel.I)
 0201                .OrderBy(f => f.Pixel.J)
 0202                .ToList();
 203
 0204            return result;
 0205        }
 206
 207        private static List<int> DuplicateIndex(List<int> values,
 208            int gridHeight)
 0209        {
 0210            List<int> result = new List<int>();
 211
 0212            for (int i = 0; i < gridHeight; i++)
 0213            {
 0214                result.AddRange(values);
 0215            }
 216
 0217            return result;
 0218        }
 219
 220        /// <summary>
 221        /// Get voxels by threshold.
 222        /// </summary>
 223        /// <param name="voxels">Voxels.</param>
 224        /// <param name="min">Minimum value.</param>
 225        /// <param name="max">Maximum value.</param>
 226        /// <param name="direction">Direction.</param>
 227        /// <returns>Collection of facades.</returns>
 228        public static List<Voxel> GetVoxelsByThreshold(List<Voxel> voxels,
 229            double min, double max,
 230            Direction direction = Direction.Z)
 0231        {
 232
 0233            if (direction == Direction.X)
 0234            {
 0235                return voxels.Where(_ => _.ValueX >= min && _.ValueX <= max)
 0236                    .ToList();
 237            }
 0238            else if (direction == Direction.Y)
 0239            {
 0240                return voxels.Where(_ => _.ValueY >= min && _.ValueY <= max)
 0241                    .ToList();
 242            }
 243            else
 0244            {
 0245                return voxels.Where(_ => _.ValueZ >= min && _.ValueZ <= max)
 0246                    .ToList();
 247            }
 0248        }
 249
 250        #endregion
 251    }
 252}