Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update samples and add to main solution #157

Merged
merged 8 commits into from
Aug 9, 2021
Merged

Conversation

JimBobSquarePants
Copy link
Member

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following matches the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

Updates the samples project to use the latest API and includes it in the solution so we don't forget about it in the future.

@JimBobSquarePants JimBobSquarePants added documentation Improvements or additions to documentation API labels Aug 2, 2021
@JimBobSquarePants JimBobSquarePants added this to the 1.0.0-rc.1 milestone Aug 2, 2021
@codecov
Copy link

codecov bot commented Aug 2, 2021

Codecov Report

Merging #157 (7bf8be5) into master (653cf5f) will increase coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #157      +/-   ##
==========================================
+ Coverage   69.87%   69.92%   +0.05%     
==========================================
  Files          88       88              
  Lines        5135     5144       +9     
  Branches     1048     1052       +4     
==========================================
+ Hits         3588     3597       +9     
  Misses       1337     1337              
  Partials      210      210              
Flag Coverage Δ
unittests 69.92% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/ImageSharp.Drawing/Shapes/PathExtensions.cs 61.90% <100.00%> (+28.57%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 653cf5f...7bf8be5. Read the comment docs.

Copy link
Member

@tocsoft tocsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally fine but looks like you have issue with the draw along path sample... looks like you have incorrectly changed the semantics.

{
WrappingWidth = path.Length,
WrappingWidth = path.Bounds.Width,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't the same thing.... (if i'm reading this right this is drawing along a path)

Imagine this path was a circle, the length of the circle in this case would be its circumference, whereas the width would just be its diameter meaning the text would only get partly around the outside before wrapping and not once it closes in itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Length isn’t a property anymore so how would I calculate it?

Copy link
Member

@antonfirsov antonfirsov Aug 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An approximation is a sum of distances between subsequent path.Flatten().Single().Points. (I assume Flatten returns a single ISimplePath for this, but I may be wrong.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without the limitation on single path...

static float GetLength(IPath path)
{
    float dist = 0;
    foreach (var s in path.Flatten())
    {
        var points = s.Points.Span;
        if (points.Length < 2)
        {
            // only a point
            continue;
        }

        for (var i = 1; i < points.Length; i++)
        {
            dist += Vector2.Distance(points[i - 1], points[i]);
        }

        if (s.IsClosed)
        {
            dist += Vector2.Distance(points[0], points[points.Length - 1]);
        }
    }

    return dist;
}

we should/could probably add this this as an extension on IPath, or re add it back to the IPath interface.

definitely makes the sample you threw into slack a little less broken.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless we had the implementations calculating the length analytically, this should be an extension method. I would name it .CalculateApproximateLength().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I recall there were issues calculating it as a property in ComplexPolygon. I’d rather avoid re-adding it to the interface and use an extension.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with ComputeLength() to match the DirectX method that does the same thing.

https://docs.microsoft.com/en-us/windows/win32/direct2d/id2d1geometry-computelength

public class ComputeLength
{
[Fact]
public void CanComputeUnrolledLength()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want a test that verifies the length calculates as expected for more complex paths too... i.e. any IPath where the Flatten() call returns multiple ISimplePaths

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked for and couldn’t find one in the codebase from before IPath.Length was removed to reuse. With nothing to verify against I’m just been guessing the result.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to need a little help here.

I used LottieSharp to calculate a polygon length since it gave me access to the original DX method and attempted to replicate a polygon to match another test one.

var p = new LottieSharp.Path();
p.MoveTo(15, 0);
p.LineTo(25, 0);
p.MoveTo(15, 30);
p.LineTo(25, 0);
p.Close();

using (var factory = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded))
{
	p.GetGeometry(factory).ComputeLength().Dump(); // 73.24555
}

Our test code.

[Fact]
public void CanComputeUnrolledLength()
{
    var pb = new PathBuilder();
    pb.AddLine(new Vector2(15, 0), new Vector2(25, 0))
        .AddLine(new Vector2(15, 30), new Vector2(25, 30))
        .CloseFigure();

    IPath p = pb.Build();
    Assert.Equal(73.24555, p.ComputeLength()); // 83.24555206298828
}

We seem to be calculating a value exactly 10 more than the DX. I couldn't figure out a way to convert their types to ours to verify the output.

Tbh though, I don't understand how either is getting that number. The path should be a rectangle of sides 10x30 which would make the length 80 shouldn't it?

SkiaSharp seems to give me junk. Not sure what I'm doing wrong there.

using var p = new SKPath();
p.MoveTo(15, 0);
p.LineTo(25, 0);
p.MoveTo(15, 30);
p.LineTo(25, 0);
p.Close();

var measure = new SKPathMeasure(p, false, 1);
measure.Length.Dump(); // 10

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel your overthinking this too much..... all you want is:

[Fact]
public void CanComputeUnrolledLengthComplexPath()
{
    var polygon = new ComplexPolygon(
        new RectangularPolygon(PointF.Empty, new PointF(100, 200)),
        new RectangularPolygon(new PointF(1000, 1000), new PointF(1100, 1200)));

    Assert.Equal(1200, polygon.ComputeLength());
}

this just ensures that Flatten() returns multilple ISimplePaths and that we sum the distances across all the ISimplePaths rather than just the first or any other logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw your use of path builder isn't the same in your 2 samples. we don't have a move line equivalent option, the 2nd add line call adds a line joining the end of the first and the start of the 2nd and then the CloseFigure joins the start and the end, which explains the confusion in the length differences.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I'm definitely overthinking things! 😄

We should probably add MoveTo and LineTo methods then shouldn't we (or have I missed an equivalent)?

I'm still intrigued by the result. I must be visualizing something badly because it seemed like a rectangle to me.

JimBobSquarePants and others added 3 commits August 5, 2021 22:55
Co-authored-by: Anton Firszov <antonfir@gmail.com>
Co-authored-by: Anton Firszov <antonfir@gmail.com>
@JimBobSquarePants JimBobSquarePants merged commit 7736ad3 into master Aug 9, 2021
@JimBobSquarePants JimBobSquarePants deleted the js/samples branch August 9, 2021 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants