取消引用UnsafeMutablePointer< UnsafeMutableRawPointer> [英] Dereference UnsafeMutablePointer<UnsafeMutableRawPointer>

查看:109
本文介绍了取消引用UnsafeMutablePointer< UnsafeMutableRawPointer>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要传递数据的块,我想将其转换为浮点数数组-例如[[0.1,0.2,0.3,1.0],[0.3、0.4、0.5、1.0],[0.5、0.6、0.7、1.0]。此数据以 data:UnsafeMutablePointer< UnsafeMutableRawPointer> 的形式传递给我(内部数组是RGBA值)

I have a block that is passing data in that I'd like to convert to an array of array of floats -- e.g. [[0.1,0.2,0.3, 1.0], [0.3, 0.4, 0.5, 1.0], [0.5, 0.6, 0.7, 1.0]]. This data is passed to me in the form of data:UnsafeMutablePointer<UnsafeMutableRawPointer> (The inner arrays are RGBA values)

fwiw-块参数来自 SCNParticleEventBlock

fwiw -- the block parameters are from SCNParticleEventBlock

如何将数据取消引用到[[Float]]中?一旦有了包含内部数组的数组,就可以使用以下命令引用内部数组(colorArray)数据:

How can I dereference data into a [[Float]]? Once I have the array containing the inner arrays, I can reference the inner array (colorArray) data with:

let rgba: UnsafeMutablePointer<Float> = UnsafeMutablePointer(mutating: colorArray)
let count = 4
for i in 0..<count {
    print((rgba+i).pointee)
}

fwiw-这是Apple引用数据的示例Objective-C代码(来自SCNParticleSystem handle(_:forProperties:handler :)

fwiw -- this is Apple's example Objective-C code for referencing the data (from SCNParticleSystem handle(_:forProperties:handler:) )

[system handleEvent:SCNParticleEventBirth
      forProperties:@[SCNParticlePropertyColor]
          withBlock:^(void **data, size_t *dataStride, uint32_t *indices , NSInteger count) {
              for (NSInteger i = 0; i < count; ++i) {
                  float *color = (float *)((char *)data[0] + dataStride[0] * i);
                  if (rand() & 0x1) { // Switch the green and red color components.
                      color[0] = color[1];
                      color[1] = 0;
                  }
              }
          }];


推荐答案

基于这个答案,我迅速将粒子系统处理函数编写为:

Based on this answer, I've written the particle system handler function in swift as:

    ps.handle(SCNParticleEvent.birth, forProperties [SCNParticleSystem.ParticleProperty.color]) {
    (data:UnsafeMutablePointer<UnsafeMutableRawPointer>, dataStride:UnsafeMutablePointer<Int>, indicies:UnsafeMutablePointer<UInt32>?, count:Int) in

    for i in 0..<count {

        // get an UnsafeMutableRawPointer to the i-th rgba element in the data
        let colorsPointer:UnsafeMutableRawPointer = data[0] + dataStride[0] * i

        //  convert the UnsafeMutableRawPointer to a typed pointer by binding it to a type:
        let floatPtr = colorsPointer.bindMemory(to: Float.self, capacity: dataStride[0])
        // convert that to a an  UnsafeMutableBufferPointer
        var rgbaBuffer = UnsafeMutableBufferPointer(start: floatPtr, count: dataStride[0])
        // At this point, I could convert the buffer to an Array, but doing so copies the data into the array and any changes made in the array are not reflected in the original data.  UnsafeMutableBufferPointer are subscriptable, nice.
        //var rgbaArray = Array(rgbaBuffer)

        // about half the time, mess with the red and green components
        if(arc4random_uniform(2) == 1) {
            rgbaBuffer[0] = rgbaBuffer[1]
            rgbaBuffer[1] = 0
        }
    }
 }

我真的不确定这是否是解决问题的最直接方法,并且与Objective-C代码相比显得笨重(请参阅上面的问题) 。我当然愿意接受其他解决方案和/或对此解决方案发表评论。

I'm really not certain if this is the most direct way to go about this and seems rather cumbersome compared to the objective-C code (see above question). I'm certainly open to other solutions and/or comments on this solution.

这篇关于取消引用UnsafeMutablePointer&lt; UnsafeMutableRawPointer&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆