c++实现2维多边形均值重心坐标(代码可能有冗余,未优化代码!):
关于3维中均值重心坐标的实现可参考我的另一篇博客:均值重心坐标Mean Value Coordinates In 3D代码实现
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
|
VectorXd MyPolygon::meanValueCoordinates(Vector2d &p, bool &postiveity) { postiveity = true; int numOfV = this->getNumberOfVertices(); VectorXd mvc_coor = VectorXd::Zero(numOfV); VectorXd tanalpha = VectorXd::Zero(numOfV); MatrixXd temp = MatrixXd::Zero(2, numOfV); MatrixXd unit_v = MatrixXd::Zero(2, numOfV); for (int i = 0; i < numOfV; ++i) { temp.col(i) = this->vertices.col(i) - p; unit_v.col(i) = temp.col(i).normalized(); } for (int i = 0; i < numOfV; ++i) { tanalpha[i] = (1 - unit_v(0, (i + 1) % numOfV)*unit_v(0, i) - unit_v(1, (i + 1) % numOfV)*unit_v(1, i)) / (unit_v(0, (i + 1) % numOfV)*unit_v(1, i) - unit_v(0, i)*unit_v(1, (i + 1) % numOfV)); } for (int i = 0; i < numOfV; ++i) { mvc_coor[i] = (tanalpha[i] + tanalpha[(i - 1 + numOfV) % numOfV]) / temp.col(i).norm(); } mvc_coor /= mvc_coor.sum(); postiveity = mvc_coor.minCoeff() >= 0; cout << mvc_coor << endl; return mvc_coor; }
|
如有任何疑问或建议欢迎下方留言 :-)