1 |
|
---|
2 | #include <math.h>
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <stdlib.h>
|
---|
5 | #include <time.h>
|
---|
6 |
|
---|
7 | #include "common.h"
|
---|
8 |
|
---|
9 | #ifdef _X_WINDOW_
|
---|
10 | #include "xwindow.h"
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #ifndef _MSC_VER
|
---|
14 | #include <unistd.h>
|
---|
15 | #include <sys/types.h>
|
---|
16 | #include <sys/time.h>
|
---|
17 | #include <sys/times.h>
|
---|
18 | #include <sys/resource.h>
|
---|
19 | #else
|
---|
20 | #include <windows.h>
|
---|
21 | #include <sys/types.h>
|
---|
22 | #include <sys/timeb.h>
|
---|
23 | #endif
|
---|
24 |
|
---|
25 |
|
---|
26 | using namespace std;
|
---|
27 |
|
---|
28 | namespace GtpVisibilityPreprocessor {
|
---|
29 |
|
---|
30 |
|
---|
31 | // global file streams
|
---|
32 | ofstream Debug;
|
---|
33 |
|
---|
34 | int CGlobals::Special=0;
|
---|
35 |
|
---|
36 |
|
---|
37 | // Puts the specified number of spaces out to cout. Used for
|
---|
38 | // indentation during CObject3D::Describe().
|
---|
39 | void
|
---|
40 | indent(ostream &app, int ind)
|
---|
41 | {
|
---|
42 | int i;
|
---|
43 |
|
---|
44 | for (i = 0; i < ind; i++)
|
---|
45 | app << ' ';
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | Real Limits::Threshold = 1e-6f;
|
---|
51 | Real Limits::Small = 0.01f;
|
---|
52 | Real Limits::Infinity = 1e20f;
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | Real
|
---|
58 | Random(Real max)
|
---|
59 | {
|
---|
60 | #ifdef __UNIX__
|
---|
61 | return (random()*max/0x7FFFFFFF);
|
---|
62 | #else
|
---|
63 | return (((Real)rand())/RAND_MAX)*max;
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | }
|
---|
67 |
|
---|
68 | int Random(int max)
|
---|
69 | {
|
---|
70 | return rand()%max;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void Randomize()
|
---|
74 | {
|
---|
75 | time_t t;
|
---|
76 | srand((unsigned) time(&t));
|
---|
77 | }
|
---|
78 |
|
---|
79 | void
|
---|
80 | Randomize(const unsigned int seed)
|
---|
81 | {
|
---|
82 | srand(seed);
|
---|
83 | }
|
---|
84 |
|
---|
85 | void GetKey(char *s)
|
---|
86 | {
|
---|
87 | #ifdef _X_WINDOW_
|
---|
88 | XWaitForButton(s);
|
---|
89 | #else
|
---|
90 | getch();
|
---|
91 | #endif
|
---|
92 | }
|
---|
93 |
|
---|
94 | #ifdef _MSC_VER
|
---|
95 | static bool hasHRTimer = false;
|
---|
96 | static LARGE_INTEGER hrFreq;
|
---|
97 | #endif
|
---|
98 |
|
---|
99 |
|
---|
100 | void
|
---|
101 | InitTiming()
|
---|
102 | {
|
---|
103 | #ifdef _MSC_VER
|
---|
104 | hasHRTimer = (bool) QueryPerformanceFrequency(&hrFreq);
|
---|
105 | #endif
|
---|
106 | }
|
---|
107 |
|
---|
108 | long
|
---|
109 | GetTime()
|
---|
110 | {
|
---|
111 | #ifndef _MSC_VER
|
---|
112 | static struct rusage r;
|
---|
113 | getrusage(RUSAGE_SELF,&r);
|
---|
114 | return r.ru_utime.tv_usec+1000000*r.ru_utime.tv_sec;
|
---|
115 | #else
|
---|
116 | if (hasHRTimer) {
|
---|
117 | LARGE_INTEGER counter;
|
---|
118 | QueryPerformanceCounter(&counter);
|
---|
119 | // return in usec
|
---|
120 | //return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart));
|
---|
121 | // $$
|
---|
122 | // tmp store time in ms
|
---|
123 | return (long) (1000*counter.QuadPart/(hrFreq.QuadPart));
|
---|
124 | } else {
|
---|
125 | static struct _timeb mtime;
|
---|
126 | _ftime(&mtime);
|
---|
127 |
|
---|
128 | return 1000*(1000*mtime.time + mtime.millitm);
|
---|
129 | }
|
---|
130 | #endif
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | long
|
---|
135 | GetRealTime()
|
---|
136 | {
|
---|
137 | #ifndef _MSC_VER
|
---|
138 | static struct timeval _tstart;
|
---|
139 | static struct timezone tz;
|
---|
140 | gettimeofday(&_tstart,&tz);
|
---|
141 | return (long)(1000000*_tstart.tv_sec + _tstart.tv_usec);
|
---|
142 | #else
|
---|
143 | if (hasHRTimer) {
|
---|
144 | LARGE_INTEGER counter;
|
---|
145 | QueryPerformanceCounter(&counter);
|
---|
146 | // return in usec
|
---|
147 | return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart));
|
---|
148 | } else {
|
---|
149 | static struct _timeb mtime;
|
---|
150 | _ftime(&mtime);
|
---|
151 | return 1000*(1000*mtime.time + mtime.millitm);
|
---|
152 | }
|
---|
153 | #endif
|
---|
154 | }
|
---|
155 |
|
---|
156 | // return time diff. in ms
|
---|
157 | Real
|
---|
158 | TimeDiff(long time1, long time2) // in ms
|
---|
159 | {
|
---|
160 | //const Real clk=1.0e-3f; // ticks per second
|
---|
161 | // $$ tmp store time in ms
|
---|
162 | const Real clk=1.0f; // ticks per second
|
---|
163 | long t=time2-time1;
|
---|
164 |
|
---|
165 | return ((t<0)?-t:t)*clk;
|
---|
166 | }
|
---|
167 |
|
---|
168 | char *
|
---|
169 | TimeString()
|
---|
170 | {
|
---|
171 | time_t t;
|
---|
172 | time(&t);
|
---|
173 | return ctime(&t);
|
---|
174 | }
|
---|
175 |
|
---|
176 | char *
|
---|
177 | GetAbsPath(char *name)
|
---|
178 | {
|
---|
179 | // char *url = new char[strlen(name)+1];
|
---|
180 | // strcpy(url,name);
|
---|
181 |
|
---|
182 | // get the absolute path
|
---|
183 | int c = strlen(name);
|
---|
184 | int i = c-1;
|
---|
185 | bool wasDot = false;
|
---|
186 | // search for double dot
|
---|
187 | for (;i>=0;i--)
|
---|
188 | if (name[i]=='.') {
|
---|
189 | if (wasDot)
|
---|
190 | break;
|
---|
191 | wasDot = true;
|
---|
192 | } else
|
---|
193 | wasDot = false;
|
---|
194 |
|
---|
195 | if (i>0)
|
---|
196 | i+=3;
|
---|
197 | if (i<0)
|
---|
198 | i=0;
|
---|
199 |
|
---|
200 | char *url = new char[c-i+1];
|
---|
201 | int j=0;
|
---|
202 | for (;i<c;i++,j++)
|
---|
203 | url[j] = name[i];
|
---|
204 | url[j]=0;
|
---|
205 |
|
---|
206 | return url;
|
---|
207 | }
|
---|
208 |
|
---|
209 | char *
|
---|
210 | GetPath(const char *s)
|
---|
211 | {
|
---|
212 | int i=strlen(s);
|
---|
213 | for (; i>0; i--) {
|
---|
214 | if (s[i]=='/' || s[i]=='\\')
|
---|
215 | break;
|
---|
216 | }
|
---|
217 |
|
---|
218 | char *path = new char[i+1];
|
---|
219 | int j=0;
|
---|
220 | for (; j<i; j++)
|
---|
221 | path[j] = s[j];
|
---|
222 | path[j] = 0;
|
---|
223 | return path;
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | char *
|
---|
228 | StripPath(const char *s)
|
---|
229 | {
|
---|
230 | const int len = strlen(s);
|
---|
231 |
|
---|
232 | int i = len;
|
---|
233 | for (; i>0; i--) {
|
---|
234 | if (s[i]=='/' || s[i]=='\\')
|
---|
235 | break;
|
---|
236 | }
|
---|
237 |
|
---|
238 | char *filename = new char[len - i];
|
---|
239 | int j = 0;
|
---|
240 |
|
---|
241 | for (; j < len - i; ++ j)
|
---|
242 | filename[j] = s[i + j + 1];
|
---|
243 | //filename[j] = 0;
|
---|
244 |
|
---|
245 | return filename;
|
---|
246 | }
|
---|
247 |
|
---|
248 | char *
|
---|
249 | strdup(char *a)
|
---|
250 | {
|
---|
251 | if (a) {
|
---|
252 | char *s = new char[strlen(a)+1];
|
---|
253 | strcpy(s,a);
|
---|
254 | return s;
|
---|
255 | } else
|
---|
256 | return NULL;
|
---|
257 | }
|
---|
258 |
|
---|
259 | bool
|
---|
260 | FileExists(char *filename)
|
---|
261 | {
|
---|
262 | FILE *f;
|
---|
263 |
|
---|
264 | f = fopen(filename,"r");
|
---|
265 | if (f) {
|
---|
266 | fclose(f);
|
---|
267 | return true;
|
---|
268 | }
|
---|
269 | return false;
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | #if 0
|
---|
274 | bool
|
---|
275 | CreateDir(char *dir)
|
---|
276 | {
|
---|
277 | #ifdef _MSC_VER
|
---|
278 | HANDLE fFile; // File Handle
|
---|
279 | WIN32_FIND_DATA fileinfo; // File Information Structure
|
---|
280 | int n = strlen(dir) - 1;
|
---|
281 |
|
---|
282 | if (dir[n] == '\\' || dir[n] == '/')
|
---|
283 | dir[n]=0;
|
---|
284 |
|
---|
285 | fFile = FindFirstFile(dir, &fileinfo);
|
---|
286 |
|
---|
287 | // if the file exists and it is a directory
|
---|
288 | if (fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {
|
---|
289 | // Directory Exists close file and return
|
---|
290 | FindClose(fFile);
|
---|
291 | return true;
|
---|
292 | }
|
---|
293 |
|
---|
294 | FindClose(fFile);
|
---|
295 |
|
---|
296 | // Now lets cycle through the String Array and create each directory in turn
|
---|
297 | if (CreateDirectory(dir, 0)) {
|
---|
298 | SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL);
|
---|
299 | return true;
|
---|
300 | } else {
|
---|
301 | char *temp = GetPath(dir);
|
---|
302 |
|
---|
303 | if (strcmp(temp, dir)!=0) {
|
---|
304 | CreateDir(temp);
|
---|
305 | if (CreateDirectory(dir, 0)) {
|
---|
306 | SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL);
|
---|
307 | return true;
|
---|
308 | }
|
---|
309 | }
|
---|
310 | delete temp;
|
---|
311 | }
|
---|
312 | Debug << "cannot create directory " << dir << endl;
|
---|
313 | #endif
|
---|
314 | return false;
|
---|
315 | }
|
---|
316 |
|
---|
317 | #endif
|
---|
318 |
|
---|
319 | }
|
---|