Skip to content

Commit

Permalink
New map projection for zoom map
Browse files Browse the repository at this point in the history
  • Loading branch information
DMagic1 committed Feb 10, 2017
1 parent 68fb180 commit a5d77ee
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 22 deletions.
119 changes: 97 additions & 22 deletions SCANsat/SCAN_Map/SCANmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ internal double projectLongitude(double lon, double lat)
{
lon = 1.3 * Math.Cos(lat) * Math.Sin(lon) + Math.PI / 2;
}
return Mathf.Rad2Deg * lon;
case MapProjection.Orthographic:
lon = Mathf.Deg2Rad * lon;
lat = Mathf.Deg2Rad * lat;
double centerLon = Mathf.Deg2Rad * centeredLong;
double centerLat = Mathf.Deg2Rad * centeredLat;

if (Math.Sin(centerLat) * Math.Sin(lat) + Math.Cos(centerLat) * Math.Cos(lat) * Math.Cos(lon - centerLon) < 0)
return -200;

lon = 1.5 * Math.Cos(lat) * Math.Sin(lon - centerLon);

return Mathf.Rad2Deg * lon;
default:
return lon;
Expand All @@ -237,6 +249,18 @@ internal double projectLatitude(double lon, double lat)
{
lat = -1.3 * Math.Cos(lat) * Math.Cos(lon);
}
return Mathf.Rad2Deg * lat;
case MapProjection.Orthographic:
lon = Mathf.Deg2Rad * lon;
lat = Mathf.Deg2Rad * lat;
double centerLon = Mathf.Deg2Rad * centeredLong;
double centerLat = Mathf.Deg2Rad * centeredLat;

if (Math.Sin(centerLat) * Math.Sin(lat) + Math.Cos(centerLat) * Math.Cos(lat) * Math.Cos(lon - centerLon) < 0)
return -200;

lat = 1.5 * (Math.Cos(centerLat) * Math.Sin(lat) - Math.Sin(centerLat) * Math.Cos(lat) * Math.Cos(lon - centerLon));

return Mathf.Rad2Deg * lat;
default:
return lat;
Expand Down Expand Up @@ -286,6 +310,26 @@ internal double unprojectLongitude(double lon, double lat)
if (lon <= -180)
lon = -180;
return lon;
case MapProjection.Orthographic:
lon = Mathf.Deg2Rad * lon;
lat = Mathf.Deg2Rad * lat;
double centerLon = Mathf.Deg2Rad * centeredLong;
double centerLat = Mathf.Deg2Rad * centeredLat;

double p2 = Math.Sqrt(lon * lon + lat * lat);
double c2 = Math.Asin(p2/1.5);

if (Math.Cos(c2) < 0)
return 300;

lon = centerLon + Math.Atan2(lon * Math.Sin(c2), p2 * Math.Cos(c2) * Math.Cos(centerLat) - lat * Math.Sin(c2) * Math.Sin(centerLat));

lon = (Mathf.Rad2Deg * lon + 180) % 360 - 180;

if (lon <= -180)
lon += 360;

return lon;
default:
return lon;
}
Expand Down Expand Up @@ -325,6 +369,20 @@ internal double unprojectLatitude(double lon, double lat)
double p = Math.Sqrt(lon * lon + lat * lat);
double c = Math.Asin(p);
lat = Math.Asin(Math.Cos(c) * Math.Sin(lat0) + (lat * Math.Sin(c) * Math.Cos(lat0)) / (p));
return Mathf.Rad2Deg * lat;
case MapProjection.Orthographic:
lon = Mathf.Deg2Rad * lon;
lat = Mathf.Deg2Rad * lat;
double centerLat = Mathf.Deg2Rad * centeredLat;

double p2 = Math.Sqrt(lon * lon + lat * lat);
double c2 = Math.Asin(p2/1.5);

if (Math.Cos(c2) < 0)
return 300;

lat = Math.Asin(Math.Cos(c2) * Math.Sin(centerLat) + (lat * Math.Sin(c2) * Math.Cos(centerLat)) / p2);

return Mathf.Rad2Deg * lat;
default:
return lat;
Expand All @@ -347,6 +405,11 @@ internal double unprojectLatitude(double lon, double lat)
private int startLine;
private int stopLine;

internal void setSize(Vector2 size)
{
setSize((int)size.x, (int)size.y);
}

internal void setSize(int w, int h, int interpolation = 2, int start = 0, int stop = 0)
{
if (w == 0)
Expand Down Expand Up @@ -410,7 +473,10 @@ internal void setWidth(int w)

internal void centerAround(double lon, double lat)
{
if (projection == MapProjection.Polar)
centeredLong = lon;
centeredLat = lat;

if (projection == MapProjection.Orthographic)
{
double lo = projectLongitude(lon, lat);
double la = projectLatitude(lon, lat);
Expand All @@ -422,8 +488,6 @@ internal void centerAround(double lon, double lat)
lon_offset = 180 + lon - (mapwidth / mapscale) / 2;
lat_offset = 90 + lat - (mapheight / mapscale) / 2;
}
centeredLong = lon;
centeredLat = lat;
}

internal double scaleLatitude(double lat)
Expand Down Expand Up @@ -622,7 +686,7 @@ internal void exportPNG()
#region Big Map Texture Generator

/* MAP: build: map to Texture2D */
internal Texture2D getPartialMap()
internal Texture2D getPartialMap(bool apply = true)
{
if (data == null)
return new Texture2D(1, 1);
Expand Down Expand Up @@ -870,25 +934,15 @@ internal Texture2D getPartialMap()
switch (projection)
{
case MapProjection.Rectangular:
{
abundance = getResoureCache(lo, la);
break;
}
case MapProjection.KavrayskiyVII:
{
case MapProjection.Polar:
abundance = getResoureCache(lon, lat);
break;
}
case MapProjection.Polar:
{
if (mSource == mapSource.ZoomMap)
abundance = resourceCache[Mathf.RoundToInt(i * (resourceMapWidth / mapwidth)), Mathf.RoundToInt(mapstep * (resourceMapWidth / mapwidth))];
else
abundance = getResoureCache(lon, lat);
case MapProjection.Orthographic:
abundance = resourceCache[Mathf.RoundToInt(i * (resourceMapWidth / mapwidth)), Mathf.RoundToInt(mapstep * (resourceMapWidth / mapwidth))];
break;
}
}
pix[i] = SCANuiUtil.resourceToColor(baseColor, resource, abundance, data, lon, lat);
pix[i] = SCANuiUtil.resourceToColor32(baseColor, resource, abundance, data, lon, lat);
}
else
pix[i] = baseColor;
Expand All @@ -898,13 +952,14 @@ internal Texture2D getPartialMap()
map.SetPixels32(0, mapstep, map.width, 1, pix);

mapstep++;

if (mapstep % 10 == 0 || mapstep >= map.height)
{
if (mapstep < map.height - 1)
map.SetPixels32(0, mapstep, map.width, 1, palette.redline);

map.Apply();
if (apply || mapstep >= map.height)
map.Apply();
}

return map;
Expand All @@ -921,7 +976,17 @@ private float terrainElevation(double Lon, double Lat, int w, int h, float[,] he
{
double lon = fixUnscale(unScaleLongitude(Lon), w);
double lat = fixUnscale(unScaleLatitude(Lat), h);
elevation = heightMap[Mathf.RoundToInt((float)lon), Mathf.RoundToInt((float)lat)];

int ilon = Mathf.RoundToInt((float)lon);
int ilat = Mathf.RoundToInt((float)lat);

if (ilon >= w)
ilon = w - 1;

if (ilat >= h)
ilat = h - 1;

elevation = heightMap[ilon, ilat];
if (elevation == 0f && !exporting)
elevation = (float)SCANUtil.getElevation(body, Lon, Lat);
}
Expand All @@ -934,7 +999,17 @@ private float terrainElevation(double Lon, double Lat, int w, int h, float[,] he
{
double lon = fixUnscale(unScaleLongitude(Lon), w);
double lat = fixUnscale(unScaleLatitude(Lat), h);
elevation = heightMap[((int)(lon * 5)) / 5, ((int)(lat * 5)) / 5];

int ilon = ((int)(lon * 5)) / 5;
int ilat = ((int)(lat * 5)) / 5;

if (ilon >= w)
ilon = w - 1;

if (ilat >= h)
ilat = h - 1;

elevation = heightMap[ilon, ilat];
if (elevation == 0f && !exporting)
elevation = (float)SCANUtil.getElevation(body, ((int)(Lon * 5)) / 5, ((int)(Lat * 5)) / 5);
}
Expand Down
1 change: 1 addition & 0 deletions SCANsat/SCAN_Map/SCANmapProjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ public enum MapProjection
Rectangular = 0,
KavrayskiyVII = 1,
Polar = 2,
Orthographic = 3,
}
}

0 comments on commit a5d77ee

Please sign in to comment.