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

Last change on this file since 171 was 171, checked in by (none), 14 years ago
File size: 9.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9
10using BCMToolbox;
11
12namespace VisualBCM
13{
14    public partial class NetVisualiser : UserControl
15    {
16        const int MARKER_LENGTH = 5;
17        const double LEGEND_FACTOR = 0.2;
18
19        BCMToolbox.NetworkState __lastNetworkState = null;
20
21        BCMToolbox.TraceableNetwork __network = null;
22        Brush __background = null;
23        bool __autoRefresh = false;
24        Bitmap __backgroundBitmap = null;
25        Bitmap __outputBitmap1 = null;
26        Bitmap __outputBitmap2 = null;
27        Color[] __wtPalette = new Color[] { Color.DarkBlue, Color.Blue, Color.Magenta, Color.Red, Color.White, Color.Pink};
28        Color[] __outPalette = new Color[] { Color.Black, Color.DarkGreen, Color.Green, Color.GreenYellow, Color.Yellow};
29
30        //Color[] __palette = new Color[] { Color.Black, Color.White };
31
32
33        double __scaleMin = 0.0;
34        double __scaleMax = 0.0;
35        bool __autoScale = true;
36
37        public NetVisualiser()
38        {
39            InitializeComponent();
40
41            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
42            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
43
44
45            __background = new SolidBrush(Color.Black);
46        }
47
48        #region Binding to Networks
49        public TraceableNetwork BoundNetwork
50        {
51            get
52            {
53                return __network;
54            }
55            set
56            {
57                if (__network != null)
58                    Unbind();
59                if(value != null)
60                    BindToNetwork(value);
61                __network_WeightsChanged(this, EventArgs.Empty);
62            }
63        }
64
65        void Unbind()
66        {
67            __network.WeightsChanged -= new EventHandler(__network_WeightsChanged);
68            __network = null;
69        }
70
71        void BindToNetwork(BCMToolbox.TraceableNetwork network)
72        {
73            __network = network;
74            __network.WeightsChanged += new EventHandler(__network_WeightsChanged);
75            __backgroundBitmap = new Bitmap(__network.Count, __network.Count, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
76            __outputBitmap1 = new Bitmap(__network.Count, MARKER_LENGTH, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
77            __outputBitmap2 = new Bitmap(MARKER_LENGTH, __network.Count, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
78        }
79
80        public bool AutoRefresh
81        {
82            get
83            {
84                return __autoRefresh;
85            }
86            set
87            {
88                __autoRefresh = value;
89            }
90        }
91
92        void __network_WeightsChanged(object sender, EventArgs e)
93        {
94            if (!__autoRefresh)
95                return;
96
97            if (this.InvokeRequired)
98                this.BeginInvoke(
99                    new Action(this.Invalidate));
100            else
101                this.Invalidate();
102        }
103
104        public void RefreshNetwork()
105        {
106            this.Invalidate();
107        }
108
109        #endregion
110
111        #region Painting
112
113        public void SetAutoScale()
114        {
115            __autoScale = true;
116            Invalidate();
117        }
118
119        public void SetManualScale(double minValue, double MaxValue)
120        {
121            __autoScale = false;
122            __scaleMin = minValue;
123            __scaleMax = MaxValue;
124            Invalidate();
125        }
126
127        public BCMToolbox.NetworkState LastNetworkState
128        {
129            get
130            {
131                return __lastNetworkState;
132            }
133        }
134
135        protected override void OnPaint(PaintEventArgs e)
136        {
137
138            // System.Diagnostics.Debug.WriteLine("Painting...");
139            if (__network == null)
140            {
141                e.Graphics.FillRectangle(
142                    __background, 0, 0, Width, Height);
143                return;
144            }
145
146            BCMToolbox.NetworkState capturedState = __network.BuildNetworkState();
147            __lastNetworkState = capturedState;
148
149            // draw output graphs
150            {
151                double min = double.MaxValue;
152                double max = 0.0;
153                if (__autoScale)
154                {
155                    for (int i = 0; i < capturedState.Count; i++)
156                        {
157                            if (capturedState[i] < min)
158                                min = capturedState[i];
159
160                            if (capturedState[i] > max)
161                                max = capturedState[i];
162                        }
163                }
164                else
165                {
166                    min = __scaleMin;
167                    max = __scaleMax;
168                }
169
170
171
172                for (int i = 0; i < capturedState.Count; i++)
173                {
174                    double val = (capturedState[i] - min) / (max - min);
175                    if (val < 0)
176                        val = 0;
177                    if (val > 1.0)
178                        val = 1.0;
179
180                    Color c = GUIUtilities.GetPaletteColor(val, __outPalette);
181
182                    for (int j = 0; j < MARKER_LENGTH; j++)
183                    {
184                        __outputBitmap1.SetPixel(i, j, c);
185                        __outputBitmap2.SetPixel(j, i, c);
186                    }
187                }
188               
189            }
190
191            // draw weight graphs
192            {
193                double min = double.MaxValue;
194                double max = 0.0;
195                if (__autoScale)
196                {
197                    for (int i = 0; i < capturedState.Count; i++)
198                        for (int j = 0; j < capturedState.Count; j++)
199                        {
200                            if (capturedState[i, j] < min)
201                                min = capturedState[i, j];
202
203                            if (capturedState[i, j] > max)
204                                max = capturedState[i, j];
205                        }
206                }
207                else
208                {
209                    min = __scaleMin;
210                    max = __scaleMax;
211                }
212
213
214
215                for (int i = 0; i < capturedState.Count; i++)
216                    for (int j = 0; j < capturedState.Count; j++)
217                    {
218                        double val = (capturedState[i, j] - min) / (max - min);
219                        if (val < 0)
220                            val = 0;
221                        if (val > 1.0)
222                            val = 1.0;
223
224                        __backgroundBitmap.SetPixel(i, j,
225                            GUIUtilities.GetPaletteColor(val, __wtPalette));
226                    }
227            }
228
229
230            // paint on the screen
231            e.Graphics.DrawImage(__backgroundBitmap, (int)(Width*LEGEND_FACTOR), (int)(Height * LEGEND_FACTOR), (int)(Width * (1-LEGEND_FACTOR)), (int)(Height * (1-LEGEND_FACTOR)));
232            e.Graphics.DrawImage(__outputBitmap1, (int)(Width*LEGEND_FACTOR), 0, (int)(Width * (1 - LEGEND_FACTOR)), (int)(Height * LEGEND_FACTOR));
233            e.Graphics.DrawImage(__outputBitmap2, 0, (int)(Height * LEGEND_FACTOR), (int)(Width * LEGEND_FACTOR), (int)(Height * (1 - LEGEND_FACTOR)));
234
235        }
236        #endregion
237
238    }
239
240
241    public class GUIUtilities
242    {
243        // returns the color corresponding to a value between 0 and 1 vs. a specified palette
244        public static Color GetPaletteColor(double val, Color[] Palette)
245        {
246            if (double.IsNaN(val))
247                return Color.Black;
248
249            int len = Palette.Length - 1;
250            Color PaletteLow = Palette[(int)Math.Floor(val * len)];
251            Color PaletteHigh = Palette[(int)Math.Ceiling(val * len)];
252            if (PaletteLow == PaletteHigh)
253                return PaletteHigh;
254            val -= Math.Floor(val * Palette.Length) * (1.0 / Palette.Length);
255            return Color.FromArgb(
256                (int)(PaletteLow.R + val * (PaletteHigh.R - PaletteLow.R)),
257                (int)(PaletteLow.G + val * (PaletteHigh.G - PaletteLow.G)),
258                (int)(PaletteLow.B + val * (PaletteHigh.B - PaletteLow.B)));
259        }
260
261        public static Color[] GetColorWheel()
262        {
263            Color[] toRet = new Color[23];
264
265            toRet[0] = Color.FromArgb(204, 0, 0);
266            toRet[1] = Color.FromArgb(102, 0, 0);
267            toRet[2] = Color.FromArgb(204, 0, 204);
268            toRet[3] = Color.FromArgb(153, 0, 153);
269            toRet[4] = Color.FromArgb(102, 0, 204);
270            toRet[5] = Color.FromArgb(51, 0, 102);
271            toRet[6] = Color.FromArgb(51, 0, 153);
272            toRet[7] = Color.FromArgb(0, 51, 153);
273            toRet[8] = Color.FromArgb(0, 0, 51);
274            toRet[9] = Color.FromArgb(102, 204, 205);
275            toRet[10] = Color.FromArgb(0, 255, 153);
276            toRet[11] = Color.FromArgb(0, 153, 51);
277            toRet[12] = Color.FromArgb(0, 102, 51);
278            toRet[13] = Color.FromArgb(0, 153, 0);
279            toRet[14] = Color.FromArgb(102, 204, 0);
280            toRet[15] = Color.FromArgb(204, 204, 0);
281            toRet[16] = Color.FromArgb(153, 153, 0);
282            toRet[17] = Color.FromArgb(255, 153, 0);
283            toRet[18] = Color.FromArgb(204, 102, 0);
284            toRet[19] = Color.FromArgb(255, 102, 0);
285            toRet[20] = Color.FromArgb(204, 51, 0);
286            toRet[21] = Color.FromArgb(153, 0, 0);
287            toRet[22] = Color.FromArgb(102, 0, 0);
288            return toRet;
289        }
290    }
291}
Note: See TracBrowser for help on using the repository browser.