[235] | 1 | #include "Polygon3.h"
|
---|
| 2 | #include "Mesh.h"
|
---|
[372] | 3 | #include "AxisAlignedBox3.h"
|
---|
| 4 | #include "Ray.h"
|
---|
[503] | 5 | #include "Triangle3.h"
|
---|
[372] | 6 |
|
---|
[860] | 7 |
|
---|
[863] | 8 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 9 |
|
---|
| 10 |
|
---|
[372] | 11 | Polygon3::Polygon3():
|
---|
[1344] | 12 | mMaterial(NULL),
|
---|
| 13 | mParent(NULL),
|
---|
| 14 | mPiercingRays(0)
|
---|
[1076] | 15 | , mPlane(NULL)
|
---|
[574] | 16 | {
|
---|
| 17 | }
|
---|
[372] | 18 |
|
---|
| 19 | Polygon3::Polygon3(const VertexContainer &vertices):
|
---|
[1344] | 20 | mMaterial(NULL),
|
---|
| 21 | mParent(NULL),
|
---|
| 22 | mPiercingRays(0)
|
---|
[1076] | 23 | , mPlane(NULL)
|
---|
[574] | 24 | {
|
---|
| 25 | mVertices.reserve(vertices.size());
|
---|
| 26 | mVertices = vertices;
|
---|
| 27 | }
|
---|
[372] | 28 |
|
---|
[574] | 29 |
|
---|
[372] | 30 | Polygon3::Polygon3(MeshInstance *parent):
|
---|
[1344] | 31 | mMaterial(NULL),
|
---|
| 32 | mParent(parent),
|
---|
| 33 | mPiercingRays(0)
|
---|
[1076] | 34 | , mPlane(NULL)
|
---|
[372] | 35 | {}
|
---|
| 36 |
|
---|
[574] | 37 |
|
---|
[372] | 38 | Polygon3::Polygon3(Face *face, Mesh *parentMesh):
|
---|
[1344] | 39 | mMaterial(NULL),
|
---|
| 40 | mParent(NULL),
|
---|
| 41 | mPiercingRays(0),
|
---|
| 42 | mPlane(NULL)
|
---|
[372] | 43 | {
|
---|
[574] | 44 | mVertices.reserve(face->mVertexIndices.size());
|
---|
| 45 |
|
---|
[1344] | 46 | VertexIndexContainer::iterator it, it_end = face->mVertexIndices.end();
|
---|
| 47 |
|
---|
| 48 | for (it = face->mVertexIndices.begin(); it != it_end; ++ it)
|
---|
[372] | 49 | {
|
---|
| 50 | mVertices.push_back(parentMesh->mVertices[*it]);
|
---|
| 51 | }
|
---|
[574] | 52 |
|
---|
| 53 | mMaterial = parentMesh->mMaterial;
|
---|
[372] | 54 | }
|
---|
| 55 |
|
---|
[574] | 56 |
|
---|
[1076] | 57 | Plane3 Polygon3::GetSupportingPlane()// const
|
---|
[372] | 58 | {
|
---|
[1076] | 59 | #if 0
|
---|
[372] | 60 | return Plane3(mVertices[0], mVertices[1], mVertices[2]);
|
---|
[1076] | 61 | #else
|
---|
| 62 | if (!mPlane)
|
---|
[1344] | 63 | {
|
---|
[1076] | 64 | mPlane = new Plane3(mVertices[0], mVertices[1], mVertices[2]);
|
---|
[1344] | 65 | }
|
---|
| 66 |
|
---|
[1076] | 67 | return *mPlane;
|
---|
| 68 | #endif
|
---|
[372] | 69 | }
|
---|
| 70 |
|
---|
[574] | 71 |
|
---|
[306] | 72 | Vector3 Polygon3::GetNormal() const
|
---|
| 73 | {
|
---|
[321] | 74 | return Normalize(CrossProd(mVertices[2] - mVertices[1],
|
---|
| 75 | mVertices[0] - mVertices[1]));
|
---|
[372] | 76 | }
|
---|
| 77 |
|
---|
[1344] | 78 |
|
---|
[372] | 79 | void Polygon3::Split(const Plane3 &partition,
|
---|
| 80 | Polygon3 &front,
|
---|
[448] | 81 | Polygon3 &back,
|
---|
| 82 | const float epsilon)
|
---|
[372] | 83 | {
|
---|
| 84 | Vector3 ptA = mVertices.back();
|
---|
[448] | 85 | int sideA = partition.Side(ptA, epsilon);
|
---|
[372] | 86 | bool foundSplit = false;
|
---|
[1344] | 87 | Vector3 lastSplit;
|
---|
| 88 |
|
---|
| 89 | //-- find line - plane intersections
|
---|
[448] | 90 |
|
---|
[1344] | 91 | VertexContainer::const_iterator it, it_end = mVertices.end();
|
---|
| 92 |
|
---|
| 93 | for (it = mVertices.begin(); it != it_end; ++ it)
|
---|
[372] | 94 | {
|
---|
| 95 | Vector3 ptB = *it;
|
---|
[448] | 96 | int sideB = partition.Side(ptB, epsilon);
|
---|
[372] | 97 |
|
---|
| 98 | // vertices on different sides => split
|
---|
[237] | 99 | if (sideB > 0)
|
---|
| 100 | {
|
---|
| 101 | if (sideA < 0)
|
---|
[372] | 102 | {
|
---|
| 103 | //-- plane - line intersection
|
---|
| 104 | Vector3 splitPt = partition.FindIntersection(ptA, ptB);
|
---|
| 105 |
|
---|
| 106 | // test if split point not too close to previous split point
|
---|
[448] | 107 | if (!foundSplit || (!EpsilonEqualV3(splitPt, lastSplit, epsilon)))
|
---|
[372] | 108 | {
|
---|
| 109 | // add vertex to both polygons
|
---|
| 110 | front.mVertices.push_back(splitPt);
|
---|
| 111 | back.mVertices.push_back(splitPt);
|
---|
| 112 |
|
---|
[436] | 113 | lastSplit = splitPt;
|
---|
[372] | 114 | foundSplit = true;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | front.mVertices.push_back(ptB);
|
---|
| 118 | }
|
---|
[237] | 119 | else if (sideB < 0)
|
---|
| 120 | {
|
---|
| 121 | if (sideA > 0)
|
---|
[372] | 122 | {
|
---|
| 123 | //-- plane - line intersection
|
---|
| 124 | Vector3 splitPt = partition.FindIntersection(ptA, ptB);
|
---|
| 125 | // test if split point not too close to other split point
|
---|
| 126 | // test if split point not too close to previous split point
|
---|
[448] | 127 | if (!foundSplit || (!EpsilonEqualV3(splitPt, lastSplit, epsilon)))
|
---|
[372] | 128 | {
|
---|
| 129 | // add vertex to both polygons
|
---|
| 130 | front.mVertices.push_back(splitPt);
|
---|
| 131 | back.mVertices.push_back(splitPt);
|
---|
| 132 |
|
---|
[436] | 133 | lastSplit = splitPt;
|
---|
[372] | 134 | foundSplit = true;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | back.mVertices.push_back(ptB);
|
---|
| 138 | }
|
---|
| 139 | else
|
---|
| 140 | {
|
---|
| 141 | // vertex on plane => add vertex to both polygons
|
---|
| 142 | front.mVertices.push_back(ptB);
|
---|
| 143 | back.mVertices.push_back(ptB);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | ptA = ptB;
|
---|
| 147 | sideA = sideB;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[574] | 151 |
|
---|
[372] | 152 | float Polygon3::GetArea() const
|
---|
| 153 | {
|
---|
[306] | 154 | Vector3 v = CrossProd(mVertices.back(), mVertices.front());
|
---|
| 155 |
|
---|
| 156 | for (int i=0; i < mVertices.size() - 1; ++i)
|
---|
| 157 | v += CrossProd(mVertices[i], mVertices[i+1]);
|
---|
| 158 |
|
---|
[544] | 159 | return 0.5f * fabs(DotProd(GetNormal(), v));
|
---|
[372] | 160 | }
|
---|
[235] | 161 |
|
---|
[574] | 162 |
|
---|
[448] | 163 | int Polygon3::Side(const Plane3 &plane,
|
---|
| 164 | const float epsilon) const
|
---|
[372] | 165 | {
|
---|
[448] | 166 | int classification = ClassifyPlane(plane, epsilon);
|
---|
[372] | 167 |
|
---|
| 168 | if (classification == BACK_SIDE)
|
---|
| 169 | return -1;
|
---|
| 170 | else if (classification == FRONT_SIDE)
|
---|
| 171 | return 1;
|
---|
[538] | 172 | // plane splits polygon
|
---|
[372] | 173 | return 0;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[574] | 176 |
|
---|
[448] | 177 | int Polygon3::ClassifyPlane(const Plane3 &plane,
|
---|
| 178 | const float epsilon) const
|
---|
[372] | 179 | {
|
---|
[574] | 180 | VertexContainer::const_iterator it, it_end = mVertices.end();
|
---|
[372] | 181 |
|
---|
| 182 | bool onFrontSide = false;
|
---|
| 183 | bool onBackSide = false;
|
---|
| 184 |
|
---|
| 185 | int count = 0;
|
---|
| 186 | // find possible line-plane intersections
|
---|
[574] | 187 | for (it = mVertices.begin(); it != it_end; ++ it)
|
---|
[372] | 188 | {
|
---|
[448] | 189 | const int side = plane.Side(*it, epsilon);
|
---|
[372] | 190 |
|
---|
| 191 | if (side > 0)
|
---|
[683] | 192 | {
|
---|
[372] | 193 | onFrontSide = true;
|
---|
[683] | 194 | }
|
---|
[372] | 195 | else if (side < 0)
|
---|
[683] | 196 | {
|
---|
[372] | 197 | onBackSide = true;
|
---|
[683] | 198 | }
|
---|
| 199 |
|
---|
[372] | 200 | //TODO: check if split goes through vertex
|
---|
| 201 | if (onFrontSide && onBackSide) // split
|
---|
| 202 | {
|
---|
| 203 | return SPLIT;
|
---|
| 204 | }
|
---|
| 205 | // 3 vertices enough to decide coincident
|
---|
| 206 | else if (((++ count) >= 3) && !onFrontSide && !onBackSide)
|
---|
| 207 | {
|
---|
| 208 | return COINCIDENT;
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | if (onBackSide)
|
---|
| 213 | {
|
---|
| 214 | return BACK_SIDE;
|
---|
| 215 | }
|
---|
| 216 | else if (onFrontSide)
|
---|
| 217 | {
|
---|
| 218 | return FRONT_SIDE;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | return COINCIDENT; // plane and polygon are coincident
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 |
|
---|
| 225 | Vector3
|
---|
| 226 | Polygon3::Center() const
|
---|
| 227 | {
|
---|
| 228 | int i;
|
---|
| 229 | Vector3 sum = mVertices[0];
|
---|
| 230 | for (i=1; i < mVertices.size(); i++)
|
---|
| 231 | sum += mVertices[i];
|
---|
| 232 |
|
---|
| 233 | return sum/(float)i;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 |
|
---|
| 237 | void
|
---|
| 238 | Polygon3::Scale(const float scale)
|
---|
| 239 | {
|
---|
| 240 | int i;
|
---|
| 241 | Vector3 center = Center();
|
---|
| 242 | for (i=0; i < mVertices.size(); i++) {
|
---|
| 243 | mVertices[i] = center + scale*(mVertices[i] - center);
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[448] | 247 | bool Polygon3::Valid(const float epsilon) const
|
---|
[372] | 248 | {
|
---|
| 249 | if (mVertices.size() < 3)
|
---|
| 250 | return false;
|
---|
| 251 |
|
---|
[574] | 252 | //TODO: remove for performance
|
---|
[648] | 253 | #if 0
|
---|
[574] | 254 | if (1)
|
---|
[372] | 255 | {
|
---|
[574] | 256 | // check if area exceeds certain size
|
---|
| 257 | if (GetArea() < AREA_LIMIT)
|
---|
| 258 | {
|
---|
| 259 | Debug << "area too small: " << GetArea() << endl;
|
---|
| 260 | return false;
|
---|
| 261 | }
|
---|
[372] | 262 | }
|
---|
[645] | 263 |
|
---|
| 264 | if (1)
|
---|
[574] | 265 | {
|
---|
| 266 | Vector3 vtx = mVertices.back();
|
---|
| 267 | VertexContainer::const_iterator it, it_end = mVertices.end();
|
---|
[372] | 268 |
|
---|
[574] | 269 | for (it = mVertices.begin(); it != it_end; ++it)
|
---|
[372] | 270 | {
|
---|
[645] | 271 | if (EpsilonEqualV3(vtx, *it, 0.0001))
|
---|
[574] | 272 | {
|
---|
| 273 | //Debug << "Malformed vertices:\n" << *this << endl;
|
---|
| 274 | return false;
|
---|
| 275 | }
|
---|
| 276 | vtx = *it;
|
---|
[372] | 277 | }
|
---|
| 278 | }
|
---|
[574] | 279 | #endif
|
---|
| 280 |
|
---|
[372] | 281 | return true;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[574] | 284 |
|
---|
[314] | 285 | // int_lineseg returns 1 if the given line segment intersects a 2D
|
---|
| 286 | // ray travelling in the positive X direction. This is used in the
|
---|
| 287 | // Jordan curve computation for polygon intersection.
|
---|
| 288 | inline int
|
---|
| 289 | int_lineseg(float px,
|
---|
| 290 | float py,
|
---|
| 291 | float u1,
|
---|
| 292 | float v1,
|
---|
| 293 | float u2,
|
---|
| 294 | float v2)
|
---|
| 295 | {
|
---|
| 296 | float t;
|
---|
| 297 | float ydiff;
|
---|
| 298 |
|
---|
| 299 | u1 -= px; u2 -= px; // translate line
|
---|
| 300 | v1 -= py; v2 -= py;
|
---|
| 301 |
|
---|
| 302 | if ((v1 > 0 && v2 > 0) ||
|
---|
| 303 | (v1 < 0 && v2 < 0) ||
|
---|
| 304 | (u1 < 0 && u2 < 0))
|
---|
| 305 | return 0;
|
---|
| 306 |
|
---|
| 307 | if (u1 > 0 && u2 > 0)
|
---|
| 308 | return 1;
|
---|
| 309 |
|
---|
| 310 | ydiff = v2 - v1;
|
---|
| 311 |
|
---|
| 312 | if (fabs(ydiff) < Limits::Small)
|
---|
| 313 | { // denominator near 0
|
---|
| 314 | if (((fabs(v1) > Limits::Small) || (u1 > 0) || (u2 > 0)))
|
---|
| 315 | return 0;
|
---|
| 316 | return 1;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | t = -v1 / ydiff; // Compute parameter
|
---|
| 320 |
|
---|
| 321 | return (u1 + t * (u2 - u1)) > 0;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[574] | 324 |
|
---|
[314] | 325 | int Polygon3::CastRay(const Ray &ray, float &t, const float nearestT)
|
---|
| 326 | {
|
---|
[1328] | 327 | const Plane3 plane = GetSupportingPlane();
|
---|
| 328 | const float dot = DotProd(plane.mNormal, ray.GetDir());
|
---|
[314] | 329 |
|
---|
| 330 | // Watch for near-zero denominator
|
---|
| 331 | // ONLY single sided polygons!!!!!
|
---|
| 332 | if (dot > -Limits::Small)
|
---|
[1328] | 333 | {
|
---|
| 334 | // if (fabs(dot) < Limits::Small)
|
---|
| 335 | return Ray::NO_INTERSECTION;
|
---|
| 336 | }
|
---|
[314] | 337 |
|
---|
| 338 | t = (-plane.mD - DotProd(plane.mNormal, ray.GetLoc())) / dot;
|
---|
| 339 |
|
---|
| 340 | if (t <= Limits::Small)
|
---|
[1328] | 341 | {
|
---|
| 342 | return Ray::INTERSECTION_OUT_OF_LIMITS;
|
---|
| 343 | }
|
---|
[314] | 344 |
|
---|
[1328] | 345 | if (t >= nearestT)
|
---|
| 346 | {
|
---|
| 347 | return Ray::INTERSECTION_OUT_OF_LIMITS; // no intersection was found
|
---|
[314] | 348 | }
|
---|
| 349 |
|
---|
| 350 | int count = 0;
|
---|
| 351 | float u, v, u1, v1, u2, v2;
|
---|
| 352 | int i;
|
---|
| 353 |
|
---|
[1328] | 354 | const int paxis = plane.mNormal.DrivingAxis();
|
---|
[314] | 355 |
|
---|
| 356 | // Project the intersection point onto the coordinate plane
|
---|
| 357 | // specified by which.
|
---|
| 358 | ray.Extrap(t).ExtractVerts(&u, &v, paxis);
|
---|
| 359 |
|
---|
[1328] | 360 | const int size = (int)mVertices.size();
|
---|
[314] | 361 |
|
---|
| 362 | mVertices.back().ExtractVerts(&u1, &v1, paxis );
|
---|
| 363 |
|
---|
| 364 | if (0 && size <= 4)
|
---|
| 365 | {
|
---|
| 366 | // assume a convex face
|
---|
| 367 | for (i = 0; i < size; i++)
|
---|
| 368 | {
|
---|
| 369 | mVertices[i].ExtractVerts(&u2, &v2, paxis);
|
---|
[1328] | 370 |
|
---|
[314] | 371 | // line u1, v1, u2, v2
|
---|
[1328] | 372 | if ((v2 - v1) * (u1 - u) > (u2 - u1)*(v1 - v))
|
---|
[314] | 373 | return Ray::NO_INTERSECTION;
|
---|
| 374 |
|
---|
| 375 | u1 = u2;
|
---|
| 376 | v1 = v2;
|
---|
[1328] | 377 | }
|
---|
| 378 |
|
---|
| 379 | return Ray::INTERSECTION;
|
---|
[314] | 380 | }
|
---|
| 381 |
|
---|
| 382 | // We're stuck with the Jordan curve computation. Count number
|
---|
| 383 | // of intersections between the line segments the polygon comprises
|
---|
| 384 | // with a ray originating at the point of intersection and
|
---|
| 385 | // travelling in the positive X direction.
|
---|
| 386 | for (i = 0; i < size; i++)
|
---|
| 387 | {
|
---|
| 388 | mVertices[i].ExtractVerts(&u2, &v2, paxis);
|
---|
| 389 |
|
---|
| 390 | count += (int_lineseg(u, v, u1, v1, u2, v2) != 0);
|
---|
| 391 |
|
---|
| 392 | u1 = u2;
|
---|
| 393 | v1 = v2;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | // We hit polygon if number of intersections is odd.
|
---|
| 397 | return (count & 1) ? Ray::INTERSECTION : Ray::NO_INTERSECTION;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
[574] | 400 |
|
---|
[372] | 401 | void Polygon3::InheritRays(Polygon3 &front_piece,
|
---|
| 402 | Polygon3 &back_piece) const
|
---|
| 403 | {
|
---|
| 404 | if (mPiercingRays.empty())
|
---|
| 405 | return;
|
---|
| 406 |
|
---|
| 407 | RayContainer::const_iterator it,
|
---|
| 408 | it_end = mPiercingRays.end();
|
---|
| 409 |
|
---|
| 410 | for (it = mPiercingRays.begin(); it != it_end; ++ it)
|
---|
| 411 | {
|
---|
| 412 | switch((*it)->GetId())
|
---|
| 413 | {
|
---|
| 414 | case Ray::BACK:
|
---|
| 415 | back_piece.mPiercingRays.push_back(*it);
|
---|
| 416 | break;
|
---|
| 417 | case Ray::FRONT:
|
---|
| 418 | front_piece.mPiercingRays.push_back(*it);
|
---|
| 419 | break;
|
---|
| 420 | case Ray::FRONT_BACK:
|
---|
| 421 | back_piece.mPiercingRays.push_back(*it);
|
---|
| 422 | break;
|
---|
| 423 | case Ray::BACK_FRONT:
|
---|
| 424 | front_piece.mPiercingRays.push_back(*it);
|
---|
| 425 | break;
|
---|
| 426 | default:
|
---|
| 427 | break;
|
---|
| 428 | }
|
---|
| 429 | }
|
---|
| 430 | }
|
---|
| 431 |
|
---|
[574] | 432 |
|
---|
[448] | 433 | int Polygon3::ClassifyPlane(const PolygonContainer &polys,
|
---|
| 434 | const Plane3 &plane,
|
---|
| 435 | const float epsilon)
|
---|
[372] | 436 | {
|
---|
[683] | 437 | bool onFrontSide = false;
|
---|
| 438 | bool onBackSide = false;
|
---|
[372] | 439 | PolygonContainer::const_iterator it;
|
---|
| 440 |
|
---|
| 441 | // find intersections
|
---|
| 442 | for (it = polys.begin(); it != polys.end(); ++ it)
|
---|
| 443 | {
|
---|
[448] | 444 | const int cf = (*it)->ClassifyPlane(plane, epsilon);
|
---|
[372] | 445 |
|
---|
| 446 | if (cf == FRONT_SIDE)
|
---|
| 447 | onFrontSide = true;
|
---|
| 448 | else if (cf == BACK_SIDE)
|
---|
| 449 | onBackSide = true;
|
---|
| 450 |
|
---|
| 451 | if ((cf == SPLIT) || (cf == COINCIDENT) || (onFrontSide && onBackSide))
|
---|
| 452 | {
|
---|
| 453 | return SPLIT;
|
---|
| 454 | }
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | if (onBackSide)
|
---|
| 458 | {
|
---|
| 459 | return BACK_SIDE;
|
---|
| 460 | }
|
---|
| 461 | else if (onFrontSide)
|
---|
| 462 | {
|
---|
| 463 | return FRONT_SIDE;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | return SPLIT;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[574] | 469 |
|
---|
[375] | 470 | int Polygon3::ParentObjectsSize(const PolygonContainer &polys)
|
---|
[372] | 471 | {
|
---|
| 472 | int count = 0;
|
---|
| 473 |
|
---|
| 474 | PolygonContainer::const_iterator it, it_end = polys.end();
|
---|
| 475 |
|
---|
| 476 | MeshInstance::NewMail();
|
---|
| 477 |
|
---|
| 478 | for (it = polys.begin(); it != it_end; ++ it)
|
---|
| 479 | {
|
---|
| 480 | if ((*it)->mParent && !(*it)->mParent->Mailed())
|
---|
| 481 | {
|
---|
| 482 | ++ count;
|
---|
| 483 | (*it)->mParent->Mail();
|
---|
| 484 | }
|
---|
| 485 | }
|
---|
| 486 | return count;
|
---|
[384] | 487 | }
|
---|
| 488 |
|
---|
[574] | 489 |
|
---|
[384] | 490 | float Polygon3::GetArea(const PolygonContainer &cell)
|
---|
| 491 | {
|
---|
| 492 | float area = 0;
|
---|
| 493 | PolygonContainer::const_iterator pit;
|
---|
| 494 |
|
---|
| 495 | for (pit = cell.begin(); pit != cell.end(); ++ pit)
|
---|
[448] | 496 | {
|
---|
[384] | 497 | area += (*pit)->GetArea();
|
---|
[448] | 498 | }
|
---|
[396] | 499 |
|
---|
[384] | 500 | return area;
|
---|
[396] | 501 | }
|
---|
| 502 |
|
---|
| 503 |
|
---|
| 504 | Polygon3 *Polygon3::CreateReversePolygon() const
|
---|
| 505 | {
|
---|
| 506 | Polygon3 *revPoly = new Polygon3();
|
---|
| 507 |
|
---|
| 508 | VertexContainer::const_reverse_iterator rit,
|
---|
| 509 | rit_end = mVertices.rend();
|
---|
| 510 |
|
---|
| 511 | for(rit = mVertices.rbegin(); rit != rit_end; ++ rit)
|
---|
[448] | 512 | revPoly->mVertices.push_back(*rit);
|
---|
| 513 |
|
---|
| 514 | return revPoly;
|
---|
[503] | 515 | }
|
---|
| 516 |
|
---|
| 517 |
|
---|
[840] | 518 | void Polygon3::Triangulate(vector<Triangle3> &triangles) const
|
---|
[503] | 519 | {
|
---|
| 520 | int i = 1;
|
---|
| 521 | int j = 0;
|
---|
[508] | 522 | int k = (int)mVertices.size() - 1;
|
---|
[503] | 523 | int count = 0;
|
---|
| 524 |
|
---|
| 525 | while (i < k)
|
---|
| 526 | {
|
---|
| 527 | triangles.push_back(Triangle3(mVertices[i], mVertices[j], mVertices[k]));
|
---|
| 528 |
|
---|
| 529 | if ((count ++) % 2)
|
---|
| 530 | j = i ++;
|
---|
| 531 | else
|
---|
| 532 | j = k --;
|
---|
| 533 | }
|
---|
| 534 | }
|
---|
[508] | 535 |
|
---|
| 536 |
|
---|
[840] | 537 | void Polygon3::Triangulate(VertexIndexContainer &indices) const
|
---|
[508] | 538 | {
|
---|
| 539 | int i = 1;
|
---|
| 540 | int j = 0;
|
---|
| 541 | int k = (int)mVertices.size() - 1;
|
---|
| 542 |
|
---|
| 543 | int count = 0;
|
---|
| 544 |
|
---|
| 545 | while (i < k)
|
---|
| 546 | {
|
---|
| 547 | indices.push_back(k);
|
---|
| 548 | indices.push_back(j);
|
---|
| 549 | indices.push_back(i);
|
---|
| 550 |
|
---|
| 551 | if ((count ++) % 2)
|
---|
[1328] | 552 | {
|
---|
[508] | 553 | j = i ++;
|
---|
[1328] | 554 | }
|
---|
[508] | 555 | else
|
---|
[1328] | 556 | {
|
---|
[508] | 557 | j = k --;
|
---|
[1328] | 558 | }
|
---|
[508] | 559 | }
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 |
|
---|
[840] | 563 | void IncludePolyInMesh(const Polygon3 &poly, Mesh &mesh)
|
---|
[508] | 564 | {
|
---|
| 565 | const int n = (int)mesh.mVertices.size();
|
---|
| 566 |
|
---|
| 567 | //-- add the vertices
|
---|
[840] | 568 | VertexContainer::const_iterator vit, vit_end = poly.mVertices.end();
|
---|
| 569 | for (vit = poly.mVertices.begin(); vit != vit_end; ++ vit)
|
---|
[508] | 570 | {
|
---|
| 571 | mesh.mVertices.push_back(*vit);
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | // one quad => no triangulation necessary
|
---|
[840] | 575 | if (poly.mVertices.size() == 4)
|
---|
[508] | 576 | {
|
---|
| 577 | mesh.AddFace(new Face(n, n + 1, n + 2, n + 3));
|
---|
[840] | 578 | return;
|
---|
[508] | 579 | }
|
---|
[840] | 580 |
|
---|
| 581 | VertexIndexContainer indices;
|
---|
| 582 | poly.Triangulate(indices);
|
---|
[508] | 583 |
|
---|
[840] | 584 | // add indices of triangle strip
|
---|
| 585 | for (int i = 0; i < (int)indices.size(); i += 3)
|
---|
| 586 | {
|
---|
| 587 | Face *face = new Face(n + indices[i],
|
---|
| 588 | n + indices[i + 1],
|
---|
| 589 | n + indices[i + 2]);
|
---|
| 590 | mesh.AddFace(face);
|
---|
[508] | 591 | }
|
---|
[860] | 592 | }
|
---|
| 593 |
|
---|
[1328] | 594 |
|
---|
| 595 | AxisAlignedBox3 Polygon3::GetBoundingBox() const
|
---|
| 596 | {
|
---|
| 597 | VertexContainer::const_iterator vit, vit_end = mVertices.end();
|
---|
| 598 |
|
---|
| 599 | AxisAlignedBox3 box;
|
---|
| 600 | box.Initialize();
|
---|
| 601 |
|
---|
| 602 | for (vit = mVertices.begin(); vit != vit_end; ++ vit)
|
---|
| 603 | {
|
---|
| 604 | box.Include(*vit);
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | return box;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 |
|
---|
[508] | 611 | } |
---|