source: GTP/trunk/Lib/Vis/Preprocessing/src/common.cpp @ 863

Revision 863, 4.7 KB checked in by mattausch, 19 years ago (diff)

working on preprocessor integration
added iv stuff

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