text:
#include "StdAfx.h"

#include "Settings.h"
#include "AssetServer.h"
#include "IntraMeshService.h"

#include <fstream>
//compiler complains about std::copy inside of boost
#pragma warning(disable:4996)
#include <boost/algorithm/string.hpp>
#pragma warning(default:4996)

#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>

namespace po = boost::program_options;
using namespace std;

namespace whip
{
	const char* const Settings::SETTINGS_FILE = "whip.cfg";
	Settings* Settings::_instance = 0;

	Settings::Settings(const std::string& configFileName)
		: _configFileName(configFileName)
	{
		this->reload();
	}

	Settings::~Settings()
	{
	}

	void Settings::reload()
	{
		const int TCP_BUFFSZ_DEFAULT = 65536;
		const int UDP_BUFFSZ_DEFAULT = 32768;
		const int DEFAULT_PULL_REPL_FREQUENCY = 720; //12 hrs
		const unsigned int DEFAULT_PULL_REPL_BATCH_SIZE = 1;

		//setup options
		po::options_description desc("Allowed options");
		desc.add_options()
			("port", po::value<unsigned short>()->default_value(AssetServer::DEFAULT_PORT), "TCP port to listen for client connections")
			("password", po::value<std::string>(), "Password for connection")
			("cache_enabled", po::value<bool>()->default_value(false), "Enable memory cache")
			("cache_size", po::value<unsigned int>()->default_value(10), "Maximum size of the asset cache in MB")
			("disk_storage_backend", po::value<std::string>(), "The name of the persistent storage backend to use")
			("disk_storage_root", po::value<std::string>(), "Where the Disk Storage Backend should store the asset files")
			("secondary_storage_root", po::value<std::string>()->default_value(""), "A second place for the disk storage backend to look for asset files")
			("migration", po::value<std::string>()->default_value("none"), "The name of the migration to perform (or 'none' for no migration)")
			("allow_purge", po::value<bool>()->default_value(false), "Whether or not to allow the purge operation")
			("debug", po::value<bool>()->default_value(false), "Enable debugging")
			("intramesh_peers", po::value<std::string>()->default_value(""), "Peers to contact on intramesh heartbeat and for intramesh queries")
			("intramesh_port", po::value<unsigned short>()->default_value(iwintramesh::IntraMeshService::DEFAULT_PORT), "Port to listen on for intramesh queries")
			("is_writable", po::value<bool>(), "Whether or not this server is accepting writes")
			("tcp_bufsz", po::value<int>()->default_value(TCP_BUFFSZ_DEFAULT), "The size of the recieve and send buffers for TCP connections")
			("udp_bufsz", po::value<int>()->default_value(UDP_BUFFSZ_DEFAULT), "The size of the recieve and send buffers for UDP connections")
			("replication_master",  po::value<std::string>()->default_value("none"), "Hostname for the master server this server is replicating from")
			("pull_replication_frequency", po::value<int>()->default_value(DEFAULT_PULL_REPL_FREQUENCY), "Frequency which the master server will be checked for changes in minutes")
			("pull_replication_batch_size", po::value<unsigned int>()->default_value(DEFAULT_PULL_REPL_BATCH_SIZE), "Number of asset requests to make in a single batch. Higher means faster replication at the cost of master server load")
			("pull_replication_start_at", po::value<int>()->default_value(0), "Asset id prefix that pull replication should start from")
			("replication_slave", po::value<std::string>()->default_value("none"), "Slave server that will take change updates from this master")
		;

		//try to load the config file
		boost::mutex::scoped_lock lock(_lock);
		std::ifstream configFile(_configFileName.c_str());

		if (configFile.good()) {
			_vm.reset(new po::variables_map());
			po::store(po::parse_config_file(configFile, desc), *_vm);
		} else {
			throw std::runtime_error("Could not open configuration file '" + _configFileName + "'");
		}

		po::notify(*_vm);
	}

	const Settings::VariablesMapPtr Settings::config() const
	{
		return _vm;
	}

	std::string Settings::replicationMaster() const
	{
		boost::mutex::scoped_lock lock(_lock);

		string masterSetting = (*_vm)["replication_master"].as<string>();

		if (masterSetting == "none" || masterSetting == "") {
			return string("");
		}

		return masterSetting;
	}

	std::string Settings::replicationSlave() const
	{
		boost::mutex::scoped_lock lock(_lock);

		string slaveSetting = (*_vm)["replication_slave"].as<string>();

		if (slaveSetting == "none" || slaveSetting == "") {
			return string("");
		}

		return slaveSetting;
	}

	std::vector<IntraMeshPeerEntry> Settings::intraMeshPeers() const
	{
		boost::mutex::scoped_lock lock(_lock);
		vector<IntraMeshPeerEntry> peers;

		string peerSetting = (*_vm)["intramesh_peers"].as<string>();

		if (peerSetting == "none") {
			return peers;
		}

		vector<string> hosts;
		boost::split(hosts, peerSetting, boost::is_any_of(","));

		//HOST:IM_PORT:AS_PORT,HOST:IM_PORT:AS_PORT
		BOOST_FOREACH(const string& host, hosts)
		{
			vector<string> hostAndPorts;
			boost::split(hostAndPorts, host, boost::is_any_of(":"));

			if (hostAndPorts.size() == 3) {
				IntraMeshPeerEntry entry;
				entry.Host = hostAndPorts[0];
				entry.IntraMeshPort = boost::lexical_cast<unsigned short>(hostAndPorts[1]);
				entry.AssetServicePort = boost::lexical_cast<unsigned short>(hostAndPorts[2]);

				peers.push_back(entry);
			}
		}

		return peers;
	}

	boost::program_options::variable_value Settings::get(const std::string& settingName) const
	{
		boost::mutex::scoped_lock lock(_lock);
		boost::program_options::variable_value val = (*_vm)[settingName];

		return val;
	}

	std::pair<std::string, unsigned short> Settings::ParseHostPortPair(const std::string& hpp)
	{
		vector<string> hostAndPorts;
		boost::split(hostAndPorts, hpp, boost::is_any_of(":"));

		if (hostAndPorts.size() != 2) {
			throw std::runtime_error("ParseHostPortPair failed, entry must be in the format HOST:PORT");
		}

		return std::make_pair(hostAndPorts[0], boost::lexical_cast<unsigned short>(hostAndPorts[1]));
	}

	boost::asio::ip::tcp::endpoint Settings::ParseHostPortPairToTcpEndpoint(const std::string& hpp)
	{
		std::pair<std::string, unsigned short> hostAndPorts(ParseHostPortPair(hpp));

		boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(hostAndPorts.first), hostAndPorts.second);
		return ep;
	}
}

ext: cpp


text:
/*
 * signals.cpp
 *
 *  Created on: Apr 9, 2015
 *      Author: sujiwo
 */

#include <iostream>
#include <ros/ros.h>
#include "Rate.h"
#include "vector_map.h"
#include <tf/tf.h>
#include <tf/transform_listener.h>
#include <sensor_msgs/CameraInfo.h>
#include <geometry_msgs/TwistStamped.h>
#include <geometry_msgs/Pose.h>
#include <signal.h>
#include <cstdio>
#include "Math.h"
#include <Eigen/Eigen>
#include "road_wizard/Signals.h"
#include <runtime_manager/adjust_xy.h>

static std::string camera_id_str;

static constexpr uint32_t SUBSCRIBE_QUEUE_SIZE = 1000;

static int adjust_proj_x = 0;
static int adjust_proj_y = 0;

typedef struct {
  double thiX;
  double thiY;
  double thiZ;
} Angle;

static VectorMap vmap;
static Angle cameraOrientation; // camera orientation = car's orientation

static Eigen::Vector3f position;
static Eigen::Quaternionf orientation;
static  float fx,
  fy,
  imageWidth,
  imageHeight,
  cx,
  cy;
static tf::StampedTransform trf;

#define SignalLampRadius 0.3

/* Callback function to shift projection result */
void adjust_xyCallback (const runtime_manager::adjust_xy::ConstPtr& config_msg)
{
  adjust_proj_x = config_msg->x;
  adjust_proj_y = config_msg->y;
}

void cameraInfoCallback (const sensor_msgs::CameraInfo::ConstPtr camInfoMsg)
{
  fx = static_cast<float>(camInfoMsg->P[0]);
  fy = static_cast<float>(camInfoMsg->P[5]);
  imageWidth = camInfoMsg->width;
  imageHeight = camInfoMsg->height;
  cx = static_cast<float>(camInfoMsg->P[2]);
  cy = static_cast<float>(camInfoMsg->P[6]);
}

/* convert degree value into 0 to 360 range */
static double setDegree0to360(double val)
{
  if (val < 0.0f) {
    return (val + 360.0f);
  }
  else if (360.0f < val) {
    return (val - 360.0f);
  }

  return val;
}

static void get_cameraRollPitchYaw(double* roll,
                                   double* pitch,
                                   double* yaw)
{
  geometry_msgs::Pose cameraPose;
  cameraPose.position.x    = (double)(position.x());
  cameraPose.position.y    = (double)(position.y());
  cameraPose.position.z    = (double)(position.z());
  cameraPose.orientation.x = (double)(orientation.x());
  cameraPose.orientation.y = (double)(orientation.y());
  cameraPose.orientation.z = (double)(orientation.z());
  cameraPose.orientation.w = (double)(orientation.w());

  tf::Quaternion quat;

  tf::quaternionMsgToTF(cameraPose.orientation, quat);
  tf::Matrix3x3(quat).getRPY(*roll, *pitch, *yaw);

  /* convert from radian to degree */
  *roll  = setDegree0to360(*roll  * 180.0f / M_PI);
  *pitch = setDegree0to360(*pitch * 180.0f / M_PI);
  *yaw   = setDegree0to360(*yaw   * 180.0f / M_PI);
}

/*
  check if lower < val < upper
  This function also considers circulation
*/
static bool isRange(const double lower, const double upper, const double val)
{
  if (lower <= upper) {
    if (lower < val && val < upper) {
      return true;
    }
  }
  else {
    if (val < upper || lower < val) {
      return true;
    }
  }

  return false;
}

void getTransform (Eigen::Quaternionf &ori, Point3 &pos)
{
  static tf::TransformListener listener;

  // target_frame    source_frame
  ros::Time now = ros::Time();
  listener.waitForTransform (camera_id_str, "map", now, ros::Duration(10.0));
  listener.lookupTransform (camera_id_str, "map", now, trf);

  tf::Vector3 &p = trf.getOrigin();
  tf::Quaternion o = trf.getRotation();
  pos.x()=p.x(); pos.y()=p.y(); pos.z()=p.z();
  ori.w()=o.w(); ori.x()=o.x(); ori.y()=o.y(); ori.z()=o.z();
}

Point3 transform (const Point3 &psrc, tf::StampedTransform &tfsource)
{
  tf::Vector3 pt3 (psrc.x(), psrc.y(), psrc.z());
  tf::Vector3 pt3s = tfsource * pt3;
  return Point3 (pt3s.x(), pt3s.y(), pt3s.z());
}

/*
 * Project a point from world coordinate to image plane
 */
bool project2 (const Point3 &pt, int &u, int &v, bool useOpenGLCoord=false)
{
  float nearPlane = 1.0;
  float farPlane = 200.0;
  Point3 _pt = transform (pt, trf);
  float _u = _pt.x()*fx/_pt.z() + cx;
  float _v = _pt.y()*fy/_pt.z() + cy;

  u = static_cast<int>(_u);
  v = static_cast<int>(_v);
  if ( u < 0 || imageWidth < u || v < 0 || imageHeight < v || _pt.z() < nearPlane || farPlane < _pt.z() ) {
    u = -1, v = -1;
    return false;
  }

  if (useOpenGLCoord) {
    v = imageHeight - v;
  }

  return true;
}

void echoSignals2 (ros::Publisher &pub, bool useOpenGLCoord=false)
{
  int countPoint = 0;
  road_wizard::Signals signalsInFrame;

  for (unsigned int i=1; i<=vmap.signals.size(); i++) {
    Signal signal = vmap.signals[i];
    int pid = vmap.vectors[signal.vid].pid;

    Point3 signalcenter = vmap.getPoint(pid);
    Point3 signalcenterx (signalcenter.x(), signalcenter.y(), signalcenter.z()+SignalLampRadius);

    int u, v;
    if (project2 (signalcenter, u, v, useOpenGLCoord) == true) {
      countPoint++;
      // std::cout << u << ", " << v << ", " << std::endl;

      int radius;
      int ux, vx;
      project2 (signalcenterx, ux, vx, useOpenGLCoord);
      radius = (int)distance (ux, vx, u, v);

      road_wizard::ExtractedPosition sign;
      sign.signalId = signal.id;

      sign.u = u + adjust_proj_x; // shift project position by configuration value from runtime manager
      sign.v = v + adjust_proj_y; // shift project position by configuration value from runtime manager

      sign.radius = radius;
      sign.x = signalcenter.x(), sign.y = signalcenter.y(), sign.z = signalcenter.z();
      sign.hang = vmap.vectors[signal.vid].hang; // hang is expressed in [0, 360] degree
      sign.type = signal.type, sign.linkId = signal.linkid;
      sign.plId = signal.plid;

      /* convert signal's horizontal angle to yaw */
      double reversed_signalYaw = setDegree0to360(sign.hang + 180.0f);

      get_cameraRollPitchYaw(&cameraOrientation.thiX,
                             &cameraOrientation.thiY,
                             &cameraOrientation.thiZ);

      // std::cout << "signal : " << reversed_signalYaw << ", car : " << cameraOrientation.thiZ << std::endl;

      /*
        check whether this signal is oriented to the camera
        interested signals have below condition orientation:
        (camera_orientation - 70deg) < (signal_orientation + 180deg) < (camera_orientatin + 70deg)
      */
      double conditionRange_lower = setDegree0to360(cameraOrientation.thiZ - 70);
      double conditionRange_upper = setDegree0to360(cameraOrientation.thiZ + 70);

      // std::cout << "lower: " << conditionRange_lower << ", upper: " << conditionRange_upper << std::endl;

      if (isRange(conditionRange_lower, conditionRange_upper, reversed_signalYaw)) {
        signalsInFrame.Signals.push_back (sign);
      }
    }
  }

  signalsInFrame.header.stamp = ros::Time::now();
  pub.publish (signalsInFrame);

  // printf ("There are %d out of %u signals in frame\n", countPoint, static_cast<unsigned int>(vmap.signals.size()));
}

void interrupt (int s)
{
  ros::shutdown();
  exit(1);
}

int main (int argc, char *argv[])
{

  ros::init(argc, argv, "feat_proj", ros::init_options::NoSigintHandler);
  ros::NodeHandle rosnode;
  ros::NodeHandle private_nh("~");
  std::string cameraInfo_topic_name;
  private_nh.param<std::string>("camera_info_topic", cameraInfo_topic_name, "/camera/camera_info");

  /* get camera ID */
  camera_id_str = cameraInfo_topic_name;
  camera_id_str.erase(camera_id_str.find("/camera/camera_info"));
  if (camera_id_str == "/") {
    camera_id_str = "camera";
  }

  /* load vector map */
  ros::Subscriber sub_point     = rosnode.subscribe("vector_map_info/point_class",
                                                    SUBSCRIBE_QUEUE_SIZE,
                                                    &VectorMap::load_points,
                                                    &vmap);
  ros::Subscriber sub_line      = rosnode.subscribe("vector_map_info/line_class",
                                                    SUBSCRIBE_QUEUE_SIZE,
                                                    &VectorMap::load_lines,
                                                    &vmap);
  ros::Subscriber sub_lane      = rosnode.subscribe("vector_map_info/lane",
                                                    SUBSCRIBE_QUEUE_SIZE,
                                                    &VectorMap::load_lanes,
                                                    &vmap);
  ros::Subscriber sub_vector    = rosnode.subscribe("vector_map_info/vector_class",
                                                    SUBSCRIBE_QUEUE_SIZE,
                                                    &VectorMap::load_vectors,
                                                    &vmap);
  ros::Subscriber sub_signal    = rosnode.subscribe("vector_map_info/signal",
                                                    SUBSCRIBE_QUEUE_SIZE,
                                                    &VectorMap::load_signals,
                                                    &vmap);
  ros::Subscriber sub_whiteline = rosnode.subscribe("vector_map_info/white_line",
                                                    SUBSCRIBE_QUEUE_SIZE,
                                                    &VectorMap::load_whitelines,
                                                    &vmap);
  ros::Subscriber sub_dtlane    = rosnode.subscribe("vector_map_info/dtlane",
                                                    SUBSCRIBE_QUEUE_SIZE,
                                                    &VectorMap::load_dtlanes,
                                                    &vmap);

  /* wait until loading all vector map is completed */
  ros::Rate wait_rate(1);
  while(vmap.points.empty() || vmap.lines.empty() || vmap.whitelines.empty() ||
        vmap.lanes.empty() || vmap.dtlanes.empty() || vmap.vectors.empty() || vmap.signals.empty())
    {
      ros::spinOnce();
      wait_rate.sleep();
    }

  vmap.loaded = true;
  std::cout << "all vector map loaded." << std::endl;

  ros::Subscriber cameraInfoSubscriber = rosnode.subscribe (cameraInfo_topic_name, 100, cameraInfoCallback);
  ros::Subscriber adjust_xySubscriber  = rosnode.subscribe("/config/adjust_xy", 100, adjust_xyCallback);
  //  ros::Subscriber ndtPoseSubscriber    = rosnode.subscribe("/current_pose", 10, ndtPoseCallback);
  ros::Publisher  signalPublisher      = rosnode.advertise <road_wizard::Signals> ("roi_signal", 100);
  signal (SIGINT, interrupt);

  Rate loop (25);
  while (true) {

    ros::spinOnce();

    try {
      getTransform (orientation, position);
    } catch (tf::TransformException &exc) {
    }

    echoSignals2 (signalPublisher, false);
    loop.sleep();
  }

}

max_stars_repo_name: sibojia/Autoware

max_issues_repo_name: sibojia/Autoware

max_forks_repo_name: sibojia/Autoware


text:
/***************************************************************************
 * NASA Glenn Research Center, Cleveland, OH
 * Released under the NASA Open Source Agreement (NOSA)
 * May  2021
 *
 ****************************************************************************
 */
#ifndef _WIN32
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#include <fcntl.h>
#endif

#include "BundleStorageManagerAsio.h"
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/make_shared.hpp>
#include <boost/make_unique.hpp>
#ifdef _WIN32
#include <windows.h> //must be included after boost
#endif

BundleStorageManagerAsio::BundleStorageManagerAsio() : BundleStorageManagerAsio("storageConfig.json") {}

BundleStorageManagerAsio::BundleStorageManagerAsio(const std::string & jsonConfigFileName) : BundleStorageManagerAsio(StorageConfig::CreateFromJsonFile(jsonConfigFileName)) {
    if (!m_storageConfigPtr) {
        std::cerr << "cannot open storage json config file: " << jsonConfigFileName << std::endl;
        hdtn::Logger::getInstance()->logError("storage", "cannot open storage json config file: " + jsonConfigFileName);
        return;
    }
}

BundleStorageManagerAsio::BundleStorageManagerAsio(const StorageConfig_ptr & storageConfigPtr) :
    BundleStorageManagerBase(storageConfigPtr),

    m_workPtr(boost::make_unique< boost::asio::io_service::work>(m_ioService)),
    m_asioHandlePtrsVec(M_NUM_STORAGE_DISKS),
    m_diskOperationInProgressVec(M_NUM_STORAGE_DISKS)
{

}

BundleStorageManagerAsio::~BundleStorageManagerAsio() {
    if (m_ioServiceThreadPtr) {
        m_workPtr.reset(); //erase the work object (destructor is thread safe) so that io_service thread will exit when it runs out of work
        m_ioServiceThreadPtr->join();
        m_ioServiceThreadPtr.reset(); //delete it
    }

    for (unsigned int diskId = 0; diskId < M_NUM_STORAGE_DISKS; ++diskId) {
        if (m_asioHandlePtrsVec[diskId]) {
            m_asioHandlePtrsVec[diskId]->close();
            m_asioHandlePtrsVec[diskId].reset(); //delete it
        }
    }

}

void BundleStorageManagerAsio::Start() {
    if (m_storageConfigPtr) {
        for (unsigned int diskId = 0; diskId < M_NUM_STORAGE_DISKS; ++diskId) {
            const char * const filePath = m_filePathsAsStringVec[diskId].c_str();
            std::cout << ((m_successfullyRestoredFromDisk) ? "reopening " : "creating ") << filePath << "\n";
#ifdef _WIN32
            //
            //https://docs.microsoft.com/en-us/windows/win32/fileio/synchronous-and-asynchronous-i-o
            //In synchronous file I/O, a thread starts an I/O operation and immediately enters a wait state until the I/O request has completed.
            //A thread performing asynchronous file I/O sends an I/O request to the kernel by calling an appropriate function.
            //If the request is accepted by the kernel, the calling thread continues processing another job until the kernel signals to
            //the thread that the I/O operation is complete. It then interrupts its current job and processes the data from the I/O operation as necessary.
            //Asynchronous I/O is also referred to as overlapped I/O.
            HANDLE hFile = CreateFile(filePath,                // name of the file
                GENERIC_READ | GENERIC_WRITE,          // open for reading and writing
                0,                      // do not share
                NULL,                   // default security
                //CREATE_ALWAYS : Creates a new file, always. If the specified file exists and is writable, the function overwrites the file
                //OPEN_EXISTING : Opens a file or device, only if it exists.  If the specified file or device does not exist, the function fails and the last - error code is set to ERROR_FILE_NOT_FOUND(2).
                (m_successfullyRestoredFromDisk) ? OPEN_EXISTING : CREATE_ALWAYS,
                FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,  // normal file
                NULL);                  // no attr. template

            if (hFile == INVALID_HANDLE_VALUE) {
                std::cerr << "error opening " << filePath << std::endl;
                return;
            }
            //
            //FILE * fileHandle = (m_successfullyRestoredFromDisk) ? fopen(filePath, "r+bR") : fopen(filePath, "w+bR");
            m_asioHandlePtrsVec[diskId] = boost::make_unique<boost::asio::windows::random_access_handle>(m_ioService, hFile);
#else
            int file_desc = open(filePath, (m_successfullyRestoredFromDisk) ? (O_RDWR|O_LARGEFILE) : (O_CREAT|O_RDWR|O_TRUNC|O_LARGEFILE), DEFFILEMODE);
            if(file_desc < 0) {
                std::cerr << "error opening " << filePath << std::endl;
                return;
            }
            m_asioHandlePtrsVec[diskId] = boost::make_unique<boost::asio::posix::stream_descriptor>(m_ioService, file_desc);
#endif
            m_diskOperationInProgressVec[diskId] = false;
        }
        m_ioServiceThreadPtr = boost::make_unique<boost::thread>(boost::bind(&boost::asio::io_service::run, &m_ioService));
    }
}

void BundleStorageManagerAsio::TryDiskOperation_Consume_NotThreadSafe(const unsigned int diskId) {
    if (!m_diskOperationInProgressVec[diskId]) {
        CircularIndexBufferSingleProducerSingleConsumerConfigurable & cb = m_circularIndexBuffersVec[diskId];
        const unsigned int consumeIndex = cb.GetIndexForRead(); //store the volatile

        if (consumeIndex != UINT32_MAX) { //if not empty
            m_diskOperationInProgressVec[diskId] = true;

            segment_id_t * const circularBufferSegmentIdsPtr = &m_circularBufferSegmentIdsPtr[diskId * CIRCULAR_INDEX_BUFFER_SIZE];

            const segment_id_t segmentId = circularBufferSegmentIdsPtr[consumeIndex];
            volatile boost::uint8_t * const readFromStorageDestPointer = m_circularBufferReadFromStoragePointers[diskId * CIRCULAR_INDEX_BUFFER_SIZE + consumeIndex];

            const bool isWriteToDisk = (readFromStorageDestPointer == NULL);
            if (segmentId == UINT32_MAX) {
                std::cout << "error segmentId is max\n";
                hdtn::Logger::getInstance()->logError("storage", "Error segmentId is max");
                //continue;
            }

            const boost::uint64_t offsetBytes = static_cast<boost::uint64_t>(segmentId / M_NUM_STORAGE_DISKS) * SEGMENT_SIZE;

#ifdef _WIN32

#else
            lseek64(m_asioHandlePtrsVec[diskId]->native_handle(), offsetBytes, SEEK_SET);
#endif

            if (isWriteToDisk) {
                boost::uint8_t * const circularBufferBlockDataPtr = &m_circularBufferBlockDataPtr[diskId * CIRCULAR_INDEX_BUFFER_SIZE * SEGMENT_SIZE];
                boost::uint8_t * const data = &circularBufferBlockDataPtr[consumeIndex * SEGMENT_SIZE]; //expected data for testing when reading
#ifdef _WIN32
                boost::asio::async_write_at(*m_asioHandlePtrsVec[diskId], offsetBytes,
#else
                boost::asio::async_write(*m_asioHandlePtrsVec[diskId],
#endif
                    boost::asio::buffer(data, SEGMENT_SIZE),
                    boost::bind(&BundleStorageManagerAsio::HandleDiskOperationCompleted, this,
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred,
                        diskId, consumeIndex, false));

            }
            else { //read from disk
#ifdef _WIN32
                boost::asio::async_read_at(*m_asioHandlePtrsVec[diskId], offsetBytes,
#else
                boost::asio::async_read(*m_asioHandlePtrsVec[diskId],
#endif
                    boost::asio::buffer((void*)readFromStorageDestPointer, SEGMENT_SIZE),
                    boost::bind(&BundleStorageManagerAsio::HandleDiskOperationCompleted, this,
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred,
                        diskId, consumeIndex, true));
            }
        }
    }
}

//virtual function to be called immediately after a disk's circular buffer CommitWrite();
void BundleStorageManagerAsio::NotifyDiskOfWorkToDo_ThreadSafe(const unsigned int diskId) {
    boost::asio::post(m_ioService, boost::bind(&BundleStorageManagerAsio::TryDiskOperation_Consume_NotThreadSafe, this, diskId));
}

void BundleStorageManagerAsio::HandleDiskOperationCompleted(const boost::system::error_code& error, std::size_t bytes_transferred, const unsigned int diskId, const unsigned int consumeIndex, const bool wasReadOperation) {
    m_diskOperationInProgressVec[diskId] = false;
    if (error) {
        std::cerr << "error in BundleStorageManagerMT::HandleDiskOperationCompleted: " << error.message() << std::endl;
    }
    else if (bytes_transferred != SEGMENT_SIZE) {
        std::cerr << "error in BundleStorageManagerMT::HandleDiskOperationCompleted: bytes_transferred(" << bytes_transferred << ") != SEGMENT_SIZE(" << SEGMENT_SIZE << ")" << std::endl;
    }
    else {
        if (wasReadOperation) {
            volatile bool * const isReadCompletedPointer = m_circularBufferIsReadCompletedPointers[diskId * CIRCULAR_INDEX_BUFFER_SIZE + consumeIndex];
            *isReadCompletedPointer = true;
        }
        CircularIndexBufferSingleProducerSingleConsumerConfigurable & cb = m_circularIndexBuffersVec[diskId];
        cb.CommitRead();
        m_conditionVariableMainThread.notify_one();
        TryDiskOperation_Consume_NotThreadSafe(diskId);
    }
}


text: #include <boost/log/sinks.hpp>

ext: hpp

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
/*
* Optimizer class to adjust parameters of a ts::Model.
*/

#pragma once

#include <Eigen/Dense>

#include "autodiff.hpp"
#include "model.hpp"

#include <vector>
#include <iostream>

namespace ts {
	template <typename T> class TrainingData;

	template <typename T> class GaElement;
	template <typename T> class GradientAccumulator;

	template <typename T> class Optimizer;
	template <typename T> class GradientDescentOptimizer;
	template <typename T> class AdamOptimizer;
};

	// ts::TrainingData
	// (helper class containing both input data and its expected result)

template <typename T>
class ts::TrainingData {
private:

public:
	TrainingData(
		Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> newInput,
		Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> newExpected
	);

	Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> input;
	Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> expected;
};

	// ts::GaElement
	// (Gradient accumulator element that keeps summed derivatives and index of
	// an optimized node/tensor)

template <typename T>
class ts::GaElement {
private:
	GaElement(ts::Tensor<T> * inputTensor);

	Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> gradSum;
	unsigned index;	// in the ts::WengertList / ts::Gradient (see WARNING below)

	void reset();

public:

	friend ts::GradientAccumulator<T>;
	friend ts::Optimizer<T>;
	friend ts::GradientDescentOptimizer<T>;
	friend ts::AdamOptimizer<T>;
};

	// ts::GradientAccumulator
	// A collection of accumulated gradient elements for all optimizable tensors
	// of a model.
	// WARNING The indices in this array are completely independent of those in
	// the Wengert List, since some nodes may not be input or optimizable.

template <typename T>
class ts::GradientAccumulator {
private:
	GradientAccumulator();
	GradientAccumulator(ts::Model<T> &model);

	std::vector<ts::GaElement<T>> elements = {};

	void reset();
	void increment(ts::Gradient<T> &gradient);
	void updateTensor(
		ts::Model<T> &model, unsigned i,
		Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> value
	);
	void clear();

public:

	friend ts::Optimizer<T>;
	friend ts::GradientDescentOptimizer<T>;
	friend ts::AdamOptimizer<T>;
};

	// ts::Optimizer

template <typename T>
class ts::Optimizer {
private:

protected:
	ts::GradientAccumulator<T> gradAccumulator;

	void resetGradAccumulator();	// Set values to 0
	void setupGradAccumulator(ts::Model<T> &model);	// Generate 0-filled elements

	// Dependent of optimizer type. Applies and the accumulated gradient.
	virtual void updateModel(ts::Model<T> &model, unsigned batchSize) = 0;

public:
	Optimizer();

	ts::Tensor<T> (*normFunction)(const ts::Tensor<T>&) = &(ts::squaredNorm);

	unsigned epochs = 1;

	// Optimizes the model by running its compute() method on the batches data
	virtual std::vector<std::vector<std::vector< T >>> run(
		ts::Model<T> &model, std::vector<std::vector< ts::TrainingData<T> >> &batches
	) = 0;

};

	// ts::GradientDescentOptimizer

template <typename T>
class ts::GradientDescentOptimizer : public ts::Optimizer<T> {
private:
	void updateModel(ts::Model<T> &model, unsigned batchSize);

public:
	using ts::Optimizer<T>::Optimizer;

	GradientDescentOptimizer(T newLearningRate);

	T learningRate = 0.1;

	std::vector<std::vector<std::vector< T >>> run(
		ts::Model<T> &model, std::vector<std::vector< ts::TrainingData<T> >> &batches
	);
};

	// ts::AdamOptimizer

template <typename T>
class ts::AdamOptimizer : public ts::Optimizer<T> {
private:
	void updateModel(ts::Model<T> &model, unsigned batchSize);

	// Moment estimates, initialized at run time
	std::vector<Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic>> m = {};
	std::vector<Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic>> v = {};

	// NOTE For now, all mHat and vHat are stored in a vector. It would be
	// possible to only use one Eigen::Array, and to resize it every time
	// instead of storing all values.
	// -> This is a tradeoff between time and memory, it would be interesting
	// to benchmark both methods in the future.
	std::vector<Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic>> mHat = {};
	std::vector<Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic>> vHat = {};

	void initMomentEstimates(
		std::vector< std::shared_ptr<ts::Node<T>> > nodes
	);

	void computeIncrement(
		std::vector< Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> >& derivatives,
		std::vector<ts::GaElement<T>>& elements
	);

	T decayedBeta1;
	T decayedBeta2;

public :
	using ts::Optimizer<T>::Optimizer;

	AdamOptimizer(
		T newAlpha,
		T newBeta1, T newBeta2,
		T newEpsilon
	);

	// Default values set according to original paper
	T alpha = 0.001;
	T beta1 = 0.9;
	T beta2 = 0.999;
	T epsilon = 0.00000001;

	std::vector<std::vector<std::vector< T >>> run(
		ts::Model<T> &model, std::vector<std::vector< ts::TrainingData<T> >> &batches
	);
};

max_stars_repo_name: PurplePachyderm/tensorslow

max_issues_repo_name: PurplePachyderm/tensorslow

max_forks_repo_name: PurplePachyderm/tensorslow


text:
//
// Copyright (c) 2016 - 2017 Mesh Consultants Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#include "Vector_ColorPalette.h"
#include "ColorDefs.h"
#include <assert.h>

#include <Urho3D/Math/Vector3.h>

#include <Eigen/Core>

using namespace Urho3D;

String Vector_ColorPalette::iconTexture = "Textures/Icons/Vector_ColorPalette.png";

Vector_ColorPalette::Vector_ColorPalette(Context* context) : IoComponentBase(context, 3, 1)
{
	SetName("ClosePoint");
	SetFullName("Closest Point");
	SetDescription("Which point in list is closest");
	SetGroup(IoComponentGroup::VECTOR);
	SetSubgroup("Point");

	inputSlots_[0]->SetName("StartColor");
	inputSlots_[0]->SetVariableName("C");
	inputSlots_[0]->SetDescription("Start Color");
	inputSlots_[0]->SetVariantType(VariantType::VAR_COLOR);
	inputSlots_[0]->SetDefaultValue(BRIGHT_ORANGE);
	inputSlots_[0]->DefaultSet();

	inputSlots_[1]->SetName("Length");
	inputSlots_[1]->SetVariableName("N");
	inputSlots_[1]->SetDescription("Number of colors to generate");
	inputSlots_[1]->SetVariantType(VariantType::VAR_FLOAT);
	inputSlots_[1]->SetDataAccess(DataAccess::ITEM);
	inputSlots_[1]->SetDefaultValue(10);
	inputSlots_[1]->DefaultSet();

	inputSlots_[2]->SetName("Variance");
	inputSlots_[2]->SetVariableName("V");
	inputSlots_[2]->SetDescription("Variance of colors");
	inputSlots_[2]->SetVariantType(VariantType::VAR_FLOAT);
	inputSlots_[2]->SetDataAccess(DataAccess::ITEM);
	inputSlots_[2]->SetDefaultValue(0.1f);
	inputSlots_[2]->DefaultSet();

	outputSlots_[0]->SetName("ResultingColors");
	outputSlots_[0]->SetVariableName("R");
	outputSlots_[0]->SetDescription("Resulting Colors");
	outputSlots_[0]->SetVariantType(VariantType::VAR_VARIANTVECTOR);
	outputSlots_[0]->SetDataAccess(DataAccess::LIST);

}

void Vector_ColorPalette::SolveInstance(
	const Vector<Variant>& inSolveInstance,
	Vector<Variant>& outSolveInstance
)
{
	Color startCol = inSolveInstance[0].GetColor();
	int numCols = inSolveInstance[1].GetInt();
	float variance = inSolveInstance[2].GetFloat();

	VariantVector colsOut;
	Urho3D::SetRandomSeed((unsigned)startCol.SumRGB());
	for (int i = 0; i < numCols; i++)
	{
		float r = Urho3D::Random(startCol.r_ - variance, startCol.r_ + variance);
		float g = Urho3D::Random(startCol.g_ - variance, startCol.g_ + variance);
		float b = Urho3D::Random(startCol.b_ - variance, startCol.b_ + variance);
		float a = Urho3D::Random(startCol.a_ - variance, startCol.a_ + variance);

		colsOut.Push(Color(r, g, b, a));
	}

	outSolveInstance[0] = colsOut;
}


text:
// This example shows how to sort structs using complex multiple part keys using
// string_sort.
//
//  Copyright Steven Ross 2009-2014.
//
// 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)

//  See http://www.boost.org/libs/sort for library home page.

#include <boost/sort/spreadsort/string_sort.hpp>
#include <boost/sort/spreadsort/float_sort.hpp>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
using std::string;
using namespace boost::sort::spreadsort;

//[generalized_functors
struct DATA_TYPE {
  time_t birth;
  float net_worth;
  string first_name;
  string last_name;
};

static const int birth_size = sizeof(time_t);
static const int first_name_offset = birth_size + sizeof(float);
static const boost::uint64_t base_mask = 0xff;

struct lessthan {
  inline bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const {
    if (x.birth != y.birth) {
      return x.birth < y.birth;
    }
    if (x.net_worth != y.net_worth) {
      return x.net_worth < y.net_worth;
    }
    if (x.first_name != y.first_name) {
      return x.first_name < y.first_name;
    }
    return x.last_name < y.last_name;
  }
};

struct bracket {
  inline unsigned char operator()(const DATA_TYPE &x, size_t offset) const {
    // Sort date as a signed int, returning the appropriate byte.
    if (offset < birth_size) {
      const int bit_shift = 8 * (birth_size - offset - 1);
      unsigned char result = (x.birth & (base_mask << bit_shift)) >> bit_shift;
      // Handling the sign bit.  Unnecessary if the data is always positive.
      if (offset == 0) {
        return result ^ 128;
      }

      return result;
    }

    // Sort a signed float.  This requires reversing the order of negatives
    // because of the way floats are represented in bits.
    if (offset < first_name_offset) {
      const int bit_shift = 8 * (first_name_offset - offset - 1);
      unsigned key = float_mem_cast<float, unsigned>(x.net_worth);
      unsigned char result = (key & (base_mask << bit_shift)) >> bit_shift;
      // Handling the sign.
      if (x.net_worth < 0) {
        return 255 - result;
      }
      // Increasing positives so they are higher than negatives.
      if (offset == birth_size) {
        return 128 + result;
      }

      return result;
    }

    // Sort a string that is before the end.  This approach supports embedded
    // nulls.  If embedded nulls are not required, then just delete the "* 2"
    // and the inside of the following if just becomes:
    // return x.first_name[offset - first_name_offset];
    const unsigned first_name_end_offset =
      first_name_offset + x.first_name.size() * 2;
    if (offset < first_name_end_offset) {
      int char_offset = offset - first_name_offset;
      // This signals that the string continues.
      if (!(char_offset & 1)) {
        return 1;
      }
      return x.first_name[char_offset >> 1];
    }

    // This signals that the string has ended, so that shorter strings come
    // before longer ones.
    if (offset == first_name_end_offset) {
      return 0;
    }

    // The final string needs no special consideration.
    return x.last_name[offset - first_name_end_offset - 1];
  }
};

struct getsize {
  inline size_t operator()(const DATA_TYPE &x) const {
    return first_name_offset + x.first_name.size() * 2 + 1 +
      x.last_name.size();
  }
};
//] [/generalized_functors]

//Pass in an argument to test std::sort
int main(int argc, const char ** argv) {
  std::ifstream indata;
  std::ofstream outfile;
  bool stdSort = false;
  unsigned loopCount = 1;
  for (int u = 1; u < argc; ++u) {
    if (std::string(argv[u]) == "-std")
      stdSort = true;
    else
      loopCount = atoi(argv[u]);
  }
  double total = 0.0;
  //Run multiple loops, if requested
  std::vector<DATA_TYPE> array;
  for (unsigned u = 0; u < loopCount; ++u) {
    indata.open("input.txt", std::ios_base::in | std::ios_base::binary);
    if (indata.bad()) {
      printf("input.txt could not be opened\n");
      return 1;
    }

    // Read in the data.
    DATA_TYPE inval;
    while (!indata.eof() ) {
      indata >> inval.first_name;
      indata >> inval.last_name;
      indata.read(reinterpret_cast<char *>(&(inval.birth)), birth_size);
      indata.read(reinterpret_cast<char *>(&(inval.net_worth)), sizeof(float));
      // Handling nan.
      if (inval.net_worth != inval.net_worth) {
        inval.net_worth = 0;
      }
      if (indata.eof())
        break;
      array.push_back(inval);
    }
    indata.close();

    // Sort the data.
    clock_t start, end;
    double elapsed;
    start = clock();
    if (stdSort) {
      std::sort(array.begin(), array.end(), lessthan());
    } else {
//[generalized_functors_call
      string_sort(array.begin(), array.end(), bracket(), getsize(), lessthan());
//] [/generalized_functors_call]
    }
    end = clock();
    elapsed = static_cast<double>(end - start);
    if (stdSort) {
      outfile.open("standard_sort_out.txt", std::ios_base::out |
                   std::ios_base::binary | std::ios_base::trunc);
    } else {
      outfile.open("boost_sort_out.txt", std::ios_base::out |
                   std::ios_base::binary | std::ios_base::trunc);
    }
    if (outfile.good()) {
      for (unsigned u = 0; u < array.size(); ++u)
        outfile << array[u].birth << " " << array[u].net_worth << " "
                << array[u].first_name << " " << array[u].last_name << "\n";
      outfile.close();
    }
    total += elapsed;
    array.clear();
  }
  if (stdSort) {
    printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  } else {
    printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  }
  return 0;
}


text:
// Copyright (c) 2016 Klemens D. Morgenstern
//
// 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)

#ifndef BOOST_PROCESS_DETAIL_USED_HANDLES_HPP_
#define BOOST_PROCESS_DETAIL_USED_HANDLES_HPP_

#include <type_traits>
#include <boost/fusion/include/filter_if.hpp>
#include <boost/fusion/include/for_each.hpp>

#if defined(BOOST_POSIX_API)
#include <boost/process/detail/posix/handles.hpp>
#include <boost/process/detail/posix/asio_fwd.hpp>
#else
#include <boost/process/detail/windows/handles.hpp>
#include <boost/process/detail/windows/asio_fwd.hpp>
#endif

namespace boost { namespace process { namespace detail {

struct uses_handles
{
    //If you get an error here, you must add a `get_handles` function that returns a range or a single handle value
    void get_used_handles() const;
};

template<typename T>
struct does_use_handle: std::is_base_of<uses_handles, T> {};

template<typename T>
struct does_use_handle<T&> : std::is_base_of<uses_handles, T> {};

template<typename T>
struct does_use_handle<const T&> : std::is_base_of<uses_handles, T> {};

template<typename Char, typename Sequence>
class executor;

template<typename Func>
struct foreach_handle_invocator
{
    Func & func;
    foreach_handle_invocator(Func & func) : func(func) {}

    template<typename Range>
    void invoke(const Range & range) const
    {
        for (auto handle_ : range)
            func(handle_);

    }
    void invoke(::boost::process::detail::api::native_handle_type handle) const {func(handle);};

    template<typename T>
    void operator()(T & val) const {invoke(val.get_used_handles());}
};

template<typename Executor, typename Function>
void foreach_used_handle(Executor &exec, Function &&func)
{
    boost::fusion::for_each(boost::fusion::filter_if<does_use_handle<boost::mpl::_>>(exec.seq),
                            foreach_handle_invocator<Function>(func));
}

template<typename Executor>
std::vector<::boost::process::detail::api::native_handle_type>
        get_used_handles(Executor &exec)
{
    std::vector<::boost::process::detail::api::native_handle_type> res;
    foreach_used_handle(exec, [&](::boost::process::detail::api::native_handle_type handle){res.push_back(handle);});
    return res;
}

}}}

#endif /* BOOST_PROCESS_DETAIL_USED_HANDLES_HPP_ */

max_stars_repo_name: kindlychung/mediasoup-sfu-cpp

max_issues_repo_name: kindlychung/mediasoup-sfu-cpp

max_forks_repo_name: kindlychung/mediasoup-sfu-cpp


text: #include <boost/proto/functional/fusion/push_back.hpp>