2017-05-25 10:57:06 +00:00
// SPDX-License-Identifier: GPL-2.0
/* planner.c
*
* code that allows us to plan future dives
*
* ( c ) Dirk Hohndel 2013
*/
# include <assert.h>
# include <unistd.h>
# include <ctype.h>
# include <string.h>
# include "dive.h"
# include "deco.h"
# include "divelist.h"
# include "planner.h"
# include "gettext.h"
# include "libdivecomputer/parser.h"
2018-02-24 22:28:13 +00:00
# include "qthelper.h"
2017-05-25 10:57:06 +00:00
# include "version.h"
int diveplan_duration ( struct diveplan * diveplan )
{
struct divedatapoint * dp = diveplan - > dp ;
int duration = 0 ;
while ( dp ) {
if ( dp - > time > duration )
duration = dp - > time ;
dp = dp - > next ;
}
2017-11-28 13:59:36 +00:00
return ( duration + 30 ) / 60 ;
2017-05-25 10:57:06 +00:00
}
2018-01-15 07:43:57 +00:00
/* Add the icd results of one trimix gas change to the dive plan html buffer. Two rows are added to the table, one
* indicating fractions of gas , the other indication partial pressures of gas . This function makes use of the
* icd_data structure that was filled with information by the function isobaric_counterdiffusion ( ) .
* Parameters : 1 ) Pointer to the output buffer position at which writing should start .
* 2 ) The size of the part of the unused output buffer that remains unused .
* 3 ) The data structure containing icd calculation results : icdvalues .
2018-01-28 15:44:54 +00:00
* 4 ) A boolean value indicating whether a table header should be written before the data . Only during 1 st call .
* 5 ) Pointers to gas mixes in the gas change : gas - from and gas - to .
2018-01-15 07:43:57 +00:00
* Returns : The size of the output buffer that has been used after the new results have been added .
*/
2018-01-28 15:44:54 +00:00
int add_icd_entry ( char * icdbuffer , unsigned int maxsize , struct icd_data * icdvalues , bool printheader , int time_seconds , int ambientpressure_mbar , struct gasmix * gas_from , struct gasmix * gas_to )
2018-01-15 07:43:57 +00:00
{
2018-01-28 15:44:54 +00:00
int len = 0 ;
if ( printheader ) { // Create a table description and a table header if no icd data have been written yet.
len = snprintf ( icdbuffer + len , maxsize - len , " <div>%s: " ,
translate ( " gettextFromC " , " Isobaric counterdiffusion information " ) ) ;
len + = snprintf ( icdbuffer + len , maxsize - len , " <table><tr><td align='left'><b>%s</b></td> " ,
translate ( " gettextFromC " , " runtime " ) ) ;
len + = snprintf ( icdbuffer + len , maxsize - len , " <td align='center'><b>%s</b></td> " ,
translate ( " gettextFromC " , " gaschange " ) ) ;
len + = snprintf ( icdbuffer + len , maxsize - len , " <td style='padding-left: 15px;'><b>%s</b></td> " ,
translate ( " gettextFromC " , " ΔHe " ) ) ;
len + = snprintf ( icdbuffer + len , maxsize - len , " <td style='padding-left: 20px;'><b>%s</b></td> " ,
translate ( " gettextFromC " , " ΔN₂ " ) ) ;
len + = snprintf ( icdbuffer + len , maxsize - len , " <td style='padding-left: 10px;'><b>%s</b></td></tr> " ,
translate ( " gettextFromC " , " max ΔN₂ " ) ) ;
} // Add one entry to the icd table:
len + = snprintf ( icdbuffer + len , maxsize - len ,
2018-01-20 10:56:04 +00:00
" <tr><td rowspan='2' style= 'vertical-align:top;'>%3d%s</td> "
" <td rowspan=2 style= 'vertical-align:top;'>%s➙ " ,
( time_seconds + 30 ) / 60 , translate ( " gettextFromC " , " min " ) , gasname ( gas_from ) ) ;
len + = snprintf ( icdbuffer + len , maxsize - len ,
2018-01-24 19:50:35 +00:00
" %s</td><td style='padding-left: 10px;'>%+5.1f%%</td> "
" <td style= 'padding-left: 15px; color:%s;'>%+5.1f%%</td> "
" <td style='padding-left: 15px;'>%+5.1f%%</td></tr> "
2018-01-20 10:56:04 +00:00
" <tr><td style='padding-left: 10px;'>%+5.2f%s</td> "
" <td style='padding-left: 15px; color:%s;'>%+5.2f%s</td> "
" <td style='padding-left: 15px;'>%+5.2f%s</td></tr> " ,
gasname ( gas_to ) , icdvalues - > dHe / 10.0 ,
2018-01-24 19:50:35 +00:00
( ( 5 * icdvalues - > dN2 ) > - icdvalues - > dHe ) ? " red " : " #383838 " , icdvalues - > dN2 / 10.0 , 0.2 * ( - icdvalues - > dHe / 10.0 ) ,
ambientpressure_mbar * icdvalues - > dHe / 1e6 f , translate ( " gettextFromC " , " bar " ) , ( ( 5 * icdvalues - > dN2 ) > - icdvalues - > dHe ) ? " red " : " #383838 " ,
ambientpressure_mbar * icdvalues - > dN2 / 1e6 f , translate ( " gettextFromC " , " bar " ) ,
ambientpressure_mbar * - icdvalues - > dHe / 5e6 f , translate ( " gettextFromC " , " bar " ) ) ;
2018-01-20 10:56:04 +00:00
return len ;
2018-01-15 07:43:57 +00:00
}
2017-05-25 10:57:06 +00:00
void add_plan_to_notes ( struct diveplan * diveplan , struct dive * dive , bool show_disclaimer , int error )
{
const unsigned int sz_buffer = 2000000 ;
const unsigned int sz_temp = 100000 ;
2018-01-15 07:43:57 +00:00
const unsigned int sz_icdbuf = 10000 ;
2017-05-25 10:57:06 +00:00
char * buffer = ( char * ) malloc ( sz_buffer ) ;
char * temp = ( char * ) malloc ( sz_temp ) ;
2018-01-15 07:43:57 +00:00
char * icdbuffer = ( char * ) malloc ( sz_icdbuf ) ;
2017-05-25 10:57:06 +00:00
const char * deco , * segmentsymbol ;
static char buf [ 1000 ] ;
int len , lastdepth = 0 , lasttime = 0 , lastsetpoint = - 1 , newdepth = 0 , lastprintdepth = 0 , lastprintsetpoint = - 1 ;
2018-01-28 15:44:54 +00:00
int icdlen = 0 ;
2017-05-25 10:57:06 +00:00
struct gasmix lastprintgasmix = { { - 1 } , { - 1 } } ;
struct divedatapoint * dp = diveplan - > dp ;
bool plan_verbatim = prefs . verbatim_plan ;
bool plan_display_runtime = prefs . display_runtime ;
bool plan_display_duration = prefs . display_duration ;
bool plan_display_transitions = prefs . display_transitions ;
bool gaschange_after = ! plan_verbatim ;
bool gaschange_before ;
bool lastentered = true ;
2018-01-28 15:44:54 +00:00
bool icdwarning = false , icdtableheader = true ;
2017-05-25 10:57:06 +00:00
struct divedatapoint * nextdp = NULL ;
struct divedatapoint * lastbottomdp = NULL ;
2018-01-15 07:43:57 +00:00
struct icd_data icdvalues ;
2017-05-25 10:57:06 +00:00
if ( decoMode ( ) = = VPMB ) {
deco = translate ( " gettextFromC " , " VPM-B " ) ;
} else {
deco = translate ( " gettextFromC " , " BUHLMANN " ) ;
}
snprintf ( buf , sizeof ( buf ) , translate ( " gettextFromC " , " DISCLAIMER / WARNING: THIS IS A NEW IMPLEMENTATION OF THE %s "
2018-01-11 13:11:45 +00:00
" ALGORITHM AND A DIVE PLANNER IMPLEMENTATION BASED ON THAT WHICH HAS "
" RECEIVED ONLY A LIMITED AMOUNT OF TESTING. WE STRONGLY RECOMMEND NOT TO "
" PLAN DIVES SIMPLY BASED ON THE RESULTS GIVEN HERE. " ) , deco ) ;
2017-05-25 10:57:06 +00:00
disclaimer = buf ;
2018-01-15 07:43:57 +00:00
if ( ! dp )
goto finished ;
2017-05-25 10:57:06 +00:00
if ( error ) {
snprintf ( temp , sz_temp , " %s " ,
2018-01-11 13:11:45 +00:00
translate ( " gettextFromC " , " Decompression calculation aborted due to excessive time " ) ) ;
2017-05-25 10:57:06 +00:00
snprintf ( buffer , sz_buffer , " <span style='color: red;'>%s </span> %s<br> " ,
2018-01-11 13:11:45 +00:00
translate ( " gettextFromC " , " Warning: " ) , temp ) ;
2017-05-25 10:57:06 +00:00
dive - > notes = strdup ( buffer ) ;
2018-01-15 07:43:57 +00:00
goto finished ;
2017-05-25 10:57:06 +00:00
}
len = show_disclaimer ? snprintf ( buffer , sz_buffer , " <div><b>%s</b><br></div> " , disclaimer ) : 0 ;
2017-10-02 09:17:10 +00:00
if ( diveplan - > surface_interval < 0 ) {
len + = snprintf ( buffer + len , sz_buffer - len , " <div><b>%s (%s) %s<br> " ,
2018-01-11 13:11:45 +00:00
translate ( " gettextFromC " , " Subsurface " ) ,
subsurface_canonical_version ( ) ,
translate ( " gettextFromC " , " dive plan</b> (overlapping dives detected) " ) ) ;
dive - > notes = strdup ( buffer ) ;
2018-02-19 20:28:03 +00:00
goto finished ;
2017-10-02 09:17:10 +00:00
} else if ( diveplan - > surface_interval > = 48 * 60 * 60 ) {
2017-12-26 23:09:01 +00:00
const char * current_date = get_current_date ( ) ;
2017-05-25 10:57:06 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " <div><b>%s (%s) %s %s</b><br> " ,
2018-01-11 13:11:45 +00:00
translate ( " gettextFromC " , " Subsurface " ) ,
subsurface_canonical_version ( ) ,
translate ( " gettextFromC " , " dive plan</b> created on " ) ,
current_date ) ;
2017-12-26 23:09:01 +00:00
free ( ( void * ) current_date ) ;
2017-10-02 09:17:10 +00:00
} else {
2017-12-26 23:09:01 +00:00
const char * current_date = get_current_date ( ) ;
2017-10-02 09:17:10 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " <div><b>%s (%s) %s %d:%02d) %s %s<br> " ,
2018-01-11 13:11:45 +00:00
translate ( " gettextFromC " , " Subsurface " ) ,
subsurface_canonical_version ( ) ,
translate ( " gettextFromC " , " dive plan</b> (surface interval " ) ,
FRACTION ( diveplan - > surface_interval / 60 , 60 ) ,
translate ( " gettextFromC " , " created on " ) ,
current_date ) ;
2017-12-26 23:09:01 +00:00
free ( ( void * ) current_date ) ;
2017-05-25 10:57:06 +00:00
}
2017-11-30 22:06:46 +00:00
if ( prefs . display_variations & & decoMode ( ) ! = RECREATIONAL )
2017-12-01 13:45:51 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , translate ( " gettextFromC " , " Runtime: %dmin%s " ) ,
2018-01-11 13:11:45 +00:00
diveplan_duration ( diveplan ) , " VARIATIONS<br></div> " ) ;
2017-09-18 14:10:47 +00:00
else
len + = snprintf ( buffer + len , sz_buffer - len , translate ( " gettextFromC " , " Runtime: %dmin<br></div> " ) ,
2018-01-11 13:11:45 +00:00
diveplan_duration ( diveplan ) ) ;
2017-09-18 14:10:47 +00:00
2017-05-25 10:57:06 +00:00
if ( ! plan_verbatim ) {
len + = snprintf ( buffer + len , sz_buffer - len , " <table><thead><tr><th></th><th>%s</th> " ,
2018-01-11 13:11:45 +00:00
translate ( " gettextFromC " , " depth " ) ) ;
2017-05-25 10:57:06 +00:00
if ( plan_display_duration )
len + = snprintf ( buffer + len , sz_buffer - len , " <th style='padding-left: 10px;'>%s</th> " ,
2018-01-11 13:11:45 +00:00
translate ( " gettextFromC " , " duration " ) ) ;
2017-05-25 10:57:06 +00:00
if ( plan_display_runtime )
len + = snprintf ( buffer + len , sz_buffer - len , " <th style='padding-left: 10px;'>%s</th> " ,
2018-01-11 13:11:45 +00:00
translate ( " gettextFromC " , " runtime " ) ) ;
2017-05-25 10:57:06 +00:00
len + = snprintf ( buffer + len , sz_buffer - len ,
" <th style='padding-left: 10px; float: left;'>%s</th></tr></thead><tbody style='float: left;'> " ,
translate ( " gettextFromC " , " gas " ) ) ;
}
2018-01-15 07:43:57 +00:00
2017-05-25 10:57:06 +00:00
do {
struct gasmix gasmix , newgasmix = { } ;
const char * depth_unit ;
double depthvalue ;
int decimals ;
bool isascent = ( dp - > depth . mm < lastdepth ) ;
nextdp = dp - > next ;
if ( dp - > time = = 0 )
continue ;
gasmix = dive - > cylinder [ dp - > cylinderid ] . gasmix ;
depthvalue = get_depth_units ( dp - > depth . mm , & decimals , & depth_unit ) ;
/* analyze the dive points ahead */
while ( nextdp & & nextdp - > time = = 0 )
nextdp = nextdp - > next ;
if ( nextdp )
newgasmix = dive - > cylinder [ nextdp - > cylinderid ] . gasmix ;
gaschange_after = ( nextdp & & ( gasmix_distance ( & gasmix , & newgasmix ) | | dp - > setpoint ! = nextdp - > setpoint ) ) ;
gaschange_before = ( gasmix_distance ( & lastprintgasmix , & gasmix ) | | lastprintsetpoint ! = dp - > setpoint ) ;
/* do we want to skip this leg as it is devoid of anything useful? */
if ( ! dp - > entered & &
nextdp & &
dp - > depth . mm ! = lastdepth & &
nextdp - > depth . mm ! = dp - > depth . mm & &
! gaschange_before & &
! gaschange_after )
continue ;
2017-12-29 00:50:20 +00:00
if ( ( dp - > time - lasttime < 10 & & lastdepth = = dp - > depth . mm ) & & ! ( gaschange_after & & dp - > next & & dp - > depth . mm ! = dp - > next - > depth . mm ) )
2017-05-25 10:57:06 +00:00
continue ;
/* Store pointer to last entered datapoint for minimum gas calculation */
if ( dp - > entered & & ! nextdp - > entered )
lastbottomdp = dp ;
len = strlen ( buffer ) ;
if ( plan_verbatim ) {
/* When displaying a verbatim plan, we output a waypoint for every gas change.
* Therefore , we do not need to test for difficult cases that mean we need to
* print a segment just so we don ' t miss a gas change . This makes the logic
* to determine whether or not to print a segment much simpler than with the
* non - verbatim plan .
*/
if ( dp - > depth . mm ! = lastprintdepth ) {
if ( plan_display_transitions | | dp - > entered | | ! dp - > next | | ( gaschange_after & & dp - > next & & dp - > depth . mm ! = nextdp - > depth . mm ) ) {
2018-01-11 13:11:45 +00:00
if ( dp - > setpoint ) {
2018-02-20 12:03:11 +00:00
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " %s to %.*f %s in %d:%02d min - runtime %d:%02u on %s (SP = %.1fbar) " ) ,
dp - > depth . mm < lastprintdepth ? translate ( " gettextFromC " , " Ascend " ) : translate ( " gettextFromC " , " Descend " ) ,
2017-05-25 10:57:06 +00:00
decimals , depthvalue , depth_unit ,
FRACTION ( dp - > time - lasttime , 60 ) ,
FRACTION ( dp - > time , 60 ) ,
gasname ( & gasmix ) ,
( double ) dp - > setpoint / 1000.0 ) ;
2018-01-11 13:11:45 +00:00
} else {
2018-02-20 12:03:11 +00:00
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " %s to %.*f %s in %d:%02d min - runtime %d:%02u on %s " ) ,
dp - > depth . mm < lastprintdepth ? translate ( " gettextFromC " , " Ascend " ) : translate ( " gettextFromC " , " Descend " ) ,
2017-05-25 10:57:06 +00:00
decimals , depthvalue , depth_unit ,
FRACTION ( dp - > time - lasttime , 60 ) ,
FRACTION ( dp - > time , 60 ) ,
gasname ( & gasmix ) ) ;
2018-01-11 13:11:45 +00:00
}
2017-05-25 10:57:06 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " %s<br> " , temp ) ;
}
newdepth = dp - > depth . mm ;
lasttime = dp - > time ;
} else {
if ( ( nextdp & & dp - > depth . mm ! = nextdp - > depth . mm ) | | gaschange_after ) {
2018-01-11 13:11:45 +00:00
if ( dp - > setpoint ) {
2017-05-25 10:57:06 +00:00
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " Stay at %.*f %s for %d:%02d min - runtime %d:%02u on %s (SP = %.1fbar) " ) ,
2018-01-11 13:11:45 +00:00
decimals , depthvalue , depth_unit ,
FRACTION ( dp - > time - lasttime , 60 ) ,
FRACTION ( dp - > time , 60 ) ,
gasname ( & gasmix ) ,
( double ) dp - > setpoint / 1000.0 ) ;
} else {
2017-05-25 10:57:06 +00:00
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " Stay at %.*f %s for %d:%02d min - runtime %d:%02u on %s " ) ,
2018-01-11 13:11:45 +00:00
decimals , depthvalue , depth_unit ,
FRACTION ( dp - > time - lasttime , 60 ) ,
FRACTION ( dp - > time , 60 ) ,
gasname ( & gasmix ) ) ;
}
2017-11-18 20:43:18 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " %s<br> " , temp ) ;
2017-05-25 10:57:06 +00:00
newdepth = dp - > depth . mm ;
lasttime = dp - > time ;
}
}
} else {
/* When not displaying the verbatim dive plan, we typically ignore ascents between deco stops,
* unless the display transitions option has been selected . We output a segment if any of the
* following conditions are met .
* 1 ) Display transitions is selected
* 2 ) The segment was manually entered
* 3 ) It is the last segment of the dive
* 4 ) The segment is not an ascent , there was a gas change at the start of the segment and the next segment
* is a change in depth ( typical deco stop )
* 5 ) There is a gas change at the end of the segment and the last segment was entered ( first calculated
* segment if it ends in a gas change )
* 6 ) There is a gaschange after but no ascent . This should only occur when backgas breaks option is selected
* 7 ) It is an ascent ending with a gas change , but is not followed by a stop . As case 5 already matches
* the first calculated ascent if it ends with a gas change , this should only occur if a travel gas is
* used for a calculated ascent , there is a subsequent gas change before the first deco stop , and zero
* time has been allowed for a gas switch .
*/
if ( plan_display_transitions | | dp - > entered | | ! dp - > next | |
( nextdp & & dp - > depth . mm ! = nextdp - > depth . mm ) | |
( ! isascent & & gaschange_before & & nextdp & & dp - > depth . mm ! = nextdp - > depth . mm ) | |
( gaschange_after & & lastentered ) | | ( gaschange_after & & ! isascent ) | |
2017-08-03 11:37:00 +00:00
( isascent & & gaschange_after & & nextdp & & dp - > depth . mm ! = nextdp - > depth . mm ) | |
( lastentered & & ! dp - > entered ) ) {
2017-05-25 10:57:06 +00:00
// Print a symbol to indicate whether segment is an ascent, descent, constant depth (user entered) or deco stop
if ( isascent )
segmentsymbol = " ➚ " ; // up-right arrow for ascent
else if ( dp - > depth . mm > lastdepth )
segmentsymbol = " ➘ " ; // down-right arrow for descent
else if ( dp - > entered )
segmentsymbol = " ➙ " ; // right arrow for entered entered segment at constant depth
else
segmentsymbol = " - " ; // minus sign (a.k.a. horizontal line) for deco stop
len + = snprintf ( buffer + len , sz_buffer - len , " <tr><td style='padding-left: 10px; float: right;'>%s</td> " , segmentsymbol ) ;
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " %3.0f%s " ) , depthvalue , depth_unit ) ;
len + = snprintf ( buffer + len , sz_buffer - len , " <td style='padding-left: 10px; float: right;'>%s</td> " , temp ) ;
if ( plan_display_duration ) {
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " %3dmin " ) , ( dp - > time - lasttime + 30 ) / 60 ) ;
len + = snprintf ( buffer + len , sz_buffer - len , " <td style='padding-left: 10px; float: right;'>%s</td> " , temp ) ;
}
if ( plan_display_runtime ) {
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " %3dmin " ) , ( dp - > time + 30 ) / 60 ) ;
len + = snprintf ( buffer + len , sz_buffer - len , " <td style='padding-left: 10px; float: right;'>%s</td> " , temp ) ;
}
/* Normally a gas change is displayed on the stopping segment, so only display a gas change at the end of
* an ascent segment if it is not followed by a stop
*/
if ( ( isascent | | dp - > entered ) & & gaschange_after & & dp - > next & & nextdp & & ( dp - > depth . mm ! = nextdp - > depth . mm | | nextdp - > entered ) ) {
if ( dp - > setpoint ) {
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " (SP = %.1fbar) " ) , ( double ) nextdp - > setpoint / 1000.0 ) ;
2018-01-11 13:11:45 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " <td style='padding-left: 10px; color: red; float: left;'><b>%s %s</b></td> " ,
gasname ( & newgasmix ) , temp ) ;
2017-05-25 10:57:06 +00:00
} else {
2018-01-11 13:11:45 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " <td style='padding-left: 10px; color: red; float: left;'><b>%s</b></td> " ,
gasname ( & newgasmix ) ) ;
2018-01-28 15:44:54 +00:00
if ( isascent & & ( get_he ( & lastprintgasmix ) > 0 ) ) { // For a trimix gas change on ascent, save ICD info if previous cylinder had helium
if ( isobaric_counterdiffusion ( & lastprintgasmix , & newgasmix , & icdvalues ) ) // Do icd calulations
2018-01-15 07:43:57 +00:00
icdwarning = true ;
2018-01-28 15:44:54 +00:00
if ( icdvalues . dN2 > 0 ) { // If the gas change involved helium as well as an increase in nitrogen..
icdlen + = add_icd_entry ( icdbuffer + icdlen , sz_icdbuf - icdlen , & icdvalues , icdtableheader , dp - > time , depth_to_mbar ( dp - > depth . mm , dive ) , & lastprintgasmix , & newgasmix ) ; // .. then print calculations to buffer.
icdtableheader = false ;
}
2018-01-15 07:43:57 +00:00
}
2017-05-25 10:57:06 +00:00
}
lastprintsetpoint = nextdp - > setpoint ;
lastprintgasmix = newgasmix ;
gaschange_after = false ;
} else if ( gaschange_before ) {
2018-01-11 13:11:45 +00:00
// If a new gas has been used for this segment, now is the time to show it
2017-05-25 10:57:06 +00:00
if ( dp - > setpoint ) {
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " (SP = %.1fbar) " ) , ( double ) dp - > setpoint / 1000.0 ) ;
2018-01-11 13:11:45 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " <td style='padding-left: 10px; color: red; float: left;'><b>%s %s</b></td> " ,
gasname ( & gasmix ) , temp ) ;
2017-05-25 10:57:06 +00:00
} else {
2018-01-11 13:11:45 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " <td style='padding-left: 10px; color: red; float: left;'><b>%s</b></td> " ,
gasname ( & gasmix ) ) ;
2018-01-28 15:44:54 +00:00
if ( get_he ( & lastprintgasmix ) > 0 ) { // For a trimix gas change, save ICD info if previous cylinder had helium
if ( isobaric_counterdiffusion ( & lastprintgasmix , & gasmix , & icdvalues ) ) // Do icd calculations
2018-01-15 07:43:57 +00:00
icdwarning = true ;
2018-01-28 15:44:54 +00:00
if ( icdvalues . dN2 > 0 ) { // If the gas change involved helium as well as an increase in nitrogen..
icdlen + = add_icd_entry ( icdbuffer + icdlen , sz_icdbuf - icdlen , & icdvalues , icdtableheader , lasttime , depth_to_mbar ( dp - > depth . mm , dive ) , & lastprintgasmix , & gasmix ) ; // .. then print data to buffer.
icdtableheader = false ;
}
2018-01-15 07:43:57 +00:00
}
2017-05-25 10:57:06 +00:00
}
// Set variables so subsequent iterations can test against the last gas printed
lastprintsetpoint = dp - > setpoint ;
lastprintgasmix = gasmix ;
gaschange_after = false ;
} else {
len + = snprintf ( buffer + len , sz_buffer - len , " <td> </td> " ) ;
}
len + = snprintf ( buffer + len , sz_buffer - len , " </tr> " ) ;
newdepth = dp - > depth . mm ;
lasttime = dp - > time ;
}
}
if ( gaschange_after ) {
2018-01-15 07:43:57 +00:00
// gas switch at this waypoint for verbatim
2017-05-25 10:57:06 +00:00
if ( plan_verbatim ) {
if ( lastsetpoint > = 0 ) {
2018-01-15 07:43:57 +00:00
if ( nextdp & & nextdp - > setpoint ) {
2017-05-25 10:57:06 +00:00
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " Switch gas to %s (SP = %.1fbar) " ) , gasname ( & newgasmix ) , ( double ) nextdp - > setpoint / 1000.0 ) ;
2018-01-15 07:43:57 +00:00
} else {
2017-05-25 10:57:06 +00:00
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " Switch gas to %s " ) , gasname ( & newgasmix ) ) ;
2018-01-28 15:44:54 +00:00
if ( ( isascent ) & & ( get_he ( & lastprintgasmix ) > 0 ) ) { // For a trimix gas change on ascent:
if ( isobaric_counterdiffusion ( & lastprintgasmix , & newgasmix , & icdvalues ) ) // Do icd calculations
2018-01-15 07:43:57 +00:00
icdwarning = true ;
2018-01-28 15:44:54 +00:00
if ( icdvalues . dN2 > 0 ) { // If the gas change involved helium as well as an increase in nitrogen..
icdlen + = add_icd_entry ( icdbuffer + icdlen , sz_icdbuf - icdlen , & icdvalues , icdtableheader , dp - > time , depth_to_mbar ( dp - > depth . mm , dive ) , & lastprintgasmix , & newgasmix ) ; // ... then print data to buffer.
icdtableheader = false ;
}
2018-01-15 07:43:57 +00:00
}
}
2017-05-25 10:57:06 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " %s<br> " , temp ) ;
}
2018-01-15 07:43:57 +00:00
lastprintgasmix = newgasmix ;
2017-05-25 10:57:06 +00:00
gaschange_after = false ;
2018-01-11 13:11:45 +00:00
gasmix = newgasmix ;
2017-05-25 10:57:06 +00:00
}
}
lastprintdepth = newdepth ;
lastdepth = dp - > depth . mm ;
lastsetpoint = dp - > setpoint ;
lastentered = dp - > entered ;
} while ( ( dp = nextdp ) ! = NULL ) ;
if ( ! plan_verbatim )
len + = snprintf ( buffer + len , sz_buffer - len , " </tbody></table><br> " ) ;
/* Print the CNS and OTU next.*/
dive - > cns = 0 ;
dive - > maxcns = 0 ;
update_cylinder_related_info ( dive ) ;
snprintf ( temp , sz_temp , " %s " , translate ( " gettextFromC " , " CNS " ) ) ;
len + = snprintf ( buffer + len , sz_buffer - len , " <div>%s: %i%% " , temp , dive - > cns ) ;
snprintf ( temp , sz_temp , " %s " , translate ( " gettextFromC " , " OTU " ) ) ;
len + = snprintf ( buffer + len , sz_buffer - len , " <br>%s: %i<br></div> " , temp , dive - > otu ) ;
/* Print the settings for the diveplan next. */
2017-10-18 13:58:36 +00:00
if ( decoMode ( ) = = BUEHLMANN ) {
2017-05-25 10:57:06 +00:00
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " Deco model: Bühlmann ZHL-16C with GFLow = %d%% and GFHigh = %d%% " ) ,
diveplan - > gflow , diveplan - > gfhigh ) ;
} else if ( decoMode ( ) = = VPMB ) {
int temp_len ;
if ( diveplan - > vpmb_conservatism = = 0 )
temp_len = snprintf ( temp , sz_temp , " %s " , translate ( " gettextFromC " , " Deco model: VPM-B at nominal conservatism " ) ) ;
else
temp_len = snprintf ( temp , sz_temp , translate ( " gettextFromC " , " Deco model: VPM-B at +%d conservatism " ) , diveplan - > vpmb_conservatism ) ;
if ( diveplan - > eff_gflow )
2018-01-11 13:11:45 +00:00
temp_len + = snprintf ( temp + temp_len , sz_temp - temp_len , translate ( " gettextFromC " , " , effective GF=%d/%d " ) , diveplan - > eff_gflow ,
diveplan - > eff_gfhigh ) ;
2017-05-25 10:57:06 +00:00
} else if ( decoMode ( ) = = RECREATIONAL ) {
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " Deco model: Recreational mode based on Bühlmann ZHL-16B with GFLow = %d%% and GFHigh = %d%% " ) ,
diveplan - > gflow , diveplan - > gfhigh ) ;
}
len + = snprintf ( buffer + len , sz_buffer - len , " <div>%s<br> " , temp ) ;
const char * depth_unit ;
int altitude = ( int ) get_depth_units ( ( int ) ( log ( 1013.0 / diveplan - > surface_pressure ) * 7800000 ) , NULL , & depth_unit ) ;
len + = snprintf ( buffer + len , sz_buffer - len , translate ( " gettextFromC " , " ATM pressure: %dmbar (%d%s)<br></div> " ) ,
2018-01-11 13:11:45 +00:00
diveplan - > surface_pressure , altitude , depth_unit ) ;
2017-05-25 10:57:06 +00:00
/* Get SAC values and units for printing it in gas consumption */
double bottomsacvalue , decosacvalue ;
int sacdecimals ;
const char * sacunit ;
bottomsacvalue = get_volume_units ( prefs . bottomsac , & sacdecimals , & sacunit ) ;
decosacvalue = get_volume_units ( prefs . decosac , NULL , NULL ) ;
/* Reduce number of decimals from 1 to 0 for bar/min, keep 2 for cuft/min */
if ( sacdecimals = = 1 ) sacdecimals - - ;
/* Print the gas consumption next.*/
if ( dive - > dc . divemode = = CCR )
snprintf ( temp , sz_temp , " %s " , translate ( " gettextFromC " , " Gas consumption (CCR legs excluded): " ) ) ;
else
snprintf ( temp , sz_temp , " %s %.*f|%.*f%s/min): " , translate ( " gettextFromC " , " Gas consumption (based on SAC " ) ,
sacdecimals , bottomsacvalue , sacdecimals , decosacvalue , sacunit ) ;
len + = snprintf ( buffer + len , sz_buffer - len , " <div>%s<br> " , temp ) ;
/* Print gas consumption: This loop covers all cylinders */
for ( int gasidx = 0 ; gasidx < MAX_CYLINDERS ; gasidx + + ) {
2017-10-03 11:17:46 +00:00
double volume , pressure , deco_volume , deco_pressure , mingas_volume , mingas_pressure , mingas_d_pressure , mingas_depth ;
2017-05-25 10:57:06 +00:00
const char * unit , * pressure_unit , * depth_unit ;
char warning [ 1000 ] = " " ;
char mingas [ 1000 ] = " " ;
cylinder_t * cyl = & dive - > cylinder [ gasidx ] ;
if ( cylinder_none ( cyl ) )
break ;
volume = get_volume_units ( cyl - > gas_used . mliter , NULL , & unit ) ;
deco_volume = get_volume_units ( cyl - > deco_gas_used . mliter , NULL , & unit ) ;
if ( cyl - > type . size . mliter ) {
int remaining_gas = lrint ( ( double ) cyl - > end . mbar * cyl - > type . size . mliter / 1000.0 / gas_compressibility_factor ( & cyl - > gasmix , cyl - > end . mbar / 1000.0 ) ) ;
2018-01-11 13:11:45 +00:00
double deco_pressure_mbar = isothermal_pressure ( & cyl - > gasmix , 1.0 , remaining_gas + cyl - > deco_gas_used . mliter ,
cyl - > type . size . mliter ) * 1000 - cyl - > end . mbar ;
2017-10-03 11:17:46 +00:00
deco_pressure = get_pressure_units ( lrint ( deco_pressure_mbar ) , & pressure_unit ) ;
2017-05-25 10:57:06 +00:00
pressure = get_pressure_units ( cyl - > start . mbar - cyl - > end . mbar , & pressure_unit ) ;
/* Warn if the plan uses more gas than is available in a cylinder
* This only works if we have working pressure for the cylinder
* 10 bar is a made up number - but it seemed silly to pretend you could breathe cylinder down to 0 */
if ( cyl - > end . mbar < 10000 )
snprintf ( warning , sizeof ( warning ) , " <br> — <span style='color: red;'>%s </span> %s " ,
translate ( " gettextFromC " , " Warning: " ) ,
translate ( " gettextFromC " , " this is more gas than available in the specified cylinder! " ) ) ;
else
Unify float calulations: use double
Internal floating point (FP) calculations should be performed using double
unless there is a very good reason. This avoids headaches with conversions.
Indeed, the vast majority of FP calculations were already done using double.
This patch adapts most remaining calculations. Not converted where things
that were based on binary representations and variables which weren't used
anyway.
An analysis of all instances follows:
core/plannernotes.c, l.404:
This was a comparison between two floats. On the left side, first an integer
was cast to float then multiplied with and integer and divided by a constant
double. The right hand side was an integer cast to a float. Simply divide by
1000.0 first to convert to double and continue with calculations. On the right
hand side, remove the cast, because the integer will be implicitely cast to
double for comparison. This conversion actually emits less instructions,
because no conversion to double and back is performed.
core/planner.c, l.613:
Same analysis as previous case.
subsurface-desktop-main.cpp, l.155:
A local variable representing the version OpenGL version. Turn this into
integer logic. Not only does this avoid dreaded FP rounding issues, it also
works correctly for minor version > 10 (not that such a thing is to be
expected anytime soon).
abstractpreferenceswidget.[h/cpp]:
A widget where the position is described as a float. Turn into double.
desktop-widgets/divelogexportdialog.cpp, l.313:
total_weight is described as float. Use double arithmetics instead. This
instance fixes a truncation warning emitted by gcc.
2017-11-20 20:39:50 +00:00
if ( cyl - > end . mbar / 1000.0 * cyl - > type . size . mliter / gas_compressibility_factor ( & cyl - > gasmix , cyl - > end . mbar / 1000.0 )
< cyl - > deco_gas_used . mliter )
2017-05-25 10:57:06 +00:00
snprintf ( warning , sizeof ( warning ) , " <br> — <span style='color: red;'>%s </span> %s " ,
translate ( " gettextFromC " , " Warning: " ) ,
translate ( " gettextFromC " , " not enough reserve for gas sharing on ascent! " ) ) ;
/* Do and print minimum gas calculation for last bottom gas, but only for OC mode, */
/* not for recreational mode and if no other warning was set before. */
else
if ( lastbottomdp & & gasidx = = lastbottomdp - > cylinderid
& & dive - > dc . divemode = = OC & & decoMode ( ) ! = RECREATIONAL ) {
/* Calculate minimum gas volume. */
volume_t mingasv ;
mingasv . mliter = lrint ( prefs . sacfactor / 100.0 * prefs . problemsolvingtime * prefs . bottomsac
* depth_to_bar ( lastbottomdp - > depth . mm , dive )
+ prefs . sacfactor / 100.0 * cyl - > deco_gas_used . mliter ) ;
/* Calculate minimum gas pressure for cyclinder. */
lastbottomdp - > minimum_gas . mbar = lrint ( isothermal_pressure ( & cyl - > gasmix , 1.0 ,
mingasv . mliter , cyl - > type . size . mliter ) * 1000 ) ;
/* Translate all results into correct units */
mingas_volume = get_volume_units ( mingasv . mliter , NULL , & unit ) ;
mingas_pressure = get_pressure_units ( lastbottomdp - > minimum_gas . mbar , & pressure_unit ) ;
2017-10-03 11:17:46 +00:00
mingas_d_pressure = get_pressure_units ( lrint ( ( double ) cyl - > end . mbar + deco_pressure_mbar - lastbottomdp - > minimum_gas . mbar ) , & pressure_unit ) ;
2017-05-25 10:57:06 +00:00
mingas_depth = get_depth_units ( lastbottomdp - > depth . mm , NULL , & depth_unit ) ;
/* Print it to results */
2017-10-18 13:58:36 +00:00
if ( cyl - > start . mbar > lastbottomdp - > minimum_gas . mbar ) {
2018-01-20 13:04:48 +00:00
snprintf ( mingas , sizeof ( mingas ) , " <br> — <span style='color: %s;'>%s</span> (%s %.1fx%s/+%d%s@%.0f%s): "
" %.0f%s/%.0f%s<span style='color: %s;'>/Δ:%+.0f%s</span> " ,
2017-10-18 13:58:36 +00:00
mingas_d_pressure > 0 ? " green " : " red " ,
translate ( " gettextFromC " , " Minimum gas " ) ,
translate ( " gettextFromC " , " based on " ) ,
prefs . sacfactor / 100.0 ,
translate ( " gettextFromC " , " SAC " ) ,
prefs . problemsolvingtime ,
translate ( " gettextFromC " , " min " ) ,
mingas_depth , depth_unit ,
mingas_volume , unit ,
mingas_pressure , pressure_unit ,
mingas_d_pressure > 0 ? " grey " : " indianred " ,
mingas_d_pressure , pressure_unit ) ;
} else {
snprintf ( warning , sizeof ( warning ) , " <br> — <span style='color: red;'>%s </span> %s " ,
translate ( " gettextFromC " , " Warning: " ) ,
translate ( " gettextFromC " , " required minimum gas for ascent already exceeding start pressure of cylinder! " ) ) ;
}
2017-05-25 10:57:06 +00:00
}
/* Print the gas consumption for every cylinder here to temp buffer. */
2018-01-11 13:11:45 +00:00
if ( lrint ( volume ) > 0 ) {
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " %.0f%s/%.0f%s of <span style='color: red;'><b>%s</b></span> (%.0f%s/%.0f%s in planned ascent) " ) ,
volume , unit , pressure , pressure_unit , gasname ( & cyl - > gasmix ) , deco_volume , unit , deco_pressure , pressure_unit ) ;
} else {
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " %.0f%s/%.0f%s of <span style='color: red;'><b>%s</b></span> " ) ,
volume , unit , pressure , pressure_unit , gasname ( & cyl - > gasmix ) ) ;
}
2017-05-25 10:57:06 +00:00
} else {
2018-01-15 07:43:57 +00:00
if ( lrint ( volume ) > 0 ) {
2017-10-20 13:07:13 +00:00
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " %.0f%s of <span style='color: red;'><b>%s</b></span> (%.0f%s during planned ascent) " ) ,
volume , unit , gasname ( & cyl - > gasmix ) , deco_volume , unit ) ;
2018-01-15 07:43:57 +00:00
} else {
2017-10-20 13:07:13 +00:00
snprintf ( temp , sz_temp , translate ( " gettextFromC " , " %.0f%s of <span style='color: red;'><b>%s</b></span> " ) ,
volume , unit , gasname ( & cyl - > gasmix ) ) ;
2018-01-15 07:43:57 +00:00
}
2017-05-25 10:57:06 +00:00
}
/* Gas consumption: Now finally print all strings to output */
2018-01-20 12:59:41 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " %s%s%s<br></div> " , temp , warning , mingas ) ;
2017-05-25 10:57:06 +00:00
}
2018-01-28 15:44:54 +00:00
/* For trimix OC dives, if an icd table header and icd data were printed to buffer, then add the ICD table here */
2018-02-24 19:07:11 +00:00
if ( ! icdtableheader & & prefs . show_icd ) {
2018-01-28 15:44:54 +00:00
icdlen + = snprintf ( icdbuffer + icdlen , sz_icdbuf - icdlen , " </tbody></table> " ) ; // End the ICD table
len + = snprintf ( buffer + len , sz_buffer - len , " %s " , icdbuffer ) ; // ..and add it to the html buffer
if ( icdwarning ) { // If necessary, add warning
2018-01-15 07:43:57 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " <span style='color: red;'>%s</span> %s " ,
translate ( " gettextFromC " , " Warning: " ) ,
translate ( " gettextFromC " , " Isobaric counterdiffusion conditions exceeded " ) ) ;
}
2018-01-20 12:59:41 +00:00
len + = snprintf ( buffer + len , sz_buffer - len , " <br></div> " ) ;
2018-01-15 07:43:57 +00:00
}
2017-05-25 10:57:06 +00:00
/* Print warnings for pO2 */
dp = diveplan - > dp ;
bool o2warning_exist = false ;
if ( dive - > dc . divemode ! = CCR ) {
while ( dp ) {
if ( dp - > time ! = 0 ) {
struct gas_pressures pressures ;
struct gasmix * gasmix = & dive - > cylinder [ dp - > cylinderid ] . gasmix ;
fill_pressures ( & pressures , depth_to_atm ( dp - > depth . mm , dive ) , gasmix , 0.0 , dive - > dc . divemode ) ;
if ( pressures . o2 > ( dp - > entered ? prefs . bottompo2 : prefs . decopo2 ) / 1000.0 ) {
const char * depth_unit ;
int decimals ;
double depth_value = get_depth_units ( dp - > depth . mm , & decimals , & depth_unit ) ;
len = strlen ( buffer ) ;
2018-01-20 12:59:41 +00:00
if ( ! o2warning_exist )
len + = snprintf ( buffer + len , sz_buffer - len , " <div> " ) ;
2017-05-25 10:57:06 +00:00
o2warning_exist = true ;
snprintf ( temp , sz_temp ,
translate ( " gettextFromC " , " high pO₂ value %.2f at %d:%02u with gas %s at depth %.*f %s " ) ,
pressures . o2 , FRACTION ( dp - > time , 60 ) , gasname ( gasmix ) , decimals , depth_value , depth_unit ) ;
len + = snprintf ( buffer + len , sz_buffer - len , " <span style='color: red;'>%s </span> %s<br> " ,
translate ( " gettextFromC " , " Warning: " ) , temp ) ;
} else if ( pressures . o2 < 0.16 ) {
const char * depth_unit ;
int decimals ;
double depth_value = get_depth_units ( dp - > depth . mm , & decimals , & depth_unit ) ;
len = strlen ( buffer ) ;
2018-01-20 12:59:41 +00:00
if ( ! o2warning_exist )
len + = snprintf ( buffer + len , sz_buffer - len , " <div> " ) ;
2017-05-25 10:57:06 +00:00
o2warning_exist = true ;
snprintf ( temp , sz_temp ,
translate ( " gettextFromC " , " low pO₂ value %.2f at %d:%02u with gas %s at depth %.*f %s " ) ,
pressures . o2 , FRACTION ( dp - > time , 60 ) , gasname ( gasmix ) , decimals , depth_value , depth_unit ) ;
len + = snprintf ( buffer + len , sz_buffer - len , " <span style='color: red;'>%s </span> %s<br> " ,
translate ( " gettextFromC " , " Warning: " ) , temp ) ;
}
}
dp = dp - > next ;
}
}
snprintf ( buffer + len , sz_buffer - len , " </div> " ) ;
dive - > notes = strdup ( buffer ) ;
2018-01-15 07:43:57 +00:00
finished :
2017-05-25 10:57:06 +00:00
free ( ( void * ) buffer ) ;
free ( ( void * ) temp ) ;
2018-01-15 07:43:57 +00:00
free ( ( void * ) icdbuffer ) ;
2018-01-11 13:11:45 +00:00
2018-01-15 07:43:57 +00:00
}