608 {
609 const MInt COPLANAR = 0;
610 const MInt FRONT = 1;
612 const MInt SPANNING = 3;
613
614 MInt polygonType = 0;
615 std::vector<MInt> types;
616
617
618 MBool normalsEqual =
false;
619 if(std::abs(std::abs(this->
normal.
xx[0]) - std::abs(polygon->plane.normal.xx[0])) < MFloatEps
620 && std::abs(std::abs(this->normal.xx[1]) - std::abs(polygon->plane.normal.xx[1])) < MFloatEps
621 && std::abs(std::abs(this->normal.xx[2]) - std::abs(polygon->plane.normal.xx[2])) < MFloatEps)
622 normalsEqual = true;
623
624 MBool coplanarVertex =
false;
625 for(
MInt i = 0; (unsigned)i < polygon->vertices.size(); i++) {
627 MInt type = (t < -
eps) ? BACK : (t >
eps) ? FRONT : COPLANAR;
628 polygonType |= type;
629 types.push_back(type);
630 if(type == 0) coplanarVertex = true;
631 }
632 ASSERT(!(std::isnan(polygon->plane.normal.xx[0])), "");
633
634 if(normalsEqual && coplanarVertex) polygonType = COPLANAR;
635
636 switch(polygonType) {
637 case COPLANAR:
638 (this->
normal.
dot((polygon->plane.normal)) > F0 ? coplanarFront : coplanarBack)->push_back(*polygon);
639 break;
640 case FRONT:
641 front->push_back(*polygon);
642 break;
643 case BACK:
644 back->push_back(*polygon);
645 break;
646 case SPANNING: {
647 std::vector<CsgVertex> f;
648 std::vector<CsgVertex>
b;
649 for(
MInt i = 0; (unsigned)i < polygon->vertices.size(); i++) {
650 MInt j = (i + 1) % polygon->vertices.size();
651 MInt ti = types[i], tj = types[j];
652 CsgVertex vi = polygon->vertices[i];
653 CsgVertex vj = polygon->vertices[j];
654 if(ti != BACK) f.push_back(vi);
655 if(ti != FRONT)
b.push_back(ti != BACK ? vi.clone() : vi);
656 if((ti | tj) == SPANNING) {
658 CsgVertex v = vi.interpolate(&vj, t);
659 f.push_back(v);
660 b.push_back(v.clone());
661 }
662 }
663 if(f.size() >= 3)
664 front->push_back(
665 CsgPolygon(f, polygon->setIndex, polygon->faceId, polygon->faceType, polygon->bodyId, polygon->plane));
667 back->push_back(
668 CsgPolygon(
b, polygon->setIndex, polygon->faceId, polygon->faceType, polygon->bodyId, polygon->plane));
669 } break;
670 default:
671 break;
672 }
673 }