Hi,

2014-01-30 Barend Gehrels <barend@xs4all.nl>:

Adam Wulkiewicz wrote On 30-1-2014 15:32:
I was wondering if it would be possible to implement some nice UDL for BG models. I thought it would be great to have a compile-time WKT which whould work e.g. like this:

"LINESTRING(0 0, 1 1, 2 2)"_wkt

if we could parse this in compile-time we'd be able to return proper type. Unfortunately AFAIK it's (currently?) impossible to parse string literals in compile-time. If you could proof me wrong I'd be very glad :). So I've implemented some simplified version which can be found here:

https://github.com/awulkiew/test_boost_geometry_udl

Any ideas are welcome. It works like this:
using namespace bg::literals::cart2d;

"(1 1)"_pt; // point<double, 2, cs::cartesian>
"(0 0, 2 2)"_box; // box<p2d>
"(0 0, 1 1, 2 1)"_ls; // linestring<p2d>

// polygon<p2d> - for now the default cw closed, should probably be ccw
"((1 0, 1 3, 3 3, 3 0, 1 0))"_poly

// ring<p2d> - for now the default cw closed, should probably be ccw
// also non-WKT definition could be used - without doubled parentheses
"((1 0, 1 3, 3 3, 3 0, 1 0))"_ring
Of course you must compile it with some compiler supporting user-defined literals, e.g. GCC >= 4.7 or Clang >= 3.1

Interesting.

Note that I did some experiments in the past to get a similar effect, though implemented differently. If we would use Proto we are able to compile things like this:

    // Initialize a ring:
    typedef bg::model::ring<bg::model::d2::point_xy<double> > ring_type;
    ring_type ring = geometry_of(16, 1)(15,2)(14, 3)(13,4)(12, 3.14)(1,6);
    std::cout << bg::wkt(ring) << std::endl;

It does obviously not have the WKT syntax, but it should be possible to parse any geometry type with this.

See ....\extensions\example\experimental\geometry_of.cpp
(it is unfinished)


Ok, thanks. This expression is quite simple, I think it could be easily implemented without Proto. Wouldn't including Proto significantly increase the compilation time? I fear that nobody would use it. But now with C++11 something similar could be easily achieved with unified initialization and initializer lists:

ring_type ring{{16,1},{15,2},{14,3},{13,4},{12,3.14},{1,6}};

// not entirely sure but maybe something like this:
polygon_type polygon{{{0,0},{0,10},{10,10},{10,0},{0,0}},{{1,1},{2,1},{2,2},{1,2},{1,1}}};

and btw we should probably support it anyway.

Regards,
Adam