Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dmc manipulator #128

Merged
merged 26 commits into from
May 20, 2022
Merged

Conversation

yufansong
Copy link
Collaborator

@yufansong
Copy link
Collaborator Author

yufansong commented May 19, 2022

file to test the GetManipulatorXML function. Need to create manipulator.xml in dmc directory.

#include <bits/stdc++.h>

std::string GetFileContent(const std::string &base_path,
                           const std::string &asset_name)
{
    // hardcode path here :(
    std::string filename = base_path + "/" + asset_name;
    std::ifstream ifs(filename);
    std::stringstream ss;
    ss << ifs.rdbuf();
    return ss.str();
}

std::string ReplaceRegex(const std::string &content, const std::string &unused_prop)
{
    std::ostringstream pattern_ss;
    pattern_ss << "<body name=\"" << unused_prop
               << R"("((?!</body>)[\s\S])+</body>)";
    std::regex pattern(pattern_ss.str());
    std::stringstream ss;
    ss << regex_replace(content, pattern, "");
    return ss.str();
}

std::string GetManipulatorXML(const std::string &base_path,
                              const std::string &task_name)
{
    std::vector<std::string> unused_props;
    if (task_name == "bring_ball") {
        unused_props = {"slot", "target_peg", "cup", "peg"};
    } else if (task_name == "bring_peg") {
        unused_props = {"slot", "target_ball", "cup", "ball"};
    } else if (task_name == "insert_ball") {
        unused_props = {"slot", "target_peg", "peg"};
    } else if (task_name == "insert_peg") {
        unused_props = {"target_ball", "cup", "ball"};
    }
    std::string content = GetFileContent(base_path, "manipulator.xml");
    for (const auto &prop : unused_props) {
        content = ReplaceRegex(content, prop);
    }
    std::cout << content.c_str() << std::endl;
    return content;
}

int main(int argc, char **argv) {
    GetManipulatorXML("dmc", argv[1]);
}

@Trinkle23897
Copy link
Collaborator

Trinkle23897 commented May 19, 2022

#include <bits/stdc++.h>

#include "pugixml.hpp"

std::string GetFileContent(const std::string& base_path,
                           const std::string& asset_name) {
  // hardcode path here :(
  std::string filename = base_path + "/" + asset_name;
  std::ifstream ifs(filename);
  std::stringstream ss;
  ss << ifs.rdbuf();
  return ss.str();
}

struct xml_string_writer: pugi::xml_writer
{
    std::string result;

    virtual void write(const void* data, size_t size)
    {
        result.append(static_cast<const char*>(data), size);
    }
};

std::string GetManipulatorXML(const std::string &base_path,
                              const std::string &task_name)
{
    std::vector<std::string> unused_props;
    if (task_name == "bring_ball") {
        unused_props = {"slot", "target_peg", "cup", "peg"};
    } else if (task_name == "bring_peg") {
        unused_props = {"slot", "target_ball", "cup", "ball"};
    } else if (task_name == "insert_ball") {
        unused_props = {"slot", "target_peg", "peg"};
    } else if (task_name == "insert_peg") {
        unused_props = {"target_ball", "cup", "ball"};
    }
    std::string content = GetFileContent(base_path, "manipulator.xml");
    pugi::xml_document doc;
    doc.load_string(content.c_str());
    for (auto prop : unused_props) {
        std::string xpath = "//body[@name='" + prop + "']";
        pugi::xml_node node = doc.select_node(xpath.c_str()).node();
        auto parent = node.parent();
        parent.remove_child(node);
    }
    xml_string_writer writer;
    doc.print(writer);
    return writer.result;
}

int main(int argc, char **argv) {
    std::cout << GetManipulatorXML("dmc", argv[1]) << std::endl;
}

@Trinkle23897 Trinkle23897 merged commit 1f38720 into sail-sg:master May 20, 2022
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants