trase
Axis.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2018, University of Oxford.
3 All rights reserved.
4 
5 University of Oxford means the Chancellor, Masters and Scholars of the
6 University of Oxford, having an administrative office at Wellington
7 Square, Oxford OX1 2JD, UK.
8 
9 This file is part of trase.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice, this
14  list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16  this list of conditions and the following disclaimer in the documentation
17  and/or other materials provided with the distribution.
18 * Neither the name of the copyright holder nor the names of its
19  contributors may be used to endorse or promote products derived from
20  this software without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
35 
36 #ifndef AXIS_H_
37 #define AXIS_H_
38 
39 #include <array>
40 #include <memory>
41 
42 #include "frontend/Data.hpp"
43 #include "frontend/Drawable.hpp"
44 #include "frontend/Geometry.hpp"
45 
46 #include "util/Colors.hpp"
47 #include "util/Exception.hpp"
48 
49 namespace trase {
50 
51 // forward declare since Axis has some functions returning legends
52 class Legend;
53 
55 struct TickInfo {
56  std::vector<float> x_val;
57  std::vector<float> y_val;
58  std::vector<float> x_pos;
59  std::vector<float> y_pos;
60 
61  void clear() {
62  x_val.clear();
63  y_val.clear();
64  x_pos.clear();
65  y_pos.clear();
66  }
67 };
68 
85 class Axis : public Drawable {
87  Limits m_limits;
88 
90  int m_sig_digits;
91 
93  int m_nx_ticks;
94 
96  int m_ny_ticks;
97 
99  float m_tick_len;
100 
102  std::string m_xlabel;
103 
105  std::string m_ylabel;
106 
108  std::string m_title;
109 
111  TickInfo m_tick_info;
112 
114  bool m_has_legend;
115 
116 public:
121  Axis(Drawable *parent, const bfloat2_t &area);
122 
123  virtual ~Axis() = default;
124 
125  TRASE_DISPATCH_BACKENDS
126 
128  const Limits &limits() const { return m_limits; }
129 
131  Limits &limits() { return m_limits; }
132 
134  void xlim(std::array<float, 2> xlimits) {
135  m_limits.bmin[Aesthetic::x::index] = xlimits[0];
136  m_limits.bmax[Aesthetic::x::index] = xlimits[1];
137  }
138 
140  void ylim(std::array<float, 2> ylimits) {
141  m_limits.bmin[Aesthetic::y::index] = ylimits[0];
142  m_limits.bmax[Aesthetic::y::index] = ylimits[1];
143  }
144 
146  void xlabel(const char *string) { m_xlabel.assign(string); }
147 
149  void ylabel(const char *string) { m_ylabel.assign(string); }
150 
152  void title(const char *string) { m_title.assign(string); }
153 
155  std::shared_ptr<Legend> legend();
156 
161  std::shared_ptr<Geometry>
162  points(const DataWithAesthetic &data,
163  const Transform &transform = Transform(Identity()));
164 
169  std::shared_ptr<Geometry>
170  rectangle(const DataWithAesthetic &data,
171  const Transform &transform = Transform(Identity()));
172 
177  std::shared_ptr<Geometry>
178  line(const DataWithAesthetic &data,
179  const Transform &transform = Transform(Identity()));
180 
185  std::shared_ptr<Geometry>
186  histogram(const DataWithAesthetic &data,
187  const Transform &transform = Transform(BinX()));
188 
193  std::shared_ptr<Geometry> plot(int n);
194 
195  template <typename AnimatedBackend> void draw(AnimatedBackend &backend);
196  template <typename Backend> void draw(Backend &backend, float time);
197 
200  template <typename Aesthetic> float from_display(float i) const {
201  return Aesthetic::from_display(i, m_limits, m_pixels);
202  }
203 
206  template <typename Aesthetic> float to_display(float i) const {
207  return Aesthetic::to_display(i, m_limits, m_pixels);
208  }
209 
215  m_nx_ticks = arg[0];
216  m_ny_ticks = arg[1];
217  }
218 
220  Vector<int, 2> get_ticks() const { return {m_nx_ticks, m_ny_ticks}; }
221 
222 private:
233  std::shared_ptr<Geometry> plot_impl(const std::shared_ptr<Geometry> &plot,
234  const Transform &transform,
235  const DataWithAesthetic &values);
236 
237  void update_tick_information();
238  vint2_t calculate_num_ticks();
239  void add_geometry_to_legend(const std::shared_ptr<Geometry> &geometry);
240  std::shared_ptr<Legend> add_legend();
241 
242  template <typename Backend> void draw_common(Backend &backend);
243  template <typename Backend> void draw_common_axis_box(Backend &backend);
244  template <typename Backend> void draw_common_ticks(Backend &backend);
245  template <typename Backend> void draw_common_gridlines(Backend &backend);
246  template <typename Backend> void draw_common_title(Backend &backend);
247  template <typename Backend> void draw_common_xlabel(Backend &backend);
248  template <typename Backend> void draw_common_ylabel(Backend &backend);
249 };
250 
251 } // namespace trase
252 
253 #include "frontend/Axis.tcc"
254 
255 #endif // AXIS_H_
a base class for all the backends that support drawing a single frame
Definition: Backend.hpp:50
Contains the minimum and maximum extents of a hypercube in D dimensional space.
Definition: BBox.hpp:54
vector_t bmin
minimum point in the box (i.e.
Definition: BBox.hpp:60
An 2D axis that can contain zero or more Geometry objects.
Definition: Axis.hpp:85
float from_display(float i) const
convert from display coordinates to data coordinates, using the given Aesthetic
Definition: Axis.hpp:200
Vector< int, 2 > get_ticks() const
gets the number of ticks on this axis
Definition: Axis.hpp:220
void ylim(std::array< float, 2 > ylimits)
a helper function to set the y Aesthetic limits manually
Definition: Axis.hpp:140
Combination of the RawData class and Aesthetics, this class points to a RawData object, and contains a mapping from aesthetics to RawData column numbers.
Definition: Data.hpp:243
holds a std::function that maps between two DataWithAesthetic classes
Definition: Transform.hpp:68
Base class for drawable objects in a figure.
Definition: Drawable.hpp:99
void xlim(std::array< float, 2 > xlimits)
a helper function to set the x Aesthetic limits manually
Definition: Axis.hpp:134
Contains classes and functions for handling data and aesthetics.
Limits & limits()
returns the current Aesthetic limits, allowing them to be set manually
Definition: Axis.hpp:131
bin x coordinates
Definition: Transform.hpp:56
float to_display(float i) const
convert from data coordinates to display coordinates, using the given Aesthetic
Definition: Axis.hpp:206
void xlabel(const char *string)
set the label on the x axis
Definition: Axis.hpp:146
a base class for all the backends that support animation over time
Definition: Backend.hpp:62
Identity transform, just pass through...
Definition: Transform.hpp:47
vector_t bmax
maximum point in the box (i.e.
Definition: BBox.hpp:65
void set_ticks(Vector< int, 2 > arg)
set the number of ticks on this axis
Definition: Axis.hpp:214
void ylabel(const char *string)
set the label on the y axis
Definition: Axis.hpp:149
TRASE_DISPATCH_BACKENDS const Limits & limits() const
returns the current Aesthetic limits
Definition: Axis.hpp:128
A helper struct for Axis that holds tick-related information.
Definition: Axis.hpp:55
Definition: Backend.cpp:39
available geometry types are Points, Line.
void title(const char *string)
set the title of the Axis
Definition: Axis.hpp:152