text:
/**
 * Copyright (c) 2018, University Osnabrück
 * 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 University Osnabrück 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 University Osnabrück 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.
 */

/**
 * LVRPointBufferBridge.hpp
 *
 *  @date Feb 6, 2014
 *  @author Thomas Wiemann
 */
#ifndef LVRPOINTBUFFERBRIDGE_HPP_
#define LVRPOINTBUFFERBRIDGE_HPP_

#include "lvr2/display/ColorMap.hpp"
#include "lvr2/io/PointBuffer.hpp"

#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkDoubleArray.h>

#include <boost/shared_ptr.hpp>

namespace lvr2
{

struct Pose;

class LVRPointBufferBridge
{
public:
    LVRPointBufferBridge(PointBufferPtr pointcloud);
    LVRPointBufferBridge(const LVRPointBufferBridge& b);
    virtual ~LVRPointBufferBridge();

    vtkSmartPointer<vtkActor>   getPointCloudActor();
    size_t                      getNumPoints();
    bool                        hasNormals();
    bool                        hasColors();

    void setBaseColor(float r, float g, float b);
    void setPointSize(int pointSize);
    void setOpacity(float opacityValue);
    void setVisibility(bool visible);
    void setNormalsVisibility(bool visible);
    void setColorsVisibility(bool visible);
    /// set the spectral channel for (r, g, b) and set if it is used
    void setSpectralChannels(color<size_t> channels, color<bool> use_channel);
    /// get spectral channel mappings
    void getSpectralChannels(color<size_t> &channels, color<bool> &use_channel) const;
    /// set the gradienttype, desired channel, if the outputcolor should be normalized and if the NDVI should be used instead of the channel
    void setSpectralColorGradient(GradientType gradient, size_t channel, bool normalized = false, bool ndvi = false);
    /// get the gradienttype, channel, normalizend and ndvi flags
    void getSpectralColorGradient(GradientType &gradient, size_t &channel, bool &normalized, bool &useNDVI) const;
    /// switch between spectral mapping and gradient
    void useGradient(bool useGradient);
    /// get the point buffer
    PointBufferPtr getPointBuffer();
    vtkSmartPointer<vtkPolyData> getPolyData();

    vtkSmartPointer<vtkPolyData> getPolyIDData();

private:
    /// update the view with gradient information
    void refreshSpectralGradient();
    /// update the view with channel mappings
    void refreshSpectralChannel();

protected:

    void computePointCloudActor(PointBufferPtr pc);

    vtkSmartPointer<vtkPolyData> m_vtk_polyData;

    //Maybe this is not neaded but
    vtkSmartPointer<vtkPolyData> m_id_polyData;

    vtkSmartPointer<vtkActor>       m_pointCloudActor;
    size_t                          m_numPoints;
    bool                            m_hasNormals;
    bool                            m_hasColors;
    PointBufferPtr                  m_pointBuffer;
    bool                            m_useGradient;
    bool                            m_useNormalizedGradient;
    color<size_t>                   m_spectralChannels;
    color<bool>                     m_useSpectralChannel;
    GradientType                    m_spectralGradient;
    size_t                          m_spectralGradientChannel;
    bool                            m_useNDVI;
    vtkSmartPointer<vtkDoubleArray> m_vtk_normals;
};

typedef boost::shared_ptr<LVRPointBufferBridge> PointBufferBridgePtr;

} /* namespace lvr2 */

#endif /* LVRPOINTBUFFERBRIDGE_HPP_ */

max_stars_repo_name: uos/lvr

max_issues_repo_name: uos/lvr

max_forks_repo_name: uos/lvr


text:
//          Copyright Gavin Band 2008 - 2012.
// 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)

#include <iostream>
#include <string>
#include "stdint.h"
#include <limits>
#include <boost/algorithm/string/replace.hpp>
#include <Eigen/Core>
#include "genfile/snp_data_utils.hpp"
#include "genfile/endianness_utils.hpp"
#include "genfile/VariantDataReader.hpp"
#include "genfile/SNPDataSink.hpp"
#include "genfile/zlib.hpp"
#include "qcdb/DBOutputter.hpp"
#include "components/SNPOutputComponent/SQLiteHaplotypesSNPDataSink.hpp"
#include "qcdb/store_samples_in_db.hpp"

// #define DEBUG_SQLITEHAPLOTYPESSNPDATASINK 1

SQLiteHaplotypesSNPDataSink::SQLiteHaplotypesSNPDataSink(
	qcdb::DBOutputter::UniquePtr outputter,
	genfile::CohortIndividualSource const& samples
):
	m_genotype_field( ":genotypes:" ),
	m_outputter( outputter ),
	m_samples( samples ),
	m_data_i( 0 )
{
	m_outputter->connection().run_statement(
		"CREATE TABLE IF NOT EXISTS Haplotype ( "
		"  analysis_id INT NOT NULL REFERENCES Analysis( id ), "
		"  variant_id INT NOT NULL REFERENCES Variant( id ), "
		"  N INT NOT NULL,"
		"  data BLOB NOT NULL"
		")"
	) ;
	m_outputter->connection().run_statement(
		"CREATE View IF NOT EXISTS HaplotypeView AS "
		"SELECT analysis_id, A.name AS analysis, variant_id, chromosome, position, rsid, alleleA, alleleB, quote( data ) "
		"FROM Haplotype H "
		"INNER JOIN Analysis A "
		"ON A.id == H.analysis_id "
		"INNER JOIN Variant V "
		"ON V.id == H.variant_id"
	) ;
	m_insert_data_stmnt = m_outputter->connection().get_statement(
		"INSERT INTO Haplotype ( analysis_id, variant_id, N, data ) VALUES( ?, ?, ?, ? ) ;"
	) ;
	m_snps.resize( 10000 ) ;
	m_data.resize( 10000 ) ;
}

std::string SQLiteHaplotypesSNPDataSink::get_spec() const {
	return "SQLiteHaplotypesSNPDataSink" ;
}

void SQLiteHaplotypesSNPDataSink::set_genotype_field( std::string field ) {
	m_genotype_field = field ;
}

namespace {
	template< typename Integer, typename Target >
	bool fits_in( Integer const& value ) {
		// This code adapted from code found on StackOverflow
		// here: http://stackoverflow.com/a/17251989/517251.

		// Return false if Integer can hold lower numbers than target and the value is less than the minimum of target
		// Or if Integer can hold larger numbers than Target and the value is larger than the target.
		// Otherwise return true.
		//
		// (I guess the point here is that the first part of each test can be done at compile time.)
		//
		const intmax_t IntegerMin = intmax_t( std::numeric_limits< Integer >::min() ) ;
		const uintmax_t IntegerMax = uintmax_t( std::numeric_limits< Integer >::max() ) ;
		const intmax_t TargetMin = intmax_t( std::numeric_limits< Target >::min() ) ;
		const uintmax_t TargetMax = uintmax_t( std::numeric_limits< Target >::max() ) ;

		bool result = !(
			( ( IntegerMin < TargetMin ) && value < static_cast< Integer >( TargetMin ))
			||
			( ( IntegerMax > TargetMax ) && value > static_cast< Integer >( TargetMax ))
		) ;

		return result ;
	}

	struct HaplotypeWriter: public genfile::VariantDataReader::PerSampleSetter {
		enum Encoding { eUBJSON = 0, eBITPACK = 1 } ; // must take contiguous values starting at zero.

		HaplotypeWriter( std::vector< uint8_t >* compression_buffer ):
			m_compression_buffer( compression_buffer ),
			m_buffer_validity( 2, true ),
			m_ploidy( 0 )
		{
			assert( compression_buffer != 0 ) ;
		}

		~HaplotypeWriter() throw() {}

		void initialise( std::size_t n, std::size_t nAlleles ) {
			assert( nAlleles == 2 ) ;
			m_buffers.resize(2) ;
			m_buf_p.resize(2) ;
			m_buf_end_p.resize(2) ;

			{
				m_buffers[ eUBJSON ].clear() ;
				m_buffers[ eUBJSON ].resize( 9 + ( n * 2 * 10 ) + 2 ) ; // name, plus two brackets per sample, two values up to 9 bytes per sample, two outside brackets.
				m_buf_p[ eUBJSON ] = &(m_buffers[ eUBJSON ][0]) ;
				m_buf_end_p[ eUBJSON ] = &(m_buffers[ eUBJSON ][0]) + m_buffers[ eUBJSON ].size() ;
				std::string const encoding_name = "ubjson" ;
				m_buffers[ eUBJSON ][0] = 's' ;
				++m_buf_p[ eUBJSON ] ;
				m_buf_p[ eUBJSON ] = genfile::write_big_endian_integer( m_buf_p[ eUBJSON ], m_buf_end_p[ eUBJSON ], static_cast< int16_t >( encoding_name.size() )) ;
				m_buf_p[ eUBJSON ] = std::copy( encoding_name.begin(), encoding_name.end(), m_buf_p[ eUBJSON ] ) ;
				// Write the UBJSON array marker
				*(m_buf_p[ eUBJSON ]++) = '[' ;
			}

			{
				m_buffers[ eBITPACK ].clear() ;
				m_buffers[ eBITPACK ].resize( 10 + ( n * 2 ) ) ; // name, plus 2 haplotypes per sample.
				m_buf_p[ eBITPACK ] = &(m_buffers[ eBITPACK ][0]) ;
				m_buf_end_p[ eBITPACK ] = &(m_buffers[ eBITPACK ][0]) + m_buffers[ eBITPACK ].size() ;
				std::string const encoding_name = "bitpack" ;
				m_buffers[ eBITPACK ][0] = 's' ;
				++m_buf_p[ eBITPACK ] ;
				m_buf_p[ eBITPACK ] = genfile::write_big_endian_integer( m_buf_p[ eBITPACK ], m_buf_end_p[ eBITPACK ], static_cast< int16_t >( encoding_name.size() )) ;
				m_buf_p[ eBITPACK ] = std::copy( encoding_name.begin(), encoding_name.end(), m_buf_p[ eBITPACK ] ) ;
				m_bitpack_index = 0 ;
			}
		}

		bool set_sample( std::size_t i ) {
			if( i > 0 ) {
				*(m_buf_p[ eUBJSON ]++) = ']' ;
			}
			*(m_buf_p[ eUBJSON ]++) = '[' ;
			return true ;
		}
		void set_number_of_entries( uint32_t ploidy, std::size_t n, OrderType const order_type, ValueType const value_type ) {
			if( order_type != genfile::ePerOrderedHaplotype ) {
				throw genfile::BadArgumentError(
					"genfile::HaplotypeWriter::set_order_type()",
					"order_type",
					"Expected values to represent ordered haplotype calls."
				) ;
			}
			if( value_type != genfile::eAlleleIndex ) {
				throw genfile::BadArgumentError(
					"genfile::HaplotypeWriter::set_order_type()",
					"value_type",
					"Expected values to represent allele indices (i.e. GT field)."
				) ;
			}
			if( n != 1 && n != 2 ) {
				m_buffer_validity[ eBITPACK ] = false ;
				throw genfile::BadArgumentError(
					"genfile::HaplotypeWriter::set_number_of_entries()",
					"n != 1 or 2"
				) ;
			}
			if( ploidy != n ) {
				throw genfile::BadArgumentError(
					"genfile::HaplotypeWriter::set_number_of_entries()",
					"ploidy != n"
				) ;
			}
			m_ploidy = n ;
		}

		void set_value( std::size_t, genfile::MissingValue const value ) {
			assert( ( m_buf_p[ eUBJSON ] + 1 ) <= m_buf_end_p[ eUBJSON ] ) ;
			*(m_buf_p[ eUBJSON ]++) = 'Z' ;
			if( m_buffer_validity[ eBITPACK ] ) {
				*(m_buf_p[ eBITPACK ]++) = 0xFF ;
			}
		}

		void set_value( std::size_t, std::string& value ) {
			assert( ( m_buf_p[ eUBJSON ] + 1 + value.size() ) <= m_buf_end_p[ eUBJSON ] ) ;
			m_buffer_validity[ eBITPACK ] = false ;
			*(m_buf_p[ eUBJSON ]++) = 'S' ;
			std::copy( value.begin(), value.end(), m_buf_p[ eUBJSON ] ) ;
			m_buf_p[ eUBJSON ] += value.size() ;
		}

		void set_value( std::size_t, Integer const value ) {
			// We allow a maximum value of 127 here to allow for reserved values.
	 		if( fits_in< Integer, uint8_t >( value ) && value < 127 ) {
				assert( ( m_buf_p[ eUBJSON ] + 2 ) <= m_buf_end_p[ eUBJSON ] ) ;
				*(m_buf_p[ eUBJSON ]++) = 'i' ;
				m_buf_p[ eUBJSON ] = genfile::write_big_endian_integer( m_buf_p[ eUBJSON ], m_buf_end_p[ eUBJSON ], static_cast< uint8_t >( value )) ;

				if( m_buffer_validity[ eBITPACK ] ) {
					*(m_buf_p[ eBITPACK ]++) = static_cast< uint8_t >( value ) ;
					if( m_ploidy == 1 ) {
						// store two haplotypes for males, the second filled wth missing values.
						*(m_buf_p[ eBITPACK ]++) = 0xFF ;
					}
				}
			} else {
				m_buffer_validity[ eBITPACK ] = false ;
				if( fits_in< Integer, int16_t >( value ) ) {
					assert( ( m_buf_p[ eUBJSON ] + 3 ) <= m_buf_end_p[ eUBJSON ] ) ;
					*(m_buf_p[ eUBJSON ]++) = 'I' ;
					m_buf_p[ eUBJSON ] = genfile::write_big_endian_integer( m_buf_p[ eUBJSON ], m_buf_end_p[ eUBJSON ], static_cast< int16_t >( value )) ;
				} else if( fits_in< Integer, int32_t >( value ) ) {
					assert( ( m_buf_p[ eUBJSON ] + 5 ) <= m_buf_end_p[ eUBJSON ] ) ;
					*(m_buf_p[ eUBJSON ]++) = 'l' ;
					m_buf_p[ eUBJSON ] = genfile::write_big_endian_integer( m_buf_p[ eUBJSON ], m_buf_end_p[ eUBJSON ], static_cast< int32_t >( value )) ;
				}
				else {
					assert( ( m_buf_p[ eUBJSON ] + 9 ) <= m_buf_end_p[ eUBJSON ] ) ;
					*(m_buf_p[ eUBJSON ]++) = 'L' ;
					m_buf_p[ eUBJSON ] = genfile::write_big_endian_integer( m_buf_p[ eUBJSON ], m_buf_end_p[ eUBJSON ], static_cast< int64_t >( value )) ;
				}
			}
		}

		void set_value( std::size_t, double const value ) {
			assert( ( m_buf_p[ eUBJSON ] + 5 ) <= m_buf_end_p[ eUBJSON ] ) ;
			m_buffer_validity[ eBITPACK ] = false ;
			float const float_value = value ;
			*(m_buf_p[ eUBJSON ]++) = 'd' ;
			m_buf_p[ eUBJSON ] = write_float( m_buf_p[ eUBJSON ], m_buf_end_p[ eUBJSON ], float_value ) ;
		}

		void finalise() {
			*(m_buf_p[ eUBJSON ]++) = ']' ;
			*(m_buf_p[ eUBJSON ]++) = ']' ;

			for( std::size_t i = 0; i < 2; ++i ) {
				m_buffers[ i ].resize( ( m_buf_p[ i ] ) - ( &( m_buffers[ i ][0] ) ) ) ;
			}

			if( m_buffer_validity[ eBITPACK ] ) {
				genfile::zlib_compress( &( m_buffers[ eBITPACK ][0] ), m_buf_p[eBITPACK], m_compression_buffer ) ;
				//std::cerr << "BITPACK: buffer size: " << m_buffers[ eBITPACK ].size() << " before compression, " << m_compression_buffer->size() << " after compression.\n" ;
			} else {
				genfile::zlib_compress( &( m_buffers[ eUBJSON ][0] ), m_buf_p[eUBJSON], m_compression_buffer ) ;
				//std::cerr << "UBJSON: buffer size: " << m_buffers[ eUBJSON ].size() << " before compression, " << m_compression_buffer->size() << " after compression.\n" ;
			}
		}

		std::string get_encoding_name() const {
			if( m_buffer_validity[ eBITPACK ] ) {
				return "bitpack" ;
			} else {
				return "ubjson" ;
			}
		}

	private:
		std::vector< uint8_t >* m_compression_buffer ;
		std::vector< std::vector< uint8_t > > m_buffers ;
		std::size_t m_bitpack_index ;
		std::vector< bool > m_buffer_validity ;
		std::vector< uint8_t* > m_buf_p ;
		std::vector< uint8_t* > m_buf_end_p ;
		std::size_t m_ploidy ;

	private:
		uint8_t* write_float( uint8_t* buf_p, uint8_t const* const buf_end_p, float const& value ) const {
			uint8_t const* v_p = reinterpret_cast< uint8_t const* >( &value ) ;
			*(buf_p++) = *v_p++ ;
			*(buf_p++) = *v_p++ ;
			*(buf_p++) = *v_p++ ;
			*(buf_p++) = *v_p++ ;
			return buf_p ;
		}
	} ;
}

void SQLiteHaplotypesSNPDataSink::set_sample_names_impl( std::size_t number_of_samples, SampleNameGetter getter ) {
	if( number_of_samples != m_samples.get_number_of_individuals() ) {
		throw genfile::BadArgumentError(
			"SQLiteGenotypesSNPDataSink::set_sample_names_impl()",
			"number_of_samples",
			"number of samples ("
			+ genfile::string_utils::to_string( number_of_samples )
			+ ") does not match that in sample files ("
			+ genfile::string_utils::to_string( m_samples.get_number_of_individuals() )
			+ ")."
		) ;
	}
	qcdb::store_samples_in_db(
		number_of_samples,
		getter,
		m_samples,
		*m_outputter,
		"Sample"
	) ;
}

void SQLiteHaplotypesSNPDataSink::write_variant_data_impl(
	genfile::VariantIdentifyingData const& id_data,
	genfile::VariantDataReader& data_reader,
	Info const& info
) {
	if( m_data_i == m_snps.size() ) {
		flush_data( m_data_i ) ;
		m_data_i = 0 ;
	}
	m_snps[ m_data_i ] = id_data ;
	HaplotypeWriter writer( &(m_data[ m_data_i ]) ) ;
	try {
		data_reader.get( ":genotypes:", writer ) ;
		writer.finalise() ;
		++m_data_i ;
	} catch( genfile::InputError const& e ) {
		std::cerr << "SQLiteHaplotypesSNPDataSink:write_variant_data_impl(): at variant " << id_data << ": "
			<< e.format_message() << ".\n" ;
		std::cerr << "I will not store haplotypes at this SNP.\n" ;
	}
}

void SQLiteHaplotypesSNPDataSink::finalise_impl() {
	if( m_data_i > 0 ) {
		flush_data( m_data_i ) ;
		m_data_i = 0 ;
	}
	m_outputter->finalise() ;
}

void SQLiteHaplotypesSNPDataSink::flush_data( std::size_t const data_count ) {
	db::Connection::ScopedTransactionPtr transaction = m_outputter->connection().open_transaction( 600 ) ;

#if DEBUG_SQLITEHAPLOTYPESSNPDATASINK
	std::cerr << "Flushing " << data_count << " elements..." ;
	std::size_t max_data_size = 0 ;
#endif
	std::vector< db::Connection::RowId > variant_ids( data_count ) ;
	for( std::size_t i = 0; i < data_count; ++i ) {
		variant_ids[i] = m_outputter->get_or_create_variant( m_snps[i] ) ;
	}
#if DEBUG_SQLITEHAPLOTYPESSNPDATASINK
	std::cerr << "stored variants..." ;
#endif
	for( std::size_t i = 0; i < data_count; ++i ) {
		uint8_t const* buffer = &(m_data[i][0]) ;
		uint8_t const* end_buffer = &(m_data[i][0]) + m_data[i].size() ;

		m_insert_data_stmnt
			->bind( 1, m_outputter->analysis_id() )
			.bind( 2, variant_ids[i] )
			.bind( 3, int64_t( m_samples.get_number_of_individuals() ) )
			.bind( 4, buffer, end_buffer )
			.step() ;
		m_insert_data_stmnt->reset() ;
#if DEBUG_SQLITEHAPLOTYPESSNPDATASINK
		max_data_size = std::max( max_data_size, m_data[i].size() ) ;
#endif
	}
#if DEBUG_SQLITEHAPLOTYPESSNPDATASINK
	std::cerr << "Done, max data size was " << max_data_size << ".\n" ;
#endif
}

ext: cpp

max_stars_repo_name: CreRecombinase/qctool

max_issues_repo_name: CreRecombinase/qctool

max_forks_repo_name: CreRecombinase/qctool


text: #include <boost/type_traits/make_unsigned.hpp>

ext: hpp

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#include <boost/asio.hpp>
#include <boost/bind.hpp>

#include <shmbus/producer.hpp>
#include <shmbus/mandatory_consumer.hpp>

#include <chrono>
#include <iostream>

namespace asio = boost::asio;

class SerialPortReader
{
public:
    SerialPortReader(asio::serial_port& port, shmbus::producer& producer) :
    m_port(port),
    m_producer(producer),
    m_bufferPtr(asio::buffer(m_buffer))
    {
        m_port.async_read_some(m_bufferPtr, boost::bind(&SerialPortReader::rxCallback, this, asio::placeholders::error, asio::placeholders::bytes_transferred));
    }

private:
    asio::serial_port& m_port;
    shmbus::producer& m_producer;
    std::array<uint8_t, 4096> m_buffer;
    asio::mutable_buffer m_bufferPtr;

    void rxCallback(const boost::system::error_code& error, std::size_t bytesTransferred)
    {
        if(not error)
        {
            std::size_t bytesWritten = m_producer.write_some(asio::buffer_cast<const void*>(m_bufferPtr), bytesTransferred);
            if(bytesWritten < bytesTransferred)
            {
                m_bufferPtr = m_bufferPtr + bytesWritten;
                asio::post(m_port.get_io_context(), boost::bind(&SerialPortReader::rxCallback, this, error, bytesTransferred - bytesWritten));
            }
            else
            {
                m_bufferPtr = asio::buffer(m_buffer);
                m_port.async_read_some(m_bufferPtr, boost::bind(&SerialPortReader::rxCallback, this, asio::placeholders::error, asio::placeholders::bytes_transferred));
            }
        }
    }
};

class SerialPortWriter
{
public:
    SerialPortWriter(asio::serial_port& port, shmbus::mandatory_consumer& consumer) :
    m_port(port),
    m_consumer(consumer),
    m_timer(port.get_io_context())
    {
        transmitOrWait();
    }

private:
    asio::serial_port& m_port;
    shmbus::mandatory_consumer& m_consumer;
    asio::steady_timer m_timer;

    void txCallback(const boost::system::error_code& error, std::size_t bytesTransferred)
    {
        if(not error)
        {
            m_consumer.consume(bytesTransferred);
            transmitOrWait();
        }
    }

    void timeout(const boost::system::error_code& error)
    {
        if(not error)
        {
            transmitOrWait();
        }
    }

    void transmitOrWait()
    {
        const void* data;
        std::size_t dataSize;
        std::tie(data, dataSize) = m_consumer.data();
        if(dataSize > 0)
            m_port.async_write_some(asio::buffer(data, dataSize), boost::bind(&SerialPortWriter::txCallback, this, asio::placeholders::error, asio::placeholders::bytes_transferred));
        else
        {
            m_timer.expires_after(std::chrono::milliseconds(10));
            m_timer.async_wait(boost::bind(&SerialPortWriter::timeout, this, asio::placeholders::error));
        }
    }
};

void run_forever(const std::string& busName, const std::string& path, unsigned int baudRate,
    asio::serial_port::flow_control::type flowControl = asio::serial_port::flow_control::none)
{
    asio::io_context ioContext;
    asio::serial_port serial(ioContext, path);
    serial.set_option(asio::serial_port::baud_rate(baudRate));
    serial.set_option(asio::serial_port::flow_control(flowControl));

    std::string rxBusName = busName + "_rx";
    shmbus::producer rxBus(shmbus::create, rxBusName, 20);

    std::string txBusName = busName + "_tx";
    uint8_t id[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
    shmbus::mandatory_consumer txBus(shmbus::create, txBusName, 20, id);

    SerialPortReader reader(serial, rxBus);
    SerialPortWriter writer(serial, txBus);
    ioContext.run();
}

int main(int argc, char* argv[])
{
    if(argc < 4 or argc > 5)
    {
        std::cout << "Usage: " << std::string(argv[0]) << " <bus_name> <serial_path> <baud_rate> [hwfc]" << std::endl;
        return 1;
    }

    std::string busName(argv[1]);
    std::string serialPath(argv[2]);
    unsigned int baudRate = std::stoul(std::string(argv[2]));
    asio::serial_port::flow_control::type flowControl = asio::serial_port::flow_control::none;
    if(argc > 4 and std::string(argv[4]) == "hwfc")
        flowControl = asio::serial_port::flow_control::hardware;

    run_forever(busName, serialPath, baudRate, flowControl);
    return 0;
}

max_stars_repo_name: jonathanherbst/shmbus

max_issues_repo_name: jonathanherbst/shmbus

max_forks_repo_name: jonathanherbst/shmbus


text:
#include <chrono>
#include <iostream>
#include <vector>
#include <utility> // std::pair
#include <algorithm> // std::max_element()
#include <string> // std::to_string()

#include <armadillo>

#include "AudioFFT/AudioFFT.h"

using std::chrono::high_resolution_clock;
using std::chrono::duration;
using AudioVec = std::vector<float>;
using audiofft::AudioFFT;
using FftwData = std::tuple<AudioFFT& // fft class
                           ,AudioVec& // input vector
						   ,AudioVec& // real vector
						   ,AudioVec& // imag vector
						   ,AudioVec&>; // output vector

namespace {
	uint32_t num = 1;
	uint32_t fft_size = 48000;
	uint32_t conv_ir_size = 72000;
	uint32_t conv_sig_size = 5760000;
}

std::pair<duration<double>, arma::fvec> Convolution(arma::fvec sig, arma::fvec ir) {
	size_t size = (::conv_sig_size+::conv_ir_size-1);
	ir = arma::flipud(ir);
	arma::fvec sig_new = arma::zeros<arma::fvec>(::conv_sig_size + 2*(::conv_ir_size-1));
	sig_new.subvec(::conv_ir_size - 1, ::conv_sig_size + ::conv_ir_size -2) = sig;
	arma::fvec output (::conv_sig_size + ::conv_ir_size - 1, arma::fill::zeros);

	auto begin = high_resolution_clock::now();
	for (uint32_t cnt=0;cnt<::num;++cnt) {
		for (uint32_t sample_cnt=0;sample_cnt<size;++sample_cnt) {
			for (uint32_t ir_cnt=0;ir_cnt<::conv_ir_size;++ir_cnt) {
				output[sample_cnt] += sig_new[sample_cnt+ir_cnt] * ir[ir_cnt];
			}
		}
	}
	AudioVec output_copy = arma::conv_to<AudioVec>::from(output);
	auto end = high_resolution_clock::now();

	return std::make_pair(end - begin, output);
}

std::pair<duration<double>, arma::fvec> ArmadilloConv(arma::fvec sig, arma::fvec ir) {
	arma::fvec output;

	auto begin = high_resolution_clock::now();
	for (uint32_t cnt=0;cnt<::num;++cnt) {
		output = arma::conv(sig, ir);
	}
	AudioVec output_copy = arma::conv_to<AudioVec>::from(output);
	auto end = high_resolution_clock::now();

	return std::make_pair(end - begin, output);
}

duration<double> FftwConv(FftwData sig, FftwData ir) {
	auto sig_in = std::get<1>(sig);
	auto sig_re = std::get<2>(sig);
	auto sig_im = std::get<3>(sig);
	auto ir_in = std::get<1>(ir);
	auto ir_re = std::get<2>(ir);
	auto ir_im = std::get<3>(ir);
	AudioVec re (sig_re.size());
	AudioVec im (sig_re.size());

	auto begin = high_resolution_clock::now();
	std::get<0>(sig).fft(sig_in.data(), sig_re.data(), sig_im.data());
	std::get<0>(ir).fft(ir_in.data(), ir_re.data(), ir_im.data());
	for (size_t cnt=0;cnt<sig_re.size();++cnt) {
		re[cnt] = sig_re[cnt]*ir_re[cnt] - sig_im[cnt]*ir_im[cnt];
		im[cnt] = sig_re[cnt]*ir_im[cnt] + sig_im[cnt]*ir_re[cnt];
	}
	std::get<0>(sig).ifft(std::get<4>(sig).data(), re.data(), im.data());
	auto end = high_resolution_clock::now();

	return end - begin;
}

std::pair<duration<double>, arma::fvec> ArmadilloFftConv(arma::fvec sig, arma::fvec ir) {
	arma::cx_fvec output;
	size_t size = sig.size() + ir.size() - 1;

	auto begin = high_resolution_clock::now();
	for (uint32_t cnt=0;cnt<::num;++cnt) {
		output = arma::ifft(arma::fft(sig, size) % arma::fft(ir, size));
	}
	AudioVec output_copy = arma::conv_to<AudioVec>::from(arma::real(output));
	auto end = high_resolution_clock::now();

	return std::make_pair(end - begin, arma::real(output));
}

std::pair<duration<double>, arma::fvec> ArmadilloFftPow2Conv(arma::fvec sig, arma::fvec ir) {
	uint32_t size = pow(2,ceil(log2(::conv_sig_size + ::conv_ir_size - 1)));
	arma::cx_fvec output;

	auto begin = high_resolution_clock::now();
	for (uint32_t cnt=0;cnt<::num;++cnt) {
		output = arma::ifft(arma::fft(sig,size) % arma::fft(ir,size));
	}
	AudioVec output_copy = arma::conv_to<AudioVec>::from(arma::real(output));
	auto end = high_resolution_clock::now();

	return std::make_pair(end - begin, arma::real(output.subvec(0, ::conv_sig_size + ::conv_ir_size - 2)));
}

std::pair<duration<double>,arma::fvec> ArmadilloFft(arma::fvec input) {
	arma::cx_fvec output_fd;
	arma::cx_fvec output_td;

	auto begin = high_resolution_clock::now();
	for (uint32_t cnt=0;cnt<::num;++cnt) {
		output_fd = arma::fft(input);
		output_td = arma::ifft(output_fd);

	}
	AudioVec output_copy = arma::conv_to<AudioVec>::from(arma::real(output_td));
	auto end = high_resolution_clock::now();

	return std::make_pair(end - begin,arma::real(output_td));
}

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

	std::cout << "generating input signals" << std::endl;
	arma::fvec sig {arma::randn<arma::fvec>(::conv_sig_size)};
	arma::fvec ir {arma::randn<arma::fvec>(::conv_ir_size)};
	size_t out_size = ::conv_sig_size + ::conv_ir_size - 1;

	std::cout << "initializing FFTW" << std::flush;
	size_t fft_size (pow(2,ceil(log2(out_size))));
	AudioVec sig_vec  (fft_size, 0);
	std::copy_n(sig.begin(), ::conv_sig_size, sig_vec.begin());
	AudioVec ir_vec (fft_size, 0);
	std::copy_n(ir.begin(), ::conv_ir_size, ir_vec.begin());
	AudioVec sig_re(AudioFFT::ComplexSize(fft_size));
	AudioVec ir_re(AudioFFT::ComplexSize(fft_size));
	AudioVec sig_im(AudioFFT::ComplexSize(fft_size));
	AudioVec ir_im(AudioFFT::ComplexSize(fft_size));
	AudioVec output(fft_size);
	AudioFFT fft_sig;
	auto begin = high_resolution_clock::now();
	fft_sig.init(fft_size);
	auto end = high_resolution_clock::now();
	std::cout << "\n\tduration: " << (std::chrono::duration_cast<duration<double>>(end - begin)).count() << std::endl;
	AudioFFT fft_ir;
	fft_ir.init(fft_size);
	FftwData sig_data = std::forward_as_tuple(fft_sig, sig_vec, sig_re, sig_im, output);
	FftwData ir_data = std::forward_as_tuple(fft_ir, ir_vec, ir_re, ir_im, output);

	std::cout << "Armadillo FFT: " << std::flush;
	auto result_arma_fft = ArmadilloFft(sig);
	std::cout << "\n\tduration: " << result_arma_fft.first.count() << std::endl;

	// normal convolution, this is our reference output
	std::cout << "convolution: " << std::flush;
	auto result_conv = Convolution(sig, ir);
	std::cout << "\n\tduration: " << result_conv.first.count() << std::endl;

	std::cout << "Armadillo FFT-Pow2-convolution: " << std::flush;
	auto result_arma_fft_pow2_conv = ArmadilloFftPow2Conv(sig, ir);
	std::cout << "\n\tduration: " << result_arma_fft_pow2_conv.first.count();
	std::cout << "\n\tmaximum difference of result: "
			  << arma::abs(result_conv.second - result_arma_fft_pow2_conv.second).max()
			  << std::endl;

	std::cout << "FFTW FFT-Pow2-convolution: " << std::flush;
	auto result_fftw_pow2_conv = FftwConv( sig_data, ir_data);
	AudioVec diff;
	std::transform(
			result_conv.second.begin()
			, result_conv.second.end()
			, std::get<4>(sig_data).begin()
			, std::back_inserter(diff)
			, [](float a, float b) { return fabs(a-b); }
			);
	std::cout << "\n\tduration: " << result_fftw_pow2_conv.count();
	std::cout << "\n\tmaximum difference of result: "
			  << std::to_string(*std::max_element(diff.begin(), diff.end()))
			  << std::endl;

	std::cout << "Armadillo FFT-convolution: " << std::flush;
	auto result_arma_fft_conv = ArmadilloFftConv(sig, ir);
	std::cout << "\n\tduration: " << result_arma_fft_conv.first.count();
	std::cout << "\n\tmaximum difference of result: "
			  << arma::abs(result_conv.second - result_arma_fft_conv.second).max()
			  << std::endl;

	std::cout << "Armadillo convolution: " << std::flush;
	auto result_arma_conv = ArmadilloConv(sig, ir);
	std::cout << "\n\tduration: " << result_arma_conv.first.count();
	std::cout << "\n\tmaximum difference of result: "
			  << arma::abs(result_conv.second - result_arma_conv.second).max()
			  << std::endl;
}

max_stars_repo_name: ThomasFeher/fft-comparison

max_issues_repo_name: ThomasFeher/fft-comparison

max_forks_repo_name: ThomasFeher/fft-comparison


text:
#pragma once
#include <string>
#include <system_error>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>

namespace ext
{
	/// formats error message like: <ErrCat>:<ErrCode>, <msg>
	/// for example: generic:0x1, file not found
	std::string format_error(std::error_code err);
	std::string format_error(boost::system::error_code err);

	/// re-formats error message std::system_error::what(), so it contains
	/// code description in format: <ErrCat>:<ErrCode>, <msg>
	std::string format_error(std::system_error & err);
	std::string format_error(boost::system::system_error err);

	inline std::string format_errno(int err) { return format_error(std::error_code(err, std::generic_category())); }

	/// returns last system error:
	///   on windows - GetLastError()
	///   on posix   - errno
	std::error_code last_system_error() noexcept;

	/// In windows system_category implemented via FormatMessageA, as result
	/// error text is returned localized in some local one-byte code page
	///
	/// This category returns text with MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US) in ASCII(or should be)
	/// On other OS - this is same as std::system_category
	std::error_category const & system_utf8_category() noexcept;
	boost::system::error_category const & boost_system_utf8_category() noexcept;

	/// equivalent throw std::system_error(last_system_error, errMsg);
	inline void throw_last_system_error(const char * errmsg)         { throw std::system_error(last_system_error(), errmsg); }
	inline void throw_last_system_error(const std::string & errmsg)  { throw std::system_error(last_system_error(), errmsg); }

	inline void throw_last_errno(const std::string & errmsg) { throw std::system_error(errno, std::generic_category(), errmsg); }
	inline void throw_last_errno(const char * errmsg)        { throw std::system_error(errno, std::generic_category(), errmsg); }
}

max_stars_repo_name: dmlys/ExtLibrary

max_issues_repo_name: dmlys/ExtLibrary

max_forks_repo_name: dmlys/ExtLibrary


text: #include <boost/preprocessor/facilities.hpp>


text:
/**
 * Copyright Soramitsu Co., Ltd. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef KAGOME_APP_CONFIG_HPP
#define KAGOME_APP_CONFIG_HPP

#include <spdlog/spdlog.h>
#include <boost/asio/ip/tcp.hpp>
#include <memory>
#include <string>

namespace kagome::application {

  /**
   * Parse and store application config.
   */
  class AppConfiguration {
   public:
    enum struct LoadScheme {
      kBlockProducing,
      kValidating,
      kFullSyncing,
    };

   public:
    virtual ~AppConfiguration() = default;

    /**
     * @return file path with genesis configuration.
     */
    virtual const std::string &genesis_path() const = 0;

    /**
     * @return keystore directory path.
     */
    virtual const std::string &keystore_path() const = 0;

    /**
     * @return leveldb directory path.
     */
    virtual const std::string &leveldb_path() const = 0;

    /**
     * @return port for peer to peer interactions.
     */
    virtual uint16_t p2p_port() const = 0;

    /**
     * @return endpoint for RPC over HTTP.
     */
    virtual const boost::asio::ip::tcp::endpoint &rpc_http_endpoint() const = 0;

    /**
     * @return endpoint for RPC over Websocket protocol.
     */
    virtual const boost::asio::ip::tcp::endpoint &rpc_ws_endpoint() const = 0;

    /**
     * @return log level (0-trace, 5-only critical, 6-no logs).
     */
    virtual spdlog::level::level_enum verbosity() const = 0;

    /**
     * @return true if node in only finalizing mode, otherwise false.
     */
    virtual bool is_only_finalizing() const = 0;
  };

  using AppConfigPtr = std::shared_ptr<AppConfiguration>;

}  // namespace kagome::application

#endif  // KAGOME_APP_CONFIG_HPP

max_stars_repo_name: FlorianFranzen/kagome

max_issues_repo_name: FlorianFranzen/kagome

max_forks_repo_name: FlorianFranzen/kagome


text:
/**
 *-----------------------------------------------------------------------------
 * Title         : SLAC Splitter Version 1
 * ----------------------------------------------------------------------------
 * File          : SplitterV1.h
 * Author        : Ryan Herbst <rherbst@slac.stanford.edu>
 * Created       : 10/26/2018
 *-----------------------------------------------------------------------------
 * This file is part of the rogue software platform. It is subject to
 * the license terms in the LICENSE.txt file found in the top-level directory
 * of this distribution and at:
    * https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
 * No part of the rogue software platform, including this file, may be
 * copied, modified, propagated, or distributed except according to the terms
 * contained in the LICENSE.txt file.
 *-----------------------------------------------------------------------------
**/
#include <stdint.h>
#include <thread>
#include <memory>
#include <rogue/interfaces/stream/Master.h>
#include <rogue/interfaces/stream/Slave.h>
#include <rogue/interfaces/stream/Frame.h>
#include <rogue/interfaces/stream/FrameLock.h>
#include <rogue/interfaces/stream/FrameIterator.h>
#include <rogue/protocols/batcher/SplitterV1.h>
#include <rogue/protocols/batcher/CoreV1.h>
#include <rogue/protocols/batcher/Data.h>
#include <rogue/Logging.h>
#include <rogue/GilRelease.h>
#include <math.h>

namespace rpb = rogue::protocols::batcher;
namespace ris = rogue::interfaces::stream;

#ifndef NO_PYTHON
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/python.hpp>
namespace bp  = boost::python;
#endif

//! Class creation
rpb::SplitterV1Ptr rpb::SplitterV1::create() {
   rpb::SplitterV1Ptr p = std::make_shared<rpb::SplitterV1>();
   return(p);
}

//! Setup class in python
void rpb::SplitterV1::setup_python() {
#ifndef NO_PYTHON
   bp::class_<rpb::SplitterV1, rpb::SplitterV1Ptr, bp::bases<ris::Master,ris::Slave>, boost::noncopyable >("SplitterV1",bp::init<>());
#endif
}

//! Creator
rpb::SplitterV1::SplitterV1() : ris::Master(), ris::Slave() { }

//! Deconstructor
rpb::SplitterV1::~SplitterV1() {}

//! Accept a frame from master
void rpb::SplitterV1::acceptFrame ( ris::FramePtr frame ) {
   rpb::CoreV1   core;
   ris::FramePtr nFrame;
   rpb::DataPtr  data;
   uint32_t x;

   // Lock frame
   rogue::GilRelease noGil;
   ris::FrameLockPtr lock = frame->lock();

   core.processFrame(frame);

   for (x=0; x < core.count(); x++) {
      data = core.record(x);

      // Create a new frame
      nFrame = reqFrame(data->size(),true);
      nFrame->setPayload(data->size());

      ris::FrameIterator fIter = nFrame->begin();
      ris::FrameIterator dIter = data->begin();
      ris::copyFrame(dIter, data->size(), fIter);

      // Set flags
      nFrame->setFirstUser(data->fUser());
      nFrame->setLastUser(data->lUser());
      nFrame->setChannel(data->dest());

      sendFrame(nFrame);
   }
}

max_stars_repo_name: slaclab/rouge

max_issues_repo_name: slaclab/rouge

max_forks_repo_name: slaclab/rouge