Skip to content
Snippets Groups Projects
Commit 1c08289c authored by Pascal Engeler's avatar Pascal Engeler
Browse files

Added c++ program to extract data

parent fbb6f07a
No related branches found
No related tags found
No related merge requests found
#pragma once
#include <vector>
#include <string>
class LineStrip{
public:
LineStrip(std::vector<double> nodes);
bool isClosed() const;
bool isActive() const;
int canBeAttached(const LineStrip& other) const;
void attach(LineStrip& other, int mode);
void clear();
void closeLoop();
std::vector<double> nodes() const;
std::string str() const;
private:
bool _isClosed;
bool _isActive;
std::vector<double> _nodes;
};
\ No newline at end of file
#include <linestrip.hpp>
#include <fstream>
#include <vector>
#include <iostream>
int main(){
std::vector<LineStrip> linestrips;
linestrips.reserve(273050); //this is the amount of linestrips expected
std::string filepath = "../python/line_strips.txt";
std::ifstream ifs(filepath);
if(!ifs){
std::cout << "Problem opening file." << std::endl;
}
int count;
double bufs;
std::vector<double> buf;
buf.reserve(100);
while(true){
buf.clear();
ifs >> count; //get number of nodes in next strip
if(!ifs){
break; //no more data to read
}
for(int i = 0; i < 2*count; ++i){
ifs >> bufs;
buf.push_back(bufs);
}
linestrips.emplace_back(buf);
}
std::cout << "Loaded " << linestrips.size() << " linestrips." << std::endl;
int start = 0;
int appends = 0;
int polys_finished = 0;
for(int iteration = 0; iteration < 500000; ++iteration){ //test with 10k iterations
bool update_start = false;
while((linestrips[start].isClosed() || !linestrips[start].isActive()) && (start < linestrips.size())){
#ifndef NDEBUG
std::cout << "Incrementing start, start=" << start+1 << std::endl;
#endif
++start;
update_start = true;
}
if(start >= linestrips.size()){
break;
}
if(update_start){
polys_finished += 1;
std::cout << "Finished " << polys_finished << " polygons" << std::endl;
}
bool change = false;
for(int i = start+1; i < linestrips.size(); ++i){
if(!linestrips[i].isActive() || linestrips[i].isClosed()){
#ifndef NDEBUG
//std::cout << "Skipping " << i << std::endl;
#endif
continue;
}
int mode = linestrips[start].canBeAttached(linestrips[i]);
if(mode != 0){
change = true;
//std::cout << "Attaching " << i << " to " << start << ", mode " << mode << std::endl;
//std::cout << "Size before insertion: " << linestrips[start].nodes().size() << std::endl;
linestrips[start].attach(linestrips[i], mode);
//std::cout << "Size after insertion: " << linestrips[start].nodes().size() << std::endl;
linestrips[i].clear();
++appends;
//std::cout << "Strip " << start << " length: " << linestrips[start].nodes().size() << std::endl;
break;
}
}
if(!change){
//assume this loop is closed, move on
linestrips[start].closeLoop();
}
}
std::cout << "Done " << appends << " appends, start is " << start << std::endl;
#ifndef NDEBUG //this only works if start is in range
std::cout << "Current Linestrip start: (" << linestrips[start].nodes()[0] << " " << linestrips[start].nodes()[1] << ")" << std::endl;
size_t size_n = linestrips[start].nodes().size();
std::cout << "Current Linestrip end: (" << linestrips[start].nodes()[size_n-2] << " " << linestrips[start].nodes()[size_n-1] << ")" << std::endl;
std::cout << "Last Linestrip:" << std::endl;
for(auto d: linestrips[start].nodes()){
std::cout << d << ", ";
}
std::cout << std::endl;
std::cout << "Previous Linestrip:" << std::endl;
while(!linestrips[--start].isClosed()){}
for(auto d: linestrips[start].nodes()){
std::cout << d << ", ";
}
std::cout << std::endl;
#endif
std::cout << "Preparing result string" << std::endl;
std::string resultString = "";
size_t num_polygons = 0;
for(auto ls: linestrips){
if(ls.isActive()){
resultString += ls.str() + "\n";
++num_polygons;
}
}
std::cout << "Writing " << num_polygons << " polygons to file" << std::endl;
std::fstream outfile("polygons.txt", std::ios::out);
outfile.write(resultString.c_str(), resultString.size());
outfile.close();
return 0;
}
\ No newline at end of file
#include <linestrip.hpp>
#include <algorithm>
bool isEqual(const double x, const double y, const double tol=1e-8){
return (x - y < tol) && (y - x < tol);
}
void reverseNodes(std::vector<double>& nodes){
std::reverse(nodes.begin(), nodes.end());
for(size_t i = 0; i < nodes.size()/2; ++i){
std::swap(nodes[2*i], nodes[2*i+1]);
}
}
LineStrip::LineStrip(std::vector<double> nodes): _nodes(nodes), _isClosed(false), _isActive(true) {}
void LineStrip::attach(LineStrip& other, int mode){
if(mode == 1){ //normal attach
_nodes.reserve(_nodes.size() + other._nodes.size()); //reserve
_nodes.insert(_nodes.end(), other._nodes.begin()+2, other._nodes.end());
}
else if(mode == 2){ //reverse attach
other._nodes.reserve(_nodes.size() + other._nodes.size()); //reserve
other._nodes.insert(other._nodes.end(), _nodes.begin()+2, _nodes.end());
_nodes.reserve(other._nodes.size());
_nodes.clear();
std::copy(other._nodes.begin(), other._nodes.end(), std::back_inserter(_nodes));
}
else if(mode == 3){ //end-to-end attach
_nodes.reserve(_nodes.size() + other._nodes.size()); //reserve
//std::reverse(other._nodes.begin(), other._nodes.end());
reverseNodes(other._nodes);
_nodes.insert(_nodes.end(), other._nodes.begin()+2, other._nodes.end());
}
else if(mode == 4){ //start-to-start attach
_nodes.reserve(_nodes.size() + other._nodes.size()); //reserve
//std::reverse(_nodes.begin(), _nodes.end());
reverseNodes(_nodes);
_nodes.insert(_nodes.end(), other._nodes.begin()+2, other._nodes.end());
}
else{ //the fuck are you even doing
return;
}
if((_nodes[0] == _nodes[_nodes.size()-2]) && (_nodes[1] == _nodes[_nodes.size()-1])){
_isClosed = true;
}
}
void LineStrip::clear(){
_nodes.clear();
_isActive = false;
}
int LineStrip::canBeAttached(const LineStrip& other) const {
bool normalAttach = isEqual(other._nodes[0], _nodes[_nodes.size()-2]) && isEqual(other._nodes[1],_nodes[_nodes.size()-1]);
bool reverseAttach = isEqual(other._nodes[other._nodes.size()-2], _nodes[0]) && isEqual(other._nodes[other._nodes.size()-1],_nodes[1]);
bool endToEndAttach = isEqual(other._nodes[other._nodes.size()-2], _nodes[_nodes.size()-2]) && isEqual(other._nodes[other._nodes.size()-1],_nodes[_nodes.size()-1]);
bool startToStartAttach = isEqual(other._nodes[0], _nodes[0]) && isEqual(other._nodes[1],_nodes[1]);
return normalAttach + 2*reverseAttach + 3*endToEndAttach + 4*startToStartAttach;
}
bool LineStrip::isActive() const {
return _isActive;
}
bool LineStrip::isClosed() const {
return _isClosed;
}
std::vector<double> LineStrip::nodes() const{
return _nodes;
}
void LineStrip::closeLoop(){
_nodes.push_back(_nodes[0]);
_nodes.push_back(_nodes[1]);
_isClosed = true;
}
std::string LineStrip::str() const{
if(!_isActive){
return "";
}
std::string returnStr = std::to_string(_nodes[0]);
for(size_t i = 1; i < _nodes.size(); ++i){
returnStr += " " + std::to_string(_nodes[i]);
}
return returnStr;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment