// ====================================================================== // // Copyright (c) 1999 The CGAL Consortium // // This software and related documentation is part of an INTERNAL release // of the Computational Geometry Algorithms Library (CGAL). It is not // intended for general use. // // ---------------------------------------------------------------------- // release : // release_date : 2000, October 15 // // source : webS3/S3.lw // file : include/CGAL/SimpleCartesian/TetrahedronS3.h // package : S3 (1.7) // maintainer : Stefan Schirra // revision : 1.7 // revision_date : 15 Oct 2000 // author(s) : Stefan Schirra // based on code by // Andreas Fabri and // Herve Brönnimann // // coordinator : MPI, Saarbrücken // ====================================================================== #ifndef CGAL_TETRAHEDRONS3_H #define CGAL_TETRAHEDRONS3_H #include #include #include #include CGAL_BEGIN_NAMESPACE template class TetrahedronS3 { public: TetrahedronS3() {} TetrahedronS3(const PointS3& p, const PointS3& q, const PointS3& r, const PointS3& s); const PointS3& vertex(int i) const; const PointS3& operator[](int i) const; bool operator==(const TetrahedronS3& t) const; bool operator!=(const TetrahedronS3& t) const; long id() const; Bbox_3 bbox() const; TetrahedronS3 transform(const Aff_transformationS3& t) const; Orientation orientation() const; Oriented_side oriented_side(const PointS3& p) const; Bounded_side bounded_side(const PointS3& p) const; bool has_on_boundary(const PointS3& p) const; bool has_on_positive_side(const PointS3& p) const; bool has_on_negative_side(const PointS3& p) const; bool has_on_bounded_side(const PointS3& p) const; bool has_on_unbounded_side(const PointS3& p) const; bool is_degenerate() const; // private: PointS3 e0; PointS3 e1; PointS3 e2; PointS3 e3; }; template < class FT > TetrahedronS3::TetrahedronS3(const PointS3& p, const PointS3& q, const PointS3& r, const PointS3& s) : e0(p), e1(q), e2(r), e3(s) {} template < class FT > bool TetrahedronS3::operator==(const TetrahedronS3& t) const { if ( orientation() != t.orientation() ) return false; std::vector< PointS3 > V1; std::vector< PointS3 > V2; typename std::vector< PointS3 >::iterator uniq_end1; typename std::vector< PointS3 >::iterator uniq_end2; int k; for ( k=0; k < 4; k++) V1.push_back( vertex(k)); for ( k=0; k < 4; k++) V2.push_back( t.vertex(k)); std::sort(V1.begin(), V1.end(), Less_xyz< PointS3 >()); std::sort(V2.begin(), V2.end(), Less_xyz< PointS3 >()); uniq_end1 = std::unique( V1.begin(), V1.end()); uniq_end2 = std::unique( V2.begin(), V2.end()); V1.erase( uniq_end1, V1.end()); V2.erase( uniq_end2, V2.end()); return V1 == V2; } template < class FT > inline bool TetrahedronS3::operator!=(const TetrahedronS3& t) const { return !(*this == t); } template < class FT > const PointS3& TetrahedronS3::vertex(int i) const { // modulo 4 is a logical operation, hence cheap if (i<0) i=(i%4)+4; else if (i>3) i=i%4; switch (i) { case 0: return e0; case 1: return e1; case 2: return e2; default: return e3; } } template < class FT > inline const PointS3& TetrahedronS3::operator[](int i) const { return vertex(i); } template < class FT > inline bool TetrahedronS3::has_on_boundary(const PointS3& p) const { return oriented_side(p) == ON_ORIENTED_BOUNDARY; } template < class FT > inline bool TetrahedronS3::has_on_positive_side(const PointS3& p) const { return oriented_side(p) == ON_POSITIVE_SIDE; } template < class FT > inline bool TetrahedronS3::has_on_negative_side(const PointS3& p) const { return oriented_side(p) == ON_NEGATIVE_SIDE; } template < class FT > inline bool TetrahedronS3::has_on_bounded_side(const PointS3& p) const { return oriented_side(p) == ON_BOUNDED_SIDE; } template < class FT > inline bool TetrahedronS3::has_on_unbounded_side(const PointS3& p) const { return oriented_side(p) == ON_UNBOUNDED_SIDE; } template < class FT > Orientation TetrahedronS3::orientation() const { return CGAL::orientation(vertex(0), vertex(1), vertex(2), vertex(3)); } template < class FT > Oriented_side TetrahedronS3::oriented_side(const PointS3& p) const { Orientation o = orientation(); if (o != ZERO) return Oriented_side(o * bounded_side(p)); CGAL_assertion (!is_degenerate()); return ON_ORIENTED_BOUNDARY; } template < class FT > Bounded_side TetrahedronS3::bounded_side(const PointS3& p) const { FT alpha, beta, gamma; VectorS3 v0 = vertex(1)-vertex(0); VectorS3 v1 = vertex(2)-vertex(0); VectorS3 v2 = vertex(3)-vertex(0); VectorS3 v3 = p - vertex(0); solve(v0.x(), v0.y(), v0.z(), v1.x(), v1.y(), v1.z(), v2.x(), v2.y(), v2.z(), v3.x(), v3.y(), v3.z(), alpha, beta, gamma); if ( (alpha < FT(0)) || (beta < FT(0)) || (gamma < FT(0)) || (alpha + beta + gamma > FT(1)) ) return ON_UNBOUNDED_SIDE; if ( (alpha == FT(0)) || (beta == FT(0)) || (gamma == FT(0)) || (alpha+beta+gamma == FT(1)) ) return ON_BOUNDARY; return ON_BOUNDED_SIDE; } template < class FT > bool TetrahedronS3::is_degenerate() const { PlaneS3 plane(vertex(0), vertex(1), vertex(2)); return (plane.is_degenerate()) ? true : plane.has_on_boundary(vertex(3)); } template < class FT > inline Bbox_3 TetrahedronS3::bbox() const { return vertex(0).bbox() + vertex(1).bbox() + vertex(2).bbox() + vertex(3).bbox(); } template < class FT > inline TetrahedronS3 TetrahedronS3::transform(const Aff_transformationS3& t) const { return TetrahedronS3(t.transform(vertex(0)), t.transform(vertex(1)), t.transform(vertex(2)), t.transform(vertex(3))); } #ifndef CGAL_NO_OSTREAM_INSERT_TETRAHEDRONS3 template < class FT > std::ostream& operator<<(std::ostream& os, const TetrahedronS3& t) { switch(os.iword(IO::mode)) { case IO::ASCII : return os << t[0] << ' ' << t[1] << ' ' << t[2] << ' ' << t[3]; case IO::BINARY : return os << t[0] << t[1] << t[2] << t[3]; default: os << "TetrahedronS3(" << t[0] << ", " << t[1] << ", " << t[2] ; os << ", " << t[3] << ")"; return os; } } #endif // CGAL_NO_OSTREAM_INSERT_TETRAHEDRONS3 #ifndef CGAL_NO_ISTREAM_EXTRACT_TETRAHEDRONS3 template < class FT > std::istream& operator>>(std::istream& is, TetrahedronS3& t) { PointS3 p, q, r, s; is >> p >> q >> r >> s; t = TetrahedronS3(p, q, r, s); return is; } #endif // CGAL_NO_ISTREAM_EXTRACT_TETRAHEDRONS3 CGAL_END_NAMESPACE #endif