I recently got into c # and Unity coding, and I'd like to put this algorithm and code for review to verify how I'm doing. I have only been working alone, so it would be interesting to know how the design and style of my code could be improved.

The task is like this:

There is a 3D cubic grid (`stateGrid`

) that contain voxels that are 1 or 0. It is stored as a flattened matrix. If several voxels 1 are neighbors to each other, they form an island together.

I want to detect islands smaller than a certain size (`blobMin`

), and convert them to 0. The function returns the voxels that are part of the islands for elimination.

Checking if voxel is on an island is implemented with an amplitude search in the Walker function.

Performance is quite important, since the cube can be 64 or even larger, and I think there is still much to gain algorithmically.

Here is my code:

```
using System.Collections.Generic;
using UnityEngine;
public class IslandRemove{
private int mapSize;
private int mapCubeSize;
private float() state;
private float() result;
private int index;
private int connectCount;
private int blobMin;
private HashSet
``` history;
private Queue queue;
private int blobSz;
public IslandRemove(int mapSize, int mapCubeSize, int blobMin){
this.mapSize = mapSize;
this.mapCubeSize = mapCubeSize;
this.blobMin = blobMin;
result = new float(mapCubeSize);
}
private int getIdx(Vector3Int v){
return v.z * mapSize * mapSize + v.y * mapSize + v.x;
}
private void ProcessNode(Vector3Int newVect){
// if not already in history
if (!history.Contains(newVect)){
blobSz++;
// add node to queue for later check
queue.Enqueue(newVect);
// mark node as counted so we don't count again, or check its connections
history.Add(newVect);
}
}
private bool walker(int x, int y, int z){
blobSz = 0;
queue = new Queue();
history = new HashSet();
ProcessNode(new Vector3Int(x, y, z)); // queue start position
var loopcount = 0;
while (blobSz < blobMin){
loopcount++;
var node = queue.Dequeue();
var idx = getIdx(node);
// check edges
if (node.x != 0){ // edge x-
if ((int)state(idx - 1) == 1)
ProcessNode(new Vector3Int(node.x - 1, node.y, node.z));
}
if (node.x != mapSize - 1){ // edge x+
if ((int) state(idx + 1) == 1)
ProcessNode(new Vector3Int(node.x + 1, node.y, node.z));
}
if (node.y != 0){ // edge y-
if ((int) state(idx - mapSize) == 1)
ProcessNode(new Vector3Int(node.x, node.y-1, node.z));
}
if (node.y != mapSize - 1){ // edge y+
if ((int) state(idx + mapSize) == 1)
ProcessNode(new Vector3Int(node.x, node.y+1, node.z));
}
if (node.z != 0){ // edge z-
if ((int) state(idx - mapSize*mapSize) == 1){
ProcessNode(new Vector3Int(node.x, node.y, node.z-1));
}
}
if (node.z != mapSize - 1){ // edge z+
if ((int) state(idx + mapSize*mapSize) == 1)
ProcessNode(new Vector3Int(node.x, node.y, node.z+1));
}
if (queue.Count == 0){
return true; // add to removelist
}
}
return false; // target is reached, don't add to removelist
}
public List IslandRemover(float() stateGrid){
state = stateGrid;
var remove = new List();
var removeThis = false;
var lastOneWasOne = false; // start with false
// iterate through cube
for (int z = 0; z < mapSize; z++){
for (int y = 0; y < mapSize; y++){
for (int x = 0; x < mapSize; x++){
var idx = z * mapSize * mapSize + y * mapSize + x;
if ((int) state(idx) == 1 && !lastOneWasOne){
removeThis = walker(x, y, z);
if (removeThis)
remove.Add(new Vector3Int(x,y,z));
else{
lastOneWasOne = true;
}
}
else{
lastOneWasOne = false;
}
if ((int)state(idx) == 0){
lastOneWasOne = false;
}
}
}
}
return remove;
}
}

Thanks in advance!