| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 | using PaintDotNet.Annotation.Enum;using PaintDotNet.Annotation.Label;using PaintDotNet.Base.SettingModel;using System;using System.Collections.Generic;using System.Diagnostics;using System.Drawing;using System.Drawing.Drawing2D;using System.Globalization;using System.Runtime.Serialization;using System.Windows.Forms;using static PaintDotNet.Base.SettingModel.LabelStyleModel;namespace PaintDotNet.Annotation.ImageCollect{    /// <summary>    /// 图像拼接->圆    /// </summary>    public class DrawStitchingCircle : DrawStithchingBase    {        private const string entryRectangle = "Rect";        /// <summary>        /// 标注样式信息model        /// </summary>        public CircleModel labelCircleStyleModel;        public DrawStitchingCircle(ISurfaceBox surfaceBox, int x, int y, int width, int height) : base()        {            this.objectType = DrawClass.Stitch;            this.drawToolType = DrawToolType.DrawStitchingCircle;            labelCircleStyleModel = surfaceBox.GetLabelStyleModel().circleModel;            rectangle.X = x;            rectangle.Y = y;            rectangle.Width = width;            rectangle.Height = height;            Initialize();        }        public DrawStitchingCircle(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()        {            this.objectType = DrawClass.Stitch;            this.drawToolType = DrawToolType.DrawStitchingCircle;            this.ISurfaceBox = surfaceBox;            labelCircleStyleModel = (CircleModel)parentStyleModel;            rectangle.X = points[0].X;            rectangle.Y = points[0].Y;            rectangle.Width = points[1].X - points[0].X;            rectangle.Height = points[1].Y - points[0].Y;        }        /// <summary>        /// Clone this instance        /// </summary>        public override DrawObject Clone()        {            DrawStitchingCircle drawRectangle = new DrawStitchingCircle(ISurfaceBox, 0, 0, 1, 0);            drawRectangle.objectType = DrawClass.Stitch;            drawRectangle.drawToolType = DrawToolType.DrawStitchingCircle;            drawRectangle.ISurfaceBox = this.ISurfaceBox;            drawRectangle.rectangle = this.rectangle;            FillDrawObjectFields(drawRectangle);            return drawRectangle;        }        public override DrawObject Clone(ISurfaceBox surfaceBox)        {            DrawStitchingCircle drawRectangle = new DrawStitchingCircle(surfaceBox, 0, 0, 1, 0);            drawRectangle.objectType = DrawClass.Stitch;            drawRectangle.drawToolType = DrawToolType.DrawStitchingCircle;            drawRectangle.ISurfaceBox = surfaceBox;            drawRectangle.rectangle = this.rectangle;            drawRectangle.labelCircleStyleModel = this.labelCircleStyleModel;            FillDrawObjectFields(drawRectangle);            return drawRectangle;        }        /// <summary>        /// Draw rectangle        /// </summary>        /// <param name="g"></param>        public override void Draw(Graphics g)        {            //Color color = Color.FromArgb(this.labelCircleStyleModel.lineColor);            Color color = Color.Gray;            Pen pen = new Pen(color, PenWidth);            int penWidth = 1; //this.labelRectStyleModel.lineWidth;            pen.DashStyle = DashStyle.Solid; // (DashStyle)this.labelRectStyleModel.lineStyle;            pen.Width = penWidth;            float x1 = Rectangle.X;            float y1 = Rectangle.Y;            float x2 = Rectangle.Right;            float y2 = Rectangle.Bottom;            if (x2 < x1)            {                float tmp = x2;                x2 = x1;                x1 = tmp;            }            if (y2 < y1)            {                float tmp = y2;                y2 = y1;                y1 = tmp;            }            float radius = (x2 - x1) < (y2 - y1) ? (x2 - x1) : (y2 - y1);            g.DrawEllipse(pen, new RectangleF(x1, y1, radius, radius));            pen.Color = Color.FromArgb(this.labelCircleStyleModel.lineColor);            float halfW = rectangle.Width / 2;            float halfH = rectangle.Height / 2;            int xNum = CalGridNum(rectangle.Width, ViewWidth);            int yNum = CalGridNum(rectangle.Height, ViewHeight);            if (xNum != tiles[0] || yNum != tiles[1])            {                tiles[0] = xNum;                tiles[1] = yNum;                this.deletedPointId.Clear();                this.zscan.Clear();                for (int j = 0; j < tiles[1]; j++)                {                    for (int i = 0; i < tiles[0]; i++)                    {                        zscan.Add(new ZScanParameter());                    }                }            }            PointF centerPoint = new PointF();            centerPoint.X = rectangle.X + halfW;            centerPoint.Y = rectangle.Y + halfH;            PointF startPoint = new PointF();            if (tiles[0] % 2 == 0)            {                startPoint.X = (float)centerPoint.X - ViewWidth * (tiles[0] / 2 - Interval / 2 - (tiles[0] / 2 - 1) * Interval);            }            else            {                int n = (tiles[0] - 1) / 2;                startPoint.X = (float)centerPoint.X - ViewWidth / 2 - n * ViewWidth * (1 - Interval);            }            if (tiles[1] % 2 == 0)            {                startPoint.Y = (float)centerPoint.Y - ViewHeight * (tiles[1] / 2 - Interval / 2 - (tiles[1] / 2 - 1) * Interval);            }            else            {                int n = (tiles[1] - 1) / 2;                startPoint.Y = (float)centerPoint.Y - ViewHeight / 2 - n * ViewHeight * (1 - Interval);            }            rectangleMax = new RectangleF(startPoint, new SizeF(ViewWidth * (tiles[0] - (tiles[0] - 1) * Interval), ViewHeight * (tiles[1] - (tiles[1] - 1) * Interval)));            List<PointF> tmpList;            _points.Clear();            int index = 0;            for (int j = 0; j < tiles[1]; j++)            {                tmpList = new List<PointF>();                for (int i = 0; i < tiles[0]; i++)                {                    float x = startPoint.X + i * (ViewWidth * (1 - Interval));                    float y = startPoint.Y + j * (ViewHeight * (1 - Interval));                    tmpList.Add(new PointF(x, y));                }                if (j % 2 == 1)                {                    for (int m = tmpList.Count - 1; m >= 0; m--)                    {                        Dictionary<int, object> myDictionary = new Dictionary<int, object>();                        myDictionary.Add(0, this.ISurfaceBox.ScalePointToRulerPoint(new PointF(tmpList[m].X, tmpList[m].Y)));                        if (!deletedPointId.Contains(index))                        {                            g.DrawRectangle(pen, tmpList[m].X, tmpList[m].Y, ViewWidth, ViewHeight);                            myDictionary.Add(1, 0);                        }                        else                        {                            myDictionary.Add(1, 1);                        }                        _points.Add(myDictionary);                        index++;                    }                }                else                {                    for (int m = 0; m < tmpList.Count; m++)                    {                        Dictionary<int, object> myDictionary = new Dictionary<int, object>();                        myDictionary.Add(0, this.ISurfaceBox.ScalePointToRulerPoint(new PointF(tmpList[m].X, tmpList[m].Y)));                        if (!deletedPointId.Contains(index))                        {                            g.DrawRectangle(pen, tmpList[m].X, tmpList[m].Y, ViewWidth, ViewHeight);                            myDictionary.Add(1, 0);                        }                        else                        {                            myDictionary.Add(1, 1);                        }                        _points.Add(myDictionary);                        index++;                    }                }            }            pen.Dispose();        }        private int CalGridNum(float width, int side)        {            for (int i = 0; i <= 10000; i++)            {                if ((i - (i + 1) * Interval) * side > width)                {                    return i;                }            }            return 0;        }        protected void SetRectangle(float x, float y, float width, float height, int handleNumber)        {            this.rectangle.X = x;            this.rectangle.Y = y;            if (handleNumber == 2 || handleNumber == 6)            {                this.rectangle.Width = height;                this.rectangle.Height = height;            }            else            {                this.rectangle.Width = width;                this.rectangle.Height = width;            }        }        public override Rectangle GetHandleRectangle(int handleNumber)        {            if (lockType != LockType.NULL)            {                return new Rectangle();            }            PointF point = GetHandle(handleNumber);            return new Rectangle((int)(point.X - 100), (int)(point.Y - 100), 201, 201);        }        /// <summary>        /// Get number of handles        /// </summary>        public override int HandleCount        {            get            {                return 8;            }        }        /// <summary>        /// Get handle pointscroll by 1-based number        /// </summary>        /// <param name="handleNumber"></param>        /// <returns></returns>        public override PointF GetHandle(int handleNumber)        {            float x, y, xCenter, yCenter;            xCenter = rectangle.X + rectangle.Width / 2;            yCenter = rectangle.Y + rectangle.Height / 2;            x = rectangle.X;            y = rectangle.Y;            switch (handleNumber)            {                case 1:                    x = rectangle.X;                    y = rectangle.Y;                    break;                case 2:                    x = xCenter;                    y = rectangle.Y;                    break;                case 3:                    x = rectangle.Right;                    y = rectangle.Y;                    break;                case 4:                    x = rectangle.Right;                    y = yCenter;                    break;                case 5:                    x = rectangle.Right;                    y = rectangle.Bottom;                    break;                case 6:                    x = xCenter;                    y = rectangle.Bottom;                    break;                case 7:                    x = rectangle.X;                    y = rectangle.Bottom;                    break;                case 8:                    x = rectangle.X;                    y = yCenter;                    break;            }            return new PointF(x, y);        }        /// <summary>        /// Hit test.        /// Return value: -1 - no hit        ///                0 - hit anywhere        ///                > 1 - handle number        /// </summary>        /// <param name="pointscroll"></param>        /// <returns></returns>        public override int HitTest(Point point)        {            if (Selected)            {                for (int i = 1; i <= HandleCount; i++)                {                    if (GetHandleRectangle(i).Contains(point))                        return i;                }            }            if (PointInObject(point))                return 0;            return -1;        }        protected override bool PointInObject(Point point)        {            return rectangle.Contains(point);        }        /// <summary>        /// Get cursor for the handle        /// </summary>        /// <param name="handleNumber"></param>        /// <returns></returns>        public override Cursor GetHandleCursor(int handleNumber)        {            switch (handleNumber)            {                case 1:                    return Cursors.SizeNWSE;                case 2:                    return Cursors.SizeNS;                case 3:                    return Cursors.SizeNESW;                case 4:                    return Cursors.SizeWE;                case 5:                    return Cursors.SizeNWSE;                case 6:                    return Cursors.SizeNS;                case 7:                    return Cursors.SizeNESW;                case 8:                    return Cursors.SizeWE;                default:                    return Cursors.Default;            }        }        /// <summary>        /// Move handle to new pointscroll (resizing)        /// </summary>        /// <param name="pointscroll"></param>        /// <param name="handleNumber"></param>        public override void MoveHandleTo(Point point, int handleNumber)        {            float left = Rectangle.Left;            float top = Rectangle.Top;            float right = Rectangle.Right;            float bottom = Rectangle.Bottom;            switch (handleNumber)            {                case 1:                    left = point.X;                    top = point.Y;                    break;                case 2:                    top = point.Y;                    break;                case 3:                    right = point.X;                    top = point.Y;                    break;                case 4:                    right = point.X;                    break;                case 5:                    right = point.X;                    bottom = point.Y;                    break;                case 6:                    bottom = point.Y;                    break;                case 7:                    left = point.X;                    bottom = point.Y;                    break;                case 8:                    left = point.X;                    break;            }            SetRectangle(left, top, right - left, bottom - top, handleNumber);        }        public override bool IntersectsWith(Rectangle rectangle)        {            return Rectangle.IntersectsWith(rectangle);        }        /// <summary>        /// Move object        /// </summary>        /// <param name="deltaX"></param>        /// <param name="deltaY"></param>        public override void Move(int deltaX, int deltaY)        {            int x = ISurfaceBox.UnscaleScalar(deltaX);            int y = ISurfaceBox.UnscaleScalar(deltaY);            rectangle.X += x;            rectangle.Y += y;        }        public override void Dump()        {            base.Dump();            Trace.WriteLine("rectangle.X = " + rectangle.X.ToString(CultureInfo.InvariantCulture));            Trace.WriteLine("rectangle.Y = " + rectangle.Y.ToString(CultureInfo.InvariantCulture));            Trace.WriteLine("rectangle.Width = " + rectangle.Width.ToString(CultureInfo.InvariantCulture));            Trace.WriteLine("rectangle.Height = " + rectangle.Height.ToString(CultureInfo.InvariantCulture));        }        /// <summary>        /// Save objevt to serialization stream        /// </summary>        /// <param name="info"></param>        /// <param name="orderNumber"></param>        public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)        {            info.AddValue(                String.Format(CultureInfo.InvariantCulture,                "{0}{1}",                entryRectangle, orderNumber),                rectangle);            base.SaveToStream(info, orderNumber);        }        /// <summary>        /// LOad object from serialization stream        /// </summary>        /// <param name="info"></param>        /// <param name="orderNumber"></param>        public override void LoadFromStream(SerializationInfo info, int orderNumber)        {            rectangle = (Rectangle)info.GetValue(                String.Format(CultureInfo.InvariantCulture,                "{0}{1}",                entryRectangle, orderNumber),                typeof(Rectangle));            base.LoadFromStream(info, orderNumber);        }        #region Helper Functions        public static Rectangle GetNormalizedRectangle(int x1, int y1, int x2, int y2)        {            if (x2 < x1)            {                int tmp = x2;                x2 = x1;                x1 = tmp;            }            if (y2 < y1)            {                int tmp = y2;                y2 = y1;                y1 = tmp;            }            return new Rectangle(x1, y1, x2 - x1, y2 - y1);        }        public static Rectangle GetNormalizedRectangle(Point p1, Point p2)        {            return GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y);        }        public static Rectangle GetNormalizedRectangle(Rectangle r)        {            return GetNormalizedRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height);        }        #endregion        public override RectangleF GetBoundingBox()        {            return rectangle;        }        public override List<PointF> GetPoints()        {            List<PointF> points = new List<PointF>();            points.Add(new PointF(rectangle.X, rectangle.Y));            points.Add(new PointF(rectangle.Right, rectangle.Bottom));            return points;        }        public override ParentStyleModel GetStyle()        {            return labelCircleStyleModel;        }    }}
 |