This Week in Boost

Well, it’s been a bit longer than a week since the first article in this series. Anyway, here we go.

Generic Geometry Library

The Formal Review of the Generic Geometry Library submitted by Barend Gehrels, Bruno Lalande, and Mateusz Loskot is scheduled for November 5, 2009 – November 15, 2009. This library has been under development for many years. It was originally proposed on the Boost developers mailing list in January of 2008.

The library uses traits templates to allow external types to model its concepts. For instance, built-in arrays can be used as points.

int a[2] = {1,1};
int b[2] = {2,3};
double d = distance(a, b);
std::cout << "Distance a-b is:" << d << std::endl;

The library provides a number of algorithms, including length, centroid, intersection, and convex hull.

Properties

Peder Holt proposed a system that allows data-member like access backed by functions. For instance, here’s a complex number class that stores its values in polar form, but allows read/write access to the real and imaginary parts as well.

class complex {
public:
    complex(double real = 0, double imag = 0)
      : r(std::hypot(real, imag)), theta(std::atan2(imag, real))
    {}
    template<class Base>
    struct real_property : Base {
        void set(const double& value) {
            double real = value;
            double imag = this->self()->imag;
            this->self()->r = std::hypot(real, imag);
            this->self()->theta = std::atan2(imag, real);
        }
        double get() const {
            return this->self()->r * std::cos(this->self()->theta);
        }
    };
    BOOST_PROPERTY(complex, real_property, real);
    template<class Base>
    struct imag_property : Base {
        void set(const double& value) {
            double real = this->self()->real;
            double imag = value;
            this->self()->r = std::hypot(real, imag);
            this->self()->theta = std::atan2(imag, real);
        }
        double get() const {
            return this->self()->r * std::sin(this->self()->theta);
        }
    };
    BOOST_PROPERTY(complex, imag_property, imag);
    double r;
    double theta;
};
 
void test() {
    complex c(1);
    c.imag = 1;
}

Vectors and Matrices

Emil Dotchevski proposed a library that supports operations on statically sized vectors and matrices. Using free functions and traits templates it can operate on any matrix or vector type. By default, the library supports its own types and built in arrays (Note that C-arrays cannot be returned by value. The library uses a template called deduce_matrix to find a type that it can return).

double matrix[3][3] = {
    {1, 1, 1},
    {1, 2, 3},
    {1, 3, 6}
};
mat<double, 3, 3> inverse_matrix = inverse(matrix);

The library supports a number of views using the | operator. For instance

double matrix[3][3] = {
    {1, 1, 1},
    {1, 2, 3},
    {1, 3, 6}
};
mat<double, 3, 3> transpose_matrix = matrix | transpose;

There are views for extracting rows and columns of a matrix as vectors, treating a vector as a 1 by N or N by 1 matrix, removing rows or columns from a matrix, and swizzling.

double vector[3] = {1, 2, 3};
vec<double, 3> rearranged_vector = vector | XZY;
// rearranged_vector = [1, 3, 2]
Posted Tuesday, October 27th, 2009 under Boost.

17 Responses to “This Week in Boost”

  1. Rob says:

    The comment font is taller than the blog font, at least in my Firefox. FF can remember how much to zoom text per site, but either the blog font is too small or the comment font is too large; I can’t find a happy medium. Can you make the too a little more balanced? Then I can zoom FF the way I want it, without having to resize my window (which didn’t work anyway).

      (Quote)
  2. alfC says:

    Steven, great posts. If you can, please write a post with an easy introduction to Proto for dummies. At moments it seems that it is very powerful but its usage is totally misterious to me. I am sure is somebody can explain it in plain english is you. thanks.

      (Quote)
  3. lego says:

    The use of operator overloading seems a bit like abuse.

    Why would I prefer

    m = matrix | transpose;

    over

    m = transpose(matrix);

    That aside it looks good.

      (Quote)
  4. Dat Chu says:

    This math library is indeed nicely done. But I think it is a bit overlapped with what is already done with C++ wrapper for OpenCV 2.0 . Then again, those who already have Boost might not want to install yet another library.

      (Quote)
  5. Jeremy says:

    Very nice. I like this library a lot more than CGAL already… not that we’re all about competition when it comes to geometry libraries (they all have different focuses… but CGAL isn’t very friendly for especially some of the expansion I sought to do with intersection testing).

    Now, I just need to see if you can cleverly wrap this up with some gfx optimizations. :D

    Thanks again for doing this column. I’ve been looking forward to GGL for a while now.

      (Quote)
  6. Alex Karpenko says:

    Very nice library. It would be amazing if the library also provided expression templates and operator overloading for arithmetic operations. Of course the custom point class could implement these itself, but I wonder whether it’s possible for the library to provide generic versions instead. That would make writing point classes 10x easier. For example, the library already provides:

    template<typename P1 , typename P2 >
    void geometry::add_point(P1 & p1, const P2 & p2)
     

    but something like this would be more convenient:

    template<typename P1 , typename P2 >
    add_expr<P1,P2> operator + (const P1 & p1, const P2 & p2)
     

    Regardless, the library’s agnosticism with respect to the point class would make many of its algorithms easily adaptable to some existing implementations out there (e.g. DLib’s Matrix class: http://dclib.sourceforge.net/matrix_ex.cpp.html )

      (Quote)
  7. Chila says:

    Hi Dave. Two things: - Is there a chance that this blog has a “printable version” link/button somewhere? - Why is the font so big? Could it be smaller?

    Thank you for creating Boost!

    C++ & Boost rock.

      (Quote)
    • Chila,

      Sorry, there’s no “printable version” link. If someone would like to develop print CSS for us, though, we’d be grateful. The font is so big because your browser window is wide. Some people hate large fonts; I hate reading long lines :-) . Try making the window narrower

        (Quote)
      • Eric says:

        I too have noticed the fonts are rather large. But I tried your resizing trick and it worked! No more zooming just for this site!

        The only problem is, see, all the content in my other tabs are NORMAL, and they don’t work so well with a narrow window, and I have to zoom ALL of them if I want to accommodate this one site without zooming its text.

        So, stop telling people to make their windows narrow, this is quite wrong. Fix your site so it doesn’t do loony things with text and people will stop complaining about the large fonts. Set the size to 13pt and forget about it.

          (Quote)
    • Sean says:

      Since most “nicely designed” sites use quasi-unreadable fonts, you might like to check out Greasemonkey. Here’s my GM script for this page, which does away with the stylish-but-strain-inducing low-contrast font color.

      // ==UserScript==
      // @name           Cpp-next.com
      // @namespace      http://aaa.com
      // @include        http://cpp-next.com/*
      // @require        http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js
      // ==/UserScript==

      $('.entry').css({ 'font-family': "Serif" }); $('body').css({ color: 'black' });

        (Quote)
  8. std::set says:

    You must have Javascript enabled to view this site.

    Unnecessary.

      (Quote)
    • That’s true, it is unnecessary. The site works fine for me in lynx or w3m. Unfortunately, I’ve been unable to figure out how to turn that check off. Are there any PhP experts out there who’d like to take a crack at it?

        (Quote)
      • Sean Hunt says:

        Just remove the <noscript> tag right after the <body> tag.

          (Quote)
        • Hmm, are you sure? For me that just makes the page come up blank when JavaScript is off.

            (Quote)
          • Sean says:

            The problem is on line 175 of style.css:

             
            page {
             
              font-size: 1.6em;
              line-height: 1.66em;
              padding: 0;
              margin: 0;
              visibility: hidden;
            }

            Remove that “visibility: hidden;” and the page will be visible with JS turned off. The file required.js has a line that says:

            this.page.style.visibility = 'visible'; // hides the initial pageload blip

            so presumably making this change will re-enable the initial pageload blip, whatever that is.

              (Quote)
          • Sean says:

            Hrm; my previous comment seems to have been eaten, so I’ll try again in brief: comment out “visibility: hidden;” from line 180 of style.css and the page will be visible with JS turned off.

            Presumably it works under lynx because that browser ignores CSS.

              (Quote)
  9. triton says:

    The math library is nice, I might use it for 3D stuff. Thanks for doing this, very useful.

      (Quote)

Leave a Comment (post replies using links below individual comments)

Spam Protection by WP-SpamFree

Subscribe without commenting