将紧凑型float值数组转换为float数组的更简洁方法

编程入门 行业动态 更新时间:2024-10-26 22:21:18
本文介绍了将紧凑型float值数组转换为float数组的更简洁方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在提出实际问题之前,先做个小前奏.我不在乎安全性,我在乎性能.我知道这是不合适的,而且我知道这很hacky,但是速度很快.

Before the actual question, small prelude. I don't care about security, I do care about performance. I KNOW this is not proper and I know it's very hacky, however this is quite fast.

vector< float>结果= move(*(((vector&float> *)& vertices));

该代码滥用C样式强制转换和指针,以迫使编译器解释左侧数组 vertices ,这是紧凑型向量,其中所有字段都以float数组形式浮动

That code is abusing C style casts and pointers to force the compiler to interpret the left hand side array vertices which is a vector of a compact type where all the fields are float as an array of floats.

struct vertex { float x; float y; float z; } vector<vertex> vertices;

这有效并且可以完成所需的工作,但是有点难以理解.我想知道是否还有另一种以更易读的方式实现相同结果的方法.

This works and does what it needs to, however it's somewhat hard to read. I want to know if there is another way of achieving the same outcome in a more readable way.

推荐答案

您应该更关心的是代码的行为是 undefined ,因为强制类型转换违反了严格的别名规则.请注意, vector< vertex> 是与 vector&float> 完全不同的 类型.

What you should care more about is that the behaviour of your code is undefined since the cast is a violation of the strict aliasing rule. Note that vector<vertex> is a completely different type to a vector<float>.

特别是,您假设 struct 中的数据是连续的,并且即使在最后, struct 中也没有填充.

In particular you are assuming the data in the struct are contiguous and there is no padding in the struct, even at the end.

为什么不从一开始就使用 vector< float> ,并要注意,第4个元素会开始下一个三角形,依此类推?

Why not use a vector<float> from the get-go, with a note to self that the 4th element starts the next triangle, and so on?

更多推荐

将紧凑型float值数组转换为float数组的更简洁方法

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

发布评论

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

>www.elefans.com

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