MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
Geometry.cs
1using MyCaffe.basecode;
2using System;
3using System.Collections.Generic;
4using System.Drawing;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using System.Windows.Forms;
9
10namespace MyCaffe.gym
11{
15 abstract class GeomObj
16 {
20 protected PointF m_location = new PointF(0, 0);
24 protected List<PointF> m_rgPoints = new List<PointF>();
28 protected Color m_clrFill = Color.LightGray;
32 protected Color m_clrBorder = Color.Black;
36 protected float m_fRotation = 0;
40 protected bool m_bClrDirty = false;
41 System.Drawing.Drawing2D.GraphicsState m_gstate = null;
42
52 public GeomObj(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder)
53 {
54 m_rgPoints.Add(new PointF(fL, fB));
55 m_rgPoints.Add(new PointF(fL, fT));
56 m_rgPoints.Add(new PointF(fR, fT));
57 m_rgPoints.Add(new PointF(fR, fB));
58 m_rgPoints.Add(new PointF(fL, fB));
59 m_clrFill = clrFill;
60 m_clrBorder = clrBorder;
61 }
62
67 protected void prerender(Graphics g)
68 {
69 m_gstate = g.Save();
70 g.TranslateTransform(m_location.X, m_location.Y);
71 g.RotateTransform(m_fRotation);
72 }
73
78 protected void postrender(Graphics g)
79 {
80 if (m_gstate != null)
81 {
82 g.Restore(m_gstate);
83 m_gstate = null;
84 }
85 }
86
92 public void SetColors(Color clrFill, Color clrBorder)
93 {
94 m_clrFill = clrFill;
95 m_clrBorder = clrBorder;
96 m_bClrDirty = true;
97 }
98
104 public float Width(PointF[] rg)
105 {
106 if (rg == null)
107 rg = m_rgPoints.ToArray();
108
109 return rg[2].X - rg[0].X;
110 }
111
117 public float Height(PointF[] rg)
118 {
119 if (rg == null)
120 rg = m_rgPoints.ToArray();
121
122 return rg[0].Y - rg[1].Y;
123 }
124
130 public PointF LeftBottom(PointF[] rg = null)
131 {
132 if (rg == null)
133 rg = m_rgPoints.ToArray();
134
135 return rg[0];
136 }
137
143 public PointF LeftTop(PointF[] rg = null)
144 {
145 if (rg == null)
146 rg = m_rgPoints.ToArray();
147
148 return rg[1];
149 }
150
156 public PointF RightTop(PointF[] rg = null)
157 {
158 if (rg == null)
159 rg = m_rgPoints.ToArray();
160
161 return rg[2];
162 }
163
169 public PointF RightBottom(PointF[] rg = null)
170 {
171 if (rg == null)
172 rg = m_rgPoints.ToArray();
173
174 return rg[3];
175 }
176
180 public PointF Location
181 {
182 get { return m_location; }
183 }
184
188 public float Rotation
189 {
190 get { return m_fRotation; }
191 }
192
198 public virtual void SetLocation(float fX, float fY)
199 {
200 m_location = new PointF(fX, fY);
201 }
202
207 public virtual void SetRotation(float fR)
208 {
209 m_fRotation = fR;
210 }
211
215 public List<PointF> Polygon
216 {
217 get { return m_rgPoints; }
218 }
219
223 public Color FillColor
224 {
225 get { return m_clrFill; }
226 }
227
231 public Color BorderColor
232 {
233 get { return m_clrBorder; }
234 }
235
240 public abstract void Render(Graphics g);
241 }
242
247 {
257 public GeomLine(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder)
258 : base(fL, fR, fT, fB, clrFill, clrBorder)
259 {
260 }
261
266 public override void Render(Graphics g)
267 {
268 prerender(g);
269 PointF[] rg = m_rgPoints.ToArray();
270 Pen p = new Pen(m_clrBorder, 1.0f);
271 g.DrawLine(p, LeftBottom(rg), RightBottom(rg));
272 p.Dispose();
273 postrender(g);
274 }
275 }
276
281 {
291 public GeomEllipse(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder)
292 : base(fL, fR, fT, fB, clrFill, clrBorder)
293 {
294 }
295
300 public override void Render(Graphics g)
301 {
302 prerender(g);
303 PointF[] rg = m_rgPoints.ToArray();
304 RectangleF rc = new RectangleF(LeftTop(rg).X, LeftTop(rg).Y, Width(rg), Height(rg));
305 Brush br = new SolidBrush(m_clrFill);
306 Pen p = new Pen(m_clrBorder, 1.0f);
307 g.FillEllipse(br, rc);
308 g.DrawEllipse(p, rc);
309 p.Dispose();
310 br.Dispose();
311 postrender(g);
312 }
313 }
314
319 {
320 ColorMapper m_clrMap = null;
321
332 public GeomRectangle(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder, ColorMapper clrMap = null)
333 : base(fL, fR, fT, fB, clrFill, clrBorder)
334 {
335 m_clrMap = clrMap;
336 }
337
342 public override void Render(Graphics g)
343 {
344 prerender(g);
345 PointF[] rg = m_rgPoints.ToArray();
346 RectangleF rc = new RectangleF(LeftTop(rg).X, LeftTop(rg).Y, Width(rg), Height(rg));
347 Brush br = new SolidBrush(m_clrFill);
348 Pen p = new Pen(m_clrBorder, 1.0f);
349 g.FillRectangle(br, rc);
350
351 if (m_clrMap != null)
352 {
353 float fX = 0;
354 float fWid = rc.Width / 20;
355
356 for (int i = 0; i < 20; i++)
357 {
358 RectangleF rc2 = new RectangleF(fX, rc.Y, fWid, rc.Height);
359 Color clr = m_clrMap.GetColor(fX);
360 Brush br1 = new SolidBrush(clr);
361 g.FillRectangle(br1, rc2);
362 br1.Dispose();
363 fX += rc2.Width;
364 }
365 }
366
367 g.DrawRectangle(p, rc.X, rc.Y, rc.Width, rc.Height);
368 p.Dispose();
369 br.Dispose();
370 postrender(g);
371 }
372 }
373
378 {
388 public GeomPolygon(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder)
389 : base(fL, fR, fT, fB, clrFill, clrBorder)
390 {
391 }
392
397 public override void Render(Graphics g)
398 {
399 prerender(g);
400
401 if (m_rgPoints.Count >= 2)
402 {
403 PointF[] rg = m_rgPoints.ToArray();
404 Brush br = new SolidBrush(m_clrFill);
405 Pen p = new Pen(m_clrBorder, 1.0f);
406 g.FillPolygon(br, rg);
407 g.DrawPolygon(p, rg);
408 p.Dispose();
409 br.Dispose();
410 }
411
412 postrender(g);
413 }
414 }
415
420 {
421 int m_nMaxPlots = int.MaxValue;
422
433 public GeomPolyLine(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder, int nMaxPlots = int.MaxValue)
434 : base(fL, fR, fT, fB, clrFill, clrBorder)
435 {
436 m_nMaxPlots = nMaxPlots;
437 }
438
439 private void clipAndShift()
440 {
441 if (m_rgPoints.Count > m_nMaxPlots)
442 {
443 float fShift = m_rgPoints[m_rgPoints.Count - 1].X - m_rgPoints[m_rgPoints.Count - 2].X;
444 int nDiff = m_rgPoints.Count - m_nMaxPlots;
445 m_rgPoints.RemoveRange(0, nDiff);
446 m_location.X -= (fShift * nDiff);
447 }
448 }
449
454 public override void Render(Graphics g)
455 {
456 prerender(g);
457
458 if (m_rgPoints.Count >= 2)
459 {
460 clipAndShift();
461
462 PointF[] rg = m_rgPoints.ToArray();
463 Pen p = new Pen(m_clrBorder, 1.0f);
464 g.DrawLines(p, rg);
465 p.Dispose();
466 }
467
468 postrender(g);
469 }
470 }
471
476 {
477 List<GeomObj> m_rgObj = new List<GeomObj>();
478
482 public GeomView()
483 {
484 }
485
490 public void AddObject(GeomObj obj)
491 {
492 m_rgObj.Add(obj);
493 }
494
503 public void RenderText(Graphics g, string str, float fX, float fY, Brush br = null)
504 {
505 if (br == null)
506 br = Brushes.Black;
507
508 Font font = new Font("Century Gothic", 9.0f);
509 g.DrawString(str, font, br, new PointF(fX, fY));
510 font.Dispose();
511 }
512
519 public void RenderSteps(Graphics g, int nSteps, int nMax)
520 {
521 RectangleF rc = g.VisibleClipBounds;
522 Font fontStep = new Font("Century Gothic", 7.0f);
523 string strStep = nSteps.ToString();
524 string strMax = nMax.ToString();
525 string strSteps = "Steps";
526 SizeF szStep = g.MeasureString(strStep, fontStep);
527 SizeF szSteps = g.MeasureString(strSteps, fontStep);
528 SizeF szMax = g.MeasureString(strMax, fontStep);
529
530 float fX = 10;
531 float fY = rc.Bottom - (szSteps.Height * 2);
532
533 g.DrawString(strSteps, fontStep, Brushes.Black, new PointF(fX, fY));
534 fX += szSteps.Width;
535
536 float fMaxWid = Math.Max(nMax, nSteps);
537 float fStepWid = nSteps;
538
539 if (fMaxWid + fX + 20 > rc.Width)
540 {
541 fMaxWid = rc.Width - (fX + 20);
542 fStepWid = ((float)nSteps / (float)Math.Max(nMax, nSteps)) * fMaxWid;
543 }
544
545 Rectangle rcMax = new Rectangle((int)fX, (int)fY, (int)fMaxWid, (int)(szStep.Height));
546 Rectangle rcStep = new Rectangle((int)fX, (int)fY, (int)fStepWid, (int)(szStep.Height));
547 Brush br = (nSteps < nMax) ? Brushes.Orange : Brushes.Lime;
548 Pen pen = (nSteps < nMax) ? Pens.Brown : Pens.DarkGreen;
549
550 g.FillRectangle(br, rcStep);
551 g.DrawRectangle(pen, rcMax);
552
553 fX = rcStep.Right - szStep.Width / 2;
554 fY = rcStep.Bottom;
555
556 g.DrawString(strStep, fontStep, Brushes.Brown, new PointF(fX, fY));
557
558 fX = rcMax.Right - szMax.Width / 2;
559 fY = rcMax.Bottom;
560
561 g.DrawString(strMax, fontStep, Brushes.DarkGreen, new PointF(fX, fY));
562
563 fontStep.Dispose();
564 }
565
570 public void Render(Graphics g)
571 {
572 System.Drawing.Drawing2D.GraphicsState gstate = g.Save();
573
574 g.TranslateTransform(0, -g.VisibleClipBounds.Height);
575 g.ScaleTransform(1, -1, System.Drawing.Drawing2D.MatrixOrder.Append);
576
577 g.DrawRectangle(Pens.SteelBlue, 1, 1, 2, 2);
578 g.DrawLine(Pens.SteelBlue, 1, 3, 1, 4);
579 g.DrawLine(Pens.SteelBlue, 3, 3, 4, 4);
580 g.DrawLine(Pens.SteelBlue, 3, 1, 4, 1);
581
582 foreach (GeomObj obj in m_rgObj)
583 {
584 obj.Render(g);
585 }
586
587 g.Restore(gstate);
588 }
589 }
590}
The ColorMapper maps a value within a number range, to a Color within a color scheme.
Definition: ColorMapper.cs:14
Color GetColor(double dfVal)
Find the color using a binary search algorithm.
Definition: ColorMapper.cs:350
The GeomEllipse object is used to render an ellipse.
Definition: Geometry.cs:281
override void Render(Graphics g)
Renders the ellipse on the Graphics specified.
Definition: Geometry.cs:300
GeomEllipse(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder)
The constructor.
Definition: Geometry.cs:291
The GeomLine object is used to render a line.
Definition: Geometry.cs:247
override void Render(Graphics g)
Renders the line on the Graphics specified.
Definition: Geometry.cs:266
GeomLine(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder)
The constructor.
Definition: Geometry.cs:257
The GoomObj is the base class for all other gometric objects used to draw Gym objects.
Definition: Geometry.cs:16
virtual void SetLocation(float fX, float fY)
Sets the object location.
Definition: Geometry.cs:198
virtual void SetRotation(float fR)
Sets the rotation of the object.
Definition: Geometry.cs:207
Color m_clrBorder
Specifies the border color of the object.
Definition: Geometry.cs:32
float Height(PointF[] rg)
Returns the height of the points.
Definition: Geometry.cs:117
GeomObj(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder)
The constructor.
Definition: Geometry.cs:52
PointF LeftTop(PointF[] rg=null)
Returns the left top.
Definition: Geometry.cs:143
float m_fRotation
Specifies the rotation of the object.
Definition: Geometry.cs:36
PointF RightBottom(PointF[] rg=null)
Returns the right bottom.
Definition: Geometry.cs:169
PointF m_location
Specifies the location of the object.
Definition: Geometry.cs:20
PointF RightTop(PointF[] rg=null)
Returns the right top.
Definition: Geometry.cs:156
List< PointF > Polygon
Returns the bounds as a Polygon.
Definition: Geometry.cs:216
float Width(PointF[] rg)
Returns the width of the points.
Definition: Geometry.cs:104
Color BorderColor
Returns the border color.
Definition: Geometry.cs:232
List< PointF > m_rgPoints
Specifies the points of the object.
Definition: Geometry.cs:24
float Rotation
Returns the rotation of the object.
Definition: Geometry.cs:189
void prerender(Graphics g)
Called just before rendering the object.
Definition: Geometry.cs:67
PointF Location
Returns the location of the object.
Definition: Geometry.cs:181
abstract void Render(Graphics g)
Override used to render the object.
Color m_clrFill
Specifies the fill color of the object.
Definition: Geometry.cs:28
PointF LeftBottom(PointF[] rg=null)
Returns the left bottom.
Definition: Geometry.cs:130
bool m_bClrDirty
Specifies that the colors have changed.
Definition: Geometry.cs:40
void SetColors(Color clrFill, Color clrBorder)
Set the colors of the object.
Definition: Geometry.cs:92
void postrender(Graphics g)
Called just after rendering the object.
Definition: Geometry.cs:78
Color FillColor
Returns the fill color.
Definition: Geometry.cs:224
The GeomPolyLine object is used to render an spline.
Definition: Geometry.cs:420
GeomPolyLine(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder, int nMaxPlots=int.MaxValue)
The constructor.
Definition: Geometry.cs:433
override void Render(Graphics g)
Renders the rectangle on the Graphics specified.
Definition: Geometry.cs:454
The GeomPolygon object is used to render an polygon.
Definition: Geometry.cs:378
override void Render(Graphics g)
Renders the rectangle on the Graphics specified.
Definition: Geometry.cs:397
GeomPolygon(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder)
The constructor.
Definition: Geometry.cs:388
The GeomEllipse object is used to render an rectangle.
Definition: Geometry.cs:319
override void Render(Graphics g)
Renders the rectangle on the Graphics specified.
Definition: Geometry.cs:342
GeomRectangle(float fL, float fR, float fT, float fB, Color clrFill, Color clrBorder, ColorMapper clrMap=null)
The constructor.
Definition: Geometry.cs:332
The GeomView manages and renders a collection of Geometric objects.
Definition: Geometry.cs:476
void Render(Graphics g)
Renders the view.
Definition: Geometry.cs:570
void AddObject(GeomObj obj)
Add a new geometric object to the view.
Definition: Geometry.cs:490
void RenderText(Graphics g, string str, float fX, float fY, Brush br=null)
Render text at a location.
Definition: Geometry.cs:503
void RenderSteps(Graphics g, int nSteps, int nMax)
Renders the Gym step information.
Definition: Geometry.cs:519
GeomView()
The constructor.
Definition: Geometry.cs:482
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.gym namespace contains all classes related to the Gym's supported by MyCaffe.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12