-
-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathMesh.cpp
More file actions
210 lines (196 loc) · 6.55 KB
/
Mesh.cpp
File metadata and controls
210 lines (196 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "Mesh.hpp"
#include <Python.h>
#include <swap.hpp>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
enum VertexFormat
{
kVertexFormatFloat,
kVertexFormatFloat16,
kVertexFormatUNorm8,
kVertexFormatSNorm8,
kVertexFormatUNorm16,
kVertexFormatSNorm16,
kVertexFormatUInt8,
kVertexFormatSInt8,
kVertexFormatUInt16,
kVertexFormatSInt16,
kVertexFormatUInt32,
kVertexFormatSInt32
};
PyObject *unpack_vertexdata(PyObject *self, PyObject *args)
{
// define vars
int componentByteSize;
uint32_t m_VertexCount;
uint8_t swap;
// char format;
Py_buffer vertexDataView;
uint32_t m_StreamOffset;
uint32_t m_StreamStride;
uint32_t m_ChannelOffset;
uint32_t m_ChannelDimension;
if (!PyArg_ParseTuple(args, "y*iIIIIIb", &vertexDataView, &componentByteSize, &m_VertexCount, &m_StreamOffset, &m_StreamStride, &m_ChannelOffset, &m_ChannelDimension, &swap))
{
if (vertexDataView.buf)
{
PyBuffer_Release(&vertexDataView);
}
return NULL;
}
uint8_t *vertexData = (uint8_t *)vertexDataView.buf;
Py_ssize_t componentBytesLength = m_VertexCount * m_ChannelDimension * componentByteSize;
// check if max values are ok
uint32_t maxVertexDataAccess = (m_VertexCount - 1) * m_StreamStride + m_ChannelOffset + m_StreamOffset + componentByteSize * (m_ChannelDimension - 1) + componentByteSize;
if (maxVertexDataAccess > vertexDataView.len)
{
PyBuffer_Release(&vertexDataView);
PyErr_SetString(PyExc_ValueError, "Vertex data access out of bounds");
return NULL;
}
PyObject *res = PyBytes_FromStringAndSize(nullptr, componentBytesLength);
if (!res)
{
PyBuffer_Release(&vertexDataView);
return NULL;
}
uint8_t *componentBytes = (uint8_t *)PyBytes_AS_STRING(res);
for (uint32_t v = 0; v < m_VertexCount; v++)
{
uint32_t vertexOffset = m_StreamOffset + m_ChannelOffset + m_StreamStride * v;
for (uint32_t d = 0; d < m_ChannelDimension; d++)
{
uint32_t vertexDataOffset = vertexOffset + componentByteSize * d;
uint32_t componentOffset = componentByteSize * (v * m_ChannelDimension + d);
memcpy(componentBytes + componentOffset, vertexData + vertexDataOffset, componentByteSize);
}
}
if (swap) // swap bytes
{
if (componentByteSize == 2)
{
uint16_t *componentUints = (uint16_t *)componentBytes;
for (uint32_t i = 0; i < componentBytesLength; i += 2)
{
swap_any_inplace(componentUints++);
}
}
else if (componentByteSize == 4)
{
uint32_t *componentUints = (uint32_t *)componentBytes;
for (uint32_t i = 0; i < componentBytesLength; i += 4)
{
swap_any_inplace(componentUints++);
}
}
}
PyBuffer_Release(&vertexDataView);
return res;
// fast enough in Python
// uint32_t itemCount = componentBytesLength / componentByteSize;
// PyObject *lst = PyList_New(itemCount);
// if (!lst)
// return NULL;
// switch (format)
// {
// case kVertexFormatFloat:
// {
// float *items = (float *)componentBytes;
// for (uint32_t i = 0; i < itemCount; i++)
// {
// PyList_SetItem(lst, i, PyFloat_FromDouble((double)*items++));
// }
// // result[i] = BitConverter.ToSingle(inputBytes, i * 4);
// break;
// }
// case kVertexFormatFloat16:
// {
// uint16_t *items = (uint16_t *)componentBytes;
// for (uint32_t i = 0; i < itemCount; i++)
// {
// double x = _PyFloat_Unpack2(items++, 0);
// if (x == -1.0 && PyErr_Occurred())
// {
// return NULL;
// }
// PyList_SetItem(lst, i, PyFloat_FromDouble(x));
// }
// // result[i] = Half.ToHalf(inputBytes, i * 2);
// break;
// }
// case kVertexFormatUNorm8:
// {
// uint8_t *items = componentBytes;
// for (uint32_t i = 0; i < itemCount; i++)
// {
// PyList_SetItem(lst, i, PyFloat_FromDouble((double)(*items++ / 255.0f)));
// }
// // result[i] = inputBytes[i] / 255f;
// break;
// }
// case kVertexFormatSNorm8:
// {
// int8_t *items = (int8_t *)componentBytes;
// for (uint32_t i = 0; i < itemCount; i++)
// {
// PyList_SetItem(lst, i, PyFloat_FromDouble((double)MAX((*items++ / 127.0f), -1.0f)));
// }
// // result[i] = Math.Max((sbyte)inputBytes[i] / 127f, -1f);
// break;
// }
// case kVertexFormatUNorm16:
// {
// uint16_t *items = (uint16_t *)componentBytes;
// for (uint32_t i = 0; i < itemCount; i++)
// {
// PyList_SetItem(lst, i, PyFloat_FromDouble((double)(*items++ / 65535.0f)));
// }
// // result[i] = BitConverter.ToUInt16(inputBytes, i * 2) / 65535f;
// break;
// }
// case kVertexFormatSNorm16:
// {
// int16_t *items = (int16_t *)componentBytes;
// for (uint32_t i = 0; i < itemCount; i++)
// {
// PyList_SetItem(lst, i, PyFloat_FromDouble((double)MAX((*items++ / 32767.0f), -1.0f)));
// }
// // result[i] = Math.Max(BitConverter.ToInt16(inputBytes, i * 2) / 32767f, -1f);
// break;
// }
// case kVertexFormatUInt8:
// case kVertexFormatSInt8:
// {
// uint8_t *items = componentBytes;
// for (uint32_t i = 0; i < itemCount; i++)
// {
// PyList_SetItem(lst, i, PyLong_FromUnsignedLong((uint32_t)*items++));
// }
// // result[i] = inputBytes[i];
// break;
// }
// case kVertexFormatUInt16:
// case kVertexFormatSInt16:
// {
// uint16_t *items = (uint16_t *)componentBytes;
// for (uint32_t i = 0; i < itemCount; i++)
// {
// PyList_SetItem(lst, i, PyLong_FromUnsignedLong((uint32_t)*items++));
// }
// // result[i] = BitConverter.ToInt16(inputBytes, i * 2);
// break;
// }
// case kVertexFormatUInt32:
// case kVertexFormatSInt32:
// {
// uint32_t *items = (uint32_t *)componentBytes;
// for (uint32_t i = 0; i < itemCount; i++)
// {
// PyList_SetItem(lst, i, PyLong_FromUnsignedLong(*items++));
// }
// // result[i] = BitConverter.ToInt32(inputBytes, i * 4);
// break;
// }
// }
// free(componentBytes);
// return lst;
}