AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ShapeDefinition.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 using HoloToolkit.Unity;
6 using System.Collections.Generic;
7 using System;
8 using System.Collections.ObjectModel;
9 
10 namespace HoloToolkit.Examples.SpatialUnderstandingFeatureOverview
11 {
12  public class ShapeDefinition : Singleton<ShapeDefinition>
13  {
14  // Properties
15  public bool HasCreatedShapes { get; private set; }
16  public ReadOnlyCollection<string> CustomShapeDefinitions { get { return customShapeDefinitions.AsReadOnly(); } }
17 
18  // Privates
19  private List<string> customShapeDefinitions = new List<string>();
20 
21  // Functions
22  private void Start()
23  {
24  if (SpatialUnderstanding.Instance != null)
25  {
26  SpatialUnderstanding.Instance.ScanStateChanged += OnScanStateChanged;
27  }
28  }
29 
30  protected override void OnDestroy()
31  {
32  base.OnDestroy();
33 
34  if (SpatialUnderstanding.Instance != null)
35  {
36  SpatialUnderstanding.Instance.ScanStateChanged -= OnScanStateChanged;
37  }
38  }
39 
40  public void CreateShapes()
41  {
42  if (HasCreatedShapes ||
43  !SpatialUnderstanding.Instance.AllowSpatialUnderstanding)
44  {
45  return;
46  }
47 
48  // Create definitions and analyze
49  CreateCustomShapeDefinitions();
51  }
52 
53  private void OnScanStateChanged()
54  {
55  // If we are leaving the None state, go ahead and register shapes now
57  {
58  // Create definitions and analyze
59  CreateShapes();
60  }
61  }
62 
63  private bool AddShape(
64  string shapeName,
65  List<SpatialUnderstandingDllShapes.ShapeComponent> shapeComponents)
66  {
67  return AddShape(shapeName, shapeComponents, null);
68  }
69 
70  private bool AddShape(
71  string shapeName,
72  List<SpatialUnderstandingDllShapes.ShapeComponent> shapeComponents,
73  List<SpatialUnderstandingDllShapes.ShapeConstraint> shapeConstraints)
74  {
75  if (!SpatialUnderstanding.Instance.AllowSpatialUnderstanding)
76  {
77  return false;
78  }
79  IntPtr shapeComponentsPtr = (shapeComponents == null) ? IntPtr.Zero : HoloToolkit.Unity.SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(shapeComponents.ToArray());
80  IntPtr shapeConstraintsPtr = (shapeConstraints == null) ? IntPtr.Zero : HoloToolkit.Unity.SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(shapeConstraints.ToArray());
82  shapeName,
83  (shapeComponents == null) ? 0 : shapeComponents.Count,
84  shapeComponentsPtr,
85  (shapeConstraints == null) ? 0 : shapeConstraints.Count,
86  shapeConstraintsPtr) == 0)
87  {
88  Debug.LogError("Failed to create custom shape description");
89  return false;
90  }
91  customShapeDefinitions.Add(shapeName);
92  return true;
93  }
94 
95  private void CreateCustomShapeDefinitions()
96  {
97  if (!SpatialUnderstanding.Instance.AllowSpatialUnderstanding)
98  {
99  return;
100  }
101 
102  List<SpatialUnderstandingDllShapes.ShapeComponent> shapeComponents;
103  List<SpatialUnderstandingDllShapes.ShapeConstraint> shapeConstraints;
104 
105  // AllSurfaces
106  shapeComponents = new List<SpatialUnderstandingDllShapes.ShapeComponent>()
107  {
109  new List<SpatialUnderstandingDllShapes.ShapeComponentConstraint>()
110  {
112  }),
113  };
114  AddShape("All Surfaces", shapeComponents);
115 
116  // Sittable
117  shapeComponents = new List<SpatialUnderstandingDllShapes.ShapeComponent>()
118  {
120  new List<SpatialUnderstandingDllShapes.ShapeComponentConstraint>()
121  {
125  }),
126  };
127  AddShape("Sittable", shapeComponents);
128 
129  // Chair
130  shapeComponents = new List<SpatialUnderstandingDllShapes.ShapeComponent>()
131  {
133  new List<SpatialUnderstandingDllShapes.ShapeComponentConstraint>()
134  {
142  }),
143  };
144  AddShape("Chair", shapeComponents);
145 
146  // LargeSurface
147  shapeComponents = new List<SpatialUnderstandingDllShapes.ShapeComponent>()
148  {
150  new List<SpatialUnderstandingDllShapes.ShapeComponentConstraint>()
151  {
157  }),
158  };
159  AddShape("Large Surface", shapeComponents);
160 
161  // EmptyTable
162  shapeComponents = new List<SpatialUnderstandingDllShapes.ShapeComponent>()
163  {
165  new List<SpatialUnderstandingDllShapes.ShapeComponentConstraint>()
166  {
172  }),
173  };
174  shapeConstraints = new List<SpatialUnderstandingDllShapes.ShapeConstraint>()
175  {
177  };
178  AddShape("Large Empty Surface", shapeComponents, shapeConstraints);
179 
180  // "Couch"
181  shapeComponents = new List<SpatialUnderstandingDllShapes.ShapeComponent>()
182  {
184  new List<SpatialUnderstandingDllShapes.ShapeComponentConstraint>()
185  {
186  // Seat
193  }),
195  new List<SpatialUnderstandingDllShapes.ShapeComponentConstraint>()
196  {
197  // Back
198  SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_SurfaceHeight_Between(0.6f, 1.0f),
199  SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_SurfaceCount_Min(1),
200  SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_IsRectangle(0.3f),
201  SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_RectangleLength_Between(0.4f, 3.0f),
202  SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_RectangleWidth_Min(0.05f),
203  }),
204  };
205  shapeConstraints = new List<SpatialUnderstandingDllShapes.ShapeConstraint>()
206  {
211  };
212  AddShape("Couch", shapeComponents, shapeConstraints);
213 
214  // Mark it
215  HasCreatedShapes = true;
216  }
217  }
218 }
static int AddShape([In, MarshalAs(UnmanagedType.LPStr)] string shapeName, [In] int componentCount, [In] IntPtr components, [In] int shapeConstraints, [In] IntPtr constraints)
Add a shape definition. A shape is defined by a list of components and a set of component constraints...
A shape component constraint. This includes its type enum and its type specific parameters.
override void OnDestroy()
Base OnDestroy method that destroys the Singleton&#39;s unique instance. Called by Unity when destroying ...
static ShapeComponentConstraint Create_RectangleWidth_Between(float minWidth, float maxWidth)
Constructs a constraint requiring the surface width to be between the given range. Width is the shorter of the two bounding edges.
static ShapeConstraint Create_RectanglesAligned(int componentIndexA, int componentIndexB, float maxDifference=0.1f)
Constructs a constraint requiring the components shapes to be either aligned with parallel or paralle...
static ShapeComponentConstraint Create_SurfaceHeight_Between(float minHeight, float maxHeight)
Constructs a constraint requiring the component to be within a height range above the floor ...
A shape component definition. Contains a list of component constraints.
static ShapeConstraint Create_AtBackOf(int componentIndexA, int componentIndexB)
Constructs a constraint requiring component B to be immediately in back of component A...
A shape constraint definition. Composed of a type and type specific parameters
static ShapeComponentConstraint Create_RectangleWidth_Min(float minWidth)
Constructs a constraint requiring a minimum width. Width is the shorter of the two bounding edges...
static void ActivateShapeAnalysis()
Runs the shape analysis. This should be called after scanning has been finalized and shapes have been...
static ShapeComponentConstraint Create_IsRectangle(float similarityMin=0.5f)
Constructs a constraint requiring the component to shaped like a rectangle.
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
static ShapeComponentConstraint Create_SurfaceArea_Min(float minArea)
Constructs a constraint requiring the component to contain a minimum surface area ...
Encapsulates the shape detection queries of the understanding DLL. Shapes are defined by the user wit...
static ShapeConstraint Create_RectanglesParallel(int componentIndexA, int componentIndexB)
Constructs a constraint requiring the components shapes longer edges to have parallel alignment...
The SpatialUnderstanding class controls the state and flow of the scanning process used in the unders...
static ShapeComponentConstraint Create_SurfaceNotPartOfShape(string shapeName)
Constructs a constraint requiring the component to not be a part of a specified shape ...
static ShapeConstraint Create_RectanglesSameLength(int componentIndexA, int componentIndexB, float similarityMin=0.8f)
Constructs a constraint requiring the components shapes longest edges to have the same length...
static ShapeComponentConstraint Create_RectangleLength_Min(float minLength)
Constructs a constraint requiring a minimum length. Length is the longer of the two bounding edges...
static ShapeComponentConstraint Create_SurfaceCount_Min(int minCount)
Constructs a constraint requiring the component to be a minimum number of discrete flat surfaces ...
static ShapeComponentConstraint Create_RectangleLength_Between(float minLength, float maxLength)
Constructs a constraint requiring the surface length to be between the given range. Length is the longer of the two bounding edges.
Singleton behaviour class, used for components that should only have one instance.
Definition: Singleton.cs:14
static ShapeConstraint Create_NoOtherSurface()
Constructs a constraint required no other surfaces be included in this shape