Base: Add fromPercent and toPercent helpers

This commit is contained in:
Kacper Donat
2025-02-20 00:02:06 +01:00
parent 7f984811e8
commit 2f35f1c561
12 changed files with 67 additions and 93 deletions

View File

@@ -31,19 +31,7 @@
#include "Application.h"
#include "Material.h"
// Helper functions to consistently convert between float and long
namespace
{
float fromPercent(long value)
{
return std::roundf(value) / 100.0F;
}
long toPercent(float value)
{
return std::lround(100.0 * value);
}
} // namespace
#include <Base/Tools.h>
using namespace App;
@@ -351,9 +339,9 @@ App::Material Material::getDefaultAppearance()
};
App::Material mat(App::Material::DEFAULT);
mat.transparency = fromPercent(hGrp->GetInt("DefaultShapeTransparency", 0));
long shininess = toPercent(mat.shininess);
mat.shininess = fromPercent(hGrp->GetInt("DefaultShapeShininess", shininess));
mat.transparency = Base::fromPercent(hGrp->GetInt("DefaultShapeTransparency", 0));
long shininess = Base::toPercent(mat.shininess);
mat.shininess = Base::fromPercent(hGrp->GetInt("DefaultShapeShininess", shininess));
// This is handled in the material code when using the object appearance
bool randomColor = hGrp->GetBool("RandomColor", false);

View File

@@ -143,6 +143,16 @@ inline T toDegrees(T r)
return static_cast<T>((r / M_PI) * 180.0);
}
inline float fromPercent(const long value)
{
return std::roundf(value) / 100.0F;
}
inline long toPercent(float value)
{
return std::lround(100.0 * value);
}
template<class T>
inline T fmod(T numerator, T denominator)
{

View File

@@ -28,6 +28,8 @@
#include "ui_DlgMaterialProperties.h"
#include "ViewProvider.h"
#include <Base/Tools.h>
using namespace Gui::Dialog;
@@ -129,7 +131,7 @@ void DlgMaterialPropertiesImp::onSpecularColorChanged()
*/
void DlgMaterialPropertiesImp::onShininessValueChanged(int sh)
{
customMaterial.shininess = (float)sh / 100.0F;
customMaterial.shininess = Base::fromPercent(sh);
}
/**
@@ -137,7 +139,7 @@ void DlgMaterialPropertiesImp::onShininessValueChanged(int sh)
*/
void DlgMaterialPropertiesImp::onTransparencyValueChanged(int sh)
{
customMaterial.transparency = (float)sh / 100.0F;
customMaterial.transparency = Base::fromPercent(sh);
}
/**

View File

@@ -95,7 +95,7 @@ DlgSettingsLightSources::DlgSettingsLightSources(QWidget* parent)
QCheckBox* enabledCheckbox,
std::function<void(bool)> setLightEnabled) {
light->color = Base::convertTo<SbColor>(colorButton->color());
light->intensity = intensitySpinBox->value() / 100.F;
light->intensity = Base::fromPercent(intensitySpinBox->value());
light->direction =
Base::convertTo<SbVec3f>(azimuthElevationToDirection(horizontalAngleSpinBox->rawValue(),
verticalAngleSpinBox->rawValue()));
@@ -161,7 +161,7 @@ DlgSettingsLightSources::DlgSettingsLightSources(QWidget* parent)
view->getEnvironment()->ambientColor =
Base::convertTo<SbColor>(ui->ambientLightColor->color());
view->getEnvironment()->ambientIntensity =
ui->ambientLightIntensitySpinBox->value() / 100.F;
Base::fromPercent(ui->ambientLightIntensitySpinBox->value());
};
connect(ui->ambientLightIntensitySpinBox,

View File

@@ -38,6 +38,8 @@
#include "View3DSettings.h"
#include "View3DInventorViewer.h"
#include <Base/Tools.h>
using namespace Gui;
View3DSettings::View3DSettings(ParameterGrp::handle hGrp,
@@ -150,9 +152,9 @@ void View3DSettings::OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::M
}
}
else if (strcmp(Reason,"HeadlightIntensity") == 0) {
long value = rGrp.GetInt("HeadlightIntensity", 100);
long value = rGrp.GetInt("HeadlightIntensity", 90);
for (auto _viewer : _viewers) {
_viewer->getHeadlight()->intensity.setValue((float)value/100.0f);
_viewer->getHeadlight()->intensity.setValue(Base::fromPercent(value));
}
}
else if (strcmp(Reason,"EnableBacklight") == 0) {
@@ -186,7 +188,7 @@ void View3DSettings::OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::M
else if (strcmp(Reason,"BacklightIntensity") == 0) {
long value = rGrp.GetInt("BacklightIntensity", 60);
for (auto _viewer : _viewers) {
_viewer->getBacklight()->intensity.setValue((float)value/100.0f);
_viewer->getBacklight()->intensity.setValue(Base::fromPercent(value));
}
}
else if (strcmp(Reason,"EnableFillLight") == 0) {
@@ -220,7 +222,7 @@ void View3DSettings::OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::M
else if (strcmp(Reason,"FillLightIntensity") == 0) {
long value = rGrp.GetInt("FillLightIntensity", 40);
for (auto _viewer : _viewers) {
_viewer->getFillLight()->intensity.setValue((float)value/100.0f);
_viewer->getFillLight()->intensity.setValue(Base::fromPercent(value));
}
}
else if (strcmp(Reason,"AmbientLightColor") == 0) {
@@ -235,7 +237,7 @@ void View3DSettings::OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::M
else if (strcmp(Reason,"AmbientLightIntensity") == 0) {
long value = rGrp.GetInt("AmbientLightIntensity", 20);
for (auto _viewer : _viewers) {
_viewer->getEnvironment()->ambientIntensity.setValue((float)value/100.0f);
_viewer->getEnvironment()->ambientIntensity.setValue(Base::fromPercent(value));
}
}
else if (strcmp(Reason,"EnablePreselection") == 0) {

View File

@@ -49,21 +49,10 @@
#include "ViewProviderGeometryObject.h"
#include "ViewProviderGeometryObjectPy.h"
#include <Base/Tools.h>
using namespace Gui;
// Helper functions to consistently convert between float and long
namespace {
float fromPercent(long value)
{
return std::roundf(value) / 100.0F;
}
long toPercent(float value)
{
return std::lround(100.0 * value);
}
}
PROPERTY_SOURCE(Gui::ViewProviderGeometryObject, Gui::ViewProviderDragger)
const App::PropertyIntegerConstraint::Constraints intPercent = {0, 100, 5};
@@ -71,7 +60,8 @@ const App::PropertyIntegerConstraint::Constraints intPercent = {0, 100, 5};
ViewProviderGeometryObject::ViewProviderGeometryObject()
{
App::Material mat = App::Material::getDefaultAppearance();
long initialTransparency = toPercent(mat.transparency);
long initialTransparency = Base::toPercent(mat.transparency);
static const char* dogroup = "Display Options";
static const char* sgroup = "Selection";
@@ -140,8 +130,8 @@ void ViewProviderGeometryObject::onChanged(const App::Property* prop)
setSelectable(Sel);
}
else if (prop == &Transparency) {
long value = toPercent(ShapeAppearance.getTransparency());
float trans = fromPercent(Transparency.getValue());
long value = Base::toPercent(ShapeAppearance.getTransparency());
float trans = Base::fromPercent(Transparency.getValue());
if (value != Transparency.getValue()) {
ShapeAppearance.setTransparency(trans);
}
@@ -152,7 +142,7 @@ void ViewProviderGeometryObject::onChanged(const App::Property* prop)
if (getObject() && getObject()->testStatus(App::ObjectStatus::TouchOnColorChange)) {
getObject()->touch(true);
}
long value = toPercent(ShapeAppearance.getTransparency());
long value = Base::toPercent(ShapeAppearance.getTransparency());
if (value != Transparency.getValue()) {
Transparency.setValue(value);
}

View File

@@ -71,17 +71,6 @@ namespace
{
constexpr const int lowPrec = 2;
constexpr const int highPrec = 16;
int toPercent(float value)
{
return static_cast<int>(100 * value); // NOLINT
}
float fromPercent(int value)
{
return static_cast<float>(value) / 100.0F; // NOLINT
}
} // namespace
PropertyItemFactory& PropertyItemFactory::instance()
@@ -3658,7 +3647,7 @@ int PropertyMaterialItem::getShininess() const
}
auto val = value.value<Material>();
return toPercent(val.shininess);
return Base::toPercent(val.shininess);
}
void PropertyMaterialItem::setShininess(int s)
@@ -3669,7 +3658,7 @@ void PropertyMaterialItem::setShininess(int s)
}
auto mat = value.value<Material>();
mat.shininess = fromPercent(s);
mat.shininess = Base::fromPercent(s);
setValue(QVariant::fromValue<Material>(mat));
}
@@ -3681,7 +3670,7 @@ int PropertyMaterialItem::getTransparency() const
}
auto val = value.value<Material>();
return toPercent(val.transparency);
return Base::toPercent(val.transparency);
}
void PropertyMaterialItem::setTransparency(int t)
@@ -3692,7 +3681,7 @@ void PropertyMaterialItem::setTransparency(int t)
}
auto mat = value.value<Material>();
mat.transparency = fromPercent(t);
mat.transparency = Base::fromPercent(t);
setValue(QVariant::fromValue<Material>(mat));
}
@@ -3747,8 +3736,8 @@ QVariant PropertyMaterialItem::toolTip(const App::Property* prop) const
.arg(ec.red())
.arg(ec.green())
.arg(ec.blue())
.arg(toPercent(value.shininess))
.arg(toPercent(value.transparency));
.arg(Base::toPercent(value.shininess))
.arg(Base::toPercent(value.transparency));
return {data};
}
@@ -4085,7 +4074,7 @@ int PropertyMaterialListItem::getShininess() const
}
auto mat = list[0].value<Material>();
return toPercent(mat.shininess);
return Base::toPercent(mat.shininess);
}
void PropertyMaterialListItem::setShininess(int s)
@@ -4105,7 +4094,7 @@ void PropertyMaterialListItem::setShininess(int s)
}
auto mat = list[0].value<Material>();
mat.shininess = fromPercent(s);
mat.shininess = Base::fromPercent(s);
list[0] = QVariant::fromValue<Material>(mat);
setValue(list);
}
@@ -4127,7 +4116,7 @@ int PropertyMaterialListItem::getTransparency() const
}
auto mat = list[0].value<Material>();
return toPercent(mat.transparency);
return Base::toPercent(mat.transparency);
}
void PropertyMaterialListItem::setTransparency(int t)
@@ -4147,7 +4136,7 @@ void PropertyMaterialListItem::setTransparency(int t)
}
auto mat = list[0].value<Material>();
mat.transparency = fromPercent(t);
mat.transparency = Base::fromPercent(t);
list[0] = QVariant::fromValue<Material>(mat);
setValue(list);
}
@@ -4235,8 +4224,8 @@ QVariant PropertyMaterialListItem::toolTip(const App::Property* prop) const
.arg(ec.red())
.arg(ec.green())
.arg(ec.blue())
.arg(toPercent(value.shininess))
.arg(toPercent(value.transparency));
.arg(Base::toPercent(value.shininess))
.arg(Base::toPercent(value.transparency));
return {data};
}

View File

@@ -69,6 +69,8 @@
#include "ViewProviderAnalysis.h"
#include "ViewProviderFemPostObject.h"
#include <Base/Tools.h>
using namespace FemGui;
namespace sp = std::placeholders;
@@ -659,7 +661,7 @@ void ViewProviderFemPostObject::WriteColorData(bool ResetColorBarRange)
if (Field.getEnumVector().empty() || Field.getValue() == 0) {
m_material->diffuseColor.setValue(SbColor(0.8, 0.8, 0.8));
float trans = float(Transparency.getValue()) / 100.0;
float trans = Base::fromPercent(Transparency.getValue());
m_material->transparency.setValue(trans);
m_materialBinding->value = SoMaterialBinding::OVERALL;
m_materialBinding->touch();
@@ -696,7 +698,7 @@ void ViewProviderFemPostObject::WriteColorData(bool ResetColorBarRange)
SbColor* diffcol = m_material->diffuseColor.startEditing();
SbColor* edgeDiffcol = m_matPlainEdges->diffuseColor.startEditing();
float overallTransp = Transparency.getValue() / 100.0f;
float overallTransp = Base::fromPercent(Transparency.getValue());
m_material->transparency.setNum(numPts);
m_matPlainEdges->transparency.setNum(numPts);
float* transp = m_material->transparency.startEditing();
@@ -737,7 +739,7 @@ void ViewProviderFemPostObject::WriteColorData(bool ResetColorBarRange)
void ViewProviderFemPostObject::WriteTransparency()
{
float trans = static_cast<float>(Transparency.getValue()) / 100.0;
float trans = Base::fromPercent(Transparency.getValue());
float* value = m_material->transparency.startEditing();
float* edgeValue = m_matPlainEdges->transparency.startEditing();
// m_material and m_matPlainEdges field containers have same size

View File

@@ -38,6 +38,8 @@
#include "ModelManager.h"
#include "ModelUuids.h"
#include <Base/Tools.h>
using namespace Materials;
@@ -174,8 +176,8 @@ std::shared_ptr<App::Material> MaterialManager::defaultAppearance()
long initialTransparency = hGrp->GetInt("DefaultShapeTransparency", 0);
long initialShininess = hGrp->GetInt("DefaultShapeShininess", 90);
mat.shininess = ((float)initialShininess / 100.0F);
mat.transparency = ((float)initialTransparency / 100.0F);
mat.shininess = Base::fromPercent(initialShininess);
mat.transparency = Base::fromPercent(initialTransparency);
return std::make_shared<App::Material>(mat);
}

View File

@@ -329,7 +329,7 @@ void ViewProviderMesh::onChanged(const App::Property* prop)
pcMatBinding->value = SoMaterialBinding::OVERALL;
}
if (prop == &LineTransparency) {
float trans = LineTransparency.getValue() / 100.0F;
float trans = Base::fromPercent(LineTransparency.getValue());
pLineColor->transparency = trans;
}
else if (prop == &LineWidth) {
@@ -590,7 +590,7 @@ void ViewProviderMesh::tryColorPerVertexOrFace(bool on)
pcMatBinding->value = SoMaterialBinding::OVERALL;
const App::Color& c = ShapeAppearance.getDiffuseColor();
pcShapeMaterial->diffuseColor.setValue(c.r, c.g, c.b);
pcShapeMaterial->transparency.setValue(Transparency.getValue() / 100.0F);
pcShapeMaterial->transparency.setValue(Base::fromPercent(Transparency.getValue()));
}
}

View File

@@ -33,6 +33,8 @@
#include "ViewProvider.h"
#include <Base/Tools.h>
using namespace PartGui;
@@ -88,7 +90,7 @@ void ViewProviderPart::applyTransparency(float transparency, std::vector<App::Co
for (auto& j : colors) {
// transparency hasn't been set for this face
if (j.a == 0.0) {
j.setTransparency(transparency/100.0F); // transparency comes in percent
j.setTransparency(Base::fromPercent(transparency)); // transparency comes in percent
}
}
}
@@ -101,7 +103,7 @@ void ViewProviderPart::applyTransparency(float transparency, std::vector<App::Ma
for (auto& j : colors) {
// transparency hasn't been set for this face
if (j.transparency == 0.0) {
j.transparency = transparency / 100.0F; // transparency comes in percent
j.transparency = Base::fromPercent(transparency); // transparency comes in percent
}
}
}

View File

@@ -97,19 +97,6 @@ FC_LOG_LEVEL_INIT("Part", true, true)
using namespace PartGui;
// Helper functions to consistently convert between float and long
namespace {
float fromPercent(long value)
{
return std::roundf(value) / 100.0F;
}
long toPercent(float value)
{
return std::lround(100.0 * value);
}
}
PROPERTY_SOURCE(PartGui::ViewProviderPartExt, Gui::ViewProviderGeometryObject)
@@ -364,9 +351,9 @@ void ViewProviderPartExt::onChanged(const App::Property* prop)
}
else if (prop == &Transparency) {
const App::Material& Mat = ShapeAppearance[0];
long value = toPercent(Mat.transparency);
long value = Base::toPercent(Mat.transparency);
if (value != Transparency.getValue()) {
float trans = fromPercent(Transparency.getValue());
float trans = Base::fromPercent(Transparency.getValue());
ShapeAppearance.setTransparency(trans);
}
}
@@ -676,7 +663,7 @@ std::map<std::string,App::Color> ViewProviderPartExt::getElementColors(const cha
if(!element || !element[0]) {
auto color = ShapeAppearance.getDiffuseColor();
color.setTransparency(Transparency.getValue()/100.0F);
color.setTransparency(Base::fromPercent(Transparency.getValue()));
ret["Face"] = color;
ret["Edge"] = LineColor.getValue();
ret["Vertex"] = PointColor.getValue();
@@ -687,7 +674,7 @@ std::map<std::string,App::Color> ViewProviderPartExt::getElementColors(const cha
auto size = ShapeAppearance.getSize();
if(element[4]=='*') {
auto color = ShapeAppearance.getDiffuseColor();
color.setTransparency(Transparency.getValue()/100.0F);
color.setTransparency(Base::fromPercent(Transparency.getValue()));
bool singleColor = true;
for(int i=0;i<size;++i) {
if (ShapeAppearance.getDiffuseColor(i) != color) {
@@ -699,7 +686,7 @@ std::map<std::string,App::Color> ViewProviderPartExt::getElementColors(const cha
}
if(size && singleColor) {
color = ShapeAppearance.getDiffuseColor(0);
color.setTransparency(Transparency.getValue()/100.0F);
color.setTransparency(Base::fromPercent(100.0F));
ret.clear();
}
ret["Face"] = color;
@@ -710,7 +697,7 @@ std::map<std::string,App::Color> ViewProviderPartExt::getElementColors(const cha
else
ret[element] = ShapeAppearance.getDiffuseColor();
if(size==1)
ret[element].setTransparency(Transparency.getValue()/100.0F);
ret[element].setTransparency(Base::fromPercent(Transparency.getValue()));
}
} else if (boost::starts_with(element,"Edge")) {
auto size = LineColorArray.getSize();