private bool AddShape( string shapeName, List <SpatialUnderstandingDllShapes.ShapeComponent> shapeComponents, List <SpatialUnderstandingDllShapes.ShapeConstraint> shapeConstraints) { if (!SpatialUnderstanding.Instance.AllowSpatialUnderstanding) { return(false); } IntPtr shapeComponentsPtr = (shapeComponents == null) ? IntPtr.Zero : HoloToolkit.Unity.SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(shapeComponents.ToArray()); IntPtr shapeConstraintsPtr = (shapeConstraints == null) ? IntPtr.Zero : HoloToolkit.Unity.SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(shapeConstraints.ToArray()); if (SpatialUnderstandingDllShapes.AddShape( shapeName, (shapeComponents == null) ? 0 : shapeComponents.Count, shapeComponentsPtr, (shapeConstraints == null) ? 0 : shapeConstraints.Count, shapeConstraintsPtr) == 0) { Debug.LogError("Failed to create custom shape description"); return(false); } customShapeDefinitions.Add(shapeName); return(true); }
/// <summary> /// /// </summary> private void CreateCustomShapeDefinitions() { if (!SpatialUnderstanding.Instance.AllowSpatialUnderstanding) { return; } List <SpatialUnderstandingDllShapes.ShapeComponent> shapeComponents; List <SpatialUnderstandingDllShapes.ShapeConstraint> shapeConstraints; //add any shape components and constraints here and use AddShape to name it and add it to the list of shapes //placeable shapeComponents = new List <SpatialUnderstandingDllShapes.ShapeComponent>() { new SpatialUnderstandingDllShapes.ShapeComponent( new List <SpatialUnderstandingDllShapes.ShapeComponentConstraint>() { SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_SurfaceHeight_Between(0.3f, 1.6f), SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_SurfaceCount_Min(1), SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_SurfaceArea_Min(0.1f), }), }; IntPtr shapeComponentsPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(shapeComponents.ToArray()); SpatialUnderstandingDllShapes.AddShape("Placeable", shapeComponents.Count, shapeComponentsPtr, 0, IntPtr.Zero); }
public void Register() { List <SpatialUnderstandingDllShapes.ShapeComponent> compList = new List <SpatialUnderstandingDllShapes.ShapeComponent>(); List <SpatialUnderstandingDllShapes.ShapeConstraint> conList = new List <SpatialUnderstandingDllShapes.ShapeConstraint>(); foreach (STShapeComponent c in Components) { compList.Add(new SpatialUnderstandingDllShapes.ShapeComponent(c.GetConstraints())); } foreach (STShapeConstraint c in Constraints) { conList.Add(c.ToNativeConstraint()); } IntPtr componentsPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(compList.ToArray()); IntPtr constraintsPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(conList.ToArray()); if (SpatialUnderstandingDllShapes.AddShape(ShapeName, compList.Count, componentsPtr, conList.Count, constraintsPtr) == 0) { Debug.LogError("Failed to create custom shape"); } else { Debug.Log("Added Shape " + ShapeName + " with " + Components.Count + " components and " + Constraints.Count + " constraints"); } }
void RegisterShapes() { foreach (STShape s in Shapes) { s.Register(); } SpatialUnderstandingDllShapes.ActivateShapeAnalysis(); }
/// <summary> /// Create shape definitions, add them and analyze the definitions to get the results to query later /// </summary> private void FindShapes() { if (HasCreatedShapes || !SpatialUnderstanding.Instance.AllowSpatialUnderstanding) { return; } CreateShapeDefinitions(); SpatialUnderstandingDllShapes.ActivateShapeAnalysis(); }
/** * Create shape definitions. */ public void CreateShapes() { if (this.HasCreatedShapes || !SpatialUnderstanding.IsInitialized || !SpatialUnderstanding.Instance.AllowSpatialUnderstanding) { return; } // Create definitions and analyze this.CreateCustomShapeDefinitions(); SpatialUnderstandingDllShapes.ActivateShapeAnalysis(); }
public void CreateShapes() { if (HasCreatedShapes || !SpatialUnderstandingManager.Instance.AllowSpatialUnderstanding) { return; } // Create definitions and analyze CreateCustomShapeDefinitions(); SpatialUnderstandingDllShapes.ActivateShapeAnalysis(); }
/** * Query the SpatialUnderstanding DLL for a specific shape. * * @param shapeName name of the shape that should be detected * @param nearPos position near which the shape should be detected * @param drawBox indicates whether an animated box should be drawn around the shape * @param callback callback method */ private void Query_Shape_FindShapeHalfDims(string shapeName, Vector3?nearPos, bool drawBox, ResponseDelegate callback) { // Check query permission if (!SpatialUnderstanding.IsInitialized || !SpatialUnderstanding.Instance.AllowSpatialUnderstanding) { return; } // Query System.IntPtr resultsShapePtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(this.resultsShape); int shapeCount = SpatialUnderstandingDllShapes.QueryShape_FindShapeHalfDims(shapeName, this.resultsShape.Length, resultsShapePtr); // Output this.HandleResults("Find Shape Min/Max '" + shapeName + "'", shapeCount, Color.blue, new Vector3(0.25f, 0.025f, 0.25f), nearPos, drawBox, callback); }
/* * private void LogSurfaceState() * { * IntPtr statPtr = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticPlayspaceStatsPtr(); * if (SpatialUnderstandingDll.Imports.QueryPlayspaceStats(statPtr) != 0) * { * var stats = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticPlayspaceStats(); * string debugInfo = String.Format("TotalSurfaceArea: {0:0.##}\n" + * "WallSurfaceArea: {1:0.##}\n" + * "HorizSurfaceArea: {2:0.##}", * stats.TotalSurfaceArea, * stats.WallSurfaceArea, * stats.HorizSurfaceArea); * Debug.Log(debugInfo); * } * } */ void AddShapeDefinition(string shapeName, List <SpatialUnderstandingDllShapes.ShapeComponent> shapeComponent, List <SpatialUnderstandingDllShapes.ShapeConstraint> shapeConstraint) { IntPtr shapeComponentsPtr = (shapeComponent == null) ? IntPtr.Zero : SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(shapeComponent.ToArray()); IntPtr shapeConstraintsPtr = (shapeConstraint == null) ? IntPtr.Zero : SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(shapeConstraint.ToArray()); if (SpatialUnderstandingDllShapes.AddShape(shapeName, (shapeComponent == null) ? 0 : shapeComponent.Count, shapeComponentsPtr, (shapeConstraint == null) ? 0 : shapeConstraint.Count, shapeConstraintsPtr) == 0) { SpatialText.text = "Failed to create object"; } }
public void Query_Shape_FindShapeHalfDims(string shapeName) { // Only if we're enabled if (!SpatialUnderstanding.Instance.AllowSpatialUnderstanding) { return; } // Query IntPtr resultsShapePtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(resultsShape); int shapeCount = SpatialUnderstandingDllShapes.QueryShape_FindShapeHalfDims( shapeName, resultsShape.Length, resultsShapePtr); // Output HandleResults_Shape("Find Shape Min/Max '" + shapeName + "'", shapeCount, Color.blue, new Vector3(0.25f, 0.025f, 0.25f)); }
/// <summary> /// /// </summary> public void InstantiateObjectOnPlaceable() { const int QueryResultMaxCount = 512; SpatialUnderstandingDllShapes.ShapeResult[] resultsShape = new SpatialUnderstandingDllShapes.ShapeResult[QueryResultMaxCount]; // Pin managed object memory going to native code IntPtr resultsShapePtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(resultsShape); // Find the half dimensions of "Sittable" objects via the DLL int shapeCount = SpatialUnderstandingDllShapes.QueryShape_FindShapeHalfDims("Placeable", resultsShape.Length, resultsShapePtr); for (int i = 0; i < shapeCount; i++) { Instantiate(PlaceablePrefab, resultsShape[i].position, Quaternion.identity); } }
/** * Add a new shape definition. * * @param shapeName shape name * @param shapeComponents list of shape components * @param shapeConstraints list of shape constraints */ private void AddShape(string shapeName, List <ShapeComp> shapeComponents, List <ShapeConst> shapeConstraints = null) { if (!SpatialUnderstanding.IsInitialized || !SpatialUnderstanding.Instance.AllowSpatialUnderstanding) { return; } IntPtr shapeComponentsPtr = (shapeComponents == null) ? IntPtr.Zero : SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(shapeComponents.ToArray()); IntPtr shapeConstraintsPtr = (shapeConstraints == null) ? IntPtr.Zero : SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(shapeConstraints.ToArray()); int shapeComponentCount = (shapeComponents == null) ? 0 : shapeComponents.Count; int shapeConstraintCount = (shapeConstraints == null) ? 0 : shapeConstraints.Count; if (SpatialUnderstandingDllShapes.AddShape(shapeName, shapeComponentCount, shapeComponentsPtr, shapeConstraintCount, shapeConstraintsPtr) == 0) { Debug.LogError("Failed to create custom shape description."); } }
public void Query_Shape_FindPositionsOnShape(string shapeName) { // Setup float minRadius = 0.1f; // Only if we're enabled if (!SpatialUnderstanding.Instance.AllowSpatialUnderstanding) { return; } // Query IntPtr resultsShapePtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(resultsShape); int shapeCount = SpatialUnderstandingDllShapes.QueryShape_FindPositionsOnShape( shapeName, minRadius, resultsShape.Length, resultsShapePtr); // Output HandleResults_Shape("Find Positions on Shape '" + shapeName + "'", shapeCount, Color.cyan, new Vector3(0.1f, 0.025f, 0.1f)); }
private void CreateElectricBoxShape() { var shapeComponents = new List <SpatialUnderstandingDllShapes.ShapeComponent>() { new SpatialUnderstandingDllShapes.ShapeComponent( new List <SpatialUnderstandingDllShapes.ShapeComponentConstraint>() { SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_IsRectangle(0.1f), SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_RectangleLength_Between(0.4f, 0.6f), SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_RectangleWidth_Between(0.3f, 0.5f) }), new SpatialUnderstandingDllShapes.ShapeComponent( new List <SpatialUnderstandingDllShapes.ShapeComponentConstraint>() { SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_IsRectangle(0.1f), SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_RectangleLength_Between(0.15f, 0.2f), SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_RectangleWidth_Between(0.3f, 0.5f) }), new SpatialUnderstandingDllShapes.ShapeComponent( new List <SpatialUnderstandingDllShapes.ShapeComponentConstraint>() { SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_IsRectangle(0.1f), SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_RectangleLength_Between(0.4f, 0.6f), SpatialUnderstandingDllShapes.ShapeComponentConstraint.Create_RectangleWidth_Between(0.15f, 0.2f) }), }; var shapeConstraints = new List <SpatialUnderstandingDllShapes.ShapeConstraint>() { SpatialUnderstandingDllShapes.ShapeConstraint.Create_RectanglesSameLength(0, 2), SpatialUnderstandingDllShapes.ShapeConstraint.Create_RectanglesPerpendicular(0, 1), SpatialUnderstandingDllShapes.ShapeConstraint.Create_RectanglesPerpendicular(0, 2) }; AddShapeDefinition("ElectricBox", shapeComponents, shapeConstraints); SpatialUnderstandingDllShapes.ActivateShapeAnalysis(); }
public int RunQuery() { Results = new SpatialUnderstandingDllShapes.ShapeResult[_queryMaxResultCount]; IntPtr resultsShapePtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(Results); int shapeCount = 0; if (String.IsNullOrEmpty(ShapeName)) { Debug.LogError("Empty shape name, cannot run query."); return(0); } switch (Type) { case STShapeQueryType.ShapeBounds: shapeCount = SpatialUnderstandingDllShapes.QueryShape_FindShapeHalfDims(ShapeName, Results.Length, resultsShapePtr); break; case STShapeQueryType.PositionsOnShape: shapeCount = SpatialUnderstandingDllShapes.QueryShape_FindPositionsOnShape(ShapeName, _minRadius, Results.Length, resultsShapePtr); break; } List <SpatialUnderstandingDllShapes.ShapeResult> resultTemp = new List <SpatialUnderstandingDllShapes.ShapeResult>(); for (int i = 0; i < shapeCount; i++) { resultTemp.Add(Results[i]); } Results = resultTemp.ToArray(); _hasRun = true; return(shapeCount); }
private void InstantiateObjectOnTable() { const int MaxResultCount = 512; var shapeResults = new SpatialUnderstandingDllShapes.ShapeResult[MaxResultCount]; var resultsShapePtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(shapeResults); var locationCount = SpatialUnderstandingDllShapes.QueryShape_FindPositionsOnShape("ElectricBox", 0.1f, shapeResults.Length, resultsShapePtr); if (locationCount > 0) { Instantiate(_object, shapeResults[0].position, Quaternion.LookRotation(shapeResults[0].position.normalized, Vector3.up)); // For some reason the halfDims of the shape result are always 0,0,0 so we can't scale // to the size of the surface. This may be a bug in the HoloToolkit? SpatialText.text = "Placed Hologram"; } else { SpatialText.text = "Hologram not placed"; } }
//if any problems arises (i.e. fps spikes when placing), this function will need to be split in multiple stages. //Then, rest must be given to Unity between the stages. I.e. call the stages one at a time in the Update() part. Don't call multiple stages in the same Update() call. //placePlat handles the placement for the Platform type of placement. //it relies on the SpatialUnderstandingDllShapes API. To add other Shape Definitions, see the ShapeDefinition script from HoloToolkit-Example/SpatialUnderstanding scripts private void placePlat() { List <Vector3>[] lPositions = new List <Vector3> [mPlatformLeftToPlace.Count]; SpatialUnderstandingDllShapes.ShapeResult[] lPlatPos = new SpatialUnderstandingDllShapes.ShapeResult[mMaxPlatLoc]; System.IntPtr lPlatPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(lPlatPos); for (int i = 0; i < mPlatformLeftToPlace.Count; i++) //prepare to find all positions for each clue { lPositions[i] = new List <Vector3>(); } for (int lIt = 0; lIt < mPlatformLeftToPlace.Count; lIt++) //here we find all Platform positions for each clue { int i = mPlatformLeftToPlace[lIt]; Collider lColl = mToPlace[i].GetComponent <Collider>(); float lMax = Mathf.Max(lColl.bounds.extents.x, Mathf.Max(lColl.bounds.extents.y, lColl.bounds.extents.z)); int lPlatCount = SpatialUnderstandingDllShapes.QueryShape_FindPositionsOnShape( "Platform", 1.1f * lMax, lPlatPos.Length, lPlatPtr); for (int lPlatIt = 0; lPlatIt < lPlatCount; lPlatIt++) { lPositions[lIt].Add(lPlatPos[lPlatIt].position); } if (lPositions[lIt].Count == 0) //if there is no position for an object, just exit. The placement failed. { lIt = mPlatformLeftToPlace.Count + 2; } } int lItObj; //here we choose a random position from all our positions for each clue. //since there might be multiple clues of the same size on platforms, those will have exactly the same positions. //So it would be faster to try to select a position at random than collide for a while on the first positions for (lItObj = 0; lItObj < mPlatformLeftToPlace.Count; lItObj++) { int iObj = mPlatformLeftToPlace[lItObj]; int lItPos = 2; bool lPlaced = false; while ((lPositions[lItObj].Count != 0) && (!lPlaced)) //choose positions at random until we run out of positions { lItPos = Random.Range(0, lPositions[lItObj].Count - 1); //put object correctly on position. Without placing it upper than the position returned, we would have the center of the object at the position on the Platform //which essentially means the lower half will go through the Platform mToPlace[iObj].transform.localPosition = lPositions[lItObj][lItPos] + mToPlace[iObj].GetComponent <Collider>().bounds.extents.y *Vector3.up; if (canPlace(iObj)) //can place the object at this position { mPlaced.Add(iObj); lPlaced = true; } else { lPositions[lItObj].RemoveAt(lItPos); //position unusable } } if (!lPlaced) //couldn't place the object, so just exit this for loop. The placement failed. { lItObj = mPlatformLeftToPlace.Count + 2; mToPlace[iObj].transform.localPosition = new Vector3(0, -1000, 0); } } if (lItObj == mPlatformLeftToPlace.Count) //successfully placed all the objects { mPlacePComplete = true; } else //could not place the objects { mPlacePComplete = false; mSuccessful = false; mComplete = true; } }
/// <summary> /// /// </summary> public void CreateShapes() { // Create definitions and analyze CreateCustomShapeDefinitions(); SpatialUnderstandingDllShapes.ActivateShapeAnalysis(); }
/** * Remove all shape definitions. */ public void Clear() { this.HasCreatedShapes = false; SpatialUnderstandingDllShapes.RemoveAllShapes(); }
//Examples of ways to et locations and store them in location list. private IEnumerator GetSpawnLocations() { yield return(new WaitForSeconds(3)); //IntPtr resultsTopologyPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(resultsTopology); //int locationCount = SpatialUnderstandingDllTopology.QueryTopology_FindLargestPositionsOnFloor( // resultsTopology.Length, resultsTopologyPtr); //Debug.Log(locationCount); //Debug.Log(resultsTopology[0].position); //END OF TEST EXAMPLES.. FINDING LOCATIONS FOR THE FLOOR NOW. SpatialUnderstandingDllObjectPlacement.Solver_RemoveAllObjects(); SpaceVisualizer.Instance.ClearGeometry(); IntPtr resultsTopologyPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(resultsTopology); int locationCount = SpatialUnderstandingDllTopology.QueryTopology_FindPositionsOnFloor( GameManager.Instance.shipManager.shipMaxHeight, GameManager.Instance.shipManager.shipMinHeight, resultsTopology.Length, resultsTopologyPtr); foreach (SpatialUnderstandingDllTopology.TopologyResult potentialShipFloor in resultsTopology) { SpatialLocation shipFloor = new SpatialLocation(); shipFloor.position = potentialShipFloor.position; shipFloor.normal = potentialShipFloor.normal; shipFloor.name = "shipFloor"; // this sould be a descriptive name of what positin is. spatialLocationList.Add(shipFloor); } // GETTING SPAWN LOCATION FOR GRID // SpaceVisualizer.Instance.ClearGeometry(); // resultsTopologyPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(resultsTopology); // locationCount = SpatialUnderstandingDllTopology.QueryTopology_FindLargestPositionsOnFloor( //resultsTopology.Length, resultsTopologyPtr); // SpatialLocation gridSittingLoc = new SpatialLocation(); // gridSittingLoc.position = resultsTopology[0].position; // gridSittingLoc.normal = resultsTopology[0].normal; // gridSittingLoc.name = "gridSittingLoc"; // this sould be a descriptive name of what positin is. // spatialLocationList.Add(gridSittingLoc); //SpatialUnderstandingDllObjectPlacement.Solver_RemoveAllObjects(); //SpaceVisualizer.Instance.ClearGeometry(); //float sitMinHeight = 0f; //float sitMaxHeight = 1.2f; //float sitMinFacingClearance = 2f; //SpatialUnderstandingDllObjectPlacement.Solver_RemoveAllObjects(); //resultsTopologyPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(resultsTopology); //int locationCount3 = SpatialUnderstandingDllTopology.QueryTopology_FindPositionsSittable( // sitMinHeight, sitMaxHeight, sitMinFacingClearance, // resultsTopology.Length, resultsTopologyPtr); //SpatialLocation gridSittingLoc = new SpatialLocation(); //gridSittingLoc.position = resultsTopology[0].position; //gridSittingLoc.normal = resultsTopology[0].normal; //gridSittingLoc.name = "gridSittingLoc"; // this sould be a descriptive name of what positin is. //spatialLocationList.Add(gridSittingLoc); SpatialUnderstandingDllObjectPlacement.Solver_RemoveAllObjects(); SpaceVisualizer.Instance.ClearGeometry(); IntPtr resultsShapePtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(resultsShape); int shapeCount = SpatialUnderstandingDllShapes.QueryShape_FindShapeHalfDims( "Grid", resultsShape.Length, resultsShapePtr); Debug.Log(resultsShape[0].position); Debug.Log(resultsShape[0].halfDims); SpatialLocation gridSittingLoc = new SpatialLocation(); gridSittingLoc.position = resultsShape[0].position; gridSittingLoc.normal = resultsShape[0].halfDims; gridSittingLoc.name = "gridSittingLoc"; // this sould be a descriptive name of what positin is. spatialLocationList.Add(gridSittingLoc); yield return(null); }