COPASI API  4.16.103
CLRelAbsVector.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layout/CLRelAbsVector.cpp,v $
3 // $Revision: 1.5 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/04/23 15:44:51 $
7 // End CVS Header
8 
9 // Copyright (C) 2012 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 #include <iostream>
15 #include <sstream>
16 #include <limits>
17 #include <cmath>
18 #include <string.h>
19 #include <stdlib.h>
20 
21 #include <sbml/packages/render/sbml/RelAbsVector.h>
22 
23 #include "CLRelAbsVector.h"
24 
25 
26 /**
27  * Constructor with two values.
28  * First value sets the absolute value, econd sets the relative value.
29  */
30 CLRelAbsVector::CLRelAbsVector(double a, double r): mAbs(a), mRel(r)
31 {
32 }
33 
34 /**
35  * Constructor with two values.
36  * First value sets the absolute value, econd sets the relative value.
37  */
38 CLRelAbsVector::CLRelAbsVector(const std::string& coordString): mAbs(0.0), mRel(0.0)
39 {
40  setCoordinate(coordString);
41 }
42 
43 /**
44  * Constructor to generate object from the corresponding SBML object.
45  */
46 CLRelAbsVector::CLRelAbsVector(const RelAbsVector& source):
47  mAbs(source.getAbsoluteValue()),
48  mRel(source.getRelativeValue())
49 {
50 }
51 
52 /**
53  * Sets the coordiantes.
54  */
55 void CLRelAbsVector::setCoordinate(double abs, double rel)
56 {
57  this->mAbs = abs;
58  this->mRel = rel;
59 }
60 
61 /**
62  * Sets the coordiantes from the given string.
63  */
64 void CLRelAbsVector::setCoordinate(const std::string& coordString)
65 {
66  bool result = true;
67  // first we remove all whitespaces from the string
68  size_t i, iMax = coordString.size();
69  std::string trimmed;
70 
71  for (i = 0; i < iMax; ++i)
72  {
73  if (!(coordString[i] == ' ' || coordString[i] == '\t' || coordString[i] == '\n' || coordString[i] == '\r'))
74  {
75  trimmed += coordString[i];
76  }
77  }
78 
79  if (trimmed.empty())
80  {
81  result = false;
82  }
83  else
84  {
85  char* s = new char[trimmed.size()+1];
86  strncpy(s, trimmed.c_str(), trimmed.size() + 1);
87  char* p = s;
88  char* pp;
89  // p is changed if the value is parsed
90  double value = strtod(p, &pp);
91 
92  // check if strtod stopped at the '%' and if it is the last character in the string
93  if ((*pp) == '%' && pp == (s + trimmed.size() - 1))
94  {
95  // we only have a relative value
96  this->mAbs = 0.0;
97  this->mRel = value;
98  }
99  else
100  {
101  // either pp is at the end, then we only have an absolute value
102  // or pp has stopped on a '+' or '-' which connects the relative to the absolute value
103  // or we have an error
104  if ((*pp) == '\0')
105  {
106  this->mAbs = value;
107  this->mRel = 0.0;
108  }
109  else if ((*pp) == '+' || (*pp) == '-')
110  {
111  this->mAbs = value;
112  p = pp;
113  double value = strtod(p, &pp);
114 
115  // pp must point to the '%' character
116  if ((*pp) != '%' || pp != (s + trimmed.size() - 1))
117  {
118  result = false;
119  }
120  else
121  {
122  this->mRel = value;
123  }
124  }
125  else
126  {
127  result = false;
128  }
129  }
130 
131  delete[] s;
132  }
133 
134  if (result == false)
135  {
136  // set relative and absolute value to NaN
137  this->mAbs = std::numeric_limits<double>::quiet_NaN();
138  this->mRel = std::numeric_limits<double>::quiet_NaN();
139  }
140 }
141 
142 /**
143  * Sets the absolute coordinate value.
144  */
146 {
147  this->mAbs = abs;
148 }
149 
150 /**
151  * Sets the relative coordinate value.
152  */
154 {
155  this->mRel = rel;
156 }
157 
158 /**
159  * Returns the absolute coordinate value.
160  */
162 {
163  return this->mAbs;
164 }
165 
166 /**
167  * Returns the relative coordinate value.
168  */
170 {
171  return this->mRel;
172 }
173 
174 /**
175  * addition operator
176  */
178 {
179  return CLRelAbsVector(this->mAbs + other.mAbs, this->mRel + other.mRel);
180 }
181 
182 /**
183  * division by a double.
184  */
186 {
187  return CLRelAbsVector(this->mAbs / x, this->mRel / x);
188 }
189 
191 {
192  bool result = true;
193 
194  if (this->mAbs == 0.0)
195  {
196  result = (fabs(other.mAbs) < 1e-12);
197  }
198  else
199  {
200  result = (fabs((this->mAbs - other.mAbs) / this->mAbs) < 1e-12);
201  }
202 
203  if (result)
204  {
205  if (this->mRel == 0.0)
206  {
207  result = (fabs(other.mRel) < 1e-12);
208  }
209  else
210  {
211  result = (fabs((this->mRel - other.mRel) / this->mRel) < 1e-12);
212  }
213  }
214 
215  return result;
216 }
217 
219 {
220  return !((*this) == other);
221 }
222 
223 std::string CLRelAbsVector::toString() const
224 {
225  std::ostringstream os;
226 
227  if (this->mAbs != 0.0 || this->mRel == 0.0)
228  {
229  os << this->mAbs;
230 
231  if (this->mRel < 0.0)
232  {
233  os << this->mRel << "%";
234  }
235  else if (this->mRel > 0.0)
236  {
237  os << "+" << this->mRel << "%";
238  }
239  }
240  else
241  {
242  os << this->mRel << "%";
243  }
244 
245  return os.str();
246 }
247 
248 std::ostream& operator<<(std::ostream& os, const CLRelAbsVector& v)
249 {
250  os << v.toString();
251  return os;
252 }
253 
255 {
256  if (&src != this)
257  {
258  this->mAbs = src.mAbs;
259  this->mRel = src.mRel;
260  }
261 
262  return *this;
263 }
264 
265 /**
266  * Converts this object to the corresponding SBML object.
267  */
268 RelAbsVector* CLRelAbsVector::toSBML() const
269 {
270  return new RelAbsVector(mAbs, mRel);
271 }
void setAbsoluteValue(double abs)
CLRelAbsVector(double a=0.0, double r=0.0)
bool operator==(const CLRelAbsVector &other) const
std::ostream & operator<<(std::ostream &os, const CLRelAbsVector &v)
double getRelativeValue() const
void setCoordinate(double abs, double rel=0.0)
CLRelAbsVector operator+(const CLRelAbsVector &other) const
CLRelAbsVector & operator=(const CLRelAbsVector &src)
void setRelativeValue(double rel)
RelAbsVector * toSBML() const
bool operator!=(const CLRelAbsVector &other) const
double getAbsoluteValue() const
std::string toString() const
#define abs(x)
Definition: f2c.h:173
CLRelAbsVector operator/(double x) const