forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatialUnderstandingSourceMesh.cs
More file actions
168 lines (140 loc) · 6.32 KB
/
SpatialUnderstandingSourceMesh.cs
File metadata and controls
168 lines (140 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;
using UnityEngine;
using HoloToolkit.Unity.SpatialMapping;
namespace HoloToolkit.Unity
{
/// <summary>
/// Provides the input meshes to the spatial understanding dll.
/// The component relies on the spatial mapping module. It maintains
/// a mesh list in the required dll format which is updated from
/// the spatial mapping's SurfaceObject list.
/// </summary>
public class SpatialUnderstandingSourceMesh : MonoBehaviour
{
// Privates
/// <summary>
/// Internal list of meshes that is passed to the dll. This is
/// kept up to date with spatial mapping's SurfaceObject list.
/// </summary>
private readonly List<SpatialUnderstandingDll.MeshData> inputMeshList = new List<SpatialUnderstandingDll.MeshData>();
// Functions
private void Start()
{
TryAttach(SpatialMappingManager.Instance.Source);
SpatialMappingManager.Instance.SourceChanged += HandleSpatialMappingSourceChanged;
}
private void HandleSpatialMappingSourceChanged(object sender, PropertyChangedEventArgsEx<SpatialMappingSource> e)
{
Debug.Assert(object.ReferenceEquals(sender, SpatialMappingManager.Instance));
TryDetach(e.OldValue);
TryAttach(e.NewValue);
}
private void TryDetach(SpatialMappingSource spatialMappingSource)
{
if (spatialMappingSource != null)
{
spatialMappingSource.SurfaceAdded -= HandleSurfaceAdded;
spatialMappingSource.SurfaceUpdated -= HandleSurfaceUpdated;
spatialMappingSource.SurfaceRemoved -= HandleSurfaceRemoved;
spatialMappingSource.RemovingAllSurfaces -= HandleRemovingAllSurfaces;
}
inputMeshList.Clear();
}
private void TryAttach(SpatialMappingSource spatialMappingSource)
{
if (spatialMappingSource != null)
{
spatialMappingSource.SurfaceAdded += HandleSurfaceAdded;
spatialMappingSource.SurfaceUpdated += HandleSurfaceUpdated;
spatialMappingSource.SurfaceRemoved += HandleSurfaceRemoved;
spatialMappingSource.RemovingAllSurfaces += HandleRemovingAllSurfaces;
Debug.Assert(inputMeshList.Count == 0);
foreach (var surface in spatialMappingSource.SurfaceObjects)
{
inputMeshList.Add(CreateMeshData(surface, meshUpdateID: 0));
}
}
}
private void HandleSurfaceAdded(object sender, DataEventArgs<SpatialMappingSource.SurfaceObject> e)
{
Debug.Assert(object.ReferenceEquals(sender, SpatialMappingManager.Instance.Source));
Debug.Assert(FindMeshIndexInInputMeshList(e.Data.ID) < 0);
inputMeshList.Add(CreateMeshData(e.Data, meshUpdateID: 0));
}
private void HandleSurfaceUpdated(object sender, DataEventArgs<SpatialMappingSource.SurfaceUpdate> e)
{
Debug.Assert(object.ReferenceEquals(sender, SpatialMappingManager.Instance.Source));
Debug.Assert(e.Data.Old.ID == e.Data.New.ID);
int iMesh = FindMeshIndexInInputMeshList(e.Data.New.ID);
Debug.Assert(iMesh >= 0);
inputMeshList[iMesh] = CreateMeshData(e.Data.New, (inputMeshList[iMesh].LastUpdateID + 1));
}
private void HandleSurfaceRemoved(object sender, DataEventArgs<SpatialMappingSource.SurfaceObject> e)
{
Debug.Assert(object.ReferenceEquals(sender, SpatialMappingManager.Instance.Source));
var iMesh = FindMeshIndexInInputMeshList(e.Data.ID);
Debug.Assert(iMesh >= 0);
inputMeshList.RemoveAt(iMesh);
}
private void HandleRemovingAllSurfaces(object sender, EventArgs e)
{
Debug.Assert(object.ReferenceEquals(sender, SpatialMappingManager.Instance.Source));
inputMeshList.Clear();
}
private int FindMeshIndexInInputMeshList(int meshID)
{
for (int i = 0; i < inputMeshList.Count; ++i)
{
if (inputMeshList[i].MeshID == meshID)
{
return i;
}
}
return -1;
}
private static SpatialUnderstandingDll.MeshData CreateMeshData(SpatialMappingSource.SurfaceObject surface, int meshUpdateID)
{
MeshFilter meshFilter = surface.Filter;
SpatialUnderstandingDll.MeshData meshData = new SpatialUnderstandingDll.MeshData();
if ((meshFilter != null) &&
(meshFilter.mesh != null) &&
(meshFilter.mesh.triangles.Length > 0))
{
// Fix surface mesh normals so we can get correct plane orientation.
meshFilter.mesh.RecalculateNormals();
// Convert
meshData.CopyFrom(meshFilter, surface.ID, meshUpdateID);
}
else
{
// No filter yet, add as an empty mesh (will be updated later in the update loop)
meshData.CopyFrom(null, surface.ID, meshUpdateID);
}
return meshData;
}
/// <summary>
/// Update the internal mesh list and provides an array pointer in
/// the form the dll will accept.
/// </summary>
/// <param name="meshCount">Number of meshes contains in the return mesh list</param>
/// <param name="meshList">Marshalled mesh list pointer. Valid only with the caller's function context</param>
/// <returns></returns>
public bool GetInputMeshList(out int meshCount, out IntPtr meshList)
{
if (inputMeshList.Count == 0)
{
meshCount = 0;
meshList = IntPtr.Zero;
return false;
}
// Convert to IntPtr
SpatialUnderstandingDll dll = SpatialUnderstanding.Instance.UnderstandingDLL;
meshCount = inputMeshList.Count;
meshList = dll.PinMeshDataForMarshalling(inputMeshList);
return true;
}
}
}