GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
point_d_vector.h
Go to the documentation of this file.
1// Copyright 2018 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
16#ifndef DRACO_COMPRESSION_ATTRIBUTES_POINT_D_VECTOR_H_
17#define DRACO_COMPRESSION_ATTRIBUTES_POINT_D_VECTOR_H_
18
19#include <cstddef>
20#include <cstring>
21#include <iterator>
22#include <memory>
23#include <vector>
24
25#include "draco/core/macros.h"
26
27namespace draco {
28
29// The main class of this file is PointDVector providing an interface similar to
30// std::vector<PointD> for arbitrary number of dimensions (without a template
31// argument). PointDVectorIterator is a random access iterator, which allows for
32// compatibility with existing algorithms. PseudoPointD provides for a view on
33// the individual items in a contiguous block of memory, which is compatible
34// with the swap function and is returned by a dereference of
35// PointDVectorIterator. Swap functions provide for compatibility/specialization
36// that allows these classes to work with currently utilized STL functions.
37
38// This class allows for swap functionality from the RandomIterator
39// It seems problematic to bring this inside PointDVector due to templating.
40template <typename internal_t>
42 public:
43 PseudoPointD(internal_t *mem, internal_t dimension)
44 : mem_(mem), dimension_(dimension) {}
45
46 // Specifically copies referenced memory
47 void swap(PseudoPointD &other) noexcept {
48 for (internal_t dim = 0; dim < dimension_; dim += 1) {
49 std::swap(mem_[dim], other.mem_[dim]);
50 }
51 }
52
54 : mem_(other.mem_), dimension_(other.dimension_) {}
55
56 const internal_t &operator[](const size_t &n) const {
57 DRACO_DCHECK_LT(n, dimension_);
58 return mem_[n];
59 }
60 internal_t &operator[](const size_t &n) {
61 DRACO_DCHECK_LT(n, dimension_);
62 return mem_[n];
63 }
64
65 bool operator==(const PseudoPointD &other) const {
66 for (auto dim = 0; dim < dimension_; dim += 1) {
67 if (mem_[dim] != other.mem_[dim]) {
68 return false;
69 }
70 }
71 return true;
72 }
73 bool operator!=(const PseudoPointD &other) const {
74 return !this->operator==(other);
75 }
76
77 private:
78 internal_t *const mem_;
79 const internal_t dimension_;
80};
81
82// It seems problematic to bring this inside PointDVector due to templating.
83template <typename internal_t>
86 a.swap(b);
87};
88template <typename internal_t>
91 a.swap(b);
92};
93
94template <typename internal_t>
96 public:
97 PointDVector(const uint32_t n_items, const uint32_t dimensionality)
98 : n_items_(n_items),
99 dimensionality_(dimensionality),
100 item_size_bytes_(dimensionality * sizeof(internal_t)),
101 data_(n_items * dimensionality),
102 data0_(data_.data()) {}
103 // random access iterator
105 friend class PointDVector;
106
107 public:
108 // Iterator traits expected by std libraries.
109 using iterator_category = std::random_access_iterator_tag;
110 using value_type = size_t;
111 using difference_type = size_t;
114
115 // std::iter_swap is called inside of std::partition and needs this
116 // specialized support
118 return PseudoPointD<internal_t>(vec_->data0_ + item_ * dimensionality_,
119 dimensionality_);
120 }
122 item_ += 1;
123 return *this;
124 }
126 item_ -= 1;
127 return *this;
128 }
130 PointDVectorIterator copy(*this);
131 item_ += 1;
132 return copy;
133 }
135 PointDVectorIterator copy(*this);
136 item_ -= 1;
137 return copy;
138 }
140 this->item_ = other.item_;
141 return *this;
142 }
143
144 bool operator==(const PointDVectorIterator &ref) const {
145 return item_ == ref.item_;
146 }
147 bool operator!=(const PointDVectorIterator &ref) const {
148 return item_ != ref.item_;
149 }
150 bool operator<(const PointDVectorIterator &ref) const {
151 return item_ < ref.item_;
152 }
153 bool operator>(const PointDVectorIterator &ref) const {
154 return item_ > ref.item_;
155 }
156 bool operator<=(const PointDVectorIterator &ref) const {
157 return item_ <= ref.item_;
158 }
159 bool operator>=(const PointDVectorIterator &ref) const {
160 return item_ >= ref.item_;
161 }
162
163 PointDVectorIterator operator+(const int32_t &add) const {
164 PointDVectorIterator copy(vec_, item_ + add);
165 return copy;
166 }
168 item_ += add;
169 return *this;
170 }
171 PointDVectorIterator operator-(const int32_t &sub) const {
172 PointDVectorIterator copy(vec_, item_ - sub);
173 return copy;
174 }
175 size_t operator-(const PointDVectorIterator &sub) const {
176 return (item_ - sub.item_);
177 }
178
179 PointDVectorIterator &operator-=(const int32_t &sub) {
180 item_ -= sub;
181 return *this;
182 }
183
184 internal_t *operator[](const size_t &n) const {
185 return vec_->data0_ + (item_ + n) * dimensionality_;
186 }
187
188 protected:
189 explicit PointDVectorIterator(PointDVector *vec, size_t start_item)
190 : item_(start_item), vec_(vec), dimensionality_(vec->dimensionality_) {}
191
192 private:
193 size_t item_; // this counts the item that should be referenced.
194 PointDVector *const vec_; // the thing that we're iterating on
195 const uint32_t dimensionality_; // local copy from vec_
196 };
197
199 PointDVectorIterator end() { return PointDVectorIterator(this, n_items_); }
200
201 // operator[] allows for unprotected user-side usage of operator[] on the
202 // return value AS IF it were a natively indexable type like Point3*
203 internal_t *operator[](const uint32_t index) {
204 DRACO_DCHECK_LT(index, n_items_);
205 return data0_ + index * dimensionality_;
206 }
207 const internal_t *operator[](const uint32_t index) const {
208 DRACO_DCHECK_LT(index, n_items_);
209 return data0_ + index * dimensionality_;
210 }
211
212 uint32_t size() const { return n_items_; }
213 size_t GetBufferSize() const { return data_.size(); }
214
215 // copy a single contiguous 'item' from one PointDVector into this one.
216 void CopyItem(const PointDVector &source, const internal_t source_index,
217 const internal_t destination_index) {
218 DRACO_DCHECK(&source != this ||
219 (&source == this && source_index != destination_index));
220 DRACO_DCHECK_LT(destination_index, n_items_);
221 DRACO_DCHECK_LT(source_index, source.n_items_);
222
223 // DRACO_DCHECK_EQ(source.n_items_, n_items_); // not technically necessary
224 DRACO_DCHECK_EQ(source.dimensionality_, dimensionality_);
225
226 const internal_t *ref = source[source_index];
227 internal_t *const dest = this->operator[](destination_index);
228 std::memcpy(dest, ref, item_size_bytes_);
229 }
230
231 // Copy data directly off of an attribute buffer interleaved into internal
232 // memory.
234 // The dimensionality of the attribute being integrated
235 const internal_t attribute_dimensionality,
236 // The offset in dimensions to insert this attribute.
237 const internal_t offset_dimensionality, const internal_t index,
238 // The direct pointer to the data
239 const void *const attribute_item_data) {
240 // chunk copy
241 const size_t copy_size = sizeof(internal_t) * attribute_dimensionality;
242
243 // a multiply and add can be optimized away with an iterator
244 std::memcpy(data0_ + index * dimensionality_ + offset_dimensionality,
245 attribute_item_data, copy_size);
246 }
247 // Copy data off of a contiguous buffer interleaved into internal memory
249 // The dimensionality of the attribute being integrated
250 const internal_t attribute_dimensionality,
251 // The offset in dimensions to insert this attribute.
252 const internal_t offset_dimensionality,
253 const internal_t *const attribute_mem) {
254 DRACO_DCHECK_LT(offset_dimensionality,
255 dimensionality_ - attribute_dimensionality);
256 // degenerate case block copy the whole buffer.
257 if (dimensionality_ == attribute_dimensionality) {
258 DRACO_DCHECK_EQ(offset_dimensionality, 0);
259 const size_t copy_size =
260 sizeof(internal_t) * attribute_dimensionality * n_items_;
261 std::memcpy(data0_, attribute_mem, copy_size);
262 } else { // chunk copy
263 const size_t copy_size = sizeof(internal_t) * attribute_dimensionality;
264 internal_t *internal_data;
265 const internal_t *attribute_data;
266 internal_t item;
267 for (internal_data = data0_ + offset_dimensionality,
268 attribute_data = attribute_mem, item = 0;
269 item < n_items_; internal_data += dimensionality_,
270 attribute_data += attribute_dimensionality, item += 1) {
271 std::memcpy(internal_data, attribute_data, copy_size);
272 }
273 }
274 }
275
276 private:
277 // internal parameters.
278 const uint32_t n_items_;
279 const uint32_t dimensionality_; // The dimension of the points in the buffer
280 const uint32_t item_size_bytes_;
281 std::vector<internal_t> data_; // contiguously stored data. Never resized.
282 internal_t *const data0_; // raw pointer to base data.
283};
284
285} // namespace draco
286
287#endif // DRACO_COMPRESSION_ATTRIBUTES_POINT_D_VECTOR_H_
int add(void *w)
Definition ui.cc:341
Definition point_d_vector.h:104
size_t difference_type
Definition point_d_vector.h:111
PointDVectorIterator & operator+=(const int32_t &add)
Definition point_d_vector.h:167
bool operator>(const PointDVectorIterator &ref) const
Definition point_d_vector.h:153
friend class PointDVector
Definition point_d_vector.h:105
PointDVectorIterator & operator=(const PointDVectorIterator &other)
Definition point_d_vector.h:139
bool operator==(const PointDVectorIterator &ref) const
Definition point_d_vector.h:144
bool operator!=(const PointDVectorIterator &ref) const
Definition point_d_vector.h:147
bool operator<(const PointDVectorIterator &ref) const
Definition point_d_vector.h:150
PointDVectorIterator operator-(const int32_t &sub) const
Definition point_d_vector.h:171
size_t operator-(const PointDVectorIterator &sub) const
Definition point_d_vector.h:175
std::random_access_iterator_tag iterator_category
Definition point_d_vector.h:109
PointDVector & reference
Definition point_d_vector.h:113
bool operator<=(const PointDVectorIterator &ref) const
Definition point_d_vector.h:156
PointDVectorIterator operator--(int32_t)
Definition point_d_vector.h:134
PseudoPointD< internal_t > operator*() const
Definition point_d_vector.h:117
PointDVectorIterator operator+(const int32_t &add) const
Definition point_d_vector.h:163
size_t value_type
Definition point_d_vector.h:110
bool operator>=(const PointDVectorIterator &ref) const
Definition point_d_vector.h:159
const PointDVectorIterator & operator++()
Definition point_d_vector.h:121
internal_t * operator[](const size_t &n) const
Definition point_d_vector.h:184
PointDVectorIterator & operator-=(const int32_t &sub)
Definition point_d_vector.h:179
PointDVectorIterator operator++(int32_t)
Definition point_d_vector.h:129
const PointDVectorIterator & operator--()
Definition point_d_vector.h:125
PointDVectorIterator(PointDVector *vec, size_t start_item)
Definition point_d_vector.h:189
PointDVector * pointer
Definition point_d_vector.h:112
void CopyAttribute(const internal_t attribute_dimensionality, const internal_t offset_dimensionality, const internal_t *const attribute_mem)
Definition point_d_vector.h:248
void CopyItem(const PointDVector &source, const internal_t source_index, const internal_t destination_index)
Definition point_d_vector.h:216
uint32_t size() const
Definition point_d_vector.h:212
PointDVectorIterator begin()
Definition point_d_vector.h:198
void CopyAttribute(const internal_t attribute_dimensionality, const internal_t offset_dimensionality, const internal_t index, const void *const attribute_item_data)
Definition point_d_vector.h:233
PointDVector(const uint32_t n_items, const uint32_t dimensionality)
Definition point_d_vector.h:97
PointDVectorIterator end()
Definition point_d_vector.h:199
const internal_t * operator[](const uint32_t index) const
Definition point_d_vector.h:207
internal_t * operator[](const uint32_t index)
Definition point_d_vector.h:203
size_t GetBufferSize() const
Definition point_d_vector.h:213
Definition point_d_vector.h:41
PseudoPointD(const PseudoPointD &other)
Definition point_d_vector.h:53
PseudoPointD(internal_t *mem, internal_t dimension)
Definition point_d_vector.h:43
bool operator!=(const PseudoPointD &other) const
Definition point_d_vector.h:73
void swap(PseudoPointD &other) noexcept
Definition point_d_vector.h:47
bool operator==(const PseudoPointD &other) const
Definition point_d_vector.h:65
internal_t & operator[](const size_t &n)
Definition point_d_vector.h:60
const internal_t & operator[](const size_t &n) const
Definition point_d_vector.h:56
#define DRACO_DCHECK_LT(a, b)
Definition macros.h:86
#define DRACO_DCHECK_EQ(a, b)
Definition macros.h:81
#define DRACO_DCHECK(x)
Definition macros.h:80
lazy data
Definition meshpage.php:1583
Definition attribute_octahedron_transform.cc:21
void swap(draco::PseudoPointD< internal_t > &&a, draco::PseudoPointD< internal_t > &&b) noexcept
Definition point_d_vector.h:84