| 1 |
/**************************************************************************** |
| 2 |
* NanoPLY * |
| 3 |
* NanoPLY is a C++11 header-only library to read and write PLY file * |
| 4 |
* * |
| 5 |
* Copyright(C) 2014-2015 * |
| 6 |
* Visual Computing Lab * |
| 7 |
* ISTI - Italian National Research Council * |
| 8 |
* * |
| 9 |
* This Source Code Form is subject to the terms of the Mozilla Public * |
| 10 |
* License, v. 2.0. If a copy of the MPL was not distributed with this * |
| 11 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * |
| 12 |
* * |
| 13 |
****************************************************************************/ |
| 14 |
|
| 15 |
|
| 16 |
#ifndef NANOPLY_HPP |
| 17 |
#define NANOPLY_HPP |
| 18 |
|
| 19 |
#include <vector> |
| 20 |
#include <unordered_map> |
| 21 |
#include <tuple> |
| 22 |
#include <cassert> |
| 23 |
#include <algorithm> |
| 24 |
#include <stdexcept> |
| 25 |
#include <cstdio> |
| 26 |
#include <cmath> |
| 27 |
#include <cstring> |
| 28 |
#include <limits> |
| 29 |
#include <fstream> |
| 30 |
#include <sstream> |
| 31 |
#include <stdint.h> |
| 32 |
#include <string> |
| 33 |
#include <iomanip> |
| 34 |
|
| 35 |
|
| 36 |
// Avoid conflicting declaration of min/max macros in windows headers |
| 37 |
#if !defined(NOMINMAX) && (defined(_WIN32) || defined(_WIN32_) || defined(WIN32) || defined(_WIN64)) |
| 38 |
# define NOMINMAX |
| 39 |
# ifdef max |
| 40 |
# undef max |
| 41 |
# undef min |
| 42 |
# endif |
| 43 |
#endif |
| 44 |
|
| 45 |
namespace nanoply |
| 46 |
{ |
| 47 |
|
| 48 |
/** Error Type. |
| 49 |
* Error type returned by the open of a PLY file. |
| 50 |
*/ |
| 51 |
typedef enum NNP_ERROR |
| 52 |
{ |
| 53 |
NNP_OK = 0x0000, /**< No error. */ |
| 54 |
NNP_UNABLE_TO_OPEN = 0x0001, /**< The file cannot be opend. */ |
| 55 |
NNP_MISSING_HEADER = 0x0002, /**< The file does not contain a valid PLY header. */ |
| 56 |
NNP_MISSING_FORMAT = 0x0004, /**< The file has an invalid internal format. */ |
| 57 |
NNP_INVALID_ELEMENT = 0x0008, /**< The file has an invalid element. */ |
| 58 |
NNP_INVALID_PROPERTY = 0x0010 /**< The file has an invalid property. */ |
| 59 |
} ErrorCode; |
| 60 |
|
| 61 |
|
| 62 |
/** PLY Element Entity. |
| 63 |
* Element that can be saved in PLY file. |
| 64 |
*/ |
| 65 |
typedef enum NNP_ELEM |
| 66 |
{ |
| 67 |
NNP_UNKNOWN_ELEM = 0x0, /**< Unknown element. */ |
| 68 |
NNP_VERTEX_ELEM = 0x1, /**< Vertex element. */ |
| 69 |
NNP_EDGE_ELEM = 0x2, /**< Edge element. */ |
| 70 |
NNP_FACE_ELEM = 0x4 /**< Face element. */ |
| 71 |
} PlyElemEntity; |
| 72 |
|
| 73 |
|
| 74 |
/** PLY Entity. |
| 75 |
* Property that can be saved in a PLY file. |
| 76 |
*/ |
| 77 |
typedef enum NNP_ENTITY |
| 78 |
{ |
| 79 |
NNP_UNKNOWN_ENTITY = 0x00000000, /**< Unknown property. */ |
| 80 |
NNP_PX = 0x00000001, /**< Position x cordinate. */ |
| 81 |
NNP_PY = 0x00000002, /**< Position y cordinate. */ |
| 82 |
NNP_PZ = 0x00000004, /**< Position z cordinate. */ |
| 83 |
NNP_PXYZ = 0x00000007, /**< Position (x, y, z). */ |
| 84 |
NNP_NX = 0x00000010, /**< Normal x component. */ |
| 85 |
NNP_NY = 0x00000020, /**< Normal y component. */ |
| 86 |
NNP_NZ = 0x00000040, /**< Normal z component. */ |
| 87 |
NNP_NXYZ = 0x00000070, /**< Normal (x, y, z). */ |
| 88 |
NNP_CR = 0x00000100, /**< Color red component. */ |
| 89 |
NNP_CG = 0x00000200, /**< Color green component. */ |
| 90 |
NNP_CB = 0x00000400, /**< Color blue component. */ |
| 91 |
NNP_CRGB = 0x00000700, /**< Color RGB. */ |
| 92 |
NNP_CA = 0x00000800, /**< Color alpha component. */ |
| 93 |
NNP_CRGBA = 0x00000F00, /**< Color RGBA. */ |
| 94 |
NNP_DENSITY = 0x00000008, /**< Density or Radius property. */ |
| 95 |
NNP_SCALE = 0x00000080, /**< Scale property. */ |
| 96 |
NNP_TEXTUREU = 0x00001000, /**< Texture coordinate u. */ |
| 97 |
NNP_TEXTUREV = 0x00002000, /**< Texture coordinate v. */ |
| 98 |
NNP_TEXTURE2D = 0x00003000, /**< Texture coordinate 2D. */ |
| 99 |
NNP_TEXTUREW = 0x00004000, /**< Texture coordinate w. */ |
| 100 |
NNP_TEXTURE3D = 0x00007000, /**< Texture coordinate 3D. */ |
| 101 |
NNP_TEXTUREINDEX = 0x00008000, /**< Texture index. */ |
| 102 |
NNP_QUALITY = 0x00010000, /**< Quality property. */ |
| 103 |
NNP_REFLECTANCE = 0x00020000, /**< Reflectance property. */ |
| 104 |
NNP_BITFLAG = 0x00040000, /**< Bit flags. */ |
| 105 |
NNP_K1 = 0x00080000, /**< Main curvaure value k1. */ |
| 106 |
NNP_K2 = 0x00100000, /**< Main curvaure value k2. */ |
| 107 |
// NNP_KG = 0x00200000, /**< Gaussian curvature value. */ |
| 108 |
// NNP_KH = 0x00400000, /**< Mean curvature value. */ |
| 109 |
NNP_K1DIR = 0x00800000, /**< Curvature direction k1. */ |
| 110 |
NNP_K2DIR = 0x01000000, /**< Curvature direction k2. */ |
| 111 |
NNP_EDGE_V1 = 0x02000000, /**< Index of the first vertex of the edge. */ |
| 112 |
NNP_EDGE_V2 = 0x04000000, /**< Index of the second vertex of the edge. */ |
| 113 |
NNP_FACE_VERTEX_LIST = 0x08000000, /**< List of vertex indices for the face. */ |
| 114 |
NNP_FACE_WEDGE_COLOR = 0x10000000, /**< List of colors for wedge. */ |
| 115 |
NNP_FACE_WEDGE_NORMAL = 0x20000000, /**< List of normals for wedge. */ |
| 116 |
NNP_FACE_WEDGE_TEX = 0x40000000 /**< List of texture coordinates for wedge. */ |
| 117 |
} PlyEntity; |
| 118 |
|
| 119 |
|
| 120 |
/** PLY Type. |
| 121 |
* Type of a PLY property. |
| 122 |
*/ |
| 123 |
typedef enum NNP_PLYTYPE |
| 124 |
{ |
| 125 |
NNP_UNKNOWN_TYPE = 0x000000, /**< Unknown type. */ |
| 126 |
NNP_FLOAT32 = 0x000001, /**< Float. */ |
| 127 |
NNP_FLOAT64 = 0x000002, /**< Double. */ |
| 128 |
NNP_INT8 = 0x000004, /**< Char. */ |
| 129 |
NNP_INT16 = 0x000008, /**< Short. */ |
| 130 |
NNP_INT32 = 0x000010, /**< Int. */ |
| 131 |
NNP_UINT8 = 0x000020, /**< Unsigned Char. */ |
| 132 |
NNP_UINT16 = 0x000040, /**< Unsigned Short. */ |
| 133 |
NNP_UINT32 = 0x000080, /**< Unsigned Int. */ |
| 134 |
NNP_LIST_UINT8_UINT32 = 0x000100, /**< List (size Unsigned Char) of Unsigned Int. */ |
| 135 |
NNP_LIST_INT8_UINT32 = 0x000200, /**< List (size Char) of Unsigned Int. */ |
| 136 |
NNP_LIST_UINT8_INT32 = 0x000400, /**< List (size Unsigned Char) of Int. */ |
| 137 |
NNP_LIST_INT8_INT32 = 0x000800, /**< List (size Char) of Int. */ |
| 138 |
NNP_LIST_UINT8_FLOAT32 = 0x001000, /**< List (size Unsigned Char) of Float. */ |
| 139 |
NNP_LIST_INT8_FLOAT32 = 0x002000, /**< List (size Char) of Float. */ |
| 140 |
NNP_LIST_UINT8_FLOAT64 = 0x004000, /**< List (size Unsigned Char) of Double. */ |
| 141 |
NNP_LIST_INT8_FLOAT64 = 0x008000, /**< List (size Char) of Double. */ |
| 142 |
NNP_LIST_UINT8_UINT8 = 0x010000, /**< List (size Unsigned Char) of Unsigned Char. */ |
| 143 |
NNP_LIST_INT8_UINT8 = 0x020000, /**< List (size Char) of Unsigned Char. */ |
| 144 |
NNP_LIST_UINT8_INT8 = 0x040000, /**< List (size Unsigned Char) of Char. */ |
| 145 |
NNP_LIST_INT8_INT8 = 0x080000, /**< List (size Char) of Char. */ |
| 146 |
NNP_LIST_UINT8_UINT16 = 0x100000, /**< List (size Unsigned Char) of Unsigned Short. */ |
| 147 |
NNP_LIST_INT8_UINT16 = 0x200000, /**< List (size Char) of Unsigned Short. */ |
| 148 |
NNP_LIST_UINT8_INT16 = 0x400000, /**< List (size Unsigned Char) of Short. */ |
| 149 |
NNP_LIST_INT8_INT16 = 0x800000 /**< List (size Char) of Short. */ |
| 150 |
} PlyType; |
| 151 |
|
| 152 |
template <typename TEnum> |
| 153 |
inline std::size_t hashEnum(const TEnum & value) |
| 154 |
{ |
| 155 |
const std::hash<unsigned int> h{}; |
| 156 |
return h((unsigned int)value); |
| 157 |
} |
| 158 |
|
| 159 |
} // end namespace nanoply |
| 160 |
|
| 161 |
// Cast of an enum to an unsigned int (maybe only for Android) |
| 162 |
namespace std |
| 163 |
{ |
| 164 |
template <> struct hash<nanoply::PlyElemEntity> { std::size_t operator () (const nanoply::PlyElemEntity & t) const { return nanoply::hashEnum(t); } }; |
| 165 |
template <> struct hash<nanoply::PlyEntity > { std::size_t operator () (const nanoply::PlyEntity & t) const { return nanoply::hashEnum(t); } }; |
| 166 |
template <> struct hash<nanoply::PlyType > { std::size_t operator () (const nanoply::PlyType & t) const { return nanoply::hashEnum(t); } }; |
| 167 |
} // end namespace std |
| 168 |
|
| 169 |
namespace nanoply |
| 170 |
{ |
| 171 |
|
| 172 |
/** |
| 173 |
* @cond HIDDEN_SYMBOLS |
| 174 |
*/ |
| 175 |
template < size_t T> struct SizeT {}; |
| 176 |
|
| 177 |
typedef std::vector<std::string> NameVector; |
| 178 |
typedef std::unordered_map<PlyType, NameVector> TypeMap; |
| 179 |
typedef std::unordered_map<PlyType, NameVector>::iterator TypeMapIterator; |
| 180 |
typedef std::unordered_map<PlyEntity, NameVector> EntityMap; |
| 181 |
typedef std::unordered_map<PlyEntity, NameVector>::iterator EntityMapIterator; |
| 182 |
typedef std::unordered_map<PlyElemEntity, NameVector> ElementMap; |
| 183 |
typedef std::unordered_map<PlyElemEntity, NameVector>::iterator ElementMapIterator; |
| 184 |
|
| 185 |
/* Names used for the PlyType */ |
| 186 |
static TypeMap mapType({ |
| 187 |
{ PlyType::NNP_UNKNOWN_TYPE, NameVector({ "unknonw" }) }, |
| 188 |
{ PlyType::NNP_FLOAT32, NameVector({ "float", "float32" }) }, |
| 189 |
{ PlyType::NNP_FLOAT64, NameVector({ "double", "float64" }) }, |
| 190 |
{ PlyType::NNP_INT8, NameVector({ "char", "int8" }) }, |
| 191 |
{ PlyType::NNP_INT16, NameVector({ "short", "int16" }) }, |
| 192 |
{ PlyType::NNP_INT32, NameVector({ "int", "int32" }) }, |
| 193 |
{ PlyType::NNP_UINT8, NameVector({ "uchar", "uint8" }) }, |
| 194 |
{ PlyType::NNP_UINT16, NameVector({ "ushort", "uint16" }) }, |
| 195 |
{ PlyType::NNP_UINT32, NameVector({ "uint", "uint32" }) }, |
| 196 |
{ PlyType::NNP_LIST_UINT8_UINT32, NameVector({ "list uchar uint", "list uint8 uint32" }) }, |
| 197 |
{ PlyType::NNP_LIST_INT8_UINT32, NameVector({ "list char uint", "list int8 uint32" }) }, |
| 198 |
{ PlyType::NNP_LIST_UINT8_INT32, NameVector({ "list uchar int", "list uint8 int32" }) }, |
| 199 |
{ PlyType::NNP_LIST_INT8_INT32, NameVector({ "list char int", "list int8 int32" }) }, |
| 200 |
{ PlyType::NNP_LIST_UINT8_FLOAT32, NameVector({ "list uchar float", "list uint8 float32" }) }, |
| 201 |
{ PlyType::NNP_LIST_INT8_FLOAT32, NameVector({ "list char float", "list int8 float32" }) }, |
| 202 |
{ PlyType::NNP_LIST_UINT8_FLOAT64, NameVector({ "list uchar double", "list uint8 float64" }) }, |
| 203 |
{ PlyType::NNP_LIST_INT8_FLOAT64, NameVector({ "list char double", "list int8 float64" }) }, |
| 204 |
{ PlyType::NNP_LIST_UINT8_UINT8, NameVector({ "list uchar uchar", "list uint8 uint8" }) }, |
| 205 |
{ PlyType::NNP_LIST_INT8_UINT8, NameVector({ "list char uchar", "list int8 uint8" }) }, |
| 206 |
{ PlyType::NNP_LIST_UINT8_INT8, NameVector({ "list uchar char", "list uint8 int8" }) }, |
| 207 |
{ PlyType::NNP_LIST_INT8_INT8, NameVector({ "list char char", "list int8 int8" }) }, |
| 208 |
{ PlyType::NNP_LIST_UINT8_UINT16, NameVector({ "list uchar ushort", "list uint8 uint16" }) }, |
| 209 |
{ PlyType::NNP_LIST_INT8_UINT16, NameVector({ "list char ushort", "list int8 uint16" }) }, |
| 210 |
{ PlyType::NNP_LIST_UINT8_INT16, NameVector({ "list uchar short", "list uint8 int16" }) }, |
| 211 |
{ PlyType::NNP_LIST_INT8_INT16, NameVector({ "list char short", "list int8 int16" }) } |
| 212 |
}); |
| 213 |
|
| 214 |
|
| 215 |
/* Names used for the PlyProperty */ |
| 216 |
static EntityMap mapProp({ |
| 217 |
{ PlyEntity::NNP_UNKNOWN_ENTITY, NameVector({ "unknonw" }) }, |
| 218 |
{ PlyEntity::NNP_PX, NameVector({ "x", "px", "posx" }) }, |
| 219 |
{ PlyEntity::NNP_PY, NameVector({ "y", "py", "posy" }) }, |
| 220 |
{ PlyEntity::NNP_PZ, NameVector({ "z", "pz", "posz" }) }, |
| 221 |
{ PlyEntity::NNP_PXYZ, NameVector({ "x y z", "px py pz", "posx posy posz" }) }, |
| 222 |
{ PlyEntity::NNP_NX, NameVector({ "nx", "normalx" }) }, |
| 223 |
{ PlyEntity::NNP_NY, NameVector({ "ny", "normaly" }) }, |
| 224 |
{ PlyEntity::NNP_NZ, NameVector({ "nz", "normalz" }) }, |
| 225 |
{ PlyEntity::NNP_NXYZ, NameVector({ "nx ny nz", "normalx normaly normalz" }) }, |
| 226 |
{ PlyEntity::NNP_CR, NameVector({ "red", "diffuse_red", "r", "diffuse_r" }) }, |
| 227 |
{ PlyEntity::NNP_CG, NameVector({ "green", "diffuse_green", "g", "diffuse_g" }) }, |
| 228 |
{ PlyEntity::NNP_CB, NameVector({ "blue", "diffuse_blue", "b", "diffuse_b" }) }, |
| 229 |
{ PlyEntity::NNP_CA, NameVector({ "alpha", "diffuse_alpha", "a", "diffuse_a" }) }, |
| 230 |
{ PlyEntity::NNP_CRGB, NameVector({ "rgb", "diffuse_rgb" }) }, |
| 231 |
{ PlyEntity::NNP_CRGBA, NameVector({ "rgba", "diffuse_rgba" }) }, |
| 232 |
{ PlyEntity::NNP_DENSITY, NameVector({ "radius", "density" }) }, |
| 233 |
{ PlyEntity::NNP_SCALE, NameVector({ "scale", "value" }) }, |
| 234 |
{ PlyEntity::NNP_TEXTUREU, NameVector({ "texture_u", "u", "s", "tx" }) }, |
| 235 |
{ PlyEntity::NNP_TEXTUREV, NameVector({ "texture_v", "v", "t", "ty" }) }, |
| 236 |
{ PlyEntity::NNP_TEXTURE2D, NameVector({ "texture_uv", "uv" }) }, |
| 237 |
{ PlyEntity::NNP_TEXTUREW, NameVector({ "texture_w", "w" }) }, |
| 238 |
{ PlyEntity::NNP_TEXTURE3D, NameVector({ "texture_uvw", "uvw" }) }, |
| 239 |
{ PlyEntity::NNP_TEXTUREINDEX, NameVector({ "texnumber", "texid" }) }, |
| 240 |
{ PlyEntity::NNP_QUALITY, NameVector({ "quality", "confidence" }) }, |
| 241 |
{ PlyEntity::NNP_REFLECTANCE, NameVector({ "reflectance" }) }, |
| 242 |
{ PlyEntity::NNP_BITFLAG, NameVector({ "flags" }) }, |
| 243 |
{ PlyEntity::NNP_K1, NameVector({ "k1" }) }, |
| 244 |
{ PlyEntity::NNP_K2, NameVector({ "k2" }) }, |
| 245 |
// { PlyEntity::NNP_KG, NameVector({ "k" }) }, |
| 246 |
// { PlyEntity::NNP_KH, NameVector({ "h" }) }, |
| 247 |
{ PlyEntity::NNP_K1DIR, NameVector({ "k1dir" }) }, |
| 248 |
{ PlyEntity::NNP_K2DIR, NameVector({ "k2dir" }) }, |
| 249 |
{ PlyEntity::NNP_EDGE_V1, NameVector({ "vertex1", "v1" }) }, |
| 250 |
{ PlyEntity::NNP_EDGE_V2, NameVector({ "vertex2", "v2" }) }, |
| 251 |
{ PlyEntity::NNP_FACE_VERTEX_LIST, NameVector({ "vertex_indices", "vertex_index" }) }, |
| 252 |
{ PlyEntity::NNP_FACE_WEDGE_COLOR, NameVector({ "color" }) }, |
| 253 |
{ PlyEntity::NNP_FACE_WEDGE_NORMAL, NameVector({ "normal" }) }, |
| 254 |
{ PlyEntity::NNP_FACE_WEDGE_TEX, NameVector({ "texcoord" }) } |
| 255 |
}); |
| 256 |
|
| 257 |
|
| 258 |
/* Names used for the PlyElement */ |
| 259 |
static ElementMap mapElem({ |
| 260 |
{ PlyElemEntity::NNP_UNKNOWN_ELEM, NameVector({ "unknown" }) }, |
| 261 |
{ PlyElemEntity::NNP_VERTEX_ELEM, NameVector({ "vertex" }) }, |
| 262 |
{ PlyElemEntity::NNP_EDGE_ELEM, NameVector({ "edge" }) }, |
| 263 |
{ PlyElemEntity::NNP_FACE_ELEM, NameVector({ "face" }) }, |
| 264 |
}); |
| 265 |
|
| 266 |
|
| 267 |
/* Returns the vector of possible name for the input PlyEntity */ |
| 268 |
static inline const NameVector& PlyPropertyName(PlyEntity ent) |
| 269 |
{ |
| 270 |
static NameVector emptyVec; |
| 271 |
if (mapProp.find(ent) != mapProp.end()) |
| 272 |
return mapProp[ent]; |
| 273 |
return emptyVec; |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
/* Returns the vector of possible name for the input PlyType */ |
| 278 |
static inline const NameVector& PlyTypeName(PlyType ent) |
| 279 |
{ |
| 280 |
static NameVector emptyVec; |
| 281 |
if (mapType.find(ent) != mapType.end()) |
| 282 |
return mapType[ent]; |
| 283 |
return emptyVec; |
| 284 |
}; |
| 285 |
|
| 286 |
|
| 287 |
/* Returns the vector of possible name for the input PlyElement */ |
| 288 |
static inline const NameVector& PlyElementName(PlyElemEntity ent) |
| 289 |
{ |
| 290 |
static NameVector emptyVec; |
| 291 |
if (mapElem.find(ent) != mapElem.end()) |
| 292 |
return mapElem[ent]; |
| 293 |
return emptyVec; |
| 294 |
}; |
| 295 |
|
| 296 |
|
| 297 |
/* Return 1 for little endian, 0 for big endian*/ |
| 298 |
inline int checkEndianness() |
| 299 |
{ |
| 300 |
unsigned int x = 1; |
| 301 |
char *c = (char*)&x; |
| 302 |
return (int)*c; |
| 303 |
}; |
| 304 |
|
| 305 |
/* Adjust the endianess of the input buffer*/ |
| 306 |
inline void adjustEndianess(unsigned char* buffer, int typeSize, int count) |
| 307 |
{ |
| 308 |
for (int i = 0; i < count; i++) |
| 309 |
{ |
| 310 |
int offset = i*typeSize; |
| 311 |
for (int j = 0; j < typeSize / 2; j++) |
| 312 |
{ |
| 313 |
unsigned char temp = buffer[offset + j]; |
| 314 |
buffer[offset + j] = buffer[offset + typeSize - 1 - j]; |
| 315 |
buffer[offset + typeSize - 1 - j] = temp; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
/** |
| 320 |
* @endcond |
| 321 |
*/ |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
/** PLY File. |
| 326 |
* Class to manage the read and write of a PLY file using a memory buffer. |
| 327 |
*/ |
| 328 |
class PlyFile |
| 329 |
{ |
| 330 |
private: |
| 331 |
std::fstream fileStream; /**< Stream. */ |
| 332 |
char mode; /**< Mode of the stream (0 = read, 1 = write). */ |
| 333 |
|
| 334 |
int64_t bufferSize; /**< Size of the buffer. */ |
| 335 |
int64_t bufferOffset; /**< Offset for the next operation. */ |
| 336 |
int64_t maxSize; /**< Maximum size of the stream. */ |
| 337 |
char* buffer; /**< Buffer. */ |
| 338 |
|
| 339 |
public: |
| 340 |
|
| 341 |
PlyFile(); |
| 342 |
|
| 343 |
~PlyFile(); |
| 344 |
|
| 345 |
/** |
| 346 |
* Open the file in read mode. |
| 347 |
* |
| 348 |
* @param filename name of the file. |
| 349 |
* @return If successful returns true. Otherwise, it returns false. |
| 350 |
*/ |
| 351 |
inline bool OpenFileToRead(const std::string &filename); |
| 352 |
|
| 353 |
/** |
| 354 |
* Open the file in write mode. |
| 355 |
* |
| 356 |
* @param filename name of the file. |
| 357 |
* @return If successful returns true. Otherwise, it returns false. |
| 358 |
*/ |
| 359 |
inline bool OpenFileToWrite(const std::string &filename); |
| 360 |
|
| 361 |
/** |
| 362 |
* Read the next header line. |
| 363 |
* |
| 364 |
* @param line read line. |
| 365 |
* @param last true if the line is the last of header. |
| 366 |
* @return If successful returns true. Otherwise, it returns false. |
| 367 |
*/ |
| 368 |
inline bool NextHeaderLine(std::string &line, bool &last); |
| 369 |
|
| 370 |
/** |
| 371 |
* Write the line in the header. |
| 372 |
* |
| 373 |
* @param line line to write. |
| 374 |
* @return If successful returns true. Otherwise, it returns false. |
| 375 |
*/ |
| 376 |
inline bool WriteHeaderLine(const std::string &line); |
| 377 |
|
| 378 |
/** |
| 379 |
* Read binary data from the file. |
| 380 |
* |
| 381 |
* @param dest pointer where to copy the data. |
| 382 |
* @param nByte number of byte to read. |
| 383 |
* @return If successful returns true. Otherwise, it returns false. |
| 384 |
*/ |
| 385 |
inline bool ReadBinaryData(char * & dest, int nByte); |
| 386 |
|
| 387 |
/** |
| 388 |
* Read ASCII data from the file. |
| 389 |
* |
| 390 |
* @param dest reference to the container of the data. |
| 391 |
* @return If successful returns true. Otherwise, it returns false. |
| 392 |
*/ |
| 393 |
template <typename T> |
| 394 |
inline bool ReadAsciiData(T &dest); |
| 395 |
|
| 396 |
/** |
| 397 |
* Write binary data in the file. |
| 398 |
* |
| 399 |
* @param src pointer to the data to write. |
| 400 |
* @param nByte number of byte to write. |
| 401 |
* @return If successful returns true. Otherwise, it returns false. |
| 402 |
*/ |
| 403 |
inline bool WriteBinaryData(void *src, int nByte); |
| 404 |
|
| 405 |
/** |
| 406 |
* Write ASCII data in the file. |
| 407 |
* |
| 408 |
* @param src reference to the container of the data. |
| 409 |
* @return If successful returns true. Otherwise, it returns false. |
| 410 |
*/ |
| 411 |
template <typename T> |
| 412 |
inline bool WriteAsciiData(const T &src); |
| 413 |
|
| 414 |
/** |
| 415 |
* Set the maximum size of the buffer. |
| 416 |
* |
| 417 |
* @return size size of the buffer. |
| 418 |
*/ |
| 419 |
inline void SetBufferSize(int64_t size); |
| 420 |
|
| 421 |
/** |
| 422 |
* Force the write of the buffer in the file. |
| 423 |
*/ |
| 424 |
inline void Flush(); |
| 425 |
}; |
| 426 |
|
| 427 |
|
| 428 |
inline PlyFile::PlyFile() |
| 429 |
{ |
| 430 |
buffer = NULL; |
| 431 |
maxSize = 10 * (1 << 20); |
| 432 |
mode = -1; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
inline PlyFile::~PlyFile() |
| 437 |
{ |
| 438 |
Flush(); |
| 439 |
if (buffer != NULL) |
| 440 |
delete[] buffer; |
| 441 |
if (fileStream.is_open()) |
| 442 |
fileStream.close(); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
inline bool PlyFile::OpenFileToRead(const std::string& filename) |
| 447 |
{ |
| 448 |
if (fileStream.is_open()) |
| 449 |
fileStream.close(); |
| 450 |
mode = 0; |
| 451 |
fileStream.open(filename, std::fstream::in | std::fstream::binary); |
| 452 |
if (fileStream.fail()) |
| 453 |
return false; |
| 454 |
bufferOffset = 0; |
| 455 |
return true; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
inline bool PlyFile::OpenFileToWrite(const std::string& filename) |
| 460 |
{ |
| 461 |
if (fileStream.is_open()) |
| 462 |
fileStream.close(); |
| 463 |
mode = 1; |
| 464 |
fileStream.open(filename, std::fstream::out | std::fstream::binary); |
| 465 |
if (fileStream.fail()) |
| 466 |
return false; |
| 467 |
bufferOffset = 0; |
| 468 |
fileStream.setf(std::ios::fixed, std::ios::floatfield); |
| 469 |
//fileStream.precision(7); |
| 470 |
return true; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
inline bool PlyFile::NextHeaderLine(std::string& line, bool& last) |
| 475 |
{ |
| 476 |
if (mode != 0) |
| 477 |
return false; |
| 478 |
std::getline(fileStream, line); |
| 479 |
std::transform(line.begin(), line.end(), line.begin(), ::tolower); |
| 480 |
last = false; |
| 481 |
if (line.find("end_header") != std::string::npos) |
| 482 |
last = true; |
| 483 |
return true; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
inline bool PlyFile::WriteHeaderLine(const std::string& line) |
| 488 |
{ |
| 489 |
if (mode != 1) |
| 490 |
return false; |
| 491 |
fileStream << line; |
| 492 |
return true; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
inline bool PlyFile::ReadBinaryData(char * & dest, int nByte) |
| 497 |
{ |
| 498 |
if (mode != 0) |
| 499 |
return false; |
| 500 |
if (buffer == NULL) |
| 501 |
{ |
| 502 |
buffer = new char[maxSize]; |
| 503 |
fileStream.read(buffer, maxSize); |
| 504 |
bufferSize = fileStream.gcount(); |
| 505 |
bufferOffset = 0; |
| 506 |
} |
| 507 |
else if (bufferOffset + nByte > bufferSize) |
| 508 |
{ |
| 509 |
const size_t lastByte = bufferSize - bufferOffset; |
| 510 |
memcpy(buffer, &buffer[bufferOffset], lastByte); |
| 511 |
fileStream.read(&buffer[lastByte], maxSize - lastByte); |
| 512 |
bufferSize = fileStream.gcount() + lastByte; |
| 513 |
bufferOffset = 0; |
| 514 |
} |
| 515 |
//memcpy(dest, &buffer[bufferOffset], nByte); |
| 516 |
dest = buffer + bufferOffset; |
| 517 |
bufferOffset += nByte; |
| 518 |
return true; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
template <typename T> |
| 523 |
inline bool PlyFile::ReadAsciiData(T& dest) |
| 524 |
{ |
| 525 |
if (mode != 0) |
| 526 |
return false; |
| 527 |
fileStream >> dest; |
| 528 |
return true; |
| 529 |
} |
| 530 |
|
| 531 |
inline bool PlyFile::WriteBinaryData(void* src, int nByte) |
| 532 |
{ |
| 533 |
if (mode != 1) |
| 534 |
return false; |
| 535 |
if (buffer == NULL) |
| 536 |
{ |
| 537 |
buffer = new char[maxSize]; |
| 538 |
bufferSize = maxSize; |
| 539 |
bufferOffset = 0; |
| 540 |
} |
| 541 |
else if (bufferOffset + nByte > bufferSize) |
| 542 |
{ |
| 543 |
fileStream.write(buffer, bufferOffset); |
| 544 |
bufferOffset = 0; |
| 545 |
} |
| 546 |
memcpy(&buffer[bufferOffset], src, nByte); |
| 547 |
bufferOffset += nByte; |
| 548 |
return true; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
template <typename T> |
| 553 |
inline bool PlyFile::WriteAsciiData(const T& src) |
| 554 |
{ |
| 555 |
if (mode != 1) |
| 556 |
return false; |
| 557 |
fileStream << src; |
| 558 |
return true; |
| 559 |
} |
| 560 |
|
| 561 |
template <> |
| 562 |
inline bool PlyFile::WriteAsciiData<double>(const double & src) |
| 563 |
{ |
| 564 |
if (mode != 1) |
| 565 |
return false; |
| 566 |
const auto precision = fileStream.precision(); |
| 567 |
fileStream << std::setprecision(std::numeric_limits<double>::max_digits10) |
| 568 |
<< src |
| 569 |
<< std::setprecision(precision); |
| 570 |
return true; |
| 571 |
} |
| 572 |
|
| 573 |
template <> |
| 574 |
inline bool PlyFile::WriteAsciiData<float>(const float & src) |
| 575 |
{ |
| 576 |
if (mode != 1) |
| 577 |
return false; |
| 578 |
const auto precision = fileStream.precision(); |
| 579 |
fileStream << std::setprecision(std::numeric_limits<float>::max_digits10) |
| 580 |
<< src |
| 581 |
<< std::setprecision(precision); |
| 582 |
return true; |
| 583 |
} |
| 584 |
|
| 585 |
inline void PlyFile::SetBufferSize(int64_t size) |
| 586 |
{ |
| 587 |
maxSize = size; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
inline void PlyFile::Flush() |
| 592 |
{ |
| 593 |
if (mode == 1) |
| 594 |
fileStream.write(buffer, bufferOffset); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
/** PLY Property. |
| 601 |
* Define a PLY property (entity and type). |
| 602 |
*/ |
| 603 |
class PlyProperty |
| 604 |
{ |
| 605 |
public: |
| 606 |
|
| 607 |
std::string name; /**< Property name. */ |
| 608 |
PlyType type; /**< Property type. */ |
| 609 |
PlyEntity elem; /**< Property entity. */ |
| 610 |
bool validToWrite; /**< Property validity (necessary to write the header). */ |
| 611 |
|
| 612 |
|
| 613 |
inline PlyProperty() {} |
| 614 |
|
| 615 |
|
| 616 |
/** |
| 617 |
* Constructor that sets the type and the entity of a standard PLY property. |
| 618 |
* |
| 619 |
* @param _t Property type. |
| 620 |
* @param _e Property entity. |
| 621 |
*/ |
| 622 |
inline PlyProperty(PlyType _t, PlyEntity _e) : name(PlyPropertyName(_e)[0]), type(_t), elem(_e), validToWrite(false){} |
| 623 |
|
| 624 |
/** |
| 625 |
* Constructor that sets the type, the entity and the name of a standard PLY property. |
| 626 |
* |
| 627 |
* @param _t Property type. |
| 628 |
* @param _e Property entity. |
| 629 |
* @param _n Property name. |
| 630 |
*/ |
| 631 |
inline PlyProperty(PlyType _t, PlyEntity _e, std::string _n) : name(_n), type(_t), elem(_e), validToWrite(false){} |
| 632 |
|
| 633 |
/** |
| 634 |
* Constructor that sets the type and the name of a custom PLY property. |
| 635 |
* |
| 636 |
* @param _t Property type. |
| 637 |
* @param _n Property name. |
| 638 |
*/ |
| 639 |
inline PlyProperty(PlyType _t, std::string _n) :name(_n), type(_t), elem(PlyEntity::NNP_UNKNOWN_ENTITY), validToWrite(false){} |
| 640 |
|
| 641 |
/** |
| 642 |
* Get the description string of the property entity. |
| 643 |
* |
| 644 |
* @return Description string of the property entity. |
| 645 |
*/ |
| 646 |
inline const char* EntityStr(); |
| 647 |
|
| 648 |
/** |
| 649 |
* Get the name of the property entity. |
| 650 |
* |
| 651 |
* @return Name of the property entity. |
| 652 |
*/ |
| 653 |
inline const char* EntityName(); |
| 654 |
|
| 655 |
/** |
| 656 |
* Get the description string of the property type. |
| 657 |
* |
| 658 |
* @return Description string of the property type. |
| 659 |
*/ |
| 660 |
inline const char* TypeStr(); |
| 661 |
|
| 662 |
/** |
| 663 |
* Get the size in byte of the property type. |
| 664 |
* |
| 665 |
* @return Size in byte of the property type. |
| 666 |
*/ |
| 667 |
inline int TypeSize(); |
| 668 |
|
| 669 |
/** |
| 670 |
* Get the number of component of the property entity. |
| 671 |
* |
| 672 |
* @return Number of component. |
| 673 |
*/ |
| 674 |
inline int CountValue(); |
| 675 |
|
| 676 |
/** |
| 677 |
* Check if the property type is signed or unsigned. |
| 678 |
* |
| 679 |
* @return true = signed, false = unsigned. |
| 680 |
*/ |
| 681 |
inline bool IsSigned(); |
| 682 |
|
| 683 |
/** |
| 684 |
* Skip the property in an Ascii file. |
| 685 |
* |
| 686 |
* @param file Opened file. |
| 687 |
* @return If successful returns true. Otherwise, it returns false. |
| 688 |
*/ |
| 689 |
inline bool SkipAsciiPropertyInFile(PlyFile &file); |
| 690 |
|
| 691 |
/** |
| 692 |
* Skip the property in a binary file. |
| 693 |
* |
| 694 |
* @param file Opened file. |
| 695 |
* @return If successful returns true. Otherwise, it returns false. |
| 696 |
*/ |
| 697 |
inline bool SkipBinaryPropertyInFile(PlyFile &file); |
| 698 |
|
| 699 |
/** |
| 700 |
* Write the property string in the header of the PLY file. |
| 701 |
* |
| 702 |
* @param file Opened file. |
| 703 |
* @return If successful returns true. Otherwise, it returns false. |
| 704 |
*/ |
| 705 |
inline bool WriteHeader(PlyFile &file); |
| 706 |
}; |
| 707 |
|
| 708 |
|
| 709 |
inline const char* PlyProperty::EntityStr() |
| 710 |
{ |
| 711 |
switch (this->elem) |
| 712 |
{ |
| 713 |
case NNP_UNKNOWN_ENTITY: return "NNP_UNKNOWN_ENTITY "; |
| 714 |
case NNP_PX: return "NNP_PX "; |
| 715 |
case NNP_PY: return "NNP_PY "; |
| 716 |
case NNP_PZ: return "NNP_PZ "; |
| 717 |
case NNP_PXYZ: return "NNP_PXYZ "; |
| 718 |
case NNP_NX: return "NNP_NX "; |
| 719 |
case NNP_NY: return "NNP_NY "; |
| 720 |
case NNP_NZ: return "NNP_NZ "; |
| 721 |
case NNP_NXYZ: return "NNP_NXYZ "; |
| 722 |
case NNP_CR: return "NNP_CR "; |
| 723 |
case NNP_CG: return "NNP_CG "; |
| 724 |
case NNP_CB: return "NNP_CB "; |
| 725 |
case NNP_CRGB: return "NNP_CRGB "; |
| 726 |
case NNP_CA: return "NNP_CA "; |
| 727 |
case NNP_CRGBA: return "NNP_CRGBA "; |
| 728 |
case NNP_DENSITY: return "NNP_DENSITY "; |
| 729 |
case NNP_SCALE: return "NNP_SCALE "; |
| 730 |
case NNP_QUALITY: return "NNP_QUALITY "; |
| 731 |
case NNP_REFLECTANCE: return "NNP_REFLECTANCE "; |
| 732 |
case NNP_TEXTUREU: return "NNP_TEXTUREU "; |
| 733 |
case NNP_TEXTUREV: return "NNP_TEXTUREV "; |
| 734 |
case NNP_TEXTURE2D: return "NNP_TEXTURE2D "; |
| 735 |
case NNP_TEXTUREW: return "NNP_TEXTUREW "; |
| 736 |
case NNP_TEXTURE3D: return "NNP_TEXTURE3D "; |
| 737 |
case NNP_TEXTUREINDEX: return "NNP_TEXTUREINDEX "; |
| 738 |
case NNP_BITFLAG: return "NNP_BITFLAG "; |
| 739 |
case NNP_K1: return "NNP_K1 "; |
| 740 |
case NNP_K2: return "NNP_K2 "; |
| 741 |
// case NNP_KG: return "NNP_K "; |
| 742 |
// case NNP_KH: return "NNP_H "; |
| 743 |
case NNP_K1DIR: return "NNP_K1DIR "; |
| 744 |
case NNP_K2DIR: return "NNP_K2DIR "; |
| 745 |
case NNP_EDGE_V1: return "NNP_EDGE_V1 "; |
| 746 |
case NNP_EDGE_V2: return "NNP_EDGE_V2 "; |
| 747 |
case NNP_FACE_VERTEX_LIST: return "NNP_FACE_VERTEX_LIST "; |
| 748 |
case NNP_FACE_WEDGE_COLOR: return "NNP_FACE_WEDGE_COLOR "; |
| 749 |
case NNP_FACE_WEDGE_NORMAL: return "NNP_FACE_WEDGE_NORMAL"; |
| 750 |
case NNP_FACE_WEDGE_TEX: return "NNP_FACE_WEDGE_TEX "; |
| 751 |
|
| 752 |
default: assert(0); |
| 753 |
break; |
| 754 |
} |
| 755 |
return 0; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
inline const char* PlyProperty::EntityName() |
| 760 |
{ |
| 761 |
|
| 762 |
if (this->elem == PlyEntity::NNP_UNKNOWN_ENTITY) |
| 763 |
return (*this).name.c_str(); |
| 764 |
#ifdef USE_NOSTANDARDPLY_OUTPUT |
| 765 |
if (this->elem == PlyEntity::NNP_SCALE) |
| 766 |
return PlyPropertyName(this->elem)[1].c_str(); |
| 767 |
if (this->elem == PlyEntity::NNP_QUALITY) |
| 768 |
return PlyPropertyName(this->elem)[1].c_str(); |
| 769 |
if (this->elem == PlyEntity::NNP_CR) |
| 770 |
return PlyPropertyName(this->elem)[1].c_str(); |
| 771 |
if (this->elem == PlyEntity::NNP_CB) |
| 772 |
return PlyPropertyName(this->elem)[1].c_str(); |
| 773 |
if (this->elem == PlyEntity::NNP_CG) |
| 774 |
return PlyPropertyName(this->elem)[1].c_str(); |
| 775 |
if (this->elem == PlyEntity::NNP_CA) |
| 776 |
return PlyPropertyName(this->elem)[1].c_str(); |
| 777 |
#endif |
| 778 |
const std::vector<std::string>& vect = PlyPropertyName(this->elem); |
| 779 |
if (vect.size() > 0) |
| 780 |
return vect[0].c_str(); |
| 781 |
assert(0); |
| 782 |
return 0; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
inline const char* PlyProperty::TypeStr() |
| 787 |
{ |
| 788 |
switch (this->type) |
| 789 |
{ |
| 790 |
case NNP_UNKNOWN_TYPE: return "NNP_UNKNOWN_TYPE "; |
| 791 |
case NNP_FLOAT32: return "NNP_FLOAT32 "; |
| 792 |
case NNP_FLOAT64: return "NNP_FLOAT64 "; |
| 793 |
case NNP_INT8: return "NNP_INT8 "; |
| 794 |
case NNP_INT16: return "NNP_INT16 "; |
| 795 |
case NNP_INT32: return "NNP_INT32 "; |
| 796 |
case NNP_UINT8: return "NNP_UINT8 "; |
| 797 |
case NNP_UINT16: return "NNP_UINT16 "; |
| 798 |
case NNP_UINT32: return "NNP_UINT32 "; |
| 799 |
case NNP_LIST_UINT8_UINT32: return "NNP_LIST_UINT8_UINT32 "; |
| 800 |
case NNP_LIST_INT8_UINT32: return "NNP_LIST_INT8_UINT32 "; |
| 801 |
case NNP_LIST_UINT8_INT32: return "NNP_LIST_UINT8_INT32 "; |
| 802 |
case NNP_LIST_INT8_INT32: return "NNP_LIST_INT8_INT32 "; |
| 803 |
case NNP_LIST_UINT8_FLOAT32: return "NNP_LIST_UINT8_FLOAT32 "; |
| 804 |
case NNP_LIST_INT8_FLOAT32: return "NNP_LIST_INT8_FLOAT32 "; |
| 805 |
case NNP_LIST_UINT8_FLOAT64: return "NNP_LIST_UINT8_FLOAT64 "; |
| 806 |
case NNP_LIST_INT8_FLOAT64: return "NNP_LIST_INT8_FLOAT64 "; |
| 807 |
case NNP_LIST_UINT8_UINT8: return "NNP_LIST_UINT8_UINT8 "; |
| 808 |
case NNP_LIST_INT8_UINT8: return "NNP_LIST_INT8_UINT8 "; |
| 809 |
case NNP_LIST_UINT8_INT8: return "NNP_LIST_UINT8_INT8 "; |
| 810 |
case NNP_LIST_INT8_INT8: return "NNP_LIST_INT8_INT8 "; |
| 811 |
case NNP_LIST_UINT8_UINT16: return "NNP_LIST_UINT8_UINT16 "; |
| 812 |
case NNP_LIST_INT8_UINT16: return "NNP_LIST_INT8_UINT16 "; |
| 813 |
case NNP_LIST_UINT8_INT16: return "NNP_LIST_UINT8_INT16 "; |
| 814 |
case NNP_LIST_INT8_INT16: return "NNP_LIST_INT8_INT16 "; |
| 815 |
default: assert(0); |
| 816 |
break; |
| 817 |
} |
| 818 |
return 0; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
inline int PlyProperty::TypeSize() |
| 823 |
{ |
| 824 |
switch (this->type) |
| 825 |
{ |
| 826 |
case NNP_UNKNOWN_TYPE: |
| 827 |
return 0; |
| 828 |
case NNP_INT8: |
| 829 |
case NNP_UINT8: |
| 830 |
return sizeof(char); |
| 831 |
case NNP_INT16: |
| 832 |
case NNP_UINT16: |
| 833 |
return sizeof(short); |
| 834 |
case NNP_FLOAT32: |
| 835 |
case NNP_INT32: |
| 836 |
case NNP_UINT32: |
| 837 |
return sizeof(int); |
| 838 |
case NNP_FLOAT64: |
| 839 |
return sizeof(double); |
| 840 |
case NNP_LIST_UINT8_UINT32: |
| 841 |
case NNP_LIST_INT8_UINT32: |
| 842 |
case NNP_LIST_UINT8_INT32: |
| 843 |
case NNP_LIST_INT8_INT32: |
| 844 |
return sizeof(int); |
| 845 |
case NNP_LIST_UINT8_UINT16: |
| 846 |
case NNP_LIST_INT8_UINT16: |
| 847 |
case NNP_LIST_UINT8_INT16: |
| 848 |
case NNP_LIST_INT8_INT16: |
| 849 |
return sizeof(short); |
| 850 |
case NNP_LIST_UINT8_UINT8: |
| 851 |
case NNP_LIST_INT8_UINT8: |
| 852 |
case NNP_LIST_UINT8_INT8: |
| 853 |
case NNP_LIST_INT8_INT8: |
| 854 |
return sizeof(char); |
| 855 |
case NNP_LIST_UINT8_FLOAT32: |
| 856 |
case NNP_LIST_INT8_FLOAT32: |
| 857 |
return sizeof(float); |
| 858 |
case NNP_LIST_UINT8_FLOAT64: |
| 859 |
case NNP_LIST_INT8_FLOAT64: |
| 860 |
return sizeof(double); |
| 861 |
default: assert(0); |
| 862 |
break; |
| 863 |
} |
| 864 |
return 0; |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
inline bool PlyProperty::IsSigned() |
| 869 |
{ |
| 870 |
switch (this->type) |
| 871 |
{ |
| 872 |
case NNP_INT8: |
| 873 |
case NNP_INT16: |
| 874 |
case NNP_INT32: |
| 875 |
case NNP_FLOAT32: |
| 876 |
case NNP_FLOAT64: |
| 877 |
case NNP_LIST_INT8_UINT32: |
| 878 |
case NNP_LIST_INT8_INT32: |
| 879 |
case NNP_LIST_INT8_UINT16: |
| 880 |
case NNP_LIST_INT8_INT16: |
| 881 |
case NNP_LIST_INT8_UINT8: |
| 882 |
case NNP_LIST_INT8_INT8: |
| 883 |
case NNP_LIST_INT8_FLOAT32: |
| 884 |
case NNP_LIST_INT8_FLOAT64: |
| 885 |
return true; |
| 886 |
case NNP_UINT8: |
| 887 |
case NNP_UINT16: |
| 888 |
case NNP_UINT32: |
| 889 |
case NNP_LIST_UINT8_UINT32: |
| 890 |
case NNP_LIST_UINT8_INT32: |
| 891 |
case NNP_LIST_UINT8_UINT16: |
| 892 |
case NNP_LIST_UINT8_INT16: |
| 893 |
case NNP_LIST_UINT8_UINT8: |
| 894 |
case NNP_LIST_UINT8_INT8: |
| 895 |
case NNP_LIST_UINT8_FLOAT32: |
| 896 |
case NNP_LIST_UINT8_FLOAT64: |
| 897 |
return false; |
| 898 |
default: |
| 899 |
return false; |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
inline int PlyProperty::CountValue() |
| 905 |
{ |
| 906 |
if (this->elem == NNP_CRGB || this->elem == NNP_NXYZ || this->elem == NNP_PXYZ || this->elem == NNP_TEXTURE3D) |
| 907 |
return 3; |
| 908 |
else if (this->elem == NNP_CRGBA) |
| 909 |
return 4; |
| 910 |
else if (this->elem == NNP_TEXTURE2D) |
| 911 |
return 2; |
| 912 |
return 1; |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
inline bool PlyProperty::SkipAsciiPropertyInFile(PlyFile &file) |
| 917 |
{ |
| 918 |
int count = CountValue(); |
| 919 |
if (this->type >= NNP_LIST_UINT8_UINT32) |
| 920 |
file.ReadAsciiData(count); |
| 921 |
switch (type) |
| 922 |
{ |
| 923 |
case NNP_INT8: |
| 924 |
case NNP_INT16: |
| 925 |
case NNP_INT32: |
| 926 |
case NNP_LIST_UINT8_INT32: |
| 927 |
case NNP_LIST_INT8_INT32: |
| 928 |
case NNP_LIST_UINT8_INT16: |
| 929 |
case NNP_LIST_INT8_INT16: |
| 930 |
case NNP_LIST_UINT8_INT8: |
| 931 |
case NNP_LIST_INT8_INT8: |
| 932 |
{ |
| 933 |
int* temp = new int[count]; |
| 934 |
for (int i = 0; i < count; i++) |
| 935 |
file.ReadAsciiData(temp[i]); |
| 936 |
delete[] temp; |
| 937 |
break; |
| 938 |
} |
| 939 |
case NNP_UINT8: |
| 940 |
case NNP_UINT16: |
| 941 |
case NNP_UINT32: |
| 942 |
case NNP_LIST_UINT8_UINT32: |
| 943 |
case NNP_LIST_INT8_UINT32: |
| 944 |
case NNP_LIST_UINT8_UINT16: |
| 945 |
case NNP_LIST_INT8_UINT16: |
| 946 |
case NNP_LIST_UINT8_UINT8: |
| 947 |
case NNP_LIST_INT8_UINT8: |
| 948 |
{ |
| 949 |
unsigned int* temp = new unsigned int[count]; |
| 950 |
for (int i = 0; i < count; i++) |
| 951 |
file.ReadAsciiData(temp[i]); |
| 952 |
delete[] temp; |
| 953 |
break; |
| 954 |
} |
| 955 |
case NNP_FLOAT32: |
| 956 |
case NNP_LIST_UINT8_FLOAT32: |
| 957 |
case NNP_LIST_INT8_FLOAT32: |
| 958 |
{ |
| 959 |
float* temp = new float[count]; |
| 960 |
for (int i = 0; i < count; i++) |
| 961 |
file.ReadAsciiData(temp[i]); |
| 962 |
delete[] temp; |
| 963 |
break; |
| 964 |
} |
| 965 |
case NNP_FLOAT64: |
| 966 |
case NNP_LIST_UINT8_FLOAT64: |
| 967 |
case NNP_LIST_INT8_FLOAT64: |
| 968 |
{ |
| 969 |
double* temp = new double[count]; |
| 970 |
for (int i = 0; i < count; i++) |
| 971 |
file.ReadAsciiData(temp[i]); |
| 972 |
delete[] temp; |
| 973 |
break; |
| 974 |
} |
| 975 |
default: assert(0); |
| 976 |
} |
| 977 |
return true; |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
inline bool PlyProperty::SkipBinaryPropertyInFile(PlyFile& file) |
| 982 |
{ |
| 983 |
char * temp = nullptr; |
| 984 |
int count = CountValue(); |
| 985 |
if (this->type >= NNP_LIST_UINT8_UINT32) |
| 986 |
{ |
| 987 |
int size; |
| 988 |
if (this->IsSigned()) |
| 989 |
{ |
| 990 |
file.ReadBinaryData(temp, sizeof(char)); |
| 991 |
size = this->TypeSize() * int(*(reinterpret_cast<unsigned char *>(temp))); |
| 992 |
} |
| 993 |
else |
| 994 |
{ |
| 995 |
file.ReadBinaryData(temp, sizeof(char)); |
| 996 |
size = this->TypeSize() * int(*(reinterpret_cast<unsigned char *>(temp))); |
| 997 |
} |
| 998 |
file.ReadBinaryData(temp, size); |
| 999 |
} |
| 1000 |
else |
| 1001 |
{ |
| 1002 |
int size = this->TypeSize() * count; |
| 1003 |
file.ReadBinaryData(temp, size); |
| 1004 |
} |
| 1005 |
return true; |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
inline bool PlyProperty::WriteHeader(PlyFile& file) |
| 1010 |
{ |
| 1011 |
if (!validToWrite) |
| 1012 |
return true; |
| 1013 |
std::string name, type; |
| 1014 |
type = PlyTypeName(this->type)[0]; |
| 1015 |
name = this->EntityName(); |
| 1016 |
std::vector<std::string> v; |
| 1017 |
switch (this->elem) |
| 1018 |
{ |
| 1019 |
case NNP_PXYZ: |
| 1020 |
{ |
| 1021 |
v.push_back(PlyPropertyName(NNP_PX)[0]); |
| 1022 |
v.push_back(PlyPropertyName(NNP_PY)[0]); |
| 1023 |
v.push_back(PlyPropertyName(NNP_PZ)[0]); |
| 1024 |
break; |
| 1025 |
} |
| 1026 |
case NNP_NXYZ: |
| 1027 |
{ |
| 1028 |
v.push_back(PlyPropertyName(NNP_NX)[0]); |
| 1029 |
v.push_back(PlyPropertyName(NNP_NY)[0]); |
| 1030 |
v.push_back(PlyPropertyName(NNP_NZ)[0]); |
| 1031 |
break; |
| 1032 |
} |
| 1033 |
case NNP_CRGB: |
| 1034 |
{ |
| 1035 |
v.push_back(PlyPropertyName(NNP_CR)[0]); |
| 1036 |
v.push_back(PlyPropertyName(NNP_CG)[0]); |
| 1037 |
v.push_back(PlyPropertyName(NNP_CB)[0]); |
| 1038 |
break; |
| 1039 |
} |
| 1040 |
case NNP_CRGBA: |
| 1041 |
{ |
| 1042 |
v.push_back(PlyPropertyName(NNP_CR)[0]); |
| 1043 |
v.push_back(PlyPropertyName(NNP_CG)[0]); |
| 1044 |
v.push_back(PlyPropertyName(NNP_CB)[0]); |
| 1045 |
v.push_back(PlyPropertyName(NNP_CA)[0]); |
| 1046 |
break; |
| 1047 |
} |
| 1048 |
case NNP_TEXTURE2D: |
| 1049 |
{ |
| 1050 |
v.push_back(PlyPropertyName(NNP_TEXTUREU)[0]); |
| 1051 |
v.push_back(PlyPropertyName(NNP_TEXTUREV)[0]); |
| 1052 |
break; |
| 1053 |
} |
| 1054 |
case NNP_TEXTURE3D: |
| 1055 |
{ |
| 1056 |
v.push_back(PlyPropertyName(NNP_TEXTUREU)[0]); |
| 1057 |
v.push_back(PlyPropertyName(NNP_TEXTUREV)[0]); |
| 1058 |
v.push_back(PlyPropertyName(NNP_TEXTUREW)[0]); |
| 1059 |
break; |
| 1060 |
} |
| 1061 |
default: |
| 1062 |
v.push_back(name); |
| 1063 |
|
| 1064 |
} |
| 1065 |
for (size_t i = 0; i < v.size(); i++) |
| 1066 |
{ |
| 1067 |
std::stringstream s; |
| 1068 |
s << "property " << type << " " << v[i] << "\n"; |
| 1069 |
if (!file.WriteHeaderLine(s.str())) |
| 1070 |
return false; |
| 1071 |
} |
| 1072 |
return true; |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
/** PLY Element. |
| 1079 |
* Define a PLY Element as a collection of properties. |
| 1080 |
*/ |
| 1081 |
class PlyElement |
| 1082 |
{ |
| 1083 |
public: |
| 1084 |
|
| 1085 |
std::string name; /**< Name of the elment in the PLY header (for example "vertex", "face", ect.). */ |
| 1086 |
PlyElemEntity plyElem; /**< Ply element entity. */ |
| 1087 |
size_t cnt; /**< Number of instances of the elment in the PLY file. */ |
| 1088 |
std::vector<PlyProperty> propVec; /**< Collection of properties that define the element. */ |
| 1089 |
bool validToWrite; /**< Element validity (necessary to write the header). */ |
| 1090 |
|
| 1091 |
/** |
| 1092 |
* Default Constructor |
| 1093 |
*/ |
| 1094 |
inline PlyElement() : cnt(0), validToWrite(false){} |
| 1095 |
|
| 1096 |
/** |
| 1097 |
* Constructor that sets the name, the properties and the number of instances of the element. |
| 1098 |
* |
| 1099 |
* @param _name Name of the element. |
| 1100 |
* @param prop Vector of properties. |
| 1101 |
* @param nElem Number of instances. |
| 1102 |
*/ |
| 1103 |
inline PlyElement(const std::string& _name, std::vector<PlyProperty> &prop, size_t nElem) :name(_name), plyElem(PlyElemEntity::NNP_UNKNOWN_ELEM), cnt(nElem), propVec(prop), validToWrite(false){} |
| 1104 |
|
| 1105 |
/** |
| 1106 |
* Constructor that sets the entity, the properties and the number of instances of the element. |
| 1107 |
* |
| 1108 |
* @param ent Element entity. |
| 1109 |
* @param prop Vector of properties. |
| 1110 |
* @param nElem Number of instances. |
| 1111 |
*/ |
| 1112 |
inline PlyElement(PlyElemEntity ent, std::vector<PlyProperty> &prop, size_t nElem) :name(PlyElementName(ent)[0]), plyElem(ent), cnt(nElem), propVec(prop), validToWrite(false){} |
| 1113 |
|
| 1114 |
/** |
| 1115 |
* Parse the input line and add the properties to the element. |
| 1116 |
* It assumes that the passed line has the folowing structure: "property PLYTYPE PLYELEMENT" |
| 1117 |
* |
| 1118 |
* @param line Input line. |
| 1119 |
* @return If successful returns true. Otherwise, it returns false. |
| 1120 |
*/ |
| 1121 |
inline bool AddProperty(std::string &line); |
| 1122 |
|
| 1123 |
/** |
| 1124 |
* Initialize an element from the header line. |
| 1125 |
* |
| 1126 |
* @param elemStr String with the element definition. |
| 1127 |
* @param propStr Strings with the property definition. |
| 1128 |
* @return If successful returns true. Otherwise, it returns false. |
| 1129 |
*/ |
| 1130 |
inline bool InitFromHeader(std::string &elemStr, std::vector<std::string> &propStr); |
| 1131 |
|
| 1132 |
/** |
| 1133 |
* Write the element descriport in the file header. |
| 1134 |
* |
| 1135 |
* @param file Ply file. |
| 1136 |
* @return If successful returns true. Otherwise, it returns false. |
| 1137 |
*/ |
| 1138 |
inline bool WriteHeader(PlyFile &file); |
| 1139 |
|
| 1140 |
/** |
| 1141 |
* Skip the element in an Ascii file. |
| 1142 |
* |
| 1143 |
* @param file Ply file |
| 1144 |
* @return If successful returns true. Otherwise, it returns false. |
| 1145 |
*/ |
| 1146 |
inline bool SkipAsciiElementsInFile(PlyFile &file); |
| 1147 |
|
| 1148 |
/** |
| 1149 |
* Skip the element in a binary file. |
| 1150 |
* |
| 1151 |
* @param file Ply file. |
| 1152 |
* @return If successful returns true. Otherwise, it returns false. |
| 1153 |
*/ |
| 1154 |
inline bool SkipBinaryElementsInFile(PlyFile &file); |
| 1155 |
|
| 1156 |
/** |
| 1157 |
* Check if the input entity is in the property of the element. |
| 1158 |
* |
| 1159 |
* @param entity Input entity. |
| 1160 |
* @return If successful returns true. Otherwise, it returns false. |
| 1161 |
*/ |
| 1162 |
inline bool Contains(NNP_ENTITY entity); |
| 1163 |
}; |
| 1164 |
|
| 1165 |
|
| 1166 |
inline bool PlyElement::InitFromHeader(std::string &elemStr, std::vector<std::string> &propStr) |
| 1167 |
{ |
| 1168 |
char* token; |
| 1169 |
char* tempStr = &elemStr[0]; |
| 1170 |
token = strtok(tempStr, " \t"); |
| 1171 |
if (strstr(token, "element") == NULL) |
| 1172 |
return false; |
| 1173 |
token = strtok(0, " \t\r\n"); |
| 1174 |
name = std::string(token); |
| 1175 |
plyElem = PlyElemEntity::NNP_UNKNOWN_ELEM; |
| 1176 |
ElementMapIterator iter = mapElem.begin(); |
| 1177 |
bool found = false; |
| 1178 |
while (iter != mapElem.end()) |
| 1179 |
{ |
| 1180 |
NameVector& v = (*iter).second; |
| 1181 |
for (size_t i = 0; i < v.size(); i++) |
| 1182 |
{ |
| 1183 |
if (v[i] == name) |
| 1184 |
{ |
| 1185 |
found = true; |
| 1186 |
break; |
| 1187 |
} |
| 1188 |
} |
| 1189 |
if (found) |
| 1190 |
{ |
| 1191 |
plyElem = (*iter).first; |
| 1192 |
break; |
| 1193 |
} |
| 1194 |
iter++; |
| 1195 |
} |
| 1196 |
token = strtok(0, " \t\r\n"); |
| 1197 |
cnt = atoi(token); |
| 1198 |
for (size_t i = 0; i < propStr.size(); i++) |
| 1199 |
if (!AddProperty(propStr[i])) |
| 1200 |
return false; |
| 1201 |
unsigned int mask = 0; |
| 1202 |
for (size_t i = 0; i < propVec.size(); i++) |
| 1203 |
mask |= propVec[i].elem; |
| 1204 |
std::vector<PlyProperty> compactPropVec; |
| 1205 |
for (size_t i = 0; i < propVec.size(); i++) |
| 1206 |
{ |
| 1207 |
switch (propVec[i].elem) |
| 1208 |
{ |
| 1209 |
case NNP_NX: |
| 1210 |
case NNP_NY: |
| 1211 |
if ((mask & NNP_NXYZ) != NNP_NXYZ) |
| 1212 |
compactPropVec.push_back(propVec[i]); |
| 1213 |
break; |
| 1214 |
case NNP_NZ: |
| 1215 |
if ((mask & NNP_NXYZ) != NNP_NXYZ) |
| 1216 |
compactPropVec.push_back(propVec[i]); |
| 1217 |
else |
| 1218 |
compactPropVec.push_back(PlyProperty(propVec[i].type, NNP_NXYZ)); |
| 1219 |
break; |
| 1220 |
case NNP_PX: |
| 1221 |
case NNP_PY: |
| 1222 |
if ((mask & NNP_PXYZ) != NNP_PXYZ) |
| 1223 |
compactPropVec.push_back(propVec[i]); |
| 1224 |
break; |
| 1225 |
case NNP_PZ: |
| 1226 |
if ((mask & NNP_PXYZ) != NNP_PXYZ) |
| 1227 |
compactPropVec.push_back(propVec[i]); |
| 1228 |
else |
| 1229 |
compactPropVec.push_back(PlyProperty(propVec[i].type, NNP_PXYZ)); |
| 1230 |
break; |
| 1231 |
case NNP_CR: |
| 1232 |
case NNP_CG: |
| 1233 |
if (((mask & NNP_CRGB) != NNP_CRGB) && ((mask & NNP_CRGBA) != NNP_CRGBA)) |
| 1234 |
compactPropVec.push_back(propVec[i]); |
| 1235 |
break; |
| 1236 |
case NNP_CB: |
| 1237 |
if (((mask & NNP_CRGB) != NNP_CRGB) & ((mask & NNP_CRGBA) != NNP_CRGBA)) |
| 1238 |
compactPropVec.push_back(propVec[i]); |
| 1239 |
else if (((mask & NNP_CRGB) == NNP_CRGB) & ((mask & NNP_CRGBA) != NNP_CRGBA)) |
| 1240 |
compactPropVec.push_back(PlyProperty(propVec[i].type, NNP_CRGB)); |
| 1241 |
break; |
| 1242 |
case NNP_CA: |
| 1243 |
if (((mask & NNP_CRGB) != NNP_CRGB) & ((mask & NNP_CRGBA) != NNP_CRGBA)) |
| 1244 |
compactPropVec.push_back(propVec[i]); |
| 1245 |
else if ((mask & NNP_CRGBA) == NNP_CRGBA) |
| 1246 |
compactPropVec.push_back(PlyProperty(propVec[i].type, NNP_CRGBA)); |
| 1247 |
break; |
| 1248 |
case NNP_TEXTUREU: |
| 1249 |
if (((mask & NNP_TEXTURE2D) != NNP_TEXTURE2D) & ((mask & NNP_TEXTURE3D) != NNP_TEXTURE3D)) |
| 1250 |
compactPropVec.push_back(propVec[i]); |
| 1251 |
break; |
| 1252 |
case NNP_TEXTUREV: |
| 1253 |
if (((mask & NNP_TEXTURE2D) != NNP_TEXTURE2D) & ((mask & NNP_TEXTURE3D) != NNP_TEXTURE3D)) |
| 1254 |
compactPropVec.push_back(propVec[i]); |
| 1255 |
else if (((mask & NNP_TEXTURE2D) == NNP_TEXTURE2D) & ((mask & NNP_TEXTURE3D) != NNP_TEXTURE3D)) |
| 1256 |
compactPropVec.push_back(PlyProperty(propVec[i].type, NNP_TEXTURE2D)); |
| 1257 |
break; |
| 1258 |
case NNP_TEXTUREW: |
| 1259 |
if (((mask & NNP_TEXTURE2D) != NNP_TEXTURE2D) & ((mask & NNP_TEXTURE3D) != NNP_TEXTURE3D)) |
| 1260 |
compactPropVec.push_back(propVec[i]); |
| 1261 |
else if ((mask & NNP_TEXTURE3D) == NNP_TEXTURE3D) |
| 1262 |
compactPropVec.push_back(PlyProperty(propVec[i].type, NNP_TEXTURE3D)); |
| 1263 |
break; |
| 1264 |
default: |
| 1265 |
compactPropVec.push_back(propVec[i]); |
| 1266 |
break; |
| 1267 |
} |
| 1268 |
} |
| 1269 |
propVec.clear(); |
| 1270 |
propVec = compactPropVec; |
| 1271 |
return true; |
| 1272 |
} |
| 1273 |
|
| 1274 |
|
| 1275 |
inline bool PlyElement::WriteHeader(PlyFile &file) |
| 1276 |
{ |
| 1277 |
if (!validToWrite || cnt == 0) |
| 1278 |
return true; |
| 1279 |
bool ok = true; |
| 1280 |
std::stringstream temp; |
| 1281 |
temp << "element " << name << " " << cnt << "\n"; |
| 1282 |
if (file.WriteHeaderLine(temp.str())) |
| 1283 |
{ |
| 1284 |
for (size_t i = 0; i < propVec.size(); i++) |
| 1285 |
ok = propVec[i].WriteHeader(file); |
| 1286 |
} |
| 1287 |
else |
| 1288 |
return false; |
| 1289 |
return ok; |
| 1290 |
} |
| 1291 |
|
| 1292 |
|
| 1293 |
inline bool PlyElement::SkipAsciiElementsInFile(PlyFile &file) |
| 1294 |
{ |
| 1295 |
for (size_t i = 0; i < this->cnt; ++i) |
| 1296 |
for (size_t j = 0; j < this->propVec.size(); ++j) |
| 1297 |
this->propVec[j].SkipAsciiPropertyInFile(file); |
| 1298 |
return true; |
| 1299 |
} |
| 1300 |
|
| 1301 |
|
| 1302 |
inline bool PlyElement::SkipBinaryElementsInFile(PlyFile &file) |
| 1303 |
{ |
| 1304 |
for (size_t i = 0; i < this->cnt; ++i) |
| 1305 |
for (size_t j = 0; j < this->propVec.size(); ++j) |
| 1306 |
this->propVec[j].SkipBinaryPropertyInFile(file); |
| 1307 |
return true; |
| 1308 |
} |
| 1309 |
|
| 1310 |
|
| 1311 |
inline bool PlyElement::AddProperty(std::string &line) |
| 1312 |
{ |
| 1313 |
char* token; |
| 1314 |
char* tempStr = &line[0]; |
| 1315 |
token = strtok(tempStr, " \t"); |
| 1316 |
if (strstr(token, "property") == NULL) |
| 1317 |
return false; |
| 1318 |
char* typeStr = strtok(0, " \t\n"); |
| 1319 |
char *ty1, *ty2; |
| 1320 |
std::string type; |
| 1321 |
type.append(typeStr); |
| 1322 |
if (strcmp(typeStr, "list") == 0) |
| 1323 |
{ |
| 1324 |
ty1 = strtok(0, " \t\n"); |
| 1325 |
ty2 = strtok(0, " \t\n"); |
| 1326 |
type.append(" "); |
| 1327 |
type.append(ty1); |
| 1328 |
type.append(" "); |
| 1329 |
type.append(ty2); |
| 1330 |
} |
| 1331 |
PlyType plyType = PlyType::NNP_UNKNOWN_TYPE; |
| 1332 |
TypeMapIterator iterType = mapType.begin(); |
| 1333 |
bool found = false; |
| 1334 |
while (iterType != mapType.end()) |
| 1335 |
{ |
| 1336 |
NameVector& v = (*iterType).second; |
| 1337 |
for (size_t i = 0; i < v.size(); i++) |
| 1338 |
{ |
| 1339 |
if (v[i] == type) |
| 1340 |
{ |
| 1341 |
found = true; |
| 1342 |
break; |
| 1343 |
} |
| 1344 |
} |
| 1345 |
if (found) |
| 1346 |
{ |
| 1347 |
plyType = (*iterType).first; |
| 1348 |
break; |
| 1349 |
} |
| 1350 |
iterType++; |
| 1351 |
} |
| 1352 |
if (plyType == PlyType::NNP_UNKNOWN_TYPE) |
| 1353 |
return false; |
| 1354 |
char* nameStr = strtok(0, " \t\n"); |
| 1355 |
PlyEntity plyEntity = PlyEntity::NNP_UNKNOWN_ENTITY; |
| 1356 |
EntityMapIterator iterEnt = mapProp.begin(); |
| 1357 |
found = false; |
| 1358 |
while (iterEnt != mapProp.end()) |
| 1359 |
{ |
| 1360 |
NameVector& v = (*iterEnt).second; |
| 1361 |
for (size_t i = 0; i < v.size(); i++) |
| 1362 |
{ |
| 1363 |
if (v[i] == nameStr) |
| 1364 |
{ |
| 1365 |
found = true; |
| 1366 |
break; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
if (found) |
| 1370 |
{ |
| 1371 |
plyEntity = (*iterEnt).first; |
| 1372 |
break; |
| 1373 |
} |
| 1374 |
iterEnt++; |
| 1375 |
} |
| 1376 |
if (plyEntity != PlyEntity::NNP_UNKNOWN_ENTITY) |
| 1377 |
propVec.push_back(PlyProperty(plyType, plyEntity, nameStr)); |
| 1378 |
else |
| 1379 |
propVec.push_back(PlyProperty(plyType, nameStr)); |
| 1380 |
return true; |
| 1381 |
} |
| 1382 |
|
| 1383 |
|
| 1384 |
inline bool PlyElement::Contains(PlyEntity entity) |
| 1385 |
{ |
| 1386 |
for (size_t i = 0; i < propVec.size(); i++) |
| 1387 |
{ |
| 1388 |
if (propVec[i].elem == entity) |
| 1389 |
return true; |
| 1390 |
} |
| 1391 |
return false; |
| 1392 |
} |
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
/** PLY header info. |
| 1398 |
* Define the data of the PLY header |
| 1399 |
*/ |
| 1400 |
class Info |
| 1401 |
{ |
| 1402 |
|
| 1403 |
public: |
| 1404 |
ErrorCode errInfo; /**< Error code returned by the reading of a PLY file. */ |
| 1405 |
bool binary; /**< Boolean about the file format (Binary = true, ASCII = false). */ |
| 1406 |
std::vector<PlyElement> elemVec; /**< Elements defined in the header. */ |
| 1407 |
bool bigEndian; /**< Endianess of the binary file. */ |
| 1408 |
std::string filename; /**< Filename. */ |
| 1409 |
std::vector<std::string> textureFile; /**< Texture file names. */ |
| 1410 |
|
| 1411 |
/** |
| 1412 |
* Default Constructor |
| 1413 |
*/ |
| 1414 |
inline Info(); |
| 1415 |
|
| 1416 |
/** |
| 1417 |
* Constructor that reads the header info from a file. |
| 1418 |
* |
| 1419 |
* @param filename Path of the file to read. |
| 1420 |
*/ |
| 1421 |
inline Info(const std::string& filename); |
| 1422 |
|
| 1423 |
/** |
| 1424 |
* Load the ply info from the header of the input filename. |
| 1425 |
* |
| 1426 |
* @param filename Path of the file to read. |
| 1427 |
* @return If successful returns true. Otherwise, it returns false. |
| 1428 |
*/ |
| 1429 |
inline bool LoadHeader(const std::string& filename); |
| 1430 |
|
| 1431 |
/** |
| 1432 |
* Write the ply info in the header of the input file. |
| 1433 |
* |
| 1434 |
* @param file File to write. |
| 1435 |
* @return If successful returns true. Otherwise, it returns false. |
| 1436 |
*/ |
| 1437 |
inline bool WriteHeader(PlyFile& file); |
| 1438 |
|
| 1439 |
/** |
| 1440 |
* Add the ply element to the header. |
| 1441 |
* |
| 1442 |
* @param pe Ply element to write in the header. |
| 1443 |
*/ |
| 1444 |
inline void AddPlyElement(PlyElement& pe); |
| 1445 |
|
| 1446 |
/** |
| 1447 |
* Clear the error code. |
| 1448 |
*/ |
| 1449 |
inline void Clear() { errInfo = NNP_OK; } |
| 1450 |
|
| 1451 |
/** |
| 1452 |
* Return the number of instances of the element with the input name |
| 1453 |
* |
| 1454 |
* @param name Name of the element. |
| 1455 |
* @return The number of instances |
| 1456 |
*/ |
| 1457 |
inline size_t GetElementCount(const std::string& name); |
| 1458 |
|
| 1459 |
/** |
| 1460 |
* Return the number of instances of the element with the input element type |
| 1461 |
* |
| 1462 |
* @param e Element type. |
| 1463 |
* @return The number of instances |
| 1464 |
*/ |
| 1465 |
inline size_t GetElementCount(PlyElemEntity e); |
| 1466 |
|
| 1467 |
/** |
| 1468 |
* Return the number of vertex instances |
| 1469 |
* |
| 1470 |
* @return The number of vertex instances |
| 1471 |
*/ |
| 1472 |
inline size_t GetVertexCount(); |
| 1473 |
|
| 1474 |
/** |
| 1475 |
* Return the number of face instances |
| 1476 |
* |
| 1477 |
* @return The number of face instances |
| 1478 |
*/ |
| 1479 |
inline size_t GetFaceCount(); |
| 1480 |
|
| 1481 |
/** |
| 1482 |
* Return the number of edge instances |
| 1483 |
* |
| 1484 |
* @return The number of edge instances |
| 1485 |
*/ |
| 1486 |
inline size_t GetEdgeCount(); |
| 1487 |
|
| 1488 |
/** |
| 1489 |
* Return a reference to the element with a specific name |
| 1490 |
* |
| 1491 |
* @param name Name of the element. |
| 1492 |
* @return The reference to the element |
| 1493 |
*/ |
| 1494 |
inline PlyElement* GetElement(const std::string & name); |
| 1495 |
|
| 1496 |
/** |
| 1497 |
* Return a reference to the element with a specific element type |
| 1498 |
* |
| 1499 |
* @param e Element type. |
| 1500 |
* @return The reference to the element |
| 1501 |
*/ |
| 1502 |
inline PlyElement* GetElement(PlyElemEntity e); |
| 1503 |
|
| 1504 |
/** |
| 1505 |
* Return a reference to the vertex element |
| 1506 |
* |
| 1507 |
* @return The reference to the vertex element |
| 1508 |
*/ |
| 1509 |
inline PlyElement* GetVertexElement(); |
| 1510 |
|
| 1511 |
/** |
| 1512 |
* Return a reference to the face element |
| 1513 |
* |
| 1514 |
* @return The reference to the face element |
| 1515 |
*/ |
| 1516 |
inline PlyElement* GetFaceElement(); |
| 1517 |
|
| 1518 |
/** |
| 1519 |
* Return a reference to the edge element |
| 1520 |
* |
| 1521 |
* @return The reference to the edge element |
| 1522 |
*/ |
| 1523 |
inline PlyElement* GetEdgeElement(); |
| 1524 |
|
| 1525 |
}; |
| 1526 |
|
| 1527 |
|
| 1528 |
inline Info::Info() |
| 1529 |
{ |
| 1530 |
this->binary = true; |
| 1531 |
this->bigEndian = true; |
| 1532 |
this->Clear(); |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
inline Info::Info(const std::string& filename) |
| 1537 |
{ |
| 1538 |
this->LoadHeader(filename); |
| 1539 |
} |
| 1540 |
|
| 1541 |
|
| 1542 |
inline bool Info::LoadHeader(const std::string& filename) |
| 1543 |
{ |
| 1544 |
this->filename = filename; |
| 1545 |
this->errInfo = NNP_OK; |
| 1546 |
std::ifstream input(filename); |
| 1547 |
if (!input.good()) |
| 1548 |
{ |
| 1549 |
this->errInfo = NNP_UNABLE_TO_OPEN; |
| 1550 |
input.close(); |
| 1551 |
return false; |
| 1552 |
} |
| 1553 |
std::string buffer; |
| 1554 |
std::getline(input, buffer); |
| 1555 |
std::transform(buffer.begin(), buffer.end(), buffer.begin(), ::tolower); |
| 1556 |
if (buffer != "ply") |
| 1557 |
{ |
| 1558 |
this->errInfo = NNP_MISSING_HEADER; |
| 1559 |
input.close(); |
| 1560 |
return false; |
| 1561 |
} |
| 1562 |
std::getline(input, buffer); |
| 1563 |
std::transform(buffer.begin(), buffer.end(), buffer.begin(), ::tolower); |
| 1564 |
std::size_t pos = buffer.find("format"); |
| 1565 |
if (pos == std::string::npos) |
| 1566 |
{ |
| 1567 |
this->errInfo = NNP_MISSING_FORMAT; |
| 1568 |
input.close(); |
| 1569 |
return false; |
| 1570 |
} |
| 1571 |
if (buffer.find("binary_lit") != std::string::npos) |
| 1572 |
{ |
| 1573 |
this->binary = true; |
| 1574 |
this->bigEndian = false; |
| 1575 |
} |
| 1576 |
else if (buffer.find("binary_big") != std::string::npos) |
| 1577 |
{ |
| 1578 |
this->binary = true; |
| 1579 |
this->bigEndian = true; |
| 1580 |
} |
| 1581 |
else if (buffer.find("ascii") != std::string::npos) |
| 1582 |
{ |
| 1583 |
this->binary = false; |
| 1584 |
this->bigEndian = false; |
| 1585 |
} |
| 1586 |
else |
| 1587 |
{ |
| 1588 |
this->errInfo = NNP_MISSING_FORMAT; |
| 1589 |
return false; |
| 1590 |
} |
| 1591 |
std::getline(input, buffer); |
| 1592 |
std::string lowBuffer; |
| 1593 |
lowBuffer.resize(buffer.size()); |
| 1594 |
std::transform(buffer.begin(), buffer.end(), lowBuffer.begin(), ::tolower); |
| 1595 |
while (lowBuffer != "end_header") |
| 1596 |
{ |
| 1597 |
if (lowBuffer.find("element") != std::string::npos) |
| 1598 |
{ |
| 1599 |
std::string elemStr = lowBuffer; |
| 1600 |
std::vector<std::string> propStr; |
| 1601 |
do |
| 1602 |
{ |
| 1603 |
std::getline(input, buffer); |
| 1604 |
lowBuffer.clear(); |
| 1605 |
lowBuffer.resize(buffer.size()); |
| 1606 |
std::transform(buffer.begin(), buffer.end(), lowBuffer.begin(), ::tolower); |
| 1607 |
pos = lowBuffer.find("property"); |
| 1608 |
if (pos != std::string::npos) |
| 1609 |
propStr.push_back(lowBuffer); |
| 1610 |
} while (pos != std::string::npos); |
| 1611 |
PlyElement pe; |
| 1612 |
if (!pe.InitFromHeader(elemStr, propStr)) |
| 1613 |
{ |
| 1614 |
this->errInfo = NNP_INVALID_ELEMENT; |
| 1615 |
input.close(); |
| 1616 |
return false; |
| 1617 |
} |
| 1618 |
elemVec.push_back(pe); |
| 1619 |
} |
| 1620 |
else |
| 1621 |
{ |
| 1622 |
if (lowBuffer.find("comment texture") != std::string::npos) |
| 1623 |
textureFile.push_back(buffer.substr(buffer.find(" ", 10) + 1)); |
| 1624 |
std::getline(input, buffer); |
| 1625 |
lowBuffer.clear(); |
| 1626 |
lowBuffer.resize(buffer.size()); |
| 1627 |
std::transform(buffer.begin(), buffer.end(), lowBuffer.begin(), ::tolower); |
| 1628 |
} |
| 1629 |
} |
| 1630 |
input.close(); |
| 1631 |
return true; |
| 1632 |
} |
| 1633 |
|
| 1634 |
|
| 1635 |
inline bool Info::WriteHeader(PlyFile& file) |
| 1636 |
{ |
| 1637 |
bool ok = true; |
| 1638 |
ok = file.WriteHeaderLine(std::string("ply\n")); |
| 1639 |
if (this->binary) |
| 1640 |
ok = file.WriteHeaderLine(std::string("format binary_little_endian 1.0\n")); |
| 1641 |
else |
| 1642 |
ok = file.WriteHeaderLine(std::string("format ascii 1.0\n")); |
| 1643 |
ok = file.WriteHeaderLine(std::string("comment nanoply generated\n")); |
| 1644 |
for (size_t i = 0; i < this->textureFile.size(); i++) |
| 1645 |
ok = file.WriteHeaderLine(std::string("comment TextureFile ") + this->textureFile[i] + "\n"); |
| 1646 |
for (size_t i = 0; i < this->elemVec.size(); i++) |
| 1647 |
ok = this->elemVec[i].WriteHeader(file); |
| 1648 |
ok = file.WriteHeaderLine(std::string("end_header\n")); |
| 1649 |
return ok; |
| 1650 |
} |
| 1651 |
|
| 1652 |
|
| 1653 |
inline void Info::AddPlyElement(PlyElement& pe) |
| 1654 |
{ |
| 1655 |
elemVec.push_back(pe); |
| 1656 |
} |
| 1657 |
|
| 1658 |
|
| 1659 |
inline size_t Info::GetElementCount(const std::string & name) |
| 1660 |
{ |
| 1661 |
PlyElement* pe = GetElement(name); |
| 1662 |
if (pe != NULL) |
| 1663 |
return pe->cnt; |
| 1664 |
return -1; |
| 1665 |
} |
| 1666 |
|
| 1667 |
|
| 1668 |
inline size_t Info::GetElementCount(PlyElemEntity e) |
| 1669 |
{ |
| 1670 |
PlyElement* pe = GetElement(e); |
| 1671 |
if (pe != NULL) |
| 1672 |
return pe->cnt; |
| 1673 |
return 0; |
| 1674 |
} |
| 1675 |
|
| 1676 |
|
| 1677 |
inline size_t Info::GetVertexCount() |
| 1678 |
{ |
| 1679 |
return GetElementCount(PlyElemEntity::NNP_VERTEX_ELEM); |
| 1680 |
} |
| 1681 |
|
| 1682 |
|
| 1683 |
inline size_t Info::GetFaceCount() |
| 1684 |
{ |
| 1685 |
return GetElementCount(PlyElemEntity::NNP_FACE_ELEM); |
| 1686 |
} |
| 1687 |
|
| 1688 |
|
| 1689 |
inline size_t Info::GetEdgeCount() |
| 1690 |
{ |
| 1691 |
return GetElementCount(PlyElemEntity::NNP_EDGE_ELEM); |
| 1692 |
} |
| 1693 |
|
| 1694 |
|
| 1695 |
inline PlyElement* Info::GetElement(const std::string& name) |
| 1696 |
{ |
| 1697 |
for (size_t i = 0; i < elemVec.size(); i++) |
| 1698 |
{ |
| 1699 |
if (elemVec[i].name == name) |
| 1700 |
return &elemVec[i]; |
| 1701 |
} |
| 1702 |
return NULL; |
| 1703 |
} |
| 1704 |
|
| 1705 |
|
| 1706 |
inline PlyElement* Info::GetElement(PlyElemEntity e) |
| 1707 |
{ |
| 1708 |
for (size_t i = 0; i < elemVec.size(); i++) |
| 1709 |
{ |
| 1710 |
if (elemVec[i].plyElem == e) |
| 1711 |
return &elemVec[i]; |
| 1712 |
} |
| 1713 |
return NULL; |
| 1714 |
} |
| 1715 |
|
| 1716 |
|
| 1717 |
inline PlyElement* Info::GetVertexElement() |
| 1718 |
{ |
| 1719 |
return GetElement(PlyElemEntity::NNP_VERTEX_ELEM); |
| 1720 |
} |
| 1721 |
|
| 1722 |
|
| 1723 |
inline PlyElement* Info::GetFaceElement() |
| 1724 |
{ |
| 1725 |
return GetElement(PlyElemEntity::NNP_FACE_ELEM); |
| 1726 |
} |
| 1727 |
|
| 1728 |
|
| 1729 |
inline PlyElement* Info::GetEdgeElement() |
| 1730 |
{ |
| 1731 |
return GetElement(PlyElemEntity::NNP_EDGE_ELEM); |
| 1732 |
} |
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
/** Abstract class for the descriptor of a Ply propertie. |
| 1737 |
* The class defines how a PlyProperty is saved in memory. |
| 1738 |
*/ |
| 1739 |
class DescriptorInterface |
| 1740 |
{ |
| 1741 |
|
| 1742 |
public: |
| 1743 |
|
| 1744 |
int64_t curPos; /**< Position of the next property to read or to write. */ |
| 1745 |
|
| 1746 |
void *base; /**< Pointer to the memory location that contains the data of the property. */ |
| 1747 |
|
| 1748 |
PlyEntity elem; /**< Ply entity managed by the descriptor. */ |
| 1749 |
|
| 1750 |
std::string name; /**< Name of the PlyProperty*/ |
| 1751 |
|
| 1752 |
/** |
| 1753 |
* Constructor of the descriptor. |
| 1754 |
* |
| 1755 |
* @param _e Ply entity managed by the descriptor. |
| 1756 |
* @param _b Pointer to the memory location that contains the data of the property. |
| 1757 |
*/ |
| 1758 |
inline DescriptorInterface(PlyEntity _e, void *_b) :curPos(0), base(_b), elem(_e), name(PlyPropertyName(_e)[0]){} |
| 1759 |
|
| 1760 |
/** |
| 1761 |
* Constructor of the descriptor. |
| 1762 |
* |
| 1763 |
* @param _s Name of the PlyProperty. |
| 1764 |
* @param _b Pointer to the memory location that contains the data of the property. |
| 1765 |
*/ |
| 1766 |
inline DescriptorInterface(const std::string& _s, void *_b) :curPos(0), base(_b), elem(PlyEntity::NNP_UNKNOWN_ENTITY), name(_s){} |
| 1767 |
|
| 1768 |
/** |
| 1769 |
* Virtual destructor |
| 1770 |
*/ |
| 1771 |
virtual ~DescriptorInterface() {} |
| 1772 |
|
| 1773 |
/** |
| 1774 |
* Restart the descriptor. |
| 1775 |
*/ |
| 1776 |
virtual void Restart() = 0; |
| 1777 |
|
| 1778 |
/** |
| 1779 |
* Read the property data from the binary file. |
| 1780 |
* |
| 1781 |
* @param file Input file. |
| 1782 |
* @param prop PLY property to read from the file. |
| 1783 |
* @param fixEndian If true the method adjust the endianess of the data. |
| 1784 |
* @return If successful returns true. Otherwise, it returns false. |
| 1785 |
*/ |
| 1786 |
virtual bool ReadElemBinary(PlyFile &file, PlyProperty &prop, bool fixEndian) = 0; |
| 1787 |
|
| 1788 |
/** |
| 1789 |
* Read the property data from the ascii file. |
| 1790 |
* |
| 1791 |
* @param file Input file. |
| 1792 |
* @param prop PLY property to read from the file. |
| 1793 |
* @return If successful returns true. Otherwise, it returns false. |
| 1794 |
*/ |
| 1795 |
virtual bool ReadElemAscii(PlyFile &file, PlyProperty &prop) = 0; |
| 1796 |
|
| 1797 |
/** |
| 1798 |
* Write the property data in the binary file. |
| 1799 |
* |
| 1800 |
* @param file Input file. |
| 1801 |
* @param prop PLY property to write in the file. |
| 1802 |
* @param fixEndian If true the method adjust the endianess of the data. |
| 1803 |
* @return If successful returns true. Otherwise, it returns false. |
| 1804 |
*/ |
| 1805 |
virtual bool WriteElemBinary(PlyFile &file, PlyProperty &prop, bool fixEndian) = 0; |
| 1806 |
|
| 1807 |
/** |
| 1808 |
* Write the property data in the ascii file. |
| 1809 |
* |
| 1810 |
* @param file Input file. |
| 1811 |
* @param prop PLY property to write in the file. |
| 1812 |
* @return If successful returns true. Otherwise, it returns false. |
| 1813 |
*/ |
| 1814 |
virtual bool WriteElemAscii(PlyFile &file, PlyProperty &prop) = 0; |
| 1815 |
}; |
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
/** Memory descriptor of a Ply element. |
| 1820 |
* The class defines how a PlyElement is saved in memory. |
| 1821 |
*/ |
| 1822 |
class ElementDescriptor |
| 1823 |
{ |
| 1824 |
public: |
| 1825 |
|
| 1826 |
typedef std::vector<nanoply::DescriptorInterface*> PropertyDescriptor; |
| 1827 |
|
| 1828 |
std::string name; /**< Name of the Ply element. */ |
| 1829 |
PlyElemEntity elem; /**< PLY Element Entity. */ |
| 1830 |
PropertyDescriptor dataDescriptor; /**< Vector of property descriptor. */ |
| 1831 |
|
| 1832 |
/** |
| 1833 |
* Constructor of the Ply element descriptor. |
| 1834 |
* |
| 1835 |
* @param _e Ply Element entity managed by the descriptor. |
| 1836 |
*/ |
| 1837 |
inline ElementDescriptor(PlyElemEntity _e) : name(PlyElementName(_e)[0]), elem(_e){} |
| 1838 |
|
| 1839 |
/** |
| 1840 |
* Constructor of the Ply element descriptor. |
| 1841 |
* |
| 1842 |
* @param _s Name of the Ply element managed by the descriptor. |
| 1843 |
*/ |
| 1844 |
inline ElementDescriptor(const std::string &_s) : name(_s), elem(PlyElemEntity::NNP_UNKNOWN_ELEM){} |
| 1845 |
|
| 1846 |
/** |
| 1847 |
* Read all the properties of the element from the binary file. |
| 1848 |
* |
| 1849 |
* @param file Input file. |
| 1850 |
* @param elem PLY element to read from the file. |
| 1851 |
* @param fixEndian If true the method adjust the endianess of the data. |
| 1852 |
* @return If successful returns true. Otherwise, it returns false. |
| 1853 |
*/ |
| 1854 |
inline bool ReadElemBinary(PlyFile &file, PlyElement &elem, bool fixEndian); |
| 1855 |
|
| 1856 |
/** |
| 1857 |
* Read all the property data of the element from the ascii file. |
| 1858 |
* |
| 1859 |
* @param file Input file. |
| 1860 |
* @param elem PLY element to read from the file. |
| 1861 |
* @return If successful returns true. Otherwise, it returns false. |
| 1862 |
*/ |
| 1863 |
inline bool ReadElemAscii(PlyFile &file, PlyElement &elem); |
| 1864 |
|
| 1865 |
/** |
| 1866 |
* Write all the property data of the element in the binary file. |
| 1867 |
* |
| 1868 |
* @param file Input file. |
| 1869 |
* @param elem PLY element to write from the file. |
| 1870 |
* @param fixEndian If true the method adjust the endianess of the data. |
| 1871 |
* @return If successful returns true. Otherwise, it returns false. |
| 1872 |
*/ |
| 1873 |
inline bool WriteElemBinary(PlyFile &file, PlyElement &elem, bool fixEndian); |
| 1874 |
|
| 1875 |
/** |
| 1876 |
* Write all the property data of the element in the ascii file. |
| 1877 |
* |
| 1878 |
* @param file Input file. |
| 1879 |
* @param elem PLY element to write from the file. |
| 1880 |
* @return If successful returns true. Otherwise, it returns false. |
| 1881 |
*/ |
| 1882 |
inline bool WriteElemAscii(PlyFile &file, PlyElement &elem); |
| 1883 |
|
| 1884 |
/** |
| 1885 |
* Check if the properties defined in input element have a proper data descriport to write in the file. |
| 1886 |
* It sets the variable "validToWrite" for all the Ply properties with a data descriport. |
| 1887 |
* |
| 1888 |
* @param elem PLY element to write from the file. |
| 1889 |
*/ |
| 1890 |
inline void CheckDescriptor(PlyElement &elem); |
| 1891 |
|
| 1892 |
private: |
| 1893 |
|
| 1894 |
inline void ExtractDescriptor(PropertyDescriptor &descr, PlyElement &elem); |
| 1895 |
|
| 1896 |
}; |
| 1897 |
|
| 1898 |
|
| 1899 |
inline void ElementDescriptor::ExtractDescriptor(PropertyDescriptor& descr, PlyElement &elem) |
| 1900 |
{ |
| 1901 |
for (size_t j = 0; j < elem.propVec.size(); j++) |
| 1902 |
{ |
| 1903 |
PlyProperty& prop = elem.propVec[j]; |
| 1904 |
size_t i = 0; |
| 1905 |
for (; i < dataDescriptor.size(); i++) |
| 1906 |
{ |
| 1907 |
if (dataDescriptor[i]->elem == prop.elem) |
| 1908 |
{ |
| 1909 |
if (prop.elem != PlyEntity::NNP_UNKNOWN_ENTITY) |
| 1910 |
{ |
| 1911 |
descr.push_back(dataDescriptor[i]); |
| 1912 |
break; |
| 1913 |
} |
| 1914 |
else //if (dataDescriptor[i]->name == prop.name) |
| 1915 |
{ |
| 1916 |
std::string name1(dataDescriptor[i]->name); |
| 1917 |
std::string name2(prop.name); |
| 1918 |
std::transform(name1.begin(), name1.end(), name1.begin(), ::tolower); |
| 1919 |
std::transform(name2.begin(), name2.end(), name2.begin(), ::tolower); |
| 1920 |
if (name1 == name2) |
| 1921 |
{ |
| 1922 |
descr.push_back(dataDescriptor[i]); |
| 1923 |
break; |
| 1924 |
} |
| 1925 |
} |
| 1926 |
} |
| 1927 |
else if ((dataDescriptor[i]->elem == PlyEntity::NNP_CRGB && prop.elem == PlyEntity::NNP_CRGBA) || |
| 1928 |
(dataDescriptor[i]->elem == PlyEntity::NNP_CRGBA && prop.elem == PlyEntity::NNP_CRGB)) |
| 1929 |
{ |
| 1930 |
descr.push_back(dataDescriptor[i]); |
| 1931 |
break; |
| 1932 |
} |
| 1933 |
} |
| 1934 |
if (i == dataDescriptor.size()) |
| 1935 |
descr.push_back(NULL); |
| 1936 |
} |
| 1937 |
} |
| 1938 |
|
| 1939 |
|
| 1940 |
inline bool ElementDescriptor::ReadElemBinary(PlyFile &file, PlyElement &elem, bool fixEndian) |
| 1941 |
{ |
| 1942 |
PropertyDescriptor descr; |
| 1943 |
ExtractDescriptor(descr, elem); |
| 1944 |
for (size_t i = 0; i < elem.cnt; i++) |
| 1945 |
{ |
| 1946 |
for (size_t j = 0; j < elem.propVec.size(); j++) |
| 1947 |
{ |
| 1948 |
PlyProperty& prop = elem.propVec[j]; |
| 1949 |
if (descr[j] != NULL) |
| 1950 |
(*descr[j]).ReadElemBinary(file, prop, fixEndian); |
| 1951 |
else |
| 1952 |
prop.SkipBinaryPropertyInFile(file); |
| 1953 |
} |
| 1954 |
} |
| 1955 |
return true; |
| 1956 |
} |
| 1957 |
|
| 1958 |
|
| 1959 |
inline bool ElementDescriptor::ReadElemAscii(PlyFile &file, PlyElement &elem) |
| 1960 |
{ |
| 1961 |
PropertyDescriptor descr; |
| 1962 |
ExtractDescriptor(descr, elem); |
| 1963 |
for (size_t i = 0; i < elem.cnt; i++) |
| 1964 |
{ |
| 1965 |
for (size_t j = 0; j < elem.propVec.size(); j++) |
| 1966 |
{ |
| 1967 |
PlyProperty& prop = elem.propVec[j]; |
| 1968 |
if (descr[j] != NULL) |
| 1969 |
(*descr[j]).ReadElemAscii(file, prop); |
| 1970 |
else |
| 1971 |
prop.SkipAsciiPropertyInFile(file); |
| 1972 |
} |
| 1973 |
} |
| 1974 |
return true; |
| 1975 |
} |
| 1976 |
|
| 1977 |
inline bool ElementDescriptor::WriteElemBinary(PlyFile &file, PlyElement &elem, bool fixEndian) |
| 1978 |
{ |
| 1979 |
PropertyDescriptor descr; |
| 1980 |
ExtractDescriptor(descr, elem); |
| 1981 |
for (size_t i = 0; i < elem.cnt; i++) |
| 1982 |
{ |
| 1983 |
for (size_t j = 0; j < elem.propVec.size(); j++) |
| 1984 |
{ |
| 1985 |
if (descr[j] != NULL) |
| 1986 |
(*descr[j]).WriteElemBinary(file, elem.propVec[j], fixEndian); |
| 1987 |
} |
| 1988 |
} |
| 1989 |
return true; |
| 1990 |
} |
| 1991 |
|
| 1992 |
inline bool ElementDescriptor::WriteElemAscii(PlyFile &file, PlyElement &elem) |
| 1993 |
{ |
| 1994 |
PropertyDescriptor descr; |
| 1995 |
ExtractDescriptor(descr, elem); |
| 1996 |
for (size_t i = 0; i < elem.cnt; i++) |
| 1997 |
{ |
| 1998 |
bool first = true; |
| 1999 |
for (size_t j = 0; j < elem.propVec.size(); j++) |
| 2000 |
{ |
| 2001 |
if (descr[j] != NULL) |
| 2002 |
{ |
| 2003 |
if (!first) |
| 2004 |
file.WriteAsciiData(std::string(" ")); |
| 2005 |
else |
| 2006 |
first = false; |
| 2007 |
(*descr[j]).WriteElemAscii(file, elem.propVec[j]); |
| 2008 |
} |
| 2009 |
} |
| 2010 |
file.WriteAsciiData(std::string("\n")); |
| 2011 |
} |
| 2012 |
return true; |
| 2013 |
} |
| 2014 |
|
| 2015 |
|
| 2016 |
inline void ElementDescriptor::CheckDescriptor(PlyElement &elem) |
| 2017 |
{ |
| 2018 |
if (elem.propVec.size() == 0) |
| 2019 |
{ |
| 2020 |
elem.validToWrite = false; |
| 2021 |
return; |
| 2022 |
} |
| 2023 |
elem.validToWrite = true; |
| 2024 |
PropertyDescriptor descr; |
| 2025 |
ExtractDescriptor(descr, elem); |
| 2026 |
for (size_t j = 0; j < elem.propVec.size(); j++) |
| 2027 |
{ |
| 2028 |
if (descr[j] != NULL) |
| 2029 |
elem.propVec[j].validToWrite = true; |
| 2030 |
} |
| 2031 |
} |
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
template<class ContainerType, int VectorSize, typename ScalarType> |
| 2036 |
class DescriptorHelper |
| 2037 |
{ |
| 2038 |
|
| 2039 |
public: |
| 2040 |
|
| 2041 |
template<typename C> |
| 2042 |
static void ReadBinary(DescriptorInterface& descr, PlyFile &file, PlyProperty &prop, bool fixEndian) |
| 2043 |
{ |
| 2044 |
char * buffer = nullptr; |
| 2045 |
int size; |
| 2046 |
int count = prop.CountValue(); |
| 2047 |
int typeSize = prop.TypeSize(); |
| 2048 |
if (prop.type >= NNP_LIST_UINT8_UINT32) |
| 2049 |
{ |
| 2050 |
file.ReadBinaryData(buffer, sizeof(char)); |
| 2051 |
const int cntList = int(*(reinterpret_cast<unsigned char *>(buffer))); |
| 2052 |
size = typeSize * cntList; |
| 2053 |
count = cntList; |
| 2054 |
} |
| 2055 |
else |
| 2056 |
size = typeSize * count; |
| 2057 |
file.ReadBinaryData(buffer, size); |
| 2058 |
|
| 2059 |
if (typeSize > 1 && fixEndian) |
| 2060 |
adjustEndianess(reinterpret_cast<unsigned char *>(buffer), typeSize, count); |
| 2061 |
|
| 2062 |
unsigned char* baseProp = (unsigned char*)descr.base + descr.curPos * sizeof(ContainerType); |
| 2063 |
C* temp = (C*)buffer; |
| 2064 |
if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA)) |
| 2065 |
{ |
| 2066 |
float norm = 1.0f; |
| 2067 |
if (std::is_same<ScalarType, float>::value && std::is_same<C, unsigned char>::value) |
| 2068 |
norm = 1.0f / 255.0f; |
| 2069 |
else if (std::is_same<ScalarType, unsigned char>::value && std::is_same<C, float>::value) |
| 2070 |
norm = 255.0f; |
| 2071 |
for (int i = 0; i < std::min(VectorSize, count); i++) |
| 2072 |
*((ScalarType *)(baseProp + i * sizeof(ScalarType))) = ScalarType(temp[i] * norm); |
| 2073 |
} |
| 2074 |
else |
| 2075 |
{ |
| 2076 |
for (int i = 0; i < std::min(VectorSize, count); i++) |
| 2077 |
*((ScalarType *)(baseProp + i * sizeof(ScalarType))) = ScalarType(temp[i]); |
| 2078 |
} |
| 2079 |
++(descr.curPos); |
| 2080 |
} |
| 2081 |
|
| 2082 |
template<typename C> |
| 2083 |
static void ReadAscii(DescriptorInterface& descr, PlyFile &file, PlyProperty &prop) |
| 2084 |
{ |
| 2085 |
int count = prop.CountValue(); |
| 2086 |
if (prop.type >= NNP_LIST_UINT8_UINT32) |
| 2087 |
file.ReadAsciiData(count); |
| 2088 |
|
| 2089 |
C* temp = new C[count]; |
| 2090 |
for (int i = 0; i < count; i++) |
| 2091 |
file.ReadAsciiData(temp[i]); |
| 2092 |
|
| 2093 |
unsigned char* baseProp = (unsigned char*)descr.base + descr.curPos * sizeof(ContainerType); |
| 2094 |
if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA)) |
| 2095 |
{ |
| 2096 |
float norm = 1.0f; |
| 2097 |
if (std::is_same<ScalarType, float>::value && prop.type == NNP_UINT8) |
| 2098 |
norm = 1.0f / 255.0f; |
| 2099 |
else if (std::is_same<ScalarType, unsigned char>::value && prop.type == NNP_FLOAT32) |
| 2100 |
norm = 255.0f; |
| 2101 |
for (int i = 0; i < std::min(VectorSize, count); i++) |
| 2102 |
*((ScalarType *)(baseProp + i * sizeof(ScalarType))) = ScalarType(temp[i] * norm); |
| 2103 |
} |
| 2104 |
else |
| 2105 |
{ |
| 2106 |
for (int i = 0; i < std::min(VectorSize, count); i++) |
| 2107 |
*((ScalarType *)(baseProp + i * sizeof(ScalarType))) = ScalarType(temp[i]); |
| 2108 |
} |
| 2109 |
delete[] temp; |
| 2110 |
++(descr.curPos); |
| 2111 |
} |
| 2112 |
|
| 2113 |
template<typename C> |
| 2114 |
static void WriteBinary(DescriptorInterface& descr, PlyFile &file, PlyProperty &prop, bool fixEndian) |
| 2115 |
{ |
| 2116 |
(void)fixEndian; |
| 2117 |
int count = prop.CountValue(); |
| 2118 |
C data[VectorSize]; |
| 2119 |
if (prop.type >= NNP_LIST_UINT8_UINT32) |
| 2120 |
{ |
| 2121 |
if (prop.IsSigned()) |
| 2122 |
{ |
| 2123 |
char listSize = (char)VectorSize; |
| 2124 |
file.WriteBinaryData(&listSize, 1); |
| 2125 |
count = VectorSize; |
| 2126 |
} |
| 2127 |
else |
| 2128 |
{ |
| 2129 |
unsigned char listSize = (unsigned char)VectorSize; |
| 2130 |
file.WriteBinaryData(&listSize, 1); |
| 2131 |
count = VectorSize; |
| 2132 |
} |
| 2133 |
} |
| 2134 |
|
| 2135 |
C temp = 0; |
| 2136 |
unsigned char* baseProp = (unsigned char*)descr.base + descr.curPos * sizeof(ContainerType); |
| 2137 |
if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA)) |
| 2138 |
{ |
| 2139 |
float norm = 1.0f; |
| 2140 |
if (std::is_same<ScalarType, float>::value && std::is_same<C, unsigned char>::value) |
| 2141 |
norm = 255.0f; |
| 2142 |
else if (std::is_same<ScalarType, unsigned char>::value && std::is_same<C, float>::value) |
| 2143 |
norm = 1.0f / 255.0f; |
| 2144 |
for (int i = 0; i < std::min(VectorSize, count); i++) |
| 2145 |
data[i] = (C)((*(ScalarType*)(baseProp + i * sizeof(ScalarType))) * norm); |
| 2146 |
} |
| 2147 |
else |
| 2148 |
{ |
| 2149 |
for (int i = 0; i < std::min(VectorSize, count); i++) |
| 2150 |
data[i] = (C)((*(ScalarType*)(baseProp + i * sizeof(ScalarType)))); |
| 2151 |
} |
| 2152 |
|
| 2153 |
if (sizeof(C) > 1 && fixEndian) |
| 2154 |
adjustEndianess((unsigned char*)data, sizeof(C), std::min(VectorSize, count)); |
| 2155 |
|
| 2156 |
file.WriteBinaryData(data, sizeof(C)*std::min(VectorSize, count)); |
| 2157 |
for (int i = 0; i < (count - VectorSize); i++) |
| 2158 |
file.WriteBinaryData(&temp, sizeof(C)); |
| 2159 |
++(descr.curPos); |
| 2160 |
} |
| 2161 |
|
| 2162 |
template<typename C> |
| 2163 |
static void WriteAscii(DescriptorInterface& descr, PlyFile &file, PlyProperty &prop) |
| 2164 |
{ |
| 2165 |
int count = prop.CountValue(); |
| 2166 |
if (prop.type >= NNP_LIST_UINT8_UINT32) |
| 2167 |
{ |
| 2168 |
if (prop.IsSigned()) |
| 2169 |
{ |
| 2170 |
int listSize = (int)VectorSize; |
| 2171 |
file.WriteAsciiData(listSize); |
| 2172 |
count = VectorSize; |
| 2173 |
} |
| 2174 |
else |
| 2175 |
{ |
| 2176 |
unsigned int listSize = (unsigned int)VectorSize; |
| 2177 |
file.WriteAsciiData(listSize); |
| 2178 |
count = VectorSize; |
| 2179 |
} |
| 2180 |
file.WriteAsciiData(std::string(" ")); |
| 2181 |
} |
| 2182 |
|
| 2183 |
C data[VectorSize]; |
| 2184 |
unsigned char* baseProp = (unsigned char*)descr.base + descr.curPos * sizeof(ContainerType); |
| 2185 |
if ((prop.elem == NNP_CRGB || prop.elem == NNP_CRGBA)) |
| 2186 |
{ |
| 2187 |
float norm = 1.0; |
| 2188 |
if (std::is_same<ScalarType, float>::value && prop.type == NNP_UINT8) |
| 2189 |
norm = 255.0f; |
| 2190 |
else if (std::is_same<ScalarType, unsigned char>::value && prop.type == NNP_FLOAT32) |
| 2191 |
norm = 1.0f / 255.0f; |
| 2192 |
for (int i = 0; i < std::min(VectorSize, count); i++) |
| 2193 |
data[i] = (C)((*(ScalarType*)(baseProp + i * sizeof(ScalarType))) * norm); |
| 2194 |
} |
| 2195 |
else |
| 2196 |
{ |
| 2197 |
for (int i = 0; i < std::min(VectorSize, count); i++) |
| 2198 |
data[i] = (C)((*(ScalarType*)(baseProp + i * sizeof(ScalarType)))); |
| 2199 |
} |
| 2200 |
|
| 2201 |
|
| 2202 |
for (int i = 0; i < (count - VectorSize); i++) |
| 2203 |
data[i] = 0; |
| 2204 |
|
| 2205 |
for (int i = 0; i < count; i++) |
| 2206 |
{ |
| 2207 |
file.WriteAsciiData(data[i]); |
| 2208 |
if (i < count - 1) |
| 2209 |
file.WriteAsciiData(std::string(" ")); |
| 2210 |
} |
| 2211 |
++(descr.curPos); |
| 2212 |
} |
| 2213 |
}; |
| 2214 |
|
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
template<class ContainerType, typename ScalarType> |
| 2219 |
class DescriptorHelper<ContainerType, 0, ScalarType> |
| 2220 |
{ |
| 2221 |
|
| 2222 |
public: |
| 2223 |
|
| 2224 |
template<typename C> |
| 2225 |
static void ReadBinary(DescriptorInterface& descr, PlyFile &file, PlyProperty &prop, bool fixEndian) |
| 2226 |
{ |
| 2227 |
char * buffer = nullptr; |
| 2228 |
int size; |
| 2229 |
int count = prop.CountValue(); |
| 2230 |
int typeSize = prop.TypeSize(); |
| 2231 |
if (prop.type >= NNP_LIST_UINT8_UINT32) |
| 2232 |
{ |
| 2233 |
file.ReadBinaryData(buffer, sizeof(char)); |
| 2234 |
const int cntList = int(*(reinterpret_cast<unsigned char *>(buffer))); |
| 2235 |
size = typeSize * cntList; |
| 2236 |
count = cntList; |
| 2237 |
} |
| 2238 |
else |
| 2239 |
size = typeSize * count; |
| 2240 |
file.ReadBinaryData(buffer, size); |
| 2241 |
|
| 2242 |
if (typeSize > 1 && fixEndian) |
| 2243 |
adjustEndianess(reinterpret_cast<unsigned char *>(buffer), typeSize, count); |
| 2244 |
|
| 2245 |
C* temp = (C*)buffer; |
| 2246 |
|
| 2247 |
ContainerType* container = (ContainerType*)((unsigned char*)descr.base + descr.curPos * sizeof(ContainerType)); |
| 2248 |
(*container).resize(count); |
| 2249 |
for (int i = 0; i < count; i++) |
| 2250 |
(*container)[i] = (ScalarType)(temp[i]); |
| 2251 |
++(descr.curPos); |
| 2252 |
} |
| 2253 |
|
| 2254 |
template<typename C> |
| 2255 |
static void ReadAscii(DescriptorInterface& descr, PlyFile &file, PlyProperty &prop) |
| 2256 |
{ |
| 2257 |
int count = prop.CountValue(); |
| 2258 |
if (prop.type >= NNP_LIST_UINT8_UINT32) |
| 2259 |
file.ReadAsciiData(count); |
| 2260 |
|
| 2261 |
C* temp = new C[count]; |
| 2262 |
for (int i = 0; i < count; i++) |
| 2263 |
file.ReadAsciiData(temp[i]); |
| 2264 |
|
| 2265 |
ContainerType* container = (ContainerType*)((unsigned char*)descr.base + descr.curPos * sizeof(ContainerType)); |
| 2266 |
(*container).resize(count); |
| 2267 |
for (int i = 0; i < count; i++) |
| 2268 |
(*container)[i] = (ScalarType)(temp[i]); |
| 2269 |
delete[] temp; |
| 2270 |
++(descr.curPos); |
| 2271 |
} |
| 2272 |
|
| 2273 |
template<typename C> |
| 2274 |
static void WriteBinary(DescriptorInterface& descr, PlyFile &file, PlyProperty &prop, bool fixEndian) |
| 2275 |
{ |
| 2276 |
size_t count = prop.CountValue(); |
| 2277 |
ContainerType* list = (ContainerType*)((unsigned char*)descr.base + descr.curPos * sizeof(ContainerType)); |
| 2278 |
if (prop.type >= NNP_LIST_UINT8_UINT32) |
| 2279 |
{ |
| 2280 |
if (prop.IsSigned()) |
| 2281 |
{ |
| 2282 |
char listSize = (char)list->size(); |
| 2283 |
file.WriteBinaryData(&listSize, 1); |
| 2284 |
count = list->size(); |
| 2285 |
} |
| 2286 |
else |
| 2287 |
{ |
| 2288 |
unsigned char listSize = (unsigned char)list->size(); |
| 2289 |
file.WriteBinaryData(&listSize, 1); |
| 2290 |
count = list->size(); |
| 2291 |
} |
| 2292 |
} |
| 2293 |
|
| 2294 |
std::vector<C> data(count); |
| 2295 |
|
| 2296 |
for (size_t i = 0; i < std::min(count, list->size()); i++) |
| 2297 |
data[i] = (C)((*list)[i]); |
| 2298 |
|
| 2299 |
if (sizeof(C) > 1 && fixEndian) |
| 2300 |
adjustEndianess((unsigned char*)data.data(), sizeof(C), std::min(count, list->size())); |
| 2301 |
|
| 2302 |
file.WriteBinaryData(data.data(), sizeof(C)*std::min(count, list->size())); |
| 2303 |
C temp = 0; |
| 2304 |
for (size_t i = 0; i < (count - list->size()); i++) |
| 2305 |
file.WriteBinaryData(&temp, sizeof(C)); |
| 2306 |
++(descr.curPos); |
| 2307 |
} |
| 2308 |
|
| 2309 |
template<typename C> |
| 2310 |
static void WriteAscii(DescriptorInterface& descr, PlyFile &file, PlyProperty &prop) |
| 2311 |
{ |
| 2312 |
size_t count = prop.CountValue(); |
| 2313 |
ContainerType* list = (ContainerType*)((unsigned char*)descr.base + descr.curPos * sizeof(ContainerType)); |
| 2314 |
if (prop.type >= NNP_LIST_UINT8_UINT32) |
| 2315 |
{ |
| 2316 |
if (prop.IsSigned()) |
| 2317 |
{ |
| 2318 |
int listSize = (int)list->size(); |
| 2319 |
file.WriteAsciiData(listSize); |
| 2320 |
count = list->size(); |
| 2321 |
} |
| 2322 |
else |
| 2323 |
{ |
| 2324 |
unsigned int listSize = (unsigned int)list->size(); |
| 2325 |
file.WriteAsciiData(listSize); |
| 2326 |
count = list->size(); |
| 2327 |
} |
| 2328 |
file.WriteAsciiData(std::string(" ")); |
| 2329 |
} |
| 2330 |
|
| 2331 |
std::vector<C> data(count); |
| 2332 |
for (size_t i = 0; i < std::min(count, list->size()); i++) |
| 2333 |
data[i] = (C)((*list)[i]); |
| 2334 |
|
| 2335 |
for (size_t i = 0; i < (count - list->size()); i++) |
| 2336 |
data[i] = 0; |
| 2337 |
|
| 2338 |
for (size_t i = 0; i < count; i++) |
| 2339 |
{ |
| 2340 |
file.WriteAsciiData(data[i]); |
| 2341 |
if (i < count - 1) |
| 2342 |
file.WriteAsciiData(std::string(" ")); |
| 2343 |
} |
| 2344 |
++(descr.curPos); |
| 2345 |
} |
| 2346 |
}; |
| 2347 |
|
| 2348 |
|
| 2349 |
|
| 2350 |
|
| 2351 |
/** Memory descriptor of a vector of properties. |
| 2352 |
* The class defines how a vector of PlyProperty is saved in memory. |
| 2353 |
* |
| 2354 |
* @tparam ContainerType Type of the container of the property |
| 2355 |
* @tparam VectorSize Number of values stored in the property. |
| 2356 |
* @tparam ScalarType Type of the values stored in the property. |
| 2357 |
*/ |
| 2358 |
template<class ContainerType, int VectorSize, typename ScalarType> |
| 2359 |
class DataDescriptor : public DescriptorInterface |
| 2360 |
{ |
| 2361 |
public: |
| 2362 |
|
| 2363 |
DescriptorHelper<ContainerType, VectorSize, ScalarType> helper; |
| 2364 |
|
| 2365 |
inline DataDescriptor(); |
| 2366 |
|
| 2367 |
/** |
| 2368 |
* Constructor of the descriptor. |
| 2369 |
* |
| 2370 |
* @param _e Ply entity managed by the descriptor. |
| 2371 |
* @param _b Pointer to the memory location that contains the data of the property. |
| 2372 |
*/ |
| 2373 |
inline DataDescriptor(PlyEntity _e, void *_b) :DescriptorInterface(_e, _b){}; |
| 2374 |
|
| 2375 |
/** |
| 2376 |
* Constructor of the descriptor. |
| 2377 |
* |
| 2378 |
* @param _s Name of the PlyProperty. |
| 2379 |
* @param _b Pointer to the memory location that contains the data of the property. |
| 2380 |
*/ |
| 2381 |
inline DataDescriptor(const std::string& _s, void *_b) :DescriptorInterface(_s, _b){}; |
| 2382 |
|
| 2383 |
inline void Restart(); |
| 2384 |
|
| 2385 |
inline bool ReadElemBinary(PlyFile &file, PlyProperty &prop, bool fixEndian); |
| 2386 |
|
| 2387 |
inline bool ReadElemAscii(PlyFile &file, PlyProperty &prop); |
| 2388 |
|
| 2389 |
inline bool WriteElemBinary(PlyFile &file, PlyProperty &prop, bool fixEndian); |
| 2390 |
|
| 2391 |
inline bool WriteElemAscii(PlyFile &file, PlyProperty &prop); |
| 2392 |
}; |
| 2393 |
|
| 2394 |
|
| 2395 |
template<class ContainerType, int VectorSize, typename ScalarType> |
| 2396 |
void DataDescriptor<ContainerType, VectorSize, ScalarType>::Restart() |
| 2397 |
{ |
| 2398 |
this->curPos = 0; |
| 2399 |
} |
| 2400 |
|
| 2401 |
|
| 2402 |
template<class ContainerType, int VectorSize, typename ScalarType> |
| 2403 |
bool DataDescriptor<ContainerType, VectorSize, ScalarType>::ReadElemBinary(PlyFile &file, PlyProperty &prop, bool fixEndian) |
| 2404 |
{ |
| 2405 |
if (prop.elem != elem && ((prop.elem != PlyEntity::NNP_CRGB && prop.elem != PlyEntity::NNP_CRGBA) || (prop.elem != PlyEntity::NNP_CRGBA && prop.elem != PlyEntity::NNP_CRGB))) |
| 2406 |
return false; |
| 2407 |
switch (prop.type) |
| 2408 |
{ |
| 2409 |
case NNP_LIST_INT8_INT8: |
| 2410 |
case NNP_LIST_UINT8_INT8: |
| 2411 |
case NNP_INT8: |
| 2412 |
helper.template ReadBinary<char>(*this, file, prop, fixEndian); break; |
| 2413 |
case NNP_LIST_INT8_UINT8: |
| 2414 |
case NNP_LIST_UINT8_UINT8: |
| 2415 |
case NNP_UINT8: |
| 2416 |
helper.template ReadBinary<unsigned char>(*this, file, prop, fixEndian); break; |
| 2417 |
case NNP_LIST_INT8_INT16: |
| 2418 |
case NNP_LIST_UINT8_INT16: |
| 2419 |
case NNP_INT16: |
| 2420 |
helper.template ReadBinary<short>(*this, file, prop, fixEndian); break; |
| 2421 |
case NNP_LIST_INT8_UINT16: |
| 2422 |
case NNP_LIST_UINT8_UINT16: |
| 2423 |
case NNP_UINT16: |
| 2424 |
helper.template ReadBinary<unsigned short>(*this, file, prop, fixEndian); break; |
| 2425 |
case NNP_LIST_INT8_FLOAT32: |
| 2426 |
case NNP_LIST_UINT8_FLOAT32: |
| 2427 |
case NNP_FLOAT32: |
| 2428 |
helper.template ReadBinary<float>(*this, file, prop, fixEndian); break; |
| 2429 |
case NNP_LIST_UINT8_INT32: |
| 2430 |
case NNP_LIST_INT8_INT32: |
| 2431 |
case NNP_INT32: |
| 2432 |
helper.template ReadBinary<int>(*this, file, prop, fixEndian); break; |
| 2433 |
case NNP_LIST_UINT8_UINT32: |
| 2434 |
case NNP_LIST_INT8_UINT32: |
| 2435 |
case NNP_UINT32: |
| 2436 |
helper.template ReadBinary<unsigned int>(*this, file, prop, fixEndian); break; |
| 2437 |
case NNP_LIST_INT8_FLOAT64: |
| 2438 |
case NNP_LIST_UINT8_FLOAT64: |
| 2439 |
case NNP_FLOAT64: |
| 2440 |
helper.template ReadBinary<double>(*this, file, prop, fixEndian); break; |
| 2441 |
case NNP_UNKNOWN_TYPE: |
| 2442 |
break; |
| 2443 |
} |
| 2444 |
return true; |
| 2445 |
} |
| 2446 |
|
| 2447 |
|
| 2448 |
template<class ContainerType, int VectorSize, typename ScalarType> |
| 2449 |
bool DataDescriptor<ContainerType, VectorSize, ScalarType>::ReadElemAscii(PlyFile &file, PlyProperty &prop) |
| 2450 |
{ |
| 2451 |
if (prop.elem != elem) |
| 2452 |
return false; |
| 2453 |
switch (prop.type) |
| 2454 |
{ |
| 2455 |
case NNP_LIST_UINT8_INT8: |
| 2456 |
case NNP_LIST_INT8_INT8: |
| 2457 |
case NNP_INT8: |
| 2458 |
helper.template ReadAscii<int>(*this, file, prop); break; |
| 2459 |
case NNP_LIST_UINT8_UINT8: |
| 2460 |
case NNP_LIST_INT8_UINT8: |
| 2461 |
case NNP_UINT8: |
| 2462 |
helper.template ReadAscii<unsigned int>(*this, file, prop); break; |
| 2463 |
case NNP_LIST_UINT8_INT16: |
| 2464 |
case NNP_LIST_INT8_INT16: |
| 2465 |
case NNP_INT16: |
| 2466 |
helper.template ReadAscii<short>(*this, file, prop); break; |
| 2467 |
case NNP_LIST_UINT8_UINT16: |
| 2468 |
case NNP_LIST_INT8_UINT16: |
| 2469 |
case NNP_UINT16: |
| 2470 |
helper.template ReadAscii<unsigned short>(*this, file, prop); break; |
| 2471 |
case NNP_LIST_UINT8_FLOAT32: |
| 2472 |
case NNP_LIST_INT8_FLOAT32: |
| 2473 |
case NNP_FLOAT32: |
| 2474 |
helper.template ReadAscii<float>(*this, file, prop); break; |
| 2475 |
case NNP_LIST_UINT8_INT32: |
| 2476 |
case NNP_LIST_INT8_INT32: |
| 2477 |
case NNP_INT32: |
| 2478 |
helper.template ReadAscii<int>(*this, file, prop); break; |
| 2479 |
case NNP_LIST_UINT8_UINT32: |
| 2480 |
case NNP_LIST_INT8_UINT32: |
| 2481 |
case NNP_UINT32: |
| 2482 |
helper.template ReadAscii<unsigned int>(*this, file, prop); break; |
| 2483 |
case NNP_LIST_UINT8_FLOAT64: |
| 2484 |
case NNP_LIST_INT8_FLOAT64: |
| 2485 |
case NNP_FLOAT64: |
| 2486 |
helper.template ReadAscii<double>(*this, file, prop); break; |
| 2487 |
case NNP_UNKNOWN_TYPE: |
| 2488 |
break; |
| 2489 |
} |
| 2490 |
return true; |
| 2491 |
} |
| 2492 |
|
| 2493 |
|
| 2494 |
template<class ContainerType, int VectorSize, typename ScalarType> |
| 2495 |
bool DataDescriptor<ContainerType, VectorSize, ScalarType>::WriteElemBinary(PlyFile &file, PlyProperty &prop, bool fixEndian) |
| 2496 |
{ |
| 2497 |
if (prop.elem != elem) |
| 2498 |
return false; |
| 2499 |
switch (prop.type) |
| 2500 |
{ |
| 2501 |
case NNP_LIST_INT8_INT8: |
| 2502 |
case NNP_LIST_UINT8_INT8: |
| 2503 |
case NNP_INT8: |
| 2504 |
helper.template WriteBinary<char>(*this, file, prop, fixEndian); break; |
| 2505 |
case NNP_LIST_INT8_UINT8: |
| 2506 |
case NNP_LIST_UINT8_UINT8: |
| 2507 |
case NNP_UINT8: |
| 2508 |
helper.template WriteBinary<unsigned char>(*this, file, prop, fixEndian); break; |
| 2509 |
case NNP_LIST_INT8_INT16: |
| 2510 |
case NNP_LIST_UINT8_INT16: |
| 2511 |
case NNP_INT16: |
| 2512 |
helper.template WriteBinary<short>(*this, file, prop, fixEndian); break; |
| 2513 |
case NNP_LIST_INT8_UINT16: |
| 2514 |
case NNP_LIST_UINT8_UINT16: |
| 2515 |
case NNP_UINT16: |
| 2516 |
helper.template WriteBinary<unsigned short>(*this, file, prop, fixEndian); break; |
| 2517 |
case NNP_LIST_INT8_FLOAT32: |
| 2518 |
case NNP_LIST_UINT8_FLOAT32: |
| 2519 |
case NNP_FLOAT32: |
| 2520 |
helper.template WriteBinary<float>(*this, file, prop, fixEndian); break; |
| 2521 |
case NNP_LIST_UINT8_INT32: |
| 2522 |
case NNP_LIST_INT8_INT32: |
| 2523 |
case NNP_INT32: |
| 2524 |
helper.template WriteBinary<int>(*this, file, prop, fixEndian); break; |
| 2525 |
case NNP_LIST_UINT8_UINT32: |
| 2526 |
case NNP_LIST_INT8_UINT32: |
| 2527 |
case NNP_UINT32: |
| 2528 |
helper.template WriteBinary<unsigned int>(*this, file, prop, fixEndian); break; |
| 2529 |
case NNP_LIST_INT8_FLOAT64: |
| 2530 |
case NNP_LIST_UINT8_FLOAT64: |
| 2531 |
case NNP_FLOAT64: |
| 2532 |
helper.template WriteBinary<double>(*this, file, prop, fixEndian); break; |
| 2533 |
case NNP_UNKNOWN_TYPE: |
| 2534 |
break; |
| 2535 |
} |
| 2536 |
return true; |
| 2537 |
} |
| 2538 |
|
| 2539 |
|
| 2540 |
template<class ContainerType, int VectorSize, typename ScalarType> |
| 2541 |
bool DataDescriptor<ContainerType, VectorSize, ScalarType>::WriteElemAscii(PlyFile &file, PlyProperty& prop) |
| 2542 |
{ |
| 2543 |
if (prop.elem != elem) |
| 2544 |
return false; |
| 2545 |
if (prop.elem == PlyEntity::NNP_UNKNOWN_ENTITY && prop.name != name) |
| 2546 |
return false; |
| 2547 |
switch (prop.type) |
| 2548 |
{ |
| 2549 |
case NNP_LIST_UINT8_INT8: |
| 2550 |
case NNP_LIST_INT8_INT8: |
| 2551 |
case NNP_INT8: |
| 2552 |
helper.template WriteAscii<int>(*this, file, prop); break; |
| 2553 |
case NNP_LIST_UINT8_UINT8: |
| 2554 |
case NNP_LIST_INT8_UINT8: |
| 2555 |
case NNP_UINT8: |
| 2556 |
helper.template WriteAscii<unsigned int>(*this, file, prop); break; |
| 2557 |
case NNP_LIST_UINT8_INT16: |
| 2558 |
case NNP_LIST_INT8_INT16: |
| 2559 |
case NNP_INT16: |
| 2560 |
helper.template WriteAscii<short>(*this, file, prop); break; |
| 2561 |
case NNP_LIST_UINT8_UINT16: |
| 2562 |
case NNP_LIST_INT8_UINT16: |
| 2563 |
case NNP_UINT16: |
| 2564 |
helper.template WriteAscii<unsigned short>(*this, file, prop); break; |
| 2565 |
case NNP_LIST_UINT8_FLOAT32: |
| 2566 |
case NNP_LIST_INT8_FLOAT32: |
| 2567 |
case NNP_FLOAT32: |
| 2568 |
helper.template WriteAscii<float>(*this, file, prop); break; |
| 2569 |
case NNP_LIST_UINT8_INT32: |
| 2570 |
case NNP_LIST_INT8_INT32: |
| 2571 |
case NNP_INT32: |
| 2572 |
helper.template WriteAscii<int>(*this, file, prop); break; |
| 2573 |
case NNP_LIST_UINT8_UINT32: |
| 2574 |
case NNP_LIST_INT8_UINT32: |
| 2575 |
case NNP_UINT32: |
| 2576 |
helper.template WriteAscii<unsigned int>(*this, file, prop); break; |
| 2577 |
case NNP_LIST_UINT8_FLOAT64: |
| 2578 |
case NNP_LIST_INT8_FLOAT64: |
| 2579 |
case NNP_FLOAT64: |
| 2580 |
helper.template WriteAscii<double>(*this, file, prop); break; |
| 2581 |
case NNP_UNKNOWN_TYPE: |
| 2582 |
break; |
| 2583 |
} |
| 2584 |
return true; |
| 2585 |
} |
| 2586 |
|
| 2587 |
|
| 2588 |
|
| 2589 |
template <size_t ActionType> |
| 2590 |
inline bool ElemProcessing(ElementDescriptor& elemDescr, PlyElement &elem, PlyFile& file, bool fixEndian) |
| 2591 |
{ |
| 2592 |
if ((elemDescr.elem != PlyElemEntity::NNP_UNKNOWN_ELEM && elemDescr.elem == elem.plyElem) || |
| 2593 |
(elemDescr.elem == PlyElemEntity::NNP_UNKNOWN_ELEM && elemDescr.name == elem.name)) |
| 2594 |
{ |
| 2595 |
if (ActionType == 0) |
| 2596 |
elemDescr.ReadElemBinary(file, elem, fixEndian); |
| 2597 |
else if (ActionType == 1) |
| 2598 |
elemDescr.ReadElemAscii(file, elem); |
| 2599 |
else if (ActionType == 2) |
| 2600 |
elemDescr.WriteElemBinary(file, elem, fixEndian); |
| 2601 |
else if (ActionType == 3) |
| 2602 |
elemDescr.WriteElemAscii(file, elem); |
| 2603 |
else if (ActionType == 4) |
| 2604 |
elemDescr.CheckDescriptor(elem); |
| 2605 |
return true; |
| 2606 |
} |
| 2607 |
return false; |
| 2608 |
} |
| 2609 |
|
| 2610 |
typedef std::vector<ElementDescriptor*> MeshDescriptor; |
| 2611 |
|
| 2612 |
/** |
| 2613 |
* Load a 3D model from a PLY file. |
| 2614 |
* |
| 2615 |
* @param meshElements Vector that defines how to manage the ply element data in memory. |
| 2616 |
* @param info Info of the file to load. |
| 2617 |
*/ |
| 2618 |
inline bool OpenModel(Info& info, MeshDescriptor& meshElements) |
| 2619 |
{ |
| 2620 |
PlyFile file; |
| 2621 |
if (!file.OpenFileToRead(info.filename)) |
| 2622 |
{ |
| 2623 |
info.errInfo = NNP_UNABLE_TO_OPEN; |
| 2624 |
return false; |
| 2625 |
} |
| 2626 |
bool last; |
| 2627 |
std::string line; |
| 2628 |
do |
| 2629 |
{ |
| 2630 |
file.NextHeaderLine(line, last); |
| 2631 |
} while (!last); |
| 2632 |
bool fixEndian = false; |
| 2633 |
if (checkEndianness() == 1) |
| 2634 |
{ |
| 2635 |
if (info.bigEndian) |
| 2636 |
fixEndian = true; |
| 2637 |
} |
| 2638 |
else |
| 2639 |
{ |
| 2640 |
if (!info.bigEndian) |
| 2641 |
fixEndian = true; |
| 2642 |
} |
| 2643 |
|
| 2644 |
if (info.binary) |
| 2645 |
{ |
| 2646 |
for (size_t i = 0; i < info.elemVec.size(); ++i) |
| 2647 |
{ |
| 2648 |
PlyElement& pe = info.elemVec[i]; |
| 2649 |
size_t j = 0; |
| 2650 |
for (; j < meshElements.size(); j++) |
| 2651 |
if (ElemProcessing<0>(*meshElements[j], pe, file, fixEndian)) |
| 2652 |
break; |
| 2653 |
if (j == meshElements.size()) |
| 2654 |
pe.SkipBinaryElementsInFile(file); |
| 2655 |
//if (!TupleForEach(meshElements, pe, file, fixEndian, SizeT<0>())) |
| 2656 |
// pe.SkipBinaryElementsInFile(file); |
| 2657 |
} |
| 2658 |
} |
| 2659 |
else |
| 2660 |
{ |
| 2661 |
for (size_t i = 0; i < info.elemVec.size(); ++i) |
| 2662 |
{ |
| 2663 |
PlyElement& pe = info.elemVec[i]; |
| 2664 |
size_t j = 0; |
| 2665 |
for (; j < meshElements.size(); j++) |
| 2666 |
if (ElemProcessing<1>(*meshElements[j], pe, file, false)) |
| 2667 |
break; |
| 2668 |
if (j == meshElements.size()) |
| 2669 |
pe.SkipAsciiElementsInFile(file); |
| 2670 |
//if (!TupleForEach(meshElements, pe, file, fixEndian, SizeT<1>())) |
| 2671 |
// pe.SkipAsciiElementsInFile(file); |
| 2672 |
} |
| 2673 |
} |
| 2674 |
return true; |
| 2675 |
} |
| 2676 |
|
| 2677 |
|
| 2678 |
|
| 2679 |
/** |
| 2680 |
* Save a 3D model in a PLY file. |
| 2681 |
* |
| 2682 |
* @param filename Path to the file to save. |
| 2683 |
* @param meshElements Vector that defines how to manage the ply element data in memory. |
| 2684 |
* @param info Info to saved in the PLY header. |
| 2685 |
*/ |
| 2686 |
inline bool SaveModel(std::string& filename, MeshDescriptor& meshElements, Info& info) |
| 2687 |
{ |
| 2688 |
PlyFile file; |
| 2689 |
if (!file.OpenFileToWrite(filename)) |
| 2690 |
{ |
| 2691 |
info.errInfo = NNP_UNABLE_TO_OPEN; |
| 2692 |
return false; |
| 2693 |
} |
| 2694 |
for (size_t i = 0; i < info.elemVec.size(); ++i) |
| 2695 |
{ |
| 2696 |
PlyElement& pe = info.elemVec[i]; |
| 2697 |
for (size_t j = 0; j < meshElements.size(); j++) |
| 2698 |
if (ElemProcessing<4>(*meshElements[j], pe, file, false)) |
| 2699 |
break; |
| 2700 |
} |
| 2701 |
info.WriteHeader(file); |
| 2702 |
bool fixEndian = false; |
| 2703 |
if (checkEndianness() == 1) |
| 2704 |
{ |
| 2705 |
if (info.bigEndian) |
| 2706 |
fixEndian = true; |
| 2707 |
} |
| 2708 |
else |
| 2709 |
{ |
| 2710 |
if (!info.bigEndian) |
| 2711 |
fixEndian = true; |
| 2712 |
} |
| 2713 |
(void)fixEndian; |
| 2714 |
|
| 2715 |
if (info.binary) |
| 2716 |
{ |
| 2717 |
for (size_t i = 0; i < info.elemVec.size(); ++i) |
| 2718 |
{ |
| 2719 |
PlyElement& pe = info.elemVec[i]; |
| 2720 |
if (pe.validToWrite) |
| 2721 |
{ |
| 2722 |
for (size_t j = 0; j < meshElements.size(); j++) |
| 2723 |
if (ElemProcessing<2>(*meshElements[j], pe, file, false)) |
| 2724 |
break; |
| 2725 |
} |
| 2726 |
} |
| 2727 |
} |
| 2728 |
else |
| 2729 |
{ |
| 2730 |
for (size_t i = 0; i < info.elemVec.size(); ++i) |
| 2731 |
{ |
| 2732 |
PlyElement& pe = info.elemVec[i]; |
| 2733 |
if (pe.validToWrite) |
| 2734 |
{ |
| 2735 |
for (size_t j = 0; j < meshElements.size(); j++) |
| 2736 |
if (ElemProcessing<3>(*meshElements[j], pe, file, false)) |
| 2737 |
break; |
| 2738 |
} |
| 2739 |
} |
| 2740 |
} |
| 2741 |
file.Flush(); |
| 2742 |
return true; |
| 2743 |
} |
| 2744 |
|
| 2745 |
|
| 2746 |
|
| 2747 |
/** |
| 2748 |
* @cond HIDDEN_SYMBOLS |
| 2749 |
*/ |
| 2750 |
template < typename TupleType, size_t ActionType> |
| 2751 |
inline bool TupleForEach(TupleType &tuple, PlyElement &elem, PlyFile& file, bool fixEndian, SizeT<ActionType> a) |
| 2752 |
{ |
| 2753 |
return TupleForEach(tuple, elem, file, fixEndian, SizeT<std::tuple_size<TupleType>::value>(), a); |
| 2754 |
} |
| 2755 |
|
| 2756 |
template < typename TupleType, size_t ActionType> |
| 2757 |
inline bool TupleForEach(TupleType &tuple, PlyElement &elem, PlyFile& file, bool fixEndian, SizeT<0> t, SizeT<ActionType> a) |
| 2758 |
{ |
| 2759 |
(void)tuple; (void)elem; (void)file; (void)fixEndian; (void)t; (void)a; |
| 2760 |
return false; |
| 2761 |
} |
| 2762 |
|
| 2763 |
template < typename TupleType, size_t N, size_t ActionType> |
| 2764 |
inline bool TupleForEach(TupleType &tuple, PlyElement &elem, PlyFile& file, bool fixEndian, SizeT<N> t, SizeT<ActionType> a) |
| 2765 |
{ |
| 2766 |
(void)t; |
| 2767 |
typename std::tuple_element<N - 1, TupleType>::type &elemDescr = std::get<N - 1>(tuple); |
| 2768 |
if ((elemDescr.elem != PlyElemEntity::NNP_UNKNOWN_ELEM && elemDescr.elem == elem.plyElem) || |
| 2769 |
(elemDescr.elem == PlyElemEntity::NNP_UNKNOWN_ELEM && elemDescr.name == elem.name)) |
| 2770 |
{ |
| 2771 |
if (ActionType == 0) |
| 2772 |
elemDescr.ReadElemBinary(file, elem, fixEndian); |
| 2773 |
else if (ActionType == 1) |
| 2774 |
elemDescr.ReadElemAscii(file, elem); |
| 2775 |
else if (ActionType == 2) |
| 2776 |
elemDescr.WriteElemBinary(file, elem, fixEndian); |
| 2777 |
else if (ActionType == 3) |
| 2778 |
elemDescr.WriteElemAscii(file, elem); |
| 2779 |
else if (ActionType == 4) |
| 2780 |
elemDescr.CheckDescriptor(elem); |
| 2781 |
return true; |
| 2782 |
} |
| 2783 |
return TupleForEach(tuple, elem, file, fixEndian, SizeT<N - 1>(), a); |
| 2784 |
} |
| 2785 |
/** |
| 2786 |
* @endcond |
| 2787 |
*/ |
| 2788 |
|
| 2789 |
} // end namespace nanoply |
| 2790 |
#endif // NANOPLY_HPP |