1 // Copyright 2017 The Draco Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #include "draco/attributes/point_attribute.h"
16 
17 #include "draco/core/draco_test_base.h"
18 
19 namespace {
20 
21 class PointAttributeTest : public ::testing::Test {
22  protected:
PointAttributeTest()23   PointAttributeTest() {}
24 };
25 
TEST_F(PointAttributeTest,TestCopy)26 TEST_F(PointAttributeTest, TestCopy) {
27   // This test verifies that PointAttribute can copy data from another point
28   // attribute.
29   draco::PointAttribute pa;
30   pa.Init(draco::GeometryAttribute::POSITION, 1, draco::DT_INT32, false, 10);
31 
32   for (int32_t i = 0; i < 10; ++i) {
33     pa.SetAttributeValue(draco::AttributeValueIndex(i), &i);
34   }
35 
36   pa.set_unique_id(12);
37 
38   draco::PointAttribute other_pa;
39   other_pa.CopyFrom(pa);
40 
41   draco::PointAttributeHasher hasher;
42   ASSERT_EQ(hasher(pa), hasher(other_pa));
43   ASSERT_EQ(pa.unique_id(), other_pa.unique_id());
44 
45   // The hash function does not actually compute the hash from attribute values,
46   // so ensure the data got copied correctly as well.
47   for (int32_t i = 0; i < 10; ++i) {
48     int32_t data;
49     other_pa.GetValue(draco::AttributeValueIndex(i), &data);
50     ASSERT_EQ(data, i);
51   }
52 }
53 
TEST_F(PointAttributeTest,TestGetValueFloat)54 TEST_F(PointAttributeTest, TestGetValueFloat) {
55   draco::PointAttribute pa;
56   pa.Init(draco::GeometryAttribute::POSITION, 3, draco::DT_FLOAT32, false, 5);
57   float points[3];
58   for (int32_t i = 0; i < 5; ++i) {
59     points[0] = i * 3.0;
60     points[1] = (i * 3.0) + 1.0;
61     points[2] = (i * 3.0) + 2.0;
62     pa.SetAttributeValue(draco::AttributeValueIndex(i), &points);
63   }
64 
65   for (int32_t i = 0; i < 5; ++i) {
66     pa.GetValue(draco::AttributeValueIndex(i), &points);
67     ASSERT_FLOAT_EQ(points[0], i * 3.0);
68     ASSERT_FLOAT_EQ(points[1], (i * 3.0) + 1.0);
69     ASSERT_FLOAT_EQ(points[2], (i * 3.0) + 2.0);
70   }
71 }
72 
TEST_F(PointAttributeTest,TestGetArray)73 TEST_F(PointAttributeTest, TestGetArray) {
74   draco::PointAttribute pa;
75   pa.Init(draco::GeometryAttribute::POSITION, 3, draco::DT_FLOAT32, false, 5);
76   float points[3];
77   for (int32_t i = 0; i < 5; ++i) {
78     points[0] = i * 3.0;
79     points[1] = (i * 3.0) + 1.0;
80     points[2] = (i * 3.0) + 2.0;
81     pa.SetAttributeValue(draco::AttributeValueIndex(i), &points);
82   }
83 
84   for (int32_t i = 0; i < 5; ++i) {
85     std::array<float, 3> att_value;
86     att_value = pa.GetValue<float, 3>(draco::AttributeValueIndex(i));
87     ASSERT_FLOAT_EQ(att_value[0], i * 3.0);
88     ASSERT_FLOAT_EQ(att_value[1], (i * 3.0) + 1.0);
89     ASSERT_FLOAT_EQ(att_value[2], (i * 3.0) + 2.0);
90   }
91   for (int32_t i = 0; i < 5; ++i) {
92     std::array<float, 3> att_value;
93     EXPECT_TRUE(
94         (pa.GetValue<float, 3>(draco::AttributeValueIndex(i), &att_value)));
95     ASSERT_FLOAT_EQ(att_value[0], i * 3.0);
96     ASSERT_FLOAT_EQ(att_value[1], (i * 3.0) + 1.0);
97     ASSERT_FLOAT_EQ(att_value[2], (i * 3.0) + 2.0);
98   }
99 }
100 
TEST_F(PointAttributeTest,TestArrayReadError)101 TEST_F(PointAttributeTest, TestArrayReadError) {
102   draco::PointAttribute pa;
103   pa.Init(draco::GeometryAttribute::POSITION, 3, draco::DT_FLOAT32, false, 5);
104   float points[3];
105   for (int32_t i = 0; i < 5; ++i) {
106     points[0] = i * 3.0;
107     points[1] = (i * 3.0) + 1.0;
108     points[2] = (i * 3.0) + 2.0;
109     pa.SetAttributeValue(draco::AttributeValueIndex(i), &points);
110   }
111 
112   std::array<float, 3> att_value;
113   EXPECT_FALSE(
114       (pa.GetValue<float, 3>(draco::AttributeValueIndex(5), &att_value)));
115 }
116 
TEST_F(PointAttributeTest,TestResize)117 TEST_F(PointAttributeTest, TestResize) {
118   draco::PointAttribute pa;
119   pa.Init(draco::GeometryAttribute::POSITION, 3, draco::DT_FLOAT32, false, 5);
120   ASSERT_EQ(pa.size(), 5);
121   ASSERT_EQ(pa.buffer()->data_size(), 4 * 3 * 5);
122 
123   pa.Resize(10);
124   ASSERT_EQ(pa.size(), 10);
125   ASSERT_EQ(pa.buffer()->data_size(), 4 * 3 * 10);
126 }
127 
128 }  // namespace
129