1 /* Copyright (c) 2015  Gerald Knizia
2  *
3  * This file is part of the IboView program (see: http://www.iboview.org)
4  *
5  * IboView is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * IboView is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with bfint (LICENSE). If not, see http://www.gnu.org/licenses/
16  *
17  * Please see IboView documentation in README.txt for:
18  * -- A list of included external software and their licenses. The included
19  *    external software's copyright is not touched by this agreement.
20  * -- Notes on re-distribution and contributions to/further development of
21  *    the IboView software
22  */
23 
24 // main header of IboView... define some stuff used in many places.
25 #ifndef CT_IV_H
26 #define CT_IV_H
27 
28 // #ifdef _WIN32_
29 //   // otherwise lots of strange errors in qopengles2ext
30 //   #define WIN32_LEAN_AND_MEAN
31 //   #include <windows.h>
32 // #endif
33 // (update: that seemed to be a problem with my QT version, which emulated
34 //  GL2ES on top of DirectX, as it seems.)
35 
36 #if defined(_MSC_VER) && defined (_M_X64)
37 	#define NEED_EXTRA_SIZE_T_TYPES
38 	// ^- in windows size_t/ptrdiff_t are neither defined with base types
39 	// int nor long (because long is 32bit there!). In this case we need to
40 	// define some functions taking extra versions of integer arguments
41 	// in order to avoid risking ambiguous conversion errors. Fortunately,
42     // at least 'int' and 'long' are always treated as different types (even
43     // if they are identical)
44 #endif
45 
46 #include "GL/glew.h"
47 // ^- only needed in a few places, but MUST be included before gl.h,
48 //    if gl.h is used anywhere (e.g., by QGLWidget...)
49 
50 #include <cmath>
51 #include <stdint.h>
52 #include <cstddef>
53 #include <stdexcept>
54 #include <stdarg.h>
55 
56 #ifndef M_PI
57    #define M_PI 3.14159265358979323846
58 #endif
59 
60 #include "vector_math.h"
61 #include "CxTypes.h"
62 #include "CxVec3.h"
63 #include "CxPodArray.h"
64 
65 using ct::TArray;
66 using ct::TVector3;
67 using std::size_t;
68 using std::ptrdiff_t;
69 
70 typedef vmath::vec3<double> vec3d;
71 typedef vmath::vec3<float> vec3f;
72 typedef vmath::mat3<double> mat3d;
73 typedef vmath::mat3<float> mat3f;
74 
75 // for homogeneous trafos.
76 typedef vmath::vec4<double> vec4d;
77 typedef vmath::vec4<float> vec4f;
78 typedef vmath::mat4<double> mat4d;
79 typedef vmath::mat4<float> mat4f;
80 
81 // for vertex formats
82 typedef vmath::vec2<float> vec2f;
83 typedef vmath::vec3<unsigned int> vec3ui;
84 
85 // hm... probably should get rid of either vector_math.h or CxVec3.h...
86 typedef ct::TVector3<double> FVec3d;
87 typedef ct::TVector3<float> FVec3f;
88 typedef ct::TVector3<unsigned int> FVec3ui;
89 using ct::sqr;
90 
91 #include <QString>
92 #include <string>
93 std::string q2s(QString const &s);
94 QString s2q(std::string const &s);
95 // std::string RemovePath(std::string const &FileName);
96 // std::string RemoveExt(std::string const &FileName);
97 // std::string ReplaceExt(std::string const &FileName, std::string const &NewExt);
98 QString RemovePath(QString const &FileName);
99 QString RemoveExt(QString const &FileName);
100 QString ReplaceExt(QString const &FileName, QString const &NewExt);
101 
102 class QMainWindow;
103 extern QMainWindow *g_pMainWindow; // for usage as dialog parent.
104 
105 extern bool g_ShowVirtualOrbitals;
106 extern int g_nMaxOmpThreads;
107 
108 enum FNotificationClass {
109    NOTIFY_Information,
110    NOTIFY_StartWork,
111    NOTIFY_FinishWork,
112    NOTIFY_Warning,
113    NOTIFY_Error,
114    NOTIFY_Unknown
115 };
116 // void IvNotify(FNotificationClass NotifyClass, std::string const &Text, std::string const &LongText = "");
117 void IvNotify(FNotificationClass NotifyClass, QString const &Text);
118 void IvWarn(std::string const &Text);
119 void IvWarn(QString const &Text);
120 void IvEmit(QString const &Text);
121 
122 // an actual log class might be more useful...  could even inherit it from QStringList.
123 #define IV_NOTIFY(Class,Text) IvNotify((Class), QString(__FUNCTION__) + ":\t" + QString(Text))
124 #define IV_NOTIFY1(Class,Text,A1) IV_NOTIFY(Class, QString((Text)).arg((A1)))
125 #define IV_NOTIFY2(Class,Text,A1,A2) IV_NOTIFY(Class, QString((Text)).arg((A1)).arg((A2)))
126 #define IV_NOTIFY3(Class,Text,A1,A2,A3) IV_NOTIFY(Class, QString((Text)).arg((A1)).arg((A2)).arg((A3)))
127 
128 
129 namespace fmt {
130    // these ones use boost formats. Maybe I should switch to cppformat instead, globally?
131    // that still would require all the iostream stuff and lead to problems with utf8 vs char*,
132    // but it would solve many other problems.
133    //
134    // note: for a start I could just use the trivial "replace {0} by arg0"-thing I put into
135    //       script.format...
136 
137 //    QString fmti(QString const &fmt, long i);
138 //    QString fmti(QString const &fmt, unsigned long i);
139 //    QString fmti(QString const &fmt, int i);
140 //    QString fmti(QString const &fmt, unsigned int i);
141 //    QString fmtf(QString const &fmt, float f);
142 //    QString fmtf(QString const &fmt, double f);
143 //    QString fmts(QString const &fmt, QString const &s);
144 
145    // these ones use explicit width/prec/base specifications (note: still
146    // options missing... e.g., field alignment, leading zeros, 'always use sign',
147    // etc..).
148    QString fmti(int i, int width=0, int base=0);
149    QString fmti(unsigned int i, int width=0, int base=0);
150    QString fmti(long i, int width=0, int base=0);
151    QString fmti(unsigned long i, int width=0, int base=0);
152 #ifdef NEED_EXTRA_SIZE_T_TYPES
153    QString fmti(size_t i, int width=0, int base=0);
154    QString fmti(ptrdiff_t i, int width=0, int base=0);
155 #endif
156    QString fmtf(float f, int width=0, int prec=-1, char format='f');
157    QString fmtf(double f, int width=0, int prec=-1, char format='f');
158    QString fmts(QString const &s, int width=0);
159    QString fmts(char const *p, int width=0);
160 
161    // format pointer
162    QString fmtp(void *p);
163    // format float as exponential (with explicit width/base)
164    inline QString fmte(double f, int width=0, int prec=-1) { return fmtf(f,width,prec,'e'); }
165    // format int as hexadecimal
166    inline QString fmtx(long i, int width=0, int base=0) { return fmti(i,width,base); };
167    inline QString fmtx(unsigned long i, int width=0, int base=0) { return fmti(i,width,base); };
168 }
169 using namespace fmt;
170 
171 
172 // dummy class which auto-converts stuff to QString via implicit constructors.
173 // It is type safe, but not very flexible (e.g., no field width, no precision, etc).
174 struct FEmitArg {
FEmitArgFEmitArg175    FEmitArg(int n) : m(QString::number(n)) {}
FEmitArgFEmitArg176    FEmitArg(uint n) : m(QString::number(n)) {}
177 //    FEmitArg(std::ptrdiff_t n) : m(QString::number(ulong(n))) {}
178 //    FEmitArg(std::size_t n) : m(QString::number(long(n))) {}
FEmitArgFEmitArg179    FEmitArg(long n) : m(QString::number(n)) {}
FEmitArgFEmitArg180    FEmitArg(unsigned long n) : m(QString::number(n)) {}
181 #ifdef NEED_EXTRA_SIZE_T_TYPES
FEmitArgFEmitArg182    FEmitArg(size_t n) : m(QString::number(n)) {}
FEmitArgFEmitArg183    FEmitArg(ptrdiff_t n) : m(QString::number(n)) {}
184 #endif
FEmitArgFEmitArg185    FEmitArg(double n) : m(QString::number(n)) {}
FEmitArgFEmitArg186    FEmitArg(float n) : m(QString::number(n)) {}
FEmitArgFEmitArg187    FEmitArg(QString s) : m(s) {}
FEmitArgFEmitArg188    FEmitArg(std::string s) : m(QString::fromStdString(s)) {}
FEmitArgFEmitArg189    FEmitArg(char const *p) : m(QString(p)) {}
190    QString
191       m;
192 };
193 QString IvFmt(QString Text, FEmitArg const &a0);
194 QString IvFmt(QString Text, FEmitArg const &a0, FEmitArg const &a1);
195 QString IvFmt(QString Text, FEmitArg const &a0, FEmitArg const &a1, FEmitArg const &a2);
196 QString IvFmt(QString Text, FEmitArg const &a0, FEmitArg const &a1, FEmitArg const &a2, FEmitArg const &a3);
197 QString IvFmt(QString Text, FEmitArg const &a0, FEmitArg const &a1, FEmitArg const &a2, FEmitArg const &a3, FEmitArg const &a4);
198 QString IvFmt(QString Text, FEmitArg const &a0, FEmitArg const &a1, FEmitArg const &a2, FEmitArg const &a3, FEmitArg const &a4, FEmitArg const &a6);
199 QString IvFmt(QString Text, FEmitArg const &a0, FEmitArg const &a1, FEmitArg const &a2, FEmitArg const &a3, FEmitArg const &a4, FEmitArg const &a6, FEmitArg const &a7);
200 void IvEmit(QString Text, FEmitArg const &a0);
201 void IvEmit(QString Text, FEmitArg const &a0, FEmitArg const &a1);
202 void IvEmit(QString Text, FEmitArg const &a0, FEmitArg const &a1, FEmitArg const &a2);
203 void IvEmit(QString Text, FEmitArg const &a0, FEmitArg const &a1, FEmitArg const &a2, FEmitArg const &a3);
204 void IvEmit(QString Text, FEmitArg const &a0, FEmitArg const &a1, FEmitArg const &a2, FEmitArg const &a3, FEmitArg const &a4);
205 void IvEmit(QString Text, FEmitArg const &a0, FEmitArg const &a1, FEmitArg const &a2, FEmitArg const &a3, FEmitArg const &a4, FEmitArg const &a6);
206 void IvEmit(QString Text, FEmitArg const &a0, FEmitArg const &a1, FEmitArg const &a2, FEmitArg const &a3, FEmitArg const &a4, FEmitArg const &a6, FEmitArg const &a7);
207 // void IvEmit(QString const &Text, ...);
208 // void IvEmit(QString const &Text, va_list va);
209 
210 QString LoadTextFromFile(QString FileName);
211 
212 #endif // CT_IV_H
213