Factor Int64ToPoint, PointToInt64 and their helper functions into a separate header,

This commit is contained in:
vng 2011-01-30 18:48:10 +02:00 committed by Alex Zolotarev
parent ede8645c54
commit 0c8521ffc3
3 changed files with 57 additions and 32 deletions

View file

@ -1,26 +1,9 @@
#pragma once
#include "mercator.hpp"
#include "point_to_int64.hpp"
#include "../geometry/cellid.hpp"
#include "../geometry/rect2d.hpp"
#include "../base/base.hpp"
#include "../base/assert.hpp"
#include "../std/utility.hpp"
#include "../std/string.hpp"
typedef double CoordT;
typedef pair<CoordT, CoordT> CoordPointT;
typedef m2::CellId<19> RectId;
int64_t PointToInt64(CoordT x, CoordT y);
inline int64_t PointToInt64(CoordPointT const & pt) { return PointToInt64(pt.first, pt.second); }
CoordPointT Int64ToPoint(int64_t v);
pair<int64_t, int64_t> RectToInt64(m2::RectD const & r);
m2::RectD Int64ToRect(pair<int64_t, int64_t> const & p);
template <int MinX, int MinY, int MaxX, int MaxY>
struct Bounds
@ -40,7 +23,6 @@ template <typename BoundsT, typename CellIdT>
class CellIdConverter
{
public:
static CoordT XToCellIdX(CoordT x)
{
return (x - BoundsT::minX) / StepX();
@ -50,6 +32,15 @@ public:
return (y - BoundsT::minY) / StepY();
}
static CoordT CellIdXToX(CoordT x)
{
return (x*StepX() + BoundsT::minX);
}
static CoordT CellIdYToY(CoordT y)
{
return (y*StepY() + BoundsT::minY);
}
static CellIdT ToCellId(CoordT x, CoordT y)
{
uint32_t const ix = static_cast<uint32_t>(XToCellIdX(x));
@ -90,7 +81,7 @@ public:
static CoordPointT FromCellId(CellIdT id)
{
pair<uint32_t, uint32_t> const xy = id.XY();
return CoordPointT(xy.first * StepX() + BoundsT::minX, xy.second * StepY() + BoundsT::minY);
return CoordPointT(CellIdXToX(xy.first), CellIdYToY(xy.second));
}
static void GetCellBounds(CellIdT id, CoordT & minX, CoordT & minY, CoordT & maxX, CoordT & maxY)

View file

@ -11,36 +11,48 @@
#define POINT_COORD_BITS 30
int64_t PointToInt64(CoordT x, CoordT y)
m2::PointU PointD2PointU(CoordT x, CoordT y)
{
if (x < MercatorBounds::minX) x = MercatorBounds::minX;
if (y < MercatorBounds::minY) y = MercatorBounds::minY;
if (x > MercatorBounds::maxX) x = MercatorBounds::maxX;
if (y > MercatorBounds::maxY) y = MercatorBounds::maxY;
uint32_t const ix = static_cast<uint32_t>(0.5 + (x - MercatorBounds::minX)
/ (MercatorBounds::maxX - MercatorBounds::minX) * (1 << POINT_COORD_BITS));
/ (MercatorBounds::maxX - MercatorBounds::minX) * (1 << POINT_COORD_BITS));
uint32_t const iy = static_cast<uint32_t>(0.5 + (y - MercatorBounds::minY)
/ (MercatorBounds::maxY - MercatorBounds::minY) * (1 << POINT_COORD_BITS));
int64_t res = static_cast<int64_t>(m2::PointUToUint64(m2::PointU(ix, iy)));
/ (MercatorBounds::maxY - MercatorBounds::minY) * (1 << POINT_COORD_BITS));
ASSERT_LESS_OR_EQUAL(ix, 1 << POINT_COORD_BITS, ());
ASSERT_LESS_OR_EQUAL(iy, 1 << POINT_COORD_BITS, ());
return m2::PointU(ix, iy);
}
int64_t PointToInt64(CoordT x, CoordT y)
{
int64_t const res = static_cast<int64_t>(m2::PointUToUint64(PointD2PointU(x, y)));
ASSERT_LESS_OR_EQUAL(res, 3ULL << 2 * POINT_COORD_BITS, ());
ASSERT_GREATER_OR_EQUAL(res, 0,
("Highest bits of (ix, iy) are not used, so res should be > 0."));
return res;
}
CoordPointT PointU2PointD(m2::PointU const & pt)
{
return CoordPointT(
static_cast<CoordT>(pt.x) * (MercatorBounds::maxX - MercatorBounds::minX)
/ (1 << POINT_COORD_BITS) + MercatorBounds::minX,
static_cast<CoordT>(pt.y) * (MercatorBounds::maxY - MercatorBounds::minY)
/ (1 << POINT_COORD_BITS) + MercatorBounds::minY);
}
CoordPointT Int64ToPoint(int64_t v)
{
ASSERT_LESS_OR_EQUAL(v, 3ULL << 2 * POINT_COORD_BITS, ());
m2::PointU const pt = m2::Uint64ToPointU(static_cast<uint64_t>(v));
CoordT const fx = static_cast<CoordT>(pt.x);
CoordT const fy = static_cast<CoordT>(pt.y);
return CoordPointT(
fx * (MercatorBounds::maxX - MercatorBounds::minX)
/ (1 << POINT_COORD_BITS) + MercatorBounds::minX,
fy * (MercatorBounds::maxY - MercatorBounds::minY)
/ (1 << POINT_COORD_BITS) + MercatorBounds::minY);
return PointU2PointD(m2::Uint64ToPointU(static_cast<uint64_t>(v)));
}
pair<int64_t, int64_t> RectToInt64(m2::RectD const & r)

View file

@ -0,0 +1,22 @@
#pragma once
#include "../geometry/cellid.hpp"
#include "../geometry/rect2d.hpp"
#include "../std/utility.hpp"
typedef double CoordT;
typedef pair<CoordT, CoordT> CoordPointT;
typedef m2::CellId<19> RectId;
m2::PointU PointD2PointU(CoordT x, CoordT y);
CoordPointT PointU2PointD(m2::PointU const & p);
int64_t PointToInt64(CoordT x, CoordT y);
inline int64_t PointToInt64(CoordPointT const & pt) { return PointToInt64(pt.first, pt.second); }
CoordPointT Int64ToPoint(int64_t v);
pair<int64_t, int64_t> RectToInt64(m2::RectD const & r);
m2::RectD Int64ToRect(pair<int64_t, int64_t> const & p);