Hum …no thanks ! – Lordinaire

Tag: C# Page 2 of 7

Intersection between two lists of dates

I faced a problem when I tried to get the intersection between two lists of DateTime. I wanted to have the intersection only by comparing dates and not dates and time. If I use the LINQ method Intersect on my lists, it will not fit what I want because if the date match but not the time (like different seconds) it will not be part of the intersection.

Of course, I can make my own Intersect method that will test only the date but it’s a little bit overkill. The easy way is to use an IEqualityComparer and pass it to the Intersect method. Here is the implementation I used :

public class DateEqualityComparer : IEqualityComparer<DateTime>
    public bool Equals(DateTime date1, DateTime date2)
        return GetHashCode(date1) == GetHashCode(date2);

    public int GetHashCode(DateTime date)
        var hashCode = date.Year ^ date.Month ^ date.Day;
        return hashCode.GetHashCode();

A little sample of the case :

public void IntersectDates()
    var dates1 = new List<DateTime>
    // 05/10/2017 21:40:24
    // 06/10/2017 21:40:29
    // 08/10/2017 21:40:34

    var dates2 = new List<DateTime>
    // 05/10/2017 21:40:24
    // 07/10/2017 21:40:39
    // 08/10/2017 21:40:44

    var dateEqualityComparer = new DateEqualityComparer();
    var intersectList = dates1.Intersect(dates2, dateEqualityComparer).ToList();

private static void Log(IEnumerable<DateTime> list)
    foreach (var someClass in list)
    // 05/10/2017
    // 08/10/2017

[Xamarin] Hololens app with UrhoSharp : Spatial Mapping – Part 3

Today, we will try to detect our environment with the spatial mapping. That mechanism allows us to detect real life objects, like a floor or a table, and gets back information in order to build a virtual object that will be the representation of the real object.

Let me do it, please

First of all, we need to add the spatial mapping capability in our project by editing the Package.appxmanifest :

<Package   xmlns=""   
           IgnorableNamespaces="uap mp">
    <span style="color: #ff0000;"><uap2:Capability Name="spatialPerception"/></span>

Override as always

The UrhoSharp framework has already handle basic scenario like spatial mapping in the class HoloApplication (the base class of or app). We only need to override some methods and enable some properties to be able to catch spatial mapping data. Our scenario will be very simple :

  • Start detection
  • Display detected objects (wireframe)
  • Tap to stop detection
  • Hide detected objects
  • Baaaaaaaaaaaaaaalls !

Let’s begin with the Start method :

private bool _isSpatialMappingActive;
private Node _detectedSurfaceNode;
protected override async void Start()
    // Enable the AirTap gesture
    EnableGestureTapped = true;
    // Create a new node to store all the detected objects we will create
    _detectedSurfaceNode = Scene.CreateChild();
    _isSpatialMappingActive = true;
    // Start the detection
    await StartSpatialMapping(new Vector3(10, 10, 10));

For the Update method, just add a new condition :

protected override void OnUpdate(float timeStep)
    // Don't do anything if the detection is activated
    if (_isSpatialMappingActive)
    _spawnDeltaTime += timeStep;
    if (_spawnDeltaTime >= _spawnTimer)
        _spawnDeltaTime = 0;
        var randomPosition = new Vector3(Randoms.Next(-0.3f, 0.3f), Randoms.Next(-0.3f, 0.3f), Randoms.Next(2, 5));

The real stuff starting here. We will override the method OnSurfaceAddedOrUpdated like that :

public override void OnSurfaceAddedOrUpdated(string surfaceId, DateTimeOffset lastUpdateTimeUtc, SpatialVertex[] vertexData,
    short[] indexData, Vector3 boundsCenter, Quaternion boundsRotation)
    StaticModel model;

    // If the surface already exists get its node otherwise creates a new one
    var node = _detectedSurfaceNode.GetChild(surfaceId, false);
    if (node != null)
        model = node.GetComponent<StaticModel>();
        node = _detectedSurfaceNode.CreateChild(surfaceId);
        model = node.CreateComponent<StaticModel>();

    // Set the position and rotation
    node.Position = boundsCenter;
    node.Rotation = boundsRotation;

    // The model is created with the vertex data
    model.Model = CreateModelFromVertexData(vertexData, indexData);

    // Add a rigidbody for the physic engine

    // Add a collision shape based on the model
    var shape = node.CreateComponent<CollisionShape>();
    shape.SetTriangleMesh(model.Model, 0, Vector3.One, Vector3.Zero, Quaternion.Identity);

    // Add a material for our model (a green wireframe)
    var material = Material.FromColor(Color.Green);
    material.FillMode = FillMode.Wireframe;

To handle the Tap gesture, override the OnGestureTapped method :

public override void OnGestureTapped(GazeInfo gaze)

    // Stop the detection
    _isSpatialMappingActive = false;

    // Disable wireframe models but keep the rest (RigidBody, CollisionShape)
    // to interact with it
    var childCount = _detectedSurfaceNode.GetNumChildren(false);
    for (uint i = 0; i < childCount; i++)
        var childNode = _detectedSurfaceNode.GetChild(i);
        var model = childNode.GetComponent<StaticModel>();
        model.Enabled = false;

That’s it folks !

Page 2 of 7

Powered by WordPress & Theme by Anders Norén

%d bloggers like this: