⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcontainment.cpp

📁 《3D游戏引擎设计》的源码
💻 CPP
字号:
// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// FREE SOURCE CODE
// http://www.magic-software.com/License.html/free.pdf

#include <MgcCore.pkg>
#include <MgcContainment.pkg>
#include <MgcDistance.pkg>

//----------------------------------------------------------------------------
void TestCircumscribeCircle2 ()
{
    MgcVector2 kV0(0.0,0.0);
    MgcVector2 kV1(1.0,0.0);
    MgcVector2 kV2(0.0,1.0);
    MgcCircle2 kCircle;

    bool bResult = MgcCircumscribe(kV0,kV1,kV2,kCircle);
}
//----------------------------------------------------------------------------
void TestCircumscribeCircle3 ()
{
    MgcVector3 kV0(2.0,2.0,0.0);
    MgcVector3 kV1(6.0,1.0,0.0);
    MgcVector3 kV2(6.0,7.0,0.0);
    MgcCircle3 kCircle;

    bool bResult = MgcCircumscribe(kV0,kV1,kV2,kCircle);
}
//----------------------------------------------------------------------------
void TestCircumscribeSphere ()
{
    MgcVector3 kV0(0.0,0.0,0.0);
    MgcVector3 kV1(1.0,0.0,0.0);
    MgcVector3 kV2(0.0,1.0,0.0);
    MgcVector3 kV3(0.0,0.0,1.0);
    MgcSphere kSphere;

    bool bResult = MgcCircumscribe(kV0,kV1,kV2,kV3,kSphere);
}
//----------------------------------------------------------------------------
void TestInscribeCircle2 ()
{
    MgcVector2 kV0(0.0,0.0);
    MgcVector2 kV1(1.0,0.0);
    MgcVector2 kV2(0.0,1.0);
    MgcCircle2 kCircle;

    bool bResult = MgcInscribe(kV0,kV1,kV2,kCircle);
}
//----------------------------------------------------------------------------
void TestInscribeCircle3 ()
{
    MgcVector3 kV0(0.0,0.0,0.0);
    MgcVector3 kV1(1.0,0.0,0.0);
    MgcVector3 kV2(0.0,1.0,0.0);
    MgcCircle3 kCircle;

    bool bResult = MgcInscribe(kV0,kV1,kV2,kCircle);
}
//----------------------------------------------------------------------------
void TestInscribeSphere ()
{
    MgcVector3 kV0(0.0,0.0,0.0);
    MgcVector3 kV1(1.0,0.0,0.0);
    MgcVector3 kV2(0.0,1.0,0.0);
    MgcVector3 kV3(0.0,0.0,1.0);
    MgcSphere kSphere;

    bool bResult = MgcInscribe(kV0,kV1,kV2,kV3,kSphere);
}
//----------------------------------------------------------------------------
void TestMinBox2 ()
{
    const int N = 128;
    MgcVector2 pt[N];

    MgcVector2 kU(1.0,1.0);
    MgcVector2 kV(1.0,-1.0);
    kU.Unitize();
    kV.Unitize();

    for (int i = 0; i < N; i++)
    {
        pt[i] = MgcMath::UnitRandom()*kU + MgcMath::UnitRandom()*kV;
    }

    MgcBox2 kBox = MgcMinBox(N,pt);
}
//----------------------------------------------------------------------------
void TestMinBox3 ()
{
    const MgcReal rt2 = MgcMath::Sqrt(2.0);
    const int N = 8;
    MgcVector3 cube0[N], cube1[N];

    cube0[0].x = -1;  cube0[0].y =    0;  cube0[0].z =  rt2;
    cube0[1].x =  1;  cube0[1].y =    0;  cube0[1].z =  rt2;
    cube0[2].x =  1;  cube0[2].y = -rt2;  cube0[2].z =    0;
    cube0[3].x = -1;  cube0[3].y = -rt2;  cube0[3].z =    0;
    cube0[4].x = -1;  cube0[4].y =  rt2;  cube0[4].z =    0;
    cube0[5].x =  1;  cube0[5].y =  rt2;  cube0[5].z =    0;
    cube0[6].x =  1;  cube0[6].y =    0;  cube0[6].z = -rt2;
    cube0[7].x = -1;  cube0[7].y =    0;  cube0[7].z = -rt2;

    MgcBox3 minimal = MgcMinBox(N,cube0);

    cube1[0].x =    0;  cube1[0].y =  1;  cube1[0].z =  rt2;
    cube1[1].x =  rt2;  cube1[1].y =  1;  cube1[1].z =    0;
    cube1[2].x =  rt2;  cube1[2].y = -1;  cube1[2].z =    0;
    cube1[3].x =    0;  cube1[3].y = -1;  cube1[3].z =  rt2;
    cube1[4].x = -rt2;  cube1[4].y =  1;  cube1[4].z =    0;
    cube1[5].x =    0;  cube1[5].y =  1;  cube1[5].z = -rt2;
    cube1[6].x =    0;  cube1[6].y = -1;  cube1[6].z = -rt2;
    cube1[7].x = -rt2;  cube1[7].y = -1;  cube1[7].z =    0;

    minimal = MgcMinBox(N,cube1);
}
//----------------------------------------------------------------------------
void TestContCapsule ()
{
    const int N = 65536;
    MgcVector3 pt[N];

    MgcVector3 kU, kV, kW(1.0,1.0,1.0);
    MgcVector3::GenerateOrthonormalBasis(kU,kV,kW,false);

    int i;
    for (i = 0; i < N; i++)
    {
        pt[i] =
            0.25*MgcMath::UnitRandom()*kU +
            0.25*MgcMath::UnitRandom()*kV +
            MgcMath::UnitRandom()*kW;
    }

    MgcCapsule kCapsule = MgcContCapsule(N,pt);

    MgcSegment3 kSeg;
    kSeg.Origin() = kCapsule.Origin();
    kSeg.Direction() = kCapsule.Direction();
    for (i = 0; i < N; i++)
    {
        MgcReal fDist = MgcDistance(pt[i],kSeg);
        assert( fDist <= kCapsule.Radius() );
    }
}
//----------------------------------------------------------------------------
void TestContLozenge ()
{
    const int N = 65536;
    MgcVector3 pt[N];

    MgcVector3 kU, kV, kW(1.0,1.0,1.0);
    MgcVector3::GenerateOrthonormalBasis(kU,kV,kW,false);

    int i;
    for (i = 0; i < N; i++)
    {
        pt[i] =
            0.25*MgcMath::UnitRandom()*kU +
            MgcMath::UnitRandom()*kV +
            MgcMath::UnitRandom()*kW;
    }

    MgcLozenge kLozenge = MgcContLozenge(N,pt);

    MgcParallelogram3 kPgm;
    kPgm.Origin() = kLozenge.Origin();
    kPgm.Edge0() = kLozenge.Edge0();
    kPgm.Edge1() = kLozenge.Edge1();
    MgcVector3 kNormal = kPgm.Edge0().Cross(kPgm.Edge1());
    kNormal.Unitize();
    MgcReal fMax = 0.0;
    for (i = 0; i < N; i++)
    {
        MgcReal fDist = MgcDistance(pt[i],kPgm);
        MgcReal fDiff = kLozenge.Radius() - fDist;
        assert( fDist <= kLozenge.Radius() + 1e-06 );
        if ( fDist > fMax )
            fMax = fDist;
    }
}
//----------------------------------------------------------------------------
void TestContCylinder ()
{
    const int N = 65536;
    MgcVector3 pt[N];

    MgcVector3 kU, kV, kW(1.0,1.0,1.0);
    MgcVector3::GenerateOrthonormalBasis(kU,kV,kW,false);

    int i;
    for (i = 0; i < N; i++)
    {
        pt[i] =
            0.25*MgcMath::UnitRandom()*kU +
            0.25*MgcMath::UnitRandom()*kV +
            MgcMath::UnitRandom()*kW;
    }

    MgcCylinder kCylinder = MgcContCylinder(N,pt);
}
//----------------------------------------------------------------------------
void TestBoxTree ()
{
    const unsigned short usVertexCount = 4;
    MgcVector3 akVertex[usVertexCount] =
    {
        MgcVector3::ZERO,
        MgcVector3::UNIT_X,
        MgcVector3::UNIT_Y,
        MgcVector3::UNIT_Z
    };

    const unsigned short usTriangleCount = 4;
    unsigned short ausConnect[3*usTriangleCount] =
    {
        0, 2, 1,
        0, 3, 1,
        0, 3, 2,
        1, 3, 2
    };

    unsigned short usMaxTrisPerLeaf = 1;

    MgcBoxTree* pkTree = new MgcBoxTree(usVertexCount,akVertex,
        usTriangleCount,ausConnect,usMaxTrisPerLeaf);

    const MgcBox3& kBox = pkTree->GetBox();

    delete pkTree;
}
//----------------------------------------------------------------------------
void TestCapsuleTree ()
{
    const unsigned short usVertexCount = 4;
    MgcVector3 akVertex[usVertexCount] =
    {
        MgcVector3::ZERO,
        MgcVector3::UNIT_X,
        MgcVector3::UNIT_Y,
        MgcVector3::UNIT_Z
    };

    const unsigned short usTriangleCount = 4;
    unsigned short ausConnect[3*usTriangleCount] =
    {
        0, 2, 1,
        0, 3, 1,
        0, 3, 2,
        1, 3, 2
    };

    unsigned short usMaxTrisPerLeaf = 1;

    MgcCapsuleTree* pkTree = new MgcCapsuleTree(usVertexCount,akVertex,
        usTriangleCount,ausConnect,usMaxTrisPerLeaf);

    const MgcCapsule& kCapsule = pkTree->GetCapsule();

    delete pkTree;
}
//----------------------------------------------------------------------------
void TestLozengeTree ()
{
    const unsigned short usVertexCount = 4;
    MgcVector3 akVertex[usVertexCount] =
    {
        MgcVector3::ZERO,
        MgcVector3::UNIT_X,
        MgcVector3::UNIT_Y,
        MgcVector3::UNIT_Z
    };

    const unsigned short usTriangleCount = 4;
    unsigned short ausConnect[3*usTriangleCount] =
    {
        0, 2, 1,
        0, 3, 1,
        0, 3, 2,
        1, 3, 2
    };

    unsigned short usMaxTrisPerLeaf = 1;

    MgcLozengeTree* pkTree = new MgcLozengeTree(usVertexCount,akVertex,
        usTriangleCount,ausConnect,usMaxTrisPerLeaf);

    const MgcLozenge& kLozenge = pkTree->GetLozenge();

    delete pkTree;
}
//----------------------------------------------------------------------------
void TestSphereTree ()
{
    const unsigned short usVertexCount = 4;
    MgcVector3 akVertex[usVertexCount] =
    {
        MgcVector3::ZERO,
        MgcVector3::UNIT_X,
        MgcVector3::UNIT_Y,
        MgcVector3::UNIT_Z
    };

    const unsigned short usTriangleCount = 4;
    unsigned short ausConnect[3*usTriangleCount] =
    {
        0, 2, 1,
        0, 3, 1,
        0, 3, 2,
        1, 3, 2
    };

    unsigned short usMaxTrisPerLeaf = 1;

    MgcSphereTree* pkTree = new MgcSphereTree(usVertexCount,akVertex,
        usTriangleCount,ausConnect,usMaxTrisPerLeaf);

    const MgcSphere& kSphere = pkTree->GetSphere();

    delete pkTree;
}
//----------------------------------------------------------------------------
void TestMergeSpheres ()
{
    MgcSphere kSphere0, kSphere1, kSphere;

    kSphere0.Center() = MgcVector3::UNIT_Z;
    kSphere0.Radius() = 1.0;
    kSphere1.Center() = 0.5*MgcVector3::UNIT_Z;
    kSphere1.Radius() = 0.25;

    kSphere = MgcMergeSpheres(kSphere0,kSphere1);
    kSphere = MgcMergeSpheres(kSphere1,kSphere0);

    kSphere1.Radius() = 1.0;
    kSphere = MgcMergeSpheres(kSphere0,kSphere1);
}
//----------------------------------------------------------------------------
void TestMergeBoxes ()
{
    MgcBox3 kBox0, kBox1, kBox;

    kBox0.Center() = MgcVector3::ZERO;
    kBox0.Axis(0) = MgcVector3::UNIT_X;
    kBox0.Axis(1) = MgcVector3::UNIT_Y;
    kBox0.Axis(2) = MgcVector3::UNIT_Z;
    kBox0.Extent(0) = 1.0;
    kBox0.Extent(1) = 1.0;
    kBox0.Extent(2) = 1.0;

    kBox1.Center() = MgcVector3::UNIT_X;
    MgcReal fRootHalf = MgcMath::Sqrt(0.5);
    kBox1.Axis(0) = MgcVector3(fRootHalf,fRootHalf,0.0);
    kBox1.Axis(1) = MgcVector3(-fRootHalf,fRootHalf,0.0);
    kBox1.Axis(2) = MgcVector3::UNIT_Z;
    kBox1.Extent(0) = 0.5;
    kBox1.Extent(1) = 0.5;
    kBox1.Extent(2) = 0.5;

    kBox = MgcMergeBoxes(kBox0,kBox1);
}
//----------------------------------------------------------------------------
int main ()
{
    //TestCircumscribeCircle2();
    //TestCircumscribeCircle3();
    //TestCircumscribeSphere();
    //TestInscribeCircle2();
    //TestInscribeCircle3();
    //TestInscribeSphere();
    //TestMinBox2();
    //TestMinBox3();
    //TestContCapsule();
    //TestContLozenge();
    //TestContCylinder();
    //TestBoxTree();
    //TestCapsuleTree();
    //TestLozengeTree();
    //TestSphereTree();
    //TestMergeSpheres();
    TestMergeBoxes();

    return 0;
}
//----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -