text:
/*
 * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
 */

#include <sys/socket.h>
#if defined(__linux__)
#include <linux/netlink.h>
#elif defined(__FreeBSD__)
#include "vr_os.h"
#endif
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <asm/types.h>
#include <boost/asio.hpp>

#include <base/timer.h>
#include <base/task_trigger.h>
#include <net/address_util.h>
#include <cmn/agent_cmn.h>
#include <services/services_init.h>
#include <uve/stats_collector.h>
#include <services/icmp_error_proto.h>
#include <pkt/flow_proto.h>
#include <ksync/ksync_index.h>
#include <ksync/ksync_entry.h>
#include <ksync/ksync_object.h>
#include <ksync/ksync_netlink.h>
#include <ksync/ksync_sock.h>
#include <ksync/ksync_sock_user.h>
#include <vrouter/flow_stats/flow_stats_collector.h>

#include <vr_types.h>
#include <nl_util.h>
#include <vr_flow.h>
#include <vr_genetlink.h>

#include "ksync_init.h"
#include "ksync_flow_memory.h"
#include "sandesh_ksync.h"

using namespace boost::asio::ip;
static const int kTestFlowTableSize = 131072 * sizeof(vr_flow_entry);

KSyncFlowMemory::KSyncFlowMemory(KSync *ksync) :
    ksync_(ksync),
    flow_table_path_(),
    major_devid_(0),
    flow_table_size_(0),
    flow_table_entries_count_(0),
    audit_timer_(TimerManager::CreateTimer
                 (*(ksync->agent()->event_manager())->io_service(),
                  "Flow Audit Timer",
                  ksync->agent()->task_scheduler()->GetTaskId(kTaskFlowAudit),
                  0)),
    audit_timeout_(0),
    audit_yield_(0),
    audit_interval_(0),
    audit_flow_idx_(0),
    audit_flow_list_() {
    hold_flow_counter_ = 0;
}

KSyncFlowMemory::~KSyncFlowMemory() {
    TimerManager::DeleteTimer(audit_timer_);
}

void KSyncFlowMemory::Init() {
    IcmpErrorProto *proto = ksync_->agent()->services()->icmp_error_proto();
    proto->Register(boost::bind(&KSyncFlowMemory::GetFlowKey, this, _1, _2));

    audit_interval_ = kAuditYieldTimer;
    audit_timeout_ = kAuditTimeout;
    uint32_t flow_table_count = ksync_->agent()->flow_table_size();
    // Compute number of entries to visit per timer interval so that complete
    // table can be visited in kAuditSweepTime
    uint32_t timer_per_sec = 1000 / kAuditYieldTimer;
    uint32_t timer_per_sweep = kAuditSweepTime * timer_per_sec;
    audit_yield_ = flow_table_count / timer_per_sweep;
    if (audit_yield_ > kAuditYieldMax)
        audit_yield_ = kAuditYieldMax;
    if (audit_yield_ < kAuditYieldMin)
        audit_yield_ = kAuditYieldMin;

    audit_timer_->Start(audit_interval_,
                        boost::bind(&KSyncFlowMemory::AuditProcess, this));
}

void KSyncFlowMemory::DecrementHoldFlowCounter() {
    hold_flow_counter_--;
    return;
}

void KSyncFlowMemory::IncrementHoldFlowCounter() {
    hold_flow_counter_++;
    return;
}

void KSyncFlowMemory::UpdateAgentHoldFlowCounter() {
    ksync_->agent()->stats()->update_hold_flow_count(hold_flow_counter_);
    hold_flow_counter_ = 0;
    return;
}

// Steps to map flow table entry
// - Query the Flow table parameters from kernel
// - Create device /dev/flow with major-num and minor-num
// - Map device memory
void KSyncFlowMemory::InitFlowMem() {
    struct nl_client *cl;
    vr_flow_table_data info;
    int attr_len;
    int encode_len, error, ret;

    assert((cl = nl_register_client()) != NULL);
    assert(nl_socket(cl, AF_NETLINK, SOCK_DGRAM, NETLINK_GENERIC) > 0);
    assert(nl_connect(cl, 0, 0) == 0);
    assert(vrouter_get_family_id(cl) > 0);

    assert(nl_build_nlh(cl, cl->cl_genl_family_id, NLM_F_REQUEST) == 0);
    assert(nl_build_genlh(cl, SANDESH_REQUEST, 0) == 0);

    attr_len = nl_get_attr_hdr_size();

    info.set_ftable_op(flow_op::FLOW_TABLE_GET);
    info.set_ftable_size(0);
    info.set_ftable_dev(0);
    info.set_ftable_file_path("");
    info.set_ftable_processed(0);
    info.set_ftable_hold_oflows(0);
    info.set_ftable_added(0);
    info.set_ftable_cpus(0);
    info.set_ftable_created(0);
    info.set_ftable_oflow_entries(0);
    encode_len = info.WriteBinary(nl_get_buf_ptr(cl) + attr_len,
                                 nl_get_buf_len(cl), &error);
    nl_build_attr(cl, encode_len, NL_ATTR_VR_MESSAGE_PROTOCOL);
    nl_update_nlh(cl);

    if ((ret = nl_sendmsg(cl)) < 0) {
        LOG(DEBUG, "Error requesting Flow Table message. Error : " << ret);
        assert(0);
    }

    while ((ret = nl_recvmsg(cl)) > 0) {
        KSyncSockNetlink::NetlinkDecoder(cl->cl_buf,
                                         KSyncSock::GetAgentSandeshContext(0));
    }
    nl_free_client(cl);

    // Remove the existing /dev/flow file first. We will add it again below
#if !defined(__FreeBSD__)
    if (unlink("/dev/flow") != 0) {
        if (errno != ENOENT) {
            LOG(DEBUG, "Error deleting </dev/flow>. Error <" << errno
                << "> : " << strerror(errno));
            assert(0);
        }
    }

    assert(flow_table_size_ != 0);
    assert(major_devid_);
    if (mknod("/dev/flow", (S_IFCHR | O_RDWR), makedev(major_devid_, 0)) < 0) {
        if (errno != EEXIST) {
            LOG(DEBUG, "Error creating device </dev/flow>. Error <" << errno
            << "> : " << strerror(errno));
            assert(0);
        }
    }
#endif

    int fd;
    if ((fd = open("/dev/flow", O_RDONLY | O_SYNC)) < 0) {
        LOG(DEBUG, "Error opening device </dev/flow>. Error <" << errno
            << "> : " << strerror(errno));
        assert(0);
    }

    flow_table_ = (vr_flow_entry *)mmap(NULL, flow_table_size_,
                                        PROT_READ, MAP_SHARED, fd, 0);
    if (flow_table_ == MAP_FAILED) {
        LOG(DEBUG, "Error mapping flow table memory. Error <" << errno
            << "> : " << strerror(errno));
        assert(0);
    }

    flow_table_entries_count_ = flow_table_size_ / sizeof(vr_flow_entry);
    ksync_->agent()->set_flow_table_size(flow_table_entries_count_);
    return;
}

void KSyncFlowMemory::InitTest() {
    flow_table_ = KSyncSockTypeMap::FlowMmapAlloc(kTestFlowTableSize);
    memset(flow_table_, 0, kTestFlowTableSize);
    flow_table_entries_count_ = kTestFlowTableSize / sizeof(vr_flow_entry);
    audit_yield_ = flow_table_entries_count_;
    audit_timeout_ = 10 * 1000; // timout immediately.
    ksync_->agent()->set_flow_table_size(flow_table_entries_count_);
}

void KSyncFlowMemory::UnmapFlowMemTest() {
    KSyncSockTypeMap::FlowMmapFree();
}

void KSyncFlowMemory::Shutdown() {
    UnmapFlowMemTest();
}

void KSyncFlowMemory::VrFlowToIp(const vr_flow_entry *kflow, IpAddress *sip,
                                 IpAddress *dip) {
    if (kflow->fe_key.flow_family == AF_INET) {
        *sip = Ip4Address(ntohl(kflow->fe_key.key_u.ip4_key.ip4_sip));
        *dip = Ip4Address(ntohl(kflow->fe_key.key_u.ip4_key.ip4_dip));
    } else {
        const unsigned char *k_sip = kflow->fe_key.key_u.ip6_key.ip6_sip;
        const unsigned char *k_dip = kflow->fe_key.key_u.ip6_key.ip6_dip;
        boost::array<unsigned char, 16> sbytes;
        boost::array<unsigned char, 16> dbytes;
        for (int i = 0; i < 16; i++) {
            sbytes[i] = k_sip[i];
            dbytes[i] = k_dip[i];
        }
        *sip = Ip6Address(sbytes);
        *dip = Ip6Address(dbytes);
    }
}

void KSyncFlowMemory::KFlow2FlowKey(const vr_flow_entry *kflow,
                                    FlowKey *key) const {
    key->nh = kflow->fe_key.flow4_nh_id;
    Address::Family family = (kflow->fe_key.flow_family == AF_INET)?
                              Address::INET : Address::INET6;
    VrFlowToIp(kflow, &key->src_addr, &key->dst_addr);
    key->src_port = ntohs(kflow->fe_key.flow4_sport);
    key->dst_port = ntohs(kflow->fe_key.flow4_dport);
    key->protocol = kflow->fe_key.flow4_proto;
    key->family = family;
}

const vr_flow_entry *KSyncFlowMemory::GetValidKFlowEntry(const FlowKey &key,
                                                         uint32_t idx,
                                                         uint8_t gen_id) const {
    const vr_flow_entry *kflow = GetKernelFlowEntry(idx, false);
    if (!kflow) {
        return NULL;
    }
    if (key.protocol == IPPROTO_TCP) {
        FlowKey rhs;
        KFlow2FlowKey(kflow, &rhs);
        if (!key.IsEqual(rhs)) {
            return NULL;
        }
        if (kflow->fe_gen_id != gen_id) {
            return NULL;
        }
    }
    return kflow;
}

const vr_flow_entry *KSyncFlowMemory::GetKernelFlowEntry
    (uint32_t idx, bool ignore_active_status) const {
    if (idx == FlowEntry::kInvalidFlowHandle) {
        return NULL;
    }

    if (idx >= flow_table_entries_count_) {
        /* if index is outside the range of flow table entries return NULL */
        return NULL;
    }

    if (ignore_active_status) {
        return &flow_table_[idx];
    }

    if (flow_table_[idx].fe_flags & VR_FLOW_FLAG_ACTIVE) {
        return &flow_table_[idx];
    }
    return NULL;
}

bool KSyncFlowMemory::GetFlowKey(uint32_t index, FlowKey *key) {
    const vr_flow_entry *kflow = GetKernelFlowEntry(index, false);
    if (!kflow) {
        return false;
    }
    key->nh = kflow->fe_key.flow4_nh_id;
    Address::Family family = (kflow->fe_key.flow_family == AF_INET)?
                              Address::INET : Address::INET6;
    VrFlowToIp(kflow, &key->src_addr, &key->dst_addr);
    key->src_port = ntohs(kflow->fe_key.flow4_sport);
    key->dst_port = ntohs(kflow->fe_key.flow4_dport);
    key->protocol = kflow->fe_key.flow4_proto;
    key->family = family;
    return true;
}

bool KSyncFlowMemory::IsEvictionMarked(const vr_flow_entry *entry,
                                       uint16_t flags) const {
    if (!entry) {
        return false;
    }
    if (flags & VR_FLOW_FLAG_EVICTED) {
        return true;
    }
    return false;
}

const vr_flow_entry *KSyncFlowMemory::GetKFlowStats(const FlowKey &key,
                                                    uint32_t idx,
                                                    uint8_t gen_id,
                                                    vr_flow_stats *stat) const {
    const vr_flow_entry *kflow = GetValidKFlowEntry(key, idx, gen_id);
    if (!kflow) {
        return NULL;
    }
    *stat = kflow->fe_stats;
    kflow = GetValidKFlowEntry(key, idx, gen_id);
    return kflow;
}

void KSyncFlowMemory::ReadFlowInfo(const vr_flow_entry *kflow,
                                   vr_flow_stats *stat, KFlowData *info) const {
    *stat = kflow->fe_stats;
    info->underlay_src_port = kflow->fe_udp_src_port;
    info->tcp_flags = kflow->fe_tcp_flags;
    info->flags = kflow->fe_flags;
}

const vr_flow_entry *KSyncFlowMemory::GetKFlowStatsAndInfo(const FlowKey &key,
                                                           uint32_t idx,
                                                           uint8_t gen_id,
                                                           vr_flow_stats *stats,
                                                           KFlowData *info)
    const {
    const vr_flow_entry *kflow = GetKernelFlowEntry(idx, false);
    if (!kflow) {
        return NULL;
    }
    if (key.protocol == IPPROTO_TCP) {
        FlowKey rhs;
        KFlow2FlowKey(kflow, &rhs);
        if (!key.IsEqual(rhs)) {
            return NULL;
        }

        ReadFlowInfo(kflow, stats, info);

        if (kflow->fe_gen_id != gen_id) {
            return NULL;
        }
    } else {
        ReadFlowInfo(kflow, stats, info);
    }
    return kflow;
}

bool KSyncFlowMemory::AuditProcess() {
    uint32_t flow_idx;
    uint8_t gen_id;
    const vr_flow_entry *vflow_entry;
    // Get current time
    uint64_t t = UTCTimestampUsec();

    while (!audit_flow_list_.empty()) {
        AuditEntry list_entry = audit_flow_list_.front();
        // audit_flow_list_ is sorted on last time of insertion in the list
        // So, break on finding first flow entry that cannot be aged
        if ((t - list_entry.timeout) < audit_timeout_) {
            /* Wait for audit_timeout_ to create short flow for the entry */
            break;
        }
        flow_idx = list_entry.audit_flow_idx;
        gen_id = list_entry.audit_flow_gen_id;
        audit_flow_list_.pop_front();

        vflow_entry = GetKernelFlowEntry(flow_idx, false);
        // Audit and remove flow entry if its still in HOLD state
        if (vflow_entry && vflow_entry->fe_gen_id == gen_id &&
            vflow_entry->fe_action == VR_FLOW_ACTION_HOLD) {
            IpAddress sip, dip;
            VrFlowToIp(vflow_entry, &sip, &dip);
            FlowKey key(vflow_entry->fe_key.flow_nh_id, sip, dip,
                        vflow_entry->fe_key.flow_proto,
                        ntohs(vflow_entry->fe_key.flow_sport),
                        ntohs(vflow_entry->fe_key.flow_dport));

            FlowProto *proto = ksync_->agent()->pkt()->get_flow_proto();
            DecrementHoldFlowCounter();
            proto->CreateAuditEntry(key, flow_idx, gen_id);
            LOG(DEBUG, "FlowAudit : Converting HOLD entry to short flow " <<
                       "for index " << flow_idx);
        }
    }

    uint32_t count = 0;
    assert(audit_yield_);
    while (count < audit_yield_) {
        vflow_entry = GetKernelFlowEntry(audit_flow_idx_, false);
        if (vflow_entry && vflow_entry->fe_action == VR_FLOW_ACTION_HOLD) {
            IncrementHoldFlowCounter();
            audit_flow_list_.push_back(AuditEntry(audit_flow_idx_,
                                                  vflow_entry->fe_gen_id, t));
        }

        count++;
        audit_flow_idx_++;
        if (audit_flow_idx_ == flow_table_entries_count_) {
            UpdateAgentHoldFlowCounter();
            audit_flow_idx_ = 0;
        }
    }
    return true;
}

void KSyncFlowMemory::GetFlowTableSize() {
    struct nl_client *cl;
    vr_flow_table_data info;
    int attr_len;
    int encode_len, error;

    assert((cl = nl_register_client()) != NULL);
    cl->cl_genl_family_id = KSyncSock::GetNetlinkFamilyId();
    assert(nl_build_nlh(cl, cl->cl_genl_family_id, NLM_F_REQUEST) == 0);
    assert(nl_build_genlh(cl, SANDESH_REQUEST, 0) == 0);

    attr_len = nl_get_attr_hdr_size();
    info.set_ftable_op(flow_op::FLOW_TABLE_GET);
    info.set_ftable_size(0);
    info.set_ftable_dev(0);
    info.set_ftable_file_path("");
    info.set_ftable_processed(0);
    info.set_ftable_hold_oflows(0);
    info.set_ftable_added(0);
    info.set_ftable_cpus(0);
    info.set_ftable_created(0);
    info.set_ftable_oflow_entries(0);
    encode_len = info.WriteBinary(nl_get_buf_ptr(cl) + attr_len,
                                 nl_get_buf_len(cl), &error);
    nl_build_attr(cl, encode_len, NL_ATTR_VR_MESSAGE_PROTOCOL);
    nl_update_nlh(cl);

    tcp::socket socket(*(ksync_->agent()->event_manager()->io_service()));
    tcp::endpoint endpoint(ksync_->agent()->vrouter_server_ip(),
                           ksync_->agent()->vrouter_server_port());
    boost::system::error_code ec;
    socket.connect(endpoint, ec);
    if (ec) {
        assert(0);
    }

    socket.send(boost::asio::buffer(cl->cl_buf, cl->cl_buf_offset), 0, ec);
    if (ec) {
        assert(0);
    }

    uint32_t len_read = 0;
    uint32_t data_len = sizeof(struct nlmsghdr);
    while (len_read < data_len) {
        len_read = socket.read_some(boost::asio::buffer(cl->cl_buf + len_read,
                                                        cl->cl_buf_len), ec);
        if (ec) {
            assert(0);
        }

        if (len_read > sizeof(struct nlmsghdr)) {
            const struct nlmsghdr *nlh =
                (const struct nlmsghdr *)((cl->cl_buf));
            data_len = nlh->nlmsg_len;
        }
    }

    KSyncSockNetlink::NetlinkDecoder(cl->cl_buf,
                                     KSyncSock::GetAgentSandeshContext(0));
    nl_free_client(cl);
}

void KSyncFlowMemory::MapSharedMemory() {
    GetFlowTableSize();

    int fd;
    if ((fd = open(flow_table_path_.c_str(), O_RDONLY | O_SYNC)) < 0) {
        LOG(DEBUG, "Error opening device " << flow_table_path_
            << ". Error <" << errno
            << "> : " << strerror(errno));
        assert(0);
    }

    flow_table_ = (vr_flow_entry *)mmap(NULL, flow_table_size_,
                                        PROT_READ, MAP_SHARED, fd, 0);
    if (flow_table_ == MAP_FAILED) {
        LOG(DEBUG, "Error mapping flow table memory. Error <" << errno
            << "> : " << strerror(errno));
        assert(0);
    }

    flow_table_entries_count_ = flow_table_size_ / sizeof(vr_flow_entry);
    ksync_->agent()->set_flow_table_size(flow_table_entries_count_);
}

void vr_flow_req::Process(SandeshContext *context) {
    AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
    ioc->FlowMsgHandler(this);
}

void vr_flow_response::Process(SandeshContext *context) {
    AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
    ioc->FlowResponseHandler(this);
}

void vr_flow_table_data::Process(SandeshContext *context) {
    AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
    ioc->FlowTableInfoHandler(this);
}


text:
////////////////////////////////////////////////////////////////////////////
//  Boost.Assign v2                                                       //
//                                                                        //
//  Copyright (C) 2003-2004 Thorsten Ottosen                              //
//  Copyright (C) 2011 Erwann Rogard                                      //
//  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 <deque>
#include <string>
#include <utility>
#include <boost/array.hpp>
#include <boost/assign/v2/include/deque.hpp>
#include <boost/assign/v2/support/config/check.hpp>
#include <boost/range/algorithm/equal.hpp>
#include <libs/assign/v2/test/deque/deque.h>

namespace test_assign_v2{
namespace xxx_deque{
namespace xxx_deque{

    void test()
    {
        namespace as2 = boost::assign::v2;
        {
            //[test_deque1
            typedef as2::result_of::deque<char*>::type C;

            C deque = as2::deque<char*>( "x" )( "y" )( "z" );

            std::deque<std::string> benchmark;
            benchmark.push_back( "x" );
            benchmark.push_back( "y" );
            benchmark.push_back( "z" );

            BOOST_ASSIGN_V2_CHECK( boost::range::equal( benchmark, deque ) );
            //]
        }
        {
            //[test_deque2
            typedef std::string word_;
            const char x[] = "foo";
            const char y[4] = { 'b', 'a', 'r', '\0' };
            word_ z = "***baz";

            std::deque<word_> benchmark;
            benchmark.push_back( word_( x, 3 ) );
            benchmark.push_back( word_( y ) );
            benchmark.push_back( word_( z, 3, 3 ) );
            benchmark.push_back( word_( "qux" ) );

            typedef as2::result_of::deque<word_>::type C;
            C cont1 = as2::deque<word_>( as2::_nil );

            BOOST_ASSIGN_V2_CHECK( cont1.empty() );

            cont1( x, 3 )( y )( z, 3, 3 )( "qux" );

            BOOST_ASSIGN_V2_CHECK( boost::range::equal( benchmark, cont1 ) );

            C cont2 = as2::deque<word_>( x, 3 )( y )( z, 3, 3 )( "qux" );

            BOOST_ASSIGN_V2_CHECK( boost::range::equal( benchmark, cont2 ) );
            //]
        }
    }

}// xxx_deque
}// xxx_deque
}// test_assign_v2

ext: cpp


text:
/* \author Adrian Arroyo - adr.arroyo.perez@gmail.com */

#include <iostream>

#include <boost/thread/thread.hpp>
#include <pcl/range_image/range_image.h>
#include <pcl/io/ply_io.h>
#include <pcl/visualization/range_image_visualizer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/features/range_image_border_extractor.h>
#include <pcl/keypoints/narf_keypoint.h>
#include <pcl/features/narf_descriptor.h>
#include <pcl/console/parse.h>
#include <pcl/point_types.h>
#include <pcl/features/pfh.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/features/normal_3d.h>
#include <pcl/surface/gp3.h>
#include <pcl/features/spin_image.h>

typedef pcl::PointXYZ PointType;

// --------------------
// -----Parameters-----
// --------------------
float angular_resolution = 0.5f;
float support_size = 0.2f;
pcl::RangeImage::CoordinateFrame coordinate_frame =
		pcl::RangeImage::CAMERA_FRAME;
bool setUnseenToMaxRange = false;
bool rotation_invariant = true;

// --------------
// -----Help-----
// --------------
void printUsage(const char* progName) {
	std::cout << "\n\nUsage: " << progName << " [options] <scene.pcd>\n\n"
			<< "Options:\n" << "-------------------------------------------\n"
			<< "-r <float>   angular resolution in degrees (default "
			<< angular_resolution << ")\n"
			<< "-c <int>     coordinate frame (default "
			<< (int) coordinate_frame << ")\n"
			<< "-m           Treat all unseen points to max range\n"
			<< "-s <float>   support size for the interest points (diameter of the used sphere - "
					"default " << support_size << ")\n"
			<< "-o <0/1>     switch rotational invariant version of the feature on/off"
			<< " (default " << (int) rotation_invariant << ")\n"
			<< "-h           this help\n" << "\n\n";
}

void setViewerPose(pcl::visualization::PCLVisualizer& viewer,
		const Eigen::Affine3f& viewer_pose) {
	Eigen::Vector3f pos_vector = viewer_pose * Eigen::Vector3f(0, 0, 0);
	Eigen::Vector3f look_at_vector = viewer_pose.rotation()
			* Eigen::Vector3f(0, 0, 1) + pos_vector;
	Eigen::Vector3f up_vector = viewer_pose.rotation()
			* Eigen::Vector3f(0, -1, 0);
	viewer.setCameraPosition(pos_vector[0], pos_vector[1], pos_vector[2],
			look_at_vector[0], look_at_vector[1], look_at_vector[2],
			up_vector[0], up_vector[1], up_vector[2]);
}

// --------------
// -----Main-----
// --------------
int main(int argc, char** argv) {
	// --------------------------------------
	// -----Parse Command Line Arguments-----
	// --------------------------------------
	if (pcl::console::find_argument(argc, argv, "-h") >= 0) {
		printUsage(argv[0]);
		return 0;
	}
	if (pcl::console::find_argument(argc, argv, "-m") >= 0) {
		setUnseenToMaxRange = true;
		cout
				<< "Setting unseen values in range image to maximum range readings.\n";
	}
	if (pcl::console::parse(argc, argv, "-o", rotation_invariant) >= 0)
		cout << "Switching rotation invariant feature version "
				<< (rotation_invariant ? "on" : "off") << ".\n";
	int tmp_coordinate_frame;
	if (pcl::console::parse(argc, argv, "-c", tmp_coordinate_frame) >= 0) {
		coordinate_frame = pcl::RangeImage::CoordinateFrame(
				tmp_coordinate_frame);
		cout << "Using coordinate frame " << (int) coordinate_frame << ".\n";
	}
	if (pcl::console::parse(argc, argv, "-s", support_size) >= 0)
		cout << "Setting support size to " << support_size << ".\n";
	if (pcl::console::parse(argc, argv, "-r", angular_resolution) >= 0)
		cout << "Setting angular resolution to " << angular_resolution
				<< "deg.\n";
	angular_resolution = pcl::deg2rad(angular_resolution);

	// ------------------------------------------------------------------
	// -----Read ply file-----
	// ------------------------------------------------------------------
	pcl::PointCloud<PointType>::Ptr point_cloud_ptr(
			new pcl::PointCloud<PointType>);
	pcl::PointCloud<PointType>& point_cloud = *point_cloud_ptr;
	pcl::PointCloud<pcl::PointWithViewpoint> far_ranges;
	Eigen::Affine3f scene_sensor_pose(Eigen::Affine3f::Identity());
	std::vector<int> pcd_filename_indices =
			pcl::console::parse_file_extension_argument(argc, argv, "ply");
	std::string filename = argv[pcd_filename_indices[0]];
	if (pcl::io::loadPLYFile(filename, point_cloud) == -1) {
		cerr << "Was not able to open file \"" << filename << "\".\n";
		printUsage(argv[0]);
		return 0;
	}
	scene_sensor_pose = Eigen::Affine3f(
			Eigen::Translation3f(point_cloud.sensor_origin_[0],
					point_cloud.sensor_origin_[1],
					point_cloud.sensor_origin_[2]))
			* Eigen::Affine3f(point_cloud.sensor_orientation_);

	//---------EXTRACTION OF NARF FEATURES------------
	// -----------------------------------------------
	// -----Create RangeImage from the PointCloud-----
	// -----------------------------------------------
	float noise_level = 0.0;
	float min_range = 0.0f;
	int border_size = 1;
	boost::shared_ptr<pcl::RangeImage> range_image_ptr(new pcl::RangeImage);
	pcl::RangeImage& range_image = *range_image_ptr;
	range_image.createFromPointCloud(point_cloud, angular_resolution,
			pcl::deg2rad(360.0f), pcl::deg2rad(180.0f), scene_sensor_pose,
			coordinate_frame, noise_level, min_range, border_size);
	range_image.integrateFarRanges(far_ranges);
	if (setUnseenToMaxRange)
		range_image.setUnseenToMaxRange();

	// --------------------------------------------
	// -----Open 3D viewer and add point cloud-----
	// --------------------------------------------
	pcl::visualization::PCLVisualizer viewer("3D Viewer");
	viewer.setBackgroundColor(1, 1, 1);
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> range_image_color_handler(
			range_image_ptr, 0, 0, 0);
	viewer.addPointCloud(range_image_ptr, range_image_color_handler,
			"range image");
	viewer.setPointCloudRenderingProperties(
			pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "range image");
	//viewer.addCoordinateSystem (1.0f, "global");
	//PointCloudColorHandlerCustom<PointType> point_cloud_color_handler (point_cloud_ptr, 150, 150, 150);
	//viewer.addPointCloud (point_cloud_ptr, point_cloud_color_handler, "original point cloud");
	viewer.initCameraParameters();
	setViewerPose(viewer, range_image.getTransformationToWorldSystem());

	// --------------------------------
	// -----Extract NARF keypoints-----
	// --------------------------------
	pcl::RangeImageBorderExtractor range_image_border_extractor;
	pcl::NarfKeypoint narf_keypoint_detector;
	narf_keypoint_detector.setRangeImageBorderExtractor(
			&range_image_border_extractor);
	narf_keypoint_detector.setRangeImage(&range_image);
	narf_keypoint_detector.getParameters().support_size = support_size;

	pcl::PointCloud<int> keypoint_indices;
	narf_keypoint_detector.compute(keypoint_indices);
	std::cout << "Found " << keypoint_indices.points.size() << " key points.\n";

	// -------------------------------------
	// -----Show keypoints in 3D viewer-----
	// -------------------------------------
	pcl::PointCloud<pcl::PointXYZ>::Ptr keypoints_ptr(
			new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PointCloud<pcl::PointXYZ>& keypoints = *keypoints_ptr;
	keypoints.points.resize(keypoint_indices.points.size());
	for (size_t i = 0; i < keypoint_indices.points.size(); ++i)
		keypoints.points[i].getVector3fMap() =
				range_image.points[keypoint_indices.points[i]].getVector3fMap();
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> keypoints_color_handler(
			keypoints_ptr, 0, 255, 0);
	viewer.addPointCloud<pcl::PointXYZ>(keypoints_ptr, keypoints_color_handler,
			"keypoints");
	viewer.setPointCloudRenderingProperties(
			pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "keypoints");

	// ------------------------------------------------------
	// -----Extract NARF descriptors for interest points-----
	// ------------------------------------------------------
	std::vector<int> keypoint_indices2;
	keypoint_indices2.resize(keypoint_indices.points.size());
	for (unsigned int i = 0; i < keypoint_indices.size(); ++i) // This step is necessary to get the right vector type
		keypoint_indices2[i] = keypoint_indices.points[i];
	pcl::NarfDescriptor narf_descriptor(&range_image, &keypoint_indices2);
	narf_descriptor.getParameters().support_size = support_size;
	narf_descriptor.getParameters().rotation_invariant = rotation_invariant;
	pcl::PointCloud<pcl::Narf36> narf_descriptors;
	narf_descriptor.compute(narf_descriptors);
	cout << "Extracted " << narf_descriptors.size() << " descriptors for "
			<< keypoint_indices.points.size() << " keypoints.\n";

	//--------------------
	// -----Main loop-----
	//--------------------
	while (!viewer.wasStopped()) {
		viewer.spinOnce();
		pcl_sleep(0.01);
	}

	// ------------------------------------------------------
	// ---------------Extract PFH features-------------------
	// ------------------------------------------------------
	// Normal estimation*
	/*pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> n;
	 pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
	 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(
	 new pcl::search::KdTree<pcl::PointXYZ>);
	 tree->setInputCloud(point_cloud_ptr);
	 n.setInputCloud(point_cloud_ptr);
	 n.setSearchMethod(tree);
	 n.setKSearch(20);
	 n.compute(*normals);

	 // Create the PFH estimation class, and pass the input dataset+normals to it
	 /*pcl::PFHEstimation<pcl::PointXYZ, pcl::Normal, pcl::PFHSignature125> pfh;
	 pfh.setInputCloud(point_cloud_ptr);
	 pfh.setInputNormals(normals);

	 // Create an empty kdtree representation, and pass it to the PFH estimation object.
	 // Its content will be filled inside the object, based on the given input dataset (as no other search surface is given).
	 pfh.setSearchMethod(tree);

	 // Output datasets
	 pcl::PointCloud<pcl::PFHSignature125>::Ptr pfhs(
	 new pcl::PointCloud<pcl::PFHSignature125>());

	 // Use all neighbors in a sphere of radius 5cm
	 // IMPORTANT: the radius used here has to be larger than the radius used to estimate the surface normals!!!
	 pfh.setRadiusSearch(0.05);

	 // Check for NaNs in normals
	 for (int i = 0; i < normals->points.size(); i++) {
	 if (!pcl::isFinite<pcl::Normal>(normals->points[i])) {
	 PCL_WARN("normals[%d] is not finite\n", i);
	 }
	 }

	 // Compute the features
	 pfh.compute(*pfhs);

	 cout << "Extracted " << pfh.k_ << "PFH features";*/

	// ------------------------------------------------------
	// ------------Extract Spin Image Estimator--------------
	// ------------------------------------------------------
	// Setup the spin images computation
	/*typedef pcl::Histogram<153> SpinImage;
	pcl::SpinImageEstimation<pcl::PointXYZ, pcl::Normal, SpinImage> spinImageEstimation(
			8, 0.5, 16);
	spinImageEstimation.setInputCloud(point_cloud_ptr);
	spinImageEstimation.setInputNormals(normals);

	// Use the same KdTree from the normal estimation
	spinImageEstimation.setSearchMethod(tree);
	pcl::PointCloud<SpinImage>::Ptr spinImages(new pcl::PointCloud<SpinImage>);
	spinImageEstimation.setRadiusSearch (2);
	//spinImageEstimation.setKSearch(10);

	// Actually compute the spin images
	spinImageEstimation.compute(*spinImages);
	std::cout << "SI output points.size (): " << spinImages->points.size()
			<< std::endl;

	// Display and retrieve the spin image descriptor vector for the first point.
	SpinImage descriptor = spinImages->points[0];
	std::cout << descriptor << std::endl;*/

	return 1;
}

max_stars_repo_name: adr-arroyo/PointCloudComparator

max_issues_repo_name: adr-arroyo/PointCloudComparator

max_forks_repo_name: adr-arroyo/PointCloudComparator


text:
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
//
// 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)
//
// Official repository: https://github.com/CPPAlliance/url
//

#ifndef BOOST_URL_IMPL_SEGMENTS_ENCODED_IPP
#define BOOST_URL_IMPL_SEGMENTS_ENCODED_IPP

#include <boost/url/segments_encoded.hpp>
#include <boost/url/url.hpp>
#include <boost/url/detail/copied_strings.hpp>
#include <boost/url/detail/path.hpp>

namespace boost {
namespace urls {

string_view
segments_encoded::
iterator::
operator*() const noexcept
{
    BOOST_ASSERT(i_ < u_->nseg_);
    auto p0 = u_->segment(i_);
    auto const p1 =
        u_->segment(i_ + 1);
    if(i_ > 0)
        ++p0;
    else
        p0 += detail::path_prefix(
            u_->get(id_path));
    string_view s(
        u_->cs_ + p0, p1 - p0);
    return s;
}

//------------------------------------------------
//
// Element Access
//
//------------------------------------------------

auto
segments_encoded::
operator[](
    std::size_t i) const noexcept ->
    reference
{
    BOOST_ASSERT(i < u_->nseg_);
    auto p0 = u_->segment(i);
    auto const p1 =
        u_->segment(i + 1);
    if(i > 0)
        ++p0;
    else
        p0 += detail::path_prefix(
            u_->get(id_path));
    string_view s(
        u_->cs_ + p0, p1 - p0);
    return s;
}

//------------------------------------------------
//
// Modifiers
//
//------------------------------------------------

auto
segments_encoded::
insert(
    iterator before,
    string_view s0) ->
        iterator
{
    BOOST_ASSERT(before.u_ == u_);
    detail::copied_strings cs(
        u_->string());
    auto s = cs.maybe_copy(s0);
    u_->edit_segments(
        before.i_,
        before.i_,
        detail::make_enc_segs_iter(
            &s, &s + 1),
        detail::make_enc_segs_iter(
            &s, &s + 1));
    return { *u_, before.i_ };
}

auto
segments_encoded::
erase(
    iterator first,
    iterator last) noexcept ->
        iterator
{
    BOOST_ASSERT(first.u_ == u_);
    BOOST_ASSERT(last.u_ == u_);
    string_view s;
    u_->edit_segments(
        first.i_, last.i_,
        detail::make_enc_segs_iter(&s, &s),
        detail::make_enc_segs_iter(&s, &s));
    return { *u_, first.i_ };
}

} // urls
} // boost

#endif

ext: ipp

max_stars_repo_name: vgrigoriu/url

max_issues_repo_name: vgrigoriu/url

max_forks_repo_name: vgrigoriu/url


text:
/**
 * @file image_load_test.cpp
 * @author Mehul Kumar Nirala
 *
 * Tests for loading and saving images.
 *
 * mlpack is free software; you may redistribute it and/or modify it under the
 * terms of the 3-clause BSD license.  You should have received a copy of the
 * 3-clause BSD license along with mlpack.  If not, see
 * http://www.opensource.org/licenses/BSD-3-Clause for more information.
 */

#include <mlpack/core.hpp>
#include <boost/test/unit_test.hpp>

using namespace mlpack;
using namespace mlpack::data;
using namespace std;

#ifdef HAS_STB // Compile this only if stb is present.

BOOST_AUTO_TEST_SUITE(ImageLoadTest);

/**
 * Test if an image with an unsupported extension throws an expected
 * exception.
 */
BOOST_AUTO_TEST_CASE(LoadInvalidExtensionFile)
{
  arma::Mat<unsigned char> matrix;
  data::ImageInfo info;
  BOOST_REQUIRE_THROW(data::Load("invalidExtendion.p4ng", matrix, info,
    false, true),  std::runtime_error);
}

/**
 * Test that the image is loaded correctly into the matrix using the API.
 */
BOOST_AUTO_TEST_CASE(LoadImageAPITest)
{
  arma::Mat<unsigned char> matrix;
  data::ImageInfo info;
  BOOST_REQUIRE(data::Load("test_image.png", matrix, info, false,
      true) == true);
  BOOST_REQUIRE_EQUAL(matrix.n_rows, 50 * 50 * 3); // width * height * channels.
  BOOST_REQUIRE_EQUAL(matrix.n_cols, 1);
}

/**
 * Test if the image is saved correctly using API.
 */
BOOST_AUTO_TEST_CASE(SaveImageAPITest)
{
  data::ImageInfo info(5, 5, 3, 90);

  arma::Mat<unsigned char> im1;
  size_t dimension = info.Width() * info.Height() * info.Channels();
  im1 = arma::randi<arma::Mat<unsigned char>>(dimension, 1);
  BOOST_REQUIRE(data::Save("APITest.bmp", im1, info, false, true) == true);

  arma::Mat<unsigned char> im2;
  BOOST_REQUIRE(data::Load("APITest.bmp", im2, info, false, true) == true);

  BOOST_REQUIRE_EQUAL(im1.n_cols, im2.n_cols);
  BOOST_REQUIRE_EQUAL(im1.n_rows, im2.n_rows);
  for (size_t i = 10; i < im1.n_elem; ++i)
    BOOST_REQUIRE_EQUAL(im1[i], im2[i]);
}

BOOST_AUTO_TEST_SUITE_END();

#endif // HAS_STB.

max_stars_repo_name: tomjpsun/mlpack

max_issues_repo_name: tomjpsun/mlpack

max_forks_repo_name: tomjpsun/mlpack


text:
//----------------------------------------------------------------------------
// Copyright (C) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the Server Side Public License, version 1,
// as published by the author.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// Server Side Public License for more details.
//
// You should have received a copy of the Server Side Public License
// along with this program. If not, see
// <https://github.com/NilFoundation/plugin/blob/master/LICENSE_1_0.txt>.
//----------------------------------------------------------------------------

#define BOOST_TEST_MODULE post_election_compound_test

#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>

#include <nil/filecoin/storage/proofs/core/sector.hpp>

#include <nil/filecoin/storage/proofs/post/election/compound.hpp>

#include "../../core/merkle/generate_tree.hpp"

using namespace nil::filecoin;

BOOST_AUTO_TEST_SUITE(post_election_compound_test_suite)

template<typename MerkleTreeType>
void election_post_test_compound() {
    const auto rng = XorShiftRng::from_seed(crate::TEST_SEED);

    std::size_t leaves = 64 * get_base_tree_count<MerkleTreeType>();
    std::uint64_t sector_size = (leaves * NODE_SIZE);
    const auto randomness = typename MerkleTreeType::hash_type::digest_type::random(rng);
    const auto prover_id = typename MerkleTreeType::hash_type::digest_type::random(rng);

    compound_proof::SetupParams setup_params =
        {election::SetupParams {sector_size, 20, 1}, partitions : None, priority : true};

    std::vector<sector_id_type> sectors;
    auto trees = BTreeMap();

    // Construct and store an MT using a named store.
    const auto temp_dir = tempfile::tempdir();
    const auto temp_path = temp_dir.path();

    for (std::size_t i = 0; i < 5; i++) {
        sectors.push_back(i.into());
        auto data, tree;
        const std::tie(_data, tree) = merkletree::generate_tree<Tree>(rng, leaves, Some(temp_path.to_path_buf()));
        trees.insert(i.into(), tree);
    }

    const auto pub_params = ElectionPoStCompound<MerkleTreeType>::setup(&setup_params).expect("setup failed");

    const auto candidates = election::generate_candidates<MerkleTreeType>(&pub_params.vanilla_params, &sectors, &trees,
                                                                   prover_id, randomness)
                         ;

    const auto candidate = &candidates[0];
    const auto tree = trees.remove(&candidate.sector_id);
    const auto comm_r_last = tree.root();
    const auto comm_c = typename MerkleTreeType::hash_type::digest_type::random(rng);
    const auto comm_r = <typename MerkleTreeType::hash_type>::Function::hash2(&comm_c, &comm_r_last);

    election::PublicInputs pub_inputs = {randomness, candidate.sector_id,      prover_id,
                                         comm_r,     candidate.partial_ticket, 0};

    election::PrivateInputs::<MerkleTreeType>priv_inputs = {tree, comm_c, comm_r_last};

    const auto(circuit, inputs) = ElectionPoStCompound::circuit_for_test(&pub_params, &pub_inputs, &priv_inputs);

    auto cs = TestConstraintSystem();

    circuit.synthesize(cs).expect("failed to synthesize");

    if (!cs.is_satisfied()) {
        panic !("failed to satisfy: {:?}", cs.which_is_unsatisfied());
    }
    BOOST_CHECK(cs.verify(&inputs), "verification failed with TestContraintSystem and generated inputs");

    // Use this to debug differences between blank and regular circuit generation.
    const auto(circuit1, _inputs) =
        ElectionPoStCompound::circuit_for_test(&pub_params, &pub_inputs, &priv_inputs);
    const auto blank_circuit = ElectionPoStCompound::<Tree>::blank_circuit(&pub_params.vanilla_params);

    auto cs_blank = MetricCS();
    blank_circuit.synthesize(cs_blank).expect("failed to synthesize");

    const auto a = cs_blank.pretty_print_list();

    auto cs1 = TestConstraintSystem();
    circuit1.synthesize(cs1).expect("failed to synthesize");
    const auto b = cs1.pretty_print_list();

    for ((i, (a, b)) : a.chunks(100).zip(b.chunks(100)).enumerate()) {
        BOOST_ASSERT_MSG(a == b, std::format("failed at chunk %d", i));
    }

    const auto blank_groth_params = ElectionPoStCompound<MerkleTreeType>::groth_params(Some(rng), &pub_params.vanilla_params)
                                 .expect("failed to generate groth params");

    const auto proof = ElectionPoStCompound::prove(&pub_params, &pub_inputs, &priv_inputs, &blank_groth_params, )
                    .expect("failed while proving");

    const auto verified = ElectionPoStCompound::verify(&pub_params, &pub_inputs, &proof, &NoRequirements)
                       .expect("failed while verifying");

    BOOST_CHECK(verified);
}

BOOST_AUTO_TEST_CASE(election_post_test_compound_pedersen) {
    election_post_test_compound::<LCTree<PedersenHasher, U8, U0, U0>>();
}

BOOST_AUTO_TEST_CASE(election_post_test_compound_poseidon) {
    election_post_test_compound::<LCTree<PoseidonHasher, U8, U0, U0>>();
}

BOOST_AUTO_TEST_SUITE_END()


text:
#include "mock/layout.hpp"
#include <boost/make_unique.hpp>

std::unique_ptr<mock_layout> make_mock_layout()
{
    return boost::make_unique<testing::NiceMock<mock_layout>>();
}

max_stars_repo_name: KazDragon/munin

max_issues_repo_name: KazDragon/munin

max_forks_repo_name: KazDragon/munin