多边形Newell向量
空间平面多边形的Newell
向量可以用来表示一个该多边形所在平面的法向量而且
Newell
向量的模是这个多边形的面积。
Code
- 计算多边形的法向量和中心点。
template<typename T>
void PolygonTriangulation::ComputePolygonPlane(const TArray<TVector<T>>& VertexPositions, TVector<T>& PlaneNormalOut, TVector<T>& PlanePointOut)
{// NOTE: This polygon plane computation code is partially based on the implementation of "Newell's method" from Real-Time// Collision Detection by Christer Ericson, published by Morgan Kaufmann Publishers, (c) 2005 Elsevier IncPlaneNormalOut = TVector<T>::Zero();PlanePointOut = TVector<T>::Zero();// Use 'Newell's Method' to compute a robust 'best fit' plane from the vertices of this polygonconst int32 NumVertices = VertexPositions.Num();for (int32 VertexNumberI = NumVertices - 1, VertexNumberJ = 0; VertexNumberJ < NumVertices; VertexNumberI = VertexNumberJ++){const TVector<T>& PositionI = VertexPositions[VertexNumberI];const TVector<T>& PositionJ = VertexPositions[VertexNumberJ];PlanePointOut += PositionJ;PlaneNormalOut.X += (PositionJ.Y - PositionI.Y) * (PositionI.Z + PositionJ.Z);PlaneNormalOut.Y += (PositionJ.Z - PositionI.Z) * (PositionI.X + PositionJ.X);PlaneNormalOut.Z += (PositionJ.X - PositionI.X) * (PositionI.Y + PositionJ.Y);}Normalize(PlaneNormalOut);PlanePointOut /= (T)NumVertices;
}
- 计算多边形面积
/** * Compute the signed area of a closed curve, assuming vertices in the XY plane*/template<typename RealType, typename VectorType>RealType SignedArea2(const TArrayView<const VectorType>& Vertices){RealType Area = 0;int N = Vertices.Num();if (N == 0){return 0;}for (int Idx = 0, PrevIdx = N-1; Idx < N; PrevIdx=Idx++){const TVector2<RealType>& V1 = Vertices[PrevIdx];const TVector2<RealType>& V2 = Vertices[Idx];Area += V1.X * V2.Y - V1.Y * V2.X;}return static_cast<RealType>(Area * 0.5);}