// reduce empty voxel and extract active voxels
        public void runExtractActiveVoxels()
        {
            var gpu = Gpu.Default;
            // compute the number of active voxels
            List <int> index_voxelActiveList = new List <int>();

            for (int i = 0; i < voxelVerts.Length; i++)
            {
                if (voxelVerts[i] > 0)
                {
                    index_voxelActiveList.Add(i);
                }
            }

            // the index of active voxel
            index_voxelActive = index_voxelActiveList.ToArray();
            num_voxelActive   = index_voxelActive.Length;
            // the number of vertices in each active voxel
            verts_voxelActive = new int[num_voxelActive];
            // the number of all vertices
            sumVerts = 0;

            Parallel.For(0, num_voxelActive, i =>
            {
                verts_voxelActive[i] = voxelVerts[index_voxelActive[i]];
            });

            // execute exclusive scan for finding out the indices of result vertices
            var op = new Func <int, int, int>((a, b) => { return(a + b); });

            Alea.Session session             = new Alea.Session(gpu);
            int[]        d_verts_voxelActive = Gpu.Default.Allocate <int>(verts_voxelActive);
            int[]        d_voxelVertsScan    = Gpu.Default.Allocate <int>(num_voxelActive);

            GpuExtension.Scan <int>(session, d_voxelVertsScan, d_verts_voxelActive, 0, op, 0);

            var result_Scan = Gpu.CopyToHost(d_voxelVertsScan);

            verts_scanIdx = new int[num_voxelActive];

            for (int i = 1; i < num_voxelActive; i++)
            {
                verts_scanIdx[i] = result_Scan[i - 1];
            }

            try
            {
                verts_scanIdx[0] = 0;
            }
            catch (Exception)
            {
                throw new Exception("No eligible isosurface can be extracted, please change isovalue.");
            }

            sumVerts = verts_scanIdx.ElementAt(verts_scanIdx.Length - 1) + verts_voxelActive.ElementAt(verts_voxelActive.Length - 1);

            Gpu.Free(d_verts_voxelActive);
            Gpu.Free(d_voxelVertsScan);
        }
Beispiel #2
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            var    gpu = Gpu.Default;
            double average;
            var    inputList = new List <double>();

            if (!DA.GetDataList(0, inputList))
            {
                return;
            }
            var inputArray = inputList.ToArray();

            average = GpuExtension.Average(gpu, inputArray);

            DA.SetData(0, average);
        }