Пример #1
0
        private static void DrawArmature(Armature2D armature, IReadOnlyList <float> solution, Vector2 target)
        {
            var builder = new SvgBuilder(10);

            builder.Outline(new[] {
                new Vector2(-50, -50),
                new Vector2(50, -50),
                new Vector2(50, 50),
                new Vector2(-50, 50),
            });

            var index           = 0;
            var pos             = Vector2.Zero;
            var parentTransform = Matrix4x4.Identity;

            foreach (var bone in armature.Bones)
            {
                builder.Circle(pos, 0.5f, "yellow");

                var localTransform = TriangulationSolver2D.CalculateLocalTransform(bone, ToRadians(solution[index]));
                parentTransform = localTransform * parentTransform;
                var end = Vector2.Transform(new Vector2(0, 0), parentTransform);
                index++;

                builder.Line(pos, end, 2, "red");

                pos = end;
            }

            builder.Circle(target, 0.5f, "blue");

            var str = Regex.Replace(builder.ToString(), "g transform=\"translate\\((.+?), (.+?)\\)\"", "g transform=\"translate(${1}, 100) scale(1, -1)\"");

            Console.WriteLine(str);
        }
Пример #2
0
        public void AssertThat_FromWorld_ToWorld_AreIdentityFunction()
        {
            //Generate a random load of points
            var rand   = new Random();
            var points = new List <Vector2>();

            for (int i = 0; i < 20; i++)
            {
                points.Add(rand.RandomNormalVector().XZ() * (float)rand.NextDouble() * 50);
            }

            //Calculate OABR
            var oabr = OABR.Fit(points);

            //Draw it
            var svg  = new SvgBuilder(2);
            var hull = points.ConvexHull().ToArray();

            svg.Outline(hull, "green");
            svg.Outline((IReadOnlyList <Vector2>)oabr.Points(new Vector2[4]));
            foreach (var vector2 in points)
            {
                svg.Circle(vector2, 1, oabr.Contains(vector2) ? "red" : "blue");
            }
            Console.WriteLine(svg.ToString());

            //Assert that every point lies within the bounds
            foreach (var vector2 in points)
            {
                var trans = oabr.FromWorld(vector2);
                var world = oabr.ToWorld(trans);

                Assert.IsTrue(vector2.TolerantEquals(world, 0.001f), string.Format("Expected {0} and {1} to be equal", vector2, world));
            }
        }
Пример #3
0
        public void AssertThat_AllPointsLiesInBounds_WithRandomShape()
        {
            //Generate a random load of points
            var rand   = new Random();
            var points = new List <Vector2>();

            for (int i = 0; i < 20; i++)
            {
                points.Add(rand.RandomNormalVector().XZ() * (float)rand.NextDouble() * 50);
            }

            //Calculate OABR
            var oabr = OABR.Fit(points);

            //Draw it
            var svg  = new SvgBuilder(2);
            var hull = points.ConvexHull().ToArray();

            svg.Outline(hull, "green");
            svg.Outline((IReadOnlyList <Vector2>)oabr.Points(new Vector2[4]));
            foreach (var vector2 in points)
            {
                svg.Circle(vector2, 1, oabr.Contains(vector2) ? "red" : "blue");
            }
            Console.WriteLine(svg.ToString());

            //Assert that every point lies within the bounds
            Assert.IsTrue(points.All(p => oabr.Contains(p)));
        }
Пример #4
0
        private static SvgBuilder Draw(IReadOnlyList <Section> sections, IReadOnlyList <IReadOnlyList <Vector2> > corners = null, IReadOnlyList <Vector2> outline = null, float scale = 30)
        {
            var svg = new SvgBuilder(scale);

            foreach (var section in sections)
            {
                svg.Outline(new[] {
                    section.Inner1, section.Inner2, section.Outer1, section.Outer2
                }, "black", "grey");
            }

            if (corners != null)
            {
                foreach (var corner in corners)
                {
                    svg.Outline(corner, "black", "darkgrey");
                }
            }

            //Draw corner sections vertices
            if (corners != null)
            {
                foreach (var corner in corners)
                {
                    foreach (var vertex in corner)
                    {
                        svg.Circle(vertex, 5f / scale, "black");
                    }
                }
            }

            //Draw wall section vertices
            foreach (var vertex in sections.SelectMany(s => new[] { s.Inner1, s.Inner2, s.Outer1, s.Outer2 }).Distinct())
            {
                svg.Circle(vertex, 5f / scale, "black");
            }

            //Draw outline
            if (outline != null)
            {
                svg.Outline(outline, "red");
            }

            return(svg);
        }
        private static SvgBuilder Draw <TA, TB, TC>(Mesh <TA, TB, TC> mesh, Func <TC, string> faceColor)
        {
            var svg = new SvgBuilder(10);

            foreach (var face in mesh.Faces)
            {
                svg.Outline(face.Vertices.Select(v => v.Position).ToArray(), stroke: "none", fill: faceColor(face.Tag));
            }
            foreach (var edge in mesh.HalfEdges.Where(a => a.IsPrimaryEdge))
            {
                svg.Line(edge.StartVertex.Position, edge.EndVertex.Position, 1, "black");
            }
            foreach (var vertex in mesh.Vertices)
            {
                svg.Circle(vertex.Position, 0.2f, "black");
            }
            return(svg);
        }
        static void Main(string[] args)
        {
            Stopwatch w = new Stopwatch();

            w.Start();

            var ssk = StraightSkeleton.Generate(new Vector2[] {
                new Vector2(-10, 10),
                new Vector2(-1, 10),
                new Vector2(0, 11),
                new Vector2(1, 10),
                new Vector2(10, 10),
                new Vector2(10, -2),
                new Vector2(5, 0),
                new Vector2(7, -10),
                new Vector2(-7, -10),
                new Vector2(-5, 0),
                new Vector2(-10, -2),
            }, new Vector2[][] {
                new Vector2[] {
                    new Vector2(2, 2),
                    new Vector2(2, -2),
                    new Vector2(-2, -2),
                    new Vector2(-2, 2)
                }
            });

            w.Stop();

            Stopwatch w2 = new Stopwatch();

            w2.Start();

            //Extract data from result
            var builder = new SvgBuilder(30);

            //Draw outline
            foreach (var edge in ssk.Borders)
            {
                builder.Circle(edge.Start.Position, 0.2f, "blue");
                builder.Circle(edge.End.Position, 0.2f, "blue");
                builder.Line(edge.Start.Position, edge.End.Position, 2, "blue");
            }

            //Draw offsets
            for (var i = 1; i < 10; i++)
            {
                var offset = ssk.Offset(i / 2f);
                foreach (var polygon in offset)
                {
                    builder.Outline(polygon, "green");
                }
            }

            //Draw spokes
            foreach (var edge in ssk.Spokes)
            {
                builder.Line(edge.Start.Position, edge.End.Position, 4, "lime");
            }

            //Draw straight skeleton
            foreach (var edge in ssk.Skeleton)
            {
                builder.Circle(edge.Start.Position, 0.2f, "hotpink");
                builder.Circle(edge.End.Position, 0.2f, "hotpink");
                builder.Line(edge.Start.Position, edge.End.Position, 2, "hotpink");
            }

            Console.WriteLine(builder);
            Console.Title = string.Format("Elapsed: {0}ms {1}ms", w.ElapsedMilliseconds, w2.ElapsedMilliseconds);
            Console.ReadLine();
        }