[191] | 1 | // This file is a part of the Framsticks GDK. |
---|
| 2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
| 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
| 4 | |
---|
| 5 | #include "meshbuilder.h" |
---|
| 6 | |
---|
| 7 | #include <frams/model/geometry/modelgeometryinfo.h> |
---|
| 8 | #include <stdio.h> |
---|
| 9 | #include <math.h> |
---|
| 10 | |
---|
| 11 | MeshBuilder::Iterator::Iterator(const double _density): |
---|
| 12 | density(_density) |
---|
| 13 | {} |
---|
| 14 | |
---|
| 15 | double MeshBuilder::Iterator::getDensity() const |
---|
| 16 | { |
---|
| 17 | return density; |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | double MeshBuilder::Iterator::setDensity(const double _density) |
---|
| 21 | { |
---|
| 22 | density = _density; |
---|
[234] | 23 | return density; |
---|
[191] | 24 | } |
---|
| 25 | |
---|
| 26 | void MeshBuilder::Iterator::forEach(Callback &callback) |
---|
| 27 | { |
---|
| 28 | Pt3D point; |
---|
| 29 | |
---|
| 30 | while (tryGetNext(point)) |
---|
| 31 | { |
---|
| 32 | callback(point); |
---|
| 33 | } |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | void MeshBuilder::Iterator::addAllPointsToList(SListTempl<Pt3D> &list) |
---|
| 37 | { |
---|
| 38 | Pt3D point; |
---|
| 39 | |
---|
| 40 | while (tryGetNext(point)) |
---|
| 41 | { |
---|
| 42 | list += point; |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | MeshBuilder::Segment::Segment(const double _density): |
---|
| 49 | Iterator(_density), point1(0), point2(0), i(0), I(0), numberOfPoints(0) |
---|
| 50 | {} |
---|
| 51 | |
---|
| 52 | void MeshBuilder::Segment::initialize(const Pt3D &point1, const Pt3D &point2, const bool skipFirst, const bool skipLast, const bool forceOddNumberOfPoints) |
---|
| 53 | { |
---|
| 54 | this->point1 = point1; |
---|
| 55 | this->point2 = point2; |
---|
| 56 | numberOfPoints = (int)ceil(density * point1.distanceTo(point2)) + 1; |
---|
| 57 | numberOfPoints += forceOddNumberOfPoints && (numberOfPoints%2 == 0) ? 1 : 0; |
---|
| 58 | i = skipFirst ? 1 : 0; |
---|
| 59 | I = numberOfPoints - (skipLast ? 1 : 0); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | bool MeshBuilder::Segment::tryGetNext(Pt3D &point) |
---|
| 63 | { |
---|
| 64 | if (i >= I) |
---|
| 65 | { |
---|
| 66 | return false; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | double position = GeometryUtils::pointPosition(i, numberOfPoints); |
---|
| 70 | point.x = GeometryUtils::combination(point1.x, point2.x, position); |
---|
| 71 | point.y = GeometryUtils::combination(point1.y, point2.y, position); |
---|
| 72 | point.z = GeometryUtils::combination(point1.z, point2.z, position); |
---|
| 73 | |
---|
| 74 | i += 1; |
---|
| 75 | return true; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | MeshBuilder::RectangleSurface::RectangleSurface(const double _density): |
---|
| 81 | Iterator(_density), edge1(_density), edge2(_density), area(_density), skipVerticalEdges(false), forceOddNumberOfPoints(false) |
---|
| 82 | {} |
---|
| 83 | |
---|
| 84 | void MeshBuilder::RectangleSurface::initialize(const Part *part, const CuboidFaces::Face face, const bool skipVerticalEdges, const bool skipHorizontalEdges, const bool forceOddNumberOfPoints) |
---|
| 85 | { |
---|
| 86 | Pt3D apex1, apex2, apex3, apex4; |
---|
| 87 | GeometryUtils::getRectangleApicesFromCuboid(part, face, apex1, apex2, apex3, apex4); |
---|
| 88 | initialize(apex1, apex2, apex3, apex4, skipVerticalEdges, skipHorizontalEdges, forceOddNumberOfPoints); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void MeshBuilder::RectangleSurface::initialize(const double width, const double height, const Pt3D &position, const Orient &orient, const bool skipVerticalEdges, const bool skipHorizontalEdges, const bool forceOddNumberOfPoints) |
---|
| 92 | { |
---|
| 93 | Pt3D apex1, apex2, apex3, apex4; |
---|
| 94 | GeometryUtils::getRectangleApices(width, height, position, orient, apex1, apex2, apex3, apex4); |
---|
| 95 | initialize(apex1, apex2, apex3, apex4, skipVerticalEdges, skipHorizontalEdges, forceOddNumberOfPoints); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | void MeshBuilder::RectangleSurface::initialize(const Pt3D &apex1, const Pt3D &apex2, const Pt3D &apex3, const Pt3D &apex4, const bool skipVerticalEdges, const bool skipHorizontalEdges, const bool forceOddNumberOfPoints) |
---|
| 99 | { |
---|
| 100 | this->skipVerticalEdges = skipVerticalEdges; |
---|
| 101 | this->forceOddNumberOfPoints = forceOddNumberOfPoints; |
---|
| 102 | |
---|
| 103 | edge1.setDensity(density); |
---|
| 104 | edge1.initialize(apex1, apex2, skipHorizontalEdges, skipHorizontalEdges, forceOddNumberOfPoints); |
---|
| 105 | |
---|
| 106 | edge2.setDensity(density); |
---|
| 107 | edge2.initialize(apex3, apex4, skipHorizontalEdges, skipHorizontalEdges, forceOddNumberOfPoints); |
---|
| 108 | |
---|
| 109 | area = Segment(density); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | bool MeshBuilder::RectangleSurface::tryGetNext(Pt3D &point) |
---|
| 113 | { |
---|
| 114 | while (!area.tryGetNext(point)) |
---|
| 115 | { |
---|
| 116 | Pt3D point1, point2; |
---|
| 117 | if (edge1.tryGetNext(point1) && edge2.tryGetNext(point2)) |
---|
| 118 | { |
---|
| 119 | area.initialize(point1, point2, skipVerticalEdges, skipVerticalEdges, forceOddNumberOfPoints); |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | { |
---|
| 123 | return false; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | return true; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | MeshBuilder::CuboidApices::CuboidApices(): |
---|
| 133 | Iterator(0), scale(0), position(0), orient(Orient_1), octant(Octants::NUMBER) |
---|
| 134 | {} |
---|
| 135 | |
---|
| 136 | void MeshBuilder::CuboidApices::initialize(const Part *part) |
---|
| 137 | { |
---|
| 138 | initialize(part->scale, part->p, part->o); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | void MeshBuilder::CuboidApices::initialize(const Pt3D &scale, const Pt3D &position, const Orient &orient) |
---|
| 142 | { |
---|
| 143 | this->scale = scale; |
---|
| 144 | this->position = position; |
---|
| 145 | this->orient = orient; |
---|
| 146 | octant = Octants::FIRST; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | bool MeshBuilder::CuboidApices::tryGetNext(Pt3D &point) |
---|
| 150 | { |
---|
| 151 | if (octant == Octants::NUMBER) |
---|
| 152 | { |
---|
| 153 | return false; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | Pt3D temp(scale); |
---|
| 157 | |
---|
| 158 | temp.x *= Octants::isPositiveX(octant) ? 1 : -1; |
---|
| 159 | temp.y *= Octants::isPositiveY(octant) ? 1 : -1; |
---|
| 160 | temp.z *= Octants::isPositiveZ(octant) ? 1 : -1; |
---|
| 161 | |
---|
| 162 | orient.transform(point, temp); |
---|
| 163 | point += position; |
---|
| 164 | |
---|
| 165 | octant = Octants::Octant(octant+1); |
---|
| 166 | |
---|
| 167 | return true; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | MeshBuilder::CuboidSurface::CuboidSurface(const double _density): |
---|
| 173 | Iterator(_density), apices(), rectangle(_density), iterator(NULL), part(NULL), face(CuboidFaces::NUMBER) |
---|
| 174 | {} |
---|
| 175 | |
---|
| 176 | void MeshBuilder::CuboidSurface::initialize(const Part *part) |
---|
| 177 | { |
---|
| 178 | this->part = part; |
---|
| 179 | face = CuboidFaces::FIRST; |
---|
| 180 | apices.setDensity(density); |
---|
| 181 | apices.initialize(part); |
---|
| 182 | iterator = &apices; |
---|
| 183 | rectangle.setDensity(density); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | bool MeshBuilder::CuboidSurface::tryGetNext(Pt3D &point) |
---|
| 187 | { |
---|
| 188 | if (iterator == NULL) |
---|
| 189 | { |
---|
| 190 | return false; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | while(!iterator->tryGetNext(point)) |
---|
| 194 | { |
---|
| 195 | if (face < CuboidFaces::NUMBER) |
---|
| 196 | { |
---|
| 197 | iterator = &rectangle; |
---|
| 198 | rectangle.initialize(part, face, true, false); |
---|
| 199 | face = CuboidFaces::Face(face+1); |
---|
| 200 | } |
---|
| 201 | else |
---|
| 202 | { |
---|
| 203 | return false; |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | return true; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | MeshBuilder::EllipseSurface::EllipseSurface(const double _density): |
---|
| 213 | Iterator(_density), rectangle(_density), position(0), orient(Orient_1), inversedSquaredWidth(0.0), inversedSquaredHeight(0.0) |
---|
| 214 | {} |
---|
| 215 | |
---|
| 216 | void MeshBuilder::EllipseSurface::initialize(const Part *part, const CylinderBases::Base base) |
---|
| 217 | { |
---|
| 218 | double relativePositionX = part->scale.x; |
---|
| 219 | relativePositionX *= CylinderBases::isPositive(base) ? +1 : -1; |
---|
| 220 | Pt3D relativePosition; |
---|
| 221 | part->o.transform(relativePosition, Pt3D(relativePositionX, 0.0, 0.0)); |
---|
| 222 | initialize(part->scale.y, part->scale.z, part->p + relativePosition, part->o); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | void MeshBuilder::EllipseSurface::initialize(const double width, const double height, const Pt3D &position, const Orient &orient) |
---|
| 226 | { |
---|
| 227 | this->position = position; |
---|
| 228 | this->orient = orient; |
---|
| 229 | rectangle.setDensity(density); |
---|
| 230 | rectangle.initialize(width, height, Pt3D(0), Orient_1, false, false, true); |
---|
| 231 | inversedSquaredWidth = 1.0 / pow(width, 2.0); |
---|
| 232 | inversedSquaredHeight = 1.0 / pow(height, 2.0); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | bool MeshBuilder::EllipseSurface::tryGetNext(Pt3D &point) |
---|
| 236 | { |
---|
| 237 | Pt3D temp; |
---|
| 238 | |
---|
| 239 | while (rectangle.tryGetNext(temp)) |
---|
| 240 | { |
---|
| 241 | double r |
---|
| 242 | = (pow(temp.y, 2.0) * inversedSquaredWidth) |
---|
| 243 | + (pow(temp.z, 2.0) * inversedSquaredHeight); |
---|
| 244 | if (r <= 1.0) |
---|
| 245 | { |
---|
| 246 | orient.transform(point, temp); |
---|
| 247 | point += position; |
---|
| 248 | return true; |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | return false; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | MeshBuilder::Ellipse::Ellipse(const double _density): |
---|
| 258 | Iterator(_density), phase(Done), position(0), orient(Orient_1), width(0.0), height(0.0), d(0.0), p(0.0), q(0.0), P(0.0), Q(0.0), D(0.0), a(0.0), b(0.0), quadrant(QuadrantsYZ::FIRST) |
---|
| 259 | {} |
---|
| 260 | |
---|
| 261 | void MeshBuilder::Ellipse::initialize(const Part *part, const CylinderBases::Base base) |
---|
| 262 | { |
---|
| 263 | double relativePositionX = part->scale.x; |
---|
| 264 | relativePositionX *= CylinderBases::isPositive(base) ? +1 : -1; |
---|
| 265 | Pt3D relativePosition; |
---|
| 266 | part->o.transform(relativePosition, Pt3D(relativePositionX, 0.0, 0.0)); |
---|
| 267 | initialize(part->scale.y, part->scale.z, part->p + relativePosition, part->o); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | void MeshBuilder::Ellipse::initialize(const double width, const double height, const Pt3D &position, const Orient &orient) |
---|
| 271 | { |
---|
| 272 | this->width = width; |
---|
| 273 | this->height = height; |
---|
| 274 | this->position = position; |
---|
| 275 | this->orient = orient; |
---|
| 276 | calculateAndSetSegmentationDistance(); |
---|
| 277 | initializePhase(yToZ); |
---|
| 278 | D = 0; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | bool MeshBuilder::Ellipse::tryGetNext(Pt3D &point) |
---|
| 282 | { |
---|
| 283 | if (phase == Done) |
---|
| 284 | { |
---|
| 285 | return false; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | setPoint(point); |
---|
| 289 | |
---|
| 290 | quadrant = QuadrantsYZ::QuadrantYZ(quadrant+1); |
---|
| 291 | |
---|
| 292 | if (quadrant == QuadrantsYZ::NUMBER) |
---|
| 293 | { |
---|
| 294 | quadrant = QuadrantsYZ::FIRST; |
---|
| 295 | findNextPQAndPhase(); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | return true; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | void MeshBuilder::Ellipse::calculateAndSetSegmentationDistance() |
---|
| 302 | { |
---|
| 303 | d = 1.0 / density; |
---|
| 304 | int i = 0; |
---|
| 305 | |
---|
| 306 | Pt3D p1(0); |
---|
| 307 | i += findLastPQOfPhase(yToZ, p1.z, p1.y); |
---|
| 308 | |
---|
| 309 | Pt3D p2(0); |
---|
| 310 | i += findLastPQOfPhase(zToY, p2.y, p2.z); |
---|
| 311 | |
---|
| 312 | double ld = p1.distanceTo(p2); |
---|
| 313 | double td = i * d + ld; |
---|
| 314 | int n = (int)ceil(td * density); |
---|
| 315 | d = td / n; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | void MeshBuilder::Ellipse::initializePhase(Phase ph) |
---|
| 319 | { |
---|
| 320 | phase = ph; |
---|
| 321 | |
---|
| 322 | if (phase != Done) |
---|
| 323 | { |
---|
| 324 | a = phase == yToZ ? height : width; |
---|
| 325 | b = phase == yToZ ? width : height; |
---|
| 326 | |
---|
| 327 | p = 0.0; |
---|
| 328 | q = b; |
---|
| 329 | P = a*a / sqrt(a*a + b*b); |
---|
| 330 | Q = b*b / sqrt(a*a + b*b); |
---|
| 331 | |
---|
| 332 | quadrant = QuadrantsYZ::POSITIVE_Y_NEGATIVE_Z; |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | void MeshBuilder::Ellipse::setPoint(Pt3D &point) |
---|
| 337 | { |
---|
| 338 | double np = p * (QuadrantsYZ::isPositiveY(quadrant) ? +1 : -1); |
---|
| 339 | double nq = q * (QuadrantsYZ::isPositiveZ(quadrant) ? +1 : -1); |
---|
| 340 | |
---|
| 341 | double y = phase == yToZ ? nq : np; |
---|
| 342 | double z = phase == yToZ ? np : nq; |
---|
| 343 | |
---|
| 344 | Pt3D temp(0.0, y, z); |
---|
| 345 | orient.transform(point, temp); |
---|
| 346 | point += position; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | void MeshBuilder::Ellipse::findNextPQAndPhase() |
---|
| 350 | { |
---|
| 351 | double _p = p; |
---|
| 352 | double _q = q; |
---|
| 353 | |
---|
| 354 | GeometryUtils::getNextEllipseSegmentationPoint(d, a, b, p, q); |
---|
| 355 | |
---|
| 356 | if (p > P) |
---|
| 357 | { |
---|
| 358 | D += sqrt(pow(P-_p, 2.0) + pow(Q-_q, 2.0)); |
---|
| 359 | |
---|
| 360 | if ((phase == zToY) && (D > 1.5 * d)) |
---|
| 361 | { |
---|
| 362 | p = P; |
---|
| 363 | q = Q; |
---|
| 364 | D = 0; |
---|
| 365 | } |
---|
| 366 | else |
---|
| 367 | { |
---|
| 368 | initializePhase(Phase(phase+1)); |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | int MeshBuilder::Ellipse::findLastPQOfPhase(Phase ph, double &lp, double &lq) |
---|
| 374 | { |
---|
| 375 | initializePhase(ph); |
---|
| 376 | int i = 0; |
---|
| 377 | |
---|
| 378 | do |
---|
| 379 | { |
---|
| 380 | lp = p; |
---|
| 381 | lq = q; |
---|
| 382 | |
---|
| 383 | GeometryUtils::getNextEllipseSegmentationPoint(d, a, b, p, q); |
---|
| 384 | |
---|
| 385 | i += p <= P ? 1 : 0; |
---|
| 386 | } while (p <= P); |
---|
| 387 | |
---|
| 388 | return i; |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | MeshBuilder::CylinderEdges::CylinderEdges(const double _density): |
---|
| 392 | Iterator(_density), ellipse(_density), edge(0.0), length(0.0), base(CylinderBases::NUMBER) |
---|
| 393 | {} |
---|
| 394 | |
---|
| 395 | void MeshBuilder::CylinderEdges::initialize(const Part *part) |
---|
| 396 | { |
---|
| 397 | initialize(part->scale, part->p, part->o); |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | void MeshBuilder::CylinderEdges::initialize(const Pt3D &scale, const Pt3D &position, const Orient &orient) |
---|
| 401 | { |
---|
| 402 | initialize(scale.x, scale.y, scale.z, position, orient); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | void MeshBuilder::CylinderEdges::initialize(const double length, const double width, const double height, const Pt3D &position, const Orient &orient) |
---|
| 406 | { |
---|
| 407 | ellipse.setDensity(density); |
---|
| 408 | ellipse.initialize(width, height, position, orient); |
---|
| 409 | |
---|
| 410 | orient.transform(this->length, Pt3D(length, 0.0, 0.0)); |
---|
| 411 | |
---|
| 412 | base = CylinderBases::NUMBER; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | bool MeshBuilder::CylinderEdges::tryGetNext(Pt3D &point) |
---|
| 416 | { |
---|
| 417 | if (base == CylinderBases::NUMBER) |
---|
| 418 | { |
---|
| 419 | if (!ellipse.tryGetNext(edge)) |
---|
| 420 | { |
---|
| 421 | return false; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | base = CylinderBases::FIRST; |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | point.x = edge.x + length.x * (CylinderBases::isPositive(base) ? +1 : -1); |
---|
| 428 | point.y = edge.y + length.y * (CylinderBases::isPositive(base) ? +1 : -1); |
---|
| 429 | point.z = edge.z + length.z * (CylinderBases::isPositive(base) ? +1 : -1); |
---|
| 430 | |
---|
| 431 | base = CylinderBases::Base(base+1); |
---|
| 432 | |
---|
| 433 | return true; |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | |
---|
| 437 | |
---|
| 438 | MeshBuilder::CylinderWallSurface::CylinderWallSurface(const double _density): |
---|
| 439 | Iterator(_density), edge(_density), length(0.0), area(_density) |
---|
| 440 | {} |
---|
| 441 | |
---|
| 442 | void MeshBuilder::CylinderWallSurface::initialize(const Part *part) |
---|
| 443 | { |
---|
| 444 | initialize(part->scale, part->p, part->o); |
---|
| 445 | } |
---|
| 446 | |
---|
| 447 | void MeshBuilder::CylinderWallSurface::initialize(const Pt3D &scale, const Pt3D &position, const Orient &orient) |
---|
| 448 | { |
---|
| 449 | initialize(scale.x, scale.y, scale.z, position, orient); |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | void MeshBuilder::CylinderWallSurface::initialize(const double length, const double width, const double height, const Pt3D &position, const Orient &orient) |
---|
| 453 | { |
---|
| 454 | edge.setDensity(density); |
---|
| 455 | edge.initialize(width, height, position, orient); |
---|
| 456 | |
---|
| 457 | orient.transform(this->length, Pt3D(length, 0.0, 0.0)); |
---|
| 458 | |
---|
| 459 | area.setDensity(density); |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | bool MeshBuilder::CylinderWallSurface::tryGetNext(Pt3D &point) |
---|
| 463 | { |
---|
| 464 | while (!area.tryGetNext(point)) |
---|
| 465 | { |
---|
| 466 | if (edge.tryGetNext(point)) |
---|
| 467 | { |
---|
| 468 | area.initialize(point + length, point - length); |
---|
| 469 | } |
---|
| 470 | else |
---|
| 471 | { |
---|
| 472 | return false; |
---|
| 473 | } |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | return true; |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | |
---|
| 480 | |
---|
| 481 | MeshBuilder::CylinderSurface::CylinderSurface(const double _density): |
---|
| 482 | Iterator(_density), wall(_density), ellipse(_density), iterator(NULL), part(NULL), base(CylinderBases::NUMBER) |
---|
| 483 | {} |
---|
| 484 | |
---|
| 485 | void MeshBuilder::CylinderSurface::initialize(const Part *part) |
---|
| 486 | { |
---|
| 487 | this->part = part; |
---|
| 488 | base = CylinderBases::FIRST; |
---|
| 489 | wall.setDensity(density); |
---|
| 490 | wall.initialize(part); |
---|
| 491 | iterator = &wall; |
---|
| 492 | ellipse.setDensity(density); |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | bool MeshBuilder::CylinderSurface::tryGetNext(Pt3D &point) |
---|
| 496 | { |
---|
| 497 | if (iterator == NULL) |
---|
| 498 | { |
---|
| 499 | return false; |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | while(!iterator->tryGetNext(point)) |
---|
| 503 | { |
---|
| 504 | if (base < CylinderBases::NUMBER) |
---|
| 505 | { |
---|
| 506 | iterator = &ellipse; |
---|
| 507 | ellipse.initialize(part, base); |
---|
| 508 | base = CylinderBases::Base(base+1); |
---|
| 509 | } |
---|
| 510 | else |
---|
| 511 | { |
---|
| 512 | return false; |
---|
| 513 | } |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | return true; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | |
---|
| 520 | |
---|
| 521 | MeshBuilder::EllipsoidSurface::EllipsoidSurface(const double _density): |
---|
| 522 | Iterator(_density), phase(Done), edge(0.0), area(0.0), scale(0.0), limit(0.0), d(0.0), part(NULL), octant(Octants::NUMBER) |
---|
| 523 | {} |
---|
| 524 | |
---|
| 525 | void MeshBuilder::EllipsoidSurface::initialize(const Part *part) |
---|
| 526 | { |
---|
| 527 | this->part = part; |
---|
| 528 | d = 1.0 / density; |
---|
| 529 | initializePhase(X); |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | bool MeshBuilder::EllipsoidSurface::tryGetNext(Pt3D &point) |
---|
| 533 | { |
---|
| 534 | if (phase == Done) |
---|
| 535 | { |
---|
| 536 | return false; |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | setPoint(point); |
---|
| 540 | proceedToNextOctant(); |
---|
| 541 | |
---|
| 542 | if (octant == Octants::NUMBER) |
---|
| 543 | { |
---|
| 544 | octant = Octants::FIRST; |
---|
| 545 | findNextAreaEdgeAndPhase(); |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | return true; |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | void MeshBuilder::EllipsoidSurface::initializePhase(Phase ph) |
---|
| 552 | { |
---|
| 553 | phase = ph; |
---|
| 554 | |
---|
| 555 | if (phase != Done) |
---|
| 556 | { |
---|
| 557 | scale.x = phase == X ? part->scale.x : phase == Y ? part->scale.y : part->scale.z; |
---|
| 558 | scale.y = phase == X ? part->scale.y : phase == Y ? part->scale.z : part->scale.x; |
---|
| 559 | scale.z = phase == X ? part->scale.z : phase == Y ? part->scale.x : part->scale.y; |
---|
| 560 | |
---|
| 561 | edge = Pt3D(scale.x, 0.0, scale.z); |
---|
| 562 | limit.y = scale.y*scale.y / sqrt(scale.x*scale.x + scale.y*scale.y); |
---|
| 563 | limit.z = edge.z*edge.z / sqrt(edge.x*edge.x + edge.z*edge.z); |
---|
| 564 | area = Pt3D(edge.x, edge.y, 0.0); |
---|
| 565 | |
---|
| 566 | octant = Octants::FIRST; |
---|
| 567 | } |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | void MeshBuilder::EllipsoidSurface::setPoint(Pt3D &point) |
---|
| 571 | { |
---|
| 572 | Pt3D temp1(area); |
---|
| 573 | temp1.x *= Octants::isPositiveX(octant) ? +1 : -1; |
---|
| 574 | temp1.y *= Octants::isPositiveY(octant) ? +1 : -1; |
---|
| 575 | temp1.z *= Octants::isPositiveZ(octant) ? +1 : -1; |
---|
| 576 | |
---|
| 577 | Pt3D temp2; |
---|
| 578 | temp2.x = phase == X ? temp1.x : phase == Y ? temp1.z : temp1.y; |
---|
| 579 | temp2.y = phase == X ? temp1.y : phase == Y ? temp1.x : temp1.z; |
---|
| 580 | temp2.z = phase == X ? temp1.z : phase == Y ? temp1.y : temp1.x; |
---|
| 581 | |
---|
| 582 | part->o.transform(point, temp2); |
---|
| 583 | point += part->p; |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | void MeshBuilder::EllipsoidSurface::proceedToNextOctant() |
---|
| 587 | { |
---|
| 588 | int step = 1; |
---|
| 589 | step += (Octants::isNegativeZ(octant) && (area.z == 0.0)) ? 1 : 0; |
---|
| 590 | step += (Octants::isNegativeY(octant) && (area.y == 0.0)) ? 2 : 0; |
---|
| 591 | octant = Octants::Octant(octant + step); |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | void MeshBuilder::EllipsoidSurface::findNextAreaEdgeAndPhase() |
---|
| 595 | { |
---|
| 596 | GeometryUtils::getNextEllipseSegmentationPoint(d, edge.z, edge.x, area.z, area.x); |
---|
| 597 | |
---|
| 598 | if ((area.z > limit.z) || (area.y > limit.y * sqrt(1.0 - (area.z*area.z) / (scale.z*scale.z)))) |
---|
| 599 | { |
---|
| 600 | GeometryUtils::getNextEllipseSegmentationPoint(d, scale.y, scale.x, edge.y, edge.x); |
---|
| 601 | edge.z = scale.z * sqrt(1.0 - (edge.y*edge.y) / (scale.y*scale.y)); |
---|
| 602 | limit.z = edge.z*edge.z / sqrt(edge.x*edge.x + edge.z*edge.z); |
---|
| 603 | area = Pt3D(edge.x, edge.y, 0.0); |
---|
| 604 | |
---|
| 605 | if (edge.y > limit.y) |
---|
| 606 | { |
---|
| 607 | initializePhase(Phase(phase+1)); |
---|
| 608 | } |
---|
| 609 | } |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | |
---|
| 613 | |
---|
| 614 | MeshBuilder::PartSurface::PartSurface(const double _density): |
---|
| 615 | Iterator(_density), cuboid(_density), cylinder(_density), ellipsoid(_density), iterator(NULL) |
---|
| 616 | {} |
---|
| 617 | |
---|
| 618 | void MeshBuilder::PartSurface::initialize(const Part *part) |
---|
| 619 | { |
---|
| 620 | switch (part->shape) |
---|
| 621 | { |
---|
| 622 | case Part::SHAPE_ELLIPSOID: |
---|
| 623 | ellipsoid.setDensity(density); |
---|
| 624 | ellipsoid.initialize(part); |
---|
| 625 | iterator = &ellipsoid; |
---|
| 626 | break; |
---|
| 627 | |
---|
| 628 | case Part::SHAPE_CUBOID: |
---|
| 629 | cuboid.setDensity(density); |
---|
| 630 | cuboid.initialize(part); |
---|
| 631 | iterator = &cuboid; |
---|
| 632 | break; |
---|
| 633 | |
---|
| 634 | case Part::SHAPE_CYLINDER: |
---|
| 635 | cylinder.setDensity(density); |
---|
| 636 | cylinder.initialize(part); |
---|
| 637 | iterator = &cylinder; |
---|
| 638 | break; |
---|
| 639 | } |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | bool MeshBuilder::PartSurface::tryGetNext(Pt3D &point) |
---|
| 643 | { |
---|
| 644 | if (iterator == NULL) |
---|
| 645 | { |
---|
| 646 | return false; |
---|
| 647 | } |
---|
| 648 | |
---|
| 649 | return iterator->tryGetNext(point); |
---|
| 650 | } |
---|
| 651 | |
---|
| 652 | |
---|
| 653 | |
---|
| 654 | MeshBuilder::ModelSurface::ModelSurface(const double _density): |
---|
| 655 | Iterator(_density), surface(_density), model(NULL), index(0) |
---|
| 656 | {} |
---|
| 657 | |
---|
| 658 | void MeshBuilder::ModelSurface::initialize(const Model *model) |
---|
| 659 | { |
---|
| 660 | this->model = model; |
---|
| 661 | surface.setDensity(density); |
---|
| 662 | |
---|
| 663 | index = 0; |
---|
| 664 | if (model->getPartCount() > index) |
---|
| 665 | { |
---|
| 666 | surface.initialize(model->getPart(index)); |
---|
| 667 | } |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | bool MeshBuilder::ModelSurface::tryGetNext(Pt3D &point) |
---|
| 671 | { |
---|
| 672 | if (model == NULL) |
---|
| 673 | { |
---|
| 674 | return false; |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | do |
---|
| 678 | { |
---|
| 679 | while (!surface.tryGetNext(point)) |
---|
| 680 | { |
---|
| 681 | index += 1; |
---|
| 682 | if (model->getPartCount() > index) |
---|
| 683 | { |
---|
| 684 | surface.initialize(model->getPart(index)); |
---|
| 685 | } |
---|
| 686 | else |
---|
| 687 | { |
---|
| 688 | return false; |
---|
| 689 | } |
---|
| 690 | } |
---|
| 691 | } while (GeometryUtils::isPointInsideModelExcludingPart(point, model, index)); |
---|
| 692 | |
---|
| 693 | return true; |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | |
---|
| 697 | |
---|
| 698 | MeshBuilder::PartApices::PartApices(const double _density): |
---|
| 699 | Iterator(_density), cuboid(), cylinder(_density), ellipsoid(_density), iterator(NULL) |
---|
| 700 | {} |
---|
| 701 | |
---|
| 702 | void MeshBuilder::PartApices::initialize(const Part *part) |
---|
| 703 | { |
---|
| 704 | switch (part->shape) |
---|
| 705 | { |
---|
| 706 | case Part::SHAPE_ELLIPSOID: |
---|
| 707 | ellipsoid.setDensity(density); |
---|
| 708 | ellipsoid.initialize(part); |
---|
| 709 | iterator = &ellipsoid; |
---|
| 710 | break; |
---|
| 711 | |
---|
| 712 | case Part::SHAPE_CUBOID: |
---|
| 713 | cuboid.initialize(part); |
---|
| 714 | iterator = &cuboid; |
---|
| 715 | break; |
---|
| 716 | |
---|
| 717 | case Part::SHAPE_CYLINDER: |
---|
| 718 | cylinder.setDensity(density); |
---|
| 719 | cylinder.initialize(part); |
---|
| 720 | iterator = &cylinder; |
---|
| 721 | break; |
---|
| 722 | } |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | bool MeshBuilder::PartApices::tryGetNext(Pt3D &point) |
---|
| 726 | { |
---|
| 727 | if (iterator == NULL) |
---|
| 728 | { |
---|
| 729 | return false; |
---|
| 730 | } |
---|
| 731 | |
---|
| 732 | return iterator->tryGetNext(point); |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | |
---|
| 736 | |
---|
| 737 | MeshBuilder::ModelApices::ModelApices(const double _density): |
---|
| 738 | Iterator(_density), surface(_density), model(NULL), index(0) |
---|
| 739 | {} |
---|
| 740 | |
---|
| 741 | void MeshBuilder::ModelApices::initialize(const Model *model) |
---|
| 742 | { |
---|
| 743 | this->model = model; |
---|
| 744 | surface.setDensity(density); |
---|
| 745 | |
---|
| 746 | index = 0; |
---|
| 747 | if (model->getPartCount() > index) |
---|
| 748 | { |
---|
| 749 | surface.initialize(model->getPart(index)); |
---|
| 750 | } |
---|
| 751 | } |
---|
| 752 | |
---|
| 753 | bool MeshBuilder::ModelApices::tryGetNext(Pt3D &point) |
---|
| 754 | { |
---|
| 755 | if (model == NULL) |
---|
| 756 | { |
---|
| 757 | return false; |
---|
| 758 | } |
---|
| 759 | |
---|
| 760 | do |
---|
| 761 | { |
---|
| 762 | while (!surface.tryGetNext(point)) |
---|
| 763 | { |
---|
| 764 | index += 1; |
---|
| 765 | if (model->getPartCount() > index) |
---|
| 766 | { |
---|
| 767 | surface.initialize(model->getPart(index)); |
---|
| 768 | } |
---|
| 769 | else |
---|
| 770 | { |
---|
| 771 | return false; |
---|
| 772 | } |
---|
| 773 | } |
---|
| 774 | } while (GeometryUtils::isPointInsideModelExcludingPart(point, model, index)); |
---|
| 775 | |
---|
| 776 | return true; |
---|
| 777 | } |
---|
| 778 | |
---|
| 779 | |
---|
| 780 | |
---|
| 781 | MeshBuilder::BoundingBoxVolume::BoundingBoxVolume(const double _density): |
---|
| 782 | Iterator(_density), edge(_density), area(_density), volume(_density), length(0.0), width(0.0), height(0.0) |
---|
| 783 | {} |
---|
| 784 | |
---|
| 785 | bool MeshBuilder::BoundingBoxVolume::initialize(const Model &model) |
---|
| 786 | { |
---|
| 787 | Pt3D lowerBoundary, upperBoundary; |
---|
| 788 | ModelGeometryInfo::boundingBox(model, lowerBoundary, upperBoundary); |
---|
| 789 | initialize(lowerBoundary, upperBoundary); |
---|
[234] | 790 | return true; |
---|
[191] | 791 | } |
---|
| 792 | |
---|
| 793 | bool MeshBuilder::BoundingBoxVolume::initialize(const Pt3D &lowerBoundary, const Pt3D &upperBoundary) |
---|
| 794 | { |
---|
| 795 | length = Pt3D(upperBoundary.x - lowerBoundary.x, 0.0, 0.0); |
---|
| 796 | width = Pt3D(0.0, upperBoundary.y - lowerBoundary.y, 0.0); |
---|
| 797 | height = Pt3D(0.0, 0.0, upperBoundary.z - lowerBoundary.z); |
---|
| 798 | |
---|
| 799 | edge.setDensity(density); |
---|
| 800 | edge.initialize(lowerBoundary, lowerBoundary + length); |
---|
| 801 | |
---|
| 802 | area.setDensity(density); |
---|
| 803 | area.initialize(lowerBoundary, lowerBoundary + width); |
---|
| 804 | |
---|
| 805 | volume = Segment(density); |
---|
[234] | 806 | return true; |
---|
[191] | 807 | } |
---|
| 808 | |
---|
| 809 | bool MeshBuilder::BoundingBoxVolume::tryGetNext(Pt3D &point) |
---|
| 810 | { |
---|
| 811 | while (!volume.tryGetNext(point)) |
---|
| 812 | { |
---|
| 813 | while (!area.tryGetNext(point)) |
---|
| 814 | { |
---|
| 815 | if (edge.tryGetNext(point)) |
---|
| 816 | { |
---|
| 817 | area.initialize(point, point + width); |
---|
| 818 | } |
---|
| 819 | else |
---|
| 820 | { |
---|
| 821 | return false; |
---|
| 822 | } |
---|
| 823 | } |
---|
| 824 | |
---|
| 825 | volume.initialize(point, point + height); |
---|
| 826 | } |
---|
| 827 | |
---|
| 828 | return true; |
---|
| 829 | } |
---|