G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsGoTools.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include <fstream>
17 
18 #include <gsUtils/gsMesh/gsMesh.h>
20 
23 
24 #include <gsIO/gsFileData.h>
25 #include <gsIO/gsFileManager.h>
26 
27 namespace gismo {
28 
29 
37 template <short_t d, typename T>
39  std::ofstream& out)
40 {
41  // The format is (similar for d < 3)
42  //
43  // - dimension of geometry space, whether or not the volume is
44  // rational: 1=rational, 0=non-rational
45  // - the number of coefficients in the first parameter direction,
46  // the polynomial order in this direction (i.e. degree+1)
47  // - the knot vector in the first parameter direction, multiple
48  // knots are represented by giving the knot value several times
49  // - the number of coefficients in the second parameter direction,
50  // the polynomial order in this direction (i.e. degree+1)
51  // - the knot vector in the second parameter direction
52  // - the number of coefficients in the third parameter direction,
53  // the polynomial order in this direction (i.e. degree+1)
54  // - the knot vector in the third parameter direction
55  // - the volume coefficients
56 
57 
58 
59  out << std::setprecision(15);
60 
61  out << bspl.geoDim() << " " << 0 << "\n";
62 
63  for (short_t dim = 0; dim < bspl.parDim(); dim++)
64  {
65  out << bspl.basis().size(dim) << " " <<
66  bspl.basis().degree(dim) + 1 << "\n";
67 
68  const gsKnotVector<T> kv = bspl.basis().knots(dim);
69  typename gsKnotVector<T>::const_iterator iter;
70  for (iter = kv.begin(); iter != kv.end(); iter++)
71  {
72  out << *iter << " ";
73  }
74  out << "\n";
75  }
76 
77  const gsMatrix<T>& coefs = bspl.coefs();
78 
79  for (int row = 0; row < coefs.rows(); row++)
80  {
81  out << coefs.row(row) << "\n";
82 
83  }
84 
85  out << std::endl;
86 }
87 
88 
93 template <short_t d, typename T>
95  std::ofstream& out)
96 {
97 
98  // first we write header
99 
100  // format ID VERSION, where
101  // ID : Class_SplineCurve = 100,
102  // Class_SplineSurface = 200,
103  // Class_SplineVolume = 700
104  // VERSION 1 0 0
105 
106  // about the headers
107  // https://github.com/SINTEF-Geometry/GoTools/blob/master/gotools-core/include/GoTools/geometry/ClassType.h
108  // each element in the patch must have its own header
109 
110  if (d == 1)
111  {
112  out << 100;
113  }
114  else if (d == 2)
115  {
116  out << 200;
117  }
118  else if (d == 3)
119  {
120  out << 700;
121  }
122  else
123  {
124  gsWarn << "Dimension is too high: GoTools does not support dimension "
125  "higher than two."
126  "Aborting ...";
127  return;
128  }
129 
130  out << " 1 0 0\n";
131 
132  gsWriteGoToolsBodySpline<d, T>(bspl, out);
133 
134 }
135 
136 
141 template <typename T>
142 void gsWriteGoTools(const gsGeometry<T>& geom,
143  const std::string& fileName)
144 {
145  std::string fn(fileName);
146 
147  // check the extension
148  std::string ext = gsFileManager::getExtension(fileName);
149  if (ext != "g2")
150  {
151  fn += ".g2";
152  }
153 
154  // opening file
155  std::ofstream file;
156  file.open(fn.c_str());
157  if (!file.is_open())
158  {
159  gsWarn << "Can not open file: " << fileName << "\n"
160  "Aborting ...";
161  return;
162  }
163 
164  gsWriteGoTools(geom, file);
165 
166  file.close();
167 
168 }
169 
170 
175 template <typename T>
176 void gsWriteGoTools(const gsGeometry<T>& geom,
177  std::ofstream& out)
178 {
179 
180  // check which object we have and call appropriate function
181  const gsTensorBSpline<1, T>* bspline1 =
182  dynamic_cast<const gsTensorBSpline<1, T>* >(&geom);
183 
184  if (bspline1 != NULL)
185  {
186  gsWriteGoToolsSpline<1, T>(*bspline1, out);
187  return;
188  }
189 
190  const gsTensorBSpline<2, T>* bspline2 =
191  dynamic_cast<const gsTensorBSpline<2, T>* >(&geom);
192 
193  if (bspline2 != NULL)
194  {
195  gsWriteGoToolsSpline<2, T>(*bspline2, out);
196  return;
197  }
198 
199  const gsTensorBSpline<3, T>* bspline3 =
200  dynamic_cast<const gsTensorBSpline<3, T>* >(&geom);
201 
202  if (bspline3 != NULL)
203  {
204  gsWriteGoToolsSpline<3, T>(*bspline3, out);
205  return;
206  }
207 
208  gsWarn << "Can not write your geometry, unsupported format.\n"
209  "Currently supported formats: BSplines.\n"
210  "Aborting ...\n";
211 
212  return;
213 }
214 
215 
220 template <typename T>
221 void gsWriteGoTools(const gsMultiPatch<T>& multiPatch,
222  const std::string& fileName)
223 {
224  std::string fn(fileName);
225 
226  // check the extension
227  std::string ext = gsFileManager::getExtension(fileName);
228  if (ext != "g2")
229  {
230  fn += ".g2";
231  }
232 
233  // opening file
234  std::ofstream file;
235  file.open(fn.c_str());
236  if (!file.is_open())
237  {
238  gsWarn << "Can not open file: " << fileName << "\n"
239  "Aborting ...";
240  return;
241  }
242 
243  typename gsMultiPatch<T>::const_iterator iter;
244  for (iter = multiPatch.begin(); iter != multiPatch.end(); ++iter)
245  {
246  gsWriteGoTools<T>(**iter, file);
247  }
248 
249  file.close();
250 
251 }
252 
253 
254 } // end namespace gismo
Provides definition of HTensorBasis abstract interface.
Abstract base class representing a geometry map.
Definition: gsGeometry.h:92
Provides declaration of iterator of hierarchical domain.
void gsWriteGoToolsSpline(const gsTensorBSpline< d, T > &bspl, std::ofstream &out)
Definition: gsGoTools.h:94
#define short_t
Definition: gsConfig.h:35
A tensor product of d B-spline functions, with arbitrary target dimension.
Definition: gsTensorBSpline.h:44
static std::string getExtension(std::string const &fn)
Returns the extension of the filename fn.
Definition: gsFileManager.cpp:568
Provides combinatorial unitilies.
#define gsWarn
Definition: gsDebug.h:50
Utility class which holds I/O XML data to read/write to/from files.
const_iterator end() const
Definition: gsMultiPatch.h:108
Container class for a set of geometry patches and their topology, that is, the interface connections ...
Definition: gsMultiPatch.h:33
short_t parDim() const
Dimension d of the parameter domain (same as domainDim()).
Definition: gsGeometry.hpp:190
Provides declaration of the Mesh class.
iterator end() const
Returns iterator pointing past the end of the repeated knots.
Definition: gsKnotVector.hpp:124
void gsWriteGoToolsBodySpline(const gsTensorBSpline< d, T > &bspl, std::ofstream &out)
Writes body part of GoTools (.g2) format to a file.
Definition: gsGoTools.h:38
Utility class for finding files and handling paths.
Class for representing a knot vector.
Definition: gsKnotVector.h:79
void gsWriteGoTools(const gsGeometry< T > &geom, const std::string &fileName)
Definition: gsGoTools.h:142
const_iterator begin() const
Definition: gsMultiPatch.h:103
iterator begin() const
Returns iterator pointing to the beginning of the repeated knots.
Definition: gsKnotVector.hpp:117
virtual const gsBasis< T > & basis() const =0
Returns a const reference to the basis of the geometry.
short_t geoDim() const
Dimension n of the absent physical space.
Definition: gsGeometry.h:292
gsMatrix< T > & coefs()
Definition: gsGeometry.h:340