Code-Eli  0.3.6
octave_helpers.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2 * Copyright (c) 2014 David D. Marshall <ddmarsha@calpoly.edu>
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * David D. Marshall - initial code and implementation
11 ********************************************************************************/
12 
13 #ifndef eli_geom_test_octave_helpers_hpp
14 #define eli_geom_test_octave_helpers_hpp
15 
16 #include <string>
17 
22 
23 namespace eli
24 {
25  namespace test
26  {
27  inline std::string random_string( size_t length )
28  {
29  auto randchar = []() -> char
30  {
31  const char charset[] =
32  "0123456789"
33  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34  "abcdefghijklmnopqrstuvwxyz";
35  const size_t max_index = (sizeof(charset) - 1);
36  return charset[ rand() % max_index ];
37  };
38  std::string str(length,0);
39  std::generate_n( str.begin(), length, randchar );
40  return str;
41  }
42 
43  inline void octave_start(int figno)
44  {
45  std::cout << "figure(" << figno << ");" << std::endl;
46  std::cout << "clf(" << figno << ", 'reset');" << std::endl;
47  std::cout << "hold on;" << std::endl;
48  std::cout << "grid on;" << std::endl;
49  }
50 
51  inline void octave_finish(int figno)
52  {
53  std::cout << "figure(" << figno << ");" << std::endl;
54  std::cout << "hold off;" << std::endl;
55  std::cout << "rotate3d on;" << std::endl;
56  std::cout << "xlabel('x');" << std::endl;
57  std::cout << "ylabel('y');" << std::endl;
58  std::cout << "zlabel('z');" << std::endl;
59  }
60 
61  template<typename data__>
63  const std::string &name="", bool show_control_points=true)
64  {
66  typedef typename piecewise_curve_type::curve_type curve_type;
67  typedef typename piecewise_curve_type::data_type data_type;
68  typedef typename piecewise_curve_type::index_type index_type;
69 
70  std::string nm, cpxbuf, cpybuf, cpzbuf, cxbuf, cybuf, czbuf;
71 
72  index_type i, pp, ns;
73  data_type tmin(pc.get_parameter_min()), tmax(pc.get_parameter_max());
74 
75  ns=pc.number_segments();
76 
77  // build name
78  if (name=="")
79  {
80  nm=random_string(5);
81  }
82  else
83  {
84  nm=name;
85  }
86 
87  // set control points
88  if (show_control_points)
89  {
90  cpxbuf=nm+"_curv_cp_x=[";
91  cpybuf=nm+"_curv_cp_y=[";
92  cpzbuf=nm+"_curv_cp_z=[";
93  for (pp=0; pp<ns; ++pp)
94  {
95  index_type bez_deg;
96  curve_type bez;
97  pc.get(bez, pp);
98  bez_deg=bez.degree();
99  for (i=0; i<=bez_deg; ++i)
100  {
101  cpxbuf+=std::to_string(bez.get_control_point(i).x());
102  cpybuf+=std::to_string(bez.get_control_point(i).y());
103  cpzbuf+=std::to_string(bez.get_control_point(i).z());
104  if ((pp<(ns-1)) || ((pp==(ns-1)) && (i<bez_deg)))
105  {
106  cpxbuf+=", ";
107  cpybuf+=", ";
108  cpzbuf+=", ";
109  }
110  }
111  }
112  cpxbuf+="];";
113  cpybuf+="];";
114  cpzbuf+="];";
115  }
116 
117  // initialize the t parameters
118  index_type nt(129);
119  std::vector<data__> t(nt);
120  for (i=0; i<nt; ++i)
121  {
122  t[i]=tmin+(tmax-tmin)*static_cast<data__>(i)/(nt-1);
123  }
124 
125  // set the curve points
126  cxbuf=nm+"_curv_x=[";
127  cybuf=nm+"_curv_y=[";
128  czbuf=nm+"_curv_z=[";
129  for (i=0; i<nt; ++i)
130  {
131  cxbuf+=std::to_string(pc.f(t[i]).x());
132  cybuf+=std::to_string(pc.f(t[i]).y());
133  czbuf+=std::to_string(pc.f(t[i]).z());
134  if (i<nt)
135  {
136  cxbuf+=", ";
137  cybuf+=", ";
138  czbuf+=", ";
139  }
140  }
141  cxbuf+="];";
142  cybuf+="];";
143  czbuf+="];";
144 
145  std::cout << "% curve: " << nm << std::endl;
146  std::cout << "figure(" << figno << ");" << std::endl;
147  std::cout << cxbuf << std::endl;
148  std::cout << cybuf << std::endl;
149  std::cout << czbuf << std::endl;
150  if (show_control_points)
151  {
152  std::cout << cpxbuf << std::endl;
153  std::cout << cpybuf << std::endl;
154  std::cout << cpzbuf << std::endl;
155  }
156  std::cout << "setenv('GNUTERM', 'x11');" << std::endl;
157  std::cout << "plot3(" << nm << "_curv_x, "
158  << nm << "_curv_y, "
159  << nm << "_curv_z, '-g');" << std::endl;
160  if (show_control_points)
161  {
162  std::cout << "plot3(" << nm << "_curv_cp_x', "
163  << nm << "_curv_cp_y', "
164  << nm << "_curv_cp_z', '-o', 'Color', [0 0.5 0], 'MarkerFaceColor', [0 0.5 0]);" << std::endl;
165  }
166  }
167 
168  template<typename data__>
171  const std::string &name="")
172  {
174  typedef typename piecewise_curve_type::data_type data_type;
175  typedef typename piecewise_curve_type::index_type index_type;
176  typedef typename piecewise_curve_type::tolerance_type tolerance_type;
177 
178  std::string nm(random_string(5)), vecxbuf, vecybuf, veczbuf, cxbuf, cybuf, czbuf;
179 
180  index_type i, pp, ns;
181  data_type tmin, tmax;
182 
183  tolerance_type tol;
184 
185  ns=pc.number_segments();
186  pc.get_parameter_min(tmin);
187  pc.get_parameter_max(tmax);
188 
189  // check parameterization of vec curve
190  if (!tol.approximately_equal(vec.get_t0(), tmin))
191  {
192  return;
193  }
194  if (!tol.approximately_equal(vec.get_tmax(), tmax))
195  {
196  return;
197  }
198 
199  // build name
200  if (name=="")
201  {
202  nm=random_string(5);
203  }
204  else
205  {
206  nm=name;
207  }
208 
209  // initialize the t parameters
210  index_type nt(11);
211  std::vector<data__> t(nt);
212  for (i=0; i<nt; ++i)
213  {
214  t[i]=tmin+(tmax-tmin)*static_cast<data__>(i)/(nt-1);
215  }
216 
217  // set the surface points
218  cxbuf=nm+"_loc_x=[";
219  cybuf=nm+"_loc_y=[";
220  czbuf=nm+"_loc_z=[";
221  vecxbuf=nm+"_vec_x=[";
222  vecybuf=nm+"_vec_y=[";
223  veczbuf=nm+"_vec_z=[";
224  for (i=0; i<nt; ++i)
225  {
226  cxbuf+=std::to_string(pc.f(t[i]).x());
227  cybuf+=std::to_string(pc.f(t[i]).y());
228  czbuf+=std::to_string(pc.f(t[i]).z());
229  vecxbuf+=std::to_string(vec.f(t[i]).x());
230  vecybuf+=std::to_string(vec.f(t[i]).y());
231  veczbuf+=std::to_string(vec.f(t[i]).z());
232  if (i<nt)
233  {
234  cxbuf+=", ";
235  cybuf+=", ";
236  czbuf+=", ";
237  vecxbuf+=", ";
238  vecybuf+=", ";
239  veczbuf+=", ";
240  }
241  }
242  cxbuf+="];";
243  cybuf+="];";
244  czbuf+="];";
245  vecxbuf+="];";
246  vecybuf+="];";
247  veczbuf+="];";
248 
249  std::cout << "% curve: " << nm << std::endl;
250  std::cout << "figure(" << figno << ");" << std::endl;
251  std::cout << cxbuf << std::endl;
252  std::cout << cybuf << std::endl;
253  std::cout << czbuf << std::endl;
254  std::cout << vecxbuf << std::endl;
255  std::cout << vecybuf << std::endl;
256  std::cout << veczbuf << std::endl;
257  std::cout << "setenv('GNUTERM', 'x11');" << std::endl;
258  std::cout << "quiver3(" << nm << "_loc_x, "
259  << nm << "_loc_y, "
260  << nm << "_loc_z, "
261  << nm << "_vec_x, "
262  << nm << "_vec_y, "
263  << nm << "_vec_z, "
264  << "'r');" << std::endl;
265  }
266 
267  template<typename data__>
269  const std::string &name="", bool show_control_points=true)
270  {
272  typedef typename piecewise_surface_type::surface_type surface_type;
273  typedef typename piecewise_surface_type::data_type data_type;
274  typedef typename piecewise_surface_type::index_type index_type;
275 
276  std::string nm, sxbuf, sybuf, szbuf;
277  std::vector<std::string> cpxbuf, cpybuf, cpzbuf;
278 
279  index_type i, j, pp, qq, nup, nvp;
280  data_type umin, vmin, umax, vmax;
281 
282  nup=ps.number_u_patches();
283  nvp=ps.number_v_patches();
284  ps.get_parameter_min(umin, vmin);
285  ps.get_parameter_max(umax, vmax);
286 
287 
288  // build name
289  if (name=="")
290  {
291  nm=random_string(5);
292  }
293  else
294  {
295  nm=name;
296  }
297 
298  // set control points
299  if (show_control_points)
300  {
301  cpxbuf.resize(nup*nvp);
302  cpybuf.resize(nup*nvp);
303  cpzbuf.resize(nup*nvp);
304 
305  for (pp=0; pp<nup; ++pp)
306  {
307  for (qq=0; qq<nvp; ++qq)
308  {
309  surface_type bez;
310  ps.get(bez, pp, qq);
311  index_type ppqq;
312 
313  ppqq=qq*nup+pp;
314  cpxbuf[ppqq]=nm+"_surf_cp"+std::to_string(pp)+std::to_string(qq)+"_x=[";
315  cpybuf[ppqq]=nm+"_surf_cp"+std::to_string(pp)+std::to_string(qq)+"_y=[";
316  cpzbuf[ppqq]=nm+"_surf_cp"+std::to_string(pp)+std::to_string(qq)+"_z=[";
317 
318  for (i=0; i<=bez.degree_u(); ++i)
319  {
320  for (j=0; j<=bez.degree_v(); ++j)
321  {
322  cpxbuf[ppqq]+=std::to_string(bez.get_control_point(i, j).x());
323  cpybuf[ppqq]+=std::to_string(bez.get_control_point(i, j).y());
324  cpzbuf[ppqq]+=std::to_string(bez.get_control_point(i, j).z());
325 
326  if ((i==bez.degree_u()) && (j==bez.degree_v()))
327  {
328  }
329  else if ((j==bez.degree_v()) && (i<bez.degree_u()))
330  {
331  cpxbuf[ppqq]+="; ";
332  cpybuf[ppqq]+="; ";
333  cpzbuf[ppqq]+="; ";
334  }
335  else
336  {
337  cpxbuf[ppqq]+=", ";
338  cpybuf[ppqq]+=", ";
339  cpzbuf[ppqq]+=", ";
340  }
341  }
342  }
343  cpxbuf[ppqq]+="];";
344  cpybuf[ppqq]+="];";
345  cpzbuf[ppqq]+="];";
346  }
347  }
348  }
349 
350  // initialize the u & v parameters
351  index_type nu(10*nup+1), nv(10*nvp+1);
352  std::vector<data__> u(nu), v(nv);
353  for (i=0; i<static_cast<index_type>(u.size()); ++i)
354  {
355  u[i]=umin+(umax-umin)*static_cast<data__>(i)/(u.size()-1);
356  }
357  for (j=0; j<static_cast<index_type>(v.size()); ++j)
358  {
359  v[j]=vmin+(vmax-vmin)*static_cast<data__>(j)/(v.size()-1);
360  }
361 
362  // set the surface points
363  sxbuf=nm+"_surf_x=[";
364  sybuf=nm+"_surf_y=[";
365  szbuf=nm+"_surf_z=[";
366  for (i=0; i<nu; ++i)
367  {
368  for (j=0; j<nv; ++j)
369  {
370  sxbuf+=std::to_string(ps.f(u[i], v[j]).x());
371  sybuf+=std::to_string(ps.f(u[i], v[j]).y());
372  szbuf+=std::to_string(ps.f(u[i], v[j]).z());
373  if (j==(nv-1))
374  {
375  if (i<(nu-1))
376  {
377  sxbuf+=";\n";
378  sybuf+=";\n";
379  szbuf+=";\n";
380  }
381  }
382  else
383  {
384  sxbuf+=", ";
385  sybuf+=", ";
386  szbuf+=", ";
387  }
388  }
389  }
390  sxbuf+="];";
391  sybuf+="];";
392  szbuf+="];";
393 
394  std::cout << "% surface: " << nm << std::endl;
395  std::cout << "figure(" << figno << ");" << std::endl;
396  std::cout << sxbuf << std::endl;
397  std::cout << sybuf << std::endl;
398  std::cout << szbuf << std::endl;
399  std::cout << "setenv('GNUTERM', 'x11');" << std::endl;
400  std::cout << "mesh(" << nm << "_surf_x, "
401  << nm << "_surf_y, "
402  << nm << "_surf_z, "
403  << "'EdgeColor', [0 0 0]);" << std::endl;
404  if (show_control_points)
405  {
406  for (pp=0; pp<nup; ++pp)
407  {
408  for (qq=0; qq<nvp; ++qq)
409  {
410  index_type ppqq;
411 
412  ppqq=qq*nup+pp;
413  std::cout << cpxbuf[ppqq] << std::endl;
414  std::cout << cpybuf[ppqq] << std::endl;
415  std::cout << cpzbuf[ppqq] << std::endl;
416 
417  std::cout << "mesh(" << nm << "_surf_cp" << std::to_string(pp) << std::to_string(qq) << "_x', "
418  << nm << "_surf_cp" << std::to_string(pp) << std::to_string(qq) << "_y', "
419  << nm << "_surf_cp" << std::to_string(pp) << std::to_string(qq) << "_z', "
420  << "'EdgeColor', [0.5 0.5 0.5], 'FaceColor', 'none');" << std::endl;
421  std::cout << "plot3(" << nm << "_surf_cp" << std::to_string(pp) << std::to_string(qq) << "_x', "
422  << nm << "_surf_cp" << std::to_string(pp) << std::to_string(qq) << "_y', "
423  << nm << "_surf_cp" << std::to_string(pp) << std::to_string(qq) << "_z', "
424  << "'o', 'MarkerEdgeColor', [0.5 0.5 0.5],'MarkerFaceColor', [0.5 0.5 0.5]);" << std::endl;
425  }
426  }
427  }
428  }
429  }
430 }
431 
432 #endif
433 
void octave_print(int figno, const eli::geom::curve::piecewise< eli::geom::curve::bezier, data__, 3 > &pc, const std::string &name="", bool show_control_points=true)
Definition: octave_helpers.hpp:62
std::string random_string(size_t length)
Definition: octave_helpers.hpp:27
void octave_start(int figno)
Definition: octave_helpers.hpp:43
Definition: math.hpp:20
data_type get_tmax() const
Definition: piecewise.hpp:333
data_type get_parameter_min() const
Definition: piecewise.hpp:366
void get_parameter_max(data_type &umax, data_type &vmax) const
Definition: piecewise.hpp:175
index_type number_segments() const
Definition: piecewise.hpp:419
error_code get(surface_type &surf, const index_type &ui, const index_type &vi) const
Definition: piecewise.hpp:487
point_type f(const data_type &u, const data_type &v) const
Definition: piecewise.hpp:897
index_type number_v_patches() const
Definition: piecewise.hpp:143
Definition: piecewise.hpp:37
Definition: piecewise.hpp:244
void octave_finish(int figno)
Definition: octave_helpers.hpp:51
void length(typename piecewise< curve__, data__, dim__, tol__ >::data_type &len, const piecewise< curve__, data__, dim__, tol__ > &pc, const typename piecewise< curve__, data__, dim__, tol__ >::data_type &tol)
Definition: length.hpp:43
void get_parameter_min(data_type &umin, data_type &vmin) const
Definition: piecewise.hpp:169
data_type get_parameter_max() const
Definition: piecewise.hpp:374
index_type number_u_patches() const
Definition: piecewise.hpp:142
data_type get_t0() const
Definition: piecewise.hpp:335
point_type f(const data_type &t) const
Definition: piecewise.hpp:1732
error_code get(curve_type &curve, const index_type &index) const
Definition: piecewise.hpp:729