2008-03-25 10:02:13 +00:00
|
|
|
|
|
|
|
#ifndef __SOLVESPACE_H
|
|
|
|
#define __SOLVESPACE_H
|
|
|
|
|
2008-03-28 10:00:37 +00:00
|
|
|
// Debugging functions
|
|
|
|
#define oops() do { dbp("oops at line %d, file %s", __LINE__, __FILE__); \
|
2008-04-27 09:03:01 +00:00
|
|
|
if(0) *(char *)0 = 1; exit(-1); } while(0)
|
2008-03-28 10:00:37 +00:00
|
|
|
#ifndef min
|
|
|
|
#define min(x, y) ((x) < (y) ? (x) : (y))
|
|
|
|
#endif
|
|
|
|
#ifndef max
|
|
|
|
#define max(x, y) ((x) > (y) ? (x) : (y))
|
|
|
|
#endif
|
|
|
|
|
2008-05-11 10:40:37 +00:00
|
|
|
#define SWAP(T, a, b) do { T temp = (a); (a) = (b); (b) = temp; } while(0)
|
|
|
|
|
2008-04-17 06:42:32 +00:00
|
|
|
#define isforname(c) (isalnum(c) || (c) == '_' || (c) == '-' || (c) == '#')
|
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
typedef signed long SDWORD;
|
|
|
|
|
2008-03-25 10:02:13 +00:00
|
|
|
#include <stdlib.h>
|
2008-04-17 06:42:32 +00:00
|
|
|
#include <ctype.h>
|
2008-03-25 10:02:13 +00:00
|
|
|
#include <string.h>
|
2008-03-26 09:18:12 +00:00
|
|
|
#include <stdio.h>
|
2008-03-27 09:53:51 +00:00
|
|
|
#include <math.h>
|
2008-04-01 10:48:44 +00:00
|
|
|
#include <windows.h> // required for GL stuff
|
|
|
|
#include <gl/gl.h>
|
|
|
|
#include <gl/glu.h>
|
|
|
|
|
2008-04-14 10:28:32 +00:00
|
|
|
class Expr;
|
2008-04-22 13:14:15 +00:00
|
|
|
class ExprVector;
|
2008-05-05 06:18:01 +00:00
|
|
|
class ExprQuaternion;
|
2008-04-14 10:28:32 +00:00
|
|
|
|
|
|
|
// From the platform-specific code.
|
2008-04-18 11:11:48 +00:00
|
|
|
int SaveFileYesNoCancel(void);
|
|
|
|
BOOL GetSaveFile(char *file, char *defExtension, char *selPattern);
|
|
|
|
BOOL GetOpenFile(char *file, char *defExtension, char *selPattern);
|
|
|
|
|
2008-04-14 10:28:32 +00:00
|
|
|
void CheckMenuById(int id, BOOL checked);
|
2008-04-18 07:06:37 +00:00
|
|
|
void EnableMenuById(int id, BOOL checked);
|
2008-04-18 11:11:48 +00:00
|
|
|
|
2008-04-21 10:12:04 +00:00
|
|
|
void ShowGraphicsEditControl(int x, int y, char *s);
|
|
|
|
void HideGraphicsEditControl(void);
|
|
|
|
BOOL GraphicsEditControlIsVisible(void);
|
|
|
|
|
2008-04-27 05:00:12 +00:00
|
|
|
void ShowTextWindow(BOOL visible);
|
2008-04-14 10:28:32 +00:00
|
|
|
void InvalidateText(void);
|
2008-04-27 05:00:12 +00:00
|
|
|
void InvalidateGraphics(void);
|
2008-04-18 11:11:48 +00:00
|
|
|
void PaintGraphics(void);
|
2008-04-27 05:00:12 +00:00
|
|
|
SDWORD GetMilliseconds(void);
|
2008-04-18 11:11:48 +00:00
|
|
|
|
2008-04-14 10:28:32 +00:00
|
|
|
void dbp(char *str, ...);
|
|
|
|
void Error(char *str, ...);
|
2008-04-18 11:11:48 +00:00
|
|
|
|
2008-04-25 10:11:29 +00:00
|
|
|
void *AllocTemporary(int n);
|
|
|
|
void FreeAllTemporary(void);
|
2008-04-18 07:06:37 +00:00
|
|
|
void *MemRealloc(void *p, int n);
|
|
|
|
void *MemAlloc(int n);
|
|
|
|
void MemFree(void *p);
|
2008-05-07 04:17:29 +00:00
|
|
|
void vl(void); // debug function to validate
|
2008-04-14 10:28:32 +00:00
|
|
|
|
|
|
|
|
2008-03-25 10:02:13 +00:00
|
|
|
#include "dsc.h"
|
2008-04-23 07:29:19 +00:00
|
|
|
#include "polygon.h"
|
2008-03-26 09:18:12 +00:00
|
|
|
#include "sketch.h"
|
2008-04-12 14:12:26 +00:00
|
|
|
#include "ui.h"
|
2008-04-08 12:54:53 +00:00
|
|
|
#include "expr.h"
|
2008-03-25 10:02:13 +00:00
|
|
|
|
2008-03-27 09:53:51 +00:00
|
|
|
|
2008-04-11 11:13:47 +00:00
|
|
|
// Utility functions that are provided in the platform-independent code.
|
|
|
|
void glxVertex3v(Vector u);
|
2008-04-25 07:04:09 +00:00
|
|
|
void glxFillPolygon(SPolygon *p);
|
2008-05-05 09:47:23 +00:00
|
|
|
void glxMarkPolygonNormal(SPolygon *p);
|
2008-04-11 11:13:47 +00:00
|
|
|
void glxWriteText(char *str);
|
2008-04-30 08:14:32 +00:00
|
|
|
void glxWriteTextRefCenter(char *str);
|
2008-05-07 08:19:37 +00:00
|
|
|
double glxStrWidth(char *str);
|
|
|
|
double glxStrHeight(void);
|
2008-04-11 11:13:47 +00:00
|
|
|
void glxTranslatev(Vector u);
|
2008-04-27 03:26:27 +00:00
|
|
|
void glxOntoWorkplane(Vector u, Vector v);
|
2008-04-19 11:09:47 +00:00
|
|
|
void glxLockColorTo(double r, double g, double b);
|
|
|
|
void glxUnlockColor(void);
|
2008-04-25 07:04:09 +00:00
|
|
|
void glxColor3d(double r, double g, double b);
|
|
|
|
void glxColor4d(double r, double g, double b, double a);
|
2008-03-27 09:53:51 +00:00
|
|
|
|
2008-03-25 10:02:13 +00:00
|
|
|
|
2008-03-26 09:18:12 +00:00
|
|
|
#define arraylen(x) (sizeof((x))/sizeof((x)[0]))
|
2008-03-27 09:53:51 +00:00
|
|
|
#define PI (3.1415926535897931)
|
|
|
|
void MakeMatrix(double *mat, double a11, double a12, double a13, double a14,
|
|
|
|
double a21, double a22, double a23, double a24,
|
|
|
|
double a31, double a32, double a33, double a34,
|
|
|
|
double a41, double a42, double a43, double a44);
|
|
|
|
|
2008-04-20 11:35:10 +00:00
|
|
|
class System {
|
|
|
|
public:
|
|
|
|
#define MAX_UNKNOWNS 200
|
|
|
|
|
|
|
|
IdList<Entity,hEntity> entity;
|
|
|
|
IdList<Param,hParam> param;
|
|
|
|
IdList<Equation,hEquation> eq;
|
|
|
|
|
|
|
|
// In general, the tag indicates the subsys that a variable/equation
|
2008-05-07 07:10:20 +00:00
|
|
|
// has been assigned to; these are exceptions for variables:
|
|
|
|
static const int VAR_ASSUMED = 10000;
|
|
|
|
static const int VAR_SUBSTITUTED = 10001;
|
|
|
|
// and for equations:
|
|
|
|
static const int EQ_SUBSTITUTED = 20000;
|
2008-04-20 11:35:10 +00:00
|
|
|
|
2008-04-21 01:26:36 +00:00
|
|
|
// The system Jacobian matrix
|
2008-04-20 11:35:10 +00:00
|
|
|
struct {
|
2008-04-21 01:26:36 +00:00
|
|
|
// The corresponding equation for each row
|
|
|
|
hEquation eq[MAX_UNKNOWNS];
|
2008-04-20 11:35:10 +00:00
|
|
|
|
2008-04-21 01:26:36 +00:00
|
|
|
// The corresponding parameter for each column
|
|
|
|
hParam param[MAX_UNKNOWNS];
|
2008-05-01 06:25:38 +00:00
|
|
|
|
2008-04-21 01:26:36 +00:00
|
|
|
bool bound[MAX_UNKNOWNS];
|
2008-04-20 11:35:10 +00:00
|
|
|
|
2008-04-21 01:26:36 +00:00
|
|
|
// We're solving AX = B
|
2008-04-20 11:35:10 +00:00
|
|
|
int m, n;
|
2008-04-21 01:26:36 +00:00
|
|
|
struct {
|
|
|
|
Expr *sym[MAX_UNKNOWNS][MAX_UNKNOWNS];
|
|
|
|
double num[MAX_UNKNOWNS][MAX_UNKNOWNS];
|
|
|
|
} A;
|
2008-05-01 06:25:38 +00:00
|
|
|
|
2008-05-12 07:29:50 +00:00
|
|
|
// Some helpers for the least squares solve
|
|
|
|
double AAt[MAX_UNKNOWNS][MAX_UNKNOWNS];
|
|
|
|
double Z[MAX_UNKNOWNS];
|
|
|
|
|
2008-04-21 01:26:36 +00:00
|
|
|
double X[MAX_UNKNOWNS];
|
2008-05-01 06:25:38 +00:00
|
|
|
|
2008-04-21 01:26:36 +00:00
|
|
|
struct {
|
|
|
|
Expr *sym[MAX_UNKNOWNS];
|
|
|
|
double num[MAX_UNKNOWNS];
|
|
|
|
} B;
|
|
|
|
} mat;
|
2008-04-20 11:35:10 +00:00
|
|
|
|
|
|
|
bool Tol(double v);
|
2008-05-12 07:29:50 +00:00
|
|
|
int GaussJordan(void);
|
|
|
|
static bool SolveLinearSystem(double X[], double A[][MAX_UNKNOWNS],
|
|
|
|
double B[], int N);
|
|
|
|
bool SolveLeastSquares(void);
|
2008-04-20 11:35:10 +00:00
|
|
|
|
|
|
|
void WriteJacobian(int eqTag, int paramTag);
|
|
|
|
void EvalJacobian(void);
|
|
|
|
|
2008-05-07 07:10:20 +00:00
|
|
|
void SolveBySubstitution(void);
|
|
|
|
|
|
|
|
static bool IsDragged(hParam p);
|
2008-05-01 06:25:38 +00:00
|
|
|
|
2008-04-20 11:35:10 +00:00
|
|
|
bool NewtonSolve(int tag);
|
|
|
|
bool Solve(void);
|
|
|
|
};
|
|
|
|
|
2008-03-26 09:18:12 +00:00
|
|
|
|
2008-03-28 10:00:37 +00:00
|
|
|
class SolveSpace {
|
|
|
|
public:
|
2008-03-26 09:18:12 +00:00
|
|
|
TextWindow TW;
|
|
|
|
GraphicsWindow GW;
|
|
|
|
|
2008-04-14 10:28:32 +00:00
|
|
|
// These lists define the sketch, and are edited by the user.
|
|
|
|
IdList<Group,hGroup> group;
|
|
|
|
IdList<Request,hRequest> request;
|
|
|
|
IdList<Constraint,hConstraint> constraint;
|
|
|
|
|
|
|
|
// These lists are generated automatically when we solve the sketch.
|
|
|
|
IdList<Entity,hEntity> entity;
|
|
|
|
IdList<Param,hParam> param;
|
|
|
|
|
|
|
|
inline Constraint *GetConstraint(hConstraint h)
|
|
|
|
{ return constraint.FindById(h); }
|
|
|
|
inline Request *GetRequest(hRequest h) { return request.FindById(h); }
|
|
|
|
inline Entity *GetEntity (hEntity h) { return entity. FindById(h); }
|
|
|
|
inline Param *GetParam (hParam h) { return param. FindById(h); }
|
2008-04-20 11:35:10 +00:00
|
|
|
inline Group *GetGroup (hGroup h) { return group. FindById(h); }
|
2008-04-13 14:28:35 +00:00
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
hGroup activeGroup;
|
|
|
|
|
|
|
|
FILE *fh;
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-04-24 06:22:16 +00:00
|
|
|
void Init(char *cmdLine);
|
2008-04-20 11:35:10 +00:00
|
|
|
|
2008-04-24 06:22:16 +00:00
|
|
|
char saveFile[MAX_PATH];
|
|
|
|
bool unsaved;
|
|
|
|
typedef struct {
|
|
|
|
char type;
|
|
|
|
char *desc;
|
|
|
|
char fmt;
|
|
|
|
void *ptr;
|
|
|
|
} SaveTable;
|
|
|
|
static const SaveTable SAVED[];
|
|
|
|
void SaveUsingTable(int type);
|
2008-04-27 10:01:23 +00:00
|
|
|
void LoadUsingTable(char *key, char *val);
|
2008-04-24 06:22:16 +00:00
|
|
|
struct {
|
|
|
|
Group g;
|
|
|
|
Request r;
|
|
|
|
Entity e;
|
|
|
|
Param p;
|
|
|
|
Constraint c;
|
|
|
|
} sv;
|
2008-04-18 11:11:48 +00:00
|
|
|
static void MenuFile(int id);
|
2008-04-24 06:22:16 +00:00
|
|
|
void NewFile(void);
|
2008-04-18 11:11:48 +00:00
|
|
|
bool SaveToFile(char *filename);
|
|
|
|
bool LoadFromFile(char *filename);
|
2008-04-20 11:35:10 +00:00
|
|
|
|
2008-04-27 09:03:01 +00:00
|
|
|
|
|
|
|
void GenerateAll(bool andSolve);
|
|
|
|
bool SolveGroup(hGroup hg);
|
|
|
|
void ForceReferences(void);
|
|
|
|
|
2008-04-20 11:35:10 +00:00
|
|
|
// The system to be solved.
|
|
|
|
System sys;
|
2008-03-28 10:00:37 +00:00
|
|
|
};
|
2008-03-25 10:02:13 +00:00
|
|
|
|
|
|
|
extern SolveSpace SS;
|
|
|
|
|
|
|
|
#endif
|