protected override void CreateVertices() { ILColormap colormap = m_panel.Colormap; if (m_vertices == null) { m_vertices = ILMemoryPool.Pool.New <VERTEXTYPEDEF>(m_Vertcount); } float val = 0.0f; float minZ = m_sourceArray.MinValue; float maxZ = m_sourceArray.MaxValue; float a = colormap.Length / (maxZ - minZ); byte ca = (byte)(m_opacity * 255); VERTEXTYPEDEF curVertex; int i = 0; // first row is special (no color, just for grid) for (int c = 0; c < m_cols - 1; c++) { curVertex = new VERTEXTYPEDEF(); curVertex.Vx = c - 0.5f; curVertex.Vy = -0.5f; curVertex.CA = ca; val = m_sourceArray.GetValue(0, c); colormap.Map((val - minZ) * a, out curVertex.CR, out curVertex.CG, out curVertex.CB); m_vertices[i++] = curVertex; } // right corner curVertex = new VERTEXTYPEDEF(); curVertex.Vx = m_cols - 1.5f; curVertex.Vy = -0.5f; curVertex.CA = ca; colormap.Map((val - minZ) * a, out curVertex.CR, out curVertex.CG, out curVertex.CB); m_vertices[i++] = curVertex; for (int r = 0; r < m_rows - 1; r++) { // first col part is special (no color) curVertex = new VERTEXTYPEDEF(); curVertex.Vx = -0.5f; curVertex.Vy = r + 0.5f; curVertex.CA = ca; colormap.Map((m_sourceArray.GetValue(r, 0) - minZ) * a, out curVertex.CR, out curVertex.CG, out curVertex.CB); m_vertices[i++] = curVertex; for (int c = 0; c < m_cols - 1; c++) { curVertex = new VERTEXTYPEDEF(); val = m_sourceArray.GetValue(r, c); // set color values colormap.Map((val - minZ) * a, out curVertex.CR, out curVertex.CG, out curVertex.CB); curVertex.CA = ca; curVertex.Vx = c + 0.5f; curVertex.Vy = r + 0.5f; m_vertices[i++] = curVertex; } } m_vertexReady = true; }
public static ILArray <int> configureVertices( ILArray <float> xVals, ILArray <float> yVals, ILArray <float> zVals, ILColormap cmap, C4fN3fV3f[] Vertices, byte opacity) { int i = 0, x, y, y0 = zVals.Dimensions[0] - 1; float minZ, maxZ; if (!zVals.GetLimits(out minZ, out maxZ, false)) { minZ = maxZ = 1.0f; } x = 0; y = 0; ILArray <float> colors = (tosingle((zVals - minZ) / (maxZ - minZ)))[":"] * (cmap.Length - 1); colors[isnan(colors)] = 0; bool useXvals = (xVals != null && !xVals.IsEmpty); bool useYvals = (yVals != null && !yVals.IsEmpty); foreach (float a in zVals.Values) { C4fN3fV3f v = Vertices[i]; v.Position = new ILPoint3Df( (useXvals)? xVals.GetValue(y, x): x , (useYvals)? yVals.GetValue(y, x): y0 - y , a); byte r, g, b; cmap.Map(colors.GetValue(i), out r, out g, out b); v.Color = Color.FromArgb(255, r, g, b); v.Alpha = opacity; Vertices[i++] = v; // set next position if (++y >= zVals.Dimensions[0]) { x++; y = 0; } } // create quad indices int numQuad = (zVals.Dimensions[0] - 1) * (zVals.Dimensions[1] - 1); x = 0; y = 0; ILArray <double> ret = zeros(4, numQuad); ILArray <double> mult = counter(0.0, 1.0, zVals.Dimensions.ToIntArray()); mult = mult["0:" + (zVals.Dimensions[0] - 2), "0:" + (zVals.Dimensions[1] - 2)]; mult = mult[":"].T; ret["0;:"] = mult; ret["3;:"] = mult + 1; mult = mult + zVals.Dimensions.SequentialIndexDistance(1); ret["2;:"] = mult + 1; ret["1;:"] = mult; return(toint32(ret)); }
internal static ILArray <double> CreateVertices(ILBaseArray dataInput , out ILArray <double> indices , double beta, double scaling , ILColormap colormap) { // each arrow needs 4 vertices (indexed rendering) ILArray <double> data = todouble(dataInput); int numRows = data.Dimensions[0]; int numCols = data.Dimensions[1]; ILArray <double> ret = new ILArray <double>(4, numCols * numRows, 2); // prepare indices indices = repmat(new ILArray <double>(new double[] { 0, 2, 1, 2, 3, 2 }, 6, 1), 1, numCols * numRows); indices = indices + repmat(counter(0.0, 4.0, 1, numCols * numRows), 6, 1); indices = indices.Reshape(2, numRows * numCols * 3); // normalize incoming data to length 1.0 ILArray <double> l = sqrt(sum(data * data, 2)); double maxL = (double)max(maxall(l), MachineParameterDouble.eps); l = (l / maxL)[":"].T * scaling; ILArray <double> alpha = atan2(data[":;:;1"], data[":;:;0"]); alpha = alpha[":"].T; ILArray <double> x = data[":;:;0"][":"].T * scaling; ILArray <double> y = data[":;:;1"][":"].T * scaling; ILArray <double> xO = repmat(linspace(1, numCols, numCols), numRows, 1)[":"].T; ILArray <double> yO = repmat(linspace(numRows, 1, numRows).T, 1, numCols)[":"].T; ret["0;:;0"] = xO - x; ret["1;:;0"] = xO + x - l / 2 * cos(alpha + beta); ret["2;:;0"] = xO + x; ret["3;:;0"] = xO + x - l / 2 * cos(alpha - beta); ret["0;:;1"] = yO - y; ret["1;:;1"] = yO + y - l / 2 * sin(alpha + beta); ret["2;:;1"] = yO + y; ret["3;:;1"] = yO + y - l / 2 * sin(alpha - beta); ret["0;0;2"] = 0.0; // prepare colors ret[":;:;3:5"] = todouble( repmat(colormap.Map (tosingle(l) * colormap.Length).Reshape(1, l.Length, 3) * 255, 4, 1, 1)); return(ret.Reshape(4 * numRows * numCols, 6).T); }
private void create(ILBaseArray data, Colormaps colormap) { ILArray <float> dataF = ILNumerics.BuiltInFunctions.ILMath.tosingle(data); m_boxes = new ILLitBox3D[data.Dimensions[0], data.Dimensions[1]]; float maxY = data.Dimensions[0] * (m_barLengthY + m_paddingY); // prepare coloring for top quads ILColormap cmap = new ILColormap(colormap); float minV, maxV, mult; dataF.GetLimits(out minV, out maxV); if (maxV > minV) { mult = (cmap.Length - 1) / (maxV - minV); } else { minV = 0; mult = 0; } for (int r = 0; r < data.Dimensions[0]; r++) { for (int c = 0; c < data.Dimensions[1]; c++) { float val = dataF.GetValue(r, c); ILPoint3Df max = new ILPoint3Df( (float)(c * (m_paddingX + m_barLengthX) + m_barLengthX) , (float)(maxY - r * (m_paddingY + m_barLengthY)) , val); ILPoint3Df min = new ILPoint3Df( max.X - m_barLengthX , max.Y - m_barLengthY , 0); Color topColor = cmap.Map((double)(val - minV) * mult); ILLitBox3D box = new ILLitBox3D(m_panel, min, max, m_barColor, topColor); box.GradientColor = m_barColorGradient; box.TopLabel.Color = topColor; box.TopLabel.Text = ""; m_boxes[r, c] = box; Add(box); } } }
protected override void CreateVertices() { ILColormap colormap = m_panel.Colormap; if (m_vertices == null) { m_vertices = ILMemoryPool.Pool.New <float>(m_Vertcount * 40); } float val = 0.0f; float minZ = m_globalClipping.ZMin, minC = (m_colors == null)? 0.0f : m_colors.MinValue; float maxZ = m_globalClipping.ZMax; bool useColorArray = !object.Equals(m_colors, null); float a; if (useColorArray) { a = colormap.Length / (m_colors.MaxValue - minC); } else { if (maxZ - minZ != 0.0f) { a = colormap.Length / (maxZ - minZ); } else { a = 0.0f; } } int curVertPos = 0, curVecPos = 0; if (m_shading == ShadingStyles.Interpolate) { #region shading interpolate for (int r = 0; r < m_rows; r++) { for (int c = 0; c < m_cols; c++) { curVecPos = r + c * m_rows; val = m_sourceArray.GetValue(r, c); // set color values if (useColorArray) { colormap.Map((m_colors.GetValue(curVecPos) - minC) * a, m_vertices, ref curVertPos); } else { if (a != 0) { colormap.Map((val - minZ) * a, m_vertices, ref curVertPos); } else { // plane: minz == maxz colormap.Map(colormap.Length / 2, m_vertices, ref curVertPos); } } m_vertices[curVertPos++] = m_opacity; curVertPos += 3; m_vertices[curVertPos++] = m_xCoords[curVecPos]; m_vertices[curVertPos++] = m_yCoords[curVecPos]; m_vertices[curVertPos++] = val; } } #endregion } else if (m_shading == ShadingStyles.Flat) { #region shading flat /** Consider a surface area like this: * * 8 - 9 -10 -11 * | / | / | / | * 4 - 5 - 6 - 7 * | / | / | / | * 0 - 1 - 2 - 3 * * The rectangle surrounded by 0-1-4-5 is than colored by the * vertex 5. This rectangle will be assembled by 2 triangles: * 0-1-5 and 0-4-5. Therefore the color for both rectangles * is the same and must be stored in the vertex No.5. * The vertices can be assembled in natural order, beginning * with 0 and approching m_vertexCount-1. The color for flat * shading is averaged over the neighbor corners of each * rectangle. In our Example the color stored in 5 is averaged * over the values of the vertices 0,1,4 and 5. The first row * and the first column are not used for the surface. However * it may _be_ used for the wireframe grid if that is drawn with * interpolated colors (Wireframe.Color.IsEmpty). So we must * prepare the color for it -> here we just take the value itself. */ // first row: no color average for (int c = 0; c < m_cols; c++) { val = m_sourceArray.GetValue(0, c); if (useColorArray) { colormap.Map((m_colors.GetValue(0, c) - minC) * a, m_vertices, ref curVertPos); } else { colormap.Map((val - minZ) * a, m_vertices, ref curVertPos); } m_vertices[curVertPos++] = m_opacity; curVertPos += 3; m_vertices[curVertPos++] = m_xCoords[m_rows * c]; m_vertices[curVertPos++] = m_yCoords[m_rows * c]; m_vertices[curVertPos++] = val; } for (int r = 1; r < m_rows; r++) { val = m_sourceArray.GetValue(r, 0); if (useColorArray) { colormap.Map((m_colors.GetValue(r) - minC) * a, m_vertices, ref curVertPos); } else { colormap.Map((val - minZ) * a, m_vertices, ref curVertPos); } m_vertices[curVertPos++] = m_opacity; curVertPos += 3; m_vertices[curVertPos++] = m_xCoords[r]; m_vertices[curVertPos++] = m_yCoords[r]; m_vertices[curVertPos++] = val; // next columns: average color over precomputed corners for (int c = 1; c < m_cols; c++) { curVecPos = r + c * m_rows; val = m_sourceArray.GetValue(r, c); val += m_sourceArray.GetValue(r - 1, c); val += m_sourceArray.GetValue(r, c - 1); val += m_sourceArray.GetValue(r - 1, c - 1); val /= 4; if (useColorArray) { colormap.Map((m_colors.GetValue(curVecPos) - minC) * a, m_vertices, ref curVertPos); } else { colormap.Map((val - minZ) * a, m_vertices, ref curVertPos); } m_vertices[curVertPos++] = m_opacity; curVertPos += 3; m_vertices[curVertPos++] = m_xCoords[curVecPos]; m_vertices[curVertPos++] = m_yCoords[curVecPos]; m_vertices[curVertPos++] = m_sourceArray.GetValue(r, c); } } #endregion m_oldColormap = colormap; } #region create normals // todo: depends on lighting enabled or not...! // reset vertices pointer and start all over curVertPos = 0; float nx, ny, nz; for (int r = 0; r < m_rows; r++) { for (int c = 0; c < m_cols; c++) { nx = m_vertices[curVertPos + 7]; ny = m_vertices[curVertPos + 8]; nz = m_vertices[curVertPos + 9]; if (c > 0) { nx += m_vertices[curVertPos - 3]; ny += m_vertices[curVertPos - 2]; nz += m_vertices[curVertPos - 1]; } if (c < m_cols - 1) { nx += m_vertices[curVertPos + 17]; ny += m_vertices[curVertPos + 18]; nz += m_vertices[curVertPos + 19]; } if (r > 0 && r < m_rows) { nx += m_vertices[curVertPos - m_cols * 10 + 10]; ny += m_vertices[curVertPos - m_cols * 10 + 11]; nz += m_vertices[curVertPos - m_cols * 10 + 12]; } if (r < m_rows - 1) { nx += m_vertices[curVertPos + m_cols * 10 + 10]; ny += m_vertices[curVertPos + m_cols * 10 + 11]; nz += m_vertices[curVertPos + m_cols * 10 + 12]; } // normalize float len = (float)Math.Sqrt(nx * nx + ny * ny + nz * nz); m_vertices[curVertPos + 4] = nx / len; m_vertices[curVertPos + 5] = ny / len; m_vertices[curVertPos + 6] = nz / len; curVertPos += 10; } } #endregion m_oldShading = m_shading; m_vertexReady = true; }
protected override void OnPaint(PaintEventArgs e) { if (!Visible) { return; // || (m_minValue == m_maxValue)) return; } base.OnPaint(e); // draw border Brush br = new SolidBrush(ForeColor); Rectangle drawSpace = ClientRectangle; Pen pen = new Pen(br); // draw labels drawSpace.Location = new Point(Padding.Left, Padding.Top); drawSpace.Width = ClientRectangle.Width - Padding.Horizontal; drawSpace.Height = ClientRectangle.Height - Padding.Vertical; string format = String.Format("g{0}", m_precision); SizeF labsize = e.Graphics.MeasureString(String.Format(".-e08" + new String('0', m_precision)), Font); int tickCount; if (m_orientation == TextOrientation.Vertical) { tickCount = (int)Math.Floor((double)(drawSpace.Height - labsize.Height) / (labsize.Height + m_tickLabelsPadding)); } else { tickCount = (int)Math.Floor((double)(drawSpace.Width - labsize.Width) / (labsize.Width + m_tickLabelsPadding)); } List <float> labels = Collections.ILTickCollection.NiceLabels(m_minValue, m_maxValue, tickCount, TickLabelRenderingHint.Auto); tickCount = labels.Count; // draw labels RectangleF dR; List <int> tickPositions = new List <int>(tickCount); if (m_orientation == TextOrientation.Vertical) { dR = new RectangleF(drawSpace.Left, drawSpace.Top + drawSpace.Height - labsize.Height, labsize.Width, labsize.Height); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Far; sf.LineAlignment = StringAlignment.Near; // draw all values in between float offsY = drawSpace.Top + drawSpace.Height; float mult = (drawSpace.Height) / (m_maxValue - m_minValue); foreach (float curVal in labels) { // translate into screen coords dR.Y = (int)(offsY - (curVal - m_minValue) * mult); tickPositions.Add((int)dR.Y); dR.Y -= (labsize.Height / 2); e.Graphics.DrawString(curVal.ToString(format), Font, br, dR, sf); } // prepare remaining space for gradient scale drawSpace.X += (int)(labsize.Width + 2 * m_tickLabelsPadding); drawSpace.Width -= (int)(labsize.Width + 2 * m_tickLabelsPadding); } else { // draw labels above gradient scale dR = new RectangleF(drawSpace.Left, drawSpace.Top, labsize.Width, labsize.Height); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; e.Graphics.DrawString(m_minValue.ToString(format), Font, br, dR, sf); tickPositions.Add(drawSpace.Left); // draw maximum sf.Alignment = StringAlignment.Far; dR.X = drawSpace.Left + drawSpace.Width - labsize.Width; e.Graphics.DrawString(m_maxValue.ToString(format), Font, br, dR, sf); tickPositions.Add(drawSpace.Left + drawSpace.Width); sf.Alignment = StringAlignment.Center; // draw all values in between float mult = (drawSpace.Width) / (m_maxValue - m_minValue); foreach (float curVal in labels) // = ls; curVal <= m_maxValue - m/2; curVal = (float) (curVal + m)) { // translate into screen coords { dR.X = (int)((curVal - m_minValue) * mult); if (dR.X < drawSpace.Left + labsize.Width) { continue; } if (dR.X > drawSpace.Left + drawSpace.Width - labsize.Width) { continue; } tickPositions.Add((int)dR.X); dR.X -= (labsize.Width / 2); e.Graphics.DrawString(curVal.ToString(format), Font, br, dR, sf); } // prep. rem. space for gradient scale drawSpace.Y += (int)(labsize.Height + 2 * m_tickLabelsPadding); drawSpace.Height -= (int)(labsize.Height + 2 * m_tickLabelsPadding); } // draw color gradient: border (gray) pen.Color = Color.FromArgb(180, 180, 180); e.Graphics.DrawRectangle(pen, drawSpace); drawSpace.X += 1; drawSpace.Y += 1; drawSpace.Width -= 1; drawSpace.Height -= 1; Internal.ILColorProvider hls = new Internal.ILColorProvider(0.0f, 0.5f, 1.0f); if (m_orientation == TextOrientation.Vertical) { for (int i = drawSpace.Height; i-- > 0;) { pen.Color = m_colormap.Map((drawSpace.Height - i) / (double)drawSpace.Height * m_colormap.Length); e.Graphics.DrawLine(pen, drawSpace.Left, drawSpace.Top + i, drawSpace.Left + drawSpace.Width, drawSpace.Top + i); } // draw tick bars int le = drawSpace.Left, re = drawSpace.Left + drawSpace.Width, le2 = le + (re - le) / 4, re2 = re - (re - le) / 4; pen.Color = ForeColor; foreach (int y in tickPositions) { e.Graphics.DrawLine(pen, le, y, le2, y); e.Graphics.DrawLine(pen, re2, y, re, y); } } else { for (int i = drawSpace.Width; i-- > 0;) { pen.Color = m_colormap.Map(i / (double)drawSpace.Width * m_colormap.Length); e.Graphics.DrawLine(pen, drawSpace.Left + i, drawSpace.Top, drawSpace.Left + i, drawSpace.Top + drawSpace.Height); } // draw tick bars int up = drawSpace.Top, bot = drawSpace.Top + drawSpace.Height, up2 = up + (bot - up) / 4, bot2 = bot - (bot - up) / 4; pen.Color = ForeColor; foreach (int x in tickPositions) { e.Graphics.DrawLine(pen, x, up, x, up2); e.Graphics.DrawLine(pen, x, bot2, x, bot); } } }