ddiakopoulos / tinyply

:earth_africa: C++11 ply 3d mesh format importer & exporter
598 stars 118 forks source link

Write methods should respect const input #39

Closed rgb2hsv closed 4 years ago

rgb2hsv commented 4 years ago

The add_properties_to_element() should have "const uint_t* data" because the write routine should principally never modify the input mesh. The more generic WriteMesh() upstream enforces a const assumption on all wrtiers, so tinyply has to be modified for things to work.

Changing the data type has consequences to other pieces of tinyply because the data is wrapped in Buffer(). One solution is to add another constant pointer and use getConstant() when writing parameters. There needs to be some sanity checking that we're using the right pointer at the right time.

class Buffer
{
    uint8_t * alias{ nullptr };
    const uint8_t * calias{ nullptr };
    struct delete_array { void operator()(uint8_t * p) { delete[] p; } };
    std::unique_ptr<uint8_t, decltype(Buffer::delete_array())> data;
    size_t size {0};
public:
    Buffer() {};
    Buffer(const size_t size) : data(new uint8_t[size], delete_array()), size(size) { alias = data.get(); } // allocating
    Buffer(uint8_t * ptr): alias(ptr) { } // non-allocating, todo: set size?
    Buffer(const uint8_t * ptr): calias(ptr) { }
    uint8_t * get() { return alias; }
    const uint8_t* getConstant() const { return calias; }
    size_t size_bytes() const { return size; }
};
ddiakopoulos commented 4 years ago

Thanks @rgb2hsv I'll fix this soon

ddiakopoulos commented 4 years ago

Fixed in ca7b279fb6c9af931ffdaed96a3b11ca3ccd79ea