/// <summary> /// Instantiate the Unity Prefab that will exist at this node location. /// </summary> /// <param name="hashkey">The spatial hashkey for this tree node.</param> /// <param name="prefab">The Unity Prefab object.</param> /// <param name="parent">The Unity transform of the GameObject parent of the prefab.</param> /// <param name="voxelResolution">The voxel resolution of the meshing cube.</param> private void InstantiatePrefab(int hashkey, GameObject prefab, Transform parent, int voxelResolution) { int x, y, z; ReverseHashKey(hashkey, out x, out y, out z); m_meshPrefab = (GameObject)GameObject.Instantiate(prefab); m_meshPrefab.transform.position = new Vector3(x, y, z); m_meshPrefab.transform.parent = parent; m_dynamicMeshCube = m_meshPrefab.GetComponent <DynamicMeshCube>(); if (m_meshPrefab == null) { Debug.LogError("Game Object does not have the dynamic mesh component"); } m_dynamicMeshCube.SetProperties(voxelResolution); m_dynamicMeshCube.Key = hashkey; }
/// <summary> /// Instantiate the Unity Prefab that will exist at this node location. /// </summary> /// <param name="hashkey">The spatial hashkey for this tree node.</param> /// <param name="prefab">The Unity Prefab object.</param> /// <param name="parent">The Unity transform of the GameObject parent of the prefab.</param> /// <param name="voxelResolution">The voxel resolution of the meshing cube.</param> private void InstantiatePrefab(int hashkey, GameObject prefab, Transform parent, int voxelResolution) { int x, y, z; ReverseHashKey(hashkey, out x, out y, out z); m_meshPrefab = (GameObject)GameObject.Instantiate(prefab); m_meshPrefab.transform.position = new Vector3(x, y, z); m_meshPrefab.transform.parent = parent; m_dynamicMeshCube = m_meshPrefab.GetComponent<DynamicMeshCube>(); if (m_meshPrefab == null) { Debug.LogError("Game Object does not have the dynamic mesh component"); } m_dynamicMeshCube.SetProperties(voxelResolution); m_dynamicMeshCube.Key = hashkey; }
/// <summary> /// Insert 3D point into the hash storage. Creates a new meshing cube at the location if needed. /// </summary> /// <returns>The value of the voxel that received the insertion.</returns> /// <param name="hashkey">Hashkey index of the target node.</param> /// <param name="p">The 3D point.</param> /// <param name="obs">Observation vector from the camera to the point.</param> /// <param name="weight">Weight of the observation.</param> /// <param name="prefab">Unity Prefab to be instantiated at this node location if needed.</param> /// <param name="parent">Unity transform of the parent object for the prefab created at this node location.</param> /// <param name="voxelResolution">Resolution of the voxels for this meshing cube.</param> private float InsertPoint(int hashkey, Vector3 p, Vector3 obs, float weight, GameObject prefab, Transform parent, int voxelResolution) { if (m_hashKey == hashkey) { if (m_meshPrefab == null) { InstantiatePrefab(hashkey, prefab, parent, voxelResolution); } if (m_meshPrefab == null) { m_dynamicMeshCube = m_meshPrefab.GetComponent<DynamicMeshCube>(); } if (m_meshPrefab == null) { Debug.Log("Error: cannot find DynamicMeshVolume"); return 0; } if (m_dynamicMeshCube.IsRegenerating) { return 0; } // adjust weight of mutiple voxels along observation ray float result = m_dynamicMeshCube.InsertPoint(p, obs, weight, ref m_volumeIndex); Vector3 closerPoint = p - (obs * m_dynamicMeshCube.VoxelSize); Vector3 furtherPoint = p + (obs * m_dynamicMeshCube.VoxelSize); // voxel was inside the surface, back out one, and insert in the next closest voxel if (result > 0) { m_dynamicMeshCube.InsertPoint(closerPoint, p, obs, weight); } else { m_dynamicMeshCube.InsertPoint(furtherPoint, p, obs, weight); } if (m_volumeIndex[0] == 0) { int neighborHashKey = hashkey - 1; result = m_rootHashTree.InsertPoint(neighborHashKey, p, obs, weight, prefab, parent, voxelResolution); } if (m_volumeIndex[1] == 0) { int neighborHashKey = hashkey - m_maximumVolumeIndexDimension; result = m_rootHashTree.InsertPoint(neighborHashKey, p, obs, weight, prefab, parent, voxelResolution); } if (m_volumeIndex[2] == 0) { int neighborHashKey = hashkey - (m_maximumVolumeIndexDimension * m_maximumVolumeIndexDimension); result = m_rootHashTree.InsertPoint(neighborHashKey, p, obs, weight, prefab, parent, voxelResolution); } return result; } if (hashkey < m_hashKey) { if (m_leftHashTree == null) { m_leftHashTree = new VolumetricHashTree(m_rootHashTree, hashkey); } return m_leftHashTree.InsertPoint(hashkey, p, obs, weight, prefab, parent, voxelResolution); } else { if (m_rightHashTree == null) { m_rightHashTree = new VolumetricHashTree(m_rootHashTree, hashkey); } return m_rightHashTree.InsertPoint(hashkey, p, obs, weight, prefab, parent, voxelResolution); } }
/// <summary> /// Insert 3D point into the hash storage. Creates a new meshing cube at the location if needed. /// </summary> /// <returns>The value of the voxel that received the insertion.</returns> /// <param name="hashkey">Hashkey index of the target node.</param> /// <param name="p">The 3D point.</param> /// <param name="obs">Observation vector from the camera to the point.</param> /// <param name="weight">Weight of the observation.</param> /// <param name="prefab">Unity Prefab to be instantiated at this node location if needed.</param> /// <param name="parent">Unity transform of the parent object for the prefab created at this node location.</param> /// <param name="voxelResolution">Resolution of the voxels for this meshing cube.</param> private float InsertPoint(int hashkey, Vector3 p, Vector3 obs, float weight, GameObject prefab, Transform parent, int voxelResolution) { if (m_hashKey == hashkey) { if (m_meshPrefab == null) { InstantiatePrefab(hashkey, prefab, parent, voxelResolution); } if (m_meshPrefab == null) { m_dynamicMeshCube = m_meshPrefab.GetComponent <DynamicMeshCube>(); } if (m_meshPrefab == null) { Debug.Log("Error: cannot find DynamicMeshVolume"); return(0); } if (m_dynamicMeshCube.IsRegenerating) { return(0); } // adjust weight of mutiple voxels along observation ray float result = m_dynamicMeshCube.InsertPoint(p, obs, weight, ref m_volumeIndex); Vector3 closerPoint = p - (obs * m_dynamicMeshCube.VoxelSize); Vector3 furtherPoint = p + (obs * m_dynamicMeshCube.VoxelSize); // voxel was inside the surface, back out one, and insert in the next closest voxel if (result > 0) { m_dynamicMeshCube.InsertPoint(closerPoint, p, obs, weight); } else { m_dynamicMeshCube.InsertPoint(furtherPoint, p, obs, weight); } if (m_volumeIndex[0] == 0) { int neighborHashKey = hashkey - 1; result = m_rootHashTree.InsertPoint(neighborHashKey, p, obs, weight, prefab, parent, voxelResolution); } if (m_volumeIndex[1] == 0) { int neighborHashKey = hashkey - m_maximumVolumeIndexDimension; result = m_rootHashTree.InsertPoint(neighborHashKey, p, obs, weight, prefab, parent, voxelResolution); } if (m_volumeIndex[2] == 0) { int neighborHashKey = hashkey - (m_maximumVolumeIndexDimension * m_maximumVolumeIndexDimension); result = m_rootHashTree.InsertPoint(neighborHashKey, p, obs, weight, prefab, parent, voxelResolution); } return(result); } if (hashkey < m_hashKey) { if (m_leftHashTree == null) { m_leftHashTree = new VolumetricHashTree(m_rootHashTree, hashkey); } return(m_leftHashTree.InsertPoint(hashkey, p, obs, weight, prefab, parent, voxelResolution)); } else { if (m_rightHashTree == null) { m_rightHashTree = new VolumetricHashTree(m_rootHashTree, hashkey); } return(m_rightHashTree.InsertPoint(hashkey, p, obs, weight, prefab, parent, voxelResolution)); } }