xref: /haiku/src/kits/interface/layouter/LayoutOptimizer.h (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #ifndef LAYOUT_OPTIMIZER_H
6 #define LAYOUT_OPTIMIZER_H
7 
8 #include <List.h>
9 #include <math.h>
10 
11 static const double kEqualsEpsilon = 0.000001;
12 
13 
14 namespace BPrivate {
15 namespace Layout {
16 
17 class LayoutOptimizer {
18 public:
19 								LayoutOptimizer(int32 variableCount);
20 								~LayoutOptimizer();
21 
22 			status_t			InitCheck() const;
23 
24 			LayoutOptimizer*	Clone() const;
25 
26 			bool				AddConstraint(int32 left, int32 right,
27 									double value, bool equality);
28 			bool				AddConstraintsFrom(
29 									const LayoutOptimizer* solver);
30 			void				RemoveAllConstraints();
31 
32 			bool				Solve(const double* desired, double size,
33 									double* values);
34 
35 private:
36 			bool				_Solve(const double* desired, double* values);
37 			bool				_SolveSubProblem(const double* d, int am,
38 									double* p);
39 			void				_SetResult(const double* x, double* values);
40 
41 
42 			struct Constraint;
43 
44 			int32				fVariableCount;
45 			BList				fConstraints;
46 			double*				fVariables;
47 			double**			fTemp1;
48 			double**			fTemp2;
49 			double**			fZtrans;
50 			double**			fQ;
51 			double**			fActiveMatrix;
52 			double**			fActiveMatrixTemp;
53 };
54 
55 }	// namespace Layout
56 }	// namespace BPrivate
57 
58 using BPrivate::Layout::LayoutOptimizer;
59 
60 
61 inline bool
62 fuzzy_equals(double a, double b)
63 {
64 	return fabs(a - b) < kEqualsEpsilon;
65 }
66 
67 
68 #endif	// LAYOUT_OPTIMIZER_H
69