source: proiecte/ParallelANN/VisualBCM/GraphForm.cs @ 171

Last change on this file since 171 was 171, checked in by (none), 14 years ago
File size: 4.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using System.Diagnostics;
10
11using BCMToolbox;
12
13namespace VisualBCM
14{
15    public partial class GraphForm : Form, INetworkMonitor
16    {
17       
18        Func<INetwork, double[]> __norm = null;
19        Color[] __wheel = null;
20
21        const int GRAPH_DEPTH = 50;
22
23        List<double[]> __gatheredNorms = new List<double[]>(GRAPH_DEPTH + 10);
24        double[] __gatheredTotals = null;
25        int __normWidth = 0;
26        int __normTotalCount = 0;
27
28
29       
30
31        public GraphForm(TraceableNetwork net, Func<INetwork,double[]> norms, string description)
32        {
33            __wheel = GUIUtilities.GetColorWheel();
34            InitializeComponent();
35
36         
37            __norm = norms;
38
39            if(net != null)
40                net.RegisterMonitor(this);
41           
42
43            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
44
45            this.TopMost = true;
46
47            if (net != null)
48                this.Text = String.Format("VisualBCM Graphing tool [{0} - {1}]", net.Identification, description);
49            else
50                this.Text = String.Format("VisualBCM Graphing tool [{0}]", description);
51        }
52
53        public void RefreshGraph()
54        {
55            if (__gatheredNorms.Count == 0)
56                return;
57
58            Bitmap bmp = new Bitmap(GRAPH_DEPTH, 100, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
59
60            lock (__gatheredNorms)
61            {
62                int start = 0;
63                if (__gatheredNorms.Count > GRAPH_DEPTH)
64                    start = __gatheredNorms.Count - GRAPH_DEPTH;
65
66                for (int j = 0; j < __normWidth; j++)
67                {
68                    double normAve = __gatheredTotals[j] / __normTotalCount;
69                    for (int i = start; i < __gatheredNorms.Count; i++)
70                    {
71                        double val = __gatheredNorms[i][j];
72                        val /= normAve;
73                        val = val * 50;
74                        val = 100 - val;
75
76                        if (val < 0)
77                            val = 0;
78                        if (val >= 100)
79                            val = 99;
80
81                        bmp.SetPixel(i - start, (int)val,
82                            __wheel[j % __wheel.Length]);
83                    }
84                }
85            }
86
87            this.pictureBox1.Image = bmp;
88            this.label1.Text = String.Format("ticks: {0}", __normTotalCount);           
89        }
90
91       
92
93        #region INetworkMonitor Members
94
95        public Func<INetwork, double[]> KernelFunction
96        {
97            get
98            {
99                return __norm;
100            }
101        }
102
103        public void RegisterNorm(double[] newNorm)
104        {
105            lock (__gatheredNorms)
106            {
107                if (__gatheredNorms.Count == 0)
108                {
109                    __normWidth = newNorm.Length;
110                    __gatheredTotals = new double[__normWidth];
111                }
112                else if (newNorm.Length != __normWidth)
113                    return;
114
115                __gatheredNorms.Add(newNorm);
116                while (__gatheredNorms.Count > GRAPH_DEPTH)
117                    __gatheredNorms.RemoveAt(0);
118
119                for (int i = 0; i < __normWidth; i++)
120                    __gatheredTotals[i] += newNorm[i];
121
122                __normTotalCount++;
123            }
124        }
125
126        #endregion
127
128        #region Plotting function graphs
129        public static GraphForm PlotFunction(Func<double, double> func, double intervalMin, double intervalMax)
130        {
131            GraphForm toRet = new GraphForm(null, null, "Function Graph");
132            double maxPlot = double.MinValue;
133            double minPlot = double.MaxValue;
134            for (int i = 0; i < GRAPH_DEPTH; i++)
135            {
136                double val = i * (intervalMax - intervalMin) / GRAPH_DEPTH + intervalMin;
137                double y = func(val);
138                if (y < minPlot)
139                    minPlot = y;
140                if (y > maxPlot)
141                    maxPlot = y;
142                toRet.RegisterNorm(new double[] { y });
143            }
144            toRet.RefreshGraph();
145            toRet.label1.Text = String.Format("domain: [{0}, {1}] | codomain: [{2}, {3}]",
146                intervalMin.ToString("#0.00"), intervalMax.ToString("#0.00"),
147                minPlot.ToString("#0.00"), maxPlot.ToString("#0.00"));
148
149            toRet.Show();
150            toRet.BringToFront();
151            return toRet;
152        }
153        #endregion
154    }
155}
Note: See TracBrowser for help on using the repository browser.