123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- #ifndef IGL_WINDINGNUMBERTREE_H
- #define IGL_WINDINGNUMBERTREE_H
- #include <list>
- #include <map>
- #include <Eigen/Dense>
- #include "WindingNumberMethod.h"
- namespace igl
- {
-
-
-
-
- template <
- typename Point,
- typename DerivedV,
- typename DerivedF >
- class WindingNumberTree
- {
- public:
-
-
- static std::map<
- std::pair<const WindingNumberTree*,const WindingNumberTree*>,
- typename DerivedV::Scalar>
- cached;
-
-
- static DerivedV dummyV;
- protected:
- WindingNumberMethod method;
- const WindingNumberTree * parent;
- std::list<WindingNumberTree * > children;
- typedef
- Eigen::Matrix<typename DerivedV::Scalar,Eigen::Dynamic,Eigen::Dynamic>
- MatrixXS;
- typedef
- Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,Eigen::Dynamic>
- MatrixXF;
-
-
-
- DerivedV & V;
-
- MatrixXS SV;
-
- MatrixXF F;
-
- MatrixXF cap;
-
- typename DerivedV::Scalar radius;
-
- Point center;
- public:
- inline WindingNumberTree();
-
- inline WindingNumberTree(
- const Eigen::MatrixBase<DerivedV> & V,
- const Eigen::MatrixBase<DerivedF> & F);
-
- inline WindingNumberTree(
- const WindingNumberTree<Point,DerivedV,DerivedF> & parent,
- const Eigen::MatrixBase<DerivedF> & F);
- inline virtual ~WindingNumberTree();
- inline void delete_children();
- inline virtual void set_mesh(
- const Eigen::MatrixBase<DerivedV> & V,
- const Eigen::MatrixBase<DerivedF> & F);
-
- inline void set_method( const WindingNumberMethod & m);
- public:
- inline const DerivedV & getV() const;
- inline const MatrixXF & getF() const;
- inline const MatrixXF & getcap() const;
-
- inline virtual void grow();
-
-
-
-
-
- inline virtual bool inside(const Point & p) const;
-
-
-
-
-
-
- inline typename DerivedV::Scalar winding_number(const Point & p) const;
-
-
- inline typename DerivedV::Scalar winding_number_all(const Point & p) const;
-
- inline typename DerivedV::Scalar winding_number_boundary(const Point & p) const;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- inline void print(const char * tab="");
-
-
-
-
-
- inline virtual typename DerivedV::Scalar max_abs_winding_number(const Point & p) const;
-
-
- inline virtual typename DerivedV::Scalar max_simple_abs_winding_number(const Point & p) const;
-
-
-
-
-
-
-
- inline virtual typename DerivedV::Scalar cached_winding_number(const WindingNumberTree & that, const Point & p) const;
- };
- }
- #include "WindingNumberTree.h"
- #include "winding_number.h"
- #include "triangle_fan.h"
- #include "exterior_edges.h"
- #include <igl/PI.h>
- #include <igl/remove_duplicate_vertices.h>
- #include <iostream>
- #include <limits>
- template <typename Point, typename DerivedV, typename DerivedF>
- std::map< std::pair<const igl::WindingNumberTree<Point,DerivedV,DerivedF>*,const igl::WindingNumberTree<Point,DerivedV,DerivedF>*>, typename DerivedV::Scalar>
- igl::WindingNumberTree<Point,DerivedV,DerivedF>::cached;
- template <typename Point, typename DerivedV, typename DerivedF>
- inline igl::WindingNumberTree<Point,DerivedV,DerivedF>::WindingNumberTree():
- method(EXACT_WINDING_NUMBER_METHOD),
- parent(NULL),
- V(dummyV),
- SV(),
- F(),
-
- cap(),
- radius(std::numeric_limits<typename DerivedV::Scalar>::infinity()),
- center(0,0,0)
- {
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline igl::WindingNumberTree<Point,DerivedV,DerivedF>::WindingNumberTree(
- const Eigen::MatrixBase<DerivedV> & _V,
- const Eigen::MatrixBase<DerivedF> & _F):
- method(EXACT_WINDING_NUMBER_METHOD),
- parent(NULL),
- V(dummyV),
- SV(),
- F(),
-
- cap(),
- radius(std::numeric_limits<typename DerivedV::Scalar>::infinity()),
- center(0,0,0)
- {
- set_mesh(_V,_F);
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline void igl::WindingNumberTree<Point,DerivedV,DerivedF>::set_mesh(
- const Eigen::MatrixBase<DerivedV> & _V,
- const Eigen::MatrixBase<DerivedF> & _F)
- {
- using namespace std;
-
-
-
- MatrixXF SF,SVI,SVJ;
- igl::remove_duplicate_vertices(_V,_F,0.0,SV,SVI,SVJ,F);
- triangle_fan(igl::exterior_edges(F),cap);
- V = SV;
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline igl::WindingNumberTree<Point,DerivedV,DerivedF>::WindingNumberTree(
- const igl::WindingNumberTree<Point,DerivedV,DerivedF> & parent,
- const Eigen::MatrixBase<DerivedF> & _F):
- method(parent.method),
- parent(&parent),
- V(parent.V),
- SV(),
- F(_F),
- cap(triangle_fan(igl::exterior_edges(_F)))
- {
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline igl::WindingNumberTree<Point,DerivedV,DerivedF>::~WindingNumberTree()
- {
- delete_children();
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline void igl::WindingNumberTree<Point,DerivedV,DerivedF>::delete_children()
- {
- using namespace std;
-
- typename list<WindingNumberTree<Point,DerivedV,DerivedF>* >::iterator cit = children.begin();
- while(cit != children.end())
- {
-
- delete (* cit);
-
- cit = children.erase(cit);
- }
- }
-
- template <typename Point, typename DerivedV, typename DerivedF>
- inline void igl::WindingNumberTree<Point,DerivedV,DerivedF>::set_method(const WindingNumberMethod & m)
- {
- this->method = m;
- for(auto child : children)
- {
- child->set_method(m);
- }
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline const DerivedV & igl::WindingNumberTree<Point,DerivedV,DerivedF>::getV() const
- {
- return V;
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline const typename igl::WindingNumberTree<Point,DerivedV,DerivedF>::MatrixXF&
- igl::WindingNumberTree<Point,DerivedV,DerivedF>::getF() const
- {
- return F;
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline const typename igl::WindingNumberTree<Point,DerivedV,DerivedF>::MatrixXF&
- igl::WindingNumberTree<Point,DerivedV,DerivedF>::getcap() const
- {
- return cap;
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline void igl::WindingNumberTree<Point,DerivedV,DerivedF>::grow()
- {
-
- return;
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline bool igl::WindingNumberTree<Point,DerivedV,DerivedF>::inside(const Point & ) const
- {
- return true;
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline typename DerivedV::Scalar
- igl::WindingNumberTree<Point,DerivedV,DerivedF>::winding_number(const Point & p) const
- {
- using namespace std;
-
-
- if(inside(p))
- {
-
- if(children.size()>0)
- {
-
- typename DerivedV::Scalar sum = 0;
- for(
- typename list<WindingNumberTree<Point,DerivedV,DerivedF>* >::const_iterator cit = children.begin();
- cit != children.end();
- cit++)
- {
- switch(method)
- {
- case EXACT_WINDING_NUMBER_METHOD:
- sum += (*cit)->winding_number(p);
- break;
- case APPROX_SIMPLE_WINDING_NUMBER_METHOD:
- case APPROX_CACHE_WINDING_NUMBER_METHOD:
-
-
- sum += (*cit)->winding_number(p);
-
- break;
- default:
- assert(false);
- break;
- }
- }
- return sum;
- }else
- {
- return winding_number_all(p);
- }
- }else{
-
-
-
- if((cap.rows() - 2) < F.rows())
- {
- switch(method)
- {
- case EXACT_WINDING_NUMBER_METHOD:
- return winding_number_boundary(p);
- case APPROX_SIMPLE_WINDING_NUMBER_METHOD:
- {
- typename DerivedV::Scalar dist = (p-center).norm();
-
- if(dist>1.0*radius)
- {
- return 0;
- }else
- {
- return winding_number_boundary(p);
- }
- }
- case APPROX_CACHE_WINDING_NUMBER_METHOD:
- {
- return parent->cached_winding_number(*this,p);
- }
- default: assert(false);break;
- }
- }else
- {
-
- return winding_number_all(p);
- }
- }
- return 0;
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline typename DerivedV::Scalar
- igl::WindingNumberTree<Point,DerivedV,DerivedF>::winding_number_all(const Point & p) const
- {
- return igl::winding_number(V,F,p);
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline typename DerivedV::Scalar
- igl::WindingNumberTree<Point,DerivedV,DerivedF>::winding_number_boundary(const Point & p) const
- {
- using namespace Eigen;
- using namespace std;
- return igl::winding_number(V,cap,p);
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline void igl::WindingNumberTree<Point,DerivedV,DerivedF>::print(const char * tab)
- {
- using namespace std;
-
- cout<<tab<<"["<<endl<<F<<endl<<"]";
-
- for(
- typename list<WindingNumberTree<Point,DerivedV,DerivedF>* >::iterator cit = children.begin();
- cit != children.end();
- cit++)
- {
- cout<<","<<endl;
- (*cit)->print((string(tab)+"").c_str());
- }
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline typename DerivedV::Scalar
- igl::WindingNumberTree<Point,DerivedV,DerivedF>::max_abs_winding_number(const Point & ) const
- {
- return std::numeric_limits<typename DerivedV::Scalar>::infinity();
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline typename DerivedV::Scalar
- igl::WindingNumberTree<Point,DerivedV,DerivedF>::max_simple_abs_winding_number(
- const Point & ) const
- {
- using namespace std;
- return numeric_limits<typename DerivedV::Scalar>::infinity();
- }
- template <typename Point, typename DerivedV, typename DerivedF>
- inline typename DerivedV::Scalar
- igl::WindingNumberTree<Point,DerivedV,DerivedF>::cached_winding_number(
- const igl::WindingNumberTree<Point,DerivedV,DerivedF> & that,
- const Point & p) const
- {
- using namespace std;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- bool is_far = this->radius<that.radius;
- if(is_far)
- {
- typename DerivedV::Scalar a = atan2(
- that.radius - this->radius,
- (that.center - this->center).norm());
- assert(a>0);
- is_far = (a<PI/8.0);
- }
- if(is_far)
- {
-
- pair<const WindingNumberTree*,const WindingNumberTree*> this_that(this,&that);
-
- if(cached.count(this_that)==0)
- {
- cached[this_that] =
- that.winding_number_boundary(this->center);
- }
- return cached[this_that];
- }else if(children.size() == 0)
- {
-
- return that.winding_number_boundary(p);
- }else
- {
- for(
- typename list<WindingNumberTree<Point,DerivedV,DerivedF>* >::const_iterator cit = children.begin();
- cit != children.end();
- cit++)
- {
- if((*cit)->inside(p))
- {
- return (*cit)->cached_winding_number(that,p);
- }
- }
-
-
-
-
- assert(false);
- }
- return 0;
- }
- template <
- typename Point,
- typename DerivedV,
- typename DerivedF >
- DerivedV igl::WindingNumberTree<Point,DerivedV,DerivedF>::dummyV;
- #endif
|