Пример #1
0
        public void TestStaticOrs()
        {
            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((Generex <int>)null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((Generex <int>[])null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((IEnumerable <Generex <int> >)null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((Generex <int, int>)null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((Generex <int, int>[])null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((IEnumerable <Generex <int, int> >)null); });
            AssertMatches(Generex.Ors(_g, _gn), _input, True, False, False, False, True, new object[] { 0, 1 }, null, new object[] { 0, 1 }, 1, 1);
            AssertMatches(Generex.Ors((IEnumerable <Generex <int> >) new[] { _g, _gn }), _input, True, False, False, False, True, new object[] { 0, 1 }, null, new object[] { 0, 1 }, 1, 1);
            AssertMatches(Generex.Ors(_gr, _gnr), _input, True, False, False, False, True, new object[] { 0, 1, 1 }, null, new object[] { 0, 1, 1 }, 1, 1);
            AssertMatches(Generex.Ors((IEnumerable <Generex <int, int> >) new[] { _gr, _gnr }), _input, True, False, False, False, True, new object[] { 0, 1, 1 }, null, new object[] { 0, 1, 1 }, 1, 1);

            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((Stringerex)null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((Stringerex[])null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((IEnumerable <Stringerex>)null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((Stringerex <int>)null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((Stringerex <int>[])null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.Ors((IEnumerable <Stringerex <int> >)null); });
            AssertMatches(Generex.Ors(_s, _sn), "MLP", True, False, False, False, True, new object[] { 0, 1 }, null, new object[] { 0, 1 }, 1, 1);
            AssertMatches(Generex.Ors((IEnumerable <Stringerex>) new[] { _s, _sn }), "MLP", True, False, False, False, True, new object[] { 0, 1 }, null, new object[] { 0, 1 }, 1, 1);
            AssertMatches(Generex.Ors(_sr, _snr), "MLP", True, False, False, False, True, new object[] { 0, 1, 1 }, null, new object[] { 0, 1, 1 }, 1, 1);
            AssertMatches(Generex.Ors((IEnumerable <Stringerex <int> >) new[] { _sr, _snr }), "MLP", True, False, False, False, True, new object[] { 0, 1, 1 }, null, new object[] { 0, 1, 1 }, 1, 1);

            Assert.Throws <ArgumentNullException>(() => { Stringerex.Ors((Stringerex)null); });
            Assert.Throws <ArgumentNullException>(() => { Stringerex.Ors((Stringerex[])null); });
            Assert.Throws <ArgumentNullException>(() => { Stringerex.Ors((IEnumerable <Stringerex>)null); });
            Assert.Throws <ArgumentNullException>(() => { Stringerex <int> .Ors((Stringerex <int>)null); });
            Assert.Throws <ArgumentNullException>(() => { Stringerex <int> .Ors((Stringerex <int>[])null); });
            Assert.Throws <ArgumentNullException>(() => { Stringerex <int> .Ors((IEnumerable <Stringerex <int> >)null); });
            AssertMatches(Stringerex.Ors(_s, _sn), "MLP", True, False, False, False, True, new object[] { 0, 1 }, null, new object[] { 0, 1 }, 1, 1);
            AssertMatches(Stringerex.Ors((IEnumerable <Stringerex>) new[] { _s, _sn }), "MLP", True, False, False, False, True, new object[] { 0, 1 }, null, new object[] { 0, 1 }, 1, 1);
            AssertMatches(Stringerex <int> .Ors(_sr, _snr), "MLP", True, False, False, False, True, new object[] { 0, 1, 1 }, null, new object[] { 0, 1, 1 }, 1, 1);
            AssertMatches(Stringerex <int> .Ors((IEnumerable <Stringerex <int> >) new[] { _sr, _snr }), "MLP", True, False, False, False, True, new object[] { 0, 1, 1 }, null, new object[] { 0, 1, 1 }, 1, 1);
        }
Пример #2
0
        public void TestToStringerex()
        {
            var ch = new Generex <char>('M').Then('L');

            AssertMatches(ch.ToStringerex(), "MLP", True, False, True, False, True, new object[] { 0, 2 }, null, new object[] { 0, 2 }, 1, 1);
            AssertMatches(ch.Process(m => string.Join(",", m.Match)).ToStringerex(), "MLP", True, False, True, False, True, new object[] { 0, 2, "M,L" }, null, new object[] { 0, 2, "M,L" }, 1, 1);
        }
Пример #3
0
        public void TestAnyOfType()
        {
            var input   = new Exception[] { new ArgumentException(), new ArgumentNullException(), new InvalidOperationException() };
            var generex = Generex <Exception> .AnyOfType <ArgumentException>();

            AssertMatches(generex, input, True, True, True, False, True, new object[] { 0, 1, input[0] }, null, new object[] { 1, 1, input[1] }, 2, 2);
        }
Пример #4
0
 public void TestNot()
 {
     Assert.Throws <ArgumentNullException>(() => { Generex.Not <int>(elements: null); });
     Assert.Throws <ArgumentNullException>(() => { Generex.Not <int>(comparer: null, elements: new[] { 1 }); });
     Assert.Throws <ArgumentNullException>(() => { Generex.Not <int>(comparer: _mod7, elements: null); });
     AssertMatches(Generex.Not(0, 1, 2, 3), _input, True, True, True, False, True, new object[] { 0, 1 }, null, new object[] { 1, 1 }, 2, 2);
     AssertMatches(Generex.Not(_mod7, 0, 1, 2, 3), _input, True, False, True, False, True, new object[] { 0, 1 }, null, new object[] { 0, 1 }, 1, 1);
 }
Пример #5
0
 public void TestNew()
 {
     Assert.Throws <ArgumentNullException>(() => { Generex.New <int>(elements: null); });
     Assert.Throws <ArgumentNullException>(() => { Generex.New <int>(generexes: null); });
     Assert.Throws <ArgumentNullException>(() => { Generex.New <int>(predicate: null); });
     Assert.Throws <ArgumentNullException>(() => { Generex.New <int>(comparer: null, elements: new[] { 1 }); });
     Assert.Throws <ArgumentNullException>(() => { Generex.New <int>(comparer: _mod7, elements: null); });
     AssertMatches(Generex.New(47, 24567837), _input, True, False, False, True, True, new object[] { 0, 2 }, new object[] { 0, 2 }, new object[] { 0, 2 }, 1, 1);
     AssertMatches(Generex.New(new Generex <int>(i => i % 7 == 5), new Generex <int>(i => i % 7 == 0)), _input, True, False, False, True, True, new object[] { 0, 2 }, new object[] { 0, 2 }, new object[] { 0, 2 }, 1, 1);
     AssertMatches(Generex.New <int>(i => i % 7 == 5), _input, True, False, True, False, True, new object[] { 0, 1 }, null, new object[] { 0, 1 }, 1, 1);
     AssertMatches(Generex.New(_mod7, 5, 0), _input, True, False, False, True, True, new object[] { 0, 2 }, new object[] { 0, 2 }, new object[] { 0, 2 }, 1, 1);
 }
Пример #6
0
        /// <summary>
        /// Set regular expression used to generate strings.
        /// </summary>
        /// <param name="regEx">Regular expression used to generate strings.</param>
        /// <returns>True if regular expression is valid, false otherwise.</returns>
        public bool SetRegEx(string regEx)
        {
            bool isValid = false;

            if (VerifyRegEx(regEx))
            {
                _generex = new Generex(regEx);
                // Using Generex iterator
                _iterator = _generex.iterator();
                isValid   = _iterator.hasNext();
            }

            return(isValid);
        }
Пример #7
0
        public void TestInAnyOrderWithResult()
        {
            Assert.Throws <ArgumentNullException>(() => { Generex.InAnyOrder <int, int>((Generex <int, int>[])null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.InAnyOrder <int, int>((IEnumerable <Generex <int, int> >)null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.InAnyOrder <int, int>((Generex <int, int>)null); });
            var iao1 = Generex.InAnyOrder(new Generex <int>(i => i % 7 == 5).Process(m => "Five"), new Generex <int>(i => i % 7 == 0).Process(m => "Zero"));
            var iao2 = Generex.InAnyOrder(new Generex <int>(i => i % 7 == 0).Process(m => "Zero"), new Generex <int>(i => i % 7 == 5).Process(m => "Five"));
            var iao3 = Generex.InAnyOrder((IEnumerable <Generex <int, string> >) new[] { new Generex <int>(i => i % 7 == 0).Process(m => "Zero"), new Generex <int>(i => i % 7 == 5).Process(m => "Five") });
            var iao4 = Generex.InAnyOrder((IEnumerable <Generex <int, string> >) new[] { new Generex <int>(i => i % 7 == 5).Process(m => "Five"), new Generex <int>(i => i % 7 == 0).Process(m => "Zero") });

            AssertMatches(iao1, _input, True, False, False, True, True, new object[] { 0, 2, new[] { "Five", "Zero" } }, new object[] { 0, 2, new[] { "Five", "Zero" } }, new object[] { 0, 2, new[] { "Five", "Zero" } }, 1, 1);
            AssertMatches(iao2, _input, True, False, False, True, True, new object[] { 0, 2, new[] { "Five", "Zero" } }, new object[] { 0, 2, new[] { "Five", "Zero" } }, new object[] { 0, 2, new[] { "Five", "Zero" } }, 1, 1);
            AssertMatches(iao3, _input, True, False, False, True, True, new object[] { 0, 2, new[] { "Five", "Zero" } }, new object[] { 0, 2, new[] { "Five", "Zero" } }, new object[] { 0, 2, new[] { "Five", "Zero" } }, 1, 1);
            AssertMatches(iao4, _input, True, False, False, True, True, new object[] { 0, 2, new[] { "Five", "Zero" } }, new object[] { 0, 2, new[] { "Five", "Zero" } }, new object[] { 0, 2, new[] { "Five", "Zero" } }, 1, 1);
        }
Пример #8
0
        public void TestInAnyOrderNoResult()
        {
            Assert.Throws <ArgumentNullException>(() => { Generex.InAnyOrder <int>(elements: null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.InAnyOrder <int>(generexes: null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.InAnyOrder <int>((Generex <int>)null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.InAnyOrder <int>(comparer: null, elements: new int[] { 47 }); });
            Assert.Throws <ArgumentNullException>(() => { Generex.InAnyOrder <int>(comparer: EqualityComparer <int> .Default, elements: null); });

            AssertMatches(Generex.InAnyOrder(47, 24567837), _input, True, False, False, True, True, new object[] { 0, 2 }, new object[] { 0, 2 }, new object[] { 0, 2 }, 1, 1);
            AssertMatches(Generex.InAnyOrder(24567837, 47), _input, True, False, False, True, True, new object[] { 0, 2 }, new object[] { 0, 2 }, new object[] { 0, 2 }, 1, 1);
            AssertMatches(Generex.InAnyOrder(new Generex <int>(i => i % 7 == 5), new Generex <int>(i => i % 7 == 0)), _input, True, False, False, True, True, new object[] { 0, 2 }, new object[] { 0, 2 }, new object[] { 0, 2 }, 1, 1);
            AssertMatches(Generex.InAnyOrder(new Generex <int>(i => i % 7 == 0), new Generex <int>(i => i % 7 == 5)), _input, True, False, False, True, True, new object[] { 0, 2 }, new object[] { 0, 2 }, new object[] { 0, 2 }, 1, 1);
            AssertMatches(Generex.InAnyOrder(_mod7, 5, 0), _input, True, False, False, True, True, new object[] { 0, 2 }, new object[] { 0, 2 }, new object[] { 0, 2 }, 1, 1);
            AssertMatches(Generex.InAnyOrder(_mod7, 0, 5), _input, True, False, False, True, True, new object[] { 0, 2 }, new object[] { 0, 2 }, new object[] { 0, 2 }, 1, 1);
        }
        public int Initialize(ref string errorMsg)
        {
            int result = Constants.Failure;

            RegExIterator = null;
            Progress      = 0;

            try
            {
                ClearAllErrors();
                if (string.IsNullOrEmpty(RegEx) || RegEx.Trim().Length == 0)
                {
                    errorMsg = "The specified Regex is empty.";
                    SetErrors("RegEx", new List <ValidationResult>()
                    {
                        new ValidationResult(false, errorMsg)
                    });
                }
                else
                {
                    Generex  generex  = new Generex(RegEx);
                    Iterator iterator = generex.iterator();
                    if (!iterator.hasNext())
                    {
                        errorMsg = "Unable to generate strings that matches the specified Regex.";
                        SetErrors("RegEx", new List <ValidationResult>()
                        {
                            new ValidationResult(false, errorMsg)
                        });
                    }
                    else
                    {
                        RegExIterator = generex.iterator();
                        result        = Constants.Success;
                    }
                }
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
                SetErrors("RegEx", new List <ValidationResult>()
                {
                    new ValidationResult(false, errorMsg)
                });
            }

            return(result);
        }
Пример #10
0
        public void TestRawMatchNullable()
        {
            // RawMatchNullable, RawMatchReverseNullable
            var g = Generex.New(47).Process(m => 1);

            Assert.AreEqual(1, g.RawMatchNullable(_input));
            Assert.AreEqual(null, g.RawMatchNullable(_input, 1));
            Assert.AreEqual(1, g.RawMatchReverseNullable(_input));
            Assert.AreEqual(1, g.RawMatchReverseNullable(_input, 1));
            Assert.AreEqual(null, g.RawMatchReverseNullable(_input, 0));
            var s = new Stringerex('M').Process(m => 1);

            Assert.AreEqual(1, s.RawMatchNullable("MLP"));
            Assert.AreEqual(null, s.RawMatchNullable("MLP", 1));
            Assert.AreEqual(1, s.RawMatchReverseNullable("MLP"));
            Assert.AreEqual(1, s.RawMatchReverseNullable("MLP", 1));
            Assert.AreEqual(null, s.RawMatchReverseNullable("MLP", 0));
        }
Пример #11
0
        public void TestOrDefault()
        {
            // OrDefault, OrDefaultGreedy, OrNull, OrNullGreedy
            Assert.Throws <ArgumentNullException>(() => { Generex.OrDefault <int, int>(inner: null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.OrDefaultGreedy <int, int>(inner: null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.OrNull <int, int>(inner: null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.OrNullGreedy <int, int>(inner: null); });

            Assert.Throws <ArgumentNullException>(() => { Generex.OrDefault <int>(inner: null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.OrDefaultGreedy <int>(inner: null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.OrNull <int>(inner: null); });
            Assert.Throws <ArgumentNullException>(() => { Generex.OrNullGreedy <int>(inner: null); });

            AssertMatches(_gr.OrDefault(), _input, True, True, True, False, True, new object[] { 0, 0, 0 }, null, new object[] { 3, 0, 0 }, 4, 4);
            AssertMatches(_gr.OrDefaultGreedy(), _input, True, True, True, False, True, new object[] { 0, 1, 1 }, null, new object[] { 3, 0, 0 }, 4, 4);
            AssertMatches(_gr.OrNull(), _input, True, True, True, False, True, new object[] { 0, 0, null }, null, new object[] { 3, 0, null }, 4, 4);
            AssertMatches(_gr.OrNullGreedy(), _input, True, True, True, False, True, new object[] { 0, 1, 1 }, null, new object[] { 3, 0, null }, 4, 4);

            AssertMatches(_sr.OrDefault(), "MLP", True, True, True, False, True, new object[] { 0, 0, 0 }, null, new object[] { 3, 0, 0 }, 4, 4);
            AssertMatches(_sr.OrDefaultGreedy(), "MLP", True, True, True, False, True, new object[] { 0, 1, 1 }, null, new object[] { 3, 0, 0 }, 4, 4);
            AssertMatches(_sr.OrNull(), "MLP", True, True, True, False, True, new object[] { 0, 0, null }, null, new object[] { 3, 0, null }, 4, 4);
            AssertMatches(_sr.OrNullGreedy(), "MLP", True, True, True, False, True, new object[] { 0, 1, 1 }, null, new object[] { 3, 0, null }, 4, 4);
        }
Пример #12
0
        static int Main(string[] args)
        {
            try { Console.OutputEncoding = Encoding.UTF8; }
            catch { }

            if (args.Length == 2 && args[0] == "--post-build-check")
            {
                return(Ut.RunPostBuildChecks(args[1], typeof(Program).Assembly, typeof(Ut).Assembly));
            }

            CommandLine cmd;

            try
            {
                cmd = CommandLineParser.Parse <CommandLine>(args);
            }
            catch (CommandLineParseException pe)
            {
                pe.WriteUsageInfoToConsole();
                return(1);
            }

            string extraCode = null;

            if (cmd.ExtraCodeFile != null)
            {
                extraCode = File.ReadAllText(cmd.ExtraCodeFile);
            }

            var gp = cmd.GetGraphicsPath();

            Ut.Assert(gp.PathTypes.Length == gp.PathPoints.Length);
            var data = gp.PathTypes.Zip(gp.PathPoints, (pt, pp) => new { Type = (PathPointType)pt, Point = pp }).ToArray();

            Ut.Assert(data.Length > 0 && data[0].Type == PathPointType.Start);
            for (int i = 0; i < data.Length; i++)
            {
                if (data[i].Type == PathPointType.Start)
                {
                    Ut.Assert(i == 0 || data[i - 1].Type.HasFlag(PathPointType.CloseSubpath));
                }
                if (data[i].Type.HasFlag(PathPointType.CloseSubpath))
                {
                    Ut.Assert(i == data.Length - 1 || data[i + 1].Type == PathPointType.Start);
                }
            }

            var start  = data.CreateGenerex(inf => inf.Type == PathPointType.Start);
            var line   = data.CreateGenerex(inf => (inf.Type & PathPointType.PathTypeMask) == PathPointType.Line);
            var bézier = data.CreateGenerex(inf => (inf.Type & PathPointType.PathTypeMask) == PathPointType.Bezier3);
            var end    = data.CreateGenerex(inf => inf.Type.HasFlag(PathPointType.CloseSubpath));

            var regex = start.Process(m => m.Match[0].Point).ThenRaw(
                Generex.Ors(
                    line.Process(m => SegmentType.Line),
                    bézier.Times(3).Process(m => SegmentType.Bézier)
                    )
                .Process(m => new { Type = m.Result, Points = m.Match.Select(inf => inf.Point).ToArray() })
                .Repeat()
                .Then(end.LookBehind())
                .Do(m => m.Match.SkipLast(1).All(inf => !inf.Type.HasFlag(PathPointType.CloseSubpath))),
                (startPoint, segment) => new { StartPoint = startPoint, Segment = segment }
                ).RepeatGreedy();

            var match = regex.MatchExact(data);

            if (match == null)
            {
                throw new InvalidOperationException("The GraphicsPath data has not been recognized by Generex. This indicates a bug in the Generex grammar.");
            }

            var curves = match.Result.ToArray();

            var getPoints = Ut.Lambda((bool close) => curves.Select(curve =>
            {
                var segments = curve.Segment.Concat(new { Type = SegmentType.Line, Points = new[] { curve.StartPoint } });
                if (close)
                {
                    segments = segments.Concat(curve.Segment.First());
                }
                return(segments.Aggregate(
                           new { LastPoint = curve.StartPoint, Points = Enumerable.Empty <PointF>() },
                           (prev, next) =>
                           next.Type == SegmentType.Line && next.Points[0] == prev.LastPoint ? prev :
                           new
                {
                    LastPoint = next.Points.Last(),
                    Points = prev.Points.Concat(next.Type == SegmentType.Bézier
                                ? prev.LastPoint.Concat(next.Points)
                                : new[] { prev.LastPoint, new PointF(prev.LastPoint.X * 2 / 3 + next.Points[0].X * 1 / 3, prev.LastPoint.Y * 2 / 3 + next.Points[0].Y * 1 / 3), new PointF(prev.LastPoint.X * 1 / 3 + next.Points[0].X * 2 / 3, prev.LastPoint.Y * 1 / 3 + next.Points[0].Y * 2 / 3), next.Points[0] }
                                                )
                }
                           ).Points.ToArray());
            }).ToArray());
            var openPoints   = getPoints(false);
            var closedPoints = getPoints(true);

            var source = @"
// Command line:
// {4}{5}

#declare {3} = union {{
    prism {{
        bezier_spline linear_sweep 0, {0}, {1}
        {2}
        rotate 90*x
        {6}
    }}".Fmt(
                /* {0} */ cmd.ExtrusionDepth,
                /* {1} */ openPoints.Sum(p => p.Length),
                /* {2} */ openPoints.SelectMany(p => p).Split(4)
                .Select(grp => grp.Select(p => "<{0}, {1}>".Fmt(p.X, p.Y)).JoinString(", "))
                .JoinString("," + Environment.NewLine)
                .Indent(8, false),
                /* {3} */ cmd.ObjectName,
                /* {4} */ CommandRunner.ArgsToCommandLine(args),
                /* {5} */ cmd.ExtraCodeFile == null ? null : "\n// Extra code file:\n" + File.ReadAllLines(cmd.ExtraCodeFile).JoinString("\n", prefix: "// "),
                /* {6} */ extraCode
                );

            source += openPoints.SelectMany(gr => gr.Split(4).Select(Enumerable.ToArray).ConsecutivePairs(true)).Select(pair =>
            {
                var pts  = pair.Item1;
                var next = pair.Item2;

                var displaced     = displace(pts, cmd.RoundingRadius);
                var displacedNext = displace(next, cmd.RoundingRadius);

                var code = "";

                if (!cmd.SkipFront)
                {
                    code += patch(
                        "Front fillet",
                        Enumerable.Range(0, 4).Select(i => pts[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, 0)).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => combine(pts[i], displaced[i], cmd.RoundingFactor)).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, 0)).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => displaced[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, (-cmd.RoundingRadius) * (1 - cmd.RoundingFactor))).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => displaced[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, (-cmd.RoundingRadius))).JoinString(", "),
                        extraCode, cmd.Smoothness);
                }

                code += patch(
                    "Side",
                    displaced.Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, -cmd.RoundingRadius)).JoinString(", "),
                    displaced.Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, -cmd.RoundingRadius * 1 / 3 - cmd.ExtrusionDepth * 1 / 3)).JoinString(", "),
                    displaced.Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, cmd.RoundingRadius * 1 / 3 - cmd.ExtrusionDepth * 2 / 3)).JoinString(", "),
                    displaced.Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, cmd.RoundingRadius - cmd.ExtrusionDepth)).JoinString(", "),
                    extraCode, cmd.Smoothness);

                if (!cmd.SkipBack)
                {
                    code += patch(
                        "Back fillet",
                        Enumerable.Range(0, 4).Select(i => displaced[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, cmd.RoundingRadius - cmd.ExtrusionDepth)).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => displaced[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, combine(cmd.RoundingRadius - cmd.ExtrusionDepth, -cmd.ExtrusionDepth, cmd.RoundingFactor))).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => combine(pts[i], displaced[i], cmd.RoundingFactor)).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, -cmd.ExtrusionDepth)).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => pts[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, -cmd.ExtrusionDepth)).JoinString(", "),
                        extraCode, cmd.Smoothness);
                }

                Ut.Assert(pts[3] == next[0]);

                var angle1     = Math.Atan2(pts[3].Y - pts[2].Y, pts[3].X - pts[2].X);
                var angle2     = Math.Atan2(next[0].Y - next[1].Y, next[0].X - next[1].X);
                var totalAngle = angle1 - angle2;
                if (totalAngle < 0)
                {
                    totalAngle += Math.PI * 2;
                }
                if (totalAngle >= Math.PI)
                {
                    return(code);
                }

                var intersection = intersect(displaced[2], displaced[3], displacedNext[1], displacedNext[0]);
                if (float.IsNaN(intersection.X))
                {
                    return(code);
                }

                var fillet = Ut.NewArray(
                    displaced[3],
                    combine(displaced[3], intersection, cmd.RoundingFactor),
                    combine(displacedNext[0], intersection, cmd.RoundingFactor),
                    displacedNext[0]
                    );

                if (!cmd.SkipFront)
                {
                    code += patch(
                        "Front corner fillet",
                        Enumerable.Range(0, 4).Select(i => next[0]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, 0)).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => combine(next[0], fillet[i], cmd.RoundingFactor)).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, 0)).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => fillet[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, -cmd.RoundingRadius * (1 - cmd.RoundingFactor))).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => fillet[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, -cmd.RoundingRadius)).JoinString(", "),
                        extraCode, cmd.Smoothness);
                }

                code += patch(
                    "Side corner fillet",
                    Enumerable.Range(0, 4).Select(i => fillet[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, -cmd.RoundingRadius)).JoinString(", "),
                    Enumerable.Range(0, 4).Select(i => fillet[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, combine(-cmd.RoundingRadius, cmd.RoundingRadius - cmd.ExtrusionDepth, 1f / 3f))).JoinString(", "),
                    Enumerable.Range(0, 4).Select(i => fillet[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, combine(-cmd.RoundingRadius, cmd.RoundingRadius - cmd.ExtrusionDepth, 2f / 3f))).JoinString(", "),
                    Enumerable.Range(0, 4).Select(i => fillet[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, cmd.RoundingRadius - cmd.ExtrusionDepth)).JoinString(", "),
                    extraCode, cmd.Smoothness);

                if (!cmd.SkipBack)
                {
                    code += patch(
                        "Back corner fillet",
                        Enumerable.Range(0, 4).Select(i => fillet[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, cmd.RoundingRadius - cmd.ExtrusionDepth)).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => fillet[i]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, combine(cmd.RoundingRadius - cmd.ExtrusionDepth, -cmd.ExtrusionDepth, cmd.RoundingFactor))).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => combine(next[0], fillet[i], cmd.RoundingFactor)).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, -cmd.ExtrusionDepth)).JoinString(", "),
                        Enumerable.Range(0, 4).Select(i => next[0]).Select(p => "<{0}, {1}, {2}>".Fmt(p.X, p.Y, -cmd.ExtrusionDepth)).JoinString(", "),
                        extraCode, cmd.Smoothness);
                }

                return(code);
            }).JoinString(Environment.NewLine);

            source += Environment.NewLine + "}" + Environment.NewLine;

            File.WriteAllText(cmd.Filename, source);

            return(0);
        }
Пример #13
0
 public void TestToStringerex()
 {
     var ch = new Generex<char>('M').Then('L');
     AssertMatches(ch.ToStringerex(), "MLP", True, False, True, False, True, new object[] { 0, 2 }, null, new object[] { 0, 2 }, 1, 1);
     AssertMatches(ch.Process(m => string.Join(",", m.Match)).ToStringerex(), "MLP", True, False, True, False, True, new object[] { 0, 2, "M,L" }, null, new object[] { 0, 2, "M,L" }, 1, 1);
 }