public static void UpdatetHistogramMesh(DataObject dobjs, int Dimension, int binSize, bool smooth, float scale, Material mat, Transform holder, float minFilter, float maxFilter, float minNormalizer, float maxNormalizer, ref Mesh mesh) { mesh.Clear(); //get the array of dimension DiscreteBinner binner = new DiscreteBinner(); float[] values = dobjs.GetCol(dobjs.DataArray, Dimension); //values = values.Where(x => x >= minNormalizer + 0.5f && x <= maxNormalizer + 0.5f).ToArray(); float minVal = dobjs.DimensionsRange[Dimension].x; float maxVal = dobjs.DimensionsRange[Dimension].y; binner.MakeIntervals(values, dobjs.Metadata[Dimension].binCount); foreach (float value in values) { binner.Bin(value); } float[] bins = binner.bins; int _binSize = values.Length; float minBin = bins.Min(); float maxBin = bins.Max(); float minNormInternal = 0f; float maxNormInternal = 0f; //create the data points height (~ histo) List <Vector3> l = new List <Vector3>(); for (int i = 0; i < bins.Length; i++) { float y1 = UtilMath.normaliseValue(i, 0, bins.Length - 1, -0.5f, 0.5f); if ((y1 >= (minFilter) && y1 <= (maxFilter))) { // normalize positions to range in -0.5...0.5 float y = UtilMath.normaliseValue(i, 0, bins.Length - 1, 0.5f, -0.5f); float ynorm = UtilMath.normaliseValue(-y, minNormalizer, maxNormalizer, 0.5f, -0.5f); if (ynorm >= -0.5f && ynorm <= 0.5f) { float x = UtilMath.normaliseValue(bins[i], minBin, maxBin, -0.5f, 0.5f); Vector3 v = new Vector3(x, ynorm, 0f); l.Add(v); } } } // generate the mesh //Mesh mesh = new Mesh(); List <Vector3> verts = new List <Vector3>(); List <int> tris = new List <int>(); float step = 0.5f / bins.Length; for (int i = 0; i < l.Count; ++i) { Vector3 v = l[i]; verts.Add(new Vector3(v.y - step, -0.5f, 0.0001f)); verts.Add(new Vector3(v.y - step, v.x, 0.0001f)); verts.Add(new Vector3(v.y + step, v.x, 0.0001f)); verts.Add(new Vector3(v.y + step, -0.5f, 0.0001f)); verts.Add(new Vector3(v.y - step, -0.5f, -0.0001f)); verts.Add(new Vector3(v.y - step, v.x, -0.0001f)); verts.Add(new Vector3(v.y + step, v.x, -0.0001f)); verts.Add(new Vector3(v.y + step, -0.5f, -0.0001f)); // front tris.Add(i * 8 + 0 + 0); tris.Add(i * 8 + 0 + 1); tris.Add(i * 8 + 0 + 3); tris.Add(i * 8 + 0 + 3); tris.Add(i * 8 + 0 + 1); tris.Add(i * 8 + 0 + 2); // back tris.Add(i * 8 + 4 + 3); tris.Add(i * 8 + 4 + 1); tris.Add(i * 8 + 4 + 0); tris.Add(i * 8 + 4 + 2); tris.Add(i * 8 + 4 + 1); tris.Add(i * 8 + 4 + 3); } mesh.vertices = verts.ToArray(); mesh.triangles = tris.ToArray(); mesh.RecalculateBounds(); mesh.RecalculateNormals(); }
public Tuple <GameObject, Vector3[]> CreateBarHistogramView(DataObject dobjs, int Dimension, int binSize, bool smooth, float scale, Material mat, Transform holder, float minFilter, float maxFilter, float minNormalizer, float maxNormalizer) { GameObject Snax = new GameObject(); //get the array of dimension DiscreteBinner binner = new DiscreteBinner(); float[] values = dobjs.GetCol(dobjs.DataArray, Dimension); values = values.Where(x => x >= minFilter + 0.5f && x <= maxFilter + 0.5f).ToArray(); float minVal = dobjs.DimensionsRange[Dimension].x; float maxVal = dobjs.DimensionsRange[Dimension].y; binner.MakeIntervals(values, dobjs.Metadata[Dimension].binCount); foreach (float value in values) { binner.Bin(value); } float[] bins = binner.bins; int _binSize = values.Length; float minBin = Mathf.Min(bins.Min(), 0); float maxBin = bins.Max(); //create the data points height (~ histo) List <Vector3> l = new List <Vector3>(); for (int i = 0; i < bins.Length; i++) { // normalize positions to range in -0.5...0.5 float y = UtilMath.normaliseValue(i, 0, bins.Length - 1, 0.5f, -0.5f); float x = UtilMath.normaliseValue(bins[i], minBin, maxBin, minNormalizer, maxNormalizer); // -0.5f, 0.5f); Vector3 v = new Vector3(x, y, 0f); l.Add(v); } // generate the mesh MeshFilter filter = Snax.AddComponent <MeshFilter>(); Mesh mesh = new Mesh(); filter.mesh = mesh; List <Vector3> verts = new List <Vector3>(); List <int> tris = new List <int>(); float step = 0.5f / bins.Length; for (int i = 0; i < bins.Length; ++i) { Vector3 v = l[i]; verts.Add(new Vector3(v.y - step, -0.5f, 0.0001f)); verts.Add(new Vector3(v.y - step, v.x, 0.0001f)); verts.Add(new Vector3(v.y + step, v.x, 0.0001f)); verts.Add(new Vector3(v.y + step, -0.5f, 0.0001f)); verts.Add(new Vector3(v.y - step, -0.5f, -0.0001f)); verts.Add(new Vector3(v.y - step, v.x, -0.0001f)); verts.Add(new Vector3(v.y + step, v.x, -0.0001f)); verts.Add(new Vector3(v.y + step, -0.5f, -0.0001f)); // front tris.Add(i * 8 + 0 + 0); tris.Add(i * 8 + 0 + 1); tris.Add(i * 8 + 0 + 3); tris.Add(i * 8 + 0 + 3); tris.Add(i * 8 + 0 + 1); tris.Add(i * 8 + 0 + 2); // back tris.Add(i * 8 + 4 + 3); tris.Add(i * 8 + 4 + 1); tris.Add(i * 8 + 4 + 0); tris.Add(i * 8 + 4 + 2); tris.Add(i * 8 + 4 + 1); tris.Add(i * 8 + 4 + 3); } mesh.vertices = verts.ToArray(); mesh.triangles = tris.ToArray(); mesh.RecalculateBounds(); mesh.RecalculateNormals(); MeshRenderer meshRenderer = Snax.AddComponent <MeshRenderer>(); meshRenderer.material = histogramMaterial; return(new Tuple <GameObject, Vector3[]>(Snax, l.ToArray())); }