一个以纯 C 语言实现的 Cassowary 约束求解算法。
Amoeba is a pure C implement of Cassowary algorithm. It:
Improvement relative to Kiwi:
This libary export a constraint solver interface, to solve a constraint problem, you should use it in steps:
am_newsolver()am_newvariable() binding the am_Num values.am_newconstraint().am_addterm() add a $a \times variable$ term to constraint equation items.am_setrelation() to specify the equal/greater/less sign in center of equation.am_addconstant() to add a number without variable.am_setstrength() to specify the priority of this constraint in all constraints.am_add().am_Num.am_sugguest().am_delsolver() to free al memory.below is a tiny example to demonstrate the steps:
…All functions below that returns int are returning error codes:
AM_OK: Operations success.AM_FAILED: The operation is failed, most caused by invalid arguments.AM_UNSATISFIED: Specific constraints added cannot satisfied.AM_UNBOUND: Failed to add constraints because there are unbound variables in itAmoeba supports to store whole state of solver into bianry data and to load back in new solver. Notice that the state of loaded solver will be earsed.
To perform a dump or load operations, you should prepare am_Dumper or
am_Loader to control the behavior in operations.
struct am_Dumper
A user-defined dump behavior control type. The user must provide reader
and var_name function pointers in it. there is a example:
…struct am_Loader
A user-defined dump behavior control type. The user must provide reader
and load_var function pointers in it. there is a example:
…am_Solver *am_newsolver(am_Allocf *allocf, void *ud);
Create a new solver with custom memory alloculator, pass NULL for use default ones.
void am_resetsolver(am_Solver *solver);
Remove all constraints from solver, all variables and constraints created are keeped.
void am_delsolver(am_Solver *solver);
Delete a solver and frees all memory it used, after that, all variables/constraints created from this solver are all freed.
void am_updatevars(am_Solver *solver);
Refresh variables' value into it's constrainted value.
You could use am_autoupdate() to update all variables automatically when solver changes.
void am_autoupdate(am_Solver *solver, int auto_update);
Set the auto update flags.
If setted, all variables will be updated to its latest values right after any changes done to the solver.
int am_dump(am_Solver *solver, am_Dumper *d);
Dump whole solver state (without edits) into binary data.
int am_load(am_Solver *solver, am_Loader *l);
Loads binary data back into solver. Note that all previous states are
discarded, but previous vars & constraints are keeped. i.e. the
am_resetsolver() called before loaded.
am_Id am_newvariable(am_Solver *solver, am_Num *pvalue);
Create a new variable with a value pvalue binding to it. Returns 0 on
error.
Notice that the pvalue pointer can be retrived in am_dump() or
am_varvalue(), so you could put context data here:
typedef struct MyVarContext {
am_Num value;
const char *name;
/* etc.. */
} MyVarContext;
MyVarContext *ctx = new_context();
am_Id var = am_newvariable(solver, &ctx->value);
/* retrive the context: */
MyVarContext *ctx = (MyVarContext*)am_varvalue(var, NULL);void am_delvariable(am_Solver *solver, am_Id var);
Remove this variable, after that, it cannot be added into further constraints.
void am_refcount(am_Solver *solver, am_Id var);
Retrieve the refcount of the variable.
void am_varvalue(am_Solver *solver, am_Id var, am_Num *newvalue);
Retrieve or reset the value binding of the variable. if newvalue is NULL,
Only retrieve the old value, otherwise the newvalue will be setted into
var and original value returned.
int am_add(am_Constraint *cons);
Add constraint into the solver it's created from.
int am_hasconstraint(am_Constraint *cons);
Check whether a constraint has been added into the solver or not.
void am_remove(am_Constraint *cons);
Remove added constraint.
int am_clearedits(am_Solver *solver);
Remove all variable suggests from solver.
int am_hasedit(am_Solver *solver, am_Id var);
Check whether a variable has suggested value in the solver.
int am_addedit(am_Solver *solver, am_Id var, am_Num strength);
Prepare to change the value of variables or the strength value if the variable is in edit now.
void am_suggest(am_Solver *solver, am_Id var, am_Num value);
Actually change the value of the variable var.
After changed, other variable may changed due to the constraints in solver.
If you do not want change the strength of suggest (default is AM_MEDIUM), you may call am_suggest() directly without am_addedit().
void am_deledit(am_Solver *solver, am_Id var);
Cancel the modify of variable.
The value binding by var will restore to the referred value according the solver.
am_Constraint *am_newconstraint(am_Solver *solver, am_Num strength);
Create a new constraints. Notice that where are one pointer's space can be
used to store context data in the head of am_Constraint, example:
am_Constraint *cons = am_newconstraint(solver, AM_REQUIRED);
MyContext *ctx = new_my_context();
ctx->mydata = 1;
*(MyContext**)cons = ctx;am_Constraint *am_cloneconstraint(am_Constraint *other, am_Num strength);
Clone a new constraint from the existing one with new strength.
void am_resetconstraint(am_Constraint *cons);
Remove all terms and variables from the constraint, restore it to blank state.
void am_delconstraint(am_Constraint *cons);
Free the constraint. if it's added into solver, remove it first.
int am_addterm(am_Constraint *cons, am_Id var, am_Num multiplier);
Add a term into constraint.
Example: a constraint like 2*m == x+y, has terms 2*m, 1*x and 1*y.
So to makeup this constraint, you could:
// 2*m == x + y
am_addterm(c, m, 2.0); // 2*m
am_setrelation(c, AM_EQUAL); // =
am_addterm(c, x, 1.0); // x
am_addterm(c, y, 1.0); // yint am_setrelation(am_Constraint *cons, int relation);
Set the relations of constraint, could be one of these:
AM_EQUALAM_GREATEQUALAM_LESSEQUALThe terms added before am_setrelation() become the left hand terms of constraints, and the terms adds after call will become the right hand terms of constraints.
int am_addconstant(am_Constraint *cons, am_Num constant);
Add a constant without variable into constraint as a term.
int am_setstrength(am_Constraint *cons, am_Num strength);
Set the strength of a constraint.
int am_mergeconstraint(am_Constraint *cons, const am_Constraint *other, am_Num multiplier);
Merge other constraints into cons, with a multiplier multiples with other.
暂无开放 Issues,或尚未同步最近议题。