From d459d0f14e146a0807da05ada5272a89ed5badd2 Mon Sep 17 00:00:00 2001 From: Anton T Johansson Date: Fri, 22 Jan 2021 11:58:39 +0100 Subject: [PATCH] Output voxel data as Grasshopper list instead of Single[,,]. (#2) * Output list instead of array --- src/components/VoxelSampleDualComponent.cs | 8 ++-- src/isosurfacing/VoxelSamplerDual.cs | 53 +++++++++++----------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/components/VoxelSampleDualComponent.cs b/src/components/VoxelSampleDualComponent.cs index 9400729..9f640b1 100644 --- a/src/components/VoxelSampleDualComponent.cs +++ b/src/components/VoxelSampleDualComponent.cs @@ -75,8 +75,8 @@ protected override void RegisterInputParams(GH_Component.GH_InputParamManager pM protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) { OutBIdx = pManager.AddBoxParameter("Box", "B", "The generated box representing voxel grid.", GH_ParamAccess.item); - OutDIdx = pManager.AddGenericParameter("Voxel Data 1", "D", "Voxel data stored in an array.", GH_ParamAccess.item); - OutPIdx = pManager.AddPointParameter("Voxel center points", "P", "Voxel center points.", GH_ParamAccess.tree); + OutDIdx = pManager.AddGenericParameter("Voxel Data 1", "D", "Voxel data stored in an array.", GH_ParamAccess.list); + OutPIdx = pManager.AddPointParameter("Voxel center points", "P", "Voxel center points.", GH_ParamAccess.list); } /// @@ -126,8 +126,8 @@ protected override void SolveInstance(IGH_DataAccess DA) sampler.ExecuteMultiThreaded(); _ = DA.SetData(OutBIdx, sampler.BBox); - _ = DA.SetData(OutDIdx, sampler.SampledData); - _ = DA.SetDataList(OutPIdx, sampler.VoxelPts); + _ = DA.SetDataList(OutDIdx, sampler.VoxelValuesList); + _ = DA.SetDataList(OutPIdx, sampler.VoxelPtsList); } /// diff --git a/src/isosurfacing/VoxelSamplerDual.cs b/src/isosurfacing/VoxelSamplerDual.cs index fa8b557..266efab 100644 --- a/src/isosurfacing/VoxelSamplerDual.cs +++ b/src/isosurfacing/VoxelSamplerDual.cs @@ -25,7 +25,6 @@ */ using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using KDTree; @@ -35,9 +34,7 @@ namespace Chromodoris { internal class VoxelSamplerDual { - public List[] _voxelPts; - public Box BBox; - public float[,,] SampledData; + private readonly Box _bBox; private readonly PointCloudVoxelData _ptCloudVoxel1; private readonly PointCloudVoxelData _ptCloudVoxel2; private readonly int _xRes; @@ -46,14 +43,16 @@ internal class VoxelSamplerDual private readonly double _ySpace; private readonly int _zRes; private readonly double _zSpace; + private List[] _voxelPts; + private List[] _voxelValues; public VoxelSamplerDual(List pointCloud1, List pointCloud2, Box box, int resX, int resY, int resZ) { _ptCloudVoxel1 = new PointCloudVoxelData(pointCloud1); _ptCloudVoxel2 = new PointCloudVoxelData(pointCloud2); - BBox = box; - BBox.RepositionBasePlane(box.Center); + _bBox = box; + _bBox.RepositionBasePlane(box.Center); _xRes = resX; _yRes = resY; @@ -63,25 +62,15 @@ public VoxelSamplerDual(List pointCloud1, List pointCloud2, Bo _ySpace = (BBox.Y.Max - BBox.Y.Min) / (_yRes - 1); _zSpace = (BBox.Z.Max - BBox.Z.Min) / (_zRes - 1); - SampledData = new float[_xRes, _yRes, _zRes]; + _voxelValues = new List[_xRes]; _voxelPts = new List[_xRes]; } - public List VoxelPts - { - get - { - List _newList = new List(); - foreach (List _list in _voxelPts) { _newList.AddRange(_list); } - return _newList; - } - } + public Box BBox { get => _bBox; } - //public Box BBox => _bBox; + public List VoxelPtsList { get { return FlattenArrayOfList(_voxelPts); } } - //public float[,,] SampledData => _sampledData; - - //public List VoxelPts => _voxelPts; + public List VoxelValuesList { get { return FlattenArrayOfList(_voxelValues); } } public void ExecuteMultiThreaded() { @@ -89,9 +78,20 @@ public void ExecuteMultiThreaded() System.Threading.Tasks.Parallel.ForEach(Enumerable.Range(0, _xRes), pLel, x => AssignSection(x)); } + private static List FlattenArrayOfList(IList[] array) + { + List newList = new List(); + foreach (List list in array) + { + newList.AddRange(list); + } + + return newList; + } private void AssignSection(int xIdx) { // List specific to xIdx slice to avoid race conditions + _voxelValues[xIdx] = new List(); _voxelPts[xIdx] = new List(); double xCoord = BBox.X.Min + xIdx * _xSpace + BBox.Center.X; @@ -101,16 +101,17 @@ private void AssignSection(int xIdx) for (int zIdx = 0; zIdx < _zRes; zIdx++) { double zCoord = BBox.Z.Min + zIdx * _zSpace + BBox.Center.Z; - int[] voxelRef = { xIdx, yIdx, zIdx }; + var voxelPt = new Point3d(xCoord, yCoord, zCoord); - _voxelPts[xIdx].Add(voxelPt); - AssignVoxelValues(voxelRef, voxelPt); + _voxelPts[xIdx].Add(voxelPt); + + float val = GetVoxelValue(voxelPt); + _voxelValues[xIdx].Add(val); } } } - - private void AssignVoxelValues(int[] voxelRef, Point3d voxelPt) + private float GetVoxelValue(Point3d voxelPt) { double voxelDist1 = _ptCloudVoxel1.GetClosestPtDistance(voxelPt); double voxelDist2 = _ptCloudVoxel2.GetClosestPtDistance(voxelPt); @@ -118,7 +119,7 @@ private void AssignVoxelValues(int[] voxelRef, Point3d voxelPt) double sumDist = voxelDist1 + voxelDist2; double val = voxelDist1 / sumDist; - SampledData.SetValue((float)val, voxelRef); + return (float)val; } private class PointCloudVoxelData