Skip to content

Commit

Permalink
Added --ref_lon --ref_lat and --ref_orient arguments
Browse files Browse the repository at this point in the history
These arguments allow for easy orientation of the cubed-sphere grid
  • Loading branch information
paullric committed Feb 8, 2022
1 parent e7154fb commit e60e46d
Show file tree
Hide file tree
Showing 11 changed files with 1,001 additions and 389 deletions.
58 changes: 44 additions & 14 deletions src/CSRefinementMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Exception.h"
#include "MathHelper.h"
#include "CubedSphereTrans.h"
#include "CoordTransforms.h"

#include "lodepng.h"

Expand Down Expand Up @@ -80,7 +81,10 @@ void CSRefinementMap::InitializeFromPNG(
double dLonBase,
double dLatBase,
bool fInvert,
bool fBlockRefine
bool fBlockRefine,
double dReferenceLonDeg,
double dReferenceLatDeg,
double dReferenceOrientDeg
) {
std::vector<unsigned char> image;
unsigned int nWidth;
Expand All @@ -98,12 +102,15 @@ void CSRefinementMap::InitializeFromPNG(
printf("PNG loaded successfully\n");
printf("... Dimensions: %i x %i\n", nWidth, nHeight);

// Convert longitude shift to radians
// Convert arguments to radians
dLonBase *= M_PI / 180.0;

// Convert latitude shift to radians
dLatBase *= M_PI / 180.0;

double dReferenceLonRad = dReferenceLonDeg * M_PI / 180.0;
double dReferenceLatRad = dReferenceLatDeg * M_PI / 180.0;
double dReferenceOrientRad = dReferenceOrientDeg * M_PI / 180.0;

// Calculate image greyscale levels
printf("... Levels: ");
int iLastLevel = (-1);
for (int iC = 0; iC < 255; iC++) {
Expand Down Expand Up @@ -131,21 +138,44 @@ void CSRefinementMap::InitializeFromPNG(
double dB = -0.25 * M_PI + 0.5 * M_PI
* ((static_cast<double>(iB + iT)) / m_nMap.GetColumns());

double dLon;
double dLat;
double dLonRad;
double dLatRad;

CubedSphereTrans::RLLFromABP(dA, dB, iP, dLonRad, dLatRad);

double dX0;
double dY0;
double dZ0;

RLLtoXYZ_Rad(dLonRad, dLatRad, dX0, dY0, dZ0);

// Rotate around X axis by +dReferenceOrientRad
double dX1 = dX0;
double dY1 = + cos(dReferenceOrientRad) * dY0 - sin(dReferenceOrientRad) * dZ0;
double dZ1 = + sin(dReferenceOrientRad) * dY0 + cos(dReferenceOrientRad) * dZ0;

// Rotate around Y axis by -dReferenceLatRad
double dX2 = + cos(dReferenceLatRad) * dX1 - sin(dReferenceLatRad) * dZ1;
double dY2 = dY1;
double dZ2 = + sin(dReferenceLatRad) * dX1 + cos(dReferenceLatRad) * dZ1;

// Rotate around Z axis by +dReferenceLonRad
double dX3 = + cos(dReferenceLonRad) * dX2 - sin(dReferenceLonRad) * dY2;
double dY3 = + sin(dReferenceLonRad) * dX2 + cos(dReferenceLonRad) * dY2;
double dZ3 = dZ2;

CubedSphereTrans::RLLFromABP(dA, dB, iP, dLon, dLat);
XYZtoRLL_Rad(dX3, dY3, dZ3, dLonRad, dLatRad);

dLat += dLatBase;
dLon += dLonBase;
dLon = dLon - 2.0 * M_PI * floor(dLon / (2.0 * M_PI));
if ((dLon < 0.0) || (dLon > 2.0 * M_PI)) {
// Sample image
dLatRad += dLatBase;
dLonRad += dLonBase;
dLonRad = dLonRad - 2.0 * M_PI * floor(dLonRad / (2.0 * M_PI));
if ((dLonRad < 0.0) || (dLonRad > 2.0 * M_PI)) {
_EXCEPTIONT("Invalid longitude");
}

int iLon = floor((dLon / (2.0 * M_PI)) * nWidth);
int iLat = floor((dLat + 0.5 * M_PI) / M_PI * nHeight);
int iLon = floor((dLonRad / (2.0 * M_PI)) * nWidth);
int iLat = floor((dLatRad + 0.5 * M_PI) / M_PI * nHeight);

if (iLon < 0) {
iLon = 0;
Expand Down
5 changes: 4 additions & 1 deletion src/CSRefinementMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ class CSRefinementMap {
double dLonBase,
double dLatBase,
bool fInvert,
bool fBlockRefine
bool fBlockRefine,
double dReferenceLonDeg,
double dReferenceLatDeg,
double dReferenceOrientDeg
);

public:
Expand Down
Loading

0 comments on commit e60e46d

Please sign in to comment.