Skip to content

Conversation

@jeanchristopheruel
Copy link
Member

@jeanchristopheruel jeanchristopheruel commented Jan 15, 2025

Introducing connected components

Regarding #21, this PR addresses the need to be able to differentiate between different solid (unconnected components) in a mesh.

This PR introduces:

  • In python: openstl.topology.find_connected_components
  • In C++: openstl::findConnectedComponents

Python Usage Example:

import openstl

# Define vertices and faces for two disconnected components
vertices = [
 [0.0, 0.0, 0.0],
 [1.0, 0.0, 0.0],
 [0.0, 1.0, 0.0],
 [2.0, 2.0, 0.0],
 [3.0, 2.0, 0.0],
 [2.5, 3.0, 0.0],
]

faces = [
 [0, 1, 2],  # Component 1
 [3, 4, 5],  # Component 2
]

# Identify connected components of faces
connected_components = openstl.topology.find_connected_components(vertices, faces)

# Print the result
print(f"Number of connected components: {len(connected_components)}")
for i, component in enumerate(connected_components):
 print(f"Component {i + 1}: {component}")

C++ Usage Example

#include <stl.h>
#include <vector>
#include <iostream>

using namespace openstl;

int main() {
    std::vector<Vec3> vertices = {
        {0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f},  // Component 1
        {2.0f, 2.0f, 0.0f}, {3.0f, 2.0f, 0.0f}, {2.5f, 3.0f, 0.0f}   // Component 2
    };

    std::vector<Face> faces = {
        {0, 1, 2},  // Component 1
        {3, 4, 5},  // Component 2
    };

    const auto& connected_components = findConnectedComponents(vertices, faces);

    std::cout << "Number of connected components: " << connected_components.size() << "\\n";
    for (size_t i = 0; i < connected_components.size(); ++i) {
        std::cout << "Component " << i + 1 << ":\\n";
        for (const auto& face : connected_components[i]) {
            std::cout << "  {" << face[0] << ", " << face[1] << ", " << face[2] << "}\\n";
        }
    }

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants