Skip to content

Commit

Permalink
Added typedef for dynamic byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
onitake committed May 1, 2017
1 parent c2a95c6 commit 1ad7ddb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
10 changes: 5 additions & 5 deletions python/MeshData.sip
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class MeshData
public:
MeshData();
virtual ~MeshData();
std::vector<uint8_t> getVerticesAsBytes();
std::vector<uint8_t> getFacesAsBytes();
bytearray getVerticesAsBytes();
bytearray getFacesAsBytes();

std::vector<uint8_t> getFlatVerticesAsBytes();
bytearray getFlatVerticesAsBytes();

void setVerticesFromBytes(const std::vector<uint8_t>& data);
void setVerticesFromBytes(const bytearray& data);

void setFacesFromBytes(const std::vector<uint8_t>& data);
void setFacesFromBytes(const bytearray& data);
};
7 changes: 4 additions & 3 deletions python/Types.sip
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,12 @@
* Convert to and from byte arrays.
* Uses the internal data vector directly to create/extract a PyBytes* instance.
*/
%MappedType std::vector<uint8_t>
%MappedType bytearray
{
%TypeHeaderCode
#include <vector>
#include <cstdint>
#include "Types.h"
%End

%ConvertFromTypeCode // From C++ to python
Expand All @@ -187,14 +188,14 @@

if (sipPy == Py_None)
{
*sipCppPtr = new std::vector<uint8_t>;
*sipCppPtr = new bytearray;
return 1;
}

if (PyBytes_Check(sipPy))
{
uint8_t *buffer = reinterpret_cast<uint8_t *>(PyBytes_AS_STRING(sipPy));
*sipCppPtr = new std::vector<uint8_t>(buffer, buffer + PyBytes_GET_SIZE(sipPy));
*sipCppPtr = new bytearray(buffer, buffer + PyBytes_GET_SIZE(sipPy));
return 1;
}
return 0;
Expand Down
16 changes: 8 additions & 8 deletions src/MeshData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ void MeshData::clear()
this->vertices.clear();
}

std::vector<uint8_t> MeshData::getVerticesAsBytes()
bytearray MeshData::getVerticesAsBytes()
{
std::vector<uint8_t> vertices_data(vertices.size() * sizeof(float) * 3);
bytearray vertices_data(vertices.size() * sizeof(float) * 3);

for(int i = 0; i < vertices.size(); i++)
{
Expand All @@ -59,9 +59,9 @@ std::vector<uint8_t> MeshData::getVerticesAsBytes()
return vertices_data;
}

std::vector<uint8_t> MeshData::getFlatVerticesAsBytes()
bytearray MeshData::getFlatVerticesAsBytes()
{
std::vector<uint8_t> vertices_data(faces.size() * sizeof(float) * 3 * 3);
bytearray vertices_data(faces.size() * sizeof(float) * 3 * 3);
for(int i = 0; i < faces.size(); i++)
{
int v1 = faces.at(i).getV1();
Expand Down Expand Up @@ -95,9 +95,9 @@ std::vector<uint8_t> MeshData::getFlatVerticesAsBytes()
return vertices_data;
}

std::vector<uint8_t> MeshData::getFacesAsBytes()
bytearray MeshData::getFacesAsBytes()
{
std::vector<uint8_t> face_data(faces.size() * sizeof(int) * 3);
bytearray face_data(faces.size() * sizeof(int) * 3);

for(int i = 0; i < faces.size(); i++)
{
Expand Down Expand Up @@ -132,7 +132,7 @@ void MeshData::toXmlNode(pugi::xml_node& node)
}
}

void MeshData::setVerticesFromBytes(const std::vector<uint8_t>& data)
void MeshData::setVerticesFromBytes(const bytearray& data)
{
vertices.clear();
const uint8_t* bytes = data.data();
Expand All @@ -149,7 +149,7 @@ void MeshData::setVerticesFromBytes(const std::vector<uint8_t>& data)
}
}

void MeshData::setFacesFromBytes(const std::vector<uint8_t>& data)
void MeshData::setFacesFromBytes(const bytearray& data)
{
faces.clear();
const uint8_t* bytes = data.data();
Expand Down
11 changes: 6 additions & 5 deletions src/MeshData.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <string>
#include <cstdint>

#include "Types.h"
#include "Vertex.h"
#include "Face.h"

Expand Down Expand Up @@ -59,34 +60,34 @@ namespace Savitar
*
* If there for example is a single vertex, it will return a byte array containing 3 floats (so 3 * 4 bytes)
*/
std::vector<uint8_t> getVerticesAsBytes();
bytearray getVerticesAsBytes();

/**
* Return the faces as flattend bytes.
*
* If there for example is a single face, it will return a byte array containing 3 ints (so 3 * 4 bytes)
*/
std::vector<uint8_t> getFacesAsBytes();
bytearray getFacesAsBytes();

/**
* Instead of getting all unique vertices, this function returns a bytearray with 3 vertices per face.
* This is usefull if you want to mimic the data type of STL files.
*/
std::vector<uint8_t> getFlatVerticesAsBytes();
bytearray getFlatVerticesAsBytes();

/**
* Set the vertices of the meshdata by bytearray (as set from python)
*
* For every vertex it's assumed that there are 12 bytes (3 floats * 4).
*/
void setVerticesFromBytes(const std::vector<uint8_t>& data);
void setVerticesFromBytes(const bytearray& data);

/**
* Set the faces of the meshdata by bytearray (as set from python)
*
* For every face it's assumed that there are 12 bytes (3 int * 4).
*/
void setFacesFromBytes(const std::vector<uint8_t>& data);
void setFacesFromBytes(const bytearray& data);

std::vector<Vertex> getVertices();

Expand Down
31 changes: 30 additions & 1 deletion src/Types.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
/*
* This file is part of libSavitar
*
* Copyright (C) 2017 Ultimaker b.v. <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef SAVITAR_TYPES_H
#define SAVITAR_TYPES_H

#include <vector>
#include <cstdint>

namespace Savitar
{
// Convenience typedef so uint can be used.
typedef unsigned int uint;
}

// Dynamic array of bytes, defined here to increase code readability.
typedef std::vector<uint8_t> bytearray;
}

#endif

0 comments on commit 1ad7ddb

Please sign in to comment.