Files
o3de/Gems/EMotionFX/Code/MCore/Source/PlaneEq.cpp
T
2021-03-08 14:30:57 -08:00

86 lines
2.4 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// include required headers
#include "PlaneEq.h"
namespace MCore
{
// clip points against the plane
bool PlaneEq::Clip(const AZStd::vector<AZ::Vector3>& pointsIn, AZStd::vector<AZ::Vector3>& pointsOut) const
{
size_t numPoints = pointsIn.size();
MCORE_ASSERT(&pointsIn != &pointsOut);
MCORE_ASSERT(numPoints >= 2);
size_t vert1 = numPoints - 1;
float firstDist = CalcDistanceTo(pointsIn[vert1]);
float nextDist = firstDist;
bool firstIn = (firstDist >= 0.0f);
bool nextIn = firstIn;
pointsOut.clear();
if (numPoints == 2)
{
numPoints = 1;
}
for (int32 vert2 = 0; vert2 < numPoints; vert2++)
{
float dist = nextDist;
bool in = nextIn;
nextDist = CalcDistanceTo(pointsIn[vert2]);
nextIn = (nextDist >= 0.0f);
if (in)
{
pointsOut.emplace_back(pointsIn[vert1]);
}
if ((in != nextIn) && (dist != 0.0f) && (nextDist != 0.0f))
{
AZ::Vector3 dir = (pointsIn[vert2] - pointsIn[vert1]);
float frac = dist / (dist - nextDist);
if ((frac > 0.0f) && (frac < 1.0f))
{
pointsOut.emplace_back(pointsIn[vert1] + frac * dir);
}
}
vert1 = vert2;
}
//if (numPoints == 1)
// return (pointsOut.GetLength() > 1);
return (pointsOut.size() > 1);
}
// clip a set of vectors <points> to this plane
bool PlaneEq::Clip(AZStd::vector<AZ::Vector3>& points) const
{
AZStd::vector<AZ::Vector3> pointsOut;
if (Clip(points, pointsOut))
{
points = pointsOut;
return true;
}
return false;
}
} // namespace MCore