2013-07-28 22:08:34 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Data structures used frequently in the program, various kinds of vectors
|
|
|
|
// (of real numbers, not symbolic algebra stuff) and our templated lists.
|
|
|
|
//
|
|
|
|
// Copyright 2008-2013 Jonathan Westhues.
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-03-25 10:02:13 +00:00
|
|
|
#ifndef __DSC_H
|
|
|
|
#define __DSC_H
|
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
class Vector;
|
2009-04-14 04:19:23 +00:00
|
|
|
class Vector4;
|
2008-05-08 07:30:30 +00:00
|
|
|
class Point2d;
|
|
|
|
class hEntity;
|
2008-06-01 08:29:59 +00:00
|
|
|
class hParam;
|
2008-04-18 11:11:48 +00:00
|
|
|
|
|
|
|
class Quaternion {
|
|
|
|
public:
|
2008-05-05 06:18:01 +00:00
|
|
|
// a + (vx)*i + (vy)*j + (vz)*k
|
|
|
|
double w, vx, vy, vz;
|
2008-04-18 11:11:48 +00:00
|
|
|
|
2009-03-15 23:04:45 +00:00
|
|
|
static const Quaternion IDENTITY;
|
|
|
|
|
2008-06-01 08:45:11 +00:00
|
|
|
static Quaternion From(double w, double vx, double vy, double vz);
|
2008-06-02 03:31:37 +00:00
|
|
|
static Quaternion From(hParam w, hParam vx, hParam vy, hParam vz);
|
2008-06-01 08:45:11 +00:00
|
|
|
static Quaternion From(Vector u, Vector v);
|
2009-04-29 02:42:44 +00:00
|
|
|
static Quaternion From(Vector axis, double dtheta);
|
2008-04-18 11:11:48 +00:00
|
|
|
|
2008-05-05 06:18:01 +00:00
|
|
|
Quaternion Plus(Quaternion b);
|
|
|
|
Quaternion Minus(Quaternion b);
|
2008-04-18 11:11:48 +00:00
|
|
|
Quaternion ScaledBy(double s);
|
|
|
|
double Magnitude(void);
|
|
|
|
Quaternion WithMagnitude(double s);
|
|
|
|
|
|
|
|
// Call a rotation matrix [ u' v' n' ]'; this returns the first and
|
|
|
|
// second rows, where that matrix is generated by this quaternion
|
|
|
|
Vector RotationU(void);
|
|
|
|
Vector RotationV(void);
|
2008-05-05 06:18:01 +00:00
|
|
|
Vector RotationN(void);
|
2008-05-11 06:09:46 +00:00
|
|
|
Vector Rotate(Vector p);
|
|
|
|
|
2008-05-14 14:23:58 +00:00
|
|
|
Quaternion ToThe(double p);
|
|
|
|
Quaternion Inverse(void);
|
2008-05-11 06:09:46 +00:00
|
|
|
Quaternion Times(Quaternion b);
|
2009-12-15 12:26:22 +00:00
|
|
|
Quaternion Mirror(void);
|
2008-04-18 11:11:48 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2008-06-01 08:45:11 +00:00
|
|
|
static Vector From(double x, double y, double z);
|
|
|
|
static Vector From(hParam x, hParam y, hParam z);
|
2008-05-19 09:23:49 +00:00
|
|
|
static Vector AtIntersectionOfPlanes(Vector n1, double d1,
|
|
|
|
Vector n2, double d2);
|
2009-01-03 12:27:33 +00:00
|
|
|
static Vector AtIntersectionOfLines(Vector a0, Vector a1,
|
|
|
|
Vector b0, Vector b1,
|
2009-01-21 05:04:38 +00:00
|
|
|
bool *skew,
|
|
|
|
double *pa=NULL, double *pb=NULL);
|
2009-02-23 10:06:02 +00:00
|
|
|
static Vector AtIntersectionOfPlaneAndLine(Vector n, double d,
|
|
|
|
Vector p0, Vector p1,
|
|
|
|
bool *parallel);
|
2009-02-27 13:04:36 +00:00
|
|
|
static Vector AtIntersectionOfPlanes(Vector na, double da,
|
|
|
|
Vector nb, double db,
|
2009-03-15 23:04:45 +00:00
|
|
|
Vector nc, double dc, bool *parallel);
|
2009-07-07 08:21:59 +00:00
|
|
|
static void ClosestPointBetweenLines(Vector pa, Vector da,
|
|
|
|
Vector pb, Vector db,
|
|
|
|
double *ta, double *tb);
|
2008-03-27 09:53:51 +00:00
|
|
|
|
2008-07-06 07:56:24 +00:00
|
|
|
double Element(int i);
|
2008-09-09 05:19:54 +00:00
|
|
|
bool Equals(Vector v, double tol=LENGTH_EPS);
|
2008-07-06 07:56:24 +00:00
|
|
|
bool EqualsExactly(Vector v);
|
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);
|
2009-06-22 06:22:30 +00:00
|
|
|
double DirectionCosineWith(Vector b);
|
2008-04-01 10:48:44 +00:00
|
|
|
double Dot(Vector b);
|
|
|
|
Vector Normal(int which);
|
2008-06-06 11:35:28 +00:00
|
|
|
Vector RotatedAbout(Vector orig, Vector axis, double theta);
|
2008-03-27 09:53:51 +00:00
|
|
|
Vector RotatedAbout(Vector axis, double theta);
|
2008-06-21 10:18:20 +00:00
|
|
|
Vector DotInToCsys(Vector u, Vector v, Vector n);
|
|
|
|
Vector ScaleOutOfCsys(Vector u, Vector v, Vector n);
|
2008-06-06 11:35:28 +00:00
|
|
|
double DistanceToLine(Vector p0, Vector dp);
|
2009-06-21 09:02:36 +00:00
|
|
|
bool OnLineSegment(Vector a, Vector b, double tol=LENGTH_EPS);
|
2008-06-14 11:16:14 +00:00
|
|
|
Vector ClosestPointOnLine(Vector p0, Vector dp);
|
2008-03-27 09:53:51 +00:00
|
|
|
double Magnitude(void);
|
2008-07-06 07:56:24 +00:00
|
|
|
double MagSquared(void);
|
2008-04-14 10:28:32 +00:00
|
|
|
Vector WithMagnitude(double s);
|
2008-04-12 14:12:26 +00:00
|
|
|
Vector ScaledBy(double s);
|
2008-05-08 07:30:30 +00:00
|
|
|
Vector ProjectInto(hEntity wrkpl);
|
2008-05-17 11:15:14 +00:00
|
|
|
Vector ProjectVectorInto(hEntity wrkpl);
|
2008-05-08 08:12:23 +00:00
|
|
|
double DivPivoting(Vector delta);
|
2008-05-11 10:40:37 +00:00
|
|
|
Vector ClosestOrtho(void);
|
2009-01-21 05:04:38 +00:00
|
|
|
void MakeMaxMin(Vector *maxv, Vector *minv);
|
2010-07-21 05:04:03 +00:00
|
|
|
Vector ClampWithin(double minv, double maxv);
|
2009-01-27 07:59:58 +00:00
|
|
|
static bool BoundingBoxesDisjoint(Vector amax, Vector amin,
|
|
|
|
Vector bmax, Vector bmin);
|
2009-03-08 10:59:57 +00:00
|
|
|
static bool BoundingBoxIntersectsLine(Vector amax, Vector amin,
|
|
|
|
Vector p0, Vector p1, bool segment);
|
2009-01-21 05:04:38 +00:00
|
|
|
bool OutsideAndNotOn(Vector maxv, Vector minv);
|
2009-03-17 16:33:46 +00:00
|
|
|
Vector InPerspective(Vector u, Vector v, Vector n,
|
|
|
|
Vector origin, double cameraTan);
|
2008-05-12 10:01:44 +00:00
|
|
|
Point2d Project2d(Vector u, Vector v);
|
2009-02-01 05:13:43 +00:00
|
|
|
Point2d ProjectXy(void);
|
2009-04-14 04:19:23 +00:00
|
|
|
Vector4 Project4d(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
class Vector4 {
|
|
|
|
public:
|
|
|
|
double w, x, y, z;
|
|
|
|
|
|
|
|
static Vector4 From(double w, double x, double y, double z);
|
|
|
|
static Vector4 From(double w, Vector v3);
|
|
|
|
static Vector4 Blend(Vector4 a, Vector4 b, double t);
|
|
|
|
|
|
|
|
Vector4 Plus(Vector4 b);
|
|
|
|
Vector4 Minus(Vector4 b);
|
|
|
|
Vector4 ScaledBy(double s);
|
|
|
|
Vector PerspectiveProject(void);
|
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
|
|
|
|
2009-03-14 20:01:20 +00:00
|
|
|
static Point2d From(double x, double y);
|
|
|
|
|
2008-04-12 14:12:26 +00:00
|
|
|
Point2d Plus(Point2d b);
|
|
|
|
Point2d Minus(Point2d b);
|
|
|
|
Point2d ScaledBy(double s);
|
2009-03-19 17:40:11 +00:00
|
|
|
double DivPivoting(Point2d delta);
|
2009-02-01 05:13:43 +00:00
|
|
|
double Dot(Point2d p);
|
2008-04-12 14:12:26 +00:00
|
|
|
double DistanceTo(Point2d p);
|
|
|
|
double DistanceToLine(Point2d p0, Point2d dp, bool segment);
|
2008-04-22 05:00:49 +00:00
|
|
|
double Magnitude(void);
|
2009-02-23 10:06:02 +00:00
|
|
|
double MagSquared(void);
|
2008-04-22 05:00:49 +00:00
|
|
|
Point2d WithMagnitude(double v);
|
2009-02-01 05:13:43 +00:00
|
|
|
Point2d Normal(void);
|
2009-02-23 10:06:02 +00:00
|
|
|
bool Equals(Point2d v, double tol=LENGTH_EPS);
|
2008-03-28 10:00:37 +00:00
|
|
|
};
|
2008-03-27 09:53:51 +00:00
|
|
|
|
2009-01-13 06:56:05 +00:00
|
|
|
// A simple list
|
|
|
|
template <class T>
|
|
|
|
class List {
|
|
|
|
public:
|
|
|
|
T *elem;
|
|
|
|
int n;
|
|
|
|
int elemsAllocated;
|
|
|
|
|
2009-06-27 05:53:56 +00:00
|
|
|
void AllocForOneMore(void) {
|
2009-01-13 06:56:05 +00:00
|
|
|
if(n >= elemsAllocated) {
|
|
|
|
elemsAllocated = (elemsAllocated + 32)*2;
|
2013-10-19 05:36:45 +00:00
|
|
|
elem = (T *)MemRealloc(elem, (size_t)elemsAllocated*sizeof(elem[0]));
|
2009-01-13 06:56:05 +00:00
|
|
|
}
|
2009-06-27 05:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Add(T *t) {
|
|
|
|
AllocForOneMore();
|
2009-01-13 06:56:05 +00:00
|
|
|
elem[n++] = *t;
|
|
|
|
}
|
|
|
|
|
2009-06-27 05:53:56 +00:00
|
|
|
void AddToBeginning(T *t) {
|
|
|
|
AllocForOneMore();
|
|
|
|
memmove(elem+1, elem, n*sizeof(elem[0]));
|
|
|
|
n++;
|
|
|
|
elem[0] = *t;
|
|
|
|
}
|
|
|
|
|
2009-01-19 10:37:10 +00:00
|
|
|
T *First(void) {
|
|
|
|
return (n == 0) ? NULL : &(elem[0]);
|
|
|
|
}
|
|
|
|
T *NextAfter(T *prev) {
|
|
|
|
if(!prev) return NULL;
|
|
|
|
if(prev - elem == (n - 1)) return NULL;
|
|
|
|
return prev + 1;
|
|
|
|
}
|
|
|
|
|
2009-01-13 06:56:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2009-01-19 03:33:15 +00:00
|
|
|
|
2009-10-29 07:16:28 +00:00
|
|
|
void RemoveLast(int cnt) {
|
|
|
|
if(n < cnt) oops();
|
|
|
|
n -= cnt;
|
|
|
|
// and elemsAllocated is untouched, same as in RemoveTagged
|
|
|
|
}
|
|
|
|
|
2009-01-19 03:33:15 +00:00
|
|
|
void Reverse(void) {
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < (n/2); i++) {
|
|
|
|
SWAP(T, elem[i], elem[(n-1)-i]);
|
|
|
|
}
|
|
|
|
}
|
2009-01-13 06:56:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// A list, where each element has an integer identifier. The list is kept
|
|
|
|
// sorted by that identifier, and items can be looked up in log n time by
|
|
|
|
// id.
|
2008-03-28 10:00:37 +00:00
|
|
|
template <class T, class H>
|
|
|
|
class IdList {
|
|
|
|
public:
|
2008-04-18 07:21:17 +00:00
|
|
|
T *elem;
|
2008-04-18 11:11:48 +00:00
|
|
|
int n;
|
2008-04-18 07:21:17 +00:00
|
|
|
int elemsAllocated;
|
2008-03-26 09:18:12 +00:00
|
|
|
|
Use C99 integer types and C++ boolean types/values
This change comprehensively replaces the use of Microsoft-standard integer
and boolean types with their C99/C++ standard equivalents, as the latter is
more appropriate for a cross-platform application. With matter-of-course
exceptions in the Win32-specific code, the types/values have been converted
as follows:
QWORD --> uint64_t
SQWORD --> int64_t
DWORD --> uint32_t
SDWORD --> int32_t
WORD --> uint16_t
SWORD --> int16_t
BYTE --> uint8_t
BOOL --> bool
TRUE --> true
FALSE --> false
The following related changes are also included:
* Added C99 integer type definitions for Windows, as stdint.h is not
available prior to Visual Studio 2010
* Changed types of some variables in the SolveSpace class from 'int' to
'bool', as they actually represent boolean settings
* Implemented new Cnf{Freeze,Thaw}Bool() functions to support boolean
variables in the Registry
* Cnf{Freeze,Thaw}DWORD() are now Cnf{Freeze,Thaw}Int()
* TtfFont::Get{WORD,DWORD}() are now TtfFont::Get{USHORT,ULONG}() (names
inspired by the OpenType spec)
* RGB colors are packed into an integer of type uint32_t (nee DWORD), but
in a few places, these were represented by an int; these have been
corrected to uint32_t
2013-10-02 05:45:13 +00:00
|
|
|
uint32_t MaximumId(void) {
|
|
|
|
uint32_t id = 0;
|
2008-03-28 10:00:37 +00:00
|
|
|
|
2009-09-18 08:14:15 +00:00
|
|
|
int i;
|
2008-04-18 11:11:48 +00:00
|
|
|
for(i = 0; i < n; i++) {
|
2008-04-18 07:21:17 +00:00
|
|
|
id = max(id, elem[i].h.v);
|
2008-03-28 10:00:37 +00:00
|
|
|
}
|
2009-09-18 08:14:15 +00:00
|
|
|
return id;
|
|
|
|
}
|
2008-03-28 10:00:37 +00:00
|
|
|
|
2009-09-18 08:14:15 +00:00
|
|
|
H AddAndAssignId(T *t) {
|
|
|
|
t->h.v = (MaximumId() + 1);
|
2008-04-09 08:39:01 +00:00
|
|
|
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-04-18 11:11:48 +00:00
|
|
|
if(n >= elemsAllocated) {
|
2008-03-28 10:00:37 +00:00
|
|
|
elemsAllocated = (elemsAllocated + 32)*2;
|
2013-10-19 05:36:45 +00:00
|
|
|
elem = (T *)MemRealloc(elem, (size_t)elemsAllocated*sizeof(elem[0]));
|
2008-03-28 10:00:37 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 04:36:33 +00:00
|
|
|
int first = 0, last = n;
|
|
|
|
// We know that we must insert within the closed interval [first,last]
|
|
|
|
while(first != last) {
|
|
|
|
int mid = (first + last)/2;
|
|
|
|
H hm = elem[mid].h;
|
|
|
|
if(hm.v > t->h.v) {
|
|
|
|
last = mid;
|
|
|
|
} else if(hm.v < t->h.v) {
|
|
|
|
first = mid + 1;
|
|
|
|
} else {
|
2009-04-20 07:30:09 +00:00
|
|
|
dbp("can't insert in list; is handle %d not unique?", t->h.v);
|
2008-06-12 04:36:33 +00:00
|
|
|
oops();
|
2008-06-06 07:50:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-12 04:36:33 +00:00
|
|
|
int i = first;
|
|
|
|
|
2013-10-19 05:36:45 +00:00
|
|
|
memmove(elem+i+1, elem+i, (size_t)(n-i)*sizeof(elem[0]));
|
2008-06-06 07:50:08 +00:00
|
|
|
elem[i] = *t;
|
2008-04-18 11:11:48 +00:00
|
|
|
n++;
|
2008-03-28 10:00:37 +00:00
|
|
|
}
|
2008-06-12 04:36:33 +00:00
|
|
|
|
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) {
|
2008-04-19 11:09:47 +00:00
|
|
|
dbp("failed to look up item %08x, searched %d items", h.v, n);
|
2008-04-13 10:57:41 +00:00
|
|
|
oops();
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
T *FindByIdNoOops(H h) {
|
2008-06-06 07:50:08 +00:00
|
|
|
int first = 0, last = n-1;
|
|
|
|
while(first <= last) {
|
|
|
|
int mid = (first + last)/2;
|
|
|
|
H hm = elem[mid].h;
|
|
|
|
if(hm.v > h.v) {
|
|
|
|
last = mid-1; // and first stays the same
|
|
|
|
} else if(hm.v < h.v) {
|
|
|
|
first = mid+1; // and last stays the same
|
|
|
|
} else {
|
|
|
|
return &(elem[mid]);
|
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
|
|
|
}
|
|
|
|
|
2009-01-19 10:37:10 +00:00
|
|
|
T *First(void) {
|
|
|
|
return (n == 0) ? NULL : &(elem[0]);
|
|
|
|
}
|
|
|
|
T *NextAfter(T *prev) {
|
|
|
|
if(!prev) return NULL;
|
|
|
|
if(prev - elem == (n - 1)) return NULL;
|
|
|
|
return prev + 1;
|
|
|
|
}
|
|
|
|
|
2008-04-08 12:54:53 +00:00
|
|
|
void ClearTags(void) {
|
|
|
|
int i;
|
2008-04-18 11:11:48 +00:00
|
|
|
for(i = 0; i < n; i++) {
|
2008-04-08 12:54:53 +00:00
|
|
|
elem[i].tag = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-03-28 10:00:37 +00:00
|
|
|
|
2008-04-14 10:28:32 +00:00
|
|
|
void Tag(H h, int tag) {
|
|
|
|
int i;
|
2008-04-18 11:11:48 +00:00
|
|
|
for(i = 0; i < n; i++) {
|
2008-04-18 07:21:17 +00:00
|
|
|
if(elem[i].h.v == h.v) {
|
2008-04-14 10:28:32 +00:00
|
|
|
elem[i].tag = tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-28 10:00:37 +00:00
|
|
|
void RemoveTagged(void) {
|
|
|
|
int src, dest;
|
|
|
|
dest = 0;
|
2008-04-18 11:11:48 +00:00
|
|
|
for(src = 0; src < n; src++) {
|
2008-03-28 10:00:37 +00:00
|
|
|
if(elem[src].tag) {
|
|
|
|
// this item should be deleted
|
|
|
|
} else {
|
|
|
|
if(src != dest) {
|
|
|
|
elem[dest] = elem[src];
|
|
|
|
}
|
|
|
|
dest++;
|
|
|
|
}
|
|
|
|
}
|
2008-04-18 11:11:48 +00:00
|
|
|
n = 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-05-17 06:04:55 +00:00
|
|
|
void RemoveById(H h) {
|
|
|
|
ClearTags();
|
|
|
|
FindById(h)->tag = 1;
|
|
|
|
RemoveTagged();
|
|
|
|
}
|
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));
|
2008-04-18 11:11:48 +00:00
|
|
|
elemsAllocated = n = 0;
|
2008-04-13 10:57:41 +00:00
|
|
|
elem = NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-21 08:16:38 +00:00
|
|
|
void DeepCopyInto(IdList<T,H> *l) {
|
|
|
|
l->elem = (T *)MemAlloc(elemsAllocated * sizeof(elem[0]));
|
|
|
|
memcpy(l->elem, elem, elemsAllocated * sizeof(elem[0]));
|
|
|
|
l->elemsAllocated = elemsAllocated;
|
|
|
|
l->n = n;
|
|
|
|
}
|
|
|
|
|
2008-03-28 10:00:37 +00:00
|
|
|
void Clear(void) {
|
2013-09-19 04:33:12 +00:00
|
|
|
for(int i = 0; i < n; i++) {
|
|
|
|
elem[i].Clear();
|
|
|
|
}
|
2008-04-18 11:11:48 +00:00
|
|
|
elemsAllocated = n = 0;
|
2008-05-07 04:17:29 +00:00
|
|
|
if(elem) MemFree(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:
|
2008-06-12 08:58:58 +00:00
|
|
|
char str[64];
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2013-08-26 18:58:35 +00:00
|
|
|
inline void strcpy(const char *in) {
|
2008-04-08 12:54:53 +00:00
|
|
|
memcpy(str, in, min(strlen(in)+1, sizeof(str)));
|
|
|
|
str[sizeof(str)-1] = '\0';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-21 04:46:01 +00:00
|
|
|
class BandedMatrix {
|
|
|
|
public:
|
2013-09-09 19:50:32 +00:00
|
|
|
enum {
|
|
|
|
MAX_UNKNOWNS = 16,
|
|
|
|
RIGHT_OF_DIAG = 1,
|
|
|
|
LEFT_OF_DIAG = 2
|
|
|
|
};
|
2009-10-21 04:46:01 +00:00
|
|
|
|
|
|
|
double A[MAX_UNKNOWNS][MAX_UNKNOWNS];
|
|
|
|
double B[MAX_UNKNOWNS];
|
|
|
|
double X[MAX_UNKNOWNS];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
void Solve(void);
|
|
|
|
};
|
|
|
|
|
Replaced RGB-color integers with dedicated data structure
RGB colors were represented using a uint32_t with the red, green and blue
values stuffed into the lower three octets (i.e. 0x00BBGGRR), like
Microsoft's COLORREF. This approach did not lend itself to type safety,
however, so this change replaces it with an RgbColor class that provides
the same infomation plus a handful of useful methods to work with it. (Note
that sizeof(RgbColor) == sizeof(uint32_t), so this change should not lead
to memory bloat.)
Some of the new methods/fields replace what were previously macro calls;
e.g. RED(c) is now c.red, REDf(c) is now c.redF(). The .Equals() method is
now used instead of == to compare colors.
RGB colors still need to be represented as packed integers in file I/O and
preferences, so the methods .FromPackedInt() and .ToPackedInt() are
provided. Also implemented are Cnf{Freeze,Thaw}Color(), type-safe wrappers
around Cnf{Freeze,Thaw}Int() that facilitate I/O with preferences.
(Cnf{Freeze,Thaw}Color() are defined outside of the system-dependent code
to minimize the footprint of the latter; because the same can be done with
Cnf{Freeze,Thaw}Bool(), those are also moved out of the system code with
this commit.)
Color integers were being OR'ed with 0x80000000 in some places for two
distinct purposes: One, to indicate use of a default color in
glxFillMesh(); this has been replaced by use of the .UseDefault() method.
Two, to indicate to TextWindow::Printf() that the format argument of a
"%Bp"/"%Fp" specifier is an RGB color rather than a color "code" from
TextWindow::bgColors[] or TextWindow::fgColors[] (as the specifier can
accept either); instead, we define a new flag "z" (as in "%Bz" or "%Fz") to
indicate an RGBcolor pointer, leaving "%Bp"/"%Fp" to indicate a color code
exclusively.
(This also allows TextWindow::meta[][].bg to be a char instead of an int,
partly compensating for the new .bgRgb field added immediately after.)
In array declarations, RGB colors could previously be specified as 0 (often
in a terminating element). As that no longer works, we define NULL_COLOR,
which serves much the same purpose for RgbColor variables as NULL serves
for pointers.
2013-10-16 20:00:58 +00:00
|
|
|
#undef RGB
|
|
|
|
#define RGB(r, g, b) RgbColor::From((r), (g), (b))
|
|
|
|
#define RGBf(r, g, b) RgbColor::FromFloat((float)(r), (float)(g), (float)(b))
|
|
|
|
#define NULL_COLOR RgbColor::Default()
|
|
|
|
|
|
|
|
// Note: sizeof(class RgbColor) should be exactly 4
|
|
|
|
//
|
|
|
|
class RgbColor {
|
|
|
|
uint8_t useDefault;
|
|
|
|
public:
|
|
|
|
uint8_t red, green, blue;
|
|
|
|
|
|
|
|
float redF(void) const { return (float)red / 255.0f; }
|
|
|
|
float greenF(void) const { return (float)green / 255.0f; }
|
|
|
|
float blueF(void) const { return (float)blue / 255.0f; }
|
|
|
|
|
|
|
|
bool UseDefault(void) const { return useDefault != 0; }
|
|
|
|
|
|
|
|
bool Equals(RgbColor c) const {
|
|
|
|
switch(c.useDefault + useDefault) {
|
|
|
|
case 0: return
|
|
|
|
c.red == red &&
|
|
|
|
c.green == green &&
|
|
|
|
c.blue == blue;
|
|
|
|
case 1: return false;
|
|
|
|
case 2: return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ToPackedInt(void) const {
|
|
|
|
return red | (uint32_t)(green << 8) | (uint32_t)(blue << 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
static RgbColor Default(void) {
|
|
|
|
RgbColor c;
|
|
|
|
c.useDefault = 1;
|
|
|
|
// Leave r, g, b uninitialized so that Valgrind will notice
|
|
|
|
// if they are used inadvertently
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RgbColor From(int r, int g, int b) {
|
|
|
|
RgbColor c;
|
|
|
|
c.useDefault = 0;
|
|
|
|
c.red = (uint8_t)r;
|
|
|
|
c.green = (uint8_t)g;
|
|
|
|
c.blue = (uint8_t)b;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RgbColor FromFloat(float r, float g, float b) {
|
|
|
|
return From(
|
|
|
|
(int)(255.1f * r),
|
|
|
|
(int)(255.1f * g),
|
|
|
|
(int)(255.1f * b));
|
|
|
|
}
|
|
|
|
|
|
|
|
static RgbColor FromPackedInt(uint32_t bgr) {
|
|
|
|
return From(
|
|
|
|
(int)((bgr) & 0xff),
|
|
|
|
(int)((bgr >> 8) & 0xff),
|
|
|
|
(int)((bgr >> 16) & 0xff));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-03-25 10:02:13 +00:00
|
|
|
#endif
|