Read@CVL
Utils.h
Go to the documentation of this file.
1 /*******************************************************************************************************
2  ReadFramework is the basis for modules developed at CVL/TU Wien for the EU project READ.
3 
4  Copyright (C) 2016 Markus Diem <diem@caa.tuwien.ac.at>
5  Copyright (C) 2016 Stefan Fiel <fiel@caa.tuwien.ac.at>
6  Copyright (C) 2016 Florian Kleber <kleber@caa.tuwien.ac.at>
7 
8  This file is part of ReadFramework.
9 
10  ReadFramework is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  ReadFramework is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23  The READ project has received funding from the European Union’s Horizon 2020
24  research and innovation programme under grant agreement No 674943
25 
26  related links:
27  [1] http://www.caa.tuwien.ac.at/cvl/
28  [2] https://transkribus.eu/Transkribus/
29  [3] https://github.com/TUWien/
30  [4] http://nomacs.org
31  *******************************************************************************************************/
32 
33 #pragma once
34 
35 #pragma warning(push, 0) // no warnings from includes
36 #include <QSharedPointer>
37 #include <QSettings>
38 #include <QTime>
39 
40 #include <opencv2/core.hpp>
41 #pragma warning(pop)
42 
43 #pragma warning (disable: 4251) // inlined Qt functions in dll interface
44 #pragma warning (disable: 4714) // force inline
45 
46 #ifndef DllCoreExport
47 #ifdef DLL_CORE_EXPORT
48 #define DllCoreExport Q_DECL_EXPORT
49 #else
50 #define DllCoreExport Q_DECL_IMPORT
51 #endif
52 #endif
53 
54 // some basic defines - yes, we try to not create too many macros...
55 #define DK_DEG2RAD 0.017453292519943
56 #define DK_RAD2DEG 57.295779513082323
57 
58 // converts a version (e.g. 3.1.0) to a comparable int
59 #define RDF_VERSION(major, minor, revision) (major << 16 | minor << 8 | revision)
60 #define RDF_OPENCV_VERSION RDF_VERSION(CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_VERSION_REVISION)
61 
62 // Qt defines
63 class QSettings;
64 
65 namespace rdf {
66 
67 #define WHO_IS_CUTE "Anna"
68 
69 // read defines
71 
72 public:
73  static Utils& instance();
74 
75  void initFramework() const;
76  void registerVersion() const;
77  static int versionToInt(char major, char minor, char revision);
78  static double rand();
79 
80  static bool hasGui();
81 
82  static bool loadToBuffer(const QString& filePath, QByteArray& ba);
83  static QString appDataPath();
84  static QString createFilePath(const QString& filePath, const QString& attribute, const QString& newSuffix = QString());
85  static QString timeStampFileName(const QString& attribute = "", const QString& suffix = ".txt");
86  static QString baseName(const QString& filePath);
87 
88  static QJsonObject readJson(const QString& filePath);
89  static int64 writeJson(const QString& filePath, const QJsonObject& jo);
90  static void initDefaultFramework();
91 
92  // little number thingies
93  template<typename num>
94  static num clamp(num val, num min, num max) {
95 
96  if (val < min)
97  val = min;
98  if (val > max)
99  val = max;
100 
101  return val;
102  }
103 
104 private:
105  Utils();
106  Utils(const Utils&);
107 };
108 
110 
111 public:
112  static QPolygon stringToPoly(const QString& pointList);
113  static QString polyToString(const QPolygon& poly);
114 
115  static QPointF cvPointToQt(const cv::Point& pt);
116  static cv::Point2d qPointToCv(const QPointF& pt);
117 
118  static QRectF cvRectToQt(const cv::Rect& r);
119  static cv::Rect2d qRectToCv(const QRectF& r);
120 
121 };
122 
129 
130 public:
131 
135  Timer();
136 
137  friend DllCoreExport QDataStream& operator<<(QDataStream& s, const Timer& t);
138  friend DllCoreExport QDebug operator<< (QDebug d, const Timer &t);
139 
140  QString getTotal() const;
141  virtual QDataStream& put(QDataStream& s) const;
142  QString stringifyTime(int ct) const;
143  int elapsed() const;
144  void start();
145 
146 protected:
147  QTime mTimer;
148 };
149 
165 template <typename EnumType, typename Underlying = int>
166 class Flags {
167  typedef Underlying Flags::* RestrictedBool;
168 
169 public:
170  Flags() : mFlags(Underlying()) {}
171 
172  Flags(EnumType f) :
173  mFlags(1 << f) {}
174 
175  Flags(const Flags& o):
176  mFlags(o.mFlags) {}
177 
178  Flags& operator |=(const Flags& f) {
179  mFlags |= f.mFlags;
180  return *this;
181  }
182 
183  Flags& operator &=(const Flags& f) {
184  mFlags &= f.mFlags;
185  return *this;
186  }
187 
188  friend Flags operator |(const Flags& f1, const Flags& f2) {
189  return Flags(f1) |= f2;
190  }
191 
192  friend Flags operator &(const Flags& f1, const Flags& f2) {
193  return Flags(f1) &= f2;
194  }
195 
196  Flags operator ~() const {
197  Flags result(*this);
198  result.mFlags = ~result.mFlags;
199  return result;
200  }
201 
202  operator RestrictedBool() const {
203  return mFlags ? &Flags::mFlags : 0;
204  }
205 
206  Underlying value() const {
207  return mFlags;
208  }
209 
210 protected:
211  Underlying mFlags;
212 };
213 
214 }
Definition: Utils.h:128
Definition: Utils.h:70
Flags turns enums into typesave flags It is strongly related (copied) from Useage: enum mDrawFlags { ...
Definition: Utils.h:166
QDataStream & operator<<(QDataStream &s, const BaseElement &e)
Definition: BaseImageElement.cpp:81
Flags()
Definition: Utils.h:170
Flags(EnumType f)
Definition: Utils.h:172
QTime mTimer
Definition: Utils.h:147
static num clamp(num val, num min, num max)
Definition: Utils.h:94
Definition: Algorithms.cpp:45
#define DllCoreExport
Definition: Utils.h:50
Definition: Utils.h:109
Underlying value() const
Definition: Utils.h:206
Flags(const Flags &o)
Definition: Utils.h:175
Underlying mFlags
Definition: Utils.h:211