Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ bool refinePoseAsItShouldbe(const Mat& pt3D,
BundleAdjustmentCeres bundle_adjustment_obj;
BundleAdjustment::ERefineOptions refineOptions = BundleAdjustment::REFINE_NONE;
if (b_refine_pose)
refineOptions |= sfm::BundleAdjustment::REFINE_ROTATION | sfm::BundleAdjustment::REFINE_TRANSLATION;
refineOptions |= sfm::BundleAdjustment::REFINE_ROTATION | sfm::BundleAdjustment::REFINE_CENTER;
if (b_refine_intrinsic)
refineOptions |= sfm::BundleAdjustment::REFINE_INTRINSICS_ALL;
const bool b_BA_Status = bundle_adjustment_obj.adjust(sfm_data, refineOptions);
Expand Down
4 changes: 2 additions & 2 deletions src/aliceVision/sfm/bundle/BundleAdjustment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class BundleAdjustment
{
REFINE_NONE = 0,
REFINE_ROTATION = 1, //< refine pose rotations
REFINE_TRANSLATION = 2, //< refine pose translations
REFINE_CENTER = 2, //< refine pose centers
REFINE_STRUCTURE = 4, //< refine structure (i.e. 3D points)
REFINE_INTRINSICS_FOCAL = 8, //< refine the focal length
REFINE_INTRINSICS_OPTICALOFFSET_ALWAYS = 16, //< refine the optical offset from the center
Expand All @@ -123,7 +123,7 @@ class BundleAdjustment
/// Refine all intrinsics parameters
REFINE_INTRINSICS_ALL = REFINE_INTRINSICS_FOCAL | REFINE_INTRINSICS_OPTICALOFFSET_IF_ENOUGH_DATA | REFINE_INTRINSICS_DISTORTION,
/// Refine all parameters
REFINE_ALL = REFINE_ROTATION | REFINE_TRANSLATION | REFINE_INTRINSICS_ALL | REFINE_STRUCTURE,
REFINE_ALL = REFINE_ROTATION | REFINE_CENTER | REFINE_INTRINSICS_ALL | REFINE_STRUCTURE,
};

/**
Expand Down
21 changes: 11 additions & 10 deletions src/aliceVision/sfm/bundle/BundleAdjustmentCeres.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,21 @@ void BundleAdjustmentCeres::addExtrinsicsToProblem(const sfmData::SfMData& sfmDa
BundleAdjustment::ERefineOptions refineOptions,
ceres::Problem& problem)
{
const bool refineTranslation = refineOptions & BundleAdjustment::REFINE_TRANSLATION;
const bool refineCenter = refineOptions & BundleAdjustment::REFINE_CENTER;
const bool refineRotation = refineOptions & BundleAdjustment::REFINE_ROTATION;

const auto addPose = [&](const sfmData::CameraPose& cameraPose, bool isConstant, std::array<double, 6>& poseBlock) {
const Mat3& R = cameraPose.getTransform().rotation();
const Vec3& t = cameraPose.getTransform().translation();
const Vec3& c = cameraPose.getTransform().center();

double angleAxis[3];
ceres::RotationMatrixToAngleAxis(static_cast<const double*>(R.data()), angleAxis);
poseBlock.at(0) = angleAxis[0];
poseBlock.at(1) = angleAxis[1];
poseBlock.at(2) = angleAxis[2];
poseBlock.at(3) = t(0);
poseBlock.at(4) = t(1);
poseBlock.at(5) = t(2);
poseBlock.at(3) = c(0);
poseBlock.at(4) = c(1);
poseBlock.at(5) = c(2);

double* poseBlockPtr = poseBlock.data();
problem.AddParameterBlock(poseBlockPtr, 6);
Expand All @@ -249,7 +249,7 @@ void BundleAdjustmentCeres::addExtrinsicsToProblem(const sfmData::SfMData& sfmDa
_allParametersBlocks.push_back(poseBlockPtr);

// keep the camera extrinsics constants
if (cameraPose.isLocked() || isConstant || (!refineTranslation && !refineRotation))
if (cameraPose.isLocked() || isConstant || (!refineCenter && !refineRotation))
{
// set the whole parameter block as constant.
_statistics.addState(EParameter::POSE, EEstimatorParameterState::CONSTANT);
Expand All @@ -269,7 +269,7 @@ void BundleAdjustmentCeres::addExtrinsicsToProblem(const sfmData::SfMData& sfmDa
}

// don't refine translations
if (!refineTranslation)
if (!refineCenter)
{
constantExtrinsic.push_back(3);
constantExtrinsic.push_back(4);
Expand Down Expand Up @@ -1095,7 +1095,7 @@ void BundleAdjustmentCeres::resetProblem()

void BundleAdjustmentCeres::updateFromSolution(sfmData::SfMData& sfmData, ERefineOptions refineOptions) const
{
const bool refinePoses = (refineOptions & REFINE_ROTATION) || (refineOptions & REFINE_TRANSLATION);
const bool refinePoses = (refineOptions & REFINE_ROTATION) || (refineOptions & REFINE_CENTER);
const bool refineIntrinsicsOpticalCenter =
(refineOptions & REFINE_INTRINSICS_OPTICALOFFSET_ALWAYS) || (refineOptions & REFINE_INTRINSICS_OPTICALOFFSET_IF_ENOUGH_DATA);
const bool refineIntrinsics =
Expand Down Expand Up @@ -1130,10 +1130,11 @@ void BundleAdjustmentCeres::updateFromSolution(sfmData::SfMData& sfmData, ERefin

Mat3 R_refined;
ceres::AngleAxisToRotationMatrix(subPoseBlock.data(), R_refined.data());
const Vec3 t_refined(subPoseBlock.at(3), subPoseBlock.at(4), subPoseBlock.at(5));
const Vec3 c_refined(subPoseBlock.at(3), subPoseBlock.at(4), subPoseBlock.at(5));

// update the sub-pose
subPose.pose = poseFromRT(R_refined, t_refined);
subPose.pose.setRotation(R_refined);
subPose.pose.setCenter(c_refined);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/aliceVision/sfm/bundle/BundleAdjustmentCeres.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class BundleAdjustmentCeres : public BundleAdjustment, ceres::EvaluationCallback
/// all parameters blocks pointers
std::vector<double*> _allParametersBlocks;
/// poses blocks wrapper
/// block: ceres angleAxis(3) + translation(3)
/// block: ceres angleAxis(3) + center(3)
std::map<IndexT, std::array<double, 6>> _posesBlocks; // TODO : maybe we can use boost::flat_map instead of std::map ?
/// intrinsics blocks wrapper
/// block: intrinsics params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ BOOST_AUTO_TEST_CASE(test_poses)
}

sfm::BundleAdjustmentCeres::CeresOptions options;
sfm::BundleAdjustment::ERefineOptions refineOptions = sfm::BundleAdjustment::REFINE_ROTATION | sfm::BundleAdjustment::REFINE_TRANSLATION;
sfm::BundleAdjustment::ERefineOptions refineOptions = sfm::BundleAdjustment::REFINE_ROTATION | sfm::BundleAdjustment::REFINE_CENTER;
options.summary = false;

double rmseBefore = sfm::RMSE(sfmData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ BOOST_AUTO_TEST_CASE(BA_temporalConstraint)
sfm::BundleAdjustment::ERefineOptions refineOptions;
refineOptions = sfm::BundleAdjustment::REFINE_ROTATION
| sfm::BundleAdjustment::REFINE_STRUCTURE
| sfm::BundleAdjustment::REFINE_TRANSLATION
| sfm::BundleAdjustment::REFINE_CENTER
| sfm::BundleAdjustment::REFINE_INTRINSICS_ALL;
options.summary = false;

Expand Down
12 changes: 7 additions & 5 deletions src/aliceVision/sfm/bundle/costfunctions/depth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ struct DepthErrorFunctor
// Apply external parameters (Pose)
//--
const T* cam_R = parameter_pose;
const T* cam_t = &parameter_pose[3];
const T* cam_c = &parameter_pose[3];

T centeredPoint[3];
centeredPoint[0] = parameter_point[0] - cam_c[0];
centeredPoint[1] = parameter_point[1] - cam_c[1];
centeredPoint[2] = parameter_point[2] - cam_c[2];

T transformedPoint[3];
// Rotate the point according the camera rotation
ceres::AngleAxisRotatePoint(cam_R, parameter_point, transformedPoint);

// Apply the camera translation
transformedPoint[2] += cam_t[2];
ceres::AngleAxisRotatePoint(cam_R, centeredPoint, transformedPoint);

residuals[0] = T(1.0 / _depthVariance) * (transformedPoint[2] - T(_depth));

Expand Down
71 changes: 37 additions & 34 deletions src/aliceVision/sfm/bundle/costfunctions/projection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ struct ProjectionSimpleErrorFunctor
// Apply external parameters (Pose)
//--
const T* cam_R = parameter_pose;
const T* cam_t = &parameter_pose[3];
const T* cam_c = &parameter_pose[3];

T centeredPoint[3];
centeredPoint[0] = parameter_point[0] - cam_c[0];
centeredPoint[1] = parameter_point[1] - cam_c[1];
centeredPoint[2] = parameter_point[2] - cam_c[2];

T transformedPoint[3];
// Rotate the point according the camera rotation
ceres::AngleAxisRotatePoint(cam_R, parameter_point, transformedPoint);

// Apply the camera translation
transformedPoint[0] += cam_t[0];
transformedPoint[1] += cam_t[1];
transformedPoint[2] += cam_t[2];
ceres::AngleAxisRotatePoint(cam_R, centeredPoint, transformedPoint);

const T * innerParameters[3];
innerParameters[0] = parameter_intrinsics;
Expand Down Expand Up @@ -112,29 +112,28 @@ struct ProjectionErrorFunctor
T transformedPoint[3];
{
const T* cam_R = parameter_pose;
const T* cam_t = &parameter_pose[3];
const T* cam_c = &parameter_pose[3];

// Rotate the point according the camera rotation
ceres::AngleAxisRotatePoint(cam_R, parameter_point, transformedPoint);
T centeredPoint[3];
centeredPoint[0] = parameter_point[0] - cam_c[0];
centeredPoint[1] = parameter_point[1] - cam_c[1];
centeredPoint[2] = parameter_point[2] - cam_c[2];

// Apply the camera translation
transformedPoint[0] += cam_t[0];
transformedPoint[1] += cam_t[1];
transformedPoint[2] += cam_t[2];
// Rotate the point according the camera rotation
ceres::AngleAxisRotatePoint(cam_R, centeredPoint, transformedPoint);
}

{
const T* cam_R = parameter_subpose;
const T* cam_t = &parameter_subpose[3];
const T* cam_c = &parameter_subpose[3];

// Rotate the point according to the camera rotation
T transformedPointBuf[3] = {transformedPoint[0], transformedPoint[1], transformedPoint[2]};
ceres::AngleAxisRotatePoint(cam_R, transformedPointBuf, transformedPoint);
T centeredPoint[3];
centeredPoint[0] = transformedPoint[0] - cam_c[0];
centeredPoint[1] = transformedPoint[1] - cam_c[1];
centeredPoint[2] = transformedPoint[2] - cam_c[2];

// Apply the camera translation
transformedPoint[0] += cam_t[0];
transformedPoint[1] += cam_t[1];
transformedPoint[2] += cam_t[2];
// Rotate the point according to the camera rotation
ceres::AngleAxisRotatePoint(cam_R, centeredPoint, transformedPoint);
}

const T * innerParameters[3];
Expand Down Expand Up @@ -212,22 +211,24 @@ struct ProjectionRelativeErrorFunctor
const T* parameter_refpose = parameters[3];
const T* parameter_relativepoint = parameters[4];

/// (cam_T_world) * (ref_T_world)^-1 * ref_p
/// cam_R_world * (ref_R_world^T * ref_p - ref_R_world^T * ref_t_world) + cam_t_world
/// cam_R_world * (ref_R_world^T * ref_p + ref_c_world) + cam_t_world
/// cam_R_world * (ref_R_world^T * ref_p + ref_c_world) - cam_R_world * cam_c_world
/// cam_R_world * (ref_R_world^T * ref_p + ref_c_world - cam_c_world)

//Retrieve point
T relpoint[3];
relpoint[0] = parameter_relativepoint[0];
relpoint[1] = parameter_relativepoint[1];
relpoint[2] = parameter_relativepoint[2];

const T* refcam_R = parameter_refpose;
const T* refcam_t = &parameter_refpose[3];
const T* refcam_c = &parameter_refpose[3];

// Apply transformation such that the point
// Which was defined in the camera geometric frame
// is now defined in the world frame
relpoint[0] = relpoint[0] - refcam_t[0];
relpoint[1] = relpoint[1] - refcam_t[1];
relpoint[2] = relpoint[2] - refcam_t[2];

T invrefcam_R[3];
invrefcam_R[0] = -refcam_R[0];
invrefcam_R[1] = -refcam_R[1];
Expand All @@ -236,20 +237,22 @@ struct ProjectionRelativeErrorFunctor
T absolutePoint[3];
ceres::AngleAxisRotatePoint(invrefcam_R, relpoint, absolutePoint);

absolutePoint[0] += refcam_c[0];
absolutePoint[1] += refcam_c[1];
absolutePoint[2] += refcam_c[2];

//--
// Apply external parameters (Pose)
//--
const T* cam_R = parameter_pose;
const T* cam_t = &parameter_pose[3];

const T* cam_c = &parameter_pose[3];

absolutePoint[0] -= cam_c[0];
absolutePoint[1] -= cam_c[1];
absolutePoint[2] -= cam_c[2];

// Rotate the point according the camera rotation
ceres::AngleAxisRotatePoint(cam_R, absolutePoint, transformedPoint);

// Apply the camera translation
transformedPoint[0] += cam_t[0];
transformedPoint[1] += cam_t[1];
transformedPoint[2] += cam_t[2];
}

const T * innerParameters[3];
Expand Down
19 changes: 6 additions & 13 deletions src/aliceVision/sfm/bundle/costfunctions/projectionMesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ struct ProjectionMeshErrorFunctor


const T* refcam_r_world = parameter_referencePose;
const T* refcam_t_world = &parameter_referencePose[3];
const T* world_t_refcam = &parameter_referencePose[3];
const T* curcam_r_world = parameter_currentPose;
const T* curcam_t_world = &parameter_currentPose[3];
const T* wolrd_t_curcam = &parameter_currentPose[3];

Comment thread
servantftransperfect marked this conversation as resolved.
T world_r_refcam[3];
world_r_refcam[0] = -refcam_r_world[0];
Expand All @@ -64,21 +64,14 @@ struct ProjectionMeshErrorFunctor
refcam_point[1] = parameter_point[1] / norm;
refcam_point[2] = 1.0 / norm;

// Compute c = - R^t * t
T c[3];
ceres::AngleAxisRotatePoint(world_r_refcam, refcam_t_world, c);
c[0] = -c[0];
c[1] = -c[1];
c[2] = -c[2];

// Compute direction = R^t reference_point
T direction[3];
ceres::AngleAxisRotatePoint(world_r_refcam, refcam_point, direction);


T worldPoint[3];
const T * intersectParameters[2];
intersectParameters[0] = c;
intersectParameters[0] = world_t_refcam;
intersectParameters[1] = direction;
if (!_meshIntersectFunctor(intersectParameters, worldPoint))
{
Expand All @@ -87,10 +80,10 @@ struct ProjectionMeshErrorFunctor

// Compute point in camera coordinates
T transformedPoint[3];
worldPoint[0] -= wolrd_t_curcam[0];
worldPoint[1] -= wolrd_t_curcam[1];
worldPoint[2] -= wolrd_t_curcam[2];
ceres::AngleAxisRotatePoint(curcam_r_world, worldPoint, transformedPoint);
transformedPoint[0] += curcam_t_world[0];
transformedPoint[1] += curcam_t_world[1];
transformedPoint[2] += curcam_t_world[2];

// Project
const T * innerParameters[3];
Expand Down
14 changes: 7 additions & 7 deletions src/aliceVision/sfm/bundle/costfunctions/survey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ struct SurveyErrorFunctor
// Apply external parameters (Pose)
//--
const T* cam_R = parameter_pose;
const T* cam_t = &parameter_pose[3];
const T* cam_c = &parameter_pose[3];

T centeredPoint[3];
centeredPoint[0] = parameter_point[0] - cam_c[0];
centeredPoint[1] = parameter_point[1] - cam_c[1];
centeredPoint[2] = parameter_point[2] - cam_c[2];

T transformedPoint[3];
// Rotate the point according the camera rotation
ceres::AngleAxisRotatePoint(cam_R, parameter_point, transformedPoint);

// Apply the camera translation
transformedPoint[0] += cam_t[0];
transformedPoint[1] += cam_t[1];
transformedPoint[2] += cam_t[2];
ceres::AngleAxisRotatePoint(cam_R, centeredPoint, transformedPoint);

const T * innerParameters[3];
innerParameters[0] = parameter_intrinsics;
Expand Down
24 changes: 10 additions & 14 deletions src/aliceVision/sfm/bundle/costfunctions/temporalConstraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,18 @@ struct TemporalConstraintFunctor
invQuaternion[3] = -quaternion[3];
};

const auto viewPreProcess = [&](const T* angleAxis, const T* translation, T* viewCenter, T* quaternion)
{
T invQuaternion[4];
ceres::AngleAxisToQuaternion(angleAxis, quaternion);
// Inverse the rotation to compute the camera position
quaternionConjugate(quaternion, invQuaternion);
ceres::QuaternionRotatePoint(invQuaternion, translation, viewCenter);
};

T viewCenter0[3], viewCenter1[3], viewCenter2[3], viewCenter3[3];
T quaternion0[4], quaternion1[4], quaternion2[4], quaternion3[4];

viewPreProcess(&para0[0], &para0[3], viewCenter0, quaternion0);
viewPreProcess(&para1[0], &para1[3], viewCenter1, quaternion1);
viewPreProcess(&para2[0], &para2[3], viewCenter2, quaternion2);
viewPreProcess(&para3[0], &para3[3], viewCenter3, quaternion3);
T quaternion0[4], quaternion1[4], quaternion2[4], quaternion3[4];
ceres::AngleAxisToQuaternion(&para0[0], quaternion0);
ceres::AngleAxisToQuaternion(&para1[0], quaternion1);
ceres::AngleAxisToQuaternion(&para2[0], quaternion2);
ceres::AngleAxisToQuaternion(&para3[0], quaternion3);

const T * viewCenter0 = &para0[3];
const T * viewCenter1 = &para1[3];
const T * viewCenter2 = &para2[3];
const T * viewCenter3 = &para3[3];

const auto computeAngleDiff = [&](const T* quaternionA, const T* quaternionB, T* angleAxisDiff, T* quaternionDiff)
{
Expand Down
2 changes: 1 addition & 1 deletion src/aliceVision/sfm/pipeline/expanding/SfmBundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bool SfmBundle::process(sfmData::SfMData & sfmData, const track::TracksHandler &
BundleAdjustment::ERefineOptions refineOptions = BundleAdjustment::REFINE_NONE;

refineOptions |= BundleAdjustment::REFINE_ROTATION;
refineOptions |= BundleAdjustment::REFINE_TRANSLATION;
refineOptions |= BundleAdjustment::REFINE_CENTER;

if (_isStructureRefinementEnabled)
{
Expand Down
2 changes: 1 addition & 1 deletion src/aliceVision/sfm/pipeline/expanding/SfmResection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ bool SfmResection::internalRefinement(
}

BundleAdjustmentCeres BA;
BundleAdjustment::ERefineOptions refineOptions = BundleAdjustment::REFINE_ROTATION | BundleAdjustment::REFINE_TRANSLATION;
BundleAdjustment::ERefineOptions refineOptions = BundleAdjustment::REFINE_ROTATION | BundleAdjustment::REFINE_CENTER;

const bool success = BA.adjust(tinyScene, refineOptions);
if(!success)
Expand Down
Loading
Loading