mirror of
https://git.mirrors.martin98.com/https://github.com/google/draco
synced 2025-08-12 02:29:00 +08:00
Opt: Use move semantics to avoid some data copies (#522)
* Opt: Use move semantics to avoid some data copies * Update decoder_webidl_wrapper.cc
This commit is contained in:
parent
ad58758459
commit
9e4d51bbcb
@ -46,7 +46,7 @@ void MetadataQuerier::GetIntEntryArray(const draco::Metadata &metadata,
|
||||
const std::string name(entry_name);
|
||||
std::vector<int32_t> values;
|
||||
metadata.GetEntryIntArray(name, &values);
|
||||
out_values->SetValues(values.data(), values.size());
|
||||
out_values->MoveData(std::move(values));
|
||||
}
|
||||
|
||||
double MetadataQuerier::GetDoubleEntry(const Metadata &metadata,
|
||||
@ -86,73 +86,6 @@ const char *MetadataQuerier::GetEntryName(const Metadata &metadata,
|
||||
return entry_names_[entry_id].c_str();
|
||||
}
|
||||
|
||||
DracoFloat32Array::DracoFloat32Array() {}
|
||||
|
||||
float DracoFloat32Array::GetValue(int index) const { return values_[index]; }
|
||||
|
||||
bool DracoFloat32Array::SetValues(const float *values, int count) {
|
||||
if (values) {
|
||||
values_.assign(values, values + count);
|
||||
} else {
|
||||
values_.resize(count);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
DracoInt8Array::DracoInt8Array() {}
|
||||
|
||||
int8_t DracoInt8Array::GetValue(int index) const { return values_[index]; }
|
||||
|
||||
bool DracoInt8Array::SetValues(const int8_t *values, int count) {
|
||||
values_.assign(values, values + count);
|
||||
return true;
|
||||
}
|
||||
|
||||
DracoUInt8Array::DracoUInt8Array() {}
|
||||
|
||||
uint8_t DracoUInt8Array::GetValue(int index) const { return values_[index]; }
|
||||
|
||||
bool DracoUInt8Array::SetValues(const uint8_t *values, int count) {
|
||||
values_.assign(values, values + count);
|
||||
return true;
|
||||
}
|
||||
|
||||
DracoInt16Array::DracoInt16Array() {}
|
||||
|
||||
int16_t DracoInt16Array::GetValue(int index) const { return values_[index]; }
|
||||
|
||||
bool DracoInt16Array::SetValues(const int16_t *values, int count) {
|
||||
values_.assign(values, values + count);
|
||||
return true;
|
||||
}
|
||||
|
||||
DracoUInt16Array::DracoUInt16Array() {}
|
||||
|
||||
uint16_t DracoUInt16Array::GetValue(int index) const { return values_[index]; }
|
||||
|
||||
bool DracoUInt16Array::SetValues(const uint16_t *values, int count) {
|
||||
values_.assign(values, values + count);
|
||||
return true;
|
||||
}
|
||||
|
||||
DracoInt32Array::DracoInt32Array() {}
|
||||
|
||||
int DracoInt32Array::GetValue(int index) const { return values_[index]; }
|
||||
|
||||
bool DracoInt32Array::SetValues(const int *values, int count) {
|
||||
values_.assign(values, values + count);
|
||||
return true;
|
||||
}
|
||||
|
||||
DracoUInt32Array::DracoUInt32Array() {}
|
||||
|
||||
uint32_t DracoUInt32Array::GetValue(int index) const { return values_[index]; }
|
||||
|
||||
bool DracoUInt32Array::SetValues(const uint32_t *values, int count) {
|
||||
values_.assign(values, values + count);
|
||||
return true;
|
||||
}
|
||||
|
||||
Decoder::Decoder() {}
|
||||
|
||||
draco_EncodedGeometryType Decoder::GetEncodedGeometryType(
|
||||
@ -204,8 +137,9 @@ bool Decoder::GetFaceFromMesh(const Mesh &m,
|
||||
draco::FaceIndex::ValueType face_id,
|
||||
DracoInt32Array *out_values) {
|
||||
const Mesh::Face &face = m.face(draco::FaceIndex(face_id));
|
||||
return out_values->SetValues(reinterpret_cast<const int *>(face.data()),
|
||||
face.size());
|
||||
const auto ptr = reinterpret_cast<const int32_t *>(face.data());
|
||||
out_values->MoveData(std::vector<int32_t>({ptr, ptr + face.size()}));
|
||||
return true;
|
||||
}
|
||||
|
||||
long Decoder::GetTriangleStripsFromMesh(const Mesh &m,
|
||||
@ -216,8 +150,7 @@ long Decoder::GetTriangleStripsFromMesh(const Mesh &m,
|
||||
m, std::back_inserter(strip_indices))) {
|
||||
return 0;
|
||||
}
|
||||
if (!strip_values->SetValues(strip_indices.data(), strip_indices.size()))
|
||||
return 0;
|
||||
strip_values->MoveData(std::move(strip_indices));
|
||||
return stripifier.num_strips();
|
||||
}
|
||||
|
||||
@ -229,7 +162,8 @@ bool Decoder::GetAttributeFloat(const PointAttribute &pa,
|
||||
float values[kMaxAttributeFloatValues] = {-2.0, -2.0, -2.0, -2.0};
|
||||
if (!pa.ConvertValue<float>(draco::AttributeValueIndex(val_index), values))
|
||||
return false;
|
||||
return out_values->SetValues(values, components);
|
||||
out_values->MoveData({values, values + components});
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Decoder::GetAttributeFloatForAllPoints(const PointCloud &pc,
|
||||
@ -242,7 +176,7 @@ bool Decoder::GetAttributeFloatForAllPoints(const PointCloud &pc,
|
||||
float values[kMaxAttributeFloatValues] = {-2.0, -2.0, -2.0, -2.0};
|
||||
int entry_id = 0;
|
||||
|
||||
out_values->SetValues(nullptr, num_entries);
|
||||
out_values->Resize(num_entries);
|
||||
for (draco::PointIndex i(0); i < num_points; ++i) {
|
||||
const draco::AttributeValueIndex val_index = pa.mapped_index(i);
|
||||
if (!pa.ConvertValue<float>(val_index, values))
|
||||
|
@ -33,108 +33,31 @@ typedef draco::Status::Code draco_StatusCode;
|
||||
|
||||
// To generate Draco JavaScript bindings you must have emscripten installed.
|
||||
// Then run make -f Makefile.emcc jslib.
|
||||
|
||||
class DracoFloat32Array {
|
||||
template <typename T>
|
||||
class DracoArray {
|
||||
public:
|
||||
DracoFloat32Array();
|
||||
float GetValue(int index) const;
|
||||
T GetValue(int index) const { return values_[index]; }
|
||||
|
||||
// In case |values| is nullptr, the data is allocated but not initialized.
|
||||
bool SetValues(const float *values, int count);
|
||||
void Resize(int size) { values_.resize(size); }
|
||||
void MoveData(std::vector<T> &&values) { values_ = std::move(values); }
|
||||
|
||||
// Directly sets a value for a specific index. The array has to be already
|
||||
// allocated at this point (using SetValues() method).
|
||||
void SetValue(int index, float val) { values_[index] = val; }
|
||||
// allocated at this point (using Resize() method).
|
||||
void SetValue(int index, T val) { values_[index] = val; }
|
||||
|
||||
int size() const { return values_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<float> values_;
|
||||
std::vector<T> values_;
|
||||
};
|
||||
|
||||
class DracoInt8Array {
|
||||
public:
|
||||
DracoInt8Array();
|
||||
|
||||
int8_t GetValue(int index) const;
|
||||
bool SetValues(const int8_t *values, int count);
|
||||
|
||||
void SetValue(int index, int8_t val) { values_[index] = val; }
|
||||
|
||||
int size() const { return values_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<int8_t> values_;
|
||||
};
|
||||
|
||||
class DracoUInt8Array {
|
||||
public:
|
||||
DracoUInt8Array();
|
||||
uint8_t GetValue(int index) const;
|
||||
bool SetValues(const uint8_t *values, int count);
|
||||
|
||||
void SetValue(int index, uint8_t val) { values_[index] = val; }
|
||||
int size() const { return values_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<uint8_t> values_;
|
||||
};
|
||||
|
||||
class DracoInt16Array {
|
||||
public:
|
||||
DracoInt16Array();
|
||||
|
||||
int16_t GetValue(int index) const;
|
||||
bool SetValues(const int16_t *values, int count);
|
||||
|
||||
void SetValue(int index, int16_t val) { values_[index] = val; }
|
||||
|
||||
int size() const { return values_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<int16_t> values_;
|
||||
};
|
||||
|
||||
class DracoUInt16Array {
|
||||
public:
|
||||
DracoUInt16Array();
|
||||
uint16_t GetValue(int index) const;
|
||||
bool SetValues(const uint16_t *values, int count);
|
||||
|
||||
void SetValue(int index, uint16_t val) { values_[index] = val; }
|
||||
int size() const { return values_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<uint16_t> values_;
|
||||
};
|
||||
|
||||
class DracoInt32Array {
|
||||
public:
|
||||
DracoInt32Array();
|
||||
|
||||
int32_t GetValue(int index) const;
|
||||
bool SetValues(const int *values, int count);
|
||||
|
||||
void SetValue(int index, int32_t val) { values_[index] = val; }
|
||||
|
||||
int size() const { return values_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<int32_t> values_;
|
||||
};
|
||||
|
||||
class DracoUInt32Array {
|
||||
public:
|
||||
DracoUInt32Array();
|
||||
uint32_t GetValue(int index) const;
|
||||
bool SetValues(const uint32_t *values, int count);
|
||||
|
||||
void SetValue(int index, uint32_t val) { values_[index] = val; }
|
||||
int size() const { return values_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<uint32_t> values_;
|
||||
};
|
||||
using DracoFloat32Array = DracoArray<float>;
|
||||
using DracoInt8Array = DracoArray<int8_t>;
|
||||
using DracoUInt8Array = DracoArray<uint8_t>;
|
||||
using DracoInt16Array = DracoArray<int16_t>;
|
||||
using DracoUInt16Array = DracoArray<uint16_t>;
|
||||
using DracoInt32Array = DracoArray<int32_t>;
|
||||
using DracoUInt32Array = DracoArray<uint32_t>;
|
||||
|
||||
class MetadataQuerier {
|
||||
public:
|
||||
@ -299,9 +222,8 @@ class Decoder {
|
||||
pa.data_type() == draco_unsigned_type) &&
|
||||
pa.is_mapping_identity()) {
|
||||
// Copy values directly to the output vector.
|
||||
out_values->SetValues(reinterpret_cast<const ValueTypeT *>(
|
||||
pa.GetAddress(draco::AttributeValueIndex(0))),
|
||||
num_entries);
|
||||
const auto ptr = pa.GetAddress(draco::AttributeValueIndex(0));
|
||||
out_values->MoveData({ptr, ptr + num_entries});
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -309,7 +231,7 @@ class Decoder {
|
||||
std::vector<ValueTypeT> values(components);
|
||||
int entry_id = 0;
|
||||
|
||||
out_values->SetValues(nullptr, num_entries);
|
||||
out_values->Resize(num_entries);
|
||||
for (draco::PointIndex i(0); i < num_points; ++i) {
|
||||
const draco::AttributeValueIndex val_index = pa.mapped_index(i);
|
||||
if (!pa.ConvertValue<ValueTypeT>(val_index, &values[0]))
|
||||
|
Loading…
x
Reference in New Issue
Block a user