/*! * * Copyright (c) 2019-2024 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file types.cpp * \author (last) Behrouz NematiPour * \date (last) 28-Mar-2023 * \author (original) Behrouz NematiPour * \date (original) 26-Dec-2019 * */ #include "types.h" // Qt // Project /*! * \brief Types::floatCompare * \details compares two floats with a tolerance. * \param f1 - float value 1 * \param f2 - float value 2 * \return true if (almost) equal */ bool Types::floatCompare(float f1, float f2) { static constexpr auto epsilon = 1.0e-05f; if (qAbs(f1 - f2) <= epsilon) return true; return qAbs(f1 - f2) <= epsilon * qMax(qAbs(f1), qAbs(f2)); } /*! * \brief Types::getBits * \details converts the array of byte to array of bits * \param vData - the array of bytes * \param vStartIndex - start index of the array to be used for conversion * \param vFlags - the array of bits * \param vLen - the amounts of bits to be converted * \return false if there is not enough data available from start index vStartIndex to the length of vLen. */ bool Types::getBits(const QByteArray &vData, int &vStartIndex, QBitArray &vFlags, int vLen) { vFlags.clear(); QByteArray data = vData.mid(vStartIndex, vLen); quint8 bytes = vLen / 8; vStartIndex = vLen % 8 ? bytes + 1 : bytes ; //CEILING DIVISION TO ROUND UP TO LATEST BIT if ( data.length() * 8 < vLen ) return false; vFlags = QBitArray::fromBits(data, vLen); return true; }