我在这种方法中从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;
}
}
}
if( _stlMesh->IsRayIntersectsPoly(root->index, rayPoints, intersectionPoint))
此特定语句中可能存在一个缺陷,您从未初始化过pointsPoint。C ++可以使您摆脱困境,它与C#的确定赋值规则没有任何相似之处。目前尚不清楚这是否是真正的问题,该变量可能通过引用传递。
在调试版本中,这样的未初始化变量将具有可预测的值。当您将其切换到十六进制显示模式时,您可以在调试器中轻松看到某些内容。此结构或类中的字段将包含值0xcccccccc,该值易于生成无意义的结果或由于访问冲突而使您的代码崩溃。在Release版本中,/ RTC选项没有打开,您将在变量中获得完全随机的值。
这与您对问题的描述非常吻合,很高的几率表明这确实是问题所在。确保使用调试器查找类似的问题,单步执行代码时,您可以在Autos调试器窗口中轻松查看局部变量的值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句