COPASI API  4.16.103
CCopasiSpringLayout.h
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 #ifndef CCopasiSpringLayout_H
7 #define CCopasiSpringLayout_H
8 
9 #include <map>
10 #include <set>
11 #include <cmath>
12 
14 #include "CLBase.h"
15 
16 class CLayout;
17 class CLGraphicalObject;
18 class CLCompartmentGlyph;
19 class CLMetabGlyph;
20 class CLReactionGlyph;
22 class CLReferenceGlyph;
23 class CLGeneralGlyph;
24 class CCopasiContainer;
25 class CCompartment;
26 class CReaction;
27 class CMetab;
28 /**
29  * This class defines how a layout optimization algorithm will see a COPASI
30  * layout, using a spring approach.
31  * No copy of the layout will be generated, meaning that setState() will
32  * change the layout.
33  */
35 {
36 public:
37 
38  class Parameters
39  {
40  public:
41  std::vector<std::string> names;
42  std::vector<double> values;
43  std::vector<double> defaultValues;
44  std::vector<double> min;
45  std::vector<double> max;
46  std::vector<bool> isLog;
47 
49  {
50  names.push_back("repulsion");
51  values.push_back(100000.0);
52  min.push_back(10);
53  max.push_back(1e7);
54  isLog.push_back(true);
55 
56  //1
57  names.push_back("edge length");
58  values.push_back(70);
59  min.push_back(10);
60  max.push_back(200);
61  isLog.push_back(false);
62 
63  names.push_back("edge strength");
64  values.push_back(1.0);
65  min.push_back(1e-3);
66  max.push_back(1e3);
67  isLog.push_back(true);
68 
69  //3
70  names.push_back("side edge length");
71  values.push_back(40);
72  min.push_back(10);
73  max.push_back(200);
74  isLog.push_back(false);
75 
76  names.push_back("side edge strength");
77  values.push_back(0.3);
78  min.push_back(1e-3);
79  max.push_back(1e3);
80  isLog.push_back(true);
81 
82  //5
83  names.push_back("2nd order edge strength");
84  values.push_back(0.1);
85  min.push_back(1e-3);
86  max.push_back(1e3);
87  isLog.push_back(true);
88 
89  //6
90  names.push_back("compartment strength on species");
91  values.push_back(4.0);
92  min.push_back(1e-2);
93  max.push_back(1e3);
94  isLog.push_back(true);
95 
96  //7
97  names.push_back("compartment strength on reactions");
98  values.push_back(0.2);
99  min.push_back(1e-2);
100  max.push_back(1e3);
101  isLog.push_back(true);
102 
104  }
105 
107  {
109  }
110  };
111 
112  /**
113  * generate a spring layout view of a COPASI layout.
114  */
115  CCopasiSpringLayout(CLayout* layout, Parameters* ppp = NULL);
116 
117  /**
118  * generates the list of variables from the layout. This method will generate
119  * variables for the coordinates of all the nodes in the layout.
120  * TODO: make it possible to only auto-layout a subset of the graph
121  */
122  bool createVariables();
123 
124  /**
125  * updates the COPASI layout from the state vector. Only the updates
126  * that are needed for the calculation of the potential (or for a rough
127  * on the fly drawing) are done.
128  */
129  virtual bool setState(const std::vector<double> & vars);
130 
131  /**
132  * complete the update of the layout. This updates the things
133  * that are not needed for the calculation of the potential.
134  * After calling this method the layout should be ready for drawing
135  * or storing
136  */
137  void finalizeState();
138 
139  /**
140  * Randomizes the layout
141  */
142  void randomize();
143 
144  /**
145  * This method creates a random layout using the elements
146  * in the compartments, reactions, species and side species
147  * containers.
148  */
149  static CLayout* createLayout(
150  CCopasiContainer* parent,
151  const std::set<const CCompartment*>& compartments,
152  const std::set<const CReaction*>& reactions,
153  const std::set<const CMetab*>& metabs,
154  const std::set<const CMetab*>& sideMetabs,
155  Parameters* mParams = NULL
156  );
157 
158 // virtual bool getState(std::vector<double> & vars);
159 
160  virtual double getPotential();
161  virtual const std::vector<double> & getInitialValues() const;
162 
163  /**
164  * if all participants of a reaction are in a single compartment return the compartment
165  * glyph, otherwise return NULL
166  */
168 
169 protected:
170 
171  /// performs all initializations that are later needed to calculate the potential
172  bool initFromLayout(CLayout* layout, Parameters* ppp);
173 
174  ///create variables for size and position of a compartment glyph
176 
177  ///create variables for the position of a species glyph
179 
180  ///create variables for the position of a reaction glyph
182 
183  static inline double distance(const double & x1, const double & y1,
184  const double & x2, const double & y2)
185  {
186  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
187  //return fabs(x1-x2)<fabs(y1-y2)?fabs(y1-y2):fabs(x1-x2);
188  }
189 
190  static inline double bound_distance(const double & x1, const double & y1,
191  const double & x2, const double & y2, const double & max)
192  {
193  //return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
194  //return fabs(x1-x2)<fabs(y1-y2)?fabs(y1-y2):fabs(x1-x2);
195 
196  if (fabs(x1 - x2) > max) return max;
197 
198  if (fabs(y1 - y2) > max) return max;
199 
200  double tmp = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
201  return tmp > max ? max : tmp;
202  }
203 
204  double potSpeciesSpecies(const CLMetabGlyph & a, const CLMetabGlyph & b) const;
205  double potSpeciesReaction(const CLMetabGlyph & a, const CLReactionGlyph & b) const;
206  double potReactionReaction(const CLReactionGlyph & a, const CLReactionGlyph & b) const;
207  double potEdge(const CLMetabReferenceGlyph & e, const CLReactionGlyph & r) const;
208  double potGeneralEdge(const CLReferenceGlyph & e, const CLGeneralGlyph & r) const;
209  //double potReaction(const CLReactionGlyph & r) const;
210  double potSpeciesCompartment(const CLMetabGlyph & s, const CLCompartmentGlyph & c) const;
211  double potReactionCompartment(const CLReactionGlyph & r, const CLCompartmentGlyph & c) const;
212  //double potCompartmentCompartment(const CLCompartmentGlyph & c1, const CLCompartmentGlyph & c2) const;
213  double potSecondOrderEdge(const CLMetabReferenceGlyph & e1, const CLMetabReferenceGlyph & e2, double & dist) const;
214 
215  /**
216  * calculate a point just outside the bounding box of a given graphical object
217  * on the line between the center of the object and the point p.
218  * d specifies the distance from the border of the object.
219  */
220  CLPoint borderProjection(CLGraphicalObject* go, const CLPoint & p, double d);
221 
223 
225 
226  std::vector<double> mInitialState;
227 
228 public:
229  ///this describes one update action that has to be performed during setState()
231  {
233  {
237  };
238 
239  UpdateAction(Update_Enum action, CLBase* target, int index1 = -1, int index2 = -1, int index3 = -1, int index4 = -1)
240  : mAction(action),
241  mpTarget(target),
242  mIndex1(index1),
243  mIndex2(index2),
244  mIndex3(index3),
245  mIndex4(index4)
246  {};
247 
250  int mIndex1;
251  int mIndex2;
252  int mIndex3;
253  int mIndex4;
254  };
255 
256 protected:
257  ///this is the list of all update actions that have to be performed during setState();
258  std::vector<UpdateAction> mUpdateActions;
259 
260  ///this map contains information about the compartment glyph a given glyph is located in
261  std::map<CLBase*, CLCompartmentGlyph*> mCompartmentMap;
262 
263  /**
264  * this struct describes a constant relation between two graphical objects.
265  * It can be used to specify that a text glyph always has a certain position relativ to another object
266  */
268  {
272  };
273 
274  /// a list of fixed positon relations between objects. Should be constructed in initFromLayout()
275  std::vector<CoordinateRelation> mFixedRelations;
276 
277 public:
278  virtual const std::vector<UpdateAction>& getUpdateActions() const;
279 };
280 
281 #endif // CCopasiSpringLayout_H
bool initFromLayout(CLayout *layout, Parameters *ppp)
performs all initializations that are later needed to calculate the potential
CCopasiSpringLayout(CLayout *layout, Parameters *ppp=NULL)
virtual const std::vector< UpdateAction > & getUpdateActions() const
std::vector< double > mInitialState
UpdateAction(Update_Enum action, CLBase *target, int index1=-1, int index2=-1, int index3=-1, int index4=-1)
double potSecondOrderEdge(const CLMetabReferenceGlyph &e1, const CLMetabReferenceGlyph &e2, double &dist) const
static double distance(const double &x1, const double &y1, const double &x2, const double &y2)
double potEdge(const CLMetabReferenceGlyph &e, const CLReactionGlyph &r) const
Definition: CMetab.h:178
double potSpeciesCompartment(const CLMetabGlyph &s, const CLCompartmentGlyph &c) const
std::vector< UpdateAction > mUpdateActions
this is the list of all update actions that have to be performed during setState(); ...
double potGeneralEdge(const CLReferenceGlyph &e, const CLGeneralGlyph &r) const
CLPoint borderProjection(CLGraphicalObject *go, const CLPoint &p, double d)
std::vector< double > defaultValues
void addPositionVariables(CLGraphicalObject *pGO)
create variables for the position of a species glyph
Definition: CLBase.h:54
static CLayout * createLayout(CCopasiContainer *parent, const std::set< const CCompartment * > &compartments, const std::set< const CReaction * > &reactions, const std::set< const CMetab * > &metabs, const std::set< const CMetab * > &sideMetabs, Parameters *mParams=NULL)
Definition: CLBase.h:33
void addCompartmentVariables(CLCompartmentGlyph *cg)
create variables for size and position of a compartment glyph
virtual double getPotential()
std::vector< CoordinateRelation > mFixedRelations
a list of fixed positon relations between objects. Should be constructed in initFromLayout() ...
double potReactionReaction(const CLReactionGlyph &a, const CLReactionGlyph &b) const
static double bound_distance(const double &x1, const double &y1, const double &x2, const double &y2, const double &max)
this describes one update action that has to be performed during setState()
double potReactionCompartment(const CLReactionGlyph &r, const CLCompartmentGlyph &c) const
std::map< CLBase *, CLCompartmentGlyph * > mCompartmentMap
this map contains information about the compartment glyph a given glyph is located in ...
double potSpeciesSpecies(const CLMetabGlyph &a, const CLMetabGlyph &b) const
virtual const std::vector< double > & getInitialValues() const
double potSpeciesReaction(const CLMetabGlyph &a, const CLReactionGlyph &b) const
void addReactionVariables(CLReactionGlyph *rg)
create variables for the position of a reaction glyph
std::vector< std::string > names
virtual bool setState(const std::vector< double > &vars)
CLCompartmentGlyph * findCompartmentForReactionNode(CLReactionGlyph &r)
#define max(a, b)
Definition: f2c.h:176