File: PLY_reader.h

package info (click to toggle)
cgal 6.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 144,952 kB
  • sloc: cpp: 811,597; ansic: 208,576; sh: 493; python: 411; makefile: 286; javascript: 174
file content (798 lines) | stat: -rw-r--r-- 25,826 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
// Copyright (c) 2017 GeometryFactory
//
// This file is part of CGAL (www.cgal.org);
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Stream_support/include/CGAL/IO/PLY/PLY_reader.h $
// $Id: include/CGAL/IO/PLY/PLY_reader.h 08b27d3db14 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s)     : Simon Giraudot

#ifndef CGAL_IO_PLY_PLY_READER_H
#define CGAL_IO_PLY_PLY_READER_H

#include <CGAL/Container_helper.h>
#include <CGAL/IO/io.h>
#include <CGAL/type_traits/is_iterator.h>
#include <CGAL/Kernel_traits.h>
#include <CGAL/property_map.h>

#include <cstdint>
#include <boost/range/value_type.hpp>

#include <iostream>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define TRY_TO_GENERATE_PROPERTY(STD_TYPE, T_TYPE, TYPE)                \
  if(type == STD_TYPE  || type == T_TYPE)                              \
    m_elements.back().add_property(new PLY_read_typed_number< TYPE >(name, format))

#define TRY_TO_GENERATE_SIZED_LIST_PROPERTY(STD_SIZE_TYPE, T_SIZE_TYPE, SIZE_TYPE, STD_INDEX_TYPE, T_INDEX_TYPE, INDEX_TYPE) \
  if((size_type == STD_SIZE_TYPE  || size_type == T_SIZE_TYPE) &&      \
  (index_type == STD_INDEX_TYPE || index_type == T_INDEX_TYPE))     \
    m_elements.back().add_property(new PLY_read_typed_list_with_typed_size< SIZE_TYPE , INDEX_TYPE >(name, format))

#define TRY_TO_GENERATE_LIST_PROPERTY(STD_INDEX_TYPE, T_INDEX_TYPE, INDEX_TYPE) \
  TRY_TO_GENERATE_SIZED_LIST_PROPERTY("uchar", "uint8", std::uint8_t, STD_INDEX_TYPE, T_INDEX_TYPE, INDEX_TYPE); \
  else TRY_TO_GENERATE_SIZED_LIST_PROPERTY("char", "int8", std::int8_t, STD_INDEX_TYPE, T_INDEX_TYPE, INDEX_TYPE); \
  else TRY_TO_GENERATE_SIZED_LIST_PROPERTY("ushort", "uint16", std::uint16_t, STD_INDEX_TYPE, T_INDEX_TYPE, INDEX_TYPE); \
  else TRY_TO_GENERATE_SIZED_LIST_PROPERTY("short", "int16", std::int16_t, STD_INDEX_TYPE, T_INDEX_TYPE, INDEX_TYPE); \
  else TRY_TO_GENERATE_SIZED_LIST_PROPERTY("uint", "uint32", std::uint32_t, STD_INDEX_TYPE, T_INDEX_TYPE, INDEX_TYPE); \
  else TRY_TO_GENERATE_SIZED_LIST_PROPERTY("int", "int32", std::int32_t, STD_INDEX_TYPE, T_INDEX_TYPE, INDEX_TYPE)

namespace CGAL {

namespace IO {

/// \cond SKIP_IN_MANUAL

// PLY types:
// name        type        number of bytes
// ---------------------------------------
// char       character                 1
// uchar      unsigned character        1
// short      short integer             2
// ushort     unsigned short integer    2
// int        integer                   4
// uint       unsigned integer          4
// float      single-precision float    4
// double     double-precision float    8

template <typename T>
struct PLY_property
{
  typedef T type;
  const char* name;
  PLY_property(const char* name) : name(name) { }
};

// Use a double property for all kernels...
template <typename FT> struct Convert_FT        { typedef double type; };
// ...except if kernel uses type float
template <>            struct Convert_FT<float> { typedef float type;  };

template <typename PointOrVectorMap>
struct Get_FT_from_map
{
  typedef typename Convert_FT<typename Kernel_traits<
                                typename boost::property_traits<
                                  PointOrVectorMap>::value_type>::Kernel::FT>::type type;
};

template <typename PointMap>
std::tuple<PointMap,
           typename Kernel_traits<typename PointMap::value_type>::Kernel::Construct_point_3,
           PLY_property<typename Get_FT_from_map<PointMap>::type>,
           PLY_property<typename Get_FT_from_map<PointMap>::type>,
           PLY_property<typename Get_FT_from_map<PointMap>::type> >
make_ply_point_reader(PointMap point_map)
{
  return std::make_tuple(point_map, typename Kernel_traits<typename PointMap::value_type>::Kernel::Construct_point_3(),
                         PLY_property<typename Get_FT_from_map<PointMap>::type>("x"),
                         PLY_property<typename Get_FT_from_map<PointMap>::type>("y"),
                         PLY_property<typename Get_FT_from_map<PointMap>::type>("z"));
}

template <typename VectorMap>
std::tuple<VectorMap,
           typename Kernel_traits<typename VectorMap::value_type>::Kernel::Construct_vector_3,
           PLY_property<typename Get_FT_from_map<VectorMap>::type>,
           PLY_property<typename Get_FT_from_map<VectorMap>::type>,
           PLY_property<typename Get_FT_from_map<VectorMap>::type> >
make_ply_normal_reader(VectorMap normal_map)
{
  return std::make_tuple(normal_map, typename Kernel_traits<typename VectorMap::value_type>::Kernel::Construct_vector_3(),
                         PLY_property<typename Get_FT_from_map<VectorMap>::type>("nx"),
                         PLY_property<typename Get_FT_from_map<VectorMap>::type>("ny"),
                         PLY_property<typename Get_FT_from_map<VectorMap>::type>("nz"));
}

template <typename PointMap>
std::tuple<PointMap,
           PLY_property<typename Get_FT_from_map<PointMap>::type>,
           PLY_property<typename Get_FT_from_map<PointMap>::type>,
           PLY_property<typename Get_FT_from_map<PointMap>::type> >
make_ply_point_writer(PointMap point_map)
{
  return std::make_tuple(point_map,
                         PLY_property<typename Get_FT_from_map<PointMap>::type>("x"),
                         PLY_property<typename Get_FT_from_map<PointMap>::type>("y"),
                         PLY_property<typename Get_FT_from_map<PointMap>::type>("z"));
}

template <typename VectorMap>
std::tuple<VectorMap,
           PLY_property<typename Get_FT_from_map<VectorMap>::type>,
           PLY_property<typename Get_FT_from_map<VectorMap>::type>,
           PLY_property<typename Get_FT_from_map<VectorMap>::type> >
make_ply_normal_writer(VectorMap normal_map)
{
  return std::make_tuple(normal_map,
                         PLY_property<typename Get_FT_from_map<VectorMap>::type>("nx"),
                         PLY_property<typename Get_FT_from_map<VectorMap>::type>("ny"),
                         PLY_property<typename Get_FT_from_map<VectorMap>::type>("nz"));
}

namespace internal {

class PLY_read_number
{
protected:
  std::string m_name;
  std::size_t m_format;

public:
  PLY_read_number(std::string name, std::size_t format)
    : m_name(name), m_format(format) { }
  virtual ~PLY_read_number() { }

  const std::string& name() const { return m_name; }

  virtual void get(std::istream& stream) const = 0;

  // The two following functions prevent the stream to only extract
  // ONE character (= what the types char imply) by requiring
  // explicitly an integer object when reading the stream
  void read_ascii(std::istream& stream, char& c) const
  {
    short s;
    if(stream >> s)
      c = static_cast<char>(s);
    else
    {
      c = 0;
      stream.clear(std::ios::badbit);
    }
  }

  void read_ascii(std::istream& stream, signed char& c) const
  {
    short s;
    if(stream >> s)
      c = static_cast<signed char>(s);
    else
    {
      c = 0;
      stream.clear(std::ios::badbit);
    }
  }

  void read_ascii(std::istream& stream, unsigned char& c) const
  {
    unsigned short s;
    if(stream >> s)
      c = static_cast<unsigned char>(s);
    else
    {
      c = 0;
      stream.clear(std::ios::badbit);
    }
  }

  void read_ascii(std::istream& stream, float& t) const
  {
    if(!(stream >> IO::iformat(t)))
      stream.clear(std::ios::badbit);
  }

  void read_ascii(std::istream& stream, double& t) const
  {
    if(!(stream >> IO::iformat(t)))
      stream.clear(std::ios::badbit);
  }

  // Default template when Type is not a char type
  template <typename Type>
  void read_ascii(std::istream& stream, Type& t) const
  {
    if(!(stream >> t))
      stream.clear(std::ios::badbit);
  }

  template <typename Type>
  Type read(std::istream& stream) const
  {
    if(m_format == 0) // ASCII
    {
      Type t;
      read_ascii(stream, t);
      return t;
    }
    else // Binary (2 = little endian)
    {
      union
      {
        char uChar[sizeof(Type)];
        Type type;
      } buffer;

      std::size_t size = sizeof(Type);

      stream.read(buffer.uChar, size);

      if(m_format == 2) // Big endian
      {
        for(std::size_t i = 0; i < size / 2; ++ i)
        {
          unsigned char tmp = buffer.uChar[i];
          buffer.uChar[i] = buffer.uChar[size - 1 - i];
          buffer.uChar[size - 1 - i] = tmp;
        }
      }
      return buffer.type;
    }
    return Type();
  }
};

template <typename Type>
class PLY_read_typed_number : public PLY_read_number
{
  mutable Type m_buffer;
public:
  PLY_read_typed_number(std::string name, std::size_t format)
    : PLY_read_number(name, format)
  { }

  void get(std::istream& stream) const { m_buffer =(this->read<Type>(stream)); }

  const Type& buffer() const { return m_buffer; }
};

template <typename Type>
class PLY_read_typed_list
  : public PLY_read_number
{
protected:
  mutable std::vector<Type> m_buffer;

public:
  PLY_read_typed_list(std::string name, std::size_t format)
    : PLY_read_number(name, format)
  { }

  virtual void get(std::istream& stream) const = 0;

  const std::vector<Type>& buffer() const { return m_buffer; }
};

template <typename SizeType, typename IndexType>
class PLY_read_typed_list_with_typed_size
  : public PLY_read_typed_list<IndexType>
{
public:
  PLY_read_typed_list_with_typed_size(std::string name, std::size_t format)
    : PLY_read_typed_list<IndexType>(name, format)
  { }

  void get(std::istream& stream) const
  {
    std::size_t size = static_cast<std::size_t>(this->template read<SizeType>(stream));
    this->m_buffer.resize(size);
    for(std::size_t i = 0; i < size; ++ i)
      this->m_buffer[i] = this->template read<IndexType>(stream);
  }
};

class PLY_element
{
  std::string m_name;
  std::size_t m_number;
  std::vector<PLY_read_number*> m_properties;

public:
  PLY_element(const std::string& name, std::size_t number)
    : m_name(name), m_number(number)
  { }

  PLY_element(const PLY_element& other)
    : m_name(other.m_name), m_number(other.m_number), m_properties(other.m_properties)
  {
    const_cast<PLY_element&>(other).m_properties.clear();
  }

  PLY_element& operator=(const PLY_element& other)
  {
    m_name = other.m_name;
    m_number = other.m_number;
    m_properties = other.m_properties;
    const_cast<PLY_element&>(other).m_properties.clear();
    return *this;
  }

  ~PLY_element()
  {
    for(std::size_t i = 0; i < m_properties.size(); ++ i)
      delete m_properties[i];
  }

  const std::string& name() const { return m_name; }
  std::size_t number_of_items() const { return m_number; }
  std::size_t number_of_properties() const { return m_properties.size(); }

  PLY_read_number* property(std::size_t idx) { return m_properties[idx]; }

  void add_property(PLY_read_number* read_number)
  {
    m_properties.push_back(read_number);
  }

  template <typename Type>
  bool has_property(const char* tag)
  {
    return has_property(tag, Type());
  }

  template <typename Type>
  bool has_property(const char* tag, const std::vector<Type>&)
  {
    for(std::size_t i = 0; i < number_of_properties(); ++ i)
      if(m_properties[i]->name() == tag)
        return (dynamic_cast<PLY_read_typed_list<Type>*>(m_properties[i]) != nullptr);
    return false;
  }

  template <typename Type>
  bool has_property(const char* tag, Type)
  {
    for(std::size_t i = 0; i < number_of_properties(); ++ i)
      if(m_properties[i]->name() == tag)
        return (dynamic_cast<PLY_read_typed_number<Type>*>(m_properties[i]) != nullptr);
    return false;
  }

  bool has_property(const char* tag, double)
  {
    for(std::size_t i = 0; i < number_of_properties(); ++ i)
      if(m_properties[i]->name() == tag)
        return (dynamic_cast<PLY_read_typed_number<double>*>(m_properties[i]) != nullptr
                                                                                 || dynamic_cast<PLY_read_typed_number<float>*>(m_properties[i]) != nullptr);

    return false;
  }

  template <typename Type>
  void assign(Type& t, const char* tag)
  {
    for(std::size_t i = 0; i < number_of_properties(); ++ i)
      if(m_properties[i]->name() == tag)
      {
        PLY_read_typed_number<Type>*
            property = dynamic_cast<PLY_read_typed_number<Type>*>(m_properties[i]);
        CGAL_assertion(property != nullptr);
        t = property->buffer();
        return;
      }
    t = {};
  }

  template <typename Type>
  void assign(std::vector<Type>& t, const char* tag)
  {
    for(std::size_t i = 0; i < number_of_properties(); ++ i)
      if(m_properties[i]->name() == tag)
      {
        PLY_read_typed_list<Type>*
            property = dynamic_cast<PLY_read_typed_list<Type>*>(m_properties[i]);
        CGAL_assertion(property != nullptr);
        t = property->buffer();
        return;
      }
    t = {};
  }

  void assign(double& t, const char* tag)
  {
    for(std::size_t i = 0; i < number_of_properties(); ++ i)
      if(m_properties[i]->name() == tag)
      {
        PLY_read_typed_number<double>*
            property_double = dynamic_cast<PLY_read_typed_number<double>*>(m_properties[i]);
        if(property_double == nullptr)
        {
          PLY_read_typed_number<float>*
              property_float = dynamic_cast<PLY_read_typed_number<float>*>(m_properties[i]);
          CGAL_assertion(property_float != nullptr);
          t = property_float->buffer();
        }
        else
          t = property_double->buffer();

        return;
      }
    t = {};
  }
};

class PLY_reader
{
  std::vector<PLY_element> m_elements;
  std::string m_comments;
  bool m_verbose;

public:
  PLY_reader(bool verbose) : m_verbose(verbose) { }

  std::size_t number_of_elements() const { return m_elements.size(); }
  PLY_element& element(std::size_t idx)
  {
    return m_elements[idx];
  }

  const std::string& comments() const { return m_comments; }

  template <typename Stream>
  bool init(Stream& stream)
  {
    std::size_t lineNumber = 0; // current line number
    enum Format { ASCII = 0, BINARY_LITTLE_ENDIAN = 1, BINARY_BIG_ENDIAN = 2};
    Format format = ASCII;

    std::string line;
    std::istringstream iss;

    while(getline(stream,line))
    {
      iss.clear();
      iss.str(line);
      ++ lineNumber;

      // Reads file signature on first line
      if(lineNumber == 1)
      {
        std::string signature;
        if(!(iss >> signature) || (signature != "ply"))
        {
          // if wrong file format
          if(m_verbose)
            std::cerr << "Error: incorrect file format line " << lineNumber << " of file" << std::endl;
          return false;
        }
      }

      // Reads format on 2nd line
      else if(lineNumber == 2)
      {
        std::string tag, format_string, version;
        if( !(iss >> tag >> format_string >> version) )
        {
          if(m_verbose)
            std::cerr << "Error line " << lineNumber << " of file" << std::endl;
          return false;
        }
        if(format_string == "ascii") format = ASCII;
        else if(format_string == "binary_little_endian") format = BINARY_LITTLE_ENDIAN;
        else if(format_string == "binary_big_endian") format = BINARY_BIG_ENDIAN;
        else
        {
          if(m_verbose)
            std::cerr << "Error: unknown file format \"" << format_string << "\" line " << lineNumber << std::endl;
          return false;
        }
      }

      // Comments and vertex properties
      else
      {
        std::string keyword;
        if(!(iss >> keyword))
        {
          if(m_verbose)
            std::cerr << "Error line " << lineNumber << " of file" << std::endl;
          return false;
        }

        if(keyword == "property")
        {
          std::string type, name;
          if(!(iss >> type >> name))
          {
            if(m_verbose)
              std::cerr << "Error line " << lineNumber << " of file" << std::endl;
            return false;
          }


          if(type == "list") // Special case
          {
            std::string size_type = name;
            std::string index_type;
            name.clear();
            if(!(iss >> index_type >> name))
            {
              if(m_verbose)
                std::cerr << "Error line " << lineNumber << " of file" << std::endl;
              return false;
            }

            TRY_TO_GENERATE_LIST_PROPERTY("char", "int8", std::int8_t);
            else TRY_TO_GENERATE_LIST_PROPERTY("uchar", "uint8", std::uint8_t);
            else TRY_TO_GENERATE_LIST_PROPERTY("short", "int16", std::int16_t);
            else TRY_TO_GENERATE_LIST_PROPERTY("ushort", "uint16", std::uint16_t);
            else TRY_TO_GENERATE_LIST_PROPERTY("int", "int32", std::int32_t);
            else TRY_TO_GENERATE_LIST_PROPERTY("uint", "uint32", std::uint32_t);
            else TRY_TO_GENERATE_LIST_PROPERTY("float", "float32", float);
            else TRY_TO_GENERATE_LIST_PROPERTY("double", "float64", double);
          }
          else
          {
            TRY_TO_GENERATE_PROPERTY("char", "int8", std::int8_t);
            else TRY_TO_GENERATE_PROPERTY("uchar", "uint8", std::uint8_t);
            else TRY_TO_GENERATE_PROPERTY("short", "int16", std::int16_t);
            else TRY_TO_GENERATE_PROPERTY("ushort", "uint16", std::uint16_t);
            else TRY_TO_GENERATE_PROPERTY("int", "int32", std::int32_t);
            else TRY_TO_GENERATE_PROPERTY("uint", "uint32", std::uint32_t);
            else TRY_TO_GENERATE_PROPERTY("float", "float32", float);
            else TRY_TO_GENERATE_PROPERTY("double", "float64", double);
          }

          continue;
        }
        else if(keyword == "comment")
        {
          std::string str = iss.str();
          if(str.size() > 8)
          {
            std::copy(str.begin() + 8, str.end(), std::back_inserter(m_comments));
            m_comments += "\n";
          }
        }
        else if(keyword == "element")
        {
          std::string type;
          std::size_t number;
          if(!(iss >> type >> number))
          {
            if(m_verbose)
              std::cerr << "Error line " << lineNumber << " of file" << std::endl;
            return false;
          }

          m_elements.push_back(PLY_element(type, number));
        }
        // When end_header is reached, stop loop and begin reading points
        else if(keyword == "end_header")
          break;
      }
    }
    return true;
  }

  ~PLY_reader()
  {
  }

};

template <class Reader, class T>
void get_value(Reader& r, T& v, PLY_property<T>& wrapper)
{
  return r.assign(v, wrapper.name);
}

template <std::size_t N>
struct Filler
{
  template <class Reader, class Value_tuple, class PLY_property_tuple>
  static void fill(Reader& r, Value_tuple& values, PLY_property_tuple wrappers)
  {
    get_value(r, std::get<N>(values), std::get<N+2>(wrappers));
    Filler<N-1>::fill(r, values, wrappers);
  }
};

template<int ...>
struct seq { };

template<int N, int ...S>
struct gens : gens<N-1, N-1, S...> { };

template<int ...S>
struct gens<0, S...> {
  typedef seq<S...> type;
};

template<class ValueType, class Functor, class Tuple, int ...S>
ValueType call_functor(Functor f, Tuple t, seq<S...>) {
  return f(std::get<S>(t) ...);
}

template <class ValueType, class Functor, typename ... T>
ValueType call_functor(Functor f, std::tuple<T...>& t)
{
  return call_functor<ValueType>(f, t, typename gens<sizeof...(T)>::type());
}

template<>
struct Filler<0>
{
  template <class Reader, class Value_tuple, class PLY_property_tuple>
  static void fill(Reader& r, Value_tuple& values, PLY_property_tuple wrappers)
  {
    get_value(r, std::get<0>(values), std::get<2>(wrappers));
  }
};

template <typename OutputValueType,
          typename PropertyMap,
          typename Constructor,
          typename ... T>
void process_properties(PLY_element& element, OutputValueType& new_element,
                        std::tuple<PropertyMap, Constructor, PLY_property<T>...>&& current)
{
  typedef typename boost::property_traits<PropertyMap>::value_type PmapValueType;

  std::tuple<T...> values;
  Filler<sizeof...(T)-1>::fill(element, values, current);
  PmapValueType new_value = call_functor<PmapValueType>(std::get<1>(current), values);
  put(std::get<0>(current), new_element, new_value);
}

template <typename OutputValueType,
          typename PropertyMap,
          typename Constructor,
          typename ... T,
          typename NextPropertyBinder,
          typename ... PropertyMapBinders>
void process_properties(PLY_element& element, OutputValueType& new_element,
                        std::tuple<PropertyMap, Constructor, PLY_property<T>...>&& current,
                        NextPropertyBinder&& next,
                        PropertyMapBinders&& ... properties)
{
  typedef typename boost::property_traits<PropertyMap>::value_type PmapValueType;

  std::tuple<T...> values;
  Filler<sizeof...(T)-1>::fill(element, values, current);
  PmapValueType new_value = call_functor<PmapValueType>(std::get<1>(current), values);
  put(std::get<0>(current), new_element, new_value);

  process_properties(element, new_element, std::forward<NextPropertyBinder>(next),
                     std::forward<PropertyMapBinders>(properties)...);
}


template <typename OutputValueType, typename PropertyMap, typename T>
void process_properties(PLY_element& element, OutputValueType& new_element,
                        std::pair<PropertyMap, PLY_property<T> >&& current)
{
  T new_value = T();
  element.assign(new_value, current.second.name);
  put(current.first, new_element, new_value);
}

template <typename OutputValueType, typename PropertyMap, typename T,
          typename NextPropertyBinder, typename ... PropertyMapBinders>
void process_properties(PLY_element& element, OutputValueType& new_element,
                        std::pair<PropertyMap, PLY_property<T> >&& current,
                        NextPropertyBinder&& next,
                        PropertyMapBinders&& ... properties)
{
  T new_value = T();
  element.assign(new_value, current.second.name);
  put(current.first, new_element, new_value);
  process_properties(element, new_element, std::forward<NextPropertyBinder>(next),
                     std::forward<PropertyMapBinders>(properties)...);
}

template <typename Integer, class PolygonRange, class ColorOutputIterator>
bool read_PLY_faces(std::istream& in,
                    PLY_element& element,
                    PolygonRange& polygons,
                    ColorOutputIterator fc_out,
                    const char* vertex_indices_tag,
                    std::enable_if_t<CGAL::is_iterator<ColorOutputIterator>::value>* = nullptr)
{
  typedef CGAL::IO::Color                                 Color_rgb;

  bool has_colors = false;
  std::string rtag = "r", gtag = "g", btag = "b";

  if((element.has_property<std::uint8_t>("red") || element.has_property<std::uint8_t>("r")) &&
     (element.has_property<std::uint8_t>("green") || element.has_property<std::uint8_t>("g")) &&
     (element.has_property<std::uint8_t>("blue") || element.has_property<std::uint8_t>("b")))
  {
    has_colors = true;
    if(element.has_property<std::uint8_t>("red"))
    {
      rtag = "red";
      gtag = "green";
      btag = "blue";
    }
  }

  for(std::size_t j = 0; j < element.number_of_items(); ++ j)
  {
    for(std::size_t k = 0; k < element.number_of_properties(); ++ k)
    {
      PLY_read_number* property = element.property(k);
      property->get(in);

      if(in.fail())
        return false;
    }

    std::tuple<std::vector<Integer>, std::uint8_t, std::uint8_t, std::uint8_t> new_face;

    if(has_colors)
    {
      process_properties(element, new_face,
                         std::make_pair(CGAL::make_nth_of_tuple_property_map<0>(new_face),
                                        PLY_property<std::vector<Integer> >(vertex_indices_tag)),
                         std::make_pair(CGAL::make_nth_of_tuple_property_map<1>(new_face),
                                        PLY_property<std::uint8_t>(rtag.c_str())),
                         std::make_pair(CGAL::make_nth_of_tuple_property_map<2>(new_face),
                                        PLY_property<std::uint8_t>(gtag.c_str())),
                         std::make_pair(CGAL::make_nth_of_tuple_property_map<3>(new_face),
                                        PLY_property<std::uint8_t>(btag.c_str())));

      *fc_out++ = Color_rgb(get<1>(new_face), get<2>(new_face), get<3>(new_face));
    }
    else
    {
      process_properties(element, new_face,
                         std::make_pair(CGAL::make_nth_of_tuple_property_map<0>(new_face),
                                        PLY_property<std::vector<Integer> >(vertex_indices_tag)));
    }

    polygons.emplace_back();
    ::CGAL::internal::resize(polygons.back(), get<0>(new_face).size());
    for(std::size_t i = 0; i < get<0>(new_face).size(); ++ i)
      polygons.back()[i] = std::size_t(get<0>(new_face)[i]);
  }

  return true;
}

template <typename Integer, class PolygonRange, class ColorRange>
bool read_PLY_faces(std::istream& in,
                    PLY_element& element,
                    PolygonRange& polygons,
                    ColorRange& fcolors,
                    const char* vertex_indices_tag,
                    std::enable_if_t<
                      boost::has_range_const_iterator<ColorRange>::value
                    >* = nullptr)
{
  return read_PLY_faces<Integer>(in, element, polygons, std::back_inserter(fcolors), vertex_indices_tag);
}

} // namespace PLY
} // namespace internal

#ifndef CGAL_NO_DEPRECATED_CODE
using IO::PLY_property;
using IO::make_ply_normal_reader;
using IO::make_ply_normal_writer;
using IO::make_ply_point_reader;
using IO::make_ply_point_writer;
#endif

/// \endcond

} // namespace CGAL

#endif // CGAL_IO_PLY_PLY_READER_H