| | 1 | | using Morpho25.Utility; |
| | 2 | | using MorphoGeometry; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Text; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | |
|
| | 9 | |
|
| | 10 | | namespace MorphoReader |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Direction enum. |
| | 14 | | /// </summary> |
| | 15 | | public enum Direction |
| | 16 | | { |
| | 17 | | X, |
| | 18 | | Y, |
| | 19 | | Z |
| | 20 | | } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Binary output of envimet. |
| | 24 | | /// </summary> |
| | 25 | | public abstract class BinaryOutput |
| | 26 | | { |
| | 27 | | protected int _numX; |
| | 28 | | protected int _numY; |
| | 29 | | protected int _numZ; |
| | 30 | |
|
| | 31 | | protected List<double> _sequenceX; |
| | 32 | | protected List<double> _sequenceY; |
| | 33 | | protected List<double> _sequenceZ; |
| | 34 | |
|
| | 35 | | protected List<double> _spacingX; |
| | 36 | | protected List<double> _spacingY; |
| | 37 | | protected List<double> _spacingZ; |
| | 38 | |
|
| | 39 | | protected int _offset; |
| | 40 | | protected int _buffer; |
| | 41 | |
|
| 1 | 42 | | public int NumX => _numX; |
| 1 | 43 | | public int NumY => _numY; |
| 0 | 44 | | public int NumZ => _numZ; |
| | 45 | |
|
| 2 | 46 | | public string[] VariableName { get; private set; } |
| 267439 | 47 | | public Vector BasePoint { get; protected set; } |
| | 48 | |
|
| 1 | 49 | | public int DataContent { get; protected set; } |
| 1 | 50 | | public string ProjectName { get; protected set; } |
| 1 | 51 | | public string LocationName { get; protected set; } |
| 1 | 52 | | public string SimulationDate { get; protected set; } |
| 1 | 53 | | public string SimulationTime { get; protected set; } |
| | 54 | |
|
| | 55 | | public delegate Face FaceByDirection(float spacingX, float spacingY, |
| | 56 | | float spacingZ, Vector centroid); |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Create new binary output object. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="edx">EDX file.</param> |
| 1 | 62 | | public BinaryOutput(string edx) |
| 1 | 63 | | { |
| 1 | 64 | | Read edxFile = new Read(edx); |
| 1 | 65 | | var outputKeys = edxFile.Information; |
| | 66 | |
|
| 1 | 67 | | SetNumberOfCells(outputKeys); |
| 1 | 68 | | SetSpacing(outputKeys); |
| 1 | 69 | | Setsequence(outputKeys); |
| 1 | 70 | | SetVariableName(outputKeys); |
| 1 | 71 | | SetGeneralInfo(outputKeys); |
| 1 | 72 | | } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Set variable names |
| | 76 | | /// </summary> |
| | 77 | | /// <param name="outputKeys">Keys.</param> |
| | 78 | | protected void SetVariableName(Dictionary<string, string> outputKeys) |
| 1 | 79 | | { |
| 1 | 80 | | VariableName = outputKeys["name_variables"].Split(','); |
| 1 | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Set X Y Z axis. |
| | 85 | | /// </summary> |
| | 86 | | /// <param name="outputKeys">Keys.</param> |
| | 87 | | protected void SetSpacing(Dictionary<string, string> outputKeys) |
| 1 | 88 | | { |
| 1 | 89 | | _spacingX = outputKeys["spacing_x"] |
| 1 | 90 | | .Split(',') |
| 53 | 91 | | .Select(_ => Convert.ToDouble(_)) |
| 1 | 92 | | .ToList(); |
| 1 | 93 | | _spacingY = outputKeys["spacing_y"] |
| 1 | 94 | | .Split(',') |
| 58 | 95 | | .Select(_ => Convert.ToDouble(_)) |
| 1 | 96 | | .ToList(); |
| 1 | 97 | | _spacingZ = outputKeys["spacing_z"] |
| 1 | 98 | | .Split(',') |
| 29 | 99 | | .Select(_ => Convert.ToDouble(_)) |
| 1 | 100 | | .ToList(); |
| 1 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Set X Y Z sequences. |
| | 105 | | /// </summary> |
| | 106 | | /// <param name="outputKeys">Keys.</param> |
| | 107 | | protected void Setsequence(Dictionary<string, string> outputKeys) |
| 1 | 108 | | { |
| 1 | 109 | | _sequenceX = Util.Accumulate(_spacingX).ToList(); |
| 1 | 110 | | _sequenceY = Util.Accumulate(_spacingY).ToList(); |
| 1 | 111 | | _sequenceZ = Util.Accumulate(_spacingZ).ToList(); |
| 1 | 112 | | } |
| | 113 | |
|
| | 114 | | /// <summary> |
| | 115 | | /// Set number of cells X Y Z. |
| | 116 | | /// </summary> |
| | 117 | | /// <param name="outputKeys">Keys.</param> |
| | 118 | | protected void SetNumberOfCells(Dictionary<string, string> outputKeys) |
| 1 | 119 | | { |
| 1 | 120 | | _numX = Convert.ToInt32(outputKeys["nr_xdata"]); |
| 1 | 121 | | _numY = Convert.ToInt32(outputKeys["nr_ydata"]); |
| 1 | 122 | | _numZ = Convert.ToInt32(outputKeys["nr_zdata"]); |
| 1 | 123 | | } |
| | 124 | |
|
| | 125 | | /// <summary> |
| | 126 | | /// Set generic information. |
| | 127 | | /// </summary> |
| | 128 | | /// <param name="outputKeys">Keys.</param> |
| | 129 | | protected void SetGeneralInfo(Dictionary<string, string> outputKeys) |
| 1 | 130 | | { |
| 1 | 131 | | ProjectName = outputKeys["projectname"]; |
| 1 | 132 | | DataContent = Convert.ToInt32(outputKeys["data_content"]); |
| 1 | 133 | | LocationName = outputKeys["locationname"]; |
| 1 | 134 | | SimulationDate = outputKeys["simulation_date"]; |
| 1 | 135 | | SimulationTime = outputKeys["simulation_time"]; |
| 1 | 136 | | } |
| | 137 | |
|
| | 138 | | /// <summary> |
| | 139 | | /// Get building voxels from EDT EDX. |
| | 140 | | /// </summary> |
| | 141 | | /// <param name="faceByDirection">Facade direction.</param> |
| | 142 | | /// <returns></returns> |
| | 143 | | protected List<Voxel> GetFacadesFromBinary(FaceByDirection faceByDirection) |
| 1 | 144 | | { |
| | 145 | |
|
| 1 | 146 | | List<Voxel> voxels = new List<Voxel>(); |
| | 147 | |
|
| | 148 | | Vector vector; |
| | 149 | | Face face; |
| | 150 | | Voxel facade; |
| | 151 | |
|
| 60 | 152 | | for (int k = 0; k < _numZ; k++) |
| 29 | 153 | | { |
| 3422 | 154 | | for (int j = 0; j < _numY; j++) |
| 1682 | 155 | | { |
| 181656 | 156 | | for (int i = 0; i < _numX; i++) |
| 89146 | 157 | | { |
| 89146 | 158 | | vector = new Vector((float)_sequenceX[i] + BasePoint.x - (float)_spacingX[i], (float)_sequenceY[ |
| 89146 | 159 | | face = faceByDirection((float)_spacingX[i], (float)_spacingY[j], (float)_spacingZ[k], vector); |
| | 160 | |
|
| 89146 | 161 | | facade = new Voxel(new Pixel(i, j, k), face); |
| 89146 | 162 | | voxels.Add(facade); |
| 89146 | 163 | | } |
| 1682 | 164 | | } |
| 29 | 165 | | } |
| | 166 | |
|
| 1 | 167 | | return voxels; |
| 1 | 168 | | } |
| | 169 | |
|
| | 170 | | /// <summary> |
| | 171 | | /// Facade in X. |
| | 172 | | /// </summary> |
| | 173 | | /// <param name="spacingX">Spacing value in X.</param> |
| | 174 | | /// <param name="spacingY">Spacing value in Y.</param> |
| | 175 | | /// <param name="spacingZ">Spacing value in Z.</param> |
| | 176 | | /// <param name="centroid">Centroid.</param> |
| | 177 | | /// <returns>Selected face.</returns> |
| | 178 | | protected Face FaceX(float spacingX, float spacingY, float spacingZ, Vector centroid) |
| 0 | 179 | | { |
| 0 | 180 | | var points = new Vector[] |
| 0 | 181 | | { |
| 0 | 182 | | new Vector(centroid.x - (spacingX / 2), centroid.y - (spacingY / 2), centroid.z - spacingZ), |
| 0 | 183 | | new Vector(centroid.x - (spacingX / 2), centroid.y + (spacingY / 2), centroid.z - spacingZ), |
| 0 | 184 | | new Vector(centroid.x - (spacingX / 2), centroid.y + (spacingY / 2), centroid.z), |
| 0 | 185 | | new Vector(centroid.x - (spacingX / 2), centroid.y - (spacingY / 2), centroid.z) |
| 0 | 186 | | }; |
| | 187 | |
|
| 0 | 188 | | Face face = new Face(points); |
| | 189 | |
|
| 0 | 190 | | return face; |
| 0 | 191 | | } |
| | 192 | |
|
| | 193 | | /// <summary> |
| | 194 | | /// Facade in Y. |
| | 195 | | /// </summary> |
| | 196 | | /// <param name="spacingX">Spacing value in X.</param> |
| | 197 | | /// <param name="spacingY">Spacing value in Y.</param> |
| | 198 | | /// <param name="spacingZ">Spacing value in Z.</param> |
| | 199 | | /// <param name="centroid">Centroid.</param> |
| | 200 | | /// <returns>Selected face.</returns> |
| | 201 | | protected Face FaceY(float spacingX, float spacingY, float spacingZ, Vector centroid) |
| 0 | 202 | | { |
| | 203 | |
|
| 0 | 204 | | var points = new Vector[] |
| 0 | 205 | | { |
| 0 | 206 | | new Vector(centroid.x - (spacingX / 2), centroid.y - (spacingY / 2), centroid.z - spacingZ), |
| 0 | 207 | | new Vector(centroid.x + (spacingX / 2), centroid.y - (spacingY / 2), centroid.z - spacingZ), |
| 0 | 208 | | new Vector(centroid.x + (spacingX / 2), centroid.y - (spacingY / 2), centroid.z), |
| 0 | 209 | | new Vector(centroid.x - (spacingX / 2), centroid.y - (spacingY / 2), centroid.z) |
| 0 | 210 | | }; |
| | 211 | |
|
| 0 | 212 | | Face face = new Face(points); |
| | 213 | |
|
| 0 | 214 | | return face; |
| 0 | 215 | | } |
| | 216 | |
|
| | 217 | | /// <summary> |
| | 218 | | /// Facade in Z. |
| | 219 | | /// </summary> |
| | 220 | | /// <param name="spacingX">Spacing value in X.</param> |
| | 221 | | /// <param name="spacingY">Spacing value in Y.</param> |
| | 222 | | /// <param name="spacingZ">Spacing value in Z.</param> |
| | 223 | | /// <param name="centroid">Centroid.</param> |
| | 224 | | /// <returns>Selected face.</returns> |
| | 225 | | protected Face FaceZ(float spacingX, float spacingY, float spacingZ, Vector centroid) |
| 89146 | 226 | | { |
| | 227 | |
|
| 89146 | 228 | | var points = new Vector[] |
| 89146 | 229 | | { |
| 89146 | 230 | | new Vector(centroid.x - (spacingX / 2), centroid.y - (spacingY / 2), centroid.z - spacingZ), |
| 89146 | 231 | | new Vector(centroid.x + (spacingX / 2), centroid.y - (spacingY / 2), centroid.z - spacingZ), |
| 89146 | 232 | | new Vector(centroid.x + (spacingX / 2), centroid.y + (spacingY / 2), centroid.z - spacingZ), |
| 89146 | 233 | | new Vector(centroid.x - (spacingX / 2), centroid.y + (spacingY / 2), centroid.z - spacingZ) |
| 89146 | 234 | | }; |
| | 235 | |
|
| 89146 | 236 | | Face face = new Face(points); |
| | 237 | |
|
| 89146 | 238 | | return face; |
| 89146 | 239 | | } |
| | 240 | |
|
| | 241 | | /// <summary> |
| | 242 | | /// Get voxels by direction. |
| | 243 | | /// </summary> |
| | 244 | | /// <param name="direction">Direction.</param> |
| | 245 | | /// <returns>Collection of voxels.</returns> |
| | 246 | | public List<Voxel> GetVoxels(Direction direction) |
| 1 | 247 | | { |
| | 248 | |
|
| 1 | 249 | | if (direction == Direction.X) |
| 0 | 250 | | { |
| 0 | 251 | | return GetFacadesFromBinary(FaceX); |
| | 252 | | } |
| 1 | 253 | | else if (direction == Direction.Y) |
| 0 | 254 | | { |
| 0 | 255 | | return GetFacadesFromBinary(FaceY); |
| | 256 | | } |
| | 257 | | else |
| 1 | 258 | | { |
| 1 | 259 | | return GetFacadesFromBinary(FaceZ); |
| | 260 | | } |
| | 261 | |
|
| 1 | 262 | | } |
| | 263 | |
|
| | 264 | | /// <summary> |
| | 265 | | /// Set values reading EDX file. |
| | 266 | | /// </summary> |
| | 267 | | /// <param name="edt">EDT file.</param> |
| | 268 | | /// <param name="voxels">Facades.</param> |
| | 269 | | /// <param name="variable">Index of the variable to read.</param> |
| | 270 | | public abstract void SetValuesFromBinary(string edt, |
| | 271 | | List<Voxel> voxels, int variable); |
| | 272 | | } |
| | 273 | | } |