| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using MorphoGeometry; |
| | 4 | | using Morpho25.Utility; |
| | 5 | |
|
| | 6 | | namespace 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> |
| 0 | 18 | | public Face Face { get; } |
| | 19 | | /// <summary> |
| | 20 | | /// Pixel. |
| | 21 | | /// </summary> |
| 89146 | 22 | | public Pixel Pixel { get; } |
| | 23 | | /// <summary> |
| | 24 | | /// Value of X. |
| | 25 | | /// </summary> |
| 89146 | 26 | | public double ValueX { get; set; } |
| | 27 | | /// <summary> |
| | 28 | | /// Value of Y. |
| | 29 | | /// </summary> |
| 89146 | 30 | | public double ValueY { get; set; } |
| | 31 | | /// <summary> |
| | 32 | | /// Value of Z. |
| | 33 | | /// </summary> |
| 181366 | 34 | | public double ValueZ { get; set; } |
| | 35 | | /// <summary> |
| | 36 | | /// Create a new facade. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="face"></param> |
| 89146 | 39 | | public Voxel(Face face) |
| 89146 | 40 | | { |
| 89146 | 41 | | Face = face; |
| 89146 | 42 | | ValueX = NULL_VALUE; |
| 89146 | 43 | | ValueY = NULL_VALUE; |
| 89146 | 44 | | ValueZ = NULL_VALUE; |
| 89146 | 45 | | } |
| | 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) |
| 89146 | 52 | | : this(face) |
| 89146 | 53 | | { |
| 89146 | 54 | | Pixel = pixel; |
| 89146 | 55 | | } |
| | 56 | | /// <summary> |
| | 57 | | /// Is direction X? |
| | 58 | | /// </summary> |
| | 59 | | /// <returns>Yes or no.</returns> |
| | 60 | | public bool IsXdirection() |
| 0 | 61 | | { |
| 0 | 62 | | return ValueX != NULL_VALUE; |
| 0 | 63 | | } |
| | 64 | | /// <summary> |
| | 65 | | /// Is direction Y? |
| | 66 | | /// </summary> |
| | 67 | | /// <returns>Yes or no.</returns> |
| | 68 | | public bool IsYdirection() |
| 0 | 69 | | { |
| 0 | 70 | | return ValueY != NULL_VALUE; |
| 0 | 71 | | } |
| | 72 | | /// <summary> |
| | 73 | | /// Is direction Z? |
| | 74 | | /// </summary> |
| | 75 | | /// <returns>Yes or no.</returns> |
| | 76 | | public bool IsZdirection() |
| 0 | 77 | | { |
| 0 | 78 | | return ValueZ != NULL_VALUE; |
| 0 | 79 | | } |
| | 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) |
| 0 | 86 | | { |
| 0 | 87 | | return voxels.Select(_ => _.ValueX) |
| 0 | 88 | | .ToList(); |
| 0 | 89 | | } |
| | 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) |
| 0 | 96 | | { |
| 0 | 97 | | return voxels.Select(_ => _.ValueY) |
| 0 | 98 | | .ToList(); |
| 0 | 99 | | } |
| | 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) |
| 1 | 106 | | { |
| 3075 | 107 | | return voxels.Select(_ => _.ValueZ) |
| 1 | 108 | | .ToList(); |
| 1 | 109 | | } |
| | 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) |
| 0 | 116 | | { |
| 0 | 117 | | return voxels.Select(_ => _.Face) |
| 0 | 118 | | .ToList(); |
| 0 | 119 | | } |
| | 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) |
| 0 | 131 | | { |
| | 132 | |
|
| 0 | 133 | | if (direction == Direction.X) |
| 0 | 134 | | { |
| 0 | 135 | | return voxels.Where(_ => _.IsXdirection()) |
| 0 | 136 | | .ToList(); |
| | 137 | | } |
| 0 | 138 | | else if (direction == Direction.Y) |
| 0 | 139 | | { |
| 0 | 140 | | return voxels.Where(_ => _.IsYdirection()) |
| 0 | 141 | | .ToList(); |
| | 142 | | } |
| | 143 | | else |
| 0 | 144 | | { |
| 0 | 145 | | return voxels.Where(_ => _.IsZdirection()) |
| 0 | 146 | | .ToList(); |
| | 147 | | } |
| 0 | 148 | | } |
| | 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) |
| 1 | 159 | | { |
| | 160 | |
|
| 1 | 161 | | if (direction == Direction.X) |
| 0 | 162 | | { |
| 0 | 163 | | return voxels.Where(_ => _.Pixel.I == index) |
| 0 | 164 | | .ToList(); |
| | 165 | | } |
| 1 | 166 | | else if (direction == Direction.Y) |
| 0 | 167 | | { |
| 0 | 168 | | return voxels.Where(_ => _.Pixel.J == index) |
| 0 | 169 | | .ToList(); |
| | 170 | | } |
| | 171 | | else |
| 1 | 172 | | { |
| 89147 | 173 | | return voxels.Where(_ => _.Pixel.K == index) |
| 1 | 174 | | .ToList(); |
| | 175 | | } |
| 1 | 176 | | } |
| | 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) |
| 0 | 187 | | { |
| 0 | 188 | | List<Voxel> result = new List<Voxel>(); |
| | 189 | |
|
| 0 | 190 | | values = DuplicateIndex(values, gridHeight); |
| | 191 | |
|
| 0 | 192 | | for (int i = 0; i < voxels.Count; i++) |
| 0 | 193 | | { |
| 0 | 194 | | if (voxels[i].Pixel.K == values[i]) |
| 0 | 195 | | { |
| 0 | 196 | | result.Add(voxels[i]); |
| 0 | 197 | | } |
| 0 | 198 | | } |
| | 199 | |
|
| 0 | 200 | | result = result.OrderBy(f => f.Pixel.I) |
| 0 | 201 | | .OrderBy(f => f.Pixel.J) |
| 0 | 202 | | .ToList(); |
| | 203 | |
|
| 0 | 204 | | return result; |
| 0 | 205 | | } |
| | 206 | |
|
| | 207 | | private static List<int> DuplicateIndex(List<int> values, |
| | 208 | | int gridHeight) |
| 0 | 209 | | { |
| 0 | 210 | | List<int> result = new List<int>(); |
| | 211 | |
|
| 0 | 212 | | for (int i = 0; i < gridHeight; i++) |
| 0 | 213 | | { |
| 0 | 214 | | result.AddRange(values); |
| 0 | 215 | | } |
| | 216 | |
|
| 0 | 217 | | return result; |
| 0 | 218 | | } |
| | 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) |
| 0 | 231 | | { |
| | 232 | |
|
| 0 | 233 | | if (direction == Direction.X) |
| 0 | 234 | | { |
| 0 | 235 | | return voxels.Where(_ => _.ValueX >= min && _.ValueX <= max) |
| 0 | 236 | | .ToList(); |
| | 237 | | } |
| 0 | 238 | | else if (direction == Direction.Y) |
| 0 | 239 | | { |
| 0 | 240 | | return voxels.Where(_ => _.ValueY >= min && _.ValueY <= max) |
| 0 | 241 | | .ToList(); |
| | 242 | | } |
| | 243 | | else |
| 0 | 244 | | { |
| 0 | 245 | | return voxels.Where(_ => _.ValueZ >= min && _.ValueZ <= max) |
| 0 | 246 | | .ToList(); |
| | 247 | | } |
| 0 | 248 | | } |
| | 249 | |
|
| | 250 | | #endregion |
| | 251 | | } |
| | 252 | | } |