File: gnuplot-function.hh

package info (click to toggle)
roboptim-core 2.0-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,488 kB
  • ctags: 1,160
  • sloc: cpp: 5,388; sh: 395; ansic: 387; makefile: 25; python: 19
file content (128 lines) | stat: -rw-r--r-- 3,984 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2009 by Thomas Moulard, AIST, CNRS, INRIA.
//
// This file is part of the roboptim.
//
// roboptim is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// roboptim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with roboptim.  If not, see <http://www.gnu.org/licenses/>.

#ifndef ROBOPTIM_CORE_VISUALIZATION_GNUPLOT_FUNCTION_HH
# define ROBOPTIM_CORE_VISUALIZATION_GNUPLOT_FUNCTION_HH
# include <roboptim/core/sys.hh>
# include <roboptim/core/debug.hh>

# include <boost/format.hpp>

# include <roboptim/core/function.hh>

namespace roboptim
{
  namespace visualization
  {
    namespace gnuplot
    {
      /// \addtogroup roboptim_visualization
      /// @{

      /// \brief Import discrete interval type from function.
      typedef Function::discreteInterval_t discreteInterval_t;

      /// \brief Plot a 1D function with Gnuplot.
      ///
      /// Plot a 1D function in Gnuplot on a specific interval.
      /// X axis is the function's argument, Y axis is result.
      /// \param f function to be plotted
      /// \param interval plot interval
      /// \return Gnuplot command
      template <typename T>
      Command plot (const GenericFunction<T>& f, discreteInterval_t interval);

      /// \brief Plot a 2D function with Gnuplot.
      ///
      /// Plot a 2D function in Gnuplot on a specific interval.
      /// X and Y axises are the function result, the original
      /// arguments are not displayed.
      /// \param f function to be plotted
      /// \param interval plot interval
      /// \return Gnuplot command
      template <typename T>
      Command plot_xy (const GenericFunction<T>& f, discreteInterval_t interval);


      template <typename T>
      Command plot (const GenericFunction<T>& f, discreteInterval_t window)
      {
	assert (f.inputSize () == 1);

	assert (boost::get<0> (window) < boost::get<1> (window)
		&& boost::get<2> (window) > 0.);
	//FIXME: compare with arg bounds?

	std::string str = (boost::format ("plot '-' title '%1%' with line")
			   % f.getName ()).str ();

	for (Function::size_type i = 1; i < f.outputSize (); ++i)
	  str += ", '-' with line";
	str += "\n";

	Function::vector_t x (f.inputSize ());
	for (Function::size_type i = 0; i < f.outputSize (); ++i)
	  {
	    for (double t = boost::get<0> (window); t < boost::get<1> (window);
		 t += boost::get<2> (window))
	      {
		x[0] = t;
		Function::vector_t res = f (x);
		str += (boost::format ("%1.2f %2.2f\n")
			% normalize (t)
			% normalize (res [0])).str ();
	      }
	    str += "e\n";
	  }

	return Command (str);
      }

      template <typename T>
      Command plot_xy (const GenericFunction<T>& f, discreteInterval_t window)
      {
	assert (f.inputSize () == 1 && f.outputSize () == 2);

	assert (boost::get<0> (window) < boost::get<1> (window)
		&& boost::get<2> (window) > 0.);
	//FIXME: compare with arg bounds?

	std::string str = (boost::format ("plot '-' title '%1%' with line\n")
			   % f.getName ()).str ();

	Function::vector_t x (f.inputSize ());

	for (double t = boost::get<0> (window); t < boost::get<1> (window);
	     t += boost::get<2> (window))
	  {
	    x[0] = t;
	    Function::vector_t res = f (x);
	    str += (boost::format ("%1.2f %2.2f\n")
		    % normalize (res[0])
		    % normalize (res [1])).str ();
	  }
	str += "e\n";

	return Command (str);
      }

      /// @}
    } // end of namespace gnuplot.
  } // end of namespace visualization.
} // end of namespace roboptim.

#endif //! ROBOPTIM_CORE_VISUALIZATION_GNUPLOT_FUNCTION_HH