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 {
|
|
|
|
public:
|
2008-04-21 10:12:04 +00:00
|
|
|
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 08:31:20 +00:00
|
|
|
enum class Op : uint32_t {
|
2013-09-09 19:50:32 +00:00
|
|
|
// A parameter, by the hParam handle
|
|
|
|
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)
|
|
|
|
PARAM_PTR = 1,
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2016-11-19 04:27:37 +00:00
|
|
|
// Operands
|
2013-09-09 19:50:32 +00:00
|
|
|
CONSTANT = 20,
|
2016-11-19 04:27:37 +00:00
|
|
|
VARIABLE = 21,
|
2008-04-12 14:12:26 +00:00
|
|
|
|
2016-11-19 04:27:37 +00:00
|
|
|
// Binary ops
|
2013-09-09 19:50:32 +00:00
|
|
|
PLUS = 100,
|
|
|
|
MINUS = 101,
|
|
|
|
TIMES = 102,
|
|
|
|
DIV = 103,
|
2016-11-19 04:27:37 +00:00
|
|
|
// Unary ops
|
2013-09-09 19:50:32 +00:00
|
|
|
NEGATE = 104,
|
|
|
|
SQRT = 105,
|
|
|
|
SQUARE = 106,
|
|
|
|
SIN = 107,
|
|
|
|
COS = 108,
|
|
|
|
ASIN = 109,
|
|
|
|
ACOS = 110,
|
|
|
|
};
|
2008-04-17 06:42:32 +00:00
|
|
|
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 08:31:20 +00:00
|
|
|
Op op;
|
2008-04-08 12:54:53 +00:00
|
|
|
Expr *a;
|
|
|
|
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;
|
2016-01-09 11:36:32 +00:00
|
|
|
Expr *b;
|
|
|
|
};
|
2008-04-13 14:28:35 +00:00
|
|
|
|
2015-10-14 16:26:19 +00:00
|
|
|
Expr() { }
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 08:31:20 +00:00
|
|
|
Expr(double val) : op(Op::CONSTANT) { v = val; }
|
2015-11-06 08:40:12 +00:00
|
|
|
|
2016-05-05 05:54:05 +00:00
|
|
|
static inline Expr *AllocExpr()
|
2008-04-25 10:11:29 +00:00
|
|
|
{ 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
|
|
|
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 08:31:20 +00:00
|
|
|
Expr *AnyOp(Op op, Expr *b);
|
|
|
|
inline Expr *Plus (Expr *b_) { return AnyOp(Op::PLUS, b_); }
|
|
|
|
inline Expr *Minus(Expr *b_) { return AnyOp(Op::MINUS, b_); }
|
|
|
|
inline Expr *Times(Expr *b_) { return AnyOp(Op::TIMES, b_); }
|
|
|
|
inline Expr *Div (Expr *b_) { return AnyOp(Op::DIV, b_); }
|
|
|
|
|
|
|
|
inline Expr *Negate() { return AnyOp(Op::NEGATE, NULL); }
|
|
|
|
inline Expr *Sqrt () { return AnyOp(Op::SQRT, NULL); }
|
|
|
|
inline Expr *Square() { return AnyOp(Op::SQUARE, NULL); }
|
|
|
|
inline Expr *Sin () { return AnyOp(Op::SIN, NULL); }
|
|
|
|
inline Expr *Cos () { return AnyOp(Op::COS, NULL); }
|
|
|
|
inline Expr *ASin () { return AnyOp(Op::ASIN, NULL); }
|
|
|
|
inline Expr *ACos () { return AnyOp(Op::ACOS, NULL); }
|
2008-04-13 14:28:35 +00:00
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Expr *PartialWrt(hParam p) const;
|
|
|
|
double Eval() const;
|
|
|
|
uint64_t ParamsUsed() const;
|
|
|
|
bool DependsOn(hParam p) const;
|
2008-04-30 04:52:34 +00:00
|
|
|
static bool Tol(double a, double b);
|
2016-05-05 05:54:05 +00:00
|
|
|
Expr *FoldConstants();
|
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;
|
2016-05-21 05:18:00 +00:00
|
|
|
hParam ReferencedParams(ParamList *pl) const;
|
2008-06-26 09:34:26 +00:00
|
|
|
|
2016-05-05 05:54:05 +00:00
|
|
|
void ParamsToPointers();
|
2008-04-14 10:28:32 +00:00
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
std::string Print() const;
|
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 (+)
|
2016-05-21 05:18:00 +00:00
|
|
|
int Children() const;
|
2008-06-02 03:31:37 +00:00
|
|
|
// total number of nodes in the tree
|
2016-05-21 05:18:00 +00:00
|
|
|
int Nodes() const;
|
2008-04-17 06:42:32 +00:00
|
|
|
|
2008-06-14 08:43:38 +00:00
|
|
|
// Make a simple copy
|
2016-05-21 05:18:00 +00:00
|
|
|
Expr *DeepCopy() const;
|
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,
|
2016-11-19 04:27:37 +00:00
|
|
|
IdList<Param,hParam> *thenTry) const;
|
2008-04-21 08:16:38 +00:00
|
|
|
|
2013-09-16 19:51:20 +00:00
|
|
|
static Expr *From(const char *in, bool popUpError);
|
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
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
ExprVector Plus(ExprVector b) const;
|
|
|
|
ExprVector Minus(ExprVector b) const;
|
|
|
|
Expr *Dot(ExprVector b) const;
|
|
|
|
ExprVector Cross(ExprVector b) const;
|
|
|
|
ExprVector ScaledBy(Expr *s) const;
|
|
|
|
ExprVector WithMagnitude(Expr *s) const;
|
|
|
|
Expr *Magnitude() const;
|
2008-05-09 05:33:23 +00:00
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Eval() const;
|
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
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
ExprVector RotationU() const;
|
|
|
|
ExprVector RotationV() const;
|
|
|
|
ExprVector RotationN() const;
|
2008-05-08 07:30:30 +00:00
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
ExprVector Rotate(ExprVector p) const;
|
|
|
|
ExprQuaternion Times(ExprQuaternion b) const;
|
2008-05-11 06:09:46 +00:00
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Expr *Magnitude() const;
|
2008-05-05 06:18:01 +00:00
|
|
|
};
|
2008-04-08 12:54:53 +00:00
|
|
|
#endif
|