Esempio n. 1
0
            static Shape <double> CreatePathShape(ConvolutionFactory factory)
            {
                var pathSegment = factory.CreateSegment(
                    startX: 0.0, startY: 0.0,
                    endX: 1.0, endY: 2.0,
                    weight: 1);

                var pathReverseSegment = factory.CreateSegment(
                    startX: 1.0, startY: 2.0,
                    endX: 0.0, endY: 0.0,
                    weight: 1);

                var smoothingArc1 = factory.CreateArc(
                    pathSegment.Start,
                    factory.CreateDirectionRange(
                        pathReverseSegment.Direction.NormalDirection().Opposite(),
                        pathSegment.Direction.NormalDirection().Opposite(),
                        Orientation.CounterClockwise),
                    0.0,
                    1);

                var smoothingArc2 = factory.CreateArc(
                    pathReverseSegment.Start,
                    factory.CreateDirectionRange(
                        pathSegment.Direction.NormalDirection().Opposite(),
                        pathReverseSegment.Direction.NormalDirection().Opposite(),
                        Orientation.CounterClockwise),
                    0.0,
                    1);

                return(factory.CreateShape(
                           new Tracing <double>[]
                           { smoothingArc1, pathSegment, smoothingArc2, pathReverseSegment }));
            }
        public void When_Start_Direction_Coincide_With_End_Then_Expected_Range_Should_Be_Returned(
            Orientation orientation,
            IEnumerable <DirectionRange <double> > expectedRange)
        {
            var factory = new ConvolutionFactory();

            var d1 = factory.CreateDirection(0.0, 1.0);
            var d2 = factory.CreateDirection(0.0, -1.0);

            var leftHalfPlan  = factory.CreateDirectionRange(d1, d2, Orientation.Clockwise);
            var rightHalfPlan = factory.CreateDirectionRange(d1, d2, orientation);

            var intersection = leftHalfPlan.Intersection(rightHalfPlan);

            intersection.Should().BeEquivalentTo(expectedRange);
        }
        public void IsShortestRange_Should_Return_Expected_Result()
        {
            var factory = new ConvolutionFactory();

            var d1 = factory.CreateDirection(1.0, 0.0);
            var d2 = factory.CreateDirection(0.0, 1.0);

            var range1 = factory.CreateDirectionRange(d1, d2, Orientation.CounterClockwise);
            var range2 = factory.CreateDirectionRange(d1, d2, Orientation.Clockwise);
            var range3 = factory.CreateDirectionRange(d2, d1, Orientation.CounterClockwise);
            var range4 = factory.CreateDirectionRange(d2, d1, Orientation.Clockwise);

            range1.IsShortestRange().Should().BeTrue();
            range2.IsShortestRange().Should().BeFalse();
            range3.IsShortestRange().Should().BeFalse();
            range4.IsShortestRange().Should().BeTrue();
        }
        public void When_Direction_Range_Is_Exactly_A_Half_Plan_Then_The_Shortest_Range_Should_Be_Clockwise()
        {
            var factory = new ConvolutionFactory();

            var d1 = factory.CreateDirection(1.0, 0.0);
            var d2 = factory.CreateDirection(-1.0, 0.0);

            var shortestRange1 = factory.CreateDirectionRange(d1, d2, Orientation.Clockwise);
            var shortestRange2 = factory.CreateDirectionRange(d2, d1, Orientation.Clockwise);

            var longestRange1 = factory.CreateDirectionRange(d1, d2, Orientation.CounterClockwise);
            var longestRange2 = factory.CreateDirectionRange(d2, d1, Orientation.CounterClockwise);

            shortestRange1.IsShortestRange().Should().BeTrue();
            shortestRange2.IsShortestRange().Should().BeTrue();

            longestRange1.IsShortestRange().Should().BeFalse();
            longestRange2.IsShortestRange().Should().BeFalse();
        }
Esempio n. 5
0
        public void When_Direction_Is_Given_Then_BelongsTo_Should_Return_Expected_Result()
        {
            var factory = new ConvolutionFactory();

            var east  = factory.CreateDirection(1.0, 0.0);
            var north = factory.CreateDirection(0.0, 1.0);

            var clockwiseRange        = factory.CreateDirectionRange(east, north, Orientation.Clockwise);
            var counterClockwiseRange = factory.CreateDirectionRange(east, north, Orientation.CounterClockwise);

            var northEast = factory.CreateDirection(1.0, 1.0);
            var southWest = northEast.Opposite();

            northEast.BelongsTo(clockwiseRange).Should().BeFalse();
            northEast.BelongsTo(counterClockwiseRange).Should().BeTrue();

            southWest.BelongsTo(clockwiseRange).Should().BeTrue();
            southWest.BelongsTo(counterClockwiseRange).Should().BeFalse();
        }
Esempio n. 6
0
            static Shape <double> CreateDiskShape(ConvolutionFactory factory)
            {
                var eastDirection = factory.CreateDirection(1.0, 0.0);

                var diskArc = factory.CreateArc(
                    center: factory.CreatePoint(0.0, 0.0),
                    directions: factory.CreateDirectionRange(eastDirection, eastDirection, Orientation.CounterClockwise),
                    radius: 1.0,
                    weight: 1);

                return(factory.CreateShape(new[] { diskArc }));
            }
        public void When_calling_Union_with_a_single_element_then_it_should_be_returned()
        {
            // Arrange
            var factory = new ConvolutionFactory();

            var d1     = factory.CreateDirection(1.0, 0.0);
            var d2     = factory.CreateDirection(0.0, 1.0);
            var range  = factory.CreateDirectionRange(d1, d2, Orientation.CounterClockwise);
            var ranges = new[] { range };

            // Act
            var union = ranges.Union();

            // Assert
            union.Single().Should().Be(range);
        }
Esempio n. 8
0
        public static IEnumerable <TestCaseData> TestCases()
        {
            var factory = new ConvolutionFactory();

            yield return(new TestCaseData(
                             Orientation.CounterClockwise, Enumerable.Empty <DirectionRange <double> >()));

            yield return(new TestCaseData(
                             Orientation.Clockwise,
                             new []
            {
                factory.CreateDirectionRange(
                    factory.CreateDirection(0, 1),
                    factory.CreateDirection(0, -1),
                    Orientation.Clockwise)
            }));
        }
Esempio n. 9
0
        public void When_Direction_Is_Close_To_Range_Boundary_Then_BelongsTo_Should_Return_Expected_Result()
        {
            var factory = new ConvolutionFactory();

            var northEast = factory.CreateDirection(1.0, 1.0);
            var northWest = factory.CreateDirection(-1.0, 1.0);

            var range = factory.CreateDirectionRange(northEast, northWest, Orientation.CounterClockwise);

            const double perturbation = 1.0e-2;

            var d1 = factory.CreateDirection(1.0 + perturbation, 1.0);

            var d2 = factory.CreateDirection(1.0 - perturbation, 1.0);

            var d3 = factory.CreateDirection(-1.0 + perturbation, 1.0);

            var d4 = factory.CreateDirection(-1.0 - perturbation, 1.0);

            d1.BelongsTo(range).Should().BeFalse();
            d2.BelongsTo(range).Should().BeTrue();
            d3.BelongsTo(range).Should().BeTrue();
            d4.BelongsTo(range).Should().BeFalse();
        }
Esempio n. 10
0
        private static TestCaseData MultipleUnionCase01()
        {
            var range1 = _factory.CreateDirectionRange(
                _factory.CreateDirection(x: 1.0, y: 0.0),
                _factory.CreateDirection(x: 0.0, y: 1.0),
                Orientation.CounterClockwise);
            var range2 = _factory.CreateDirectionRange(
                _factory.CreateDirection(x: 1.0, y: 1.0),
                _factory.CreateDirection(x: -1.0, y: 1.0),
                Orientation.CounterClockwise);
            var range3 = _factory.CreateDirectionRange(
                _factory.CreateDirection(x: -1.0, y: 0.0),
                _factory.CreateDirection(x: 0.0, y: -1.0),
                Orientation.CounterClockwise);
            var range4 = _factory.CreateDirectionRange(
                _factory.CreateDirection(x: -1.0, y: -1.0),
                _factory.CreateDirection(x: 1.0, y: -1.0),
                Orientation.CounterClockwise);
            var range5 = _factory.CreateDirectionRange(
                _factory.CreateDirection(x: 7.0, y: -3.0),
                _factory.CreateDirection(x: -0.1, y: 1.0),
                Orientation.CounterClockwise);

            var ranges = new List <DirectionRange <double> > {
                range1, range2, range3, range4, range5
            };

            var union1 = _factory.CreateDirectionRange(
                _factory.CreateDirection(x: -1.0, y: 0.0),
                _factory.CreateDirection(x: 1.0, y: -1.0),
                Orientation.CounterClockwise);
            var union2 = _factory.CreateDirectionRange(
                _factory.CreateDirection(x: 7.0, y: -3.0),
                _factory.CreateDirection(x: -1.0, y: 1.0),
                Orientation.CounterClockwise);

            var expected = new List <DirectionRange <double> > {
                union1, union2
            };

            return(new TestCaseData(ranges, expected)
                   .SetName($"{nameof(DirectionRangeExtensionsUnionTestCaseDataSource)} - {nameof(MultipleUnionCase01)}"));
        }
Esempio n. 11
0
        public void When_Two_Shapes_Are_Convolved_Then_Parent1_Should_Belong_To_Shape1_And_Parent2_Should_Belong_To_Shape2()
        {
            // Arrange
            var shape1 = createShape1();
            var shape2 = createShape2();

            // Act
            var convolution = _factory.ConvolveShapes(shape1, shape2);

            // Assert
            convolution.ConvolvedTracings.ToList()
            .ForEach(convolvedTracing =>
                     shape1.Tracings.Contains(convolvedTracing.Parent1).Should().BeTrue());

            convolution.ConvolvedTracings.ToList()
            .ForEach(convolvedTracing =>
                     shape2.Tracings.Contains(convolvedTracing.Parent2).Should().BeTrue());

            Shape <double> createShape1()
            {
                var weight = new Fraction(1, 2);

                var d1 = _factory.CreateDirection(-1, 2);
                var d2 = _factory.CreateDirection(-1, -2);
                var d3 = _factory.CreateDirection(2, 0);

                var c1 = _factory.CreatePoint(1, 0);
                var c2 = _factory.CreatePoint(0, 2);
                var c3 = _factory.CreatePoint(-1, 0);

                var range1 = _factory.CreateDirectionRange(
                    d3.NormalDirection().Opposite(),
                    d1.NormalDirection().Opposite(),
                    Orientation.CounterClockwise);

                var range2 = _factory.CreateDirectionRange(
                    d1.NormalDirection().Opposite(),
                    d2.NormalDirection().Opposite(),
                    Orientation.CounterClockwise);

                var range3 = _factory.CreateDirectionRange(
                    d2.NormalDirection().Opposite(),
                    d3.NormalDirection().Opposite(),
                    Orientation.CounterClockwise);

                var arc1 = _factory.CreateArc(c1, range1, 1.0, weight);
                var arc2 = _factory.CreateArc(c2, range2, 1.0, weight);
                var arc3 = _factory.CreateArc(c3, range3, 1.0, weight);

                var segment1 = _factory.CreateSegment(arc1.End, arc2.Start, weight);
                var segment2 = _factory.CreateSegment(arc2.End, arc3.Start, weight);
                var segment3 = _factory.CreateSegment(arc3.End, arc1.Start, weight);

                return(_factory.CreateShape(
                           new Tracing <double>[]
                {
                    arc1, segment1, arc2, segment2, arc3, segment3
                }));
            }

            Shape <double> createShape2()
            {
                var weight = new Fraction(1, 2);

                var d1 = _factory.CreateDirection(0, 3);
                var d2 = _factory.CreateDirection(-3, 0);
                var d3 = _factory.CreateDirection(0, -3);
                var d4 = _factory.CreateDirection(3, 0);

                var c1 = _factory.CreatePoint(3, 0);
                var c2 = _factory.CreatePoint(3, 3);
                var c3 = _factory.CreatePoint(0, 3);
                var c4 = _factory.CreatePoint(0, 0);

                var range1 = _factory.CreateDirectionRange(
                    d4.NormalDirection().Opposite(),
                    d1.NormalDirection().Opposite(),
                    Orientation.CounterClockwise);

                var range2 = _factory.CreateDirectionRange(
                    d1.NormalDirection().Opposite(),
                    d2.NormalDirection().Opposite(),
                    Orientation.CounterClockwise);

                var range3 = _factory.CreateDirectionRange(
                    d2.NormalDirection().Opposite(),
                    d3.NormalDirection().Opposite(),
                    Orientation.CounterClockwise);

                var range4 = _factory.CreateDirectionRange(
                    d3.NormalDirection().Opposite(),
                    d4.NormalDirection().Opposite(),
                    Orientation.CounterClockwise);

                var arc1 = _factory.CreateArc(c1, range1, 1.0, weight);
                var arc2 = _factory.CreateArc(c2, range2, 1.0, weight);
                var arc3 = _factory.CreateArc(c3, range3, 1.0, weight);
                var arc4 = _factory.CreateArc(c4, range4, 1.0, weight);

                var segment1 = _factory.CreateSegment(arc1.End, arc2.Start, weight);
                var segment2 = _factory.CreateSegment(arc2.End, arc3.Start, weight);
                var segment3 = _factory.CreateSegment(arc3.End, arc4.Start, weight);
                var segment4 = _factory.CreateSegment(arc4.End, arc1.Start, weight);

                return(_factory.CreateShape(
                           new Tracing <double>[]
                {
                    arc1, segment1, arc2, segment2, arc3, segment3, arc4, segment4
                }));
            }
        }