libSBNW
 All Classes Namespaces Files Functions Variables Typedefs Macros Modules Pages
network.h
Go to the documentation of this file.
1 /*== SAGITTARIUS =====================================================================
2  * Copyright (c) 2012, Jesse K Medley
3  * All rights reserved.
4 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of The University of Washington nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 //== FILEDOC =========================================================================
29 
34 //== BEGINNING OF CODE ===============================================================
35 
36 #ifndef __GRAPHFAB_NETWORK_H_
37 #define __GRAPHFAB_NETWORK_H_
38 
39 //== INCLUDES ========================================================================
40 
42 #include "graphfab/layout/point.h"
43 #include "graphfab/layout/curve.h"
44 #include "graphfab/layout/box.h"
46 
47 //-- C++ code --
48 #ifdef __cplusplus
49 
50 #include "graphfab/sbml/autolayoutSBML.h"
51 
52 #include "sbml/SBMLTypes.h"
53 #include "sbml/packages/layout/common/LayoutExtensionTypes.h"
54 
55 #include <string>
56 #include <iostream>
57 #include <typeinfo>
58 
59 namespace Graphfab {
60 
61  typedef enum {
62  NET_ELT_TYPE_SPEC,
63  NET_ELT_TYPE_RXN,
64  NET_ELT_TYPE_COMP
65  } NetworkEltType;
66 
67  std::string eltTypeToStr(const NetworkEltType t);
68 
69  void dumpEltType(std::ostream& os, const NetworkEltType t, uint32 ind);
70 
72  inline bool typeMatchEither(const NetworkEltType a, const NetworkEltType b, const NetworkEltType z) {
73  if(a == z)
74  return true;
75  else if(b == z)
76  return true;
77  else
78  return false;
79  }
80 
81  class Compartment;
82 
85  class SubCurve : public RxnBezier {
86  public:
88  RxnCurveType getRole() const {
89  return RXN_CURVE_SUBSTRATE;
90  }
91 
92  Point getCentroidCP() const { return c2; }
93 
94  bool isStartNodeSide() const { return true; }
95 
96  virtual bool hasArrowhead() const { return true; }
97 
98  ArrowheadStyle getArrowheadStyle() const;
99 
101  virtual Arrowhead* getArrowhead() {
102  Arrowhead* result = new SubstrateArrowhead();
103  transformArrowhead(*result);
104  return result;
105  }
106  };
107 
110  class PrdCurve : public RxnBezier {
111  public:
113  RxnCurveType getRole() const {
114  return RXN_CURVE_PRODUCT;
115  }
116 
117  Point getCentroidCP() const { return c1; }
118 
119  bool isStartNodeSide() const { return false; }
120 
121  virtual bool hasArrowhead() const { return true; }
122 
123  ArrowheadStyle getArrowheadStyle() const;
124 
126  virtual Arrowhead* getArrowhead() {
127  Arrowhead* result = new ProductArrowhead();
128  transformArrowhead(*result);
129  return result;
130  }
131  };
132 
135  class ActCurve : public RxnBezier {
136  public:
138  RxnCurveType getRole() const {
139  return RXN_CURVE_ACTIVATOR;
140  }
141 
142  Point getCentroidCP() const { return c2; }
143 
144  bool isStartNodeSide() const { return true; }
145 
146  virtual bool hasArrowhead() const { return true; }
147 
148  ArrowheadStyle getArrowheadStyle() const;
149 
151  virtual Arrowhead* getArrowhead() {
152  Arrowhead* result = new ActivatorArrowhead();
153  transformArrowhead(*result);
154  return result;
155  }
156  };
157 
160  class InhCurve : public RxnBezier {
161  public:
163  RxnCurveType getRole() const {
164  return RXN_CURVE_INHIBITOR;
165  }
166 
167  Point getCentroidCP() const { return c2; }
168 
169  bool isStartNodeSide() const { return true; }
170 
171  virtual bool hasArrowhead() const { return true; }
172 
173  ArrowheadStyle getArrowheadStyle() const;
174 
176  virtual Arrowhead* getArrowhead() {
177  Arrowhead* result = new InhibitorArrowhead();
178  transformArrowhead(*result);
179  return result;
180  }
181  };
182 
185  class ModCurve : public RxnBezier {
186  public:
188  RxnCurveType getRole() const {
189  return RXN_CURVE_MODIFIER;
190  }
191 
192  Point getCentroidCP() const { return c2; }
193 
194  bool isStartNodeSide() const { return true; }
195 
196  virtual bool hasArrowhead() const { return true; }
197 
198  ArrowheadStyle getArrowheadStyle() const;
199 
201  virtual Arrowhead* getArrowhead() {
202  Arrowhead* result = new ModifierArrowhead();
203  transformArrowhead(*result);
204  return result;
205  }
206  };
207 
208  extern ArrowheadStyle sub_arrow_style_;
209  inline ArrowheadStyle ArrowheadStyleLookup(const SubCurve* ) {
210  return sub_arrow_style_;
211  }
212 
213  extern ArrowheadStyle prod_arrow_style_;
214  inline ArrowheadStyle ArrowheadStyleLookup(const PrdCurve* ) {
215  return prod_arrow_style_;
216  }
217 
218  extern ArrowheadStyle act_arrow_style_;
219  inline ArrowheadStyle ArrowheadStyleLookup(const ActCurve* ) {
220  return act_arrow_style_;
221  }
222 
223  extern ArrowheadStyle inh_arrow_style_;
224  inline ArrowheadStyle ArrowheadStyleLookup(const InhCurve* ) {
225  return inh_arrow_style_;
226  }
227 
228  extern ArrowheadStyle mod_arrow_style_;
229  inline ArrowheadStyle ArrowheadStyleLookup(const ModCurve* ) {
230  return mod_arrow_style_;
231  }
232 
234  typedef enum {
235  ELT_SHAPE_ROUND,
236  ELT_SHAPE_RECT
237  } NetworkEltShape;
238 
241  class NetworkElement {
242  public:
243 
244  enum COORD_SYSTEM {
245  COORD_SYSTEM_LOCAL,
246  COORD_SYSTEM_GLOBAL
247  };
248 
249  NetworkElement()
250  : _pset(0), _v(0,0), _deg(0), _ldeg(0), _lock(0), networkEltBytePattern_(0x1199) {}
251 
253  NetworkEltType getType() const { return _type; }
254 
255  bool hasNetworkElementBase() {
256  if(networkEltBytePattern_ == 0x1199)
257  return true;
258  else
259  return false;
260  }
261 
263  void set_degree(uint64 deg) { _deg = deg; }
264  virtual uint64 degree() const { return _deg; }
265  virtual uint64& degree() { return _deg; }
266 
268  virtual void resetActivity();
269 
271  void addDelta(const Point& d);
272 
274  void capDelta(const Real cap);
275 
277  virtual void capDelta2(const Real cap2);
278 
280  virtual void doMotion(const Real scale);
281 
283  virtual void setCentroid(const Point& p);
284  virtual void setGlobalCentroid(const Point& p);
285  void setCentroid(Real x, Real y) { setCentroid(Point(x,y)); }
286 
287  virtual bool isCentroidSet() const { return _pset; }
288 
290  virtual Point getCentroid(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const;
291 
292 // SAGITTARIUS_DEPRECATED(Point getGlobalCentroid() const) { return tf_*_p; }
293 
295  Point getMin(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getMin(); }
296  Point getMax(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getMax(); }
297 
298  Real getMinX() const { return getExtents().getMin().x; }
299  Real getMaxX() const { return getExtents().getMax().x; }
300  Real getMinY() const { return getExtents().getMin().y; }
301  Real getMaxY() const { return getExtents().getMax().y; }
302 
304  Real getWidth() const { AT(getMaxX() >= getMinX()); return getMaxX() - getMinX(); }
305  Real getHeight() const { AT(getMaxY() >= getMinY()); return getMaxY() - getMinY(); }
306 
308  Real getGlobalWidth() const { AT(getMaxX() >= getMinX()); return (getMaxX() - getMinX())*tf_.scaleFactor(); }
309  Real getGlobalHeight() const { AT(getMaxY() >= getMinY()); return (getMaxY() - getMinY())*tf_.scaleFactor(); }
310 
313  virtual Box getExtents(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const {
314  switch (coord) {
315  case COORD_SYSTEM_LOCAL:
316  return getLocalExtents();
317  case COORD_SYSTEM_GLOBAL:
318  return tf_*getLocalExtents();
319  default:
320  AN(0, "Unknown coord system");
321  return getLocalExtents();
322  }
323  }
324 
327  virtual Box getLocalExtents() const {
328  return _ext;
329  }
330 // virtual SAGITTARIUS_DEPRECATED(Box getGlobalExtents() const) { return tf_*_ext; }
331 
333  void setExtents(const Box& b) { _ext = b; recalcCentroid(); }
334 
335  Box getBoundingBox() const { return getExtents(); }
336 // Box getBoundingBox() const { return Box(); }
337 
338  virtual void applyTransform(const Affine2d& t) {
339  _ext = xformBox(_ext, t);
340  _p = xformPoint(_p, t);
341  }
342 
344  void recalcCentroid() { _p = (_ext.getMin() + _ext.getMax())*0.5; }
345 
347  virtual void recalcExtents() = 0;
348 
350  void lock() { _lock = 1; }
351 
353  void unlock() { _lock = 0; }
354 
355  // Is the node locked?
356  bool isLocked() const { return _lock; }
357 
358  NetworkEltShape getShape() const { return _shape; }
359 
360  //TODO: cache in member & ditch v func
361  virtual bool isContainer() const = 0;
362 
364  Real radius() const { return _r; }
365 
367  Real distance(const NetworkElement& e) const;
368 
370  bool overlap(const NetworkElement& e) const;
371 
373  // TODO: rename; is actually displacement
374  Point forceVec(const NetworkElement& e) const;
375 
377  Point centroidDisplacementFrom(const NetworkElement& e) const;
378 
380  void forceVec_(const NetworkElement& e, Point& p) const;
381 
382  virtual void dump(std::ostream& os, uint32 ind) = 0;
383 
385  virtual void dumpForces(std::ostream& os, uint32 ind) const = 0;
386 
387  virtual Affine2d getTransform() const { return tf_; }
388 
389  virtual void setTransform(const Affine2d& tf, bool recurse = true) { tf_ = tf; }
390 
391  virtual Affine2d getInverseTransform() const { return itf_; }
392 
393  virtual void setInverseTransform(const Affine2d& itf, bool recurse = true) { itf_ = itf; }
394 
396  Point _p;
398  uint64 _deg;
400  uint64 _ldeg;
401  protected:
403  int _pset;
405  Point _v;
407  Box _ext;
409  Real _r;
411  NetworkEltShape _shape;
413  NetworkEltType _type;
415  int _lock;
417  Affine2d tf_;
419  Affine2d itf_;
420 
421  long networkEltBytePattern_;
422  };
423 
424  class Network;
425 
428  class Node : public NetworkElement {
429  public:
430 
431  Node()
432  : NetworkElement() {
433  _shape = ELT_SHAPE_RECT;
434  _comp = NULL;
435  _type = NET_ELT_TYPE_SPEC;
436  _ext = Box(0,0,40,20);
437  bytepattern = 0xc455;
438  isub_ = -1;
439  }
440 
441  // Model:
442 
444  void setName(const std::string& name);
445 
447  const std::string& getName() const;
448 
450  const std::string& getId() const;
451 
453  void setId(const std::string& id);
454 
456  const std::string& getGlyph() const;
457 
459  void setGlyph(const std::string& id);
460 
461  // Alias info:
462 
464  uint32& numUses() { return _numUses; }
465  uint32 numUses() const { return _numUses; }
466 
468  //TODO: change to isAliased. There is no such thing as an "alias node"
469  bool isAlias() const { return _isAlias; }
470 
472  void setAlias(bool b) { _isAlias = b; }
473 
474  int alias(Network* net);
475 
476  int getSubgraphIndex() const {
477  if (isub_ < 0)
478  GRAPHFAB_THROW(InvalidParameterException, "No subgraph index set", "Network::getSubgraphIndex");
479  return isub_;
480  }
481 
482  void setSubgraphIndex(int v) { isub_ = v; }
483 
484  bool isSetSubgraphIndex() { return isub_ < 0; }
485 
486  void clearSubgraphIndex() { isub_ = -1; }
487 
488  bool excludeFromSubgraphEnum() const { return exsub_; }
489 
490  bool setExcludeFromSubgraphEnum() { return exsub_ = true; }
491 
492  void clearExcludeFromSubgraphEnum() { exsub_ = false; }
493 
494  // Coordinates/dimensions:
495 
497  Point getUpperLeftCorner() const;
499  Point getLowerRightCorner() const;
500 
501  void recalcExtents() {
502  Real width = _ext.width();
503  Real height = _ext.height();
504  Point del(0.5*width, 0.5*height);
505 
506  _ext = Box(_p - del, _p + del);
507  _r = _ext.maxDim()*0.5;
508  }
509 
511  void setWidth(Real w);
512 
514  void setHeight(Real h);
515 
517  void affectGlobalWidth(Real w);
518 
520  void affectGlobalHeight(Real h);
521 
522  void set_i(size_t i) { i_ = i; }
523  size_t get_i() const { return i_; }
524 
525  // Layout:
526 
527  bool isContainer() const { return false; }
528 
529  // IO:
530 
532  void dump(std::ostream& os, uint32 ind);
533 
535  void dumpForces(std::ostream& os, uint32 ind) const;
536 
537  Compartment* _comp;
538 
539  bool doByteCheck() { if(bytepattern == 0xc455) return true; else return false; }
540 
541  long bytepattern;
542  protected:
543  // model info:
544  std::string _name, _id;
546  std::string _gly;
547  // aliasing:
548  uint32 _numUses;
549  bool _isAlias;
550  // rendering:
552 // Real _hemiw, _hemih;
553 
554  // index in network
555  size_t i_;
556  int isub_;
557  bool exsub_;
558  };
559 
561  inline Node* CastToNode(void* p) {
562  NetworkElement* e = (NetworkElement*)p;
563  AN(e->hasNetworkElementBase(), "Runtime type check failed");
564 // std::cout << typeid((Node&)*e).name() << std::endl;
565  AN(dynamic_cast<Node*>(e), "Runtime type check failed");
566  return dynamic_cast<Node*>(e);
567 // Node* x = (Node*)e;
568 // AN(x->doByteCheck(), "Runtime type check failed");
569 // return x;
570  }
571 
572  typedef enum {
573  RXN_ROLE_SUBSTRATE,
574  RXN_ROLE_PRODUCT,
575  RXN_ROLE_SIDESUBSTRATE,
576  RXN_ROLE_SIDEPRODUCT,
577  RXN_ROLE_MODIFIER,
578  RXN_ROLE_ACTIVATOR,
579  RXN_ROLE_INHIBITOR,
580  } RxnRoleType;
581 
582  class RxnCurveFactory {
583  public:
584  static RxnBezier* CreateCurve(RxnRoleType role);
585  };
586 
589  class Reaction : public NetworkElement {
590  public:
591  // Exposed types:
592  typedef std::pair<Node*, RxnRoleType> SpeciesElt;
594  typedef std::vector< SpeciesElt > NodeVec;
596  //typedef std::vector<RxnRoleType> RoleVec;
598  typedef std::vector<RxnBezier*> CurveVec;
599 
600  // Iterators:
601  typedef NodeVec::iterator NodeIt;
602  typedef NodeVec::const_iterator ConstNodeIt;
603 
604  /*typedef RoleVec::iterator RoleIt;
605  typedef RoleVec::const_iterator ConstRoleIt;*/
606 
607  typedef CurveVec::iterator CurveIt;
608  typedef CurveVec::const_iterator ConstCurveIt;
609 
610  NodeIt NodesBegin() { return _spec.begin(); }
611  NodeIt NodesEnd() { return _spec.end(); }
612 
613  ConstNodeIt NodesBegin() const { return _spec.begin(); }
614  ConstNodeIt NodesEnd() const { return _spec.end(); }
615 
616  CurveIt CurvesBegin() { return _curv.begin(); }
617  CurveIt CurvesEnd() { return _curv.end(); }
618 
619  ConstCurveIt CurvesBegin() const { return _curv.begin(); }
620  ConstCurveIt CurvesEnd() const { return _curv.end(); }
621 
622  // Methods:
623 
624  Reaction()
625  : NetworkElement() {
626  _shape = ELT_SHAPE_ROUND;
627  _type = NET_ELT_TYPE_RXN;
628  bytepattern = 0xff83;
629  }
630 
631  void hierarchRelease();
632 
633  // Model:
634 
636  const std::string& getId() const { return _id; }
637 
639  void setId(const std::string& id) { _id = id; }
640 
641  // Species:
642 
644  uint64 numSpecies() const { return _spec.size(); }
645 
647  void addSpeciesRef(Node* n, RxnRoleType role);
648 
650  void removeNode(Node* n);
651 
653  Node* findSpeciesById(const std::string& id);
654 
656  bool hasSpecies(const Node* n) const;
657 
659  uint64 degree(const Node* n);
660 
661  RxnRoleType getSpeciesRole(size_t i) { return _spec.at(i).second; }
662 
663  RxnRoleType getSpeciesRole(Node* n);
664 
665  Node* getSpecies(size_t i) { return _spec.at(i).first; }
666 
671  void substituteSpeciesById(const std::string& id, Node* spec);
672 
675  void substituteSpeciesByIdwRole(const std::string& id, Node* spec, RxnRoleType role);
676 
679  void substituteSpecies(Node* before, Node* after);
680 
682  CurveVec& getCurves();
683  // this space intentionally left blank
684 
686  size_t getNumCurves() { curveGuard(); return _curv.size(); }
687 
688 
690  RxnBezier* getCurve(size_t i) { curveGuard(); return _curv.at(i); }
691 
693  NodeVec& getSpec() { return _spec; }
694  const NodeVec& getSpec() const { return _spec; }
695 
697  void forceRecalcCentroid();
698 
702  void rebuildCurves();
703 
708  void recalcCurveCPs();
709 
712  void recenter();
713 
714  // Layout
715 
716  bool isContainer() const { return false; }
717 
719  void recalcExtents() {
720  _r = 10.;
721  _ext = Box(_p - Point(_r,_r), _p + Point(_r,_r));
722  }
723 
724  virtual Box getLocalExtents() const {
725  return Box(getCentroid() - Point(5, 5), getCentroid() + Point(5, 5));
726  }
727 
728  virtual void applyTransform(const Affine2d& t) {
729  NetworkElement::applyTransform(t);
730  for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
731  (*i)->applyTransform(t);
732  }
733  }
734 
735  virtual void setTransform(const Affine2d& tf, bool recurse = true) {
736  tf_ = tf;
737  for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
738  (*i)->setTransform(tf);
739  }
740  }
741 
742  virtual void setInverseTransform(const Affine2d& itf, bool recurse = true) {
743  itf_ = itf;
744  for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
745  (*i)->setInverseTransform(itf);
746  }
747  }
748 
749  // IO
750  void dump(std::ostream& os, uint32 ind);
751 
753  void dumpForces(std::ostream& os, uint32 ind) const;
754 
755  bool doByteCheck() { if(bytepattern == 0xff83) return true; else return false; }
756 
757  void clearDirtyFlag() { _cdirty = false; }
758 
760  RxnBezier* addCurve(RxnRoleType role) {
761  _curv.push_back(RxnCurveFactory::CreateCurve(role));
762  _curv.back()->setTransform(itf_);
763  _curv.back()->setInverseTransform(itf_);
764  return _curv.back();
765  }
766 
768  void deleteCurves();
769 
770  protected:
771  // Methods:
772 
774  void rebuildAll();
775 
777  void recompCentroid();
778 
779  void curveGuard() {
780  if(_cdirty && _spec.size()) {
781  rebuildCurves();
782 // recenter();
783  }
784  }
785 
786  // Variables:
787  // model:
788  std::string _id;
789  // dims:
791  Point _v;
793  //NodeVec _rct;
795  //NodeVec _prd;
797  NodeVec _spec;
799  //RoleVec _role;
801  CurveVec _curv;
803  bool _cdirty;
804 
805  long bytepattern;
806  };
807 
809  inline Reaction* CastToReaction(void* p) {
810  NetworkElement* e = (NetworkElement*)p;
811  AN(e->hasNetworkElementBase(), "Runtime type check failed");
812  AN(dynamic_cast<Reaction*>(e), "Runtime type check failed");
813  return dynamic_cast<Reaction*>(e);
814  }
815 
816  typedef enum {
817  COMP_EDGE_TYPE_TOP,
818  COMP_EDGE_TYPE_LEFT,
819  COMP_EDGE_TYPE_BOTTOM,
820  COMP_EDGE_TYPE_RIGHT
821  } CompartmentEdgeType;
822 
825  class Compartment : public NetworkElement {
826  public:
827 
828  // Exposed types:
829 
831  typedef std::vector<Graphfab::NetworkElement*> EltVec;
832 
833  typedef EltVec::iterator EltIt;
834  typedef EltVec::const_iterator ConstEltIt;
835 
836  EltIt EltsBegin() { return _elt.begin(); }
837  EltIt EltsEnd() { return _elt.end(); }
838 
839  ConstEltIt EltsBegin() const { return _elt.begin(); }
840  ConstEltIt EltsEnd() const { return _elt.end(); }
841 
842  Graphfab::NetworkElement* getElt(const uint64 i) { return _elt.at(i); }
843 
844  const Graphfab::NetworkElement* getElt(const uint64 i) const { return _elt.at(i); }
845 
846  uint64 getNElts() const { return _elt.size(); }
847 
848  Compartment()
849  : /*_nu(0.3),*/ _ra(50.*50.), _E(10.), _res(0.25), NetworkElement() {
850  _shape = ELT_SHAPE_RECT;
851  _type = NET_ELT_TYPE_COMP;
852  }
853 
855  const std::string& getId() const { return _id; }
856 
858  void setId(const std::string& id) { _id = id; }
859 
861  const std::string& getGlyph() const { return _gly; }
862 
864  void setGlyph(const std::string& glyph) { _gly = glyph; }
865 
866  void setCentroid(const Point& p) {
867  AN(0, "setCentroid should not be called on a compt");
868  }
869 
871  void recalcExtents() {
872  _r = _ext.maxDim()*0.5;
873  _p = (_ext.getMin() + _ext.getMax())*0.5;
874  }
875 
876  // Elements
877 
879  void addElt(NetworkElement* e);
880 
881  bool containsElt(const NetworkElement* e) const;
882 
884  void removeElt(NetworkElement* e);
885 
887  void setRestExtents(const Box& ext);
888 
890  void resizeEnclose();
891 
893  void autoSize();
894 
896  Real restArea() const { return _ra; }
897 
898  void setMin(const Point& p) { _ext.setMin(p); }
899  void setMax(const Point& p) { _ext.setMax(p); }
900 
901  // Layout engine:
902 
904  virtual void resetActivity();
905 
912  void applyBoundaryForce(const Real fx1, const Real fy1, const Real fx2, const Real fy2);
913 
919  void doInternalForce(NetworkElement* e, const Real f, const Real t);
920 
922  void doInternalForceAll(const Real f, const Real t);
923 
925  void doMotion(const Real scale);
926 
927  void capDelta2(const Real cap2);
928 
929  bool isContainer() const { return true; }
930 
932  bool contains(const NetworkElement* e) const;
933 
935  bool empty() const { return _elt.size() ? false : true; }
936 
938  void dump(std::ostream& os, uint32 ind);
939 
941  void dumpForces(std::ostream& os, uint32 ind) const;
942 
943  protected:
945  std::string _id;
947  std::string _gly;
949  EltVec _elt;
951  Real _ra;
953  Real _E;
955  //Real _nu;
957  Real _fx1, _fy1, _fx2, _fy2;
959  Real _res;
960  };
961 
964  class Network : public Compartment {
965  public:
966  // Exposed types:
967 
969  typedef std::vector<Node*> NodeVec;
971  typedef std::vector<Graphfab::Reaction*> RxnVec;
973  typedef std::vector<Graphfab::Compartment*> CompVec;
974 
975  //iterators
976  typedef NodeVec::iterator NodeIt;
977  typedef NodeVec::const_iterator ConstNodeIt;
978 
979  typedef RxnVec::iterator RxnIt;
980  typedef RxnVec::const_iterator ConstRxnIt;
981 
982  typedef CompVec::iterator CompIt;
983  typedef CompVec::const_iterator ConstCompIt;
984 
985  // Constructors:
986 
987  Network() {
988  bytepattern = 0x3355;
989  layoutspecified_ = false;
990  }
991 
992  // Methods:
993 
995  void hierarchRelease();
996 
997  // Nodes:
998 
1000  void addNode(Node* n);
1001 
1004  void removeNode(Node* n);
1005 
1007  Node* findNodeById(const std::string& id);
1008  const Node* findNodeById(const std::string& id) const;
1009 
1011  std::string getUniqueId() const;
1012 
1014  std::size_t getUniqueIndex() const;
1015 
1017  Node* findNodeByGlyph(const std::string& gly);
1018 
1019  Node* getNodeAt(const size_t i) { return _nodes.at(i); }
1020 
1021  bool containsNode(const Node* n) const;
1022 
1023  typedef std::vector<Reaction*> AttachedRxnList;
1024 
1025  AttachedRxnList getConnectedReactions(const Node* n);
1026 
1027  typedef std::vector<RxnBezier*> AttachedCurveList;
1028 
1029  AttachedCurveList getAttachedCurves(const Node* n);
1030 
1031  int getNumSubgraphs();
1032 
1034  void enumerateSubgraphs();
1035 
1037  void propagateSubgraphIndex(Node* x, int isub);
1038 
1039  void clearSubgraphInfo();
1040 
1041  void clearExcludeFromSubgraphEnum();
1042 
1044  Reaction* findReactionById(const std::string& id);
1045 
1046  Reaction* getRxnAt(const size_t i) { return _rxn.at(i); }
1047 
1049  void resetUsageInfo();
1050 
1052  const std::string& getId() const { return _id; }
1053 
1054  bool isLayoutSpecified() const { return layoutspecified_; }
1055 
1056  void setLayoutSpecified(bool value) {
1057  layoutspecified_ = value;
1058  }
1059 
1060  // Reactions:
1061 
1063  void addReaction(Reaction* rxn);
1064 
1066  void removeReaction(Reaction* r);
1067 
1068  // Compartments:
1069 
1071  void addCompartment(Compartment* c) { _comp.push_back(c); addElt(c); }
1072 
1076  Compartment* findCompById(const std::string& id);
1077 
1081  Compartment* findCompByGlyph(const std::string& gly);
1082 
1083  Compartment* getCompAt(const size_t i) { return _comp.at(i); }
1084 
1085  Compartment* findContainingCompartment(const NetworkElement* e);
1086 
1087  // Layout:
1088 
1089  uint64 getTotalNumComps() const { return _comp.size(); }
1090 
1092  uint64 getTotalNumPts() const { return _nodes.size() + _rxn.size(); }
1093 
1095  uint64 getTotalNumRxns() const { return _rxn.size(); }
1096 
1098  uint64 getTotalNumNodes() const { return _nodes.size(); }
1099 
1100  Box getBoundingBox() const;
1101 
1102  void fitToWindow(const Box& w);
1103 
1104  void applyTransform(const Affine2d& t);
1105 
1106  void setTransform(const Affine2d& t, bool recurse = true);
1107 
1108  void setInverseTransform(const Affine2d& it, bool recurse = true);
1109 
1111  void elideEmptyComps();
1112 
1114  void randomizePositions(const Box& bounds);
1115 
1117  void rebuildCurves();
1118 
1121  void recalcCurveCPs();
1122 
1124  void recenterJunctions();
1125 
1127  void resetActivity();
1128 
1134  //void doNodeBoxContactForce(const Box& b, const Real f, const Real t);
1135 
1139  void capDeltas(const Real cap);
1140 
1144  void updatePositions(const Real scale);
1145 
1147  void updateExtents();
1148 
1150  void resizeCompsEnclose();
1151 
1153  void autosizeComps();
1154 
1157  Point pmean() const;
1158 
1161  Point center() const;
1162 
1165  Box getExtents() const;
1166 
1170  void recenter(const Point& p);
1171 
1174  //Real diam(const Point& p);
1175 
1178  Point pvariance() const;
1179 
1180  // IO/Diagnostics:
1181 
1183  void dump(std::ostream& os, uint32 ind);
1184 
1186  void dumpEltForces(std::ostream& os, uint32 ind) const;
1187 
1188  Node* getNodeAtIndex(int index) { return _nodes[index]; }
1189 
1190  //iterators
1191 
1192  NodeIt NodesBegin() { return _nodes.begin(); }
1193  NodeIt NodesEnd() { return _nodes.end(); }
1194 
1195  ConstNodeIt NodesBegin() const { return _nodes.begin(); }
1196  ConstNodeIt NodesEnd() const { return _nodes.end(); }
1197 
1198  RxnIt RxnsBegin() { return _rxn.begin(); }
1199  RxnIt RxnsEnd() { return _rxn.end(); }
1200 
1201  ConstRxnIt RxnsBegin() const { return _rxn.begin(); }
1202  ConstRxnIt RxnsEnd() const { return _rxn.end(); }
1203 
1204  CompIt CompsBegin() { return _comp.begin(); }
1205  CompIt CompsEnd() { return _comp.end(); }
1206 
1207  ConstCompIt CompsBegin() const { return _comp.begin(); }
1208  ConstCompIt CompsEnd() const { return _comp.end(); }
1209 
1210  bool doByteCheck() const { if(bytepattern == 0x3355) return true; else return false; }
1211  protected:
1212 
1213  void removeReactionsForNode(Node* n);
1214 
1216  NodeVec _nodes;
1218  RxnVec _rxn;
1220  CompVec _comp;
1221 
1222  long bytepattern;
1223  bool layoutspecified_;
1224 
1226  int nsub_;
1227  };
1228 
1230  inline Network* CastToNetwork(void* p) {
1231  NetworkElement* e = (NetworkElement*)p;
1232  AN(e->hasNetworkElementBase(), "Runtime type check failed");
1233  return dynamic_cast<Network*>(e);
1234  }
1235 
1236  // Methods:
1237 
1244  Network* networkFromLayout(const Layout& lay, const Model& mod);
1245 
1251  Network* networkFromModel(const Model& mod);
1252 
1255  int layout_getNumFloatingSpecies(const Layout& lay, const Model& mod);
1256 
1257 }
1258 
1259 #endif
1260 
1261 #endif
SAGITTARIUS_REAL Real
Make Real visible to C. Consider letting Real lie in top namespace.
Definition: SagittariusCommon.h:136
Definition: SagittariusCommon.cpp:38
A box.
First file included.
Roots of cubic equations.