1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004-2016 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
#ifndef VCG_POINT_MATCHING_SCALE
#define VCG_POINT_MATCHING_SCALE
#include <vcg/math/matrix44.h>
#include <vcg/space/point3.h>
#include <vcg/space/box3.h>
#include <wrap/newuoa/include/newuoa.h>
namespace vcg {
template <class Scalar>
struct RotoTranslation
{
RotoTranslation(){}
Scalar _v[6];
void toMatrix(vcg::Matrix44<Scalar> & m)
{
vcg::Matrix44<Scalar> rot,tra;
rot.FromEulerAngles(_v[0],_v[1],_v[2]);
tra.SetTranslate(vcg::Point3<Scalar>(_v[3],_v[4],_v[5]));
m = tra * rot;
}
};
class PointMatchingScale {
private:
static std::vector<vcg::Point3d> *fix;
static std::vector<vcg::Point3d> *mov;
static vcg::Box3d b;
public:
/**
* Compute a scaling transformation that bring PMov point as close as possible to Pfix
*/
static void computeScalingMatchMatrix(
vcg::Matrix44d &res,
std::vector<vcg::Point3d> &Pfix,
std::vector<vcg::Point3d> &Pmov)
{
fix = &Pfix;
mov = &Pmov;
b.SetNull();
for(std::vector<vcg::Point3d>::iterator i = Pmov.begin(); i != Pmov.end(); ++i)
b.Add(*i);
double scale = 1.0;
min_newuoa(1,&scale,errorScale);
res.SetTranslate( b.Center()*(1.0-scale));
res[0][0] = res[1][1] = res[2][2] = scale;
}
/**
* Compute a rototranslation + scaling transformation that bring PMov point as close as possible to Pfix
*/
static void computeRotoTranslationScalingMatchMatrix(
vcg::Matrix44d &res,
std::vector<vcg::Point3d> &Pfix,
std::vector<vcg::Point3d> &Pmov)
{
fix = &Pfix;
mov = &Pmov;
b.SetNull();
for(std::vector<vcg::Point3d>::iterator i = Pmov.begin(); i != Pmov.end(); ++i)
b.Add(*i);
double x[7]={1.0,0.0,0.0,0.0,0.0,0.0,0.0};
min_newuoa(7,&x[0],errorRotoTranslationScale);
// rtm = rototranslation
RotoTranslation<double> rt;
vcg::Matrix44d rtm;
memcpy(&rt._v[0],&x[1],6*sizeof(double));
rt.toMatrix(rtm);
// res= scaling w.r.t. barycenter
res.SetTranslate( b.Center()*(1.0-x[0]));
res[0][0] = res[1][1] = res[2][2] = x[0];
res = rtm*res;
}
static double errorScale(int n, double *x)
{
assert(n==1); (void)n;
double dist = 0;
std::vector<vcg::Point3d>::iterator i = mov->begin();
std::vector<vcg::Point3d>::iterator ifix = fix->begin();
for(; i != mov->end(); ++i,++ifix)
dist += vcg::SquaredDistance(((*i)-b.Center())*(*x)+b.Center() , *ifix);
return dist;
}
static double errorRotoTranslationScale(int n, double *x) {
assert(n==7); (void)n;
double dist = 0;
std::vector<vcg::Point3d>::iterator i = mov->begin();
std::vector<vcg::Point3d>::iterator ifix = fix->begin();
RotoTranslation<double> rt;
vcg::Matrix44d m;
memcpy(&rt._v[0],&x[1],6*sizeof(double));
rt.toMatrix(m);
for(; i != mov->end(); ++i,++ifix) {
dist += vcg::SquaredDistance( m*(((*i)-b.Center())*(x[0])+b.Center()),*ifix);
}
return dist;
}
};
}
#endif
|