1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)
#include "ExtremalQuery.h"
#include "Wm5ExtremalQuery3BSP.h"
#include "Wm5ExtremalQuery3PRJ.h"
WM5_WINDOW_APPLICATION(ExtremalQuery);
//----------------------------------------------------------------------------
ExtremalQuery::ExtremalQuery ()
:
WindowApplication3("SamplePhysics/ExtremalQuery", 0, 0, 640, 480,
Float4(0.8f, 0.8f, 0.8f, 1.0f)),
mTextColor(1.0f, 1.0f, 1.0f, 1.0f)
{
mConvexPolyhedron = 0;
mExtremalQuery = 0;
}
//----------------------------------------------------------------------------
bool ExtremalQuery::OnInitialize ()
{
if (!WindowApplication3::OnInitialize())
{
return false;
}
// Set up an orthogonal camera. This projection type is used to make it
// clear that the displayed extreme points really are extreme! (The
// perspective projection is deceptive.)
mCamera = new0 Camera(false);
mRenderer->SetCamera(mCamera);
mCamera->SetFrustum(1.0f, 1000.0f, -1.5f, 1.5f, -2.0, 2.0f);
APoint camPosition(4.0f, 0.0f, 0.0f);
AVector camDVector(-1.0f, 0.0f, 0.0f);
AVector camUVector(0.0f, 0.0f, 1.0f);
AVector camRVector = camDVector.Cross(camUVector);
mCamera->SetFrame(camPosition, camDVector, camUVector, camRVector);
// Set up the scene.
CreateScene();
// Initial update of objects.
mScene->Update();
// Initial culling of scene.
mCuller.SetCamera(mCamera);
mCuller.ComputeVisibleSet(mScene);
InitializeObjectMotion(mScene);
return true;
}
//----------------------------------------------------------------------------
void ExtremalQuery::OnTerminate ()
{
delete0(mConvexPolyhedron);
delete0(mExtremalQuery);
mScene = 0;
mWireState = 0;
mCullState = 0;
mMaxSphere = 0;
mMinSphere = 0;
WindowApplication3::OnTerminate();
}
//----------------------------------------------------------------------------
void ExtremalQuery::OnIdle ()
{
MeasureTime();
if (MoveCamera())
{
mCuller.ComputeVisibleSet(mScene);
}
if (MoveObject())
{
mScene->Update();
mCuller.ComputeVisibleSet(mScene);
}
if (mRenderer->PreDraw())
{
mRenderer->ClearBuffers();
mRenderer->Draw(mCuller.GetVisibleSet());
DrawFrameRate(8, GetHeight()-8, mTextColor);
mRenderer->PostDraw();
mRenderer->DisplayColorBuffer();
}
UpdateFrameCount();
}
//----------------------------------------------------------------------------
bool ExtremalQuery::OnKeyDown (unsigned char key, int x, int y)
{
if (WindowApplication3::OnKeyDown(key, x, y))
{
return true;
}
switch (key)
{
case 'w':
case 'W':
mWireState->Enabled = !mWireState->Enabled;
return true;
}
return false;
}
//----------------------------------------------------------------------------
bool ExtremalQuery::OnMotion (int button, int x, int y,
unsigned int modifiers)
{
WindowApplication3::OnMotion(button, x, y, modifiers);
UpdateExtremePoints();
return true;
}
//----------------------------------------------------------------------------
void ExtremalQuery::CreateScene ()
{
mScene = new0 Node();
mWireState = new0 WireState();
mRenderer->SetOverrideWireState(mWireState);
mCullState = new0 CullState();
mCullState->Enabled = false;
mRenderer->SetOverrideCullState(mCullState);
const int numVertices = 32;
CreateConvexPolyhedron(numVertices);
mScene->AttachChild(CreateVisualConvexPolyhedron());
// Use small spheres to show the extreme points in the camera's right
// direction.
VertexFormat* vformat = VertexFormat::Create(2,
VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);
StandardMesh sm(vformat);
VertexColor3Effect* effect = new0 VertexColor3Effect();
// maximum sphere
mMaxSphere = sm.Sphere(8, 8, 0.05f);
mMaxSphere->SetEffectInstance(effect->CreateInstance());
mScene->AttachChild(mMaxSphere);
// minimum sphere
mMinSphere = sm.Sphere(8, 8, 0.05f);
mMinSphere->SetEffectInstance(effect->CreateInstance());
mScene->AttachChild(mMinSphere);
UpdateExtremePoints();
}
//----------------------------------------------------------------------------
void ExtremalQuery::CreateConvexPolyhedron (int numVertices)
{
// Create the convex hull of a randomly generated set of points on the
// unit sphere.
Vector3f* vertices = new1<Vector3f>(numVertices);
int i, j;
for (i = 0; i < numVertices; ++i)
{
for (j = 0; j < 3; ++j)
{
vertices[i][j] = Mathf::SymmetricRandom();
}
vertices[i].Normalize();
}
ConvexHull3f hull(numVertices, vertices, 0.001f, false, Query::QT_INT64);
assertion(hull.GetDimension() == 3, "Invalid polyhedron.\n");
int numIndices = 3*hull.GetNumSimplices();
int* indices = new1<int>(numIndices);
memcpy(indices, hull.GetIndices(), numIndices*sizeof(int));
mConvexPolyhedron = new0 ConvexPolyhedron3f(numVertices, vertices,
numIndices/3, indices, 0);
#ifdef USE_BSP_QUERY
mExtremalQuery = new0 ExtremalQuery3BSPf(mConvexPolyhedron);
#else
mExtremalQuery = new0 ExtremalQuery3PRJf(mConvexPolyhedron);
#endif
#ifdef MEASURE_TIMING_OF_QUERY
// For timing purposes and determination of asymptotic order.
const int imax = 10000000;
Vector3f* directions = new1<Vector3f>(imax);
for (i = 0; i < imax; ++i)
{
for (j = 0; j < 3; ++j)
{
directions[i][j] = Mathf::SymmetricRandom();
}
directions[i].Normalize();
}
clock_t start = clock();
for (i = 0; i < imax; ++i)
{
int pos, neg;
mExtremalQuery->GetExtremeVertices(directions[i], pos, neg);
}
clock_t final = clock();
long diff = final - start;
double time = ((double)diff)/(double)CLOCKS_PER_SEC;
std::ofstream outFile("timing.txt");
outFile << "time = " << time << " seconds" << std::endl;
outFile.close();
delete1(directions);
#endif
}
//----------------------------------------------------------------------------
Node* ExtremalQuery::CreateVisualConvexPolyhedron ()
{
const Vector3f* vertices = mConvexPolyhedron->GetVertices();
int numTriangles = mConvexPolyhedron->GetNumTriangles();
int numIndices = 3*numTriangles;
const int* polyIndices = mConvexPolyhedron->GetIndices();
// Visualize the convex polyhedron as a collection of face-colored
// triangles.
VertexFormat* vformat = VertexFormat::Create(2,
VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);
int vstride = vformat->GetStride();
VertexBuffer* vbuffer = new0 VertexBuffer(numIndices, vstride);
VertexBufferAccessor vba(vformat, vbuffer);
IndexBuffer* ibuffer = new0 IndexBuffer(numIndices, sizeof(int));
int* indices = (int*)ibuffer->GetData();
int i;
for (i = 0; i < numIndices; ++i)
{
vba.Position<Vector3f>(i) = vertices[polyIndices[i]];
indices[i] = i;
}
TriMesh* mesh = new0 TriMesh(vformat, vbuffer, ibuffer);
// Use randomly generated vertex colors.
for (i = 0; i < numTriangles; ++i)
{
Float3 color;
for (int j = 0; j < 3; ++j)
{
color[j] = Mathf::UnitRandom();
}
vba.Color<Float3>(0, 3*i ) = color;
vba.Color<Float3>(0, 3*i+1) = color;
vba.Color<Float3>(0, 3*i+2) = color;
}
mesh->SetEffectInstance(VertexColor3Effect::CreateUniqueInstance());
Node* root = new0 Node();
root->AttachChild(mesh);
return root;
}
//----------------------------------------------------------------------------
void ExtremalQuery::UpdateExtremePoints ()
{
AVector rVector = mScene->WorldTransform.Inverse()*mCamera->GetRVector();
Vector3f direction(rVector[0], rVector[1], rVector[2]);
int posDir, negDir;
mExtremalQuery->GetExtremeVertices(direction, posDir, negDir);
Vector3f extreme = mConvexPolyhedron->GetVertex(posDir);
mMaxSphere->LocalTransform.SetTranslate(extreme);
extreme = mConvexPolyhedron->GetVertex(negDir);
mMinSphere->LocalTransform.SetTranslate(extreme);
mScene->Update();
}
//----------------------------------------------------------------------------
|