Skip to content

Commit

Permalink
code to load .st files in the interface -- useful when trying to unde…
Browse files Browse the repository at this point in the history
…rstand .st files made by other programs
  • Loading branch information
ixchow committed May 8, 2024
1 parent d7a2a20 commit 8b4f270
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
19 changes: 15 additions & 4 deletions Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1902,17 +1902,28 @@ void Interface::update_traced_tristrip() {
glm::u8vec4(0xee, 0x55, 0xbb, 0xff)
};

struct YarnInfo {
glm::vec3 prev;
uint32_t color;
};

std::unordered_map< uint32_t, YarnInfo > yarn_infos;

uint32_t yarn_color = 0;
std::vector< GLAttribBuffer< glm::vec3, glm::vec3, glm::u8vec4 >::Vertex > attribs;
for (uint32_t ti = 1; ti < traced.size(); ++ti) {
if (traced[ti].yarn != traced[ti-1].yarn) {
for (auto const &ts : traced) {
auto res = yarn_infos.emplace(ts.yarn, YarnInfo());
if (res.second) {
res.first->second.prev = ts.at;
res.first->second.color = yarn_color;
yarn_color = (yarn_color + 1) % yarn_colors.size();
continue;
}
make_tube(&attribs,
traced[ti-1].at, traced[ti].at,
res.first->second.prev, ts.at,
0.01f,
yarn_colors[yarn_color]);
yarn_colors[res.first->second.color]);
res.first->second.prev = ts.at;
}
for (uint32_t ti = 0; ti < traced.size(); ++ti) {
glm::vec3 const &at = traced[ti].at;
Expand Down
31 changes: 31 additions & 0 deletions init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "Interface.hpp"
#include "TaggedArguments.hpp"

#include "Stitch.hpp"

#include <kit/kit.hpp>
#include <kit/Load.hpp>

Expand All @@ -20,6 +22,7 @@ std::shared_ptr< kit::Mode > kit_mode() {
std::string save_constraints_file = "";
std::string constraints_file = "";
std::string save_traced_file = "";
std::string load_traced_file = "";
float constraint_tolerance = 0.01f;
int32_t peel_test = 0;
int32_t peel_step = 0;
Expand All @@ -35,6 +38,7 @@ std::shared_ptr< kit::Mode > kit_mode() {
args.emplace_back("constraints", &constraints_file, "try to load constraints from the named file, and definitely save them to it (load_constraints_file or save_constraints_file will override)");
args.emplace_back("constraint-tolerance", &constraint_tolerance, "maximum distance away from a model vertex that a loaded constraint vertex can be before it is considered missing");
args.emplace_back("save-traced", &save_traced_file, "save traced stitches to this file");
args.emplace_back("load-traced", &load_traced_file, "load and view traced stitches from this file (may break other interface actions)");
args.emplace_back("stitch-width", &parameters.stitch_width_mm, "stitch width (mm)");
args.emplace_back("stitch-height", &parameters.stitch_height_mm, "stitch height (mm)");
args.emplace_back("peel-test", &peel_test, "run N rounds of peeling then quit (-1 to run until done)");
Expand Down Expand Up @@ -90,6 +94,33 @@ std::shared_ptr< kit::Mode > kit_mode() {
interface->DEBUG_test_linking(test_constraints < 0);
}

//hack to make it easier to view output ".st" files:
if (load_traced_file != "") {
std::vector< Stitch > stitches;
if (!load_stitches(load_traced_file, &stitches)) {
std::cerr << "ERROR: failed to load traced stitches from '" << load_traced_file << "'." << std::endl;
return nullptr;
}
interface->traced.clear();
interface->traced.reserve(stitches.size());
for (auto const &stitch : stitches) {
ak::TracedStitch ts;
ts.yarn = stitch.yarn;
ts.type = ak::TracedStitch::Type(stitch.type);
ts.dir = ak::TracedStitch::Dir(stitch.direction);
ts.ins[0] = stitch.in[0];
ts.ins[1] = stitch.in[1];
ts.outs[0] = stitch.out[0];
ts.outs[1] = stitch.out[1];
ts.at = stitch.at;
interface->traced.emplace_back(ts);
}

std::cout << "Loaded traced stitches from '" << load_traced_file << "' -- these will probably be visible until you do anything with tracing." << std::endl;
interface->show = Interface::ShowTraced;
interface->traced_tristrip_dirty = true;
}

if (peel_test != 0 || peel_step != 0) {
uint32_t target = (peel_test != 0 ? uint32_t(peel_test) : uint32_t(peel_step));
interface->clear_peeling();
Expand Down
2 changes: 1 addition & 1 deletion pipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ void build_next_active_chains(


struct TracedStitch {
uint32_t yarn = -1U; //yarn ID (why is this on a yarn_in? I guess the schedule.cpp code will tell me someday.
uint32_t yarn = -1U; //yarn ID (why is this on a yarn_in? I guess the schedule.cpp code will tell me someday.)
//ins and outs are in construction order (OLD was: CW direction):
uint32_t ins[2] = {-1U, -1U};
uint32_t outs[2] = {-1U, -1U};
Expand Down

0 comments on commit 8b4f270

Please sign in to comment.