Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

[Dotnet] port of ScanningAndDetecting3DObjects #435

Conversation

stephen-hawley
Copy link
Contributor

This is a huge sample, thanks for your patience.

@stephen-hawley stephen-hawley changed the title port of ScanningAndDetecting3DObjects [Dotnet] port of ScanningAndDetecting3DObjects Jan 14, 2022

I occasionally refactored the Swift code where I thought it helped legibility or reduced repetition. Usually this was in the form of extracting a function.

One area where there is considerably variance from the Swift code is in the "Utility_Extensions" folder. Xamarin.iOS uses OpenTK vector and matrix classes for manipulating 3D objects and transforms. The OpenTK classes have different APIs and
Copy link
Member

Choose a reason for hiding this comment

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

and ...?

// Output is: "(1, 0, 0, 0)\n(0, 1, 0, 0)\n(0, 0, 1, 0)\n(2, 3, 4, 1)"
```

As you can see, the position is encoded in the bottom row's first 3 elements. This might be transposed (sideways) to your expectations. The functions `SCNMatrix4 ToSCNMatrix4(this NMatrix4 self)` and `NMatrix4 ToNMatrix4(this SCNMatrix4 self)` use the `M{col}{row}` fields to assure the values are proper, so that's fine, but there _is_ a small performance cost to changing the representation, so avoid needless conversions.
Copy link
Member

Choose a reason for hiding this comment

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

The managed SCNMatrix4 type will change in .NET (xamarin/xamarin-macios#13695), so maybe we'll want to wait with completing this sample until the sample can be updated according to those changes?

Comment on lines +332 to +337
internal new void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}

Copy link
Member

Choose a reason for hiding this comment

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

Implementing a new Dispose method shouldn't needed, this is what our does:

https://github.com/xamarin/xamarin-macios/blob/1d9a02cbef9092717f04a451e11c9279348b5fa9/src/Foundation/NSObject2.cs#L210-L213

So I think this should be removed (it also happens in at least one other file).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I'm just going to remove these. They were sketchy anyway. At least one was marked as being put in for debugging.

disposed = true;
if (disposing)
{
base.Dispose ();
Copy link
Member

Choose a reason for hiding this comment

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

The Dispose pattern is wrong, the Dispose (bool) method should call the same base method, so:

base.Dispose (disposing);

This effectively changes the whole method, and the comment in the README about Xamarin's weird Dispose pattern is also wrong (the original author got the Dispose pattern wrong).

This method would become:

if (!disposed)
{
    disposed = true;
    NSNotificationCenter.DefaultCenter.RemoveObserver (notificationObserverHandle);
    Origin?.Dispose ();
    BoundingBox?.Dispose ();
    GhostBoundingBox?.Dispose ();
}
base.Dispose (disposing);

Ref: https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern-for-a-derived-class

This occurs in other files as well.


void ScanningStateChanged (NSNotification notification)
{
var scanState = notification.UserInfo? [Scan.StateUserInfoKey] as SimpleBox<Scan.ScanState>;

Choose a reason for hiding this comment

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

I think this one would be a good candidate for an is operator!

case Axis.Y: return new [] { sides [BoundingBoxSide.PositionName.Top], sides [BoundingBoxSide.PositionName.Bottom] };
case Axis.Z: return new [] { sides [BoundingBoxSide.PositionName.Front], sides [BoundingBoxSide.PositionName.Back] };
}
throw new ArgumentOutOfRangeException ("axis");

Choose a reason for hiding this comment

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

Suggested change
throw new ArgumentOutOfRangeException ("axis");
throw new ArgumentOutOfRangeException (nameof (axis));


foreach (var result in hitResults)
{
var hitAxis = result.Node.ParentNode as ObjectOriginAxis;

Choose a reason for hiding this comment

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

Maybe another good place to use is operator

// Perform a hit test against the feature point cloud.
var result = sceneView.SmartHitTest (ViewController.Instance.ScreenCenter);
if (result is null)
{

Choose a reason for hiding this comment

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

Not sure if worth going through many of these, but mono guidelines recommend if () { as well as the else statement


void ScanningStateChanged (NSNotification notification)
{
var state = notification.UserInfo [Scan.StateUserInfoKey] as SimpleBox<Scan.ScanState>;

Choose a reason for hiding this comment

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

good place for is operator


void ScanningStateChanged (NSNotification notification)
{
var state = notification.UserInfo? [Scan.StateUserInfoKey] as SimpleBox<Scan.ScanState>;

Choose a reason for hiding this comment

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

is operator would be good here

Opacity = (nfloat) newOpacity;
}
}
}

Choose a reason for hiding this comment

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

new line

{
DetectedObject?.RemoveFromParentNode ();

if (sceneView.Session.Configuration as ARWorldTrackingConfiguration is not null)

Choose a reason for hiding this comment

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

Suggested change
if (sceneView.Session.Configuration as ARWorldTrackingConfiguration is not null)
if (sceneView.Session.Configuration as ARWorldTrackingConfiguration)

Think this is redundant

startOverButton = new UIBarButtonItem ("Restart", UIBarButtonItemStyle.Plain, restartButtonTapped);

var navigationItem = new UINavigationItem ("Start");
navigationItem.LeftBarButtonItem = backButton;

Choose a reason for hiding this comment

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

Can these be added to the constructor?

@rolfbjarne
Copy link
Member

Microsoft support for Xamarin will end on May 1, 2024 for all Xamarin SDKs. In preparation for this, all issues and PRs in this repository are being closed.

@rolfbjarne rolfbjarne closed this Apr 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants