skip to Main Content

In our C++ code we have the following:

arraySize = regionsvertices.size();
double* arr = new double[arraySize];
int k = 0;
for (double i : regionsvertices) {
    arr[k++] = i;
}

return arr;

The related C# code looks like this:

var regionsPtr = region_growing_algorithm(
    pointsArr,
    simpleShape.points.Length / 3,
    simpleShape.faces,
    simpleShape.faces.Length / 3,
    filename);

var arrayLength = get_array_size();
var regions = new double[arrayLength];
Marshal.Copy(regionsPtr, regions, 0, arrayLength);

return new JsonResult
{
    Data = regions
};

And in TypeScript we do:

$.ajax({
    type: "POST",
    url: "/ShapeDetection/RegionGrowing",
    data: {
        points: points,
        faces: faces
    },
    success: function (result) {
        console.log("result from RegionGrowing", result.Data);
    },
    dataType: "json"
});

The problem is that we get in result.Data numerical values which are integers but not double values. Somehow the numbers do not get converted correctly.

Can you show us what we are doing wrong?

2

Answers


  1. Chosen as BEST ANSWER

    It seems that the original values which we get in the C++ section of the code are integers, so there is nothing wrong with the conversion. It is however strange that these values do not have a decimal part, so this requires further investigation.


  2. success: function (result) {
        var data = JSON.parse(result.Data); // Parse the JSON to ensure types
        console.log("result from RegionGrowing", data);
    },
    

    By using JSON.parse, you explicitly convert the received JSON data into JavaScript objects, which should maintain the double precision. This can help ensure that the numbers are treated as doubles on the client side, regardless of how they were serialized on the server side.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search