活动介绍
file-type

C++编写的Oculus Rift示例:OVR_SDL2应用程序分析

ZIP文件

下载需积分: 10 | 128KB | 更新于2024-11-10 | 22 浏览量 | 2 下载量 举报 收藏
download 立即下载
在本资源中,我们得到了一个使用C++编写的Oculus Rift的最小跨平台示例应用程序。该应用程序仅依赖于SDL2、Oculus VR SDK和GLEW,代码编写时针对的是Oculus SDK 0.4.3 beta版本。 首先,我们来了解Oculus Rift,它是一款虚拟现实头戴式显示设备,由Oculus公司开发。通过将Oculus Rift与计算机连接,用户可以沉浸在一个全新的虚拟世界中。 接着,我们来看看SDL2。SDL,全称为Simple DirectMedia Layer,是一个跨平台的开发库,提供访问音频、键盘、鼠标、游戏手柄和图形硬件的功能。SDL2是其更新的版本,与旧版本相比,其API的许多部分被重新设计,以支持现代操作系统。 GLEW,即OpenGL扩展库,是一个跨平台的C/C++扩展加载库,用于访问OpenGL的全部功能。通过GLEW,开发者可以轻松地使用OpenGL的最新特性。 这个示例应用程序提供了一个简单的SDL2应用程序框架,这个框架支持OpenGL 3.2核心配置文件和FPS风格的交互。这种交互方式可以使用户以第一人称的视角进行游戏或体验虚拟现实,增加了沉浸感。 这个示例应用的模块化设计使其可以轻松重复使用,这意味着开发者可以将这些示例应用中的代码片段应用到自己的项目中,从而节省开发时间和精力。 此外,这个示例应用的许可证允许开发者出于任何目的复制它们。这意味着开发者可以自由地使用这些示例应用,无论是在商业项目还是在个人项目中。 然而,由于Oculus SDK的不断发展,文档并没有跟上实现的步伐。尽管如此,这个示例应用OVR_SDL2_app仍是Oculus应用程序的一个完整实现,正如开发人员指南所述,但需要开发者进行一定的探索和学习。 最后,这个示例应用很好地利用了SDL2,特别是SDL2的新游戏控制器API,它可以将所有的输入都呈现为来自XBox控制器。这为使用XBox控制器的用户提供了方便。此外,SDL2的事件循环还提供了控制器连接和断开的处理。 总的来说,这个示例应用是一个宝贵的资源,它不仅可以帮助开发者学习如何使用Oculus Rift、SDL2和GLEW,还可以帮助他们理解如何设计和构建一个跨平台的虚拟现实应用。

相关推荐

filetype

from sklearn.datasets import load_iris import matplotlib.pyplot as plt import numpy as np # 数据获取 def get_data(): iris = load_iris() data = iris.data result = iris.target return data, result # 将数据处理为训练集和测试集 def data_deal(data, result): data_list = [] for i in data: tem_list = [i[0], i[1]] data_list.append(tem_list) res_list = [] for j in result: res_list.append(j) train_list = data_list[0: 10] + data_list[20: 80] + data_list[90: 100] train_result = res_list[0: 10] + res_list[20: 80] + res_list[90: 100] test_list = data_list[0: 40] + data_list[60: 100] test_result = res_list[0: 40] + res_list[60: 100] return data_list, train_list, test_list, train_result, test_result # 回归方法训练 def train(learning_rate, iter_num, train_data, result): x_c = 0 y_c = 0 for i in train_data: x_c = x_c + i[0] y_c = y_c + i[1] m = x_c/len(train_data) n = y_c/len(train_data) w = 0 b = 0 ok_rate = 0 for i in range(iter_num): train_r = [] b = n-w*m count = 0 for j in train_data: if j[1] > w*j[0]+b: train_r.append(0) else: train_r.append(1) for ii in range(len(result)): if result[ii] == train_r[ii]: count = count+1 train_ok_rate = count/len(train_data) if ok_rate <= train_ok_rate: w = w + learning_rate else: w = w - learning_rate learning_rate = learning_rate*0.9 ok_rate = train_ok_rate return ok_rate, w, b # 回归方法测试 def test(w, b, test_list, test_result): test_res = [] count = 0 for j in test_list: if j[1] > w * j[0] + b: test_res.append(0) else: test_res.append(1) for i in range(len(test_result)): if result[i] == test_res[i]: count = count + 1 oks = count/len(test_result) return oks # 绘制函数图像和

filetype

from osgeo import gdal import os def img_to_geotiff_with_pyramids(input_img, output_tif): """ 将IMG格式影像转换为带金字塔的GeoTIFF格式 参数: input_img: 输入的IMG文件路径 output_tif: 输出的TIFF文件路径(不含扩展名) """ try: # 1. 打开输入IMG文件 input_ds = gdal.Open(input_img) if input_ds is None: raise Exception("无法打开输入文件: " + input_img) # 2. 设置输出选项 - 使用无损压缩 options = [ 'COMPRESS=DEFLATE', # 无损压缩 'PREDICTOR=2', # 对浮点数据使用预测器 'ZLEVEL=9', # 最高压缩级别 'TILED=YES', # 使用分块存储 'BIGTIFF=IF_NEEDED' # 如果需要则使用BigTIFF ] # 3. 创建输出GeoTIFF文件 output_tif_full = output_tif + '.tif' gdal.Translate(output_tif_full, input_ds, format='GTiff', creationOptions=options) # 4. 关闭输入数据集 input_ds = None # 5. 重新打开输出文件以构建金字塔 output_ds = gdal.Open(output_tif_full, gdal.GA_Update) if output_ds is None: raise Exception("无法打开输出文件: " + output_tif_full) # 6. 构建金字塔(概览) print("正在构建金字塔...") output_ds.BuildOverviews("AVERAGE", [2, 4, 8, 16, 32, 64]) # 7. 关闭输出数据集 output_ds = None # 8. 检查文件是否生成 if not os.path.exists(output_tif_full): raise Exception("输出文件未生成: " + output_tif_full) print(f"转换成功! 输出文件: {output_tif_full}") print(f"同时生成了TFW文件和金字塔文件(.ovr)") except Exception as e: print("转换过程中出错:", str(e)) # 清理可能生成的不完整文件 if os.path.exists(output_tif_full): os.remove(output_tif_full) if os.path.exists(output_tif_full + '.tfw'): os.remove(output_tif_full + '.tfw') if os.path.exists(output_tif_full + '.ovr'): os.remove(output_tif_full + '.ovr') def remove_black_borders(input_path, output_path, nodata_value=0): """ 去除TIFF影像的黑边(基于NoData值) :param input_path: 输入TIFF文件路径 :param output_path: 输出TIFF文件路径 :param nodata_value: 被视为黑边的值(默认为0) """ # 原始文件 input_path_full = input_path + '.tif' # 打开原始影像 ds = gdal.Open(input_path_full) band = ds.GetRasterBand(1) # 如果原始影像没有设置NoData值,则设置一个 if band.GetNoDataValue() is None: band.SetNoDataValue(nodata_value) # 创建临时文件进行裁剪 temp_path = "/vsimem/temp.tif" # 使用虚拟文件系统避免磁盘IO gdal.Translate(temp_path, ds, format='GTiff') # 使用gdal.Warp自动裁剪无效数据区域 gdal.Warp(output_path, temp_path, format='GTiff', cutlineDSName=None, cropToCutline=True, dstNodata=nodata_value) # 清理临时文件 gdal.Unlink(temp_path) # 关闭数据集 ds = None # 清除多余文件 if os.path.exists(input_path_full): os.remove(input_path_full) else: print(f"文件 {input_path_full} 不存在。") print("影像转换完成。。。") # 使用示例 if __name__ == "__main__": input_img_path = r"E:\2024年5月曹妃甸影像\ZY1F_VNIC_E118.8_N39.3_20240405_L1B0000702978-MUX_truecolor.img" # 替换为你的IMG文件路径 output_tif_path = r"E:\A结果\output" # 不需要加扩展名 output_path = r"E:\A结果\ZY1F_VNIC_E118.8_N39.3_20240405_L1B0000702978-MUX_truecolor.tif" # 最终成果 img_to_geotiff_with_pyramids(input_img_path, output_tif_path) print("正在处理影像。。。") remove_black_borders(output_tif_path, output_path) 将上述代码进行修改实现保持原有基本功能的情况下能够处理33GB以上的img文件,并优化效率

filetype

那如果要用来配置ADC读取电压,该怎么配置ADC_GetFlagStatus(ADCx, ADC_FLAG_RSTCAL)和ADC_GetFlagStatus(ADCx, ADC_FLAG_CAL)的 参数 /** * @brief Checks whether the specified ADC flag is set or not. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral. * @param ADC_FLAG: specifies the flag to check. * This parameter can be one of the following values: * @arg ADC_FLAG_AWD: Analog watchdog flag * @arg ADC_FLAG_EOC: End of conversion flag * @arg ADC_FLAG_JEOC: End of injected group conversion flag * @arg ADC_FLAG_JSTRT: Start of injected group conversion flag * @arg ADC_FLAG_STRT: Start of regular group conversion flag * @arg ADC_FLAG_OVR: Overrun flag * @retval The new state of ADC_FLAG (SET or RESET). */ FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG) { FlagStatus bitstatus = RESET; /* Check the parameters */ assert_param(IS_ADC_ALL_PERIPH(ADCx)); assert_param(IS_ADC_GET_FLAG(ADC_FLAG)); /* Check the status of the specified ADC flag */ if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET) { /* ADC_FLAG is set */ bitstatus = SET; } else { /* ADC_FLAG is reset */ bitstatus = RESET; } /* Return the ADC_FLAG status */ return bitstatus; } /** * @brief Checks whether the specified ADC flag is set or not. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral. * @param ADC_FLAG: specifies the flag to check. * This parameter can be one of the following values: * @arg ADC_FLAG_AWD: Analog watchdog flag * @arg ADC_FLAG_EOC: End of conversion flag * @arg ADC_FLAG_JEOC: End of injected group conversion flag * @arg ADC_FLAG_JSTRT: Start of injected group conversion flag * @arg ADC_FLAG_STRT: Start of regular group conversion flag * @arg ADC_FLAG_OVR: Overrun flag * @retval The new state of ADC_FLAG (SET or RESET). */ FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG) { FlagStatus bitstatus = RESET; /* Check the parameters */ assert_param(IS_ADC_ALL_PERIPH(ADCx)); assert_param(IS_ADC_GET_FLAG(ADC_FLAG)); /* Check the status of the specified ADC flag */ if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET) { /* ADC_FLAG is set */ bitstatus = SET; } else { /* ADC_FLAG is reset */ bitstatus = RESET; } /* Return the ADC_FLAG status */ return bitstatus; }

filetype

def evaluate(model, loader, criterion): model.eval() total_loss = 0 all_labels, all_probs = [], [] with torch.no_grad(): for x, y in loader: x, y = x.to(device), y.to(device) outputs = model(x) loss = criterion(outputs, y) total_loss += loss.item() # 获取概率 probs = torch.softmax(outputs, dim=1) all_probs.extend(probs.cpu().numpy()) all_labels.extend(y.cpu().numpy()) all_labels = np.array(all_labels) all_probs = np.array(all_probs) n_classes = all_probs.shape[1] # 计算 AUROC if n_classes == 2: auroc = roc_auc_score(all_labels, all_probs[:, 1]) else: auroc = roc_auc_score( all_labels, all_probs, multi_class='ovr', average='weighted' ) # 计算 AUPRC if n_classes == 2: # 二分类:直接使用正类概率 auprc = average_precision_score(all_labels, all_probs[:, 1]) else: # 多分类:对每个类别计算 PR 曲线面积,再加权平均 from sklearn.preprocessing import label_binarize y_true_bin = label_binarize(all_labels, classes=np.arange(n_classes)) auprc_list = [] for i in range(n_classes): precision, recall, _ = precision_recall_curve( y_true_bin[:, i], all_probs[:, i] ) auprc_i = auc(recall, precision) # 注意顺序:recall在前,precision在后 auprc_list.append(auprc_i) # 按类别样本数加权平均 class_counts = np.sum(y_true_bin, axis=0) auprc = np.average(auprc_list, weights=class_counts) # 其他指标 all_preds = np.argmax(all_probs, axis=1) metrics = { 'accuracy': accuracy_score(all_labels, all_preds), 'f1': f1_score(all_labels, all_preds, average='weighted'), 'auroc': auroc, 'auprc': auprc } return total_loss / len(loader), metrics修改为将预测的结果输出到一个文件夹里