text:
/**
BSD 3-Clause License

This file is part of the code accompanying the paper
Gradient-SDF: A Semi-Implicit Surface Representation for 3D Reconstruction
by Christiane Sommer*, Lu Sang*, David Schubert, and Daniel Cremers (* denotes equal contribution).

Copyright (c) 2021, Christiane Sommer and Lu Sang.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// standard includes
#include <iostream>
#include <fstream>
#include <vector>
// library includes
#include <cstdlib>
#include <Eigen/Dense>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <CLI/CLI.hpp>
// class includes
#include "Timer.h"
#include "normals/NormalEstimator.h"
#include "sdf_tracker/MapGradPixelSdf.h"
#include "sdf_tracker/RigidPointOptimizer.h"
#include "img_loader/img_loader.h"
#include "ps_optimizer/PhotometricOptimizer.h"
#include "ps_optimizer/ColorUpsampler.h"
#include "ps_optimizer/SharpDetector.h"
// own includes
#include "mat.h"

void sampleKeyFrame(std::vector<int>& key_frames, std::vector<std::string>& key_stamps, std::vector<std::shared_ptr<cv::Mat>>& key_images, std::vector<Mat4f, Eigen::aligned_allocator<Mat4f>>& key_poses, int max_num);
/**
 * main function
 */
int main(int argc, char *argv[]) {

    Timer T;
    // Default input sequence in folder
    std::string input = "";
    std::string output = "../results/";
    std::string stype = "map-gp";
    std::string dtype = "";
    size_t first = 0;
    size_t last = 300;
    float voxel_size = 0.01; // Voxel size in m
    float z_max = 3.5; // maximal depth to take into account in m
    float truncation_factor = 5; // truncation in voxels
    float sharp_threshold = 0.0001;
    int num_frame = 30;

    CLI::App app{"Hash Table-Based 3D Scanning and Texture Optimization"};
    app.add_option("--input", input, "folder of input sequence");
    app.add_option("--results", output, "folder to store results in");
    app.add_option("--first", first, "number of first frame to be processed (default: 0)");
    app.add_option("--last", last, "number of last frame (default: all)");
    app.add_option("--data-type", dtype, "type of dataset");
    app.add_option("--voxel-size", voxel_size, "voxel size in meters (default: 0.01)");
    app.add_option("--trunc", truncation_factor, "truncation in multiples of voxels (default: 5)");
    app.add_option("--key-frame", num_frame, "number of the key frames to be sampled (default: 20)");

    // parse input arguments
    try {
        app.parse(argc, argv);
    } catch (const CLI::ParseError& e) {
        return app.exit(e);
    }

    // parse dataset type
    enum class DataType {
        TUM_RGBD,
        SYNTH,
        PRINTED_3D,
        REDWOOD,
    };
    DataType DT;
    if (dtype == "tum" || dtype == "tumrgbd") {
        DT = DataType::TUM_RGBD;
        sharp_threshold = 0.026;
    }
    else if (dtype == "synth") {
        DT = DataType::SYNTH;
    }
    else if (dtype == "printed") {
        DT = DataType::PRINTED_3D;
        sharp_threshold = 0.026;
    }
    else if (dtype == "rw" || dtype == "redwood") {
        DT = DataType::REDWOOD;
        sharp_threshold = 0.033;
    }
    else {
        std::cerr << "Your specified dataset type is not supported (yet)." << std::endl;
        return 1;
    }

    // create image loader
    ImageLoader* loader;
    switch (DT) {
    case DataType::TUM_RGBD :
        loader = new TumrgbdLoader(input);
        break;
    case DataType::SYNTH :
        loader = new SynthLoader(input);
        break;
    case DataType::PRINTED_3D :
        loader = new Printed3dLoader(input);
        break;
    case DataType::REDWOOD :
        loader = new RedwoodLoader(input);
        break;
    default:
        std::cerr << "Specified dataset type not recognized, return" << std::endl;
        return 1;
    }

    // Load camera intrinsics
    if (!loader->load_intrinsics("intrinsics.txt")) {
        std::cerr << "No intrinsics file found in " << input << "!" << std::endl;
        return 1;
    }
    const Mat3f K = loader->K();
    std::cout << "K: " << std::endl << K << std::endl;

    // create normal estimator
    T.tic();
    cv::NormalEstimator<float>* NEst;
    NEst = new cv::NormalEstimator<float>(640, 480, K, cv::Size(2*5+1, 2*5+1));
    T.toc("Init normal estimation");

    std::stringstream stream;
    stream << std::fixed << std::setprecision(3) << voxel_size;
    std::string voxel_size_str = stream.str();

    // Trunction distance for the tSDF
    const float truncation = truncation_factor * voxel_size;

    Sdf* tSDF;
    RigidPointOptimizer* pOpt;
    PhotometricOptimizer* cOpt;

    std::ofstream pose_file(output + stype + "_" + voxel_size_str + "m_T" + std::to_string(int(truncation_factor)) + "_poses.txt");

    std::vector<Mat4f, Eigen::aligned_allocator<Mat4f>> poses;
    // poses.push_back(Mat4f::Identity());
    std::vector<int> valid_frames;
    std::vector<int> invalid_frames;
    std::vector<int> keyframes;
    valid_frames.push_back(0);
    keyframes.push_back(0);
    // vector of sampled poses and frames
    std::vector<std::string> key_stamps;
    std::vector<std::shared_ptr<cv::Mat>> key_images;
    std::vector<Mat4f, Eigen::aligned_allocator<Mat4f>> key_poses;
    key_poses.push_back(Mat4f::Identity());

    // Frames to be processed
    cv::Mat color, depth;
    int dist_to_last_keyframe = 0;

    // Proceed until first frame
    for (size_t i = 0; i < first; ++i) {
        loader->load_next(color, depth);
    }

    // Actual scanning loop
    for (size_t i = first; i <= last; ++i) {
        std::cout << "Working on frame: " << i << std::endl;

        // Load data
        T.tic();
        if (!loader->load_next(color, depth)) {
            std::cerr << " -> Frame " << i << " could not be loaded!" << std::endl;
            T.toc("Load data");
            break;
        }
        T.toc("Load data");

        // Get initial volume pose from centroid of first depth map
        if (i == first) {
            // create SDF data
            T.tic();
            tSDF = new MapGradPixelSdf(voxel_size, truncation);
            T.toc("Create Sdf");
            // Initialize tSDF
            T.tic();
            tSDF->setup(color, depth, K, NEst);
            T.toc("Integrate depth data into Sdf");

			// Initialize optimizers
			T.tic();
			pOpt = new RigidPointOptimizer(tSDF);
			T.toc("Create RigidOptimizer");
            T.tic();
            cOpt = new PhotometricOptimizer(static_cast<MapGradPixelSdf*>(tSDF), voxel_size, K, output);
            T.toc("Create PhotometricOptimizer");
            key_stamps.push_back(loader->rgb_timestamp());
            cv::Mat new_color;
            color.copyTo(new_color);
            key_images.push_back(std::make_shared<cv::Mat>(new_color));

        }
		else {
            // Perform optimization
            T.tic();
            bool conv = pOpt->optimize(depth, K);
            T.toc("Point optimization");
            // Integrate data into model
            if (conv) {
                valid_frames.push_back(i-first);
                T.tic();
                tSDF->update(color, depth, K, pOpt->pose(), NEst);
                T.toc("Integrate depth data into Sdf");

                if (sharpDetector(color, sharp_threshold) || dist_to_last_keyframe > 5)
                {
                    dist_to_last_keyframe = 0;
                    keyframes.push_back(i-first);
                    key_stamps.push_back(loader->rgb_timestamp());
                    key_poses.push_back(pOpt->pose().matrix());
                    cv::Mat new_color;
                    color.copyTo(new_color);
                    key_images.push_back(std::make_shared<cv::Mat>(new_color));
                }
                else{

                    dist_to_last_keyframe++;
                }
            }
            else {
                invalid_frames.push_back(i-first);
            }
		}
		// write timestamp + pose in tx ty tz qx qy qz qw format
		std::cout << "Current pose:" << std::endl
		          << pOpt->pose().matrix() << std::endl;
        poses.push_back(pOpt->pose().matrix());
		Vec3f t(pOpt->pose().translation());
		Eigen::Quaternion<float> q(pOpt->pose().rotationMatrix());
		pose_file << loader->depth_timestamp() << " "
		          << t[0] << " " << t[1] << " " << t[2] << " "
		          << q.x() << " " << q.y() << " " << q.z() << " " << q.w() << "\n";
    }
    pose_file.close();

    // extract mesh and write to file
    T.tic();
    if (!tSDF->extract_mesh(output + "mesh_lr.ply")) {
        std::cerr << "Could not save mesh!" << std::endl;
    }
    T.toc("Save mesh to disk");

    // extract point cloud and write to file
    T.tic();
    if (!tSDF->extract_pc(output + "cloud_lr.ply")) {
        std::cerr << "Could not save point cloud!" << std::endl;
    }
    T.toc("Save point cloud to disk");

    sampleKeyFrame(keyframes, key_stamps, key_images, key_poses, num_frame);

    cOpt->setImages(key_images);
    cOpt->setKeyframes(keyframes);
    cOpt->setPoses(key_poses);
    cOpt->setKeytimestamps(key_stamps);
    cOpt->optimize();

    // up sampling
    ColorUpsampler tOpt((static_cast<MapGradPixelSdf*>(tSDF))->get_tsdf(),
                    (static_cast<MapGradPixelSdf*>(tSDF))->get_vis(),
                    key_images,
                    key_poses,
                    keyframes,
                    voxel_size,
                    K);

    std::cout << "Color optimizer constructed" << std::endl;
    tOpt.computeColor(); // just to be able to compare better
    tOpt.extractMesh(output + "coarse_BA_mesh_after_upsample");
    tOpt.extractCloud(output + "coarse_BA_cloud_after_upsample");

    return 0;
}

//! To selected limited number of frames if there are too many input as key frames.
void sampleKeyFrame(std::vector<int>& key_frames, std::vector<std::string>& key_stamps, std::vector<std::shared_ptr<cv::Mat>>& key_images, std::vector<Mat4f, Eigen::aligned_allocator<Mat4f>>& key_poses, int max_num){
    if (key_frames.size() < max_num ){
        return;
    }
    max_num -= 1;
    float step = static_cast<float>(key_frames.size()) / static_cast<float>(max_num);
    std::vector<int> frames;
    std::vector<std::string> stamps;
    std::vector<std::shared_ptr<cv::Mat>> images;
    std::vector<Mat4f, Eigen::aligned_allocator<Mat4f>> poses;
    float idx = 0;
    for(int count = 0; count < max_num; count++){
        int i = static_cast<int>(idx);
        frames.push_back(key_frames[i]);
        stamps.push_back(key_stamps[i]);
        images.push_back(key_images[i]);
        poses.push_back(key_poses[i]);
        idx+=step;
    }
    frames.push_back(key_frames.back()); //we need the last frame for resize the visibility vector
    stamps.push_back(key_stamps.back());
    images.push_back(key_images.back());
    poses.push_back(key_poses.back());

    key_frames = frames;
    key_stamps = stamps;
    key_poses = poses;
    key_images = images;
}

max_stars_repo_name: c-sommer/gradient-sdf

max_issues_repo_name: c-sommer/gradient-sdf

max_forks_repo_name: c-sommer/gradient-sdf


text:
#ifndef BOOST_QVM_GEN_MAT_ASSIGN4_HPP_INCLUDED
#define BOOST_QVM_GEN_MAT_ASSIGN4_HPP_INCLUDED

// Copyright 2008-2022 Emil Dotchevski and Reverge Studios, Inc.

// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// This file was generated by a program. Do not edit manually.

#include <boost/qvm/config.hpp>
#include <boost/qvm/enable_if.hpp>
#include <boost/qvm/mat_traits.hpp>

namespace boost { namespace qvm {

template <class A,class B>
BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
typename enable_if_c<
    mat_traits<A>::rows==4 && mat_traits<B>::rows==4 &&
    mat_traits<A>::cols==4 && mat_traits<B>::cols==4,
    A &>::type
assign( A & a, B const & b )
    {
    write_mat_element<0,0>(a,mat_traits<B>::template read_element<0,0>(b));
    write_mat_element<0,1>(a,mat_traits<B>::template read_element<0,1>(b));
    write_mat_element<0,2>(a,mat_traits<B>::template read_element<0,2>(b));
    write_mat_element<0,3>(a,mat_traits<B>::template read_element<0,3>(b));
    write_mat_element<1,0>(a,mat_traits<B>::template read_element<1,0>(b));
    write_mat_element<1,1>(a,mat_traits<B>::template read_element<1,1>(b));
    write_mat_element<1,2>(a,mat_traits<B>::template read_element<1,2>(b));
    write_mat_element<1,3>(a,mat_traits<B>::template read_element<1,3>(b));
    write_mat_element<2,0>(a,mat_traits<B>::template read_element<2,0>(b));
    write_mat_element<2,1>(a,mat_traits<B>::template read_element<2,1>(b));
    write_mat_element<2,2>(a,mat_traits<B>::template read_element<2,2>(b));
    write_mat_element<2,3>(a,mat_traits<B>::template read_element<2,3>(b));
    write_mat_element<3,0>(a,mat_traits<B>::template read_element<3,0>(b));
    write_mat_element<3,1>(a,mat_traits<B>::template read_element<3,1>(b));
    write_mat_element<3,2>(a,mat_traits<B>::template read_element<3,2>(b));
    write_mat_element<3,3>(a,mat_traits<B>::template read_element<3,3>(b));
    return a;
    }

namespace
sfinae
    {
    using ::boost::qvm::assign;
    }

namespace
qvm_detail
    {
    template <int R,int C>
    struct assign_mm_defined;

    template <>
    struct
    assign_mm_defined<4,4>
        {
        static bool const value=true;
        };
    }

template <class A,class B>
BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
typename enable_if_c<
    mat_traits<A>::rows==4 && mat_traits<B>::rows==4 &&
    mat_traits<A>::cols==1 && mat_traits<B>::cols==1,
    A &>::type
assign( A & a, B const & b )
    {
    write_mat_element<0,0>(a,mat_traits<B>::template read_element<0,0>(b));
    write_mat_element<1,0>(a,mat_traits<B>::template read_element<1,0>(b));
    write_mat_element<2,0>(a,mat_traits<B>::template read_element<2,0>(b));
    write_mat_element<3,0>(a,mat_traits<B>::template read_element<3,0>(b));
    return a;
    }

namespace
sfinae
    {
    using ::boost::qvm::assign;
    }

namespace
qvm_detail
    {
    template <int R,int C>
    struct assign_mm_defined;

    template <>
    struct
    assign_mm_defined<4,1>
        {
        static bool const value=true;
        };
    }

template <class A,class B>
BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_OPERATIONS
typename enable_if_c<
    mat_traits<A>::rows==1 && mat_traits<B>::rows==1 &&
    mat_traits<A>::cols==4 && mat_traits<B>::cols==4,
    A &>::type
assign( A & a, B const & b )
    {
    write_mat_element<0,0>(a,mat_traits<B>::template read_element<0,0>(b));
    write_mat_element<0,1>(a,mat_traits<B>::template read_element<0,1>(b));
    write_mat_element<0,2>(a,mat_traits<B>::template read_element<0,2>(b));
    write_mat_element<0,3>(a,mat_traits<B>::template read_element<0,3>(b));
    return a;
    }

namespace
sfinae
    {
    using ::boost::qvm::assign;
    }

namespace
qvm_detail
    {
    template <int R,int C>
    struct assign_mm_defined;

    template <>
    struct
    assign_mm_defined<1,4>
        {
        static bool const value=true;
        };
    }

} }

#endif

ext: hpp

max_stars_repo_name: boostorg/boost-qvm

max_issues_repo_name: boostorg/boost-qvm

max_forks_repo_name: boostorg/boost-qvm


text:
//  (C) Copyright Edward Diener 2011-2015
//  Use, modification and distribution are subject to the Boost Software License,
//  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt).

#include <boost/vmd/equal.hpp>
#include <boost/detail/lightweight_test.hpp>

int main()
  {

#if BOOST_PP_VARIADICS

 #define BOOST_VMD_REGISTER_AN_ID1 (AN_ID1)
 #define BOOST_VMD_REGISTER_AN_ID2 (AN_ID2)

 #define BOOST_VMD_DETECT_AN_ID1_AN_ID1
 #define BOOST_VMD_DETECT_AN_ID2_AN_ID2

 #define AN_IDENTIFIER1 AN_ID1
 #define AN_IDENTIFIER2 AN_ID2
 #define AN_IDENTIFIER3 AN_ID1 // same as AN_IDENTIFIER1 = AN_ID1

 #define A_NUMBER1 33
 #define A_NUMBER2 145
 #define A_NUMBER3 33 // same as A_NUMBER1 = 33

 #define A_TUPLE1 (AN_IDENTIFIER1,A_NUMBER1)
 #define A_TUPLE2 (AN_IDENTIFIER1,A_NUMBER2)
 #define A_TUPLE3 (AN_IDENTIFIER3,A_NUMBER3) // same as A_TUPLE1 = (AN_ID1,33)

 #define A_SEQ1 (A_NUMBER1)(A_TUPLE1)
 #define A_SEQ2 (A_NUMBER2)(A_TUPLE2)
 #define A_SEQ3 (A_NUMBER3)(A_TUPLE3) // same as A_SEQ1 = (33)((AN_ID1,33))

 BOOST_TEST(!BOOST_VMD_EQUAL(AN_IDENTIFIER1,AN_IDENTIFIER2));
 BOOST_TEST(BOOST_VMD_EQUAL(AN_IDENTIFIER1,AN_IDENTIFIER3));
 BOOST_TEST(!BOOST_VMD_EQUAL(A_NUMBER1,A_NUMBER2));
 BOOST_TEST(BOOST_VMD_EQUAL(A_NUMBER1,A_NUMBER3));
 BOOST_TEST(!BOOST_VMD_EQUAL(A_TUPLE1,A_TUPLE2));
 BOOST_TEST(BOOST_VMD_EQUAL(A_TUPLE1,A_TUPLE3));
 BOOST_TEST(!BOOST_VMD_EQUAL(A_SEQ1,A_SEQ2));
 BOOST_TEST(BOOST_VMD_EQUAL(A_SEQ1,A_SEQ3));

 #define TUPLE_IS_ARRAY1 (2,(3,4))
 #define TUPLE_IS_ARRAY2 (2,(4,5))
 #define TUPLE_IS_ARRAY3 (2,(3,4))

 #define TUPLE_IS_LIST1 (55,BOOST_PP_NIL)
 #define TUPLE_IS_LIST2 (135,BOOST_PP_NIL)
 #define TUPLE_IS_LIST3 (55,BOOST_PP_NIL)

 #define TUPLE_IS_LIST_OR_ARRAY1 (2,(3,BOOST_PP_NIL))
 #define TUPLE_IS_LIST_OR_ARRAY2 (2,(4,BOOST_PP_NIL))
 #define TUPLE_IS_LIST_OR_ARRAY3 (2,(3,BOOST_PP_NIL))

 BOOST_TEST(!BOOST_VMD_EQUAL(TUPLE_IS_ARRAY1,TUPLE_IS_ARRAY2));
 BOOST_TEST(BOOST_VMD_EQUAL(TUPLE_IS_ARRAY1,TUPLE_IS_ARRAY3));
 BOOST_TEST(!BOOST_VMD_EQUAL(TUPLE_IS_LIST1,TUPLE_IS_LIST2));
 BOOST_TEST(BOOST_VMD_EQUAL(TUPLE_IS_LIST1,TUPLE_IS_LIST3));
 BOOST_TEST(!BOOST_VMD_EQUAL(TUPLE_IS_LIST_OR_ARRAY1,TUPLE_IS_LIST_OR_ARRAY2));
 BOOST_TEST(BOOST_VMD_EQUAL(TUPLE_IS_LIST_OR_ARRAY1,TUPLE_IS_LIST_OR_ARRAY3));

 #define A_TUPLE_1 (3,4,"astring")
 #define A_TUPLE_2 (3,4)

 BOOST_TEST(!BOOST_VMD_EQUAL(A_TUPLE_1,A_TUPLE_2));

#else

BOOST_ERROR("No variadic macro support");

#endif

  return boost::report_errors();

  }

ext: cxx


text:
// Copyright 2019 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

//[ guide_fill_weighted_profile

#include <boost/format.hpp>
#include <boost/histogram.hpp>
#include <cassert>
#include <iostream>
#include <sstream>

int main() {
  using namespace boost::histogram;
  using namespace boost::histogram::literals; // _c suffix creates compile-time numbers

  // make 2D weighted profile
  auto h = make_weighted_profile(axis::integer<>(0, 2), axis::integer<>(0, 2));

  // The mean is computed from the values marked with the sample() helper function.
  // Weights can be passed as well. The `sample` and `weight` arguments can appear in any
  // order, but they must be the first or last arguments.
  h(0, 0, sample(1));            // sample goes to cell (0, 0); weight is 1
  h(0, 0, sample(2), weight(3)); // sample goes to cell (0, 0); weight is 3
  h(1, 0, sample(3));            // sample goes to cell (1, 0); weight is 1
  h(1, 0, sample(4));            // sample goes to cell (1, 0); weight is 1
  h(0, 1, sample(5));            // sample goes to cell (1, 0); weight is 1
  h(0, 1, sample(6));            // sample goes to cell (1, 0); weight is 1
  h(1, 1, weight(4), sample(7)); // sample goes to cell (1, 1); weight is 4
  h(weight(5), sample(8), 1, 1); // sample goes to cell (1, 1); weight is 5

  std::ostringstream os;
  for (auto&& x : indexed(h)) {
    const auto i = x.index(0_c);
    const auto j = x.index(1_c);
    const auto m = x->value();    // weighted mean
    const auto v = x->variance(); // estimated variance of weighted mean
    os << boost::format("index %i,%i mean %.1f variance %.1f\n") % i % j % m % v;
  }

  std::cout << os.str() << std::flush;

  assert(os.str() == "index 0,0 mean 1.8 variance 0.5\n"
                     "index 1,0 mean 3.5 variance 0.5\n"
                     "index 0,1 mean 5.5 variance 0.5\n"
                     "index 1,1 mean 7.6 variance 0.5\n");
}

//]

ext: cpp

max_stars_repo_name: cpp-pm/boost

max_issues_repo_name: cpp-pm/boost

max_forks_repo_name: cpp-pm/boost


text:
#include <fstream>
#include <streambuf>
#include <unistd.h>
#include <libvirt/libvirt.h>
#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid_io.hpp>
#include "testing/gunit.h"
#include "oper/libvirt_instance_adapter.h"
#include "oper/service_instance.h"
#include "oper/instance_task.h"
#include <boost/uuid/uuid.hpp>
#include <boost/lexical_cast.hpp>

/*
 *  ! Does not test networking.
 *  Those tests creates (and destroys) virtual machine using libvirt api.
 *  Developer is responsible for:
 *  a) providing .iso image conforming to data/libvirt_domain.xml
 *  b) installing libvirt service
 *  c) running test "by hand". Test is not run automatically using simple "scons test"
 *  vrouter-agent must be compiled with --with-libvirt option.
 */

class Agent;

class LibvirtAdapterTest : public ::testing::Test {
 protected:

  virtual void SetUp() {
      agent_ =  new Agent;
      libvirt_adapter_ = new LibvirtInstanceAdapter(agent_, "qemu:///system");

      properties_.virtualization_type = ServiceInstance::VRouterInstance;
      properties_.vrouter_instance_type = ServiceInstance::KVM;
      properties_.instance_id =
        boost::lexical_cast<boost::uuids::uuid>("ea294ce2-8182-11e4-ae5f-c32f93853088");

      properties_.vmi_inside =
        boost::lexical_cast<boost::uuids::uuid>("ea9b8b18-8182-11e4-8ad5-47899f2d45f1");
      properties_.vmi_outside =
        boost::lexical_cast<boost::uuids::uuid>("eb0e2d30-8182-11e4-b09d-533359975d6a");
      properties_.vmi_management =
        boost::lexical_cast<boost::uuids::uuid>("324d6de6-8183-11e4-8a75-13b524544c2f");

      properties_.mac_addr_inside = "00:50:56:00:00:01";
      properties_.mac_addr_outside = "00:50:56:00:00:02";
      properties_.mac_addr_management = "00:50:56:00:00:03";

      properties_.ip_addr_inside = "10.0.0.1";
      properties_.ip_addr_outside = "10.0.1.1";
      properties_.ip_addr_management = "10.0.2.1";

      properties_.image_name = "test";

      properties_.ip_prefix_len_inside = 24;
      properties_.ip_prefix_len_outside = 24;
      properties_.ip_prefix_len_management = 24;

      std::ifstream data("data/libvirt_domain.xml");
      properties_.instance_data = std::string((std::istreambuf_iterator<char>(data)),
                                              std::istreambuf_iterator<char>());
  }

  virtual void TearDown() {
      delete libvirt_adapter_;
      delete agent_;
  }

  ServiceInstance::Properties properties_;
  LibvirtInstanceAdapter *libvirt_adapter_;
  Agent* agent_;
};

TEST_F(LibvirtAdapterTest, DomainConfigFileExists) {
    ASSERT_NE(properties_.instance_data.size(), 0);
}

TEST_F(LibvirtAdapterTest, LibvirtConnection) {
    virConnectPtr conn = virConnectOpen("qemu:///system");
    ASSERT_NE(conn, (virConnectPtr)(NULL));
    virConnectClose(conn);
}

TEST_F(LibvirtAdapterTest, IsApplicable) {
    ASSERT_TRUE(libvirt_adapter_->isApplicable(properties_));
}

TEST_F(LibvirtAdapterTest, LibvirtStart) {
    InstanceTask* task = libvirt_adapter_->CreateStartTask(properties_ , false);
    ASSERT_NE(task, (InstanceTask *)(NULL));
    ASSERT_EQ(task->Run(), true);
    delete task;
}

TEST_F(LibvirtAdapterTest, LibvirtDomainCreated) {
    virConnectPtr conn = virConnectOpen("qemu:///system");
    ASSERT_NE(conn, (virConnectPtr)(NULL));
    std::string dom_uuid_str =
        boost::lexical_cast<std::string>(properties_.instance_id);
    virDomainPtr dom;
    for (int i = 0; i < 20; i++) {
        dom = virDomainLookupByUUIDString(conn,
            dom_uuid_str.c_str());
        if (dom != NULL)
            break;
        usleep(0.5 * 1e6);
    }
    ASSERT_NE(dom, (virDomainPtr)(NULL));
    virDomainFree(dom);
    virConnectClose(conn);
}

TEST_F(LibvirtAdapterTest, LibvirtStop) {
    InstanceTask* task = libvirt_adapter_->CreateStopTask(properties_);
    ASSERT_NE(task, (InstanceTask *)(NULL));
    ASSERT_EQ(task->Run(), true);
    delete task;
}

TEST_F(LibvirtAdapterTest, LibvirtDomainDestroyed) {
    virConnectPtr conn = virConnectOpen("qemu:///system");
    ASSERT_NE(conn, (virConnectPtr)(NULL));
    std::string dom_uuid_str =
        boost::lexical_cast<std::string>(properties_.instance_id);
    virDomainPtr dom;
    for (int i = 0; i < 20; i++) {
        dom = virDomainLookupByUUIDString(conn,
            dom_uuid_str.c_str());
        if (dom == NULL)
            break;
        usleep(0.5 * 1e6);
    }
    ASSERT_EQ(dom, (virDomainPtr)(NULL));
    virConnectClose(conn);
}

int main(int argc, char **argv) {
    LoggingInit();
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

ext: cc

max_stars_repo_name: jnpr-pranav/contrail-controller

max_issues_repo_name: jnpr-pranav/contrail-controller

max_forks_repo_name: jnpr-pranav/contrail-controller


text:
#include "chunk/ChunkUtils.hpp"

#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include <boost/range/iterator_range.hpp>
#include <limits>

#include "chunk/XORChunk.hpp"

namespace tsdb {
namespace chunk {

const uint32_t MAGIC_CHUNK = 0x51705259;
const int DEFAULT_CHUNK_SIZE = 512 * 1024 * 1024;
const int CHUNK_FORMAT_V1 = 1;
const uint8_t DEFAULT_MEDIAN_SEGMENT = 2;
const uint8_t DEFAULT_TUPLE_SIZE = 8;

bool is_number(const std::string &s) {
  std::string::const_iterator it = s.begin();
  while (it != s.end() && std::isdigit(*it)) ++it;
  return !s.empty() && it == s.end();
}

std::pair<int, std::string> next_sequence_file(const std::string &dir) {
  // ATTENTION leave one end byte for \0
  int max = -1;
  boost::filesystem::path p(dir);
  for (auto &entry : boost::make_iterator_range(
           boost::filesystem::directory_iterator(p), {})) {
    int temp2 = std::stoi(entry.path().filename().string());
    if (temp2 > max) max = temp2;
  }
  boost::format fmt("%06d");
  fmt % (max + 1);
  return {max + 1, (p / boost::filesystem::path(fmt.str())).string()};
}

// Will get the path including the dir name
std::deque<std::string> sequence_files(const std::string &dir) {
  std::deque<std::string> r;
  boost::filesystem::path p(dir);
  if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
    return r;
  for (auto const &entry : boost::make_iterator_range(
           boost::filesystem::directory_iterator(p), {})) {
    if (entry.path().filename().string().length() >= 6 &&
        is_number(entry.path().filename().string().substr(
            entry.path().filename().string().length() - 6, 6)))
      r.push_back(entry.path().string());
  }
  std::sort(r.begin(), r.end());
  return r;
}

// merge_chunks vertically merges a and b, i.e., if there is any sample
// with same timestamp in both a and b, the sample in a is discarded.
std::pair<std::unique_ptr<chunk::ChunkInterface>, error::Error> merge_chunks(
    chunk::ChunkInterface *c1, chunk::ChunkInterface *c2) {
  std::unique_ptr<chunk::ChunkInterface> new_chunk =
      std::unique_ptr<chunk::XORChunk>(new chunk::XORChunk());
  std::unique_ptr<chunk::ChunkAppenderInterface> app;
  try {
    app = new_chunk->appender();
  } catch (const base::TSDBException &e) {
    return {std::unique_ptr<chunk::ChunkInterface>(), error::Error(e.what())};
  }

  std::unique_ptr<ChunkIteratorInterface> it1 = c1->iterator();
  std::unique_ptr<ChunkIteratorInterface> it2 = c2->iterator();
  bool ok1 = it1->next();
  bool ok2 = it2->next();
  while (ok1 && ok2) {
    std::pair<int64_t, double> p1 = it1->at();
    std::pair<int64_t, double> p2 = it2->at();
    if (p1.first < p2.first) {
      app->append(p1.first, p1.second);
      ok1 = it1->next();
    } else if (p1.first > p2.first) {
      app->append(p2.first, p2.second);
      ok2 = it2->next();
    } else {
      app->append(p2.first, p2.second);
      ok1 = it1->next();
      ok2 = it2->next();
    }
  }

  while (ok1) {
    std::pair<int64_t, double> p1 = it1->at();
    app->append(p1.first, p1.second);
    ok1 = it1->next();
  }
  while (ok2) {
    std::pair<int64_t, double> p2 = it2->at();
    app->append(p2.first, p2.second);
    ok2 = it2->next();
  }

  if (it1->error())
    return {std::unique_ptr<chunk::ChunkInterface>(),
            error::Error("Error in first chunk iterator")};
  if (it2->error())
    return {std::unique_ptr<chunk::ChunkInterface>(),
            error::Error("Error in second chunk iterator")};
  return {std::move(new_chunk), error::Error()};
}

// merge_overlapping_chunks removes the samples whose timestamp is overlapping.
// The last appearing sample is retained in case there is overlapping.
// This assumes that `chunks` are sorted w.r.t. min_time.
std::pair<std::deque<std::shared_ptr<ChunkMeta>>, error::Error>
merge_overlapping_chunks(const std::deque<std::shared_ptr<ChunkMeta>> &chunks) {
  if (chunks.size() < 2) return {chunks, error::Error()};

  std::deque<std::shared_ptr<ChunkMeta>> new_chunks;
  new_chunks.push_back(chunks[0]);
  int last = 0;
  for (int i = 1; i < chunks.size(); i++) {
    // We need to check only the last chunk in newChks.
    // Reason: (1) newChks[last-1].MaxTime < newChks[last].MinTime (non
    // overlapping).
    //         (2) As chks are sorted w.r.t. MinTime, newChks[last].MinTime <
    //         c.MinTime.
    // So never overlaps with newChks[last-1] or anything before that.
    if (chunks[i]->min_time > new_chunks[last]->max_time) {
      new_chunks.push_back(chunks[i]);
      ++last;
      continue;
    }
    if (new_chunks[last]->max_time < chunks[i]->max_time)
      new_chunks[last]->max_time = chunks[i]->max_time;

    std::pair<std::unique_ptr<chunk::ChunkInterface>, error::Error> chk =
        merge_chunks(new_chunks[last]->chunk.get(), chunks[i]->chunk.get());
    if (chk.second)
      return {std::deque<std::shared_ptr<ChunkMeta>>(), chk.second};
    new_chunks[last]->chunk = std::move(chk.first);
  }

  return {new_chunks, error::Error()};
}

}  // namespace chunk
}  // namespace tsdb

max_stars_repo_name: naivewong/timeunion

max_issues_repo_name: naivewong/timeunion

max_forks_repo_name: naivewong/timeunion


text:
// Copyright (C) 2015 team-diana MIT license

#include "hlcanopen/sdo_data_converter.hpp"
#include "hlcanopen/sdo_entry_converter_visitor.hpp"
#include "hlcanopen/object_dictionary.hpp"
#include "hlcanopen/types.hpp"

#include <boost/variant/static_visitor.hpp>

#include "hlcanopen/logging/easylogging++.h"

namespace hlcanopen {

SdoData SdoEntryConverterVisitor::operator()(uint32_t i) const
{
    return convertValue(i);
}

SdoData SdoEntryConverterVisitor::operator()(int32_t i) const
{
    return convertValue(i);
}

SdoData SdoEntryConverterVisitor::operator()(const std::string& str) const
{
    return convertValue(str);
}

SdoEntryWriteVisitor::SdoEntryWriteVisitor(SdoData data) :
    data(data) {}

ODEntryValue SdoEntryWriteVisitor::operator()(uint32_t)
{
    checkAndFixSize(data, 4);
    return convertSdoData<uint32_t>(data);
}

ODEntryValue SdoEntryWriteVisitor::operator()(int32_t)
{
    checkAndFixSize(data, 4);
    return convertSdoData<int32_t>(data);
}

ODEntryValue SdoEntryWriteVisitor::operator()(const std::string&)
{
    return convertSdoData<std::string>(data);
}

void SdoEntryWriteVisitor::checkAndFixSize(SdoData& d, unsigned int nBytes)
{
    if(d.size() != nBytes) {
        LOG(WARNING) << "resizing sdo data from " << static_cast<unsigned int>(d.size()) << " to " << nBytes;
        d.resize(nBytes);
    }
}

}

max_stars_repo_name: team-diana/hlCANopen

max_issues_repo_name: team-diana/hlCANopen

max_forks_repo_name: team-diana/hlCANopen