-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
274 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2017, | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
#pragma once | ||
|
||
#include<limits> | ||
#include <cstdio> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <memory> | ||
|
||
#include <vector> | ||
#include <string> | ||
|
||
namespace gsimp { | ||
|
||
class drawing { | ||
struct impl; | ||
std::shared_ptr<impl> pimpl; | ||
std::string filename, temp_filename; | ||
public: | ||
drawing(std::string, int, int); | ||
drawing &operator=(const drawing &other); | ||
void add_arrow_definition(); | ||
void draw_arrow(std::vector<double>, std::vector<double>, std::string, double); | ||
void draw_edge(std::vector<double>, std::vector<double>, std::string, double); | ||
void draw_triangle(std::vector<std::vector<double>>, std::string); | ||
}; | ||
|
||
struct drawing::impl { | ||
std::ofstream file; | ||
double maxx = -std::numeric_limits<double>::infinity(); | ||
double maxy = -std::numeric_limits<double>::infinity(); | ||
double minx = std::numeric_limits<double>::infinity(); | ||
double miny = std::numeric_limits<double>::infinity(); | ||
int size_x, size_y; | ||
std::string element_definitions; | ||
bool has_arrows; | ||
std::string filename; | ||
std::string temp_filename; | ||
|
||
|
||
impl(std::string filename, int sizex, int sizey) | ||
: size_x(sizex), size_y(sizey) { | ||
this->filename = filename + ".svg"; | ||
temp_filename = filename + "-temp.svg"; | ||
file.open(temp_filename); | ||
} | ||
|
||
~impl() { | ||
file.close(); | ||
std::ifstream temp_file(temp_filename); | ||
std::ofstream file_finished; | ||
file_finished.open(filename); | ||
|
||
file_finished << "<svg xmlns='http://www.w3.org/2000/svg' version='1.1' viewBox='" // | ||
<< 0 << " " // | ||
<< 0 << " " // | ||
<< maxx - minx << " " // | ||
<< maxy - miny << "' height = '500px' width='500px'>\n"; | ||
file_finished << "<g transform='translate(" << -minx << "," << -miny << ")' >\n"; | ||
for (std::string file_line; std::getline(temp_file, file_line);) { | ||
file_finished << file_line + "\n"; | ||
} | ||
file_finished << "</g>\n"; | ||
file_finished << "</svg>\n"; | ||
file_finished.close(); | ||
std::remove(temp_filename.c_str()); | ||
} | ||
|
||
void adjust_to_point(std::vector<double> point) { | ||
minx = fmin(point[0] , minx); | ||
maxx = fmax(point[0] , maxx); | ||
miny = fmin(point[1] , miny); | ||
maxy = fmax(point[1] , maxy); | ||
} | ||
}; | ||
|
||
drawing& drawing::operator=(const drawing& other) { | ||
pimpl = std::move(other.pimpl); | ||
return *this; | ||
} | ||
|
||
drawing::drawing(std::string filename, int sizex, int sizey) { | ||
pimpl = std::shared_ptr<impl>(new impl(filename, sizex, sizey)); | ||
} | ||
|
||
void drawing::draw_triangle(std::vector<std::vector<double>> coords, | ||
std::string color) { | ||
if (coords.size() != 3) throw "A triangle should have 3 vertices"; | ||
|
||
std::string tri("<polyline points='"); | ||
for (auto coord : coords) { | ||
double x(coord[0]), y(coord[1]); | ||
tri += std::to_string(x) + ", " + std::to_string(y) + " "; | ||
pimpl->adjust_to_point(coord); | ||
} | ||
tri += "' stroke='none' fill='" + color + "'/>\n"; | ||
pimpl->file << tri; | ||
} | ||
|
||
void drawing::add_arrow_definition() { | ||
std::string arrow_defn = "<defs>\n"; // | ||
arrow_defn += "<marker id='head' orient= auto "; // | ||
arrow_defn += "markerHeight='6' markerWidth='6'", // | ||
arrow_defn += "refX='0' refY='1.5'>\n"; // | ||
arrow_defn += "<path d='M 0 0 V 3 L 4 1.5 Z' fill='#ff0000'/>\n"; // | ||
arrow_defn += "</marker>\n"; // | ||
arrow_defn += "</defs>\n"; | ||
|
||
pimpl->file << arrow_defn; | ||
} | ||
|
||
void drawing::draw_edge(std::vector<double> src, std::vector<double> trg, | ||
std::string color, double stroke) { | ||
std::string line_s = "<path stroke-linecap='round' "; // | ||
line_s += "stroke-width='"; // | ||
line_s += std::to_string(stroke); // | ||
line_s += "' fill='none' stroke='"; // | ||
line_s += color; // | ||
line_s += "' d='M"; // | ||
|
||
std::string bgn(""), end(","); | ||
for (int i = 0; i < src.size(); ++i) { | ||
bgn += std::to_string(src[i]) + " "; | ||
end += std::to_string(trg[i]) + " "; | ||
} | ||
line_s += bgn + end + "'/>\n"; | ||
pimpl->file << line_s; | ||
} | ||
|
||
void drawing::draw_arrow(std::vector<double> src, std::vector<double> trg, | ||
std::string color, double stroke) { | ||
std::string arrow = // | ||
"<path id = 'arrow' stroke-linecap='round' stroke-width='"; // | ||
arrow += std::to_string(stroke); // | ||
arrow += "' fill='none' stroke="; // | ||
arrow += color; // | ||
arrow += " d = 'M "; // | ||
|
||
std::string bgn(" "), mid(" L "), end(" L "); | ||
for (int i = 0; i < src.size(); ++i) { | ||
bgn += std::to_string(src[i]) + " "; | ||
mid += std::to_string(trg[i] - src[i] / 2) + " "; | ||
end += std::to_string(trg[i]) + "' "; | ||
} | ||
arrow += bgn + mid + end + " />"; | ||
(pimpl->file) << arrow; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include <scomplex/types.hpp> | ||
#include <scomplex/drawing_utils.hpp> | ||
#include <scomplex/simplicial_complex.hpp> | ||
|
||
namespace gsimp { | ||
|
||
class DimensionException : public std::exception {}; | ||
|
||
void draw_complex(simplicial_complex& scomp, drawing& my_drawing, | ||
double line_thickness, std::string fill_color = "#00ff00", | ||
std::string line_color = "#000000") { | ||
if (scomp.dimension() > 2) DimensionException(); | ||
|
||
auto points = scomp.get_points(); | ||
auto edges = scomp.get_level(1); | ||
auto tris = scomp.get_level(2); | ||
|
||
for (auto cell : tris) { | ||
std::vector<std::vector<double>> cell_pt; | ||
for (auto i : cell) cell_pt.push_back(points[i]); | ||
my_drawing.draw_triangle(cell_pt, fill_color); | ||
} | ||
|
||
for (auto cell : scomp.get_level(1)) { | ||
std::vector<std::vector<double>> cell_pt; | ||
for (auto i : cell) cell_pt.push_back(points[i]); | ||
my_drawing.draw_edge(cell_pt[0], cell_pt[1], line_color, | ||
line_thickness); | ||
} | ||
} | ||
|
||
void draw_path(path_snapper& p_snap, drawing& my_drawing, | ||
std::vector<std::vector<double>> path, double stroke, | ||
std::string path_color = "#ff0000") { | ||
auto snapped = p_snap.snap_path_to_points(path); | ||
auto it = snapped.begin(); | ||
auto trail = it++; | ||
for (; it != snapped.end(); ++it, ++trail) { | ||
auto init = *trail; | ||
auto dest = *it; | ||
my_drawing.draw_edge(init, dest, path_color, stroke); | ||
} | ||
} | ||
|
||
void draw_2_chain(simplicial_complex& s_comp, drawing& my_drawing, | ||
chain_t chain, std::string chain_color = "#ff0000") { | ||
if (chain_dim(chain) != 2) DimensionException(); | ||
|
||
auto points = s_comp.get_points(); | ||
auto level_2 = s_comp.get_level(2); | ||
Eigen::SparseVector<double>::InnerIterator it(chain_rep(chain)); | ||
for (; it; ++it) { | ||
cell_t cell = level_2[it.index()]; | ||
std::vector<point_t> cell_pt; | ||
for (size_t i : cell) cell_pt.push_back(points[i]); | ||
// TODO: get rid of these thresholds | ||
if (it.value() > 0.01 || it.value() < -0.01) | ||
my_drawing.draw_triangle(cell_pt, chain_color); | ||
} | ||
} | ||
void draw_1_chain(simplicial_complex& s_comp, drawing& my_drawing, | ||
chain_t chain, double stroke, | ||
std::string chain_color = "#ff0000") { | ||
if (chain_dim(chain) != 1) DimensionException(); | ||
|
||
auto points = s_comp.get_points(); | ||
auto level_1 = s_comp.get_level(1); | ||
Eigen::SparseVector<double>::InnerIterator it(chain_rep(chain)); | ||
for (; it; ++it) { | ||
cell_t edge = level_1[it.index()]; | ||
my_drawing.draw_edge(points[edge[0]], points[edge[1]], chain_color, | ||
stroke); | ||
} | ||
} | ||
|
||
void draw_path(simplicial_complex& scomp, drawing& my_drawing, | ||
std::vector<std::vector<double>> path, double stroke, | ||
std::string path_color = "#ff0000") { | ||
path_snapper p_snap(scomp); | ||
auto snapped = p_snap.snap_path_to_points(path); | ||
auto it = snapped.begin(); | ||
auto trail = it++; | ||
for (; it != snapped.end(); ++it, ++trail) { | ||
auto init = *trail; | ||
auto dest = *it; | ||
my_drawing.draw_edge(init, dest, path_color, stroke); | ||
} | ||
} | ||
|
||
drawing draw_complex(simplicial_complex& scomp, std::string filename, | ||
double stroke, int width = 500, int height = 500, | ||
std::string fill_color = "#00ff00", | ||
std::string line_color = "#000000") { | ||
drawing my_drawing(filename, width, height); | ||
draw_complex(scomp, my_drawing, stroke, fill_color, line_color); | ||
return my_drawing; | ||
} | ||
}; |