1 // Copyright 2016 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/geometry_attribute.h"
16 
17 namespace draco {
18 
GeometryAttribute()19 GeometryAttribute::GeometryAttribute()
20     : buffer_(nullptr),
21       num_components_(1),
22       data_type_(DT_FLOAT32),
23       byte_stride_(0),
24       byte_offset_(0),
25       attribute_type_(INVALID),
26       unique_id_(0) {}
27 
Init(GeometryAttribute::Type attribute_type,DataBuffer * buffer,int8_t num_components,DataType data_type,bool normalized,int64_t byte_stride,int64_t byte_offset)28 void GeometryAttribute::Init(GeometryAttribute::Type attribute_type,
29                              DataBuffer *buffer, int8_t num_components,
30                              DataType data_type, bool normalized,
31                              int64_t byte_stride, int64_t byte_offset) {
32   buffer_ = buffer;
33   if (buffer) {
34     buffer_descriptor_.buffer_id = buffer->buffer_id();
35     buffer_descriptor_.buffer_update_count = buffer->update_count();
36   }
37   num_components_ = num_components;
38   data_type_ = data_type;
39   normalized_ = normalized;
40   byte_stride_ = byte_stride;
41   byte_offset_ = byte_offset;
42   attribute_type_ = attribute_type;
43 }
44 
CopyFrom(const GeometryAttribute & src_att)45 bool GeometryAttribute::CopyFrom(const GeometryAttribute &src_att) {
46   num_components_ = src_att.num_components_;
47   data_type_ = src_att.data_type_;
48   normalized_ = src_att.normalized_;
49   byte_stride_ = src_att.byte_stride_;
50   byte_offset_ = src_att.byte_offset_;
51   attribute_type_ = src_att.attribute_type_;
52   buffer_descriptor_ = src_att.buffer_descriptor_;
53   unique_id_ = src_att.unique_id_;
54   if (src_att.buffer_ == nullptr) {
55     buffer_ = nullptr;
56   } else {
57     if (buffer_ == nullptr) {
58       return false;
59     }
60     buffer_->Update(src_att.buffer_->data(), src_att.buffer_->data_size());
61   }
62   return true;
63 }
64 
operator ==(const GeometryAttribute & va) const65 bool GeometryAttribute::operator==(const GeometryAttribute &va) const {
66   if (attribute_type_ != va.attribute_type_) {
67     return false;
68   }
69   // It's OK to compare just the buffer descriptors here. We don't need to
70   // compare the buffers themselves.
71   if (buffer_descriptor_.buffer_id != va.buffer_descriptor_.buffer_id) {
72     return false;
73   }
74   if (buffer_descriptor_.buffer_update_count !=
75       va.buffer_descriptor_.buffer_update_count) {
76     return false;
77   }
78   if (num_components_ != va.num_components_) {
79     return false;
80   }
81   if (data_type_ != va.data_type_) {
82     return false;
83   }
84   if (byte_stride_ != va.byte_stride_) {
85     return false;
86   }
87   if (byte_offset_ != va.byte_offset_) {
88     return false;
89   }
90   return true;
91 }
92 
ResetBuffer(DataBuffer * buffer,int64_t byte_stride,int64_t byte_offset)93 void GeometryAttribute::ResetBuffer(DataBuffer *buffer, int64_t byte_stride,
94                                     int64_t byte_offset) {
95   buffer_ = buffer;
96   buffer_descriptor_.buffer_id = buffer->buffer_id();
97   buffer_descriptor_.buffer_update_count = buffer->update_count();
98   byte_stride_ = byte_stride;
99   byte_offset_ = byte_offset;
100 }
101 
102 }  // namespace draco
103