CGAL K-D 树 - 在范围搜索时如何将信息关联到点?

问题描述 投票:0回答:1

我正在使用 CGAL 在正方形上随机生成点,我想为每个点分配一个任意的 int 值。目标是使用

Fuzzy_sphere
进行范围搜索。我的代码在没有添加 int 值的情况下运行,但是,我似乎无法添加 int 值并使其运行。

我已经查看了手册中提供的示例,用于通过邻居搜索执行此操作,但我无法使其与范围搜索查询一起使用。

到目前为止我的工作代码:

#include <CGAL/Simple_cartesian.h>
#include <CGAL/Kd_tree.h>
#include <CGAL/Search_traits_2.h>
#include <CGAL/point_generators_2.h>
#include <CGAL/algorithm.h>
#include <CGAL/Fuzzy_sphere.h>
#include <CGAL/Random.h>

typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_2 Point;
typedef CGAL::Random_points_in_square_2<Point> Random_points_iterator;
typedef CGAL::Counting_iterator<Random_points_iterator> N_Random_points_iterator;
typedef CGAL::Search_traits_2<K> Traits;
typedef CGAL::Fuzzy_sphere<Traits> Fuzzy_circle;
typedef CGAL::Kd_tree<Traits> Tree;
typedef CGAL::Random Random;

int main() {

    const int no_points = 2000; // arbitrary 
    const int size = 1000;
    Random rand;
    // Vector to store Points
    std::vector<Point> point_vec;

    for (int i = 0; i < no_points; ++i)
        point_vec.push_back(Point(rand.get_double(0, size), rand.get_double(0, size))); 

        // Add this to a K-D tree 
        Tree tree(point_vec.begin(), point_vec.end());

    // Find a random point to search
    Point search_point = Point(rand.get_double(0, 12), rand.get_double(0, 12));

    // Creating a fuzzy circle 
    Fuzzy_circle search_circle(search_point, 20, 0.1); // Center, sqrd radius, fuzziness 

    // Searching an the area that is specified in "search_area"
    boost::optional<Point> search_area = tree.search_any_point(search_circle);

    // declare list to store points found in search query
    std::list<Point> search_result;

    // Doing a range search on the tree KD tree space 
    tree.search(std::back_inserter(search_result), search_circle);

    std::cout << "\n Points in search area: " << search_point << ":\n";

    // initiate iterator that will be used to iterate over the elements of the result list.
    std::list<Point>::iterator iter;

    for (iter = search_result.begin(); (iter != search_result.end()); ++iter)
        std::cout << *iter << "\n";

    search_result.clear();
    return 0;
}

目标是访问范围查询内的点及其关联值。有什么想法吗?

总的来说,我对 CGAL 和 C++ 还很陌生,所以如果这很简单,我很抱歉。

c++ boost cgal
1个回答
0
投票

找到了一种可能非常低效的方法,我将在程序的后面看看性能如何。它有效,但要付出什么代价


#include <CGAL/Simple_cartesian.h>
#include <CGAL/Kd_tree.h>
#include <CGAL/Search_traits_2.h>
#include <CGAL/point_generators_2.h>
#include <CGAL/algorithm.h>
#include <CGAL/Fuzzy_sphere.h>
#include <CGAL/Random.h>

#include <iostream>
#include <map>
#include <string>
#include <boost/property_map/property_map.hpp>

// Vector to store Points
//std::vector<Point> point_vec;
//point_vec.push_back(Point(rand.get_double(0, size), rand.get_double(0, size)));

typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_2 Point;
typedef CGAL::Random_points_in_square_2<Point> Random_points_iterator;
typedef CGAL::Counting_iterator<Random_points_iterator> N_Random_points_iterator;
typedef CGAL::Search_traits_2<K> Traits;
typedef CGAL::Fuzzy_sphere<Traits> Fuzzy_circle;
typedef CGAL::Kd_tree<Traits> Tree;
typedef CGAL::Random Random;

int main() {

    const int no_points = 2000; // arbitrary 
    const int size = 10;
    Random rand;


    // New stuff
    std::map<int, Point> Point_map;
   
    boost::associative_property_map < std::map<int, Point> > address_map(Point_map);


    for (int i = 0; i < no_points; ++i){

        Point position_i = Point(rand.get_double(0, size), rand.get_double(0, size));
        int value_i = rand.get_int(0, 123);

        Point_map.insert(std::make_pair(value_i, position_i));

    }
    
        // Add this to a K-D tree 
    Tree tree; //(Point_map.begin(), Point_map.end());
    for (const auto& pair : Point_map) {
        tree.insert(pair.second);
    }



    // Find a random point to search
    Point search_point = Point(rand.get_double(0, 12), rand.get_double(0, 12));

    // Creating a fuzzy circle 
    Fuzzy_circle search_circle(search_point, 20, 0.1); // Center, sqrd radius, fuzziness 

    // Searching an the area that is specified in "search_area"
    boost::optional<Point> search_area = tree.search_any_point(search_circle);

    // declare list to store points found in search query
    std::list<Point> search_result;

    // Doing a range search on the tree KD tree space 
    tree.search(std::back_inserter(search_result), search_circle);

    std::cout << "\n Points in search area: " << search_point << ":\n";

    // Printing the points and their int value 
    std::cout << "\nPoints in search area: " << search_point << ":\n ";

    for (const auto& point : search_result) {

        // Find the int value associated with the current point
        int value = Point_map.begin()->first;

        for (const auto& pair : Point_map) {

            if (pair.second == point) {

                value = pair.first;
                break;

            }
        }

        std::cout << "Point: " << point << ", Value: " << value << "\n";
    }


    search_result.clear();
    return 0;
}

我的这篇文章显然冒犯了某人,因为他们决定否决它,而我什至不知道你可以这样做。所以希望这可能有所帮助,或者至少他们会得到同情。

© www.soinside.com 2019 - 2024. All rights reserved.