// Copyright Epic Games, Inc. All Rights Reserved. #include "MetaHumanStereoCalibrator.h" #include "api/MultiCameraCalibration.h" #include "SetCamerasHelper.h" namespace UE { namespace Wrappers { void PointsVectorToTArrayPoints(const std::vector& InPoints, TArray& OutPoints) { std::size_t PointsSize = InPoints.size() / 2; OutPoints.Reserve(PointsSize); for (std::size_t PointIndex = 0; PointIndex < PointsSize; ++PointIndex) { double x = InPoints[PointIndex * 2]; double y = InPoints[PointIndex * 2 + 1]; OutPoints.Emplace(FVector2D(x,y)); } } void TArrayToPointsVector(const TArray& InPoints, std::vector& OutPoints) { OutPoints.reserve(InPoints.Num() * 2); for (int32 PointIndex = 0; PointIndex < InPoints.Num(); PointIndex++) { OutPoints.push_back(InPoints[PointIndex].X); OutPoints.push_back(InPoints[PointIndex].Y); } } struct FMetaHumanStereoCalibrator::Private { TITAN_API_NAMESPACE::MultiCameraCalibration API; }; FMetaHumanStereoCalibrator::FMetaHumanStereoCalibrator() { Impl = MakePimpl(); } bool FMetaHumanStereoCalibrator::Init(uint32 PatternWidth, uint32 PatternHeight, float SquareSize) { return Impl->API.Init(PatternWidth, PatternHeight, SquareSize); } bool FMetaHumanStereoCalibrator::AddCamera(const FString& InCameraName, uint32 InWidth, uint32 InHeight) { return Impl->API.AddCamera(TCHAR_TO_ANSI(*InCameraName), InWidth, InHeight); } bool FMetaHumanStereoCalibrator::DetectPattern(const FString& InCameraName, const unsigned char* InImage, TArray& OutCornerPoints, double& OutChessboardSharpness) { std::vector DetectedPoints; bool DetectionSucessful = Impl->API.DetectPattern(TCHAR_TO_ANSI(*InCameraName), InImage, DetectedPoints, OutChessboardSharpness); PointsVectorToTArrayPoints(DetectedPoints, OutCornerPoints); return DetectionSucessful; } bool FMetaHumanStereoCalibrator::Calibrate(const TArray>>& InPointsPerCameraPerFrame, TArray& OutCalibrations, double& OutMse) { std::vector>> PointsVectorPerCameraPerFrame; std::map Calibrations; for (const TMap>& PointsPerCamera : InPointsPerCameraPerFrame) { std::map>& PointsVectorPerCamera = PointsVectorPerCameraPerFrame.emplace_back(); for (const TPair>& Points: PointsPerCamera) { PointsVectorPerCamera[TCHAR_TO_ANSI(*Points.Key)] = std::vector(); TArrayToPointsVector(Points.Value, PointsVectorPerCamera[TCHAR_TO_ANSI(*Points.Key)]); } } bool CalibrationSucessful = Impl->API.Calibrate(PointsVectorPerCameraPerFrame, Calibrations, OutMse); if (CalibrationSucessful) { GetCalibrationsHelper(Calibrations, OutCalibrations); } return CalibrationSucessful; } bool FMetaHumanStereoCalibrator::ExportCalibrations(const TArray& InCalibrations, const FString& ExportFilepath) { std::map OpenCVCameras; SetCamerasHelper(InCalibrations, OpenCVCameras); return Impl->API.ExportCalibrations(OpenCVCameras, TCHAR_TO_ANSI(*ExportFilepath)); } } }