C ++ generates (xyz) points in a range

Is there a better way to create a list of points like this? Libraries Wise I am open to any method based on Eigen.

auto it = voxels.begin(); for(auto i = -180; i < 90; i++) { for(auto j = -80; j < 70; j++) { for(auto k = 20; k < 460; k++) { *it = (Point3(i,j,k)); it++; } } } 
+4
source share
1 answer

There is an immediate way to improve performance by reserving enough space in the vector before you fill it with values.

There are many โ€œgoodโ€ ways to do this, depending on what you think is enjoyable.

Here is one way:

 std::vector<Point3> populate() { // (arguable) maintainability benefit constexpr auto I = axis_limits(-180, 90); constexpr auto J = axis_limits(-80, 70); constexpr auto K = axis_limits(20, 460); // pre-reserve the space std::vector<Point3> voxels; voxels.reserve(volume(I, J, K)); // although it looks like it might be more work for the compiler, it gets optimised // there is no loss of performance for(i : I) for(j : J) for(k : J) voxels.emplace_back(i, j, k); return voxels; } 

Which will rely on the following infrastructure code:

 struct Point3 { Point3(int, int, int) {} }; struct int_generator { int_generator(int v) : _v(v) {} int operator*() const { return _v; } int_generator& operator++() { ++_v; return *this; } bool operator!=(const int_generator& rhs) const { return _v != rhs._v; } private: int _v; }; struct axis_limits : std::tuple<int, int> { using std::tuple<int, int>::tuple; int_generator begin() const { return std::get<0>(*this); } int_generator end() const { return std::get<1>(*this); } }; constexpr int lower(const axis_limits& t) { return std::get<0>(t); } constexpr int upper(const axis_limits& t) { return std::get<1>(t); } int_generator begin(const axis_limits& t) { return std::get<0>(t); } int_generator end(const axis_limits& t) { return std::get<1>(t); } constexpr int volume(const axis_limits& x, const axis_limits& y, const axis_limits& z) { return (upper(x) - lower(x)) * (upper(y) - lower(y)) * (upper(z) - lower(z)); } 
+4
source

Source: https://habr.com/ru/post/986982/


All Articles