|
| 1 | +// This file is part of the AliceVision project. |
| 2 | +// Copyright (c) 2025 AliceVision contributors. |
| 3 | +// This Source Code Form is subject to the terms of the Mozilla Public License, |
| 4 | +// v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 | +// You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +#include <aliceVision/sfmDataIO/UsdExporter.hpp> |
| 8 | +#include <aliceVision/camera/IntrinsicScaleOffset.hpp> |
| 9 | +#include <aliceVision/system/Logger.hpp> |
| 10 | + |
| 11 | +#include <pxr/usd/usd/stage.h> |
| 12 | + |
| 13 | +#include <pxr/usd/usdGeom/xform.h> |
| 14 | +#include <pxr/usd/usdGeom/points.h> |
| 15 | +#include <pxr/usd/usdGeom/camera.h> |
| 16 | + |
| 17 | +#include <pxr/base/gf/vec3f.h> |
| 18 | +#include <pxr/base/vt/array.h> |
| 19 | + |
| 20 | + |
| 21 | +PXR_NAMESPACE_USING_DIRECTIVE |
| 22 | + |
| 23 | +namespace aliceVision { |
| 24 | +namespace sfmDataIO { |
| 25 | + |
| 26 | + |
| 27 | +UsdExporter::UsdExporter(const std::string & filename, double frameRate) |
| 28 | +{ |
| 29 | + _stage = UsdStage::CreateNew(filename); |
| 30 | + |
| 31 | + UsdGeomXform worldPrim = UsdGeomXform::Define(_stage, SdfPath("/World")); |
| 32 | + |
| 33 | + _stage->SetTimeCodesPerSecond(frameRate); |
| 34 | + _stage->SetFramesPerSecond(frameRate); |
| 35 | + _startTimeCode = std::numeric_limits<IndexT>::max(); |
| 36 | + _endTimeCode = 0; |
| 37 | +} |
| 38 | + |
| 39 | +void UsdExporter::terminate() |
| 40 | +{ |
| 41 | + _stage->SetStartTimeCode(_startTimeCode); |
| 42 | + _stage->SetEndTimeCode(_endTimeCode); |
| 43 | + |
| 44 | + _stage->Save(); |
| 45 | +} |
| 46 | + |
| 47 | +void UsdExporter::createNewCamera(const std::string & cameraName) |
| 48 | +{ |
| 49 | + SdfPath cameraPath("/World/" + cameraName); |
| 50 | + UsdGeomCamera camera = UsdGeomCamera::Define(_stage, cameraPath); |
| 51 | + |
| 52 | + UsdAttribute projectionAttr = camera.GetProjectionAttr(); |
| 53 | + projectionAttr.Set(UsdGeomTokens->perspective); |
| 54 | + |
| 55 | + UsdGeomXformable xformable(camera); |
| 56 | + UsdGeomXformOp motion = xformable.MakeMatrixXform(); |
| 57 | + GfMatrix4d identity(1.0); |
| 58 | + motion.Set(identity); |
| 59 | +} |
| 60 | + |
| 61 | +void UsdExporter::addFrame(const std::string & cameraName, const sfmData::CameraPose & pose, const camera::Pinhole & intrinsic, IndexT frameId) |
| 62 | +{ |
| 63 | + SdfPath cameraPath("/World/" + cameraName); |
| 64 | + UsdGeomCamera camera = UsdGeomCamera::Get(_stage, cameraPath); |
| 65 | + |
| 66 | + _startTimeCode = std::min(_startTimeCode, frameId); |
| 67 | + _endTimeCode = std::max(_endTimeCode, frameId); |
| 68 | + |
| 69 | + UsdAttribute focalLengthAttr = camera.GetFocalLengthAttr(); |
| 70 | + UsdAttribute horizontalApertureAttr = camera.GetHorizontalApertureAttr(); |
| 71 | + UsdAttribute verticalApertureAttr = camera.GetVerticalApertureAttr(); |
| 72 | + UsdAttribute horizontalApertureOffsetAttr = camera.GetHorizontalApertureOffsetAttr(); |
| 73 | + UsdAttribute verticalApertureOffsetAttr = camera.GetVerticalApertureOffsetAttr(); |
| 74 | + |
| 75 | + horizontalApertureAttr.Set(static_cast<float>(intrinsic.sensorWidth())); |
| 76 | + verticalApertureAttr.Set(static_cast<float>(intrinsic.sensorHeight())); |
| 77 | + |
| 78 | + UsdTimeCode t(frameId); |
| 79 | + double pixToMillimeters = intrinsic.sensorWidth() / intrinsic.w(); |
| 80 | + |
| 81 | + horizontalApertureOffsetAttr.Set(static_cast<float>(intrinsic.getOffset().x() * pixToMillimeters), t); |
| 82 | + verticalApertureOffsetAttr.Set(static_cast<float>(intrinsic.getOffset().y() * pixToMillimeters), t); |
| 83 | + focalLengthAttr.Set(static_cast<float>(intrinsic.getFocalLength()), t); |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + //Transform sfmData pose to usd pose |
| 88 | + Eigen::Matrix4d glTransform = Eigen::Matrix4d::Identity(); |
| 89 | + glTransform(1, 1) = -1.0; |
| 90 | + glTransform(2, 2) = -1.0; |
| 91 | + |
| 92 | + // Inverse the pose and change the geometric frame |
| 93 | + Eigen::Matrix4d camera_T_world = pose.getTransform().getHomogeneous(); |
| 94 | + Eigen::Matrix4d world_T_camera = camera_T_world.inverse(); |
| 95 | + Eigen::Matrix4d world_gl_T_camera_gl = glTransform * world_T_camera * glTransform; |
| 96 | + |
| 97 | + //Copy element by element while transposing |
| 98 | + GfMatrix4d usdT; |
| 99 | + for (int i = 0; i < 4; i++) |
| 100 | + { |
| 101 | + for (int j = 0; j < 4; j++) |
| 102 | + { |
| 103 | + usdT[j][i] = world_gl_T_camera_gl(i, j); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + //Assign pose to motion |
| 108 | + UsdGeomXformable xformable(camera); |
| 109 | + bool dummy = false; |
| 110 | + std::vector<UsdGeomXformOp> xformOps = xformable.GetOrderedXformOps(&dummy); |
| 111 | + |
| 112 | + |
| 113 | + if (!xformOps.empty()) { |
| 114 | + UsdGeomXformOp motion = xformOps[0]; |
| 115 | + motion.Set(usdT, t); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +} // namespace sfmDataIO |
| 120 | +} // namespace aliceVision |
0 commit comments