trase
Drawable.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 DRAWABLE_H_
37 #define DRAWABLE_H_
38 
39 #include <array>
40 #include <memory>
41 #include <ostream>
42 #include <vector>
43 
44 #include "util/BBox.hpp"
45 #include "util/Style.hpp"
46 
47 namespace trase {
48 
49 class Backend;
50 class AnimatedBackend;
51 
53 struct FrameInfo {
54 
55  int frame_above;
56  float frame;
57  float w1;
58  float w2;
59 
60  void update(std::vector<float> &times, const float time_now) {
61  frame_above = static_cast<int>(std::distance(
62  times.begin(), std::lower_bound(times.begin(), times.end(), time_now)));
63 
64  if (frame_above == 0) {
65  w1 = 1.f;
66  w2 = 0.f;
67  frame = 0.f;
68  } else {
69  const float delta_t = times[frame_above] - times[frame_above - 1];
70  w1 = (time_now - times[frame_above - 1]) / delta_t;
71  w2 = 1.f - w1;
72  frame = static_cast<float>(frame_above - 1) + w1;
73  }
74  }
75 };
76 
77 // forward declare all backends here
78 class BackendGL;
79 class BackendSVG;
80 
99 class Drawable {
100 protected:
102  std::vector<std::shared_ptr<Drawable>> m_children;
103 
106 
109 
112 
114  float m_time_span;
115 
117  std::vector<float> m_times;
118 
121 
124 
125 public:
132  Drawable(Drawable *parent, const bfloat2_t &area_of_parent);
133 
136  void resize(const bfloat2_t &parent_pixels);
137 
140  void update_time_span(float time);
141 
143  void add_frame_time(float time);
144 
148  void update_frame_info(float time);
149 
153  const FrameInfo &get_frame_info() const;
154 
156  const bfloat2_t &pixels() const { return m_pixels; }
157 
159  const bfloat2_t &area() const { return m_area; }
160 
162  bfloat2_t &area() { return m_area; }
163 
165  Style &style() noexcept;
166 
168  const float &time_span() const { return m_time_span; }
169 
170 #ifdef TRASE_BACKEND_GL
171  virtual void dispatch(BackendGL &figure, float time) = 0;
172 #endif
173  virtual void dispatch(BackendSVG &file, float time) = 0;
174  virtual void dispatch(BackendSVG &file) = 0;
175 
177  template <typename AnimatedBackend> void draw(AnimatedBackend &backend);
178 
180  template <typename Backend> void draw(Backend &backend, float time);
181 };
182 
183 } // namespace trase
184 
185 #define TRASE_DISPATCH(backend_type) \
186  void dispatch(backend_type &backend, float time) override { \
187  draw(backend, time); \
188  for (auto &i : m_children) { \
189  i->dispatch(backend, time); \
190  } \
191  }
192 
193 #define TRASE_ANIMATED_DISPATCH(backend_type) \
194  void dispatch(backend_type &backend) override { \
195  if (m_times.size() == 1) { \
196  draw(backend, 0); \
197  } else { \
198  draw(backend); \
199  } \
200  for (auto &i : m_children) { \
201  i->dispatch(backend); \
202  } \
203  }
204 
205 #define TRASE_DISPATCH_SVG \
206  TRASE_DISPATCH(BackendSVG) \
207  TRASE_ANIMATED_DISPATCH(BackendSVG)
208 
209 #ifdef TRASE_BACKEND_GL
210 #define TRASE_DISPATCH_GL TRASE_DISPATCH(BackendGL)
211 #else
212 #define TRASE_DISPATCH_GL
213 #endif
214 
215 #define TRASE_DISPATCH_BACKENDS \
216  TRASE_DISPATCH_SVG \
217  TRASE_DISPATCH_GL
218 
219 #ifdef TRASE_BACKEND_GL
220 #include "backend/BackendGL.hpp"
221 #endif
222 #include "backend/BackendSVG.hpp"
223 
224 #endif // DRAWABLE_H_
std::vector< float > m_times
the animation frame times (see add_frame_time())
Definition: Drawable.hpp:117
a base class for all the backends that support drawing a single frame
Definition: Backend.hpp:50
Drawable * m_parent
parent of this object
Definition: Drawable.hpp:105
Style m_style
fully styling information for each drawable
Definition: Drawable.hpp:123
std::shared_ptr< Figure > figure(std::array< float, 2 > pixels={ {800.0, 600.0}})
create a new Figure
Definition: Figure.hpp:125
FrameInfo m_frame_info
stores information on the current draw time (see update_frame_info())
Definition: Drawable.hpp:120
float m_time_span
the animation time span (see update_time_span())
Definition: Drawable.hpp:114
Base class for drawable objects in a figure.
Definition: Drawable.hpp:99
const bfloat2_t & area() const
returns this objects drawable area as a ratio of the parents drawable area
Definition: Drawable.hpp:159
bfloat2_t m_pixels
the area of this object in raw pixels (updated by resize())
Definition: Drawable.hpp:111
Definition: BackendGL.hpp:55
bfloat2_t m_area
the area of this object as a ratio of its parent object
Definition: Drawable.hpp:108
bfloat2_t & area()
returns this objects drawable area as a ratio of the parents drawable area
Definition: Drawable.hpp:162
a base class for all the backends that support animation over time
Definition: Backend.hpp:62
const float & time_span() const
returns time span of the animation
Definition: Drawable.hpp:168
A helper struct for Drawable that holds frame-related information.
Definition: Drawable.hpp:53
Definition: BackendSVG.hpp:73
std::vector< std::shared_ptr< Drawable > > m_children
a list of Drawables that are children of this object
Definition: Drawable.hpp:102
Definition: Backend.cpp:39
Definition: Style.hpp:43
const bfloat2_t & pixels() const
returns this objects drawable area in raw pixels
Definition: Drawable.hpp:156