Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scenehandler.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_RENDER_SCENEHANDLER_H_)
21 #define __MITSUBA_RENDER_SCENEHANDLER_H_
22 
23 #include <xercesc/sax/HandlerBase.hpp>
24 #include <xercesc/sax/AttributeList.hpp>
25 #include <mitsuba/core/plugin.h>
27 #include <mitsuba/core/version.h>
28 #include <boost/unordered_map.hpp>
29 #include <stack>
30 #include <map>
31 
32 XERCES_CPP_NAMESPACE_BEGIN
33 class SAXParser;
34 class XMLTranscoder;
35 XERCES_CPP_NAMESPACE_END
36 
38 namespace xercesc = XERCES_CPP_NAMESPACE;
39 
40 #ifdef _MSC_VER
41 // Disable warning 4275: non dll-interface used as base for dll-interface class
42 // Can be safely ignored when deriving from a type in the Standard C++ Library
43 # pragma warning( push )
44 # pragma warning( disable : 4275 )
45 #endif
46 
47 /**
48  * \brief This exception is thrown when attempting to load an outdated file
49  * \ingroup librender
50  */
51 class MTS_EXPORT_RENDER VersionException : public std::runtime_error {
52 public:
53  VersionException(const std::string &str, const Version &version) :
54  std::runtime_error(str), m_version(version) { }
55 
56  /* For stupid and subtle reasons when compiling with GCC, it is important
57  that this class has a virtual member. This will ensure that its typeid
58  structure is in librender, which is important for throwing exceptions
59  across DLL boundaries */
60  virtual ~VersionException() throw ();
61 
62  inline const Version &getVersion() const { return m_version; }
63 private:
64  Version m_version;
65 };
66 
67 #ifdef _MSC_VER
68 # pragma warning( pop )
69 #endif
70 
71 /// Push a cleanup handler to be executed after loading the scene is done
72 extern MTS_EXPORT_RENDER void pushSceneCleanupHandler(void (*cleanup)());
73 
74 /**
75  * \brief XML parser for Mitsuba scene files. To be used with the
76  * SAX interface of Xerces-C++.
77  *
78  * \remark In the Python bindings, only the static function
79  * \ref loadScene() is exposed.
80  * \ingroup librender
81  * \ingroup libpython
82  */
83 class MTS_EXPORT_RENDER SceneHandler : public xercesc::HandlerBase {
84 public:
85  typedef std::map<std::string, ConfigurableObject *> NamedObjectMap;
86  typedef std::map<std::string, std::string, SimpleStringOrdering> ParameterMap;
87 
88  SceneHandler(const ParameterMap &params, NamedObjectMap *objects = NULL,
89  bool isIncludedFile = false);
90  virtual ~SceneHandler();
91 
92  /// Convenience method -- load a scene from a given filename
93  static ref<Scene> loadScene(const fs::path &filename,
94  const ParameterMap &params= ParameterMap());
95 
96  /// Convenience method -- load a scene from a given string
97  static ref<Scene> loadSceneFromString(const std::string &string,
98  const ParameterMap &params= ParameterMap());
99 
100  /// Initialize Xerces-C++ (needs to be called once at program startup)
101  static void staticInitialization();
102 
103  /// Free the memory taken up by staticInitialization()
104  static void staticShutdown();
105 
106  // -----------------------------------------------------------------------
107  // Implementation of the SAX DocumentHandler interface
108  // -----------------------------------------------------------------------
109  virtual void startDocument();
110  virtual void endDocument();
111  virtual void startElement(
112  const XMLCh* const name,
113  xercesc::AttributeList& attributes
114  );
115  virtual void endElement(const XMLCh* const name);
116  virtual void characters(const XMLCh* const chars, const XMLSize_t length);
117  virtual void setDocumentLocator(const xercesc::Locator* const locator);
118 
119  inline const Scene *getScene() const { return m_scene.get(); }
120  inline Scene *getScene() { return m_scene; }
121 
122  // -----------------------------------------------------------------------
123  // Implementation of the SAX ErrorHandler interface
124  // -----------------------------------------------------------------------
125  void warning(const xercesc::SAXParseException& exc);
126  void error(const xercesc::SAXParseException& exc);
127  void fatalError(const xercesc::SAXParseException& exc);
128 protected:
129  std::string transcode(const XMLCh * input) const;
130 
131  Float parseFloat(const std::string &name, const std::string &str,
132  Float defVal = -1) const;
133 
134  void clear();
135 
136 private:
137  /**
138  * Enumeration of all possible tags that can be encountered in a
139  * Mitsuba scene file
140  */
141  enum ETag {
142  EScene, EShape, ESampler, EFilm,
143  EIntegrator, ETexture, ESensor,
144  EEmitter, ESubsurface, EMedium,
145  EVolume, EPhase, EBSDF, ERFilter,
146  ENull, EReference, EInteger, EFloat,
147  EBoolean, EString, ETranslate, ERotate,
148  ELookAt, EScale, EMatrix, EPoint,
149  EVector, ERGB, ESRGB, EBlackBody,
150  ESpectrum, ETransform, EAnimation,
151  EInclude, EAlias, EDefault
152  };
153 
154  struct ParseContext {
155  inline ParseContext(ParseContext *_parent, ETag tag)
156  : parent(_parent), tag(tag) { }
157 
158  ParseContext *parent;
159  ETag tag;
160  Properties properties;
161  std::map<std::string, std::string> attributes;
162  std::vector<std::pair<std::string, ConfigurableObject *> > children;
163  };
164 
165 
166  typedef std::pair<ETag, const Class *> TagEntry;
167  typedef boost::unordered_map<std::string, TagEntry> TagMap;
168 
169  const xercesc::Locator *m_locator;
170  xercesc::XMLTranscoder* m_transcoder;
171  ref<Scene> m_scene;
172  ParameterMap m_params;
173  NamedObjectMap *m_namedObjects;
174  PluginManager *m_pluginManager;
175  std::stack<ParseContext> m_context;
176  TagMap m_tags;
177  Transform m_transform;
178  ref<AnimatedTransform> m_animatedTransform;
179  bool m_isIncludedFile;
180 };
181 
183 
184 #endif /* __MITSUBA_RENDER_SCENEHANDLER_H_ */
std::map< std::string, ConfigurableObject * > NamedObjectMap
Definition: scenehandler.h:85
std::map< std::string, std::string, SimpleStringOrdering > ParameterMap
Definition: scenehandler.h:86
Principal scene data structure.
Definition: scene.h:49
const Scene * getScene() const
Definition: scenehandler.h:119
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
VersionException(const std::string &str, const Version &version)
Definition: scenehandler.h:53
Reference counting helper.
Definition: ref.h:40
Scene * getScene()
Definition: scenehandler.h:120
void pushSceneCleanupHandler(void(*cleanup)())
Push a cleanup handler to be executed after loading the scene is done.
XML parser for Mitsuba scene files. To be used with the SAX interface of Xerces-C++.
Definition: scenehandler.h:83
#define MTS_EXPORT_RENDER
Definition: platform.h:109
#define MTS_NAMESPACE_END
Definition: platform.h:138
This exception is thrown when attempting to load an outdated file.
Definition: scenehandler.h:51
A simple data structure for representing and comparing Mitsuba version strings.
Definition: version.h:43