G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsGeometry.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include <gsCore/gsFunction.h>
17 #include <gsCore/gsBoundary.h>
18 
19 
20 #define GISMO_BASIS_ACCESSORS \
21  Basis & basis() { return static_cast<Basis&>(*this->m_basis); } \
22  const Basis & basis() const { return static_cast<const Basis&>(*this->m_basis); }
23  // bool isProjective() const{ return Basis::IsRational; }
24 
25 namespace gismo
26 {
27 
91 template<class T>
92 class gsGeometry : public gsFunction<T>
93 {
94 
95 public:
97  typedef memory::shared_ptr< gsGeometry > Ptr;
98 
100  typedef memory::unique_ptr< gsGeometry > uPtr;
101 
102  typedef T Scalar_t;
103 
104 public:
105 
108 
111  gsGeometry() :m_basis( NULL ), m_id(0)
112  { }
113 
119  m_basis(basis.clone().release()), m_id(0)
120  {
121  m_coefs.swap(coefs);
122  GISMO_ASSERT( basis.size() == m_coefs.rows(),
123  "The coefficient matrix of the geometry (rows="<<m_coefs.rows()
124  <<") does not match the number of basis functions in its basis("
125  << basis.size() <<").");
126  }
127 
129  gsGeometry(const gsGeometry & o);
131 
132  gsGeometry& operator=( const gsGeometry & o);
133 
134  virtual ~gsGeometry();
135 
136 
137 #if EIGEN_HAS_RVALUE_REFERENCES
138  gsGeometry(gsGeometry&& other)
139  : m_coefs(std::move(other.m_coefs)), m_basis(other.m_basis),
140  m_id(std::move(other.m_id))
141  {
142  other.m_basis = NULL;
143  }
144  gsGeometry & operator=(gsGeometry&& other)
145  {
146  m_coefs.swap(other.m_coefs); other.m_coefs.clear();
147  delete m_basis;
148  m_basis = other.m_basis; other.m_basis = NULL;
149  m_id = std::move(other.m_id);
150  return *this;
151  }
152 #endif
153 
154 public:
155 
175  // Look at gsFunction class for documentation
176  void eval_into(const gsMatrix<T>& u, gsMatrix<T>& result) const;
177 
221  // Look at gsFunction class for documentation
222  virtual void deriv_into(const gsMatrix<T>& u, gsMatrix<T>& result) const;
223 
224 
243  // Look at gsFunctionSet class for documentation
244  virtual void deriv2_into(const gsMatrix<T>& u, gsMatrix<T>& result) const;
245 
246  virtual void evalAllDers_into(const gsMatrix<T> & u, int n,
247  std::vector<gsMatrix<T> > & result,
248  bool sameElement = false) const;
249 
250  // Look at gsFunctionSet for documentation
251  virtual void compute(const gsMatrix<T> & in, gsFuncData<T> & out) const;
252 
254 
260  int orientation() const
261  {
262  if ( parDim() == geoDim() )
263  {
264  const T val = gsFunction<T>::jacobian( this->parameterCenter() ).determinant();
265  return (T(0) < val) - (val < (T)(0));
266  }
267  return 1;
268  }
269 
270  virtual void toggleOrientation();
271 
272  /*************************************************************************/
273 
276 
279  virtual const gsBasis<T> & basis() const = 0;
280 
283  virtual gsBasis<T> & basis() = 0;
284 
286  short_t targetDim() const { return this->coefDim(); }
287 
289  short_t coefDim() const { return static_cast<short_t>(m_coefs.cols()); }
290 
292  short_t geoDim() const { return this->coefDim(); }
293 
295  virtual short_t domainDim() const;
296 
298  short_t parDim() const;
299 
301  short_t coDim() const;
302 
304  gsMatrix<T> support() const;
305 
307  gsMatrix<T> parameterRange() const;
308 
310  //boxSide sideOf(const gsVector<T> & u); //
311 
313 
322  std::vector<boxSide> locateOn(const gsMatrix<T> & u, gsVector<bool> & onG2, gsMatrix<T> & preIm, bool lookForBoundary = false, real_t tol = 1.e-6) const; //
323 
324  // Whether the coefficients of this geometry are stored in projective or affine form
325  //virtual bool isProjective() const = 0;
326 
328 
329  /*************************************************************************/
330 
337  // todo: coefsSize() x (geoDim() + 1) if projective
340  gsMatrix<T> & coefs() { return this->m_coefs; }
341 
343  const gsMatrix<T> & coefs() const { return this->m_coefs; }
344 
346  typename gsMatrix<T>::RowXpr coef(index_t i) { return m_coefs.row(i); }
347 
349  typename gsMatrix<T>::ConstRowXpr coef(index_t i) const { return m_coefs.row(i); }
350 
352  T & coef(index_t i, index_t j)
353  {
354  GISMO_ASSERT( ((i < m_coefs.rows()) && (j < m_coefs.cols()) ),
355  "Coefficient or coordinate which is out of range requested.");
356  return m_coefs(i,j);
357  }
358 
360  const T coef(index_t i, index_t j) const
361  {
362  GISMO_ASSERT( ((i < m_coefs.rows()) && (j < m_coefs.cols()) ),
363  "Coefficient or coordinate which is out of range requested.");
364  return m_coefs(i,j);
365  }
366 
368  void setCoefs(gsMatrix<T> cc) { this->m_coefs.swap(cc); }
369 
371  index_t coefsSize() const { return m_coefs.rows(); }
372  // Warning: This can cause some clash while using periodic basis, since the ghost coefs are stored but ignored.
373 
374 
375 
377 
378  /*************************************************************************/
379 
387  void linearTransform(const gsMatrix<T>& mat)
389  {
390  this->m_coefs = this->m_coefs * mat.transpose();
391  }
392 
394  void rotate(T angle, const gsVector<T,3> & axis )
395  {
396  assert( geoDim() == 3 );
397  gsEigen::Transform<T,3,gsEigen::Affine>
398  rot( gsEigen::AngleAxis<T> (angle,axis.normalized()) );
399  // To do: Simpler way to use transforms ?
400  this->m_coefs = (this->m_coefs.rowwise().homogeneous() *
401  rot.matrix().transpose() ).leftCols(3) ;
402  }
403 
405  void rotate(T angle)
406  {
407  GISMO_ASSERT( geoDim() == 2, "Only for 2D");
408  gsEigen::Rotation2D<T> rot(angle);
409  this->m_coefs *= rot.matrix().transpose();
410  }
411 
413  void scale(T s, int coord = -1)
414  {
415  if ( coord == -1) // Uniform scaling
416  this->m_coefs *= s;
417  else if ( coord <geoDim() )//scale coordinate coord
418  this->m_coefs.col(coord) *= s;
419  }
420 
422  void scale(gsVector<T> const & v)
423  {
424  GISMO_ASSERT( v.rows() == this->m_coefs.cols(), "Sizes do not agree." );
425  this->m_coefs.array().rowwise() *= v.array().transpose();
426  }
427 
429  void translate(gsVector<T> const & v)
430  {
431  this->m_coefs.rowwise() += v.transpose();
432  }
433 
435  typename gsMatrix<T>::RowXpr
436  coefAtCorner(boxCorner const & c);
437 
439  typename gsMatrix<T>::ConstRowXpr
440  coefAtCorner(boxCorner const & c) const;
441 
443 
444  /*************************************************************************/
445 
448 
450  virtual void uniformRefine(int numKnots = 1, int mul=1, int dir=-1); // todo: int dir = -1
451 
453  virtual void uniformCoarsen(int numKnots = 1); // todo: int dir = -1
454 
460  void refineElements( std::vector<index_t> const & boxes );
461 
462  void unrefineElements( std::vector<index_t> const & boxes );
463 
464  typename gsGeometry::uPtr coord(const index_t c) const;
465 
467  void embed3d()
468  {
469  embed(3);
470  }
471 
476  void embed(index_t N, bool pad_right = true)
477  {
478  GISMO_ASSERT( N > 0, "Embed dimension must be positive");
479 
480  const index_t nc = N - m_coefs.cols();
481  if ( nc == 0 ) return;
482 
483  if (!pad_right && nc<0)
484  m_coefs.leftCols(N) = m_coefs.rightCols(N);
485  m_coefs.conservativeResize(gsEigen::NoChange, N);
486 
487  if ( nc > 0 )
488  {
489  if (pad_right)
490  m_coefs.rightCols(nc).setZero();
491  else
492  {
493  m_coefs.rightCols(N-nc) = m_coefs.leftCols(N-nc);
494  m_coefs.leftCols(nc).setZero();
495  }
496  }
497 # ifndef NDEBUG
498  else gsWarn<<"Coefficients projected (deleted)..\n";
499 # endif
500  }
501 
503  short_t degree(const short_t & i) const;
504 
506  virtual void insertKnot( T knot, index_t dir, index_t i = 1);
507 
511  virtual void degreeElevate(short_t const i = 1, short_t const dir = -1);
512 
516  virtual void degreeIncrease(short_t const i = 1, short_t const dir = -1);
517 
521  virtual void degreeReduce(short_t const i = 1, short_t const dir = -1);
522 
526  virtual void degreeDecrease(short_t const i = 1, short_t const dir = -1);
527 
530  virtual void hessian_into(const gsMatrix<T>& u, gsMatrix<T> & result,
531  index_t coord) const;
532 
534  void controlNet( gsMesh<T> & mesh) const;
535 
539  void outerNormal_into(const gsMatrix<T>& u, gsMatrix<T> & result) const;
540 
542  std::vector<gsGeometry *> boundary() const;
543 
545  virtual typename gsGeometry::uPtr boundary(boxSide const& s) const;
546 
548  virtual typename gsGeometry::uPtr iface(const boundaryInterface & bi,
549  const gsGeometry & other) const;
550 
552  typename gsGeometry::uPtr component(boxComponent const& bc) const;
553 
554  GISMO_UPTR_FUNCTION_PURE(gsGeometry, clone)
555 
556 
557  virtual std::ostream &print(std::ostream &os) const;
558 
560  virtual void merge( gsGeometry * other );
561 
562  virtual void toMesh(gsMesh<T> & msh, int npoints) const;
563 
564  typename gsGeometry::uPtr approximateLinearly() const;
565 
572  void evaluateMesh(gsMesh<T>& mesh) const;
573 
574 
578  virtual std::vector<gsGeometry<T>* > uniformSplit(index_t dir =-1 ) const;
579 
581 
584  gsGeometrySlice<T> getIsoParametricSlice(index_t dir_fixed, T par) const;
585 
588  T closestPointTo(const gsVector<T> & pt,
589  gsVector<T> & result,
590  const T accuracy = 1e-6,
591  const bool useInitialPoint = false) const;
592 
596  T directedHausdorffDistance(const gsGeometry & other,
597  const index_t nsamples = 1000,
598  const T accuracy = 1e-6) const;
599 
601  T HausdorffDistance( const gsGeometry & other,
602  const index_t nsamples = 1000,
603  const T accuracy = 1e-6,
604  const bool directed=false) const;
605 
607  void setId(const size_t i) { m_id = i; }
608 
610  size_t id() const { return m_id; }
611 
612 protected:
613  void swap(gsGeometry & other)
614  {
615  std::swap(m_basis, other.m_basis);
616  m_coefs.swap(other.m_coefs);
617  std::swap(m_id, other.m_id);
618  }
619 
620 protected:
621 
623  //todo: coefsSize() x (geoDim() + 1) if projective
625 
628 
631  size_t m_id;
632 
633 }; // class gsGeometry
634 
636 template<class T>
637 std::ostream &operator<<(std::ostream &os, const gsGeometry<T>& b)
638 {return b.print(os); }
639 
640 } // namespace gismo
641 
642 #include <gsCore/gsCurve.h>
643 #include <gsCore/gsSurface.h>
644 #include <gsCore/gsVolume.h>
645 #include <gsCore/gsBulk.h>
646 
647 namespace gismo
648 {
649 
650 // Generic traits for geometry with dimension known at compile time
651 template <short_t d, typename T>
652 struct gsGeoTraits
653 {
654  typedef gsGeometry<T> GeometryBase;
655 };
656 
657 // Traits for curve
658 template <typename T>
659 struct gsGeoTraits<1,T>
660 {
661  typedef gsCurve<T> GeometryBase;
662 };
663 
664 // Traits for surface
665 template <typename T>
666 struct gsGeoTraits<2,T>
667 {
668  typedef gsSurface<T> GeometryBase;
669 };
670 
671 // Traits for volume
672 template <typename T>
673 struct gsGeoTraits<3,T>
674 {
675  typedef gsVolume<T> GeometryBase;
676 };
677 
678 // Traits for bulk
679 template <typename T>
680 struct gsGeoTraits<4,T>
681 {
682  typedef gsBulk<T> GeometryBase;
683 };
684 
685 #ifdef GISMO_WITH_PYBIND11
686 
690  void pybind11_init_gsGeometry(pybind11::module &m);
691 
692 #endif // GISMO_WITH_PYBIND11
693 
694 } // namespace gismo
695 
696 
697 #ifndef GISMO_BUILD_LIB
698 #include GISMO_HPP_HEADER(gsGeometry.hpp)
699 #endif
virtual void compute(const gsMatrix< T > &in, gsFuncData< T > &out) const
Computes function data.
Definition: gsGeometry.hpp:539
void linearTransform(const gsMatrix< T > &mat)
Apply the given square matrix to every control point.
Definition: gsGeometry.h:388
std::vector< boxSide > locateOn(const gsMatrix< T > &u, gsVector< bool > &onG2, gsMatrix< T > &preIm, bool lookForBoundary=false, real_t tol=1.e-6) const
Get back the side of point u.
Definition: gsGeometry.hpp:606
virtual std::vector< gsGeometry< T > * > uniformSplit(index_t dir=-1) const
Definition: gsGeometry.hpp:280
gsGeometry()
Default constructor. Note: Derived constructors (except for the default) should assign m_basis to a v...
Definition: gsGeometry.h:111
void evaluateMesh(gsMesh< T > &mesh) const
Definition: gsGeometry.hpp:255
Abstract base class representing a geometry map.
Definition: gsGeometry.h:92
gsMatrix< T > m_coefs
Coefficient matrix of size coefsSize() x geoDim()
Definition: gsGeometry.h:624
gsMatrix< T >::RowXpr coef(index_t i)
Returns the i-th coefficient of the geometry as a row expression.
Definition: gsGeometry.h:346
std::vector< T * > release(std::vector< unique_ptr< T > > &cont)
Takes a vector of smart pointers, releases them and returns the corresponding raw pointers...
Definition: gsMemory.h:228
void controlNet(gsMesh< T > &mesh) const
Return the control net of the geometry.
Definition: gsGeometry.hpp:524
Provides declaration of a 4D bulk.
short_t coefDim() const
Dimension n of the coefficients (control points)
Definition: gsGeometry.h:289
T HausdorffDistance(const gsGeometry &other, const index_t nsamples=1000, const T accuracy=1e-6, const bool directed=false) const
Computes the Hausdorff distance between *this to other.
Definition: gsGeometry.hpp:377
memory::shared_ptr< gsGeometry > Ptr
Shared pointer for gsGeometry.
Definition: gsGeometry.h:97
gsMatrix< T > parameterRange() const
Returns the range of parameters as a matrix with two columns, [lower upper].
Definition: gsGeometry.hpp:198
gsBasis< T > * m_basis
Pointer to the basis of this geometry.
Definition: gsGeometry.h:627
virtual gsMatrix< T > parameterCenter() const
Returns a &quot;central&quot; point inside inside the parameter domain.
Definition: gsFunction.h:261
#define short_t
Definition: gsConfig.h:35
void outerNormal_into(const gsMatrix< T > &u, gsMatrix< T > &result) const
Computes the outer normals at parametric points u.
Definition: gsGeometry.hpp:419
void embed3d()
Embeds coefficients in 3D.
Definition: gsGeometry.h:467
Provides structs and classes related to interfaces and boundaries.
void refineElements(std::vector< index_t > const &boxes)
Refines the basis and adjusts the coefficients to keep the geometry the same.
Definition: gsGeometry.hpp:318
gsMatrix< T >::RowXpr coefAtCorner(boxCorner const &c)
Returns the control point at corner c.
Definition: gsGeometry.hpp:293
virtual void degreeElevate(short_t const i=1, short_t const dir=-1)
Elevate the degree by the given amount i for the direction dir. If dir is -1 then degree elevation is...
Definition: gsGeometry.hpp:436
Provides declaration of Surface abstract interface.
void scale(gsVector< T > const &v)
Apply Scaling coord-wise by a vector v.
Definition: gsGeometry.h:422
short_t degree(const short_t &i) const
Returns the degree wrt direction i.
Definition: gsGeometry.hpp:333
Provides declaration of Volume abstract interface.
size_t id() const
Returns the patch index for this patch.
Definition: gsGeometry.h:610
virtual index_t size() const
size
Definition: gsFunctionSet.h:578
virtual void degreeReduce(short_t const i=1, short_t const dir=-1)
Reduces the degree by the given amount i for the direction dir. If dir is -1 then degree reduction is...
Definition: gsGeometry.hpp:449
#define index_t
Definition: gsConfig.h:32
A function from a n-dimensional domain to an m-dimensional image.
Definition: gsFunction.h:59
virtual void evalAllDers_into(const gsMatrix< T > &u, int n, std::vector< gsMatrix< T > > &result, bool sameElement=false) const
Evaluate the nonzero functions and their derivatives up to order n at points u into result...
Definition: gsGeometry.hpp:178
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition: gsMatrix.h:38
virtual void degreeIncrease(short_t const i=1, short_t const dir=-1)
Elevate the degree by the given amount i for the direction dir. If dir is -1 then degree elevation is...
Definition: gsGeometry.hpp:462
index_t coefsSize() const
Return the number of coefficients (control points)
Definition: gsGeometry.h:371
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
gsMatrix< T >::ConstRowXpr coef(index_t i) const
Returns the i-th coefficient of the geometry as a row expression.
Definition: gsGeometry.h:349
T & coef(index_t i, index_t j)
Returns the j-th coordinate of the i-th coefficient of the geometry.
Definition: gsGeometry.h:352
virtual void insertKnot(T knot, index_t dir, index_t i=1)
Inserts knot knot at direction dir, i times.
Definition: gsGeometry.hpp:430
virtual void merge(gsGeometry *other)
Merge the given other geometry into this one.
Definition: gsGeometry.hpp:406
Class Representing a triangle mesh with 3D vertices.
Definition: gsMesh.h:31
void scale(T s, int coord=-1)
Apply Scaling by factor s.
Definition: gsGeometry.h:413
virtual std::ostream & print(std::ostream &os) const
Prints the object as a string.
Definition: gsGeometry.hpp:396
virtual void deriv2_into(const gsMatrix< T > &u, gsMatrix< T > &result) const
Evaluate second derivatives of the function at points u into result.
Definition: gsGeometry.hpp:174
void rotate(T angle)
Apply 2D Rotation by angle radians.
Definition: gsGeometry.h:405
int orientation() const
Evaluates if the geometry orientation coincide with the ambient orientation. This is computed in the ...
Definition: gsGeometry.h:260
#define gsWarn
Definition: gsDebug.h:50
virtual void deriv_into(const gsMatrix< T > &u, gsMatrix< T > &result) const
Evaluate derivatives of the function at points u into result.
Definition: gsGeometry.hpp:170
short_t coDim() const
Co-dimension of the geometric object.
Definition: gsGeometry.hpp:187
virtual short_t domainDim() const
Dimension d of the parameter domain (overriding gsFunction::domainDim()).
Definition: gsGeometry.hpp:184
const gsMatrix< T > & coefs() const
Returns the coefficient matrix of the geometry.
Definition: gsGeometry.h:343
std::vector< gsGeometry * > boundary() const
Get boundary of this geometry as a vector of new gsGeometry instances.
Definition: gsGeometry.hpp:423
gsGeometrySlice is a class representing an iso parametric slice of a geometry object. It stores a pointer to the geometry object, which is only valid as long as this object is alive. Methods for printing to paraview are available in gsWriteToParaview.h
Definition: gsGeometrySlice.h:27
virtual void uniformRefine(int numKnots=1, int mul=1, int dir=-1)
Refine the geometry uniformly, inserting numKnots new knots into each knot span.
Definition: gsGeometry.hpp:306
void embed(index_t N, bool pad_right=true)
Embeds coefficients in N dimensions For the new dimensions zeros are added (or removed) on the right ...
Definition: gsGeometry.h:476
const T coef(index_t i, index_t j) const
Returns the j-th coordinate of the i-th coefficient of the geometry.
Definition: gsGeometry.h:360
void setId(const size_t i)
Sets the patch index for this patch.
Definition: gsGeometry.h:607
Struct which represents a certain side of a box.
Definition: gsBoundary.h:84
short_t parDim() const
Dimension d of the parameter domain (same as domainDim()).
Definition: gsGeometry.hpp:190
gsGeometry & operator=(const gsGeometry &o)
Definition: gsGeometry.hpp:202
uPtr clone()
Clone methode. Produceds a deep copy inside a uPtr.
memory::unique_ptr< gsGeometry > uPtr
Unique pointer for gsGeometry.
Definition: gsGeometry.h:100
virtual gsGeometry::uPtr iface(const boundaryInterface &bi, const gsGeometry &other) const
Computes and returns the interface with other as a new geometry.
Definition: gsGeometry.hpp:232
void rotate(T angle, const gsVector< T, 3 > &axis)
Apply 3D Rotation by angle radians around axis axis.
Definition: gsGeometry.h:394
T closestPointTo(const gsVector< T > &pt, gsVector< T > &result, const T accuracy=1e-6, const bool useInitialPoint=false) const
Definition: gsGeometry.hpp:339
gsMatrix< T > support() const
Returns the range of parameters (same as parameterRange())
Definition: gsGeometry.hpp:193
void eval_into(const gsMatrix< T > &u, gsMatrix< T > &result) const
Evaluate the function at points u into result.
Definition: gsGeometry.hpp:166
virtual void degreeDecrease(short_t const i=1, short_t const dir=-1)
Reduces the degree by the given amount i for the direction dir. If dir is -1 then degree reduction is...
Definition: gsGeometry.hpp:475
void setCoefs(gsMatrix< T > cc)
Set the coefficient matrix of the geometry, taking ownership of the matrix.
Definition: gsGeometry.h:368
Provides declaration of Curve abstract interface.
gsGeometry::uPtr component(boxComponent const &bc) const
Get parametrization of box component bc as a new gsGeometry uPtr.
Definition: gsGeometry.hpp:240
gsGeometrySlice< T > getIsoParametricSlice(index_t dir_fixed, T par) const
Definition: gsGeometry.hpp:286
Struct which represents an interface between two patches.
Definition: gsBoundary.h:649
virtual void hessian_into(const gsMatrix< T > &u, gsMatrix< T > &result, index_t coord) const
Definition: gsGeometry.hpp:488
short_t targetDim() const
Dimension of the ambient physical space (overriding gsFunction::targetDim())
Definition: gsGeometry.h:286
Provides declaration of Function abstract interface.
virtual const gsBasis< T > & basis() const =0
Returns a const reference to the basis of the geometry.
size_t m_id
Definition: gsGeometry.h:631
Struct which represents a certain corner of a hyper-cube.
Definition: gsBoundary.h:291
T directedHausdorffDistance(const gsGeometry &other, const index_t nsamples=1000, const T accuracy=1e-6) const
Definition: gsGeometry.hpp:360
void translate(gsVector< T > const &v)
Apply translation by vector v.
Definition: gsGeometry.h:429
short_t geoDim() const
Dimension n of the absent physical space.
Definition: gsGeometry.h:292
A basis represents a family of scalar basis functions defined over a common parameter domain...
Definition: gsBasis.h:78
virtual void uniformCoarsen(int numKnots=1)
Coarsen the geometry uniformly, removing numKnots new knots into each knot span.
Definition: gsGeometry.hpp:312
gsMatrix< T > & coefs()
Definition: gsGeometry.h:340
Struct which represents a certain component (interior, face, egde, corner).
Definition: gsBoundary.h:445