libSBNW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations 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 __SBNW_NETWORK_H_
37 #define __SBNW_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 #include <stdint.h>
59 
60 namespace Graphfab {
61 
62  typedef enum {
63  NET_ELT_TYPE_SPEC,
64  NET_ELT_TYPE_RXN,
65  NET_ELT_TYPE_COMP
66  } NetworkEltType;
67 
68  std::string eltTypeToStr(const NetworkEltType t);
69 
70  void dumpEltType(std::ostream& os, const NetworkEltType t, uint32 ind);
71 
73  inline bool typeMatchEither(const NetworkEltType a, const NetworkEltType b, const NetworkEltType z) {
74  if(a == z)
75  return true;
76  else if(b == z)
77  return true;
78  else
79  return false;
80  }
81 
82  class Compartment;
83 
84  bool haveDefaultCompartmentId();
85 
86  void setDefaultCompartmentId(const std::string& id);
87 
88  std::string getDefaultCompartmentId();
89 
92  class SubCurve : public RxnBezier {
93  public:
95  RxnCurveType getRole() const {
96  return RXN_CURVE_SUBSTRATE;
97  }
98 
99  Point getCentroidCP() const { return c2; }
100 
101  bool isStartNodeSide() const { return true; }
102 
103  virtual bool hasArrowhead() const { return true; }
104 
105  ArrowheadStyle getArrowheadStyle() const;
106 
108  virtual Arrowhead* getArrowhead() {
109  Arrowhead* result = new SubstrateArrowhead();
110  transformArrowhead(*result);
111  return result;
112  }
113  };
114 
117  class PrdCurve : public RxnBezier {
118  public:
120  RxnCurveType getRole() const {
121  return RXN_CURVE_PRODUCT;
122  }
123 
124  Point getCentroidCP() const { return c1; }
125 
126  bool isStartNodeSide() const { return false; }
127 
128  virtual bool hasArrowhead() const { return true; }
129 
130  ArrowheadStyle getArrowheadStyle() const;
131 
133  virtual Arrowhead* getArrowhead() {
134  Arrowhead* result = new ProductArrowhead();
135  transformArrowhead(*result);
136  return result;
137  }
138  };
139 
142  class ActCurve : public RxnBezier {
143  public:
145  RxnCurveType getRole() const {
146  return RXN_CURVE_ACTIVATOR;
147  }
148 
149  Point getCentroidCP() const { return c2; }
150 
151  bool isStartNodeSide() const { return true; }
152 
153  virtual bool hasArrowhead() const { return true; }
154 
155  ArrowheadStyle getArrowheadStyle() const;
156 
158  virtual Arrowhead* getArrowhead() {
159  Arrowhead* result = new ActivatorArrowhead();
160  transformArrowhead(*result);
161  return result;
162  }
163  };
164 
167  class InhCurve : public RxnBezier {
168  public:
170  RxnCurveType getRole() const {
171  return RXN_CURVE_INHIBITOR;
172  }
173 
174  Point getCentroidCP() const { return c2; }
175 
176  bool isStartNodeSide() const { return true; }
177 
178  virtual bool hasArrowhead() const { return true; }
179 
180  ArrowheadStyle getArrowheadStyle() const;
181 
183  virtual Arrowhead* getArrowhead() {
184  Arrowhead* result = new InhibitorArrowhead();
185  transformArrowhead(*result);
186  return result;
187  }
188  };
189 
192  class ModCurve : public RxnBezier {
193  public:
195  RxnCurveType getRole() const {
196  return RXN_CURVE_MODIFIER;
197  }
198 
199  Point getCentroidCP() const { return c2; }
200 
201  bool isStartNodeSide() const { return true; }
202 
203  virtual bool hasArrowhead() const { return true; }
204 
205  ArrowheadStyle getArrowheadStyle() const;
206 
208  virtual Arrowhead* getArrowhead() {
209  Arrowhead* result = new ModifierArrowhead();
210  transformArrowhead(*result);
211  return result;
212  }
213  };
214 
215  extern ArrowheadStyle sub_arrow_style_;
216  inline ArrowheadStyle ArrowheadStyleLookup(const SubCurve* ) {
217  return sub_arrow_style_;
218  }
219 
220  extern ArrowheadStyle prod_arrow_style_;
221  inline ArrowheadStyle ArrowheadStyleLookup(const PrdCurve* ) {
222  return prod_arrow_style_;
223  }
224 
225  extern ArrowheadStyle act_arrow_style_;
226  inline ArrowheadStyle ArrowheadStyleLookup(const ActCurve* ) {
227  return act_arrow_style_;
228  }
229 
230  extern ArrowheadStyle inh_arrow_style_;
231  inline ArrowheadStyle ArrowheadStyleLookup(const InhCurve* ) {
232  return inh_arrow_style_;
233  }
234 
235  extern ArrowheadStyle mod_arrow_style_;
236  inline ArrowheadStyle ArrowheadStyleLookup(const ModCurve* ) {
237  return mod_arrow_style_;
238  }
239 
241  typedef enum {
242  ELT_SHAPE_ROUND,
243  ELT_SHAPE_RECT
244  } NetworkEltShape;
245 
248  class NetworkElement {
249  public:
250 
251  enum COORD_SYSTEM {
252  COORD_SYSTEM_LOCAL,
253  COORD_SYSTEM_GLOBAL
254  };
255 
256  NetworkElement()
257  : _pset(0), _v(0,0), _deg(0), _ldeg(0), _lock(0), networkEltBytePattern_(0x1199) {}
258 
260  NetworkEltType getType() const { return _type; }
261 
262  bool hasNetworkElementBase() {
263  if(networkEltBytePattern_ == 0x1199)
264  return true;
265  else
266  return false;
267  }
268 
270  void set_degree(uint64 deg) { _deg = deg; }
271  virtual uint64 degree() const { return _deg; }
272  virtual uint64& degree() { return _deg; }
273 
275  virtual void resetActivity();
276 
278  void addDelta(const Point& d);
279 
281  void capDelta(const Real cap);
282 
284  virtual void capDelta2(const Real cap2);
285 
287  virtual void doMotion(const Real scale);
288 
290  virtual void setCentroid(const Point& p);
291  virtual void setGlobalCentroid(const Point& p);
292  void setCentroid(Real x, Real y) { setCentroid(Point(x,y)); }
293 
294  virtual bool isCentroidSet() const { return _pset; }
295 
297  virtual Point getCentroid(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const;
298 
299 // SAGITTARIUS_DEPRECATED(Point getGlobalCentroid() const) { return tf_*_p; }
300 
302  Point getMin(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getMin(); }
303  Point getMax(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getMax(); }
304 
305  Real getMinX() const { return getExtents().getMin().x; }
306  Real getMaxX() const { return getExtents().getMax().x; }
307  Real getMinY() const { return getExtents().getMin().y; }
308  Real getMaxY() const { return getExtents().getMax().y; }
309 
311  Real getWidth() const { AT(getMaxX() >= getMinX()); return getMaxX() - getMinX(); }
312  Real getHeight() const { AT(getMaxY() >= getMinY()); return getMaxY() - getMinY(); }
313 
315  Real getGlobalWidth() const { AT(getMaxX() >= getMinX()); return (getMaxX() - getMinX())*tf_.scaleFactor(); }
316  Real getGlobalHeight() const { AT(getMaxY() >= getMinY()); return (getMaxY() - getMinY())*tf_.scaleFactor(); }
317 
320  virtual Box getExtents(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const {
321  switch (coord) {
322  case COORD_SYSTEM_LOCAL:
323  return getLocalExtents();
324  case COORD_SYSTEM_GLOBAL:
325  return tf_*getLocalExtents();
326  default:
327  AN(0, "Unknown coord system");
328  return getLocalExtents();
329  }
330  }
331 
334  virtual Box getLocalExtents() const {
335  return _ext;
336  }
337 // virtual SAGITTARIUS_DEPRECATED(Box getGlobalExtents() const) { return tf_*_ext; }
338 
340  void setExtents(const Box& b) { _ext = b; recalcCentroid(); }
341 
342  Box getBoundingBox() const { return getExtents(); }
343 // Box getBoundingBox() const { return Box(); }
344 
345  virtual void applyTransform(const Affine2d& t) {
346  _ext = xformBox(_ext, t);
347  _p = xformPoint(_p, t);
348  }
349 
350  virtual void applyDisplacement(const Point& d) {
351  _ext.displace(d);
352  _p += d;
353  }
354 
356  void recalcCentroid() { _p = (_ext.getMin() + _ext.getMax())*0.5; }
357 
359  virtual void recalcExtents() = 0;
360 
362  void lock() { _lock = 1; }
363 
365  void unlock() { _lock = 0; }
366 
367  // Is the node locked?
368  bool isLocked() const { return _lock; }
369 
370  NetworkEltShape getShape() const { return _shape; }
371 
372  //TODO: cache in member & ditch v func
373  virtual bool isContainer() const = 0;
374 
376  Real radius() const { return _r; }
377 
379  Real distance(const NetworkElement& e) const;
380 
382  bool overlap(const NetworkElement& e) const;
383 
385  // TODO: rename; is actually displacement
386  Point forceVec(const NetworkElement& e) const;
387 
389  Point centroidDisplacementFrom(const NetworkElement& e) const;
390 
392  void forceVec_(const NetworkElement& e, Point& p) const;
393 
394  virtual void dump(std::ostream& os, uint32 ind) = 0;
395 
397  virtual void dumpForces(std::ostream& os, uint32 ind) const = 0;
398 
399  virtual Affine2d getTransform() const { return tf_; }
400 
401  virtual void setTransform(const Affine2d& tf, bool recurse = true) { tf_ = tf; }
402 
403  virtual Affine2d getInverseTransform() const { return itf_; }
404 
405  virtual void setInverseTransform(const Affine2d& itf, bool recurse = true) { itf_ = itf; }
406 
408  Point _p;
410  uint64 _deg;
412  uint64 _ldeg;
413  protected:
415  int _pset;
417  Point _v;
419  Box _ext;
421  Real _r;
423  NetworkEltShape _shape;
425  NetworkEltType _type;
427  int _lock;
429  Affine2d tf_;
431  Affine2d itf_;
432 
433  long networkEltBytePattern_;
434  };
435 
436  class Network;
437 
440  class Node : public NetworkElement {
441  public:
442 
443  Node()
444  : NetworkElement() {
445  _shape = ELT_SHAPE_RECT;
446  _comp = NULL;
447  _type = NET_ELT_TYPE_SPEC;
448  _ext = Box(0,0,40,20);
449  bytepattern = 0xc455;
450  isub_ = -1;
451  }
452 
453  // Model:
454 
456  void setName(const std::string& name);
457 
459  const std::string& getName() const;
460 
462  const std::string& getId() const;
463 
465  void setId(const std::string& id);
466 
468  const std::string& getGlyph() const;
469 
471  void setGlyph(const std::string& id);
472 
473  // Alias info:
474 
476  uint32& numUses() { return _numUses; }
477  uint32 numUses() const { return _numUses; }
478 
480  //TODO: change to isAliased. There is no such thing as an "alias node"
481  bool isAlias() const { return _isAlias; }
482 
484  bool isCommonInstance(const Node* other) const;
485 
487  void setAlias(bool b) { _isAlias = b; }
488 
489  int alias(Network* net);
490 
491  int getSubgraphIndex() const {
492  if (isub_ < 0)
493  SBNW_THROW(InvalidParameterException, "No subgraph index set", "Network::getSubgraphIndex");
494  return isub_;
495  }
496 
497  void setSubgraphIndex(int v) { isub_ = v; }
498 
499  bool isSetSubgraphIndex() { return isub_ < 0; }
500 
501  void clearSubgraphIndex() { isub_ = -1; }
502 
503  bool excludeFromSubgraphEnum() const { return exsub_; }
504 
505  bool setExcludeFromSubgraphEnum() { return exsub_ = true; }
506 
507  void clearExcludeFromSubgraphEnum() { exsub_ = false; }
508 
509  // Coordinates/dimensions:
510 
512  Point getUpperLeftCorner() const;
514  Point getLowerRightCorner() const;
515 
516  void recalcExtents() {
517  Real width = _ext.width();
518  Real height = _ext.height();
519  Point del(0.5*width, 0.5*height);
520 
521  _ext = Box(_p - del, _p + del);
522  _r = _ext.maxDim()*0.5;
523  }
524 
526  void setWidth(Real w);
527 
529  void setHeight(Real h);
530 
532  void affectGlobalWidth(Real w);
533 
535  void affectGlobalHeight(Real h);
536 
537  void set_i(size_t i) { i_ = i; }
538  size_t get_i() const { return i_; }
539 
540  // Layout:
541 
542  bool isContainer() const { return false; }
543 
544  // IO:
545 
547  void dump(std::ostream& os, uint32 ind);
548 
550  void dumpForces(std::ostream& os, uint32 ind) const;
551 
552  Compartment* _comp;
553 
554  bool doByteCheck() { if(bytepattern == 0xc455) return true; else return false; }
555 
556  long bytepattern;
557  protected:
558  // model info:
559  std::string _name, _id;
561  std::string _gly;
562  // aliasing:
563  uint32 _numUses;
564  bool _isAlias;
565  // rendering:
567 // Real _hemiw, _hemih;
568 
569  // index in network
570  size_t i_;
571  int isub_;
572  bool exsub_;
573  };
574 
576  inline Node* CastToNode(void* p) {
577  NetworkElement* e = (NetworkElement*)p;
578  AN(e->hasNetworkElementBase(), "Runtime type check failed");
579 // std::cout << typeid((Node&)*e).name() << std::endl;
580  AN(dynamic_cast<Node*>(e), "Runtime type check failed");
581  return dynamic_cast<Node*>(e);
582 // Node* x = (Node*)e;
583 // AN(x->doByteCheck(), "Runtime type check failed");
584 // return x;
585  }
586 
587  typedef enum {
588  RXN_ROLE_SUBSTRATE,
589  RXN_ROLE_PRODUCT,
590  RXN_ROLE_SIDESUBSTRATE,
591  RXN_ROLE_SIDEPRODUCT,
592  RXN_ROLE_MODIFIER,
593  RXN_ROLE_ACTIVATOR,
594  RXN_ROLE_INHIBITOR,
595  } RxnRoleType;
596 
597  class RxnCurveFactory {
598  public:
599  static RxnBezier* CreateCurve(RxnRoleType role);
600  };
601 
604  class Reaction : public NetworkElement {
605  public:
606  // Exposed types:
607  typedef std::pair<Node*, RxnRoleType> SpeciesElt;
609  typedef std::vector< SpeciesElt > NodeVec;
611  //typedef std::vector<RxnRoleType> RoleVec;
613  typedef std::vector<RxnBezier*> CurveVec;
614 
615  // Iterators:
616  typedef NodeVec::iterator NodeIt;
617  typedef NodeVec::const_iterator ConstNodeIt;
618 
619  /*typedef RoleVec::iterator RoleIt;
620  typedef RoleVec::const_iterator ConstRoleIt;*/
621 
622  typedef CurveVec::iterator CurveIt;
623  typedef CurveVec::const_iterator ConstCurveIt;
624 
625  NodeIt NodesBegin() { return _spec.begin(); }
626  NodeIt NodesEnd() { return _spec.end(); }
627 
628  ConstNodeIt NodesBegin() const { return _spec.begin(); }
629  ConstNodeIt NodesEnd() const { return _spec.end(); }
630 
631  CurveIt CurvesBegin() { return _curv.begin(); }
632  CurveIt CurvesEnd() { return _curv.end(); }
633 
634  ConstCurveIt CurvesBegin() const { return _curv.begin(); }
635  ConstCurveIt CurvesEnd() const { return _curv.end(); }
636 
637  // Methods:
638 
639  Reaction()
640  : NetworkElement() {
641  _shape = ELT_SHAPE_ROUND;
642  _type = NET_ELT_TYPE_RXN;
643  bytepattern = 0xff83;
644  }
645 
646  void hierarchRelease();
647 
648  // Model:
649 
651  const std::string& getId() const { return _id; }
652 
654  void setId(const std::string& id) { _id = id; }
655 
656  void setName(const std::string& name) { name_ = name; }
657 
658  // Species:
659 
661  uint64 numSpecies() const { return _spec.size(); }
662 
664  void addSpeciesRef(Node* n, RxnRoleType role);
665 
667  void removeNode(Node* n);
668 
670  Node* findSpeciesById(const std::string& id);
671 
673  bool hasSpecies(const Node* n) const;
674 
676  uint64 degree(const Node* n);
677 
678  RxnRoleType getSpeciesRole(size_t i) { return _spec.at(i).second; }
679 
680  RxnRoleType getSpeciesRole(Node* n);
681 
682  Node* getSpecies(size_t i) { return _spec.at(i).first; }
683 
688  void substituteSpeciesById(const std::string& id, Node* spec);
689 
692  void substituteSpeciesByIdwRole(const std::string& id, Node* spec, RxnRoleType role);
693 
696  void substituteSpecies(Node* before, Node* after);
697 
699  CurveVec& getCurves();
700  // this space intentionally left blank
701 
703  size_t getNumCurves() { curveGuard(); return _curv.size(); }
704 
705 
707  RxnBezier* getCurve(size_t i) { curveGuard(); return _curv.at(i); }
708 
710  NodeVec& getSpec() { return _spec; }
711  const NodeVec& getSpec() const { return _spec; }
712 
714  void forceRecalcCentroid();
715 
719  void rebuildCurves();
720 
725  void recalcCurveCPs();
726 
729  void recenter();
730 
731  // Layout
732 
733  bool isContainer() const { return false; }
734 
736  void recalcExtents() {
737  _r = 10.;
738  _ext = Box(_p - Point(_r,_r), _p + Point(_r,_r));
739  }
740 
741  virtual Box getLocalExtents() const {
742  return Box(getCentroid() - Point(5, 5), getCentroid() + Point(5, 5));
743  }
744 
745  virtual void applyTransform(const Affine2d& t) {
746  NetworkElement::applyTransform(t);
747  for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
748  (*i)->applyTransform(t);
749  }
750  }
751 
752  virtual void setTransform(const Affine2d& tf, bool recurse = true) {
753  tf_ = tf;
754  for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
755  (*i)->setTransform(tf);
756  }
757  }
758 
759  virtual void setInverseTransform(const Affine2d& itf, bool recurse = true) {
760  itf_ = itf;
761  for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
762  (*i)->setInverseTransform(itf);
763  }
764  }
765 
766  // IO
767  void dump(std::ostream& os, uint32 ind);
768 
770  void dumpForces(std::ostream& os, uint32 ind) const;
771 
772  bool doByteCheck() { if(bytepattern == 0xff83) return true; else return false; }
773 
774  void clearDirtyFlag() { _cdirty = false; }
775 
777  RxnBezier* addCurve(RxnRoleType role) {
778  _curv.push_back(RxnCurveFactory::CreateCurve(role));
779  _curv.back()->setTransform(itf_);
780  _curv.back()->setInverseTransform(itf_);
781  return _curv.back();
782  }
783 
785  void deleteCurves();
786 
787  protected:
788  // Methods:
789 
791  void rebuildAll();
792 
794  void recompCentroid();
795 
797  void doCentroidCalc();
798 
799  void curveGuard() {
800  if(_cdirty && _spec.size()) {
801  rebuildCurves();
802 // recenter();
803  }
804  }
805 
806  // Variables:
807  // model:
808  std::string _id;
809  std::string name_;
810  // dims:
812  Point _v;
814  //NodeVec _rct;
816  //NodeVec _prd;
818  NodeVec _spec;
820  //RoleVec _role;
822  CurveVec _curv;
824  bool _cdirty;
825 
826  long bytepattern;
827  };
828 
830  inline Reaction* CastToReaction(void* p) {
831  NetworkElement* e = (NetworkElement*)p;
832  AN(e->hasNetworkElementBase(), "Runtime type check failed");
833  AN(dynamic_cast<Reaction*>(e), "Runtime type check failed");
834  return dynamic_cast<Reaction*>(e);
835  }
836 
837  typedef enum {
838  COMP_EDGE_TYPE_TOP,
839  COMP_EDGE_TYPE_LEFT,
840  COMP_EDGE_TYPE_BOTTOM,
841  COMP_EDGE_TYPE_RIGHT
842  } CompartmentEdgeType;
843 
846  class Compartment : public NetworkElement {
847  public:
848 
849  // Exposed types:
850 
852  typedef std::vector<Graphfab::NetworkElement*> EltVec;
853 
854  typedef EltVec::iterator EltIt;
855  typedef EltVec::const_iterator ConstEltIt;
856 
857  EltIt EltsBegin() { return _elt.begin(); }
858  EltIt EltsEnd() { return _elt.end(); }
859 
860  ConstEltIt EltsBegin() const { return _elt.begin(); }
861  ConstEltIt EltsEnd() const { return _elt.end(); }
862 
863  Graphfab::NetworkElement* getElt(const uint64 i) { return _elt.at(i); }
864 
865  const Graphfab::NetworkElement* getElt(const uint64 i) const { return _elt.at(i); }
866 
867  uint64 getNElts() const { return _elt.size(); }
868 
869  Compartment()
870  : /*_nu(0.3),*/ _ra(50.*50.), _E(10.), _res(0.25), bytepattern(0xffae11), NetworkElement() {
871  _shape = ELT_SHAPE_RECT;
872  _type = NET_ELT_TYPE_COMP;
873  }
874 
876  const std::string& getId() const { return _id; }
877 
879  void setId(const std::string& id) { _id = id; }
880 
882  void setName(const std::string& name) { name_ = name; }
883 
885  const std::string& getGlyph() const { return _gly; }
886 
888  void setGlyph(const std::string& glyph) { _gly = glyph; }
889 
890  void setCentroid(const Point& p) {
891  AN(0, "setCentroid should not be called on a compt");
892  }
893 
895  void recalcExtents() {
896  _r = _ext.maxDim()*0.5;
897  _p = (_ext.getMin() + _ext.getMax())*0.5;
898  }
899 
900  // Elements
901 
903  void addElt(NetworkElement* e);
904 
905  bool containsElt(const NetworkElement* e) const;
906 
908  void removeElt(NetworkElement* e);
909 
911  void setRestExtents(const Box& ext);
912 
914  void resizeEnclose(double padding = 0);
915 
917  void autoSize();
918 
920  Real restArea() const { return _ra; }
921 
922  void setMin(const Point& p) { _ext.setMin(p); }
923  void setMax(const Point& p) { _ext.setMax(p); }
924 
925  virtual Point getCentroid(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL) const { return getExtents(coord).getCenter(); }
926 
927  // Layout engine:
928 
930  virtual void resetActivity();
931 
938  void applyBoundaryForce(const Real fx1, const Real fy1, const Real fx2, const Real fy2);
939 
945  void doInternalForce(NetworkElement* e, const Real f, const Real t);
946 
948  void doInternalForceAll(const Real f, const Real t);
949 
951  void doMotion(const Real scale);
952 
953  void capDelta2(const Real cap2);
954 
955  bool isContainer() const { return true; }
956 
958  bool contains(const NetworkElement* e) const;
959 
961  bool empty() const { return _elt.size() ? false : true; }
962 
964  void dump(std::ostream& os, uint32 ind);
965 
967  void dumpForces(std::ostream& os, uint32 ind) const;
968 
969  bool doByteCheck() { if(bytepattern == 0xffae11) return true; else return false; }
970 
971  protected:
973  std::string _id;
975  std::string name_;
977  std::string _gly;
979  EltVec _elt;
981  Real _ra;
983  Real _E;
985  //Real _nu;
987  Real _fx1, _fy1, _fx2, _fy2;
989  Real _res;
990 
991  uint64_t bytepattern;
992  };
993 
996  class Network : public Compartment {
997  public:
998  // Exposed types:
999 
1001  typedef std::vector<Node*> NodeVec;
1003  typedef std::vector<Graphfab::Reaction*> RxnVec;
1005  typedef std::vector<Graphfab::Compartment*> CompVec;
1006 
1007  //iterators
1008  typedef NodeVec::iterator NodeIt;
1009  typedef NodeVec::const_iterator ConstNodeIt;
1010 
1011  typedef RxnVec::iterator RxnIt;
1012  typedef RxnVec::const_iterator ConstRxnIt;
1013 
1014  typedef CompVec::iterator CompIt;
1015  typedef CompVec::const_iterator ConstCompIt;
1016 
1017  // Constructors:
1018 
1019  Network() {
1020  bytepattern = 0x3355;
1021  layoutspecified_ = false;
1022  }
1023 
1024  // Methods:
1025 
1027  void hierarchRelease();
1028 
1029  // Nodes:
1030 
1032  void addNode(Node* n);
1033 
1036  void removeNode(Node* n);
1037 
1039  void connectNode(Node* n, Reaction* r, RxnRoleType role);
1040 
1042  bool isNodeConnected(Node* n, Reaction* r) const;
1043 
1045  Node* findNodeById(const std::string& id);
1046  const Node* findNodeById(const std::string& id) const;
1047 
1049  std::string getUniqueId() const;
1050 
1051  std::string getUniqueGlyphId(const Node& src) const;
1052 
1054  std::size_t getUniqueIndex() const;
1055 
1057  Node* findNodeByGlyph(const std::string& gly);
1058 
1059  Node* getNodeAt(const size_t i) { return _nodes.at(i); }
1060 
1061  Node* getUniqueNodeAt(const size_t n);
1062 
1063  size_t getNumInstances(const Node* u);
1064 
1066  Node* getInstance(const Node* u, const size_t n);
1067 
1068  bool containsNode(const Node* n) const;
1069 
1070  bool containsReaction(const Reaction* r) const;
1071 
1072  typedef std::vector<Reaction*> AttachedRxnList;
1073 
1074  AttachedRxnList getConnectedReactions(const Node* n);
1075 
1076  typedef std::vector<RxnBezier*> AttachedCurveList;
1077 
1078  AttachedCurveList getAttachedCurves(const Node* n);
1079 
1080  int getNumSubgraphs();
1081 
1083  void enumerateSubgraphs();
1084 
1086  void propagateSubgraphIndex(Node* x, int isub);
1087 
1088  void clearSubgraphInfo();
1089 
1090  void clearExcludeFromSubgraphEnum();
1091 
1093  Reaction* findReactionById(const std::string& id);
1094 
1095  Reaction* getRxnAt(const size_t i) { return _rxn.at(i); }
1096 
1098  void resetUsageInfo();
1099 
1101  const std::string& getId() const { return _id; }
1102 
1103  void setId(const std::string& id) { _id = id; }
1104 
1105  bool isSetId() const { return _id.size(); }
1106 
1107  bool isLayoutSpecified() const { return layoutspecified_; }
1108 
1109  void setLayoutSpecified(bool value) {
1110  layoutspecified_ = value;
1111  }
1112 
1113  // Reactions:
1114 
1116  void addReaction(Reaction* rxn);
1117 
1119  void removeReaction(Reaction* r);
1120 
1121  // Compartments:
1122 
1124  void addCompartment(Compartment* c) { _comp.push_back(c); addElt(c); }
1125 
1129  Compartment* findCompById(const std::string& id);
1130 
1134  Compartment* findCompByGlyph(const std::string& gly);
1135 
1136  Compartment* getCompAt(const size_t i) { return _comp.at(i); }
1137 
1138  Compartment* findContainingCompartment(const NetworkElement* e);
1139 
1140  // Layout:
1141 
1142  uint64 getTotalNumComps() const { return _comp.size(); }
1143 
1145  uint64 getTotalNumPts() const { return _nodes.size() + _rxn.size(); }
1146 
1148  uint64 getTotalNumRxns() const { return _rxn.size(); }
1149 
1151  uint64 getTotalNumNodes() const { return _nodes.size(); }
1152 
1153  uint64 getNumUniqueNodes() const;
1154 
1155  Box getBoundingBox() const;
1156 
1157  void fitToWindow(const Box& w);
1158 
1159  void applyTransform(const Affine2d& t);
1160 
1161  void setTransform(const Affine2d& t, bool recurse = true);
1162 
1163  void setInverseTransform(const Affine2d& it, bool recurse = true);
1164 
1165  void applyDisplacement(const Point& d);
1166 
1168  void elideEmptyComps();
1169 
1171  void randomizePositions(const Box& bounds);
1172 
1174  void rebuildCurves();
1175 
1178  void recalcCurveCPs();
1179 
1181  void recenterJunctions();
1182 
1184  void resetActivity();
1185 
1191  //void doNodeBoxContactForce(const Box& b, const Real f, const Real t);
1192 
1196  void capDeltas(const Real cap);
1197 
1201  void updatePositions(const Real scale);
1202 
1204  void updateExtents();
1205 
1207  void resizeCompsEnclose(double padding = 0);
1208 
1210  void autosizeComps();
1211 
1214  Point pmean() const;
1215 
1218  Point center() const;
1219 
1222  Box getExtents() const;
1223 
1227  void recenter(const Point& p);
1228 
1231  //Real diam(const Point& p);
1232 
1235  Point pvariance() const;
1236 
1237  // IO/Diagnostics:
1238 
1240  void dump(std::ostream& os, uint32 ind);
1241 
1243  void dumpEltForces(std::ostream& os, uint32 ind) const;
1244 
1245  Node* getNodeAtIndex(int index) { return _nodes[index]; }
1246 
1247  //iterators
1248 
1249  NodeIt NodesBegin() { return _nodes.begin(); }
1250  NodeIt NodesEnd() { return _nodes.end(); }
1251 
1252  ConstNodeIt NodesBegin() const { return _nodes.begin(); }
1253  ConstNodeIt NodesEnd() const { return _nodes.end(); }
1254 
1255  RxnIt RxnsBegin() { return _rxn.begin(); }
1256  RxnIt RxnsEnd() { return _rxn.end(); }
1257 
1258  ConstRxnIt RxnsBegin() const { return _rxn.begin(); }
1259  ConstRxnIt RxnsEnd() const { return _rxn.end(); }
1260 
1261  CompIt CompsBegin() { return _comp.begin(); }
1262  CompIt CompsEnd() { return _comp.end(); }
1263 
1264  ConstCompIt CompsBegin() const { return _comp.begin(); }
1265  ConstCompIt CompsEnd() const { return _comp.end(); }
1266 
1267  bool doByteCheck() const { if(bytepattern == 0x3355) return true; else return false; }
1268  protected:
1269 
1270  void removeReactionsForNode(Node* n);
1271 
1273  NodeVec _nodes;
1275  RxnVec _rxn;
1277  CompVec _comp;
1278 
1279  long bytepattern;
1280  bool layoutspecified_;
1281 
1283  int nsub_;
1284  };
1285 
1287  inline Network* CastToNetwork(void* p) {
1288  NetworkElement* e = (NetworkElement*)p;
1289  AN(e->hasNetworkElementBase(), "Runtime type check failed");
1290  return dynamic_cast<Network*>(e);
1291  }
1292 
1293  // Methods:
1294 
1301  Network* networkFromLayout(const Layout& lay, const Model& mod);
1302 
1308  Network* networkFromModel(const Model& mod);
1309 
1312  int layout_getNumFloatingSpecies(const Layout& lay, const Model& mod);
1313 
1314 }
1315 
1316 #endif
1317 
1318 #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.