Skip to content

Commit 2263255

Browse files
committed
Refine libnest2d_js bindings: introduce ItemArray type, optimize array handling in nestWrapper, and improve code style consistency.
1 parent 9d8a1da commit 2263255

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

libnest2d_js/libnest2d_js.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,23 @@ EMSCRIPTEN_DECLARE_VAL_TYPE(PointList);
3232
EMSCRIPTEN_DECLARE_VAL_TYPE(ItemList);
3333
EMSCRIPTEN_DECLARE_VAL_TYPE(DoubleList);
3434

35+
// Define a more specific type for Item arrays
36+
using ItemArray = Item[];
37+
3538
// Helper function to convert JavaScript arrays to std::vector<Point>
3639
std::vector<Point> jsArrayToPointVector(const emscripten::val& jsArray) {
3740
std::vector<Point> vertices;
3841
unsigned length = jsArray["length"].as<unsigned>();
3942
vertices.reserve(length);
40-
43+
4144
for (unsigned i = 0; i < length; i++) {
4245
emscripten::val jsPoint = jsArray[i];
4346
// Use property access instead of method calls for better compatibility
4447
long x = jsPoint["x"].as<long>();
4548
long y = jsPoint["y"].as<long>();
4649
vertices.emplace_back(x, y);
4750
}
48-
51+
4952
return vertices;
5053
}
5154

@@ -63,41 +66,41 @@ std::vector<double> jsArrayToVectorDouble(const emscripten::val& jsArray) {
6366
std::vector<double> vec;
6467
unsigned length = jsArray["length"].as<unsigned>();
6568
vec.reserve(length);
66-
69+
6770
for (unsigned i = 0; i < length; i++) {
6871
vec.push_back(jsArray[i].as<double>());
6972
}
70-
73+
7174
return vec;
7275
}
7376

7477
// Wrapper function for nest() to handle JavaScript arrays
75-
size_t nestWrapper(emscripten::val jsItems, const Box& bin, long distance = 1, const NfpConfig& config = NfpConfig()) {
76-
// Convert JavaScript array to std::vector<Item>
77-
std::vector<Item> items;
78-
unsigned length = jsItems["length"].as<unsigned>();
79-
items.reserve(length);
80-
81-
for (unsigned i = 0; i < length; i++) {
82-
Item item = jsItems[i].as<Item>();
83-
items.push_back(item);
84-
}
78+
size_t nestWrapper(const ItemArray& jsItems, const Box& bin, long distance = 1, const NfpConfig& config = NfpConfig()) {
79+
// Validate that jsItems is actually an array
80+
// if (!jsItems.isArray()) {
81+
// throw std::invalid_argument("First parameter must be an array of Items");
82+
// }
8583

84+
// // Convert JavaScript array to std::vector<Item>
85+
// std::vector<Item> items;
86+
// unsigned length = jsItems["length"].as<unsigned>();
87+
// items.reserve(length);
88+
89+
// for (unsigned i = 0; i < length; i++) {
90+
// Item item = jsItems[i];
91+
// items.push_back(item);
92+
// }
93+
8694
// Pre-process distance
8795
if (distance <= 0) {
8896
distance = 1;
8997
}
9098

9199
// Create nest config
92100
NestConfig<> nestConfig(config);
93-
101+
94102
// Call the nest function
95-
size_t result = nest(items, bin, distance, nestConfig);
96-
97-
// Copy results back to original JavaScript items
98-
for (size_t i = 0; i < items.size() && i < length; ++i) {
99-
jsItems.set(i, val(items[i]));
100-
}
103+
size_t result = nest(jsItems, bin, distance, nestConfig);
101104

102105
return result;
103106
}

0 commit comments

Comments
 (0)