[167] | 1 | #include "Ray.h"
|
---|
[162] | 2 | #include "Mesh.h"
|
---|
[170] | 3 | #include "MeshKdTree.h"
|
---|
[191] | 4 | #include "Triangle3.h"
|
---|
[162] | 5 |
|
---|
[863] | 6 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 7 |
|
---|
[878] | 8 | bool MeshDebug = false;
|
---|
[860] | 9 |
|
---|
[339] | 10 | int Intersectable::sMailId = 21843194198;
|
---|
[382] | 11 | int Intersectable::sReservedMailboxes = 1;
|
---|
[162] | 12 |
|
---|
[752] | 13 | struct SortableVertex {
|
---|
| 14 |
|
---|
| 15 | Vector3 vertex;
|
---|
| 16 |
|
---|
| 17 | int originalId;
|
---|
| 18 | int newId;
|
---|
| 19 | int finalPos;
|
---|
| 20 |
|
---|
| 21 | SortableVertex() {}
|
---|
| 22 |
|
---|
| 23 | SortableVertex(const Vector3 &v,
|
---|
| 24 | const int id):
|
---|
| 25 | vertex(v),
|
---|
| 26 | originalId(id),
|
---|
| 27 | newId(id)
|
---|
| 28 | {}
|
---|
| 29 |
|
---|
| 30 | friend bool operator<(const SortableVertex &a,
|
---|
| 31 | const SortableVertex &b)
|
---|
| 32 | {
|
---|
| 33 | if (a.vertex.x < b.vertex.x)
|
---|
| 34 | return true;
|
---|
| 35 | else
|
---|
| 36 | if (a.vertex.x > b.vertex.x)
|
---|
| 37 | return false;
|
---|
| 38 |
|
---|
| 39 | if (a.vertex.y < b.vertex.y)
|
---|
| 40 | return true;
|
---|
| 41 | else
|
---|
| 42 | if (a.vertex.y > b.vertex.y)
|
---|
| 43 | return false;
|
---|
| 44 |
|
---|
| 45 | if (a.vertex.z < b.vertex.z)
|
---|
| 46 | return true;
|
---|
| 47 | else
|
---|
| 48 | // if (a.z > b.z)
|
---|
| 49 | return false;
|
---|
| 50 |
|
---|
| 51 | // return false;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | };
|
---|
| 55 |
|
---|
[863] | 56 |
|
---|
[162] | 57 | void
|
---|
[859] | 58 | Mesh::ComputeBoundingBox()
|
---|
[863] | 59 | {
|
---|
| 60 |
|
---|
[752] | 61 | mBox.Initialize();
|
---|
[162] | 62 | VertexContainer::const_iterator vi = mVertices.begin();
|
---|
| 63 | for (; vi != mVertices.end(); vi++) {
|
---|
[176] | 64 | mBox.Include(*vi);
|
---|
[162] | 65 | }
|
---|
[873] | 66 | //mBox.Enlarge(1e-4f);
|
---|
[863] | 67 | }
|
---|
| 68 |
|
---|
[859] | 69 | void
|
---|
| 70 | Mesh::Preprocess()
|
---|
| 71 | {
|
---|
[870] | 72 | Cleanup();
|
---|
[859] | 73 |
|
---|
[870] | 74 | ComputeBoundingBox();
|
---|
[859] | 75 |
|
---|
[870] | 76 | /** true if it is a watertight convex mesh
|
---|
| 77 | */
|
---|
| 78 | mIsConvex = false;
|
---|
| 79 |
|
---|
| 80 | if (mFaces.size() > MeshKdTree::mTermMinCost)
|
---|
| 81 | {
|
---|
| 82 | mKdTree = new MeshKdTree(this);
|
---|
| 83 | MeshKdLeaf *root = (MeshKdLeaf *)mKdTree->GetRoot();
|
---|
| 84 |
|
---|
| 85 | for (int i = 0; i < mFaces.size(); i++)
|
---|
| 86 | root->mFaces.push_back(i);
|
---|
| 87 |
|
---|
| 88 | cout<<"KD";
|
---|
| 89 | mKdTree->Construct();
|
---|
| 90 |
|
---|
| 91 | if (mKdTree->GetRoot()->IsLeaf())
|
---|
| 92 | {
|
---|
| 93 | cout<<"d";
|
---|
| 94 | delete mKdTree;
|
---|
| 95 | mKdTree = NULL;
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
[162] | 98 | }
|
---|
| 99 |
|
---|
[752] | 100 |
|
---|
| 101 | void
|
---|
| 102 | Mesh::IndexVertices()
|
---|
| 103 | {
|
---|
| 104 | int i;
|
---|
| 105 | // check whether the vertices can be simplfied and reindexed
|
---|
| 106 | vector<SortableVertex> svertices(mVertices.size());
|
---|
| 107 |
|
---|
| 108 | for (i=0; i < mVertices.size(); i++)
|
---|
| 109 | svertices[i] = SortableVertex(mVertices[i], i);
|
---|
| 110 |
|
---|
| 111 | sort(svertices.begin(), svertices.end());
|
---|
| 112 |
|
---|
| 113 | for (i=0; i < svertices.size() - 1; i++)
|
---|
| 114 | if (svertices[i].vertex == svertices[i+1].vertex)
|
---|
| 115 | svertices[i+1].newId = svertices[i].newId;
|
---|
| 116 |
|
---|
| 117 | // remove the same vertices
|
---|
| 118 | int k = 0;
|
---|
| 119 | mVertices[0] = svertices[0].vertex;
|
---|
| 120 | svertices[0].finalPos = 0;
|
---|
| 121 |
|
---|
| 122 | for (i=1; i < svertices.size(); i++) {
|
---|
| 123 | if (svertices[i].newId != svertices[i-1].newId)
|
---|
| 124 | k++;
|
---|
| 125 |
|
---|
| 126 | mVertices[k] = svertices[i].vertex;
|
---|
| 127 | svertices[i].finalPos = k;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | mVertices.resize(k + 1);
|
---|
| 131 |
|
---|
| 132 | vector<int> remapBuffer(svertices.size());
|
---|
| 133 | for (i = 0; i < svertices.size(); i++)
|
---|
| 134 | remapBuffer[svertices[i].originalId] = svertices[i].finalPos;
|
---|
| 135 |
|
---|
| 136 | // remap all faces
|
---|
| 137 |
|
---|
| 138 | for (int faceIndex = 0; faceIndex < mFaces.size(); faceIndex++) {
|
---|
| 139 | Face *face = mFaces[faceIndex];
|
---|
| 140 | for (int i = 0; i < face->mVertexIndices.size(); i++) {
|
---|
| 141 | face->mVertexIndices[i] = remapBuffer[face->mVertexIndices[i]];
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[170] | 146 | AxisAlignedBox3
|
---|
| 147 | Mesh::GetFaceBox(const int faceIndex)
|
---|
| 148 | {
|
---|
| 149 | Face *face = mFaces[faceIndex];
|
---|
| 150 | AxisAlignedBox3 box;
|
---|
| 151 | box.SetMin( mVertices[face->mVertexIndices[0]] );
|
---|
| 152 | box.SetMax(box.Min());
|
---|
| 153 | for (int i = 1; i < face->mVertexIndices.size(); i++) {
|
---|
| 154 | box.Include(mVertices[face->mVertexIndices[i]]);
|
---|
| 155 | }
|
---|
| 156 | return box;
|
---|
| 157 | }
|
---|
[162] | 158 |
|
---|
| 159 | int
|
---|
[170] | 160 | Mesh::CastRayToFace(
|
---|
[878] | 161 | const int faceIndex,
|
---|
| 162 | Ray &ray,
|
---|
| 163 | float &nearestT,
|
---|
| 164 | int &nearestFace,
|
---|
| 165 | Intersectable *instance
|
---|
| 166 | )
|
---|
[170] | 167 | {
|
---|
| 168 | float t;
|
---|
| 169 | int hit = 0;
|
---|
| 170 | if (RayFaceIntersection(faceIndex, ray, t, nearestT) == Ray::INTERSECTION) {
|
---|
| 171 | switch (ray.GetType()) {
|
---|
| 172 | case Ray::GLOBAL_RAY:
|
---|
[191] | 173 | ray.intersections.push_back(Ray::Intersection(t, instance, faceIndex));
|
---|
[170] | 174 | hit++;
|
---|
| 175 | break;
|
---|
| 176 | case Ray::LOCAL_RAY:
|
---|
| 177 | nearestT = t;
|
---|
| 178 | nearestFace = faceIndex;
|
---|
| 179 | hit++;
|
---|
| 180 | break;
|
---|
[245] | 181 | case Ray::LINE_SEGMENT:
|
---|
| 182 | if (t <= 1.0f) {
|
---|
[878] | 183 | ray.intersections.push_back(Ray::Intersection(t, instance, faceIndex));
|
---|
| 184 | hit++;
|
---|
[245] | 185 | }
|
---|
| 186 | break;
|
---|
[170] | 187 | }
|
---|
| 188 | }
|
---|
| 189 | return hit;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | int
|
---|
[162] | 193 | Mesh::CastRay(
|
---|
[444] | 194 | Ray &ray,
|
---|
| 195 | MeshInstance *instance
|
---|
| 196 | )
|
---|
[162] | 197 | {
|
---|
[170] | 198 | if (mKdTree) {
|
---|
| 199 | return mKdTree->CastRay(ray, instance);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[162] | 202 | int faceIndex = 0;
|
---|
| 203 | int hits = 0;
|
---|
| 204 | float nearestT = MAX_FLOAT;
|
---|
[170] | 205 | int nearestFace = -1;
|
---|
| 206 |
|
---|
[162] | 207 | if (ray.GetType() == Ray::LOCAL_RAY && ray.intersections.size())
|
---|
| 208 | nearestT = ray.intersections[0].mT;
|
---|
| 209 |
|
---|
[376] | 210 |
|
---|
[162] | 211 | for ( ;
|
---|
[492] | 212 | faceIndex < mFaces.size();
|
---|
| 213 | faceIndex++) {
|
---|
[170] | 214 | hits += CastRayToFace(faceIndex, ray, nearestT, nearestFace, instance);
|
---|
| 215 | if (mIsConvex && nearestFace != -1)
|
---|
| 216 | break;
|
---|
[162] | 217 | }
|
---|
| 218 |
|
---|
| 219 | if ( hits && ray.GetType() == Ray::LOCAL_RAY ) {
|
---|
| 220 | if (ray.intersections.size())
|
---|
[191] | 221 | ray.intersections[0] = Ray::Intersection(nearestT, instance, nearestFace);
|
---|
[162] | 222 | else
|
---|
[191] | 223 | ray.intersections.push_back(Ray::Intersection(nearestT, instance, nearestFace));
|
---|
[162] | 224 | }
|
---|
| 225 |
|
---|
| 226 | return hits;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[170] | 229 | int
|
---|
| 230 | Mesh::CastRayToSelectedFaces(
|
---|
[492] | 231 | Ray &ray,
|
---|
| 232 | const vector<int> &faces,
|
---|
| 233 | Intersectable *instance
|
---|
| 234 | )
|
---|
[170] | 235 | {
|
---|
| 236 | vector<int>::const_iterator fi;
|
---|
| 237 | int faceIndex = 0;
|
---|
| 238 | int hits = 0;
|
---|
| 239 | float nearestT = MAX_FLOAT;
|
---|
| 240 | int nearestFace = -1;
|
---|
[162] | 241 |
|
---|
[170] | 242 | if (ray.GetType() == Ray::LOCAL_RAY && ray.intersections.size())
|
---|
| 243 | nearestT = ray.intersections[0].mT;
|
---|
| 244 |
|
---|
| 245 | for ( fi = faces.begin();
|
---|
[878] | 246 | fi != faces.end();
|
---|
| 247 | fi++) {
|
---|
[170] | 248 | hits += CastRayToFace(*fi, ray, nearestT, nearestFace, instance);
|
---|
| 249 | if (mIsConvex && nearestFace != -1)
|
---|
| 250 | break;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | if ( hits && ray.GetType() == Ray::LOCAL_RAY ) {
|
---|
| 254 | if (ray.intersections.size())
|
---|
[191] | 255 | ray.intersections[0] = Ray::Intersection(nearestT, instance, nearestFace);
|
---|
[170] | 256 | else
|
---|
[191] | 257 | ray.intersections.push_back(Ray::Intersection(nearestT, instance, nearestFace));
|
---|
[170] | 258 | }
|
---|
| 259 |
|
---|
| 260 | return hits;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 |
|
---|
[162] | 264 | // int_lineseg returns 1 if the given line segment intersects a 2D
|
---|
| 265 | // ray travelling in the positive X direction. This is used in the
|
---|
| 266 | // Jordan curve computation for polygon intersection.
|
---|
| 267 | inline int
|
---|
| 268 | int_lineseg(float px,
|
---|
[492] | 269 | float py,
|
---|
| 270 | float u1,
|
---|
| 271 | float v1,
|
---|
| 272 | float u2,
|
---|
| 273 | float v2)
|
---|
[162] | 274 | {
|
---|
| 275 | float ydiff;
|
---|
| 276 |
|
---|
| 277 | u1 -= px; u2 -= px; // translate line
|
---|
| 278 | v1 -= py; v2 -= py;
|
---|
| 279 |
|
---|
| 280 | if ((v1 > 0 && v2 > 0) ||
|
---|
| 281 | (v1 < 0 && v2 < 0) ||
|
---|
| 282 | (u1 < 0 && u2 < 0))
|
---|
| 283 | return 0;
|
---|
| 284 |
|
---|
| 285 | if (u1 > 0 && u2 > 0)
|
---|
| 286 | return 1;
|
---|
| 287 |
|
---|
| 288 | ydiff = v2 - v1;
|
---|
| 289 | if (fabs(ydiff) < Limits::Small) { // denominator near 0
|
---|
| 290 | if (((fabs(v1) > Limits::Small) ||
|
---|
[492] | 291 | (u1 > 0) || (u2 > 0)))
|
---|
[162] | 292 | return 0;
|
---|
| 293 | return 1;
|
---|
| 294 | }
|
---|
[492] | 295 |
|
---|
| 296 | double t = -v1 / ydiff; // Compute parameter
|
---|
[162] | 297 |
|
---|
[492] | 298 | double thresh;
|
---|
[878] | 299 |
|
---|
[492] | 300 | if (ydiff < 0.0f)
|
---|
[878] | 301 | thresh = -1e-20;
|
---|
[492] | 302 | else
|
---|
[878] | 303 | thresh = 1e-20;
|
---|
[162] | 304 |
|
---|
[492] | 305 |
|
---|
| 306 | return (u1 + t * (u2 - u1)) > thresh; //-Limits::Small;
|
---|
[162] | 307 | }
|
---|
| 308 |
|
---|
| 309 |
|
---|
| 310 |
|
---|
| 311 | // intersection with the polygonal face of the mesh
|
---|
| 312 | int
|
---|
| 313 | Mesh::RayFaceIntersection(const int faceIndex,
|
---|
[492] | 314 | const Ray &ray,
|
---|
| 315 | float &t,
|
---|
| 316 | const float nearestT
|
---|
| 317 | )
|
---|
[162] | 318 | {
|
---|
| 319 | Face *face = mFaces[faceIndex];
|
---|
| 320 |
|
---|
| 321 | Plane3 plane = GetFacePlane(faceIndex);
|
---|
| 322 | float dot = DotProd(plane.mNormal, ray.GetDir());
|
---|
[878] | 323 |
|
---|
| 324 | if (MeshDebug) {
|
---|
| 325 | cout<<endl<<endl;
|
---|
| 326 | cout<<"normal="<<plane.mNormal<<endl;
|
---|
| 327 | cout<<"dot="<<dot<<endl;
|
---|
| 328 | }
|
---|
[162] | 329 |
|
---|
[878] | 330 |
|
---|
[162] | 331 | // Watch for near-zero denominator
|
---|
| 332 | // ONLY single sided polygons!!!!!
|
---|
[534] | 333 | if (ray.mFlags & Ray::CULL_BACKFACES) {
|
---|
| 334 | if (dot > -Limits::Small)
|
---|
| 335 | // if (fabs(dot) < Limits::Small)
|
---|
| 336 | return Ray::NO_INTERSECTION;
|
---|
| 337 | } else {
|
---|
| 338 | if (fabs(dot) < Limits::Small)
|
---|
| 339 | return Ray::NO_INTERSECTION;
|
---|
| 340 | }
|
---|
[162] | 341 |
|
---|
| 342 | t = (-plane.mD - DotProd(plane.mNormal, ray.GetLoc())) / dot;
|
---|
[878] | 343 |
|
---|
| 344 | if (MeshDebug)
|
---|
| 345 | cout<<"t="<<t<<endl;
|
---|
| 346 |
|
---|
[162] | 347 | if (t <= Limits::Small)
|
---|
| 348 | return Ray::INTERSECTION_OUT_OF_LIMITS;
|
---|
| 349 |
|
---|
| 350 | if (t >= nearestT) {
|
---|
| 351 | return Ray::INTERSECTION_OUT_OF_LIMITS; // no intersection was found
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | int count = 0;
|
---|
| 355 | float u, v, u1, v1, u2, v2;
|
---|
| 356 | int i;
|
---|
| 357 |
|
---|
| 358 | int paxis = plane.mNormal.DrivingAxis();
|
---|
| 359 |
|
---|
[878] | 360 |
|
---|
[162] | 361 | // Project the intersection point onto the coordinate plane
|
---|
| 362 | // specified by which.
|
---|
| 363 | ray.Extrap(t).ExtractVerts(&u, &v, paxis);
|
---|
| 364 |
|
---|
| 365 |
|
---|
[469] | 366 | int size = (int)face->mVertexIndices.size();
|
---|
[170] | 367 |
|
---|
[878] | 368 | if (MeshDebug)
|
---|
| 369 | cout<<"size="<<size<<endl;
|
---|
| 370 |
|
---|
[170] | 371 | mVertices[face->mVertexIndices[size - 1]].
|
---|
[162] | 372 | ExtractVerts(&u1, &v1, paxis );
|
---|
[170] | 373 |
|
---|
[752] | 374 | //$$JB changed 12.4.2006 from 0 ^^
|
---|
[170] | 375 | if (0 && size <= 4) {
|
---|
[162] | 376 | // assume a convex face
|
---|
[170] | 377 | for (i = 0; i < size; i++) {
|
---|
[162] | 378 | mVertices[face->mVertexIndices[i]].ExtractVerts(&u2, &v2, paxis);
|
---|
| 379 | // line u1, v1, u2, v2
|
---|
[878] | 380 | if ((v1 - v2)*(u - u1) + (u2 - u1)*(v - v1) > 0) {
|
---|
| 381 | if (MeshDebug)
|
---|
| 382 | cout<<"exit on "<<i<<endl;
|
---|
[492] | 383 | return Ray::NO_INTERSECTION;
|
---|
[878] | 384 | }
|
---|
[162] | 385 | u1 = u2;
|
---|
| 386 | v1 = v2;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | return Ray::INTERSECTION;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | // We're stuck with the Jordan curve computation. Count number
|
---|
| 393 | // of intersections between the line segments the polygon comprises
|
---|
| 394 | // with a ray originating at the point of intersection and
|
---|
| 395 | // travelling in the positive X direction.
|
---|
[170] | 396 | for (i = 0; i < size; i++) {
|
---|
[162] | 397 | mVertices[face->mVertexIndices[i]].ExtractVerts(&u2, &v2, paxis);
|
---|
| 398 | count += (int_lineseg(u, v, u1, v1, u2, v2) != 0);
|
---|
| 399 | u1 = u2;
|
---|
| 400 | v1 = v2;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[878] | 403 | if (MeshDebug)
|
---|
| 404 | cout<<"count="<<count<<endl;
|
---|
| 405 |
|
---|
[162] | 406 | // We hit polygon if number of intersections is odd.
|
---|
| 407 | return (count & 1) ? Ray::INTERSECTION : Ray::NO_INTERSECTION;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[349] | 410 | int
|
---|
[176] | 411 | Mesh::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 412 | {
|
---|
[485] | 413 | int faceIndex = (int)RandomValue(0, (Real)((int)mFaces.size()-1));
|
---|
[176] | 414 |
|
---|
| 415 | // assume the face is convex and generate a convex combination
|
---|
| 416 | //
|
---|
| 417 | Face *face = mFaces[faceIndex];
|
---|
| 418 | point = Vector3(0,0,0);
|
---|
| 419 | float sum = 0.0f;
|
---|
| 420 | for (int i = 0; i < face->mVertexIndices.size(); i++) {
|
---|
| 421 | float r = RandomValue(0,1);
|
---|
| 422 | sum += r;
|
---|
| 423 | point += mVertices[face->mVertexIndices[i]]*r;
|
---|
| 424 | }
|
---|
| 425 | point *= 1.0f/sum;
|
---|
[340] | 426 |
|
---|
| 427 | normal = GetFacePlane(faceIndex).mNormal;
|
---|
[349] | 428 |
|
---|
| 429 | return faceIndex;
|
---|
[176] | 430 | }
|
---|
| 431 |
|
---|
[162] | 432 | int
|
---|
[354] | 433 | Mesh::GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 434 | Vector3 &normal,
|
---|
| 435 | const Vector3 &viewpoint,
|
---|
| 436 | const int maxTries
|
---|
| 437 | )
|
---|
| 438 | {
|
---|
[359] | 439 | Plane3 plane;
|
---|
[485] | 440 | int faceIndex = (int)RandomValue(0, (Real)((int)mFaces.size()-1));
|
---|
[359] | 441 | int tries;
|
---|
| 442 | for (tries = 0; tries < maxTries; tries++) {
|
---|
| 443 | Face *face = mFaces[faceIndex];
|
---|
| 444 | plane = GetFacePlane(faceIndex);
|
---|
| 445 |
|
---|
| 446 | if (plane.Side(viewpoint) > 0) {
|
---|
| 447 | point = Vector3(0,0,0);
|
---|
| 448 | float sum = 0.0f;
|
---|
| 449 | // pickup a point inside this triangle
|
---|
| 450 | for (int i = 0; i < face->mVertexIndices.size(); i++) {
|
---|
[354] | 451 | float r = RandomValue(0,1);
|
---|
| 452 | sum += r;
|
---|
| 453 | point += mVertices[face->mVertexIndices[i]]*r;
|
---|
[359] | 454 | }
|
---|
| 455 | point *= 1.0f/sum;
|
---|
| 456 | break;
|
---|
| 457 | }
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | normal = plane.mNormal;
|
---|
| 461 | return (tries < maxTries) ? faceIndex + 1 : 0;
|
---|
[354] | 462 | }
|
---|
| 463 |
|
---|
| 464 |
|
---|
| 465 | int
|
---|
[162] | 466 | MeshInstance::CastRay(
|
---|
[444] | 467 | Ray &ray
|
---|
| 468 | )
|
---|
[162] | 469 | {
|
---|
| 470 | int res = mMesh->CastRay(ray, this);
|
---|
| 471 | return res;
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[170] | 474 | int
|
---|
| 475 | MeshInstance::CastRay(
|
---|
[444] | 476 | Ray &ray,
|
---|
| 477 | const vector<int> &faces
|
---|
| 478 | )
|
---|
[170] | 479 | {
|
---|
| 480 | return mMesh->CastRayToSelectedFaces(ray, faces, this);
|
---|
| 481 | }
|
---|
[162] | 482 |
|
---|
[170] | 483 |
|
---|
[176] | 484 |
|
---|
[349] | 485 | int
|
---|
[176] | 486 | MeshInstance::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 487 | {
|
---|
[349] | 488 | return mMesh->GetRandomSurfacePoint(point, normal);
|
---|
[176] | 489 | }
|
---|
| 490 |
|
---|
[349] | 491 | int
|
---|
[354] | 492 | MeshInstance::GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 493 | Vector3 &normal,
|
---|
| 494 | const Vector3 &viewpoint,
|
---|
| 495 | const int maxTries
|
---|
| 496 | )
|
---|
| 497 | {
|
---|
| 498 | return mMesh->GetRandomVisibleSurfacePoint(point, normal, viewpoint, maxTries);
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 |
|
---|
| 502 | int
|
---|
[176] | 503 | TransformedMeshInstance::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 504 | {
|
---|
[349] | 505 | int index = mMesh->GetRandomSurfacePoint(point, normal);
|
---|
[176] | 506 | point = mWorldTransform*point;
|
---|
| 507 | normal = TransformNormal(mWorldTransform, normal);
|
---|
[349] | 508 | return index;
|
---|
[176] | 509 | }
|
---|
| 510 |
|
---|
[162] | 511 | Plane3
|
---|
| 512 | Mesh::GetFacePlane(const int faceIndex)
|
---|
| 513 | {
|
---|
| 514 | Face *face = mFaces[faceIndex];
|
---|
[340] | 515 | return Plane3(mVertices[face->mVertexIndices[0]],
|
---|
[333] | 516 | mVertices[face->mVertexIndices[1]],
|
---|
| 517 | mVertices[face->mVertexIndices[2]]);
|
---|
[162] | 518 | }
|
---|
| 519 |
|
---|
[340] | 520 | bool
|
---|
| 521 | Mesh::ValidateFace(const int i)
|
---|
| 522 | {
|
---|
| 523 | Face *face = mFaces[i];
|
---|
| 524 |
|
---|
| 525 | Plane3 plane = Plane3(mVertices[face->mVertexIndices[0]],
|
---|
| 526 | mVertices[face->mVertexIndices[1]],
|
---|
| 527 | mVertices[face->mVertexIndices[2]]);
|
---|
| 528 |
|
---|
| 529 | if (!eq(Magnitude(plane.mNormal), 1.0f))
|
---|
| 530 | return false;
|
---|
[350] | 531 |
|
---|
| 532 | return true;
|
---|
[340] | 533 | }
|
---|
| 534 |
|
---|
| 535 | void
|
---|
| 536 | Mesh::Cleanup()
|
---|
| 537 | {
|
---|
| 538 | int toRemove = 0;
|
---|
| 539 | FaceContainer newFaces;
|
---|
| 540 | for (int i=0; i < mFaces.size(); i++)
|
---|
| 541 | if (ValidateFace(i)) {
|
---|
| 542 | newFaces.push_back(mFaces[i]);
|
---|
| 543 | } else {
|
---|
| 544 | cout<<"d";
|
---|
| 545 | delete mFaces[i];
|
---|
| 546 | toRemove++;
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | if (toRemove) {
|
---|
| 550 | mFaces = newFaces;
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 | // cleanup vertices??
|
---|
| 554 | }
|
---|
| 555 |
|
---|
[162] | 556 | int
|
---|
[176] | 557 | TransformedMeshInstance::CastRay(
|
---|
[492] | 558 | Ray &ray
|
---|
| 559 | )
|
---|
[162] | 560 | {
|
---|
| 561 | ray.ApplyTransform(Invert(mWorldTransform));
|
---|
| 562 | int res = mMesh->CastRay(ray, this);
|
---|
| 563 | ray.ApplyTransform(mWorldTransform);
|
---|
| 564 |
|
---|
| 565 | return res;
|
---|
| 566 | }
|
---|
[170] | 567 |
|
---|
[209] | 568 |
|
---|
[191] | 569 | void
|
---|
| 570 | Mesh::AddTriangle(const Triangle3 &triangle)
|
---|
| 571 | {
|
---|
[469] | 572 | int index = (int)mVertices.size();
|
---|
[191] | 573 |
|
---|
| 574 | for (int i=0; i < 3; i++) {
|
---|
| 575 | mVertices.push_back(triangle.mVertices[i]);
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | AddFace(new Face(index + 0, index + 1, index + 2) );
|
---|
| 579 | }
|
---|
[209] | 580 |
|
---|
| 581 | void
|
---|
| 582 | Mesh::AddRectangle(const Rectangle3 &rect)
|
---|
| 583 | {
|
---|
[469] | 584 | int index = (int)mVertices.size();
|
---|
[209] | 585 |
|
---|
| 586 | for (int i=0; i < 4; i++) {
|
---|
| 587 | mVertices.push_back(rect.mVertices[i]);
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 | AddFace(new Face(index + 0, index + 1, index + 2, index + 3) );
|
---|
| 591 | }
|
---|
[750] | 592 |
|
---|
[752] | 593 | void
|
---|
| 594 | Mesh::AssignRandomMaterial()
|
---|
| 595 | {
|
---|
| 596 | if (!mMaterial)
|
---|
| 597 | mMaterial = new Material;
|
---|
| 598 | *mMaterial = RandomMaterial();
|
---|
[750] | 599 |
|
---|
[752] | 600 | }
|
---|
[750] | 601 |
|
---|
[752] | 602 |
|
---|
[750] | 603 | Mesh *CreateBox(const AxisAlignedBox3 &box)
|
---|
| 604 | {
|
---|
| 605 | Mesh *mesh = new Mesh;
|
---|
| 606 | // add 8 vertices of the box
|
---|
| 607 | int index = (int)mesh->mVertices.size();
|
---|
| 608 | for (int i=0; i < 8; i++) {
|
---|
| 609 | Vector3 v;
|
---|
| 610 | box.GetVertex(i, v);
|
---|
| 611 | mesh->mVertices.push_back(v);
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 615 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 616 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 617 |
|
---|
| 618 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 619 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 620 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 621 |
|
---|
| 622 | return mesh;
|
---|
[863] | 623 | }
|
---|
| 624 |
|
---|
[878] | 625 | }
|
---|