2008-04-23 07:29:19 +00:00
|
|
|
|
|
|
|
#ifndef __POLYGON_H
|
|
|
|
#define __POLYGON_H
|
|
|
|
|
2008-04-25 07:04:09 +00:00
|
|
|
class SPolygon;
|
2008-05-22 10:28:28 +00:00
|
|
|
class SMesh;
|
2008-05-24 10:34:06 +00:00
|
|
|
class SBsp3;
|
2008-04-25 07:04:09 +00:00
|
|
|
|
2008-04-24 06:22:16 +00:00
|
|
|
template <class T>
|
|
|
|
class SList {
|
2008-04-23 07:29:19 +00:00
|
|
|
public:
|
2008-04-24 06:22:16 +00:00
|
|
|
T *elem;
|
|
|
|
int n;
|
|
|
|
int elemsAllocated;
|
2008-04-25 07:04:09 +00:00
|
|
|
|
|
|
|
void Add(T *t) {
|
|
|
|
if(n >= elemsAllocated) {
|
|
|
|
elemsAllocated = (elemsAllocated + 32)*2;
|
|
|
|
elem = (T *)MemRealloc(elem, elemsAllocated*sizeof(elem[0]));
|
|
|
|
}
|
|
|
|
elem[n++] = *t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearTags(void) {
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; i++) {
|
|
|
|
elem[i].tag = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear(void) {
|
|
|
|
if(elem) MemFree(elem);
|
|
|
|
elem = NULL;
|
|
|
|
n = elemsAllocated = 0;
|
|
|
|
}
|
2008-05-24 23:10:00 +00:00
|
|
|
|
|
|
|
void RemoveTagged(void) {
|
|
|
|
int src, dest;
|
|
|
|
dest = 0;
|
|
|
|
for(src = 0; src < n; src++) {
|
|
|
|
if(elem[src].tag) {
|
|
|
|
// this item should be deleted
|
|
|
|
} else {
|
|
|
|
if(src != dest) {
|
|
|
|
elem[dest] = elem[src];
|
|
|
|
}
|
|
|
|
dest++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n = dest;
|
|
|
|
// and elemsAllocated is untouched, because we didn't resize
|
|
|
|
}
|
2008-04-23 07:29:19 +00:00
|
|
|
};
|
|
|
|
|
2008-04-24 06:22:16 +00:00
|
|
|
class SEdge {
|
2008-04-23 07:29:19 +00:00
|
|
|
public:
|
2008-04-25 07:04:09 +00:00
|
|
|
int tag;
|
2008-04-24 06:22:16 +00:00
|
|
|
Vector a, b;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SEdgeList {
|
|
|
|
public:
|
|
|
|
SList<SEdge> l;
|
|
|
|
|
2008-05-22 10:28:28 +00:00
|
|
|
void Clear(void);
|
|
|
|
void AddEdge(Vector a, Vector b);
|
2008-04-25 07:04:09 +00:00
|
|
|
bool AssemblePolygon(SPolygon *dest, SEdge *errorAt);
|
|
|
|
};
|
|
|
|
|
|
|
|
class SPoint {
|
|
|
|
public:
|
|
|
|
int tag;
|
|
|
|
Vector p;
|
2008-04-23 07:29:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SContour {
|
|
|
|
public:
|
2008-04-25 07:04:09 +00:00
|
|
|
SList<SPoint> l;
|
2008-05-02 10:54:22 +00:00
|
|
|
|
|
|
|
void MakeEdgesInto(SEdgeList *el);
|
2008-05-05 09:47:23 +00:00
|
|
|
void Reverse(void);
|
|
|
|
Vector ComputeNormal(void);
|
|
|
|
bool IsClockwiseProjdToNormal(Vector n);
|
|
|
|
bool ContainsPointProjdToNormal(Vector n, Vector p);
|
2008-04-23 07:29:19 +00:00
|
|
|
};
|
|
|
|
|
2008-04-24 06:22:16 +00:00
|
|
|
class SPolygon {
|
|
|
|
public:
|
|
|
|
SList<SContour> l;
|
2008-05-05 09:47:23 +00:00
|
|
|
Vector normal;
|
2008-04-25 07:04:09 +00:00
|
|
|
|
2008-05-05 09:47:23 +00:00
|
|
|
Vector ComputeNormal(void);
|
2008-04-25 07:04:09 +00:00
|
|
|
void AddEmptyContour(void);
|
|
|
|
void AddPoint(Vector p);
|
2008-05-19 09:23:49 +00:00
|
|
|
bool ContainsPoint(Vector p);
|
2008-05-02 10:54:22 +00:00
|
|
|
void MakeEdgesInto(SEdgeList *el);
|
2008-05-05 09:47:23 +00:00
|
|
|
void FixContourDirections(void);
|
2008-05-22 10:28:28 +00:00
|
|
|
void TriangulateInto(SMesh *m);
|
2008-04-25 07:04:09 +00:00
|
|
|
void Clear(void);
|
2008-05-21 03:58:14 +00:00
|
|
|
};
|
2008-05-19 09:23:49 +00:00
|
|
|
|
2008-05-21 03:58:14 +00:00
|
|
|
class STriangle {
|
|
|
|
public:
|
2008-05-22 10:28:28 +00:00
|
|
|
int tag;
|
|
|
|
Vector a, b, c;
|
2008-05-23 10:05:07 +00:00
|
|
|
|
|
|
|
Vector Normal(void);
|
2008-05-24 10:34:06 +00:00
|
|
|
bool ContainsPoint(Vector p);
|
2008-05-21 03:58:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SBsp2 {
|
2008-05-22 10:28:28 +00:00
|
|
|
public:
|
2008-05-24 10:34:06 +00:00
|
|
|
Vector np; // normal to the plane
|
|
|
|
|
|
|
|
Vector no; // outer normal to the edge
|
|
|
|
double d;
|
2008-05-21 03:58:14 +00:00
|
|
|
SEdge edge;
|
2008-05-19 09:23:49 +00:00
|
|
|
|
2008-05-22 10:28:28 +00:00
|
|
|
SBsp2 *pos;
|
|
|
|
SBsp2 *neg;
|
|
|
|
|
|
|
|
SBsp2 *more;
|
2008-05-23 10:05:07 +00:00
|
|
|
|
2008-05-24 10:34:06 +00:00
|
|
|
static const int POS = 100, NEG = 101, COPLANAR = 200;
|
2008-05-24 23:10:00 +00:00
|
|
|
void InsertTriangleHow(int how, STriangle *tr, SMesh *m, SBsp3 *bsp3);
|
|
|
|
void InsertTriangle(STriangle *tr, SMesh *m, SBsp3 *bsp3);
|
2008-05-24 10:34:06 +00:00
|
|
|
Vector IntersectionWith(Vector a, Vector b);
|
|
|
|
SBsp2 *InsertEdge(SEdge *nedge, Vector nnp, Vector out);
|
2008-05-23 10:05:07 +00:00
|
|
|
static SBsp2 *Alloc(void);
|
2008-05-24 10:34:06 +00:00
|
|
|
|
|
|
|
void DebugDraw(Vector n, double d);
|
2008-04-24 06:22:16 +00:00
|
|
|
};
|
|
|
|
|
2008-05-21 03:58:14 +00:00
|
|
|
class SBsp3 {
|
2008-04-23 07:29:19 +00:00
|
|
|
public:
|
2008-05-23 10:05:07 +00:00
|
|
|
Vector n;
|
|
|
|
double d;
|
|
|
|
|
2008-05-21 03:58:14 +00:00
|
|
|
STriangle tri;
|
|
|
|
SBsp3 *pos;
|
|
|
|
SBsp3 *neg;
|
2008-05-19 09:23:49 +00:00
|
|
|
|
2008-05-21 03:58:14 +00:00
|
|
|
SBsp3 *more;
|
|
|
|
|
|
|
|
SBsp2 *edges;
|
2008-05-23 10:05:07 +00:00
|
|
|
|
|
|
|
static SBsp3 *Alloc(void);
|
|
|
|
static SBsp3 *FromMesh(SMesh *m);
|
|
|
|
|
|
|
|
Vector IntersectionWith(Vector a, Vector b);
|
|
|
|
|
|
|
|
static const int POS = 100, NEG = 101, COPLANAR = 200;
|
2008-05-24 23:10:00 +00:00
|
|
|
void InsertHow(int how, STriangle *str, SMesh *instead);
|
|
|
|
SBsp3 *Insert(STriangle *str, SMesh *instead);
|
2008-05-23 10:05:07 +00:00
|
|
|
|
2008-05-24 23:10:00 +00:00
|
|
|
void InsertConvexHow(int how, Vector *vertex, int n, SMesh *instead);
|
|
|
|
SBsp3 *InsertConvex(Vector *vertex, int n, SMesh *instead);
|
|
|
|
|
|
|
|
void InsertInPlane(bool pos2, STriangle *tr, SMesh *m);
|
2008-05-24 10:34:06 +00:00
|
|
|
|
2008-05-23 10:05:07 +00:00
|
|
|
void DebugDraw(void);
|
2008-05-21 03:58:14 +00:00
|
|
|
};
|
2008-05-19 09:23:49 +00:00
|
|
|
|
2008-05-21 03:58:14 +00:00
|
|
|
|
|
|
|
class SMesh {
|
|
|
|
public:
|
|
|
|
SList<STriangle> l;
|
2008-05-22 10:28:28 +00:00
|
|
|
|
2008-05-24 23:10:00 +00:00
|
|
|
bool flipNormal;
|
|
|
|
bool keepCoplanar;
|
|
|
|
bool atLeastOneDiscarded;
|
|
|
|
|
2008-05-22 10:28:28 +00:00
|
|
|
void Clear(void);
|
2008-05-24 12:23:25 +00:00
|
|
|
void AddTriangle(STriangle *st);
|
2008-05-22 10:28:28 +00:00
|
|
|
void AddTriangle(Vector a, Vector b, Vector c);
|
2008-05-23 10:05:07 +00:00
|
|
|
void AddTriangle(Vector n, Vector a, Vector b, Vector c);
|
2008-05-24 12:23:25 +00:00
|
|
|
void DoBounding(Vector v, Vector *vmax, Vector *vmin);
|
|
|
|
void GetBounding(Vector *vmax, Vector *vmin);
|
2008-05-24 23:10:00 +00:00
|
|
|
|
|
|
|
void AddAgainstBsp(SMesh *srcm, SBsp3 *bsp3);
|
2008-05-25 13:11:44 +00:00
|
|
|
void MakeFromUnion(SMesh *a, SMesh *b);
|
|
|
|
void MakeFromDifference(SMesh *a, SMesh *b);
|
2008-04-23 07:29:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2008-04-24 06:22:16 +00:00
|
|
|
|