private Image ReadSegments(Stream stream)
        {
            var bytes = new ByteBuffer(stream);

            Segments = new List <SegmentBase>();
            while (true)
            {
                var TempSegment = SegmentBase.Read(bytes, Segments);
                if (TempSegment != null)
                {
                    TempSegment.Setup(Segments);
                    if (!Segments.Contains(TempSegment))
                    {
                        Segments.Add(TempSegment);
                    }
                    if (TempSegment.Type == SegmentTypes.EndOfImage)
                    {
                        break;
                    }
                }
            }
            var StartOfScanSegment = Segments.OfType <StartOfScan>().FirstOrDefault();

            if (StartOfScanSegment == null)
            {
                throw new ImageException("No scan information found.");
            }
            return(StartOfScanSegment.Convert(ReturnValue, Segments));
        }
Exemple #2
0
        private static void CalculateSelfIntersections(SegmentBase segment)
        {
            if (!(segment is CubicBezierSegment))
            {
                // only cubic bezier segment can self-intersect
                return;
            }
            var points = segment.PolylineApproximation.Points;

            for (var i = 1; i < points.Count - 2; i++)
            {
                var p11  = points[i - 1];
                var p12  = points[i];
                var box1 = new Box(p11, p12);
                for (var j = i + 2; j < points.Count; j++)
                {
                    var p21  = points[j - 1];
                    var p22  = points[j];
                    var box2 = new Box(p21, p22);
                    if (!box1.IntersectsWith(box2))
                    {
                        continue;
                    }
                    var intersection = CalculateIntersection(p11, p12, p21, p22);
                    if (intersection.HasValue)
                    {
                        segment.Intersections.Add(intersection.Value);
                    }
                }
            }
        }
Exemple #3
0
 private bool ValidateFieldChecks(FieldCheckAttribute check, SegmentBase segment)
 {
     if (!check.Valid(segment))
     {
         return(false);
     }
     return(true);
 }
 private static bool IsDegenerate(Point startPoint, SegmentBase segment)
 {
     if (segment is LineSegment || segment is EllipticalArcSegment)
     {
         return(segment.EndPoint == startPoint);
     }
     return(false);
 }
Exemple #5
0
        static internal Point SegmentIntersection(SegmentBase first, SegmentBase second)
        {
            // Caller expects segments to intersect.
            Point intersect;

            if (!SegmentsIntersect(first, second, out intersect))
            {
                Debug.Assert(false, "intersect is not on both segments");
            }
            return(intersect);
        }
Exemple #6
0
 private bool ValidateFieldChecks(List <FieldCheckAttribute> checks, SegmentBase segment)
 {
     foreach (FieldCheckAttribute att in checks)
     {
         if (!ValidateFieldChecks(att, segment))
         {
             return(false);
         }
     }
     return(true);
 }
        public static Subpath ReverseDirection(Subpath subpath)
        {
            var segments   = new SegmentBase[subpath.Segments.Count];
            var startPoint = subpath.StartPoint;

            for (var i = 0; i < segments.Length; i++)
            {
                var segment = subpath.Segments[i];
                segments[i] = segment.Reverse(startPoint);
                startPoint  = segment.EndPoint;
            }
            Array.Reverse(segments);
            return(new Subpath(startPoint, segments, subpath.IsClosed));
        }
Exemple #8
0
        public void LoadFile(TextReader reader)
        {
            this.CurrentSeperators = new Seperators();
            char[] charData = new char[105];
            int    count    = reader.Read(charData, 0, 105);

            if (count != 105)
            {
                throw new Exception("File is not long enough");
            }
            string isaString = new String(charData);

            if (isaString.Substring(0, 3) != "ISA")
            {
                throw new Exception("This File does not start with an ISA Segment");
            }
            CurrentSeperators.DataElement = isaString[3];
            string[] isaFields = isaString.Split(CurrentSeperators.DataElement);
            CurrentSeperators.ComponentDataElement = isaFields[16].First();
            CurrentSeperators.Repetition           = isaFields[11] == "U" ? '~' : isaFields[11].First();
            this.CurrentSeperators.Segment         = (char)reader.Read();
            this.InterchangeHeader = ParseUsingPosition(typeof(ISA_Segment), isaFields) as ISA_Segment;
            string[] segments = reader.ReadToEnd().Split(this.CurrentSeperators.Segment);

            foreach (string segment in segments)
            {
                string[] fields = segment.Trim(new char[] { '\r', '\n', ' ' }).Split(CurrentSeperators.DataElement);
                Type     tp;
                if (SegmentDefinitions.TryGetValue(fields[0], out tp))
                {
                    SegmentBase segmentbase = ParseUsingPosition(tp, fields);
                    switch (segmentbase.SegmentID)
                    {
                    case "IEA":
                        this.InterchangeFooter = segmentbase as IEA_Segment;
                        break;

                    case "GS":
                        this.Groups.Add(new Group(this));
                        this.Groups.Last().AddSegment(segmentbase);
                        break;

                    default:
                        this.Groups.Last().AddSegment(segmentbase);
                        break;
                    }
                }
            }
        }
Exemple #9
0
        private static void CalculateIntersections(SegmentBase segment1, SegmentBase segment2, bool skipInner, bool skipOuter)
        {
            var boundingBox1 = segment1.PolylineApproximation.BoundingBox;
            var boundingBox2 = segment2.PolylineApproximation.BoundingBox;

            if (!boundingBox1.IntersectsWith(boundingBox2))
            {
                return;
            }
            var points1 = segment1.PolylineApproximation.Points;
            var points2 = segment2.PolylineApproximation.Points;

            for (var i = 1; i < points1.Count; i++)
            {
                var p11  = points1[i - 1];
                var p12  = points1[i];
                var box1 = new Box(p11, p12);
                if (!box1.IntersectsWith(boundingBox2))
                {
                    continue;
                }
                for (var j = 1; j < points2.Count; j++)
                {
                    var p21  = points2[j - 1];
                    var p22  = points2[j];
                    var box2 = new Box(p21, p22);
                    if (!box1.IntersectsWith(box2))
                    {
                        continue;
                    }
                    if ((skipInner && i == points1.Count - 1 && j == 1) ||
                        (skipOuter && i == 1 && j == points2.Count - 1))
                    {
                        continue;
                    }
                    var intersection = CalculateIntersection(p11, p12, p21, p22);
                    if (intersection.HasValue)
                    {
                        segment1.Intersections.Add(intersection.Value);
                        segment2.Intersections.Add(intersection.Value);
                    }
                }
            }
        }
        private static void CalculateIntersections(SegmentBase segment1, SegmentBase segment2, bool skipInner, bool skipOuter)
        {
            if (!segment1.PolylineApproximation.BoundingBox.IntersectsWith(segment2.PolylineApproximation.BoundingBox))
            {
                return;
            }
            ConvertTimer.ThrowIfOvertime();
            var iterator1 = new LineIterator(segment1.PolylineApproximation);

            while (iterator1.MoveNextLine())
            {
                var iterator2        = new LineIterator(segment2.PolylineApproximation);
                var skipCurrentPart1 = true;
                while (iterator2.MoveNextLine())
                {
                    if (!iterator1.CurrentPart.BoundingBox.IntersectsWith(iterator2.CurrentPart.BoundingBox))
                    {
                        iterator2.SkipCurrentPart();
                        continue;
                    }
                    skipCurrentPart1 = false;
                    if ((skipInner && iterator1.IsLastLine && iterator2.IsFirstLine) ||
                        (skipOuter && iterator1.IsFirstLine && iterator2.IsLastLine))
                    {
                        continue;
                    }
                    var intersection = CalculateIntersection(iterator1.CurrentStartPoint, iterator1.CurrentEndPoint, iterator2.CurrentStartPoint, iterator2.CurrentEndPoint);
                    if (intersection.HasValue)
                    {
                        segment1.Intersections.Add(intersection.Value);
                        segment2.Intersections.Add(intersection.Value);
                    }
                }
                if (skipCurrentPart1)
                {
                    iterator1.SkipCurrentPart();
                }
            }
        }
Exemple #11
0
        public void AddSegment(SegmentBase segment)
        {
            switch (segment.SegmentID)
            {
            case "GS":
                this.Header = segment as GS_Segment;
                break;

            case "GE":
                this.Footer = segment as GE_Segment;
                break;

            case "ST":
                this.TransactionSet.Add(new TransactionSet(this.eDIParser));
                this.TransactionSet.Last().AddSegment(segment);
                break;

            default:
                this.TransactionSet.Last().AddSegment(segment);
                break;
            }
        }
Exemple #12
0
        private static Point SplitByNextIntersection(Point startPoint, SegmentBase segment, out SegmentBase segment1, out SegmentBase segment2,
                                                     CurveEquation equation,
                                                     Func <double, Point, SegmentBase> factory1,
                                                     Func <double, SegmentBase> factory2)
        {
            var intersection = segment.Intersections.Select(x => new { Point = x, T = equation.GetT(x) }).MinBy(x => x.T);
            var t            = Math.Min(Math.Max(intersection.T, 0), 1);
            var point        = equation.GetPoint(t);

            segment1 = point == startPoint ? null : factory1(t, intersection.Point);
            if (point == segment.EndPoint)
            {
                segment2 = null;
                Debugger.BreakWhen(segment.Intersections.Count > 1);
            }
            else
            {
                segment2 = factory2(t);
                segment2.Intersections.AddRange(segment.Intersections.Where(x => x != intersection.Point));
            }
            return(intersection.Point);
        }
        private static void CalculateSelfIntersections(SegmentBase segment)
        {
            if (!(segment is CubicBezierSegment))
            {
                // only cubic bezier segment can self-intersect
                return;
            }
            ConvertTimer.ThrowIfOvertime();
            var iterator1 = new LineIterator(segment.PolylineApproximation);

            while (iterator1.MoveNextLine())
            {
                var iterator2 = iterator1.Clone();
                if (!iterator2.MoveNextLine())
                {
                    return;
                }
                var skipCurrentPart1 = true;
                while (iterator2.MoveNextLine())
                {
                    if (!iterator1.CurrentPart.BoundingBox.IntersectsWith(iterator2.CurrentPart.BoundingBox))
                    {
                        iterator2.SkipCurrentPart();
                        continue;
                    }
                    skipCurrentPart1 = false;
                    var intersection = CalculateIntersection(iterator1.CurrentStartPoint, iterator1.CurrentEndPoint, iterator2.CurrentStartPoint, iterator2.CurrentEndPoint);
                    if (intersection.HasValue)
                    {
                        segment.Intersections.Add(intersection.Value);
                    }
                }
                if (skipCurrentPart1)
                {
                    iterator1.SkipCurrentPart();
                }
            }
        }
        private static SegmentBase Simplify(Point startPoint, SegmentBase segment)
        {
            var endPoint       = segment.EndPoint;
            var simplifyToLine = false;

            if (segment is EllipticalArcSegment)
            {
                var x = (EllipticalArcSegment)segment;
                simplifyToLine = x.Radii.X.IsZero() || x.Radii.Y.IsZero();
            }
            if (segment is QuadraticBezierSegment)
            {
                var x = (QuadraticBezierSegment)segment;
                simplifyToLine = x.ControlPoint == startPoint || x.ControlPoint == endPoint;
            }
            if (segment is CubicBezierSegment)
            {
                var x = (CubicBezierSegment)segment;
                simplifyToLine = (x.ControlPoint1 == startPoint || x.ControlPoint1 == endPoint) &&
                                 (x.ControlPoint2 == startPoint || x.ControlPoint2 == endPoint);
            }
            return(simplifyToLine ? new LineSegment(endPoint) : segment);
        }
Exemple #15
0
        private SegmentBase ParseUsingPosition(Type type, string[] fields)
        {
            SegmentBase ret = Activator.CreateInstance(type) as SegmentBase;

            foreach (PropertyInfo p in ret.GetType().GetProperties())
            {
                PositionIndexAttribute pos = p.GetCustomAttributes().Where(a => a is PositionIndexAttribute).FirstOrDefault() as PositionIndexAttribute;
                if (pos != null && pos.Index < fields.Length)
                {
                    if (p.PropertyType.GetInterfaces().Contains(typeof(IComplexField)))
                    {
                        IComplexField complexField = Activator.CreateInstance(p.PropertyType) as IComplexField;
                        this.UpdateComplexField(complexField, fields[pos.Index]);
                        p.SetValue(ret, complexField);
                    }
                    else
                    {
                        p.SetValue(ret, fields[pos.Index]);
                    }
                }
            }
            return(ret);
        }
Exemple #16
0
    void ReadMapHeader(StreamReader strm)
    {
        Regex regExNmFile      = new Regex("^[0-9a-f]{8} ");
        Regex regEx            = new Regex(Reg_SegmentInfo);
        Regex regHeaderSection = new Regex(@"\s*Address\s*Publics by Value\s*Rva\+Base\s*Lib:Object", RegexOptions.IgnoreCase);
        Regex regnonNMHeader   = new Regex(@"\s*Start\s*Length\s*Name\s*Class", RegexOptions.IgnoreCase);
        Match match            = null;

        UInt32 lastSegmentIndex = 0;
        UInt32 segmentIndex;
        UInt32 sectionStart  = 0;
        UInt32 sectionLength = 0;

        String       line;
        bool         bInSegmentDecl = false;
        const UInt32 baseOfCode     = 0x1000;

        SegmentBase.Add((UInt32)baseOfCode);
        for (;;)
        {
            line = strm.ReadLine();

            if (bInSegmentDecl)
            {
                if (regHeaderSection.IsMatch(line))
                {
                    // Header section ends.
                    break;
                }
#if DACTABLEGEN_DEBUG
                Console.WriteLine("SegmentDecl: " + line);
#endif

                match = regEx.Match(line);
                if (match.Success)
                {
                    segmentIndex = UInt32.Parse(match.Groups["addrPart1"].ToString(), NumberStyles.AllowHexSpecifier);
                    if (segmentIndex != lastSegmentIndex)
                    {
                        // Enter the new segment. Record what we have.
                        // Note, SegmentBase[i] is built upon SegmentBase[i-1]
                        SegmentBase.Add((UInt32)SegmentBase[SegmentBase.Count - 1] + (sectionStart + sectionLength + (UInt32)0xFFF) & (~((UInt32)0xFFF)));
                        lastSegmentIndex = segmentIndex;
                    }
                    sectionStart  = UInt32.Parse(match.Groups["addrPart2"].ToString(), NumberStyles.AllowHexSpecifier);
                    sectionLength = UInt32.Parse(match.Groups["length"].ToString(), NumberStyles.AllowHexSpecifier);
                }

                if (line == null)
                {
                    throw new InvalidOperationException("Invalid MAP header format.");
                }
            }
            else
            {
                if (!regnonNMHeader.IsMatch(line))
                {
                    match = regExNmFile.Match(line);
                    if (match.Success)
                    {
                        // It's a nm file format. There is no header for it.
                        break;
                    }
                    continue;
                }
                bInSegmentDecl    = true;
                bIsWindowsMapfile = true;
            }
        }

        if (bIsWindowsMapfile)
        {
            //
            // Only Windows map file has SegmentBase
            //
            for (int i = 1; i <= lastSegmentIndex; i++)
            {
#if DACTABLEGEN_DEBUG
                Console.WriteLine("SegmentBase[{0}] = {1:x8}", i, SegmentBase[i]);
#endif
            }
        }
    }
 static double SegmentPosition(SegmentBase segment, Directions direction)
 {
     return(direction == Directions.North ? segment.Start.X : -segment.Start.Y);
 }
Exemple #18
0
 static internal bool IntervalsOverlap(SegmentBase first, SegmentBase second)
 {
     return(IntervalsOverlap(first.Start, first.End, second.Start, second.End));
 }
Exemple #19
0
 static internal bool PointIsOnSegment(SegmentBase seg, Point test)
 {
     return(PointIsOnSegment(seg.Start, seg.End, test));
 }
Exemple #20
0
        static internal bool SegmentsIntersect(SegmentBase first, SegmentBase second)
        {
            Point intersect;

            return(SegmentsIntersect(first, second, out intersect));
        }
Exemple #21
0
 static internal bool SegmentsIntersect(SegmentBase first, SegmentBase second, out Point intersect)
 {
     return(IntervalsIntersect(first.Start, first.End, second.Start, second.End, out intersect));
 }
 internal bool IsPerpendicular(SegmentBase seg)
 {
     return(IsPerpendicular(seg.Start, seg.End));
 }
Exemple #23
0
 static internal Point SegmentIntersection(SegmentBase seg, Point from)
 {
     return(SegmentIntersection(seg.Start, seg.End, from));
 }
Exemple #24
0
 public static Point SplitByNextIntersection(Point startPoint, LineSegment segment, out SegmentBase segment1, out SegmentBase segment2)
 {
     return(SplitByNextIntersection(startPoint, segment, out segment1, out segment2,
                                    new LineEquation(startPoint, segment),
                                    (t, p) => new LineSegment(p),
                                    t => new LineSegment(segment.EndPoint)));
 }
 internal bool IsFlat(SegmentBase seg)
 {
     return(IsFlat(seg.Start, seg.End));
 }
 static internal bool IsVertical(SegmentBase seg)
 {
     return(IsVertical(PointComparer.GetPureDirection(seg.Start, seg.End)));
 }
Exemple #27
0
        public static Point SplitByNextIntersection(Point startPoint, EllipticalArcSegment segment, out SegmentBase segment1, out SegmentBase segment2)
        {
            Func <Point, EllipticalArcSegment> create = p => new EllipticalArcSegment(segment.Radii, segment.XAxisRotation, segment.IsLargeArc, segment.IsSweep, p);

            return(SplitByNextIntersection(startPoint, segment, out segment1, out segment2,
                                           new EllipticalArcEquation(startPoint, segment),
                                           (t, p) => create(p),
                                           t => create(segment.EndPoint)));
        }
Exemple #28
0
 public static Point SplitByNextIntersection(Point startPoint, QuadraticBezierSegment segment, out SegmentBase segment1, out SegmentBase segment2)
 {
     return(SplitByNextIntersection(startPoint, segment, out segment1, out segment2,
                                    new QuadraticBezierEquation(startPoint, segment),
                                    (t, p) => new QuadraticBezierSegment(Mid(startPoint, segment.ControlPoint, t), p),
                                    t => new QuadraticBezierSegment(Mid(segment.ControlPoint, segment.EndPoint, t), segment.EndPoint)));
 }
 static internal double Slope(SegmentBase seg, ScanDirection scanDir)
 {
     return(Slope(seg.Start, seg.End, scanDir));
 }
Exemple #30
0
 public static Point SplitByNextIntersection(Point startPoint, CubicBezierSegment segment, out SegmentBase segment1, out SegmentBase segment2)
 {
     return(SplitByNextIntersection(startPoint, segment, out segment1, out segment2,
                                    new CubicBezierEquation(startPoint, segment),
                                    (t, p) =>
     {
         var a = Mid(startPoint, segment.ControlPoint1, t);
         var b = Mid(segment.ControlPoint1, segment.ControlPoint2, t);
         return new CubicBezierSegment(a, Mid(a, b, t), p);
     },
                                    t =>
     {
         var b = Mid(segment.ControlPoint1, segment.ControlPoint2, t);
         var c = Mid(segment.ControlPoint2, segment.EndPoint, t);
         return new CubicBezierSegment(Mid(b, c, t), c, segment.EndPoint);
     }));
 }