MorphoGraphX  2.0-1-227
Misc.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2016 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef MISC_H
12 #define MISC_H
13 
18 #include <Config.hpp>
19 #include <GL.hpp>
20 
21 #include <Color.hpp>
22 #include <EdgeData.hpp>
23 #include <MGXViewer/qglviewer.h>
24 #include <Vector.hpp>
25 #include <VertexData.hpp>
26 #include <VVGraph.hpp>
27 
28 #include <QDir>
29 #include <QList>
30 #include <QtCore>
31 
32 namespace mgx
33 {
34  static const QString UM(QString::fromWCharArray(L"\xb5m")); // um
35  static const QString UM2(QString::fromWCharArray(L"\xb5m\xb2")); // um^2
36  static const QString UM3(QString::fromWCharArray(L"\xb5m\xb3")); // um^3
37  static const QString UM_1(QString::fromWCharArray(L"\xb5m\x207B\xb9")); // um^-1
38  static const QString UM_2(QString::fromWCharArray(L"\xb5m\x207B\xb2")); // um^-2
39 
40  #ifdef _MSC_VER
41  template class mgx_EXPORT Vertex<VertexData>;
42  template class mgx_EXPORT Edge<EdgeData>;
43  template class mgx_EXPORT VVGraph<VertexData, EdgeData>;
44  #endif
45 
46  // Type of the VV graph
48 
49  // Type of a vertex
51 
52  // Type of an edge
54 
55  typedef Vector<2, int> Point2i;
60  typedef Color<float> Colorf;
61  typedef Vector<3, float> Point3f;
62 
64  mgx_EXPORT QDir resourceDir();
66  mgx_EXPORT QList<QDir> userProcessDirs();
68  mgx_EXPORT QList<QDir> processDirs();
70  mgx_EXPORT QDir libDir();
72  mgx_EXPORT QDir includeDir();
74  mgx_EXPORT QDir docDir();
75 
78  {
79  u++;
80  return (Point3GLub(u / (256 * 256), u / 256 % 256, u % 256));
81  }
82 
84  inline uint vMapColor(Point3GLub& p) {
85  return ((int(p.x()) * 256 * 256 + int(p.y()) * 256 + int(p.z())) - 1);
86  }
87 
89  inline QString shield(QString s)
90  {
91  s.replace("\\", "\\\\");
92  s.replace(" ", "\\s");
93  s.replace("\n", "\\n");
94  s.replace("\t", "\\t");
95  s.replace("\"", "\\\"");
96  s.replace("\'", "\\\'");
97  return s;
98  }
99 
101  inline QString unshield(QString s)
102  {
103  s.replace("\\\'", "\'");
104  s.replace("\\\"", "\"");
105  s.replace("\\s", " ");
106  s.replace("\\n", "\n");
107  s.replace("\\t", "\t");
108  s.replace("\\\\", "\\");
109  return s;
110  }
111 
113  inline QString shield_python(QString s)
114  {
115  s.replace("\\", "\\\\");
116  s.replace("\n", "\\n");
117  s.replace("\t", "\\t");
118  s.replace("\"", "\\\"");
119  s.replace("\'", "\\\'");
120  return s;
121  }
122 
124  inline QString unshield_python(QString s)
125  {
126  s.replace("\\\'", "\'");
127  s.replace("\\\"", "\"");
128  s.replace("\\n", "\n");
129  s.replace("\\t", "\t");
130  s.replace("\\\\", "\\");
131  return s;
132  }
133 
134  // Maybe make a template?
136  inline Point3d stringToPoint3d(const QString &s)
137  {
138  QStringList sl = s.split(" ");
139  size_t sz = sl.size();
140  if(sz > 3)
141  sz = 3;
142  Point3d result;
143  for(size_t i = 0; i < sz; ++i)
144  result[i] = sl[i].toDouble();
145  return result;
146  }
147  inline Point2f stringToPoint2f(const QString &s)
148  {
149  QStringList sl = s.split(" ");
150  size_t sz = sl.size();
151  if(sz > 2)
152  sz = 2;
153  Point2f result;
154  for(size_t i = 0; i < sz; ++i)
155  result[i] = sl[i].toFloat();
156  return result;
157  }
158  inline Point2i stringToPoint2i(const QString &s)
159  {
160  QStringList sl = s.split(" ");
161  size_t sz = sl.size();
162  if(sz > 2)
163  sz = 2;
164  Point2i result;
165  for(size_t i = 0; i < sz; ++i)
166  result[i] = sl[i].toInt();
167  return result;
168  }
169  inline Point2u stringToPoint2u(const QString &s)
170  {
171  QStringList sl = s.split(" ");
172  size_t sz = sl.size();
173  if(sz > 2)
174  sz = 2;
175  Point2u result;
176  for(size_t i = 0; i < sz; ++i)
177  result[i] = sl[i].toUInt();
178  return result;
179  }
180  inline Point2d stringToPoint2d(const QString &s)
181  {
182  QStringList sl = s.split(" ");
183  size_t sz = sl.size();
184  if(sz > 2)
185  sz = 2;
186  Point2d result;
187  for(size_t i = 0; i < sz; ++i)
188  result[i] = sl[i].toDouble();
189  return result;
190  }
191  inline Point3u stringToPoint3u(const QString &s)
192  {
193  QStringList sl = s.split(" ");
194  size_t sz = sl.size();
195  if(sz > 3)
196  sz = 3;
197  Point3u result;
198  for(size_t i = 0; i < sz; ++i)
199  result[i] = sl[i].toUInt();
200  return result;
201  }
202 
203  inline bool isGuiThread()
204  {
205  if(QThread::currentThread() == QCoreApplication::instance()->thread())
206  return true;
207  return false;
208  }
209 
210  class Guard
211  {
212  public:
213  Guard(bool &lock) : _lock(lock)
214  {
215  if(not _lock) {
216  _owner = true;
217  _lock = true;
218  } else {
219  _owner = false;
220  }
221  }
223  {
224  if(_owner)
225  _lock = false;
226  }
227  bool owner()
228  {
229  return _owner;
230  }
231 
232  private:
233  bool _owner;
234  bool &_lock;
235  };
236 }
237 #endif
mgx::vMapColor
Point3GLub vMapColor(uint u)
Map unique colors to indices.
Definition: Misc.hpp:77
EdgeData.hpp
mgx::userProcessDirs
mgx_EXPORT QList< QDir > userProcessDirs()
Returns user processes directory.
mgx::uint
unsigned int uint
Definition: Geometry.hpp:41
Vector.hpp
mgx::vertex
vvGraph::vertex_t vertex
Type of a vertex.
Definition: Misc.hpp:50
GL.hpp
mgx::resourceDir
mgx_EXPORT QDir resourceDir()
Returns resource directory.
mgx::Colorf
Color< float > Colorf
Definition: Color.hpp:317
mgx::libDir
mgx_EXPORT QDir libDir()
Returns the libraries directory.
mgx::Point2i
Vector< 2, int > Point2i
Definition: CuttingSurface.hpp:22
mgx::Edge
Definition: Edge.hpp:42
mgx::Guard::owner
bool owner()
Definition: Misc.hpp:227
mgx::Vector::z
CU_HOST_DEVICE void z(const T &v)
Short access to the third element.
Definition: Vector.hpp:739
mgx::Guard::~Guard
~Guard()
Definition: Misc.hpp:222
VertexData.hpp
VVGraph.hpp
mgx::unshield_python
QString unshield_python(QString s)
Retrieve a string that is retrieved from the python representation.
Definition: Misc.hpp:124
mgx::stringToPoint2u
Point2u stringToPoint2u(const QString &s)
Definition: Misc.hpp:169
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::Point4GLui
Vector< 4, GLuint > Point4GLui
Definition: Misc.hpp:57
mgx::shield
QString shield(QString s)
Create representation of a string that can be written in a single line, without spaces.
Definition: Misc.hpp:89
mgx::Point3GLub
Vector< 3, GLubyte > Point3GLub
Definition: ImageData.hpp:43
mgx::unshield
QString unshield(QString s)
Retrieve a string that has been shielded with shield.
Definition: Misc.hpp:101
mgx::processDirs
mgx_EXPORT QList< QDir > processDirs()
Returns processes directory.
mgx::shield_python
QString shield_python(QString s)
Shield a string to send it to the python interpreter.
Definition: Misc.hpp:113
mgx::Point3f
Vector< 3, float > Point3f
Definition: CuttingSurface.hpp:25
mgx::vvGraph
VVGraph< VertexData, EdgeData > vvGraph
Simpler names for the various containers and iterators related to vertices and VV.
Definition: Misc.hpp:47
mgx::Vector::y
CU_HOST_DEVICE void y(const T &v)
Short access to the second element.
Definition: Vector.hpp:730
mgx::stringToPoint2i
Point2i stringToPoint2i(const QString &s)
Definition: Misc.hpp:158
mgx::Guard
Definition: Misc.hpp:210
mgx::Point4GLub
Vector< 4, GLubyte > Point4GLub
Definition: Misc.hpp:59
mgx::Vector::size
static CU_HOST_DEVICE size_t size()
Returns the size of the vector (i.e.
Definition: Vector.hpp:297
mgx::docDir
mgx_EXPORT QDir docDir()
Returns the documentation directory.
mgx::Vector< 2, int >
mgx::stringToPoint3u
Point3u stringToPoint3u(const QString &s)
Definition: Misc.hpp:191
mgx::isGuiThread
bool isGuiThread()
Definition: Misc.hpp:203
Color.hpp
mgx::Guard::Guard
Guard(bool &lock)
Definition: Misc.hpp:213
mgx::stringToPoint3d
Point3d stringToPoint3d(const QString &s)
Convert a string to Point3d.
Definition: Misc.hpp:136
mgx::Color< float >
mgx::edge
vvGraph::edge_t edge
Type of an edge.
Definition: Misc.hpp:53
mgx::stringToPoint2f
Point2f stringToPoint2f(const QString &s)
Definition: Misc.hpp:147
mgx::Vector::x
CU_HOST_DEVICE void x(const T &v)
Short access to the first element.
Definition: Vector.hpp:721
mgx::VVGraph< VertexData, EdgeData >
mgx::stringToPoint2d
Point2d stringToPoint2d(const QString &s)
Definition: Misc.hpp:180
mgx::includeDir
mgx_EXPORT QDir includeDir()
Returns the includes directory.
mgx::Point3GLui
Vector< 3, GLuint > Point3GLui
Definition: Misc.hpp:56
mgx::Vertex
Definition: Vertex.hpp:58