Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Platform/OpenGL/ILOGLImageSCGraph.cs @ 10903

Last change on this file since 10903 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 12.6 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Drawing;
42using ILNumerics.Exceptions;
43using OpenTK;
44using OpenTK.Graphics;
45using OpenTK.Graphics.OpenGL;
46using OpenTK.Graphics.OpenGL.Enums;
47using ILNumerics.Drawing.Graphs;
48using ILNumerics.Drawing.Misc;
49using ILNumerics.Misc;   
50using VERTEXTYPEDEF = ILNumerics.Drawing.Platform.OpenGL.ILOGLImageSCGraph.VertexC4V2;
51
52namespace ILNumerics.Drawing.Platform.OpenGL {
53    /// <summary>
54    /// OpenGL implementation for ILImageSCGraph
55    /// </summary>
56    public class ILOGLImageSCGraph : ILImageSCGraph {
57
58        #region attributes
59        protected VERTEXTYPEDEF[] m_vertices;
60        #endregion
61
62        #region vertex definition
63        /// <summary>
64        /// Vertex definition: 4Color (uint), 2 Vertex (float)
65        /// </summary>
66        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
67        [System.Diagnostics.DebuggerDisplay("RGBA:{CR},{CG},{CB},{CA} (X,Y)=({Vx},{Vy})", Name="Vertex")]
68        public struct VertexC4V2 {
69            public byte CR;
70            public byte CG;
71            public byte CB;
72            public byte CA;
73            public float Vx;
74            public float Vy;
75        }
76        #endregion
77
78        #region constructors
79        internal ILOGLImageSCGraph(ILOGLPanel panel, ILArray<float> X,
80                                  ILArray<float> Y, ILArray<float> Z, ILArray<float> C,
81                                 ILClippingData clippingContainer)
82            : base(panel, X, Y, Z, C, clippingContainer) {
83            using (ILScope.Enter(X, Y, Z, C)) {
84                m_panel = panel;
85                m_localClipping.ZMax = 0;
86                m_localClipping.ZMin = 0;
87                m_localClipping.XMax = (float)m_cols + 0.5f;
88                m_localClipping.XMin = -0.5f;
89                m_localClipping.YMax = (float)m_rows + 0.5f;
90                m_localClipping.YMin = -0.5f;
91            }
92        }
93        #endregion
94
95        #region helper functions
96        protected override void CreateVertices() {
97            ILColormap colormap = m_panel.Colormap; 
98            if (m_vertices == null) {
99                m_vertices = ILMemoryPool.Pool.New<VERTEXTYPEDEF>(m_Vertcount);
100            }
101            float val = 0.0f;
102            float minZ = m_sourceArray.MinValue;
103            float maxZ = m_sourceArray.MaxValue;
104            float a = colormap.Length / (maxZ - minZ);
105            byte ca = (byte)(m_opacity * 255);
106            VERTEXTYPEDEF curVertex; int i = 0;
107            // first row is special (no color, just for grid)
108            for (int c = 0; c < m_cols-1; c++) {
109                curVertex = new VERTEXTYPEDEF();
110                curVertex.Vx = c-0.5f;
111                curVertex.Vy = -0.5f;
112                curVertex.CA = ca;
113                val = m_sourceArray.GetValue(0,c);
114                colormap.Map((val - minZ) * a,
115                    out curVertex.CR, out curVertex.CG,out curVertex.CB);
116                m_vertices[i++] = curVertex; 
117            }
118            // right corner
119            curVertex = new VERTEXTYPEDEF();
120            curVertex.Vx = m_cols-1.5f;
121            curVertex.Vy = -0.5f;
122            curVertex.CA = ca;
123            colormap.Map((val - minZ) * a,
124                out curVertex.CR, out curVertex.CG,out curVertex.CB);
125            m_vertices[i++] = curVertex; 
126            for (int r = 0; r < m_rows-1; r++) {
127                // first col part is special (no color)
128                curVertex = new VERTEXTYPEDEF();
129                curVertex.Vx = -0.5f;
130                curVertex.Vy = r+0.5f;
131                curVertex.CA = ca;
132                colormap.Map((m_sourceArray.GetValue(r,0) - minZ) * a,
133                    out curVertex.CR, out curVertex.CG,out curVertex.CB);
134                m_vertices[i++] = curVertex; 
135                for (int c = 0; c < m_cols-1; c++) {
136                    curVertex = new VERTEXTYPEDEF();
137                    val = m_sourceArray.GetValue(r,c);
138                    // set color values
139                    colormap.Map((val- minZ) * a,
140                        out curVertex.CR, out curVertex.CG,out curVertex.CB);
141                    curVertex.CA = ca;
142                    curVertex.Vx = c+0.5f;
143                    curVertex.Vy = r+0.5f;
144                    m_vertices[i++] = curVertex;
145                }
146            }
147            m_vertexReady = true;
148        }
149        protected override void m_globalClipping_Changed(object sender, ClippingChangedEventArgs e) {
150            base.m_globalClipping_Changed(sender, e);
151            //m_vertexReady = false;
152            //m_indexReady = false;
153        }
154        #endregion
155
156        #region public interface
157        /// <summary>
158        /// Draws the graph into existing context
159        /// </summary>
160        public override void Draw(ILRenderProperties p) {
161            if (m_panel.Camera.Rho > Math.PI/2) return;
162            if (!m_isReady)
163                Configure();
164            float zPos = m_zPosition;
165            if (zPos == float.PositiveInfinity) {
166                zPos = m_panel.Limits.ZMax;
167            } else if (zPos == float.NegativeInfinity) {
168                zPos = m_panel.Limits.ZMin;
169            }
170            GL.Translate(0.0f, 0.0f, zPos);
171            GL.Enable(EnableCap.Blend);
172            GL.Disable(EnableCap.DepthTest);
173            GL.BlendFunc (BlendingFactorSrc.SrcAlpha,
174                          BlendingFactorDest.OneMinusSrcAlpha);           
175            GL.ShadeModel(ShadingModel.Flat);
176            unsafe {
177                fixed (VERTEXTYPEDEF* pVertices = m_vertices) {
178                    GL.InterleavedArrays(
179                           InterleavedArrayFormat.C4ubV2f,0,(IntPtr)pVertices);
180                    if (m_filled) {
181                        fixed (UInt32* pIndices = m_indices) {
182                            for (int i = 0; i < m_stripesCount; i++) {
183                                GL.DrawElements(BeginMode.Triangles,m_stripesLen,
184                                    DrawElementsType.UnsignedInt,
185                                    (IntPtr)(pIndices+i*m_stripesLen));
186                            }
187                            GL.Finish();
188                        }
189                    }
190                    #region draw grid
191                    if (m_wireLines.Visible) {
192                        if (m_wireLines.Style == LineStyle.Solid) {
193                            GL.Disable(EnableCap.LineStipple);
194                        } else {
195                            int stipFactr = 1; 
196                            short stipple;
197                            if (m_wireLines.Style != LineStyle.UserPattern)
198                                stipple = ILPanel.StippleFromLineStyle(
199                                          m_wireLines.Style, ref stipFactr);
200                            else
201                                stipple = m_wireLines.Pattern;
202                            GL.Enable(EnableCap.LineStipple);
203                            GL.LineStipple((int)m_wireLines.PatternScale,stipple);
204                        }
205                        if (m_wireLines.Antialiasing)
206                            GL.Enable(EnableCap.LineSmooth);
207                        else
208                            GL.Disable(EnableCap.LineSmooth);
209                        GL.Disable(EnableCap.Blend);
210                        GL.LineWidth(m_wireLines.Width);
211                        if (m_wireLines.Color != System.Drawing.Color.Empty) {
212                            GL.DisableClientState(EnableCap.ColorArray);
213                            GL.Color3(m_wireLines.Color);
214                        }
215                        fixed (UInt32* pIndices = m_gridIndices) {
216                            GL.DrawElements(BeginMode.Lines,
217                                        m_gridIndicesCount,
218                                        DrawElementsType.UnsignedInt,
219                                        (IntPtr)pIndices);
220                            GL.Finish();
221                        }
222                    }
223                    #endregion
224                }
225            }
226            GL.Translate(0.0f, 0.0f, -zPos);
227        }
228        /// <summary>
229        /// Dispose off this graph's vertices
230        /// </summary>
231        public override void Dispose() {
232            base.Dispose();
233            if (m_vertices != null) {
234                ILMemoryPool.Pool.Free<VERTEXTYPEDEF>(m_vertices);
235            }
236        }
237        /// <summary>
238        /// Ensures the recalculation of vertices if neccessary
239        /// </summary>
240        public override void Invalidate() {
241            if (Math.Floor(m_panel.Camera.Phi / (Math.PI / 4.0)) != m_oldSubQuadrant) {   
242                //must only recalculate indices, and only if the camera subquadrant has changed
243                m_indexReady = false;
244                m_isReady = false;
245            }
246        }
247        public override void DrawToLegend(ILRenderProperties p, Rectangle sampleRect, Rectangle labelRect) {
248            if (m_filled) {
249                // draw inner filled area
250                GL.ShadeModel(ShadingModel.Smooth);
251                GL.Begin(BeginMode.TriangleStrip);
252                    GL.Color3(m_panel.Colormap[m_panel.Colormap.Length-1]);
253                    GL.Vertex2(sampleRect.X,sampleRect.Y + sampleRect.Height);
254                    GL.Color3(m_panel.Colormap[(int)(m_panel.Colormap.Length/2)]);
255                    GL.Vertex2(sampleRect.X,sampleRect.Y);
256                    GL.Vertex2(sampleRect.X+sampleRect.Width,sampleRect.Y + sampleRect.Height);
257                    GL.Color3(m_panel.Colormap[0]);
258                    GL.Vertex2(sampleRect.X+sampleRect.Width,sampleRect.Y);
259                GL.End();
260            }
261            if (m_wireLines.Visible) {
262                ILNumerics.Drawing.Platform.OpenGL.ILOGLPanel.SetupLineStyle(m_wireLines);
263                GL.Begin(BeginMode.LineStrip);
264                    GL.Vertex2(sampleRect.X,sampleRect.Y);
265                    GL.Vertex2(sampleRect.X+sampleRect.Width,sampleRect.Y);
266                    GL.Vertex2(sampleRect.X+sampleRect.Width,sampleRect.Y + sampleRect.Height);
267                    GL.Vertex2(sampleRect.X,sampleRect.Y + sampleRect.Height);
268                    GL.Vertex2(sampleRect.X,sampleRect.Y);
269                GL.End();
270            }
271            m_label.m_position.X = labelRect.X + labelRect.Width / 2;
272            m_label.m_position.Y = labelRect.Y + labelRect.Height / 2;
273            m_label.Anchor = new PointF(.5f,.5f);  //  TickLabelAlign.center | TickLabelAlign.vertCenter;
274            m_label.Draw(p);
275        }
276        #endregion
277
278    }
279}
Note: See TracBrowser for help on using the repository browser.