-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
An abstract projection class and child mercprojection class #7
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <cmath> | ||
|
||
#include "projection.h" | ||
|
||
|
||
//Constructor for MercProjection | ||
MercProjection::MercProjection(ProjectionType _type, int _tileSize) : MapProjection(_type), m_TileSize(_tileSize) { | ||
float invTileSize = 1.0/m_TileSize; | ||
m_Res = 2.0 * HALF_CIRCUMFERENCE * invTileSize; | ||
} | ||
|
||
glm::vec2 MercProjection::LatLonToMeters(glm::vec2 _latLon) { | ||
glm::vec2 meters; | ||
float inv180 = 1.0/180.0; | ||
float inv360 = 1.0/360.0; | ||
//Lon -> origin shift, as parallel lines | ||
meters.x = _latLon.y * HALF_CIRCUMFERENCE * inv180; | ||
//Lat -> first do scaling as per mercator then origin shift | ||
meters.y = glm::log( glm::tan(90.0 + _latLon.x) * PI * inv360) * PI * inv180; | ||
meters.y = meters.y * HALF_CIRCUMFERENCE * inv180; | ||
return meters; | ||
} | ||
|
||
glm::vec2 MercProjection::MetersToLatLon(glm::vec2 _meters) { | ||
glm::vec2 latLon; | ||
float invHalfCircum = 1.0/HALF_CIRCUMFERENCE; | ||
float inv180 = 1.0/180.0; | ||
float invPI = 1.0/PI; | ||
latLon.y = _meters.x * invHalfCircum * 180.0; | ||
latLon.x = _meters.y * invHalfCircum * 180.0; | ||
latLon.x = 180.0 * invPI * (2.0 * glm::atan( glm::exp(latLon.x * PI * inv180) ) - PI * 0.5); | ||
return latLon; | ||
} | ||
|
||
glm::vec2 MercProjection::PixelsToMeters(glm::vec2 _pix, int _zoom) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not clear on the purpose of the "pixel" functions, the results of any pixel measurements here aren't meaningful without knowing the display size (which only the view module would have, at the moment). Even then, "pixels" are a problematic unit of measurement on mobile devices where native pixel doubling or tripling is common. Measuring tiles in pixels is a common practice on legacy raster tile maps, but I don't know that it has a use for us. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally agree, but there are still cases where you want to refer to & project or un-project a particular point in screenspace. On the WebGL side, I track "CSS pixels" (which are "logical" pixels), and "device pixels" (which are the 2x or 3x ones). We don't need to do that here but we do need a way to transform the position of an individual pixel. |
||
glm::vec2 meters; | ||
// resolution: meters/pixel for a given zoom level | ||
float res = m_Res * pow(2, -_zoom); | ||
meters.x = _pix.x * res - HALF_CIRCUMFERENCE; | ||
meters.y = _pix.y * res - HALF_CIRCUMFERENCE; | ||
return meters; | ||
} | ||
|
||
glm::vec2 MercProjection::MetersToPixel(glm::vec2 _meters, int _zoom) { | ||
glm::vec2 pix; | ||
float res = m_Res * pow(2, -_zoom); | ||
float invRes = 1.0/res; | ||
pix.x = ( _meters.x + HALF_CIRCUMFERENCE ) * invRes; | ||
pix.y = ( _meters.y + HALF_CIRCUMFERENCE ) * invRes; | ||
return pix; | ||
} | ||
|
||
glm::ivec2 MercProjection::PixelsToTileXY(glm::vec2 _pix) { | ||
//returns the tile covering a region of a pixel | ||
glm::ivec2 tileXY; | ||
float invTileSize = 1.0/m_TileSize; | ||
tileXY.x = int(glm::ceil( _pix.x * invTileSize) - 1); | ||
tileXY.y = int(glm::ceil( _pix.y * invTileSize) - 1); | ||
return tileXY; | ||
} | ||
|
||
glm::ivec2 MercProjection::MetersToTileXY(glm::vec2 _meters, int _zoom) { | ||
return PixelsToTileXY( MetersToPixel(_meters, _zoom)); | ||
} | ||
|
||
glm::vec4 MercProjection::TileBounds(glm::ivec3 _tileCoord) { | ||
glm::vec2 boundMin, boundMax; | ||
boundMin = PixelsToMeters( glm::vec2(_tileCoord.x*m_TileSize, _tileCoord.y*m_TileSize), _tileCoord.z); | ||
boundMax = PixelsToMeters( glm::vec2((_tileCoord.x+1)*m_TileSize, (_tileCoord.y+1)*m_TileSize), _tileCoord.z); | ||
return glm::vec4(boundMin.x, boundMin.y, boundMax.x, boundMax.y); | ||
} | ||
|
||
glm::vec4 MercProjection::TileLatLonBounds(glm::ivec3 _tileCoord) { | ||
glm::vec4 bounds = TileBounds(_tileCoord); | ||
glm::vec2 minBound, maxBound; | ||
minBound = MetersToLatLon(glm::vec2(bounds.x, bounds.y)); | ||
maxBound = MetersToLatLon(glm::vec2(bounds.z, bounds.w)); | ||
return glm::vec4(minBound.x, minBound.y, maxBound.x, maxBound.y); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma one | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: should be "once" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh ... :| |
||
|
||
//Define global constants | ||
#define PI 3.1415926535 | ||
#define HALF_CIRCUMFERENCE 20037508.342789244 | ||
|
||
#include "glm/glm.hpp" | ||
|
||
const glm::vec2 ORIGIN(HALF_CIRCUMFERENCE, HALF_CIRCUMFERENCE); | ||
|
||
enum class ProjectionType { | ||
mercator | ||
}; | ||
|
||
class MapProjection { | ||
protected: | ||
/* m_type: type of map projection: example: mercator*/ | ||
ProjectionType m_type; | ||
public: | ||
MapProjection(ProjectionType _type) : m_type(_type) {}; | ||
virtual glm::vec2 LatLonToMeters(glm::vec2 _latLon) = 0; | ||
virtual glm::vec2 MetersToLatLon(glm::vec2 _meters) = 0; | ||
virtual glm::vec2 PixelsToMeters(glm::vec2 _pix, int _zoom) = 0; | ||
virtual glm::vec2 MetersToPixel(glm::vec2 _meters, int _zoom) = 0; | ||
virtual glm::ivec2 PixelsToTileXY(glm::vec2 _pix) = 0; | ||
virtual glm::ivec2 MetersToTileXY(glm::vec2 _meters, int _zoom) = 0; | ||
virtual glm::vec4 TileBounds(glm::ivec3 _tileCoord) = 0; | ||
virtual glm::vec4 TileLatLonBounds(glm::ivec3 _tileCoord) = 0; | ||
virtual ProjectionType GetMapProjectionType() {return m_type;} | ||
virtual ~MapProjection() {} | ||
}; | ||
|
||
class MercProjection : public MapProjection { | ||
/* | ||
* Following define the boundry covered by this mercator projection | ||
*/ | ||
float m_TileSize; | ||
float m_Res; | ||
public: | ||
MercProjection(ProjectionType _type, int _tileSize=256); | ||
virtual glm::vec2 LatLonToMeters(glm::vec2 _latLon); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does C++11 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I completely forgot about override keyword :P. |
||
virtual glm::vec2 MetersToLatLon(glm::vec2 _meters); | ||
virtual glm::vec2 PixelsToMeters(glm::vec2 _pix, int _zoom); | ||
virtual glm::vec2 MetersToPixel(glm::vec2 _meters, int _zoom); | ||
virtual glm::ivec2 PixelsToTileXY(glm::vec2 _pix); | ||
virtual glm::ivec2 MetersToTileXY(glm::vec2 _meters, int _zoom); | ||
virtual glm::vec4 TileBounds(glm::ivec3 _tileCoord); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment describing the returned value would be helpful when the structure is not obvious There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed.. Will do. |
||
virtual glm::vec4 TileLatLonBounds(glm::ivec3 _tileCoord); | ||
virtual ~MercProjection() {} | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be useful to #DEFINE these common ratios
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or at least make them
const
(#define
makes sense though)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, will do it. Me and @blair1618 were discussing on a standard between const vs #define... but not sure which one to pick. I will use #define for the time being.