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