Skip to content

Commit 76fed02

Browse files
author
EMEA\vonhorc
committed
Using get/set to change the observed volume
1 parent 5b39f22 commit 76fed02

File tree

1 file changed

+134
-41
lines changed

1 file changed

+134
-41
lines changed

Assets/HoloToolkit/SpatialMapping/Scripts/SpatialMappingObserver.cs

Lines changed: 134 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ public enum ObserverStates
2424
Stopped = 1
2525
}
2626

27+
/// <summary>
28+
/// Spatial Mapping Volume Type
29+
/// </summary>
30+
public enum ObserverVolumeTypes
31+
{
32+
/// <summary>
33+
/// The observed volume is an axis aligned box.
34+
/// </summary>
35+
AxisAlignedBox = 0,
36+
37+
/// <summary>
38+
/// The observed volume is an oriented box.
39+
/// </summary>
40+
OrientedBox = 1,
41+
42+
/// <summary>
43+
/// The observed volume is a sphere.
44+
/// </summary>
45+
Sphere = 2
46+
}
47+
48+
2749
/// <summary>
2850
/// The SpatialMappingObserver class encapsulates the SurfaceObserver into an easy to use
2951
/// object that handles managing the observed surfaces and the rendering of surface geometry.
@@ -32,22 +54,34 @@ public class SpatialMappingObserver : SpatialMappingSource
3254
{
3355
[Tooltip("The number of triangles to calculate per cubic meter.")]
3456
public float TrianglesPerCubicMeter = 500f;
35-
36-
[Tooltip("The extents of the observation volume.")]
37-
public Vector3 Extents = Vector3.one * 10.0f;
38-
57+
3958
[Tooltip("How long to wait (in sec) between Spatial Mapping updates.")]
4059
public float TimeBetweenUpdates = 3.5f;
4160

42-
/// <summary>
43-
/// The origin of the observation volume.
44-
/// </summary>
45-
private Vector3 origin = Vector3.zero;
46-
4761
/// <summary>
4862
/// Indicates the current state of the Surface Observer.
4963
/// </summary>
5064
public ObserverStates ObserverState { get; private set; }
65+
66+
/// <summary>
67+
/// Indicates the current type of the observed volume
68+
/// </summary>
69+
private ObserverVolumeTypes observerVolumeType = ObserverVolumeTypes.AxisAlignedBox;
70+
public ObserverVolumeTypes ObserverVolumeType
71+
{
72+
get
73+
{
74+
return observerVolumeType;
75+
}
76+
set
77+
{
78+
if(observerVolumeType != value)
79+
{
80+
observerVolumeType = value;
81+
SwitchObservedVolume();
82+
}
83+
}
84+
}
5185

5286
/// <summary>
5387
/// Our Surface Observer object for generating/updating Spatial Mapping data.
@@ -77,7 +111,69 @@ public class SpatialMappingObserver : SpatialMappingSource
77111
/// Used to track when the Observer was last updated.
78112
/// </summary>
79113
private float updateTime;
80-
114+
115+
[Tooltip("The extents of the observation volume.")]
116+
private Vector3 extents = Vector3.one * 10.0f;
117+
public Vector3 Extents
118+
{
119+
get
120+
{
121+
return extents;
122+
}
123+
set
124+
{
125+
if(extents != value)
126+
{
127+
extents = value;
128+
SwitchObservedVolume();
129+
}
130+
}
131+
}
132+
133+
/// <summary>
134+
/// The origin of the observation volume.
135+
/// </summary>
136+
private Vector3 origin = Vector3.zero;
137+
public Vector3 Origin
138+
{
139+
get
140+
{
141+
return origin;
142+
}
143+
set
144+
{
145+
if(origin != value)
146+
{
147+
origin = value;
148+
SwitchObservedVolume();
149+
}
150+
}
151+
}
152+
153+
/// <summary>
154+
/// The direction of the observed volume if an oriented box is choosen.
155+
/// </summary>
156+
private Quaternion orientation = Quaternion.identity;
157+
public Quaternion Orientation
158+
{
159+
get
160+
{
161+
return orientation;
162+
}
163+
set
164+
{
165+
if(orientation != value)
166+
{
167+
orientation = value;
168+
// Only needs to be changed if the corresponding mode is active.
169+
if(ObserverVolumeType == ObserverVolumeTypes.OrientedBox)
170+
{
171+
SwitchObservedVolume();
172+
}
173+
}
174+
}
175+
}
176+
81177
protected override void Awake()
82178
{
83179
base.Awake();
@@ -171,7 +267,7 @@ public void StartObserving()
171267
if (observer == null)
172268
{
173269
observer = new SurfaceObserver();
174-
observer.SetVolumeAsAxisAlignedBox(Vector3.zero, Extents);
270+
SwitchObservedVolume();
175271
}
176272

177273
if (ObserverState != ObserverStates.Running)
@@ -230,48 +326,45 @@ public void CleanupObserver()
230326

231327
/// <summary>
232328
/// Can be called to override the default origin for the observed volume. Can only be called while observer has been started.
329+
/// Kept for compatibility with Examples/SpatialUnderstanding
233330
/// </summary>
234-
public bool SetObserverOrigin(Vector3 origin)
331+
public bool SetObserverOrigin(Vector3 _origin)
235332
{
236333
bool originUpdated = false;
237334

238335
if (observer != null)
239336
{
240-
observer.SetVolumeAsAxisAlignedBox(origin, Extents);
337+
Origin = _origin;
241338
originUpdated = true;
242339
}
243340

244341
return originUpdated;
245342
}
246343

247-
/// <summary>
248-
/// Can be called to override the default extents as an axis aligned box for the observed volume. Can only be called while observer has been started.
249-
/// </summary>
250-
public bool SetObserverExtentsAxisAligned(Vector3 extents)
251-
{
252-
bool extentsUpdated = false;
253-
if (observer != null)
254-
{
255-
observer.SetVolumeAsAxisAlignedBox(origin, extents);
256-
Extents = extents;
257-
extentsUpdated = true;
258-
}
259-
return extentsUpdated;
260-
}
261-
262-
/// <summary>
263-
/// Can be called to override the default extents as an oriented box for the observed volume. Can only be called while observer has been started.
264-
/// </summary>
265-
public bool SetObserverExtentsOrientedBox(Vector3 extents, Quaternion orientation)
266-
{
267-
bool extentsUpdated = false;
268-
if (observer != null)
269-
{
270-
observer.SetVolumeAsOrientedBox(origin, extents, orientation);
271-
extentsUpdated = true;
272-
}
273-
return extentsUpdated;
274-
}
344+
/// <summary>
345+
/// Change the observed volume according to ObserverVolumeType.
346+
/// </summary>
347+
private void SwitchObservedVolume()
348+
{
349+
if (observer != null)
350+
{
351+
switch (observerVolumeType)
352+
{
353+
case ObserverVolumeTypes.AxisAlignedBox:
354+
observer.SetVolumeAsAxisAlignedBox(origin, extents);
355+
break;
356+
case ObserverVolumeTypes.OrientedBox:
357+
observer.SetVolumeAsOrientedBox(origin, extents, orientation);
358+
break;
359+
case ObserverVolumeTypes.Sphere:
360+
observer.SetVolumeAsSphere(origin, extents.magnitude); //workaround
361+
break;
362+
default:
363+
observer.SetVolumeAsAxisAlignedBox(origin, extents);
364+
break;
365+
}
366+
}
367+
}
275368

276369
/// <summary>
277370
/// Handles the SurfaceObserver's OnDataReady event.

0 commit comments

Comments
 (0)