#2337 Add an improved tick mark generator

This commit is contained in:
Jacob Støren 2018-02-09 11:01:00 +01:00
parent 911571e267
commit e76f8cfbcc
2 changed files with 200 additions and 0 deletions

View File

@ -5,6 +5,74 @@
#include <QDebug>
#include "RigFemPartResultsCollection.h"
#include "cafTickMarkGenerator.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(ScalarMapperTest, TickMarkGenerator)
{
EXPECT_EQ(10.0e6, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(5.5e6) );
EXPECT_EQ(5.0e6, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(2.1e6) );
EXPECT_EQ(2.0e6, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(1.1e6) );
EXPECT_EQ(1.0e6, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(1.0e6) );
EXPECT_EQ(1.0e6, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.9e6) );
EXPECT_EQ(50.0, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(50.0) );
EXPECT_EQ(50.0, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(21.00023) );
EXPECT_EQ(20.0, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(11.2324556) );
EXPECT_EQ(10.0, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(5.5) );
EXPECT_EQ(5.0, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(2.1) );
EXPECT_EQ(2.0, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(1.1) );
EXPECT_EQ(1.0, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(1.0) );
EXPECT_EQ(1.0, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.9) );
EXPECT_EQ(1.0, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.7) );
EXPECT_EQ(0.5, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.5) );
EXPECT_EQ(0.5, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.435) );
EXPECT_EQ(0.5, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.200001) );
EXPECT_EQ(0.2, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.20000) );
EXPECT_EQ(0.2, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.12) );
EXPECT_EQ(0.1, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.056) );
EXPECT_EQ(0.5e-6, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.200001e-6) );
EXPECT_EQ(0.2e-6, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.20000e-6) );
EXPECT_EQ(0.2e-6, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.12e-6) );
EXPECT_EQ(0.1e-6, caf::TickMarkGenerator::roundUpToLog_1_2_5_10(0.056e-6) );
{
caf::TickMarkGenerator tickGen(1234.34521, 2346.67293, 104.2768);
EXPECT_EQ(size_t(5), tickGen.tickMarkValues().size());
EXPECT_EQ(1400, tickGen.tickMarkValues()[0]);
EXPECT_EQ(1600, tickGen.tickMarkValues()[1]);
EXPECT_EQ(1800, tickGen.tickMarkValues()[2]);
EXPECT_EQ(2000, tickGen.tickMarkValues()[3]);
EXPECT_EQ(2200, tickGen.tickMarkValues()[4]);
}
{
caf::TickMarkGenerator tickGen(0.02134, 0.17829, 0.03267);
EXPECT_EQ(size_t(3), tickGen.tickMarkValues().size());
EXPECT_NEAR(0.05, tickGen.tickMarkValues()[0], 1e-15);
EXPECT_NEAR(0.1, tickGen.tickMarkValues()[1], 1e-15);
EXPECT_NEAR(0.15, tickGen.tickMarkValues()[2], 1e-15);
}
{
caf::TickMarkGenerator tickGen(0.02134, 0.0335, 0.001267);
EXPECT_EQ(size_t(6), tickGen.tickMarkValues().size());
EXPECT_EQ(0.022, tickGen.tickMarkValues()[0]);
EXPECT_EQ(0.024, tickGen.tickMarkValues()[1]);
EXPECT_NEAR(0.026, tickGen.tickMarkValues()[2], 1e-15);
EXPECT_EQ(0.028, tickGen.tickMarkValues()[3]);
EXPECT_EQ(0.03, tickGen.tickMarkValues()[4]);
EXPECT_EQ(0.032, tickGen.tickMarkValues()[5]);
}
}
//--------------------------------------------------------------------------------------------------
///

View File

@ -0,0 +1,132 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2018- Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma once
#include <vector>
#include <cmath>
#include "cafAssert.h"
namespace caf
{
class TickMarkGenerator
{
public:
TickMarkGenerator(double min, double max, double minAllowedStepSize)
{
double step = roundUpToLog_1_2_5_10(minAllowedStepSize);
double startStepCount = ceil(min / step);
if ( startStepCount*step < (min + 0.5*minAllowedStepSize) )
{
++startStepCount;
}
double tick = startStepCount*step;
double currentStepCount = startStepCount;
while ( tick < (max - 0.5*minAllowedStepSize) )
{
m_tickMarkValues.push_back(tick);
++currentStepCount;
tick = currentStepCount*step;
}
}
const std::vector<double>& tickMarkValues() { return m_tickMarkValues; }
static double roundUpToLog_1_2_5_10(double val)
{
CAF_ASSERT(val >= 0.0);
const static double logOf5 = log10(5.0);
const static double logOf2 = log10(2.0);
const static double logOf0_5 = log10(0.5);
const static double logOf0_2 = log10(0.2);
double logValue = log10(val);
double intPart = 0.0;
double fraction = modf(logValue, &intPart);
double factor = 1.0;
if (fraction == 0.0)
{
factor = 1.0;
}
else if (fraction > 0.0)
{
if ( fraction > logOf5 )
{
factor = 10.0;
}
else if ( fraction > logOf2 )
{
factor = 5.0;
}
else
{
factor = 2.0;
}
}
else
{
if (fraction > logOf0_5)
{
factor = 1;
}
else if ( fraction > logOf0_2 )
{
factor = 0.5;
}
else
{
factor = 0.2;
}
}
double roundedValue = pow(10.0, intPart) * factor;
return roundedValue;
}
private:
std::vector<double> m_tickMarkValues;
};
}