gpt4 book ai didi

objective-c - #import #import #define - Swift 等价物

转载 作者:行者123 更新时间:2023-11-28 09:09:30 26 4
gpt4 key购买 nike

这个翻译对我来说相当困难。我从来没有处理过#define 语句,在 processValue() 函数中它们看起来像嵌套数组之类的,但我知道如何处理它们。怎么样#import <vector> #import <algorithm> ?如果我已经拥有 UIKit,我可以忽略这些吗?

我决定将两个 Obj-C 自定义类的所有代码放在一个 ViewController.swift 文件中。我认识常见的方法和属性,但除此之外我迷路了,因为这涉及很多数学方法。

有人可以帮我把这个烂摊子翻译成 Swift 吗?

//Detector.h     

#import <Foundation/Foundation.h>
#define MAX_PERIODS_TO_STORE 20
#define AVERAGE_SIZE 20
#define INVALID_DETECT_PERIOD -1

@interface Detector : NSObject {
float upVals[AVERAGE_SIZE];
float downVals[AVERAGE_SIZE];
int upValIndex;
int downValIndex;

float lastVal;
float periodStart;
double periods[MAX_PERIODS_TO_STORE];
double periodTimes[MAX_PERIODS_TO_STORE];

int periodIndex;
bool started;
float freq;
float average;

bool wasDown;
}

@property (nonatomic, assign) float periodStart;


-(float) addNewValue:(float) newVal atTime:(double) time;
-(float) getAverage;
-(void) reset;
@end


//Detector.m

#import <QuartzCore/QuartzCore.h>
#import "Detector.h"
#import <vector>
#import <algorithm>

#define MAX_PERIOD 1.5
#define MIN_PERIOD 0.1
#define INVALID_ENTRY -100

@implementation Detector

@synthesize periodStart;


- (id) init
{
self = [super init];
if (self != nil) {
// set everything to invalid
[self reset];
}
return self;
}

-(void) reset {
for(int i=0; i<MAX_PERIODS_TO_STORE; i++) {
periods[i]=INVALID_ENTRY;
}
for(int i=0; i<AVERAGE_SIZE; i++) {
upVals[i]=INVALID_ENTRY;
downVals[i]=INVALID_ENTRY;
}
freq=0.5;
periodIndex=0;
downValIndex=0;
upValIndex=0;
}

-(float) addNewValue:(float) newVal atTime:(double) time {
// we keep track of the number of values above and below zero
if(newVal>0) {
upVals[upValIndex]=newVal;
upValIndex++;
if(upValIndex>=AVERAGE_SIZE) {
upValIndex=0;
}
}
if(newVal<0) {
downVals[downValIndex]=-newVal;
downValIndex++;
if(downValIndex>=AVERAGE_SIZE) {
downValIndex=0;
}
}
// work out the average value above zero
float count=0;
float total=0;
for(int i=0; i<AVERAGE_SIZE; i++) {
if(upVals[i]!=INVALID_ENTRY) {
count++;
total+=upVals[i];
}
}
float averageUp=total/count;
// and the average value below zero
count=0;
total=0;
for(int i=0; i<AVERAGE_SIZE; i++) {
if(downVals[i]!=INVALID_ENTRY) {
count++;
total+=downVals[i];
}
}
float averageDown=total/count;

// is the new value a down value?
if(newVal<-0.5*averageDown) {
wasDown=true;
}

// is the new value an up value and were we previously in the down state?
if(newVal>=0.5*averageUp && wasDown) {
wasDown=false;
// work out the difference between now and the last time this happenned
if(time-periodStart<MAX_PERIOD && time-periodStart>MIN_PERIOD) {
periods[periodIndex]=time-periodStart;
periodTimes[periodIndex]=time;
periodIndex++;
if(periodIndex>=MAX_PERIODS_TO_STORE) {
periodIndex=0;
}
}
// track when the transition happened
periodStart=time;
}
// return up or down
if(newVal<-0.5*averageDown) {
return -1;
} else if(newVal>0.5*averageUp) {
return 1;
}
return 0;
}

-(float) getAverage {
double time=CACurrentMediaTime();
double total=0;
double count=0;
for(int i=0; i<MAX_PERIODS_TO_STORE; i++) {
// only use upto 10 seconds worth of data
if(periods[i]!=INVALID_ENTRY && time-periodTimes[i]<10) {
count++;
total+=periods[i];
}
}
// do we have enough values?
if(count>2) {
return total/count;
}
return INVALID_DETECT_PERIOD;
}

@end


//Filter.h

#import <Foundation/Foundation.h>

#define NZEROS 10
#define NPOLES 10

@interface Filter : NSObject {
float xv[NZEROS+1], yv[NPOLES+1];
}

-(float) processValue:(float) value;

@end


//Filter.m

#import "Filter.h"

#define GAIN 1.894427025e+01


@implementation Filter

-(float) processValue:(float) value { // the function processValue that was declared in .h
xv[0] = xv[1]; xv[1] = xv[2]; xv[2] = xv[3]; xv[3] = xv[4]; xv[4] = xv[5]; xv[5] = xv[6]; xv[6] = xv[7]; xv[7] = xv[8]; xv[8] = xv[9]; xv[9] = xv[10];
xv[10] = value / GAIN;
yv[0] = yv[1]; yv[1] = yv[2]; yv[2] = yv[3]; yv[3] = yv[4]; yv[4] = yv[5]; yv[5] = yv[6]; yv[6] = yv[7]; yv[7] = yv[8]; yv[8] = yv[9]; yv[9] = yv[10];
yv[10] = (xv[10] - xv[0]) + 5 * (xv[2] - xv[8]) + 10 * (xv[6] - xv[4])
+ ( -0.0000000000 * yv[0]) + ( 0.0357796363 * yv[1])
+ ( -0.1476158522 * yv[2]) + ( 0.3992561394 * yv[3])
+ ( -1.1743136181 * yv[4]) + ( 2.4692165842 * yv[5])
+ ( -3.3820859632 * yv[6]) + ( 3.9628972812 * yv[7])
+ ( -4.3832594900 * yv[8]) + ( 3.2101976096 * yv[9]);
return yv[10];
}

@end

//尝试 Swift 翻译

import UIKit
import Foundation
import AVFoundation


#define GAIN 1.894427025e+01
#define MAX_PERIODS_TO_STORE 20
#define AVERAGE_SIZE 20
#define INVALID_DETECT_PERIOD -1

class ViewController: UIViewController {

// Set up Camera
let session = AVCaptureSession()
// If we find a device we'll store it here for later use
var camera : AVCaptureDevice?


var detector = Detector.self
var filter = Filter.self


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



}

class Filter: NSObject {

var xv:Float
var yv:Float

func processValue(value: Float) -> Float {
xv[0] = xv[1]; xv[1] = xv[2]; xv[2] = xv[3]; xv[3] = xv[4]; xv[4] = xv[5]; xv[5] = xv[6]; xv[6] = xv[7]; xv[7] = xv[8]; xv[8] = xv[9]; xv[9] = xv[10]; xv[10] = value / GAIN;
yv[0] = yv[1]; yv[1] = yv[2]; yv[2] = yv[3]; yv[3] = yv[4]; yv[4] = yv[5]; yv[5] = yv[6]; yv[6] = yv[7]; yv[7] = yv[8]; yv[8] = yv[9]; yv[9] = yv[10];
yv[10] = (xv[10] - xv[0]) + 5 * (xv[2] - xv[8]) + 10 * (xv[6] - xv[4])
+ ( -0.0000000000 * yv[0]) + ( 0.0357796363 * yv[1])
+ ( -0.1476158522 * yv[2]) + ( 0.3992561394 * yv[3])
+ ( -1.1743136181 * yv[4]) + ( 2.4692165842 * yv[5])
+ ( -3.3820859632 * yv[6]) + ( 3.9628972812 * yv[7])
+ ( -4.3832594900 * yv[8]) + ( 3.2101976096 * yv[9]);
return yv[10];
}

}

class Detector: NSObject {

var upVals: Float
var downVals: Float
var upValIndex: Int
var downValIndex: Int
var lastVal: Float
var periodStart: Float
var periods: Double
var periodTimes: Double
var periodIndex: Int
var started: Bool
var freq: Float
var average: Float
var wasDown: Bool


func reset() {
for var i=0; i < MAX_PERIODS_TO_STORE; i++ {
periods[i]=INVALID_ENTRY
}
for var i=0; i<AVERAGE_SIZE; i++ {
upVals[i]=INVALID_ENTRY
downVals[i]=INVALID_ENTRY
}
freq=0.5;
periodIndex=0;
downValIndex=0;
upValIndex=0;
}

func addNewValue(newVal:Float, atTime:Double) -> Float { // the function addNewValue that was declared in .h
// we keep track of the number of values above and below zero
if newVal > 0 {
upVals[upValIndex] = newVal
upValIndex++
if(upValIndex>=AVERAGE_SIZE) {
upValIndex = 0
}
}
if newVal < 0 {
downVals[downValIndex] =- newVal
downValIndex++
if downValIndex >= AVERAGE_SIZE {
downValIndex = 0
}
}
// work out the average value above zero
var count: Float
var total: Float
for var i=0; i < AVERAGE_SIZE; i++) {
if upVals[i]!=INVALID_ENTRY {
count++
total+=upVals[i]
}
}
var averageUp = total/count
// and the average value below zero
count=0;
total=0;
for var i=0; i < AVERAGE_SIZE; i++ {
if downVals[i]!=INVALID_ENTRY {
count++
total+=downVals[i]
}
}
var averageDown = total/count

// is the new value a down value?
if newVal < (-0.5*averageDown) {
wasDown = true
}

// is the new value an up value and were we previously in the down state?
if newVal >= (0.5*averageUp) && (wasDown) {
wasDown = false
// work out the difference between now and the last time this happenned
if time-periodStart < MAX_PERIOD && time-periodStart > MIN_PERIOD {
periods[periodIndex]=time-periodStart
periodTimes[periodIndex]=time
periodIndex++
if periodIndex >= MAX_PERIODS_TO_STORE {
periodIndex = 0
}
}
// track when the transition happened
periodStart = time
}
// return up or down
if newVal < (-0.5*averageDown) {
return -1
} else if newVal > (0.5*averageUp) {
return 1
}
return 0
}




}

最佳答案

#define C/C++/Obj-C 中的 s 用于创建全局常量。在 Swift 中,它们可以通过以下方式轻松翻译:

let GAIN = 1.894427025e+01
let AVERAGE_SIZE = 20
let INVALID_DETECT_PERIOD = -1

当然,它们不必在全局范围(或​​ Swift 中的模块范围)中声明。如果您不想在类或模块之间共享它们,那么在类内甚至方法/函数内声明它们会更好。始终尝试尽可能地限制范围。

宏实际上不是 C/C++/Obj-C 的一部分,宏在预处理期间被替换为它的值。使用 const 在 C/C++/Obj-C 中定义全局常量有更好的方法但宏仍然被广泛使用。由于宏不是语言本身的一部分,您可以将它们放在文件中的任何位置(在首次使用之前),它们不受范围限制。

标题 <vector><algorithm>不包含在 UIKit 中,但是,我很确定您不需要它们,因为我没有看到您的代码中使用的那些 header 中的任何函数。 <vector>与 C++ 容器类型有关 vector (元素列表)。 <algorithms>与容器上的操作有关。无论如何,我认为 Swift 不会很好地使用模板化的 C++ 类型。

数组的初始化在 Swift 和 C 中是不同的。在 C 中,数组是“愚蠢的”。您首先声明数组,为其指定大小,然后为每个元素分配一个值。

float xv[NZEROS+1]

在 swift 中,您可以直接用值初始化数组。

var xv = [Float](count: NZEROS + 1, repeatedValue: 0)

关于objective-c - #import <vector> #import <algorithm> #define - Swift 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29740419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com