Node

Within Visual Studio, create a new class called Node in the Content folder. This class acts as our breadcrumb, which is essentially a wrapper around SpatialAnchor. To gain access to SpatialAnchor, we must import the following namespace: Windows.Perception.Spatial. Copy the following snippet to the newly created Node class:

internal class Node
{
static int NodeID = 0;
public string Name { get; private set; }
public Vector3 Position { get; set; }
public Vector3 Forward { get; set; }
public long Timestamp { get; set; }
public SpatialAnchor Anchor { get; set; }

public Node(Spatial​Anchor anchor, Vector3 position, Vector3 forward)
{
Name = $"Node_{++Node.NodeID}";
Anchor = anchor;
Position = position;
Forward = forward;
Timestamp = Utils.GetCurrentUnixTimestampMillis();
}
}

In the preceding code snippet, we have created the variables that Node will need to track, the main one being the SpatialAnchor. The properties Position and Forward are the position and gaze direction of the user relative to the assigned SpatialAnchor. Along with the variables, we will also add some helper methods. Let's add each, one by one, starting with GetTransform, as shown in the following snippet:

public Matrix4x4? GetTransform(SpatialCoordinateSystem targetCoordinateSystem)
{
var mat = Anchor.CoordinateSystem.TryGetTransformTo(targetCoordinateSystem);
if (!mat.HasValue)
{
return null;
}
Matrix4x4 modelTranslation = Matrix4x4.CreateTranslation(Position);
return (modelTranslation) * mat.Value;
}

Our first method, GetTransform, will return the transformation matrix from the assigned spatial anchor's coordinate system to targetCoordinateSystem. This can then be used to transform holograms that are anchored to the assigned SpatialAnchor to the coordinate system passed in targetCoordinateSystem. You will see how we use this when we come to render each Node:

public Vector3? TryGetTransformedPosition(SpatialCoordinateSystem targetCoordinateSystem)
{
if (targetCoordinateSystem == Anchor.CoordinateSystem)
{
return Position;
}
var trans =
Anchor.CoordinateSystem.TryGetTransformTo(targetCoordinateSystem);
if (trans.HasValue)
{
return Vector3.Transform(Position, trans.Value);
}
return null;
}

The method TryGetTransformedPosition is very similar to the previous method, GetTransform; however instead of returning a transformation matrix, it will return Position relative to targetCoordinateSystem passed in as a parameter. This method will be used when determining the position of Node relative to the reference frame's position (the reference frame being the spatial details associated to the HoloLens device). As we did previously, we obtain the transformation matrix via the line var trans = Anchor.CoordinateSystem.TryGetTransformTo(targetCoordinateSystem);; now, armed with the transformation matrix, we simply transform the position of Node to targetCoordinateSystem using the Vector3.Transform method, as follows:

public float? TryGetDistance(SpatialCoordinateSystem targetCoordinateSystem, Vector3 targetPosition)
{
if (targetCoordinateSystem == Anchor.CoordinateSystem)
{
return (targetPosition - Position).Length();
}

var trans = Anchor.CoordinateSystem.TryGetTransformTo(targetCoordinateSystem);
if (trans.HasValue)
{
return (targetPosition - Vector3.Transform(Position,
trans.Value)).Length();
}
return null;
}

The TryGetDistance method returns the euclidean distance between two positions relative to targetCoordinateSystem. The method starts by checking whether the coordinate system of Node is the same as the coordinate system passed in and, if so, returns the distance between the two positions with no further calculation. Otherwise, it attempts to create a transformation matrix from the anchor's coordinate system and the coordinate system passed in. If successful, it will transform the position of Node before calculating and returning the distance. 

With that method now complete, this concludes our Node class, for now at least. Next, we will turn our attention to the Edge class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.214.32