Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 299 lines (257 sloc) 5.829 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
#if !defined(_UTILS_H)
#define _UTILS_H

#include <list>
#include <map>
#include <math.h>
#include <string>
#include <assert.h>

#define warning(...) fprintf(stderr, "WARNING: " __VA_ARGS__)

#define UNUSED(x) ((void) (x))

#define DISABLE_COPY_AND_ASSIGN(x) \
private: \
x(const x &from); \
void operator =(const x &from)

class vec3 {
public:
float x, y, z;

vec3() {}
vec3(float i_x, float i_y, float i_z) :
x(i_x), y(i_y), z(i_z)
{
}
};

class vec2 {
public:
float x, y;

vec2() {}
vec2(float i_x, float i_y) :
x(i_x), y(i_y)
{
}
};

extern inline vec3 operator + (const vec3 &a, const vec3 &b)
{
return vec3(a.x + b.x, a.y + b.y, a.z + b.z);
}

extern inline vec3 operator - (const vec3 &a, const vec3 &b)
{
return vec3(a.x - b.x, a.y - b.y, a.z - b.z);
}

extern inline vec3 operator * (const vec3 &v, float s)
{
return vec3(v.x * s, v.y * s, v.z * s);
}

extern inline bool operator > (const vec3 &a, const vec3 &b)
{
return a.x > b.x && a.y > b.y && a.z > b.z;
}

extern inline bool operator < (const vec3 &a, const vec3 &b)
{
return a.x < b.x && a.y < b.y && a.z < b.z;
}

extern inline vec2 operator + (const vec2 &a, const vec2 &b)
{
return vec2(a.x + b.x, a.y + b.y);
}

extern inline vec2 operator - (const vec2 &a, const vec2 &b)
{
return vec2(a.x - b.x, a.y - b.y);
}

extern inline vec2 operator * (const vec2 &v, float s)
{
return vec2(v.x * s, v.y * s);
}

extern inline bool operator > (const vec2 &a, const vec2 &b)
{
return a.x > b.x && a.y > b.y;
}

extern inline bool operator < (const vec2 &a, const vec2 &b)
{
return a.x < b.x && a.y < b.y;
}

extern inline float dot(const vec3 &a, const vec3 &b)
{
return a.x*b.x + a.y*b.y + a.z*b.z;
}

extern inline float dot(const vec2 &a, const vec2 &b)
{
return a.x*b.x + a.y*b.y;
}

extern inline float length(const vec3 &v)
{
return sqrtf(dot(v, v));
}

extern inline float length(const vec2 &v)
{
return sqrtf(dot(v, v));
}

template<class T>
class list_iter {
public:
list_iter(std::list<T> &l) :
i(l.begin()),
end(l.end())
{
}
T *operator -> () const { return &*i; }
T &operator *() const { return *i; }
operator bool() const { return i != end; }
void operator ++()
{
i++;
}

private:
typename std::list<T>::iterator i, end;
};

template<class T>
class safe_list_iter {
public:
safe_list_iter(std::list<T> &l) :
i(l.begin()),
next(l.begin()),
end(l.end())
{
if (next != end)
next++;
}
typename std::list<T>::iterator iter() const { return i; }
T *operator -> () const { return &*i; }
T &operator *() const { return *i; }
operator bool() const { return i != end; }
void operator ++()
{
i = next;
if (next != end)
next++;
}

private:
typename std::list<T>::iterator i, next, end;
};

template<class T>
class const_list_iter {
public:
const_list_iter(const std::list<T> &l) :
i(l.begin()),
end(l.end())
{
}
const T *operator -> () const { return &*i; }
T operator *() const { return *i; }
operator bool() const { return i != end; }
void operator ++()
{
i++;
}

private:
typename std::list<T>::const_iterator i, end;
};

template<class Key, class Value>
class map_iter {
public:
map_iter(std::map<Key, Value> &l) :
i(l.begin()),
end(l.end())
{
}
Key key() const { return i->first; }
Value *operator -> () const { return &i->second; }
Value &operator *() const { return i->second; }
operator bool() const { return i != end; }
void operator ++()
{
i++;
}

private:
typename std::map<Key, Value>::iterator i, end;
};

template<class Key, class Value>
class safe_map_iter {
public:
safe_map_iter(std::map<Key, Value> &l) :
i(l.begin()),
next(l.begin()),
end(l.end())
{
if (next != end)
next++;
}
Key key() const { return i->first; }
typename std::map<Key, Value>::iterator iter() const { return i; }
Value *operator -> () const { return &i->second; }
Value &operator *() const { return i->second; }
operator bool() const { return i != end; }
void operator ++()
{
i = next;
if (next != end)
next++;
}

private:
typename std::map<Key, Value>::iterator i, next, end;
};

template<class Key, class Value>
class const_map_iter {
public:
const_map_iter(const std::map<Key, Value> &l) :
i(l.begin()),
end(l.end())
{
}
Key key() const { return i->first; }
const Value *operator -> () const { return &i->second; }
Value operator *() const { return i->second; }
operator bool() const { return i != end; }
void operator ++()
{
i++;
}

private:
typename std::map<Key, Value>::const_iterator i, end;
};

template<class Key, class Value>
void ins(std::multimap<Key, Value> &map, const Key &key, const Value &value)
{
map.insert(std::pair<Key, Value>(key, value));
}

template<class Key, class Value>
void del(std::multimap<Key, Value> &map, const Key &key, const Value &value)
{
typename std::multimap<Key, Value>::iterator i = map.lower_bound(key);
while (i != map.end() && i->first == key) {
if (i->second == value) {
map.erase(i);
return;
}
i++;
}
assert(0);
}

template<class Key, class Value>
void ins(std::map<Key, Value> &map, const Key &key, const Value &value)
{
map.insert(std::pair<Key, Value>(key, value));
}

template<class Key, class Value>
void del(std::map<Key, Value> &map, const Key &key)
{
typename std::map<Key, Value>::iterator i = map.find(key);
assert(i != map.end());
map.erase(i);
}

template<class Key, class Value>
Value get(const std::map<Key, Value> &map, const Key &key)
{
typename std::map<Key, Value>::const_iterator i = map.find(key);
assert(i != map.end());
return i->second;
}

template<class Key, class Value>
Value &get(std::map<Key, Value> &map, const Key &key)
{
typename std::map<Key, Value>::iterator i = map.find(key);
assert(i != map.end());
return i->second;
}

std::string strf(const char *fmt, ...);
vec3 cross(const vec3 &a, const vec3 &b);

#endif
Something went wrong with that request. Please try again.