<commit_before>/*
 * main.cpp
 *
 *  Created on: Feb 20, 2014
 *      Author: <NAME>
 */

#include <pcl/common/common.h>
#include <pcl_conversions.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include "ros/ros.h"
#include "sensor_msgs/PointCloud2.h"
#include "std_msgs/String.h"
#include "do_learning_srv_definitions/clear.h"
#include "do_learning_srv_definitions/learn_object.h"
#include "do_learning_srv_definitions/save_model.h"
#include "do_learning_srv_definitions/visualize.h"
#include <opencv2/opencv.hpp>
#include <v4r/common/io/filesystem_utils.h>

class DOLDemoFromFiles
{
private:
    typedef pcl::PointXYZRGB PointT;
    ros::NodeHandle *n_;
    std::string directory_,
                models_dir_,
                recognition_structure_dir_;
    bool visualize_;

public:
    bool callDOL()
    {
        //read files from directory
        std::string so_far = "";

        std::vector<std::string> keyframes_str;
        std::vector<std::string> object_indices_str;
        std::vector<std::string> poses_str;

        {
            std::string pattern = ".*.pcd";
            v4r::common::io::getFilesInDirectory(directory_, keyframes_str, so_far, pattern, false);
        }

        {
            std::string pattern = ".*object_indices.*.pcd";
            v4r::common::io::getFilesInDirectory(directory_, object_indices_str, so_far, pattern, false);
        }

        {
            std::string pattern = ".*pose.*.txt";
            v4r::common::io::getFilesInDirectory(directory_, poses_str, so_far, pattern, false);
        }

        std::sort(keyframes_str.begin(), keyframes_str.end());
        std::sort(poses_str.begin(), poses_str.end());
        std::sort(object_indices_str.begin(), object_indices_str.end());

        std::string service_name_learn = "/dynamic_object_learning/learn_object";
        ros::ServiceClient DOLclient = n_->serviceClient<do_learning_srv_definitions::learn_object>(service_name_learn);
        do_learning_srv_definitions::learn_object srv_learn;

        std::string service_name_save = "/dynamic_object_learning/save_model";
        ros::ServiceClient DOLclient_save = n_->serviceClient<do_learning_srv_definitions::save_model>(service_name_save);
        do_learning_srv_definitions::save_model srv_save;

        std::string service_name_vis = "/dynamic_object_learning/visualize";
        ros::ServiceClient DOLclient_vis = n_->serviceClient<do_learning_srv_definitions::visualize>(service_name_vis);
        do_learning_srv_definitions::visualize srv_vis;

        std::ifstream initial_mask_file;
        initial_mask_file.open(directory_ + "/mask.txt");

        int idx_tmp;
        pcl::PointIndices pind;
        while (initial_mask_file >> idx_tmp)
        {
            srv_learn.request.intial_object_indices.push_back(idx_tmp);
            pind.indices.push_back(idx_tmp);
        }
        initial_mask_file.close();

        for(size_t i=0; i < keyframes_str.size(); i++)
        {
            pcl::PointCloud<PointT>::Ptr pCloud(new pcl::PointCloud<PointT>());
            std::stringstream str;
            str << directory_ << "/" << keyframes_str[i];
            std::cout << str.str() << std::endl;
            pcl::io::loadPCDFile(str.str(), *pCloud);

            sensor_msgs::PointCloud2 msg_cloud;
            pcl::toROSMsg(*pCloud, msg_cloud);

            srv_learn.request.keyframes.push_back(msg_cloud);

            Eigen::Matrix4f trans;
            geometry_msgs::Transform tt;
            tt.translation.x = pCloud->sensor_origin_[0];
            tt.translation.y = pCloud->sensor_origin_[1];
            tt.translation.z = pCloud->sensor_origin_[2];

            Eigen::Matrix3f rotation = trans.block<3,3>(0,0);
            Eigen::Quaternionf q(rotation);
            tt.rotation.x = pCloud->sensor_orientation_.x();
            tt.rotation.y = pCloud->sensor_orientation_.y();
            tt.rotation.z = pCloud->sensor_orientation_.z();
            tt.rotation.w = pCloud->sensor_orientation_.w();
            srv_learn.request.transforms.push_back(tt);
        }

        if ( ! DOLclient.call(srv_learn) )
        {
            std::stringstream mm;
            mm << "Error calling service: " << service_name_learn << std::endl;
            std::cerr << mm.str() << std::endl;
            return false;
        }

        // Saving model
        srv_save.request.object_name.data = "my_dynamic_object.pcd";
        srv_save.request.models_folder.data = models_dir_;
        srv_save.request.recognition_structure_folder.data = recognition_structure_dir_;

        if ( ! DOLclient_save.call(srv_save) )
        {
            std::stringstream mm;
            mm << "Error calling service: " << service_name_save << std::endl;
            std::cerr << mm << std::endl;
            return false;
        }

        if (visualize_)
        {
            if ( ! DOLclient_vis.call ( srv_vis ) )
            {
                std::stringstream mm;
                mm << "Error calling service: " << service_name_vis << std::endl;
                std::cerr << mm.str() << std::endl;
            }
        }

        std::string service_name_clear = "/dynamic_object_learning/clear_cached_model";
        ros::ServiceClient DOL_clear_client = n_->serviceClient<do_learning_srv_definitions::clear>(service_name_clear);
        do_learning_srv_definitions::clear srv_clear;

        if ( ! DOL_clear_client.call ( srv_clear ) )
        {
            std::stringstream mm;
            mm << "Error calling service: " << service_name_clear << std::endl;
            std::cerr << mm.str() << std::endl;
        }
        return true;
    }

public:
    DOLDemoFromFiles()
    {
        visualize_ = true;
    }

    bool initialize(int argc, char ** argv)
    {
        ros::init (argc, argv, "DOLDemoFromFiles");
        if (sizeof(int) != 4)
        {
            ROS_WARN("PC Architectur does not use 32bit for integer - check conflicts with pcl indices.");
        }
        n_ = new ros::NodeHandle ( "~" );

        n_->getParam("visualize", visualize_);

        if(!n_->getParam ( "directory", directory_ ))
        {
            //directory_ = "/media/aitor14/DATA/STRANDS_MODELS/recognition_structure/playstation_turn_table.pcd/";
            ROS_ERROR("Specify a directory using param directory.\n");
            return false;
        }

        if(!n_->getParam( "models_dir", models_dir_) )
        {
            ROS_ERROR("Specify a model directory using param models_dir.\n");
            return false;
        }

        if(!n_->getParam( "recognition_dir", recognition_structure_dir_) )
        {
            ROS_ERROR("Specify a model directory using param recognition_dir.\n");
            exit(-1);
        }
        return true;
    }
};

int
main (int argc, char ** argv)
{
    DOLDemoFromFiles m;
    if (m.initialize(argc, argv))
        m.callDOL();
    else
        return -1;
    return 0;
}<commit_msg>Added parameter for object model name<commit_after>/*
 * main.cpp
 *
 *  Created on: Feb 20, 2014
 *      Author: <NAME>
 */

#include <pcl/common/common.h>
#include <pcl_conversions.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include "ros/ros.h"
#include "sensor_msgs/PointCloud2.h"
#include "std_msgs/String.h"
#include "do_learning_srv_definitions/clear.h"
#include "do_learning_srv_definitions/learn_object.h"
#include "do_learning_srv_definitions/save_model.h"
#include "do_learning_srv_definitions/visualize.h"
#include <opencv2/opencv.hpp>
#include <v4r/common/io/filesystem_utils.h>

class DOLDemoFromFiles
{
private:
    typedef pcl::PointXYZRGB PointT;
    ros::NodeHandle *n_;
    std::string directory_,
                models_dir_,
                recognition_structure_dir_,
                object_name_;
    bool visualize_;

public:
    bool callDOL()
    {
        //read files from directory
        std::string so_far = "";

        std::vector<std::string> keyframes_str;
        std::vector<std::string> object_indices_str;
        std::vector<std::string> poses_str;

        {
            std::string pattern = ".*.pcd";
            v4r::common::io::getFilesInDirectory(directory_, keyframes_str, so_far, pattern, false);
        }

        {
            std::string pattern = ".*object_indices.*.pcd";
            v4r::common::io::getFilesInDirectory(directory_, object_indices_str, so_far, pattern, false);
        }

        {
            std::string pattern = ".*pose.*.txt";
            v4r::common::io::getFilesInDirectory(directory_, poses_str, so_far, pattern, false);
        }

        std::sort(keyframes_str.begin(), keyframes_str.end());
        std::sort(poses_str.begin(), poses_str.end());
        std::sort(object_indices_str.begin(), object_indices_str.end());

        std::string service_name_learn = "/dynamic_object_learning/learn_object";
        ros::ServiceClient DOLclient = n_->serviceClient<do_learning_srv_definitions::learn_object>(service_name_learn);
        do_learning_srv_definitions::learn_object srv_learn;

        std::string service_name_save = "/dynamic_object_learning/save_model";
        ros::ServiceClient DOLclient_save = n_->serviceClient<do_learning_srv_definitions::save_model>(service_name_save);
        do_learning_srv_definitions::save_model srv_save;

        std::string service_name_vis = "/dynamic_object_learning/visualize";
        ros::ServiceClient DOLclient_vis = n_->serviceClient<do_learning_srv_definitions::visualize>(service_name_vis);
        do_learning_srv_definitions::visualize srv_vis;

        std::ifstream initial_mask_file;
        initial_mask_file.open(directory_ + "/mask.txt");

        int idx_tmp;
        pcl::PointIndices pind;
        while (initial_mask_file >> idx_tmp)
        {
            srv_learn.request.intial_object_indices.push_back(idx_tmp);
            pind.indices.push_back(idx_tmp);
        }
        initial_mask_file.close();

        for(size_t i=0; i < keyframes_str.size(); i++)
        {
            pcl::PointCloud<PointT>::Ptr pCloud(new pcl::PointCloud<PointT>());
            std::stringstream str;
            str << directory_ << "/" << keyframes_str[i];
            std::cout << str.str() << std::endl;
            pcl::io::loadPCDFile(str.str(), *pCloud);

            sensor_msgs::PointCloud2 msg_cloud;
            pcl::toROSMsg(*pCloud, msg_cloud);

            srv_learn.request.keyframes.push_back(msg_cloud);

            Eigen::Matrix4f trans;
            geometry_msgs::Transform tt;
            tt.translation.x = pCloud->sensor_origin_[0];
            tt.translation.y = pCloud->sensor_origin_[1];
            tt.translation.z = pCloud->sensor_origin_[2];

            Eigen::Matrix3f rotation = trans.block<3,3>(0,0);
            Eigen::Quaternionf q(rotation);
            tt.rotation.x = pCloud->sensor_orientation_.x();
            tt.rotation.y = pCloud->sensor_orientation_.y();
            tt.rotation.z = pCloud->sensor_orientation_.z();
            tt.rotation.w = pCloud->sensor_orientation_.w();
            srv_learn.request.transforms.push_back(tt);
        }

        if ( ! DOLclient.call(srv_learn) )
        {
            std::stringstream mm;
            mm << "Error calling service: " << service_name_learn << std::endl;
            std::cerr << mm.str() << std::endl;
            return false;
        }

        // Saving model
        srv_save.request.object_name.data = object_name_;
        srv_save.request.models_folder.data = models_dir_;
        srv_save.request.recognition_structure_folder.data = recognition_structure_dir_;

        if ( ! DOLclient_save.call(srv_save) )
        {
            std::stringstream mm;
            mm << "Error calling service: " << service_name_save << std::endl;
            std::cerr << mm << std::endl;
            return false;
        }

        if (visualize_)
        {
            if ( ! DOLclient_vis.call ( srv_vis ) )
            {
                std::stringstream mm;
                mm << "Error calling service: " << service_name_vis << std::endl;
                std::cerr << mm.str() << std::endl;
            }
        }

        std::string service_name_clear = "/dynamic_object_learning/clear_cached_model";
        ros::ServiceClient DOL_clear_client = n_->serviceClient<do_learning_srv_definitions::clear>(service_name_clear);
        do_learning_srv_definitions::clear srv_clear;

        if ( ! DOL_clear_client.call ( srv_clear ) )
        {
            std::stringstream mm;
            mm << "Error calling service: " << service_name_clear << std::endl;
            std::cerr << mm.str() << std::endl;
        }
        return true;
    }

public:
    DOLDemoFromFiles()
    {
        visualize_ = true;
    }

    bool initialize(int argc, char ** argv)
    {
        ros::init (argc, argv, "DOLDemoFromFiles");
        if (sizeof(int) != 4)
        {
            ROS_WARN("PC Architectur does not use 32bit for integer - check conflicts with pcl indices.");
        }
        n_ = new ros::NodeHandle ( "~" );

        n_->getParam("visualize", visualize_);

        if(!n_->getParam ( "directory", directory_ ))
        {
            //directory_ = "/media/aitor14/DATA/STRANDS_MODELS/recognition_structure/playstation_turn_table.pcd/";
            ROS_ERROR("Specify a directory using param directory.\n");
            return false;
        }

        if(!n_->getParam( "models_dir", models_dir_) )
        {
            ROS_ERROR("Specify a model directory using param models_dir.\n");
            return false;
        }

        if(!n_->getParam( "recognition_dir", recognition_structure_dir_) )
        {
            ROS_ERROR("Specify a model directory using param recognition_dir.\n");
            exit(-1);
        }

        if(!n_->getParam( "object_name", object_name_) )
        {
            object_name_ = "my_dynamic_object";
        }
        return true;
    }
};

int
main (int argc, char ** argv)
{
    DOLDemoFromFiles m;
    if (m.initialize(argc, argv))
        m.callDOL();
    else
        return -1;
    return 0;
}
{'fraction_non_alphanumeric': 0.12431548254035986, 'fraction_numerical': 0.00440935922053908, 'mean_word_length': 2.4541881601572095, 'pattern_counts': {'":': 0, '<': 139, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3743726', 'n_tokens_mistral': 4465, 'n_tokens_neox': 4319, 'n_words': 703}