libsbml-draw
network.h
Go to the documentation of this file.
1 /* MIT License
2  */
3 
4 //== FILEDOC =========================================================================
5 
10 //== BEGINNING OF CODE ===============================================================
11 
12 #ifndef __SBNW_NETWORK_H_
13 #define __SBNW_NETWORK_H_
14 
15 //== INCLUDES ========================================================================
16 
17 #include "SagittariusCore.h"
18 #include "point.h"
19 #include "curve.h"
20 #include "box.h"
21 #include "transform.h"
22 
23 //-- C++ code --
24 #ifdef __cplusplus
25 
26 #include "autolayoutSBML.h"
27 
28 #include "sbml/SBMLTypes.h"
29 #include "sbml/packages/layout/common/LayoutExtensionTypes.h"
30 
31 #include <string>
32 #include <iostream>
33 #include <typeinfo>
34 #include <stdint.h>
35 
36 namespace LibsbmlDraw {
37 
38  typedef enum {
39  NET_ELT_TYPE_SPEC,
40  NET_ELT_TYPE_RXN,
41  NET_ELT_TYPE_COMP
42  } NetworkEltType;
43 
44  std::string eltTypeToStr(const NetworkEltType t);
45 
46  void dumpEltType(std::ostream& os, const NetworkEltType t, uint32 ind);
47 
49  inline bool typeMatchEither(const NetworkEltType a, const NetworkEltType b, const NetworkEltType z) {
50  if(a == z)
51  return true;
52  else if(b == z)
53  return true;
54  else
55  return false;
56  }
57 
58  class Compartment;
59 
60  bool haveDefaultCompartmentId();
61 
62  void setDefaultCompartmentId(const std::string& id);
63 
64  std::string getDefaultCompartmentId();
65 
68  class SubCurve : public RxnBezier {
69  public:
71  RxnCurveType getRole() const {
72  return RXN_CURVE_SUBSTRATE;
73  }
74 
75  Point getCentroidCP() const { return c2; }
76 
77  bool isStartNodeSide() const { return true; }
78 
79  virtual bool hasArrowhead() const { return true; }
80 
81  ArrowheadStyle getArrowheadStyle() const;
82 
84  virtual Arrowhead* getArrowhead() {
85  Arrowhead* result = new SubstrateArrowhead();
86  transformArrowhead(*result);
87  return result;
88  }
89  };
90 
93  class PrdCurve : public RxnBezier {
94  public:
96  RxnCurveType getRole() const {
97  return RXN_CURVE_PRODUCT;
98  }
99 
100  Point getCentroidCP() const { return c1; }
101 
102  bool isStartNodeSide() const { return false; }
103 
104  virtual bool hasArrowhead() const { return true; }
105 
106  ArrowheadStyle getArrowheadStyle() const;
107 
109  virtual Arrowhead* getArrowhead() {
110  Arrowhead* result = new ProductArrowhead();
111  transformArrowhead(*result);
112  return result;
113  }
114  };
115 
118  class ActCurve : public RxnBezier {
119  public:
121  RxnCurveType getRole() const {
122  return RXN_CURVE_ACTIVATOR;
123  }
124 
125  Point getCentroidCP() const { return c2; }
126 
127  bool isStartNodeSide() const { return true; }
128 
129  virtual bool hasArrowhead() const { return true; }
130 
131  ArrowheadStyle getArrowheadStyle() const;
132 
134  virtual Arrowhead* getArrowhead() {
135  Arrowhead* result = new ActivatorArrowhead();
136  transformArrowhead(*result);
137  return result;
138  }
139  };
140 
143  class InhCurve : public RxnBezier {
144  public:
146  RxnCurveType getRole() const {
147  return RXN_CURVE_INHIBITOR;
148  }
149 
150  Point getCentroidCP() const { return c2; }
151 
152  bool isStartNodeSide() const { return true; }
153 
154  virtual bool hasArrowhead() const { return true; }
155 
156  ArrowheadStyle getArrowheadStyle() const;
157 
159  virtual Arrowhead* getArrowhead() {
160  Arrowhead* result = new InhibitorArrowhead();
161  transformArrowhead(*result);
162  return result;
163  }
164  };
165 
168  class ModCurve : public RxnBezier {
169  public:
171  RxnCurveType getRole() const {
172  return RXN_CURVE_MODIFIER;
173  }
174 
175  Point getCentroidCP() const { return c2; }
176 
177  bool isStartNodeSide() const { return true; }
178 
179  virtual bool hasArrowhead() const { return true; }
180 
181  ArrowheadStyle getArrowheadStyle() const;
182 
184  virtual Arrowhead* getArrowhead() {
185  Arrowhead* result = new ModifierArrowhead();
186  transformArrowhead(*result);
187  return result;
188  }
189  };
190 
191  extern ArrowheadStyle sub_arrow_style_;
192  inline ArrowheadStyle ArrowheadStyleLookup(const SubCurve* ) {
193  return sub_arrow_style_;
194  }
195 
196  extern ArrowheadStyle prod_arrow_style_;
197  inline ArrowheadStyle ArrowheadStyleLookup(const PrdCurve* ) {
198  return prod_arrow_style_;
199  }
200 
201  extern ArrowheadStyle act_arrow_style_;
202  inline ArrowheadStyle ArrowheadStyleLookup(const ActCurve* ) {
203  return act_arrow_style_;
204  }
205 
206  extern ArrowheadStyle inh_arrow_style_;
207  inline ArrowheadStyle ArrowheadStyleLookup(const InhCurve* ) {
208  return inh_arrow_style_;
209  }
210 
211  extern ArrowheadStyle mod_arrow_style_;
212  inline ArrowheadStyle ArrowheadStyleLookup(const ModCurve* ) {
213  return mod_arrow_style_;
214  }
215 
217  typedef enum {
218  ELT_SHAPE_ROUND,
219  ELT_SHAPE_RECT
220  } NetworkEltShape;
221 
224  class NetworkElement {
225  public:
226 
227  enum COORD_SYSTEM {
228  COORD_SYSTEM_LOCAL,
229  COORD_SYSTEM_GLOBAL
230  };
231 
232  NetworkElement()
233  : _pset(0), _v(0,0), _deg(0), _ldeg(0), _lock(0), networkEltBytePattern_(0x1199) {}
234 
236  NetworkEltType getType() const { return _type; }
237 
238  bool hasNetworkElementBase() {
239  if(networkEltBytePattern_ == 0x1199)
240  return true;
241  else
242  return false;
243  }
244 
246  void set_degree(uint64 deg) { _deg = deg; }
247  virtual uint64 degree() const { return _deg; }
248  virtual uint64& degree() { return _deg; }
249 
251  virtual void resetActivity();
252 
254  void addDelta(const Point& d);
255 
257  void capDelta(const Real cap);
258 
260  virtual void capDelta2(const Real cap2);
261 
263  virtual void doMotion(const Real scale);
264 
266  virtual void setCentroid(const Point& p);
267  virtual void setGlobalCentroid(const Point& p);
268  void setCentroid(Real x, Real y) { setCentroid(Point(x,y)); }
269 
270  virtual bool isCentroidSet() const { return _pset; }
271 
273  virtual Point getCentroid(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const;
274 
275 // SAGITTARIUS_DEPRECATED(Point getGlobalCentroid() const) { return tf_*_p; }
276 
278  Point getMin(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getMin(); }
279  Point getMax(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getMax(); }
280 
281  Real getMinX(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getMin().x; }
282  Real getMaxX(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getMax().x; }
283  Real getMinY(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getMin().y; }
284  Real getMaxY(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getMax().y; }
285 
287  Real getWidth(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { AT(getMaxX(coord) >= getMinX(coord)); return getMaxX(coord) - getMinX(coord); }
288  Real getHeight(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { AT(getMaxY(coord) >= getMinY(coord)); return getMaxY(coord) - getMinY(coord); }
289 
291  Real getGlobalWidth() const { AT(getMaxX() >= getMinX()); return (getMaxX() - getMinX())*tf_.scaleFactor(); }
292  Real getGlobalHeight() const { AT(getMaxY() >= getMinY()); return (getMaxY() - getMinY())*tf_.scaleFactor(); }
293 
296  virtual Box getExtents(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const {
297  switch (coord) {
298  case COORD_SYSTEM_LOCAL:
299  return getLocalExtents();
300  case COORD_SYSTEM_GLOBAL:
301  return tf_*getLocalExtents();
302  default:
303  AN(0, "Unknown coord system");
304  return getLocalExtents();
305  }
306  }
307 
310  virtual Box getLocalExtents() const {
311  return _ext;
312  }
313 // virtual SAGITTARIUS_DEPRECATED(Box getGlobalExtents() const) { return tf_*_ext; }
314 
316  void setExtents(const Box& b) { _ext = b; recalcCentroid(); }
317 
318  Box getBoundingBox() const { return getExtents(); }
319 // Box getBoundingBox() const { return Box(); }
320 
321  virtual void applyTransform(const Affine2d& t) {
322  _ext = xformBox(_ext, t);
323  _p = xformPoint(_p, t);
324  }
325 
326  virtual void applyDisplacement(const Point& d) {
327  _ext.displace(d);
328  _p += d;
329  }
330 
332  void recalcCentroid() { _p = (_ext.getMin() + _ext.getMax())*0.5; }
333 
335  virtual void recalcExtents() = 0;
336 
338  void lock() { _lock = 1; }
339 
341  void unlock() { _lock = 0; }
342 
343  // Is the node locked?
344  bool isLocked() const { return _lock; }
345 
346  NetworkEltShape getShape() const { return _shape; }
347 
348  //TODO: cache in member & ditch v func
349  virtual bool isContainer() const = 0;
350 
352  Real radius() const { return _r; }
353 
355  Real distance(const NetworkElement& e) const;
356 
358  bool overlap(const NetworkElement& e) const;
359 
361  // TODO: rename; is actually displacement
362  Point forceVec(const NetworkElement& e) const;
363 
365  Point centroidDisplacementFrom(const NetworkElement& e) const;
366 
368  void forceVec_(const NetworkElement& e, Point& p) const;
369 
370  virtual void dump(std::ostream& os, uint32 ind) = 0;
371 
373  virtual void dumpForces(std::ostream& os, uint32 ind) const = 0;
374 
375  virtual Affine2d getTransform() const { return tf_; }
376 
377  virtual void setTransform(const Affine2d& tf, bool recurse = true) { tf_ = tf; }
378 
379  virtual Affine2d getInverseTransform() const { return itf_; }
380 
381  virtual void setInverseTransform(const Affine2d& itf, bool recurse = true) { itf_ = itf; }
382 
384  Point _p;
386  uint64 _deg;
388  uint64 _ldeg;
389  protected:
391  int _pset;
393  Point _v;
395  Box _ext;
397  Real _r;
399  NetworkEltShape _shape;
401  NetworkEltType _type;
403  int _lock;
405  Affine2d tf_;
407  Affine2d itf_;
408 
409  long networkEltBytePattern_;
410  };
411 
412  class Network;
413 
416  class Node : public NetworkElement {
417  public:
418 
419  Node()
420  : NetworkElement() {
421  _shape = ELT_SHAPE_RECT;
422  _comp = NULL;
423  _type = NET_ELT_TYPE_SPEC;
424  _ext = Box(0,0,40,20);
425  bytepattern = 0xc455;
426  isub_ = -1;
427  }
428 
429  // Model:
430 
432  void setName(const std::string& name);
433 
435  const std::string& getName() const;
436 
438  const std::string& getId() const;
439 
441  void setId(const std::string& id);
442 
444  const std::string& getGlyph() const;
445 
447  void setGlyph(const std::string& id);
448 
449  // Alias info:
450 
452  uint32& numUses() { return _numUses; }
453  uint32 numUses() const { return _numUses; }
454 
456  //TODO: change to isAliased. There is no such thing as an "alias node"
457  bool isAlias() const { return _isAlias; }
458 
460  bool isCommonInstance(const Node* other) const;
461 
463  void setAlias(bool b) { _isAlias = b; }
464 
465  int alias(Network* net);
466 
467  int getSubgraphIndex() const {
468  if (isub_ < 0)
469  SBNW_THROW(InvalidParameterException, "No subgraph index set", "Network::getSubgraphIndex");
470  return isub_;
471  }
472 
473  void setSubgraphIndex(int v) { isub_ = v; }
474 
475  bool isSetSubgraphIndex() { return isub_ < 0; }
476 
477  void clearSubgraphIndex() { isub_ = -1; }
478 
479  bool excludeFromSubgraphEnum() const { return exsub_; }
480 
481  bool setExcludeFromSubgraphEnum() { return exsub_ = true; }
482 
483  void clearExcludeFromSubgraphEnum() { exsub_ = false; }
484 
485  // Coordinates/dimensions:
486 
488  Point getUpperLeftCorner() const;
490  Point getLowerRightCorner() const;
491 
492  void recalcExtents() {
493  Real width = _ext.width();
494  Real height = _ext.height();
495  Point del(0.5*width, 0.5*height);
496 
497  _ext = Box(_p - del, _p + del);
498  _r = _ext.maxDim()*0.5;
499  }
500 
502  void setWidth(Real w);
503 
505  void setHeight(Real h);
506 
508  void affectGlobalWidth(Real w);
509 
511  void affectGlobalHeight(Real h);
512 
513  void set_i(size_t i) { i_ = i; }
514  size_t get_i() const { return i_; }
515 
516  // Layout:
517 
518  bool isContainer() const { return false; }
519 
520  // IO:
521 
523  void dump(std::ostream& os, uint32 ind);
524 
526  void dumpForces(std::ostream& os, uint32 ind) const;
527 
528  Compartment* _comp;
529 
530  bool doByteCheck() { if(bytepattern == 0xc455) return true; else return false; }
531 
532  long bytepattern;
533  protected:
534  // model info:
535  std::string _name, _id;
537  std::string _gly;
538  // aliasing:
539  uint32 _numUses;
540  bool _isAlias;
541  // rendering:
543 // Real _hemiw, _hemih;
544 
545  // index in network
546  size_t i_;
547  int isub_;
548  bool exsub_;
549  };
550 
552  inline Node* CastToNode(void* p) {
553  NetworkElement* e = (NetworkElement*)p;
554  AN(e->hasNetworkElementBase(), "Runtime type check failed");
555 // std::cout << typeid((Node&)*e).name() << std::endl;
556  AN(dynamic_cast<Node*>(e), "Runtime type check failed");
557  return dynamic_cast<Node*>(e);
558 // Node* x = (Node*)e;
559 // AN(x->doByteCheck(), "Runtime type check failed");
560 // return x;
561  }
562 
563  typedef enum {
564  RXN_ROLE_SUBSTRATE,
565  RXN_ROLE_PRODUCT,
566  RXN_ROLE_SIDESUBSTRATE,
567  RXN_ROLE_SIDEPRODUCT,
568  RXN_ROLE_MODIFIER,
569  RXN_ROLE_ACTIVATOR,
570  RXN_ROLE_INHIBITOR,
571  } RxnRoleType;
572 
573  class RxnCurveFactory {
574  public:
575  static RxnBezier* CreateCurve(RxnRoleType role);
576  };
577 
580  class Reaction : public NetworkElement {
581  public:
582  // Exposed types:
583  typedef std::pair<Node*, RxnRoleType> SpeciesElt;
585  typedef std::vector< SpeciesElt > NodeVec;
587  //typedef std::vector<RxnRoleType> RoleVec;
589  typedef std::vector<RxnBezier*> CurveVec;
590 
591  // Iterators:
592  typedef NodeVec::iterator NodeIt;
593  typedef NodeVec::const_iterator ConstNodeIt;
594 
595  /*typedef RoleVec::iterator RoleIt;
596  typedef RoleVec::const_iterator ConstRoleIt;*/
597 
598  typedef CurveVec::iterator CurveIt;
599  typedef CurveVec::const_iterator ConstCurveIt;
600 
601  NodeIt NodesBegin() { return _spec.begin(); }
602  NodeIt NodesEnd() { return _spec.end(); }
603 
604  ConstNodeIt NodesBegin() const { return _spec.begin(); }
605  ConstNodeIt NodesEnd() const { return _spec.end(); }
606 
607  CurveIt CurvesBegin() { return _curv.begin(); }
608  CurveIt CurvesEnd() { return _curv.end(); }
609 
610  ConstCurveIt CurvesBegin() const { return _curv.begin(); }
611  ConstCurveIt CurvesEnd() const { return _curv.end(); }
612 
613  // Methods:
614 
615  Reaction()
616  : NetworkElement() {
617  _shape = ELT_SHAPE_ROUND;
618  _type = NET_ELT_TYPE_RXN;
619  bytepattern = 0xff83;
620  }
621 
622  void hierarchRelease();
623 
624  // Model:
625 
627  const std::string& getId() const { return _id; }
628 
630  void setId(const std::string& id) { _id = id; }
631 
632  void setName(const std::string& name) { name_ = name; }
633 
634  // Species:
635 
637  uint64 numSpecies() const { return _spec.size(); }
638 
640  void addSpeciesRef(Node* n, RxnRoleType role);
641 
643  void removeNode(Node* n);
644 
646  Node* findSpeciesById(const std::string& id);
647 
649  bool hasSpecies(const Node* n) const;
650 
652  uint64 degree(const Node* n);
653 
654  RxnRoleType getSpeciesRole(size_t i) { return _spec.at(i).second; }
655 
656  RxnRoleType getSpeciesRole(Node* n);
657 
658  Node* getSpecies(size_t i) { return _spec.at(i).first; }
659 
664  void substituteSpeciesById(const std::string& id, Node* spec);
665 
668  void substituteSpeciesByIdwRole(const std::string& id, Node* spec, RxnRoleType role);
669 
672  void substituteSpecies(Node* before, Node* after);
673 
675  CurveVec& getCurves();
676  // this space intentionally left blank
677 
679  size_t getNumCurves() { curveGuard(); return _curv.size(); }
680 
681 
683  RxnBezier* getCurve(size_t i) { curveGuard(); return _curv.at(i); }
684 
686  NodeVec& getSpec() { return _spec; }
687  const NodeVec& getSpec() const { return _spec; }
688 
690  void forceRecalcCentroid();
691 
695  void rebuildCurves();
696 
697  Real calcArrowGap(RxnBezier* c, Node* n);
698 
703  void recalcCurveCPs();
704 
707  void recenter();
708 
709  // Layout
710 
711  bool isContainer() const { return false; }
712 
714  void recalcExtents() {
715  _r = 10.;
716  _ext = Box(_p - Point(_r,_r), _p + Point(_r,_r));
717  }
718 
719  virtual Box getLocalExtents() const {
720  return Box(getCentroid() - Point(5, 5), getCentroid() + Point(5, 5));
721  }
722 
723  virtual void applyTransform(const Affine2d& t) {
724  NetworkElement::applyTransform(t);
725  for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
726  (*i)->applyTransform(t);
727  }
728  }
729 
730  virtual void setTransform(const Affine2d& tf, bool recurse = true) {
731  tf_ = tf;
732  for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
733  (*i)->setTransform(tf);
734  }
735  }
736 
737  virtual void setInverseTransform(const Affine2d& itf, bool recurse = true) {
738  itf_ = itf;
739  for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
740  (*i)->setInverseTransform(itf);
741  }
742  }
743 
744  // IO
745  void dump(std::ostream& os, uint32 ind);
746 
748  void dumpForces(std::ostream& os, uint32 ind) const;
749 
750  bool doByteCheck() { if(bytepattern == 0xff83) return true; else return false; }
751 
752  void clearDirtyFlag() { _cdirty = false; }
753 
755  RxnBezier* addCurve(RxnRoleType role) {
756  _curv.push_back(RxnCurveFactory::CreateCurve(role));
757  _curv.back()->setTransform(itf_);
758  _curv.back()->setInverseTransform(itf_);
759  return _curv.back();
760  }
761 
763  void deleteCurves();
764 
765  protected:
766  // Methods:
767 
769  void rebuildAll();
770 
772  void recompCentroid();
773 
775  void doCentroidCalc();
776 
777  void curveGuard() {
778  if(_cdirty && _spec.size()) {
779  rebuildCurves();
780 // recenter();
781  }
782  }
783 
784  // Variables:
785  // model:
786  std::string _id;
787  std::string name_;
788  // dims:
790  Point _v;
792  //NodeVec _rct;
794  //NodeVec _prd;
796  NodeVec _spec;
798  //RoleVec _role;
800  CurveVec _curv;
802  bool _cdirty;
803 
804  long bytepattern;
805  };
806 
808  inline Reaction* CastToReaction(void* p) {
809  NetworkElement* e = (NetworkElement*)p;
810  AN(e->hasNetworkElementBase(), "Runtime type check failed");
811  AN(dynamic_cast<Reaction*>(e), "Runtime type check failed");
812  return dynamic_cast<Reaction*>(e);
813  }
814 
815  typedef enum {
816  COMP_EDGE_TYPE_TOP,
817  COMP_EDGE_TYPE_LEFT,
818  COMP_EDGE_TYPE_BOTTOM,
819  COMP_EDGE_TYPE_RIGHT
820  } CompartmentEdgeType;
821 
824  class Compartment : public NetworkElement {
825  public:
826 
827  // Exposed types:
828 
830  typedef std::vector<LibsbmlDraw::NetworkElement*> EltVec;
831 
832  typedef EltVec::iterator EltIt;
833  typedef EltVec::const_iterator ConstEltIt;
834 
835  EltIt EltsBegin() { return _elt.begin(); }
836  EltIt EltsEnd() { return _elt.end(); }
837 
838  ConstEltIt EltsBegin() const { return _elt.begin(); }
839  ConstEltIt EltsEnd() const { return _elt.end(); }
840 
841  LibsbmlDraw::NetworkElement* getElt(const uint64 i) { return _elt.at(i); }
842 
843  const LibsbmlDraw::NetworkElement* getElt(const uint64 i) const { return _elt.at(i); }
844 
845  uint64 getNElts() const { return _elt.size(); }
846 
847  Compartment()
848  : /*_nu(0.3),*/ _ra(50.*50.), _E(10.), _res(0.25), bytepattern(0xffae11), NetworkElement() {
849  _shape = ELT_SHAPE_RECT;
850  _type = NET_ELT_TYPE_COMP;
851  }
852 
854  const std::string& getId() const { return _id; }
855 
857  void setId(const std::string& id) { _id = id; }
858 
860  void setName(const std::string& name) { name_ = name; }
861 
863  const std::string& getGlyph() const { return _gly; }
864 
866  void setGlyph(const std::string& glyph) { _gly = glyph; }
867 
868  void setCentroid(const Point& p) {
869  AN(0, "setCentroid should not be called on a compt");
870  }
871 
873  void recalcExtents() {
874  _r = _ext.maxDim()*0.5;
875  _p = (_ext.getMin() + _ext.getMax())*0.5;
876  }
877 
878  // Elements
879 
881  void addElt(NetworkElement* e);
882 
883  bool containsElt(const NetworkElement* e) const;
884 
886  void removeElt(NetworkElement* e);
887 
889  void setRestExtents(const Box& ext);
890 
892  void resizeEnclose(double padding = 0);
893 
895  void autoSize();
896 
898  Real restArea() const { return _ra; }
899 
900  void setMin(const Point& p) { _ext.setMin(p); }
901  void setMax(const Point& p) { _ext.setMax(p); }
902 
903  virtual Point getCentroid(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getCenter(); }
904 
905  // Layout engine:
906 
908  virtual void resetActivity();
909 
916  void applyBoundaryForce(const Real fx1, const Real fy1, const Real fx2, const Real fy2);
917 
923  void doInternalForce(NetworkElement* e, const Real f, const Real t);
924 
926  void doInternalForceAll(const Real f, const Real t);
927 
929  void doMotion(const Real scale);
930 
931  void capDelta2(const Real cap2);
932 
933  bool isContainer() const { return true; }
934 
936  bool contains(const NetworkElement* e) const;
937 
939  bool empty() const { return _elt.size() ? false : true; }
940 
942  void dump(std::ostream& os, uint32 ind);
943 
945  void dumpForces(std::ostream& os, uint32 ind) const;
946 
947  bool doByteCheck() { if(bytepattern == 0xffae11) return true; else return false; }
948 
949  protected:
951  std::string _id;
953  std::string name_;
955  std::string _gly;
957  EltVec _elt;
959  Real _ra;
961  Real _E;
963  //Real _nu;
965  Real _fx1, _fy1, _fx2, _fy2;
967  Real _res;
968 
969  uint64_t bytepattern;
970  };
971 
974  class Network : public Compartment {
975  public:
976  // Exposed types:
977 
979  typedef std::vector<Node*> NodeVec;
981  typedef std::vector<LibsbmlDraw::Reaction*> RxnVec;
983  typedef std::vector<LibsbmlDraw::Compartment*> CompVec;
984 
985  //iterators
986  typedef NodeVec::iterator NodeIt;
987  typedef NodeVec::const_iterator ConstNodeIt;
988 
989  typedef RxnVec::iterator RxnIt;
990  typedef RxnVec::const_iterator ConstRxnIt;
991 
992  typedef CompVec::iterator CompIt;
993  typedef CompVec::const_iterator ConstCompIt;
994 
995  // Constructors:
996 
997  Network() {
998  bytepattern = 0x3355;
999  layoutspecified_ = false;
1000  }
1001 
1002  // Methods:
1003 
1005  void hierarchRelease();
1006 
1007  // Nodes:
1008 
1010  void addNode(Node* n);
1011 
1014  void removeNode(Node* n);
1015 
1017  void connectNode(Node* n, Reaction* r, RxnRoleType role);
1018 
1020  bool isNodeConnected(Node* n, Reaction* r) const;
1021 
1023  Node* findNodeById(const std::string& id);
1024  const Node* findNodeById(const std::string& id) const;
1025 
1027  std::string getUniqueId() const;
1028 
1029  std::string getUniqueGlyphId(const Node& src) const;
1030 
1032  std::size_t getUniqueIndex() const;
1033 
1035  Node* findNodeByGlyph(const std::string& gly);
1036 
1037  Node* getNodeAt(const size_t i) { return _nodes.at(i); }
1038 
1039  Node* getUniqueNodeAt(const size_t n);
1040 
1041  size_t getNumInstances(const Node* u);
1042 
1044  Node* getInstance(const Node* u, const size_t n);
1045 
1046  bool containsNode(const Node* n) const;
1047 
1048  bool containsReaction(const Reaction* r) const;
1049 
1050  typedef std::vector<Reaction*> AttachedRxnList;
1051 
1052  AttachedRxnList getConnectedReactions(const Node* n);
1053 
1054  typedef std::vector<RxnBezier*> AttachedCurveList;
1055 
1056  AttachedCurveList getAttachedCurves(const Node* n);
1057 
1058  int getNumSubgraphs();
1059 
1061  void enumerateSubgraphs();
1062 
1064  void propagateSubgraphIndex(Node* x, int isub);
1065 
1066  void clearSubgraphInfo();
1067 
1068  void clearExcludeFromSubgraphEnum();
1069 
1071  Reaction* findReactionById(const std::string& id);
1072 
1073  Reaction* getRxnAt(const size_t i) { return _rxn.at(i); }
1074 
1076  void resetUsageInfo();
1077 
1079  const std::string& getId() const { return _id; }
1080 
1081  void setId(const std::string& id) { _id = id; }
1082 
1083  bool isSetId() const { return _id.size(); }
1084 
1085  bool isLayoutSpecified() const { return layoutspecified_; }
1086 
1087  void setLayoutSpecified(bool value) {
1088  layoutspecified_ = value;
1089  }
1090 
1091  // Reactions:
1092 
1094  void addReaction(Reaction* rxn);
1095 
1097  void removeReaction(Reaction* r);
1098 
1099  // Compartments:
1100 
1102  void addCompartment(Compartment* c) { _comp.push_back(c); addElt(c); }
1103 
1107  Compartment* findCompById(const std::string& id);
1108 
1112  Compartment* findCompByGlyph(const std::string& gly);
1113 
1114  Compartment* getCompAt(const size_t i) { return _comp.at(i); }
1115 
1116  Compartment* findContainingCompartment(const NetworkElement* e);
1117 
1118  // Layout:
1119 
1120  uint64 getTotalNumComps() const { return _comp.size(); }
1121 
1123  uint64 getTotalNumPts() const { return _nodes.size() + _rxn.size(); }
1124 
1126  uint64 getTotalNumRxns() const { return _rxn.size(); }
1127 
1129  uint64 getTotalNumNodes() const { return _nodes.size(); }
1130 
1131  uint64 getNumUniqueNodes() const;
1132 
1133  Box getBoundingBox() const;
1134 
1135  void fitToWindow(const Box& w);
1136 
1137  void applyTransform(const Affine2d& t);
1138 
1139  void setTransform(const Affine2d& t, bool recurse = true);
1140 
1141  void setInverseTransform(const Affine2d& it, bool recurse = true);
1142 
1143  void applyDisplacement(const Point& d);
1144 
1146  void elideEmptyComps();
1147 
1149  void randomizePositions(const Box& bounds);
1150 
1152  void rebuildCurves();
1153 
1156  void recalcCurveCPs();
1157 
1159  void recenterJunctions();
1160 
1162  void resetActivity();
1163 
1169  //void doNodeBoxContactForce(const Box& b, const Real f, const Real t);
1170 
1174  void capDeltas(const Real cap);
1175 
1179  void updatePositions(const Real scale);
1180 
1182  void updateExtents();
1183 
1185  void resizeCompsEnclose(double padding = 0);
1186 
1188  void autosizeComps();
1189 
1192  Point pmean() const;
1193 
1196  Point center() const;
1197 
1200  Box getExtents() const;
1201 
1205  void recenter(const Point& p);
1206 
1209  //Real diam(const Point& p);
1210 
1213  Point pvariance() const;
1214 
1215  // IO/Diagnostics:
1216 
1218  void dump(std::ostream& os, uint32 ind);
1219 
1221  void dumpEltForces(std::ostream& os, uint32 ind) const;
1222 
1223  Node* getNodeAtIndex(int index) { return _nodes[index]; }
1224 
1225  //iterators
1226 
1227  NodeIt NodesBegin() { return _nodes.begin(); }
1228  NodeIt NodesEnd() { return _nodes.end(); }
1229 
1230  ConstNodeIt NodesBegin() const { return _nodes.begin(); }
1231  ConstNodeIt NodesEnd() const { return _nodes.end(); }
1232 
1233  RxnIt RxnsBegin() { return _rxn.begin(); }
1234  RxnIt RxnsEnd() { return _rxn.end(); }
1235 
1236  ConstRxnIt RxnsBegin() const { return _rxn.begin(); }
1237  ConstRxnIt RxnsEnd() const { return _rxn.end(); }
1238 
1239  CompIt CompsBegin() { return _comp.begin(); }
1240  CompIt CompsEnd() { return _comp.end(); }
1241 
1242  ConstCompIt CompsBegin() const { return _comp.begin(); }
1243  ConstCompIt CompsEnd() const { return _comp.end(); }
1244 
1245  bool doByteCheck() const { if(bytepattern == 0x3355) return true; else return false; }
1246  protected:
1247 
1248  void removeReactionsForNode(Node* n);
1249 
1251  NodeVec _nodes;
1253  RxnVec _rxn;
1255  CompVec _comp;
1256 
1257  long bytepattern;
1258  bool layoutspecified_;
1259 
1261  int nsub_;
1262  };
1263 
1265  inline Network* CastToNetwork(void* p) {
1266  NetworkElement* e = (NetworkElement*)p;
1267  AN(e->hasNetworkElementBase(), "Runtime type check failed");
1268  return dynamic_cast<Network*>(e);
1269  }
1270 
1271  // Methods:
1272 
1279  Network* networkFromLayout(const Layout& lay, const Model& mod);
1280 
1286  Network* networkFromModel(const Model& mod);
1287 
1290  int layout_getNumFloatingSpecies(const Layout& lay, const Model& mod);
1291 
1292 }
1293 
1294 #endif
1295 
1296 #endif
SAGITTARIUS_REAL Real
Make Real visible to C. Consider letting Real lie in top namespace.
Definition: SagittariusCommon.h:112
A box.
First file included.
Roots of cubic equations.