RCube
Rcube Rest Server calculates sail routes based on Grib files and sailing boat polar files
Loading...
Searching...
No Matches
engine.c File Reference
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>
#include "r3types.h"
#include "glibwrapper.h"
#include "inline.h"
#include "r3util.h"
#include "grib.h"
Include dependency graph for engine.c:

Go to the source code of this file.

Data Structures

struct  Sector
 

Macros

#define MAX_N_INTERVAL   1000
 \Engine tws = True Wind Speed twd = True Wind Direction twa = True Wind Angle - the angle of the boat to the wind sog = Speed over Ground of the boat cog = Course over Ground of the boat
 
#define LIMIT   1
 
#define MIN_VMC_RATIO   0.5
 
#define MAX_N_HISTORY   20
 
#define MAX_UNREACHABLE   0
 
#define MIN_DT   0.1
 

Functions

static double distSegmentWithFoot (double latX, double lonX, double latA, double lonA, double latB, double lonB, double *latH, double *lonH)
 return distance (nm) from point X do segment [AB].
 
static int findFirst (int nIsoc)
 find first point in isochrone.
 
static void initSector (int nIsoc, int nMax)
 initialization of sector
 
static int forwardSectorOptimize (const Pp *pOr, const Pp *pDest, int nIsoc, const Pp *isoList, int isoLen, Pp *optIsoc)
 reduce the size of Isolist note influence of parameters par.nSector, par.jFactor and par.kFactor make new isochrone optIsoc return the length of this isochrone side effect: update isoDesc
 
static int optimize (const Pp *pOr, const Pp *pDest, int nIsoc, int algo, const Pp *isoList, int isoLen, Pp *optIsoc)
 choice of algorithm used to reduce the size of Isolist
 
static int buildNextIsochrone (const Pp *pOr, const Pp *pDest, const Pp *isoList, int isoLen, double t, double dt, Pp *newList, double *bestVmc, double *biggestOrthoVmc)
 build the new list describing the next isochrone, starting from isoList returns length of the newlist built or -1 if error
 
static int findFather (int ptId, int i, int lIsoc)
 find father of point in previous isochrone
 
bool isoDescToStr (char *str, size_t maxLen)
 copy isoc Descriptors in a string true if enough space, false if truncated
 
bool dumpIsocToFile (const char *fileName)
 write in CSV file Isochrones
 
void saveRoute (SailRoute *route)
 store current route in history
 
void freeHistoryRoute ()
 free space for history route
 
static double calcDuration (SailRoute *route)
 return the total duration of the route, taking into acount last steps duration
 
static void statRoute (SailRoute *route)
 add additionnal information to the route
 
bool storeRoute (SailRoute *route, const Pp *pOr, const Pp *pDest)
 store route response false if error
 
static char * motorTribordBabord (bool motor, int amure, char *str, size_t maxLen)
 produce string that says if Motor, Tribord, Babord
 
bool routeToStr (const SailRoute *route, char *str, size_t maxLen, char *footer, size_t maxLenFooter)
 copy route in a string true if enough space, false if truncated
 
static bool simpleGoalP (const Pp *pA, const Pp *pDest, double t, double dt, double *timeTo, double *distance, bool *motor, int *amure, int *sail)
 return true if pDest can be reached from pA - in less time than dt timeTo give the time to get to pDest motor true if goal reached with motor current not considered for this last step penalty not considered because may bug
 
static bool simpleGoal (Pp *pDest, Pp *isoList, int len, double t, double dt, double *lastStepDuration, bool *motor, int *amure)
 true if goal can be reached directly in dt from isochrone update isoDesc side effect : pDest.father can be modified !
 
static bool goalP (const Pp *pA, const Pp *pB, const Pp *pDest, double t, double dt, double *timeTo, double *distance, bool *motor, int *amure, int *sail, bool *bestFirst)
 return closest index point to pDest in Isoc, and this point
 
static bool goal (Pp *pDest, Pp *isoList, int len, double t, double dt, double *lastStepDuration, bool *motor, int *amure)
 true if goal can be reached directly in dt from isochrone update isoDesc side effect : pDest.father can be modified !
 
static int fClosest (const Pp *isoc, int n, const Pp *pDest, Pp *closest)
 return closest index point to pDest in Isoc, and this point
 
static void replicate (int n)
 when no wind, build next isochrone as a replica of previous isochrone Manage carefully id and father fields
 
static int routing (Pp *pOr, Pp *pDest, int toIndexWp, double t, double dt, double *lastStepDuration)
 find optimal routing from p0 to pDest using grib file and polar return number of steps to reach pDest, NIL if unreached, -1 if problem, -2 if stopped by user, 0 reserved for not terminated return also lastStepDuration if destination reached (0 if unreached) side effects: nIsoc, maxNIsoc, pOrToPDestCog, isoDesc, isocArray, route pOr and pDest modified
 
static void initRouting (void)
 global variable initialization for routing
 
static void checkArrival (SailRoute *route, Pp *pDest)
 check arrival between pDest and segments [pPrev-pCurr] and [pCurr-pNext] pFoot0 is the projection of pDest on [pPrev-pCurr] pFoot1 is the projection of pDest on [pCurr-pNext] produces in Json distance informations: dSeg0: pPrev-pCurr dSeg1: pCurr-pNext dPrev: pDest-pPrev dNext: pDest-pNext dCurr: pDest-pCurr dFoot0: pDest-pFoot0 dFoot1: pDest-pFoot1 When destination is reached, pDest = par.pDest When destination is unreached, pDest is the arrival point on last isochrone and pDest = pCurr = pFoot0 = pFoot1
 
void * routingLaunch ()
 launch routing with parameters
 
void * bestTimeDeparture ()
 choose best time to reach pDest in minimum time
 
void * allCompetitors ()
 launch all competitors
 
void logReport (int n)
 log one CSV line report.
 
bool exportRouteToGpx (const SailRoute *route, const char *fileName)
 export route with GPX format
 

Variables

PpisocArray = NULL
 global variables
 
IsoDescisoDesc = NULL
 
int maxNIsoc = 0
 
int nIsoc = 0
 
Pp lastClosest
 
SailRoute route
 store sail route calculated in engine.c by routing
 
HistoryRouteList historyRoute = { .n = 0, .r = NULL }
 
ChooseDeparture chooseDeparture
 
static double pOrToPDestCog = 0.0
 static global variable.
 
static int pId = 1
 
static double tDeltaCurrent = 0.0
 
Sector sector [2][MAX_N_SECTORS]
 

Macro Definition Documentation

◆ LIMIT

#define LIMIT   1

Definition at line 25 of file engine.c.

◆ MAX_N_HISTORY

#define MAX_N_HISTORY   20

Definition at line 27 of file engine.c.

◆ MAX_N_INTERVAL

#define MAX_N_INTERVAL   1000

\Engine tws = True Wind Speed twd = True Wind Direction twa = True Wind Angle - the angle of the boat to the wind sog = Speed over Ground of the boat cog = Course over Ground of the boat

compilation: gcc -c engine.c pkg-config --cflags glib-2.0

Definition at line 24 of file engine.c.

◆ MAX_UNREACHABLE

#define MAX_UNREACHABLE   0

Definition at line 28 of file engine.c.

◆ MIN_DT

#define MIN_DT   0.1

Definition at line 29 of file engine.c.

◆ MIN_VMC_RATIO

#define MIN_VMC_RATIO   0.5

Definition at line 26 of file engine.c.

Function Documentation

◆ allCompetitors()

◆ bestTimeDeparture()

◆ buildNextIsochrone()

static int buildNextIsochrone ( const Pp pOr,
const Pp pDest,
const Pp isoList,
int  isoLen,
double  t,
double  dt,
Pp newList,
double *  bestVmc,
double *  biggestOrthoVmc 
)
static

◆ calcDuration()

static double calcDuration ( SailRoute route)
static

return the total duration of the route, taking into acount last steps duration

Definition at line 412 of file engine.c.

References SailRoute::lastStepDuration, SailRoute::lastStepWpDuration, SailRoute::n, SailRoute::nWayPoints, par, route, and Par::tStep.

Referenced by statRoute().

Here is the caller graph for this function:

◆ checkArrival()

static void checkArrival ( SailRoute route,
Pp pDest 
)
static

check arrival between pDest and segments [pPrev-pCurr] and [pCurr-pNext] pFoot0 is the projection of pDest on [pPrev-pCurr] pFoot1 is the projection of pDest on [pCurr-pNext] produces in Json distance informations: dSeg0: pPrev-pCurr dSeg1: pCurr-pNext dPrev: pDest-pPrev dNext: pDest-pNext dCurr: pDest-pCurr dFoot0: pDest-pFoot0 dFoot1: pDest-pFoot1 When destination is reached, pDest = par.pDest When destination is unreached, pDest is the arrival point on last isochrone and pDest = pCurr = pFoot0 = pFoot1

Definition at line 1142 of file engine.c.

References SailRoute::destinationReached, distSegmentWithFoot(), Pp::father, findFather(), Pp::id, isocArray, isoDesc, SailRoute::lastPointInfo, Pp::lat, Pp::lon, MAX_SIZE_ISOC, SailRoute::nIsoc, orthoDist(), route, and IsoDesc::size.

Referenced by routingLaunch().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ distSegmentWithFoot()

static double distSegmentWithFoot ( double  latX,
double  lonX,
double  latA,
double  lonA,
double  latB,
double  lonB,
double *  latH,
double *  lonH 
)
inlinestatic

return distance (nm) from point X do segment [AB].

Meaning to foot H, orthogonal projection of X on [AB]

Definition at line 58 of file engine.c.

References CLAMP, and DEG_TO_RAD.

Referenced by checkArrival(), and goalP().

Here is the caller graph for this function:

◆ dumpIsocToFile()

bool dumpIsocToFile ( const char *  fileName)

write in CSV file Isochrones

Definition at line 356 of file engine.c.

References Pp::amure, Pp::dd, Pp::father, Pp::id, isocArray, isoDesc, Pp::lat, Pp::lon, MAX_SIZE_ISOC, Pp::motor, nIsoc, Pp::sail, IsoDesc::size, Pp::toIndexWp, and Pp::vmc.

◆ exportRouteToGpx()

bool exportRouteToGpx ( const SailRoute route,
const char *  fileName 
)

export route with GPX format

Definition at line 1420 of file engine.c.

References SailRoute::dataDate, SailRoute::dataTime, Pp::lat, SailPoint::lat, Pp::lon, SailPoint::lon, MAX_SIZE_DATE, SailRoute::n, newDate(), SailPoint::oCap, par, Par::pDest, PROG_NAME, route, SailPoint::sog, Par::startTimeInHours, SailRoute::t, and SailPoint::time.

Referenced by launchAction().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fClosest()

static int fClosest ( const Pp isoc,
int  n,
const Pp pDest,
Pp closest 
)
static

return closest index point to pDest in Isoc, and this point

Definition at line 923 of file engine.c.

References Pp::lat, Pp::lon, and orthoDist().

Referenced by routing().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ findFather()

static int findFather ( int  ptId,
int  i,
int  lIsoc 
)
static

find father of point in previous isochrone

Definition at line 317 of file engine.c.

References isocArray, and MAX_SIZE_ISOC.

Referenced by checkArrival(), and storeRoute().

Here is the caller graph for this function:

◆ findFirst()

static int findFirst ( int  nIsoc)
inlinestatic

find first point in isochrone.

Useful for drawAllIsochrones

Definition at line 91 of file engine.c.

References DEG_TO_RAD, isocArray, isoDesc, Pp::lat, Pp::lon, MAX_SIZE_ISOC, nIsoc, and IsoDesc::size.

Referenced by routing().

Here is the caller graph for this function:

◆ forwardSectorOptimize()

static int forwardSectorOptimize ( const Pp pOr,
const Pp pDest,
int  nIsoc,
const Pp isoList,
int  isoLen,
Pp optIsoc 
)
inlinestatic

reduce the size of Isolist note influence of parameters par.nSector, par.jFactor and par.kFactor make new isochrone optIsoc return the length of this isochrone side effect: update isoDesc

Definition at line 123 of file engine.c.

References IsoDesc::bestVmc, IsoDesc::biggestOrthoVmc, Pp::dd, DEG_TO_RAD, IsoDesc::focalLat, IsoDesc::focalLon, initSector(), isoDesc, Par::jFactor, Par::kFactor, Pp::lat, LIMIT, Pp::lon, MIN_VMC_RATIO, nIsoc, Sector::nPt, Par::nSectors, orthoCap(), Sector::orthoVmc, Pp::orthoVmc, par, pOrToPDestCog, sector, Sector::vmc, and Pp::vmc.

Referenced by optimize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ freeHistoryRoute()

void freeHistoryRoute ( )

free space for history route

Definition at line 405 of file engine.c.

References historyRoute, HistoryRouteList::n, and HistoryRouteList::r.

◆ goal()

static bool goal ( Pp pDest,
Pp isoList,
int  len,
double  t,
double  dt,
double *  lastStepDuration,
bool *  motor,
int *  amure 
)
inlinestatic

true if goal can be reached directly in dt from isochrone update isoDesc side effect : pDest.father can be modified !

Definition at line 889 of file engine.c.

References Par::allwaysSea, Pp::amure, Pp::father, goalP(), Pp::id, isSeaTolerant(), Pp::lat, Pp::lon, Pp::motor, par, Pp::sail, and tIsSea.

Here is the call graph for this function:

◆ goalP()

static bool goalP ( const Pp pA,
const Pp pB,
const Pp pDest,
double  t,
double  dt,
double *  timeTo,
double *  distance,
bool *  motor,
int *  amure,
int *  sail,
bool *  bestFirst 
)
inlinestatic

return closest index point to pDest in Isoc, and this point

return true if pDest can be reached from segment [pA pB] - in less time than dt timeTo give the time to get to pDest motor true if goal reached with motor bestFirst true if pA is the best, false if pB is best current not considered for this last step penalty not considered because may bug

Definition at line 811 of file engine.c.

References BABORD, Zone::dataDate, Zone::dataTime, Par::dayEfficiency, DEG_TO_RAD, distSegmentWithFoot(), findPolar(), findWindGrib(), fTwa(), isDay(), Pp::lat, Pp::lon, maxSpeedInPolarAt(), Par::motorSpeed, Par::nightEfficiency, orthoDist(), par, polMat, RAD_TO_DEG, sailPolMat, Par::threshold, TRIBORD, wavePolMat, Par::withWaves, Par::xWind, and zone.

Referenced by goal(), and routing().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initRouting()

static void initRouting ( void  )
static

global variable initialization for routing

Definition at line 1109 of file engine.c.

References currentZone, SailRoute::destinationReached, Pp::father, g_atomic_int_set(), Pp::id, isocArray, isoDesc, lastClosest, maxNIsoc, nIsoc, par, Par::pDest, pId, Par::pOr, pOrToPDestCog, SailRoute::ret, route, ROUTING_RUNNING, sector, tDeltaCurrent, zone, and zoneTimeDiff().

Referenced by routingLaunch().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initSector()

static void initSector ( int  nIsoc,
int  nMax 
)
inlinestatic

initialization of sector

Definition at line 114 of file engine.c.

References nIsoc, and sector.

Referenced by forwardSectorOptimize(), and routing().

Here is the caller graph for this function:

◆ isoDescToStr()

bool isoDescToStr ( char *  str,
size_t  maxLen 
)

copy isoc Descriptors in a string true if enough space, false if truncated

Definition at line 330 of file engine.c.

References Par::dispDms, g_strlcat, g_strlcpy, isoDesc, latToStr(), lonToStr(), MAX_SIZE_LINE, nIsoc, and par.

Here is the call graph for this function:

◆ logReport()

◆ motorTribordBabord()

static char * motorTribordBabord ( bool  motor,
int  amure,
char *  str,
size_t  maxLen 
)
static

produce string that says if Motor, Tribord, Babord

Definition at line 615 of file engine.c.

References g_strlcpy, and TRIBORD.

Referenced by routeToStr().

Here is the caller graph for this function:

◆ optimize()

static int optimize ( const Pp pOr,
const Pp pDest,
int  nIsoc,
int  algo,
const Pp isoList,
int  isoLen,
Pp optIsoc 
)
inlinestatic

choice of algorithm used to reduce the size of Isolist

Definition at line 205 of file engine.c.

References forwardSectorOptimize(), and nIsoc.

Referenced by routing().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ replicate()

static void replicate ( int  n)
static

when no wind, build next isochrone as a replica of previous isochrone Manage carefully id and father fields

Definition at line 943 of file engine.c.

References Pp::id, isocArray, isoDesc, MAX_SIZE_ISOC, and IsoDesc::size.

Referenced by routing().

Here is the caller graph for this function:

◆ routeToStr()

◆ routing()

static int routing ( Pp pOr,
Pp pDest,
int  toIndexWp,
double  t,
double  dt,
double *  lastStepDuration 
)
static

find optimal routing from p0 to pDest using grib file and polar return number of steps to reach pDest, NIL if unreached, -1 if problem, -2 if stopped by user, 0 reserved for not terminated return also lastStepDuration if destination reached (0 if unreached) side effects: nIsoc, maxNIsoc, pOrToPDestCog, isoDesc, isocArray, route pOr and pDest modified

Definition at line 966 of file engine.c.

References Pp::amure, buildNextIsochrone(), IsoDesc::closest, competitors, Pp::dd, directCap(), Pp::father, fClosest(), findFirst(), IsoDesc::first, IsoDesc::focalLat, IsoDesc::focalLon, g_atomic_int_get(), goalP(), Pp::id, initSector(), isocArray, isoDesc, lastClosest, Pp::lat, Pp::lon, MAX_N_ISOC, MAX_SIZE_ISOC, maxNIsoc, Pp::motor, Competitor::name, NIL, nIsoc, Par::nSectors, Zone::nTimeStamp, Par::opt, optimize(), orthoDist(), par, pOrToPDestCog, replicate(), SailRoute::ret, route, ROUTING_STOPPED, CompetitorsList::runIndex, Pp::sail, simpleGoal(), IsoDesc::size, CompetitorsList::t, SailRoute::t, Zone::timeStamp, Pp::toIndexWp, IsoDesc::toIndexWp, Pp::vmc, and zone.

Referenced by routingLaunch().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ routingLaunch()

◆ saveRoute()

void saveRoute ( SailRoute route)

store current route in history

Definition at line 378 of file engine.c.

References historyRoute, MAX_N_HISTORY, HistoryRouteList::n, SailRoute::nIsoc, HistoryRouteList::r, route, and SailRoute::t.

Referenced by allCompetitors().

Here is the caller graph for this function:

◆ simpleGoal()

static bool simpleGoal ( Pp pDest,
Pp isoList,
int  len,
double  t,
double  dt,
double *  lastStepDuration,
bool *  motor,
int *  amure 
)
inlinestatic

true if goal can be reached directly in dt from isochrone update isoDesc side effect : pDest.father can be modified !

Definition at line 772 of file engine.c.

References Par::allwaysSea, Pp::amure, Pp::father, Pp::id, isSea(), Pp::lat, Pp::lon, Pp::motor, par, Pp::sail, simpleGoalP(), and tIsSea.

Referenced by routing().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ simpleGoalP()

static bool simpleGoalP ( const Pp pA,
const Pp pDest,
double  t,
double  dt,
double *  timeTo,
double *  distance,
bool *  motor,
int *  amure,
int *  sail 
)
inlinestatic

return true if pDest can be reached from pA - in less time than dt timeTo give the time to get to pDest motor true if goal reached with motor current not considered for this last step penalty not considered because may bug

Definition at line 712 of file engine.c.

References BABORD, Zone::dataDate, Zone::dataTime, Par::dayEfficiency, DEG_TO_RAD, findPolar(), findWindGrib(), fTwa(), isDay(), Pp::lat, Pp::lon, maxSpeedInPolarAt(), Par::motorSpeed, Par::nightEfficiency, orthoDist(), par, polMat, RAD_TO_DEG, sailPolMat, Par::threshold, TRIBORD, wavePolMat, Par::withWaves, Par::xWind, and zone.

Referenced by simpleGoal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ statRoute()

◆ storeRoute()

bool storeRoute ( SailRoute route,
const Pp pOr,
const Pp pDest 
)

store route response false if error

Definition at line 561 of file engine.c.

References Pp::amure, SailPoint::amure, Pp::father, SailPoint::father, findFather(), Pp::id, SailPoint::id, isocArray, isoDesc, Pp::lat, SailPoint::lat, Pp::lon, SailPoint::lon, lonCanonize(), MAX_SIZE_ISOC, Pp::motor, SailPoint::motor, SailRoute::n, nIsoc, SailRoute::nIsoc, SailRoute::nWayPoints, route, Pp::sail, SailPoint::sail, IsoDesc::size, SailRoute::t, Pp::toIndexWp, and SailPoint::toIndexWp.

Referenced by routingLaunch().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ chooseDeparture

ChooseDeparture chooseDeparture

Definition at line 42 of file engine.c.

Referenced by bestTimeDeparture().

◆ historyRoute

HistoryRouteList historyRoute = { .n = 0, .r = NULL }

Definition at line 40 of file engine.c.

Referenced by freeHistoryRoute(), and saveRoute().

◆ isocArray

Pp* isocArray = NULL

◆ isoDesc

◆ lastClosest

Pp lastClosest

Definition at line 36 of file engine.c.

Referenced by initRouting(), logReport(), routing(), and routingLaunch().

◆ maxNIsoc

int maxNIsoc = 0

Definition at line 34 of file engine.c.

Referenced by initRouting(), and routing().

◆ nIsoc

◆ pId

int pId = 1
static

Definition at line 46 of file engine.c.

Referenced by buildNextIsochrone(), initRouting(), and routingLaunch().

◆ pOrToPDestCog

double pOrToPDestCog = 0.0
static

static global variable.

If linking issue, remove static

Definition at line 45 of file engine.c.

Referenced by buildNextIsochrone(), forwardSectorOptimize(), initRouting(), and routing().

◆ route

◆ sector

Sector sector[2][MAX_N_SECTORS]

Definition at line 55 of file engine.c.

Referenced by forwardSectorOptimize(), initRouting(), and initSector().

◆ tDeltaCurrent

double tDeltaCurrent = 0.0
static

Definition at line 47 of file engine.c.

Referenced by buildNextIsochrone(), and initRouting().