当前位置: 首页>>代码示例>>C++>>正文


C++ Segment3类代码示例

本文整理汇总了C++中Segment3的典型用法代码示例。如果您正苦于以下问题:C++ Segment3类的具体用法?C++ Segment3怎么用?C++ Segment3使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Segment3类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: SqrDistance

Real Wml::SqrDistance (const Vector3<Real>& rkPoint,
    const Segment3<Real>& rkSegment, Real* pfParam)
{
    Vector3<Real> kDiff = rkPoint - rkSegment.Origin();
    Real fT = kDiff.Dot(rkSegment.Direction());

    if ( fT <= (Real)0.0 )
    {
        fT = (Real)0.0;
    }
    else
    {
        Real fSqrLen= rkSegment.Direction().SquaredLength();
        if ( fT >= fSqrLen )
        {
            fT = (Real)1.0;
            kDiff -= rkSegment.Direction();
        }
        else
        {
            fT /= fSqrLen;
            kDiff -= fT*rkSegment.Direction();
        }
    }

    if ( pfParam )
        *pfParam = fT;

    return kDiff.SquaredLength();
}
开发者ID:Steven0818,项目名称:Deform,代码行数:30,代码来源:WmlDistVec3Lin3.cpp

示例2:

Segment3<Real> Cylinder3<Real>::GetSegment () const
{
    Segment3<Real> kSegment;
    kSegment.Direction() = m_fHeight*m_kDirection;
    kSegment.Origin() = m_kCenter - ((Real)0.5)*kSegment.Direction();
    return kSegment;
}
开发者ID:Hengplank,项目名称:kucgbowling,代码行数:7,代码来源:WmlCylinder3.cpp

示例3: SqrDistance

Real Mgc::SqrDistance (const Vector3& rkPoint, const Segment3& rkSegment,

    Real* pfParam)

{

    Vector3 kDiff = rkPoint - rkSegment.Origin();

    Real fT = kDiff.Dot(rkSegment.Direction());



    if ( fT <= 0.0f )

    {

        fT = 0.0f;

    }

    else

    {

        Real fSqrLen= rkSegment.Direction().SquaredLength();

        if ( fT >= fSqrLen )

        {

            fT = 1.0f;

            kDiff -= rkSegment.Direction();

        }

        else

        {

            fT /= fSqrLen;

            kDiff -= fT*rkSegment.Direction();

        }

    }



    if ( pfParam )

        *pfParam = fT;



    return kDiff.SquaredLength();

}
开发者ID:jason-amju,项目名称:amjulib,代码行数:59,代码来源:MgcDist3DVecLin.cpp

示例4: querySR

Real DistTriangle3Rectangle3<Real>::GetSquared ()
{
    // Compare edges of triangle to the interior of rectangle.
    Real sqrDist = Math<Real>::MAX_REAL, sqrDistTmp;
    Segment3<Real> edge;
    int i0, i1;
    for (i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
    {
        edge.Center = ((Real)0.5)*(mTriangle->V[i0] +
            mTriangle->V[i1]);
        edge.Direction = mTriangle->V[i1] - mTriangle->V[i0];
        edge.Extent = ((Real)0.5)*edge.Direction.Normalize();
        edge.ComputeEndPoints();

        DistSegment3Rectangle3<Real> querySR(edge, *mRectangle);
        sqrDistTmp = querySR.GetSquared();
        if (sqrDistTmp < sqrDist)
        {
            // The triangle point is reported in mClosestPoint0 and the
            // rectangle point is reported in mClosestPoint1.  The querySR
            // calculator is for triangleEdge-rectangle, so GetClosestPoint0()
            // and GetClosestPoint1() must be called as listed next.
            mClosestPoint0 = querySR.GetClosestPoint0();
            mClosestPoint1 = querySR.GetClosestPoint1();
            sqrDist = sqrDistTmp;
        }
    }

    // Compare edges of rectangle to the interior of triangle.
    for (i1 = 0; i1 < 2; ++i1)
    {
        for (i0 = -1; i0 <= 1; i0 += 2)
        {
            edge.Center = mRectangle->Center +
                (i0*mRectangle->Extent[1-i1]) *
                mRectangle->Axis[1-i1];
            edge.Direction = mRectangle->Axis[i1];
            edge.Extent = mRectangle->Extent[i1];
            edge.ComputeEndPoints();

            DistSegment3Triangle3<Real> queryST(edge, *mTriangle);
            sqrDistTmp = queryST.GetSquared();
            if (sqrDistTmp < sqrDist)
            {
                // The triangle point is reported in mClosestPoint0 and the
                // rectangle point is reported in mClosestPoint1.  The queryST
                // calculator is for rectangleEdge-triangle, so
                // GetClosestPoint1() and GetClosestPoint0() must be called as
                // listed next.
                mClosestPoint0 = queryST.GetClosestPoint1();
                mClosestPoint1 = queryST.GetClosestPoint0();
                sqrDist = sqrDistTmp;
            }
        }
    }

    return sqrDist;
}
开发者ID:2asoft,项目名称:GeometricTools,代码行数:58,代码来源:Wm5DistTriangle3Rectangle3.cpp

示例5: SqrDistance

//----------------------------------------------------------------------------
Real Mgc::SqrDistance (const Segment3& rkSeg, const Box3& rkBox,
    Real* pfLParam, Real* pfBParam0, Real* pfBParam1, Real* pfBParam2)
{
#ifdef _DEBUG
    // The four parameters pointers are either all non-null or all null.
    if ( pfLParam )
    {
        assert( pfBParam0 && pfBParam1 && pfBParam2 );
    }
    else
    {
        assert( !pfBParam0 && !pfBParam1 && !pfBParam2 );
    }
#endif

    Line3 kLine;
    kLine.Origin() = rkSeg.Origin();
    kLine.Direction() = rkSeg.Direction();

    Real fLP, fBP0, fBP1, fBP2;
    Real fSqrDistance = SqrDistance(kLine,rkBox,&fLP,&fBP0,&fBP1,&fBP2);
    if ( fLP >= 0.0f )
    {
        if ( fLP <= 1.0f )
        {
            if ( pfLParam )
            {
                *pfLParam = fLP;
                *pfBParam0 = fBP0;
                *pfBParam1 = fBP1;
                *pfBParam2 = fBP2;
            }

            return fSqrDistance;
        }
        else
        {
            fSqrDistance = SqrDistance(rkSeg.Origin()+rkSeg.Direction(),
                rkBox,pfBParam0,pfBParam1,pfBParam2);

            if ( pfLParam )
                *pfLParam = 1.0f;

            return fSqrDistance;
        }
    }
    else
    {
        fSqrDistance = SqrDistance(rkSeg.Origin(),rkBox,pfBParam0,
            pfBParam1,pfBParam2);

        if ( pfLParam )
            *pfLParam = 0.0f;

        return fSqrDistance;
    }
}
开发者ID:BackupTheBerlios,项目名称:lwpp-svn,代码行数:58,代码来源:MgcDist3DLinBox.cpp

示例6: GetWeight

//----------------------------------------------------------------------------
float VertexCollapse::GetWeight (int iM, int iZ, int iP, Vector3* akVertex)
{
    Segment3 kSegment;
    kSegment.Origin() = akVertex[iM];
    kSegment.Direction() = akVertex[iP] - akVertex[iM];
    float fSqrDist = SqrDistance(akVertex[iZ],kSegment);
    float fSqrLen = kSegment.Direction().SquaredLength();

    return ( fSqrLen > 0.0f ? fSqrDist/fSqrLen : FLT_MAX );
}
开发者ID:OpenXRay,项目名称:xray,代码行数:11,代码来源:VertexCollapse.cpp

示例7: FindIntersection

//----------------------------------------------------------------------------
bool Mgc::FindIntersection (const Segment3& rkSegment,
    const Triangle3& rkTriangle, Vector3& rkPoint)
{
    Real fSegP;
    if ( SqrDistance(rkSegment,rkTriangle,&fSegP) <= gs_fEpsilon )
    {
        rkPoint = rkSegment.Origin() + fSegP*rkSegment.Direction();
        return true;
    }
    return false;
}
开发者ID:BackupTheBerlios,项目名称:lwpp-svn,代码行数:12,代码来源:MgcIntr3DLinTri.cpp

示例8: querySR

Real DistRectangle3Rectangle3<Real>::GetSquared ()
{
    // Compare edges of rectangle0 to the interior of rectangle1.
    Real sqrDist = Math<Real>::MAX_REAL, sqrDistTmp;
    Segment3<Real> edge;
    int i0, i1;
    for (i1 = 0; i1 < 2; ++i1)
    {
        for (i0 = -1; i0 <= 1; i0 += 2)
        {
            edge.Center = mRectangle0->Center +
                (i0*mRectangle0->Extent[1-i1]) *
                mRectangle0->Axis[1-i1];
            edge.Direction = mRectangle0->Axis[i1];
            edge.Extent = mRectangle0->Extent[i1];
            edge.ComputeEndPoints();

            DistSegment3Rectangle3<Real> querySR(edge, *mRectangle1);
            sqrDistTmp = querySR.GetSquared();
            if (sqrDistTmp < sqrDist)
            {
                mClosestPoint0 = querySR.GetClosestPoint0();
                mClosestPoint1 = querySR.GetClosestPoint1();
                sqrDist = sqrDistTmp;
            }
        }
    }

    // Compare edges of rectangle1 to the interior of rectangle0.
    for (i1 = 0; i1 < 2; ++i1)
    {
        for (i0 = -1; i0 <= 1; i0 += 2)
        {
            edge.Center = mRectangle1->Center +
                (i0*mRectangle1->Extent[1-i1]) *
                mRectangle1->Axis[1-i1];
            edge.Direction = mRectangle1->Axis[i1];
            edge.Extent = mRectangle1->Extent[i1];
            edge.ComputeEndPoints();

            DistSegment3Rectangle3<Real> querySR(edge, *mRectangle0);
            sqrDistTmp = querySR.GetSquared();
            if (sqrDistTmp < sqrDist)
            {
                mClosestPoint0 = querySR.GetClosestPoint0();
                mClosestPoint1 = querySR.GetClosestPoint1();
                sqrDist = sqrDistTmp;
            }
        }
    }

    return sqrDist;
}
开发者ID:JackTing,项目名称:SkpColl,代码行数:53,代码来源:Wm5DistRectangle3Rectangle3.cpp

示例9: FindIntersection

//----------------------------------------------------------------------------
bool Mgc::FindIntersection (const Segment3& rkSegment, const Box3& rkBox,
    int& riQuantity, Vector3 akPoint[2])
{
    // convert segment to box coordinates
    Vector3 kDiff = rkSegment.Origin() - rkBox.Center();
    Vector3 kOrigin(
        kDiff.Dot(rkBox.Axis(0)),
        kDiff.Dot(rkBox.Axis(1)),
        kDiff.Dot(rkBox.Axis(2))
    );
    Vector3 kDirection(
        rkSegment.Direction().Dot(rkBox.Axis(0)),
        rkSegment.Direction().Dot(rkBox.Axis(1)),
        rkSegment.Direction().Dot(rkBox.Axis(2))
    );

    Real fT0 = 0.0f, fT1 = 1.0f;
    bool bIntersects = FindIntersection(kOrigin,kDirection,rkBox.Extents(),
        fT0,fT1);

    if ( bIntersects )
    {
        if ( fT0 > 0.0f )
        {
            if ( fT1 < 1.0f )
            {
                riQuantity = 2;
                akPoint[0] = rkSegment.Origin() + fT0*rkSegment.Direction();
                akPoint[1] = rkSegment.Origin() + fT1*rkSegment.Direction();
            }
            else
            {
                riQuantity = 1;
                akPoint[0] = rkSegment.Origin() + fT0*rkSegment.Direction();
            }
        }
        else  // fT0 == 0
        {
            if ( fT1 < 1.0f )
            {
                riQuantity = 1;
                akPoint[0] = rkSegment.Origin() + fT1*rkSegment.Direction();
            }
            else  // fT1 == 1
            {
                // segment entirely in box
                riQuantity = 0;
            }
        }
    }
    else
    {
        riQuantity = 0;
    }

    return bIntersects;
}
开发者ID:BackupTheBerlios,项目名称:lwpp-svn,代码行数:58,代码来源:MgcIntr3DLinBox.cpp

示例10: TestIntersection

//----------------------------------------------------------------------------
bool Mgc::TestIntersection (const Segment3& rkSegment, const Box3& rkBox)
{
    Real fAWdU[3], fADdU[3], fAWxDdU[3], fRhs;
    Vector3 kSDir = 0.5f*rkSegment.Direction();
    Vector3 kSCen = rkSegment.Origin() + kSDir;

    Vector3 kDiff = kSCen - rkBox.Center();

    fAWdU[0] = Math::FAbs(kSDir.Dot(rkBox.Axis(0)));
    fADdU[0] = Math::FAbs(kDiff.Dot(rkBox.Axis(0)));
    fRhs = rkBox.Extent(0) + fAWdU[0];
    if ( fADdU[0] > fRhs )
        return false;

    fAWdU[1] = Math::FAbs(kSDir.Dot(rkBox.Axis(1)));
    fADdU[1] = Math::FAbs(kDiff.Dot(rkBox.Axis(1)));
    fRhs = rkBox.Extent(1) + fAWdU[1];
    if ( fADdU[1] > fRhs )
        return false;

    fAWdU[2] = Math::FAbs(kSDir.Dot(rkBox.Axis(2)));
    fADdU[2] = Math::FAbs(kDiff.Dot(rkBox.Axis(2)));
    fRhs = rkBox.Extent(2) + fAWdU[2];
    if ( fADdU[2] > fRhs )
        return false;

    Vector3 kWxD = kSDir.Cross(kDiff);

    fAWxDdU[0] = Math::FAbs(kWxD.Dot(rkBox.Axis(0)));
    fRhs = rkBox.Extent(1)*fAWdU[2] + rkBox.Extent(2)*fAWdU[1];
    if ( fAWxDdU[0] > fRhs )
        return false;

    fAWxDdU[1] = Math::FAbs(kWxD.Dot(rkBox.Axis(1)));
    fRhs = rkBox.Extent(0)*fAWdU[2] + rkBox.Extent(2)*fAWdU[0];
    if ( fAWxDdU[1] > fRhs )
        return false;

    fAWxDdU[2] = Math::FAbs(kWxD.Dot(rkBox.Axis(2)));
    fRhs = rkBox.Extent(0)*fAWdU[1] + rkBox.Extent(1)*fAWdU[0];
    if ( fAWxDdU[2] > fRhs )
        return false;

    return true;
}
开发者ID:BackupTheBerlios,项目名称:lwpp-svn,代码行数:46,代码来源:MgcIntr3DLinBox.cpp

示例11: FindIntersection

bool Wml::FindIntersection (const Segment3<Real>& rkSegment,
    const Cylinder3<Real>& rkCylinder, int& riQuantity,
    Vector3<Real> akPoint[2])
{
    Real afT[2];

    if ( rkCylinder.Capped() )
    {
        riQuantity = Find(rkSegment.Origin(),rkSegment.Direction(),
            rkCylinder,afT);
    }
    else
    {
        riQuantity = FindHollow(rkSegment.Origin(),rkSegment.Direction(),
            rkCylinder,afT);
    }

    int iClipQuantity = 0;
    for (int i = 0; i < riQuantity; i++)
    {
        if ( (Real)0.0 <= afT[i] && afT[i] <= (Real)1.0 )
        {
            akPoint[iClipQuantity++] = rkSegment.Origin() +
                afT[i]*rkSegment.Direction();
        }
    }

    riQuantity = iClipQuantity;
    return riQuantity > 0;
}
开发者ID:Steven0818,项目名称:Deform,代码行数:30,代码来源:WmlIntrLin3Cyl3.cpp

示例12: queryST

Real DistTriangle3Triangle3<Real>::GetSquared ()
{
	// Compare edges of triangle0 to the interior of triangle1.
	Real sqrDist = Math<Real>::MAX_REAL, sqrDistTmp;
	Segment3<Real> edge;
	Real ratio;
	int i0, i1;
	for (i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
	{
		edge.Center = ((Real)0.5)*(mTriangle0->V[i0] +
		                           mTriangle0->V[i1]);
		edge.Direction = mTriangle0->V[i1] - mTriangle0->V[i0];
		edge.Extent = ((Real)0.5)*edge.Direction.Normalize();
		edge.ComputeEndPoints();

		DistSegment3Triangle3<Real> queryST(edge, *mTriangle1);
		sqrDistTmp = queryST.GetSquared();
		if (sqrDistTmp < sqrDist)
		{
			mClosestPoint0 = queryST.GetClosestPoint0();
			mClosestPoint1 = queryST.GetClosestPoint1();
			sqrDist = sqrDistTmp;

			ratio = queryST.GetSegmentParameter()/edge.Extent;
			mTriangleBary0[i0] = ((Real)0.5)*((Real)1 - ratio);
			mTriangleBary0[i1] = (Real)1 - mTriangleBary0[i0];
			mTriangleBary0[3-i0-i1] = (Real)0;
			mTriangleBary1[0] = queryST.GetTriangleBary(0);
			mTriangleBary1[1] = queryST.GetTriangleBary(1);
			mTriangleBary1[2] = queryST.GetTriangleBary(2);

			if (sqrDist <= Math<Real>::ZERO_TOLERANCE)
			{
				return (Real)0;
			}
		}
	}

	// Compare edges of triangle1 to the interior of triangle0.
	for (i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
	{
		edge.Center = ((Real)0.5)*(mTriangle1->V[i0] +
		                           mTriangle1->V[i1]);
		edge.Direction = mTriangle1->V[i1] - mTriangle1->V[i0];
		edge.Extent = ((Real)0.5)*edge.Direction.Normalize();
		edge.ComputeEndPoints();

		DistSegment3Triangle3<Real> queryST(edge, *mTriangle0);
		sqrDistTmp = queryST.GetSquared();
		if (sqrDistTmp < sqrDist)
		{
			mClosestPoint0 = queryST.GetClosestPoint0();
			mClosestPoint1 = queryST.GetClosestPoint1();
			sqrDist = sqrDistTmp;

			ratio = queryST.GetSegmentParameter()/edge.Extent;
			mTriangleBary1[i0] = ((Real)0.5)*((Real)1 - ratio);
			mTriangleBary1[i1] = (Real)1 - mTriangleBary1[i0];
			mTriangleBary1[3-i0-i1] = (Real)0;
			mTriangleBary0[0] = queryST.GetTriangleBary(0);
			mTriangleBary0[1] = queryST.GetTriangleBary(1);
			mTriangleBary0[2] = queryST.GetTriangleBary(2);

			if (sqrDist <= Math<Real>::ZERO_TOLERANCE)
			{
				return (Real)0;
			}
		}
	}

	return sqrDist;
}
开发者ID:bhlzlx,项目名称:WildMagic,代码行数:72,代码来源:Wm5DistTriangle3Triangle3.cpp

示例13: FindIntersection

bool Wml::FindIntersection (const Triangle3<Real>& rkTri,
    const Vector3<Real>& rkTriVelocity, const Sphere3<Real>& rkSphere,
    const Vector3<Real>& rkSphVelocity, Real& rfTFirst, Real fTMax,
    int& riQuantity, Vector3<Real> akP[6])
{
    // triangle vertices
    Vector3<Real> akV[3] =
    {
        rkTri.Origin(),
        rkTri.Origin() + rkTri.Edge0(),
        rkTri.Origin() + rkTri.Edge1()
    };

    // triangle edges
    Vector3<Real> akE[3] =
    {
        akV[1] - akV[0],
        akV[2] - akV[1], 
        akV[0] - akV[2]
    };

    // triangle normal
    Vector3<Real> kN = akE[1].Cross(akE[0]);

    // sphere center projection on triangle normal
    Real fNdC = kN.Dot(rkSphere.Center());

    // Radius projected length in normal direction.  This defers the square
    // root to normalize kN until absolutely needed.
    Real fNormRadiusSqr =
        kN.SquaredLength()*rkSphere.Radius()*rkSphere.Radius();

    // triangle projection on triangle normal
    Real fNdT = kN.Dot(akV[0]);

    // Distance from sphere to triangle along the normal
    Real fDist = fNdC - fNdT;
    
    // normals for the plane formed by edge i and the triangle normal
    Vector3<Real> akExN[3] =
    {
        akE[0].Cross(kN),
        akE[1].Cross(kN),
        akE[2].Cross(kN)
    };

    Segment3<Real> kSeg;

    if ( fDist*fDist <= fNormRadiusSqr )
    {
        // sphere currently intersects the plane of the triangle

        // see which edges the sphere center is inside/outside of
        bool bInside[3];
        for (int i = 0; i < 3; i++ )
        {
            bInside[i] = ( akExN[i].Dot(rkSphere.Center()) >=
                akExN[i].Dot(akV[i]) );
        }

        if ( bInside[0] )
        {
            if ( bInside[1] )
            {
                if ( bInside[2] )
                {
                    // triangle inside sphere
                    return false;
                }
                else // !bInside[2]
                {
                    // potential intersection with edge 2
                    kSeg.Origin() = akV[2];
                    kSeg.Direction() = akE[2];
                    return FindIntersection(kSeg,rkTriVelocity,rkSphere,
                        rkSphVelocity,rfTFirst,fTMax,riQuantity,akP);
                }
            }
            else // !bInside[1]
            {
                if ( bInside[2] )
                {
                    // potential intersection with edge 1
                    kSeg.Origin() = akV[1];
                    kSeg.Direction() = akE[1];
                    return FindIntersection(kSeg,rkTriVelocity,rkSphere,
                        rkSphVelocity,rfTFirst,fTMax,riQuantity,akP);
                }
                else // !bInside[2]
                {
                    // potential intersection with edges 1,2
                    return FindTriSphrCoplanarIntersection(2,akV,kN,akExN[2],
                        akE[2],rkSphere,rkTriVelocity,rkSphVelocity,rfTFirst,
                        fTMax,riQuantity,akP);
                }            
            }
        } 
        else // !bInside[0]
        {
            if ( bInside[1] )
//.........这里部分代码省略.........
开发者ID:Steven0818,项目名称:Deform,代码行数:101,代码来源:WmlIntrTri3Sph3.cpp

示例14: FindIntersection

bool Mgc::FindIntersection (const Segment3& rkSegment, const Sphere& rkSphere,

    int& riQuantity, Vector3 akPoint[2])

{

    // set up quadratic Q(t) = a*t^2 + 2*b*t + c

    Vector3 kDiff = rkSegment.Origin() - rkSphere.Center();

    Real fA = rkSegment.Direction().SquaredLength();

    Real fB = kDiff.Dot(rkSegment.Direction());

    Real fC = kDiff.SquaredLength() -

        rkSphere.Radius()*rkSphere.Radius();



    // no intersection if Q(t) has no real roots

    Real afT[2];

    Real fDiscr = fB*fB - fA*fC;

    if ( fDiscr < 0.0f )

    {

        riQuantity = 0;

        return false;

    }

    else if ( fDiscr > 0.0f )

    {

        Real fRoot = Math::Sqrt(fDiscr);

        Real fInvA = 1.0f/fA;

        afT[0] = (-fB - fRoot)*fInvA;

        afT[1] = (-fB + fRoot)*fInvA;



        // assert: t0 < t1 since A > 0



        if ( afT[0] > 1.0f || afT[1] < 0.0f )

        {

            riQuantity = 0;

            return false;

        }

        else if ( afT[0] >= 0.0f )

        {

            if ( afT[1] > 1.0f )

            {

                riQuantity = 1;

                akPoint[0] = rkSegment.Origin()+afT[0]*rkSegment.Direction();

                return true;

            }

            else

            {

                riQuantity = 2;

                akPoint[0] = rkSegment.Origin()+afT[0]*rkSegment.Direction();

                akPoint[1] = rkSegment.Origin()+afT[1]*rkSegment.Direction();

                return true;

            }

        }

        else  // afT[1] >= 0

        {

//.........这里部分代码省略.........
开发者ID:jason-amju,项目名称:amjulib,代码行数:101,代码来源:MgcIntr3DLinSphr.cpp

示例15: return

Real DistLine3Rectangle3<Real>::GetSquared ()
{
    // Test if line intersects rectangle.  If so, the squared distance is
    // zero.
    Vector3<Real> N = mRectangle->Axis[0].Cross( mRectangle->Axis[1] );
    Real NdD = N.Dot( mLine->Direction );
    if ( Math<Real>::FAbs( NdD ) > Math<Real>::ZERO_TOLERANCE )
    {
        // The line and rectangle are not parallel, so the line intersects
        // the plane of the rectangle.
        Vector3<Real> diff = mLine->Origin - mRectangle->Center;
        Vector3<Real> U, V;
        Vector3<Real>::GenerateComplementBasis( U, V, mLine->Direction );
        Real UdD0 = U.Dot( mRectangle->Axis[0] );
        Real UdD1 = U.Dot( mRectangle->Axis[1] );
        Real UdPmC = U.Dot( diff );
        Real VdD0 = V.Dot( mRectangle->Axis[0] );
        Real VdD1 = V.Dot( mRectangle->Axis[1] );
        Real VdPmC = V.Dot( diff );
        Real invDet = ( ( Real )1 ) / ( UdD0 * VdD1 - UdD1 * VdD0 );

        // Rectangle coordinates for the point of intersection.
        Real s0 = ( VdD1 * UdPmC - UdD1 * VdPmC ) * invDet;
        Real s1 = ( UdD0 * VdPmC - VdD0 * UdPmC ) * invDet;

        if ( Math<Real>::FAbs( s0 ) <= mRectangle->Extent[0]
                &&  Math<Real>::FAbs( s1 ) <= mRectangle->Extent[1] )
        {
            // Line parameter for the point of intersection.
            Real DdD0 = mLine->Direction.Dot( mRectangle->Axis[0] );
            Real DdD1 = mLine->Direction.Dot( mRectangle->Axis[1] );
            Real DdDiff = mLine->Direction.Dot( diff );
            mLineParameter = s0 * DdD0 + s1 * DdD1 - DdDiff;

            // Rectangle coordinates for the point of intersection.
            mRectCoord[0] = s0;
            mRectCoord[1] = s1;

            // The intersection point is inside or on the rectangle.
            mClosestPoint0 = mLine->Origin +
                             mLineParameter * mLine->Direction;

            mClosestPoint1 = mRectangle->Center +
                             s0 * mRectangle->Axis[0] + s1 * mRectangle->Axis[1];

            return ( Real )0;
        }
    }

    // Either (1) the line is not parallel to the rectangle and the point of
    // intersection of the line and the plane of the rectangle is outside the
    // rectangle or (2) the line and rectangle are parallel.  Regardless, the
    // closest point on the rectangle is on an edge of the rectangle.  Compare
    // the line to all four edges of the rectangle.
    Real sqrDist = Math<Real>::MAX_REAL;
    Vector3<Real> scaledDir[2] =
    {
        mRectangle->Extent[0]* mRectangle->Axis[0],
        mRectangle->Extent[1]* mRectangle->Axis[1]
    };
    for ( int i1 = 0; i1 < 2; ++i1 )
    {
        for ( int i0 = 0; i0 < 2; ++i0 )
        {
            Segment3<Real> segment;
            segment.Center = mRectangle->Center +
                             ( ( Real )( 2 * i0 - 1 ) ) * scaledDir[i1];
            segment.Direction = mRectangle->Axis[1 - i1];
            segment.Extent = mRectangle->Extent[1 - i1];
            segment.ComputeEndPoints();

            DistLine3Segment3<Real> queryLS( *mLine, segment );
            Real sqrDistTmp = queryLS.GetSquared();
            if ( sqrDistTmp < sqrDist )
            {
                mClosestPoint0 = queryLS.GetClosestPoint0();
                mClosestPoint1 = queryLS.GetClosestPoint1();
                sqrDist = sqrDistTmp;

                mLineParameter = queryLS.GetLineParameter();
                Real ratio = queryLS.GetSegmentParameter() / segment.Extent;
                mRectCoord[0] = mRectangle->Extent[0] * ( ( 1 - i1 ) * ( 2 * i0 - 1 ) +
                                i1 * ratio );
                mRectCoord[1] = mRectangle->Extent[1] * ( ( 1 - i0 ) * ( 2 * i1 - 1 ) +
                                i0 * ratio );
            }
        }
    }

    return sqrDist;
}
开发者ID:kanbang,项目名称:myexercise,代码行数:91,代码来源:Wm5DistLine3Rectangle3.cpp


注:本文中的Segment3类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。