// // Created on 2026/3/6. // // Node APIs are not fully supported. To solve the compilation error of the interface cannot be found, // please include "napi/native_api.h". #include "Camera.h" #ifndef NATIVE_TAG #define NATIVE_TAG "CAMERA" #endif namespace NativeOpenCAX { Camera::Camera() : camera(nullptr) {} Camera::~Camera() {} bool Camera::InitCamera(Handle(V3d_View)& view) { try { camera = new Graphic3d_Camera; // 将角度转换为弧度并计算对应的缩放因子 camera->SetFOVy(45.0); camera->SetZRange(1, 10.0); view->SetCamera(camera); HILOG_ERROR(NATIVE_TAG,"Camera Scale:%{public}f",camera->Scale()); HILOG_INFO(NATIVE_TAG, "InitCamera Done"); return true; } catch (std::exception &e) { HILOG_INFO(NATIVE_TAG, "InitCamera Fail:%{public}d", e.what()); return false; } } void Camera::SetRotation(float xAngle, float yAngle) { gp_Pnt currentEye = camera->Eye(); gp_Pnt currentCenter = camera->Center(); gp_Dir currentUp = camera->Up(); // 计算当前的视线方向和距离 gp_Vec viewDir = currentCenter.XYZ() - currentEye.XYZ(); double distance = viewDir.Magnitude(); gp_Pnt rotationCenter = currentCenter; // 旋转中心设为当前目标点 // 创建旋转增量 gp_Quaternion rotX, rotY; // 从 gp_Ax1 中提取方向向量 (gp_Dir -> gp_Vec) gp_Vec xRotationAxis = camera->Direction().XYZ().Crossed(currentUp.XYZ()); // 叉积得到X旋转轴 if (xRotationAxis.Magnitude() > gp::Resolution()) { // 防止零向量导致的无效四元数 rotX.SetVectorAndAngle(xRotationAxis, xAngle * M_PI / 180.0); } else { rotX.SetIdent(); // 如果叉积为零,则无旋转 } // 从 gp_Ax1 中提取方向向量 (gp_Dir -> gp_Vec) gp_Vec yRotationAxis = currentUp.XYZ(); // Y旋转轴就是当前的Up方向 if (yRotationAxis.Magnitude() > gp::Resolution()) { rotY.SetVectorAndAngle(yRotationAxis, yAngle * M_PI / 180.0); } else { rotY.SetIdent(); // 如果Up向量无效,则无旋转 } // 组合旋转 gp_Quaternion totalRotation = rotY * rotX; // 应用旋转到眼睛位置 gp_Vec newViewDir = totalRotation * viewDir.Normalized(); gp_Pnt newEye = rotationCenter.XYZ() - newViewDir.XYZ() * distance; // 计算新的 Up 方向 gp_Dir newUp = totalRotation * currentUp; // 设置新相机参数 camera->SetEye(newEye); camera->SetCenter(rotationCenter); camera->SetUp(newUp); HILOG_ERROR(NATIVE_TAG,"Rotation"); } } // namespace NativeOpenCAX