2013-07-28 22:08:34 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// An expression in our symbolic algebra system, used to write, linearize,
|
|
|
|
// and solve our constraint equations.
|
|
|
|
//
|
|
|
|
// Copyright 2008-2013 Jonathan Westhues.
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-04-08 12:54:53 +00:00
|
|
|
|
|
|
|
#ifndef __EXPR_H
|
|
|
|
#define __EXPR_H
|
|
|
|
|
|
|
|
class Expr;
|
|
|
|
|
|
|
|
class Expr {
|
|
|
|
public:
|
2008-04-21 10:12:04 +00:00
|
|
|
DWORD marker;
|
|
|
|
|
2008-04-08 12:54:53 +00:00
|
|
|
// A parameter, by the hParam handle
|
|
|
|
static const int PARAM = 0;
|
|
|
|
// A parameter, by a pointer straight in to the param table (faster,
|
|
|
|
// if we know that the param table won't move around)
|
|
|
|
static const int PARAM_PTR = 1;
|
|
|
|
|
2008-04-12 14:12:26 +00:00
|
|
|
// These are used only for user-entered expressions.
|
|
|
|
static const int POINT = 10;
|
|
|
|
static const int ENTITY = 11;
|
|
|
|
|
|
|
|
static const int CONSTANT = 20;
|
|
|
|
|
|
|
|
static const int PLUS = 100;
|
|
|
|
static const int MINUS = 101;
|
|
|
|
static const int TIMES = 102;
|
|
|
|
static const int DIV = 103;
|
|
|
|
static const int NEGATE = 104;
|
|
|
|
static const int SQRT = 105;
|
|
|
|
static const int SQUARE = 106;
|
|
|
|
static const int SIN = 107;
|
|
|
|
static const int COS = 108;
|
2009-01-08 17:22:59 +00:00
|
|
|
static const int ASIN = 109;
|
|
|
|
static const int ACOS = 110;
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-04-17 06:42:32 +00:00
|
|
|
// Special helpers for when we're parsing an expression from text.
|
|
|
|
// Initially, literals (like a constant number) appear in the same
|
|
|
|
// format as they will in the finished expression, but the operators
|
|
|
|
// are different until the parser fixes things up (and builds the
|
|
|
|
// tree from the flat list that the lexer outputs).
|
|
|
|
static const int ALL_RESOLVED = 1000;
|
|
|
|
static const int PAREN = 1001;
|
|
|
|
static const int BINARY_OP = 1002;
|
|
|
|
static const int UNARY_OP = 1003;
|
|
|
|
|
2008-04-08 12:54:53 +00:00
|
|
|
int op;
|
|
|
|
Expr *a;
|
|
|
|
Expr *b;
|
|
|
|
union {
|
2008-04-13 14:28:35 +00:00
|
|
|
double v;
|
2008-04-08 12:54:53 +00:00
|
|
|
hParam parh;
|
2008-04-13 14:28:35 +00:00
|
|
|
Param *parp;
|
2008-04-12 14:12:26 +00:00
|
|
|
hEntity entity;
|
2008-04-17 06:42:32 +00:00
|
|
|
|
|
|
|
// For use while parsing
|
|
|
|
char c;
|
2008-04-08 12:54:53 +00:00
|
|
|
} x;
|
2008-04-13 14:28:35 +00:00
|
|
|
|
2008-04-25 10:11:29 +00:00
|
|
|
static inline Expr *AllocExpr(void)
|
|
|
|
{ return (Expr *)AllocTemporary(sizeof(Expr)); }
|
|
|
|
|
2008-06-01 08:45:11 +00:00
|
|
|
static Expr *From(hParam p);
|
|
|
|
static Expr *From(double v);
|
2008-04-13 14:28:35 +00:00
|
|
|
|
|
|
|
Expr *AnyOp(int op, Expr *b);
|
|
|
|
inline Expr *Plus (Expr *b) { return AnyOp(PLUS, b); }
|
|
|
|
inline Expr *Minus(Expr *b) { return AnyOp(MINUS, b); }
|
|
|
|
inline Expr *Times(Expr *b) { return AnyOp(TIMES, b); }
|
|
|
|
inline Expr *Div (Expr *b) { return AnyOp(DIV, b); }
|
|
|
|
|
|
|
|
inline Expr *Negate(void) { return AnyOp(NEGATE, NULL); }
|
|
|
|
inline Expr *Sqrt (void) { return AnyOp(SQRT, NULL); }
|
|
|
|
inline Expr *Square(void) { return AnyOp(SQUARE, NULL); }
|
|
|
|
inline Expr *Sin (void) { return AnyOp(SIN, NULL); }
|
|
|
|
inline Expr *Cos (void) { return AnyOp(COS, NULL); }
|
2009-01-08 17:22:59 +00:00
|
|
|
inline Expr *ASin (void) { return AnyOp(ASIN, NULL); }
|
|
|
|
inline Expr *ACos (void) { return AnyOp(ACOS, NULL); }
|
2008-04-13 14:28:35 +00:00
|
|
|
|
|
|
|
Expr *PartialWrt(hParam p);
|
|
|
|
double Eval(void);
|
2009-04-19 03:55:46 +00:00
|
|
|
QWORD ParamsUsed(void);
|
|
|
|
bool DependsOn(hParam p);
|
2008-04-30 04:52:34 +00:00
|
|
|
static bool Tol(double a, double b);
|
|
|
|
Expr *FoldConstants(void);
|
2008-05-07 07:10:20 +00:00
|
|
|
void Substitute(hParam oldh, hParam newh);
|
2008-04-13 14:28:35 +00:00
|
|
|
|
2008-06-26 09:34:26 +00:00
|
|
|
static const hParam NO_PARAMS, MULTIPLE_PARAMS;
|
|
|
|
hParam ReferencedParams(ParamList *pl);
|
|
|
|
|
2008-04-13 14:28:35 +00:00
|
|
|
void ParamsToPointers(void);
|
2008-04-14 10:28:32 +00:00
|
|
|
|
|
|
|
void App(char *str, ...);
|
|
|
|
char *Print(void);
|
|
|
|
void PrintW(void); // worker
|
2008-04-17 06:42:32 +00:00
|
|
|
|
2008-04-18 07:06:37 +00:00
|
|
|
// number of child nodes: 0 (e.g. constant), 1 (sqrt), or 2 (+)
|
|
|
|
int Children(void);
|
2008-06-02 03:31:37 +00:00
|
|
|
// total number of nodes in the tree
|
|
|
|
int Nodes(void);
|
2008-04-17 06:42:32 +00:00
|
|
|
|
2008-06-14 08:43:38 +00:00
|
|
|
// Make a simple copy
|
|
|
|
Expr *DeepCopy(void);
|
2008-04-21 08:16:38 +00:00
|
|
|
// Make a copy, with the parameters (usually referenced by hParam)
|
|
|
|
// resolved to pointers to the actual value. This speeds things up
|
|
|
|
// considerably.
|
|
|
|
Expr *DeepCopyWithParamsAsPointers(IdList<Param,hParam> *firstTry,
|
|
|
|
IdList<Param,hParam> *thenTry);
|
|
|
|
|
2010-01-04 00:35:28 +00:00
|
|
|
static Expr *From(char *in, bool popUpError);
|
2008-04-17 06:42:32 +00:00
|
|
|
static void Lex(char *in);
|
|
|
|
static Expr *Next(void);
|
|
|
|
static void Consume(void);
|
|
|
|
|
|
|
|
static void PushOperator(Expr *e);
|
|
|
|
static Expr *PopOperator(void);
|
|
|
|
static Expr *TopOperator(void);
|
|
|
|
static void PushOperand(Expr *e);
|
|
|
|
static Expr *PopOperand(void);
|
|
|
|
|
|
|
|
static void Reduce(void);
|
|
|
|
static void ReduceAndPush(Expr *e);
|
|
|
|
static int Precedence(Expr *e);
|
|
|
|
|
|
|
|
static int Precedence(int op);
|
|
|
|
static void Parse(void);
|
2008-04-08 12:54:53 +00:00
|
|
|
};
|
|
|
|
|
2008-04-22 13:14:15 +00:00
|
|
|
class ExprVector {
|
|
|
|
public:
|
|
|
|
Expr *x, *y, *z;
|
|
|
|
|
2008-06-01 08:45:11 +00:00
|
|
|
static ExprVector From(Expr *x, Expr *y, Expr *z);
|
|
|
|
static ExprVector From(Vector vn);
|
|
|
|
static ExprVector From(hParam x, hParam y, hParam z);
|
2008-06-02 03:31:37 +00:00
|
|
|
static ExprVector From(double x, double y, double z);
|
2008-04-22 13:14:15 +00:00
|
|
|
|
|
|
|
ExprVector Plus(ExprVector b);
|
|
|
|
ExprVector Minus(ExprVector b);
|
|
|
|
Expr *Dot(ExprVector b);
|
2008-04-28 09:40:02 +00:00
|
|
|
ExprVector Cross(ExprVector b);
|
2008-04-22 13:14:15 +00:00
|
|
|
ExprVector ScaledBy(Expr *s);
|
2008-06-06 08:46:55 +00:00
|
|
|
ExprVector WithMagnitude(Expr *s);
|
2008-04-22 13:14:15 +00:00
|
|
|
Expr *Magnitude(void);
|
2008-05-09 05:33:23 +00:00
|
|
|
|
|
|
|
Vector Eval(void);
|
2008-04-22 13:14:15 +00:00
|
|
|
};
|
|
|
|
|
2008-05-05 06:18:01 +00:00
|
|
|
class ExprQuaternion {
|
|
|
|
public:
|
|
|
|
Expr *w, *vx, *vy, *vz;
|
|
|
|
|
2008-06-01 08:45:11 +00:00
|
|
|
static ExprQuaternion From(Expr *w, Expr *vx, Expr *vy, Expr *vz);
|
|
|
|
static ExprQuaternion From(Quaternion qn);
|
2008-06-01 08:57:16 +00:00
|
|
|
static ExprQuaternion From(hParam w, hParam vx, hParam vy, hParam vz);
|
2008-05-05 06:18:01 +00:00
|
|
|
|
|
|
|
ExprVector RotationU(void);
|
|
|
|
ExprVector RotationV(void);
|
2008-05-05 09:47:23 +00:00
|
|
|
ExprVector RotationN(void);
|
2008-05-08 07:30:30 +00:00
|
|
|
|
2008-05-11 06:09:46 +00:00
|
|
|
ExprVector Rotate(ExprVector p);
|
|
|
|
ExprQuaternion Times(ExprQuaternion b);
|
|
|
|
|
2008-05-08 07:30:30 +00:00
|
|
|
Expr *Magnitude(void);
|
2008-05-05 06:18:01 +00:00
|
|
|
};
|
|
|
|
|
2008-04-08 12:54:53 +00:00
|
|
|
#endif
|
2008-05-05 06:18:01 +00:00
|
|
|
|