Boost logo

Boost Users :

Subject: [Boost-users] [units] subtract_typeof_helper<> help!
From: Terry Golubiewski (tjgolubi_at_[hidden])
Date: 2010-05-18 00:45:29


I'm trying to use boost::geometry and boost::units together.
For some reason boost::geometry doesn't represent the difference of two
points as vectors.
So, I'm making my own Vector class and defining Vector = Point - Point.
The sample program below doesn't compile because I need to tell the units
library about the special properties of point subtraction (I think).

terry

#include <boost/geometry/geometry.hpp>

#include <boost/units/systems/si.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/io.hpp>

#include <iostream>
#include <iomanip>

using namespace std;
using namespace boost;
using namespace boost::geometry;
using namespace boost::units;

typedef point<double, 3, cs::cartesian> Point;

class Vector {
  Point _impl;

  friend Vector operator-(const Point& lhs, const Point& rhs);
  explicit Vector(Point p) : _impl(p) { }

public:
  Vector() : _impl(0) { }

  Vector(double dx, double dy, double dz=0) : _impl(dx, dy, dz) { }

  friend ostream& operator<<(ostream& os, const Vector& v)
    { return os << dsv(v._impl); }

}; // Vector

inline
Vector operator-(const Point& lhs, const Point& rhs) {
  Point tmp(lhs);
  subtract_point(tmp, rhs);
  return Vector(tmp);
}

inline
ostream& operator<<(ostream& os, const Point& pt)
  { return os << dsv(pt); }

int main() {
  Point p1(1, 2, 3);
  Point p2(4, 5, 6);
  typedef quantity<si::length, Point> Position;
  typedef quantity<si::length, Vector> Displacement;
  Position start( p1 * si::meters);
  Position finish(p2 * si::meters);
  Displacement disp(finish - start);
  cout << "disp=" << disp << endl;
  return EXIT_SUCCESS;
} // main


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net