1 #ifndef FILE_CSGEOM
2 #define FILE_CSGEOM
3 
4 /**************************************************************************/
5 /* File:   csgeom.hh                                                      */
6 /* Author: Joachim Schoeberl                                              */
7 /* Date:   27. Nov. 97                                                    */
8 /**************************************************************************/
9 
10 namespace netgen
11 {
12 
13   /**
14      Constructive Solid Geometry
15   */
16 
17 
18   class TriangleApproximation;
19   class TATriangle;
20 
21 
22   /**
23      A top level object is an entity to be meshed.
24      I can be either a solid, or one surface patch of a solid.
25   */
26   class DLL_HEADER TopLevelObject
27   {
28     Solid * solid;
29     Surface * surface;
30 
31     double red, blue, green;
32     bool visible, transp;
33     double maxh;
34     string material;
35     int layer;
36     int bc;     // for surface patches, only
37     string bcname;
38 
39   public:
40     TopLevelObject (Solid * asolid,
41 		    Surface * asurface = NULL);
42     // default constructor for archive
TopLevelObject()43     TopLevelObject() {}
44 
DoArchive(Archive & archive)45     void DoArchive(Archive& archive)
46     {
47       archive & solid & surface & red & blue & green & visible & transp & maxh
48         & material & layer & bc & bcname;
49     }
GetSolid() const50     const Solid * GetSolid() const { return solid; }
GetSolid()51     Solid * GetSolid() { return solid; }
52 
GetSurface() const53     const Surface * GetSurface () const { return surface; }
GetSurface()54     Surface  * GetSurface () { return surface; }
55 
56     void GetData (ostream & ost);
57     void SetData (istream & ist);
58 
SetMaxH(double amaxh)59     void SetMaxH (double amaxh) { maxh = amaxh; }
GetMaxH() const60     double GetMaxH () const { return maxh; }
61 
SetRGB(double ared,double agreen,double ablue)62     void SetRGB (double ared, double agreen, double ablue)
63     {
64       red = ared;
65       green = agreen;
66       blue = ablue;
67     }
68 
GetRed() const69     double GetRed () const { return red; }
GetGreen() const70     double GetGreen () const { return green; }
GetBlue() const71     double GetBlue () const { return blue; }
72 
SetTransparent(bool atransp)73     void SetTransparent (bool atransp)
74     { transp = atransp; }
GetTransparent() const75     bool GetTransparent () const { return transp; }
76 
SetVisible(bool avisible)77     void SetVisible (bool avisible)
78     { visible = avisible; }
GetVisible() const79     bool GetVisible () const { return visible; }
80 
GetMaterial() const81     const string GetMaterial () const { return material; }
SetMaterial(const string & mat)82     void SetMaterial (const string & mat) { material = mat; }
83 
GetLayer() const84     int GetLayer () const { return layer; }
SetLayer(int alayer)85     void SetLayer (int alayer) { layer = alayer; }
86 
SetBCProp(int abc)87     void SetBCProp (int abc) { bc = abc; }
GetBCProp() const88     int GetBCProp () const { return bc; }
89 
SetBCName(string abc)90     void SetBCName (string abc) { bcname = abc; }
GetBCName() const91     const string GetBCName () const { return bcname; }
92   };
93 
94 
95 
96 
97 
98   /**
99      CSGeometry has the whole geometric information
100   */
101   class DLL_HEADER CSGeometry : public NetgenGeometry
102   {
103   private:
104     /// all surfaces
105     SymbolTable<Surface*> surfaces;
106 
107   public:
108     /// primitive of surface
109     NgArray<const Primitive*> surf2prim;
110 
111   private:
112     NgArray<Surface*> delete_them;
113 
114     /// all named solids
115     SymbolTable<Solid*> solids;
116 
117     /// all 2d splinecurves
118     SymbolTable<shared_ptr<SplineGeometry<2>>> splinecurves2d;
119     /// all 3d splinecurves
120     SymbolTable<shared_ptr<SplineGeometry<3>>> splinecurves3d;
121 
122     /// all top level objects: solids and surfaces
123     NgArray<TopLevelObject*> toplevelobjects;
124 
125   public:
126     /// additional points specified by user
127     class UserPoint : public Point<3>
128     {
129       int index;
130       string name;
131     public:
132       UserPoint() = default;
UserPoint(Point<3> p,int _index)133       UserPoint (Point<3> p, int _index) : Point<3>(p), index(_index) { ; }
UserPoint(Point<3> p,const string & _name)134       UserPoint (Point<3> p, const string & _name) : Point<3>(p), name(_name), index(-1) { ; }
GetIndex() const135       int GetIndex() const { return index; }
GetName() const136       const string & GetName() const { return name; }
DoArchive(Archive & archive)137       void DoArchive(Archive& archive)
138       {
139         archive & index & name;
140         Point<3>::DoArchive(archive);
141       }
142     };
143 
144   private:
145     // NgArray<Point<3> > userpoints;
146     NgArray<UserPoint> userpoints;
147     NgArray<double> userpoints_ref_factor;
148 
149     mutable NgArray<Point<3> > identpoints;
150 
151     /// triangular approximation of top level objects
152     NgArray<TriangleApproximation*> triapprox;
153 
154     /// increment, if geometry is changed
155     static int changeval;
156 
157     /// bounding box of geometry
158     Box<3> boundingbox;
159 
160     /// bounding box, if not set by input file
161     static Box<3> default_boundingbox;
162 
163     /// identic surfaces are stored by pair of indizes, val = inverse
164     INDEX_2_HASHTABLE<int> identicsurfaces;
165     NgArray<int> isidenticto;
166     /// identification of boundaries (periodic, thin domains, ...)
167 
168     double ideps;
169 
170     /// filename of inputfile
171     string filename;
172 
173     /// store splinesurfaces, such that added ones do not get deleted before geometry does
174     NgArray<shared_ptr<SplineSurface>> spline_surfaces;
175 
176     shared_ptr<BlockAllocator> solid_ball = Solid::ball;
177 
178   public:
179     CSGeometry ();
180     CSGeometry (const string & afilename);
181     virtual ~CSGeometry ();
182 
183     void Clean ();
184 
185     virtual void Save (string filename) const override;
186     void Save (ostream & ost) const;
187     void Load (istream & ist);
188 
189     void SaveSurfaces (ostream & out) const;
190     void LoadSurfaces (istream & in);
191 
192     virtual void SaveToMeshFile (ostream & ost) const override;
193 
194     PointGeomInfo ProjectPoint(INDEX surfind, Point<3> & p) const override;
195     bool ProjectPointGI (int surfind, Point<3> & p, PointGeomInfo & gi) const override;
196     void ProjectPointEdge(INDEX surfind, INDEX surfind2, Point<3> & p,
197                           EdgePointGeomInfo* gi = nullptr) const override;
198     Vec<3> GetNormal(int surfind, const Point<3> & p, const PointGeomInfo* gi = nullptr) const override;
199 
200     void PointBetween(const Point<3> & p1, const Point<3> & p2,
201                       double secpoint, int surfi,
202                       const PointGeomInfo & gi1,
203                       const PointGeomInfo & gi2,
204                       Point<3> & newp, PointGeomInfo & newgi) const override;
205 
206     void PointBetweenEdge(const Point<3> & p1, const Point<3> & p2, double secpoint,
207                       int surfi1, int surfi2,
208                       const EdgePointGeomInfo & ap1,
209                       const EdgePointGeomInfo & ap2,
210                       Point<3> & newp, EdgePointGeomInfo & newgi) const override;
211 
212     Vec<3> GetTangent (const Point<3> & p, int surfi1, int surfi2,
213                        const EdgePointGeomInfo & ap1) const override;
214 
GetChangeVal()215     int GetChangeVal() { return changeval; }
Change()216     void Change() { changeval++; }
217 
218     void AddSurface (Surface * surf);
219     void AddSurface (char * name, Surface * surf);
220     void AddSurfaces (Primitive * prim);
221 
GetNSurf() const222     int GetNSurf () const { return surfaces.Size(); }
223     const Surface * GetSurface (const char * name) const;
GetSurface(int i) const224     const Surface * GetSurface (int i) const
225     { return surfaces[i]; }
226 
227     void SetSolid (const char * name, Solid * sol);
228     const Solid * GetSolid (const char * name) const;
229     const Solid * GetSolid (const string & name) const;
GetNSolids() const230     int GetNSolids () const { return solids.Size(); }
GetSolid(int i) const231     const Solid * GetSolid (int i) const { return solids[i]; }
GetSolids() const232     const SymbolTable<Solid*> & GetSolids () const { return solids; }
233 
234 
235     void SetSplineCurve (const char * name, shared_ptr<SplineGeometry<2>> spl);
236     void SetSplineCurve (const char * name, shared_ptr<SplineGeometry<3>> spl);
237     shared_ptr<SplineGeometry<2>> GetSplineCurve2d (const string & name) const;
238     shared_ptr<SplineGeometry<3>> GetSplineCurve3d (const string & name) const;
239 
240     void DoArchive(Archive& archive) override;
241 
242 
243     void SetFlags (const char * solidname, const Flags & flags);
244 
245 
GetNTopLevelObjects() const246     int GetNTopLevelObjects () const
247     { return toplevelobjects.Size(); }
248     int SetTopLevelObject (Solid * sol, Surface * surf = NULL);
GetTopLevelObject(int nr,Solid * & sol,Surface * & surf)249     void GetTopLevelObject (int nr, Solid *& sol, Surface *& surf)
250     {
251       sol = toplevelobjects[nr]->GetSolid();
252       surf = toplevelobjects[nr]->GetSurface();
253     }
GetTopLevelObject(int nr,const Solid * & sol,const Surface * & surf) const254     void GetTopLevelObject (int nr, const Solid *& sol, const Surface *& surf) const
255     {
256       sol = toplevelobjects[nr]->GetSolid();
257       surf = toplevelobjects[nr]->GetSurface();
258     }
259 
260     TopLevelObject * GetTopLevelObject (const Solid * sol, const Surface * surf = NULL);
GetTopLevelObject(int nr) const261     TopLevelObject * GetTopLevelObject (int nr) const
262     { return toplevelobjects[nr]; }
263     // const TopLevelObject * GetTopLevelObject (int nr) const
264     // { return toplevelobjects[nr]; }
265     void RemoveTopLevelObject (Solid * sol, Surface * surf = NULL);
266 
267 
AddUserPoint(const Point<3> & p,double ref_factor=0)268     void AddUserPoint (const Point<3> & p, double ref_factor = 0)
269     { userpoints.Append (UserPoint(p,userpoints.Size()+1)); userpoints_ref_factor.Append (ref_factor); }
AddUserPoint(const UserPoint up,double ref_factor=0)270     void AddUserPoint (const UserPoint up, double ref_factor = 0)
271     { userpoints.Append (up); userpoints_ref_factor.Append (ref_factor); }
GetNUserPoints() const272     int GetNUserPoints () const
273     { return userpoints.Size(); }
GetUserPoint(int nr) const274     const UserPoint & GetUserPoint (int nr) const
275     { return userpoints[nr]; }
GetUserPointRefFactor(int nr) const276     double GetUserPointRefFactor (int nr) const
277     { return userpoints_ref_factor[nr]; }
278 
AddIdentPoint(const Point<3> & p) const279     void AddIdentPoint (const Point<3> & p) const
280     { identpoints.Append(p);}
GetNIdentPoints(void) const281     int GetNIdentPoints (void) const
282     { return identpoints.Size();}
GetIdentPoint(int nr) const283     const Point<3> & GetIdentPoint(int nr) const
284     { return identpoints[nr]; }
DeleteIdentPoints(void) const285     void DeleteIdentPoints(void) const
286     { identpoints.DeleteAll();}
287 
288 
289     // quick implementations:
290     NgArray<SingularFace*> singfaces;
291     NgArray<SingularEdge*> singedges;
292     NgArray<SingularPoint*> singpoints;
293     NgArray<Identification*> identifications;
294 
GetNIdentifications(void) const295     int GetNIdentifications (void) const { return identifications.Size(); }
296     void AddIdentification (Identification * ident);
297 
298 
299     ///
300     void CalcTriangleApproximation(double detail, double facets);
301 
302     ///
303     void FindIdenticSurfaces (double eps);
304     ///
305     void GetSurfaceIndices (const Solid * sol,
306 			    const BoxSphere<3> & box,
307 			    NgArray<int> & locsurf) const;
308     ///
309     void GetIndependentSurfaceIndices (const Solid * sol,
310 				       const BoxSphere<3> & box,
311 				       NgArray<int> & locsurf) const;
312     ///
313     /*
314     void GetIndependentSurfaceIndices (const Solid * sol,
315 				       const Point<3> & p, Vec<3> & v,
316 				       NgArray<int> & locsurf) const;
317     */
318     ///
319     void GetIndependentSurfaceIndices (NgArray<int> & locsurf) const;
320 
321     ///
GetSurfaceClassRepresentant(int si) const322     int GetSurfaceClassRepresentant (int si) const
323     { return isidenticto[si]; }
324 
325     ///
GetTriApprox(int msnr)326     const TriangleApproximation * GetTriApprox (int msnr)
327     {
328       if (msnr < triapprox.Size())
329 	return triapprox[msnr];
330       return 0;
331     }
332 
333 
334     void IterateAllSolids (SolidIterator & it, bool only_once = false) const;
335 
336     void RefineTriangleApprox (Solid * locsol,
337 			       int surfind,
338 			       const BoxSphere<3> & box,
339 			       double detail,
340 			       const TATriangle & tria,
341 			       TriangleApproximation & tams,
342 			       IndexSet & iset,
343 			       int level);
344 
BoundingBox() const345     const Box<3> & BoundingBox () const { return boundingbox; }
346 
SetBoundingBox(const Box<3> & abox)347     void SetBoundingBox (const Box<3> & abox)
348     {
349       boundingbox = abox;
350     }
351 
352 
SetDefaultBoundingBox(const Box<3> & abox)353     static void SetDefaultBoundingBox (const Box<3> & abox)
354     {
355       default_boundingbox = abox;
356     }
357 
358     double MaxSize () const;
359 
SetIdEps(double eps)360     void SetIdEps(double eps){ideps = eps;}
GetIdEps(void) const361     double GetIdEps(void) const {return ideps;}
362 
363     class BCModification {
364     public:
365       int si;
366       int tlonr;
367       int bcnr;
368       string * bcname;
369     };
370 
371     NgArray<BCModification> bcmodifications;
372 
373 
374     map<tuple<Surface*,Surface*>, string> named_edges;
375 
376 
377 
378     virtual int GenerateMesh (shared_ptr<Mesh> & mesh, MeshingParameters & mparam) override;
379 
AddSplineSurface(shared_ptr<SplineSurface> ss)380     void AddSplineSurface (shared_ptr<SplineSurface> ss) { spline_surfaces.Append(ss); }
381   };
382 
383 
384 
385 
386 
387 }
388 
389 #endif
390 
391