random_r.c
Go to the documentation of this file.
1 /*<html><pre> -<a href="index_r.htm#TOC"
2  >-------------------------------</a><a name="TOP">-</a>
3 
4  random_r.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_r.h"
12 #include "random_r.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_r.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_r.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_r.htm#TOC"
125  >-------------------------------</a><a name="rand">-</a>
126 
127  qh_rand()
128  qh_srand(qh, 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 #define qh_rand_a 16807
144 #define qh_rand_m 2147483647
145 #define qh_rand_q 127773 /* m div a */
146 #define qh_rand_r 2836 /* m mod a */
147 
148 int qh_rand(qhT *qh) {
149  int lo, hi, test;
150  int seed = qh->last_random;
151 
152  hi = seed / qh_rand_q; /* seed div q */
153  lo = seed % qh_rand_q; /* seed mod q */
154  test = qh_rand_a * lo - qh_rand_r * hi;
155  if (test > 0)
156  seed= test;
157  else
158  seed= test + qh_rand_m;
159  qh->last_random= seed;
160  /* seed = seed < qh_RANDOMmax/2 ? 0 : qh_RANDOMmax; for testing */
161  /* seed = qh_RANDOMmax; for testing */
162  return seed;
163 } /* rand */
164 
165 void qh_srand(qhT *qh, int seed) {
166  if (seed < 1)
167  qh->last_random= 1;
168  else if (seed >= qh_rand_m)
169  qh->last_random= qh_rand_m - 1;
170  else
171  qh->last_random= seed;
172 } /* qh_srand */
173 
174 /*-<a href="qh-geom_r.htm#TOC"
175 >-------------------------------</a><a name="randomfactor">-</a>
176 
177 qh_randomfactor(qh, scale, offset )
178  return a random factor r * scale + offset
179 
180 notes:
181  qh.RANDOMa/b are defined in global_r.c
182  qh_RANDOMint requires 'qh'
183 */
184 realT qh_randomfactor(qhT *qh, realT scale, realT offset) {
185  realT randr;
186 
187  randr= qh_RANDOMint;
188  return randr * scale + offset;
189 } /* randomfactor */
190 
191 /*-<a href="qh-geom_r.htm#TOC"
192 >-------------------------------</a><a name="randommatrix">-</a>
193 
194 qh_randommatrix(qh, buffer, dim, rows )
195  generate a random dim X dim matrix in range [-1,1]
196  assumes buffer is [dim+1, dim]
197 
198  returns:
199  sets buffer to random numbers
200  sets rows to rows of buffer
201  sets row[dim] as scratch row
202 
203  notes:
204  qh_RANDOMint requires 'qh'
205 */
206 void qh_randommatrix(qhT *qh, realT *buffer, int dim, realT **rows) {
207  int i, k;
208  realT **rowi, *coord, realr;
209 
210  coord= buffer;
211  rowi= rows;
212  for (i=0; i < dim; i++) {
213  *(rowi++)= coord;
214  for (k=0; k < dim; k++) {
215  realr= qh_RANDOMint;
216  *(coord++)= 2.0 * realr/(qh_RANDOMmax+1) - 1.0;
217  }
218  }
219  *rowi= coord;
220 } /* randommatrix */
221 
222 /*-<a href="qh-globa_r.htm#TOC"
223  >-------------------------------</a><a name="strtol">-</a>
224 
225  qh_strtol( s, endp) qh_strtod( s, endp)
226  internal versions of strtol() and strtod()
227  does not skip trailing spaces
228  notes:
229  some implementations of strtol()/strtod() skip trailing spaces
230 */
231 double qh_strtod(const char *s, char **endp) {
232  double result;
233 
234  result= strtod(s, endp);
235  if (s < (*endp) && (*endp)[-1] == ' ')
236  (*endp)--;
237  return result;
238 } /* strtod */
239 
240 int qh_strtol(const char *s, char **endp) {
241  int result;
242 
243  result= (int) strtol(s, endp, 10); /* WARN64 */
244  if (s< (*endp) && (*endp)[-1] == ' ')
245  (*endp)--;
246  return result;
247 } /* strtol */
seed
void seed(unsigned int seed_value)
qh_randommatrix
void qh_randommatrix(qhT *qh, realT *buffer, int dim, realT **rows)
Definition: random_r.c:206
test
void test(const Shape &original_shape, const FCL_REAL inflation, const FCL_REAL tol=1e-8)
Definition: shape_inflation.cpp:110
random_r.h
realT
#define realT
Definition: user.h:154
rows
int rows
qhT
Definition: libqhull.h:465
qh_argv_to_command
int qh_argv_to_command(int argc, char *argv[], char *command, int max_size)
Definition: random_r.c:42
libqhull_r.h
qh
#define qh
Definition: libqhull.h:457
qh_rand
int qh_rand(qhT *qh)
Definition: random_r.c:148
qh_argv_to_command_size
int qh_argv_to_command_size(int argc, char *argv[])
Definition: random_r.c:105
qh_RANDOMint
#define qh_RANDOMint
Definition: user.h:283
t
tuple t
qh_rand_m
#define qh_rand_m
Definition: random_r.c:144
qh_RANDOMmax
#define qh_RANDOMmax
Definition: user.h:282
qh_rand_r
#define qh_rand_r
Definition: random_r.c:146
dim
int dim
qh_randomfactor
realT qh_randomfactor(qhT *qh, realT scale, realT offset)
Definition: random_r.c:184
qh_rand_a
#define qh_rand_a
Definition: random_r.c:143
qh_srand
void qh_srand(qhT *qh, int seed)
Definition: random_r.c:165
qh_rand_q
#define qh_rand_q
Definition: random_r.c:145
qh_strtol
int qh_strtol(const char *s, char **endp)
Definition: random_r.c:240
qh_strtod
double qh_strtod(const char *s, char **endp)
Definition: random_r.c:231


hpp-fcl
Author(s):
autogenerated on Fri Aug 2 2024 02:45:15