⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 segment.h

📁 任意给定三维空间的点集
💻 H
字号:
/*-------------------------------------------------------------------- * cutting - compute palanr cuttings * Copyright (C) 1998 Sariel Har-Peled * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. *  * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. *  * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA  02111-1307, USA.\*--------------------------------------------------------------------*//*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* * segment.h - *     \*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/#ifndef  __SEGMENT__H#define  __SEGMENT__H#ifndef  __LINE__H#include  "line.h"#endif  /* __LINE__H */ class  segment{private:    point  varr[ 2 ];    bool  f_empty;public:    segment();    segment(const segment  &s);    segment(const point &sp, const point &ep);        ~segment();    segment   &operator=(const segment &s);    bool                 is_horizontal() const;    bool                 is_vertical() const;    bool                 has_on(const point &p) const;    bool                 collinear_has_on(const point &p) const;    bool                 operator==(const segment &s) const;    bool                 operator!=(const segment &s) const;    point     start() const;    point     end() const;    point     source() const;    point     target() const;    point     min() const;    point     max() const;    point     vertex(int i) const;    point     point_of(int i) const;    point     operator[](int i) const;    real      squared_length() const;    point     direction() const;    line      supporting_line() const;    segment   opposite() const;    bool                 intersect( const segment  & seg, point  & res ) const;    bool                 is_intersect( const line  & l )  const    {        point  tmp;        return  intersect( l, tmp );    }    bool                 intersect( const line  & l, point  & res ) const;    bool                 is_intersect( const segment  & seg )  const    {        point  tmp;        return  intersect( seg, tmp );    }    real      getPointParam( const point  & p )  const {      if  ( f_empty )        return  0;      point  diff( p - source() );      if  ( source().x != target().x )         return  diff.x / ( target().x - source().x );      else        return  diff.y / ( target().y - source().y );      return  0;    }    real      getXCoordParam( const real  & x )  const {      assert( ! is_empty() );            real  diff( x - source().x );      assert( source().x != target().x );      return  diff / ( target().x - source().x );    }    real      getYCoordParam( const real  & y )  const {      assert( ! is_empty() );            real  diff( y - source().y );      assert( source().y != target().y );      return  diff / ( target().y - source().y );    }    point     getPointByParam( const real  & param )  const {      return  point( (target() - source()) * param + source() );    }    bool  is_in_x_range( const real  & x ) const;    bool  is_in_y_range( const real  & x ) const;    bool      is_degenerate() const;    bool      is_empty() const;    bool      isContain( const point  & pnt ) const;    //    CGAL_Bbox_2          bbox const;};//inline bool   segment::intersect( const segment  & seg, point  & res ) const;inline bool   segment::intersect( const line  & l, point  & res ) const{  bool  f_intersect;  res = supporting_line().intersectx( l, f_intersect );  if  ( ! f_intersect )    return  false;  return   is_in_x_range( res.x )  &&  is_in_y_range( res.y );}inline point  segment::start() const{    return  varr[ 0 ];}inline point  segment::end() const{    return  varr[ 1 ];}inline point  segment::source() const{    return   varr[ 0 ];}inline point  segment::target() const{    return   varr[ 1 ];}inline segment::segment(){    varr[ 0 ] = varr[ 1 ] = point( 0, 0, 0 );    f_empty = true;}inline segment::segment(const segment  &s) {    f_empty = false;    varr[ 0 ] = s.varr[ 0 ];    varr[ 1 ] = s.varr[ 1 ];}inline segment::segment(const point &sp,                 const point &ep){    f_empty = false;    varr[ 0 ] = sp;    varr[ 1 ] = ep;}inline segment::~segment(){}inline segment &segment::operator=(const segment &s){    varr[ 0 ] = s.varr[ 0 ];    varr[ 1 ] = s.varr[ 1 ];    f_empty = s.f_empty;    return *this;}inline bool  segment::operator==(const segment &s) const{    return ( (source() == s.source())  && (target() == s.target()) );}inline bool  segment::operator!=(const segment &s) const{    return !(*this == s);}inline point  segment::min() const{    if  ( source().x < target().x )        return  source();    if  ( source().x > target().x )        return  target();    if  ( source().y < target().y )        return  source();    if  ( source().y > target().y )        return  target();    return  source();}inline   bool  CG_lexicographically_xy_smaller( const point  & a,                                                const point  & b ){    if  ( a.x < b.x )        return  true;    if  ( a.x > b.x )        return  false;    if  ( a.y < b.y )        return  true;    if  ( a.y > b.y )        return  false;    return  false;}inline point  segment::max() const{    return (CG_lexicographically_xy_smaller(source(),target())) ? target()        : source();}inline point segment::vertex(int i) const{    if(i%2 == 0) {        return source();    }    return target();}inline point segment::point_of(int i) const{    if(i%2 == 0) {        return source();    }    return target();}inline point  segment::operator[](int i) const{    return vertex(i);}inline real  segment::squared_length() const{    return  ( (target() - source()) * (target() - source()));}inline point   segment::direction() const{    return   point( target() - source() );}inline line    segment::supporting_line() const{    return   line( vertex( 0 ), vertex( 1 ) );}inline segment segment::opposite() const{    return segment(target(), source());}/*segment segment::transform(  const CGAL_Aff_transformationC2<F T> &t ) const                            {                             return segment(t.transform(source()),                      t.transform(target()));} CGAL_Bbox_2 segment::bbox const{   return source().bbox + target().bbox;}*/inline bool  segment::is_degenerate() const{    return  (source() == target());}inline bool  segment::is_empty() const{    return  f_empty;}inline bool segment::is_horizontal() const{    return (source().y == target().y);}inline bool segment::is_vertical() const{    return (source().x == target().x);}inline bool segment::has_on(const point &p) const{    return(( p == source() )           || ( p == target() )           || ( CG_collinear(source(), p, target())                &&( point(p - source())                    !=                    point (p - target()))                )           );} template <class NT>NT CG_abs(const NT &x){    return (x >= NT(0)) ? x: -x;}                  inline bool segment::collinear_has_on(const point &p)  const{    assert( CG_collinear(source(), p, target()) );    if ( CG_abs(target().x-source().x) > CG_abs(target().y-source().y)) {        if (p.x < source().x)            return (p.x >= target().x);        if (p.x <= target().x)            return true;        return (p.x == source().x);    } else {        if (p.y < source().y)            return (p.y >= target().y);        if (p.y <= target().y)            return true;        return (p.y == source().y);    }}inline  void   print( int  ind, const segment  & s ) {    print_spaces( ind );    printf( "s:(%g, %g) - (%g, %g)\n",             ( s.vertex( 0 ).x ), ( s.vertex( 0 ).y ),            (s.vertex( 1 ).x ), ( s.vertex( 1 ).y ) );}#else   /* __SEGMENT__H */#error  Header file segment.h included twice#endif  /* __SEGMENT__H *//*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* *      * segment.h - End of File\*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -