martinnormark / neural-mesh-simplification

Un-official and WIP Implementation of the Neural Mesh Simplification paper
MIT License
10 stars 0 forks source link

Implement Edge Crossings loss #31

Closed martinnormark closed 3 months ago

martinnormark commented 3 months ago

We need to implement the Edge Crossings Loss (Le) as described in the paper to penalize triangles with edges that cross edges of nearby triangles. This loss is crucial for maintaining the topological integrity of the simplified mesh, especially in cases where triangles with parallel planes overlap.

Key points:

  1. The loss should focus on nearby triangles rather than all possible pairs.
  2. It should detect edge crossings between edges of nearby triangles.
  3. Triangles carrying an edge that crosses another edge should be directly penalized.
  4. The penalty should be proportional to the number of edge crossings.

Implementation requirements:

  1. Efficient nearest neighbor search for triangles:

    • Implement a method to find k-nearest triangles for each triangle in the simplified mesh.
    • This could be based on the distances between triangle centroids or a more sophisticated spatial partitioning structure.
  2. Edge crossing detection:

    • Implement a function to detect if two edges cross in 3D space.
    • This function should work for edges that are not necessarily coplanar.
  3. Loss calculation:

    • For each triangle $t_i$ in the simplified mesh: a. Find its k-nearest neighbor triangles ${tj}{j=1}^k$ b. Check for edge crossings between $t_i$ and each $t_j$ c. Count the number of crossings $c_i$
    • Calculate the loss as: $$Le = \sum{i} p_i c_i$$ where $p_i$ is the probability of triangle $t_i$ (from the face classifier)
  4. Integration with existing code:

    • The loss should be implemented as a PyTorch module (nn.Module) for easy integration with the existing neural network architecture.
    • It should be compatible with the output format of the Neural Mesh Simplification model.
  5. Efficiency considerations:

    • The implementation should be computationally efficient, possibly utilizing GPU acceleration where appropriate.
    • Consider using vectorized operations in PyTorch to speed up calculations.
  6. Testing:

    • Develop unit tests to verify the correctness of edge crossing detection and loss calculation.
    • Create test cases with known edge crossings to validate the loss function.

Expected input:

Expected output:

This implementation will contribute to the overall mesh simplification loss, helping to preserve the mesh's topological structure during the simplification process.