Skip to content

Commit

Permalink
[Vision] Improve and simplify manually bound constructors for VNCircl…
Browse files Browse the repository at this point in the history
…e and VNVector. (#21653)
  • Loading branch information
rolfbjarne authored Nov 20, 2024
1 parent 6dd3a8d commit 611c7a1
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 36 deletions.
52 changes: 36 additions & 16 deletions src/Vision/VNCircle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,48 @@
using ObjCRuntime;

namespace Vision {
/// <summary>This enum is used to select how to initialize a new instance of a <see cref="VNCircle" />.</summary>
public enum VNCircleInitializationOption {
/// <summary>The <c>radiusOrDiameter</c> parameter passed to the constructor is the radius of the circle.</summary>
Radius,
/// <summary>The <c>radiusOrDiameter</c> parameter passed to the constructor is the diameter of the circle.</summary>
Diameter,
}

public partial class VNCircle {
/// <summary>Create a new <see cref="VNCircle" /> instance.</summary>
/// <param name="center">The center of the circle.</param>
/// <param name="radiusOrDiameter">The radius or diameter of the circle. Use <paramref name="option" /> to specify which.</param>
/// <param name="option">Specifies whether <paramref name="radiusOrDiameter" /> is a neutral or a camera value.</param>
public VNCircle (VNPoint center, double radiusOrDiameter, VNCircleInitializationOption option)
: base (NSObjectFlag.Empty)
{
switch (option) {
case VNCircleInitializationOption.Radius:
InitializeHandle (_InitWithCenterRadius (center, radiusOrDiameter));
break;
case VNCircleInitializationOption.Diameter:
InitializeHandle (_InitWithCenterDiameter (center, radiusOrDiameter));
break;
default:
throw new ArgumentOutOfRangeException (nameof (option), option, "Invalid enum value.");
}
}

public static VNCircle? CreateUsingRadius (VNPoint center, double radius)
/// <summary>Create a new <see cref="VNCircle" /> instance.</summary>
/// <param name="center">The center of the circle.</param>
/// <param name="radius">The radius of the circle.</param>
public static VNCircle CreateUsingRadius (VNPoint center, double radius)
{
var handle = Messaging.IntPtr_objc_msgSend (class_ptr, Selector.GetHandle ("alloc"));
#if NET
handle = Messaging.NativeHandle_objc_msgSend_NativeHandle_Double (handle, Selector.GetHandle ("initWithCenter:radius:"), center.Handle, radius);
#else
handle = Messaging.IntPtr_objc_msgSend_IntPtr_Double (handle, Selector.GetHandle ("initWithCenter:radius:"), center.Handle, radius);
#endif
return Runtime.GetNSObject<VNCircle> (handle, true);
return new VNCircle (center, radius, VNCircleInitializationOption.Radius);
}

public static VNCircle? CreateUsingDiameter (VNPoint center, double diameter)
/// <summary>Create a new <see cref="VNCircle" /> instance.</summary>
/// <param name="center">The center of the circle.</param>
/// <param name="diameter">The diameter of the circle.</param>
public static VNCircle CreateUsingDiameter (VNPoint center, double diameter)
{
var handle = Messaging.IntPtr_objc_msgSend (class_ptr, Selector.GetHandle ("alloc"));
#if NET
handle = Messaging.NativeHandle_objc_msgSend_NativeHandle_Double (handle, Selector.GetHandle ("initWithCenter:diameter:"), center.Handle, diameter);
#else
handle = Messaging.IntPtr_objc_msgSend_IntPtr_Double (handle, Selector.GetHandle ("initWithCenter:diameter:"), center.Handle, diameter);
#endif
return Runtime.GetNSObject<VNCircle> (handle, true);
return new VNCircle (center, diameter, VNCircleInitializationOption.Diameter);
}
}
}
16 changes: 12 additions & 4 deletions src/Vision/VNVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@

namespace Vision {
public partial class VNVector {
/// <summary>Create a new <see cref="VNVector" /> instance using polar coordinates.</summary>
/// <param name="polarCoordinates">The r and theta values of the vector.</param>
public VNVector ((double R, double Theta) polarCoordinates)
: base (NSObjectFlag.Empty)
{
InitializeHandle (_InitWithRTheta (polarCoordinates.R, polarCoordinates.Theta));
}

public static VNVector? Create (double r, double theta)
/// <summary>Create a new <see cref="VNVector" /> instance using polar coordinates.</summary>
/// <param name="r">The r value (length) of the vector.</param>
/// <param name="theta">The theta value (angle) of the vector.</param>
public static VNVector Create (double r, double theta)
{
var handle = Messaging.IntPtr_objc_msgSend (class_ptr, Selector.GetHandle ("alloc"));
handle = Messaging.IntPtr_objc_msgSend_Double_Double (handle, Selector.GetHandle ("initWithR:theta:"), r, theta);
return Runtime.GetNSObject<VNVector> (handle, true);
return new VNVector ((r, theta));
}
}
}
6 changes: 3 additions & 3 deletions src/vision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3745,7 +3745,7 @@ interface VNVector : NSCopying, NSSecureCoding {

[Internal]
[Export ("initWithR:theta:")]
IntPtr InitWithRTheta (double r, double theta);
IntPtr _InitWithRTheta (double r, double theta);

[Export ("initWithVectorHead:tail:")]
NativeHandle Constructor (VNPoint head, VNPoint tail);
Expand Down Expand Up @@ -3781,11 +3781,11 @@ interface VNCircle : NSCopying, NSSecureCoding {

[Internal]
[Export ("initWithCenter:radius:")]
IntPtr InitWithCenterRadius (VNPoint center, double radius);
IntPtr _InitWithCenterRadius (VNPoint center, double radius);

[Internal]
[Export ("initWithCenter:diameter:")]
IntPtr InitWithCenterDiameter (VNPoint center, double diameter);
IntPtr _InitWithCenterDiameter (VNPoint center, double diameter);

[Export ("containsPoint:")]
bool Contains (VNPoint point);
Expand Down
3 changes: 0 additions & 3 deletions tests/cecil-tests/Documentation.KnownFailures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47448,8 +47448,6 @@ M:VideoToolbox.VTVideoEncoderSpecification.#ctor(Foundation.NSDictionary)
M:Vision.VNBarcodeSymbologyExtensions.GetConstants(Vision.VNBarcodeSymbology[])
M:Vision.VNBarcodeSymbologyExtensions.GetValues(Foundation.NSString[])
M:Vision.VNCircle.Copy(Foundation.NSZone)
M:Vision.VNCircle.CreateUsingDiameter(Vision.VNPoint,System.Double)
M:Vision.VNCircle.CreateUsingRadius(Vision.VNPoint,System.Double)
M:Vision.VNCircle.EncodeTo(Foundation.NSCoder)
M:Vision.VNContour.Copy(Foundation.NSZone)
M:Vision.VNDetectBarcodesRequest.GetSupportedSymbologies(Foundation.NSError@)
Expand Down Expand Up @@ -47608,7 +47606,6 @@ M:Vision.VNUtils.GetNormalizedRect(CoreGraphics.CGRect,System.UIntPtr,System.UIn
M:Vision.VNUtils.GetNormalizedRect(CoreGraphics.CGRect,System.UIntPtr,System.UIntPtr)
M:Vision.VNUtils.IsIdentityRect(CoreGraphics.CGRect)
M:Vision.VNVector.Copy(Foundation.NSZone)
M:Vision.VNVector.Create(System.Double,System.Double)
M:Vision.VNVector.EncodeTo(Foundation.NSCoder)
M:Vision.VNVideoProcessorCadence.Copy(Foundation.NSZone)
M:Vision.VNVideoProcessorRequestProcessingOptions.Copy(Foundation.NSZone)
Expand Down
4 changes: 0 additions & 4 deletions tests/introspection/ApiSelectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,10 +1290,6 @@ protected virtual bool SkipInit (string selector, MethodBase m)
case "initWithMinCenterCoordinateDistance:":
case "initExcludingCategories:":
case "initIncludingCategories:":
// Vision
case "initWithCenter:diameter:":
case "initWithCenter:radius:":
case "initWithR:theta:":
// PassKit
case "initWithProvisioningCredentialIdentifier:sharingInstanceIdentifier:cardTemplateIdentifier:preview:":
case "initWithProvisioningCredentialIdentifier:sharingInstanceIdentifier:cardConfigurationIdentifier:preview:":
Expand Down
34 changes: 31 additions & 3 deletions tests/monotouch-test/Vision/VNCircleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// Copyright (c) Microsoft Corporation.
//

#if !__WATCHOS__

using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -48,6 +46,36 @@ public void CreateUsingDiameterTest ()
Assert.AreEqual (circle.Center.X, 5, "X");
Assert.That (circle.RetainCount, Is.EqualTo ((nuint) 1), "RetainCount");
}

[Test]
public void CreateUsingRadiusCtorTest ()
{
using var circle = new VNCircle (new VNPoint (10, 10), radiusOrDiameter: 10, option: VNCircleInitializationOption.Radius);
Assert.NotNull (circle, "Circle not null");
Assert.AreEqual (circle.Radius, 10, "Radius");
Assert.AreEqual (circle.Center.X, 10, "X");
Assert.AreEqual (circle.Center.Y, 10, "Y");
Assert.That (circle.RetainCount, Is.EqualTo ((nuint) 1), "RetainCount");
}

[Test]
public void CreateUsingDiameterCtorTest ()
{
using var circle = new VNCircle (new VNPoint (5, 6), radiusOrDiameter: 7, option: VNCircleInitializationOption.Diameter);
Assert.NotNull (circle, "Circle not null");
Assert.AreEqual (circle.Diameter, 7, "Diameter");
Assert.AreEqual (circle.Center.Y, 6, "Y");
Assert.AreEqual (circle.Center.X, 5, "X");
Assert.That (circle.RetainCount, Is.EqualTo ((nuint) 1), "RetainCount");
}

[Test]
public void CreateUsingInvalidOptionCtorTest ()
{
Assert.Throws<ArgumentOutOfRangeException> (() => {
using (var circle = new VNCircle (new VNPoint (5, 6), radiusOrDiameter: 7, option: (VNCircleInitializationOption) (-1))) {
}
});
}
}
}
#endif
13 changes: 10 additions & 3 deletions tests/monotouch-test/Vision/VNVectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// Copyright (c) Microsoft Corporation.
//

#if !__WATCHOS__

using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -36,6 +34,15 @@ public void VNVectorCreateTest ()
Assert.AreEqual (vector.Theta, 0.5, "Theta");
Assert.That (vector.RetainCount, Is.EqualTo ((nuint) 1), "RetainCount");
}

[Test]
public void VNVectorCtorTest ()
{
using var vector = new VNVector ((R: 10, Theta: 0.5));
Assert.NotNull (vector, "vector not null");
Assert.AreEqual (vector.R, 10, "R");
Assert.AreEqual (vector.Theta, 0.5, "Theta");
Assert.That (vector.RetainCount, Is.EqualTo ((nuint) 1), "RetainCount");
}
}
}
#endif

6 comments on commit 611c7a1

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.