Boost logo

Boost :

Subject: Re: [boost] GGL - segments [was: Updating the Boost Review Process]
From: Barend Gehrels (barend_at_[hidden])
Date: 2009-11-19 12:21:21


Hi Brandon,

>> The segment type provided by default is indeed holding references and
>> it does that on purpose. That avoids copying points all the time.
>> AFAIK the segment intersection algorithm does not use those
>> references, it refers to segments in the generic way, using
>> get<0,0>(segment) etc. It makes no difference if you're using a
>> segment by reference or a segment by value.
>
> Maybe so, but that still doesn't help when you want to have a
> collection of segments. For example, in my testing I wanted to
> calculate the intersection between a huge number of segments, and with
> GGL's you can't do this:
>
> typedef boost::tuple<double, double> point_type;
> typedef ggl::segment<point_type> segment_type;
>
> //! segment_type does not model the default constructible concept.
> std::vector< segment_type > segments;
>
That is true indeed. The segment is not meant for that. However, you can
use it like below. This was meant to be included in the review, tests
folder, but apparently I forgot that, or considered it as too rough, sorry.
I hope that the speed of my answer will prove here that at least this
one is not concocted on the fly :-)

Regards, Barend

// Generic Geometry Library test file
//
// Copyright Barend Gehrels, 1995-2009, Geodan Holding B.V. Amsterdam,
the Netherlands.
// Copyright Bruno Lalande 2008, 2009
// Use, modification and distribution is subject to the Boost Software
License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <iostream>

#include <ggl_test_common.hpp>

#include <ggl/geometries/concepts/segment_concept.hpp>

#include <ggl/geometries/point.hpp>
#include <ggl/geometries/segment.hpp>

#include <ggl/geometries/register/point.hpp>
#include <ggl/geometries/register/segment.hpp>

#include <ggl/geometries/adapted/c_array_cartesian.hpp>
#include <ggl/geometries/adapted/tuple_cartesian.hpp>

#include <ggl/util/write_dsv.hpp>

#include <test_common/test_point.hpp>

template <typename P>
void test_all()
{
    typedef ggl::segment<P> S;

    P p1;
    P p2;
    S s(p1, p2);
    BOOST_CHECK_EQUAL(&s.first, &p1);
    BOOST_CHECK_EQUAL(&s.second, &p2);

    // Compilation tests, all things should compile.
    BOOST_CONCEPT_ASSERT( (ggl::concept::ConstSegment<S>) );
    BOOST_CONCEPT_ASSERT( (ggl::concept::Segment<S>) );

    typedef typename ggl::coordinate_type<S>::type T;
    typedef typename ggl::point_type<S>::type SP;

    //std::cout << sizeof(typename coordinate_type<S>::type) << std::endl;

    typedef ggl::segment<const P> CS;
    //BOOST_CONCEPT_ASSERT( (concept::ConstSegment<CS>) );

    CS cs(p1, p2);

    typedef typename ggl::coordinate_type<CS>::type CT;
    typedef typename ggl::point_type<CS>::type CSP;
}

struct custom_point
{
    double x, y;
};
struct custom_segment
{
    custom_point one, two;
};
template <typename P>
struct custom_segment_of
{
    P p1, p2;
};
struct custom_segment_4
{
    double a, b, c, d;
};

GEOMETRY_REGISTER_POINT_2D(custom_point, double, ggl::cs::cartesian, x, y)
GEOMETRY_REGISTER_SEGMENT(custom_segment, custom_point, one, two)
GEOMETRY_REGISTER_SEGMENT_TEMPLATIZED(custom_segment_of, p1, p2)
GEOMETRY_REGISTER_SEGMENT_2D_4VALUES(custom_segment_4, custom_point, a,
b, c, d)

template <typename S>
void test_custom()
{
    S seg;
    ggl::set<0,0>(seg, 1);
    ggl::set<0,1>(seg, 2);
    ggl::set<1,0>(seg, 3);
    ggl::set<1,1>(seg, 4);
    std::ostringstream out;
    out << ggl::dsv(seg);
    BOOST_CHECK_EQUAL(out.str(), "((1, 2), (3, 4))");
}

int test_main(int, char* [])
{
    test_all<int[3]>();
    test_all<float[3]>();
    test_all<double[3]>();
    //test_all<test_point>();
    test_all<ggl::point<int, 3, ggl::cs::cartesian> >();
    test_all<ggl::point<float, 3, ggl::cs::cartesian> >();
    test_all<ggl::point<double, 3, ggl::cs::cartesian> >();

    test_custom<custom_segment>();
    test_custom<custom_segment_of<ggl::point<double, 2,
ggl::cs::cartesian> > >();
    test_custom<custom_segment_of<custom_point> >();
    test_custom<custom_segment_4>();

    return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk