| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGDB_PLUGIN_IMAGE_WRITER |
|---|
| 15 | #define OSGDB_PLUGIN_IMAGE_WRITER 1 |
|---|
| 16 | |
|---|
| 17 | #include <osgDB/Export> |
|---|
| 18 | #include <string> |
|---|
| 19 | #include <map> |
|---|
| 20 | |
|---|
| 21 | namespace osg |
|---|
| 22 | { |
|---|
| 23 | class Object; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | namespace osgDB |
|---|
| 27 | { |
|---|
| 28 | |
|---|
| 29 | class Options; |
|---|
| 30 | |
|---|
| 31 | /// Helper allowing 'intelligent' writing of external files (images, shaders, etc.), regarding to a main file (a scene), especially in plugins. |
|---|
| 32 | /// Goals are: |
|---|
| 33 | /// - Enable writing out objects only once (even if referenced multiple times) |
|---|
| 34 | /// - Handle duplicates (avoid writing two different objects at the same place, renaming files as needed) |
|---|
| 35 | /// - Handle directory creation when paths don't just exist |
|---|
| 36 | /// - Generate writing paths which may keep original directory structure (depending on user wishes). Ex: |
|---|
| 37 | /// Reading: model.osg and images/img1.jpg |
|---|
| 38 | /// Writing with 'keepRelativePaths': /somePath/newmodel.osg and /somePath/images/img1.jpg |
|---|
| 39 | /// Writing without 'keepRelativePaths': /somePath/newmodel.osg and /somePath/img1.jpg |
|---|
| 40 | ///\author Sukender |
|---|
| 41 | ///\todo Handling of naming constraints (such as "8.3" names in 3DS) |
|---|
| 42 | class OSGDB_EXPORT ExternalFileWriter |
|---|
| 43 | { |
|---|
| 44 | public: |
|---|
| 45 | /// Builds the helper class with all options. |
|---|
| 46 | ///\param srcDirectory Directory of the initial main file (if any), used as a base when relativising objects names. Not used if keepRelativePaths==false. |
|---|
| 47 | ///\param destDirectory Directory where to write the main file. |
|---|
| 48 | ///\param keepRelativePaths If true, then relative paths of source objects are kept if possible (ex: If an image is initially "imageDir/image.jpg" relatively to the source dir, then we'd like to get "destDir/imageDir/image.jpg"). If false, then only the simple file name is used to write the object file. |
|---|
| 49 | ///\param allowUpDirs When relativising objects paths, sets the maximum number of directories the objects can be written "up" the destination directory. Not used if keepRelativePaths==false. Examples: If an image is initially "../image.jpg" relatively to the source dir *AND* if we allow one dir level up, then we'd like to get "destDirParent/destDir/../image.jpg" (= "destDirParent/image.jpg"). If we *DO NOT* allow one dir level up, then we'd like to get "destDir/image.jpg". |
|---|
| 50 | ExternalFileWriter(const std::string & srcDirectory, const std::string & destDirectory, bool keepRelativePaths, unsigned int allowUpDirs=0); |
|---|
| 51 | |
|---|
| 52 | /// Short constructor used when not relativising objects paths, or when having no initial model file (which is pretty the same here). |
|---|
| 53 | ExternalFileWriter(const std::string & destDirectory); |
|---|
| 54 | |
|---|
| 55 | /// Writes the current object if not already done. |
|---|
| 56 | ///\param obj Object to write, using corresponding osgDB::write method. |
|---|
| 57 | ///\param options Writing options to pass to corresponding osgDB::write method. |
|---|
| 58 | ///\param [out] out_absolutePath Pointer to a string to be filled with absolute writing path, or NULL. |
|---|
| 59 | ///\param [out] out_relativePath Pointer to a string to be filled with write path relative to the destination directory if possible (absolute path if not), or NULL. |
|---|
| 60 | ///\return true on success, false otherwise. |
|---|
| 61 | bool write(const osg::Object & obj, const osgDB::Options * options, std::string * out_absolutePath=NULL, std::string * out_relativePath=NULL); |
|---|
| 62 | |
|---|
| 63 | struct ObjectData |
|---|
| 64 | { |
|---|
| 65 | ObjectData() : written(false) {} |
|---|
| 66 | ObjectData(const std::string & absolutePath, const std::string & relativePath, bool written) : absolutePath(absolutePath), relativePath(relativePath), written(written) {} |
|---|
| 67 | std::string absolutePath; |
|---|
| 68 | std::string relativePath; |
|---|
| 69 | bool written; ///< Says if write succeded or not. |
|---|
| 70 | }; |
|---|
| 71 | |
|---|
| 72 | /// Set of written objects, with their absolute writing path. |
|---|
| 73 | /// Objects being passed to the write() method but which have failed to be effectively written are also included. |
|---|
| 74 | typedef std::map<const osg::Object*, ObjectData> ObjectsSet; |
|---|
| 75 | |
|---|
| 76 | /// Returns the written objects. |
|---|
| 77 | const ObjectsSet & getObjects() const { return _objects; } |
|---|
| 78 | |
|---|
| 79 | protected: |
|---|
| 80 | // Dev note: |
|---|
| 81 | // A multi-indexed structure would be more efficient for ObjectsSet (such as boost::multi_index, indexed on object pointer (unique), and hashed indexed on absolute path (unique)). |
|---|
| 82 | // In order to get a correct search time, SearchMap "replaces" the multi-index structure for hashed indexes on absolute paths. |
|---|
| 83 | typedef std::multimap<unsigned int, const osg::Object*> SearchMap; |
|---|
| 84 | typedef unsigned int ObjectIndex; ///< Integer type used for indices of unnamed objects |
|---|
| 85 | ObjectsSet _objects; |
|---|
| 86 | SearchMap _searchMap; ///< Map used to search by absolute file path. |
|---|
| 87 | ObjectIndex _lastGeneratedObjectIndex; |
|---|
| 88 | const std::string _srcDirectory; |
|---|
| 89 | const std::string _destDirectory; |
|---|
| 90 | bool _keepRelativePaths; |
|---|
| 91 | const unsigned int _allowUpDirs; |
|---|
| 92 | |
|---|
| 93 | /// Generates a unique name for an object to be written on disk. |
|---|
| 94 | /// Side effect: updates _lastGeneratedObjectIndex to the index associated withe the returned name. |
|---|
| 95 | void generateObjectName(std::string & out_relativePath, std::string & out_absolutePath, int type); |
|---|
| 96 | |
|---|
| 97 | bool absoluteObjectPathExists(const std::string & path); |
|---|
| 98 | |
|---|
| 99 | private: |
|---|
| 100 | // Prevent copy |
|---|
| 101 | ExternalFileWriter & operator=(const ExternalFileWriter &); |
|---|
| 102 | ExternalFileWriter(const ExternalFileWriter &); |
|---|
| 103 | }; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | #endif // OSGDB_PLUGIN_IMAGE_WRITER |
|---|