forked from AuroraMiddleware/gtk
Use explicit values for constraint strength
Instead of playing games with mapping negative symbolic values to positive ones, let's use the appropriate constants everywhere. This allows us to use: GTK_CONSTRAINT_STRENGTH_WEAK * 2 Or GTK_CONSTRAINT_STRENGTH_STRONG + 1 In code using the public API. We also store the strength values as integers, so we can compare them properly, and only turn them into doubles when they are inserted into the solver, just like every other variable.
This commit is contained in:
parent
48e6cd4255
commit
4dd1de4129
@ -292,7 +292,7 @@ gtk_constraint_class_init (GtkConstraintClass *klass)
|
||||
g_param_spec_int ("strength",
|
||||
P_("Strength"),
|
||||
P_("The strength of the constraint"),
|
||||
GTK_CONSTRAINT_STRENGTH_WEAK, G_MAXINT,
|
||||
0, GTK_CONSTRAINT_STRENGTH_REQUIRED,
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_STATIC_STRINGS |
|
||||
@ -521,42 +521,6 @@ gtk_constraint_get_strength (GtkConstraint *constraint)
|
||||
return constraint->strength;
|
||||
}
|
||||
|
||||
/*< private >
|
||||
* gtk_constraint_get_weight:
|
||||
* @constraint: a #GtkConstraint
|
||||
*
|
||||
* Computes the weight of the @constraint to be used with
|
||||
* #GtkConstraintSolver.
|
||||
*
|
||||
* Returns: the weight of the constraint
|
||||
*/
|
||||
double
|
||||
gtk_constraint_get_weight (GtkConstraint *constraint)
|
||||
{
|
||||
if (constraint->strength > 0)
|
||||
return constraint->strength;
|
||||
|
||||
switch (constraint->strength)
|
||||
{
|
||||
case GTK_CONSTRAINT_STRENGTH_REQUIRED:
|
||||
return GTK_CONSTRAINT_WEIGHT_REQUIRED;
|
||||
|
||||
case GTK_CONSTRAINT_STRENGTH_STRONG:
|
||||
return GTK_CONSTRAINT_WEIGHT_STRONG;
|
||||
|
||||
case GTK_CONSTRAINT_STRENGTH_MEDIUM:
|
||||
return GTK_CONSTRAINT_WEIGHT_MEDIUM;
|
||||
|
||||
case GTK_CONSTRAINT_STRENGTH_WEAK:
|
||||
return GTK_CONSTRAINT_WEIGHT_WEAK;
|
||||
|
||||
default:
|
||||
g_assert_not_reached ();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* gtk_constraint_is_required:
|
||||
* @constraint: a #GtkConstraint
|
||||
|
@ -158,7 +158,7 @@ gtk_constraint_guide_update_constraint (GtkConstraintGuide *guide,
|
||||
var,
|
||||
GTK_CONSTRAINT_RELATION_GE,
|
||||
gtk_constraint_expression_new (guide->values[index]),
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
else if ((index == NAT_WIDTH && guide->values[MIN_WIDTH] != guide->values[MAX_WIDTH]) ||
|
||||
(index == NAT_HEIGHT && guide->values[MIN_HEIGHT] != guide->values[MAX_HEIGHT]))
|
||||
@ -177,7 +177,7 @@ gtk_constraint_guide_update_constraint (GtkConstraintGuide *guide,
|
||||
var,
|
||||
GTK_CONSTRAINT_RELATION_LE,
|
||||
gtk_constraint_expression_new (guide->values[index]),
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
|
||||
gtk_layout_manager_layout_changed (GTK_LAYOUT_MANAGER (guide->layout));
|
||||
|
@ -246,7 +246,7 @@ gtk_constraint_layout_get_attribute (GtkConstraintLayout *layout,
|
||||
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
res, GTK_CONSTRAINT_RELATION_EQ, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -268,7 +268,7 @@ gtk_constraint_layout_get_attribute (GtkConstraintLayout *layout,
|
||||
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
res, GTK_CONSTRAINT_RELATION_EQ, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -292,7 +292,7 @@ gtk_constraint_layout_get_attribute (GtkConstraintLayout *layout,
|
||||
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
res, GTK_CONSTRAINT_RELATION_EQ, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -316,7 +316,7 @@ gtk_constraint_layout_get_attribute (GtkConstraintLayout *layout,
|
||||
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
res, GTK_CONSTRAINT_RELATION_EQ, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -329,7 +329,7 @@ gtk_constraint_layout_get_attribute (GtkConstraintLayout *layout,
|
||||
expr = gtk_constraint_expression_new (0.0);
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
res, GTK_CONSTRAINT_RELATION_GE, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -466,7 +466,7 @@ get_layout_attribute (GtkConstraintLayout *self,
|
||||
|
||||
gtk_constraint_solver_add_constraint (self->solver,
|
||||
res, GTK_CONSTRAINT_RELATION_EQ, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -488,7 +488,7 @@ get_layout_attribute (GtkConstraintLayout *self,
|
||||
|
||||
gtk_constraint_solver_add_constraint (self->solver,
|
||||
res, GTK_CONSTRAINT_RELATION_EQ, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -512,7 +512,7 @@ get_layout_attribute (GtkConstraintLayout *self,
|
||||
|
||||
gtk_constraint_solver_add_constraint (self->solver,
|
||||
res, GTK_CONSTRAINT_RELATION_EQ, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -536,7 +536,7 @@ get_layout_attribute (GtkConstraintLayout *self,
|
||||
|
||||
gtk_constraint_solver_add_constraint (self->solver,
|
||||
res, GTK_CONSTRAINT_RELATION_EQ, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -549,7 +549,7 @@ get_layout_attribute (GtkConstraintLayout *self,
|
||||
expr = gtk_constraint_expression_new (0.0);
|
||||
gtk_constraint_solver_add_constraint (self->solver,
|
||||
res, GTK_CONSTRAINT_RELATION_GE, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -695,7 +695,7 @@ layout_add_constraint (GtkConstraintLayout *self,
|
||||
target_attr,
|
||||
gtk_constraint_get_relation (constraint),
|
||||
expr,
|
||||
gtk_constraint_get_weight (constraint));
|
||||
gtk_constraint_get_strength (constraint));
|
||||
}
|
||||
|
||||
static void
|
||||
@ -736,7 +736,7 @@ update_child_constraint (GtkConstraintLayout *self,
|
||||
child_info->constraints[index] =
|
||||
gtk_constraint_solver_add_stay_variable (self->solver,
|
||||
var,
|
||||
GTK_CONSTRAINT_WEIGHT_MEDIUM);
|
||||
GTK_CONSTRAINT_STRENGTH_MEDIUM);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -745,7 +745,7 @@ update_child_constraint (GtkConstraintLayout *self,
|
||||
var,
|
||||
relation[index],
|
||||
gtk_constraint_expression_new (value),
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -824,9 +824,9 @@ gtk_constraint_layout_measure (GtkLayoutManager *manager,
|
||||
* natural state of the system. Once we get the value out, we can
|
||||
* remove these constraints
|
||||
*/
|
||||
gtk_constraint_solver_add_edit_variable (solver, size, GTK_CONSTRAINT_WEIGHT_STRONG * 2);
|
||||
gtk_constraint_solver_add_edit_variable (solver, size, GTK_CONSTRAINT_STRENGTH_STRONG * 2);
|
||||
if (for_size > 0)
|
||||
gtk_constraint_solver_add_edit_variable (solver, opposite_size, GTK_CONSTRAINT_WEIGHT_STRONG * 2);
|
||||
gtk_constraint_solver_add_edit_variable (solver, opposite_size, GTK_CONSTRAINT_STRENGTH_STRONG * 2);
|
||||
gtk_constraint_solver_begin_edit (solver);
|
||||
gtk_constraint_solver_suggest_value (solver, size, 0.0);
|
||||
if (for_size > 0)
|
||||
@ -883,19 +883,19 @@ gtk_constraint_layout_allocate (GtkLayoutManager *manager,
|
||||
gtk_constraint_variable_set_value (layout_top, 0.0);
|
||||
stay_t = gtk_constraint_solver_add_stay_variable (solver,
|
||||
layout_top,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
gtk_constraint_variable_set_value (layout_left, 0.0);
|
||||
stay_l = gtk_constraint_solver_add_stay_variable (solver,
|
||||
layout_left,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
gtk_constraint_variable_set_value (layout_width, width);
|
||||
stay_w = gtk_constraint_solver_add_stay_variable (solver,
|
||||
layout_width,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
gtk_constraint_variable_set_value (layout_height, height);
|
||||
stay_h = gtk_constraint_solver_add_stay_variable (solver,
|
||||
layout_height,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
GTK_NOTE (LAYOUT,
|
||||
g_print ("Layout [%p]: { .x: %g, .y: %g, .w: %g, .h: %g }\n",
|
||||
self,
|
||||
|
@ -52,8 +52,6 @@ struct _GtkConstraint
|
||||
guint active : 1;
|
||||
};
|
||||
|
||||
double gtk_constraint_get_weight (GtkConstraint *constraint);
|
||||
|
||||
void gtk_constraint_attach (GtkConstraint *constraint,
|
||||
GtkConstraintSolver *solver,
|
||||
GtkConstraintRef *ref);
|
||||
|
@ -73,14 +73,14 @@
|
||||
* e = gtk_constraint_expression_builder_finish (&builder);
|
||||
* gtk_constraint_solver_add_constraint (solver,
|
||||
* right, GTK_CONSTRAINT_RELATION_EQ, e,
|
||||
* GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
* GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
*
|
||||
* // right ≤ 100
|
||||
* gtk_constraint_expression_builder_constant (&builder, 100.0);
|
||||
* e = gtk_constraint_expression_builder_finish (&builder);
|
||||
* gtk_constraint_solver_add_constraint (solver,
|
||||
* right, GTK_CONSTRAINT_RELATION_LE, e,
|
||||
* GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
* GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
*
|
||||
* // middle = (left + right) / 2
|
||||
* gtk_constraint_expression_builder_term (&builder, left);
|
||||
@ -91,14 +91,14 @@
|
||||
* e = gtk_constraint_expression_builder_finish (&builder);
|
||||
* gtk_constraint_solver_add_constraint (solver
|
||||
* middle, GTK_CONSTRAINT_RELATION_EQ, e,
|
||||
* GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
* GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
*
|
||||
* // left ≥ 0
|
||||
* gtk_constraint_expression_builder_constant (&builder, 0.0);
|
||||
* e = gtk_constraint_expression_builder_finish (&builder);
|
||||
* gtk_constraint_solver_add_constraint (solver,
|
||||
* left, GTK_CONSTRAINT_RELATION_GE, e,
|
||||
* GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
* GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
* ]|
|
||||
*
|
||||
* Now that we have all our constraints in place, suppose we wish to find
|
||||
@ -110,8 +110,8 @@
|
||||
* |[
|
||||
* // Set the value first
|
||||
* gtk_constraint_variable_set_value (middle, 45.0);
|
||||
* // and then add the stay constraint, with a weak weight
|
||||
* gtk_constraint_solver_add_stay_variable (solver, middle, GTK_CONSTRAINT_WEIGHT_WEAK);
|
||||
* // and then add the stay constraint, with a weak strength
|
||||
* gtk_constraint_solver_add_stay_variable (solver, middle, GTK_CONSTRAINT_STRENGTH_WEAK);
|
||||
* ]|
|
||||
*
|
||||
* GtkConstraintSolver incrementally solves the system every time a constraint
|
||||
@ -185,8 +185,11 @@ struct _GtkConstraintRef
|
||||
/* The original relation used when creating the constraint */
|
||||
GtkConstraintRelation relation;
|
||||
|
||||
/* The weight, or strength, of the constraint */
|
||||
double weight;
|
||||
/* The strength of the constraint; this value is used to strengthen
|
||||
* or weaken a constraint weight in the tableau when coming to a
|
||||
* solution
|
||||
*/
|
||||
int strength;
|
||||
|
||||
GtkConstraintSolver *solver;
|
||||
|
||||
@ -374,7 +377,7 @@ gtk_constraint_ref_is_inequality (const GtkConstraintRef *self)
|
||||
static gboolean
|
||||
gtk_constraint_ref_is_required (const GtkConstraintRef *self)
|
||||
{
|
||||
return self->weight >= GTK_CONSTRAINT_WEIGHT_REQUIRED;
|
||||
return self->strength == GTK_CONSTRAINT_STRENGTH_REQUIRED;
|
||||
}
|
||||
|
||||
static const char *relations[] = {
|
||||
@ -390,15 +393,12 @@ relation_to_string (GtkConstraintRelation r)
|
||||
}
|
||||
|
||||
static const char *
|
||||
weight_to_string (double s)
|
||||
strength_to_string (int s)
|
||||
{
|
||||
if (s >= GTK_CONSTRAINT_WEIGHT_REQUIRED)
|
||||
return "required";
|
||||
|
||||
if (s >= GTK_CONSTRAINT_WEIGHT_STRONG)
|
||||
if (s >= GTK_CONSTRAINT_STRENGTH_STRONG)
|
||||
return "strong";
|
||||
|
||||
if (s >= GTK_CONSTRAINT_WEIGHT_MEDIUM)
|
||||
if (s >= GTK_CONSTRAINT_STRENGTH_MEDIUM)
|
||||
return "medium";
|
||||
|
||||
return "weak";
|
||||
@ -423,9 +423,12 @@ gtk_constraint_ref_to_string (const GtkConstraintRef *self)
|
||||
g_string_append (buf, relation_to_string (self->relation));
|
||||
g_string_append (buf, " 0.0");
|
||||
|
||||
g_string_append_printf (buf, " [weight:%s (%g)]",
|
||||
weight_to_string (self->weight),
|
||||
self->weight);
|
||||
if (gtk_constraint_ref_is_required (self))
|
||||
g_string_append (buf, " [strength:required]");
|
||||
else
|
||||
g_string_append_printf (buf, " [strength:%d (%s)]",
|
||||
self->strength,
|
||||
strength_to_string (self->strength));
|
||||
|
||||
return g_string_free (buf, FALSE);
|
||||
}
|
||||
@ -909,7 +912,7 @@ gtk_constraint_solver_new_expression (GtkConstraintSolver *self,
|
||||
gtk_constraint_variable_unref (eminus);
|
||||
|
||||
z_row = g_hash_table_lookup (self->rows, self->objective);
|
||||
gtk_constraint_expression_set_variable (z_row, eminus, constraint->weight);
|
||||
gtk_constraint_expression_set_variable (z_row, eminus, constraint->strength);
|
||||
|
||||
gtk_constraint_solver_insert_error_variable (self, constraint, eminus);
|
||||
gtk_constraint_solver_note_added_variable (self, eminus, self->objective);
|
||||
@ -968,8 +971,8 @@ gtk_constraint_solver_new_expression (GtkConstraintSolver *self,
|
||||
|
||||
z_row = g_hash_table_lookup (self->rows, self->objective);
|
||||
|
||||
gtk_constraint_expression_set_variable (z_row, eplus, constraint->weight);
|
||||
gtk_constraint_expression_set_variable (z_row, eminus, constraint->weight);
|
||||
gtk_constraint_expression_set_variable (z_row, eplus, constraint->strength);
|
||||
gtk_constraint_expression_set_variable (z_row, eminus, constraint->strength);
|
||||
gtk_constraint_solver_note_added_variable (self, eplus, self->objective);
|
||||
gtk_constraint_solver_note_added_variable (self, eminus, self->objective);
|
||||
|
||||
@ -1522,7 +1525,7 @@ gtk_constraint_solver_resolve (GtkConstraintSolver *solver)
|
||||
* @variable: the subject of the constraint
|
||||
* @relation: the relation of the constraint
|
||||
* @expression: the expression of the constraint
|
||||
* @strength: the weight of the constraint
|
||||
* @strength: the strength of the constraint
|
||||
*
|
||||
* Adds a new constraint in the form of:
|
||||
*
|
||||
@ -1541,12 +1544,12 @@ gtk_constraint_solver_add_constraint (GtkConstraintSolver *self,
|
||||
GtkConstraintVariable *variable,
|
||||
GtkConstraintRelation relation,
|
||||
GtkConstraintExpression *expression,
|
||||
double strength)
|
||||
int strength)
|
||||
{
|
||||
GtkConstraintRef *res = g_new0 (GtkConstraintRef, 1);
|
||||
|
||||
res->solver = self;
|
||||
res->weight = strength;
|
||||
res->strength = strength;
|
||||
res->is_edit = FALSE;
|
||||
res->is_stay = FALSE;
|
||||
res->relation = relation;
|
||||
@ -1598,7 +1601,7 @@ gtk_constraint_solver_add_constraint (GtkConstraintSolver *self,
|
||||
* gtk_constraint_solver_add_stay_variable:
|
||||
* @self: a #GtkConstraintSolver
|
||||
* @variable: a stay #GtkConstraintVariable
|
||||
* @strength: the weight of the constraint
|
||||
* @strength: the strength of the constraint
|
||||
*
|
||||
* Adds a constraint on a stay @variable with the given @strength.
|
||||
*
|
||||
@ -1612,14 +1615,14 @@ gtk_constraint_solver_add_constraint (GtkConstraintSolver *self,
|
||||
GtkConstraintRef *
|
||||
gtk_constraint_solver_add_stay_variable (GtkConstraintSolver *self,
|
||||
GtkConstraintVariable *variable,
|
||||
double strength)
|
||||
int strength)
|
||||
{
|
||||
GtkConstraintRef *res = g_new0 (GtkConstraintRef, 1);
|
||||
|
||||
res->solver = self;
|
||||
res->variable = gtk_constraint_variable_ref (variable);
|
||||
res->relation = GTK_CONSTRAINT_RELATION_EQ;
|
||||
res->weight = strength;
|
||||
res->strength = strength;
|
||||
res->is_stay = TRUE;
|
||||
res->is_edit = FALSE;
|
||||
|
||||
@ -1692,14 +1695,14 @@ gtk_constraint_solver_remove_stay_variable (GtkConstraintSolver *self,
|
||||
GtkConstraintRef *
|
||||
gtk_constraint_solver_add_edit_variable (GtkConstraintSolver *self,
|
||||
GtkConstraintVariable *variable,
|
||||
double strength)
|
||||
int strength)
|
||||
{
|
||||
GtkConstraintRef *res = g_new0 (GtkConstraintRef, 1);
|
||||
|
||||
res->solver = self;
|
||||
res->variable = gtk_constraint_variable_ref (variable);
|
||||
res->relation = GTK_CONSTRAINT_RELATION_EQ;
|
||||
res->weight = strength;
|
||||
res->strength = strength;
|
||||
res->is_stay = FALSE;
|
||||
res->is_edit = TRUE;
|
||||
|
||||
@ -1782,7 +1785,7 @@ gtk_constraint_solver_remove_constraint (GtkConstraintSolver *self,
|
||||
{
|
||||
gtk_constraint_expression_add_variable (z_row,
|
||||
v,
|
||||
constraint->weight,
|
||||
constraint->strength,
|
||||
self->objective,
|
||||
self);
|
||||
}
|
||||
@ -1790,7 +1793,7 @@ gtk_constraint_solver_remove_constraint (GtkConstraintSolver *self,
|
||||
{
|
||||
gtk_constraint_expression_add_expression (z_row,
|
||||
e,
|
||||
constraint->weight,
|
||||
constraint->strength,
|
||||
self->objective,
|
||||
self);
|
||||
}
|
||||
|
@ -29,37 +29,6 @@ G_BEGIN_DECLS
|
||||
|
||||
G_DECLARE_FINAL_TYPE (GtkConstraintSolver, gtk_constraint_solver, GTK, CONSTRAINT_SOLVER, GObject)
|
||||
|
||||
/* Symbolic weight thresholds
|
||||
*
|
||||
* Constraint weights live on a continuum, but we use thresholds for simplicity's
|
||||
* sake, so we don't have to necessarily reason in terms of numeric values.
|
||||
*
|
||||
* The public API has a similar approach, where the symbolic constants are negative
|
||||
* values, and positive values are explicit weights. We map those values into
|
||||
* numeric values that the GtkConstraintSolver can plug into the linear equations
|
||||
* tableau.
|
||||
*/
|
||||
#define GTK_CONSTRAINT_WEIGHT_REQUIRED (make_weight (1000, 1000, 1000, 1))
|
||||
#define GTK_CONSTRAINT_WEIGHT_STRONG (make_weight ( 1, 0, 0, 1))
|
||||
#define GTK_CONSTRAINT_WEIGHT_MEDIUM (make_weight ( 0, 1, 0, 1))
|
||||
#define GTK_CONSTRAINT_WEIGHT_WEAK (make_weight ( 0, 0, 1, 1))
|
||||
|
||||
G_GNUC_PURE
|
||||
static inline double
|
||||
make_weight (double a,
|
||||
double b,
|
||||
double c,
|
||||
double w)
|
||||
{
|
||||
double res = 0;
|
||||
|
||||
res += CLAMP (a * w, 0, 1000) * 1000000;
|
||||
res += CLAMP (b * w, 0, 1000) * 1000;
|
||||
res += CLAMP (c * w, 0, 1000);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
GtkConstraintSolver *
|
||||
gtk_constraint_solver_new (void);
|
||||
|
||||
@ -83,7 +52,7 @@ gtk_constraint_solver_add_constraint (GtkConstraintSolver *solver,
|
||||
GtkConstraintVariable *variable,
|
||||
GtkConstraintRelation relation,
|
||||
GtkConstraintExpression *expression,
|
||||
double strength);
|
||||
int strength);
|
||||
|
||||
void
|
||||
gtk_constraint_solver_remove_constraint (GtkConstraintSolver *solver,
|
||||
@ -92,7 +61,7 @@ gtk_constraint_solver_remove_constraint (GtkConstraintSolver *solver,
|
||||
GtkConstraintRef *
|
||||
gtk_constraint_solver_add_stay_variable (GtkConstraintSolver *solver,
|
||||
GtkConstraintVariable *variable,
|
||||
double strength);
|
||||
int strength);
|
||||
|
||||
void
|
||||
gtk_constraint_solver_remove_stay_variable (GtkConstraintSolver *solver,
|
||||
@ -105,7 +74,7 @@ gtk_constraint_solver_has_stay_variable (GtkConstraintSolver *solver,
|
||||
GtkConstraintRef *
|
||||
gtk_constraint_solver_add_edit_variable (GtkConstraintSolver *solver,
|
||||
GtkConstraintVariable *variable,
|
||||
double strength);
|
||||
int strength);
|
||||
|
||||
void
|
||||
gtk_constraint_solver_remove_edit_variable (GtkConstraintSolver *solver,
|
||||
|
@ -1080,10 +1080,10 @@ typedef enum {
|
||||
* integer; the values of this enumeration can be used for readability.
|
||||
*/
|
||||
typedef enum {
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED = 0,
|
||||
GTK_CONSTRAINT_STRENGTH_STRONG = -1,
|
||||
GTK_CONSTRAINT_STRENGTH_MEDIUM = -2,
|
||||
GTK_CONSTRAINT_STRENGTH_WEAK = -3
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED = 1001001000,
|
||||
GTK_CONSTRAINT_STRENGTH_STRONG = 1000000000,
|
||||
GTK_CONSTRAINT_STRENGTH_MEDIUM = 1000,
|
||||
GTK_CONSTRAINT_STRENGTH_WEAK = 1
|
||||
} GtkConstraintStrength;
|
||||
|
||||
/**
|
||||
|
@ -16,7 +16,7 @@ constraint_solver_simple (void)
|
||||
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
x, GTK_CONSTRAINT_RELATION_EQ, e,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
double x_value = gtk_constraint_variable_get_value (x);
|
||||
double y_value = gtk_constraint_variable_get_value (y);
|
||||
@ -39,8 +39,8 @@ constraint_solver_stay (void)
|
||||
GtkConstraintVariable *x = gtk_constraint_solver_create_variable (solver, NULL, "x", 5.0);
|
||||
GtkConstraintVariable *y = gtk_constraint_solver_create_variable (solver, NULL, "y", 10.0);
|
||||
|
||||
gtk_constraint_solver_add_stay_variable (solver, x, GTK_CONSTRAINT_WEIGHT_WEAK);
|
||||
gtk_constraint_solver_add_stay_variable (solver, y, GTK_CONSTRAINT_WEIGHT_WEAK);
|
||||
gtk_constraint_solver_add_stay_variable (solver, x, GTK_CONSTRAINT_STRENGTH_WEAK);
|
||||
gtk_constraint_solver_add_stay_variable (solver, y, GTK_CONSTRAINT_STRENGTH_WEAK);
|
||||
|
||||
double x_value = gtk_constraint_variable_get_value (x);
|
||||
double y_value = gtk_constraint_variable_get_value (y);
|
||||
@ -64,7 +64,7 @@ constraint_solver_variable_geq_constant (void)
|
||||
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
x, GTK_CONSTRAINT_RELATION_GE, e,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
double x_value = gtk_constraint_variable_get_value (x);
|
||||
|
||||
@ -85,7 +85,7 @@ constraint_solver_variable_leq_constant (void)
|
||||
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
x, GTK_CONSTRAINT_RELATION_LE, e,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
double x_value = gtk_constraint_variable_get_value (x);
|
||||
|
||||
@ -106,7 +106,7 @@ constraint_solver_variable_eq_constant (void)
|
||||
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
x, GTK_CONSTRAINT_RELATION_EQ, e,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
double x_value = gtk_constraint_variable_get_value (x);
|
||||
|
||||
@ -133,11 +133,11 @@ constraint_solver_eq_with_stay (void)
|
||||
gtk_constraint_expression_builder_term (&builder, width);
|
||||
GtkConstraintExpression *right = gtk_constraint_expression_builder_finish (&builder);
|
||||
|
||||
gtk_constraint_solver_add_stay_variable (solver, width, GTK_CONSTRAINT_WEIGHT_WEAK);
|
||||
gtk_constraint_solver_add_stay_variable (solver, right_min, GTK_CONSTRAINT_WEIGHT_WEAK);
|
||||
gtk_constraint_solver_add_stay_variable (solver, width, GTK_CONSTRAINT_STRENGTH_WEAK);
|
||||
gtk_constraint_solver_add_stay_variable (solver, right_min, GTK_CONSTRAINT_STRENGTH_WEAK);
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
right_min, GTK_CONSTRAINT_RELATION_EQ, right,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
double x_value = gtk_constraint_variable_get_value (x);
|
||||
double width_value = gtk_constraint_variable_get_value (width);
|
||||
@ -165,22 +165,22 @@ constraint_solver_cassowary (void)
|
||||
e = gtk_constraint_expression_new_from_variable (y);
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
x, GTK_CONSTRAINT_RELATION_LE, e,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
e = gtk_constraint_expression_plus_constant (gtk_constraint_expression_new_from_variable (x), 3.0);
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
y, GTK_CONSTRAINT_RELATION_EQ, e,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
e = gtk_constraint_expression_new (10.0);
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
x, GTK_CONSTRAINT_RELATION_EQ, e,
|
||||
GTK_CONSTRAINT_WEIGHT_WEAK);
|
||||
GTK_CONSTRAINT_STRENGTH_WEAK);
|
||||
|
||||
e = gtk_constraint_expression_new (10.0);
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
y, GTK_CONSTRAINT_RELATION_EQ, e,
|
||||
GTK_CONSTRAINT_WEIGHT_WEAK);
|
||||
GTK_CONSTRAINT_STRENGTH_WEAK);
|
||||
|
||||
double x_val = gtk_constraint_variable_get_value (x);
|
||||
double y_val = gtk_constraint_variable_get_value (y);
|
||||
@ -205,11 +205,11 @@ constraint_solver_edit_var_required (void)
|
||||
GtkConstraintSolver *solver = gtk_constraint_solver_new ();
|
||||
|
||||
GtkConstraintVariable *a = gtk_constraint_solver_create_variable (solver, NULL, "a", 0.0);
|
||||
gtk_constraint_solver_add_stay_variable (solver, a, GTK_CONSTRAINT_WEIGHT_STRONG);
|
||||
gtk_constraint_solver_add_stay_variable (solver, a, GTK_CONSTRAINT_STRENGTH_STRONG);
|
||||
|
||||
g_assert_cmpfloat_with_epsilon (gtk_constraint_variable_get_value (a), 0.0, 0.001);
|
||||
|
||||
gtk_constraint_solver_add_edit_variable (solver, a, GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
gtk_constraint_solver_add_edit_variable (solver, a, GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
gtk_constraint_solver_begin_edit (solver);
|
||||
gtk_constraint_solver_suggest_value (solver, a, 2.0);
|
||||
gtk_constraint_solver_resolve (solver);
|
||||
@ -236,19 +236,19 @@ constraint_solver_edit_var_suggest (void)
|
||||
GtkConstraintVariable *a = gtk_constraint_solver_create_variable (solver, NULL, "a", 0.0);
|
||||
GtkConstraintVariable *b = gtk_constraint_solver_create_variable (solver, NULL, "b", 0.0);
|
||||
|
||||
gtk_constraint_solver_add_stay_variable (solver, a, GTK_CONSTRAINT_WEIGHT_STRONG);
|
||||
gtk_constraint_solver_add_stay_variable (solver, a, GTK_CONSTRAINT_STRENGTH_STRONG);
|
||||
|
||||
GtkConstraintExpression *e = gtk_constraint_expression_new_from_variable (b);
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
a, GTK_CONSTRAINT_RELATION_EQ, e,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
gtk_constraint_solver_resolve (solver);
|
||||
|
||||
g_assert_cmpfloat_with_epsilon (gtk_constraint_variable_get_value (a), 0.0, 0.001);
|
||||
g_assert_cmpfloat_with_epsilon (gtk_constraint_variable_get_value (b), 0.0, 0.001);
|
||||
|
||||
gtk_constraint_solver_add_edit_variable (solver, a, GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
gtk_constraint_solver_add_edit_variable (solver, a, GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
gtk_constraint_solver_begin_edit (solver);
|
||||
|
||||
gtk_constraint_solver_suggest_value (solver, a, 2.0);
|
||||
@ -302,7 +302,7 @@ constraint_solver_paper (void)
|
||||
expr = gtk_constraint_expression_builder_finish (&builder);
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
middle, GTK_CONSTRAINT_RELATION_EQ, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
gtk_constraint_expression_builder_init (&builder, solver);
|
||||
gtk_constraint_expression_builder_term (&builder, left);
|
||||
@ -311,17 +311,17 @@ constraint_solver_paper (void)
|
||||
expr = gtk_constraint_expression_builder_finish (&builder);
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
right, GTK_CONSTRAINT_RELATION_EQ, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
expr = gtk_constraint_expression_new (100.0);
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
right, GTK_CONSTRAINT_RELATION_LE, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
expr = gtk_constraint_expression_new (0.0);
|
||||
gtk_constraint_solver_add_constraint (solver,
|
||||
left, GTK_CONSTRAINT_RELATION_GE, expr,
|
||||
GTK_CONSTRAINT_WEIGHT_REQUIRED);
|
||||
GTK_CONSTRAINT_STRENGTH_REQUIRED);
|
||||
|
||||
g_test_message ("Check constraints hold");
|
||||
|
||||
@ -335,7 +335,7 @@ constraint_solver_paper (void)
|
||||
g_assert_cmpfloat (gtk_constraint_variable_get_value (left), >=, 0.0);
|
||||
|
||||
gtk_constraint_variable_set_value (middle, 45.0);
|
||||
gtk_constraint_solver_add_stay_variable (solver, middle, GTK_CONSTRAINT_WEIGHT_WEAK);
|
||||
gtk_constraint_solver_add_stay_variable (solver, middle, GTK_CONSTRAINT_STRENGTH_WEAK);
|
||||
|
||||
g_test_message ("Check constraints hold after setting middle");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user