将“const float *”从C ++翻译为C#(translating “const float *” from C++ to C#)

编程入门 行业动态 更新时间:2024-10-27 22:30:13
将“const float *”从C ++翻译为C#(translating “const float *” from C++ to C#)

如何将此C ++代码段转换为C#?

void fm_getAABB(..., const float *points, ...) { // ... const unsigned char *source = (const unsigned char *) points; //... const float *p = (const float *) source; //... }

我已经尝试过使用C ++到C#转换器 ,但它似乎也无法翻译它。

编辑: 这是整个功能:

void fm_getAABB(unsigned int vcount, const float *points, unsigned int pstride, float *bmin, float *bmax) { const unsigned char *source = (const unsigned char *) points; bmin[0] = points[0]; bmin[1] = points[1]; bmin[2] = points[2]; bmax[0] = points[0]; bmax[1] = points[1]; bmax[2] = points[2]; for (unsigned int i = 1; i < vcount; i++) { source+=pstride; const float *p = (const float *) source; if ( p[0] < bmin[0] ) bmin[0] = p[0]; if ( p[1] < bmin[1] ) bmin[1] = p[1]; if ( p[2] < bmin[2] ) bmin[2] = p[2]; if ( p[0] > bmax[0] ) bmax[0] = p[0]; if ( p[1] > bmax[1] ) bmax[1] = p[1]; if ( p[2] > bmax[2] ) bmax[2] = p[2]; } }

How can I translate this C++ code snippet into C# ?

void fm_getAABB(..., const float *points, ...) { // ... const unsigned char *source = (const unsigned char *) points; //... const float *p = (const float *) source; //... }

I've already tried to use a C++ to C# converter, but it doesn't seem able to translate it either.

EDIT: Here's the whole function:

void fm_getAABB(unsigned int vcount, const float *points, unsigned int pstride, float *bmin, float *bmax) { const unsigned char *source = (const unsigned char *) points; bmin[0] = points[0]; bmin[1] = points[1]; bmin[2] = points[2]; bmax[0] = points[0]; bmax[1] = points[1]; bmax[2] = points[2]; for (unsigned int i = 1; i < vcount; i++) { source+=pstride; const float *p = (const float *) source; if ( p[0] < bmin[0] ) bmin[0] = p[0]; if ( p[1] < bmin[1] ) bmin[1] = p[1]; if ( p[2] < bmin[2] ) bmin[2] = p[2]; if ( p[0] > bmax[0] ) bmax[0] = p[0]; if ( p[1] > bmax[1] ) bmax[1] = p[1]; if ( p[2] > bmax[2] ) bmax[2] = p[2]; } }

最满意答案

一种选择是使用C#的不安全模式将其翻译为原样。 C#完全能够通过少量修改来运行此代码。 例如,您需要删除const修饰符。

如果您设法生成仅限托管版本,那么当然更可取。 但是,这甚至不可能完全清楚:您的函数正在执行某些指针操作,这可能会导致未对齐的访问。 无法使用仅托管功能以未对齐方式访问托管阵列。

One option is to translate this as-is, by using the unsafe mode of C#. C# is perfectly capable of running this code with small modifications. You need to remove the const modifier for example.

If you manage to produce a managed-only version that would be preferable of course. But it is not entirely clear that that is even possible: Your function is performing certain pointer manipulations which might lead to unaligned access. Managed arrays cannot pe accessed in an unaligned way using managed-only features.

更多推荐

本文发布于:2023-07-21 11:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1209777.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:译为   const   float   translating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!