Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / BipartiteBoxPruning.cpp
1 /*
2 CDTestFramework http://codercorner.com
3 Copyright (c) 2007-2008 Pierre Terdiman,  pierre@codercorner.com
4
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:
10
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.
14 */
15
16 #include "stdafx.h"
17 #include "BipartiteBoxPruning.h"
18 #include "RenderingHelpers.h"
19 #include "GLFontRenderer.h"
20
21 BipartiteBoxPruningTest::BipartiteBoxPruningTest() :
22         mBar                    (null),
23         mNbBoxes                (0),
24         mBoxes                  (null),
25         mBoxPtrs                (null),
26         mBoxTime                (null),
27         mSpeed                  (0.0f),
28         mAmplitude              (100.0f)
29 {
30 }
31
32 BipartiteBoxPruningTest::~BipartiteBoxPruningTest()
33 {
34         DELETEARRAY(mBoxTime);
35         DELETEARRAY(mBoxPtrs);
36         DELETEARRAY(mBoxes);
37 }
38
39 void BipartiteBoxPruningTest::Init()
40 {
41         mNbBoxes        = 1024;
42         mBoxes = new AABB[mNbBoxes];
43         mBoxPtrs = new const AABB*[mNbBoxes];
44         mBoxTime = new float[mNbBoxes];
45         for(udword i=0;i<mNbBoxes;i++)
46         {
47                 Point Center, Extents;
48
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;
55
56                 mBoxes[i].SetCenterExtents(Center, Extents);
57                 mBoxPtrs[i] = &mBoxes[i];
58
59                 mBoxTime[i] = 2000.0f*UnitRandomFloat();
60         }
61 }
62
63 void BipartiteBoxPruningTest::Release()
64 {
65         DELETEARRAY(mBoxTime);
66         DELETEARRAY(mBoxes);
67 }
68
69 void BipartiteBoxPruningTest::Select()
70 {
71         // Create a tweak bar
72         {
73                 mBar = TwNewBar("BipartiteBoxPruning");
74
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");
77         }
78 }
79
80 void BipartiteBoxPruningTest::Deselect()
81 {
82         if(mBar)
83         {
84                 TwDeleteBar(mBar);
85                 mBar = null;
86         }
87 }
88
89 bool BipartiteBoxPruningTest::UpdateBoxes()
90 {
91         for(udword i=0;i<mNbBoxes;i++)
92         {
93                 mBoxTime[i] += mSpeed;
94
95                 Point Center,Extents;
96                 mBoxes[i].GetExtents(Extents);
97
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;
101
102                 mBoxes[i].SetCenterExtents(Center, Extents);
103         }
104         return true;
105 }
106
107 void BipartiteBoxPruningTest::PerformTest()
108 {
109         UpdateBoxes();
110
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;
114
115         mPairs.ResetPairs();
116         mProfiler.Start();
117         BipartiteBoxPruning(Nb0, mBoxPtrs, Nb1, mBoxPtrs+Nb0, mPairs, Axes(AXES_XZY));
118         mProfiler.End();
119         mProfiler.Accum();
120
121 //      printf("%d pairs colliding\r     ", mPairs.GetNbPairs());
122
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++)
127         {
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;
131         }
132
133         // Render boxes
134         OBB CurrentBox;
135         CurrentBox.mRot.Identity();
136         for(udword i=0;i<mNbBoxes;i++)
137         {
138                 if(Flags[i])    glColor3f(1.0f, 0.0f, 0.0f);
139                 else
140                 {
141                         if(i<Nb0)
142                                 glColor3f(0.0f, 1.0f, 0.0f);
143                         else
144                                 glColor3f(0.0f, 0.0f, 1.0f);
145                 }
146                 mBoxes[i].GetCenter(CurrentBox.mCenter);
147                 mBoxes[i].GetExtents(CurrentBox.mExtents);
148                 DrawOBB(CurrentBox);
149         }
150
151         char Buffer[4096];
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);
154 }
155
156 void BipartiteBoxPruningTest::KeyboardCallback(unsigned char key, int x, int y)
157 {
158 }
159
160 void BipartiteBoxPruningTest::MouseCallback(int button, int state, int x, int y)
161 {
162 }
163
164 void BipartiteBoxPruningTest::MotionCallback(int x, int y)
165 {
166 }