I have a voxelization that I need to write to a binary STL file. These voxels are within a higher level 3D grid, and each cell in the grid is made up of voxels. Individual cells may or may not have active voxels in them.

I have significantly optimized the file size by not writing triangles for inner faces, faces between active voxels.

However, the method I used is very ugly and I think it could be improved. I am not sure if there is an applicable design pattern or simply a better way to think about it.

- It's ugly
- It's repetitive

Anyway, I want to learn something from this code and see how I can do better in the future.

The space between notes can be a bit strange, using the clang format and a limit of 80 characters.

writeVoxelSTL ():

```
void writeVoxelAsSTL(
std::ofstream& fileStream,
unsigned& numTris,
const unsigned xCell,
const unsigned yCell,
const unsigned zCell,
const unsigned xVoxel,
const unsigned yVoxel,
const unsigned zVoxel) const
{
// Now need to check if each of its sides need to be written out
bool left = isNeighborActive(
xCell, yCell, zCell, static_cast(xVoxel) - 1, yVoxel, zVoxel);
bool right =
isNeighborActive(xCell, yCell, zCell, xVoxel + 1, yVoxel, zVoxel);
bool bottom = isNeighborActive(
xCell, yCell, zCell, xVoxel, static_cast(yVoxel) - 1, zVoxel);
bool top = isNeighborActive(xCell, yCell, zCell, xVoxel, yVoxel + 1, zVoxel);
bool front = isNeighborActive(
xCell, yCell, zCell, xVoxel, yVoxel, static_cast(zVoxel) - 1);
bool back = isNeighborActive(xCell, yCell, zCell, xVoxel, yVoxel, zVoxel + 1);
Point minPoint = highLevelGridMinpoint;
// Center of the voxel
Point center;
center.x = minPoint.x + (((xCell * cellDim.x) + xVoxel) * voxelWidth);
center.y = minPoint.y + (((yCell * cellDim.y) + yVoxel) * voxelWidth);
center.z = minPoint.z + (((zCell * cellDim.z) + zVoxel) * voxelWidth);
// From this center, we can get the 8 corners of the voxel and thus the 12
// triangles
// They are defined like this:
// 7-------6
// /| /|
// 4-+-----5 |
// | | | | y
// | 3-----+-2 | z
// |/ |/ |/
// 0-------1 +--x
Point p0(center.x - halfVoxel, center.y - halfVoxel, center.z - halfVoxel);
Point p1(center.x + halfVoxel, center.y - halfVoxel, center.z - halfVoxel);
Point p2(center.x + halfVoxel, center.y - halfVoxel, center.z + halfVoxel);
Point p3(center.x - halfVoxel, center.y - halfVoxel, center.z + halfVoxel);
Point p4(center.x - halfVoxel, center.y + halfVoxel, center.z - halfVoxel);
Point p5(center.x + halfVoxel, center.y + halfVoxel, center.z - halfVoxel);
Point p6(center.x + halfVoxel, center.y + halfVoxel, center.z + halfVoxel);
Point p7(center.x - halfVoxel, center.y + halfVoxel, center.z + halfVoxel);
// Now for the 12 triangles made of these points
// Left side
if (!left)
{
writeTriangleSTL(fileStream, p0, p3, p4);
writeTriangleSTL(fileStream, p3, p4, p7);
numTris += 2;
}
// Right Side
if (!right)
{
writeTriangleSTL(fileStream, p1, p2, p5);
writeTriangleSTL(fileStream, p2, p5, p6);
numTris += 2;
}
// Bottom Side
if (!bottom)
{
writeTriangleSTL(fileStream, p0, p1, p2);
writeTriangleSTL(fileStream, p0, p2, p3);
numTris += 2;
}
// Top Side
if (!top)
{
writeTriangleSTL(fileStream, p4, p5, p6);
writeTriangleSTL(fileStream, p4, p6, p7);
numTris += 2;
}
// Front side
if (!front)
{
writeTriangleSTL(fileStream, p0, p1, p4);
writeTriangleSTL(fileStream, p1, p4, p5);
numTris += 2;
}
// Back side
if (!back)
{
writeTriangleSTL(fileStream, p3, p2, p7);
writeTriangleSTL(fileStream, p2, p7, p6);
numTris += 2;
}
}
```

isNeighborActive ():

Note: cellDim is the number of voxels in each direction for a cell.

gridDim is the number of cells in each direction.

```
bool isNeighborActive(
long xCell, long yCell, long zCell, long xVoxel, long yVoxel, long zVoxel)
const
{
// Get the "real" coordinates
if (xVoxel < 0)
{
xCell--;
xVoxel = cellDim.x - 1;
if (xCell < 0)
{
return false;
}
}
else if (xVoxel > (cellDim.x - 1))
{
xCell++;
xVoxel = 0;
if (xCell > (gridDim.x - 1))
{
return false;
}
}
if (yVoxel < 0)
{
yCell--;
yVoxel = cellDim.y - 1;
if (yCell < 0)
{
return false;
}
}
else if (yVoxel > (cellDim.y - 1))
{
yCell++;
yVoxel = 0;
if (yCell > (gridDim.y - 1))
{
return false;
}
}
if (zVoxel < 0)
{
zCell--;
zVoxel = cellDim.z - 1;
if (zCell < 0)
{
return false;
}
}
else if (zVoxel > (cellDim.z - 1))
{
zCell++;
zVoxel = 0;
if (zCell > (gridDim.z - 1))
{
return false;
}
}
const cellVoxelization& cell = modelVoxelization.at(xCell, yCell, zCell);
if (cell.getSize() == 0)
{
return false;
}
bool active = cell.at(xVoxel, yVoxel, zVoxel).active;
return active;
}
```

I thought about using some kind of enumeration for the instructions, but that only changes the ugliness to the next method that has a switch, unfortunately.