/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="field"></param> /// <param name="getColor"></param> /// <param name="parallel"></param> /// <returns></returns> public static PointCloud ToPointCloud <T>(this GridField3d <T> field, Func <T, Color> getColor, bool parallel = false) where T : struct { var cloud = new PointCloud(field.Coordinates.Select(p => p.ToPoint3d())); var vals = field.Values; Action <Tuple <int, int> > body = range => { for (int i = range.Item1; i < range.Item2; i++) { cloud[i].Color = getColor(vals[i]); } }; if (parallel) { Parallel.ForEach(Partitioner.Create(0, field.Count), body); } else { body(Tuple.Create(0, field.Count)); } return(cloud); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="images"></param> /// <param name="field"></param> /// <param name="mapper"></param> public static void ReadFromImageStack <T>(IEnumerable <Bitmap> images, GridField3d <T> field, Func <Color, T> mapper) where T : struct { int nxy = field.CountXY; int index = 0; Parallel.ForEach(images, image => { ReadFromImage(image, field.Values, index, mapper); index += nxy; }); }
/// <summary> /// /// </summary> /// <param name="field"></param> /// <param name="path"></param> /// <param name="mapper"></param> public static void SaveAsImageStack <T>(GridField3d <T> field, string path, Func <T, Color> mapper) where T : struct { string dir = Path.GetDirectoryName(path); string name = Path.GetFileNameWithoutExtension(path); string ext = Path.GetExtension(path); Parallel.For(0, field.CountZ, z => { using (Bitmap bmp = new Bitmap(field.CountX, field.CountY, PixelFormat.Format32bppArgb)) { WriteToImage(field, z, bmp, mapper); bmp.Save(String.Format(@"{0}\{1}_{2}{3}", dir, name, z, ext)); } }); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="field"></param> /// <param name="layer"></param> /// <param name="mapper"></param> /// <param name="image"></param> public static void ReadFromImage <T>(Bitmap image, GridField3d <T> field, int layer, Func <Color, T> mapper) where T : struct { ReadFromImage(image, field.Values, layer * field.CountXY, mapper); }
/// <summary> /// Writes a layer of the given field to an existing image. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="field"></param> /// <param name="layer"></param> /// <param name="mapper"></param> /// <param name="image"></param> public static void WriteToImage <T>(GridField3d <T> field, int layer, Bitmap image, Func <T, Color> mapper) where T : struct { WriteToImage(field.Values, layer * field.CountXY, image, mapper); }