当前位置: 首页 > news >正文

2.13 转换矩阵

转换矩阵引用了库nalgebra,使用时研究具体实现。

use std::ops;use nalgebra::Perspective3;use crate::Scalar;use super::{Aabb, LineSegment, Point, Triangle, Vector};/// An affine transform
#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
pub struct Transform(nalgebra::Transform<f64, nalgebra::TAffine, 3>);impl Transform {/// Construct an identity transformpub fn identity() -> Self {Self(nalgebra::Transform::identity())}/// Construct a translationpub fn translation(offset: impl Into<Vector<3>>) -> Self {let offset = offset.into();Self(nalgebra::Transform::from_matrix_unchecked(nalgebra::OMatrix::new_translation(&offset.to_na()),))}/// Construct a rotation////// The direction of the vector defines the rotation axis. Its length/// defines the angle of the rotation.pub fn rotation(axis_angle: impl Into<Vector<3>>) -> Self {let axis_angle = axis_angle.into();Self(nalgebra::Transform::from_matrix_unchecked(nalgebra::OMatrix::<_, nalgebra::Const<4>, _>::new_rotation(axis_angle.to_na(),),))}/// Construct a scalingpub fn scale(scaling_factor: f64) -> Self {Self(nalgebra::Transform::from_matrix_unchecked(nalgebra::OMatrix::new_scaling(scaling_factor),))}/// # Extract the "right" vector from the rotational componentpub fn right(&self) -> Vector<3> {let d = self.data();Vector::from([d[0], d[1], d[2]])}/// # Extract the "up" vector from the rotational componentpub fn up(&self) -> Vector<3> {let d = self.data();Vector::from([d[4], d[5], d[6]])}/// Transform the given pointpub fn transform_point(&self, point: &Point<3>) -> Point<3> {Point::from(self.0.transform_point(&point.to_na()))}/// Inverse transform given pointpub fn inverse_transform_point(&self, point: &Point<3>) -> Point<3> {Point::from(self.0.inverse_transform_point(&point.to_na()))}/// Transform the given vectorpub fn transform_vector(&self, vector: &Vector<3>) -> Vector<3> {Vector::from(self.0.transform_vector(&vector.to_na()))}/// Transform the given segmentpub fn transform_segment(&self,segment: &LineSegment<3>,) -> LineSegment<3> {let [a, b] = &segment.points;LineSegment::from([self.transform_point(a), self.transform_point(b)])}/// Transform the given trianglepub fn transform_triangle(&self, triangle: &Triangle<3>) -> Triangle<3> {let [a, b, c] = &triangle.points;Triangle::from([self.transform_point(a),self.transform_point(b),self.transform_point(c),])}/// Inverse transformpub fn inverse(&self) -> Self {Self(self.0.inverse())}/// Transpose transformpub fn transpose(&self) -> Self {Self(nalgebra::Transform::from_matrix_unchecked(self.0.to_homogeneous().transpose(),))}/// Project transform according to camera specification, return data as an array./// Used primarily for graphics code.pub fn project_to_array(&self,aspect_ratio: f64,fovy: f64,znear: f64,zfar: f64,) -> [Scalar; 16] {let projection = Perspective3::new(aspect_ratio, fovy, znear, zfar);let mut array = [0.; 16];array.copy_from_slice((projection.to_projective() * self.0).matrix().as_slice(),);array.map(Scalar::from)}/// Return a copy of the inner nalgebra transformpub fn get_inner(&self) -> nalgebra::Transform<f64, nalgebra::TAffine, 3> {self.0}/// Transform the given axis-aligned bounding boxpub fn transform_aabb(&self, aabb: &Aabb<3>) -> Aabb<3> {Aabb {min: self.transform_point(&aabb.min),max: self.transform_point(&aabb.max),}}/// Exposes the data of this Transform as a slice of f64.pub fn data(&self) -> &[f64] {self.0.matrix().data.as_slice()}/// Extract the rotation component of this transformpub fn extract_rotation(&self) -> Self {Self(nalgebra::Transform::from_matrix_unchecked(self.0.matrix().fixed_resize::<3, 3>(0.).to_homogeneous(),))}/// Extract the translation component of this transformpub fn extract_translation(&self) -> Self {*self * self.extract_rotation().inverse()}
}impl ops::Mul<Self> for Transform {type Output = Self;fn mul(self, rhs: Self) -> Self::Output {Self(self.0.mul(rhs.0))}
}#[cfg(test)]
mod tests {use approx::assert_abs_diff_eq;use crate::{Scalar, Vector};use super::Transform;#[test]fn extract_rotation_translation() {let rotation =Transform::rotation(Vector::unit_z() * (Scalar::PI / 2.));let translation = Transform::translation([1., 2., 3.]);assert_abs_diff_eq!((translation * rotation).extract_rotation().data(),rotation.data(),epsilon = 1e-8,);assert_abs_diff_eq!((translation * rotation).extract_translation().data(),translation.data(),epsilon = 1e-8,);assert_abs_diff_eq!((rotation * translation).extract_rotation().data(),rotation.data(),epsilon = 1e-8,);assert_abs_diff_eq!((rotation * translation).extract_translation().data(),Transform::translation([-2., 1., 3.]).data(),epsilon = 1e-8,);}
}

相关文章:

2.13 转换矩阵

转换矩阵引用了库nalgebra&#xff0c;使用时研究具体实现。 use std::ops;use nalgebra::Perspective3;use crate::Scalar;use super::{Aabb, LineSegment, Point, Triangle, Vector};/// An affine transform #[repr(C)] #[derive(Debug, Clone, Copy, Default)] pub struct…...

【C语言】遗传算法matlab程序

遗传算法matlab程序 遗传算法是一种模拟自然选择过程的优化技术&#xff0c;用于解决复杂问题。在MATLAB中编写遗传算法程序&#xff0c;通常包括以下几个步骤&#xff1a; 初始化种群&#xff1a;创建一个初始解集&#xff08;种群&#xff09;&#xff0c;每个解代表一个问题…...

Java LinkedList 详解

LinkedList 是 Java 集合框架中常用的数据结构之一&#xff0c;位于 java.util 包中。它实现了 List、Deque 和 Queue 接口&#xff0c;是一个双向链表结构&#xff0c;适合频繁的插入和删除操作。 1. LinkedList 的特点 数据结构&#xff1a;基于双向链表实现&#xff0c;每个…...

mac-mini的时间机器,数据备份到alist 中的网盘

苹果的时间机器不能直接将备份存储在 alist 上的网盘&#xff0c;但可以通过一些中间步骤来实现类似的效果&#xff0c;以下是具体介绍&#xff1a; 方法原理 通过将支持 WebDAV 协议的网络存储挂载到本地&#xff0c;再将其设置为时间机器的备份磁盘&#xff0c;从而间接实现…...

【HarmonyOS】鸿蒙应用加载读取csv文件

【HarmonyOS】鸿蒙应用加载读取csv文件 一、问题背景&#xff1a; 1. csv文件是什么&#xff1f; csv是一种文本文件格式&#xff0c;与json类似。会存储一些文本内容&#xff0c;应用需要读取该文件&#xff0c;进行UI内容得填充等。 文件中的数据是以纯文本形式存储的&…...

Java retainAll() 详解

在 Java 中&#xff0c;retainAll() 是 Collection 接口&#xff08;List、Set 等集合类实现该接口&#xff09;的一种方法&#xff0c;用于保留集合中与指定集合交集的元素&#xff0c;删除其他所有元素。 以下是对 retainAll() 方法的详细讲解。 1. 方法定义 方法签名 boo…...

Redis的基本数据类型

初识Redis缓存 Redis缓存&#xff1a; 实际开发中经常使用Redis作为缓存数据库&#xff0c;从而提高数据存取效率&#xff0c;减轻后端数据库的压力。 可以将经常被查询的数据缓存起来&#xff0c;比如热点数据&#xff0c;这样当用户来访问的时候&#xff0c;就不需要到MyS…...

通过vite+vue3+pinia从0到1搭建一个uniapp应用

最近项目上要做一个app&#xff0c;选择了用uniapp作为开发框架&#xff1b;我大概看了一下uniapp的文档&#xff0c;根据文档从0到1搭了一个uniapp应用供大家参考。 因为本人习惯使用了WebStorm编译器&#xff0c;但是uniapp官方推荐使用HBuilder搭建&#xff0c;如果和我一样…...

Linux的桌面

Linux的桌面是可以卸载的 的确&#xff0c;Linux并不像Windows&#xff0c;Linux本身是一个基于命令行的操作系统&#xff0c;在内核眼中&#xff0c;桌面只不过是个普通的应用程序&#xff0c;所以&#xff0c;在Linux的桌面中可以完成的事情&#xff0c;命令行中也基本可以完…...

Easyexcel(5-自定义列宽)

相关文章链接 Easyexcel&#xff08;1-注解使用&#xff09;Easyexcel&#xff08;2-文件读取&#xff09;Easyexcel&#xff08;3-文件导出&#xff09;Easyexcel&#xff08;4-模板文件&#xff09;Easyexcel&#xff08;5-自定义列宽&#xff09; 注解 ColumnWidth Data…...

操作系统实验 C++实现死锁检测算法

实验目的 模拟实现死锁检测算法 实验内容 1、 输入&#xff1a; “资源分配表”文件&#xff0c;每一行包含资源编号、进程编号两项&#xff08;均用整数表示&#xff0c;并用空格分隔开&#xff09;&#xff0c;记录资源分配给了哪个进程。 “进程等待表”文件&…...

小鹏汽车智慧材料数据库系统项目总成数据同步

1、定时任务处理 2、提供了接口 小鹏方面提供的推送的数据表结构&#xff1a; 这几个表总数为100多万&#xff0c;经过条件筛选过滤后大概2万多条数据 小鹏的人给的示例图&#xff1a; 界面&#xff1a; SQL: -- 查询车型 select bmm.md_material_id, bmm.material_num, bm…...

1、HCIP之RSTP协议与STP相关安全配置

目录 RSTP—快速生成树协议 STP STP的缺点&#xff1a; STP的选举&#xff08;Listening状态中&#xff09;&#xff1a; RSTP P/A&#xff08;提议/同意&#xff09;机制 同步机制&#xff1a; 边缘端口的配置&#xff1a; RSTP的端口角色划分&#xff1a; ensp模拟…...

Linux云服务器docker使用教程

诸神缄默不语-个人CSDN博文目录 我用的是腾讯云服务器&#xff0c;操作系统是OpenCloudOS 9&#xff0c;基本上可以当特色版CentOS用。 docker安装跟各个系统关系太大了&#xff0c;我就不写了。OpenCloudOS 9安装docker见这篇博文&#xff1a;腾讯云服务器使用教程 文章目录 …...

如何从android的webview 取得页面上的数据

要从Android的WebView中获取页面上的数据&#xff0c;通常有几种常见的方法&#xff1a; JavaScript Interface&#xff1a;通过JavaScript和Android Interface进行通信。这种方法允许你在JavaScript中调用Android的方法&#xff0c;反之亦然。 Evaluate JavaScript&#xff…...

VTK知识学习(12)- 读取PNG图像

1、代码 private void ShowPngImage(){vtkPNGReader pngReader vtkPNGReader.New();pngReader.SetFileName("D:\\图像\\boxes\\cardboard_boxes_01.png");pngReader.Update();vtkImageActor imageActor vtkImageActor.New();imageActor.SetInputData(pngReader.Get…...

Springboot项目搭建(3)-更改用户信息与文件上传

1.概要 前一章节完成了用户信息的注册、登录、详细信息查询&#xff0c;以及线程池与拦截器技术。 这一章完善了用户信息更新/更改功能&#xff0c;包括昵称、邮箱、头像、密码等... 而后接触到了本地上传和云上传&#xff0c;其二者区别&#xff1a; 选择本地上传还是云上…...

Docker1:认识docker、在Linux中安装docker

欢迎来到“雪碧聊技术”CSDN博客&#xff01; 在这里&#xff0c;您将踏入一个专注于Java开发技术的知识殿堂。无论您是Java编程的初学者&#xff0c;还是具有一定经验的开发者&#xff0c;相信我的博客都能为您提供宝贵的学习资源和实用技巧。作为您的技术向导&#xff0c;我将…...

python成绩分级 2024年6月python二级真题 青少年编程电子学会编程等级考试python二级真题解析

目录 python成绩分级 一、题目要求 1、编程实现 2、输入输出 二、算法分析 三、程序代码 四、程序说明 五、运行结果 六、考点分析 七、 推荐资料 1、蓝桥杯比赛 2、考级资料 3、其它资料 python成绩分级 2024年6月 python编程等级考试二级编程题 一、题目要求 …...

android 如何获取当前 Activity 的类名和包名

其一&#xff1a;getClass().getSimpleName() public static String getTopActivity(Context context){ ActivityManager am (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); ComponentName cn am.getRunningTasks(1).get(0).topAct…...

Foobar2000终极歌词体验:三平台逐字歌词完整配置指南

Foobar2000终极歌词体验&#xff1a;三平台逐字歌词完整配置指南 【免费下载链接】ESLyric-LyricsSource Advanced lyrics source for ESLyric in foobar2000 项目地址: https://gitcode.com/gh_mirrors/es/ESLyric-LyricsSource 还在为Foobar2000找不到高质量的逐字歌词…...

AI教材编写必备:低查重AI工具,助力快速完成教材创作!

教材编写工具的选择与使用 在开始写教材之前&#xff0c;选择合适的工具几乎就像是一场“纠结大赛”&#xff01;如果使用办公软件&#xff0c;功能通常很有限&#xff0c;框架和格式都需要手动去调整&#xff1b;而使用专业的编写工具&#xff0c;又往往因操作繁琐和学习曲线…...

基于FPGA的嵌入式频谱分析仪设计:低功耗实时信号处理方案

1. 项目概述&#xff1a;为什么要在FPGA上做频谱分析仪&#xff1f;做射频测试的工程师&#xff0c;对频谱分析仪肯定不陌生。实验室里动辄几十万上百万的台式机&#xff0c;性能强悍&#xff0c;功能全面&#xff0c;但有个问题&#xff1a;它离不开实验室。当你需要做外场测试…...

Android Debug Bridge (adb) 深度解析:从架构原理到自动化实战

1. 项目概述&#xff1a;从“黑盒”到“白盒”的调试桥梁如果你是一名移动应用开发者、测试工程师&#xff0c;或者是一名热衷于折腾手机、平板的极客&#xff0c;那么“adb”这个词对你来说一定不陌生。它就像一把万能钥匙&#xff0c;静静地躺在你的开发工具目录里&#xff0…...

从零玩转 Linux:网络配置、软件安装及 Docker 实战

下载镜像地址 一、基础命令篇 显示网络状态工具 netstat -nltup #显示当前服务以及端口信息等 查看某个端口是否开启 1.2.1、使用 netstat 命令 sudo netstat -tuln | grep 80 1.2.2、使用 ss 命令 sudo ss -tuln | grep 80 1.2.3、使用 lsof 命令 sudo lsof -i :80 1.2.4、使用…...

免费开源鼠标连点器:5分钟上手跨平台自动化点击完整指南

免费开源鼠标连点器&#xff1a;5分钟上手跨平台自动化点击完整指南 【免费下载链接】MouseClick &#x1f5b1;️ MouseClick &#x1f5b1;️ 是一款功能强大的鼠标连点器和管理工具&#xff0c;采用 QT Widget 开发 &#xff0c;具备跨平台兼容性 。软件界面美观 &#xff0…...

防爆控制柜制造:从危险区域适配到电气安全的完整解析

一、什么是防爆控制柜制造&#xff1f;防爆控制柜制造&#xff0c;是指根据化工厂、石油化工、制药车间、喷涂车间、粉尘车间、油漆房、燃气站、危化品仓库、煤化工、粮食加工、木粉加工、新能源材料、电子化学品等存在爆炸性气体、蒸气或粉尘环境的场所需求&#xff0c;对防爆…...

AltStore技术深度解析:非越狱iOS侧载方案实战指南

AltStore技术深度解析&#xff1a;非越狱iOS侧载方案实战指南 【免费下载链接】AltStore AltStore is an alternative app store for non-jailbroken iOS devices. 项目地址: https://gitcode.com/gh_mirrors/al/AltStore 在iOS生态系统中&#xff0c;应用分发一直受到A…...

真正的爱是接受对方本来的样子

武志红说&#xff1a;爱是如TA所是&#xff0c;而非如你所愿。真正的爱是接受对方本来的样子&#xff0c;而不是把对方改造成你想要的样子。爱是如TA所是意味着&#xff1a;你爱的是这个人本身&#xff0c;而不是你想象中的TA。你不需要改变对方来满足你的期望。你接受TA的优点…...

AI双轨制实战指南:MoE架构、异构模态与弹性推理的工程落地

1. 这不是新闻简报&#xff0c;而是一份AI地缘技术格局的实操观察手记你点开这篇文字&#xff0c;大概率不是为了读一篇“本周AI大事件汇总”。如果你真需要那种信息&#xff0c;直接刷Twitter或Hugging Face的Weekly Digest就够了。我写这个&#xff0c;是因为过去三个月里&am…...