1 // Gmsh - Copyright (C) 1997-2021 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef GMSH_SURFACE_H
7 #define GMSH_SURFACE_H
8 
9 #include <cmath>
10 #include <map>
11 #include "Pair.h"
12 #include "Range.h"
13 #include "SPoint2.h"
14 #include "SPoint3.h"
15 #include "SVector3.h"
16 #include "SBoundingBox3d.h"
17 #include "Numeric.h"
18 
19 class gmshSurface {
20 protected:
21   static std::map<int, gmshSurface *> allGmshSurfaces;
22 
23 public:
24   // there are points define in this surface parameterization
25   bool vertex_defined_on_surface;
~gmshSurface()26   virtual ~gmshSurface() {}
reset()27   static void reset()
28   {
29     auto it = allGmshSurfaces.begin();
30     for(; it != allGmshSurfaces.end(); ++it) {
31       if(!it->second->vertex_defined_on_surface) delete it->second;
32     }
33     allGmshSurfaces.clear();
34   };
35   static gmshSurface *getSurface(int tag);
36   virtual Range<double> parBounds(int i) const = 0;
37   // Underlying geometric representation of this entity.
38   enum gmshSurfaceType {
39     Plane,
40     Nurbs,
41     Cylinder,
42     Sphere,
43     Cone,
44     Torus,
45     ParametricSurface,
46     PolarSphere
47   };
48   virtual gmshSurface::gmshSurfaceType geomType() const = 0;
49   virtual SPoint3 point(double par1, double par2) const = 0;
point(const SPoint2 & p)50   virtual SPoint3 point(const SPoint2 &p) const { return point(p.x(), p.y()); }
51   virtual SPoint2 parFromPoint(double x, double y, double z);
52   // Return the normal to the face at the given parameter location.
53   virtual SVector3 normal(const SPoint2 &param) const;
54   // Return the first derivate of the face at the parameter location.
55   virtual Pair<SVector3, SVector3> firstDer(const SPoint2 &param);
56   virtual double getMetricEigenvalue(const SPoint2 &);
57 };
58 
59 class gmshSphere : public gmshSurface {
60 private:
61   double xc, yc, zc, r;
gmshSphere(double _x,double _y,double _z,double _r)62   gmshSphere(double _x, double _y, double _z, double _r)
63     : xc(_x), yc(_y), zc(_z), r(_r)
64   {
65   }
66 
67 public:
68   static gmshSurface *NewSphere(int _iSphere, double _x, double _y, double _z,
69                                 double _r);
parBounds(int i)70   virtual Range<double> parBounds(int i) const
71   {
72     if(i == 0)
73       return Range<double>(0., 2 * M_PI);
74     else
75       return Range<double>(0., M_PI);
76   }
geomType()77   virtual gmshSurface::gmshSurfaceType geomType() const
78   {
79     return gmshSurface::Sphere;
80   }
81   using gmshSurface::point;
82   virtual SPoint3 point(double par1, double par2) const;
normal(const SPoint2 & param)83   virtual SVector3 normal(const SPoint2 &param) const
84   {
85     SPoint3 p1 = gmshSurface::point(param);
86     SPoint3 p2(xc, yc, zc);
87     SVector3 n(p1, p2);
88     n.normalize();
89     return n;
90   }
91 };
92 
93 #include "stdio.h"
94 class gmshPolarSphere : public gmshSurface {
95 private:
96   double r;
97   SPoint3 o;
98   gmshPolarSphere(double x, double y, double z, double _r);
99 
100 public:
101   static gmshSurface *NewPolarSphere(int _iSphere, double _x, double _y,
102                                      double _z, double _r);
parBounds(int i)103   virtual Range<double> parBounds(int i) const
104   {
105     if(i == 0)
106       return Range<double>(-M_PI, M_PI);
107     else
108       return Range<double>(-M_PI, M_PI);
109   }
geomType()110   virtual gmshSurface::gmshSurfaceType geomType() const
111   {
112     return gmshSurface::PolarSphere;
113   }
114   using gmshSurface::point;
115   virtual SPoint3 point(double par1, double par2) const;
normal(const SPoint2 & param)116   virtual SVector3 normal(const SPoint2 &param) const
117   {
118     SPoint3 p1 = gmshSurface::point(param);
119     SVector3 n(p1, o);
120     n.normalize();
121     return n;
122   }
getMetricEigenvalue(const SPoint2 & p)123   virtual double getMetricEigenvalue(const SPoint2 &p)
124   {
125     double l = (4 * r * r) / (4 * r * r + p.x() * p.x() + p.y() * p.y());
126     return l * l;
127   }
128 };
129 
130 class mathEvaluator;
131 
132 class gmshParametricSurface : public gmshSurface {
133 private:
134   mathEvaluator *_f;
135   gmshParametricSurface(char *, char *, char *);
136   ~gmshParametricSurface();
137 
138 public:
139   static gmshSurface *NewParametricSurface(int iSurf, char *, char *, char *);
140   virtual Range<double> parBounds(int i) const;
geomType()141   virtual gmshSurface::gmshSurfaceType geomType() const
142   {
143     return gmshSurface::ParametricSurface;
144   }
145   using gmshSurface::point;
146   virtual SPoint3 point(double par1, double par2) const;
147 };
148 
149 #endif
150