[167] | 1 | #include "Ray.h"
|
---|
[162] | 2 | #include "Mesh.h"
|
---|
[170] | 3 | #include "MeshKdTree.h"
|
---|
[191] | 4 | #include "Triangle3.h"
|
---|
[1001] | 5 | #include "ResourceManager.h"
|
---|
[162] | 6 |
|
---|
[863] | 7 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 8 |
|
---|
[878] | 9 | bool MeshDebug = false;
|
---|
[860] | 10 |
|
---|
[1297] | 11 | int Intersectable::sMailId = 1;//2147483647;
|
---|
[382] | 12 | int Intersectable::sReservedMailboxes = 1;
|
---|
[162] | 13 |
|
---|
[752] | 14 | struct SortableVertex {
|
---|
| 15 |
|
---|
| 16 | Vector3 vertex;
|
---|
| 17 |
|
---|
| 18 | int originalId;
|
---|
| 19 | int newId;
|
---|
| 20 | int finalPos;
|
---|
| 21 |
|
---|
| 22 | SortableVertex() {}
|
---|
| 23 |
|
---|
| 24 | SortableVertex(const Vector3 &v,
|
---|
| 25 | const int id):
|
---|
| 26 | vertex(v),
|
---|
| 27 | originalId(id),
|
---|
| 28 | newId(id)
|
---|
| 29 | {}
|
---|
| 30 |
|
---|
| 31 | friend bool operator<(const SortableVertex &a,
|
---|
| 32 | const SortableVertex &b)
|
---|
| 33 | {
|
---|
| 34 | if (a.vertex.x < b.vertex.x)
|
---|
| 35 | return true;
|
---|
| 36 | else
|
---|
| 37 | if (a.vertex.x > b.vertex.x)
|
---|
| 38 | return false;
|
---|
| 39 |
|
---|
| 40 | if (a.vertex.y < b.vertex.y)
|
---|
| 41 | return true;
|
---|
| 42 | else
|
---|
| 43 | if (a.vertex.y > b.vertex.y)
|
---|
| 44 | return false;
|
---|
| 45 |
|
---|
| 46 | if (a.vertex.z < b.vertex.z)
|
---|
| 47 | return true;
|
---|
| 48 | else
|
---|
| 49 | // if (a.z > b.z)
|
---|
| 50 | return false;
|
---|
| 51 |
|
---|
| 52 | // return false;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | };
|
---|
| 56 |
|
---|
[863] | 57 |
|
---|
[162] | 58 | void
|
---|
[859] | 59 | Mesh::ComputeBoundingBox()
|
---|
[863] | 60 | {
|
---|
| 61 |
|
---|
[752] | 62 | mBox.Initialize();
|
---|
[162] | 63 | VertexContainer::const_iterator vi = mVertices.begin();
|
---|
| 64 | for (; vi != mVertices.end(); vi++) {
|
---|
[176] | 65 | mBox.Include(*vi);
|
---|
[162] | 66 | }
|
---|
[873] | 67 | //mBox.Enlarge(1e-4f);
|
---|
[863] | 68 | }
|
---|
| 69 |
|
---|
[859] | 70 | void
|
---|
[1292] | 71 | Mesh::Preprocess(bool cleanup)
|
---|
[859] | 72 | {
|
---|
[1292] | 73 | if (cleanup)
|
---|
[870] | 74 | Cleanup();
|
---|
[1076] | 75 |
|
---|
[870] | 76 | ComputeBoundingBox();
|
---|
[1076] | 77 |
|
---|
[870] | 78 | /** true if it is a watertight convex mesh
|
---|
| 79 | */
|
---|
| 80 | mIsConvex = false;
|
---|
| 81 |
|
---|
| 82 | if (mFaces.size() > MeshKdTree::mTermMinCost)
|
---|
| 83 | {
|
---|
| 84 | mKdTree = new MeshKdTree(this);
|
---|
| 85 | MeshKdLeaf *root = (MeshKdLeaf *)mKdTree->GetRoot();
|
---|
| 86 |
|
---|
| 87 | for (int i = 0; i < mFaces.size(); i++)
|
---|
| 88 | root->mFaces.push_back(i);
|
---|
| 89 |
|
---|
| 90 | cout<<"KD";
|
---|
[1076] | 91 |
|
---|
[870] | 92 | mKdTree->Construct();
|
---|
| 93 |
|
---|
| 94 | if (mKdTree->GetRoot()->IsLeaf())
|
---|
| 95 | {
|
---|
| 96 | cout<<"d";
|
---|
| 97 | delete mKdTree;
|
---|
| 98 | mKdTree = NULL;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
[162] | 101 | }
|
---|
| 102 |
|
---|
[752] | 103 |
|
---|
| 104 | void
|
---|
| 105 | Mesh::IndexVertices()
|
---|
| 106 | {
|
---|
| 107 | int i;
|
---|
| 108 | // check whether the vertices can be simplfied and reindexed
|
---|
| 109 | vector<SortableVertex> svertices(mVertices.size());
|
---|
| 110 |
|
---|
| 111 | for (i=0; i < mVertices.size(); i++)
|
---|
| 112 | svertices[i] = SortableVertex(mVertices[i], i);
|
---|
| 113 |
|
---|
| 114 | sort(svertices.begin(), svertices.end());
|
---|
| 115 |
|
---|
| 116 | for (i=0; i < svertices.size() - 1; i++)
|
---|
| 117 | if (svertices[i].vertex == svertices[i+1].vertex)
|
---|
| 118 | svertices[i+1].newId = svertices[i].newId;
|
---|
| 119 |
|
---|
| 120 | // remove the same vertices
|
---|
| 121 | int k = 0;
|
---|
| 122 | mVertices[0] = svertices[0].vertex;
|
---|
| 123 | svertices[0].finalPos = 0;
|
---|
| 124 |
|
---|
| 125 | for (i=1; i < svertices.size(); i++) {
|
---|
| 126 | if (svertices[i].newId != svertices[i-1].newId)
|
---|
| 127 | k++;
|
---|
| 128 |
|
---|
| 129 | mVertices[k] = svertices[i].vertex;
|
---|
| 130 | svertices[i].finalPos = k;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | mVertices.resize(k + 1);
|
---|
| 134 |
|
---|
| 135 | vector<int> remapBuffer(svertices.size());
|
---|
| 136 | for (i = 0; i < svertices.size(); i++)
|
---|
| 137 | remapBuffer[svertices[i].originalId] = svertices[i].finalPos;
|
---|
| 138 |
|
---|
| 139 | // remap all faces
|
---|
| 140 |
|
---|
| 141 | for (int faceIndex = 0; faceIndex < mFaces.size(); faceIndex++) {
|
---|
| 142 | Face *face = mFaces[faceIndex];
|
---|
| 143 | for (int i = 0; i < face->mVertexIndices.size(); i++) {
|
---|
| 144 | face->mVertexIndices[i] = remapBuffer[face->mVertexIndices[i]];
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[170] | 149 | AxisAlignedBox3
|
---|
| 150 | Mesh::GetFaceBox(const int faceIndex)
|
---|
| 151 | {
|
---|
| 152 | Face *face = mFaces[faceIndex];
|
---|
| 153 | AxisAlignedBox3 box;
|
---|
| 154 | box.SetMin( mVertices[face->mVertexIndices[0]] );
|
---|
| 155 | box.SetMax(box.Min());
|
---|
| 156 | for (int i = 1; i < face->mVertexIndices.size(); i++) {
|
---|
| 157 | box.Include(mVertices[face->mVertexIndices[i]]);
|
---|
| 158 | }
|
---|
| 159 | return box;
|
---|
| 160 | }
|
---|
[162] | 161 |
|
---|
| 162 | int
|
---|
[170] | 163 | Mesh::CastRayToFace(
|
---|
[878] | 164 | const int faceIndex,
|
---|
| 165 | Ray &ray,
|
---|
| 166 | float &nearestT,
|
---|
[1199] | 167 | Vector3 &nearestNormal,
|
---|
[878] | 168 | int &nearestFace,
|
---|
| 169 | Intersectable *instance
|
---|
| 170 | )
|
---|
[170] | 171 | {
|
---|
| 172 | float t;
|
---|
| 173 | int hit = 0;
|
---|
[1199] | 174 | Vector3 normal;
|
---|
| 175 | if (RayFaceIntersection(faceIndex, ray, t, normal, nearestT) == Ray::INTERSECTION) {
|
---|
[170] | 176 | switch (ray.GetType()) {
|
---|
| 177 | case Ray::GLOBAL_RAY:
|
---|
[1199] | 178 | ray.intersections.push_back(Ray::Intersection(t, normal, instance, faceIndex));
|
---|
[170] | 179 | hit++;
|
---|
| 180 | break;
|
---|
| 181 | case Ray::LOCAL_RAY:
|
---|
| 182 | nearestT = t;
|
---|
[1199] | 183 | nearestNormal = normal;
|
---|
[170] | 184 | nearestFace = faceIndex;
|
---|
| 185 | hit++;
|
---|
| 186 | break;
|
---|
[245] | 187 | case Ray::LINE_SEGMENT:
|
---|
| 188 | if (t <= 1.0f) {
|
---|
[1199] | 189 | ray.intersections.push_back(Ray::Intersection(t, normal, instance, faceIndex));
|
---|
[878] | 190 | hit++;
|
---|
[245] | 191 | }
|
---|
| 192 | break;
|
---|
[170] | 193 | }
|
---|
| 194 | }
|
---|
| 195 | return hit;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | int
|
---|
[162] | 199 | Mesh::CastRay(
|
---|
[444] | 200 | Ray &ray,
|
---|
| 201 | MeshInstance *instance
|
---|
| 202 | )
|
---|
[162] | 203 | {
|
---|
[170] | 204 | if (mKdTree) {
|
---|
| 205 | return mKdTree->CastRay(ray, instance);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[162] | 208 | int faceIndex = 0;
|
---|
| 209 | int hits = 0;
|
---|
| 210 | float nearestT = MAX_FLOAT;
|
---|
[1199] | 211 | Vector3 nearestNormal;
|
---|
[170] | 212 | int nearestFace = -1;
|
---|
| 213 |
|
---|
[162] | 214 | if (ray.GetType() == Ray::LOCAL_RAY && ray.intersections.size())
|
---|
| 215 | nearestT = ray.intersections[0].mT;
|
---|
| 216 |
|
---|
[376] | 217 |
|
---|
[162] | 218 | for ( ;
|
---|
[492] | 219 | faceIndex < mFaces.size();
|
---|
| 220 | faceIndex++) {
|
---|
[1199] | 221 | hits += CastRayToFace(faceIndex, ray, nearestT, nearestNormal, nearestFace, instance);
|
---|
[170] | 222 | if (mIsConvex && nearestFace != -1)
|
---|
| 223 | break;
|
---|
[162] | 224 | }
|
---|
| 225 |
|
---|
| 226 | if ( hits && ray.GetType() == Ray::LOCAL_RAY ) {
|
---|
| 227 | if (ray.intersections.size())
|
---|
[1199] | 228 | ray.intersections[0] = Ray::Intersection(nearestT, nearestNormal, instance, nearestFace);
|
---|
[162] | 229 | else
|
---|
[1199] | 230 | ray.intersections.push_back(Ray::Intersection(nearestT, nearestNormal, instance, nearestFace));
|
---|
[162] | 231 | }
|
---|
| 232 |
|
---|
| 233 | return hits;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[170] | 236 | int
|
---|
| 237 | Mesh::CastRayToSelectedFaces(
|
---|
[492] | 238 | Ray &ray,
|
---|
| 239 | const vector<int> &faces,
|
---|
| 240 | Intersectable *instance
|
---|
| 241 | )
|
---|
[170] | 242 | {
|
---|
| 243 | vector<int>::const_iterator fi;
|
---|
| 244 | int faceIndex = 0;
|
---|
| 245 | int hits = 0;
|
---|
| 246 | float nearestT = MAX_FLOAT;
|
---|
[1199] | 247 | Vector3 nearestNormal;
|
---|
[170] | 248 | int nearestFace = -1;
|
---|
[1199] | 249 |
|
---|
[170] | 250 | if (ray.GetType() == Ray::LOCAL_RAY && ray.intersections.size())
|
---|
| 251 | nearestT = ray.intersections[0].mT;
|
---|
| 252 |
|
---|
| 253 | for ( fi = faces.begin();
|
---|
[878] | 254 | fi != faces.end();
|
---|
| 255 | fi++) {
|
---|
[1199] | 256 | hits += CastRayToFace(*fi, ray, nearestT, nearestNormal, nearestFace, instance);
|
---|
[170] | 257 | if (mIsConvex && nearestFace != -1)
|
---|
| 258 | break;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | if ( hits && ray.GetType() == Ray::LOCAL_RAY ) {
|
---|
| 262 | if (ray.intersections.size())
|
---|
[1199] | 263 | ray.intersections[0] = Ray::Intersection(nearestT, nearestNormal,instance, nearestFace);
|
---|
[170] | 264 | else
|
---|
[1199] | 265 | ray.intersections.push_back(Ray::Intersection(nearestT, nearestNormal,instance, nearestFace));
|
---|
[170] | 266 | }
|
---|
| 267 |
|
---|
| 268 | return hits;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 |
|
---|
[162] | 272 | // int_lineseg returns 1 if the given line segment intersects a 2D
|
---|
| 273 | // ray travelling in the positive X direction. This is used in the
|
---|
| 274 | // Jordan curve computation for polygon intersection.
|
---|
| 275 | inline int
|
---|
| 276 | int_lineseg(float px,
|
---|
[492] | 277 | float py,
|
---|
| 278 | float u1,
|
---|
| 279 | float v1,
|
---|
| 280 | float u2,
|
---|
| 281 | float v2)
|
---|
[162] | 282 | {
|
---|
| 283 | float ydiff;
|
---|
| 284 |
|
---|
| 285 | u1 -= px; u2 -= px; // translate line
|
---|
| 286 | v1 -= py; v2 -= py;
|
---|
| 287 |
|
---|
| 288 | if ((v1 > 0 && v2 > 0) ||
|
---|
| 289 | (v1 < 0 && v2 < 0) ||
|
---|
| 290 | (u1 < 0 && u2 < 0))
|
---|
| 291 | return 0;
|
---|
| 292 |
|
---|
| 293 | if (u1 > 0 && u2 > 0)
|
---|
| 294 | return 1;
|
---|
| 295 |
|
---|
| 296 | ydiff = v2 - v1;
|
---|
| 297 | if (fabs(ydiff) < Limits::Small) { // denominator near 0
|
---|
| 298 | if (((fabs(v1) > Limits::Small) ||
|
---|
[492] | 299 | (u1 > 0) || (u2 > 0)))
|
---|
[162] | 300 | return 0;
|
---|
| 301 | return 1;
|
---|
| 302 | }
|
---|
[492] | 303 |
|
---|
| 304 | double t = -v1 / ydiff; // Compute parameter
|
---|
[162] | 305 |
|
---|
[492] | 306 | double thresh;
|
---|
[878] | 307 |
|
---|
[492] | 308 | if (ydiff < 0.0f)
|
---|
[878] | 309 | thresh = -1e-20;
|
---|
[492] | 310 | else
|
---|
[878] | 311 | thresh = 1e-20;
|
---|
[162] | 312 |
|
---|
[492] | 313 |
|
---|
| 314 | return (u1 + t * (u2 - u1)) > thresh; //-Limits::Small;
|
---|
[162] | 315 | }
|
---|
| 316 |
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 | // intersection with the polygonal face of the mesh
|
---|
| 320 | int
|
---|
| 321 | Mesh::RayFaceIntersection(const int faceIndex,
|
---|
[492] | 322 | const Ray &ray,
|
---|
| 323 | float &t,
|
---|
[1199] | 324 | Vector3 &normal,
|
---|
[492] | 325 | const float nearestT
|
---|
| 326 | )
|
---|
[162] | 327 | {
|
---|
| 328 | Face *face = mFaces[faceIndex];
|
---|
| 329 |
|
---|
| 330 | Plane3 plane = GetFacePlane(faceIndex);
|
---|
| 331 | float dot = DotProd(plane.mNormal, ray.GetDir());
|
---|
[878] | 332 |
|
---|
| 333 | if (MeshDebug) {
|
---|
| 334 | cout<<endl<<endl;
|
---|
| 335 | cout<<"normal="<<plane.mNormal<<endl;
|
---|
| 336 | cout<<"dot="<<dot<<endl;
|
---|
| 337 | }
|
---|
[162] | 338 |
|
---|
[878] | 339 |
|
---|
[162] | 340 | // Watch for near-zero denominator
|
---|
| 341 | // ONLY single sided polygons!!!!!
|
---|
[534] | 342 | if (ray.mFlags & Ray::CULL_BACKFACES) {
|
---|
| 343 | if (dot > -Limits::Small)
|
---|
| 344 | // if (fabs(dot) < Limits::Small)
|
---|
| 345 | return Ray::NO_INTERSECTION;
|
---|
| 346 | } else {
|
---|
| 347 | if (fabs(dot) < Limits::Small)
|
---|
| 348 | return Ray::NO_INTERSECTION;
|
---|
| 349 | }
|
---|
[162] | 350 |
|
---|
[1199] | 351 | normal = plane.mNormal;
|
---|
| 352 |
|
---|
[162] | 353 | t = (-plane.mD - DotProd(plane.mNormal, ray.GetLoc())) / dot;
|
---|
[878] | 354 |
|
---|
| 355 | if (MeshDebug)
|
---|
| 356 | cout<<"t="<<t<<endl;
|
---|
| 357 |
|
---|
[162] | 358 | if (t <= Limits::Small)
|
---|
| 359 | return Ray::INTERSECTION_OUT_OF_LIMITS;
|
---|
| 360 |
|
---|
| 361 | if (t >= nearestT) {
|
---|
| 362 | return Ray::INTERSECTION_OUT_OF_LIMITS; // no intersection was found
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | int count = 0;
|
---|
| 366 | float u, v, u1, v1, u2, v2;
|
---|
| 367 | int i;
|
---|
| 368 |
|
---|
| 369 | int paxis = plane.mNormal.DrivingAxis();
|
---|
| 370 |
|
---|
[878] | 371 |
|
---|
[162] | 372 | // Project the intersection point onto the coordinate plane
|
---|
| 373 | // specified by which.
|
---|
| 374 | ray.Extrap(t).ExtractVerts(&u, &v, paxis);
|
---|
| 375 |
|
---|
| 376 |
|
---|
[469] | 377 | int size = (int)face->mVertexIndices.size();
|
---|
[170] | 378 |
|
---|
[878] | 379 | if (MeshDebug)
|
---|
| 380 | cout<<"size="<<size<<endl;
|
---|
| 381 |
|
---|
[170] | 382 | mVertices[face->mVertexIndices[size - 1]].
|
---|
[162] | 383 | ExtractVerts(&u1, &v1, paxis );
|
---|
[170] | 384 |
|
---|
[752] | 385 | //$$JB changed 12.4.2006 from 0 ^^
|
---|
[170] | 386 | if (0 && size <= 4) {
|
---|
[162] | 387 | // assume a convex face
|
---|
[170] | 388 | for (i = 0; i < size; i++) {
|
---|
[162] | 389 | mVertices[face->mVertexIndices[i]].ExtractVerts(&u2, &v2, paxis);
|
---|
| 390 | // line u1, v1, u2, v2
|
---|
[878] | 391 | if ((v1 - v2)*(u - u1) + (u2 - u1)*(v - v1) > 0) {
|
---|
| 392 | if (MeshDebug)
|
---|
| 393 | cout<<"exit on "<<i<<endl;
|
---|
[492] | 394 | return Ray::NO_INTERSECTION;
|
---|
[878] | 395 | }
|
---|
[162] | 396 | u1 = u2;
|
---|
| 397 | v1 = v2;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | return Ray::INTERSECTION;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | // We're stuck with the Jordan curve computation. Count number
|
---|
| 404 | // of intersections between the line segments the polygon comprises
|
---|
| 405 | // with a ray originating at the point of intersection and
|
---|
| 406 | // travelling in the positive X direction.
|
---|
[170] | 407 | for (i = 0; i < size; i++) {
|
---|
[162] | 408 | mVertices[face->mVertexIndices[i]].ExtractVerts(&u2, &v2, paxis);
|
---|
| 409 | count += (int_lineseg(u, v, u1, v1, u2, v2) != 0);
|
---|
| 410 | u1 = u2;
|
---|
| 411 | v1 = v2;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
[878] | 414 | if (MeshDebug)
|
---|
| 415 | cout<<"count="<<count<<endl;
|
---|
| 416 |
|
---|
[162] | 417 | // We hit polygon if number of intersections is odd.
|
---|
| 418 | return (count & 1) ? Ray::INTERSECTION : Ray::NO_INTERSECTION;
|
---|
| 419 | }
|
---|
| 420 |
|
---|
[349] | 421 | int
|
---|
[176] | 422 | Mesh::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 423 | {
|
---|
[1020] | 424 | const int faceIndex = (int)RandomValue(0, (Real)((int)mFaces.size()-1));
|
---|
[176] | 425 |
|
---|
| 426 | // assume the face is convex and generate a convex combination
|
---|
| 427 | //
|
---|
| 428 | Face *face = mFaces[faceIndex];
|
---|
[1020] | 429 |
|
---|
[176] | 430 | point = Vector3(0,0,0);
|
---|
| 431 | float sum = 0.0f;
|
---|
[1020] | 432 |
|
---|
[176] | 433 | for (int i = 0; i < face->mVertexIndices.size(); i++) {
|
---|
| 434 | float r = RandomValue(0,1);
|
---|
| 435 | sum += r;
|
---|
| 436 | point += mVertices[face->mVertexIndices[i]]*r;
|
---|
| 437 | }
|
---|
| 438 | point *= 1.0f/sum;
|
---|
[340] | 439 |
|
---|
| 440 | normal = GetFacePlane(faceIndex).mNormal;
|
---|
[349] | 441 |
|
---|
| 442 | return faceIndex;
|
---|
[176] | 443 | }
|
---|
| 444 |
|
---|
[162] | 445 | int
|
---|
[354] | 446 | Mesh::GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
[1001] | 447 | Vector3 &normal,
|
---|
| 448 | const Vector3 &viewpoint,
|
---|
| 449 | const int maxTries)
|
---|
[354] | 450 | {
|
---|
[359] | 451 | Plane3 plane;
|
---|
[485] | 452 | int faceIndex = (int)RandomValue(0, (Real)((int)mFaces.size()-1));
|
---|
[359] | 453 | int tries;
|
---|
| 454 | for (tries = 0; tries < maxTries; tries++) {
|
---|
| 455 | Face *face = mFaces[faceIndex];
|
---|
| 456 | plane = GetFacePlane(faceIndex);
|
---|
| 457 |
|
---|
| 458 | if (plane.Side(viewpoint) > 0) {
|
---|
| 459 | point = Vector3(0,0,0);
|
---|
| 460 | float sum = 0.0f;
|
---|
| 461 | // pickup a point inside this triangle
|
---|
| 462 | for (int i = 0; i < face->mVertexIndices.size(); i++) {
|
---|
[354] | 463 | float r = RandomValue(0,1);
|
---|
| 464 | sum += r;
|
---|
| 465 | point += mVertices[face->mVertexIndices[i]]*r;
|
---|
[359] | 466 | }
|
---|
| 467 | point *= 1.0f/sum;
|
---|
| 468 | break;
|
---|
| 469 | }
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | normal = plane.mNormal;
|
---|
| 473 | return (tries < maxTries) ? faceIndex + 1 : 0;
|
---|
[354] | 474 | }
|
---|
| 475 |
|
---|
| 476 |
|
---|
[162] | 477 | Plane3
|
---|
[1418] | 478 | Mesh::GetFacePlane(const int faceIndex) const
|
---|
[162] | 479 | {
|
---|
| 480 | Face *face = mFaces[faceIndex];
|
---|
[340] | 481 | return Plane3(mVertices[face->mVertexIndices[0]],
|
---|
[333] | 482 | mVertices[face->mVertexIndices[1]],
|
---|
| 483 | mVertices[face->mVertexIndices[2]]);
|
---|
[162] | 484 | }
|
---|
| 485 |
|
---|
[340] | 486 | bool
|
---|
| 487 | Mesh::ValidateFace(const int i)
|
---|
| 488 | {
|
---|
| 489 | Face *face = mFaces[i];
|
---|
| 490 |
|
---|
| 491 | Plane3 plane = Plane3(mVertices[face->mVertexIndices[0]],
|
---|
[1076] | 492 | mVertices[face->mVertexIndices[1]],
|
---|
| 493 | mVertices[face->mVertexIndices[2]]);
|
---|
[340] | 494 |
|
---|
| 495 | if (!eq(Magnitude(plane.mNormal), 1.0f))
|
---|
| 496 | return false;
|
---|
[350] | 497 |
|
---|
| 498 | return true;
|
---|
[340] | 499 | }
|
---|
| 500 |
|
---|
[1419] | 501 |
|
---|
| 502 | bool Mesh::CheckMesh() const
|
---|
| 503 | {
|
---|
| 504 | VertexContainer::const_iterator vit, vit_end = mVertices.end();
|
---|
| 505 |
|
---|
| 506 | for (vit = mVertices.begin(); vit != vit_end; ++ vit)
|
---|
| 507 | {
|
---|
| 508 | for (int i = 0; i < 3; ++ i)
|
---|
| 509 | {
|
---|
| 510 | const float x = (*vit)[i];
|
---|
| 511 | if (x != x) // nan
|
---|
| 512 | //if (isnan((*vit)[i]))
|
---|
| 513 | {
|
---|
| 514 | cout << "warning: vertex is ill defined" << endl;
|
---|
| 515 | return false;
|
---|
| 516 | }
|
---|
| 517 | }
|
---|
| 518 | }
|
---|
| 519 | return true;
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 |
|
---|
[1420] | 523 | static void PrintFace(const Mesh *mesh, const int idx, ostream &app)
|
---|
| 524 | {
|
---|
| 525 | Face *face = mesh->mFaces[idx];
|
---|
| 526 |
|
---|
| 527 | VertexIndexContainer::iterator it, it_end = face->mVertexIndices.end();
|
---|
| 528 |
|
---|
| 529 | cout << "face " << idx << endl;
|
---|
| 530 | for (it = face->mVertexIndices.begin(); it != it_end; ++ it)
|
---|
| 531 | {
|
---|
| 532 | cout << (*it) << ": " << mesh->mVertices[*it] << " ";
|
---|
| 533 | }
|
---|
| 534 | cout << endl;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 |
|
---|
[1419] | 538 | void Mesh::Print(ostream &app) const
|
---|
| 539 | {
|
---|
| 540 | VertexContainer::const_iterator vit, vit_end = mVertices.end();
|
---|
| 541 |
|
---|
| 542 | for (vit = mVertices.begin(); vit != vit_end; ++ vit)
|
---|
| 543 | {
|
---|
| 544 | app << (*vit) << " ";
|
---|
| 545 | }
|
---|
| 546 | app << endl;
|
---|
| 547 |
|
---|
[1420] | 548 | for (int i = 0; i < (int)mFaces.size(); ++ i)
|
---|
[1419] | 549 | {
|
---|
[1420] | 550 | PrintFace(this, i, app);
|
---|
[1419] | 551 | }
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 |
|
---|
[340] | 555 | void
|
---|
| 556 | Mesh::Cleanup()
|
---|
| 557 | {
|
---|
| 558 | int toRemove = 0;
|
---|
| 559 | FaceContainer newFaces;
|
---|
| 560 | for (int i=0; i < mFaces.size(); i++)
|
---|
| 561 | if (ValidateFace(i)) {
|
---|
| 562 | newFaces.push_back(mFaces[i]);
|
---|
| 563 | } else {
|
---|
| 564 | cout<<"d";
|
---|
| 565 | delete mFaces[i];
|
---|
| 566 | toRemove++;
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 | if (toRemove) {
|
---|
| 570 | mFaces = newFaces;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | // cleanup vertices??
|
---|
| 574 | }
|
---|
| 575 |
|
---|
[170] | 576 |
|
---|
[1418] | 577 | Vector3 Mesh::GetNormal(const int idx) const
|
---|
| 578 | {
|
---|
| 579 | return GetFacePlane(idx).mNormal;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 |
|
---|
[191] | 583 | void
|
---|
| 584 | Mesh::AddTriangle(const Triangle3 &triangle)
|
---|
| 585 | {
|
---|
[469] | 586 | int index = (int)mVertices.size();
|
---|
[191] | 587 |
|
---|
| 588 | for (int i=0; i < 3; i++) {
|
---|
| 589 | mVertices.push_back(triangle.mVertices[i]);
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 | AddFace(new Face(index + 0, index + 1, index + 2) );
|
---|
| 593 | }
|
---|
[209] | 594 |
|
---|
| 595 | void
|
---|
| 596 | Mesh::AddRectangle(const Rectangle3 &rect)
|
---|
| 597 | {
|
---|
[469] | 598 | int index = (int)mVertices.size();
|
---|
[209] | 599 |
|
---|
| 600 | for (int i=0; i < 4; i++) {
|
---|
| 601 | mVertices.push_back(rect.mVertices[i]);
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | AddFace(new Face(index + 0, index + 1, index + 2, index + 3) );
|
---|
| 605 | }
|
---|
[750] | 606 |
|
---|
[752] | 607 | void
|
---|
| 608 | Mesh::AssignRandomMaterial()
|
---|
[1001] | 609 | {
|
---|
| 610 | mMaterial = MaterialManager::GetSingleton()->CreateResource();
|
---|
| 611 |
|
---|
| 612 | Material randMat = RandomMaterial();
|
---|
[750] | 613 |
|
---|
[1001] | 614 | mMaterial->mDiffuseColor = randMat.mDiffuseColor;
|
---|
| 615 | mMaterial->mSpecularColor = randMat.mSpecularColor;
|
---|
| 616 | mMaterial->mAmbientColor = randMat.mAmbientColor;
|
---|
[752] | 617 | }
|
---|
[750] | 618 |
|
---|
[752] | 619 |
|
---|
[991] | 620 | Mesh *CreateMeshFromBox(const AxisAlignedBox3 &box)
|
---|
[750] | 621 | {
|
---|
[1001] | 622 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
| 623 |
|
---|
| 624 | // add 8 vertices of the box
|
---|
| 625 | const int index = (int)mesh->mVertices.size();
|
---|
| 626 |
|
---|
| 627 | for (int i=0; i < 8; ++ i)
|
---|
| 628 | {
|
---|
| 629 | Vector3 v;
|
---|
| 630 | box.GetVertex(i, v);
|
---|
| 631 | mesh->mVertices.push_back(v);
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 635 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 636 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
[750] | 637 |
|
---|
[1001] | 638 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 639 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 640 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
[750] | 641 |
|
---|
[1001] | 642 | return mesh;
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 |
|
---|
| 646 | Mesh::Mesh(const int id, const int vertices, const int faces):
|
---|
| 647 | mFaces(),
|
---|
| 648 | mMaterial(NULL),
|
---|
| 649 | mKdTree(NULL),
|
---|
| 650 | mVertices(),
|
---|
| 651 | mIsConvex(false),
|
---|
| 652 | mIsWatertight(false),
|
---|
| 653 | mId(id)
|
---|
| 654 | {
|
---|
| 655 | mVertices.reserve(vertices);
|
---|
| 656 | mFaces.reserve(faces);
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 |
|
---|
| 660 | Mesh::Mesh(const int id):
|
---|
| 661 | mId(id), mVertices(), mFaces(), mMaterial(NULL), mKdTree(NULL)
|
---|
| 662 | {}
|
---|
| 663 |
|
---|
| 664 |
|
---|
| 665 | // apply transformation to each vertex
|
---|
| 666 | void Mesh::ApplyTransformation(const Matrix4x4 &m)
|
---|
| 667 | {
|
---|
| 668 | VertexContainer::iterator it, it_end = mVertices.end();
|
---|
| 669 |
|
---|
| 670 | for (it = mVertices.begin(); it != it_end; ++ it)
|
---|
| 671 | {
|
---|
| 672 | (*it) = m * (*it);
|
---|
| 673 | }
|
---|
| 674 | }
|
---|
| 675 |
|
---|
| 676 |
|
---|
[1020] | 677 | Mesh::Mesh(const Mesh &rhs):
|
---|
| 678 | mKdTree(NULL)
|
---|
[1001] | 679 | {
|
---|
| 680 | mVertices = rhs.mVertices;
|
---|
| 681 | mFaces.reserve(rhs.mFaces.size());
|
---|
| 682 | mId = rhs.mId;
|
---|
[1002] | 683 | mMaterial = rhs.mMaterial;
|
---|
[1020] | 684 |
|
---|
[1001] | 685 | FaceContainer::const_iterator it, it_end = rhs.mFaces.end();
|
---|
| 686 |
|
---|
| 687 | for (it = rhs.mFaces.begin(); it != it_end; ++ it)
|
---|
| 688 | {
|
---|
| 689 | Face *face = *it;
|
---|
| 690 | mFaces.push_back(new Face(*face));
|
---|
| 691 | }
|
---|
| 692 | }
|
---|
| 693 |
|
---|
| 694 |
|
---|
| 695 | Mesh& Mesh::operator=(const Mesh& m)
|
---|
| 696 | {
|
---|
| 697 | if (this == &m)
|
---|
| 698 | return *this;
|
---|
| 699 |
|
---|
| 700 | CLEAR_CONTAINER(mFaces);
|
---|
| 701 |
|
---|
| 702 | mVertices = m.mVertices;
|
---|
| 703 | mFaces.reserve(m.mFaces.size());
|
---|
[1002] | 704 | mMaterial = m.mMaterial;
|
---|
| 705 | // note: we don't copy id on purpose
|
---|
[1001] | 706 | //mId = m.mId;
|
---|
[1002] | 707 |
|
---|
[1001] | 708 | FaceContainer::const_iterator it, it_end = m.mFaces.end();
|
---|
| 709 |
|
---|
| 710 | for (it = m.mFaces.begin(); it != it_end; ++ it)
|
---|
| 711 | {
|
---|
| 712 | Face *face = *it;
|
---|
| 713 | mFaces.push_back(new Face(*face));
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | return *this;
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 |
|
---|
[1005] | 720 | Mesh::~Mesh()
|
---|
| 721 | {
|
---|
| 722 | for (int i=0; i < mFaces.size(); ++ i)
|
---|
| 723 | delete mFaces[i];
|
---|
[1001] | 724 |
|
---|
[1005] | 725 | DEL_PTR(mKdTree);
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 |
|
---|
| 729 | void Mesh::Clear()
|
---|
| 730 | {
|
---|
| 731 | mVertices.clear();
|
---|
| 732 | mFaces.clear();
|
---|
| 733 |
|
---|
| 734 | DEL_PTR(mKdTree);
|
---|
| 735 | }
|
---|
| 736 |
|
---|
[1449] | 737 |
|
---|
| 738 |
|
---|
[1001] | 739 | /********************************************************/
|
---|
| 740 | /* MeshInstance implementation */
|
---|
| 741 | /********************************************************/
|
---|
| 742 |
|
---|
| 743 | int
|
---|
| 744 | MeshInstance::CastRay(
|
---|
| 745 | Ray &ray
|
---|
| 746 | )
|
---|
| 747 | {
|
---|
| 748 | int res = mMesh->CastRay(ray, this);
|
---|
| 749 | return res;
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | int
|
---|
| 753 | MeshInstance::CastRay(
|
---|
| 754 | Ray &ray,
|
---|
| 755 | const vector<int> &faces
|
---|
| 756 | )
|
---|
| 757 | {
|
---|
| 758 | return mMesh->CastRayToSelectedFaces(ray, faces, this);
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 |
|
---|
| 762 |
|
---|
| 763 | int
|
---|
| 764 | MeshInstance::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 765 | {
|
---|
| 766 | return mMesh->GetRandomSurfacePoint(point, normal);
|
---|
| 767 | }
|
---|
| 768 |
|
---|
| 769 | int
|
---|
| 770 | MeshInstance::GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 771 | Vector3 &normal,
|
---|
| 772 | const Vector3 &viewpoint,
|
---|
| 773 | const int maxTries)
|
---|
| 774 | {
|
---|
| 775 | return mMesh->GetRandomVisibleSurfacePoint(point, normal, viewpoint, maxTries);
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 |
|
---|
| 779 | void MeshInstance::SetMaterial(Material *mat)
|
---|
| 780 | {
|
---|
| 781 | mMaterial = mat;
|
---|
| 782 | }
|
---|
| 783 |
|
---|
| 784 | Material *MeshInstance::GetMaterial() const
|
---|
| 785 | {
|
---|
| 786 | return mMaterial;
|
---|
| 787 | }
|
---|
| 788 |
|
---|
| 789 |
|
---|
[1344] | 790 | Vector3 MeshInstance::GetNormal(const int idx) const
|
---|
| 791 | {
|
---|
[1418] | 792 | return mMesh->GetNormal(idx);
|
---|
[1344] | 793 | }
|
---|
| 794 |
|
---|
| 795 |
|
---|
| 796 |
|
---|
[1001] | 797 | /*************************************************************/
|
---|
[1002] | 798 | /* TransformedMeshInstance implementation */
|
---|
[1001] | 799 | /*************************************************************/
|
---|
| 800 |
|
---|
[1344] | 801 |
|
---|
[1001] | 802 | TransformedMeshInstance::TransformedMeshInstance(Mesh *mesh):
|
---|
| 803 | MeshInstance(mesh)
|
---|
| 804 | {
|
---|
| 805 | mWorldTransform = IdentityMatrix();
|
---|
| 806 | }
|
---|
| 807 |
|
---|
| 808 |
|
---|
| 809 | int TransformedMeshInstance::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 810 | {
|
---|
| 811 | int index = mMesh->GetRandomSurfacePoint(point, normal);
|
---|
| 812 | point = mWorldTransform*point;
|
---|
| 813 | normal = TransformNormal(mWorldTransform, normal);
|
---|
| 814 | return index;
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 |
|
---|
| 818 | int TransformedMeshInstance::CastRay(Ray &ray)
|
---|
| 819 | {
|
---|
[1004] | 820 | ray.ApplyTransform(Invert(mWorldTransform));
|
---|
| 821 |
|
---|
| 822 | const int res = mMesh->CastRay(ray, this);
|
---|
| 823 | ray.ApplyTransform(mWorldTransform);
|
---|
| 824 |
|
---|
| 825 | return res;
|
---|
[863] | 826 | }
|
---|
| 827 |
|
---|
[1344] | 828 |
|
---|
[1004] | 829 | int TransformedMeshInstance::CastRay(Ray &ray, const vector<int> &faces)
|
---|
| 830 | {
|
---|
| 831 | ray.ApplyTransform(Invert(mWorldTransform));
|
---|
[1001] | 832 |
|
---|
[1004] | 833 | const int res = mMesh->CastRayToSelectedFaces(ray, faces, this);
|
---|
| 834 | ray.ApplyTransform(mWorldTransform);
|
---|
| 835 |
|
---|
| 836 | return res;
|
---|
| 837 | }
|
---|
| 838 |
|
---|
| 839 |
|
---|
[1001] | 840 | void TransformedMeshInstance::ApplyWorldTransform(const Matrix4x4 &m)
|
---|
| 841 | {
|
---|
| 842 | mWorldTransform = m * mWorldTransform;
|
---|
[878] | 843 | }
|
---|
[1001] | 844 |
|
---|
| 845 |
|
---|
| 846 | void TransformedMeshInstance::LoadWorldTransform(const Matrix4x4 &m)
|
---|
| 847 | {
|
---|
| 848 | mWorldTransform = m;
|
---|
| 849 | }
|
---|
| 850 |
|
---|
| 851 |
|
---|
[1020] | 852 | void TransformedMeshInstance::GetWorldTransform(Matrix4x4 &m) const
|
---|
[1001] | 853 | {
|
---|
| 854 | m = mWorldTransform;
|
---|
| 855 | }
|
---|
| 856 |
|
---|
| 857 |
|
---|
[1020] | 858 | AxisAlignedBox3 TransformedMeshInstance::GetBox() const
|
---|
[1001] | 859 | {
|
---|
| 860 | return Transform(mMesh->mBox, mWorldTransform);
|
---|
| 861 | }
|
---|
| 862 |
|
---|
[1020] | 863 |
|
---|
| 864 | void TransformedMeshInstance::GetTransformedMesh(Mesh &transformedMesh) const
|
---|
| 865 | {
|
---|
| 866 | // copy mesh
|
---|
[1002] | 867 | transformedMesh = *mMesh;
|
---|
| 868 | transformedMesh.ApplyTransformation(mWorldTransform);
|
---|
[1020] | 869 | }
|
---|
[1001] | 870 |
|
---|
[1328] | 871 |
|
---|
[1344] | 872 | Vector3 TransformedMeshInstance::GetNormal(const int idx) const
|
---|
| 873 | {
|
---|
| 874 | Mesh mesh;
|
---|
| 875 | GetTransformedMesh(mesh);
|
---|
| 876 | return mesh.GetFacePlane(idx).mNormal;
|
---|
[1001] | 877 | }
|
---|
[1344] | 878 |
|
---|
| 879 | }
|
---|