chrono_batch_render/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
89 / 127
70.1%
Branches:
54 / 121
44.6%
Functions:
20 / 28
71.4%

python/src/values.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2 #include "values.h"
3
4 #include <cstring>
5
6 #include <nanobind/stl/map.h>
7 #include <nanobind/stl/vector.h>
8
9 namespace nb = nanobind;
10 using namespace nb::literals;
11
12 namespace chrono::batch_render::python {
13
14 namespace {
15
16 156 template <class T> void AssignCell(T& out, nb::handle value, const char* dtype) {
17
10/18
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Double3>(chrono::batch_render::Double3&, nanobind::handle, char const*):
✓ Branch 2 taken 126 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 126 times.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float2>(chrono::batch_render::Float2&, nanobind::handle, char const*):
✓ Branch 2 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float3>(chrono::batch_render::Float3&, nanobind::handle, char const*):
✓ Branch 2 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float4>(chrono::batch_render::Float4&, nanobind::handle, char const*):
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Quatd>(chrono::batch_render::Quatd&, nanobind::handle, char const*):
✓ Branch 2 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Uint2>(chrono::batch_render::Uint2&, nanobind::handle, char const*):
✓ Branch 2 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
312 const std::vector<std::byte> bytes = NumpyBytes(value, dtype);
18
5/12
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Double3>(chrono::batch_render::Double3&, nanobind::handle, char const*):
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float2>(chrono::batch_render::Float2&, nanobind::handle, char const*):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float3>(chrono::batch_render::Float3&, nanobind::handle, char const*):
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float4>(chrono::batch_render::Float4&, nanobind::handle, char const*):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Quatd>(chrono::batch_render::Quatd&, nanobind::handle, char const*):
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Uint2>(chrono::batch_render::Uint2&, nanobind::handle, char const*):
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
156 if (bytes.size() != sizeof(T)) {
19
0/36
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Double3>(chrono::batch_render::Double3&, nanobind::handle, char const*):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float2>(chrono::batch_render::Float2&, nanobind::handle, char const*):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float3>(chrono::batch_render::Float3&, nanobind::handle, char const*):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float4>(chrono::batch_render::Float4&, nanobind::handle, char const*):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Quatd>(chrono::batch_render::Quatd&, nanobind::handle, char const*):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Uint2>(chrono::batch_render::Uint2&, nanobind::handle, char const*):
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ throw nb::value_error(("Expected " + std::to_string(sizeof(T) / 4) + " values, not " +
20
0/6
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Double3>(chrono::batch_render::Double3&, nanobind::handle, char const*):
✗ Branch 1 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float2>(chrono::batch_render::Float2&, nanobind::handle, char const*):
✗ Branch 1 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float3>(chrono::batch_render::Float3&, nanobind::handle, char const*):
✗ Branch 1 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Float4>(chrono::batch_render::Float4&, nanobind::handle, char const*):
✗ Branch 1 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Quatd>(chrono::batch_render::Quatd&, nanobind::handle, char const*):
✗ Branch 1 not taken.
void chrono::batch_render::python::(anonymous namespace)::AssignCell<chrono::batch_render::Uint2>(chrono::batch_render::Uint2&, nanobind::handle, char const*):
✗ Branch 1 not taken.
✗ std::to_string(bytes.size() / 4))
21 .c_str());
22 }
23 156 std::memcpy(&out, bytes.data(), sizeof(T));
24 156 }
25
26 }
27
28 41273 std::vector<std::byte> NumpyBytes(nb::handle value, const std::string& dtype) {
29 41273 const nb::object array =
30
2/2
✓ Branch 1 taken 41273 times.
✓ Branch 4 taken 41272 times.
123819 nb::module_::import_("numpy").attr("ascontiguousarray")(value, "dtype"_a = dtype);
31
1/1
✓ Branch 1 taken 41272 times.
82544 const nb::bytes bytes = nb::cast<nb::bytes>(array.attr("tobytes")());
32
1/1
✓ Branch 1 taken 41272 times.
41272 const auto* first = reinterpret_cast<const std::byte*>(bytes.c_str());
33
2/2
✓ Branch 1 taken 41272 times.
✓ Branch 4 taken 41272 times.
41272 return {first, first + bytes.size()};
34 41272 }
35
36 35 std::vector<std::string> Strings(nb::handle value) {
37
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 28 times.
35 if (nb::isinstance<nb::str>(value)) {
38
2/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
14 return {nb::cast<std::string>(value)};
39 }
40 28 std::vector<std::string> strings;
41
5/5
✓ Branch 1 taken 28 times.
✓ Branch 4 taken 42 times.
✓ Branch 7 taken 70 times.
✓ Branch 9 taken 42 times.
✓ Branch 10 taken 28 times.
70 for (nb::handle item : value) {
42
5/5
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 36 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 42 times.
✓ Branch 11 taken 42 times.
84 strings.push_back(nb::isinstance<ScenePrim>(item) ? nb::cast<ScenePrim&>(item).prim.Path()
43 : nb::cast<std::string>(item));
44 28 }
45 28 return strings;
46 35 }
47
48 16 uint32_t EnumIndex(nb::handle value, std::span<const std::string_view> tokens) {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!nb::isinstance<nb::str>(value)) {
50 15 return nb::cast<uint32_t>(value);
51 }
52 16 const std::string text = nb::cast<std::string>(value);
53
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 1 time.
26 for (uint32_t index = 0; index < tokens.size(); ++index) {
54
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 10 times.
25 if (tokens[index] == text) {
55 15 return index;
56 }
57 }
58 1 std::string allowed;
59
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 for (const std::string_view token : tokens) {
60
4/4
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 6 taken 2 times.
6 allowed += (allowed.empty() ? "" : ", ") + std::string(token);
61 }
62
2/3
✓ Branch 2 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
3 throw nb::value_error((text + " isn't one of " + allowed).c_str());
63 17 }
64
65 ✗ void Assign(bool& out, nb::handle value) {
66 ✗ out = nb::cast<bool>(value);
67 ✗ }
68
69 ✗ void Assign(int32_t& out, nb::handle value) {
70 ✗ out = nb::cast<int32_t>(value);
71 ✗ }
72
73 ✗ void Assign(uint32_t& out, nb::handle value) {
74 ✗ out = nb::cast<uint32_t>(value);
75 ✗ }
76
77 43 void Assign(float& out, nb::handle value) {
78 43 out = nb::cast<float>(value);
79 42 }
80
81 1 void Assign(Float2& out, nb::handle value) {
82 1 AssignCell(out, value, "float32");
83 1 }
84
85 12 void Assign(Uint2& out, nb::handle value) {
86 12 AssignCell(out, value, "uint32");
87 12 }
88
89 3 void Assign(Float3& out, nb::handle value) {
90 3 AssignCell(out, value, "float32");
91 3 }
92
93 ✗ void Assign(Float4& out, nb::handle value) {
94 ✗ AssignCell(out, value, "float32");
95 ✗ }
96
97 126 void Assign(Double3& out, nb::handle value) {
98 126 AssignCell(out, value, "float64");
99 126 }
100
101 14 void Assign(Quatd& out, nb::handle value) {
102 14 AssignCell(out, value, "float64");
103 14 }
104
105 32 void Assign(std::string& out, nb::handle value) {
106 32 out = nb::cast<std::string>(value);
107 32 }
108
109 25 void Assign(std::vector<std::string>& out, nb::handle value) {
110 50 out = Strings(value);
111 25 }
112
113 20 void Assign(std::vector<int32_t>& out, nb::handle value) {
114
2/2
✓ Branch 2 taken 20 times.
✓ Branch 5 taken 20 times.
60 out = Items<int32_t>(NumpyBytes(value, "int32"));
115 20 }
116
117 ✗ void Assign(std::vector<int64_t>& out, nb::handle value) {
118
0/2
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ out = Items<int64_t>(NumpyBytes(value, "int64"));
119 ✗ }
120
121 7 void Assign(std::vector<float>& out, nb::handle value) {
122
2/2
✓ Branch 2 taken 7 times.
✓ Branch 5 taken 7 times.
21 out = Items<float>(NumpyBytes(value, "float32"));
123 7 }
124
125 16 void Assign(std::vector<Float3>& out, nb::handle value) {
126
2/2
✓ Branch 2 taken 16 times.
✓ Branch 5 taken 16 times.
48 out = Items<Float3>(NumpyBytes(value, "float32"));
127 16 }
128
129 ✗ void Assign(std::vector<Float2>& out, nb::handle value) {
130
0/2
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ out = Items<Float2>(NumpyBytes(value, "float32"));
131 ✗ }
132
133 ✗ void Assign(std::vector<Float4>& out, nb::handle value) {
134
0/2
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ out = Items<Float4>(NumpyBytes(value, "float32"));
135 ✗ }
136
137 2 void Assign(std::vector<Primvar>& out, nb::handle value) {
138 2 out = nb::cast<std::vector<Primvar>>(value);
139 2 }
140
141 }
142