<commit_before>#include <cilantro/connected_component_segmentation.hpp>
#include <set>
//#include <stack>

#include <iostream>

ConnectedComponentSegmentation::ConnectedComponentSegmentation(const std::vector<Eigen::Vector3f> &points, const std::vector<Eigen::Vector3f> &normals, const std::vector<Eigen::Vector3f> &colors)
        : points_(&points),
          normals_((normals.size() == points.size()) ? &normals : NULL),
          colors_((colors.size() == points.size()) ? &colors : NULL),
          kd_tree_(new KDTree3D(points)),
          kd_tree_owned_(true)
{}

ConnectedComponentSegmentation::ConnectedComponentSegmentation(const std::vector<Eigen::Vector3f> &points, const std::vector<Eigen::Vector3f> &normals, const std::vector<Eigen::Vector3f> &colors, const KDTree3D &kd_tree)
        : points_(&points),
          normals_((normals.size() == points.size()) ? &normals : NULL),
          colors_((colors.size() == points.size()) ? &colors : NULL),
          kd_tree_((KDTree3D*)&kd_tree),
          kd_tree_owned_(false)
{}

ConnectedComponentSegmentation::ConnectedComponentSegmentation(const PointCloud &cloud)
        : points_(&cloud.points),
          normals_((cloud.normals.size() == cloud.points.size()) ? &cloud.normals : NULL),
          colors_((cloud.colors.size() == cloud.points.size()) ? &cloud.colors : NULL),
          kd_tree_(new KDTree3D(cloud.points)),
          kd_tree_owned_(true)
{}

ConnectedComponentSegmentation::ConnectedComponentSegmentation(const PointCloud &cloud, const KDTree3D &kd_tree)
        : points_(&cloud.points),
          normals_((cloud.normals.size() == cloud.points.size()) ? &cloud.normals : NULL),
          colors_((cloud.colors.size() == cloud.points.size()) ? &cloud.colors : NULL),
          kd_tree_((KDTree3D*)&kd_tree),
          kd_tree_owned_(false)
{}

ConnectedComponentSegmentation::~ConnectedComponentSegmentation() {
    if (kd_tree_owned_) delete kd_tree_;
}

ConnectedComponentSegmentation& ConnectedComponentSegmentation::segment(std::vector<size_t> seeds_ind,
                                                                        float dist_thresh,
                                                                        float normal_angle_thresh,
                                                                        float color_diff_thresh,
                                                                        size_t min_segment_size,
                                                                        size_t max_segment_size)
{
    float radius_sq = dist_thresh*dist_thresh;

    normal_angle_thresh_ = normal_angle_thresh;
    color_diff_thresh_sq_ = color_diff_thresh*color_diff_thresh;

    const size_t unassigned = std::numeric_limits<size_t>::max();
    std::vector<size_t> current_label(points_->size(), unassigned);

    std::vector<size_t> frontier_set;
    frontier_set.reserve(points_->size());

    std::vector<std::set<size_t> > ind_per_seed(seeds_ind.size());
    std::vector<std::set<size_t> > seeds_to_merge_with(seeds_ind.size());

    std::vector<size_t> neighbors;
    std::vector<float> distances;

#pragma omp parallel for shared (seeds_ind, current_label, ind_per_seed, seeds_to_merge_with) private (neighbors, distances, frontier_set)
    for (size_t i = 0; i < seeds_ind.size(); i++) {
        if (current_label[seeds_ind[i]] != unassigned) continue;

        seeds_to_merge_with[i].insert(i);

        frontier_set.clear();
        frontier_set.emplace_back(seeds_ind[i]);

        current_label[seeds_ind[i]] = i;

        while (!frontier_set.empty()) {
            size_t curr_seed = frontier_set[frontier_set.size()-1];
            frontier_set.resize(frontier_set.size()-1);

            ind_per_seed[i].insert(curr_seed);
<commit_msg>ConnectedComponentSegmentation tweaking<commit_after>#include <cilantro/connected_component_segmentation.hpp>
#include <set>
//#include <stack>

#include <iostream>

ConnectedComponentSegmentation::ConnectedComponentSegmentation(const std::vector<Eigen::Vector3f> &points, const std::vector<Eigen::Vector3f> &normals, const std::vector<Eigen::Vector3f> &colors)
        : points_(&points),
          normals_((normals.size() == points.size()) ? &normals : NULL),
          colors_((colors.size() == points.size()) ? &colors : NULL),
          kd_tree_(new KDTree3D(points)),
          kd_tree_owned_(true)
{}

ConnectedComponentSegmentation::ConnectedComponentSegmentation(const std::vector<Eigen::Vector3f> &points, const std::vector<Eigen::Vector3f> &normals, const std::vector<Eigen::Vector3f> &colors, const KDTree3D &kd_tree)
        : points_(&points),
          normals_((normals.size() == points.size()) ? &normals : NULL),
          colors_((colors.size() == points.size()) ? &colors : NULL),
          kd_tree_((KDTree3D*)&kd_tree),
          kd_tree_owned_(false)
{}

ConnectedComponentSegmentation::ConnectedComponentSegmentation(const PointCloud &cloud)
        : points_(&cloud.points),
          normals_((cloud.normals.size() == cloud.points.size()) ? &cloud.normals : NULL),
          colors_((cloud.colors.size() == cloud.points.size()) ? &cloud.colors : NULL),
          kd_tree_(new KDTree3D(cloud.points)),
          kd_tree_owned_(true)
{}

ConnectedComponentSegmentation::ConnectedComponentSegmentation(const PointCloud &cloud, const KDTree3D &kd_tree)
        : points_(&cloud.points),
          normals_((cloud.normals.size() == cloud.points.size()) ? &cloud.normals : NULL),
          colors_((cloud.colors.size() == cloud.points.size()) ? &cloud.colors : NULL),
          kd_tree_((KDTree3D*)&kd_tree),
          kd_tree_owned_(false)
{}

ConnectedComponentSegmentation::~ConnectedComponentSegmentation() {
    if (kd_tree_owned_) delete kd_tree_;
}

ConnectedComponentSegmentation& ConnectedComponentSegmentation::segment(std::vector<size_t> seeds_ind,
                                                                        float dist_thresh,
                                                                        float normal_angle_thresh,
                                                                        float color_diff_thresh,
                                                                        size_t min_segment_size,
                                                                        size_t max_segment_size)
{
    float radius_sq = dist_thresh*dist_thresh;

    normal_angle_thresh_ = normal_angle_thresh;
    color_diff_thresh_sq_ = color_diff_thresh*color_diff_thresh;

    const size_t unassigned = std::numeric_limits<size_t>::max();
    std::vector<size_t> current_label(points_->size(), unassigned);

    std::vector<size_t> frontier_set;
    frontier_set.reserve(points_->size());

//    std::vector<std::set<size_t> > ind_per_seed(seeds_ind.size());
    std::vector<std::vector<size_t> > ind_per_seed(seeds_ind.size());
    std::vector<std::set<size_t> > seeds_to_merge_with(seeds_ind.size());

    std::vector<size_t> neighbors;
    std::vector<float> distances;

#pragma omp parallel for shared (seeds_ind, current_label, ind_per_seed, seeds_to_merge_with) private (neighbors, distances, frontier_set)
    for (size_t i = 0; i < seeds_ind.size(); i++) {
        if (current_label[seeds_ind[i]] != unassigned) continue;

        seeds_to_merge_with[i].insert(i);

        frontier_set.clear();
        frontier_set.emplace_back(seeds_ind[i]);

        current_label[seeds_ind[i]] = i;

        while (!frontier_set.empty()) {
            size_t curr_seed = frontier_set[frontier_set.size()-1];
            frontier_set.resize(frontier_set.size()-1);

//            ind_per_seed[i].insert(curr_seed);
            ind_per_seed[i].emplace_back(curr_seed);

{'fraction_non_alphanumeric': 0.1276207839562443, 'fraction_numerical': 0.003906758692538091, 'mean_word_length': 2.910386965376782, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2221309', 'n_tokens_mistral': 2342, 'n_tokens_neox': 2307, 'n_words': 330}