2008-03-25 10:02:13 +00:00
|
|
|
|
|
|
|
#ifndef __DSC_H
|
|
|
|
#define __DSC_H
|
|
|
|
|
|
|
|
typedef unsigned long DWORD;
|
|
|
|
typedef unsigned char BYTE;
|
|
|
|
|
2008-03-28 10:00:37 +00:00
|
|
|
class Vector {
|
|
|
|
public:
|
2008-03-25 10:02:13 +00:00
|
|
|
double x, y, z;
|
2008-04-09 08:39:01 +00:00
|
|
|
|
|
|
|
static Vector MakeFrom(double x, double y, double z);
|
2008-03-27 09:53:51 +00:00
|
|
|
|
2008-04-01 10:48:44 +00:00
|
|
|
Vector Plus(Vector b);
|
|
|
|
Vector Minus(Vector b);
|
|
|
|
Vector Negated(void);
|
2008-03-27 09:53:51 +00:00
|
|
|
Vector Cross(Vector b);
|
2008-04-01 10:48:44 +00:00
|
|
|
double Dot(Vector b);
|
|
|
|
Vector Normal(int which);
|
2008-03-27 09:53:51 +00:00
|
|
|
Vector RotatedAbout(Vector axis, double theta);
|
|
|
|
double Magnitude(void);
|
2008-04-12 14:12:26 +00:00
|
|
|
Vector ScaledBy(double s);
|
2008-03-27 09:53:51 +00:00
|
|
|
|
2008-04-09 08:39:01 +00:00
|
|
|
// Call a rotation matrix [ u' v' n' ]'; this returns the first and
|
|
|
|
// second rows, where that matrix is generated by the given quaternion
|
|
|
|
// a + bi + cj + dk
|
|
|
|
static Vector RotationU(double a, double b, double c, double d);
|
|
|
|
static Vector RotationV(double a, double b, double c, double d);
|
2008-03-28 10:00:37 +00:00
|
|
|
};
|
2008-03-25 10:02:13 +00:00
|
|
|
|
2008-03-28 10:00:37 +00:00
|
|
|
class Point2d {
|
|
|
|
public:
|
2008-03-27 09:53:51 +00:00
|
|
|
double x, y;
|
2008-04-12 14:12:26 +00:00
|
|
|
|
|
|
|
Point2d Plus(Point2d b);
|
|
|
|
Point2d Minus(Point2d b);
|
|
|
|
Point2d ScaledBy(double s);
|
|
|
|
double DistanceTo(Point2d p);
|
|
|
|
double DistanceToLine(Point2d p0, Point2d dp, bool segment);
|
2008-03-28 10:00:37 +00:00
|
|
|
};
|
2008-03-27 09:53:51 +00:00
|
|
|
|
2008-03-28 10:00:37 +00:00
|
|
|
template <class T, class H>
|
|
|
|
class IdList {
|
|
|
|
public:
|
2008-03-26 09:18:12 +00:00
|
|
|
typedef struct {
|
2008-04-09 08:39:01 +00:00
|
|
|
T t;
|
2008-03-26 09:18:12 +00:00
|
|
|
int tag;
|
|
|
|
} Elem;
|
|
|
|
|
2008-03-28 10:00:37 +00:00
|
|
|
Elem *elem;
|
2008-03-26 09:18:12 +00:00
|
|
|
int elems;
|
|
|
|
int elemsAllocated;
|
|
|
|
|
2008-04-09 08:39:01 +00:00
|
|
|
H AddAndAssignId(T *t) {
|
2008-03-28 10:00:37 +00:00
|
|
|
int i;
|
2008-04-09 08:39:01 +00:00
|
|
|
DWORD id = 0;
|
2008-03-28 10:00:37 +00:00
|
|
|
|
|
|
|
for(i = 0; i < elems; i++) {
|
2008-04-09 08:39:01 +00:00
|
|
|
id = max(id, elem[i].t.h.v);
|
2008-03-28 10:00:37 +00:00
|
|
|
}
|
|
|
|
|
2008-04-09 08:39:01 +00:00
|
|
|
t->h.v = (id + 1);
|
|
|
|
Add(t);
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-04-09 08:39:01 +00:00
|
|
|
return t->h;
|
2008-03-28 10:00:37 +00:00
|
|
|
}
|
|
|
|
|
2008-04-09 08:39:01 +00:00
|
|
|
void Add(T *t) {
|
2008-03-28 10:00:37 +00:00
|
|
|
if(elems >= elemsAllocated) {
|
|
|
|
elemsAllocated = (elemsAllocated + 32)*2;
|
|
|
|
elem = (Elem *)realloc(elem, elemsAllocated*sizeof(elem[0]));
|
|
|
|
if(!elem) oops();
|
|
|
|
}
|
|
|
|
|
2008-04-09 08:39:01 +00:00
|
|
|
elem[elems].t = *t;
|
2008-03-28 10:00:37 +00:00
|
|
|
elem[elems].tag = 0;
|
|
|
|
elems++;
|
|
|
|
}
|
2008-04-08 12:54:53 +00:00
|
|
|
|
|
|
|
T *FindById(H h) {
|
2008-04-13 10:57:41 +00:00
|
|
|
T *t = FindByIdNoOops(h);
|
|
|
|
if(!t) {
|
|
|
|
dbp("failed to look up item %16lx, searched %d items", h.v, elems);
|
|
|
|
oops();
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
T *FindByIdNoOops(H h) {
|
2008-04-08 12:54:53 +00:00
|
|
|
int i;
|
|
|
|
for(i = 0; i < elems; i++) {
|
2008-04-09 08:39:01 +00:00
|
|
|
if(elem[i].t.h.v == h.v) {
|
|
|
|
return &(elem[i].t);
|
2008-04-08 12:54:53 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-13 10:57:41 +00:00
|
|
|
return NULL;
|
2008-04-08 12:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClearTags(void) {
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < elems; i++) {
|
|
|
|
elem[i].tag = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-03-28 10:00:37 +00:00
|
|
|
|
|
|
|
void RemoveTagged(void) {
|
|
|
|
int src, dest;
|
|
|
|
dest = 0;
|
|
|
|
for(src = 0; src < elems; src++) {
|
|
|
|
if(elem[src].tag) {
|
|
|
|
// this item should be deleted
|
|
|
|
} else {
|
|
|
|
if(src != dest) {
|
|
|
|
elem[dest] = elem[src];
|
|
|
|
}
|
|
|
|
dest++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elems = dest;
|
2008-04-08 12:54:53 +00:00
|
|
|
// and elemsAllocated is untouched, because we didn't resize
|
2008-03-28 10:00:37 +00:00
|
|
|
}
|
|
|
|
|
2008-04-13 10:57:41 +00:00
|
|
|
void MoveSelfInto(IdList<T,H> *l) {
|
|
|
|
memcpy(l, this, sizeof(*this));
|
|
|
|
elemsAllocated = elems = 0;
|
|
|
|
elem = NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-28 10:00:37 +00:00
|
|
|
void Clear(void) {
|
|
|
|
elemsAllocated = elems = 0;
|
|
|
|
if(elem) free(elem);
|
2008-04-12 16:28:48 +00:00
|
|
|
elem = NULL;
|
2008-03-28 10:00:37 +00:00
|
|
|
}
|
2008-03-26 09:18:12 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2008-04-08 12:54:53 +00:00
|
|
|
class NameStr {
|
|
|
|
public:
|
|
|
|
char str[20];
|
|
|
|
|
|
|
|
inline void strcpy(char *in) {
|
|
|
|
memcpy(str, in, min(strlen(in)+1, sizeof(str)));
|
|
|
|
str[sizeof(str)-1] = '\0';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-03-25 10:02:13 +00:00
|
|
|
#endif
|