protected override void Awake() { base.Awake(); ObserverState = ObserverStates.Stopped; if (Test) { // create dummy SurfaceObject List <Vector3> testVerts = new List <Vector3>(); testVerts.Add(new Vector3(-0.5f, -0.5f, -0.5f)); testVerts.Add(new Vector3(0, 0, 0)); testVerts.Add(new Vector3(0.5f, 0.5f, 0.5f)); Mesh testMesh = new Mesh(); testMesh.SetVertices(testVerts); SurfaceObject testSO = CreateSurfaceObject(testMesh, "test", gameObject.transform); testSO.Renderer.transform.position = new Vector3(1, 1, 1); // create matching dummy SurfacePoints Transform tmpTransform = testSO.Renderer.transform; List <Vector3> tmpWvertices = testSO.Filter.sharedMesh.vertices.ToList(); int tmpTriCount = testSO.Filter.sharedMesh.triangles.ToList().Count; for (int i = 0; i < tmpWvertices.Count; i++) { tmpWvertices[i] = tmpTransform.TransformPoint(tmpWvertices[i]); } SurfacePoints extras = new SurfacePoints( MeshManager.IntersectionPoints(testSO.Renderer.bounds), tmpWvertices, tmpTriCount, testSO.Renderer.bounds); // Log UpdateOrAddSurfaceObject(testSO, extras); } }
protected override void Awake() { base.Awake(); ObserverState = ObserverStates.Stopped; density = DefaultTrianglesPerCubicMeter; // debug StartObserving(); if (Test) { // create dummy SurfaceObject List <Vector3> testVerts = new List <Vector3>(); testVerts.Add(new Vector3(-0.5f, -0.5f, -0.5f)); testVerts.Add(new Vector3(0, 0, 0)); testVerts.Add(new Vector3(0.5f, 0.5f, 0.5f)); Mesh testMesh = new Mesh(); testMesh.SetVertices(testVerts); SurfaceObject testSO1 = CreateSurfaceObject(testMesh, "test", gameObject.transform, 0); testSO1.Renderer.transform.position = new Vector3(1, 1, 1); SurfaceObject testSO2 = CreateSurfaceObject(testMesh, "test", gameObject.transform, 1); testSO2.Renderer.transform.position = new Vector3(-1, -1, -1); // create matching dummy SurfacePoints: 1 Transform tmpTransform1 = testSO1.Renderer.transform; List <Vector3> tmpWvertices1 = testSO1.Filter.sharedMesh.vertices.ToList(); int tmpTriCount1 = testSO1.Filter.sharedMesh.triangles.ToList().Count; for (int i = 0; i < tmpWvertices1.Count; i++) { tmpWvertices1[i] = tmpTransform1.TransformPoint(tmpWvertices1[i]); } SurfacePoints extras1 = new SurfacePoints( MeshManager.IntersectionPoints(testSO1.Renderer.bounds), tmpWvertices1, tmpTriCount1, testSO1.Renderer.bounds, Density); // create matching dummy SurfacePoints: 2 Transform tmpTransform2 = testSO2.Renderer.transform; List <Vector3> tmpWvertices2 = testSO2.Filter.sharedMesh.vertices.ToList(); int tmpTriCount2 = testSO2.Filter.sharedMesh.triangles.ToList().Count; for (int i = 0; i < tmpWvertices2.Count; i++) { tmpWvertices2[i] = tmpTransform2.TransformPoint(tmpWvertices2[i]); } SurfacePoints extras2 = new SurfacePoints( MeshManager.IntersectionPoints(testSO2.Renderer.bounds), tmpWvertices2, tmpTriCount1, testSO2.Renderer.bounds, Density); // Log UpdateOrAddSurfaceObject(testSO1, extras1); UpdateOrAddSurfaceObject(testSO2, extras2); } }
/// <summary> /// Handles the SurfaceObserver's OnDataReady event. /// </summary> /// <param name="cookedData">Struct containing output data.</param> /// <param name="outputWritten">Set to true if output has been written.</param> /// <param name="elapsedCookTimeSeconds">Seconds between mesh cook request and propagation of this event.</param> private void SurfaceObserver_OnDataReady(SurfaceData cookedData, bool outputWritten, float elapsedCookTimeSeconds) { if (outstandingMeshRequest == null) { Debug.LogErrorFormat("Got OnDataReady for surface {0} while no request was outstanding.", cookedData.id.handle ); return; } if (!IsMatchingSurface(outstandingMeshRequest.Value, cookedData)) { Debug.LogErrorFormat("Got mismatched OnDataReady for surface {0} while request for surface {1} was outstanding.", cookedData.id.handle, outstandingMeshRequest.Value.ID ); ReclaimSurface(outstandingMeshRequest.Value); outstandingMeshRequest = null; return; } if (ObserverState != ObserverStates.Running) { Debug.LogFormat("Got OnDataReady for surface {0}, but observer was no longer running.", cookedData.id.handle ); ReclaimSurface(outstandingMeshRequest.Value); outstandingMeshRequest = null; return; } if (!outputWritten) { ReclaimSurface(outstandingMeshRequest.Value); outstandingMeshRequest = null; return; } Debug.Assert(outstandingMeshRequest.Value.Object.activeSelf); // alters properites of mesh as desired: first sets renderer enabled field outstandingMeshRequest.Value.Renderer.enabled = SpatialMappingManager.Instance.DrawVisualMeshes; // create extra data SurfaceObject tmpSO = outstandingMeshRequest.Value; Transform tmpTransform = tmpSO.Renderer.transform; // Wvertices List <Vector3> tmpWvertices = tmpSO.Filter.sharedMesh.vertices.ToList(); for (int i = 0; i < tmpWvertices.Count; i++) { tmpWvertices[i] = tmpTransform.TransformPoint(tmpWvertices[i]); } // TriCount int tmpTriCount = tmpSO.Filter.sharedMesh.triangles.ToList().Count; // SurfacePoints obj SurfacePoints extras = new SurfacePoints( MeshManager.IntersectionPoints(outstandingMeshRequest.Value.Renderer.bounds), tmpWvertices, tmpTriCount, tmpSO.Renderer.bounds, cookedData.trianglesPerCubicMeter); // updates/adds mesh in source stored list of SurfaceObjects. SurfaceObject?replacedSurface = UpdateOrAddSurfaceObject(outstandingMeshRequest.Value, extras, destroyGameObjectIfReplaced: false); outstandingMeshRequest = null; if (replacedSurface != null) { ReclaimSurface(replacedSurface.Value); } }
// Update is called once per frame void Update() { StopWatch.Reset(); StopWatch.Start(); // update sensor position UpdateSensor(ref SensorField, ref SensorData); // update visible mesh vertices SubStopWatch.Reset(); SubStopWatch.Start(); List <bool> MeshGuide = MeshMan.UpdateVertices(SensorField); MeshDensity = MeshMan.Density; SubStopWatch.Stop(); MeshManSpeed = SubStopWatch.ElapsedTicks / (double)Stopwatch.Frequency; // project to non-occluded vertices SubStopWatch.Reset(); SubStopWatch.Start(); Oc = new Intersector.Occlusion(OcclusionObjSize, OcclusionObjDistance, SensorField.FOV); List <Intersector.PointValue <byte> > Updates = VertexInter.ProjectToVisible(SensorData, SensorField, Oc, Observer.ExtraData, MeshGuide, Closest, Furthest, SensorField.Transform.position); SubStopWatch.Stop(); IntersectSpeed = SubStopWatch.ElapsedTicks / (double)Stopwatch.Frequency; // update voxel grid SubStopWatch.Reset(); SubStopWatch.Start(); // reset if filtered to live data only if (VoxGridFilter == DataFilter.live) { VoxGridMan.Reset(); } VoxGridMan.Set(Updates, UpdateVoxStruct); SubStopWatch.Stop(); VoxGridManSpeed = SubStopWatch.ElapsedTicks / (double)Stopwatch.Frequency; // visualize data SubStopWatch.Reset(); SubStopWatch.Start(); content = new List <Visualizer.Content>(); // render spheres if (RenderVerts) { if (VoxGridFilter == DataFilter.mesh || VoxGridFilter == DataFilter.live) { // render all non-occluded mesh vertices content.AddRange(Visualizer.CreateMarkers(Updates, VertexMarkerSize, MinColorVal, MaxColorVal, MinColor, MaxColor)); } else { // render all visible voxel vertices List <Voxel <byte> > curVoxels = VoxGridMan.Voxels(); for (int i = 0; i < curVoxels.Count; i++) { List <Intersector.PointValue <byte> > tmp = new List <Intersector.PointValue <byte> >(); tmp.Add(new Intersector.PointValue <byte>(curVoxels[i].point, curVoxels[i].value)); content.AddRange(Visualizer.CreateMarkers(tmp, VertexMarkerSize, MinColorVal, MaxColorVal, MinColor, MaxColor)); } } } // visualize voxels if (VoxVis) { for (int i = 0; i < Voxels.Count; i++) { if (BoundsRandNums[i] <= ShowFrac) { Bounds voxBound = new Bounds(); voxBound.min = Voxels[i].min; voxBound.max = Voxels[i].max; if (VertexInter.AnyInView(MeshManager.IntersectionPoints(voxBound), SensorField)) { content.AddRange(Visualizer.CreateBoundingLines(voxBound, BoundsLineWidth, BoundColors[i])); } } } } // visualize mesh bounds if (MeshBoundsVis) { for (int i = 0; i < MeshBounds.Count; i++) { if (BoundsRandNums[i] <= ShowFrac && VertexInter.AnyInView(MeshManager.IntersectionPoints(MeshBounds[i]), SensorField)) { content.AddRange(Visualizer.CreateBoundingLines(MeshBounds[i], BoundsLineWidth, BoundColors[i])); } } } VertVis.Visualize(content); // color mesh if (ColoredMesh) { // color mesh by voxel grid value if Data Filter is not .all. If so, colored all 'no data' if (VoxGridFilter != DataFilter.all) { Visualizer.ColorMesh(Observer.SurfaceObjects, Observer.ExtraData, MeshGuide, VoxGridMan, MinColor, MaxColor, NoDataColor, MinColorVal, MaxColorVal); } else { Visualizer.ColorMesh(Observer.SurfaceObjects, Observer.ExtraData, MeshGuide, VoxGridMan, NoDataColor, NoDataColor, NoDataColor, MinColorVal, MaxColorVal); } } SubStopWatch.Stop(); VertVisSpeed = SubStopWatch.ElapsedTicks / (double)Stopwatch.Frequency; StopWatch.Stop(); DriverSpeed = StopWatch.ElapsedTicks / (double)Stopwatch.Frequency; }
// handles all script's visulation for better organization private void HandleVis(List <Intersector.PointValue <byte> > Updates, List <bool> MeshGuide) { content = new List <Visualizer.Content>(); // color mesh if (ColoredMesh) { // color mesh by voxel grid value if Data Filter is not .all. If so, colored all 'no data' if (VoxGridFilter != DataFilter.all) { Visualizer.ColorMesh(Observer.SurfaceObjects, Observer.ExtraData, MeshGuide, VoxGridMan, MinColor, MaxColor, NoDataColor, MinColorVal, MaxColorVal, MeshAlpha); } else { Visualizer.ColorMesh(Observer.SurfaceObjects, Observer.ExtraData, MeshGuide, VoxGridMan, NoDataColor, NoDataColor, NoDataColor, MinColorVal, MaxColorVal, MeshAlpha); } } // render spheres if (RenderVerts) { if (VoxGridFilter == DataFilter.mesh || VoxGridFilter == DataFilter.live) { // render all non-occluded mesh vertices content.AddRange(Visualizer.CreateMarkers(Updates, VertexMarkerSize, MinColorVal, MaxColorVal, MinColor, MaxColor)); } else { // render all visible voxel vertices List <Voxel <byte> > curVoxels = VoxGridMan.Voxels(); for (int i = 0; i < curVoxels.Count; i++) { List <Intersector.PointValue <byte> > tmp = new List <Intersector.PointValue <byte> >(); tmp.Add(new Intersector.PointValue <byte>(curVoxels[i].point, curVoxels[i].value)); content.AddRange(Visualizer.CreateMarkers(tmp, VertexMarkerSize, MinColorVal, MaxColorVal, MinColor, MaxColor)); } } } // visualize voxels if (VoxVis) { for (int i = 0; i < Voxels.Count; i++) { if (BoundsRandNums[i] <= ShowFrac) { Bounds voxBound = new Bounds(); voxBound.min = Voxels[i].min; voxBound.max = Voxels[i].max; if (VertexInter.AnyInView(MeshManager.IntersectionPoints(voxBound), SensorField)) { content.AddRange(Visualizer.CreateBoundingLines(voxBound, BoundsLineWidth, BoundColors[i])); } } } } // visualize mesh bounds if (MeshBoundsVis) { for (int i = 0; i < MeshBounds.Count; i++) { if (BoundsRandNums[i] <= ShowFrac && VertexInter.AnyInView(MeshManager.IntersectionPoints(MeshBounds[i]), SensorField)) { content.AddRange(Visualizer.CreateBoundingLines(MeshBounds[i], BoundsLineWidth, BoundColors[i])); } } } VertVis.Visualize(content); }