Visualization Library 2.0.0-b3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
init_core.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
34 #include <vlCore/VLXRegistry.hpp>
35 #include <vlCore/FileSystem.hpp>
37 #include <vlCore/Log.hpp>
38 #include <vlCore/Say.hpp>
39 #include <vlCore/Time.hpp>
40 #include <vlCore/Quaternion.hpp>
41 #include <vlCore/AABB.hpp>
42 #include <vlCore/Sphere.hpp>
44 #include <cassert>
45 
46 using namespace vl;
47 
48 #if defined(VL_IO_2D_JPG)
49  #include "plugins/ioJPG.hpp"
50 #endif
51 #if defined(VL_IO_2D_PNG)
52  #include "plugins/ioPNG.hpp"
53 #endif
54 #if defined(VL_IO_2D_TIFF)
55  #include "plugins/ioTIFF.hpp"
56 #endif
57 #if defined(VL_IO_2D_TGA)
58  #include "plugins/ioTGA.hpp"
59 #endif
60 #if defined(VL_IO_2D_DAT)
61  #include "plugins/ioDAT.hpp"
62 #endif
63 #if defined(VL_IO_2D_MHD)
64  #include "plugins/ioMHD.hpp"
65 #endif
66 #if defined(VL_IO_2D_DDS)
67  #include "plugins/ioDDS.hpp"
68 #endif
69 #if defined(VL_IO_2D_BMP)
70  #include "plugins/ioBMP.hpp"
71 #endif
72 #if defined(VL_IO_2D_DICOM)
73  #include "plugins/ioDICOM.hpp"
74 #endif
75 
76 //------------------------------------------------------------------------------
77 VL_COMPILE_TIME_CHECK( sizeof(double) == 8 )
78 VL_COMPILE_TIME_CHECK( sizeof(float) == 4 )
79 VL_COMPILE_TIME_CHECK( sizeof(long long) == 8 )
80 VL_COMPILE_TIME_CHECK( sizeof(int) == 4 )
81 VL_COMPILE_TIME_CHECK( sizeof(short) == 2 )
82 VL_COMPILE_TIME_CHECK( sizeof(char) == 1 )
83 VL_COMPILE_TIME_CHECK( sizeof(wchar_t) >= 2 )
84 VL_COMPILE_TIME_CHECK( sizeof(vec2) == sizeof(real)*2 )
85 VL_COMPILE_TIME_CHECK( sizeof(vec3) == sizeof(real)*3 )
86 VL_COMPILE_TIME_CHECK( sizeof(vec4) == sizeof(real)*4 )
87 VL_COMPILE_TIME_CHECK( sizeof(mat2) == sizeof(real)*2*2 )
88 VL_COMPILE_TIME_CHECK( sizeof(mat3) == sizeof(real)*3*3 )
89 VL_COMPILE_TIME_CHECK( sizeof(mat4) == sizeof(real)*4*4 )
90 VL_COMPILE_TIME_CHECK( sizeof(quat) == sizeof(real)*4 )
91 VL_COMPILE_TIME_CHECK( sizeof(AABB) == sizeof(real)*6 )
92 VL_COMPILE_TIME_CHECK( sizeof(Sphere) == sizeof(real)*4 )
93 //------------------------------------------------------------------------------
94 // VL misc
95 //------------------------------------------------------------------------------
96 namespace
97 {
98  std::string gVersionString = String( Say("%n.%n.%s") << VL_Major << VL_Minor << VL_Patch ).toStdString();
99  bool gInitializedCore = false;
100 };
101 //------------------------------------------------------------------------------
102 bool VisualizationLibrary::isCoreInitialized() { return gInitializedCore; }
103 //------------------------------------------------------------------------------
104 const char* VisualizationLibrary::versionString() { return gVersionString.c_str(); }
105 //------------------------------------------------------------------------------
106 // Global GlobalSettings
107 //------------------------------------------------------------------------------
108 namespace
109 {
110  ref<GlobalSettings> gSettings = NULL;
111 }
113 {
114  return gSettings.get();
115 }
116 //------------------------------------------------------------------------------
117 // Default logger
118 //------------------------------------------------------------------------------
119 namespace
120 {
121  ref<Log> gDefaultLogger;
122 }
123 void vl::setDefLogger(Log* logger)
124 {
125  gDefaultLogger = logger;
126 }
128 {
129  return gDefaultLogger.get();
130 }
131 //------------------------------------------------------------------------------
132 // Default LoadWriterManager
133 //------------------------------------------------------------------------------
134 namespace
135 {
136  ref<LoadWriterManager> gDefaultLoadWriterManager = NULL;
137 }
139 {
140  return gDefaultLoadWriterManager.get();
141 }
143 {
144  gDefaultLoadWriterManager = lwm;
145 }
146 //-----------------------------------------------------------------------------
147 // Default FileSystem
148 //-----------------------------------------------------------------------------
149 namespace
150 {
151  ref<FileSystem> gDefaultFileSystem = NULL;;
152 }
154 {
155  return gDefaultFileSystem.get();
156 }
158 {
159  gDefaultFileSystem = fs;
160 }
161 //-----------------------------------------------------------------------------
162 // Default VLXRegistry
163 //-----------------------------------------------------------------------------
164 namespace
165 {
166  ref<VLXRegistry> gDefaultVLXRegistry = NULL;
167 }
169 {
170  return gDefaultVLXRegistry.get();
171 }
173 {
174  gDefaultVLXRegistry = reg;
175 }
176 //-----------------------------------------------------------------------------
177 // Default MersenneTwister
178 //-----------------------------------------------------------------------------
179 namespace
180 {
181  ref<MersenneTwister> gDefaultMersenneTwister = NULL;
182 }
184 {
185  return gDefaultMersenneTwister.get();
186 }
188 {
189  gDefaultMersenneTwister= reg;
190 }
191 //------------------------------------------------------------------------------
193 {
194  VL_CHECK(!gInitializedCore);
195  if (gInitializedCore)
196  {
197  Log::bug("VisualizationLibrary::initCore(): Visualization Library Core is already initialized!\n");
198  return;
199  }
200 
201  // --- Init Core ---
202 
203  // Install globabl settings
204  gSettings = new GlobalSettings;
205 
206  // Install default logger
207  ref<StandardLog> logger = new StandardLog;
208  logger->setLogFile( globalSettings()->defaultLogPath() );
209  setDefLogger( logger.get() );
210 
211  // Install default LoadWriterManager
212  gDefaultLoadWriterManager = new LoadWriterManager;
213 
214  // Install default FileSystem
215  gDefaultFileSystem = new FileSystem;
216  gDefaultFileSystem->directories().push_back( new DiskDirectory( globalSettings()->defaultDataPath() ) );
217 
218  // Install default VLXRegistry
219  gDefaultVLXRegistry = new VLXRegistry;
220 
221  // Install default MersenneTwister (seed done automatically)
222  gDefaultMersenneTwister = new MersenneTwister;
223 
224  // Register 2D modules
225  #if defined(VL_IO_2D_JPG)
227  #endif
228  #if defined(VL_IO_2D_PNG)
230  #endif
231  #if defined(VL_IO_2D_TIFF)
233  #endif
234  #if defined(VL_IO_2D_TGA)
236  #endif
237  #if defined(VL_IO_2D_BMP)
239  #endif
240  #if defined(VL_IO_2D_DDS)
242  #endif
243  #if defined(VL_IO_2D_DAT)
245  #endif
246  #if defined(VL_IO_2D_MHD)
248  #endif
249  #if defined(VL_IO_2D_DICOM)
251  #endif
252 
253  // Log VL and system information.
254  if (globalSettings()->verbosityLevel() && log_info)
256 
257  // Initialized = on
258  gInitializedCore = true;
259 }
260 //------------------------------------------------------------------------------
262 {
263  // Initialized = off
264  gInitializedCore = false;
265 
266  // --- Dispose Core ---
267 
268  // Dispose default MersenneTwister
269  gDefaultMersenneTwister = NULL;
270 
271  // Dispose default VLXRegistry
272  gDefaultVLXRegistry = NULL;
273 
274  // Dispose default LoadWriterManager
275  gDefaultLoadWriterManager->loadCallbacks().clear();
276  gDefaultLoadWriterManager->writeCallbacks().clear();
277  gDefaultLoadWriterManager->loadWriters().clear();
278  gDefaultLoadWriterManager = NULL;
279 
280  // Dispose default FileSystem
281  gDefaultFileSystem->directories().clear();
282  gDefaultFileSystem = NULL;
283 
284  // Dispose default logger
285  if (globalSettings()->verbosityLevel())
286  {
287  Log::print("Visualization Library shutdown.\n");
288  }
289  // we keep the logger alive as much as we can.
290  // setDefLogger( NULL );
291 
292  // keep global settings (used by logger)
293  // gSettings = NULL;
294 }
295 //------------------------------------------------------------------------------
297 {
298  vl::Time::sleep(3000);
299  exit(1);
300 }
301 //------------------------------------------------------------------------------
302 #if defined(VL_PLATFORM_WINDOWS)
303  // console includes
304  #include <io.h>
305  #include <fcntl.h>
306 #endif
307 
309 {
310  #if defined(VL_PLATFORM_WINDOWS)
311  AllocConsole();
312  // stdout
313  HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
314  int hCrt = _open_osfhandle((intptr_t)handle_out, _O_TEXT);
315  FILE* hf_out = _fdopen(hCrt, "w");
316  setvbuf(hf_out, NULL, _IONBF, 1);
317  *stdout = *hf_out;
318  // stderr
319  handle_out = GetStdHandle(STD_ERROR_HANDLE);
320  hCrt = _open_osfhandle((intptr_t)handle_out, _O_TEXT);
321  hf_out = _fdopen(hCrt, "w");
322  setvbuf(hf_out, NULL, _IONBF, 1);
323  *stderr = *hf_out;
324  #endif
325 }
326 //------------------------------------------------------------------------------
Utility class to generate logs.
Definition: Log.hpp:45
std::vector< ref< VirtualDirectory > > & directories()
Returns the list of VirtualDirectory objects added to a FileSystem.
Definition: FileSystem.hpp:84
VLCORE_EXPORT FileSystem * defFileSystem()
Returns the default FileSystem used by VisualizationLibrary.
Definition: init_core.cpp:153
static VLCORE_EXPORT const char * versionString()
Returns the Visualization Library&#39;s version string.
Definition: init_core.cpp:104
VLCORE_EXPORT VLXRegistry * defVLXRegistry()
Definition: init_core.cpp:168
const T * get() const
Definition: Object.hpp:128
static VLCORE_EXPORT void initCore(bool log_info=true)
Initializes only VLCore library.
Definition: init_core.cpp:192
VLCORE_EXPORT void abort_vl()
Definition: init_core.cpp:296
A simple String formatting class.
Definition: Say.hpp:124
VLCORE_EXPORT void showWin32Console()
Shows a console window that displays the standard output. This function is meant to be used only unde...
Definition: init_core.cpp:308
static VLCORE_EXPORT bool isCoreInitialized()
Returns true if VLCore library is initialized and shutdown has not been called.
Definition: init_core.cpp:102
The LoadWriterTGA class is a ResourceLoadWriter capable of reading TGA files.
Definition: ioTGA.hpp:57
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
void setLogFile(const String &file)
Definition: Log.cpp:266
Global application settings controlling how Visualization Library behaves.
The LoadWriterManager class loads and writes resources using the registered ResourceLoadWriter object...
The Matrix2 class is a template class that implements a generic 2x2 matrix, see also vl::dmat2...
Definition: Matrix2.hpp:49
VLCORE_EXPORT void setDefVLXRegistry(VLXRegistry *reg)
Definition: init_core.cpp:172
The LoadWriterDDS class is a ResourceLoadWriter capable of reading DDS files.
Definition: ioDDS.hpp:55
A VirtualDirectory that operates on reguar disk directories.
The LoadWriterJPG class is a ResourceLoadWriter capable of reading JPG files.
Definition: ioJPG.hpp:58
Implements a Quaternion usually used to represent rotations and orientations.
Definition: Quaternion.hpp:44
Visualization Library main namespace.
The LoadWriterPNG class is a ResourceLoadWriter capable of reading PNG files.
Definition: ioPNG.hpp:57
The Matrix3 class is a template class that implements a generic 3x3 matrix, see also vl::dmat3...
Definition: Matrix3.hpp:48
The LoadWriterDICOM class is a ResourceLoadWriter capable of reading DICOM files (.dcm, .dicom, .dic, .ima, .ph, .mag) using GDCM 2.0 library.
Definition: ioDICOM.hpp:60
static VLCORE_EXPORT void shutdownCore()
Releases all the resources acquired by Visualization Library Core Call shutdownCore() instead of shut...
Definition: init_core.cpp:261
static void bug(const String &message)
Use this function to provide information about programming errors: wrong parameter initialization...
Definition: Log.cpp:174
The LoadWriterDAT class is a ResourceLoadWriter capable of reading DAT files.
Definition: ioDAT.hpp:55
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
Manages multiple VirtualDirectory objects.
Definition: FileSystem.hpp:58
static void print(const String &message)
Application message for the user.
Definition: Log.cpp:135
The LoadWriterTIFF class is a ResourceLoadWriter capable of reading TIFF files.
Definition: ioTIFF.hpp:57
VL_COMPILE_TIME_CHECK(sizeof(i8) *8==8)
VLCORE_EXPORT void setDefFileSystem(FileSystem *fs)
Sets the default FileSystem used by VisualizationLibrary.
Definition: init_core.cpp:157
#define NULL
Definition: OpenGLDefs.hpp:81
The Sphere class defines a sphere using a center and a radius using vl::real precision.
Definition: Sphere.hpp:43
The StandardLog class outputs the log messages on the stdout device and optionally also on a specifie...
Definition: Log.hpp:231
The Vector2 class is a template class that implements a generic 2 components vector, see also vl::fvec2, vl::dvec2, vl::uvec2, vl::ivec2, vl::svec2, vl::usvec2, vl::bvec2, vl::ubvec2.
Definition: Vector2.hpp:97
VLCORE_EXPORT void setDefLoadWriterManager(LoadWriterManager *lwm)
Sets the default LoadWriterManager used by Visualization Library.
Definition: init_core.cpp:142
VLCORE_EXPORT void setDefLogger(Log *logger)
Installs the default logger used by Visualization Library. Setting this to NULL will disable logging...
Definition: init_core.cpp:123
VLCORE_EXPORT void setDefMersenneTwister(MersenneTwister *)
Definition: init_core.cpp:187
Registry of vl::VLXClassWrapper objects, used by vl::VLXSerializer, see also vl::defVLXRegistry().
Definition: VLXRegistry.hpp:42
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
static void logSystemInfo()
Logs VL and system information.
Definition: Log.cpp:184
std::string toStdString() const
Returns a UTF8 encoded std::string.
Definition: String.cpp:1156
VLCORE_EXPORT Log * defLogger()
Returns the currently installed default logger.
Definition: init_core.cpp:127
static void sleep(unsigned int milliseconds)
Definition: Time.cpp:144
The LoadWriterBMP class is a ResourceLoadWriter capable of reading BMP files.
Definition: ioBMP.hpp:55
The LoadWriterMHD class is a ResourceLoadWriter capable of reading MHD files.
Definition: ioMHD.hpp:55
VLCORE_EXPORT MersenneTwister * defMersenneTwister()
Definition: init_core.cpp:183
#define VL_CHECK(expr)
Definition: checks.hpp:73
VLCORE_EXPORT LoadWriterManager * defLoadWriterManager()
Returs the default LoadWriterManager used by Visualization Library.
Definition: init_core.cpp:138
VLCORE_EXPORT GlobalSettings * globalSettings()
Returns VisulizationLibrary&#39;s global settings.
Definition: init_core.cpp:112
void registerLoadWriter(ResourceLoadWriter *rlw)
Utility function, equivalent to defLoadWriterManager()->registerLoadWriter(rlw).