[CC]平面拟合

时间:2022-02-28 07:00:35

  常见的平面拟合方法一般是最小二乘法。当误差服从正态分布时,最小二乘方法的拟合效果还是很好的,可以转化成PCA问题。

  当观测值的误差大于2倍中误差时,认为误差较大。采用最小二乘拟合时精度降低,不够稳健。

  提出了一些稳健的方法:有移动最小二乘法(根据距离残差增加权重);采用2倍距离残差的协方差剔除离群点;迭代重权重方法(选权迭代法)。

  MainWindow中的平面拟合方法,调用了ccPlane的Fit方法。

 void MainWindow::doActionFitPlane()
{
doComputePlaneOrientation(false);
} void MainWindow::doActionFitFacet()
{
doComputePlaneOrientation(true);
} static double s_polygonMaxEdgeLength = ;
void MainWindow::doComputePlaneOrientation(bool fitFacet)
{
ccHObject::Container selectedEntities = m_selectedEntities;
size_t selNum = selectedEntities.size();
if (selNum < )
return; double maxEdgeLength = ;
if (fitFacet)
{
bool ok = true;
maxEdgeLength = QInputDialog::getDouble(this,"Fit facet", "Max edge length (0 = no limit)", s_polygonMaxEdgeLength, , 1.0e9, , &ok);
if (!ok)
return;
s_polygonMaxEdgeLength = maxEdgeLength;
} for (size_t i=; i<selNum; ++i)
{
ccHObject* ent = selectedEntities[i];
ccShiftedObject* shifted = ;
CCLib::GenericIndexedCloudPersist* cloud = ; if (ent->isKindOf(CC_TYPES::POLY_LINE))
{
ccPolyline* poly = ccHObjectCaster::ToPolyline(ent);
cloud = static_cast<CCLib::GenericIndexedCloudPersist*>(poly);
shifted = poly;
}
else
{
ccGenericPointCloud* gencloud = ccHObjectCaster::ToGenericPointCloud(ent);
if (gencloud)
{
cloud = static_cast<CCLib::GenericIndexedCloudPersist*>(gencloud);
shifted = gencloud;
}
} if (cloud)
{
double rms = 0.0;
CCVector3 C,N; ccHObject* plane = ;
if (fitFacet)
{
ccFacet* facet = ccFacet::Create(cloud, static_cast<PointCoordinateType>(maxEdgeLength));
if (facet)
{
plane = static_cast<ccHObject*>(facet);
N = facet->getNormal();
C = facet->getCenter();
rms = facet->getRMS(); //manually copy shift & scale info!
if (shifted)
{
ccPolyline* contour = facet->getContour();
if (contour)
{
contour->setGlobalScale(shifted->getGlobalScale());
contour->setGlobalShift(shifted->getGlobalShift());
}
}
}
}
else
{
ccPlane* pPlane = ccPlane::Fit(cloud, &rms);
if (pPlane)
{
plane = static_cast<ccHObject*>(pPlane);
N = pPlane->getNormal();
C = *CCLib::Neighbourhood(cloud).getGravityCenter();
pPlane->enableStippling(true);
}
} //as all information appears in Console...
forceConsoleDisplay(); if (plane)
{
ccConsole::Print(QString("[Orientation] Entity '%1'").arg(ent->getName()));
ccConsole::Print("\t- plane fitting RMS: %f",rms); //We always consider the normal with a positive 'Z' by default!
if (N.z < 0.0)
N *= -1.0;
ccConsole::Print("\t- normal: (%f,%f,%f)",N.x,N.y,N.z); //we compute strike & dip by the way
PointCoordinateType dip = , dipDir = ;
ccNormalVectors::ConvertNormalToDipAndDipDir(N,dip,dipDir);
QString dipAndDipDirStr = ccNormalVectors::ConvertDipAndDipDirToString(dip,dipDir);
ccConsole::Print(QString("\t- %1").arg(dipAndDipDirStr)); //hack: output the transformation matrix that would make this normal points towards +Z
ccGLMatrix makeZPosMatrix = ccGLMatrix::FromToRotation(N,CCVector3(,,PC_ONE));
CCVector3 Gt = C;
makeZPosMatrix.applyRotation(Gt);
makeZPosMatrix.setTranslation(C-Gt);
ccConsole::Print("[Orientation] A matrix that would make this plane horizontal (normal towards Z+) is:");
ccConsole::Print(makeZPosMatrix.toString(,' ')); //full precision
ccConsole::Print("[Orientation] You can copy this matrix values (CTRL+C) and paste them in the 'Apply transformation tool' dialog"); plane->setName(dipAndDipDirStr);
plane->applyGLTransformation_recursive(); //not yet in DB
plane->setVisible(true);
plane->setSelectionBehavior(ccHObject::SELECTION_FIT_BBOX); ent->addChild(plane);
plane->setDisplay(ent->getDisplay());
plane->prepareDisplayForRefresh_recursive();
addToDB(plane);
}
else
{
ccConsole::Warning(QString("Failed to fit a plane/facet on entity '%1'").arg(ent->getName()));
}
}
} refreshAll();
updateUI();
}

ccPlane的fit方法:

ccPlane* ccPlane::Fit(CCLib::GenericIndexedCloudPersist *cloud, double* rms/*=0*/)
{
//number of points
unsigned count = cloud->size();
if (count < 3)
{
ccLog::Warning("[ccPlane::Fit] Not enough points in input cloud to fit a plane!");
return 0;
} CCLib::Neighbourhood Yk(cloud); //plane equation
const PointCoordinateType* theLSPlane = Yk.getLSPlane();
if (!theLSPlane)
{
ccLog::Warning("[ccPlane::Fit] Not enough points to fit a plane!");
return 0;
} //get the centroid
const CCVector3* G = Yk.getGravityCenter();
assert(G); //and a local base
CCVector3 N(theLSPlane);
const CCVector3* X = Yk.getLSPlaneX(); //main direction
assert(X);
CCVector3 Y = N * (*X); //compute bounding box in 2D plane
CCVector2 minXY(0,0), maxXY(0,0);
cloud->placeIteratorAtBegining();
for (unsigned k=0; k<count; ++k)
{
//projection into local 2D plane ref.
CCVector3 P = *(cloud->getNextPoint()) - *G; CCVector2 P2D( P.dot(*X), P.dot(Y) ); if (k != 0)
{
if (minXY.x > P2D.x)
minXY.x = P2D.x;
else if (maxXY.x < P2D.x)
maxXY.x = P2D.x;
if (minXY.y > P2D.y)
minXY.y = P2D.y;
else if (maxXY.y < P2D.y)
maxXY.y = P2D.y;
}
else
{
minXY = maxXY = P2D;
}
} //we recenter the plane
PointCoordinateType dX = maxXY.x-minXY.x;
PointCoordinateType dY = maxXY.y-minXY.y;
CCVector3 Gt = *G + *X * (minXY.x + dX / 2) + Y * (minXY.y + dY / 2);
ccGLMatrix glMat(*X,Y,N,Gt); ccPlane* plane = new ccPlane(dX, dY, &glMat); //compute least-square fitting RMS if requested
if (rms)
{
*rms = CCLib::DistanceComputationTools::computeCloud2PlaneDistanceRMS(cloud, theLSPlane);
plane->setMetaData(QString("RMS"),QVariant(*rms));
} return plane;
}

Efficient Ransac shape extract插件调用的模板类Plane实现,可以看到使用的Jacobi特征值分解的方法实现。

        template< class PointT >
template< class PointsForwardIt, class WeightsForwardIt >
bool Plane< PointT >::Fit(const PointType &origin, PointsForwardIt begin,PointsForwardIt end, WeightsForwardIt weights)
{
MatrixXX< PointType::Dim, PointType::Dim, ScalarType > c, v;
CovarianceMatrix(origin, begin, end, weights, &c);
VectorXD< PointType::Dim, ScalarType > d;
if(!Jacobi(c, &d, &v))
{
//std::cout << "Jacobi failed:" << std::endl;
//std::cout << "origin = " << origin[0] << "," << origin[1] << "," << origin[2] << std::endl
// << "cov:" << std::endl
// << c[0][0] << c[1][0] << c[2][0] << std::endl
// << c[0][1] << c[1][1] << c[2][1] << std::endl
// << c[0][2] << c[1][2] << c[2][2] << std::endl;
//std::cout << "recomp origin:" << std::endl;
//PointT com;
//Mean(begin, end, weights, &com);
//std::cout << "origin = " << origin[0] << "," << origin[1] << "," << origin[2] << std::endl;
//std::cout << "recomp covariance:" << std::endl;
//CovarianceMatrix(com, begin, end, weights, &c);
//std::cout << "cov:" << std::endl
//<< c[0][0] << c[1][0] << c[2][0] << std::endl
//<< c[0][1] << c[1][1] << c[2][1] << std::endl
//<< c[0][2] << c[1][2] << c[2][2] << std::endl;
//std::cout << "weights and points:" << std::endl;
//WeightsForwardIt w = weights;
//for(PointsForwardIt i = begin; i != end; ++i, ++w)
// std::cout << (*i)[0] << "," << (*i)[1] << "," << (*i)[2]
// << " weight=" << (*w) << std::endl;
return false;
}
for(unsigned int i = 0; i < PointType::Dim; ++i)
d[i] = Math< ScalarType >::Abs(d[i]);
EigSortDesc(&d, &v);
_normal = PointType(v[PointType::Dim - 1]);
_d = -(_normal * origin);
return true;
}