2 CDTestFramework http://codercorner.com
3 Copyright (c) 2007-2008 Pierre Terdiman, pierre@codercorner.com
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
17 #include "BipartiteBoxPruning.h"
18 #include "RenderingHelpers.h"
19 #include "GLFontRenderer.h"
21 BipartiteBoxPruningTest::BipartiteBoxPruningTest() :
32 BipartiteBoxPruningTest::~BipartiteBoxPruningTest()
34 DELETEARRAY(mBoxTime);
35 DELETEARRAY(mBoxPtrs);
39 void BipartiteBoxPruningTest::Init()
42 mBoxes = new AABB[mNbBoxes];
43 mBoxPtrs = new const AABB*[mNbBoxes];
44 mBoxTime = new float[mNbBoxes];
45 for(udword i=0;i<mNbBoxes;i++)
47 Point Center, Extents;
49 Center.x = (UnitRandomFloat()-0.5f) * 100.0f;
50 Center.y = (UnitRandomFloat()-0.5f) * 10.0f;
51 Center.z = (UnitRandomFloat()-0.5f) * 100.0f;
52 Extents.x = 2.0f + UnitRandomFloat() * 2.0f;
53 Extents.y = 2.0f + UnitRandomFloat() * 2.0f;
54 Extents.z = 2.0f + UnitRandomFloat() * 2.0f;
56 mBoxes[i].SetCenterExtents(Center, Extents);
57 mBoxPtrs[i] = &mBoxes[i];
59 mBoxTime[i] = 2000.0f*UnitRandomFloat();
63 void BipartiteBoxPruningTest::Release()
65 DELETEARRAY(mBoxTime);
69 void BipartiteBoxPruningTest::Select()
73 mBar = TwNewBar("BipartiteBoxPruning");
75 TwAddVarRW(mBar, "Speed", TW_TYPE_FLOAT, &mSpeed, " min=0.0 max=0.01 step=0.00001");
76 TwAddVarRW(mBar, "Amplitude", TW_TYPE_FLOAT, &mAmplitude, " min=10.0 max=200.0 step=0.1");
80 void BipartiteBoxPruningTest::Deselect()
89 bool BipartiteBoxPruningTest::UpdateBoxes()
91 for(udword i=0;i<mNbBoxes;i++)
93 mBoxTime[i] += mSpeed;
96 mBoxes[i].GetExtents(Extents);
98 Center.x = cosf(mBoxTime[i]*2.17f)*mAmplitude + sinf(mBoxTime[i])*mAmplitude*0.5f;
99 Center.y = cosf(mBoxTime[i]*1.38f)*mAmplitude + sinf(mBoxTime[i]*mAmplitude);
100 Center.z = sinf(mBoxTime[i]*0.777f)*mAmplitude;
102 mBoxes[i].SetCenterExtents(Center, Extents);
107 void BipartiteBoxPruningTest::PerformTest()
111 // We pretend that half the boxes belong to first group, and the other half to the second group.
112 udword Nb0 = mNbBoxes/2;
113 udword Nb1 = mNbBoxes - Nb0;
117 BipartiteBoxPruning(Nb0, mBoxPtrs, Nb1, mBoxPtrs+Nb0, mPairs, Axes(AXES_XZY));
121 // printf("%d pairs colliding\r ", mPairs.GetNbPairs());
123 bool* Flags = (bool*)_alloca(sizeof(bool)*mNbBoxes);
124 ZeroMemory(Flags, sizeof(bool)*mNbBoxes);
125 const Pair* P = mPairs.GetPairs();
126 for(udword i=0;i<mPairs.GetNbPairs();i++)
128 // A colliding pair is (i,j) where 0 <= i < Nb0 and 0 <= j < Nb1
129 Flags[P[i].id0] = true;
130 Flags[P[i].id1+Nb0] = true;
135 CurrentBox.mRot.Identity();
136 for(udword i=0;i<mNbBoxes;i++)
138 if(Flags[i]) glColor3f(1.0f, 0.0f, 0.0f);
142 glColor3f(0.0f, 1.0f, 0.0f);
144 glColor3f(0.0f, 0.0f, 1.0f);
146 mBoxes[i].GetCenter(CurrentBox.mCenter);
147 mBoxes[i].GetExtents(CurrentBox.mExtents);
152 sprintf(Buffer, "BipartiteBoxPruning - %5.1f us (%d cycles) - %d pairs\n", mProfiler.mMsTime, mProfiler.mCycles, mPairs.GetNbPairs());
153 GLFontRenderer::print(10.0f, 10.0f, 0.02f, Buffer);
156 void BipartiteBoxPruningTest::KeyboardCallback(unsigned char key, int x, int y)
160 void BipartiteBoxPruningTest::MouseCallback(int button, int state, int x, int y)
164 void BipartiteBoxPruningTest::MotionCallback(int x, int y)