geographiclib / geographiclib-c

C implementation of geodesic routines
MIT License
30 stars 3 forks source link

unwrap geod_direct #1

Closed mcleantom closed 2 years ago

mcleantom commented 2 years ago

Hello :)

I was just wondering if there was a way to perform the geod_directcalculation so that it unwraps along the meridian and anti-meridian

My test is:

TEST(GeodesicCoordinate, forward_antimeridian_unrolls) {
    GEOS x;
    GeodesicCoordinate start(*x.m_xG, 180, 0, 90*DEG_TO_RAD);
    GeodesicCoordinate to = start.forward(10);
    ASSERT_TRUE(to.m_lon > start.m_lon); // We need the coordinates to unroll around the antimeridian
};

And the implementation of forward just a wrapper around geod_direct:

GeodesicCoordinate GeodesicCoordinate::forward(float NM) {
    double lat, lon, azi;
    geod_direct(&m_GeodGeodesic, m_lat, m_lon, m_azi, NM*NM_TO_M, &lat, &lon, &azi);
    return GeodesicCoordinate(m_GeodGeodesic, lat, lon, azi);
};

Thanks for any help!

cffk commented 2 years ago

I'm not exactly sure what you're asking; an example of input and expect output would help. In the meantime, please check geod_gendirect. Setting flags to GEOD_UNROLL might do what you want.

mcleantom commented 2 years ago

@cffk Sorry I saw that in the documentation but I am very new to C++ (a few days new) and was a bit confused because of the extra parameters like ps12 which I didn't need to keep, but now I realize I can just create that variable, pass the pointer into geod_gendirect and then ignore its value afterwards:

GeodesicCoordinate GeodesicCoordinate::forward(float NM) {
    double lat2, lon2, azi2, s12, m12, M12, M21, S12;
    geod_gendirect(m_GeodGeodesic, m_lat, m_lon, m_azi, GEOD_LONG_UNROLL, NM*NM_TO_M, &lat2, &lon2, &azi2, &s12, &m12, &M12, &M21, &S12);
    return GeodesicCoordinate(m_GeodGeodesic, lon2, lat2, azi2);
};

Sorry for the silly question and thank you for your time!

cffk commented 2 years ago

This is more complicated than necessary; according to the documentation, you can replace the unneeded pointer arguments by 0:

geod_gendirect(m_GeodGeodesic, m_lat, m_lon, m_azi, GEOD_LONG_UNROLL,
    NM*NM_TO_M, &lat2, &lon2, &azi2, 0, 0, 0, 0, 0);
mcleantom commented 2 years ago

@cffk Thanks, that is a lot nicer!

Sorry for the silly questions, again, very new to c++ (and thank you for your time :))

cffk commented 2 years ago

For the record, you are using the C version of GeographicLib. The C++ library is substantially more capable.