Skip to content

Commit 116aed7

Browse files
committed
updated with pybind11 tests
1 parent d8b6ce3 commit 116aed7

File tree

2 files changed

+105
-80
lines changed

2 files changed

+105
-80
lines changed

PyModules/skin_plus_plus/skin_plus_plus_pymxs/src/SkinPlusPlusPymxs/SkinPlusPlusPymxs.cpp

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,18 @@ std::vector<std::vector<std::vector <float>>> SkinData::getSkinWeights()
8888
//SkinArray skinDataArray(2, VertexArray(vertexCount));
8989
for (unsigned int vertexIndex = 0; vertexIndex < vertexCount; vertexIndex++)
9090
{
91-
int16_t influenceCount = this->iSkinContextData->GetNumAssignedBones(vertexIndex);
91+
auto influenceCount = this->iSkinContextData->GetNumAssignedBones(vertexIndex);
9292
std::vector<float> influenceWeights(influenceCount);
9393
std::vector<float> influenceBoneIDs(influenceCount);
9494
//influenceWeights[0] = 1.0f;
9595
//influenceWeights->data()[0] = 1.0f;
9696
skinDataArray[0][vertexIndex] = influenceWeights; //influenceWeights
9797
skinDataArray[1][vertexIndex] = influenceBoneIDs; //influenceBoneIDs
98-
for (int influenceIndex = 0; influenceIndex < influenceCount; influenceIndex++)
98+
for (auto influenceIndex = 0; influenceIndex < influenceCount; influenceIndex++)
9999
{
100-
float infuenceWeight = this->iSkinContextData->GetBoneWeight(vertexIndex, influenceIndex);
100+
auto infuenceWeight = this->iSkinContextData->GetBoneWeight(vertexIndex, influenceIndex);
101101
if (infuenceWeight <= 0.0f) continue;
102-
int influenceBoneID = this->iSkinContextData->GetAssignedBone(vertexIndex, influenceIndex);
102+
auto influenceBoneID = this->iSkinContextData->GetAssignedBone(vertexIndex, influenceIndex);
103103
influenceWeights[influenceIndex] = infuenceWeight;
104104
influenceBoneIDs[influenceIndex] = float(influenceBoneID);
105105
}
@@ -174,41 +174,33 @@ PYBIND11_MODULE(SkinPlusPlusPymxs, m) {
174174
.def("initialise", &SkinData::initialise)
175175
.def("get_skin_weights", &SkinData::getSkinWeightsPy)
176176
;
177-
178-
m.def("get_skin_weights", [&](wchar_t* name) {
177+
//def("__init__", [](...) { ... }, py::arg().noconvert(), py::arg("arg2") = false);
178+
m.def("get_skin_weights", [](wchar_t* name, int return_type) {
179179
std::vector<std::vector<std::vector <float>>> weights = getSkinWeights(name);
180-
return py::cast(weights);
180+
switch (return_type) {
181+
case 0:
182+
return py::cast(weights);
183+
case 1:
184+
return py::cast(weights, py::return_value_policy::take_ownership);
185+
case 2:
186+
return py::cast(weights, py::return_value_policy::copy);
187+
case 3:
188+
return py::cast(weights, py::return_value_policy::move);
189+
case 4:
190+
return py::cast(weights, py::return_value_policy::reference);
191+
case 5:
192+
return py::cast(weights, py::return_value_policy::reference_internal);
193+
case 6:
194+
return py::cast(weights, py::return_value_policy::automatic);
195+
case 7:
196+
return py::cast(weights, py::return_value_policy::automatic_reference);
197+
default:
198+
return py::cast(weights);
199+
}
200+
181201
}, "Get Skin Weights",
182-
py::arg("name")
183-
);
184-
m.def(
185-
"get_skin_weights_np", [&](wchar_t* name) {
186-
std::vector<std::vector<std::vector <float>>> weights = getSkinWeights(name);
187-
py::array npArray = py::cast(weights);
188-
return npArray;
189-
}, "Get Skin Weights as a numpy array",
190-
py::arg("name")
191-
);
192-
m.def("get_skin_weights_np_move", [&](wchar_t* name) {
193-
std::vector<std::vector<std::vector <float>>> weights = getSkinWeights(name);
194-
py::array npArray = py::cast(weights, py::return_value_policy::move);
195-
return npArray;
196-
}, "Get Skin Weights as a numpy array",
197-
py::arg("name")
198-
);
199-
m.def("get_skin_weights_np_take_ownership", [&](wchar_t* name) {
200-
std::vector<std::vector<std::vector <float>>> weights = getSkinWeights(name);
201-
py::array npArray = py::cast(weights, py::return_value_policy::take_ownership);
202-
return npArray;
203-
}, "Get Skin Weights as a numpy array",
204-
py::arg("name")
205-
);
206-
m.def("get_skin_weights_np_take_ownership", [&](wchar_t* name) {
207-
std::vector<std::vector<std::vector <float>>> weights = getSkinWeights(name);
208-
py::array npArray = py::cast(weights, py::return_value_policy::take_ownership);
209-
return npArray;
210-
}, "Get Skin Weights as a numpy array",
211-
py::arg("name")
202+
py::arg("name"),
203+
py::arg("return_type")
212204
);
213205
//m.def("f", []() {
214206
// // Allocate and initialize some data; make this big so

test/test.py

Lines changed: 76 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import numpy as np
34
import functools
45
import pathlib
56
import time
@@ -29,10 +30,10 @@
2930

3031
import SkinPlusPlusPymxs
3132

32-
skin_data = SkinPlusPlusPymxs.SkinData()
33-
skin_data.initialise("Sphere001")
34-
skin_weights = skin_data.getSkinWeights()
35-
print(skin_weights[0][1])
33+
# skin_data = SkinPlusPlusPymxs.SkinData()
34+
# skin_data.initialise("Sphere001")
35+
# skin_weights = skin_data.getSkinWeights()
36+
# print(skin_weights[0][1])
3637

3738

3839
__loops__ = 1
@@ -45,7 +46,7 @@ def get_loops():
4546
return __loops__
4647

4748

48-
def timer(data_dict: dict[str, tuple[float, Any]]) -> Callable:
49+
def timer(data_dict: dict[str, tuple[float, Any, str]]) -> Callable:
4950
def wrapper(function: Callable) -> Callable:
5051
@functools.wraps(function)
5152
def wrapper_timer(*args, **kwargs) -> Any:
@@ -54,7 +55,7 @@ def wrapper_timer(*args, **kwargs) -> Any:
5455
for _ in range(get_loops()):
5556
value = function(*args, **kwargs)
5657
run_time = time.perf_counter() - start_time
57-
data_dict[f"{function.__name__!r}"] = (run_time, value)
58+
data_dict[f"{function.__name__!r}"] = (run_time, value, function.__name__)
5859
return value
5960
return wrapper_timer
6061
return wrapper
@@ -91,7 +92,7 @@ def SetSkinWeights(node, boneIDs, weights) -> None:
9192
ReplaceVertexWeights(skinModifier, vertexIndex, boneIDs[vertexIndex], weights[vertexIndex])
9293

9394

94-
get_timer_dict: dict[str, tuple[float, Any]] = {}
95+
get_timer_dict: dict[str, tuple[float, Any, str]] = {}
9596
@timer(get_timer_dict)
9697
def mxs_GetSkinWeights(_obj):
9798
data = mxsGetSkinWeights(_obj)
@@ -100,6 +101,15 @@ def mxs_GetSkinWeights(_obj):
100101

101102
return weights, boneIDs
102103

104+
@timer(get_timer_dict)
105+
def mxs_GetSkinWeights_NP(_obj):
106+
data = mxsGetSkinWeights(_obj)
107+
108+
weights = np.array([np.array(list(weights), dtype=float) for weights in data[0]], dtype=float)
109+
boneIDs = np.array([np.array(list(boneIDs), dtype=float) for boneIDs in data[1]], dtype=float)
110+
# boneIDs = [list(boneIDs) for boneIDs in data[1]]
111+
112+
return weights, boneIDs
103113

104114
@timer(get_timer_dict)
105115
def pymxs_GetSkinWeights(_obj):
@@ -132,26 +142,37 @@ def cpppf_GetSkinWeights(_obj):
132142

133143
@timer(get_timer_dict)
134144
def pybind11_GetSkinWeights(_obj):
135-
136-
return SkinPlusPlusPymxs.get_skin_weights(_obj.Name)
145+
return SkinPlusPlusPymxs.get_skin_weights(_obj.Name, 0)
137146

138147
@timer(get_timer_dict)
139-
def pybind11np_GetSkinWeights(_obj):
148+
def pybind11_GetSkinWeights_take_ownership(_obj):
149+
return SkinPlusPlusPymxs.get_skin_weights(_obj.Name, 1)
140150

141-
return SkinPlusPlusPymxs.get_skin_weights_np(_obj.Name)
151+
@timer(get_timer_dict)
152+
def pybind11_GetSkinWeights_copy(_obj):
153+
return SkinPlusPlusPymxs.get_skin_weights(_obj.Name, 2)
142154

143155
@timer(get_timer_dict)
144-
def pybind11npmove_GetSkinWeights(_obj):
156+
def pybind11_GetSkinWeights_move(_obj):
157+
return SkinPlusPlusPymxs.get_skin_weights(_obj.Name, 3)
145158

146-
return SkinPlusPlusPymxs.get_skin_weights_np_move(_obj.Name)
159+
@timer(get_timer_dict)
160+
def pybind11_GetSkinWeights_reference(_obj):
161+
return SkinPlusPlusPymxs.get_skin_weights(_obj.Name, 4)
147162

148163
@timer(get_timer_dict)
149-
def pybind11nptakeownership_GetSkinWeights(_obj):
164+
def pybind11_GetSkinWeights_reference_internal(_obj):
165+
return SkinPlusPlusPymxs.get_skin_weights(_obj.Name, 5)
150166

151-
return SkinPlusPlusPymxs.get_skin_weights_np_take_ownership(_obj.Name)
167+
@timer(get_timer_dict)
168+
def pybind11_GetSkinWeights_automatic(_obj):
169+
return SkinPlusPlusPymxs.get_skin_weights(_obj.Name, 6)
152170

171+
@timer(get_timer_dict)
172+
def pybind11_GetSkinWeights_automatic_reference(_obj):
173+
return SkinPlusPlusPymxs.get_skin_weights(_obj.Name, 7)
153174

154-
set_timer_dict: dict[str, tuple[float, Any]] = {}
175+
set_timer_dict: dict[str, tuple[float, Any, str]] = {}
155176
@timer(set_timer_dict)
156177
def mxs_SetSkinWeights( _obj, _boneIDs, _weights):
157178
return SetSkinWeights(_obj, _boneIDs, _weights)
@@ -172,26 +193,22 @@ def cpppf_SetSkinWeights( _obj, _boneIDs, _weights):
172193
obj = mxRt.GetNodeByName("Sphere001")
173194

174195
get_function_list = (
175-
# pymxs_GetSkinWeights,
176-
# mxs_GetSkinWeights,
177-
# cppfp_GetSkinWeights,
178-
# cpppm_GetSkinWeights,
179-
# cpppf_GetSkinWeights,
196+
pymxs_GetSkinWeights,
197+
mxs_GetSkinWeights,
198+
mxs_GetSkinWeights_NP,
199+
cppfp_GetSkinWeights,
200+
cpppm_GetSkinWeights,
201+
cpppf_GetSkinWeights,
180202
pybind11_GetSkinWeights,
181-
pybind11np_GetSkinWeights,
182-
pybind11npmove_GetSkinWeights,
183-
pybind11nptakeownership_GetSkinWeights,
184-
# lambda: pybind11np_GetSkinWeights(obj),
203+
pybind11_GetSkinWeights_take_ownership,
204+
pybind11_GetSkinWeights_copy,
205+
pybind11_GetSkinWeights_move,
206+
pybind11_GetSkinWeights_reference,
207+
pybind11_GetSkinWeights_reference_internal,
208+
pybind11_GetSkinWeights_automatic,
209+
pybind11_GetSkinWeights_automatic_reference,
185210
)
186211

187-
# weights = [(0.5, 0.5) for _ in range(obj.Verts.Count)]
188-
# ids = [(1, 2) for _ in range(obj.Verts.Count)]
189-
# set_function_list = (
190-
# lambda: mxs_SetSkinWeights(obj, weights, ids),
191-
# lambda: cppfp_SetSkinWeights(obj, weights, ids),
192-
# lambda: cpppm_SetSkinWeights(obj, weights, ids),
193-
# lambda: cpppf_SetSkinWeights(obj, weights, ids),
194-
# )
195212

196213
set_loops(1)
197214
def run_functions(function_list, _obj):
@@ -202,15 +219,15 @@ def run_functions(function_list, _obj):
202219
print(type(result))
203220
print(len(result))
204221

205-
def process_results(time_data: dict[str, tuple[float, Any]]):
206-
times = []
207-
values = []
208-
for time, value in time_data.values():
209-
times.append(time)
210-
values.append(value)
211-
212-
max_time = max(times)
213-
for function_name, (time, value) in time_data.items():
222+
def process_results(time_data: "dict[str, tuple[float, Any, str]]"):
223+
# times = []
224+
# values = []
225+
data = list(time_data.values())
226+
data.sort(key=lambda x: x[0])
227+
max_time = data[-1][0]
228+
data.reverse()
229+
# max_time = max(times)
230+
for time, _, function_name in data:
214231
percentage_ratio = (max_time / time)
215232
message = f"{function_name}: {time} -"
216233
if percentage_ratio == 1.0:
@@ -223,4 +240,20 @@ def process_results(time_data: dict[str, tuple[float, Any]]):
223240
run_functions(get_function_list, obj)
224241
process_results(get_timer_dict)
225242

226-
# print(np_GetSkinWeights())
243+
244+
| Function | Time-secs | x Faster | % Faster |
245+
|---------------------------------------------|---------------------|----------------------|----------------------|
246+
| pymxs_GetSkinWeights | 20.34769090000009 | base line | base line |
247+
| mxs_GetSkinWeights_NP | 15.51825759999997 | 1.3112097649416599 x | 131.12097649416597 % |
248+
| mxs_GetSkinWeights | 14.42323169999986 | 1.4107580966060669 x | 141.0758096606067 % |
249+
| cpppf_GetSkinWeights | 7.435437399999955 | 2.7365829076847867 x | 273.65829076847865 % |
250+
| cppfp_GetSkinWeights | 6.338866400000143 | 3.2099889185232917 x | 320.99889185232917 % |
251+
| cpppm_GetSkinWeights | 5.98266609999996 | 3.4011075597216136 x | 340.11075597216137 % |
252+
| pybind11_GetSkinWeights_automatic | 1.2681291999999758 | 16.045439928360988 x | 1604.5439928360988 % |
253+
| pybind11_GetSkinWeights_move | 1.09791139999993 | 18.533090101807293 x | 1853.3090101807293 % |
254+
| pybind11_GetSkinWeights_copy | 0.9864563000000999 | 20.627057579740764 x | 2062.7057579740763 % |
255+
| pybind11_GetSkinWeights | 0.9028401000000486 | 22.537424844110262 x | 2253.7424844110265 % |
256+
| pybind11_GetSkinWeights_reference_internal | 0.4243109000001368 | 47.954674037347445 x | 4795.467403734745 % |
257+
| pybind11_GetSkinWeights_automatic_reference | 0.4236172999999326 | 48.03319151508526 x | 4803.3191515085255 % |
258+
| pybind11_GetSkinWeights_take_ownership | 0.41753419999986363 | 48.73299217167536 x | 4873.299217167536 % |
259+
| pybind11_GetSkinWeights_reference | 0.41740709999999126 | 48.747831313843285 x | 4874.783131384329 % |

0 commit comments

Comments
 (0)