COPASI API  4.16.103
CLCurve.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 
17 #include <sbml/packages/layout/sbml/Curve.h>
18 #include <sbml/packages/layout/sbml/CubicBezier.h>
19 
20 #include "CLCurve.h"
21 
22 CLLineSegment::CLLineSegment(const LineSegment & ls)
23  : CLBase(ls),
24  mStart(*ls.getStart()),
25  mEnd(*ls.getEnd()),
26  mBase1(),
27  mBase2(),
28  mIsBezier(false)
29 {
30  //handle bezier
31  const CubicBezier * cb = dynamic_cast<const CubicBezier *>(&ls);
32 
33  if (cb)
34  {
35  mIsBezier = true;
36  mBase1 = CLPoint(*cb->getBasePoint1());
37  mBase2 = CLPoint(*cb->getBasePoint2());
38  }
39 }
40 
41 void CLLineSegment::exportToSBMLBezier(CubicBezier * c, const std::map<const CCopasiObject*, SBase*> & copasimodelmap) const
42 {
43  if (!c) return;
44 
45  //first the part that is inherited from line segment
46  exportToSBMLLineSegment(c, copasimodelmap);
47 
48  //now the rest
49  c->setBasePoint1(this->getBase1().getX(), this->getBase1().getY());
50  c->setBasePoint2(this->getBase2().getX(), this->getBase2().getY());
51 }
52 
54  const std::map<const CCopasiObject*, SBase*> & /* copasimodelmap */) const
55 {
56  if (!l) return;
57 
58  l->setStart(this->getStart().getX(), this->getStart().getY());
59  l->setEnd(this->getEnd().getX(), this->getEnd().getY());
60 }
61 
62 std::ostream & operator<<(std::ostream &os, const CLLineSegment & ls)
63 {
64  os << "[" << ls.mStart << "->" << ls.mEnd << "]";
65 
66  if (ls.mIsBezier)
67  os << " " << ls.mBase1 << ", " << ls.mBase2;
68 
69  return os;
70 }
71 
72 //****************************************************
73 
75  : CLBase(c),
76  mvCurveSegments(c.mvCurveSegments)
77 {}
78 
79 CLCurve::CLCurve(const Curve & sbmlcurve)
80  : CLBase(sbmlcurve),
81  mvCurveSegments()
82 {
83  //TODO
84  C_INT32 i, imax = sbmlcurve.getListOfCurveSegments()->size();
85 
86  for (i = 0; i < imax; ++i)
87  {
88  const LineSegment* tmp
89  = dynamic_cast<const LineSegment*>(sbmlcurve.getListOfCurveSegments()->get(i));
90 
91  if (tmp)
92  mvCurveSegments.push_back(CLLineSegment(*tmp));
93 
94  //addCurveSegment(CLLineSegment(*tmp));
95  }
96 }
97 
99 {}
100 
102 {
103  mvCurveSegments.push_back(ls);
104 }
105 
107 {
108  if (pLs)
109  mvCurveSegments.push_back(*pLs);
110 }
111 
113 {
114  mvCurveSegments.clear();
115 }
116 
118 {
119  if (mvCurveSegments.size() <= 1) return true;
120 
121  size_t i, imax = mvCurveSegments.size() - 1;
122 
123  for (i = 0; i < imax; ++i)
124  if (!(mvCurveSegments[i].getEnd() == mvCurveSegments[i + 1].getStart()))
125  return false;
126 
127  return true;
128 }
129 
130 std::vector <CLPoint> CLCurve::getListOfPoints() const
131 {
132  std::vector <CLPoint> ret;
133 
134  if (mvCurveSegments.size() == 0) return ret;
135 
136  if (!isContinuous()) return ret;
137 
138  size_t i, imax = mvCurveSegments.size();
139 
140  for (i = 0; i < imax; ++i)
141  ret.push_back(mvCurveSegments[i].getStart());
142 
143  ret.push_back(mvCurveSegments[i - 1].getEnd());
144 
145  return ret;
146 }
147 
148 void CLCurve::exportToSBML(Curve * c, const std::map<const CCopasiObject*, SBase*> & copasimodelmap) const
149 {
150  if (!c) return;
151 
152  //we will reconstruct the curve completely without checking the existing curve
153  size_t i, imax = c->getListOfCurveSegments()->size();
154 
155  for (i = 0; i < imax; ++i) delete c->getListOfCurveSegments()->remove(0);
156 
157  //curve segments
158  imax = mvCurveSegments.size();
159 
160  for (i = 0; i < imax; ++i)
161  {
162  if (mvCurveSegments[i].isBezier())
163  {
164  mvCurveSegments[i].exportToSBMLBezier(c->createCubicBezier(), copasimodelmap);
165  }
166  else
167  {
168  mvCurveSegments[i].exportToSBMLLineSegment(c->createLineSegment(), copasimodelmap);
169  }
170  }
171 }
172 
173 std::ostream & operator<<(std::ostream &os, const CLCurve & c)
174 {
175  if (c.getNumCurveSegments())
176  {
177  os << " Curve:\n";
178  size_t i, imax = c.getNumCurveSegments();
179 
180  for (i = 0; i < imax; ++i)
181  os << " " << c.getCurveSegments()[i] << "\n";
182  }
183 
184  return os;
185 }
186 
187 void CLCurve::moveBy(const CLPoint &p)
188 {
189  CLLineSegment* pLS = NULL;
190  CLPoint* pP = NULL;
191  size_t i, iMax = this->getNumCurveSegments();
192 
193  for (i = 0; i < iMax; ++i)
194  {
195  pLS = this->getSegmentAt(i);
196  pP = &pLS->getStart();
197  pP->moveBy(p);
198 
199  pP = &pLS->getEnd();
200  pP->moveBy(p);
201 
202  if (pLS->isBezier())
203  {
204  pP = &pLS->getBase1();
205  pP->moveBy(p);
206 
207  pP = &pLS->getBase2();
208  pP->moveBy(p);
209  }
210  }
211 }
212 
213 /**
214  * Calculates the bounding box for the curve.
215  * Basepoints for cubic beziers are considered to belong inside the bounding
216  * box.
217  */
219 {
220  double xMin = std::numeric_limits<double>::max();
221  double yMin = xMin;
222  double xMax = -xMin;
223  double yMax = -xMin;
224  double x, y;
225  size_t i, iMax = this->getNumCurveSegments();
226  const CLLineSegment* pLS = NULL;
227  const CLPoint* pP = NULL;
228 
229  for (i = 0; i < iMax; ++i)
230  {
231  pLS = this->getSegmentAt(i);
232  pP = &pLS->getStart();
233  x = pP->getX();
234  y = pP->getY();
235  xMin = (xMin < x) ? xMin : x;
236  yMin = (yMin < y) ? yMin : y;
237  xMax = (xMax > x) ? xMax : x;
238  yMax = (yMax > y) ? yMax : y;
239  pP = &pLS->getEnd();
240  x = pP->getX();
241  y = pP->getY();
242  xMin = (xMin < x) ? xMin : x;
243  yMin = (yMin < y) ? yMin : y;
244  xMax = (xMax > x) ? xMax : x;
245  yMax = (yMax > y) ? yMax : y;
246 
247  if (pLS->isBezier())
248  {
249  pP = &pLS->getBase1();
250  x = pP->getX();
251  y = pP->getY();
252  xMin = (xMin < x) ? xMin : x;
253  yMin = (yMin < y) ? yMin : y;
254  xMax = (xMax > x) ? xMax : x;
255  yMax = (yMax > y) ? yMax : y;
256  pP = &pLS->getBase2();
257  x = pP->getX();
258  y = pP->getY();
259  xMin = (xMin < x) ? xMin : x;
260  yMin = (yMin < y) ? yMin : y;
261  xMax = (xMax > x) ? xMax : x;
262  yMax = (yMax > y) ? yMax : y;
263  }
264  }
265 
266  return CLBoundingBox(CLPoint(xMin, yMin), CLDimensions(xMax - xMin, yMax - yMin));
267 }
CLBoundingBox calculateBoundingBox() const
Definition: CLCurve.cpp:218
void exportToSBML(Curve *c, const std::map< const CCopasiObject *, SBase * > &copasimodelmap) const
Definition: CLCurve.cpp:148
CLPoint mEnd
Definition: CLCurve.h:38
bool mIsBezier
Definition: CLCurve.h:43
const std::vector< CLLineSegment > & getCurveSegments() const
Definition: CLCurve.h:154
void exportToSBMLLineSegment(LineSegment *l, const std::map< const CCopasiObject *, SBase * > &copasimodelmap) const
Definition: CLCurve.cpp:53
~CLCurve()
Definition: CLCurve.cpp:98
void exportToSBMLBezier(CubicBezier *c, const std::map< const CCopasiObject *, SBase * > &copasimodelmap) const
Definition: CLCurve.cpp:41
#define C_INT32
Definition: copasi.h:90
virtual void moveBy(const CLPoint &p)
Definition: CLCurve.cpp:187
const CLPoint & getBase1() const
Definition: CLCurve.h:82
CLPoint mBase1
Definition: CLCurve.h:40
CLCurve()
Definition: CLCurve.h:139
const CLPoint & getBase2() const
Definition: CLCurve.h:83
const CLPoint & getEnd() const
Definition: CLCurve.h:75
std::ostream & operator<<(std::ostream &os, const CLLineSegment &ls)
Definition: CLCurve.cpp:62
const C_FLOAT64 & getX() const
Definition: CLBase.h:83
Definition: CLBase.h:54
Definition: CLBase.h:33
void addCurveSegment(const CLLineSegment &ls)
Definition: CLCurve.cpp:101
std::vector< CLPoint > getListOfPoints() const
Definition: CLCurve.cpp:130
CLPoint mBase2
Definition: CLCurve.h:41
const C_FLOAT64 & getY() const
Definition: CLBase.h:84
bool isBezier() const
Definition: CLCurve.h:90
const CLLineSegment * getSegmentAt(size_t i) const
Definition: CLCurve.h:156
size_t getNumCurveSegments() const
Definition: CLCurve.h:168
virtual void moveBy(const CLPoint &p)
Definition: CLBase.h:120
void clear()
Definition: CLCurve.cpp:112
bool isContinuous() const
Definition: CLCurve.cpp:117
const CLPoint & getStart() const
Definition: CLCurve.h:74
CLPoint mStart
Definition: CLCurve.h:37
std::vector< CLLineSegment > mvCurveSegments
Definition: CLCurve.h:135
CLLineSegment()
Definition: CLCurve.h:47
#define max(a, b)
Definition: f2c.h:176