1 module cassowary.LinearInequality;
2 
3 import cassowary.LinearConstraint;
4 import cassowary.LinearExpression;
5 import cassowary.Strength;
6 import cassowary.Variable;
7 import cassowary.AbstractVariable;
8 import cassowary.Error;
9 
10 enum InequalityType
11 {
12 	GEQ,
13 	LEQ
14 }
15 
16 class ClLinearInequality : ClLinearConstraint
17 {
18 	this(ClLinearExpression cle, const ClStrength strength = ClStrength.required, double weight = 1)
19 	{
20 		super(cle, strength, weight);
21 	}
22 
23 	this(ClVariable clv1, InequalityType op, ClVariable clv2, const ClStrength strength = ClStrength.required, double weight = 1)
24 	{
25 		super(new ClLinearExpression(clv2), strength, weight);
26 		if (op == InequalityType.GEQ)
27 		{
28 			_expression.multiplyMe(-1.0);
29 			_expression.addVariable(clv1);
30 		}
31 		else if (op == InequalityType.LEQ)
32 		{
33 			_expression.addVariable(clv1, -1.0);
34 		}
35 		else // the operator was invalid
36 			throw new ClErrorInternal("Invalid operator in ClLinearInequality constructor");
37 	}
38 
39 	this(ClVariable clv, InequalityType op, double val, const ClStrength strength = ClStrength.required, double weight = 1)
40 	{
41 		super(new ClLinearExpression(val), strength, weight);
42 		if (op == InequalityType.GEQ)
43 		{
44 			_expression.multiplyMe(-1.0);
45 			_expression.addVariable(clv);
46 		}
47 		else if (op == InequalityType.LEQ)
48 		{
49 			_expression.addVariable(clv, -1.0);
50 		}
51 		else // the operator was invalid
52 			throw new ClErrorInternal("Invalid operator in ClLinearInequality constructor");
53 	}
54 
55 	this(ClLinearExpression cle1, InequalityType op, ClLinearExpression cle2, const ClStrength strength = ClStrength.required, double weight = 1)
56 	{
57 		super((cast(ClLinearExpression) cle2.clone()), strength, weight);
58 		if (op == InequalityType.GEQ)
59 		{
60 			_expression.multiplyMe(-1.0);
61 			_expression.addExpression(cle1);
62 		}
63 		else if (op == InequalityType.LEQ)
64 		{
65 			_expression.addExpression(cle1, -1.0);
66 		}
67 		else // the operator was invalid
68 			throw new ClErrorInternal("Invalid operator in ClLinearInequality constructor");
69 	}
70 
71 	this(ClAbstractVariable clv, InequalityType op, ClLinearExpression cle, const ClStrength strength = ClStrength.required, double weight = 1)
72 	{
73 		super((cast(ClLinearExpression) cle.clone()), strength, weight);
74 		if (op == InequalityType.GEQ)
75 		{
76 			_expression.multiplyMe(-1.0);
77 			_expression.addVariable(clv);
78 		}
79 		else if (op == InequalityType.LEQ)
80 		{
81 			_expression.addVariable(clv, -1.0);
82 		}
83 		else // the operator was invalid
84 			throw new ClErrorInternal("Invalid operator in ClLinearInequality constructor");
85 	}
86 
87 	this(ClLinearExpression cle, InequalityType op, ClAbstractVariable clv, const ClStrength strength = ClStrength.required, double weight = 1)
88 	{
89 		super((cast(ClLinearExpression) cle.clone()), strength, weight);
90 		if (op == InequalityType.LEQ)
91 		{
92 			_expression.multiplyMe(-1.0);
93 			_expression.addVariable(clv);
94 		}
95 		else if (op == InequalityType.GEQ)
96 		{
97 			_expression.addVariable(clv, -1.0);
98 		}
99 		else // the operator was invalid
100 			throw new ClErrorInternal("Invalid operator in ClLinearInequality constructor");
101 	}
102 
103 	override bool isInequality() const
104 	{
105 		return true;
106 	}
107 
108 	override string toString() const
109 	{
110 		return super.toString() ~ " >= 0 )";
111 	}
112 }