I want to have class, which uses boost::geometry::index::rtree for spatial indexers. This class alone should know about boost, so I use something like this:
struct VeryImportantInfo
{
...
float x;
float y;
}
class Catalogue
{
...
public:
std::vector<std::shared_ptr<VeryImportantInfo> > FindIn(float x1, float x2, float y1, float y2);
protected:
using point = bg::model::point<float, 2, bg::cs::cartesian>;
using value = std::pair<point, std::shared_ptr<VeryImportantInfo> >;
using box = bg::model::box<point>;
boost::geometry::index::rtree< value, bgi::quadratic<16> > rtree;
}
std::vector<std::shared_ptr<VeryImportantInfo> > Catalogue::FindIn(float x1, float y1, float x2, float y2)
{
box query_box(point(x1, y1), point(x2, y2));
???
}
I don't know how to do query properly (Please don't look at this awful vector return by copy, it is just for example sake). I can do this:
std::vector<std::shared_ptr<VeryImportantInfo> > Catalogue::FindIn(float x1, float y1, float x2, float y2)
{
box query_box(point(x1, y1), point(x2, y2));
std::vector<value> result_s;
rtree.query(bgi::intersects(query_box), std::back_inserter(result_s));
std::vector<std::shared_ptr<VeryImportantInfo> > results;
results.reserve(result_s.size());
for( auto& p : result_s)
{
results.emplace_back(p.second);
}
return results;
}
I want to know, how can I get rid of internal copy(not return copy, results.emplace_back(p.second); - this one). Because I can have more that 10k results in result_s and it will be a waste.
Thank you!
Update to the comment
If the worry was about the temporary vector in the first place, just don't use one. You can use the qbegin()/qend() free functions from boost::geometry::index:
std::vector<std::shared_ptr<VeryImportantInfo> > Catalogue::FindIn(float x1, float y1, float x2, float y2)
{
box query_box(point(x1, y1), point(x2, y2));
auto b = bgi::qbegin(rtree, bgi::intersects(query_box)),
e = bgi::qend(rtree);
auto range = boost::make_iterator_range(b, e);
using namespace boost::adaptors;
return boost::copy_range<std::vector<std::shared_ptr<VeryImportantInfo>>>(
range | transformed([](value const& p) { return p.second; }));
}
In fact, if the rtree is known to be constant, you could even return the lazy range directly and not allocate even the single vector.
original/old answer text follows:
You cannot copy shared pointers without reference counting.
Of course, you could change the value pair to contain a reference to the shared_ptr, but instead you could then employ either raw references (std::reference_wrapper) or weak_ptr.
std::reference_wrapper<T>
Here's my take on it with raw references (just keep your important data around :)):
Live On Coliru
#include <iostream>
#include <vector>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/index/rtree.hpp>
namespace bg = boost::geometry;
namespace bgi = bg::index;
struct VeryImportantInfo {
float x;
float y;
};
VeryImportantInfo a = { 2, 2 };
VeryImportantInfo b = { 3, 3 };
VeryImportantInfo c = { 4, 4 };
class Catalogue
{
public:
Catalogue() {
rtree.insert(value(point(a.x, a.y), a));
rtree.insert(value(point(b.x, b.y), b));
rtree.insert(value(point(c.x, c.y), c));
}
std::vector<std::reference_wrapper<VeryImportantInfo> > FindIn(float x1, float x2, float y1, float y2);
protected:
using point = bg::model::point<float, 2, bg::cs::cartesian>;
using value = std::pair<point, std::reference_wrapper<VeryImportantInfo> >;
using box = bg::model::box<point>;
boost::geometry::index::rtree< value, bgi::quadratic<16> > rtree;
};
std::vector<std::reference_wrapper<VeryImportantInfo> > Catalogue::FindIn(float x1, float y1, float x2, float y2)
{
box query_box(point(x1, y1), point(x2, y2));
std::vector<value> result_s;
rtree.query(bgi::intersects(query_box), std::back_inserter(result_s));
std::vector<std::reference_wrapper<VeryImportantInfo> > results;
results.reserve(result_s.size());
for(auto& p : result_s) {
results.push_back(p.second);
}
return results;
}
int main() {
Catalogue cat;
for (VeryImportantInfo& vii : cat.FindIn(1,2,3,4))
std::cout << vii.x << ", " << vii.y << "\n";
}
std::weak_ptr<T>
Here is the same with weak_ptr<>. One could argue this doesn't solve much (because ref counting is still happening) but at least less work is required.
Live On Coliru
#include <iostream>
#include <memory>
#include <vector>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/index/rtree.hpp>
namespace bg = boost::geometry;
namespace bgi = bg::index;
struct VeryImportantInfo {
float x;
float y;
};
auto a = std::make_shared<VeryImportantInfo>(VeryImportantInfo{2, 2});
auto b = std::make_shared<VeryImportantInfo>(VeryImportantInfo{3, 3});
auto c = std::make_shared<VeryImportantInfo>(VeryImportantInfo{4, 4});
class Catalogue
{
public:
Catalogue() {
rtree.insert(value(point(a->x, a->y), a));
rtree.insert(value(point(b->x, b->y), b));
rtree.insert(value(point(c->x, c->y), c));
}
std::vector<std::weak_ptr<VeryImportantInfo> > FindIn(float x1, float x2, float y1, float y2);
protected:
using point = bg::model::point<float, 2, bg::cs::cartesian>;
using value = std::pair<point, std::shared_ptr<VeryImportantInfo> >;
using box = bg::model::box<point>;
boost::geometry::index::rtree< value, bgi::quadratic<16> > rtree;
};
std::vector<std::weak_ptr<VeryImportantInfo> > Catalogue::FindIn(float x1, float y1, float x2, float y2)
{
box query_box(point(x1, y1), point(x2, y2));
std::vector<value> result_s;
rtree.query(bgi::intersects(query_box), std::back_inserter(result_s));
std::vector<std::weak_ptr<VeryImportantInfo> > results;
results.reserve(result_s.size());
for(auto& p : result_s) {
results.push_back(p.second);
}
return results;
}
int main() {
Catalogue cat;
for (auto& vii_p : cat.FindIn(1,2,3,4))
if (auto vii = vii_p.lock())
std::cout << vii->x << ", " << vii->y << "\n";
}
Related
I have dozens of such functions, those are calls to some 3rd party external library. Is there any way to simplify it using some modern c++ feature? I want to avoid macros.
class Renderer;
Renderer* pointer;
void DrawSetRedColor(int r) {
using DrawSetRedColor_prototype = void(__thiscall**)(Renderer*, int);
return (*(DrawSetRedColor_prototype)(*(DWORD*)pointer + 0x48))(pointer, r);
}
int DrawLine(int x0, int y0, int x1, int y1) {
using DrawLine_prototype = int(__thiscall**)(Renderer*, int, int, int, int);
return (*(DrawLine_prototype)(*(DWORD*)pointer + 0x4C))(pointer, x0, y0, x1, y1);
}
Something like this would probably work:
template <typename R, typename... Args>
R CallThroughVtable(void* pointer, size_t offset, Args... args) {
using prototype = R (__thiscall**)(void*, Args...);
return (*(prototype)(*(DWORD*)pointer + offset))(pointer, args...);
}
void DrawSetRedColor(int r) {
CallThroughVtable<void>(pointer, 0x48, r);
}
int DrawLine(int x0, int y0, int x1, int y1) {
return CallThroughVtable<int>(pointer, 0x4C, x0, y0, x1, y1);
}
I am trying to use the boost geometry method intersects with my own point class, successfully registered with the boost geometry library.
The boost documentation ( https://www.boost.org/doc/libs/1_73_0/libs/geometry/doc/html/geometry/reference/algorithms/intersection/intersection_3.html ) says that I can use a vector of points as the output parameter. So I wrote this:
#include <iostream>
#include <deque>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
namespace bg = boost::geometry;
using namespace std;
class cxy
{
public:
double x;
double y;
cxy( double X, double Y )
: x( X )
, y( Y )
{
}
/// boost geometry insists on a default constructor
cxy()
: cxy(0,0)
{
}
};
BOOST_GEOMETRY_REGISTER_POINT_2D( cxy, double, bg::cs::cartesian, x, y )
typedef bg::model::segment<cxy> segment_t;
int main()
{
cxy a(0,0);
cxy b(1,1);
cxy c(1,0);
cxy d(0,1) ;
std::vector<cxy> out;
//////////////// this compiles
bg::intersection(segment_t{a, b}, segment_t{c, d}, out);
//////////////// this does not!!!
segment_t ab( a, b );
segment_t cd( c, d );
bg::intersects( ab, cd, out );
return 0;
}
To be clear: my problem was that I was confused between intersection and intersects
The following code compiles and produces the expected results:
#include <iostream>
#include <deque>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
namespace bg = boost::geometry;
using namespace std;
class cxy
{
public:
double x;
double y;
cxy( double X, double Y )
: x( X )
, y( Y )
{
}
/// boost geometry insists on a default constructor
cxy()
: cxy(0,0)
{
}
};
BOOST_GEOMETRY_REGISTER_POINT_2D( cxy, double, bg::cs::cartesian, x, y )
typedef bg::model::segment<cxy> segment_t;
int main()
{
cxy a(0,0);
cxy b(1,1);
cxy c(1,0);
cxy d(0,1) ;
segment_t ab( a, b );
segment_t cd( c, d );
std::vector<cxy> out;
if( ! bg::intersection( ab, cd, out ) ) {
std::cout << "no intersection\n";
return 1;
}
std::cout << "intersection at " << out[0].x <<" " << out[0].y << "\n";
return 0;
}
You're asking "whether a intersects b".
However, your third argument suggests you wanted instead to ask for "the intersection":
Live On Coliru
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <iostream>
namespace bg = boost::geometry;
using namespace std;
struct cxy {
double x = 0;
double y = 0;
cxy(double X, double Y) : x(X), y(Y) {}
cxy() = default;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(cxy, double, bg::cs::cartesian, x, y)
using segment_t = bg::model::segment<cxy>;
using points_t = bg::model::multi_point<cxy>;
int main() {
cxy a{0, 0}, b{1, 1}, c{1, 0}, d{0, 1};
std::vector<cxy> out;
bg::intersection(segment_t{a, b}, segment_t{c, d}, out);
// for output, use a multipoint model or register your vector as one
points_t pts;
bg::intersection(segment_t{a, b}, segment_t{c, d}, pts);
std::cout << bg::wkt(pts) << "\n";
}
Prints
MULTIPOINT((0.5 0.5))
I am trying to calculate distance between a point and a line segment. But the following code terminates during runtime abruptly (#comparable_distance).
using namespace std;
namespace bg = boost::geometry;
class Vertex {
public:
Vertex(int px, int py):x(px), y(py){}
int x;
int y;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, cs::cartesian, x, y)
typedef Vertex* vertref;
typedef bg::model::segment<vertref> segment_type;
std::vector<segment_type> segments;
int main()
{
segment_type l(new Vertex(1,2), new Vertex(2,2));
Vertex p(4,5);
double comp_dist = 0;
comp_dist = bg::comparable_distance(p, l);
cout<<comp_dist<<endl;
return 0;
}
If I replace bg::model::segment with linestring; add two points to it, it works without any error as in below ...
using namespace std;
namespace bg = boost::geometry;
class Vertex {
public:
Vertex(int px, int py):x(px), y(py){}
int x;
int y;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, cs::cartesian, x, y)
typedef Vertex* vertref;
typedef bg::model::linestring<vertref> segment_type;
std::vector<segment_type> segments;
int main()
{
segment_type l;
l.push_back(new Vertex(1,2));
l.push_back(new Vertex(2,2));
Vertex p(4,5);
double comp_dist = 0;
comp_dist = bg::comparable_distance(p, l);
cout<<comp_dist<<endl;
return 0;
}
Any ideas what I am doing wrong in the first code using segment?
Thanks!
Call Stack ...
#0 0x40486f boost::geometry::traits::access<Vertex, 0u, void>::set(p=..., value=#0x63fde8: 1) (C:\Projects\test\main.cpp:17)
#1 0x403e14 boost::geometry::core_dispatch::access<boost::geometry::point_tag, Vertex, double, 0u, boost::integral_constant<bool, true> >::set(p=0x77b12580, value=#0x63fde8: 1) (C:/boost_1_68_0/boost/geometry/core/access.hpp:187)
#2 0x404182 boost::geometry::set<0u, Vertex*>(geometry=#0x63fe34: 0x77b12580, value=#0x63fde8: 1, dummy=0x0) (C:/boost_1_68_0/boost/geometry/core/access.hpp:321)
#3 0x40469a boost::geometry::detail::assign::assign_point_from_index<boost::geometry::model::segment<Vertex*>, Vertex*, 0u, 0u, 2u>::apply(geometry=..., point=#0x63fe34: 0x77b12580) (C:/boost_1_68_0/boost/geometry/algorithms/detail/assign_values.hpp:195)
#4 0x4045de boost::geometry::detail::assign_point_from_index<0u, Vertex*, boost::geometry::model::segment<Vertex*> >(geometry=..., point=#0x63fe34: 0x77b12580) (C:/boost_1_68_0/boost/geometry/algorithms/detail/assign_indexed_point.hpp:80)
#5 0x4049fc boost::geometry::dispatch::distance<Vertex, boost::geometry::model::segment<Vertex*>, boost::geometry::strategy::distance::projected_point<void, boost::geometry::strategy::distance::comparable::pythagoras<void> >, boost::geometry::point_tag, boost::geometry::segment_tag, boost::geometry::strategy_tag_distance_point_segment, false>::apply(point=..., segment=..., strategy=...) (C:/boost_1_68_0/boost/geometry/algorithms/detail/distance/point_to_geometry.hpp:419)
#6 0x403f9b boost::geometry::resolve_strategy::comparable_distance::apply<Vertex, boost::geometry::model::segment<Vertex*> >(geometry1=..., geometry2=...) (C:/boost_1_68_0/boost/geometry/algorithms/detail/comparable_distance/interface.hpp:83)
#7 0x403f38 boost::geometry::resolve_variant::comparable_distance<Vertex, boost::geometry::model::segment<Vertex*> >::apply<boost::geometry::default_strategy>(geometry1=..., geometry2=..., strategy=...) (C:/boost_1_68_0/boost/geometry/algorithms/detail/comparable_distance/interface.hpp:106)
#8 0x403ff5 boost::geometry::comparable_distance<Vertex, boost::geometry::model::segment<Vertex*>, boost::geometry::default_strategy>(geometry1=..., geometry2=..., strategy=...) (C:/boost_1_68_0/boost/geometry/algorithms/detail/comparable_distance/interface.hpp:328)
#9 0x403fc9 boost::geometry::comparable_distance<Vertex, boost::geometry::model::segment<Vertex*> >(geometry1=..., geometry2=...) (C:/boost_1_68_0/boost/geometry/algorithms/detail/comparable_distance/interface.hpp:356)
#10 0x401518 main() (C:\Projects\test\main.cpp:30)
The Segment Concept is modeled by model::segment, which takes a template parameter that models a Point.
Now you used:
typedef Vertex *vertref;
typedef bg::model::segment<vertref> segment_type;
Note that vertref does not in fact model the Point concept. So, by contrast, consider:
Live On Coliru
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <iostream>
namespace bg = boost::geometry;
struct Vertex {
double x;
double y;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, bg::cs::cartesian, x, y)
typedef bg::model::segment<Vertex> segment_type;
int main() {
segment_type l({1, 2}, {2, 2});
Vertex p {4, 5};
auto comp_dist = bg::comparable_distance(p, l);
std::cout << comp_dist << "\n";
}
Prints:
13
Indeed 13 is the square of the actual distance:
BONUS
In response to the comment, yes there's even an out-of-the-box model for segments referring to points (from other geometries). Here's how to use referring_segment<>:
Live On Coliru
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <iostream>
namespace bg = boost::geometry;
struct Vertex {
double x;
double y;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, bg::cs::cartesian, x, y)
typedef bg::model::referring_segment<Vertex> segment_type;
int main() {
Vertex A{1,2},
B{2,2};
segment_type l(A, B);
Vertex p {4, 5};
std::cout << bg::distance(p, l) << "\n";
bg::multiply_value(A, -1);
bg::multiply_value(B, -1);
std::cout << bg::distance(p, l) << "\n";
}
Which prints distance before and after mutation:
3.60555
8.60233
Again this matches what you'd expect to see:
Notes:
using new in C++ is almost always a mistake. This mistake is most often made by beginners coming from garbage-collected languages like Java or C# (Why should C++ programmers minimize use of 'new'?)
there was an inconsistency of types (the members x, y were ints)
The essence of what I want to do is to take two instances of Vector2D and create a third vector that is to be returned and made into the third instance. The problem I am facing is that I am not entirely sure on how to go ahead in doing so. I have tried to find the syntax for sending in instances, if there is such a one, but I have not managed to find anything useful in any of my books.
#include<iostream>
#include<string>
#include<array>
using namespace std;
class vector2D
{
public:
array<float, 2> get()
{
return xy_coord;
}
void set(float x, float y)
{
xy_coord[0] = x;
xy_coord[1] = y;
}
array<float, 2> vectorAdd(a, b)
{
array<float, 2> c;
for (int i = 0; i < 2; i++)
{
c[i] = a[i] + b[i];
}
return c;
}
private:
array<float, 2> xy_coord;
};
int main()
{
string y;
vector2D a, b, c;
array<float, 2> temp;
a.set(2.0, 3.0);
b.set(4.0, 5.0);
temp = c.vectorAdd(a, b);
c.set(temp[0], temp[1]);
getline(cin, y);
}
The idea is to send in the instances a and b to vectorAdd and sum them up and then set c equal to the returned value (I am sure there is a better way to write the code in the main(), but I am not sure how). In short, what would a and b need to be defined as to make this work, assuming it can work at all.
Maybe you could do something like this instead, so you don't have to pass array around:
#include <iostream>
class Vector2D
{
private:
double _x;
double _y;
public:
Vector2D() = delete;
Vector2D(double x, double y) : _x(x), _y(y) {}
double X() const { return _x; }
double Y() const { return _y; }
Vector2D operator+(Vector2D const &v) const
{
return Vector2D(X() + v.X(), Y() + v.Y());
}
};
int main()
{
Vector2D v1(10.0, 20.0);
Vector2D v2(100.0, 200.0);
Vector2D v3 = v1 + v2;
std::cout << v3.X() << " " << v3.Y();
return 0;
}
Prints:
110 220
Do you need to use array<float, 2>? Have you thought of using pair<float, float>?
A lot (all?) of the operations that you have in your Vector2D class come for free with Pair<>.
Then you just create operator+ as others have suggested.
#include <iostream>
#include <utility>
using namespace std;
using Coord = pair<float, float>;
template <typename L, typename R>
Coord operator+(const L& x, const R& y) { return std::make_pair(x.first + y.first, x.second + y.second); }
int main()
{
Coord a { 5.0f, 6.0f };
Coord b { 7.0f, 9.0f };
Coord c = a + b;
std::cout.precision(5);
std::cout << "c= (" << std::fixed << c.first << ", " << c.second << ")" << std::endl;
return 0;
}
This answer is helpful, but I would like to know how to pass multiple parameters of different types to the ODE model, perhaps in a struct. For my immediate use case, I need to be able to pass one std::array<double, 6>, two std::vector<std::vector<double>> and two two double scalars for a total of four parameters to be passed. In the linked example, as well as in harmonic_oscillator.cpp, there is only a single double passed parameter. Thanks.
Here's an example of the struct I would need passed to the ODE force model and used within the rate equations.
struct T
{
std::array<double, 6> IC;
double S;
double M;
std::vector<std::vector<double>> C;
std::vector<std::vector<double>> WT;
};
I believe I've come up with a struct solution that works, but am not sure if it has any variable/memory scope no-no's. Here's an example:
#include <vector>
#include <boost/numeric/odeint.hpp>
// define structure
struct T
{
std::array<double, 6> IC;
double S;
};
// force model
class harm_osc
{
struct T T1;
public:
harm_osc(struct T G) : T1(G) {}
void operator() ( const std::vector< double > &x , std::vector< double > &dxdt , const double /* t */ )
{
dxdt[0] = x[1];
dxdt[1] = -x[0] - T1.IC[0]*x[1] + T1.S;
}
};
// print integrated state solution
void write_solution( const std::vector< double > &x , const double t )
{
printf("%-6.2f %-6.2f %-6.2f\n", t, x[0], x[1]);
}
// problem setup
int main()
{
std::vector< double > x(2);
x[0] = 1.0;
x[1] = 0.0;
struct T T2;
T2.IC = {0.15, 0.15, 0.15, 0.15, 0.15, 0.15};
T2.S = 0.0;
harm_osc ho(T2);
boost::numeric::odeint::integrate(ho, x, 0.0, 10.0, 0.1, write_solution);
}