COPASI API  4.16.103
utility_classes.cpp
Go to the documentation of this file.
1 // Copyright (C) 2015 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 #include "utility_classes.h"
7 
8 #include <cmath>
9 #include <assert.h>
10 #include <string.h>
11 
12 // includes from the layout extension
14 #include "copasi/layout/CLGlyphs.h"
16 
17 CLLineStippleTexture::CLLineStippleTexture(const std::vector<unsigned int>& dasharray):
18  mPatternLength(0),
19  mTextureLength(0),
20  mTextureName(0)
21 {
22  if (!dasharray.empty())
23  {
24  std::vector<unsigned int>::const_iterator it = dasharray.begin(), endit = dasharray.end();
25 
26  while (it != endit)
27  {
28  this->mPatternLength += *it;
29  ++it;
30  }
31 
32  // we only need to create a texture if the pattern length is not 0
33  if (this->mPatternLength != 0)
34  {
35  // the texture size should be a multiple of 2
36  unsigned int exponent = (unsigned int)ceil(log((double)this->mPatternLength) / log(2.0));
37 
38  if (exponent <= 31)
39  {
40  this->mTextureLength = 1 << exponent;
41  // make sure the texture name is really free
42  this->mTextureName = 0;
43  GLint w = -1;
44  glTexImage1D(GL_PROXY_TEXTURE_1D, 0, GL_ALPHA, this->mTextureLength, 0, GL_ALPHA, GL_UNSIGNED_BYTE, NULL);
45  glGetTexLevelParameteriv(GL_PROXY_TEXTURE_1D, 0, GL_TEXTURE_WIDTH, &w);
46 
47  if (w != 0)
48  {
49  GLubyte* pTextureData = new GLubyte[this->mTextureLength];
50  // now we create the pattern
51  it = dasharray.begin();
52  unsigned int i;
53  bool gap = false;
54  unsigned int index = 0;
55 
56  while (it != endit)
57  {
58  for (i = 0; i < *it; ++i)
59  {
60  // if it is a gap, we write 0, otherwise we write 255
61  pTextureData[index] = gap ? 0 : 255;
62  ++index;
63  }
64 
65  gap = !gap;
66  ++it;
67  }
68 
69  // fill the rest of the texture with 255
70  while (index < this->mTextureLength)
71  {
72  pTextureData[index++] = 70;
73  }
74 
75  // now we load the texture data into OpenGL
76  glGenTextures(1, &this->mTextureName);
77  // make sure a new texture name has been created
78  assert(this->mTextureName != 0);
79  glBindTexture(GL_TEXTURE_1D, this->mTextureName);
80  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
81  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
82  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
83  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
84  glTexImage1D(GL_TEXTURE_1D, 0, GL_ALPHA, this->mTextureLength, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pTextureData);
85  // delete the memory
86  delete[] pTextureData;
87  }
88  else
89  {
90  std::cerr << "Line stipple texture of this size is not supported by the current OpenGL implementation." << std::endl;
91  }
92  }
93  else
94  {
95  std::cerr << "texture to large." << std::endl;
96  }
97  }
98  }
99 }
100 
102 {
103  if (this->mTextureName != 0)
104  {
105  glDeleteTextures(1, &this->mTextureName);
106  }
107 }
108 
110 {
111  const CLReactionGlyph* pRG1 = dynamic_cast<const CLReactionGlyph*>(pObject1);
112  const CLReactionGlyph* pRG2 = dynamic_cast<const CLReactionGlyph*>(pObject2);
113  const CLMetabReferenceGlyph* pSRG1 = dynamic_cast<const CLMetabReferenceGlyph*>(pObject1);
114  const CLMetabReferenceGlyph* pSRG2 = dynamic_cast<const CLMetabReferenceGlyph*>(pObject2);
115  bool result = false;
116 
117  if (!(((pRG1 && pRG1->getCurve().getNumCurveSegments() != 0) || (pSRG1 && pSRG1->getCurve().getNumCurveSegments() != 0)) && ((pRG2 && pRG2->getCurve().getNumCurveSegments() != 0) || (pSRG2 && pSRG2->getCurve().getNumCurveSegments() != 0))))
118  {
119  result = ((pObject1->getBoundingBox().getDimensions().getWidth() * pObject1->getBoundingBox().getDimensions().getHeight()) < (pObject2->getBoundingBox().getDimensions().getWidth() * pObject2->getBoundingBox().getDimensions().getHeight()));
120  }
121 
122  return result;
123 }
124 
125 /**
126  * Constructor that fills all values with default attributes.
127  */
129  mStroke("none"),
130  mStrokeWidth(0.0),
131  mStrokeDasharray(),
132  mpTransform(NULL),
133  mFill("none"),
134  mFillRule(CLGraphicalPrimitive2D::NONZERO),
135  mFontFamily("sans-serif"),
136  mFontSize(CLRelAbsVector(0.0, 0.0)),
137  mFontWeight(CLText::WEIGHT_NORMAL),
138  mFontStyle(CLText::STYLE_NORMAL),
139  mTextAnchor(CLText::ANCHOR_START),
140  mVTextAnchor(CLText::ANCHOR_TOP),
141  mStartHead("none"),
142  mEndHead("none"),
143  mX(0),
144  mY(0),
145  mZ(0),
146  mWidth(0),
147  mHeight(0)
148 {
149  this->mpTransform = new double[12];
150  memcpy(this->mpTransform, CLTransformation::getIdentityMatrix(), 12 * sizeof(double));
151 }
152 /**
153  * Destructor that deletes the memory used for the transformation
154  * array.
155  */
157 {
158  if (this->mpTransform != NULL)
159  {
160  delete[] this->mpTransform;
161  }
162 }
163 
164 /**
165  * Copy constructor.
166  */
168  mStroke(src.mStroke),
169  mStrokeWidth(src.mStrokeWidth),
170  mStrokeDasharray(src.mStrokeDasharray),
171  mpTransform(NULL),
172  mFill(src.mFill),
173  mFillRule(src.mFillRule),
174  mFontFamily(src.mFontFamily),
175  mFontSize(src.mFontSize),
176  mFontWeight(src.mFontWeight),
177  mFontStyle(src.mFontStyle),
178  mTextAnchor(src.mTextAnchor),
179  mVTextAnchor(src.mVTextAnchor),
180  mStartHead(src.mStartHead),
181  mEndHead(src.mEndHead),
182  mX(src.mX),
183  mY(src.mY),
184  mZ(src.mZ),
185  mWidth(src.mWidth),
186  mHeight(src.mHeight)
187 {
188  this->mpTransform = new double[12];
189  memcpy(this->mpTransform, src.mpTransform, 12 * sizeof(double));
190 }
191 
192 /**
193  * Assignment operator.
194  */
196 {
197  if (&src != this)
198  {
199  this->mStroke = src.mStroke;
200  this->mStrokeWidth = src.mStrokeWidth;
202 
203  if (!this->mpTransform)
204  {
205  this->mpTransform = new double[12];
206  }
207 
208  // contains 12 double values
209  memcpy(this->mpTransform, src.mpTransform, 12 * sizeof(double));
210  this->mFill = src.mFill;
211  this->mFillRule = src.mFillRule;
212  this->mFontFamily = src.mFontFamily;
213  this->mFontSize = src.mFontSize;
214  this->mFontWeight = src.mFontWeight;
215  this->mFontStyle = src.mFontStyle;
216  this->mTextAnchor = src.mTextAnchor;
217  this->mVTextAnchor = src.mVTextAnchor;
218  this->mStartHead = src.mStartHead;
219  this->mEndHead = src.mEndHead;
220  this->mX = src.mX;
221  this->mY = src.mY;
222  this->mZ = src.mZ;
223  this->mWidth = src.mWidth;
224  this->mHeight = src.mHeight;
225  }
226 
227  return *this;
228 }
229 
231  mFamily(""),
232  mSize(0.0),
233  mWeight(CLText::WEIGHT_UNSET),
234  mStyle(CLText::STYLE_UNSET)
235 {
236 }
237 
238 bool CLFontSpec::operator<(const CLFontSpec& right) const
239 {
240  bool result = false;
241 
242  if (this->mFamily < right.mFamily)
243  {
244  result = true;
245  }
246  else if (this->mFamily == right.mFamily)
247  {
248  if (this->mSize < right.mSize)
249  {
250  result = true;
251  }
252  else if (this->mSize == right.mSize)
253  {
254  if (this->mWeight < right.mWeight)
255  {
256  result = true;
257  }
258  else if (this->mWeight == right.mWeight)
259  {
260  if (this->mStyle < right.mStyle)
261  {
262  result = true;
263  }
264  }
265  }
266  }
267 
268  return result;
269 }
const C_FLOAT64 & getWidth() const
Definition: CLBase.h:211
Definition: CLText.h:27
const CLBoundingBox & getBoundingBox() const
CLText::TEXT_ANCHOR mTextAnchor
std::string mEndHead
unsigned int mPatternLength
CLText::FONT_STYLE mFontStyle
bool operator()(const CLGraphicalObject *pObject1, const CLGraphicalObject *pObject2) const
std::string mFontFamily
static const double * getIdentityMatrix()
const CLDimensions & getDimensions() const
Definition: CLBase.h:266
CLRelAbsVector mFontSize
std::vector< unsigned int > mStrokeDasharray
CLGraphicalPrimitive2D::FILL_RULE mFillRule
CLLineStippleTexture(const std::vector< unsigned int > &dasharray)
const C_FLOAT64 & getHeight() const
Definition: CLBase.h:212
CLGroupAttributes & operator=(const CLGroupAttributes &src)
unsigned int mTextureLength
CLText::FONT_WEIGHT mWeight
CLText::TEXT_ANCHOR mVTextAnchor
CLText::FONT_WEIGHT mFontWeight
bool operator<(const CLFontSpec &right) const
std::string mStartHead
std::string mFamily
const CLCurve & getCurve() const
size_t getNumCurveSegments() const
Definition: CLCurve.h:168
CLText::FONT_STYLE mStyle
std::string mStroke