@@ -19,18 +19,43 @@ EMSCRIPTEN_DECLARE_VAL_TYPE(Float32Array);
1919EMSCRIPTEN_DECLARE_VAL_TYPE (Uint32Array);
2020EMSCRIPTEN_DECLARE_VAL_TYPE (Int32Array);
2121
22+ // Wrapper for array of Float32Array to provide proper TypeScript typing
23+ class PolygonArray {
24+ public:
25+ emscripten::val polygons;
26+
27+ PolygonArray () : polygons(emscripten::val::array()) {}
28+ explicit PolygonArray (emscripten::val arr) : polygons(arr) {}
29+
30+ void push (const Float32Array& polygon) {
31+ polygons.call <void >(" push" , polygon);
32+ }
33+
34+ emscripten::val get (int index) const {
35+ return polygons[index];
36+ }
37+
38+ int size () const {
39+ return polygons[" length" ].as <int >();
40+ }
41+
42+ emscripten::val toArray () const {
43+ return polygons;
44+ }
45+ };
46+
2247// Return type for unwrap function
2348struct UnwrapResult
2449{
25- std::vector< float > uvCoordinates;
50+ Float32Array uvCoordinates = Float32Array{ emscripten::val::array ()} ;
2651 uint32_t textureWidth;
2752 uint32_t textureHeight;
2853};
2954
3055// Return type for project function (array of polygons as flat arrays)
3156struct ProjectResult
3257{
33- std::vector<std::vector< float >> polygons;
58+ PolygonArray polygons;
3459};
3560
3661// Parameter structure for project function
@@ -98,16 +123,15 @@ UnwrapResult unwrapTyped(const Float32Array& vertices_js, const Uint32Array& ind
98123 }
99124
100125 // Convert result to structured return type
101- std::vector<float > uv_array;
102- uv_array.reserve (uv_coords.size () * 2 );
126+ emscripten::val uv_array = emscripten::val::array ();
103127
104- for (const auto & coord : uv_coords) {
105- uv_array.push_back (coord .x );
106- uv_array.push_back (coord .y );
128+ for (size_t i = 0 ; i < uv_coords. size (); ++i ) {
129+ uv_array.set (i * 2 , uv_coords[i] .x );
130+ uv_array.set (i * 2 + 1 , uv_coords[i] .y );
107131 }
108132
109133 return UnwrapResult{
110- .uvCoordinates = uv_array,
134+ .uvCoordinates = Float32Array{ uv_array} ,
111135 .textureWidth = texture_width,
112136 .textureHeight = texture_height
113137 };
@@ -212,17 +236,17 @@ ProjectResult projectTyped(
212236 );
213237
214238 // Convert result to structured return type
215- std::vector<std::vector<float >> result_polygons;
216- result_polygons.reserve (result.size ());
217-
218- for (const auto & polygon : result) {
219- std::vector<float > polygon_flat;
220- polygon_flat.reserve (polygon.size () * 2 );
221- for (const auto & point : polygon) {
222- polygon_flat.push_back (point.x );
223- polygon_flat.push_back (point.y );
239+ PolygonArray result_polygons;
240+
241+ for (size_t i = 0 ; i < result.size (); ++i) {
242+ emscripten::val polygon_array = emscripten::val::array ();
243+ const auto & polygon = result[i];
244+ for (size_t j = 0 ; j < polygon.size (); ++j) {
245+ polygon_array.set (j * 2 , polygon[j].x );
246+ polygon_array.set (j * 2 + 1 , polygon[j].y );
224247 }
225- result_polygons.push_back (std::move (polygon_flat));
248+ // Add Float32Array to the result array
249+ result_polygons.push (Float32Array{polygon_array});
226250 }
227251
228252 return ProjectResult{.polygons = result_polygons};
@@ -421,15 +445,20 @@ emscripten::val jsProject(
421445
422446EMSCRIPTEN_BINDINGS (uvula)
423447{
424- // Register typed array types
425- register_vector<float >(" Float32Array" );
426- register_vector<std::vector<float >>(" Float32ArrayArray" );
427448
428449 // Register TypeScript-style typed arrays
429450 emscripten::register_type<Float32Array>(" Float32Array" );
430451 emscripten::register_type<Uint32Array>(" Uint32Array" );
431452 emscripten::register_type<Int32Array>(" Int32Array" );
432453
454+ // Register PolygonArray class for proper TypeScript typing
455+ class_<PolygonArray>(" PolygonArray" )
456+ .constructor <>()
457+ .function (" push" , &PolygonArray::push)
458+ .function (" get" , &PolygonArray::get)
459+ .function (" size" , &PolygonArray::size)
460+ .function (" toArray" , &PolygonArray::toArray);
461+
433462 // Register structured return types
434463 value_object<UnwrapResult>(" UnwrapResult" )
435464 .field (" uvCoordinates" , &UnwrapResult::uvCoordinates)
0 commit comments