Exemple #1
0
    private bool TryPlaceObject(
        out SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult placementResult,
        string placementName,
        SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition placementDefinition,
        List <Rule> rules = null)
    {
        placementResult = null;
        List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule>       placementRules       = new List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule>();
        List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint> placementConstraints = new List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint>();

        if (rules != null)
        {
            foreach (Rule rule in rules)
            {
                rule.AddTo(placementRules);
                rule.AddTo(placementConstraints);
            }
        }
        int result = SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(
            placementName,
            SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementDefinition),
            placementRules.Count,
            placementRules.Count > 0 ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementRules.ToArray()) : IntPtr.Zero,
            placementConstraints.Count,
            placementConstraints.Count > 0 ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementConstraints.ToArray()) : IntPtr.Zero,
            SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr());

        if (result > 0)
        {
            placementResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult();
            return(true);
        }
        return(false);
    }
Exemple #2
0
    private PlacementResult PlaceObject(string placementName,
                                        SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition placementDefinition,
                                        Vector3 boxFullDims,
                                        ObjectType objType,
                                        List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule> placementRules             = null,
                                        List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint> placementConstraints = null)

    {
        //Solver_PlaceObject()返回0代表失败,返回1代表成功
        //故判断是否大于0,成功则获取放置结果
        //PinObject直接返回指定的物体在内存中的位置
        if (SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(
                placementName,
                SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementDefinition),
                (placementRules != null) ? placementRules.Count : 0,
                ((placementRules != null) && (placementRules.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementRules.ToArray()) : IntPtr.Zero,
                (placementConstraints != null) ? placementConstraints.Count : 0,
                (placementConstraints != null) && (placementConstraints.Count > 0)?SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementConstraints.ToArray()):IntPtr.Zero,
                SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr())
            > 0)
        {
            SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult placementResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult();

            return(new PlacementResult(placementResult.Clone() as SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult, boxFullDims, objType));
        }
        return(null);
    }
Exemple #3
0
    private bool TryPlaceObject(
        out SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult placementResult,
        string placementName,
        PlacementQuery query)
    {
        placementResult = null;
        if (!useSpatialUnderstanding)
        {
            return(false);
        }
        int result = SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(
            placementName,
            SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(query.placementDefinition),
            (query.placementRules != null) ? query.placementRules.Count : 0,
            ((query.placementRules != null) && (query.placementRules.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(query.placementRules.ToArray()) : IntPtr.Zero,
            (query.placementConstraints != null) ? query.placementConstraints.Count : 0,
            ((query.placementConstraints != null) && (query.placementConstraints.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(query.placementConstraints.ToArray()) : IntPtr.Zero,
            SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr());

        if (result > 0)
        {
            placementResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult();
            return(true);
        }
        return(false);
    }
Exemple #4
0
    private bool PlaceObject(
        string placementName,
        GameObject input,
        SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition placementDefinition,
        List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule> placementRules             = null,
        List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint> placementConstraints = null,
        bool clearObjectsFirst = true,
        bool isASync           = false)
    {
        isASync = false;
        // Clear objects (if requested)
        if (!isASync && clearObjectsFirst)
        {
        }
        if (!SpatialUnderstanding.Instance.AllowSpatialUnderstanding)
        {
            return(false);
        }

        // New query
        if (SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(
                placementName,
                SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementDefinition),
                (placementRules != null) ? placementRules.Count : 0,
                ((placementRules != null) && (placementRules.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementRules.ToArray()) : IntPtr.Zero,
                (placementConstraints != null) ? placementConstraints.Count : 0,
                ((placementConstraints != null) && (placementConstraints.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementConstraints.ToArray()) : IntPtr.Zero,
                SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr()) > 0)
        {
            SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult placementResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult();
            if (!isASync)
            {
                // Add to instantiation list here

                // If not running async, we can just add the results to the draw list right now
                AppState.Instance.ObjectPlacementDescription = placementName + " (1)";

                placementResults.Add(new PlacementResult(input, 1.0f, placementResult.Clone() as SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult));
                Quaternion thisRotation = Quaternion.LookRotation(placementResult.Forward, placementResult.Up);
                //print("Space Found");
                objectsToMake.Add(new ObjectToInstantiate(input, placementResult.Position, thisRotation));
                //print(objectsToMake.Count);
            }
            else
            {
                queryStatus.QueryResult.Add(placementResult.Clone() as SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult);
            }
            return(true);
        }
        if (!isASync)
        {
            print("No Spaces Found!");
            AppState.Instance.ObjectPlacementDescription = "Placement Failed";
        }
        return(false);
    }
Exemple #5
0
        private bool PlaceObject(
            string placementName,
            SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition placementDefinition,
            List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule> placementRules             = null,
            List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint> placementConstraints = null,
            bool clearObjectsFirst = true,
            bool isASync           = false)
        {
            // Clear objects (if requested)
            if (!isASync && clearObjectsFirst)
            {
                ClearGeometry();
            }
            if (!SpatialUnderstanding.Instance.AllowSpatialUnderstanding)
            {
                return(false);
            }

            // New query
            if (SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(
                    placementName,
                    SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementDefinition),
                    (placementRules != null) ? placementRules.Count : 0,
                    ((placementRules != null) && (placementRules.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementRules.ToArray()) : IntPtr.Zero,
                    (placementConstraints != null) ? placementConstraints.Count : 0,
                    ((placementConstraints != null) && (placementConstraints.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementConstraints.ToArray()) : IntPtr.Zero,
                    SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr()) > 0)
            {
                SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult placementResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult();
                if (!isASync)
                {
                    // If not running async, we can just add the results to the draw list right now
                    AppState.Instance.ObjectPlacementDescription = placementName + " (1)";
                    float timeDelay = (float)placementResults.Count * AnimatedBox.DelayPerItem;
                    placementResults.Add(new PlacementResult(timeDelay, placementResult.Clone() as SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult));
                }
                else
                {
                    queryStatus.QueryResult.Add(placementResult.Clone() as SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult);
                }
                return(true);
            }
            if (!isASync)
            {
                AppState.Instance.ObjectPlacementDescription = placementName + " (0)";
            }
            return(false);
        }
Exemple #6
0
 private bool _PlaceGameObject(string name, SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition placementDefinition, List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule> placementRules = null, List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint> placementConstraints = null)
 {
     return
         (
         SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject
         (
             name,
             SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementDefinition),
             (placementRules != null) ? placementRules.Count : 0,
             ((placementRules != null) && (placementRules.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementRules.ToArray()) : System.IntPtr.Zero,
             (placementConstraints != null) ? placementConstraints.Count : 0,
             ((placementConstraints != null) && (placementConstraints.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementConstraints.ToArray()) : System.IntPtr.Zero,
             SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr()
         ) > 0
         );
 }
Exemple #7
0
    void CreateScene()
    {
        // DLLの初期化
        SpatialUnderstandingDllObjectPlacement.Solver_Init();

        var halfBoxDims = boxFullDims * .5f;
        // 他のオブジェクトから離す距離
        var disctanceFromOtherObjects = halfBoxDims.x > halfBoxDims.z ? halfBoxDims.x * 3f : halfBoxDims.z * 3f;
        // 作成したいオブジェクトの数
        var desiredLocationCount = 3;

        for (int i = 0; i < desiredLocationCount; ++i)
        {
            // ルールの作成(複数追加可能)
            var placementRules = new List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule>();
            placementRules.Add(SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule.Create_AwayFromOtherObjects(disctanceFromOtherObjects));

            // 制約の作成(複数追加可能)
            var placementConstraints = new List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint>();
            placementConstraints.Add(SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint.Create_AwayFromOtherObjects());

            // 定義の作成(1つだけ)
            SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition placementDefinition = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnFloor(halfBoxDims);

            int ret = SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(
                "my placement",
                SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementDefinition),
                placementRules.Count,
                SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementRules.ToArray()),
                placementConstraints.Count,
                SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementConstraints.ToArray()),
                SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr()
                );

            if (ret > 0)
            {
                SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult placementResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult();

                var rotation = Quaternion.LookRotation(placementResult.Forward, Vector3.up);
                var obj      = Instantiate(toPlaceObj, placementResult.Position, rotation);

                print("Placed:" + obj.transform.position);
            }
        }
    }
Exemple #8
0
 IEnumerator ObjectPlacement()
 {
     SpawnInformation.PlacementQuery query = SpawnInformation.QueryByPlacementType(PlacementType, HalfDims);
     //Mit Definition nicht so sicher (Online-Beispiel ist falsch bzw. nicht komplett)
     if (SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(Prefab.name,
                                                                   _understandingDll.PinObject(query.PlacementDefinition),
                                                                   query.PlacementRules != null?query.PlacementRules.Count:0,
                                                                   _understandingDll.PinObject(query.PlacementRules.ToArray()),
                                                                   query.PlacementConstraints != null ? query.PlacementConstraints.Count : 0,
                                                                   _understandingDll.PinObject(query.PlacementConstraints.ToArray()),
                                                                   _understandingDll.GetStaticObjectPlacementResultPtr()) > 0)
     {
         SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult placementResult =
             _understandingDll.GetStaticObjectPlacementResult();
         Quaternion rot = Quaternion.LookRotation(placementResult.Forward, Vector3.up);
         Instantiate(Prefab, placementResult.Position, rot);
     }
     else
     {
         Debug.Log("Couldn't spawn object");
     }
     yield return(null);
 }
Exemple #9
0
 private PlacementResult PlaceObject(string placementName,
                                     SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition placementDefinition,
                                     Vector3 boxFullDims,
                                     ObjectType objType,
                                     List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule> placementRules             = null,
                                     List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint> placementConstraints = null)
 {
     // New query
     if (SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(
             placementName,
             SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementDefinition),
             (placementRules != null) ? placementRules.Count : 0,
             ((placementRules != null) && (placementRules.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementRules.ToArray()) : IntPtr.Zero,
             (placementConstraints != null) ? placementConstraints.Count : 0,
             ((placementConstraints != null) && (placementConstraints.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementConstraints.ToArray()) : IntPtr.Zero,
             SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr()) > 0)
     {
         SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult placementResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult();
         Debug.Log("TO BE Placed:---------" + (objType == ObjectType.Menu ? "turtorial":"object"));
         return(new PlacementResult(placementResult.Clone() as SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult, boxFullDims, objType));
     }
     Debug.Log("Not Placed:-----------" + (objType == ObjectType.Menu ? "turtorial":"object"));
     return(null);
 }
Exemple #10
0
    bool RunQuery(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition placementDefinition,
                  List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule> placementRules             = null,
                  List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint> placementConstraints = null)
    {
        if (SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(
                this.name,
                SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementDefinition),
                (placementRules != null) ? placementRules.Count : 0,
                ((placementRules != null) && (placementRules.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementRules.ToArray()) : IntPtr.Zero,
                (placementConstraints != null) ? placementConstraints.Count : 0,
                ((placementConstraints != null) && (placementConstraints.Count > 0)) ? SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementConstraints.ToArray()) : IntPtr.Zero,
                SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr()) > 0)
        {
            SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult placementResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult();

            queryPlacementResults.Add(placementResult.Clone() as SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult);

            return(true);
        }

        State = States.Finished;

        return(true);
    }
Exemple #11
0
    private IEnumerator SetupMenu()
    {
        // Setup for queries
        SpatialUnderstandingDllTopology.TopologyResult[] resultsTopology = new SpatialUnderstandingDllTopology.TopologyResult[1];
        IntPtr resultsTopologyPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(resultsTopology);

#if UNITY_WSA && !UNITY_EDITOR
        // Place on a wall (do it in a thread, as it can take a little while)
        SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition placeOnWallDef =
            SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnWall(new Vector3(MenuWidth * 0.5f, MenuHeight * 0.5f, MenuMinDepth * 0.5f), 0.5f, 3.0f);
        SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult placementResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult();
        System.Threading.Tasks.Task thread = System.Threading.Tasks.Task.Run(() =>
        {
            if (SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(
                    "UIPlacement",
                    SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placeOnWallDef),
                    0,
                    IntPtr.Zero,
                    0,
                    IntPtr.Zero,
                    SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr()) == 0)
            {
                placementResult = null;
            }
        });
        while (!thread.IsCompleted)
        {
            yield return(null);
        }
        if (placementResult != null)
        {
            Debug.Log("PlaceMenu - ObjectSolver-OnWall");
            Vector3 posOnWall = placementResult.Position - placementResult.Forward * MenuMinDepth * 0.5f;
            PlaceMenu(posOnWall, -placementResult.Forward);
            yield break;
        }
#endif

        // Wait a frame
        yield return(null);

        // Fallback, place floor (add a facing, if so)
        int locationCount = SpatialUnderstandingDllTopology.QueryTopology_FindLargestPositionsOnFloor(
            resultsTopology.Length, resultsTopologyPtr);
        if (locationCount > 0)
        {
            Debug.Log("PlaceMenu - LargestPositionsOnFloor");
            SpatialUnderstandingDllTopology.TopologyResult menuLocation = resultsTopology[0];
            Vector3 menuPosition   = menuLocation.position + Vector3.up * MenuHeight;
            Vector3 menuLookVector = Camera.main.transform.position - menuPosition;
            PlaceMenu(menuPosition, (new Vector3(menuLookVector.x, 0.0f, menuLookVector.z)).normalized, true);
            yield break;
        }

        // Final fallback just in front of the user
        SpatialUnderstandingDll.Imports.QueryPlayspaceAlignment(SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticPlayspaceAlignmentPtr());
        SpatialUnderstandingDll.Imports.PlayspaceAlignment alignment = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticPlayspaceAlignment();
        Vector3 defaultPosition = Camera.main.transform.position + Camera.main.transform.forward * 2.0f;
        PlaceMenu(new Vector3(defaultPosition.x, Math.Max(defaultPosition.y, alignment.FloorYValue + 1.5f), defaultPosition.z), (new Vector3(Camera.main.transform.forward.x, 0.0f, Camera.main.transform.forward.z)).normalized, true);
        Debug.Log("PlaceMenu - InFrontOfUser");
    }
Exemple #12
0
    /// <summary>
    /// Place given object
    /// </summary>
    /// <param name="placementObject">The object to place</param>
    /// <param name="requiredSpace">The bounds of the object to place</param>
    /// <param name="requiredSurfaceSpace">The bounds of the bottom of the shape to place, only used for edge and floor/ceiling placement</param>
    /// <returns></returns>
    public STPlacementResult Place(GameObject placementObject, Vector3 requiredSpace, Vector3 requiredSurfaceSpace = default(Vector3))
    {
        if (!solverIsInit)
        {
            solverIsInit = SpatialUnderstandingDllObjectPlacement.Solver_Init() == 1;
        }

        Result = new SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult();

        List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule>       nativeRules       = new List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule>();
        List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint> nativeConstraints = new List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint>();

        foreach (STPlacementRule rule in Rules)
        {
            nativeRules.Add(rule.ToNativeRule());
        }

        foreach (STPlacementConstraint con in Constraints)
        {
            nativeConstraints.Add(con.ToNativeConstraint());
        }

        IntPtr rulePtr       = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(nativeRules.ToArray());
        IntPtr constraintPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(nativeConstraints.ToArray());
        IntPtr definitionPtr = IntPtr.Zero;

        switch (Type)
        {
        case STPlacementType.Floor:
            definitionPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnFloor(requiredSpace));
            break;

        case STPlacementType.Wall:
            definitionPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnWall(requiredSpace, MinHeight, MaxHeight));
            break;

        case STPlacementType.Ceiling:
            definitionPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnCeiling(requiredSpace));
            break;

        case STPlacementType.Shape:
            definitionPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnShape(requiredSpace, TargetShape.ShapeName, TargetShape.GetComponentIndex(TargetShapeComponent)));
            break;

        case STPlacementType.Edge:
            definitionPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnEdge(requiredSpace, requiredSurfaceSpace));
            break;

        case STPlacementType.FloorAndCeiling:
            definitionPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnFloorAndCeiling(requiredSpace, requiredSurfaceSpace));
            break;

        case STPlacementType.RandomInAir:
            definitionPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_RandomInAir(requiredSpace));
            break;

        case STPlacementType.MidAir:
            definitionPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_InMidAir(requiredSpace));
            break;

        case STPlacementType.UnderPlatformEdge:
            definitionPtr = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_UnderPlatformEdge(requiredSpace));
            break;

        default:
            return(null);
        }

        if (SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(QueryName + "_" + placementObject.name, definitionPtr, Rules.Count, rulePtr, Constraints.Count, constraintPtr, SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr()) > 0)
        {
            _hasRun = true;
            Debug.Log("Placing object for query " + QueryName);

            Result = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult();

            GameObject resultantObject = GameObject.Instantiate(placementObject, Result.Position, Quaternion.LookRotation(Result.Forward, Result.Up));
            if (resultantObject.GetComponent <STPlacedObject>() == null)
            {
                STPlacedObject placedObject = resultantObject.AddComponent <STPlacedObject>();
                placedObject.PlacedObjectName = QueryName + "_" + placementObject.name;
            }
            // TODO: Add a component to the object to remove it from Spatial Understanding if removed in Unity.
            return(new STPlacementResult(resultantObject, Result));
        }
        else
        {
            Debug.Log("Failed to place object for query " + QueryName);
            _hasRun = true;
            return(null);
        }
    }
Exemple #13
0
        /// <summary>
        /// Attempts to place an object given the parameters.
        /// Returns false if object could not be placed, or spatial mapping could not be initialized.
        /// </summary>
        /// <param name="target"></param>
        /// <param name="position"></param>
        /// <param name="customHalfDims"></param>
        /// <param name="minDistFromOthers"></param>
        /// <param name="minDistFromPlayer"></param>
        /// <param name="maxDistFromPlayer"></param>
        /// <returns></returns>
        public static bool PlaceObject(GameObject target, WrappedPlacement position, Vector3?customHalfDims = null, float minDistFromOthers = 3f, float minDistFromPlayer = .25f, float maxDistFromPlayer = 4.0f)
        {
            if (!Init())
            {
                return(false);
            }

            Vector3  halfDims = Vector3.one * .5f;
            Renderer r        = target.GetComponentInChildren <Renderer>();

            if (r != null)
            {
                halfDims = (r.bounds.size * .5f);
                WorldErrors.Print("halfDims: " + (r.bounds.size * .5f).ToString());
            }

            if (customHalfDims != null)
            {
                halfDims = customHalfDims.Value;
            }

            List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule> placementRules = new List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule>()
            {
                SpatialUnderstandingDllObjectPlacement.ObjectPlacementRule.Create_AwayFromOtherObjects(halfDims.magnitude * minDistFromOthers)
            };

            List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint> placementConstraints = new List <SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint>()
            {
                SpatialUnderstandingDllObjectPlacement.ObjectPlacementConstraint.Create_NearPoint(Camera.main.transform.position, minDistFromPlayer, maxDistFromPlayer)
            };

            SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition def = new SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition();

            switch (position)
            {
            case WrappedPlacement.Place_InMidAir:
                def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_InMidAir(halfDims);
                break;

            case WrappedPlacement.Place_OnCeiling:
                def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnCeiling(halfDims);
                break;

            case WrappedPlacement.Place_OnEdge: {
                Vector3 halfDimsBot = halfDims;
                halfDimsBot.y *= .5f;
                def            = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnEdge(halfDims, halfDimsBot);
            }
            break;

            case WrappedPlacement.Place_OnFloor:
                def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnFloor(halfDims);
                break;

            case WrappedPlacement.Place_OnFloorAndCeiling: {
                Vector3 halfDimsBot = halfDims;
                halfDimsBot.y *= .5f;
                def            = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnFloorAndCeiling(halfDims, halfDimsBot);
            }
            break;

            case WrappedPlacement.Place_OnShape:
                Debug.LogWarning("Not supported");
                def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnShape(halfDims, "DefaultShape", 0);
                break;

            case WrappedPlacement.Place_OnWall:
                def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_OnWall(halfDims, halfDims.y, 2.4f);
                break;

            case WrappedPlacement.Place_RandomInAir:
                def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_RandomInAir(halfDims);
                break;

            case WrappedPlacement.Place_UnderPlatformEdge:
                def = SpatialUnderstandingDllObjectPlacement.ObjectPlacementDefinition.Create_UnderPlatformEdge(halfDims);
                break;
            }

            //SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult result = new SpatialUnderstandingDllObjectPlacement.ObjectPlacementResult();
            if (SpatialUnderstandingDllObjectPlacement.Solver_PlaceObject(target.name,
                                                                          SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(def),
                                                                          placementRules.Count, SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementRules.ToArray()),             //rules
                                                                          placementConstraints.Count, SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(placementConstraints.ToArray()), //constraints
                                                                          SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResultPtr()
                                                                          ) > 0)
            {
                target.transform.position = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticObjectPlacementResult().Position;
                return(true);
            }

            return(false);
        }