summaryrefslogtreecommitdiffstats
path: root/src/libvpsc/constraint.h
diff options
context:
space:
mode:
authorSylvain Chiron <chironsylvain@orange.fr>2017-07-01 11:36:41 +0000
committerSylvain Chiron <chironsylvain@orange.fr>2017-07-01 11:36:41 +0000
commitfd733201b82f39655488a286c89142f321ef9dc9 (patch)
treea12c70f213414f69467f666619b1552103f6370e /src/libvpsc/constraint.h
parentHackfest icon work: restore selected menu icons and make theming easier (diff)
downloadinkscape-fd733201b82f39655488a286c89142f321ef9dc9.tar.gz
inkscape-fd733201b82f39655488a286c89142f321ef9dc9.zip
Updated libs from the Adaptagrams project: libavoid, libcola and libvspc; changed the code to match the new API
Signed-off-by: Sylvain Chiron <chironsylvain@orange.fr>
Diffstat (limited to 'src/libvpsc/constraint.h')
-rw-r--r--src/libvpsc/constraint.h161
1 files changed, 119 insertions, 42 deletions
diff --git a/src/libvpsc/constraint.h b/src/libvpsc/constraint.h
index a3173359c..271a2cfce 100644
--- a/src/libvpsc/constraint.h
+++ b/src/libvpsc/constraint.h
@@ -1,62 +1,139 @@
/*
- * Authors:
- * Tim Dwyer <tgdwyer@gmail.com>
+ * vim: ts=4 sw=4 et tw=0 wm=0
*
- * Copyright (C) 2005 Authors
+ * libvpsc - A solver for the problem of Variable Placement with
+ * Separation Constraints.
*
- * Released under GNU LGPL. Read the file 'COPYING' for more information.
- */
+ * Copyright (C) 2005-2008 Monash University
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * See the file LICENSE.LGPL distributed with the library.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Author(s): Tim Dwyer
+*/
+
+
+#ifndef VPSC_CONSTRAINT_H
+#define VPSC_CONSTRAINT_H
-#ifndef SEEN_REMOVEOVERLAP_CONSTRAINT_H
-#define SEEN_REMOVEOVERLAP_CONSTRAINT_H
+// cmath needs ::strcpy_s under MinGW so include cstring.
+#include <cstring>
+#include <cfloat>
#include <iostream>
-#include "variable.h"
+#include <vector>
+#include <sstream>
+
+#include "libvpsc/variable.h"
+
namespace vpsc {
-/**
- * A constraint determines a minimum or exact spacing required between
- * two variables.
- *
- */
+class Variable;
+typedef std::vector<Variable *> Variables;
+
+//! @brief A constraint determines a minimum or exact spacing required between
+//! two Variable objects.
+//!
class Constraint
{
friend std::ostream& operator <<(std::ostream &os,const Constraint &c);
public:
- Variable *left;
+ //! @brief Constructs a minimum or exact spacing constraint between two
+ //! Variable objects.
+ //!
+ //! (left + gap < right) or (left + gap == right)
+ //!
+ //! @param[in] left The left Variable.
+ //! @param[in] right The right Variable.
+ //! @param[in] gap The minimum or exact distance to separate the
+ //! variables by.
+ //! @param[in] equality Whether the separation is an exact distance or
+ //! not. The default is false.
+ Constraint(Variable *left, Variable *right, double gap,
+ bool equality = false);
+ ~Constraint();
+
+ /**
+ * @brief Returns a textual description of the constraint.
+ *
+ * @return A string describing the constraint.
+ */
+ std::string toString(void) const
+ {
+ std::stringstream stream;
+ stream << "Constraint: var(" << left->id << ") ";
+ if (gap < 0)
+ {
+ stream << "- " << -gap << " ";
+ }
+ else
+ {
+ stream << "+ " << gap << " ";
+ }
+ stream << ((equality) ? "==" : "<=");
+ stream << " var(" << right->id << ") ";
+ return stream.str();
+ }
+
+ inline double slack(void) const
+ {
+ if (unsatisfiable)
+ {
+ return DBL_MAX;
+ }
+ if (needsScaling)
+ {
+ return right->scale * right->position() - gap -
+ left->scale * left->position();
+ }
+ COLA_ASSERT(left->scale == 1);
+ COLA_ASSERT(right->scale == 1);
+ return right->unscaledPosition() - gap - left->unscaledPosition();
+ }
+
+ //! @brief The left Variable.
+ Variable *left;
+ //! @brief The right Variable.
Variable *right;
+ //! @brief The minimum or exact distance to separate the variables by.
double gap;
double lm;
- Constraint(Variable *left, Variable *right, double gap, bool equality=false);
- virtual ~Constraint();
- inline double slack() const { return right->position() - gap - left->position(); }
long timeStamp;
bool active;
- bool visited;
- bool equality;
+ //! @brief Whether the separation is an exact distance or not.
+ const bool equality;
+ //! @brief Denote whether this constraint was unsatisifable (once the VPSC
+ //! instance has been solved or satisfied).
+ bool unsatisfiable;
+ bool needsScaling;
+ void *creator;
};
-#include <float.h>
-#include "block.h"
-static inline bool compareConstraints(Constraint *const &l, Constraint *const &r) {
- double const sl =
- l->left->block->timeStamp > l->timeStamp
- ||l->left->block==l->right->block
- ?-DBL_MAX:l->slack();
- double const sr =
- r->left->block->timeStamp > r->timeStamp
- ||r->left->block==r->right->block
- ?-DBL_MAX:r->slack();
- if(sl==sr) {
- // arbitrary choice based on id
- if(l->left->id==r->left->id) {
- if(l->right->id<r->right->id) return true;
- return false;
- }
- if(l->left->id<r->left->id) return true;
- return false;
- }
- return sl < sr;
-}
+
+class CompareConstraints {
+public:
+ bool operator() (Constraint *const &l, Constraint *const &r) const;
+};
+
+//! @brief A vector of pointers to Constraint objects.
+typedef std::vector<Constraint*> Constraints;
+
+/** @brief Given a set of variables and constraints, returns a modified set
+ * of constraints with all redundant equality constraints removed.
+ *
+ * VPSC doesn't work well with redundant equality constraints, usually showing
+ * them as unsatisfiable. This function looks for cycles of equality
+ * constraints and removes the redundant ones.
+ */
+extern Constraints constraintsRemovingRedundantEqualities(
+ const Variables& vars, const Constraints& constraints);
+
}
-#endif // SEEN_REMOVEOVERLAP_CONSTRAINT_H
+#endif // VPSC_CONSTRAINT_H