123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- using System;
- using HalconDotNet;
- using System.Xml.Serialization;
- using System.Windows.Forms;
- namespace ViewWindow.Model
- {
- /// <summary>
- /// This class demonstrates one of the possible implementations for a
- /// (simple) rectangularly shaped ROI. To create this rectangle we use
- /// a center point (midR, midC), an orientation 'phi' and the half
- /// edge lengths 'length1' and 'length2', similar to the HALCON
- /// operator gen_rectangle2().
- /// The class ROIRectangle2 inherits from the base class ROI and
- /// implements (besides other auxiliary methods) all virtual methods
- /// defined in ROI.cs.
- /// </summary>
- [Serializable]
- public class ROIRectangle2 : ROI
- {
- [XmlElement(ElementName = "Row")]
- public double Row
- {
- get { return this.midR; }
- set { this.midR = value; }
- }
- [XmlElement(ElementName = "Column")]
- public double Column
- {
- get { return this.midC; }
- set { this.midC = value; }
- }
- [XmlElement(ElementName = "Phi")]
- public double Phi
- {
- get { return this.phi; }
- set { this.phi = value; }
- }
- [XmlElement(ElementName = "Length1")]
- public double Lenth1
- {
- get { return this.length1; }
- set { this.length1 = value; }
- }
- [XmlElement(ElementName = "Length2")]
- public double Lenth2
- {
- get { return this.length2; }
- set { this.length2 = value; }
- }
- /// <summary>Half length of the rectangle side, perpendicular to phi</summary>
- private double length1;
- /// <summary>Half length of the rectangle side, in direction of phi</summary>
- private double length2;
- /// <summary>Row coordinate of midpoint of the rectangle</summary>
- private double midR;
- /// <summary>Column coordinate of midpoint of the rectangle</summary>
- private double midC;
- /// <summary>Orientation of rectangle defined in radians.</summary>
- private double phi;
- //auxiliary variables
- HTuple rowsInit;
- HTuple colsInit;
- public HTuple rows;
- public HTuple cols;
- HHomMat2D hom2D, tmp;
- /// <summary>Constructor</summary>
- public ROIRectangle2()
- {
- NumHandles = 10; // 4 corners + 1 midpoint + 1 rotationpoint
- activeHandleIdx = 4;
- }
- public ROIRectangle2(double row, double col, double phi, double length1, double length2)
- {
- createRectangle2(row, col, phi, length1, length2);
- }
- public override void createRectangle2(double row, double col, double phi, double length1, double length2)
- {
- base.createRectangle2(row, col, phi, length1, length2);
- this.midR = row;
- this.midC = col;
- this.length1 = length1;
- this.length2 = length2;
- this.phi = phi;
- rowsInit = new HTuple(new double[] {-1.0, -1.0, 1.0,
- 1.0, 0.0, 0.0 ,0,-1,0,1});
- colsInit = new HTuple(new double[] {-1.0, 1.0, 1.0,
- -1.0, 0.0, 1.8 ,-1,0,1,0});
- //order ul , ur, lr, ll, mp, arrowMidpoint
- hom2D = new HHomMat2D();
- tmp = new HHomMat2D();
- updateHandlePos();
- }
- public override void createInitRectangle2(double imageHeight)
- {
- double size = 0;
- if (imageHeight < 300) size = 10;
- else if (imageHeight < 600) size = 20;
- else if (imageHeight < 900) size = 30;
- else if (imageHeight < 1200) size = 40;
- else if (imageHeight < 1500) size = 50;
- else if (imageHeight < 1800) size = 60;
- else if (imageHeight < 2100) size = 70;
- else if (imageHeight < 2400) size = 80;
- else if (imageHeight < 2700) size = 90;
- else if (imageHeight < 3000) size = 100;
- else if (imageHeight < 3300) size = 110;
- else if (imageHeight < 3600) size = 120;
- else if (imageHeight < 3900) size = 130;
- else if (imageHeight < 4200) size = 140;
- else if (imageHeight < 4500) size = 150;
- else if (imageHeight < 4800) size = 160;
- else if (imageHeight < 5100) size = 170;
- else size = 180;
- double length1 = size * 3;
- double length2 = size * 4;
- base.createRectangle2(midR , midC , phi, length1, length2);
- this.midR = midR ;
- this.midC = midC ;
- this.length1 = length1;
- this.length2 = length2;
- this.phi = phi;
- rowsInit = new HTuple(new double[] {-1.0, -1.0, 1.0,
- 1.0, 0.0, 0.0 ,0,-1,0,1});
- colsInit = new HTuple(new double[] {-1.0, 1.0, 1.0,
- -1.0, 0.0, 1.8 ,-1,0,1,0});
- //order ul , ur, lr, ll, mp, arrowMidpoint
- hom2D = new HHomMat2D();
- tmp = new HHomMat2D();
- updateHandlePos();
- }
- /// <summary>Creates a new ROI instance at the mouse position</summary>
- /// <param name="midX">
- /// x (=column) coordinate for interactive ROI
- /// </param>
- /// <param name="midY">
- /// y (=row) coordinate for interactive ROI
- /// </param>
- public override void createROI(double midX, double midY)
- {
- midR = midY;
- midC = midX;
- length1 = 100;
- length2 = 50;
- phi = 0.0;
- rowsInit = new HTuple(new double[] {-1.0, -1.0, 1.0,
- 1.0, 0.0, 0.0 });
- colsInit = new HTuple(new double[] {-1.0, 1.0, 1.0,
- -1.0, 0.0, 1.8 });
- //order ul , ur, lr, ll, mp, arrowMidpoint
- hom2D = new HHomMat2D();
- tmp = new HHomMat2D();
- updateHandlePos();
- }
- /// <summary>Paints the ROI into the supplied window</summary>
- /// <param name="window">HALCON window</param>
- public override void draw(HalconDotNet.HWindow window, int imageWidth, int imageHeight)
- {
- double littleRecSize = 0;
- if (imageHeight < 300) littleRecSize = 1;
- else if (imageHeight < 600) littleRecSize = 2;
- else if (imageHeight < 900) littleRecSize = 3;
- else if (imageHeight < 1200) littleRecSize = 4;
- else if (imageHeight < 1500) littleRecSize = 5;
- else if (imageHeight < 1800) littleRecSize = 6;
- else if (imageHeight < 2100) littleRecSize = 7;
- else if (imageHeight < 2400) littleRecSize = 8;
- else if (imageHeight < 2700) littleRecSize = 9;
- else if (imageHeight < 3000) littleRecSize = 10;
- else if (imageHeight < 3300) littleRecSize = 11;
- else if (imageHeight < 3600) littleRecSize = 12;
- else if (imageHeight < 3900) littleRecSize = 13;
- else if (imageHeight < 4200) littleRecSize = 14;
- else if (imageHeight < 4500) littleRecSize = 15;
- else if (imageHeight < 4800) littleRecSize = 16;
- else if (imageHeight < 5100) littleRecSize = 17;
- else littleRecSize = 18;
- if (littleRecSize % 2 != 0)
- littleRecSize++;
- HOperatorSet.SetDraw(window, "margin");
-
- window.DispRectangle2(midR, midC, -phi, length1, length2);
- HOperatorSet.SetDraw(window, "fill");
- for (int i = 0; i < NumHandles; i++)
- {
- window.DispRectangle2(rows[i].D, cols[i].D, -phi, littleRecSize, littleRecSize);
- Application.DoEvents();
- }
- window.DispArrow(midR, midC, midR + (Math.Sin(phi) * length1 * 1.8),
- midC + (Math.Cos(phi) * length1 * 1.8), littleRecSize);
- }
- /// <summary>
- /// Returns the distance of the ROI handle being
- /// closest to the image point(x,y)
- /// </summary>
- /// <param name="x">x (=column) coordinate</param>
- /// <param name="y">y (=row) coordinate</param>
- /// <returns>
- /// Distance of the closest ROI handle.
- /// </returns>
- public override double distToClosestHandle(double x, double y)
- {
- double max = 10000;
- double [] val = new double[NumHandles];
- for (int i=0; i < NumHandles; i++)
- val[i] = HMisc.DistancePp(y, x, rows[i].D, cols[i].D);
- for (int i=0; i < NumHandles; i++)
- {
- if (val[i] < max)
- {
- max = val[i];
- activeHandleIdx = i;
- }
- }
- return val[activeHandleIdx];
- }
- /// <summary>
- /// Paints the active handle of the ROI object into the supplied window
- /// </summary>
- /// <param name="window">HALCON window</param>
- public override void displayActive(HalconDotNet.HWindow window, int imageWidth, int imageHeight)
- {
- double littleRecSize = 0;
- if (imageHeight < 300) littleRecSize = 1;
- else if (imageHeight < 600) littleRecSize = 2;
- else if (imageHeight < 900) littleRecSize = 3;
- else if (imageHeight < 1200) littleRecSize = 4;
- else if (imageHeight < 1500) littleRecSize = 5;
- else if (imageHeight < 1800) littleRecSize = 6;
- else if (imageHeight < 2100) littleRecSize = 7;
- else if (imageHeight < 2400) littleRecSize = 8;
- else if (imageHeight < 2700) littleRecSize = 9;
- else if (imageHeight < 3000) littleRecSize = 10;
- else if (imageHeight < 3300) littleRecSize = 11;
- else if (imageHeight < 3600) littleRecSize = 12;
- else if (imageHeight < 3900) littleRecSize = 13;
- else if (imageHeight < 4200) littleRecSize = 14;
- else if (imageHeight < 4500) littleRecSize = 15;
- else if (imageHeight < 4800) littleRecSize = 16;
- else if (imageHeight < 5100) littleRecSize = 17;
- else littleRecSize = 18;
- if (littleRecSize % 2 != 0)
- littleRecSize++;
- window.DispRectangle2(rows[activeHandleIdx].D,
- cols[activeHandleIdx].D,
- -phi, littleRecSize, littleRecSize);
- if (activeHandleIdx == 5)
- window.DispArrow(midR, midC,
- midR + (Math.Sin(phi) * length1 * 1.8),
- midC + (Math.Cos(phi) * length1 * 1.8),
- littleRecSize);
- }
- /// <summary>Gets the HALCON region described by the ROI</summary>
- public override HRegion getRegion()
- {
- HRegion region = new HRegion();
- region.GenRectangle2(midR, midC, -phi, length1, length2);
- return region;
- }
- /// <summary>
- /// Gets the model information described by
- /// the interactive ROI
- /// </summary>
- public override HTuple getModelData()
- {
- return new HTuple(new double[] { midR, midC, phi, length1, length2 });
- }
- public override HTuple getRowsData()
- {
- return new HTuple(rows);
- }
- public override HTuple getColsData()
- {
- return new HTuple(cols );
- }
- /// <summary>
- /// Recalculates the shape of the ROI instance. Translation is
- /// performed at the active handle of the ROI object
- /// for the image coordinate (x,y)
- /// </summary>
- /// <param name="newX">x mouse coordinate</param>
- /// <param name="newY">y mouse coordinate</param>
- public override void moveByHandle(double newX, double newY, HWindowControl window)
- {
- double vX, vY, x=0, y=0;
- switch (activeHandleIdx)
- {
- case 0:
- case 1:
- case 2:
- case 3:
-
- tmp = hom2D.HomMat2dInvert();
- x = tmp.AffineTransPoint2d(newX, newY, out y);
- length2 = Math.Abs(y);
- length1 = Math.Abs(x);
- checkForRange(x, y);
- window.Cursor = System.Windows.Forms.Cursors.Hand ;
- break;
- case 4:
- midC = newX;
- midR = newY;
- window.Cursor = System.Windows.Forms.Cursors.SizeAll ;
- break;
- case 5:
- vY = newY - rows[4].D;
- vX = newX - cols[4].D;
- phi = Math.Atan2(vY, vX);
- window.Cursor = System.Windows.Forms.Cursors.Hand;
- break;
- case 7:
- case 9:
- tmp = hom2D.HomMat2dInvert();
- x = tmp.AffineTransPoint2d(newX, newY, out y);
-
-
- length2 = Math.Abs(y);
- checkForRange(x, y);
- window.Cursor = System.Windows.Forms.Cursors.Hand;
- break;
- case 6:
- case 8:
- tmp = hom2D.HomMat2dInvert();
- x = tmp.AffineTransPoint2d(newX, newY, out y);
- length1 = Math.Abs(x);
- checkForRange(x, y);
- window.Cursor = System.Windows.Forms.Cursors.Hand;
- break;
- }
- updateHandlePos();
- }//end of method
- /// <summary>
- /// Auxiliary method to recalculate the contour points of
- /// the rectangle by transforming the initial row and
- /// column coordinates (rowsInit, colsInit) by the updated
- /// homography hom2D
- /// </summary>
- private void updateHandlePos()
- {
- hom2D.HomMat2dIdentity();
- hom2D = hom2D.HomMat2dTranslate(midC, midR);
- hom2D = hom2D.HomMat2dRotateLocal(phi);
- tmp = hom2D.HomMat2dScaleLocal(length1, length2);
- cols = tmp.AffineTransPoint2d(colsInit, rowsInit, out rows);
- }
- /* This auxiliary method checks the half lengths
- * (length1, length2) using the coordinates (x,y) of the four
- * rectangle corners (handles 0 to 3) to avoid 'bending' of
- * the rectangular ROI at its midpoint, when it comes to a
- * 'collapse' of the rectangle for length1=length2=0.
- * */
- private void checkForRange(double x, double y)
- {
- switch (activeHandleIdx)
- {
- case 0:
- if ((x < 0) && (y < 0))
- return;
- if (x >= 0) length1 = 0.01;
- if (y >= 0) length2 = 0.01;
- break;
- case 1:
- if ((x > 0) && (y < 0))
- return;
- if (x <= 0) length1 = 0.01;
- if (y >= 0) length2 = 0.01;
- break;
- case 2:
- if ((x > 0) && (y > 0))
- return;
- if (x <= 0) length1 = 0.01;
- if (y <= 0) length2 = 0.01;
- break;
- case 3:
- if ((x < 0) && (y > 0))
- return;
- if (x >= 0) length1 = 0.01;
- if (y <= 0) length2 = 0.01;
- break;
- default:
- break;
- }
- }
- }//end of class
- }//end of namespace
|