示例#1
0
unit_t ProfilePlotView::unitForQuantity(quantity_t q) const
{
	QSettings s;
	s.beginGroup("Settings");
	QVariant uname = s.value(QString("Unit%1").arg(q));
	s.endGroup();

	try
	{
		if (! uname.isValid())
			return findUnit(q, 0);
		else
			return findUnit(q, (const char *)uname.toByteArray());
	}
	catch (std::runtime_error & e)
	{
	}

	return unit_t();
}
示例#2
0
namespace adobe {

/****************************************************************************************************/

/*!
    \ingroup apl_widgets_number_unit

    This struct is a decoration utility used by widgets that need to
    format number information for the user. As it stands the struct is
    to describe a single linear transformation of a number from its base
    unit to this unit. For instance, a cell in the property model may
    hold a number in units inches, and a unit_t decorator for a
    display_number_t would detail how to convert that value to units
    centimeters by means of a linear transformation. The
    display_number_t will select the most appropriate linear
    transformation of its set of possibilities when displaying the value
    from the property model.
*/

struct unit_t
{
    /*!
        Default constructor. Yay.
    */
    unit_t() :
                base_unit_id_m(0),
        decimal_places_m(0),    
                trailing_zeroes_m(false),
                format_m("#"),
        increment_m(1),
        scale_m_m(1),
        scale_b_m(0),
        min_value_m((std::numeric_limits<double>::min)()),
        max_value_m((std::numeric_limits<double>::max)())
    { }

    /// this is the name of the unit this linear transformation represents
    std::string name_m;
        /// this is the short name  of the unit this linear transformation represents (ie cm for centimeters) 
        std::string short_name_m;
        /// this is the name of the "base unit" group to which this linear transformation is to relate
        adobe::name_t base_unit_filter_m;
        /// this is the name of the "base unit" group to which this linear transformation is to relate
        adobe::name_t base_unit_m;
        // Unique id for base unit
        unsigned int base_unit_id_m;    /// this is the name of the "base unit" group to which this linear transformation is to relate

    /// this will be the formatting information for the result of the transformation instead of format_m
    unsigned int decimal_places_m;
        bool trailing_zeroes_m;

    /// legacy format printf-style string
        std::string format_m;   

    /// in some instances the user is able to increment the value. This specifies the amount (in this unit) that the user can increment the value.
    double increment_m;

    /// multiplier to go from this unit to the base unit using y = mx + b
    double scale_m_m;

    /// y-intersect to go from this unit to the base unit using y = mx + b
    double scale_b_m; 

    /// optional minimum value for this unit -- the value cannot go below this value when this unit represents it.
    double min_value_m;

    /// optional maximum value for this unit -- the value cannot go above this value when this unit represents it.
    double max_value_m;
};

/*!
    \ingroup apl_widgets_number_unit

    \param dict is the dictionary holding relevant unit information within.
    \param default_unit contains values for the resultant unit should they not be defined in \c dict.

    \return a complete unit_t as a result of the merging of the two parameters.

    \par Dictionary Values
    The values extracted from the dictionary are the following:
    <table>
    <tr>
        <th>key</th>
        <th>value type</th>
        <th>default value</th>
        <th>description</th>
    </tr>
    <tr>
        <td><code>name</code></td>
        <td><code>string</code></td>
        <td>""</td>
        <td>Name of this unit</td>
    </tr>
    <tr>
        <td><code>bind</code></td>
        <td><code>name</code></td>
        <td><i>empty</i></td>
        <td>Name of base unit to which this unit is bound</td>
    </tr>
    <tr>
        <td><code>format</code></td>
        <td><code>string</code></td>
        <td><code>"#.00"</code></td>
        <td>printf-style format for the widget when this unit is selected</td>
    </tr>
    <tr>
        <td><code>increment</code></td>
        <td><code>double</code></td>
        <td><code>1</code></td>
        <td>amount by which the property model value should be increemented when this unit is selected</td>
    </tr>
    <tr>
        <td><code>scale</code></td>
        <td><code>array of two doubles</code></td>
        <td><code>[ 1.0, 0.0 ]</code></td>
        <td>
            Scale factor for the property model's value to this specified unit.
            This allows you to represent a given cell (say, <code>\@width_inches</code>) in an arbitrary unit given a linear conversion. The linear formula is <code>y = a(x) + b</code>, so the default settings are simply <code>y = x</code>. As an example, if you cell is saving values in unit inches and you want to display your value in centimeters, the scale would be <code>[ 2.54, 0.0 ]</code>.
        </td>
    </tr>
    <tr>
        <td><code>min_value</code></td>
        <td><code>double</code></td>
        <td><i>N/A</i></td>
        <td>Optional. Minimum value allowed for this unit. Note that if you have a value that can be represented as multiple units, you have to specify a minimum value for each unit.</td>
    </tr>
    <tr>
        <td><code>max_value</code></td>
        <td><code>double</code></td>
        <td><i>N/A</i></td>
        <td>Optional. Maximum value allowed for this unit. Note that if you have a value that can be represented as multiple units, you have to specify a maximum value for each unit.</td>
    </tr>
*/
unit_t to_unit(const dictionary_t& dict,  const unit_t& default_unit = unit_t());

/*!
    \ingroup apl_widgets_number_unit

    Performs a linear transformation on a base value given a unit.

    \param base_value is the value to be converted to the scaled value
    \param unit is the unit by which the base value is to be transformed

    \return the scaled value according to the unit_t's linear transformation.
*/
inline double to_scaled_value(double base_value, const unit_t& unit)
{ return (unit.scale_m_m * base_value) + unit.scale_b_m; }

/*!
    \ingroup apl_widgets_number_unit

    Performs an inverse-linear transformation on a scaled value given a unit.

    \param scaled_value is the value to be converted to the base value
    \param unit is the unit by which the scaled value is to be transformed

    \return the base value according to the unit_t's linear transformation.
*/
inline double to_base_value(double scaled_value, const unit_t& unit)
{ return (scaled_value - unit.scale_b_m) / unit.scale_m_m; }

/*!
    \ingroup apl_widgets_number_unit

    Performs an inverse-linear transformation on a scaled value given a
    unit. It will also pin the value after the transformation has taken
    place to the unit_t's specified minimum and maximum values.

    \todo the min and max values should come from the unit_t instead of parameters.

    \param scaled_value is the value to be converted to the base value
    \param unit is the unit by which the scaled value is to be transformed
    \param min is the minimum value allowed for the result
    \param max is the maximumn value allowed for the result

    \return the pinned base value according to the unit_t's linear transformation.
*/
inline double to_pinned_base_value(double               scaled_value,
                                   const unit_t& unit,
                                   double               min,
                                   double               max)
{
    double base(to_base_value(scaled_value, unit));

    if (min == (std::numeric_limits<double>::min)())
        min = base;

    if (max == (std::numeric_limits<double>::max)())
        max = base;

    return adobe::clamp(base, min, max);
}

/****************************************************************************************************/

} //namespace adobe
示例#3
0
文件: smooth.hpp 项目: ybouret/yocto4
            //! compute the approximation of Y[i]@X[i]
            inline T compute(const expand<T> &xp,
                             const unit_t     i,
                             const array<T>  &X,
                             const array<T>  &Y,
                             const unit_t     N,
                             T               *dYdX=0)
            {
                assert(N>0);
                assert(X.size()==Y.size());
                assert(unit_t(X.size())==N);

                //______________________________________________________________
                //
                // prepare with central point
                //______________________________________________________________
                const T Xc = xp.get_x(i,X,N);
                const T Yc = xp.get_y(i,Y,N);
                points.free();
                push_back(0,Yc);

                //______________________________________________________________
                //
                // add next points
                //______________________________________________________________
                {
                    const T Xup = Xc + max_of<T>(0,upper_range);
                    unit_t  iup = i;
                    size_t  cnt = 0;
                    while(true)
                    {
                        const T Xi=xp.get_x(++iup,X,N);
                        if(Xi>Xup&&cnt>0) break;
                        ++cnt;
                        push_back(Xi-Xc,xp.get_y(iup,Y,N));
                    }
                }

                //______________________________________________________________
                //
                // add prev points
                //______________________________________________________________
                {
                    const T Xdn = Xc - max_of<T>(0,lower_range);
                    unit_t  idn = i;
                    size_t  cnt = 0;
                    while(true)
                    {
                        const T Xi=xp.get_x(--idn,X,N);
                        if(Xi<Xdn&&cnt>0) break;
                        ++cnt;
                        push_front(Xi-Xc,xp.get_y(idn,Y,N));
                    }
                }

                //______________________________________________________________
                //
                // use local fit
                //______________________________________________________________
                polynomial();
                if(dYdX)
                {
                    *dYdX = coeff[2];
                }
                return coeff[1];
            }
示例#4
0
文件: types.hpp 项目: ybouret/yocto4
 static inline unit_t float2unit(const float x) throw() { return unit_t(floorf(x+0.5f)); }