0

我在这个方法中从 c# 调用 c++/cli 方法:

        bool SetProperty(Element element, Node referencePoint, List<Materializer> materializers, List<ulong> properties)
    {

        // Loop over STLs
        for (int i = 0; i < materializers.Count; i++)
        {               
            Materializer materializer = materializers[i];
            PentalTreeNode pentalTreeRoot = pentalTreeDatasets[i].top;


            if (materializer.IsPointInside(referencePoint.X, referencePoint.Y, referencePoint.Z, pentalTreeRoot))
            {
                element.PropertyId = properties[i];
                return true;
            };

        }

        return false;
    }

C++/cli 方法是这样的:

bool IsPointInside(double x, double y, double z, PentalTreeNode ^root)
    {
        int intersectionCount = 0;

        Math3d::M3d rayPoints[2], intersectionPoint;

        rayPoints[0].set(x,y,z);
        rayPoints[1].set(x,y,1.0e6);


        if(_box->IsContainingPoint(x,y,z))
        {           
            intersectionCount=CountIntersects(x,y,z,root);
            return (intersectionCount%2!=0);

        }   

    }

出了什么问题,因为 c++/cli 方法并不总是返回相同的结果?如何固定或编组?

c++/cli 中的方法(也许这不行?):

int CountIntersects(double x, double y, double z, PentalTreeNode ^root)
    {

        Math3d::M3d rayPoints[2], intersectionPoint;

        rayPoints[0].set(x,y,z);
        rayPoints[1].set(x,y,1.0e6);

        if(!root) 
            return 0;
        else
        {
            int special = CountIntersects(x,y,z,root->special);
            if (x <= root->xMax && x >= root->xMin && y <= root->yMax && y >= root->yMin)
            {

                if( _stlMesh->IsRayIntersectsPoly(root->index, rayPoints, intersectionPoint))
                {
                    return (1 + special);
                }
                else 
                    return special;
            }
            else
            {
             if (y>root->yMax)
              {
                    return (CountIntersects(x,y,z,root->top)+special);
              }
              else if(y<root->yMin)
                    {
                        return (CountIntersects(x,y,z,root->bottom)+special);
                    }
                    else if(x<root->xMin)
                            {
                                return (CountIntersects(x,y,z,root->left)+special);
                            }
                            else if(x>root->xMax)
                            {
                                return (CountIntersects(x,y,z,root->right)+special);
                            }
                            else 
                                return special;
            }

        }

    }
4

2 回答 2

1
if( _stlMesh->IsRayIntersectsPoly(root->index, rayPoints, intersectionPoint))

这个特定的语句有一个可能的缺陷,你从来没有初始化intersectionPoint。C++ 可以让你摆脱这种情况,它没有任何类似于 C# 的明确分配规则的东西。不是 100% 清楚这是否是真正的问题,变量可能是通过引用传递的。

在 Debug 构建中,此类未初始化的变量将具有可预测的值。当您将调试器切换到十六进制显示模式时,您可以轻松地在调试器中看到一些东西。此结构或类中的字段将包含值 0xcccccccc,该值易于生成无意义的结果或因访问冲突而使您的代码崩溃。在 Release 版本中,/RTC 选项未打开,您将在变量中获得完全随机的值。

这与您对问题的描述非常吻合,因此很有可能这确实是问题所在。请务必使用调试器来查找此类问题,您可以在单步执行代码时通过 Autos 调试器窗口轻松查看局部变量的值。

于 2013-08-10T13:04:01.413 回答
0

您不是在调用 C++ 方法!您正在调用 C++/CLI 方法。因此它是正常的 .NET 代码,并且总是正确传递。在这种情况下,无需在 C# 中固定或编组任何内容!

如果它返回的不是预期值,您应该尝试在您的 C++/CLI 项目中找到问题。

于 2013-08-10T10:31:50.320 回答