|
| 1 | +/* |
| 2 | +This file forked from https://github.com/srajotte/libplyxx |
| 3 | +
|
| 4 | +Copyright (c) 2016 Simon Rajotte |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +
|
| 24 | +Updated (c) 2021 Runette Software Ltd to make multiplatform, to complete the typemaps and add to voxel types. |
| 25 | +
|
| 26 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 27 | +of this software and associated documentation files (the "Software"), to deal |
| 28 | +in the Software without restriction, including without limitation the rights |
| 29 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 30 | +copies of the Software, and to permit persons to whom the Software is |
| 31 | +furnished to do so, subject to the following conditions: |
| 32 | +
|
| 33 | +The above copyright notice and this permission notice shall be included in all |
| 34 | +copies or substantial portions of the Software. |
| 35 | +
|
| 36 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 37 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 38 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 39 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 40 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 41 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 42 | +SOFTWARE. |
| 43 | +
|
| 44 | +*/ |
| 45 | + |
| 46 | +#pragma once |
| 47 | + |
| 48 | +#include <vector> |
| 49 | +#include <array> |
| 50 | +#include <map> |
| 51 | +#include <unordered_map> |
| 52 | +#include <type_traits> |
| 53 | +#include <cassert> |
| 54 | +#include <memory> |
| 55 | +#include <functional> |
| 56 | + |
| 57 | +#include "textio.h" |
| 58 | + |
| 59 | +namespace libply |
| 60 | +{ |
| 61 | + enum class Type |
| 62 | + { |
| 63 | + INT8, |
| 64 | + UINT8, |
| 65 | + INT16, |
| 66 | + UINT16, |
| 67 | + INT32, |
| 68 | + UINT32, |
| 69 | + FLOAT32, |
| 70 | + FLOAT64 |
| 71 | + }; |
| 72 | + |
| 73 | + class IProperty |
| 74 | + { |
| 75 | + public: |
| 76 | + virtual ~IProperty() = default; |
| 77 | + |
| 78 | + virtual IProperty &operator=( unsigned int value ) = 0; |
| 79 | + virtual IProperty &operator=( int value ) = 0; |
| 80 | + virtual IProperty &operator=( float value ) = 0; |
| 81 | + virtual IProperty &operator=( double value ) = 0; |
| 82 | + |
| 83 | + virtual operator unsigned int() = 0; |
| 84 | + virtual operator int() = 0; |
| 85 | + virtual operator float() = 0; |
| 86 | + virtual operator double() = 0; |
| 87 | + |
| 88 | + virtual bool isList() = 0; |
| 89 | + |
| 90 | + }; |
| 91 | + |
| 92 | + template<typename InternalType> |
| 93 | + class ScalarProperty: public IProperty |
| 94 | + { |
| 95 | + public : |
| 96 | + |
| 97 | + virtual ~ScalarProperty() = default; |
| 98 | + |
| 99 | + virtual ScalarProperty &operator=( unsigned int value ) override |
| 100 | + { |
| 101 | + m_value = static_cast<InternalType>( value ); |
| 102 | + return *this; |
| 103 | + }; |
| 104 | + virtual ScalarProperty &operator=( int value ) override |
| 105 | + { |
| 106 | + m_value = static_cast<InternalType>( value ); |
| 107 | + return *this; |
| 108 | + }; |
| 109 | + virtual ScalarProperty &operator=( float value ) override |
| 110 | + { |
| 111 | + m_value = static_cast<InternalType>( value ); |
| 112 | + return *this; |
| 113 | + }; |
| 114 | + virtual ScalarProperty &operator=( double value ) override |
| 115 | + { |
| 116 | + m_value = static_cast<InternalType>( value ); |
| 117 | + return *this; |
| 118 | + }; |
| 119 | + |
| 120 | + virtual operator unsigned int() override |
| 121 | + { |
| 122 | + return static_cast<unsigned int>( m_value ); |
| 123 | + }; |
| 124 | + virtual operator int() override |
| 125 | + { |
| 126 | + return static_cast<int>( m_value ); |
| 127 | + }; |
| 128 | + virtual operator float() override |
| 129 | + { |
| 130 | + return static_cast<float>( m_value ); |
| 131 | + }; |
| 132 | + virtual operator double() override |
| 133 | + { |
| 134 | + return static_cast<double>( m_value ); |
| 135 | + }; |
| 136 | + |
| 137 | + virtual bool isList() override { return false; } |
| 138 | + |
| 139 | + public: |
| 140 | + InternalType value() const { return m_value; }; |
| 141 | + |
| 142 | + private : |
| 143 | + InternalType m_value; |
| 144 | + }; |
| 145 | + |
| 146 | + class ListProperty : public IProperty |
| 147 | + { |
| 148 | + public: |
| 149 | + |
| 150 | + IProperty &operator=( unsigned int ) override { return *this; }; |
| 151 | + IProperty &operator=( int ) override { return *this; }; |
| 152 | + IProperty &operator=( float ) override { return *this; }; |
| 153 | + IProperty &operator=( double ) override { return *this; }; |
| 154 | + |
| 155 | + operator unsigned int() override { return 0; }; |
| 156 | + operator int() override { return 0; }; |
| 157 | + operator float() override { return 0; }; |
| 158 | + operator double() override { return 0; }; |
| 159 | + |
| 160 | + bool isList() override { return true; } |
| 161 | + |
| 162 | + void define( Type type, size_t size ); |
| 163 | + size_t size() const { return list.size(); } |
| 164 | + |
| 165 | + IProperty &value( size_t index ); |
| 166 | + |
| 167 | + private: |
| 168 | + std::vector<std::unique_ptr<IProperty>> list; |
| 169 | + std::unique_ptr<IProperty> getScalarProperty( Type type ); |
| 170 | + }; |
| 171 | + |
| 172 | + struct ElementDefinition; |
| 173 | + |
| 174 | + class ElementBuffer |
| 175 | + { |
| 176 | + public: |
| 177 | + ElementBuffer() = default; |
| 178 | + ElementBuffer( const ElementDefinition &definition ); |
| 179 | + |
| 180 | + public: |
| 181 | + size_t size() const { return properties.size(); }; |
| 182 | + IProperty &operator[]( size_t index ); |
| 183 | + |
| 184 | + private: |
| 185 | + void appendScalarProperty( Type type ); |
| 186 | + void appendListProperty( Type type ); |
| 187 | + std::unique_ptr<IProperty> getScalarProperty( Type type ); |
| 188 | + |
| 189 | + private: |
| 190 | + std::vector<std::unique_ptr<IProperty>> properties; |
| 191 | + }; |
| 192 | + |
| 193 | + struct Property |
| 194 | + { |
| 195 | + Property( const std::string &name, Type type, bool isList ) |
| 196 | + : name( name ), type( type ), isList( isList ) {}; |
| 197 | + |
| 198 | + std::string name; |
| 199 | + Type type; |
| 200 | + bool isList; |
| 201 | + size_t listCount; |
| 202 | + }; |
| 203 | + |
| 204 | + typedef std::size_t ElementSize; |
| 205 | + |
| 206 | + struct Element |
| 207 | + { |
| 208 | + Element( const std::string &name, ElementSize size, const std::vector<Property> &properties ) |
| 209 | + : name( name ), size( size ), properties( properties ) {}; |
| 210 | + |
| 211 | + std::string name; |
| 212 | + ElementSize size; |
| 213 | + std::vector<Property> properties; |
| 214 | + }; |
| 215 | + |
| 216 | + typedef std::function< void( ElementBuffer & ) > ElementReadCallback; |
| 217 | + |
| 218 | + class FileParser; |
| 219 | + |
| 220 | + typedef std::vector<Element> ElementsDefinition; |
| 221 | + typedef std::unordered_map<std::string, std::string> Metadata; |
| 222 | + |
| 223 | + class File |
| 224 | + { |
| 225 | + public: |
| 226 | + File( const std::string &filename ); |
| 227 | + ~File(); |
| 228 | + |
| 229 | + ElementsDefinition definitions() const; |
| 230 | + Metadata metadata() const; |
| 231 | + void setElementReadCallback( std::string elementName, ElementReadCallback &readCallback ); |
| 232 | + void read(); |
| 233 | + |
| 234 | + public: |
| 235 | + enum class Format |
| 236 | + { |
| 237 | + ASCII, |
| 238 | + BINARY_LITTLE_ENDIAN, |
| 239 | + BINARY_BIG_ENDIAN |
| 240 | + }; |
| 241 | + |
| 242 | + private: |
| 243 | + std::string m_filename; |
| 244 | + std::unique_ptr<FileParser> m_parser; |
| 245 | + }; |
| 246 | + |
| 247 | + |
| 248 | + typedef std::function< void( ElementBuffer &, size_t index ) > ElementWriteCallback; |
| 249 | + |
| 250 | + class FileOut |
| 251 | + { |
| 252 | + public: |
| 253 | + FileOut( const std::string &filename, File::Format format ); |
| 254 | + |
| 255 | + void setElementsDefinition( const ElementsDefinition &definitions ); |
| 256 | + void setElementWriteCallback( const std::string &elementName, ElementWriteCallback &writeCallback ); |
| 257 | + void write(); |
| 258 | + Metadata metadata; |
| 259 | + |
| 260 | + private: |
| 261 | + void createFile(); |
| 262 | + void writeHeader(); |
| 263 | + void writeData(); |
| 264 | + |
| 265 | + private: |
| 266 | + std::string m_filename; |
| 267 | + File::Format m_format; |
| 268 | + ElementsDefinition m_definitions; |
| 269 | + std::map<std::string, ElementWriteCallback> m_writeCallbacks; |
| 270 | + }; |
| 271 | +} |
0 commit comments