Trase is a lightweight scientific plotting library for C++ with animation support. It enables you to construct plots and write them out to animated svg files, or display them in an OpenGL window. The main library and svg backend have no dependencies other than the standard library. The OpenGL backend requires GLFW.
For example, the above svg image was generated with the following code.
auto fig = figure();
auto ax = fig->axis();
const int n = 100;
const int nframes = 10;
std::vector<float> x(n);
std::vector<float> y(n);
// define x points
for (int i = 0; i < n; ++i) {
x[i] = static_cast<float>(i) / n;
}
// define y = sin(x) with given amplitude and frequency
auto get_ysinx = [&](const float amplitude, const float freq) {
for (int i = 0; i < n; ++i) {
y[i] = amplitude * std::sin(6.28f * freq * x[i]);
}
return create_data().x(x).y(y);
};
// create a static sin(x) function
auto static_plot = ax->line(get_ysinx(1.f, 2.f));
static_plot->set_label("static");
// create a moving sin(x) function with varying amplitude
auto moving_plot = ax->line(get_ysinx(1.f, 5.f));
moving_plot->set_label("moving");
for (int i = 1; i <= nframes; ++i) {
const float nf = static_cast<float>(nframes);
const float amplitude = 1.f - 0.5f * std::sin(6.28f * i / nf);
moving_plot->add_frame(get_ysinx(amplitude, 5.f), 3.f * i / nf);
}
// set label axes
ax->xlabel("x");
ax->ylabel("y");
ax->title("the svg test");
ax->legend();
// output to svg
std::ofstream out;
out.open("readme.svg");
BackendSVG backend(out);
fig->draw(backend);
out.close();
- Clone this repository
$ git clone https://github.com/martinjrobins/trase
$ cd trase
- Build and install Trase. This uses an install dir of
$HOME/trase
, change this to wherever you like. This also builds the OpenGL backend of Trase (requires GLFW installed), switch this toOFF
if you only want the svg backend.
$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/trase -Dtrase_BUILD_OPENGL=ON ..
$ make install
- (alternate) If you are using the Xcode or Visual Studio generator, you need to specify the configuration in build time
$ mkdir build
$ cd build
$ cmake -DCMAKE_INSTALL_PREFIX=$HOME/trase -Dtrase_BUILD_OPENGL=ON ..
$ cmake --build . --target install --config Release
- In your C++ project, you might link against Trase with a
CMakeLists.txt
file like so:
cmake_minimum_required(VERSION 2.8.12)
project(test)
find_package(trase REQUIRED)
add_executable(myexe test.cpp)
target_link_libraries(myexe trase)
- When you build your project, you can tell CMake where you installed Trase
using the
CMAKE_PREFIX_PATH
variable
$ cmake -DCMAKE_PREFIX_PATH=$HOME/trase ..
$ make
Trase uses Dear ImGui and NanoVG for the OpenGL backend. The Dirent port for Windows is used for finding local font files.