#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/index/rtree.hpp>
using PointType = boost::geometry::model::point<float,3,boost::geometry::cs::cartesian>;
// Build the r-tree
boost::geometry::index::rtree< PointType,boost::geometry::index::quadratic<16> > rtree;
for(...) {
rtree.insert( PointType( x, y, z ) );
}
// Find the nearest point
PointType roiPoint( x, y, z );
std::vector< PointType > nearestPoints;
rtree.query( boost::geometry::index::nearest( roiPoint, 1 ), std::back_inserter( nearestPoints ) );
const auto nearest = nearestPoints[0];
std::cout << nearest.get<0>() << ", "<< nearest.get<1>() << ", " << nearest.get<2>() << std::endl;
Category: Boost
boost::combine
boost::combine() packs a group of variables.
#include <vector>
#include <boost/range/combine.hpp>
int main(int argc, char** argv)
{
// The ranges mush have the same size
std::vector<int> ids = {1, 2, 3, 4};
std::vector<std::string> names = { "A", "B", "C", "D" };
std::vector<float> heights = { 1.71, 1.65, 1.80, 1.75 };
for( const auto &zipped : boost::combine(ids, names, heights) ) {
{
// tie()
int id;
std::string name;
float height;
boost::tie(id, name, height) = zipped;
std::cout << id << "," << name << "," << height << std::endl;
}
{
// get<>()
int id = boost::get<0>( zipped );
std::string name = boost::get<1>( zipped );
float height = boost::get<2>( zipped );
std::cout << id << "," << name << "," << height << std::endl;
}
}
}
Output:
1,A,1.71
1,A,1.71
2,B,1.65
2,B,1.65
3,C,1.8
3,C,1.8
4,D,1.75
4,D,1.75
Note, the boost version used in this example is 1.71.
Create directories using Boost
Boost::filesystem provides two methods to check if directories exist and create directories if not.
#include <boost/filesystem.hpp>
boost::filesystem::path filePath( "/a/b/c" );
if( !boost::filesystem::exists( filePath ) &&
!boost::filesystem::create_directories( filePath ) ) {
...
}
C++ standard didn’t provide them until C++17.
boost::endian
Boost introduced a new library endian in version 1.58: Types and conversion functions for correct byte ordering and more regardless of processor endianness.
#include <boost/endian/conversion.hpp>
int32_t x;
... read into x from a file ...
boost::endian::big_to_native_inplace(x);
for (int32_t i = 0; i < 1000000; ++i)
x += i;
boost::endian::native_to_big_inplace(x);
... write x to a file ...