Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Graphs/ILPlot2DGraph.cs @ 11194

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

#1967: ILNumerics source for experimentation

File size: 12.2 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.Collections.Generic;
42using System.Text;
43using System.Drawing;
44using ILNumerics;
45using ILNumerics.Drawing;
46using ILNumerics.Exceptions;
47using ILNumerics.Drawing.Marker;
48using ILNumerics.Drawing.Interfaces;
49using ILNumerics.Drawing.Shapes;
50
51namespace ILNumerics.Drawing.Graphs {
52    /// <summary>
53    /// 2D line &amp; point graph
54    /// </summary>
55    public abstract class ILPlot2DGraph : ILGraph, IILLegendRenderer, IILPanelConfigurator {
56
57        #region attributes
58        protected C4bV3f[] m_vertices;
59        protected int m_vertexCount;
60        protected ILLineProperties m_properties;
61        protected ILMarker m_marker;
62        int m_autoLimitsUpdateCount = 0;
63        int m_updateCount = 0;
64        protected int m_startID;
65
66        #endregion
67
68        #region properties
69        /// <summary>
70        /// number of subsequent updates ('Queue') before limits get recalculated. Default: 0 (recalculate on every update)
71        /// </summary>
72        public int AutoLimitsUpdateCount {
73            get { return m_autoLimitsUpdateCount; }
74            set { m_autoLimitsUpdateCount = value; }
75        }
76        /// <summary>
77        /// Get properties of lines
78        /// </summary>
79        public ILLineProperties Line {
80            get {
81                return m_properties;
82            }
83        }
84        /// <summary>
85        /// Get properties of markers
86        /// </summary>
87        public ILMarker Marker {
88            get {
89                return m_marker;
90            }
91        }
92        /// <summary>
93        /// access to internal vertex array
94        /// </summary>
95        /// <remarks><para>after altering vertex data, one must call
96        /// Invalidate() to signal those changes.</para></remarks>
97        public C4bV3f[] Vertices {
98            get {
99                return m_vertices;
100            }
101        }
102        /// <summary>
103        /// Invalidate the graph after vertex data have been changed.
104        /// </summary>
105        public override void Invalidate() {
106            base.Invalidate();
107            updateClipping(); 
108            OnChanged("Data");
109        }
110        /// <summary>
111        /// draws a small example of the visual output
112        /// </summary>
113        /// <param name="p">render properties</param>
114        /// <param name="sampleArea">area to draw the line + marker into</param>
115        /// <param name="labelArea">area to draw corresponding label into</param>
116        /// <remarks>derived classes implement this for current device contexts</remarks>
117        public virtual void DrawToLegend(ILRenderProperties p, Rectangle sampleArea, Rectangle labelArea) {
118            //if (g == null) throw new ILArgumentException ("ILGraph: DrawIntoLegend: invalid graphics object (null)");
119            //throw new NotImplementedException ("ILGraph cannot draw to bitmap yet!");
120        }
121
122        /// <summary>
123        /// Size of label
124        /// </summary>
125        /// <value>size</value>
126        public Size LabelSize { get { return m_label.Size; } }
127        #endregion
128
129        #region constructor
130        /// <summary>
131        /// [internal] constructor - do not use this! Use ILPanel.Graphs.Add...() instead!
132        /// </summary>
133        /// <param name="panel">panel hosting the scene</param>
134        /// <param name="sourceArray">data array</param>
135        /// <param name="clippingContainer">hosting panels clipping data</param>
136        public ILPlot2DGraph(ILPanel panel, ILArray<float> sourceArray,
137                              ILClippingData clippingContainer)
138            : base(panel, clippingContainer) {
139            using (ILScope.Enter(sourceArray)) {
140                if (object.Equals(sourceArray, null) || !sourceArray.IsVector || !sourceArray.IsNumeric)
141                    throw new ILArgumentException("Plot2D: supplied data must be numeric (real valued) vector!");
142                int pos = 0;
143                ILArray<float> data;
144                C4bV3f vert = new C4bV3f();
145                data = (ILArray<float>)sourceArray;
146                m_vertices = new C4bV3f[data.Length + 1];
147                m_vertexCount = m_vertices.Length;
148                m_updateCount = 0;
149                m_startID = 0;
150                m_properties = new ILLineProperties();
151                m_properties.Color = Color.DarkBlue;
152                m_properties.Width = 2;
153                //m_properties.Antialiasing = true;
154                m_properties.Changed += new EventHandler(m_properties_Changed);
155                foreach (float val in data) {
156                    vert.Position = new ILPoint3Df(pos, val, 0);
157                    vert.Color = Color.Red;
158                    m_vertices[pos++] = vert;
159                }
160                m_marker = new ILMarker(panel);
161                m_marker.Changed += new EventHandler(m_properties_Changed);
162                m_graphType = GraphType.Plot2D;
163                m_localClipping.Set(new ILPoint3Df(0, data.MinValue, 0), new ILPoint3Df(data.Length - 1, data.MaxValue, 0));
164            }
165        }
166        /// <summary>
167        /// [internal] constructor - do not use this! Use ILPanel.Graphs.Add...() instead!
168        /// </summary>
169        /// <param name="panel">panel hosting the scene</param>
170        /// <param name="XData">x data array</param>
171        /// <param name="YData">y data array</param>
172        /// <param name="clippingContainer">hosting panels clipping data</param>
173        internal ILPlot2DGraph(ILPanel panel, ILArray<float> XData, ILArray<float> YData,
174                              ILClippingData clippingContainer)
175            : base (panel,clippingContainer) {
176                using (ILScope.Enter(XData, YData)) {
177                    if (!XData.IsVector)
178                        throw new ILArgumentException("Plot2D: supplied data must be a real vector!");
179                    if (!YData.IsVector)
180                        throw new ILArgumentException("Plot2D: XData and YData must be real vectors!");
181                    if (YData.Length != XData.Length)
182                        throw new ILArgumentException("Plot2D: XData and YData must have the same length!");
183                    int pos = 0;
184                    ILArray<float> dataX, dataY;
185                    C4bV3f vert = new C4bV3f();
186                    dataX = (ILArray<float>)XData;
187                    dataY = (ILArray<float>)YData;
188                    m_vertices = new C4bV3f[dataX.Length + 1];
189                    m_vertexCount = m_vertices.Length;
190                    m_startID = m_vertexCount - 1;
191                    m_updateCount = 0;
192                    m_properties = new ILLineProperties();
193                    m_properties.Color = Color.DarkBlue;
194                    m_properties.Changed += new EventHandler(m_properties_Changed);
195                    foreach (float val in dataX) {
196                        vert.Position = new ILPoint3Df(val, dataY.GetValue(pos), 0);
197                        vert.Color = m_properties.Color;
198                        m_vertices[pos++] = vert;
199                    }
200                    m_marker = new ILMarker(panel);
201                    m_marker.Changed += new EventHandler(m_marker_Changed);
202                    m_graphType = GraphType.Plot2D;
203                    updateClipping();
204                }
205        }
206        public void Queue(IILVertexDefinition vertex) {
207            if (m_startID == 0) {
208                SetVertex(m_vertexCount-1,vertex);
209                m_startID = 1;
210            } else {
211                SetVertex(m_startID,vertex);
212                m_vertices[0] = m_vertices[m_vertexCount-1];
213                m_startID++;
214            }
215            if (m_startID == m_vertexCount-1) {
216                m_startID = 0;
217            }
218            if (m_updateCount++ < m_autoLimitsUpdateCount) {
219                m_localClipping.Update(vertex.Position,vertex.Position);
220            } else {
221                m_updateCount = 0;
222                Invalidate();
223            }
224        }
225
226        #endregion
227
228        #region private helper
229        public void SetVertex(int vertexID,IILVertexDefinition vertex) {
230            C4bV3f curVert = m_vertices[vertexID];
231            if (vertex.StoresColor) {
232                curVert.Color = vertex.Color;
233            }
234            curVert.Position = vertex.Position;
235            m_vertices[vertexID] = curVert;
236        }
237
238        /// <summary>
239        /// called, if a property for markers have changed
240        /// </summary>
241        /// <param name="sender">this graph instance</param>
242        /// <param name="e">(no args)</param>
243        /// <remarks>derived classes should override this function in order to
244        /// (re-)configure vertex ressources etc.</remarks>
245        protected virtual void m_marker_Changed(object sender, EventArgs e) {
246            m_isReady = false;
247        }
248        private void updateClipping() {
249            ILPoint3Df max = ILPoint3Df.MinValue;
250            ILPoint3Df min = ILPoint3Df.MaxValue;
251            for (int i = 0; i < m_vertexCount; i++) {
252                C4bV3f vert = m_vertices[i];
253                max = ILPoint3Df.Max(vert.Position,max);
254                min = ILPoint3Df.Min(vert.Position,min);
255            }
256            m_localClipping.Set ( min, max );
257        }
258        /// <summary>
259        /// called, if a property for lines have changed
260        /// </summary>
261        /// <param name="sender">this graph instance</param>
262        /// <param name="e">(no args)</param>
263        /// <remarks>derived classes should override this function in order to
264        /// (re-)configure vertex ressources etc.</remarks>
265        protected virtual void m_properties_Changed(object sender, EventArgs e) {
266            m_isReady = false;
267        }
268        public override bool Is3DGraph() {
269            return false;
270        }
271        #endregion
272
273        #region IILPanelConfigurator
274        public void ConfigurePanel(ILPanel panel) {
275            panel.InteractiveMode = InteractiveModes.ZoomRectangle;
276            panel.AspectRatio = AspectRatioMode.StretchToFill;
277            panel.PlotBoxScreenSizeMode = PlotBoxScreenSizeMode.Optimal;
278            panel.ClipViewData = true;
279            panel.DefaultView.Set(0f, 0f, panel.DefaultView.Distance);
280        }
281        #endregion
282
283    }
284}
Note: See TracBrowser for help on using the repository browser.