| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using MorphoGeometry; |
| | 5 | | using Morpho25.Geometry; |
| | 6 | |
|
| | 7 | | namespace Morpho25.Utility |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Envimet utility class. |
| | 11 | | /// </summary> |
| | 12 | | public static class EnvimetUtility |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Raycasting 2D between facegroup <> rays. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="rays">Rays.</param> |
| | 18 | | /// <param name="facegroup">Facegroup to hit.</param> |
| | 19 | | /// <param name="reverse">True to reverse faces of Facegroup.</param> |
| | 20 | | /// <param name="project">True to project intersections to plane.</param> |
| | 21 | | /// <returns>Intersection points.</returns> |
| | 22 | | public static IEnumerable<Vector> Raycasting2D(List<Ray> rays, |
| | 23 | | FaceGroup facegroup, bool reverse = false, |
| | 24 | | bool project = false) |
| 0 | 25 | | { |
| 0 | 26 | | var intersections = Intersection.RaysFaceGroupIntersect(rays, facegroup, |
| 0 | 27 | | Intersection.RayFaceIntersect, |
| 0 | 28 | | reverse, project); |
| | 29 | | // Clean duplicates |
| 0 | 30 | | var groups = intersections |
| 0 | 31 | | .GroupBy(_ => new { x = _.x, y = _.y, }) |
| 0 | 32 | | .ToList(); |
| | 33 | |
|
| 0 | 34 | | var pts = new List<Vector>(); |
| 0 | 35 | | foreach (var group in groups) |
| 0 | 36 | | { |
| 0 | 37 | | var vectors = group.OrderBy(_ => _.z); |
| 0 | 38 | | if (reverse) |
| 0 | 39 | | { |
| 0 | 40 | | pts.Add(vectors.Last()); |
| 0 | 41 | | } |
| | 42 | | else |
| 0 | 43 | | { |
| 0 | 44 | | pts.Add(vectors.First()); |
| 0 | 45 | | } |
| 0 | 46 | | } |
| 0 | 47 | | return pts; |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Raycasting 3D between facegroup <> rays. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="rays">Rays.</param> |
| | 54 | | /// <param name="facegroup">Facegroup to hit.</param> |
| | 55 | | /// <param name="reverse">True to reverse faces of Facegroup.</param> |
| | 56 | | /// <param name="project">True to project intersections to plane.</param> |
| | 57 | | /// <returns>Intersection points.</returns> |
| | 58 | | public static IEnumerable<Vector> Raycasting3D(List<Ray> rays, |
| | 59 | | FaceGroup facegroup, bool reverse = false, |
| | 60 | | bool project = false) |
| 0 | 61 | | { |
| 0 | 62 | | return Intersection.RaysFaceGroupIntersect(rays, facegroup, |
| 0 | 63 | | Intersection.RayFaceIntersectFrontBack, |
| 0 | 64 | | reverse, project); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Get voxel centroids. |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="grid">Grid to use as a guide.</param> |
| | 71 | | /// <param name="intersections">Intersection points from raycasting.</param> |
| | 72 | | /// <returns>Voxel centroids.</returns> |
| | 73 | | public static IEnumerable<Vector> GetCentroids(Grid grid, |
| | 74 | | IEnumerable<Vector> intersections) |
| 0 | 75 | | { |
| 0 | 76 | | var groups = intersections |
| 0 | 77 | | .GroupBy(_ => new { x = _.x, y = _.y, }) |
| 0 | 78 | | .ToList(); |
| | 79 | |
|
| 0 | 80 | | var centroids = new List<Vector>(); |
| 0 | 81 | | foreach (var group in groups) |
| 0 | 82 | | { |
| 0 | 83 | | var sortGroup = group.OrderBy(_ => _.z); |
| 0 | 84 | | var chunks = sortGroup.ToList().ChunkBy(2); |
| 0 | 85 | | foreach (var pts in chunks) |
| 0 | 86 | | { |
| 0 | 87 | | var heights = pts.Select(_ => _.z); |
| 0 | 88 | | var min = heights.Min(); |
| 0 | 89 | | var max = heights.Max(); |
| | 90 | |
|
| 0 | 91 | | var zCoords = Util.FilterByMinMax(grid.Zaxis, max, min); |
| 0 | 92 | | var voxels = zCoords.Select(_ => new Vector(pts[0].x, pts[0].y, Convert.ToSingle(_))); |
| | 93 | |
|
| 0 | 94 | | centroids.AddRange(voxels); |
| 0 | 95 | | } |
| 0 | 96 | | } |
| | 97 | |
|
| 0 | 98 | | return centroids; |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Get rows for 3D part of envimet. |
| | 103 | | /// </summary> |
| | 104 | | /// <param name="matrix">Matrix to wrap.</param> |
| | 105 | | /// <returns></returns> |
| | 106 | | public static IEnumerable<string> GetStringRows(Matrix3d<string[]> matrix) |
| 0 | 107 | | { |
| 0 | 108 | | for (int k = 0; k < matrix.GetLengthZ(); k++) |
| 0 | 109 | | for (int j = 0; j < matrix.GetLengthY(); j++) |
| 0 | 110 | | for (int i = 0; i < matrix.GetLengthX(); i++) |
| 0 | 111 | | { |
| 0 | 112 | | if (matrix[i, j, k].Count(_ => _ == null) < 3) |
| 0 | 113 | | { |
| 0 | 114 | | var value = string.Join(",", matrix[i, j, k]); |
| 0 | 115 | | yield return String.Format("{0},{1},{2},{3}", i, j, k, value); |
| 0 | 116 | | } |
| 0 | 117 | | } |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// Convert vector to pixel. |
| | 122 | | /// </summary> |
| | 123 | | /// <param name="vector">Vector.</param> |
| | 124 | | /// <param name="grid">Grid to use for mapping.</param> |
| | 125 | | /// <returns>Pixel.</returns> |
| | 126 | | public static Pixel ToPixel(this Vector vector, Grid grid) |
| 0 | 127 | | { |
| 0 | 128 | | int i = Util.ClosestValue(grid.Xaxis, vector.x); |
| 0 | 129 | | int j = Util.ClosestValue(grid.Yaxis, vector.y); |
| 0 | 130 | | int k = Util.ClosestValue(grid.Zaxis, vector.z); |
| 0 | 131 | | return new Pixel(i, j, k); |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | /// <summary> |
| | 135 | | /// Get minimum rays from Facegroup's boundary box. |
| | 136 | | /// </summary> |
| | 137 | | /// <param name="grid">Grid to use for mapping.</param> |
| | 138 | | /// <param name="facegroup">Facegroup to use for |
| | 139 | | /// the boundary box.</param> |
| | 140 | | /// <returns>Rays.</returns> |
| | 141 | | public static List<Ray> GetRayFromFacegroupBbox(Grid grid, |
| | 142 | | FaceGroup facegroup) |
| 0 | 143 | | { |
| 0 | 144 | | BoundaryBox box = new BoundaryBox(facegroup); |
| | 145 | |
|
| 0 | 146 | | Vector minPt = box.MinPoint; |
| 0 | 147 | | Vector maxPt = box.MaxPoint; |
| | 148 | |
|
| 0 | 149 | | var rayXcomponent = Util.FilterByMinMax(grid.Xaxis, |
| 0 | 150 | | maxPt.x, minPt.x); |
| 0 | 151 | | var rayYcomponent = Util.FilterByMinMax(grid.Yaxis, |
| 0 | 152 | | maxPt.y, minPt.y); |
| | 153 | |
|
| 0 | 154 | | List<Ray> rays = new List<Ray>(); |
| 0 | 155 | | foreach (double y in rayYcomponent) |
| 0 | 156 | | foreach (double x in rayXcomponent) |
| 0 | 157 | | { |
| 0 | 158 | | rays.Add(new Ray(new Vector( |
| 0 | 159 | | (float)x, (float)y, 0), |
| 0 | 160 | | new Vector(0, 0, 1))); |
| 0 | 161 | | } |
| | 162 | |
|
| 0 | 163 | | return rays; |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | /// <summary> |
| | 167 | | /// Get ASCII matrix from Matrix 2D. |
| | 168 | | /// </summary> |
| | 169 | | /// <param name="matrix">Matrix 2D to use.</param> |
| | 170 | | /// <returns>ASCII matrix for envimet.</returns> |
| | 171 | | public static string GetASCIImatrix(Matrix2d matrix) |
| 0 | 172 | | { |
| 0 | 173 | | string text = string.Empty; |
| 0 | 174 | | List<string> rows = new List<string>(); |
| | 175 | |
|
| 0 | 176 | | for (int j = matrix.GetLengthY() - 1; j >= 0; j--) |
| 0 | 177 | | { |
| 0 | 178 | | string[] line = new string[matrix.GetLengthX()]; |
| 0 | 179 | | for (int i = 0; i < matrix.GetLengthX(); i++) |
| 0 | 180 | | { |
| 0 | 181 | | line[i] = matrix[i, j]; |
| 0 | 182 | | } |
| 0 | 183 | | rows.Add(String.Join(",", line)); |
| 0 | 184 | | } |
| 0 | 185 | | text = String.Join("\n", rows) + "\n"; |
| | 186 | |
|
| 0 | 187 | | return text; |
| 0 | 188 | | } |
| | 189 | |
|
| | 190 | | /// <summary> |
| | 191 | | /// Calculate specific humidity of the atmosphere at 2500m. |
| | 192 | | /// </summary> |
| | 193 | | /// <param name="temperature">List of temperature |
| | 194 | | /// values [°C].</param> |
| | 195 | | /// <param name="relativeHumidity">List of relative |
| | 196 | | /// humidity values [%].</param> |
| | 197 | | /// <returns>Specific humidity at 2500m.</returns> |
| | 198 | | public static double GetAtmosphereSpecificHumidity(List<double> temperature, |
| | 199 | | List<double> relativeHumidity) |
| 0 | 200 | | { |
| | 201 | | const double AIR_PRESSURE = 1013.25; |
| | 202 | |
|
| 0 | 203 | | List<double> kelvinTemperature = temperature |
| 0 | 204 | | .Select(_ => _ + Util.TO_KELVIN) |
| 0 | 205 | | .ToList(); |
| | 206 | |
|
| 0 | 207 | | double meanTemperature = kelvinTemperature.Average(); |
| 0 | 208 | | double meanRelativeHumidity = relativeHumidity.Average(); |
| | 209 | |
|
| 0 | 210 | | double eSaturation = 0.6112 * Math.Exp(17.67 * ( |
| 0 | 211 | | meanTemperature - 273.15) / (meanTemperature - 29.66)) * 10; |
| 0 | 212 | | double qSaturation = (0.6112 * (eSaturation / AIR_PRESSURE)) * 1000; |
| 0 | 213 | | double specificHumidity = qSaturation * (meanRelativeHumidity / 100); |
| | 214 | |
|
| 0 | 215 | | return specificHumidity; |
| 0 | 216 | | } |
| | 217 | | } |
| | 218 | | } |