Permalink
Browse files

derive transformations instead of hard coding magic numbers

  • Loading branch information...
1 parent c510bad commit 5e6bb73461b771966ce2a55bca0efdbb349fc7f9 @RandomEtc committed Jul 31, 2011
Showing with 60 additions and 10 deletions.
  1. +4 −1 include/BingMapsProvider.h
  2. +5 −2 include/OpenStreetMapProvider.h
  3. +8 −5 include/TemplatedMapProvider.h
  4. +43 −2 include/Transformation.h
@@ -20,7 +20,10 @@ class BingMapsProvider : public AbstractMapProvider {
BingMapsProvider():
// this is the projection and transform you'll want for any Google-style map tile source:
- AbstractMapProvider(new MercatorProjection(26, Transformation(1.068070779e7, 0.0, 3.355443185e7, 0.0, -1.068070890e7, 3.355443057e7)))
+ AbstractMapProvider(new MercatorProjection( 0,
+ Transformation::deriveTransformation( -M_PI, M_PI, 0, 0,
+ M_PI, M_PI, 1, 0,
+ -M_PI, -M_PI, 0, 1 ) ) )
{
// TODO: a better way to init a constant length vector or array of strings?
subdomains.push_back("t0");
@@ -17,8 +17,11 @@ class OpenStreetMapProvider : public AbstractMapProvider {
std::vector<std::string> subdomains;
OpenStreetMapProvider():
- // this is the projection and transform you'll want for any Google-style map tile source:
- AbstractMapProvider(new MercatorProjection(26, Transformation(1.068070779e7, 0.0, 3.355443185e7, 0.0, -1.068070890e7, 3.355443057e7)))
+ // this is the projection and transform you'll want for any Google-style map tile source:
+ AbstractMapProvider(new MercatorProjection( 0,
+ Transformation::deriveTransformation( -M_PI, M_PI, 0, 0,
+ M_PI, M_PI, 1, 0,
+ -M_PI, -M_PI, 0, 1 ) ) )
{
// TODO: is there a better way to initialize a constant size vector or array of strings?
subdomains.push_back("");
@@ -16,11 +16,14 @@ class TemplatedMapProvider : public AbstractMapProvider {
std::string urlTemplate;
- TemplatedMapProvider(std::string _urlTemplate): urlTemplate(_urlTemplate),
- // this is the projection and transform you'll want for any Google-style map tile source:
- AbstractMapProvider(new MercatorProjection(26, Transformation(1.068070779e7, 0.0, 3.355443185e7, 0.0, -1.068070890e7, 3.355443057e7)))
- {
- }
+ TemplatedMapProvider(std::string _urlTemplate):
+ urlTemplate(_urlTemplate),
+ // this is the projection and transform you'll want for any Google-style map tile source:
+ AbstractMapProvider(new MercatorProjection( 0,
+ Transformation::deriveTransformation( -M_PI, M_PI, 0, 0,
+ M_PI, M_PI, 1, 0,
+ -M_PI, -M_PI, 0, 1 ) ) )
+ { }
int tileWidth() {
return 256;
@@ -12,8 +12,9 @@ class Transformation {
Transformation() : ax(1), bx(0), cx(0), ay(0), by(1), cy(0) {}
- Transformation(float _ax, float _bx, float _cx, float _ay, float _by, float _cy) : ax(_ax), bx(_bx), cx(_cx), ay(_ay), by(_by), cy(_cy) {}
-
+ Transformation(const double &_ax, const double &_bx, const double &_cx,
+ const double &_ay, const double &_by, const double &_cy): ax(_ax), bx(_bx), cx(_cx), ay(_ay), by(_by), cy(_cy) {}
+
Transformation(const Transformation &t) : ax(t.ax), bx(t.bx), cx(t.cx), ay(t.ay), by(t.by), cy(t.cy) {}
Vec2d transform(const Vec2d &point) {
@@ -24,6 +25,46 @@ class Transformation {
return Vec2d((point.x*by - point.y*bx - cx*by + cy*bx) / (ax*by - ay*bx), (point.x*ay - point.y*ax - cx*ay + cy*ax) / (bx*ay - by*ax));
}
+ // Generates a transform based on three pairs of points,
+ // a1 -> a2, b1 -> b2, c1 -> c2.
+ static Transformation deriveTransformation( const double &a1x, const double &a1y,
+ const double &a2x, const double &a2y,
+ const double &b1x, const double &b1y,
+ const double &b2x, const double &b2y,
+ const double &c1x, const double &c1y,
+ const double &c2x, const double &c2y ) {
+ const Vec3f d = linearSolution( a1x, a1y, a2x,
+ b1x, b1y, b2x,
+ c1x, c1y, c2x );
+ const Vec3f e = linearSolution( a1x, a1y, a2y,
+ b1x, b1y, b2y,
+ c1x, c1y, c2y );
+ return Transformation(d.x, d.y, d.z, e.x, e.y, e.z);
+ };
+
+ // Solves a system of linear equations.
+ //
+ // t1 = (a * r1) + (b + s1) + c
+ // t2 = (a * r2) + (b + s2) + c
+ // t3 = (a * r3) + (b + s3) + c
+ //
+ // r1 - t3 are the known values.
+ // a, b, c are the unknowns to be solved.
+ // returns the a, b, c coefficients.
+ static Vec3d linearSolution( const double &r1, const double &s1, const double &t1,
+ const double &r2, const double &s2, const double &t2,
+ const double &r3, const double &s3, const double &t3 ) {
+ const double a = (((t2 - t3) * (s1 - s2)) - ((t1 - t2) * (s2 - s3)))
+ / (((r2 - r3) * (s1 - s2)) - ((r1 - r2) * (s2 - s3)));
+
+ const double b = (((t2 - t3) * (r1 - r2)) - ((t1 - t2) * (r2 - r3)))
+ / (((s2 - s3) * (r1 - r2)) - ((s1 - s2) * (r2 - r3)));
+
+ const double c = t1 - (r1 * a) - (s1 * b);
+
+ return Vec3d(a,b,c);
+ };
+
};
} } // namespace

0 comments on commit 5e6bb73

Please sign in to comment.