factor out SegmentsFromFile
Signed-off-by: Jeff Epler <jepler@unpythonic.net>
This commit is contained in:
parent
ad50918ad9
commit
68f603353a
3 changed files with 80 additions and 38 deletions
4
Makefile
4
Makefile
|
|
@ -5,9 +5,9 @@ default: dashing
|
|||
bench: dashing
|
||||
perf stat --log-fd=2 ./dashing -b -s .1 data/HWOOD6E1.pat data/sf.seg
|
||||
|
||||
dashing: main.cc dashing.cc dashing.hh parse_numbers.hh
|
||||
dashing: main.cc dashing.cc dashing.hh parse_numbers.hh contours_and_segments.hh
|
||||
g++ -W -Wall -O2 -g -std=c++11 $(filter %.cc, $^) -o $@ -lboost_random
|
||||
dashing-noopt: main.cc dashing.cc dashing.hh parse_numbers.hh
|
||||
dashing-noopt: main.cc dashing.cc dashing.hh parse_numbers.hh contours_and_segments.hh
|
||||
g++ -W -Wall -O0 -g -std=c++11 $(filter %.cc, $^) -o $@ -lboost_random
|
||||
dashing-clang: main.cc dashing.cc dashing.hh parse_numbers.hh
|
||||
clang++ -W -Wall -O2 -g -std=c++11 $(filter %.cc, $^) -o $@ -lboost_random
|
||||
|
|
|
|||
77
contours_and_segments.hh
Normal file
77
contours_and_segments.hh
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#include "dashing.hh"
|
||||
#include "parse_numbers.hh"
|
||||
#include <random>
|
||||
#include <iostream>
|
||||
|
||||
namespace dashing
|
||||
{
|
||||
|
||||
typedef std::vector<Segment> Segments;
|
||||
typedef std::vector<Point> Contour;
|
||||
typedef std::vector<Contour> Contours;
|
||||
|
||||
void ContourToSegments(Segments &dest,
|
||||
/* EXPLICIT COPY */ Contour src,
|
||||
double jitter = 0)
|
||||
{
|
||||
if(jitter)
|
||||
{
|
||||
static auto && gen = std::minstd_rand(std::random_device{}());
|
||||
std::uniform_real_distribution<> dist(-jitter/2, jitter/2);
|
||||
for(auto &p : src)
|
||||
{
|
||||
p.x += dist(gen);
|
||||
p.y += dist(gen);
|
||||
}
|
||||
}
|
||||
|
||||
for(size_t i=0; i<src.size()-1; i++)
|
||||
dest.emplace_back(Segment{src[i], src[i+1], false});
|
||||
|
||||
int i = src.size();
|
||||
dest.emplace_back(Segment{src[i-1], src[0], false});
|
||||
}
|
||||
|
||||
void ContoursToSegments(Segments &dest, Contours const &src, double jitter=0)
|
||||
{
|
||||
dest.clear();
|
||||
|
||||
for(auto &contour : src)
|
||||
{
|
||||
ContourToSegments( dest, contour, jitter);
|
||||
}
|
||||
}
|
||||
|
||||
Contours ContoursFromFile(std::istream &fi)
|
||||
{
|
||||
auto result = Contours{};
|
||||
auto line = std::string{};
|
||||
|
||||
while(getline(fi, line)) {
|
||||
auto && coordinates = parse_numbers(line);
|
||||
if(coordinates.size() % 2 != 0)
|
||||
throw std::invalid_argument("odd number of values in segment line");
|
||||
if(coordinates.size() < 6)
|
||||
throw std::invalid_argument("too few values in segment line");
|
||||
|
||||
auto contour = Contour{};
|
||||
for(size_t i=0; i<coordinates.size(); i += 2)
|
||||
contour.emplace_back(Point{coordinates[i], coordinates[i+1]});
|
||||
result.emplace_back(std::move(contour));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Segments SegmentsFromFile(std::istream &fi, double jitter) {
|
||||
auto && contours = ContoursFromFile(fi);
|
||||
auto result = Segments{};
|
||||
ContoursToSegments(result, contours, jitter);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<Segment> SegmentsFromFile(const char *filename, double jitter) {
|
||||
std::fstream fi(filename);
|
||||
return SegmentsFromFile(fi, jitter);
|
||||
}
|
||||
|
||||
}
|
||||
37
main.cc
37
main.cc
|
|
@ -21,45 +21,10 @@ freely, subject to the following restrictions:
|
|||
// demo and benchmark program for dashing
|
||||
|
||||
#include "dashing.hh"
|
||||
#include "parse_numbers.hh"
|
||||
#include <iostream>
|
||||
#include <boost/random.hpp>
|
||||
#include <boost/random/random_device.hpp>
|
||||
#include "contours_and_segments.hh"
|
||||
|
||||
using namespace dashing;
|
||||
|
||||
std::vector<Segment> SegmentsFromFile(std::istream &fi, double jitter) {
|
||||
static boost::random_device urandom;
|
||||
static boost::taus88 gen(urandom);
|
||||
boost::uniform_real<> dist(-jitter/2, jitter/2);
|
||||
boost::variate_generator<boost::taus88&, boost::uniform_real<> >
|
||||
random(gen, dist);
|
||||
std::vector<Segment> result;
|
||||
std::string line;
|
||||
while(getline(fi, line)) {
|
||||
auto numbers = parse_numbers(line);
|
||||
if(jitter)
|
||||
for(auto & n : numbers) n += random();
|
||||
if(numbers.size() % 2 != 0)
|
||||
throw std::invalid_argument("odd number of values in segment line");
|
||||
if(numbers.size() < 6)
|
||||
throw std::invalid_argument("too few values in segment line");
|
||||
for(size_t i=0; i<numbers.size() - 3 ; i += 2) {
|
||||
Segment s{{numbers[i], numbers[i+1]}, {numbers[i+2], numbers[i+3]}, false};
|
||||
result.push_back(s);
|
||||
}
|
||||
int i = numbers.size();
|
||||
Segment s{{numbers[i-2], numbers[i-1]}, {numbers[0], numbers[1]}, false};
|
||||
result.push_back(s);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<Segment> SegmentsFromFile(const char *filename, double jitter) {
|
||||
std::fstream fi(filename);
|
||||
return SegmentsFromFile(fi, jitter);
|
||||
}
|
||||
|
||||
const char *argv0 = "dashing";
|
||||
|
||||
#ifdef __GNUC__
|
||||
|
|
|
|||
Loading…
Reference in a new issue