| Bsoft 2.0.5 Bernard's software package

utilities.h
Go to the documentation of this file.
1
9 #define BVERSION "2.0.6-20191125"
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <unistd.h>
15 #include <pwd.h>
16 #include <time.h>
17 #include <math.h>
18
19 #include <climits>
20 #include <cfloat>
21 #include <algorithm>
22 #include <iostream>
23 #include <iomanip>
24 #include <vector>
25 #include <tuple>
26
27 using namespace std;
28
29 #ifdef SUNOS
30 #include <ieeefp.h>
31 #endif
32
33 #ifdef HAVE_OMP
34 #include <omp.h>
35 #endif
36
37 #ifdef HAVE_GCD
38 #include <dispatch/dispatch.h>
39 #endif
40
41 #include "Bstring.h"
42
43 // Constants
44 #define MAXLINELEN 1024 // Maximum length of line for input
45 #define SMALLFLOAT 1e-30 // Threshold for considering a number to be zero
46 #define MIN_SIZE_FOR_THREADING 100000
47 #define TRIGPRECISION 1e-30
48 #define PLANCK 6.626070E-34 // Js
49 #define ECHARGE 1.602177E-19 // C
50 #define EMASS 9.109384E-31 // kg
51 #define LIGHTSPEED 299792458 // m/s
52
53 /************************************************************************
54 @Constant: M_PI
55 @Description:
56 Mathematical PI and variants.
57 *************************************************************************/
58 #ifndef M_PI
59 #define M_PI 3.14159265358979323846264338327950288
60 #endif
61 #ifndef M_PI_2
62 #define M_PI_2 1.57079632679489661923132169163975144
63 #endif
64 #ifndef M_PI_4
65 #define M_PI_4 0.785398163397448309615660845819875721
66 #endif
67 #ifndef TWOPI
68 #define TWOPI 6.28318530717958647692528676655900576
69 #endif
70 #ifndef MIN2PI
71 #define MIN2PI -6.28318530717958647692528676655900576
72 #endif
73 //end
74
75 /************************************************************************
76 @Constant: GOLDEN
77 @Description:
78 Golden number: g = (sqrt(5) + 1)/2.
79 *************************************************************************/
80 #ifndef GOLDEN
81 #define GOLDEN 1.61803398874989484820458683436563811772
82 #endif
83 //end
84
85 /************************************************************************
86 @Constant: RHO
87 @Description:
88 Protein density.
89 *************************************************************************/
90 #ifndef RHO
91 #define RHO 0.81 // Protein density in Da/A3
92 #endif
93 //end
94
95 /************************************************************************
96 @Constants: VERB_
97 @Description:
98 Verbosity designations - controlling output to stdout.
99 @Features:
100 The notion is that any program should not automatically generate output,
101 so that it could be used within a script. Increasing levels of verbosity
102 then results in increasing output.
103 *************************************************************************/
104 #define VERB_NONE 0
105 #define VERB_RESULT 1
106 #define VERB_LABEL 2
107 #define VERB_PROCESS 4
108 #define VERB_STATS 8
109 #define VERB_FULL 16
110 #define VERB_TIME 32
111 #define VERB_MEMORY 64
112 #define VERB_DEBUG 128
113 #define VERB_DEBUG_STAR 256
114 #define VERB_DEBUG_XML 512
115 #define VERB_DEBUG_DM 1024
116 //end
117
118 #ifndef _systype_
119 /************************************************************************
120 @Object: enum SysType
121 @Description:
122 System type enumeration.
123 @Features:
124 BigIEEE must be the lowest number type
125 LittleIEEE must be the little-endian type with the lowest number
126 *************************************************************************/
127 enum SysType {
128 Unknown_System = 0, // Indeterminate
129 BigIEEE = 1, // Big-endian IEEE (unix: MIPS, Motorola PPC)
130 BigOther = 5, // Big-endian systems other than IEEE
131 LittleIEEE = 6, // Little-endian IEEE (unix, Intel, VMS Alpha)
132 LittleAlpha = 7, // Little-endian VMS non-IEEE (Alpha)
133 LittleVAX = 8, // Little-endian VMS (VAX)
134 LittleOther = 10 // Little-endian other systems
135 } ;
136 #define _systype_
137 #endif
138
139 #ifndef _datatype_
140
146 enum DataType {
147 Unknown_Type = 0, // Undefined data type
148 Bit = 1, // Bit/binary type
149 UCharacter = 2, // Unsigned character or byte type
150 SCharacter = 3, // Signed character
151 UShort = 4, // Unsigned integer (2-byte)
152 Short = 5, // Signed integer (2-byte)
153 UInteger = 6, // Unsigned integer (4-byte)
154 Integer = 7, // Signed integer (4-byte)
155 ULong = 8, // Unsigned integer (4 or 8 byte, depending on system)
156 Long = 9, // Signed integer (4 or 8 byte, depending on system)
157 Float = 10, // Floating point (4-byte)
158 Double = 11, // Double precision floating point (8-byte)
159 } ;
160 #define _datatype_
161 #endif
162
163
164 /************************************************************************
165 @Constants: FILL_
166 @Description:
167 Fill value designations - controlling the type of filling new pixels.
168 @Features:
169 The fill value for pixels/voxels when new ones are created or old
170 ones replaced.
171 *************************************************************************/
172 #define FILL_USER 0
173 #define FILL_AVERAGE 1
174 #define FILL_BACKGROUND 2
175 #define FILL_MIN 3
176 #define FILL_MAX 4
177 //end
178
179
180
181
182 // Function prototypes
183 ostream &tab(ostream &out);
184 string get_cmd_line(int argc, char **argv);
185 Bstring command_line_time(int n, char** strings);
186 string get_user_name();
187 void usage(const char** use, int all);
188 SysType systype(int show);
189 size_t system_processors();
190 long system_memory();
191 int memory_check(long mem_required);
192 int bexit(int value);
193 DataType getdatatype(char letter);
194 int select_numbers(Bstring& string, int n, int* numsel);
195 int findNextPowerOf(int startNumber, int powerOf);
196 int get_integer(char* ptr, int len);
197 float get_float(char* ptr, int len);
198 void swapbytes(unsigned char* v, size_t n);
199 void swapbytes(size_t size, unsigned char* v, size_t n);
200 void vax2ieee(unsigned char* v, int sb);
201 double angle_set_negPI_to_PI(double angle);
202 float remove_negative_zeros(float value0, float threshold);
203 size_t get_chunk_size(size_t datasize);
204 size_t get_chunk_size(size_t datasize, size_t c);
205 //int error_show(const char* message, const char* file, int line);
206 int error_show(string message, string file, int line);
207
remove_negative_zeros
float remove_negative_zeros(float value0, float threshold)
Prevent a negative sign from being placed in front of zero value in a text file.
Definition: utilities.cpp:622
usage
void usage(const char **use, int all)
Prints usage information.
Definition: utilities.cpp:119
BigOther
Definition: utilities.h:130
getdatatype
DataType getdatatype(char letter)
Get the data type indicated by a single letter code.
Definition: utilities.cpp:288
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
LittleOther
Definition: utilities.h:134
LittleIEEE
Definition: utilities.h:131
BigIEEE
Definition: utilities.h:129
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:186
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:594
LittleVAX
Definition: utilities.h:133
Integer
Definition: utilities.h:154
get_chunk_size
size_t get_chunk_size(size_t datasize)
Returns the chunk size per thread.
Definition: utilities.cpp:645
SCharacter
Definition: utilities.h:150
SysType
SysType
Definition: utilities.h:127
Long
Definition: utilities.h:156
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Float
Definition: utilities.h:157
UInteger
Definition: utilities.h:153
get_user_name
string get_user_name()
Returns the user name.
Definition: utilities.cpp:85
select_numbers
int select_numbers(Bstring &string, int n, int *numsel)
Converts a string with a selection specification into an integer array.
Definition: utilities.cpp:328
Double
Definition: utilities.h:158
ULong
Definition: utilities.h:155
vax2ieee
void vax2ieee(unsigned char *v, int sb)
Converts VAX floating point format to IEEE floating point format.
Definition: utilities.cpp:548
sb
int sb(0)
system_memory
long system_memory()
Returns system memory size.
Definition: utilities.cpp:204
use
const char * use[]
Definition: b3v.cpp:19
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:504
memory_check
int memory_check(long mem_required)
Checking if there is enough memory, abort if not.
Definition: utilities.cpp:248
get_integer
int get_integer(char *ptr, int len)
Converts a defined length string into an integer.
Definition: utilities.cpp:459
UCharacter
Definition: utilities.h:149
Bstring
Definition: Bstring.h:29
Bit
Definition: utilities.h:148
LittleAlpha
Definition: utilities.h:132
Bstring.h
Header file for the string class.
show
int show(0)
command_line_time
Bstring command_line_time(int n, char **strings)
Returns a set of strings and time in a string.
Definition: utilities.cpp:60
Short
Definition: utilities.h:152
Unknown_Type
Definition: utilities.h:147
UShort
Definition: utilities.h:151
Unknown_System
Definition: utilities.h:128
findNextPowerOf
int findNextPowerOf(int startNumber, int powerOf)
Finds the next greatest number that is a power of a given number.
Definition: utilities.cpp:433
systype
SysType systype(int show)
Finds the system type - mostly just for byte order.
Definition: utilities.cpp:144
get_float
float get_float(char *ptr, int len)
Converts a defined length string into a floating point number.
Definition: utilities.cpp:482
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:755
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21