# Detect and Remove Hidden Surfaces of a Mesh

For the past few weeks, I have been working on an algorithm that finds hidden surfaces of complex meshes and removes them. These hidden surfaces are completely occluded, and will never be seen. Due to the nature of the meshes I'm working with, there are a ton of these hidden triangles. In some cases, there are more hidden surfaces than visible surfaces. As removing them manually is prohibitive for larger meshes, I am looking to automate this with software.

My current algorithm consists of:

- Generating several points on the surface of a triangle.
- For each point, generate a hemisphere sampler aligned to the normal of the triangle.
- Cast rays up into the hemispheres.
- If there are less than a certain number of rays unoccluded, I flag the triangle for deletion.

However, this algorithm is causing a lot of grief. It's very inconsistent. While some of the "occluded" faces are not found as occluded by the algorithm, I'm more worried about very visible faces that get removed due to issues with the current implementation. Therefore, I'm wondering about two things, mainly:

- Is there a better way to find and remove these hidden surfaces than raytracing?
- Should I investigate non-random ray generation? I'm currently generating random directions in a cosine-weighted hemisphere, which could be causing issues. The only reason I haven't investigated this is because I have yet to find an algorithm to generate evenly-spaced rays in a hemisphere.

Note: This is intended to be an object space algorithm. That is, visibility from any angle--not a fixed camera.

I've actually never implemented ray tracing, but I have a few suggestions anyhow. As your goal is to detect every hidden triangle, you could turn the problem around and instead find every visible triangle.

I'm thinking of something along the lines of either:

or

The advantage of the last one is that it should be relatively cheap to implement using a graphics API, if you can read/write the pixels reliably.

A disadvantage of both would be the resolution needed. Triangles inside small openings that should not be culled may still be, thus the number of rays may be prohibitive (in the first algorithm) or you will require very large off screen frame buffers (in the second).

A couple of ideas that may help.