Skip to content

Commit

Permalink
clean up to static
Browse files Browse the repository at this point in the history
  • Loading branch information
jvde-github committed Feb 4, 2025
1 parent 65700f4 commit 7ace619
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 48 deletions.
5 changes: 3 additions & 2 deletions Library/ADSB.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace Plane
struct Plane::CPR cpr;
bool even;
} CPR_history[3];

int CPR_history_idx = 0;

uint8_t msg[14]; // Raw message
Expand Down Expand Up @@ -250,8 +251,8 @@ namespace Plane
int decodeAC13Field();
double decodeMovement();

int MOD(int a, int b);
int NL(double lat);
static int MOD(int a, int b);
static int NL(double lat);

bool decodeCPR(FLOAT32 ref_lat, FLOAT32 ref_lon, bool is_even, bool &, FLOAT32 &lt, FLOAT32 &ln);
bool decodeCPR_airborne(bool is_even, bool &, FLOAT32 &lt, FLOAT32 &ln);
Expand Down
23 changes: 13 additions & 10 deletions Tracking/DB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ void DB::setup()

if (server_mode)
{
N *= 32;
M *= 32;
Nships *= 32;
Npaths *= 32;

Info() << "DB: internal ship database extended to " << N << " ships and " << M << " path points";
Info() << "DB: internal ship database extended to " << Nships << " ships and " << Npaths << " path points";
}

ships.resize(N);
paths.resize(M);
ships.resize(Nships);
paths.resize(Npaths);

first = N - 1;
first = Nships - 1;
last = 0;
count = 0;

// set up linked list
for (int i = 0; i < N; i++)
for (int i = 0; i < Nships; i++)
{
ships[i].next = i - 1;
ships[i].prev = i + 1;
}
ships[N - 1].prev = -1;
ships[Nships - 1].prev = -1;
}

bool DB::isValidCoord(float lat, float lon)
Expand All @@ -56,6 +56,9 @@ bool DB::isValidCoord(float lat, float lon)
// https://www.movable-type.co.uk/scripts/latlong.html
void DB::getDistanceAndBearing(float lat1, float lon1, float lat2, float lon2, float &distance, int &bearing)
{
const float EarthRadius = 6371.0f; // Earth radius in kilometers
const float NauticalMilePerKm = 0.5399568f; // Conversion factor

// Convert the latitudes and longitudes from degrees to radians
lat1 = deg2rad(lat1);
lon1 = deg2rad(lon1);
Expand Down Expand Up @@ -493,7 +496,7 @@ int DB::findShip(uint32_t mmsi)
int DB::createShip()
{
int ptr = last;
count = MIN(count + 1, N);
count = MIN(count + 1, Nships);
ships[ptr].reset();

return ptr;
Expand Down Expand Up @@ -563,7 +566,7 @@ void DB::addToPath(int ptr)
paths[path_idx].count = ships[ptr].count;

ships[ptr].path_ptr = path_idx;
path_idx = (path_idx + 1) % M;
path_idx = (path_idx + 1) % Npaths;
}

bool DB::updateFields(const JSON::Property &p, const AIS::Message *msg, Ship &v, bool allowApproximate)
Expand Down
15 changes: 6 additions & 9 deletions Tracking/DB.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,16 @@ class DB : public StreamIn<JSON::JSON>, public StreamIn<AIS::GPS>, public Stream
bool use_GPS = true;
uint32_t own_mmsi = 0;

int N = 4096;
int M = 4096 * 16;
int Nships = 4096;
int Npaths = 4096 * 16;

std::vector<Ship> ships;
std::vector<PathPoint> paths;

bool isValidCoord(float lat, float lon);
const float EarthRadius = 6371.0f; // Earth radius in kilometers
const float NauticalMilePerKm = 0.5399568f; // Conversion factor

float deg2rad(float deg) { return deg * PI / 180.0f; }
int rad2deg(float rad) { return (int)(360 + rad * 180 / PI) % 360; }
static float deg2rad(float deg) { return deg * PI / 180.0f; }
static int rad2deg(float rad) { return (int)(360 + rad * 180 / PI) % 360; }

int findShip(uint32_t mmsi);
int createShip();
Expand All @@ -70,9 +68,8 @@ class DB : public StreamIn<JSON::JSON>, public StreamIn<AIS::GPS>, public Stream

bool updateShip(const JSON::JSON&, TAG&, Ship&);
void addToPath(int ptr);
void addValidation(int, TAG&, float, float);

void getDistanceAndBearing(float lat1, float lon1, float lat2, float lon2, float& distance, int& bearing);
static void getDistanceAndBearing(float lat1, float lon1, float lat2, float lon2, float& distance, int& bearing);

void getShipJSON(const Ship& ship, std::string& content, long int now);
std::string getSinglePathJSON(int);
Expand Down Expand Up @@ -122,7 +119,7 @@ class DB : public StreamIn<JSON::JSON>, public StreamIn<AIS::GPS>, public Stream
std::string getGeoJSON();

int getCount() { return count; }
int getMaxCount() { return N; }
int getMaxCount() { return Nships; }

void setServerMode(bool b) { server_mode = b; }
void setMsgSave(bool b) { msg_save = b; }
Expand Down
54 changes: 27 additions & 27 deletions Tracking/PlaneDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class PlaneDB : public StreamIn<Plane::ADSB>
{
private:
std::mutex mtx;

struct LL
Expand All @@ -22,7 +23,31 @@ class PlaneDB : public StreamIn<Plane::ADSB>
int CPR_cache_even_idx = 0;
int CPR_cache_odd_idx = 0;

private:
static float deg2rad(float deg) { return deg * PI / 180.0f; }
static int rad2deg(float rad) { return (int)(360 + rad * 180 / PI) % 360; }

// https://www.movable-type.co.uk/scripts/latlong.html
static void getDistanceAndBearing(float lat1, float lon1, float lat2, float lon2, float &distance, int &bearing)
{
const float EarthRadius = 6371.0f; // Earth radius in kilometers
const float NauticalMilePerKm = 0.5399568f; // Conversion factor

// Convert the latitudes and longitudes from degrees to radians
lat1 = deg2rad(lat1);
lon1 = deg2rad(lon1);
lat2 = deg2rad(lat2);
lon2 = deg2rad(lon2);

// Compute the distance using the haversine formula
float dlat = lat2 - lat1, dlon = lon2 - lon1;
float a = sin(dlat / 2) * sin(dlat / 2) + cos(lat1) * cos(lat2) * sin(dlon / 2) * sin(dlon / 2);
distance = 2 * EarthRadius * NauticalMilePerKm * asin(sqrt(a));

float y = sin(dlon) * cos(lat2);
float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dlon);
bearing = rad2deg(atan2(y, x));
}

int first = -1;
int last = -1;
int count = 0;
Expand Down Expand Up @@ -188,31 +213,6 @@ class PlaneDB : public StreamIn<Plane::ADSB>
return -1;
}

const float EarthRadius = 6371.0f; // Earth radius in kilometers
const float NauticalMilePerKm = 0.5399568f; // Conversion factor

float deg2rad(float deg) { return deg * PI / 180.0f; }
int rad2deg(float rad) { return (int)(360 + rad * 180 / PI) % 360; }

// https://www.movable-type.co.uk/scripts/latlong.html
void getDistanceAndBearing(float lat1, float lon1, float lat2, float lon2, float &distance, int &bearing)
{
// Convert the latitudes and longitudes from degrees to radians
lat1 = deg2rad(lat1);
lon1 = deg2rad(lon1);
lat2 = deg2rad(lat2);
lon2 = deg2rad(lon2);

// Compute the distance using the haversine formula
float dlat = lat2 - lat1, dlon = lon2 - lon1;
float a = sin(dlat / 2) * sin(dlat / 2) + cos(lat1) * cos(lat2) * sin(dlon / 2) * sin(dlon / 2);
distance = 2 * EarthRadius * NauticalMilePerKm * asin(sqrt(a));

float y = sin(dlon) * cos(lat2);
float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dlon);
bearing = rad2deg(atan2(y, x));
}

bool checkInCPRCache(const Plane::CPR &cpr, bool even, FLOAT32 &lat)
{
bool duplicate = false;
Expand All @@ -233,7 +233,7 @@ class PlaneDB : public StreamIn<Plane::ADSB>
(even ? CPR_cache_even[idx] : CPR_cache_odd[idx]) = cpr;
idx = (idx + 1) % CPR_CACHE_SIZE;
}

return duplicate;
}

Expand Down

0 comments on commit 7ace619

Please sign in to comment.