-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <cmath> | ||
|
||
#include "projection.h" | ||
|
||
|
||
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; | ||
//Lon -> origin shift, as parallel lines | ||
meters.x = _latLon.y * HALF_CIRCUMFERENCE * INV_180; | ||
//Lat -> first do scaling as per mercator then origin shift | ||
meters.y = glm::log( glm::tan(90.0 + _latLon.x) * PI * INV_360) * PI * INV_180; | ||
meters.y = meters.y * HALF_CIRCUMFERENCE * INV_180; | ||
return meters; | ||
} | ||
|
||
glm::vec2 MercProjection::MetersToLatLon(glm::vec2 _meters) { | ||
glm::vec2 latLon; | ||
float invHalfCircum = 1.0/HALF_CIRCUMFERENCE; | ||
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 * INV_180) ) - PI * 0.5); | ||
return latLon; | ||
} | ||
|
||
glm::vec2 MercProjection::PixelsToMeters(glm::vec2 _pix, int _zoom) { | ||
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#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 | ||
#define INV_180 0.005555555 | ||
#define INV_360 0.002777777 | ||
|
||
|
||
#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) {}; | ||
|
||
/*LatLong to ProjectionType-Meter | ||
* Arguments: | ||
* _latlon: glm::vec2 having lat and lon info. x: lon, y: lat | ||
* Return value: | ||
* meter (glm::vec2). | ||
*/ | ||
virtual glm::vec2 LatLonToMeters(glm::vec2 _latLon) = 0; | ||
|
||
/* ProjectionType-Meters to Lat Lon | ||
* Arguments: | ||
* _meter: glm::vec2 having the projection units in meters. x: lon, y:lat | ||
* Return value: | ||
* latlon (glm::vec2) | ||
*/ | ||
virtual glm::vec2 MetersToLatLon(glm::vec2 _meters) = 0; | ||
|
||
/* Screen pixels to Meters | ||
* (TODO: Update description based on usage in the native app, might be moved to viewModule | ||
* Arguments: | ||
* _pix: screen pixels defined as glm::vec2 | ||
* _zoom: zoom level to determine the projection-meters | ||
* Return value: | ||
* projection-meters (glm::vec2) | ||
*/ | ||
virtual glm::vec2 PixelsToMeters(glm::vec2 _pix, int _zoom) = 0; | ||
|
||
/* Meters to Screen Pixels | ||
* (TODO: Update description based on usage in the native app, might be moved to viewModule | ||
* Arguments: | ||
* _meters: projection-meters (glm::vec2) | ||
* _zoom: zoom level to determine screen pixels | ||
* Return Value: | ||
* screen pixels (glm::vec2) | ||
*/ | ||
virtual glm::vec2 MetersToPixel(glm::vec2 _meters, int _zoom) = 0; | ||
|
||
/* TODO: Define when more clear on the use case for this. Might be moved to viewModule | ||
*/ | ||
virtual glm::ivec2 PixelsToTileXY(glm::vec2 _pix) = 0; | ||
|
||
/* Projection-meters to TILEXY | ||
* Arguments: | ||
* _meters: projection-meters (glm::vec2) | ||
* _zoom: zoom level for which tile coordinates need to be determined | ||
* Return Value: | ||
* Tile coordinates (x and y) (glm::ivec2) | ||
*/ | ||
virtual glm::ivec2 MetersToTileXY(glm::vec2 _meters, int _zoom) = 0; | ||
|
||
/* bounds of space in projection-meters | ||
* Arguments: | ||
* _tileCoord: glm::ivec3 (x,y and zoom) | ||
* Return value: | ||
* bounds in projection-meters (glm::vec4) | ||
* x,y : min bounds in projection meters | ||
* z,w : max bounds in projection meters | ||
*/ | ||
virtual glm::vec4 TileBounds(glm::ivec3 _tileCoord) = 0; | ||
|
||
/* bounds of space in lat lon | ||
* Arguments: | ||
* _tileCoord: glm::ivec3 (x,y and zoom) | ||
* Return value: | ||
* bounds in lat lon (glm::vec4) | ||
* x,y: min bounds in lat lon | ||
* z,w: max bounds in lat lon | ||
*/ | ||
virtual glm::vec4 TileLatLonBounds(glm::ivec3 _tileCoord) = 0; | ||
|
||
/* Returns the projection type of a given projection instance | ||
* (example: ProjectionType::Mercator) | ||
*/ | ||
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: | ||
/*Constructor for MercProjection | ||
* _type: type of map projection, example ProjectionType::Mercator | ||
* _tileSize: size of the map tile, default is 256 | ||
*/ | ||
MercProjection(ProjectionType _type, int _tileSize=256); | ||
|
||
virtual glm::vec2 LatLonToMeters(glm::vec2 _latLon) override; | ||
virtual glm::vec2 MetersToLatLon(glm::vec2 _meters) override; | ||
virtual glm::vec2 PixelsToMeters(glm::vec2 _pix, int _zoom) override; | ||
virtual glm::vec2 MetersToPixel(glm::vec2 _meters, int _zoom) override; | ||
virtual glm::ivec2 PixelsToTileXY(glm::vec2 _pix) override; | ||
virtual glm::ivec2 MetersToTileXY(glm::vec2 _meters, int _zoom) override; | ||
virtual glm::vec4 TileBounds(glm::ivec3 _tileCoord) override; | ||
virtual glm::vec4 TileLatLonBounds(glm::ivec3 _tileCoord) override; | ||
virtual ~MercProjection() {} | ||
}; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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.