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