Skip to content

Commit 1ad7ddb

Browse files
committed
Added typedef for dynamic byte array
1 parent c2a95c6 commit 1ad7ddb

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

python/MeshData.sip

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ class MeshData
2424
public:
2525
MeshData();
2626
virtual ~MeshData();
27-
std::vector<uint8_t> getVerticesAsBytes();
28-
std::vector<uint8_t> getFacesAsBytes();
27+
bytearray getVerticesAsBytes();
28+
bytearray getFacesAsBytes();
2929

30-
std::vector<uint8_t> getFlatVerticesAsBytes();
30+
bytearray getFlatVerticesAsBytes();
3131

32-
void setVerticesFromBytes(const std::vector<uint8_t>& data);
32+
void setVerticesFromBytes(const bytearray& data);
3333

34-
void setFacesFromBytes(const std::vector<uint8_t>& data);
34+
void setFacesFromBytes(const bytearray& data);
3535
};

python/Types.sip

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,12 @@
168168
* Convert to and from byte arrays.
169169
* Uses the internal data vector directly to create/extract a PyBytes* instance.
170170
*/
171-
%MappedType std::vector<uint8_t>
171+
%MappedType bytearray
172172
{
173173
%TypeHeaderCode
174174
#include <vector>
175175
#include <cstdint>
176+
#include "Types.h"
176177
%End
177178

178179
%ConvertFromTypeCode // From C++ to python
@@ -187,14 +188,14 @@
187188

188189
if (sipPy == Py_None)
189190
{
190-
*sipCppPtr = new std::vector<uint8_t>;
191+
*sipCppPtr = new bytearray;
191192
return 1;
192193
}
193194

194195
if (PyBytes_Check(sipPy))
195196
{
196197
uint8_t *buffer = reinterpret_cast<uint8_t *>(PyBytes_AS_STRING(sipPy));
197-
*sipCppPtr = new std::vector<uint8_t>(buffer, buffer + PyBytes_GET_SIZE(sipPy));
198+
*sipCppPtr = new bytearray(buffer, buffer + PyBytes_GET_SIZE(sipPy));
198199
return 1;
199200
}
200201
return 0;

src/MeshData.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ void MeshData::clear()
4343
this->vertices.clear();
4444
}
4545

46-
std::vector<uint8_t> MeshData::getVerticesAsBytes()
46+
bytearray MeshData::getVerticesAsBytes()
4747
{
48-
std::vector<uint8_t> vertices_data(vertices.size() * sizeof(float) * 3);
48+
bytearray vertices_data(vertices.size() * sizeof(float) * 3);
4949

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

62-
std::vector<uint8_t> MeshData::getFlatVerticesAsBytes()
62+
bytearray MeshData::getFlatVerticesAsBytes()
6363
{
64-
std::vector<uint8_t> vertices_data(faces.size() * sizeof(float) * 3 * 3);
64+
bytearray vertices_data(faces.size() * sizeof(float) * 3 * 3);
6565
for(int i = 0; i < faces.size(); i++)
6666
{
6767
int v1 = faces.at(i).getV1();
@@ -95,9 +95,9 @@ std::vector<uint8_t> MeshData::getFlatVerticesAsBytes()
9595
return vertices_data;
9696
}
9797

98-
std::vector<uint8_t> MeshData::getFacesAsBytes()
98+
bytearray MeshData::getFacesAsBytes()
9999
{
100-
std::vector<uint8_t> face_data(faces.size() * sizeof(int) * 3);
100+
bytearray face_data(faces.size() * sizeof(int) * 3);
101101

102102
for(int i = 0; i < faces.size(); i++)
103103
{
@@ -132,7 +132,7 @@ void MeshData::toXmlNode(pugi::xml_node& node)
132132
}
133133
}
134134

135-
void MeshData::setVerticesFromBytes(const std::vector<uint8_t>& data)
135+
void MeshData::setVerticesFromBytes(const bytearray& data)
136136
{
137137
vertices.clear();
138138
const uint8_t* bytes = data.data();
@@ -149,7 +149,7 @@ void MeshData::setVerticesFromBytes(const std::vector<uint8_t>& data)
149149
}
150150
}
151151

152-
void MeshData::setFacesFromBytes(const std::vector<uint8_t>& data)
152+
void MeshData::setFacesFromBytes(const bytearray& data)
153153
{
154154
faces.clear();
155155
const uint8_t* bytes = data.data();

src/MeshData.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <string>
2424
#include <cstdint>
2525

26+
#include "Types.h"
2627
#include "Vertex.h"
2728
#include "Face.h"
2829

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

6465
/**
6566
* Return the faces as flattend bytes.
6667
*
6768
* If there for example is a single face, it will return a byte array containing 3 ints (so 3 * 4 bytes)
6869
*/
69-
std::vector<uint8_t> getFacesAsBytes();
70+
bytearray getFacesAsBytes();
7071

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

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

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

9192
std::vector<Vertex> getVertices();
9293

src/Types.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
1+
/*
2+
* This file is part of libSavitar
3+
*
4+
* Copyright (C) 2017 Ultimaker b.v. <j.vankessel@ultimaker.com>
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published
8+
* by the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef SAVITAR_TYPES_H
20+
#define SAVITAR_TYPES_H
21+
22+
#include <vector>
23+
#include <cstdint>
24+
125
namespace Savitar
226
{
327
// Convenience typedef so uint can be used.
428
typedef unsigned int uint;
5-
}
29+
30+
// Dynamic array of bytes, defined here to increase code readability.
31+
typedef std::vector<uint8_t> bytearray;
32+
}
33+
34+
#endif

0 commit comments

Comments
 (0)