<commit_before>/***********************************************************************
    created:    Tue Feb 17 2009
    author:     Paul D Turner
*************************************************************************/
/***************************************************************************
 *   Copyright (C) 2004 - 2013 Paul D Turner & The CEGUI Development Team
 *
 *   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 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 "CEGUI/RendererModules/Ogre/RenderTarget.h"
#include "CEGUI/GeometryBuffer.h"
#include "CEGUI/RenderQueue.h"
#include "CEGUI/RendererModules/Ogre/GeometryBuffer.h"

#include <OgreRenderSystem.h>
#include <OgreCamera.h>
#include <OgreViewport.h>

// Start of CEGUI namespace section
namespace CEGUI
{
//----------------------------------------------------------------------------//
template <typename T>
OgreRenderTarget<T>::OgreRenderTarget(OgreRenderer& owner,
                                      Ogre::RenderSystem& rs) :
    d_owner(owner),
    d_renderSystem(rs),
    d_area(0, 0, 0, 0),
    d_renderTarget(0),
#ifdef CEGUI_USE_OGRE_COMPOSITOR2
    d_renderTargetUpdated(false),
#else
    d_viewport(0),
    d_ogreViewportDimensions(0, 0, 0, 0),
#endif // CEGUI_USE_OGRE_COMPOSITOR2
    d_matrix(Ogre::Matrix3::ZERO),
    d_matrixValid(false),
    d_viewportValid(false),
    d_viewDistance(0)
{
}

//----------------------------------------------------------------------------//
template <typename T>
OgreRenderTarget<T>::~OgreRenderTarget()
{
#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
    delete d_viewport;
#endif // CEGUI_USE_OGRE_COMPOSITOR2
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::draw(const GeometryBuffer& buffer)
{
    buffer.draw();
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::draw(const RenderQueue& queue)
{
    queue.draw();
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::setArea(const Rectf& area)
{
    d_area = area;
#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
    setOgreViewportDimensions(area);
#endif

    d_matrixValid = false;

    RenderTargetEventArgs args(this);
    T::fireEvent(RenderTarget::EventAreaChanged, args);
}

//----------------------------------------------------------------------------//
#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
template <typename T>
void OgreRenderTarget<T>::setOgreViewportDimensions(const Rectf& area)
{
    d_ogreViewportDimensions = area;

    if (d_viewport)
        updateOgreViewportDimensions(d_viewport->getTarget());

    d_viewportValid = false;
}
#endif

//----------------------------------------------------------------------------//
#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
template <typename T>
void OgreRenderTarget<T>::updateOgreViewportDimensions(
                                            const Ogre::RenderTarget* const rt)
{
    if (rt)
    {
        d_viewport->setDimensions(
            d_ogreViewportDimensions.left() / rt->getWidth(),
            d_ogreViewportDimensions.top() / rt->getHeight(),
            d_ogreViewportDimensions.getWidth() / rt->getWidth(),
            d_ogreViewportDimensions.getHeight() / rt->getHeight());
    }
}
#endif

//----------------------------------------------------------------------------//
template <typename T>
const Rectf& OgreRenderTarget<T>::getArea() const
{
    return d_area;
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::activate()
{
    if (!d_matrixValid)
        updateMatrix();

    if (!d_viewportValid)
        updateViewport();

#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
    d_renderSystem._setViewport(d_viewport);
#endif // CEGUI_USE_OGRE_COMPOSITOR2

    d_owner.setProjectionMatrix(d_matrix);
    d_owner.setViewMatrix(Ogre::Matrix4::IDENTITY);
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::deactivate()
{
    // currently nothing to do in the basic case
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::unprojectPoint(const GeometryBuffer& buff,
                                      const Vector2f& p_in,
                                      Vector2f& p_out) const
{
    if (!d_matrixValid)
        updateMatrix();

    const OgreGeometryBuffer& gb = static_cast<const OgreGeometryBuffer&>(buff);

    const Ogre::Real midx = d_area.getWidth() * 0.5f;
    const Ogre::Real midy = d_area.getHeight() * 0.5f;

    // viewport matrix
    const Ogre::Matrix4 vpmat(
        midx,    0,    0,    d_area.left() + midx,
         0,    -midy,  0,    d_area.top() + midy,
         0,      0,    1,    0,
         0,      0,    0,    1
    );

    // matrices used for projecting and unprojecting points
    const Ogre::Matrix4 proj(gb.getMatrix() * d_matrix * vpmat);
    const Ogre::Matrix4 unproj(proj.inverse());

    Ogre::Vector3 in;

    // unproject the ends of the ray
    in.x = midx;
    in.y = midy;
    in.z = -d_viewDistance;
    const Ogre::Vector3 r1(unproj * in);
    in.x = p_in.d_x;
    in.y = p_in.d_y;
    in.z = 0;
    // calculate vector of picking ray
    const Ogre::Vector3 rv(r1 - unproj * in);

    // project points to orientate them with GeometryBuffer plane
    in.x = 0.0;
    in.y = 0.0;
    const Ogre::Vector3 p1(proj * in);
    in.x = 1.0;
    in.y = 0.0;
    const Ogre::Vector3 p2(proj * in);
    in.x = 0.0;
    in.y = 1.0;
    const Ogre::Vector3 p3(proj * in);

    // calculate the plane normal
    const Ogre::Vector3 pn((p2 - p1).crossProduct(p3 - p1));
    // calculate distance from origin
    const Ogre::Real plen = pn.length();
    const Ogre::Real dist = -(p1.x * (pn.x / plen) +
                              p1.y * (pn.y / plen) +
                              p1.z * (pn.z / plen));

    // calculate intersection of ray and plane
    const Ogre::Real pn_dot_rv = pn.dotProduct(rv);
    const Ogre::Real tmp = pn_dot_rv != 0.0 ?
                            (pn.dotProduct(r1) + dist) / pn_dot_rv :
                            0.0;

    p_out.d_x = static_cast<float>(r1.x - rv.x * tmp);
    p_out.d_y = static_cast<float>(r1.y - rv.y * tmp);
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::updateMatrix() const
{
    const float w = d_area.getWidth();
    const float h = d_area.getHeight();
    const float aspect = w / h;
    const float midx = w * 0.5f;
    d_viewDistance = midx / (aspect * 0.267949192431123f);

    const float nearZ = d_viewDistance * 0.5f;
    const float farZ = d_viewDistance * 2.0f;
    const float nr_sub_far = nearZ - farZ;

    Ogre::Matrix4 tmp(Ogre::Matrix4::ZERO);
    tmp[0][0] = 3.732050808f / aspect;
    tmp[0][3] = -d_viewDistance;
    tmp[1][1] = -3.732050808f;
    tmp[1][3] = d_viewDistance;
    tmp[2][2] = -((farZ + nearZ) / nr_sub_far);
    tmp[3][2] = 1.0f;
    tmp[3][3] = d_viewDistance;
    d_renderSystem._convertProjectionMatrix(tmp, d_matrix);

    d_matrixValid = true;
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::updateViewport()
{
#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
    if (!d_viewport)
    {
        d_viewport = OGRE_NEW Ogre::Viewport(0, d_renderTarget, 0, 0, 1, 1, 0);
        updateOgreViewportDimensions(d_renderTarget);
    }

    d_viewport->_updateDimensions();
#endif // CEGUI_USE_OGRE_COMPOSITOR2

    d_viewportValid = true;
}

//----------------------------------------------------------------------------//

} // End of  CEGUI namespace section
<commit_msg>Removed duplicate new lines.<commit_after>/***********************************************************************
    created:    Tue Feb 17 2009
    author:     Paul D Turner
*************************************************************************/
/***************************************************************************
 *   Copyright (C) 2004 - 2013 Paul D Turner & The CEGUI Development Team
 *
 *   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 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 "CEGUI/RendererModules/Ogre/RenderTarget.h"
#include "CEGUI/GeometryBuffer.h"
#include "CEGUI/RenderQueue.h"
#include "CEGUI/RendererModules/Ogre/GeometryBuffer.h"

#include <OgreRenderSystem.h>
#include <OgreCamera.h>
#include <OgreViewport.h>

// Start of CEGUI namespace section
namespace CEGUI
{
//----------------------------------------------------------------------------//
template <typename T>
OgreRenderTarget<T>::OgreRenderTarget(OgreRenderer& owner,
                                      Ogre::RenderSystem& rs) :
    d_owner(owner),
    d_renderSystem(rs),
    d_area(0, 0, 0, 0),
    d_renderTarget(0),
#ifdef CEGUI_USE_OGRE_COMPOSITOR2
    d_renderTargetUpdated(false),
#else
    d_viewport(0),
    d_ogreViewportDimensions(0, 0, 0, 0),
#endif // CEGUI_USE_OGRE_COMPOSITOR2
    d_matrix(Ogre::Matrix3::ZERO),
    d_matrixValid(false),
    d_viewportValid(false),
    d_viewDistance(0)
{
}

//----------------------------------------------------------------------------//
template <typename T>
OgreRenderTarget<T>::~OgreRenderTarget()
{
#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
    delete d_viewport;
#endif // CEGUI_USE_OGRE_COMPOSITOR2
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::draw(const GeometryBuffer& buffer)
{
    buffer.draw();
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::draw(const RenderQueue& queue)
{
    queue.draw();
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::setArea(const Rectf& area)
{
    d_area = area;
#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
    setOgreViewportDimensions(area);
#endif

    d_matrixValid = false;

    RenderTargetEventArgs args(this);
    T::fireEvent(RenderTarget::EventAreaChanged, args);
}

//----------------------------------------------------------------------------//
#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
template <typename T>
void OgreRenderTarget<T>::setOgreViewportDimensions(const Rectf& area)
{
    d_ogreViewportDimensions = area;

    if (d_viewport)
        updateOgreViewportDimensions(d_viewport->getTarget());

    d_viewportValid = false;
}
#endif

//----------------------------------------------------------------------------//
#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
template <typename T>
void OgreRenderTarget<T>::updateOgreViewportDimensions(
                                            const Ogre::RenderTarget* const rt)
{
    if (rt)
    {
        d_viewport->setDimensions(
            d_ogreViewportDimensions.left() / rt->getWidth(),
            d_ogreViewportDimensions.top() / rt->getHeight(),
            d_ogreViewportDimensions.getWidth() / rt->getWidth(),
            d_ogreViewportDimensions.getHeight() / rt->getHeight());
    }
}
#endif

//----------------------------------------------------------------------------//
template <typename T>
const Rectf& OgreRenderTarget<T>::getArea() const
{
    return d_area;
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::activate()
{
    if (!d_matrixValid)
        updateMatrix();

    if (!d_viewportValid)
        updateViewport();

#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
    d_renderSystem._setViewport(d_viewport);
#endif // CEGUI_USE_OGRE_COMPOSITOR2

    d_owner.setProjectionMatrix(d_matrix);
    d_owner.setViewMatrix(Ogre::Matrix4::IDENTITY);
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::deactivate()
{
    // currently nothing to do in the basic case
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::unprojectPoint(const GeometryBuffer& buff,
                                      const Vector2f& p_in,
                                      Vector2f& p_out) const
{
    if (!d_matrixValid)
        updateMatrix();

    const OgreGeometryBuffer& gb = static_cast<const OgreGeometryBuffer&>(buff);

    const Ogre::Real midx = d_area.getWidth() * 0.5f;
    const Ogre::Real midy = d_area.getHeight() * 0.5f;

    // viewport matrix
    const Ogre::Matrix4 vpmat(
        midx,    0,    0,    d_area.left() + midx,
         0,    -midy,  0,    d_area.top() + midy,
         0,      0,    1,    0,
         0,      0,    0,    1
    );

    // matrices used for projecting and unprojecting points
    const Ogre::Matrix4 proj(gb.getMatrix() * d_matrix * vpmat);
    const Ogre::Matrix4 unproj(proj.inverse());

    Ogre::Vector3 in;

    // unproject the ends of the ray
    in.x = midx;
    in.y = midy;
    in.z = -d_viewDistance;
    const Ogre::Vector3 r1(unproj * in);
    in.x = p_in.d_x;
    in.y = p_in.d_y;
    in.z = 0;
    // calculate vector of picking ray
    const Ogre::Vector3 rv(r1 - unproj * in);

    // project points to orientate them with GeometryBuffer plane
    in.x = 0.0;
    in.y = 0.0;
    const Ogre::Vector3 p1(proj * in);
    in.x = 1.0;
    in.y = 0.0;
    const Ogre::Vector3 p2(proj * in);
    in.x = 0.0;
    in.y = 1.0;
    const Ogre::Vector3 p3(proj * in);

    // calculate the plane normal
    const Ogre::Vector3 pn((p2 - p1).crossProduct(p3 - p1));
    // calculate distance from origin
    const Ogre::Real plen = pn.length();
    const Ogre::Real dist = -(p1.x * (pn.x / plen) +
                              p1.y * (pn.y / plen) +
                              p1.z * (pn.z / plen));

    // calculate intersection of ray and plane
    const Ogre::Real pn_dot_rv = pn.dotProduct(rv);
    const Ogre::Real tmp = pn_dot_rv != 0.0 ?
                            (pn.dotProduct(r1) + dist) / pn_dot_rv :
                            0.0;

    p_out.d_x = static_cast<float>(r1.x - rv.x * tmp);
    p_out.d_y = static_cast<float>(r1.y - rv.y * tmp);
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::updateMatrix() const
{
    const float w = d_area.getWidth();
    const float h = d_area.getHeight();
    const float aspect = w / h;
    const float midx = w * 0.5f;
    d_viewDistance = midx / (aspect * 0.267949192431123f);

    const float nearZ = d_viewDistance * 0.5f;
    const float farZ = d_viewDistance * 2.0f;
    const float nr_sub_far = nearZ - farZ;

    Ogre::Matrix4 tmp(Ogre::Matrix4::ZERO);
    tmp[0][0] = 3.732050808f / aspect;
    tmp[0][3] = -d_viewDistance;
    tmp[1][1] = -3.732050808f;
    tmp[1][3] = d_viewDistance;
    tmp[2][2] = -((farZ + nearZ) / nr_sub_far);
    tmp[3][2] = 1.0f;
    tmp[3][3] = d_viewDistance;
    d_renderSystem._convertProjectionMatrix(tmp, d_matrix);

    d_matrixValid = true;
}

//----------------------------------------------------------------------------//
template <typename T>
void OgreRenderTarget<T>::updateViewport()
{
#if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
    if (!d_viewport)
    {
        d_viewport = OGRE_NEW Ogre::Viewport(0, d_renderTarget, 0, 0, 1, 1, 0);
        updateOgreViewportDimensions(d_renderTarget);
    }

    d_viewport->_updateDimensions();
#endif // CEGUI_USE_OGRE_COMPOSITOR2

    d_viewportValid = true;
}

//----------------------------------------------------------------------------//

} // End of  CEGUI namespace section
<|endoftext|>


<commit_before>#include <iostream>
/**
 * Exercise that (I) changes the value of a pointer and (II) changes the value
 * to which the pointer points.
 */
int main ()
{
    int i1 = 1;
    int i2 = 1;
    int *p = &i1;
    std::cout << "p = " << p << " *p = " << *p << std::endl;
    std::cout << "Let's change p by pointing somewhere else." << std::endl;
    p = &i2;
    std::cout << "p = " << p << " *p = " << *p << std::endl;
    std::cout << "Let's change the value of what p points at." << std::endl;
    *p = 2;
    std::cout << "p = " << p << " *p = " << *p << std::endl;
    return 0;
}
<commit_msg>Committing exercise 2.18.<commit_after>#include <iostream>
/**
 * Exercise that (I) changes the value of a pointer and (II) changes the value
 * to which the pointer points.
 */
int main ()
{
    int i1 = 1;
    int i2 = 1;
    int* p = &i1;
    std::cout << "p = " << p << " *p = " << *p << std::endl;
    std::cout << "Let's change p by pointing somewhere else." << std::endl;
    p = &i2;
    std::cout << "p = " << p << " *p = " << *p << std::endl;
    std::cout << "Let's change the value of what p points at." << std::endl;
    *p = 2;
    std::cout << "p = " << p << " *p = " << *p << std::endl;
    return 0;
}
<|endoftext|>


<commit_before>/**
 * @file sparse_autoencoder_test.cpp
 * @author Siddharth Agrawal
 *
 * Test the SparseAutoencoder class.
 */
#include <mlpack/methods/sparse_autoencoder/sparse_autoencoder_function.hpp>
#include <mlpack/methods/ann/activation_functions/logistic_function.hpp>

#include <mlpack/core.hpp>
#include <mlpack/methods/ann/layer/base_layer.hpp>

#include <boost/test/unit_test.hpp>
#include "old_boost_test_definitions.hpp"

using namespace mlpack;
using namespace arma;

using FSigmoidLayer = ann::SigmoidLayer<ann::LogisticFunction>;

//sparse autoencoder function
using SAEF = nn::SparseAutoencoderFunction<FSigmoidLayer, FSigmoidLayer>;
//sparse autoencoder function greedy
using SAEFG = nn::SparseAutoencoderFunction<FSigmoidLayer, FSigmoidLayer, std::true_type>;

BOOST_AUTO_TEST_SUITE(SparseAutoencoderTest2);

BOOST_AUTO_TEST_CASE(SparseAutoencoderFunctionEvaluate)
{
    const size_t vSize = 5;
    const size_t hSize = 3;
    const size_t r = 2 * hSize + 1;
    const size_t c = vSize + 1;

    // Simple fake dataset.
    arma::mat data1("0.1 0.2 0.3 0.4 0.5;"
                    "0.1 0.2 0.3 0.4 0.5;"
                    "0.1 0.2 0.3 0.4 0.5;"
                    "0.1 0.2 0.3 0.4 0.5;"
                    "0.1 0.2 0.3 0.4 0.5");
    // Transpose of the above dataset.
    arma::mat data2 = data1.t();

    // Create a SparseAutoencoderFunction. Regularization and KL divergence terms
    // ignored.
    SAEF saf1(data1, vSize, hSize, 0, 0);

    // Test using first dataset. Values were calculated using Octave.
    BOOST_REQUIRE_CLOSE(saf1.Evaluate(arma::ones(r, c)), 1.190472606540, 1e-5);
    BOOST_REQUIRE_CLOSE(saf1.Evaluate(arma::zeros(r, c)), 0.150000000000, 1e-5);
    BOOST_REQUIRE_CLOSE(saf1.Evaluate(-arma::ones(r, c)), 0.048800332266, 1e-5);

    // Create a SparseAutoencoderFunction. Regularization and KL divergence terms
    // ignored.
    SAEF saf2(data2, vSize, hSize, 0, 0);

    // Test using second dataset. Values were calculated using Octave.
    BOOST_REQUIRE_CLOSE(saf2.Evaluate(arma::ones(r, c)), 1.197585812647, 1e-5);
    BOOST_REQUIRE_CLOSE(saf2.Evaluate(arma::zeros(r, c)), 0.150000000000, 1e-5);
    BOOST_REQUIRE_CLOSE(saf2.Evaluate(-arma::ones(r, c)), 0.063466617408, 1e-5);
}

BOOST_AUTO_TEST_CASE(SparseAutoencoderFunctionRandomEvaluate)
{
    const size_t points = 1000;
    const size_t trials = 50;
    const size_t vSize = 20;
    const size_t hSize = 10;
    const size_t l1 = hSize;
    const size_t l2 = vSize;
    const size_t l3 = 2 * hSize;

    // Initialize a random dataset.
    arma::mat data;
    data.randu(vSize, points);

    // Create a SparseAutoencoderFunction. Regularization and KL divergence terms
    // ignored.
    SAEF saf(data, vSize, hSize, 0, 0);

    // Run a number of trials.
    for (size_t i = 0; i < trials; i++)
    {
        // Create a random set of parameters.
        arma::mat parameters;
        parameters.randu(l3 + 1, l2 + 1);

        double reconstructionError = 0;

        // Compute error for each training example.
        for (size_t j = 0; j < points; j++)
        {
            arma::mat hiddenLayer, outputLayer, diff;

            hiddenLayer = 1.0 /
                    (1 + arma::exp(-(parameters.submat(0, 0, l1 - 1, l2 - 1) *
                                     data.col(j) + parameters.submat(0, l2, l1 - 1, l2))));
            outputLayer = 1.0 /
                    (1 + arma::exp(-(parameters.submat(l1, 0, l3 - 1,l2 - 1).t()
                                     * hiddenLayer + parameters.submat(l3, 0, l3, l2 - 1).t())));
            diff = outputLayer - data.col(j);

            reconstructionError += 0.5 * arma::sum(arma::sum(diff % diff));
        }
        reconstructionError /= points;

        // Compare with the value returned by the function.
        BOOST_REQUIRE_CLOSE(saf.Evaluate(parameters), reconstructionError, 1e-5);
    }
}

BOOST_AUTO_TEST_CASE(SparseAutoencoderFunctionRegularizationEvaluate)
{
    const size_t points = 1000;
    const size_t trials = 50;
    const size_t vSize = 20;
    const size_t hSize = 10;
    const size_t l2 = vSize;
    const size_t l3 = 2 * hSize;

    // Initialize a random dataset.
    arma::mat data;
    data.randu(vSize, points);

    // 3 objects for comparing regularization costs.
    SAEF safNoReg(data, vSize, hSize, 0, 0);
    SAEF safSmallReg(data, vSize, hSize, 0.5, 0);
    SAEF safBigReg(data, vSize, hSize, 20, 0);

    // Run a number of trials.
    for (size_t i = 0; i < trials; i++)
    {
        // Create a random set of parameters.
        arma::mat parameters;
        parameters.randu(l3 + 1, l2 + 1);

        double wL2SquaredNorm;

        wL2SquaredNorm = arma::accu(parameters.submat(0, 0, l3 - 1, l2 - 1) %
                                    parameters.submat(0, 0, l3 - 1, l2 - 1));

        // Calculate regularization terms.
        const double smallRegTerm = 0.25 * wL2SquaredNorm;
        const double bigRegTerm = 10 * wL2SquaredNorm;

        BOOST_REQUIRE_CLOSE(safNoReg.Evaluate(parameters) + smallRegTerm,
                            safSmallReg.Evaluate(parameters), 1e-5);
        BOOST_REQUIRE_CLOSE(safNoReg.Evaluate(parameters) + bigRegTerm,
                            safBigReg.Evaluate(parameters), 1e-5);
    }
}

BOOST_AUTO_TEST_CASE(SparseAutoencoderFunctionKLDivergenceEvaluate)
{
    const size_t points = 1000;
    const size_t trials = 50;
    const size_t vSize = 20;
    const size_t hSize = 10;
    const size_t l1 = hSize;
    const size_t l2 = vSize;
    const size_t l3 = 2 * hSize;

    const double rho = 0.01;

    // Initialize a random dataset.
    arma::mat data;
    data.randu(vSize, points);

    // 3 objects for comparing divergence costs.
    SAEF safNoDiv(data, vSize, hSize, 0, 0, rho);
    SAEF safSmallDiv(data, vSize, hSize, 0, 5, rho);
    SAEF safBigDiv(data, vSize, hSize, 0, 20, rho);

    // Run a number of trials.
    for(size_t i = 0; i < trials; i++)
    {
        // Create a random set of parameters.
        arma::mat parameters;
        parameters.randu(l3 + 1, l2 + 1);

        arma::mat rhoCap;
        rhoCap.zeros(hSize, 1);

        // Compute hidden layer activations for each example.
        for (size_t j = 0; j < points; j++)
        {
            arma::mat hiddenLayer;

            hiddenLayer = 1.0 / (1 +
                                 arma::exp(-(parameters.submat(0, 0, l1 - 1, l2 - 1) *
                                             data.col(j) + parameters.submat(0, l2, l1 - 1, l2))));
            rhoCap += hiddenLayer;
        }
        rhoCap /= points;

        // Calculate divergence terms.
        const double smallDivTerm = 5 * arma::accu(rho * arma::log(rho / rhoCap) +
                                                   (1 - rho) * arma::log((1 - rho) / (1 - rhoCap)));
        const double bigDivTerm = 20 * arma::accu(rho * arma::log(rho / rhoCap) +
                                                  (1 - rho) * arma::log((1 - rho) / (1 - rhoCap)));

        BOOST_REQUIRE_CLOSE(safNoDiv.Evaluate(parameters) + smallDivTerm,
                            safSmallDiv.Evaluate(parameters), 1e-5);
        BOOST_REQUIRE_CLOSE(safNoDiv.Evaluate(parameters) + bigDivTerm,
                            safBigDiv.Evaluate(parameters), 1e-5);
    }
}

BOOST_AUTO_TEST_CASE(SparseAutoencoderFunctionGradient)
{
    const size_t points = 1000;
    const size_t vSize = 20;
    const size_t hSize = 10;
    const size_t l2 = vSize;
    const size_t l3 = 2 * hSize;

    // Initialize a random dataset.
    arma::mat data;
    data.randu(vSize, points);

    // 3 objects for 3 terms in the cost function. Each term contributes towards
    // the gradient and thus need to be checked independently.
    SAEFG saf1(data, vSize, hSize, 0, 0);
    SAEFG saf2(data, vSize, hSize, 20, 0);
    SAEFG saf3(data, vSize, hSize, 20, 20);

    // Create a random set of parameters.
    arma::mat parameters;
    parameters.randu(l3 + 1, l2 + 1);

    // Get gradients for the current parameters.
    arma::mat gradient1, gradient2, gradient3;
    saf1.Gradient(parameters, gradient1);
    saf2.Gradient(parameters, gradient2);
    saf3.Gradient(parameters, gradient3);

    // Perturbation constant.
    const double epsilon = 0.0001;
    double costPlus1, costMinus1, numGradient1;
    double costPlus2, costMinus2, numGradient2;
    double costPlus3, costMinus3, numGradient3;

    // For each parameter.
    for (size_t i = 0; i <= l3; i++)
    {
        for (size_t j = 0; j <= l2; j++)
        {
            // Perturb parameter with a positive constant and get costs.
            parameters(i, j) += epsilon;
            costPlus1 = saf1.Evaluate(parameters);
            costPlus2 = saf2.Evaluate(parameters);
            costPlus3 = saf3.Evaluate(parameters);

            // Perturb parameter with a negative constant and get costs.
            parameters(i, j) -= 2 * epsilon;
            costMinus1 = saf1.Evaluate(parameters);
            costMinus2 = saf2.Evaluate(parameters);
            costMinus3 = saf3.Evaluate(parameters);

            // Compute numerical gradients using the costs calculated above.
            numGradient1 = (costPlus1 - costMinus1) / (2 * epsilon);
            numGradient2 = (costPlus2 - costMinus2) / (2 * epsilon);
            numGradient3 = (costPlus3 - costMinus3) / (2 * epsilon);

            // Restore the parameter value.
            parameters(i, j) += epsilon;

            // Compare numerical and backpropagation gradient values.
            BOOST_REQUIRE_CLOSE(numGradient1, gradient1(i, j), 1e-2);
            BOOST_REQUIRE_CLOSE(numGradient2, gradient2(i, j), 1e-2);
            BOOST_REQUIRE_CLOSE(numGradient3, gradient3(i, j), 1e-2);
        }
    }
}

BOOST_AUTO_TEST_SUITE_END();
<commit_msg>1 : adjust format 2 : use the layer and activation function from mlpack::nn<commit_after>/**
 * @file sparse_autoencoder_test.cpp
 * @author Siddharth Agrawal
 *
 * Test the SparseAutoencoder class.
 */
#include <mlpack/methods/sparse_autoencoder/sparse_autoencoder_function.hpp>
#include <mlpack/methods/sparse_autoencoder/activation_functions/logistic_function.hpp>

#include <mlpack/core.hpp>
#include <mlpack/methods/sparse_autoencoder/layer/base_layer.hpp>

#include <boost/test/unit_test.hpp>
#include "old_boost_test_definitions.hpp"

using namespace mlpack;
using namespace arma;

using SigmoidLayer = nn::SigmoidLayer<nn::LogisticFunction>;

//sparse autoencoder function
using SAEF = nn::SparseAutoencoderFunction<SigmoidLayer, SigmoidLayer>;
//sparse autoencoder function greedy
using SAEFG = nn::SparseAutoencoderFunction<SigmoidLayer, SigmoidLayer, std::true_type>;

BOOST_AUTO_TEST_SUITE(SparseAutoencoderTest2);

BOOST_AUTO_TEST_CASE(SparseAutoencoderFunctionEvaluate)
{
  const size_t vSize = 5;
  const size_t hSize = 3;
  const size_t r = 2 * hSize + 1;
  const size_t c = vSize + 1;

  // Simple fake dataset.
  arma::mat data1("0.1 0.2 0.3 0.4 0.5;"
                  "0.1 0.2 0.3 0.4 0.5;"
                  "0.1 0.2 0.3 0.4 0.5;"
                  "0.1 0.2 0.3 0.4 0.5;"
                  "0.1 0.2 0.3 0.4 0.5");
  // Transpose of the above dataset.
  arma::mat data2 = data1.t();

  // Create a SparseAutoencoderFunction. Regularization and KL divergence terms
  // ignored.
  SAEF saf1(data1, vSize, hSize, 0, 0);

  // Test using first dataset. Values were calculated using Octave.
  BOOST_REQUIRE_CLOSE(saf1.Evaluate(arma::ones(r, c)), 1.190472606540, 1e-5);
  BOOST_REQUIRE_CLOSE(saf1.Evaluate(arma::zeros(r, c)), 0.150000000000, 1e-5);
  BOOST_REQUIRE_CLOSE(saf1.Evaluate(-arma::ones(r, c)), 0.048800332266, 1e-5);

  // Create a SparseAutoencoderFunction. Regularization and KL divergence terms
  // ignored.
  SAEF saf2(data2, vSize, hSize, 0, 0);

  // Test using second dataset. Values were calculated using Octave.
  BOOST_REQUIRE_CLOSE(saf2.Evaluate(arma::ones(r, c)), 1.197585812647, 1e-5);
  BOOST_REQUIRE_CLOSE(saf2.Evaluate(arma::zeros(r, c)), 0.150000000000, 1e-5);
  BOOST_REQUIRE_CLOSE(saf2.Evaluate(-arma::ones(r, c)), 0.063466617408, 1e-5);
}

BOOST_AUTO_TEST_CASE(SparseAutoencoderFunctionRandomEvaluate)
{
  const size_t points = 1000;
  const size_t trials = 50;
  const size_t vSize = 20;
  const size_t hSize = 10;
  const size_t l1 = hSize;
  const size_t l2 = vSize;
  const size_t l3 = 2 * hSize;

  // Initialize a random dataset.
  arma::mat data;
  data.randu(vSize, points);

  // Create a SparseAutoencoderFunction. Regularization and KL divergence terms
  // ignored.
  SAEF saf(data, vSize, hSize, 0, 0);

  // Run a number of trials.
  for (size_t i = 0; i < trials; i++)
  {
    // Create a random set of parameters.
    arma::mat parameters;
    parameters.randu(l3 + 1, l2 + 1);

    double reconstructionError = 0;

    // Compute error for each training example.
    for (size_t j = 0; j < points; j++)
    {
      arma::mat hiddenLayer, outputLayer, diff;

      hiddenLayer = 1.0 /
                   (1 + arma::exp(-(parameters.submat(0, 0, l1 - 1, l2 - 1) *
                                  data.col(j) + parameters.submat(0, l2, l1 - 1, l2))));
      outputLayer = 1.0 /
                    (1 + arma::exp(-(parameters.submat(l1, 0, l3 - 1,l2 - 1).t()
                                   * hiddenLayer + parameters.submat(l3, 0, l3, l2 - 1).t())));
      diff = outputLayer - data.col(j);

      reconstructionError += 0.5 * arma::sum(arma::sum(diff % diff));
    }
    reconstructionError /= points;

    // Compare with the value returned by the function.
    BOOST_REQUIRE_CLOSE(saf.Evaluate(parameters), reconstructionError, 1e-5);
  }
}

BOOST_AUTO_TEST_CASE(SparseAutoencoderFunctionRegularizationEvaluate)
{
  const size_t points = 1000;
  const size_t trials = 50;
  const size_t vSize = 20;
  const size_t hSize = 10;
  const size_t l2 = vSize;
  const size_t l3 = 2 * hSize;

  // Initialize a random dataset.
  arma::mat data;
  data.randu(vSize, points);

  // 3 objects for comparing regularization costs.
  SAEF safNoReg(data, vSize, hSize, 0, 0);
  SAEF safSmallReg(data, vSize, hSize, 0.5, 0);
  SAEF safBigReg(data, vSize, hSize, 20, 0);

  // Run a number of trials.
  for (size_t i = 0; i < trials; i++)
  {
    // Create a random set of parameters.
    arma::mat parameters;
    parameters.randu(l3 + 1, l2 + 1);

    double wL2SquaredNorm;

    wL2SquaredNorm = arma::accu(parameters.submat(0, 0, l3 - 1, l2 - 1) %
                                parameters.submat(0, 0, l3 - 1, l2 - 1));

    // Calculate regularization terms.
    const double smallRegTerm = 0.25 * wL2SquaredNorm;
    const double bigRegTerm = 10 * wL2SquaredNorm;

    BOOST_REQUIRE_CLOSE(safNoReg.Evaluate(parameters) + smallRegTerm,
                        safSmallReg.Evaluate(parameters), 1e-5);
    BOOST_REQUIRE_CLOSE(safNoReg.Evaluate(parameters) + bigRegTerm,
                        safBigReg.Evaluate(parameters), 1e-5);
  }
}

BOOST_AUTO_TEST_CASE(SparseAutoencoderFunctionKLDivergenceEvaluate)
{
  const size_t points = 1000;
  const size_t trials = 50;
  const size_t vSize = 20;
  const size_t hSize = 10;
  const size_t l1 = hSize;
  const size_t l2 = vSize;
  const size_t l3 = 2 * hSize;

  const double rho = 0.01;

  // Initialize a random dataset.
  arma::mat data;
  data.randu(vSize, points);

  // 3 objects for comparing divergence costs.
  SAEF safNoDiv(data, vSize, hSize, 0, 0, rho);
  SAEF safSmallDiv(data, vSize, hSize, 0, 5, rho);
  SAEF safBigDiv(data, vSize, hSize, 0, 20, rho);

  // Run a number of trials.
  for(size_t i = 0; i < trials; i++)
  {
    // Create a random set of parameters.
    arma::mat parameters;
    parameters.randu(l3 + 1, l2 + 1);

    arma::mat rhoCap;
    rhoCap.zeros(hSize, 1);

    // Compute hidden layer activations for each example.
    for (size_t j = 0; j < points; j++)
    {
      arma::mat hiddenLayer;

      hiddenLayer = 1.0 / (1 +
                           arma::exp(-(parameters.submat(0, 0, l1 - 1, l2 - 1) *
                                       data.col(j) + parameters.submat(0, l2, l1 - 1, l2))));
      rhoCap += hiddenLayer;
    }
    rhoCap /= points;

    // Calculate divergence terms.
    const double smallDivTerm = 5 * arma::accu(rho * arma::log(rho / rhoCap) +
                                               (1 - rho) * arma::log((1 - rho) / (1 - rhoCap)));
    const double bigDivTerm = 20 * arma::accu(rho * arma::log(rho / rhoCap) +
                                              (1 - rho) * arma::log((1 - rho) / (1 - rhoCap)));

    BOOST_REQUIRE_CLOSE(safNoDiv.Evaluate(parameters) + smallDivTerm,
                        safSmallDiv.Evaluate(parameters), 1e-5);
    BOOST_REQUIRE_CLOSE(safNoDiv.Evaluate(parameters) + bigDivTerm,
                        safBigDiv.Evaluate(parameters), 1e-5);
  }
}

BOOST_AUTO_TEST_CASE(SparseAutoencoderFunctionGradient)
{
  const size_t points = 1000;
  const size_t vSize = 20;
  const size_t hSize = 10;
  const size_t l2 = vSize;
  const size_t l3 = 2 * hSize;

  // Initialize a random dataset.
  arma::mat data;
  data.randu(vSize, points);

  // 3 objects for 3 terms in the cost function. Each term contributes towards
  // the gradient and thus need to be checked independently.
  SAEFG saf1(data, vSize, hSize, 0, 0);
  SAEFG saf2(data, vSize, hSize, 20, 0);
  SAEFG saf3(data, vSize, hSize, 20, 20);

  // Create a random set of parameters.
  arma::mat parameters;
  parameters.randu(l3 + 1, l2 + 1);

  // Get gradients for the current parameters.
  arma::mat gradient1, gradient2, gradient3;
  saf1.Gradient(parameters, gradient1);
  saf2.Gradient(parameters, gradient2);
  saf3.Gradient(parameters, gradient3);

  // Perturbation constant.
  const double epsilon = 0.0001;
  double costPlus1, costMinus1, numGradient1;
  double costPlus2, costMinus2, numGradient2;
  double costPlus3, costMinus3, numGradient3;

  // For each parameter.
  for (size_t i = 0; i <= l3; i++)
  {
    for (size_t j = 0; j <= l2; j++)
    {
      // Perturb parameter with a positive constant and get costs.
      parameters(i, j) += epsilon;
      costPlus1 = saf1.Evaluate(parameters);
      costPlus2 = saf2.Evaluate(parameters);
      costPlus3 = saf3.Evaluate(parameters);

      // Perturb parameter with a negative constant and get costs.
      parameters(i, j) -= 2 * epsilon;
      costMinus1 = saf1.Evaluate(parameters);
      costMinus2 = saf2.Evaluate(parameters);
      costMinus3 = saf3.Evaluate(parameters);

      // Compute numerical gradients using the costs calculated above.
      numGradient1 = (costPlus1 - costMinus1) / (2 * epsilon);
      numGradient2 = (costPlus2 - costMinus2) / (2 * epsilon);
      numGradient3 = (costPlus3 - costMinus3) / (2 * epsilon);

      // Restore the parameter value.
      parameters(i, j) += epsilon;

      // Compare numerical and backpropagation gradient values.
      BOOST_REQUIRE_CLOSE(numGradient1, gradient1(i, j), 1e-2);
      BOOST_REQUIRE_CLOSE(numGradient2, gradient2(i, j), 1e-2);
      BOOST_REQUIRE_CLOSE(numGradient3, gradient3(i, j), 1e-2);
    }
  }
}

BOOST_AUTO_TEST_SUITE_END();
<|endoftext|>


<commit_before>/****************************************************************************
 *
 *   Copyright (c) 2013, 2014 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/**
 * @file airspeed_calibration.cpp
 * Airspeed sensor calibration routine
 */

#include "airspeed_calibration.h"
#include "calibration_messages.h"
#include "commander_helper.h"

#include <stdio.h>
#include <fcntl.h>
#include <poll.h>
#include <math.h>
#include <drivers/drv_hrt.h>
#include <drivers/drv_airspeed.h>
#include <uORB/topics/sensor_combined.h>
#include <uORB/topics/differential_pressure.h>
#include <mavlink/mavlink_log.h>
#include <systemlib/param/param.h>
#include <systemlib/err.h>

/* oddly, ERROR is not defined for c++ */
#ifdef ERROR
# undef ERROR
#endif
static const int ERROR = -1;

static const char *sensor_name = "dpress";

int do_airspeed_calibration(int mavlink_fd)
{
	/* give directions */
	mavlink_log_info(mavlink_fd, CAL_STARTED_MSG, sensor_name);
	mavlink_log_info(mavlink_fd, "ensure airspeed sensor is not registering wind");

	const int calibration_count = 2000;

	int diff_pres_sub = orb_subscribe(ORB_ID(differential_pressure));
	struct differential_pressure_s diff_pres;

	int calibration_counter = 0;
	float diff_pres_offset = 0.0f;

	/* Reset sensor parameters */
	struct airspeed_scale airscale = {
		diff_pres_offset,
		1.0f,
	};

	bool paramreset_successful = false;
	int  fd = open(AIRSPEED_DEVICE_PATH, 0);

	if (fd > 0) {
		if (OK == ioctl(fd, AIRSPEEDIOCSSCALE, (long unsigned int)&airscale)) {
			paramreset_successful = true;

		} else {
			mavlink_log_critical(mavlink_fd, "airspeed offset zero failed");
		}

		close(fd);
	}

	if (!paramreset_successful) {
		warn("FAILED to reset - assuming analog");
		mavlink_log_critical(mavlink_fd, "assuming analog sensor");

		if (param_set(param_find("SENS_DPRES_OFF"), &(diff_pres_offset))) {
			mavlink_log_critical(mavlink_fd, CAL_FAILED_SET_PARAMS_MSG);
			close(diff_pres_sub);
			return ERROR;
		}
	}

	while (calibration_counter < calibration_count) {

		/* wait blocking for new data */
		struct pollfd fds[1];
		fds[0].fd = diff_pres_sub;
		fds[0].events = POLLIN;

		int poll_ret = poll(fds, 1, 1000);

		if (poll_ret) {
			orb_copy(ORB_ID(differential_pressure), diff_pres_sub, &diff_pres);
			diff_pres_offset += diff_pres.differential_pressure_raw_pa;
			calibration_counter++;

			if (calibration_counter % (calibration_count / 20) == 0) {
				mavlink_log_info(mavlink_fd, CAL_PROGRESS_MSG, sensor_name, (calibration_counter * 100) / calibration_count);
			}

		} else if (poll_ret == 0) {
			/* any poll failure for 1s is a reason to abort */
			mavlink_log_critical(mavlink_fd, CAL_FAILED_MSG, sensor_name);
			close(diff_pres_sub);
			return ERROR;
		}
	}

	diff_pres_offset = diff_pres_offset / calibration_count;

	if (isfinite(diff_pres_offset)) {

		if (param_set(param_find("SENS_DPRES_OFF"), &(diff_pres_offset))) {
			mavlink_log_critical(mavlink_fd, CAL_FAILED_SET_PARAMS_MSG);
			close(diff_pres_sub);
			return ERROR;
		}

		/* auto-save to EEPROM */
		int save_ret = param_save_default();

		if (save_ret != 0) {
			warn("WARNING: auto-save of params to storage failed");
			mavlink_log_critical(mavlink_fd, CAL_FAILED_SAVE_PARAMS_MSG);
			close(diff_pres_sub);
			return ERROR;
		}

		mavlink_log_info(mavlink_fd, CAL_DONE_MSG, sensor_name);
		tune_neutral(true);
		close(diff_pres_sub);
		return OK;

	} else {
		mavlink_log_info(mavlink_fd, CAL_FAILED_MSG, sensor_name);
		close(diff_pres_sub);
		return ERROR;
	}
}
<commit_msg>airspeed cal: Improve user feedback<commit_after>/****************************************************************************
 *
 *   Copyright (c) 2013, 2014 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/**
 * @file airspeed_calibration.cpp
 * Airspeed sensor calibration routine
 */

#include "airspeed_calibration.h"
#include "calibration_messages.h"
#include "commander_helper.h"

#include <stdio.h>
#include <fcntl.h>
#include <poll.h>
#include <math.h>
#include <drivers/drv_hrt.h>
#include <drivers/drv_airspeed.h>
#include <uORB/topics/sensor_combined.h>
#include <uORB/topics/differential_pressure.h>
#include <mavlink/mavlink_log.h>
#include <systemlib/param/param.h>
#include <systemlib/err.h>

/* oddly, ERROR is not defined for c++ */
#ifdef ERROR
# undef ERROR
#endif
static const int ERROR = -1;

static const char *sensor_name = "dpress";

int do_airspeed_calibration(int mavlink_fd)
{
	/* give directions */
	mavlink_log_info(mavlink_fd, CAL_STARTED_MSG, sensor_name);
	mavlink_log_info(mavlink_fd, "ensure airspeed sensor is not registering wind");

	const int calibration_count = 2000;

	int diff_pres_sub = orb_subscribe(ORB_ID(differential_pressure));
	struct differential_pressure_s diff_pres;

	int calibration_counter = 0;
	float diff_pres_offset = 0.0f;

	/* Reset sensor parameters */
	struct airspeed_scale airscale = {
		diff_pres_offset,
		1.0f,
	};

	bool paramreset_successful = false;
	int  fd = open(AIRSPEED_DEVICE_PATH, 0);

	if (fd > 0) {
		if (OK == ioctl(fd, AIRSPEEDIOCSSCALE, (long unsigned int)&airscale)) {
			paramreset_successful = true;

		} else {
			mavlink_log_critical(mavlink_fd, "airspeed offset zero failed");
		}

		close(fd);
	}

	if (!paramreset_successful) {
		warn("FAILED to reset - assuming analog");
		mavlink_log_critical(mavlink_fd, "If analog sens, retry with [SENS_DPRES_ANSC=1000]");

		if (param_set(param_find("SENS_DPRES_OFF"), &(diff_pres_offset))) {
			mavlink_log_critical(mavlink_fd, CAL_FAILED_SET_PARAMS_MSG);
			close(diff_pres_sub);
			return ERROR;
		}
	}

	while (calibration_counter < calibration_count) {

		/* wait blocking for new data */
		struct pollfd fds[1];
		fds[0].fd = diff_pres_sub;
		fds[0].events = POLLIN;

		int poll_ret = poll(fds, 1, 1000);

		if (poll_ret) {
			orb_copy(ORB_ID(differential_pressure), diff_pres_sub, &diff_pres);
			diff_pres_offset += diff_pres.differential_pressure_raw_pa;
			calibration_counter++;

			if (calibration_counter % (calibration_count / 20) == 0) {
				mavlink_log_info(mavlink_fd, CAL_PROGRESS_MSG, sensor_name, (calibration_counter * 100) / calibration_count);
			}

		} else if (poll_ret == 0) {
			/* any poll failure for 1s is a reason to abort */
			mavlink_log_critical(mavlink_fd, CAL_FAILED_MSG, sensor_name);
			close(diff_pres_sub);
			return ERROR;
		}
	}

	diff_pres_offset = diff_pres_offset / calibration_count;

	if (isfinite(diff_pres_offset)) {

		if (param_set(param_find("SENS_DPRES_OFF"), &(diff_pres_offset))) {
			mavlink_log_critical(mavlink_fd, CAL_FAILED_SET_PARAMS_MSG);
			close(diff_pres_sub);
			return ERROR;
		}

		/* auto-save to EEPROM */
		int save_ret = param_save_default();

		if (save_ret != 0) {
			warn("WARNING: auto-save of params to storage failed");
			mavlink_log_critical(mavlink_fd, CAL_FAILED_SAVE_PARAMS_MSG);
			close(diff_pres_sub);
			return ERROR;
		}

		mavlink_log_info(mavlink_fd, CAL_DONE_MSG, sensor_name);
		tune_neutral(true);
		close(diff_pres_sub);
		return OK;

	} else {
		mavlink_log_info(mavlink_fd, CAL_FAILED_MSG, sensor_name);
		close(diff_pres_sub);
		return ERROR;
	}
}
<|endoftext|>


<commit_before>#pragma once

#include <eosio/chain/action.hpp>
#include <numeric>

namespace eosio { namespace chain {

   struct deferred_transaction_generation_context : fc::reflect_init {
      static constexpr uint16_t extension_id() { return 0; }
      static constexpr bool     enforce_unique() { return true; }

      deferred_transaction_generation_context() = default;

      deferred_transaction_generation_context( const transaction_id_type& sender_trx_id, uint128_t sender_id, account_name sender )
      :sender_trx_id( sender_trx_id )
      ,sender_id( sender_id )
      ,sender( sender )
      {}

      void reflector_init();

      transaction_id_type sender_trx_id;
      uint128_t           sender_id;
      account_name        sender;
   };

   namespace detail {
      template<typename... Ts>
      struct transaction_extension_types {
         using transaction_extension_t = fc::static_variant< Ts... >;
         using decompose_t = decompose< Ts... >;
      };
   }

   using transaction_extension_types = detail::transaction_extension_types<
      deferred_transaction_generation_context
   >;

   using transaction_extension = transaction_extension_types::transaction_extension_t;

   /**
    *  The transaction header contains the fixed-sized data
    *  associated with each transaction. It is separated from
    *  the transaction body to facilitate partial parsing of
    *  transactions without requiring dynamic memory allocation.
    *
    *  All transactions have an expiration time after which they
    *  may no longer be included in the blockchain. Once a block
    *  with a block_header::timestamp greater than expiration is
    *  deemed irreversible, then a user can safely trust the transaction
    *  will never be included.
    *

    *  Each region is an independent blockchain, it is included as routing
    *  information for inter-blockchain communication. A contract in this
    *  region might generate or authorize a transaction intended for a foreign
    *  region.
    */
   struct transaction_header {
      transaction_header() = default;
      explicit transaction_header( const transaction_header& ) = default;
      transaction_header( transaction_header&& ) = default;
      transaction_header& operator=(const transaction_header&) = delete;
      transaction_header& operator=(transaction_header&&) = default;

      time_point_sec         expiration;   ///< the time at which a transaction expires
      uint16_t               ref_block_num       = 0U; ///< specifies a block num in the last 2^16 blocks.
      uint32_t               ref_block_prefix    = 0UL; ///< specifies the lower 32 bits of the blockid at get_ref_blocknum
      fc::unsigned_int       max_net_usage_words = 0UL; /// upper limit on total network bandwidth (in 8 byte words) billed for this transaction
      uint8_t                max_cpu_usage_ms    = 0; /// upper limit on the total CPU time billed for this transaction
      fc::unsigned_int       delay_sec           = 0UL; /// number of seconds to delay this transaction for during which it may be canceled.

      /**
       * @return the absolute block number given the relative ref_block_num
       */
      block_num_type get_ref_blocknum( block_num_type head_blocknum )const {
         return ((head_blocknum/0xffff)*0xffff) + head_blocknum%0xffff;
      }
      void set_reference_block( const block_id_type& reference_block );
      bool verify_reference_block( const block_id_type& reference_block )const;
      void validate()const;
   };

   /**
    *  A transaction consits of a set of messages which must all be applied or
    *  all are rejected. These messages have access to data within the given
    *  read and write scopes.
    */
   struct transaction : public transaction_header {
      transaction() = default;
      explicit transaction( const transaction& ) = default;
      transaction( transaction&& ) = default;
      transaction& operator=(const transaction&) = delete;
      transaction& operator=(transaction&&) = default;

      vector<action>         context_free_actions;
      vector<action>         actions;
      extensions_type        transaction_extensions;

      transaction_id_type        id()const;
      digest_type                sig_digest( const chain_id_type& chain_id, const vector<bytes>& cfd = vector<bytes>() )const;
      fc::microseconds           get_signature_keys( const vector<signature_type>& signatures,
                                                     const chain_id_type& chain_id,
                                                     fc::time_point deadline,
                                                     const vector<bytes>& cfd,
                                                     flat_set<public_key_type>& recovered_pub_keys,
                                                     bool allow_duplicate_keys = false) const;

      uint32_t total_actions()const { return context_free_actions.size() + actions.size(); }

      account_name first_authorizer()const {
         for( const auto& a : actions ) {
            for( const auto& u : a.authorization )
               return u.actor;
         }
         return account_name();
      }

      flat_multimap<uint16_t, transaction_extension> validate_and_extract_extensions()const;
   };

   struct signed_transaction : public transaction
   {
      signed_transaction() = default;
      explicit signed_transaction( const signed_transaction& ) = default;
      signed_transaction( signed_transaction&& ) = default;
      signed_transaction& operator=(const signed_transaction&) = delete;
      signed_transaction& operator=(signed_transaction&&) = default;
      signed_transaction( transaction trx, vector<signature_type> signatures, vector<bytes> context_free_data)
      : transaction(std::move(trx))
      , signatures(std::move(signatures))
      , context_free_data(std::move(context_free_data))
      {}

      vector<signature_type>    signatures;
      vector<bytes>             context_free_data; ///< for each context-free action, there is an entry here

      const signature_type&     sign(const private_key_type& key, const chain_id_type& chain_id);
      signature_type            sign(const private_key_type& key, const chain_id_type& chain_id)const;
      fc::microseconds          get_signature_keys( const chain_id_type& chain_id, fc::time_point deadline,
                                                    flat_set<public_key_type>& recovered_pub_keys,
                                                    bool allow_duplicate_keys = false )const;
   };

   struct packed_transaction_v0 : fc::reflect_init {
      enum class compression_type {
         none = 0,
         zlib = 1,
      };

      packed_transaction_v0() = default;
      packed_transaction_v0(packed_transaction_v0&&) = default;
      explicit packed_transaction_v0(const packed_transaction_v0&) = default;
      packed_transaction_v0& operator=(const packed_transaction_v0&) = delete;
      packed_transaction_v0& operator=(packed_transaction_v0&&) = default;

      explicit packed_transaction_v0(const signed_transaction& t, compression_type _compression = compression_type::none)
      :signatures(t.signatures), compression(_compression), unpacked_trx(t), trx_id(unpacked_trx.id())
      {
         local_pack_transaction();
         local_pack_context_free_data();
      }

      explicit packed_transaction_v0(signed_transaction&& t, compression_type _compression = compression_type::none)
      :signatures(t.signatures), compression(_compression), unpacked_trx(std::move(t)), trx_id(unpacked_trx.id())
      {
         local_pack_transaction();
         local_pack_context_free_data();
      }

      packed_transaction_v0(const bytes& packed_txn, const vector<signature_type>& sigs, const bytes& packed_cfd, compression_type _compression);

      // used by abi_serializer
      packed_transaction_v0( bytes&& packed_txn, vector<signature_type>&& sigs, bytes&& packed_cfd, compression_type _compression );
      packed_transaction_v0( bytes&& packed_txn, vector<signature_type>&& sigs, vector<bytes>&& cfd, compression_type _compression );
      packed_transaction_v0( transaction&& t, vector<signature_type>&& sigs, bytes&& packed_cfd, compression_type _compression );

      uint32_t get_unprunable_size()const;
      uint32_t get_prunable_size()const;

      digest_type packed_digest()const;

      const transaction_id_type& id()const { return trx_id; }

      time_point_sec                expiration()const { return unpacked_trx.expiration; }
      const vector<bytes>&          get_context_free_data()const { return unpacked_trx.context_free_data; }
      const transaction&            get_transaction()const { return unpacked_trx; }
      const signed_transaction&     get_signed_transaction()const { return unpacked_trx; }
      const vector<signature_type>& get_signatures()const { return signatures; }
      const fc::enum_type<uint8_t,compression_type>& get_compression()const { return compression; }
      const bytes&                  get_packed_context_free_data()const { return packed_context_free_data; }
      const bytes&                  get_packed_transaction()const { return packed_trx; }

   private:
      void local_unpack_transaction(vector<bytes>&& context_free_data);
      void local_unpack_context_free_data();
      void local_pack_transaction();
      void local_pack_context_free_data();

      friend struct fc::reflector<packed_transaction_v0>;
      friend struct fc::reflector_init_visitor<packed_transaction_v0>;
      friend struct fc::has_reflector_init<packed_transaction_v0>;
      friend struct packed_transaction;
      void reflector_init();
   private:
     friend struct pruned_transaction;
     friend struct prunable_transaction_data;
     vector<signature_type>                   signatures;
     fc::enum_type<uint8_t, compression_type> compression;
     bytes                                    packed_context_free_data;
     bytes                                    packed_trx;

   private:
      // cache unpacked trx, for thread safety do not modify after construction
      signed_transaction                      unpacked_trx;
      transaction_id_type                     trx_id;
   };

   using packed_transaction_v0_ptr = std::shared_ptr<const packed_transaction_v0>;

   struct prunable_transaction_data {
      enum class compression_type : uint8_t {
         none = 0,
         zlib = 1,
         COMPRESSION_TYPE_COUNT
      };
      // Do not exceed 127 as that will break compatibility in serialization format
      static_assert( static_cast<uint8_t>(compression_type::COMPRESSION_TYPE_COUNT) <= 127 );

      struct none {
         digest_type                     prunable_digest;
      };

      struct signatures_only {
         std::vector<signature_type>     signatures;
         digest_type                     context_free_mroot;
      };

      using segment_type = fc::static_variant<digest_type, bytes>;

      struct partial {
         std::vector<signature_type>     signatures;
         std::vector<segment_type>       context_free_segments;
      };

      struct full {
         std::vector<signature_type>     signatures;
         std::vector<bytes>              context_free_segments;
      };

      struct full_legacy {
         std::vector<signature_type>     signatures;
         bytes                           packed_context_free_data;
         vector<bytes>                   context_free_segments;
      };

      using prunable_data_type = fc::static_variant< full_legacy,
                                                     none,
                                                     signatures_only,
                                                     partial,
                                                     full >;

      prunable_transaction_data prune_all() const;
      digest_type digest() const;

      // Returns the maximum pack size of any prunable_transaction_data that is reachable
      // by pruning this object.
      std::size_t maximum_pruned_pack_size( compression_type segment_compression ) const;

      prunable_data_type  prunable_data;
   };

   struct packed_transaction : fc::reflect_init {
      using compression_type = packed_transaction_v0::compression_type;
      using cf_compression_type = prunable_transaction_data::compression_type;

      packed_transaction() = default;
      packed_transaction(packed_transaction&&) = default;
      explicit packed_transaction(const packed_transaction&) = default;
      packed_transaction& operator=(const packed_transaction&) = delete;
      packed_transaction& operator=(packed_transaction&&) = default;

      packed_transaction(const packed_transaction_v0& other, bool legacy);
      packed_transaction(packed_transaction_v0&& other, bool legacy);
      explicit packed_transaction(signed_transaction t, bool legacy, compression_type _compression = compression_type::none);

      packed_transaction_v0_ptr to_packed_transaction_v0() const;

      uint32_t get_unprunable_size()const;
      uint32_t get_prunable_size()const;
      uint32_t get_estimated_size()const { return estimated_size; }

      digest_type packed_digest()const;

      const transaction_id_type& id()const { return trx_id; }

      time_point_sec                expiration()const { return unpacked_trx.expiration; }
      const transaction&            get_transaction()const { return unpacked_trx; }
      // Returns nullptr if the signatures were pruned
      const vector<signature_type>* get_signatures()const;
      // Returns nullptr if any context_free_data segment was pruned
      const vector<bytes>*          get_context_free_data()const;
      // Returns nullptr if the context_free_data segment was pruned or segment_ordinal is out of range.
      const bytes*                  get_context_free_data(std::size_t segment_ordinal)const;
      const fc::enum_type<uint8_t,compression_type>& get_compression()const { return compression; }
      const bytes&                  get_packed_transaction()const { return packed_trx; }
      const prunable_transaction_data& get_prunable_data() const { return prunable_data; }

      void prune_all();

      std::size_t maximum_pruned_pack_size( cf_compression_type segment_compression ) const;

   private:
      friend struct fc::reflector<packed_transaction>;
      friend struct fc::reflector_init_visitor<packed_transaction>;
      friend struct fc::has_reflector_init<packed_transaction>;
      void reflector_init();
      uint32_t calculate_estimated_size() const;
   private:
      uint32_t                                estimated_size = 0;
      fc::enum_type<uint8_t,compression_type> compression;
      prunable_transaction_data               prunable_data;
      bytes                                   packed_trx; // packed and compressed (according to compression) transaction

   private:
      // cache unpacked trx, for thread safety do not modify any attributes after construction
      transaction                             unpacked_trx;
      transaction_id_type                     trx_id;
   };

   using packed_transaction_ptr = std::shared_ptr<const packed_transaction>;

   uint128_t transaction_id_to_sender_id( const transaction_id_type& tid );

} } /// namespace eosio::chain

FC_REFLECT(eosio::chain::deferred_transaction_generation_context, (sender_trx_id)(sender_id)(sender) )
FC_REFLECT( eosio::chain::transaction_header, (expiration)(ref_block_num)(ref_block_prefix)
                                              (max_net_usage_words)(max_cpu_usage_ms)(delay_sec) )
FC_REFLECT_DERIVED( eosio::chain::transaction, (eosio::chain::transaction_header), (context_free_actions)(actions)(transaction_extensions) )
FC_REFLECT_DERIVED( eosio::chain::signed_transaction, (eosio::chain::transaction), (signatures)(context_free_data) )
FC_REFLECT_ENUM( eosio::chain::packed_transaction_v0::compression_type, (none)(zlib))
// @ignore unpacked_trx
FC_REFLECT( eosio::chain::packed_transaction_v0, (signatures)(compression)(packed_context_free_data)(packed_trx) )
FC_REFLECT( eosio::chain::packed_transaction, (compression)(prunable_data)(packed_trx) )
FC_REFLECT( eosio::chain::prunable_transaction_data, (prunable_data));
FC_REFLECT( eosio::chain::prunable_transaction_data::none, (prunable_digest))
FC_REFLECT( eosio::chain::prunable_transaction_data::signatures_only, (signatures)(context_free_mroot))
FC_REFLECT( eosio::chain::prunable_transaction_data::partial, (signatures)(context_free_segments))
FC_REFLECT( eosio::chain::prunable_transaction_data::full, (signatures)(context_free_segments))
FC_REFLECT( eosio::chain::prunable_transaction_data::full_legacy, (signatures)(packed_context_free_data))
<commit_msg>Remove unused method<commit_after>#pragma once

#include <eosio/chain/action.hpp>
#include <numeric>

namespace eosio { namespace chain {

   struct deferred_transaction_generation_context : fc::reflect_init {
      static constexpr uint16_t extension_id() { return 0; }
      static constexpr bool     enforce_unique() { return true; }

      deferred_transaction_generation_context() = default;

      deferred_transaction_generation_context( const transaction_id_type& sender_trx_id, uint128_t sender_id, account_name sender )
      :sender_trx_id( sender_trx_id )
      ,sender_id( sender_id )
      ,sender( sender )
      {}

      void reflector_init();

      transaction_id_type sender_trx_id;
      uint128_t           sender_id;
      account_name        sender;
   };

   namespace detail {
      template<typename... Ts>
      struct transaction_extension_types {
         using transaction_extension_t = fc::static_variant< Ts... >;
         using decompose_t = decompose< Ts... >;
      };
   }

   using transaction_extension_types = detail::transaction_extension_types<
      deferred_transaction_generation_context
   >;

   using transaction_extension = transaction_extension_types::transaction_extension_t;

   /**
    *  The transaction header contains the fixed-sized data
    *  associated with each transaction. It is separated from
    *  the transaction body to facilitate partial parsing of
    *  transactions without requiring dynamic memory allocation.
    *
    *  All transactions have an expiration time after which they
    *  may no longer be included in the blockchain. Once a block
    *  with a block_header::timestamp greater than expiration is
    *  deemed irreversible, then a user can safely trust the transaction
    *  will never be included.
    *

    *  Each region is an independent blockchain, it is included as routing
    *  information for inter-blockchain communication. A contract in this
    *  region might generate or authorize a transaction intended for a foreign
    *  region.
    */
   struct transaction_header {
      transaction_header() = default;
      explicit transaction_header( const transaction_header& ) = default;
      transaction_header( transaction_header&& ) = default;
      transaction_header& operator=(const transaction_header&) = delete;
      transaction_header& operator=(transaction_header&&) = default;

      time_point_sec         expiration;   ///< the time at which a transaction expires
      uint16_t               ref_block_num       = 0U; ///< specifies a block num in the last 2^16 blocks.
      uint32_t               ref_block_prefix    = 0UL; ///< specifies the lower 32 bits of the blockid at get_ref_blocknum
      fc::unsigned_int       max_net_usage_words = 0UL; /// upper limit on total network bandwidth (in 8 byte words) billed for this transaction
      uint8_t                max_cpu_usage_ms    = 0; /// upper limit on the total CPU time billed for this transaction
      fc::unsigned_int       delay_sec           = 0UL; /// number of seconds to delay this transaction for during which it may be canceled.

      void set_reference_block( const block_id_type& reference_block );
      bool verify_reference_block( const block_id_type& reference_block )const;
      void validate()const;
   };

   /**
    *  A transaction consits of a set of messages which must all be applied or
    *  all are rejected. These messages have access to data within the given
    *  read and write scopes.
    */
   struct transaction : public transaction_header {
      transaction() = default;
      explicit transaction( const transaction& ) = default;
      transaction( transaction&& ) = default;
      transaction& operator=(const transaction&) = delete;
      transaction& operator=(transaction&&) = default;

      vector<action>         context_free_actions;
      vector<action>         actions;
      extensions_type        transaction_extensions;

      transaction_id_type        id()const;
      digest_type                sig_digest( const chain_id_type& chain_id, const vector<bytes>& cfd = vector<bytes>() )const;
      fc::microseconds           get_signature_keys( const vector<signature_type>& signatures,
                                                     const chain_id_type& chain_id,
                                                     fc::time_point deadline,
                                                     const vector<bytes>& cfd,
                                                     flat_set<public_key_type>& recovered_pub_keys,
                                                     bool allow_duplicate_keys = false) const;

      uint32_t total_actions()const { return context_free_actions.size() + actions.size(); }

      account_name first_authorizer()const {
         for( const auto& a : actions ) {
            for( const auto& u : a.authorization )
               return u.actor;
         }
         return account_name();
      }

      flat_multimap<uint16_t, transaction_extension> validate_and_extract_extensions()const;
   };

   struct signed_transaction : public transaction
   {
      signed_transaction() = default;
      explicit signed_transaction( const signed_transaction& ) = default;
      signed_transaction( signed_transaction&& ) = default;
      signed_transaction& operator=(const signed_transaction&) = delete;
      signed_transaction& operator=(signed_transaction&&) = default;
      signed_transaction( transaction trx, vector<signature_type> signatures, vector<bytes> context_free_data)
      : transaction(std::move(trx))
      , signatures(std::move(signatures))
      , context_free_data(std::move(context_free_data))
      {}

      vector<signature_type>    signatures;
      vector<bytes>             context_free_data; ///< for each context-free action, there is an entry here

      const signature_type&     sign(const private_key_type& key, const chain_id_type& chain_id);
      signature_type            sign(const private_key_type& key, const chain_id_type& chain_id)const;
      fc::microseconds          get_signature_keys( const chain_id_type& chain_id, fc::time_point deadline,
                                                    flat_set<public_key_type>& recovered_pub_keys,
                                                    bool allow_duplicate_keys = false )const;
   };

   struct packed_transaction_v0 : fc::reflect_init {
      enum class compression_type {
         none = 0,
         zlib = 1,
      };

      packed_transaction_v0() = default;
      packed_transaction_v0(packed_transaction_v0&&) = default;
      explicit packed_transaction_v0(const packed_transaction_v0&) = default;
      packed_transaction_v0& operator=(const packed_transaction_v0&) = delete;
      packed_transaction_v0& operator=(packed_transaction_v0&&) = default;

      explicit packed_transaction_v0(const signed_transaction& t, compression_type _compression = compression_type::none)
      :signatures(t.signatures), compression(_compression), unpacked_trx(t), trx_id(unpacked_trx.id())
      {
         local_pack_transaction();
         local_pack_context_free_data();
      }

      explicit packed_transaction_v0(signed_transaction&& t, compression_type _compression = compression_type::none)
      :signatures(t.signatures), compression(_compression), unpacked_trx(std::move(t)), trx_id(unpacked_trx.id())
      {
         local_pack_transaction();
         local_pack_context_free_data();
      }

      packed_transaction_v0(const bytes& packed_txn, const vector<signature_type>& sigs, const bytes& packed_cfd, compression_type _compression);

      // used by abi_serializer
      packed_transaction_v0( bytes&& packed_txn, vector<signature_type>&& sigs, bytes&& packed_cfd, compression_type _compression );
      packed_transaction_v0( bytes&& packed_txn, vector<signature_type>&& sigs, vector<bytes>&& cfd, compression_type _compression );
      packed_transaction_v0( transaction&& t, vector<signature_type>&& sigs, bytes&& packed_cfd, compression_type _compression );

      uint32_t get_unprunable_size()const;
      uint32_t get_prunable_size()const;

      digest_type packed_digest()const;

      const transaction_id_type& id()const { return trx_id; }

      time_point_sec                expiration()const { return unpacked_trx.expiration; }
      const vector<bytes>&          get_context_free_data()const { return unpacked_trx.context_free_data; }
      const transaction&            get_transaction()const { return unpacked_trx; }
      const signed_transaction&     get_signed_transaction()const { return unpacked_trx; }
      const vector<signature_type>& get_signatures()const { return signatures; }
      const fc::enum_type<uint8_t,compression_type>& get_compression()const { return compression; }
      const bytes&                  get_packed_context_free_data()const { return packed_context_free_data; }
      const bytes&                  get_packed_transaction()const { return packed_trx; }

   private:
      void local_unpack_transaction(vector<bytes>&& context_free_data);
      void local_unpack_context_free_data();
      void local_pack_transaction();
      void local_pack_context_free_data();

      friend struct fc::reflector<packed_transaction_v0>;
      friend struct fc::reflector_init_visitor<packed_transaction_v0>;
      friend struct fc::has_reflector_init<packed_transaction_v0>;
      friend struct packed_transaction;
      void reflector_init();
   private:
     friend struct pruned_transaction;
     friend struct prunable_transaction_data;
     vector<signature_type>                   signatures;
     fc::enum_type<uint8_t, compression_type> compression;
     bytes                                    packed_context_free_data;
     bytes                                    packed_trx;

   private:
      // cache unpacked trx, for thread safety do not modify after construction
      signed_transaction                      unpacked_trx;
      transaction_id_type                     trx_id;
   };

   using packed_transaction_v0_ptr = std::shared_ptr<const packed_transaction_v0>;

   struct prunable_transaction_data {
      enum class compression_type : uint8_t {
         none = 0,
         zlib = 1,
         COMPRESSION_TYPE_COUNT
      };
      // Do not exceed 127 as that will break compatibility in serialization format
      static_assert( static_cast<uint8_t>(compression_type::COMPRESSION_TYPE_COUNT) <= 127 );

      struct none {
         digest_type                     prunable_digest;
      };

      struct signatures_only {
         std::vector<signature_type>     signatures;
         digest_type                     context_free_mroot;
      };

      using segment_type = fc::static_variant<digest_type, bytes>;

      struct partial {
         std::vector<signature_type>     signatures;
         std::vector<segment_type>       context_free_segments;
      };

      struct full {
         std::vector<signature_type>     signatures;
         std::vector<bytes>              context_free_segments;
      };

      struct full_legacy {
         std::vector<signature_type>     signatures;
         bytes                           packed_context_free_data;
         vector<bytes>                   context_free_segments;
      };

      using prunable_data_type = fc::static_variant< full_legacy,
                                                     none,
                                                     signatures_only,
                                                     partial,
                                                     full >;

      prunable_transaction_data prune_all() const;
      digest_type digest() const;

      // Returns the maximum pack size of any prunable_transaction_data that is reachable
      // by pruning this object.
      std::size_t maximum_pruned_pack_size( compression_type segment_compression ) const;

      prunable_data_type  prunable_data;
   };

   struct packed_transaction : fc::reflect_init {
      using compression_type = packed_transaction_v0::compression_type;
      using cf_compression_type = prunable_transaction_data::compression_type;

      packed_transaction() = default;
      packed_transaction(packed_transaction&&) = default;
      explicit packed_transaction(const packed_transaction&) = default;
      packed_transaction& operator=(const packed_transaction&) = delete;
      packed_transaction& operator=(packed_transaction&&) = default;

      packed_transaction(const packed_transaction_v0& other, bool legacy);
      packed_transaction(packed_transaction_v0&& other, bool legacy);
      explicit packed_transaction(signed_transaction t, bool legacy, compression_type _compression = compression_type::none);

      packed_transaction_v0_ptr to_packed_transaction_v0() const;

      uint32_t get_unprunable_size()const;
      uint32_t get_prunable_size()const;
      uint32_t get_estimated_size()const { return estimated_size; }

      digest_type packed_digest()const;

      const transaction_id_type& id()const { return trx_id; }

      time_point_sec                expiration()const { return unpacked_trx.expiration; }
      const transaction&            get_transaction()const { return unpacked_trx; }
      // Returns nullptr if the signatures were pruned
      const vector<signature_type>* get_signatures()const;
      // Returns nullptr if any context_free_data segment was pruned
      const vector<bytes>*          get_context_free_data()const;
      // Returns nullptr if the context_free_data segment was pruned or segment_ordinal is out of range.
      const bytes*                  get_context_free_data(std::size_t segment_ordinal)const;
      const fc::enum_type<uint8_t,compression_type>& get_compression()const { return compression; }
      const bytes&                  get_packed_transaction()const { return packed_trx; }
      const prunable_transaction_data& get_prunable_data() const { return prunable_data; }

      void prune_all();

      std::size_t maximum_pruned_pack_size( cf_compression_type segment_compression ) const;

   private:
      friend struct fc::reflector<packed_transaction>;
      friend struct fc::reflector_init_visitor<packed_transaction>;
      friend struct fc::has_reflector_init<packed_transaction>;
      void reflector_init();
      uint32_t calculate_estimated_size() const;
   private:
      uint32_t                                estimated_size = 0;
      fc::enum_type<uint8_t,compression_type> compression;
      prunable_transaction_data               prunable_data;
      bytes                                   packed_trx; // packed and compressed (according to compression) transaction

   private:
      // cache unpacked trx, for thread safety do not modify any attributes after construction
      transaction                             unpacked_trx;
      transaction_id_type                     trx_id;
   };

   using packed_transaction_ptr = std::shared_ptr<const packed_transaction>;

   uint128_t transaction_id_to_sender_id( const transaction_id_type& tid );

} } /// namespace eosio::chain

FC_REFLECT(eosio::chain::deferred_transaction_generation_context, (sender_trx_id)(sender_id)(sender) )
FC_REFLECT( eosio::chain::transaction_header, (expiration)(ref_block_num)(ref_block_prefix)
                                              (max_net_usage_words)(max_cpu_usage_ms)(delay_sec) )
FC_REFLECT_DERIVED( eosio::chain::transaction, (eosio::chain::transaction_header), (context_free_actions)(actions)(transaction_extensions) )
FC_REFLECT_DERIVED( eosio::chain::signed_transaction, (eosio::chain::transaction), (signatures)(context_free_data) )
FC_REFLECT_ENUM( eosio::chain::packed_transaction_v0::compression_type, (none)(zlib))
// @ignore unpacked_trx
FC_REFLECT( eosio::chain::packed_transaction_v0, (signatures)(compression)(packed_context_free_data)(packed_trx) )
FC_REFLECT( eosio::chain::packed_transaction, (compression)(prunable_data)(packed_trx) )
FC_REFLECT( eosio::chain::prunable_transaction_data, (prunable_data));
FC_REFLECT( eosio::chain::prunable_transaction_data::none, (prunable_digest))
FC_REFLECT( eosio::chain::prunable_transaction_data::signatures_only, (signatures)(context_free_mroot))
FC_REFLECT( eosio::chain::prunable_transaction_data::partial, (signatures)(context_free_segments))
FC_REFLECT( eosio::chain::prunable_transaction_data::full, (signatures)(context_free_segments))
FC_REFLECT( eosio::chain::prunable_transaction_data::full_legacy, (signatures)(packed_context_free_data))
<|endoftext|>


<commit_before><commit_msg>Fix a browser crash when an extension with multiple active pages crashes.<commit_after><|endoftext|>


<commit_before><commit_msg>Fix a race bug where content scripts would not apply to the first page load.<commit_after><|endoftext|>


<commit_before>// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/simple_message_box.h"

#include "base/message_loop.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/ui/gtk/gtk_util.h"

namespace {

void SetDialogTitle(GtkWidget* dialog, const string16& title) {
  gtk_window_set_title(GTK_WINDOW(dialog), UTF16ToUTF8(title).c_str());

  // The following code requires the dialog to be realized.
  gtk_widget_realize(dialog);

  // Make sure it's big enough to show the title.
  GtkRequisition req;
  gtk_widget_size_request(dialog, &req);
  int width;
  gtk_util::GetWidgetSizeFromCharacters(dialog, title.length(), 0,
                                        &width, NULL);
  // The fudge factor accounts for extra space needed by the frame
  // decorations as well as width differences between average text and the
  // actual title text.
  width = width * 1.2 + 50;

  if (width > req.width)
    gtk_widget_set_size_request(dialog, width, -1);
}

int g_dialog_response;

void HandleOnResponseDialog(GtkWidget* widget, int response, void* user_data) {
  g_dialog_response = response;
  gtk_widget_destroy(widget);
  MessageLoop::current()->QuitNow();
}

}  // namespace

namespace browser {

void ShowErrorBox(gfx::NativeWindow parent,
                  const string16& title,
                  const string16& message) {
  GtkWidget* dialog = gtk_message_dialog_new(parent,
                                             GTK_DIALOG_MODAL,
                                             GTK_MESSAGE_ERROR,
                                             GTK_BUTTONS_OK,
                                             "%s",
                                             UTF16ToUTF8(message).c_str());
  gtk_util::ApplyMessageDialogQuirks(dialog);
  SetDialogTitle(dialog, title);

  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
  g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), NULL);
  gtk_util::ShowDialog(dialog);
}

bool ShowYesNoBox(gfx::NativeWindow parent,
                  const string16& title,
                  const string16& message) {
  GtkWidget* dialog = gtk_message_dialog_new(parent,
                                             GTK_DIALOG_MODAL,
                                             GTK_MESSAGE_QUESTION,
                                             GTK_BUTTONS_YES_NO,
                                             "%s",
                                             UTF16ToUTF8(message).c_str());
  gtk_util::ApplyMessageDialogQuirks(dialog);
  SetDialogTitle(dialog, title);

  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_YES);
  g_signal_connect(dialog,
                   "response",
                   G_CALLBACK(HandleOnResponseDialog),
                   NULL);
  gtk_util::ShowDialog(dialog);
  // Not gtk_dialog_run as it prevents timers from running in the unit tests.
  MessageLoop::current()->Run();
  return g_dialog_response == GTK_RESPONSE_YES;
}

}  // namespace browser
<commit_msg>gtk: Change the message type of the ShowErrorBox() function to "warning".<commit_after>// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/simple_message_box.h"

#include "base/message_loop.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/ui/gtk/gtk_util.h"

namespace {

void SetDialogTitle(GtkWidget* dialog, const string16& title) {
  gtk_window_set_title(GTK_WINDOW(dialog), UTF16ToUTF8(title).c_str());

  // The following code requires the dialog to be realized.
  gtk_widget_realize(dialog);

  // Make sure it's big enough to show the title.
  GtkRequisition req;
  gtk_widget_size_request(dialog, &req);
  int width;
  gtk_util::GetWidgetSizeFromCharacters(dialog, title.length(), 0,
                                        &width, NULL);
  // The fudge factor accounts for extra space needed by the frame
  // decorations as well as width differences between average text and the
  // actual title text.
  width = width * 1.2 + 50;

  if (width > req.width)
    gtk_widget_set_size_request(dialog, width, -1);
}

int g_dialog_response;

void HandleOnResponseDialog(GtkWidget* widget, int response, void* user_data) {
  g_dialog_response = response;
  gtk_widget_destroy(widget);
  MessageLoop::current()->QuitNow();
}

}  // namespace

namespace browser {

void ShowErrorBox(gfx::NativeWindow parent,
                  const string16& title,
                  const string16& message) {
  GtkWidget* dialog = gtk_message_dialog_new(parent,
                                             GTK_DIALOG_MODAL,
                                             GTK_MESSAGE_WARNING,
                                             GTK_BUTTONS_OK,
                                             "%s",
                                             UTF16ToUTF8(message).c_str());
  gtk_util::ApplyMessageDialogQuirks(dialog);
  SetDialogTitle(dialog, title);

  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
  g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), NULL);
  gtk_util::ShowDialog(dialog);
}

bool ShowYesNoBox(gfx::NativeWindow parent,
                  const string16& title,
                  const string16& message) {
  GtkWidget* dialog = gtk_message_dialog_new(parent,
                                             GTK_DIALOG_MODAL,
                                             GTK_MESSAGE_QUESTION,
                                             GTK_BUTTONS_YES_NO,
                                             "%s",
                                             UTF16ToUTF8(message).c_str());
  gtk_util::ApplyMessageDialogQuirks(dialog);
  SetDialogTitle(dialog, title);

  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_YES);
  g_signal_connect(dialog,
                   "response",
                   G_CALLBACK(HandleOnResponseDialog),
                   NULL);
  gtk_util::ShowDialog(dialog);
  // Not gtk_dialog_run as it prevents timers from running in the unit tests.
  MessageLoop::current()->Run();
  return g_dialog_response == GTK_RESPONSE_YES;
}

}  // namespace browser
<|endoftext|>