<commit_before>// =================================================================== //
// Copyright (C) 2018-2019 <NAME>                                  //
//                                                                     //
// This Source Code Form is subject to the terms of the Mozilla Public //
// License, v. 2.0. If a copy of the MPL was not distributed with this //
// file, You can obtain one at http://mozilla.org/MPL/2.0/.            //
// =================================================================== //

#include "TransmittanceModelDockWidget.h"

#include <osg/Timer>

#include <libbsdf/Brdf/HalfDifferenceCoordinatesBrdf.h>
#include <libbsdf/Brdf/SpecularCoordinatesBrdf.h>
#include <libbsdf/Brdf/SphericalCoordinatesBrdf.h>

#include <libbsdf/ReflectanceModel/GGX.h>
#include <libbsdf/ReflectanceModel/GgxAnisotropic.h>
#include <libbsdf/ReflectanceModel/Lambertian.h>
#include <libbsdf/ReflectanceModel/MultipleScatteringSmith.h>
#include <libbsdf/ReflectanceModel/ReflectanceModelUtility.h>

TransmittanceModelDockWidget::TransmittanceModelDockWidget(QWidget* parent)
                                                           : AnalyticBsdfDockWidget(parent)
{
    initializeReflectanceModels();
    updateParameterWidget(0);

    int lambertIndex = ui_->reflectanceModelComboBox->findText("Lambertian");
    ui_->reflectanceModelComboBox->setCurrentIndex(lambertIndex);
    ui_->reflectanceModelComboBox->activated(lambertIndex);

    // Select a specular coordinate system.
    ui_->coordSysComboBox->setCurrentIndex(1);
    ui_->coordSysComboBox->activated(1);

    connect(ui_->generateBrdfPushButton, SIGNAL(clicked()), this, SLOT(generateBrdf()));
}

void TransmittanceModelDockWidget::generateBrdf()
{
    lbTrace << "[TransmittanceModelDockWidget::generateBrdf]";

    osg::Timer_t startTick = osg::Timer::instance()->tick();

    QString name = ui_->reflectanceModelComboBox->currentText();
    lb::ReflectanceModel* model = reflectanceModels_[name.toLocal8Bit().data()];

    std::shared_ptr<lb::Brdf> brdf = initializeBrdf(model->isIsotropic());

    bool iorUsed = (dynamic_cast<lb::Ggx*>(model) ||
                    dynamic_cast<lb::GgxAnisotropic*>(model) ||
                    dynamic_cast<lb::MultipleScatteringSmith*>(model));

    lb::SpecularCoordinatesBrdf* specBrdf = dynamic_cast<lb::SpecularCoordinatesBrdf*>(brdf.get());

    // Offset specular directions for refraction.
    if (iorUsed && specBrdf) {
        bool found = false;

        const std::string iorParamName("Refractive index");

        using std::asin;
        using std::max;
        using std::min;
        using std::sin;

        lb::ReflectanceModel::Parameters& params = model->getParameters();
        for (auto it = params.begin(); it != params.end(); ++it) {
            if (it->getName() == iorParamName) {
                // If the refractive index used for offset is less than 1.0, lb::SpecularCoordinatesBrdf is inefficient.
                float ior = max(*it->getFloat(), 1.0f);

                if (ior == 1.0f) break;

                for (int i = 0; i < specBrdf->getNumInTheta(); ++i) {
                    float inTheta = specBrdf->getInTheta(i);
                    float sinT = min(sin(inTheta) / ior, 1.0f);
                    float refractedTheta = asin(sinT);
                    specBrdf->setSpecularOffset(i, refractedTheta - inTheta);
                }

                found = true;
                break;
            }
        }

        if (!found) {
            lbError << "[TransmittanceModelDockWidget::generateBrdf] \"" << iorParamName << "\" is not found.";
        }
    }

    lb::reflectance_model_utility::setupTabularBrdf(*model, brdf.get(), lb::BTDF_DATA);

    osg::Timer_t endTick = osg::Timer::instance()->tick();
    double delta = osg::Timer::instance()->delta_s(startTick, endTick);
    lbInfo << "[TransmittanceModelDockWidget::generateBrdf] " << delta << "(s)";

    emit generated(brdf, lb::BTDF_DATA);
    emit generated();
}

void TransmittanceModelDockWidget::initializeReflectanceModels()<commit_msg>Refactoring<commit_after>// =================================================================== //
// Copyright (C) 2018-2019 <NAME>                                  //
//                                                                     //
// This Source Code Form is subject to the terms of the Mozilla Public //
// License, v. 2.0. If a copy of the MPL was not distributed with this //
// file, You can obtain one at http://mozilla.org/MPL/2.0/.            //
// =================================================================== //

#include "TransmittanceModelDockWidget.h"

#include <osg/Timer>

#include <libbsdf/Brdf/HalfDifferenceCoordinatesBrdf.h>
#include <libbsdf/Brdf/SpecularCoordinatesBrdf.h>
#include <libbsdf/Brdf/SphericalCoordinatesBrdf.h>

#include <libbsdf/ReflectanceModel/GGX.h>
#include <libbsdf/ReflectanceModel/GgxAnisotropic.h>
#include <libbsdf/ReflectanceModel/Lambertian.h>
#include <libbsdf/ReflectanceModel/MultipleScatteringSmith.h>
#include <libbsdf/ReflectanceModel/ReflectanceModelUtility.h>

TransmittanceModelDockWidget::TransmittanceModelDockWidget(QWidget* parent)
                                                           : AnalyticBsdfDockWidget(parent)
{
    initializeReflectanceModels();
    updateParameterWidget(0);

    int lambertIndex = ui_->reflectanceModelComboBox->findText("Lambertian");
    ui_->reflectanceModelComboBox->setCurrentIndex(lambertIndex);
    ui_->reflectanceModelComboBox->activated(lambertIndex);

    // Select a specular coordinate system.
    ui_->coordSysComboBox->setCurrentIndex(1);
    ui_->coordSysComboBox->activated(1);

    connect(ui_->generateBrdfPushButton, SIGNAL(clicked()), this, SLOT(generateBrdf()));
}

void TransmittanceModelDockWidget::generateBrdf()
{
    lbTrace << "[TransmittanceModelDockWidget::generateBrdf]";

    osg::Timer_t startTick = osg::Timer::instance()->tick();

    QString name = ui_->reflectanceModelComboBox->currentText();
    lb::ReflectanceModel* model = reflectanceModels_[name.toLocal8Bit().data()];

    std::shared_ptr<lb::Brdf> brdf = initializeBrdf(model->isIsotropic());

    bool iorUsed = (dynamic_cast<lb::Ggx*>(model) ||
                    dynamic_cast<lb::GgxAnisotropic*>(model) ||
                    dynamic_cast<lb::MultipleScatteringSmith*>(model));

    lb::SpecularCoordinatesBrdf* specBrdf = dynamic_cast<lb::SpecularCoordinatesBrdf*>(brdf.get());

    // Offset specular directions for refraction.
    if (iorUsed && specBrdf) {
        bool found = false;

        const std::string iorParamName("Refractive index");

        lb::ReflectanceModel::Parameters& params = model->getParameters();
        for (auto it = params.begin(); it != params.end(); ++it) {
            if (it->getName() == iorParamName) {
                // If the refractive index used for offset is less than 1.0, lb::SpecularCoordinatesBrdf is inefficient.
                float ior = std::max(*it->getFloat(), 1.0f);
                if (ior == 1.0f) break;

                specBrdf->setupSpecularOffsets(ior);

                found = true;
                break;
            }
        }

        if (!found) {
            lbError << "[TransmittanceModelDockWidget::generateBrdf] \"" << iorParamName << "\" is not found.";
        }
    }

    lb::reflectance_model_utility::setupTabularBrdf(*model, brdf.get(), lb::BTDF_DATA);

    osg::Timer_t endTick = osg::Timer::instance()->tick();
    double delta = osg::Timer::instance()->delta_s(startTick, endTick);
    lbInfo << "[TransmittanceModelDockWidget::generateBrdf] " << delta << "(s)";

    emit generated(brdf, lb::BTDF_DATA);
    emit generated();
}

void TransmittanceModelDockWidget::initializeReflectanceModels()
{'fraction_non_alphanumeric': 0.1623117518342129, 'fraction_numerical': 0.006049684644098339, 'mean_word_length': 3.0279937791601865, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6702656', 'n_tokens_mistral': 2351, 'n_tokens_neox': 2211, 'n_words': 401}