COPASI API  4.16.103
CLayout.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #define USE_LAYOUT 1
16 #define USE_RENDER 1
17 
18 #include "iostream"
19 #include "sbml/packages/layout/sbml/Layout.h"
20 #include "sbml/packages/render/sbml/LocalRenderInformation.h"
21 #include "sbml/packages/render/extension/RenderLayoutPlugin.h"
22 #include "sbml/packages/render/extension/RenderListOfLayoutsPlugin.h"
23 
24 #if LIBSBML_VERSION >= 50800
25 #include "sbml/packages/layout/sbml/GeneralGlyph.h"
26 #endif // LIBSBML_VERSION >= 50800
27 
28 #include "copasi.h"
29 
30 #include "CLayout.h"
31 #include "CLReactionGlyph.h"
32 #include "SBMLDocumentLoader.h"
33 
34 #include "report/CKeyFactory.h"
35 #include "sbml/CSBMLExporter.h"
37 
38 CLayout::CLayout(const std::string & name,
39  const CCopasiContainer * pParent)
40  : CLBase(),
41  CCopasiContainer(name, pParent, "Layout"),
42  mKey(CCopasiRootContainer::getKeyFactory()->add("Layout", this)),
43  mDimensions(),
44  mvCompartments("ListOfCompartmentGlyphs", this),
45  mvMetabs("ListOfMetaboliteGlyphs", this),
46  mvReactions("ListOfReactionGlyphs", this),
47  mvLabels("ListOfTextGlyphs", this),
48  mvGraphicalObjects("ListOfGraphicalObjects", this)
49  , mvLocalRenderInformationObjects("ListOfLocalRenderInformationObjects", this)
50 {}
51 
53  const CCopasiContainer * pParent)
54  : CLBase(src),
55  CCopasiContainer(src, pParent),
56  mKey(CCopasiRootContainer::getKeyFactory()->add("Layout", this)),
57  mDimensions(src.mDimensions),
58  mvCompartments("ListOfCompartmentGlyphs", this),
59  mvMetabs("ListOfMetaboliteGlyphs", this),
60  mvReactions("ListOfReactionGlyphs", this),
61  mvLabels("ListOfTextGlyphs", this),
62  mvGraphicalObjects("ListOfGraphicalObjects", this)
63  , mvLocalRenderInformationObjects(src.mvLocalRenderInformationObjects, this)
64 {
65  //TODO references from one glyph to another have to be reconstructed after
66  // copying. This applies to Labels and species reference glyphs
67 
68  std::map<std::string, std::string> forward;
69  std::map<std::string, std::string> reverse;
70 
72 
73  for (; compIt != src.mvCompartments.end(); ++compIt)
74  {
75  CLCompartmentGlyph *comp = new CLCompartmentGlyph(*(*compIt));
76  mvCompartments.add(comp, true);
77  forward[(*compIt)->getKey()] = comp->getKey();
78  reverse[comp->getKey()] = (*compIt)->getKey();
79  }
80 
82 
83  for (; metabIt != src.mvMetabs.end(); ++metabIt)
84  {
85  CLMetabGlyph *metab = new CLMetabGlyph(*(*metabIt));
86  mvMetabs.add(metab, true);
87  forward[(*metabIt)->getKey()] = metab->getKey();
88  reverse[metab->getKey()] = (*metabIt)->getKey();
89  }
90 
92 
93  for (; reactIt != src.mvReactions.end(); ++reactIt)
94  {
95  CLReactionGlyph *r = new CLReactionGlyph(*(*reactIt));
96  mvReactions.add(r, true);
97  forward[(*reactIt)->getKey()] = r->getKey();
98  reverse[r->getKey()] = (*reactIt)->getKey();
99 
100  for (size_t i = 0; i < r->getListOfMetabReferenceGlyphs().size(); ++i)
101  {
102  forward[(*reactIt)->getListOfMetabReferenceGlyphs()[i]->getKey()]
104  reverse[r->getListOfMetabReferenceGlyphs()[i]->getKey()]
105  = (*reactIt)->getListOfMetabReferenceGlyphs()[i]->getKey();
106  }
107  }
108 
110 
111  for (; textIt != src.mvLabels.end(); ++textIt)
112  {
113  CLTextGlyph *text = new CLTextGlyph(*(*textIt));
114  mvLabels.add(text, true);
115  forward[(*textIt)->getKey()] = text->getKey();
116  reverse[text->getKey()] = (*textIt)->getKey();
117  }
118 
120 
121  for (; generalIt != src.mvGraphicalObjects.end(); ++generalIt)
122  {
123  CLGeneralGlyph *general = new CLGeneralGlyph(*(*generalIt));
124  mvGraphicalObjects.add(general, true);
125  forward[(*generalIt)->getKey()] = general->getKey();
126  reverse[general->getKey()] = (*generalIt)->getKey();
127 
128  for (size_t i = 0; i < general->getListOfReferenceGlyphs().size(); ++i)
129  {
130  forward[(*generalIt)->getListOfReferenceGlyphs()[i]->getKey()]
131  = general->getListOfReferenceGlyphs()[i]->getKey();
132  reverse[general->getListOfReferenceGlyphs()[i]->getKey()]
133  = (*generalIt)->getListOfReferenceGlyphs()[i]->getKey();
134  }
135 
136  for (size_t i = 0; i < general->getListOfSubglyphs().size(); ++i)
137  {
138  forward[(*generalIt)->getListOfSubglyphs()[i]->getKey()]
139  = general->getListOfSubglyphs()[i]->getKey();
140  reverse[general->getListOfSubglyphs()[i]->getKey()]
141  = (*generalIt)->getListOfSubglyphs()[i]->getKey();
142  }
143  }
144 
145  {
146 
147  // by now we have collected all keys, now we need to replace them
148  std::map<std::string, std::string>::const_iterator constIt;
150 
151  for (; textIt != mvLabels.end(); ++textIt)
152  {
153  CLTextGlyph *text = *textIt;
154  const std::string& key = text->getGraphicalObjectKey();
155  constIt = forward.find(key);
156 
157  if (constIt == forward.end())
158  continue;
159 
160  text->setGraphicalObjectKey(constIt->second);
161  }
162 
164 
165  for (; reactIt != mvReactions.end(); ++reactIt)
166  {
167  CLReactionGlyph *r = *reactIt;
169 
170  for (; refIt != r->getListOfMetabReferenceGlyphs().end(); ++refIt)
171  {
172  CLMetabReferenceGlyph* current = *refIt;
173  const std::string& key = current->getMetabGlyphKey();
174  constIt = forward.find(key);
175 
176  if (constIt == forward.end())
177  continue;
178 
179  current->setMetabGlyphKey(constIt->second);
180  }
181  }
182 
184 
185  for (; generalIt != mvGraphicalObjects.end(); ++generalIt)
186  {
187  CLGeneralGlyph *g = *generalIt;
189 
190  for (; refIt != g->getListOfReferenceGlyphs().end(); ++refIt)
191  {
192  CLReferenceGlyph* current = *refIt;
193  const std::string& key = current->getTargetGlyphKey();
194  constIt = forward.find(key);
195 
196  if (constIt == forward.end())
197  continue;
198 
199  current->setTargetGlyphKey(constIt->second);
200  }
201 
202  //CCopasiVector<CLGraphicalObject>::iterator refIt = g->getListOfSubglyphs().begin();
203  //for(;refIt != g->getListOfReferenceGlyphs().end(); ++refIt)
204  //{
205  // CLGraphicalObject* current = *refIt;
206  // const std::string& key = current->getTargetGlyphKey();
207  // constIt = forward.find(key);
208  // if (constIt == forward.end())
209  // continue;
210  // current->setTargetGlyphKey(constIt->second);
211  //}
212  }
213  }
214 }
215 
216 CLayout::CLayout(const Layout & sbml,
217  std::map<std::string, std::string> & layoutmap,
218  const CCopasiContainer * pParent)
219  : CLBase(sbml),
220  CCopasiContainer(sbml.getId(), pParent, "Layout"),
221  mKey(CCopasiRootContainer::getKeyFactory()->add("Layout", this)),
222  mDimensions(*sbml.getDimensions()),
223  mvCompartments("ListOfCompartmentGlyphs", this),
224  mvMetabs("ListOfMetaboliteGlyphs", this),
225  mvReactions("ListOfReactionGlyphs", this),
226  mvLabels("ListOfTextGlyphs", this),
227  mvGraphicalObjects("ListOfGraphicalObjects", this)
228  , mvLocalRenderInformationObjects("ListOfLocalRenderInformationObjects", this)
229 {
230  //add the copasi key to the map
231  layoutmap[sbml.getId()] = mKey;
232 }
233 
235 {
237 }
238 
240 {
241  if (glyph)
242  mvCompartments.add(glyph, true); //true means vector takes ownership
243 }
244 
246 {
247  if (glyph)
248  mvMetabs.add(glyph, true); //true means vector takes ownership
249 }
250 
252 {
253  if (glyph)
254  mvReactions.add(glyph, true); //true means vector takes ownership
255 }
256 
258 {
259  if (glyph)
260  mvLabels.add(glyph, true); //true means vector takes ownership
261 }
262 
264 {
265  if (glyph)
266  mvGraphicalObjects.add(glyph, true); //true means vector takes ownership
267 }
268 
269 std::ostream & operator<<(std::ostream &os, const CLayout & l)
270 {
271  size_t i, imax;
272 
273  os << "Layout \"" << l.getObjectName() << "\" " << l.mDimensions << "\n\n";
274 
275  imax = l.mvCompartments.size();
276 
277  if (imax)
278  {
279  os << "List of compartment glyphs: \n\n";
280 
281  for (i = 0; i < imax; ++i)
282  os << *l.mvCompartments[i];
283  }
284 
285  imax = l.mvMetabs.size();
286 
287  if (imax)
288  {
289  os << "\nList of species glyphs: \n\n";
290 
291  for (i = 0; i < imax; ++i)
292  os << *l.mvMetabs[i];
293  }
294 
295  imax = l.mvReactions.size();
296 
297  if (imax)
298  {
299  os << "\nList of reaction glyphs: \n\n";
300 
301  for (i = 0; i < imax; ++i)
302  os << *l.mvReactions[i];
303  }
304 
305  imax = l.mvLabels.size();
306 
307  if (imax)
308  {
309  os << "\nList of labels: \n\n";
310 
311  for (i = 0; i < imax; ++i)
312  os << *l.mvLabels[i];
313  }
314 
315  imax = l.mvGraphicalObjects.size();
316 
317  if (imax)
318  {
319  os << "\nList of graphical objects: \n\n";
320 
321  for (i = 0; i < imax; ++i)
322  os << *l.mvGraphicalObjects[i];
323  }
324 
325  return os;
326 }
327 
328 void CLayout::print(std::ostream * os) const
329 {*os << *this;}
330 
331 void CLayout::exportToDotFile(std::ostream & os) const
332 {
333  os << "digraph G {\n";
334 
335  //species glyphs
336  size_t i, imax = mvMetabs.size();
337 
338  for (i = 0; i < imax; ++i)
339  {
340  writeDotNode(os, mvMetabs[i]->getKey(), mvMetabs[i]->getModelObjectDisplayName());
341  }
342 
343  //reaction glyphs
344  imax = mvReactions.size();
345 
346  for (i = 0; i < imax; ++i)
347  {
348  writeDotNode(os, mvReactions[i]->getKey() + "_S", "", 1);
349  writeDotNode(os, mvReactions[i]->getKey() + "_P", "", 1);
350  writeDotEdge(os, mvReactions[i]->getKey() + "_S", mvReactions[i]->getKey() + "_P", 1);
351 
352  size_t j, jmax = mvReactions[i]->getListOfMetabReferenceGlyphs().size();
353 
354  for (j = 0; j < jmax; ++j)
355  {
356  CLMetabReferenceGlyph* mrg = mvReactions[i]->getListOfMetabReferenceGlyphs()[j];
357 
359  writeDotEdge(os, mrg->getMetabGlyphKey(), mvReactions[i]->getKey() + "_S");
360  else if (mrg->getRole() == CLMetabReferenceGlyph::PRODUCT)
361  writeDotEdge(os, mvReactions[i]->getKey() + "_P", mrg->getMetabGlyphKey());
362  }
363  }
364 
365  os << "}" << std::endl;
366 }
367 
368 void CLayout::writeDotNode(std::ostream & os, const std::string & id,
369  const std::string & label,
370  int t) const
371 {
372  std::string tmp;
373 
374  if (t == 1)
375  tmp = " shape=point ";
376 
377  os << id << " [" << tmp << " label=\"" << label << "\"] \n";
378 }
379 
380 void CLayout::writeDotEdge(std::ostream & os, const std::string & id1,
381  const std::string & id2,
382  int t) const
383 {
384  std::string tmp;
385 
386  if (t == 1)
387  tmp = " [len=0.2] ";
388 
389  os << id1 << " -> " << id2 << tmp << "\n"; //[label=\"" << label << "\"] \n";
390 }
391 
392 void CLayout::exportToSBML(Layout * layout, const std::map<const CCopasiObject*, SBase*> & copasimodelmap,
393  std::map<std::string, const SBase*>& sbmlIDs
394  , const std::map<std::string, std::string>& globalKeyToIdMap
395  //,const std::map<std::string,std::map<std::string,std::string> >& globalColorKeyToIdMapMap
396  //,const std::map<std::string,std::map<std::string,std::string> >& globalGradientKeyToIdMapMap
397  //,const std::map<std::string,std::map<std::string,std::string> >& globalLineEndingKeyToIdMapMap
398  ) const
399 {
400  if (!layout) return;
401 
402  //Name and ID
403  std::string id = CSBMLExporter::createUniqueId(sbmlIDs, "layout", true);
404  layout->setId(id);
405  sbmlIDs.insert(std::pair<const std::string, const SBase*>(id, layout));
406  //we do not check if the layout is already present in the libsbml data
407  //structures. This is no big deal since at the moment no software
408  //relies on persistent IDs for layout elements.
409 
410  //Dimensions
411  Dimensions tmpDim = mDimensions.getSBMLDimensions();
412  layout->setDimensions(&tmpDim);
413 
414  //some of the following code is not used at the moment: the COPASI model map
415  //does not contain glyphs. Since this may change in the future I leave the code
416  //below.
417 
418  // create a map from COPASI layout object to SBML objects. We do not put
419  //the layout objects into the global map (copasimodelmap) but we need to have
420  //access to all objects in the current layout since speciesReferenceGlyph and
421  //textGlyph need to reference other graphical objects.
422  std::map<const CLBase*, const SBase*> layoutmap;
423 
424  //Compartment glyphs
425  size_t i, imax = mvCompartments.size();
426 
427  for (i = 0; i < imax; ++i)
428  {
430 
431  //check if the compartment glyph exists in the libsbml data
432  std::map<const CCopasiObject*, SBase*>::const_iterator it;
433  it = copasimodelmap.find(tmp);
434 
435  CompartmentGlyph * pCG;
436 
437  if (it == copasimodelmap.end()) //not found
438  {
439  pCG = layout->createCompartmentGlyph();
440  }
441  else
442  {
443  pCG = dynamic_cast<CompartmentGlyph*>(it->second);
444  }
445 
446  layoutmap.insert(std::pair<const CLBase*, const SBase*>(tmp, pCG));
447  tmp->exportToSBML(pCG, copasimodelmap, sbmlIDs);
448  }
449 
450  //Species glyphs
451  imax = mvMetabs.size();
452 
453  for (i = 0; i < imax; ++i)
454  {
455  CLMetabGlyph * tmp = mvMetabs[i];
456 
457  //check if the glyph exists in the libsbml data
458  std::map<const CCopasiObject*, SBase*>::const_iterator it;
459  it = copasimodelmap.find(tmp);
460 
461  SpeciesGlyph * pG;
462 
463  if (it == copasimodelmap.end()) //not found
464  {
465  pG = layout->createSpeciesGlyph();
466  }
467  else
468  {
469  pG = dynamic_cast<SpeciesGlyph*>(it->second);
470  }
471 
472  layoutmap.insert(std::pair<const CLBase*, const SBase*>(tmp, pG));
473  tmp->exportToSBML(pG, copasimodelmap, sbmlIDs);
474  }
475 
476  //Reaction glyphs
477  imax = mvReactions.size();
478 
479  for (i = 0; i < imax; ++i)
480  {
481  CLReactionGlyph * tmp = mvReactions[i];
482 
483  //check if the glyph exists in the libsbml data
484  std::map<const CCopasiObject*, SBase*>::const_iterator it;
485  it = copasimodelmap.find(tmp);
486 
487  ReactionGlyph * pG;
488 
489  if (it == copasimodelmap.end()) //not found
490  {
491  pG = layout->createReactionGlyph();
492  }
493  else
494  {
495  pG = dynamic_cast<ReactionGlyph*>(it->second);
496  }
497 
498  layoutmap.insert(std::pair<const CLBase*, const SBase*>(tmp, pG));
499  //we need to pass the layoutmap here for 2 reasons:
500  //1. the metabreferenceglyphs need to be added
501  //2. the metabreferenceglyphs need to resolve the reference to the metabglyph
502  tmp->exportToSBML(pG, copasimodelmap, sbmlIDs, layoutmap);
503  }
504 
505  //Text glyphs
506  imax = mvLabels.size();
507 
508  for (i = 0; i < imax; ++i)
509  {
510  CLTextGlyph * tmp = mvLabels[i];
511 
512  //check if the glyph exists in the libsbml data
513  std::map<const CCopasiObject*, SBase*>::const_iterator it;
514  it = copasimodelmap.find(tmp);
515 
516  TextGlyph * pG;
517 
518  if (it == copasimodelmap.end()) //not found
519  {
520  pG = layout->createTextGlyph();
521  }
522  else
523  {
524  pG = dynamic_cast<TextGlyph*>(it->second);
525  }
526 
527  layoutmap.insert(std::pair<const CLBase*, const SBase*>(tmp, pG));
528  tmp->exportToSBML(pG, copasimodelmap, sbmlIDs);
529  }
530 
531  //generic glyphs
532  imax = mvGraphicalObjects.size();
533 
534  for (i = 0; i < imax; ++i)
535  {
537 
538  //check if the glyph exists in the libsbml data
539  std::map<const CCopasiObject*, SBase*>::const_iterator it;
540  it = copasimodelmap.find(tmp);
541 
542  GraphicalObject * pG;
543 
544  if (it == copasimodelmap.end()) //not found
545  {
546 #if LIBSBML_VERSION >= 50800
547 
548  if (tmp->getListOfReferenceGlyphs().size() > 0)
549  pG = layout->createGeneralGlyph();
550  else
551 #endif // LIBSBML_VERSION >= 50800
552  pG = layout->createAdditionalGraphicalObject();
553  }
554  else
555  {
556  pG = dynamic_cast<GraphicalObject*>(it->second);
557  }
558 
559  layoutmap.insert(std::pair<const CLBase*, const SBase*>(tmp, pG));
560  tmp->exportToSBML(pG, copasimodelmap, sbmlIDs, layoutmap);
561  }
562 
563  //now that we have all graphical objects in the layoutmap we can resolve the references
564  //in the text glyphs
565  imax = mvLabels.size();
566 
567  for (i = 0; i < imax; ++i)
568  {
569  const CLTextGlyph * tmp = mvLabels[i];
570 
571  //find the corresponding SBML object
572  std::map<const CLBase*, const SBase*>::const_iterator it = layoutmap.find(tmp);
573 
574  if (it != layoutmap.end() && it->second && dynamic_cast<const TextGlyph*>(it->second))
575  {
576  tmp->exportReferenceToSBML(const_cast<TextGlyph*>(dynamic_cast<const TextGlyph*>(it->second)), layoutmap);
577  }
578  }
579 
580  // export the local render information
581  imax = this->mvLocalRenderInformationObjects.size();
582  LocalRenderInformation* pLRI = NULL;
583  std::map<std::string, std::string> keyToIdMap;
584 
585  /*
586  std::map<std::string,std::string> colorKeyToIdMap;
587  std::map<std::string,std::string> gradientKeyToIdMap;
588  std::map<std::string,std::string> lineEndingKeyToIdMap;
589  std::map<std::string,std::map<std::string,std::string> > colorKeyToIdMapMap;
590  std::map<std::string,std::map<std::string,std::string> > gradientKeyToIdMapMap;
591  std::map<std::string,std::map<std::string,std::string> > lineEndingKeyToIdMapMap;
592  */
593  for (i = 0; i < imax; ++i)
594  {
595  //colorKeyToIdMap.clear();
596  //gradientKeyToIdMap.clear();
597  //lineEndingKeyToIdMap.clear();
598  //pLRI=this->mvLocalRenderInformationObjects[i]->toSBML(colorKeyToIdMap,gradientKeyToIdMap,lineEndingKeyToIdMap);
599  pLRI = this->mvLocalRenderInformationObjects[i]->toSBML(layout->getLevel(), layout->getVersion());
600  keyToIdMap.insert(std::pair<std::string, std::string>(this->mvLocalRenderInformationObjects[i]->getKey(), pLRI->getId()));
601  //colorKeyToIdMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pLRI->getId(),colorKeyToIdMap));
602  //gradientKeyToIdMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pLRI->getId(),gradientKeyToIdMap));
603  //lineEndingKeyToIdMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pLRI->getId(),lineEndingKeyToIdMap));
604  // fix the references to layout objects in id lists
605  std::map<const CLBase*, const SBase*>::const_iterator layoutMapIt = layoutmap.begin(), layoutMapEndit = layoutmap.end();
606  std::map<std::string, std::string> layoutObjectKeyToIdMap;
607  const CLGraphicalObject* pGObject = NULL;
608 
609  while (layoutMapIt != layoutMapEndit)
610  {
611  pGObject = dynamic_cast<const CLGraphicalObject*>(layoutMapIt->first);
612 
613  if (pGObject != NULL)
614  {
615  layoutObjectKeyToIdMap.insert(std::pair<std::string, std::string>(pGObject->getKey(), layoutMapIt->second->getId()));
616  }
617 
618  ++layoutMapIt;
619  }
620 
621  size_t j, jMax = pLRI->getNumStyles();
622 
623  for (j = 0; j < jMax; ++j)
624  {
625  SBMLDocumentLoader::convertLayoutObjectKeys(*(pLRI->getStyle((unsigned int) j)), layoutObjectKeyToIdMap);
626  }
627 
628  RenderLayoutPlugin* rlolPlugin = (RenderLayoutPlugin*) layout->getPlugin("render");
629 
630  if (rlolPlugin == NULL)
631  {
632  const std::string uri = (layout->getLevel() < 3 ? RenderExtension::getXmlnsL2() : RenderExtension::getXmlnsL3V1V1());
633  layout->enablePackage(uri, "render", true);
634 
635  if (layout->getLevel() > 2)
636  layout->getSBMLDocument()->setPackageRequired("render", false);
637 
638  rlolPlugin = (RenderLayoutPlugin*) layout->getPlugin("render");
639  }
640 
641  if (rlolPlugin != NULL)
642  rlolPlugin->getListOfLocalRenderInformation()->appendAndOwn(pLRI);
643  }
644 }
645 
647 {
648  if (pRenderInfo)
649  {
650  this->mvLocalRenderInformationObjects.add(pRenderInfo, true); //true means vector takes ownership
651  }
652 }
653 
654 /**
655  * Returns a const pointer to the local render information with the given index or NULL
656  * if the index is invalid.
657  */
659 {
660  if (index >= this->mvLocalRenderInformationObjects.size()) return NULL;
661 
662  return this->mvLocalRenderInformationObjects[index];
663 }
664 
665 /**
666  * Returns a pointer to the local render information with the given index or NULL
667  * if the index is invalid.
668  */
670 {
671  if (index >= this->mvLocalRenderInformationObjects.size()) return NULL;
672 
673  return this->mvLocalRenderInformationObjects[index];
674 }
675 
676 /**
677  * This methods calculates the bounding box of the layout.
678  * It traverses all layout objects and looks for the minimal and maximal x
679  * and y values that occur in the layout.
680  * These values are returned in the form of a bounding box where the minimal
681  * values are stored in the position and the maxima are given as the minimal
682  * values plus the corresponding dimension.
683  */
685 {
686  double minX = std::numeric_limits<double>::max();
687  double minY = minX;
688  double maxX = -minX;
689  double maxY = -minX;
690 
691  const CLGraphicalObject* pObject = NULL;
692  const CLCurve* pCurve = NULL;
693  const CLBoundingBox* pBB;
694  const CLPoint* pP = NULL;
695  const CLDimensions* pDim;
696  size_t i, iMax = this->getListOfCompartmentGlyphs().size();
697  double x, y, x2, y2;
698 
699  for (i = 0; i < iMax; ++i)
700  {
701  pObject = this->getListOfCompartmentGlyphs()[i];
702  pBB = &pObject->getBoundingBox();
703  pP = &pBB->getPosition();
704  x = pP->getX();
705  y = pP->getY();
706  pDim = &pBB->getDimensions();
707  x2 = x + pDim->getWidth();
708  y2 = y + pDim->getHeight();
709  minX = (minX < x) ? minX : x;
710  minY = (minY < y) ? minY : y;
711  maxX = (maxX > x2) ? maxX : x2;
712  maxY = (maxY > y2) ? maxY : y2;
713  }
714 
715  iMax = this->getListOfMetaboliteGlyphs().size();
716 
717  for (i = 0; i < iMax; ++i)
718  {
719  pObject = this->getListOfMetaboliteGlyphs()[i];
720  pBB = &pObject->getBoundingBox();
721  pP = &pBB->getPosition();
722  x = pP->getX();
723  y = pP->getY();
724  pDim = &pBB->getDimensions();
725  x2 = x + pDim->getWidth();
726  y2 = y + pDim->getHeight();
727  minX = (minX < x) ? minX : x;
728  minY = (minY < y) ? minY : y;
729  maxX = (maxX > x2) ? maxX : x2;
730  maxY = (maxY > y2) ? maxY : y2;
731  }
732 
733  iMax = this->getListOfTextGlyphs().size();
734 
735  for (i = 0; i < iMax; ++i)
736  {
737  pObject = this->getListOfTextGlyphs()[i];
738  pBB = &pObject->getBoundingBox();
739  pP = &pBB->getPosition();
740  x = pP->getX();
741  y = pP->getY();
742  pDim = &pBB->getDimensions();
743  x2 = x + pDim->getWidth();
744  y2 = y + pDim->getHeight();
745  minX = (minX < x) ? minX : x;
746  minY = (minY < y) ? minY : y;
747  maxX = (maxX > x2) ? maxX : x2;
748  maxY = (maxY > y2) ? maxY : y2;
749  }
750 
751  size_t j, jMax;
752  const CLGeneralGlyph* pGG = NULL;
753  const CLReferenceGlyph* pRefG = NULL;
754  iMax = this->getListOfGeneralGlyphs().size();
755 
756  for (i = 0; i < iMax; ++i)
757  {
758  pGG = this->getListOfGeneralGlyphs()[i];
759 
760  if (pGG->getCurve().getNumCurveSegments() > 0)
761  {
762  pCurve = &pGG->getCurve();
763  CLBoundingBox bb = pCurve->calculateBoundingBox();
764  pP = &bb.getPosition();
765  x = pP->getX();
766  y = pP->getY();
767  pDim = &bb.getDimensions();
768  x2 = x + pDim->getWidth();
769  y2 = y + pDim->getHeight();
770  minX = (minX < x) ? minX : x;
771  minY = (minY < y) ? minY : y;
772  maxX = (maxX > x2) ? maxX : x2;
773  maxY = (maxY > y2) ? maxY : y2;
774  }
775  else
776  {
777  pBB = &pGG->getBoundingBox();
778  pP = &pBB->getPosition();
779  x = pP->getX();
780  y = pP->getY();
781  pDim = &pBB->getDimensions();
782  x2 = x + pDim->getWidth();
783  y2 = y + pDim->getHeight();
784  minX = (minX < x) ? minX : x;
785  minY = (minY < y) ? minY : y;
786  maxX = (maxX > x2) ? maxX : x2;
787  maxY = (maxY > y2) ? maxY : y2;
788  }
789 
790  jMax = pGG->getListOfReferenceGlyphs().size();
791 
792  for (j = 0; j < jMax; ++j)
793  {
794  pRefG = pGG->getListOfReferenceGlyphs()[j];
795 
796  if (pRefG->getCurve().getNumCurveSegments() > 0)
797  {
798  pCurve = &pRefG->getCurve();
799  CLBoundingBox bb = pCurve->calculateBoundingBox();
800  pP = &bb.getPosition();
801  x = pP->getX();
802  y = pP->getY();
803  pDim = &bb.getDimensions();
804  x2 = x + pDim->getWidth();
805  y2 = y + pDim->getHeight();
806  minX = (minX < x) ? minX : x;
807  minY = (minY < y) ? minY : y;
808  maxX = (maxX > x2) ? maxX : x2;
809  maxY = (maxY > y2) ? maxY : y2;
810  }
811  else
812  {
813  pBB = &pRefG->getBoundingBox();
814  pP = &pBB->getPosition();
815  x = pP->getX();
816  y = pP->getY();
817  pDim = &pBB->getDimensions();
818  x2 = x + pDim->getWidth();
819  y2 = y + pDim->getHeight();
820  minX = (minX < x) ? minX : x;
821  minY = (minY < y) ? minY : y;
822  maxX = (maxX > x2) ? maxX : x2;
823  maxY = (maxY > y2) ? maxY : y2;
824  }
825  }
826  }
827 
828  const CLReactionGlyph* pRG = NULL;
829 
830  const CLMetabReferenceGlyph* pSRG = NULL;
831 
832  iMax = this->getListOfReactionGlyphs().size();
833 
834  for (i = 0; i < iMax; ++i)
835  {
836  pRG = this->getListOfReactionGlyphs()[i];
837 
838  if (pRG->getCurve().getNumCurveSegments() > 0)
839  {
840  pCurve = &pRG->getCurve();
841  CLBoundingBox bb = pCurve->calculateBoundingBox();
842  pP = &bb.getPosition();
843  x = pP->getX();
844  y = pP->getY();
845  pDim = &bb.getDimensions();
846  x2 = x + pDim->getWidth();
847  y2 = y + pDim->getHeight();
848  minX = (minX < x) ? minX : x;
849  minY = (minY < y) ? minY : y;
850  maxX = (maxX > x2) ? maxX : x2;
851  maxY = (maxY > y2) ? maxY : y2;
852  }
853  else
854  {
855  pBB = &pRG->getBoundingBox();
856  pP = &pBB->getPosition();
857  x = pP->getX();
858  y = pP->getY();
859  pDim = &pBB->getDimensions();
860  x2 = x + pDim->getWidth();
861  y2 = y + pDim->getHeight();
862  minX = (minX < x) ? minX : x;
863  minY = (minY < y) ? minY : y;
864  maxX = (maxX > x2) ? maxX : x2;
865  maxY = (maxY > y2) ? maxY : y2;
866  }
867 
868  jMax = pRG->getListOfMetabReferenceGlyphs().size();
869 
870  for (j = 0; j < jMax; ++j)
871  {
872  pSRG = pRG->getListOfMetabReferenceGlyphs()[j];
873 
874  if (pSRG->getCurve().getNumCurveSegments() > 0)
875  {
876  pCurve = &pSRG->getCurve();
877  CLBoundingBox bb = pCurve->calculateBoundingBox();
878  pP = &bb.getPosition();
879  x = pP->getX();
880  y = pP->getY();
881  pDim = &bb.getDimensions();
882  x2 = x + pDim->getWidth();
883  y2 = y + pDim->getHeight();
884  minX = (minX < x) ? minX : x;
885  minY = (minY < y) ? minY : y;
886  maxX = (maxX > x2) ? maxX : x2;
887  maxY = (maxY > y2) ? maxY : y2;
888  }
889  else
890  {
891  pBB = &pSRG->getBoundingBox();
892  pP = &pBB->getPosition();
893  x = pP->getX();
894  y = pP->getY();
895  pDim = &pBB->getDimensions();
896  x2 = x + pDim->getWidth();
897  y2 = y + pDim->getHeight();
898  minX = (minX < x) ? minX : x;
899  minY = (minY < y) ? minY : y;
900  maxX = (maxX > x2) ? maxX : x2;
901  maxY = (maxY > y2) ? maxY : y2;
902  }
903  }
904  }
905 
906  return CLBoundingBox(CLPoint(minX, minY), CLDimensions(maxX - minX, maxY - minY));
907 }
908 
909 void CLayout::moveBy(const CLPoint &p)
910 {
911  if (p.isEmpty()) return;
912 
913  CLGraphicalObject* pObject = NULL;
914  size_t i, iMax = this->getListOfCompartmentGlyphs().size();
915 
916  for (i = 0; i < iMax; ++i)
917  {
918  pObject = this->getListOfCompartmentGlyphs()[i];
919  pObject->moveBy(p);
920  }
921 
922  iMax = this->getListOfMetaboliteGlyphs().size();
923 
924  for (i = 0; i < iMax; ++i)
925  {
926  pObject = this->getListOfMetaboliteGlyphs()[i];
927  pObject->moveBy(p);
928  }
929 
930  iMax = this->getListOfReactionGlyphs().size();
931 
932  for (i = 0; i < iMax; ++i)
933  {
934  pObject = this->getListOfReactionGlyphs()[i];
935  pObject->moveBy(p);
936  }
937 
938  iMax = this->getListOfTextGlyphs().size();
939 
940  for (i = 0; i < iMax; ++i)
941  {
942  pObject = this->getListOfTextGlyphs()[i];
943  pObject->moveBy(p);
944  }
945 
946  iMax = this->getListOfGeneralGlyphs().size();
947 
948  for (i = 0; i < iMax; ++i)
949  {
950  pObject = this->getListOfGeneralGlyphs()[i];
951  pObject->moveBy(p);
952  }
953 }
954 
956 {
958  // move into the origin
959  CLPoint differenceToOrigin(
960  -bb.getPosition().getX(),
961  -bb.getPosition().getY()
962  , -bb.getPosition().getZ()
963  );
964  moveBy(differenceToOrigin);
966 }
CCopasiVector< CLCompartmentGlyph > mvCompartments
Definition: CLayout.h:42
bool remove(const std::string &key)
CLBoundingBox calculateBoundingBox() const
Definition: CLCurve.cpp:218
void addReactionGlyph(CLReactionGlyph *glyph)
Definition: CLayout.cpp:251
const C_FLOAT64 & getZ() const
Definition: CLBase.h:85
const CCopasiVector< CLTextGlyph > & getListOfTextGlyphs() const
Definition: CLayout.h:120
const C_FLOAT64 & getWidth() const
Definition: CLBase.h:211
virtual void exportToSBML(ReactionGlyph *g, const std::map< const CCopasiObject *, SBase * > &copasimodelmap, std::map< std::string, const SBase * > &sbmlIDs, std::map< const CLBase *, const SBase * > &layoutmap) const
void writeDotEdge(std::ostream &os, const std::string &id1, const std::string &id2, int t=0) const
Definition: CLayout.cpp:380
void addGeneralGlyph(CLGeneralGlyph *glyph)
Definition: CLayout.cpp:263
const CLBoundingBox & getBoundingBox() const
const std::string & getObjectName() const
void addLocalRenderInformation(CLLocalRenderInformation *pRenderInfo)
Definition: CLayout.cpp:646
virtual size_t size() const
CLayout(const std::string &name="Layout", const CCopasiContainer *pParent=NULL)
Definition: CLayout.cpp:38
iterator begin()
Dimensions getSBMLDimensions() const
Definition: CLBase.cpp:43
void setDimensions(const CLDimensions &d)
Definition: CLayout.h:77
static void convertLayoutObjectKeys(LocalStyle &style, const std::map< std::string, std::string > &keyToIdMap)
CCopasiVector< CLReactionGlyph > mvReactions
Definition: CLayout.h:44
std::ostream & operator<<(std::ostream &os, const CLayout &l)
Definition: CLayout.cpp:269
const CCopasiVector< CLGraphicalObject > & getListOfSubglyphs() const
virtual void exportToSBML(CompartmentGlyph *cg, const std::map< const CCopasiObject *, SBase * > &copasimodelmap, std::map< std::string, const SBase * > &sbmlIDs) const
Definition: CLGlyphs.cpp:145
void setTargetGlyphKey(const std::string &k)
const std::string & getMetabGlyphKey() const
const CLLocalRenderInformation * getRenderInformation(size_t index) const
Definition: CLayout.cpp:658
void addMetaboliteGlyph(CLMetabGlyph *glyph)
Definition: CLayout.cpp:245
const CCopasiVector< CLCompartmentGlyph > & getListOfCompartmentGlyphs() const
Definition: CLayout.h:81
static const std::string createUniqueId(const std::map< std::string, const SBase * > &idMap, const std::string &prefix, bool addIndexForFirst, const std::string &separator="_")
virtual const std::string & getKey() const
const CLDimensions & getDimensions() const
Definition: CLBase.h:266
void addTextGlyph(CLTextGlyph *glyph)
Definition: CLayout.cpp:257
virtual const std::string & getKey() const
iterator end()
void print(std::ostream *ostream) const
Definition: CLayout.cpp:328
~CLayout()
Definition: CLayout.cpp:234
const C_FLOAT64 & getX() const
Definition: CLBase.h:83
Definition: CLBase.h:54
bool isEmpty() const
Definition: CLBase.h:163
CLDimensions mDimensions
Definition: CLayout.h:40
void writeDotNode(std::ostream &os, const std::string &id, const std::string &label, int t=0) const
Definition: CLayout.cpp:368
const CLPoint & getPosition() const
Definition: CLBase.h:265
virtual bool add(const CType &src)
virtual void exportToSBML(SpeciesGlyph *g, const std::map< const CCopasiObject *, SBase * > &copasimodelmap, std::map< std::string, const SBase * > &sbmlIDs) const
Definition: CLGlyphs.cpp:73
void calculateAndAssignBounds()
Definition: CLayout.cpp:955
Definition: CLBase.h:33
const CCopasiVector< CLMetabGlyph > & getListOfMetaboliteGlyphs() const
Definition: CLayout.h:94
const CCopasiVector< CLReferenceGlyph > & getListOfReferenceGlyphs() const
CLBoundingBox calculateBoundingBox() const
Definition: CLayout.cpp:684
void setGraphicalObjectKey(const std::string &k)
Definition: CLGlyphs.cpp:338
virtual const std::string & getKey() const
Definition: CLayout.h:71
const CCopasiVector< CLGeneralGlyph > & getListOfGeneralGlyphs() const
Definition: CLayout.h:133
CCopasiVector< CLTextGlyph > mvLabels
Definition: CLayout.h:45
CCopasiVector< CLLocalRenderInformation > mvLocalRenderInformationObjects
Definition: CLayout.h:47
void addCompartmentGlyph(CLCompartmentGlyph *glyph)
Definition: CLayout.cpp:239
std::string mKey
Definition: CLayout.h:38
void setMetabGlyphKey(const std::string &k)
const C_FLOAT64 & getY() const
Definition: CLBase.h:84
static CKeyFactory * getKeyFactory()
const C_FLOAT64 & getHeight() const
Definition: CLBase.h:212
CCopasiVector< CLMetabGlyph > mvMetabs
Definition: CLayout.h:43
virtual void moveBy(const CLPoint &p)
Definition: CLayout.cpp:909
virtual void moveBy(const CLPoint &p)
void exportReferenceToSBML(TextGlyph *g, const std::map< const CLBase *, const SBase * > &layoutmap) const
Definition: CLGlyphs.cpp:297
CCopasiVector< CLGeneralGlyph > mvGraphicalObjects
Definition: CLayout.h:46
const CCopasiVector< CLMetabReferenceGlyph > & getListOfMetabReferenceGlyphs() const
void exportToDotFile(std::ostream &os) const
Definition: CLayout.cpp:331
virtual void exportToSBML(TextGlyph *g, const std::map< const CCopasiObject *, SBase * > &copasimodelmap, std::map< std::string, const SBase * > &sbmlIDs) const
Definition: CLGlyphs.cpp:267
virtual void exportToSBML(GraphicalObject *g, const std::map< const CCopasiObject *, SBase * > &copasimodelmap, std::map< std::string, const SBase * > &sbmlIDs, std::map< const CLBase *, const SBase * > &layoutmap) const
void exportToSBML(Layout *layout, const std::map< const CCopasiObject *, SBase * > &copasimodelmap, std::map< std::string, const SBase * > &sbmlIDs, const std::map< std::string, std::string > &globalKeyToIdMap) const
Definition: CLayout.cpp:392
const std::string & getTargetGlyphKey() const
const CLCurve & getCurve() const
size_t getNumCurveSegments() const
Definition: CLCurve.h:168
const CCopasiVector< CLReactionGlyph > & getListOfReactionGlyphs() const
Definition: CLayout.h:107
const std::string & getGraphicalObjectKey() const
Definition: CLGlyphs.h:163
#define max(a, b)
Definition: f2c.h:176