Library Proposal: Boost.Geom 2D and 3D Geometric Primitives

Hi ! It is very common for graphical libraries to declare some rectangle and point classes. In fact, all of them do that: SDL has SDL_Rect, X has XRectangle and XPoint, Qt has QRect and QPoint, Window$ has POINT and RECT (and POINTF and RECTF), rect, Rect, Rectangle, point, Point2D, Point3D, point<float> ..... etc .... And you know what ? they all are incompatible, both interface and implementation. Time to have a look at this, heu ? After few months of work here is what I have: // Code Start int main() { // point_xy is an implementation type it contains // "some implementation" of the point abtraction // Other implementaions include point_xyz<>, for 3D points typedef geom::point_xy<double> point_xy; // point<> is a wrapper class, in which you "plug" implementation types // to obtain a working ``point'' typedef geom::point<point_xy> point; // Plug, then save the result in ``point'' point pt=point_xy(3.4, 3.0), pt2=point_xy(0.2, 1.4); // Cool property access pt.x()++; pt.y()=pt2.rho(); pt.theta()+=3*pt2.y(); // Handy operator overloads pt+=pt2*4; // std::complex<> is also a possible implementation geom::point< std::complex<double> > pt_cplx=std::complex<double>(4, 6); pt2=pt_cplx-pt; // Mixed-implementation computations // point::impl() extracts the implementation out of its wrapper std::complex<double>& impl=pt_cplx.impl(); // Now for Boxes. Boxes are some generalization of rectanlges that are not always // ``flat'' typedef geom::box_xywh<int> box_xywh; // Implementation type .. typedef geom::box<box_xywh> box; // .. being plugged in a wrapper box bx, bx2; bx.xmin()=bx2.x2(); bx.width()=bx2.height(); bx.y2()=bx2.height(); // Accessing corners bx.corner< geom::x1, geom::y1>()=pt; // Displace bx.corner< geom::xmin, geom::ymax >()=bx2.corner<geom::x2, geom::y1, geom::z1>(); // Accessing the center bx.center()=bx2.center()+pt; bx2.x1()=bx.center().x(); // ..... } // Code End So, wadda you say ? -- Anis Benyelloul

graphical libraries usually are incompatible and sometimes even "unusual" for a reason, performance, when you´re processing lots and lots of points per second, the simple act of calling a non inline function is too much overhead. Don´t take me wrong I also believe that a "unique" implementation for points would be great, but such implementation is not trivial. I´m not sure about the internals of your implementation, but I implemented something with a similar interface sometime ago and the overhead caused by the function forwarding was unacceptable. A template solution was the fastest I could get, something like: point<2, int> my_point; If you´re intested in it take a look at http://osl.iu.edu/~tveldhui/ especially in the blitz++ library On 5/20/04, Anis Benylloul <benyelloul@mailbolt.com> wrote:
Hi !
It is very common for graphical libraries to declare some rectangle and point classes. In fact, all of them do that: SDL has SDL_Rect, X has XRectangle and XPoint, Qt has QRect and QPoint, Window$ has POINT and RECT (and POINTF and RECTF), rect, Rect, Rectangle, point, Point2D, Point3D, point<float> ..... etc ....
And you know what ? they all are incompatible, both interface and implementation. Time to have a look at this, heu ?
After few months of work here is what I have:
// Code Start int main() { // point_xy is an implementation type it contains // "some implementation" of the point abtraction // Other implementaions include point_xyz<>, for 3D points typedef geom::point_xy<double> point_xy;
// point<> is a wrapper class, in which you "plug" implementation types // to obtain a working ``point'' typedef geom::point<point_xy> point; // Plug, then save the result in ``point''
point pt=point_xy(3.4, 3.0), pt2=point_xy(0.2, 1.4);
// Cool property access pt.x()++; pt.y()=pt2.rho(); pt.theta()+=3*pt2.y();
// Handy operator overloads pt+=pt2*4;
// std::complex<> is also a possible implementation geom::point< std::complex<double> > pt_cplx=std::complex<double>(4, 6);
pt2=pt_cplx-pt; // Mixed-implementation computations
// point::impl() extracts the implementation out of its wrapper std::complex<double>& impl=pt_cplx.impl();
// Now for Boxes. Boxes are some generalization of rectanlges that are not always // ``flat''
typedef geom::box_xywh<int> box_xywh; // Implementation type .. typedef geom::box<box_xywh> box; // .. being plugged in a wrapper
box bx, bx2;
bx.xmin()=bx2.x2(); bx.width()=bx2.height(); bx.y2()=bx2.height();
// Accessing corners bx.corner< geom::x1, geom::y1>()=pt; // Displace bx.corner< geom::xmin, geom::ymax >()=bx2.corner<geom::x2, geom::y1, geom::z1>();
// Accessing the center bx.center()=bx2.center()+pt; bx2.x1()=bx.center().x();
// ..... } // Code End
So, wadda you say ?
-- Anis Benyelloul
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

I just have posted an improved version of Boost.Geom (geom-0.1.0.zip) (in the Boost SandBox and Boost Yahoo Files). Boost.Geom now includes full support for SDL, Windows' GDI, Qt, and Xlib. This means that if you're using one of them you can try Boost.Geom right now ! For example if you're using SDL: <code> #include <boost/geom/sdl/rect.hpp> // Activate SDL support typedef boost::geom::box<SDL_Rect> rect; typedef boost::geom::point< point_xy<Sint16> > point; // Now enjoy Boost.Geom rect re; point center=re.center(); // Put the topleft corner where the center was re.corner<x1, y1>()=center; // etc .... // Pass re to SDL_FillRect !!!! SDL_FillRect(re.impl(), 0xffffff); </code> And if you're using Window's GDI <code> #include <boost/geom/gdi/rect.hpp> #include <boost/geom/gdi/point.hpp> typedef boost::geom::point<POINT> point; typedef boost::geom::box<RECT> rect; //.. RECT ff={43, 34, 43, 43}; rect re=ff; // Init with ff ! // MoveTo puts the result in point (which is a geom::point<POINT>) point old_pen_pos; MoveTo(hdc, 3, 4, &old_pen_pos.impl()); </code> Now one can easily write test programs that play with geometric primitives (notably with box::operator& and box::operator|) before displaying them on some graphical output ! I'm now working to improve the manual (which is unnecessarily hard to grasp) ... -- Anis Benylloul
participants (3)
-
Anis Benyelloul
-
Anis Benylloul
-
Sérgio Vale e Pace