random.c
Go to the documentation of this file.
1 /*<html><pre> -<a href="index.htm#TOC"
2  >-------------------------------</a><a name="TOP">-</a>
3 
4  random.c and utilities
5  Park & Miller's minimimal standard random number generator
6  argc/argv conversion
7 
8  Used by rbox. Do not use 'qh'
9 */
10 
11 #include "libqhull.h"
12 #include "random.h"
13 
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 
18 #ifdef _MSC_VER /* Microsoft Visual C++ -- warning level 4 */
19 #pragma warning( disable : 4706) /* assignment within conditional function */
20 #pragma warning( disable : 4996) /* function was declared deprecated(strcpy, localtime, etc.) */
21 #endif
22 
23 /*-<a href="qh-globa.htm#TOC"
24  >-------------------------------</a><a name="argv_to_command">-</a>
25 
26  qh_argv_to_command( argc, argv, command, max_size )
27 
28  build command from argc/argv
29  max_size is at least
30 
31  returns:
32  a space-delimited string of options (just as typed)
33  returns false if max_size is too short
34 
35  notes:
36  silently removes
37  makes option string easy to input and output
38  matches qh_argv_to_command_size()
39 
40  argc may be 0
41 */
42 int qh_argv_to_command(int argc, char *argv[], char* command, int max_size) {
43  int i, remaining;
44  char *s;
45  *command= '\0'; /* max_size > 0 */
46 
47  if (argc) {
48  if ((s= strrchr( argv[0], '\\')) /* get filename w/o .exe extension */
49  || (s= strrchr( argv[0], '/')))
50  s++;
51  else
52  s= argv[0];
53  if ((int)strlen(s) < max_size) /* WARN64 */
54  strcpy(command, s);
55  else
56  goto error_argv;
57  if ((s= strstr(command, ".EXE"))
58  || (s= strstr(command, ".exe")))
59  *s= '\0';
60  }
61  for (i=1; i < argc; i++) {
62  s= argv[i];
63  remaining= max_size - (int)strlen(command) - (int)strlen(s) - 2; /* WARN64 */
64  if (!*s || strchr(s, ' ')) {
65  char *t= command + strlen(command);
66  remaining -= 2;
67  if (remaining < 0) {
68  goto error_argv;
69  }
70  *t++= ' ';
71  *t++= '"';
72  while (*s) {
73  if (*s == '"') {
74  if (--remaining < 0)
75  goto error_argv;
76  *t++= '\\';
77  }
78  *t++= *s++;
79  }
80  *t++= '"';
81  *t= '\0';
82  }else if (remaining < 0) {
83  goto error_argv;
84  }else
85  strcat(command, " ");
86  strcat(command, s);
87  }
88  return 1;
89 
90 error_argv:
91  return 0;
92 } /* argv_to_command */
93 
94 /*-<a href="qh-globa.htm#TOC"
95 >-------------------------------</a><a name="argv_to_command_size">-</a>
96 
97 qh_argv_to_command_size( argc, argv )
98 
99  return size to allocate for qh_argv_to_command()
100 
101 notes:
102  argc may be 0
103  actual size is usually shorter
104 */
105 int qh_argv_to_command_size(int argc, char *argv[]) {
106  unsigned int count= 1; /* null-terminator if argc==0 */
107  int i;
108  char *s;
109 
110  for (i=0; i<argc; i++){
111  count += (int)strlen(argv[i]) + 1; /* WARN64 */
112  if (i>0 && strchr(argv[i], ' ')) {
113  count += 2; /* quote delimiters */
114  for (s=argv[i]; *s; s++) {
115  if (*s == '"') {
116  count++;
117  }
118  }
119  }
120  }
121  return count;
122 } /* argv_to_command_size */
123 
124 /*-<a href="qh-geom.htm#TOC"
125  >-------------------------------</a><a name="rand">-</a>
126 
127  qh_rand()
128  qh_srand( seed )
129  generate pseudo-random number between 1 and 2^31 -2
130 
131  notes:
132  For qhull and rbox, called from qh_RANDOMint(),etc. [user.h]
133 
134  From Park & Miller's minimal standard random number generator
135  Communications of the ACM, 31:1192-1201, 1988.
136  Does not use 0 or 2^31 -1
137  this is silently enforced by qh_srand()
138  Can make 'Rn' much faster by moving qh_rand to qh_distplane
139 */
140 
141 /* Global variables and constants */
142 
143 int qh_last_random= 1; /* define as global variable instead of using qh */
144 
145 #define qh_rand_a 16807
146 #define qh_rand_m 2147483647
147 #define qh_rand_q 127773 /* m div a */
148 #define qh_rand_r 2836 /* m mod a */
149 
150 int qh_rand( void) {
151  int lo, hi, test;
152  int seed = qh_last_random;
153 
154  hi = seed / qh_rand_q; /* seed div q */
155  lo = seed % qh_rand_q; /* seed mod q */
156  test = qh_rand_a * lo - qh_rand_r * hi;
157  if (test > 0)
158  seed= test;
159  else
160  seed= test + qh_rand_m;
162  /* seed = seed < qh_RANDOMmax/2 ? 0 : qh_RANDOMmax; for testing */
163  /* seed = qh_RANDOMmax; for testing */
164  return seed;
165 } /* rand */
166 
167 void qh_srand( int seed) {
168  if (seed < 1)
169  qh_last_random= 1;
170  else if (seed >= qh_rand_m)
172  else
174 } /* qh_srand */
175 
176 /*-<a href="qh-geom.htm#TOC"
177 >-------------------------------</a><a name="randomfactor">-</a>
178 
179 qh_randomfactor( scale, offset )
180  return a random factor r * scale + offset
181 
182 notes:
183  qh.RANDOMa/b are defined in global.c
184 */
186  realT randr;
187 
188  randr= qh_RANDOMint;
189  return randr * scale + offset;
190 } /* randomfactor */
191 
192 /*-<a href="qh-geom.htm#TOC"
193 >-------------------------------</a><a name="randommatrix">-</a>
194 
195 qh_randommatrix( buffer, dim, rows )
196  generate a random dim X dim matrix in range [-1,1]
197  assumes buffer is [dim+1, dim]
198 
199 returns:
200  sets buffer to random numbers
201  sets rows to rows of buffer
202  sets row[dim] as scratch row
203 */
204 void qh_randommatrix(realT *buffer, int dim, realT **rows) {
205  int i, k;
206  realT **rowi, *coord, realr;
207 
208  coord= buffer;
209  rowi= rows;
210  for (i=0; i < dim; i++) {
211  *(rowi++)= coord;
212  for (k=0; k < dim; k++) {
213  realr= qh_RANDOMint;
214  *(coord++)= 2.0 * realr/(qh_RANDOMmax+1) - 1.0;
215  }
216  }
217  *rowi= coord;
218 } /* randommatrix */
219 
220 /*-<a href="qh-globa.htm#TOC"
221  >-------------------------------</a><a name="strtol">-</a>
222 
223  qh_strtol( s, endp) qh_strtod( s, endp)
224  internal versions of strtol() and strtod()
225  does not skip trailing spaces
226  notes:
227  some implementations of strtol()/strtod() skip trailing spaces
228 */
229 double qh_strtod(const char *s, char **endp) {
230  double result;
231 
232  result= strtod(s, endp);
233  if (s < (*endp) && (*endp)[-1] == ' ')
234  (*endp)--;
235  return result;
236 } /* strtod */
237 
238 int qh_strtol(const char *s, char **endp) {
239  int result;
240 
241  result= (int) strtol(s, endp, 10); /* WARN64 */
242  if (s< (*endp) && (*endp)[-1] == ' ')
243  (*endp)--;
244  return result;
245 } /* strtol */
qh_strtod
double qh_strtod(const char *s, char **endp)
Definition: random.c:229
qh_rand_r
#define qh_rand_r
Definition: random.c:148
seed
void seed(unsigned int seed_value)
qh_last_random
int qh_last_random
Definition: random.c:143
test
void test(const Shape &original_shape, const FCL_REAL inflation, const FCL_REAL tol=1e-8)
Definition: shape_inflation.cpp:110
qh_argv_to_command
int qh_argv_to_command(int argc, char *argv[], char *command, int max_size)
Definition: random.c:42
qh_argv_to_command_size
int qh_argv_to_command_size(int argc, char *argv[])
Definition: random.c:105
realT
#define realT
Definition: user.h:154
rows
int rows
qh_strtol
int qh_strtol(const char *s, char **endp)
Definition: random.c:238
libqhull.h
qh_srand
void qh_srand(int seed)
Definition: random.c:167
random.h
dim
int dim
qh_rand_a
#define qh_rand_a
Definition: random.c:145
qh_randomfactor
realT qh_randomfactor(realT scale, realT offset)
Definition: random.c:185
qh_rand
int qh_rand(void)
Definition: random.c:150
qh_RANDOMint
#define qh_RANDOMint
Definition: user.h:283
t
tuple t
qh_RANDOMmax
#define qh_RANDOMmax
Definition: user.h:282
qh_rand_m
#define qh_rand_m
Definition: random.c:146
qh_rand_q
#define qh_rand_q
Definition: random.c:147
qh_randommatrix
void qh_randommatrix(realT *buffer, int dim, realT **rows)
Definition: random.c:204


hpp-fcl
Author(s):
autogenerated on Fri Jan 26 2024 03:46:15