Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ply_reader_test.cc « io « draco « src « dracoenc « draco « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6804dc33de3015b424d612c052c552c6a574819d (plain)
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
// Copyright 2016 The Draco Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "draco/io/ply_reader.h"

#include <fstream>

#include "draco/core/draco_test_base.h"
#include "draco/core/draco_test_utils.h"
#include "draco/io/ply_property_reader.h"

namespace draco {

class PlyReaderTest : public ::testing::Test {
 protected:
  std::vector<char> ReadPlyFile(const std::string &file_name) const {
    const std::string path = GetTestFileFullPath(file_name);
    std::ifstream file(path.c_str(), std::ios::binary);
    if (!file)
      return std::vector<char>();
    auto is_size = file.tellg();
    file.seekg(0, std::ios::end);
    is_size = file.tellg() - is_size;
    file.seekg(0, std::ios::beg);
    std::vector<char> data(is_size);
    file.read(&data[0], is_size);
    return data;
  }
};

TEST_F(PlyReaderTest, TestReader) {
  const std::string file_name = "test_pos_color.ply";
  const std::vector<char> data = ReadPlyFile(file_name);
  DecoderBuffer buf;
  buf.Init(data.data(), data.size());
  PlyReader reader;
  Status status = reader.Read(&buf);
  ASSERT_TRUE(status.ok()) << status;
  ASSERT_EQ(reader.num_elements(), 2);
  ASSERT_EQ(reader.element(0).num_properties(), 7);
  ASSERT_EQ(reader.element(1).num_properties(), 1);
  ASSERT_TRUE(reader.element(1).property(0).is_list());

  ASSERT_TRUE(reader.element(0).GetPropertyByName("red") != nullptr);
  const PlyProperty *const prop = reader.element(0).GetPropertyByName("red");
  PlyPropertyReader<uint8_t> reader_uint8(prop);
  PlyPropertyReader<uint32_t> reader_uint32(prop);
  PlyPropertyReader<float> reader_float(prop);
  for (int i = 0; i < reader.element(0).num_entries(); ++i) {
    ASSERT_EQ(reader_uint8.ReadValue(i), reader_uint32.ReadValue(i));
    ASSERT_EQ(reader_uint8.ReadValue(i), reader_float.ReadValue(i));
  }
}

TEST_F(PlyReaderTest, TestReaderAscii) {
  const std::string file_name = "test_pos_color.ply";
  const std::vector<char> data = ReadPlyFile(file_name);
  DecoderBuffer buf;
  buf.Init(data.data(), data.size());
  PlyReader reader;
  Status status = reader.Read(&buf);
  ASSERT_TRUE(status.ok()) << status;

  const std::string file_name_ascii = "test_pos_color_ascii.ply";
  const std::vector<char> data_ascii = ReadPlyFile(file_name_ascii);
  buf.Init(data_ascii.data(), data_ascii.size());
  PlyReader reader_ascii;
  status = reader_ascii.Read(&buf);
  ASSERT_TRUE(status.ok()) << status;
  ASSERT_EQ(reader.num_elements(), reader_ascii.num_elements());
  ASSERT_EQ(reader.element(0).num_properties(),
            reader_ascii.element(0).num_properties());

  ASSERT_TRUE(reader.element(0).GetPropertyByName("x") != nullptr);
  const PlyProperty *const prop = reader.element(0).GetPropertyByName("x");
  const PlyProperty *const prop_ascii =
      reader_ascii.element(0).GetPropertyByName("x");
  PlyPropertyReader<float> reader_float(prop);
  PlyPropertyReader<float> reader_float_ascii(prop_ascii);
  for (int i = 0; i < reader.element(0).num_entries(); ++i) {
    ASSERT_NEAR(reader_float.ReadValue(i), reader_float_ascii.ReadValue(i),
                1e-4f);
  }
}

TEST_F(PlyReaderTest, TestReaderExtraWhitespace) {
  const std::string file_name = "test_extra_whitespace.ply";
  const std::vector<char> data = ReadPlyFile(file_name);
  DecoderBuffer buf;
  buf.Init(data.data(), data.size());
  PlyReader reader;
  Status status = reader.Read(&buf);
  ASSERT_TRUE(status.ok()) << status;

  ASSERT_EQ(reader.num_elements(), 2);
  ASSERT_EQ(reader.element(0).num_properties(), 7);
  ASSERT_EQ(reader.element(1).num_properties(), 1);
  ASSERT_TRUE(reader.element(1).property(0).is_list());

  ASSERT_TRUE(reader.element(0).GetPropertyByName("red") != nullptr);
  const PlyProperty *const prop = reader.element(0).GetPropertyByName("red");
  PlyPropertyReader<uint8_t> reader_uint8(prop);
  PlyPropertyReader<uint32_t> reader_uint32(prop);
  PlyPropertyReader<float> reader_float(prop);
  for (int i = 0; i < reader.element(0).num_entries(); ++i) {
    ASSERT_EQ(reader_uint8.ReadValue(i), reader_uint32.ReadValue(i));
    ASSERT_EQ(reader_uint8.ReadValue(i), reader_float.ReadValue(i));
  }
}

TEST_F(PlyReaderTest, TestReaderMoreDataTypes) {
  const std::string file_name = "test_more_datatypes.ply";
  const std::vector<char> data = ReadPlyFile(file_name);
  DecoderBuffer buf;
  buf.Init(data.data(), data.size());
  PlyReader reader;
  Status status = reader.Read(&buf);
  ASSERT_TRUE(status.ok()) << status;

  ASSERT_EQ(reader.num_elements(), 2);
  ASSERT_EQ(reader.element(0).num_properties(), 7);
  ASSERT_EQ(reader.element(1).num_properties(), 1);
  ASSERT_TRUE(reader.element(1).property(0).is_list());

  ASSERT_TRUE(reader.element(0).GetPropertyByName("red") != nullptr);
  const PlyProperty *const prop = reader.element(0).GetPropertyByName("red");
  PlyPropertyReader<uint8_t> reader_uint8(prop);
  PlyPropertyReader<uint32_t> reader_uint32(prop);
  PlyPropertyReader<float> reader_float(prop);
  for (int i = 0; i < reader.element(0).num_entries(); ++i) {
    ASSERT_EQ(reader_uint8.ReadValue(i), reader_uint32.ReadValue(i));
    ASSERT_EQ(reader_uint8.ReadValue(i), reader_float.ReadValue(i));
  }
}

}  // namespace draco