Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/manual/manual/visualization/figures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@ VIBes only
Some methods are exclusive to the real-time display with VIBes :

- center_viewbox : takes two Vector as arguments, the center and radius of each axis
- auto_scale : takes no argument, the figure will be automatically scaled to fit the window
- auto_scale : takes no argument, the figure will be automatically scaled to fit the window
- save : takes the name of the file as argument and saves the current state of the VIBes window to the file. Allowed formats are png, jpg, bmp and svg.
21 changes: 21 additions & 0 deletions examples/00_graphics/graphic_animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,25 @@ int main()
theta += 2.0 * M_PI / (double) steps;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

Figure2D fig_save ("Animation with exports",GraphicOutput::VIBES|GraphicOutput::IPE);
fig_save.set_window_properties({600,50},{500,500}); // position, window size
fig_save.set_axes(axis(0,{-0.5,4}), axis(1,{-0.5,1.5}));

Vector X({0,0,0});

ColorMap cmap = ColorMap::rainbow();
for (double t = 0; t < 2.*PI; t+=PI/20.)
{
fig_save.draw_tank(X, 0.05, cmap.color(t/(2.*PI)));
X+=Vector({0.1*cos(X[2]),0.1*sin(X[2]),0.1*cos(t)});
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (Interval(PI).inflate(0.1).contains(t))
{
fig_save.save("animation_tank.png");
fig_save.save("animation_tank.jpg");
fig_save.save("animation_tank.bmp");
fig_save.save("animation_tank.svg");
}
}
}
22 changes: 21 additions & 1 deletion examples/00_graphics/graphic_animation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from codac import *
import math
import time

fig = Figure2D("Animation", GraphicOutput.VIBES | GraphicOutput.IPE)
Expand All @@ -11,4 +12,23 @@
fig.clear()
fig.draw_point([5*cos(theta).mid(), 5*sin(theta).mid()], Color.red())
theta += 2*PI / steps
time.sleep(0.1)
time.sleep(0.1)

fig_save = Figure2D("Animation with exports", GraphicOutput.VIBES | GraphicOutput.IPE)
fig_save.set_window_properties([600,50],[500,500]) # position, window size
fig_save.set_axes(axis(0,[-0.5,4]), axis(1,[-0.5,1.5]))

X = Vector([0,0,0])
t = 0
cmap = ColorMap.rainbow()

while t <2*PI:
fig_save.draw_tank(X, 0.05, cmap.color(t/(2*PI)))
X+=Vector([0.1*math.cos(X[2]),0.1*math.sin(X[2]),0.1*math.cos(t)])
time.sleep(0.1)
if (Interval(PI).inflate(0.1).contains(t)):
fig_save.save("animation_tank.png")
fig_save.save("animation_tank.jpg")
fig_save.save("animation_tank.bmp")
fig_save.save("animation_tank.svg")
t += PI/20
4 changes: 4 additions & 0 deletions python/src/graphics/figures/codac2_py_Figure2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ void export_Figure2D(py::module& m)

.def("clear", &Figure2D::clear,
VOID_FIGURE2D_CLEAR)

.def("save", &Figure2D::save,
VOID_FIGURE2D_SAVE_CONST_STRING_REF,
"filename"_a)

.def("scaled_unit", &Figure2D::scaled_unit,
DOUBLE_FIGURE2D_SCALED_UNIT_CONST)
Expand Down
4 changes: 4 additions & 0 deletions src/graphics/3rd/ipe/codac2_Figure2D_IPE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,14 @@ void Figure2D_IPE::clear()
// clear _color map and layers
_colors.clear();
_layers.clear();
_items.clear();

init_figure();
}

void Figure2D_IPE::save(const std::string& filename)
{}

std::string ipe_str(const Color& c)
{
return c.hex_str().substr(1);
Expand Down
7 changes: 7 additions & 0 deletions src/graphics/3rd/ipe/codac2_Figure2D_IPE.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ namespace codac2
*/
void clear();

/**
* \brief Saves the figure to a file
*
* \param filename Name of the file to save the figure to
*/
void save(const std::string& filename);

/**
* \brief Begins a new path in the IPE file
*
Expand Down
5 changes: 5 additions & 0 deletions src/graphics/3rd/vibes/codac2_Figure2D_VIBes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ void Figure2D_VIBes::clear()
_layers.clear();
}

void Figure2D_VIBes::save(const std::string& filename)
{
vibes::saveImage(filename, _fig.name());
}

void Figure2D_VIBes::draw_point(const Vector& c, const StyleProperties& style)
{
assert(_fig.size() <= c.size());
Expand Down
7 changes: 7 additions & 0 deletions src/graphics/3rd/vibes/codac2_Figure2D_VIBes.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ namespace codac2
* \brief Clears the figure
*/
void clear();

/**
* \brief Saves the figure to a file
*
* \param filename Name of the file to save the figure to
*/
void save(const std::string& filename);

// Geometric shapes

Expand Down
6 changes: 6 additions & 0 deletions src/graphics/figures/codac2_Figure2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ void Figure2D::clear()
output_fig->clear();
}

void Figure2D::save(const std::string& filename)
{
for(const auto& output_fig : _output_figures)
output_fig->save(filename);
}

double Figure2D::scaled_unit() const
{
return std::max(_axes[0].limits.diam(),_axes[1].limits.diam()) / _window_size.max_coeff();
Expand Down
18 changes: 18 additions & 0 deletions src/graphics/figures/codac2_Figure2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ namespace codac2
*/
void clear();

/**
* \brief Saves the figure to a file
*
* \param filename Name of the file to save the figure to
*/
void save(const std::string& filename);

/**
* \brief Getter for the scaling factor of the figure
*
Expand Down Expand Up @@ -715,6 +722,17 @@ namespace codac2
selected_fig()->clear();
}

/**
* \brief Saves the figure to a file
*
* \param filename Name of the file to save the figure to
*/
static void save(const std::string& filename)
{
auto_init();
selected_fig()->save(filename);
}

// Geometric shapes

/**
Expand Down
7 changes: 7 additions & 0 deletions src/graphics/figures/codac2_OutputFigure2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ namespace codac2
* \brief Clears the figure
*/
virtual void clear() = 0;

/**
* \brief Saves the figure to a file
*
* \param filename Name of the file to save the figure to
*/
virtual void save(const std::string& filename) = 0;

protected:

Expand Down
Loading