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

Revision 2123, 5.2 KB checked in by mattausch, 17 years ago (diff)

worded on obj loading in Ogre

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        // $$
119        // tmp store time in ms
120    return (long) (1000*counter.QuadPart/(hrFreq.QuadPart));
121  } else {
122    static struct _timeb mtime;
123    _ftime(&mtime);
124   
125    return 1000*(1000*mtime.time + mtime.millitm);
126  }
127#endif
128}
129
130
131long
132GetRealTime()
133{
134#ifndef  _MSC_VER
135  static struct timeval _tstart;
136  static struct timezone tz;
137  gettimeofday(&_tstart,&tz);
138  return (long)(1000000*_tstart.tv_sec + _tstart.tv_usec);
139#else
140  if (hasHRTimer) {
141    LARGE_INTEGER counter;
142    QueryPerformanceCounter(&counter);
143    // return in usec
144    return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart));
145  } else {
146    static struct _timeb mtime;
147    _ftime(&mtime);
148    return 1000*(1000*mtime.time + mtime.millitm);
149  }
150#endif
151}
152
153// return time diff. in ms
154Real
155TimeDiff(long time1, long time2) // in ms
156{
157  const Real clk=1.0e-3f; // ticks per second
158  // $$ tmp store time in ms
159  //const Real clk=1.0f; // ticks per second
160  long t=time2-time1;
161 
162  return ((t<0)?-t:t)*clk;
163}
164
165char *
166TimeString()
167{
168  time_t t;
169  time(&t);
170  return ctime(&t);
171}
172
173char *
174GetAbsPath(char *name)
175{
176  //  char *url = new char[strlen(name)+1];
177  //  strcpy(url,name);
178
179  // get the absolute path
180  int c = strlen(name);
181  int i = c-1;
182  bool wasDot = false;
183  // search for double dot
184  for (;i>=0;i--)
185    if (name[i]=='.') {
186      if (wasDot)
187   break;
188      wasDot = true;
189    } else
190      wasDot = false;
191
192  if (i>0)
193    i+=3;
194  if (i<0)
195    i=0;
196 
197  char *url = new char[c-i+1];
198  int j=0;
199  for (;i<c;i++,j++)
200    url[j] = name[i];
201  url[j]=0;
202 
203  return url;
204}
205
206char *
207GetPath(const char *s)
208{
209  int i=strlen(s);
210  for (; i>0; i--) {
211    if (s[i]=='/' || s[i]=='\\')
212      break;
213  }
214 
215  char *path = new char[i+1];
216  int j=0;
217  for (; j<i; j++)
218    path[j] = s[j];
219  path[j] = 0;
220  return path;
221}
222
223
224char *
225StripPath(const char *s)
226{
227        const int len = strlen(s);
228
229        int i = len;
230        for (; i>0; i--) {
231                if (s[i]=='/' || s[i]=='\\')
232                        break;
233        }
234
235        char *filename = new char[len - i];
236        int j = 0;
237
238        for (; j < len - i; ++ j)
239                filename[j] = s[i + j + 1];
240        //filename[j] = 0;
241
242        return filename;
243}
244
245char *
246strdup(char *a)
247{
248  if (a) {
249    char *s = new char[strlen(a)+1];
250    strcpy(s,a);
251    return s;
252  } else
253    return NULL;
254}
255
256bool
257FileExists(char *filename)
258{
259  FILE *f;
260
261  f = fopen(filename,"r");
262  if (f) {
263    fclose(f);
264    return true;
265  }
266  return false;
267}
268
269
270#if 0
271bool
272CreateDir(char *dir)
273{
274#ifdef _MSC_VER
275  HANDLE fFile; // File Handle
276  WIN32_FIND_DATA fileinfo; // File Information Structure
277  int n = strlen(dir) - 1;
278 
279  if (dir[n] == '\\' || dir[n] == '/')
280    dir[n]=0;
281 
282  fFile = FindFirstFile(dir, &fileinfo);
283 
284  // if the file exists and it is a directory
285  if (fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {
286    // Directory Exists close file and return
287    FindClose(fFile);
288    return true;
289  }
290 
291  FindClose(fFile);
292
293  // Now lets cycle through the String Array and create each directory in turn
294  if (CreateDirectory(dir, 0)) {
295    SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL);
296    return true;
297  } else {
298    char *temp = GetPath(dir);
299
300    if (strcmp(temp, dir)!=0) {
301      CreateDir(temp);
302      if (CreateDirectory(dir, 0)) {
303   SetFileAttributes(dir, FILE_ATTRIBUTE_NORMAL);
304   return true;
305      }
306    }
307    delete temp;
308  }
309  Debug << "cannot create directory " << dir << endl;
310#endif
311  return false;
312}
313
314#endif
315
316}
Note: See TracBrowser for help on using the repository browser.