/*
 * Copyright (c) 2018, Linz Center of Mechatronics GmbH (LCM), web: www.lcm.at
 * All rights reserved.
 */
/*
 * This file is licensed according to the BSD 3-clause license as follows:
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the "Linz Center of Mechatronics GmbH" and "LCM" nor
 *       the names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL "Linz Center of Mechatronics GmbH" BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * This file is part of X2C. web: x2c.lcm.at
 * $LastChangedRevision: 4463 $
 * $LastChangedDate:: 2026-08-17 13:59:56 +0200#$
 */
/* USERCODE-BEGIN:Description */
/* Description: */
/* USERCODE-END:Description */

package at.lcm.x2c.library.filter;

import at.lcm.x2c.core.structure.*;
import at.lcm.bu21.general.dtypes.*;

/* USERCODE-BEGIN:Imports */
/* USERCODE-END:Imports */

@SuppressWarnings("unused")
public class ConvFnc_AdaptiveNotch_Float32 extends JavaConversionFunction {
    private static final long serialVersionUID = 1L;

    @Override
    public void convertMaskToImplementation() throws Exception {
        MaskDouble QMaskVal = (MaskDouble)this.getMaskParameter("Q").getMaskDataType();
        TNumeric QMaskData = (TNumeric)QMaskVal.getData();
        double mp_Q = QMaskData.getReal(0, 0);
        MaskDouble n_threshMaskVal = (MaskDouble)this.getMaskParameter("n_thresh").getMaskDataType();
        TNumeric n_threshMaskData = (TNumeric)n_threshMaskVal.getData();
        double mp_n_thresh = n_threshMaskData.getReal(0, 0);
        MaskDouble n_maxMaskVal = (MaskDouble)this.getMaskParameter("n_max").getMaskDataType();
        TNumeric n_maxMaskData = (TNumeric)n_maxMaskVal.getData();
        double mp_n_max = n_maxMaskData.getReal(0, 0);
        MaskDouble pMaskVal = (MaskDouble)this.getMaskParameter("p").getMaskDataType();
        TNumeric pMaskData = (TNumeric)pMaskVal.getData();
        double mp_p = pMaskData.getReal(0, 0);
        MaskDouble ts_factMaskVal = (MaskDouble)this.getMaskParameter("ts_fact").getMaskDataType();
        TNumeric ts_factMaskData = (TNumeric)ts_factMaskVal.getData();
        double mp_ts_fact = ts_factMaskData.getReal(0, 0);
        MaskComboBox methodMaskVal = (MaskComboBox)this.getMaskParameter("method").getMaskDataType();
        TString methodMaskData = (TString)methodMaskVal.getData();
        String mp_method = methodMaskData.getString();

        TNumeric a_factCtrVal = this.getImplementationParameterData("a_fact");
        double cp_a_fact = Double.NaN;
        TNumeric b_factCtrVal = this.getImplementationParameterData("b_fact");
        double cp_b_fact = Double.NaN;
        TNumeric c_factCtrVal = this.getImplementationParameterData("c_fact");
        double cp_c_fact = Double.NaN;
        TNumeric n_threshCtrVal = this.getImplementationParameterData("n_thresh");
        double cp_n_thresh = Double.NaN;

        /* Check if sample time factor is an integer value */
        if (Math.rint(mp_ts_fact) != mp_ts_fact) {
            throw new Exception("Sample time factor must be a whole number");
        }
        /* Check if sample time factor is within the valid range (upper limit defined by unsigned 16-bit integer range) */
        if ((mp_ts_fact < 1.0) || (mp_ts_fact > 65535.0)) {
            throw new Exception("Sample time factor must be at least 1 and at most 65535");
        }
        /* Block sample time */
        double bp_ts = mp_ts_fact * this.getDedicatedBlock().getModel().getSampleTime();
        /* Check if Block sample time has a valid value */
        if (bp_ts <= 0.0) {
            throw new Exception("Block sample time must be greater than zero");
        }

        /* ATTENTION: */
        /* The M2I conversion function may either use the mp_* or *MaskVal value to get the Mask Parameter value */
        /* For new or updated implementations, the mp_* value shall be used where possible */
        /* Currently, the mp_* value cannot be used for getting non-scalar Mask Parameter data but will return only the first element for these */
        /* The Implementation Parameter assignment variables cp_* can and shall be used for all Implementation Parameter data types */

        /* INFO: Implementation data assignment for array types when using the cp_* variables */
        /* If the cp_* parameter is a FLASH ARRAY, the cp_* variable is assigned an array type but without an actual array */
        /* The M2I conversion function has to assign an array with the correct size and fill it with content */
        /* If the cp_* parameter is a RAM ARRAY, the cp_* variable is assigned an array with the correct size */
        /* The first element is assigned an invalid value while the other elements are (auto)initialized with zero */
        /* The M2I conversion function has to fill the array with content */

        /* Mask parameter values */
        /* mp_Q OR QMaskVal (Number) */
        /* mp_n_thresh OR n_threshMaskVal (Number) */
        /* mp_n_max OR n_maxMaskVal (Number) */
        /* mp_p OR pMaskVal (Number) */
        /* mp_ts_fact OR ts_factMaskVal (Number) */
        /* mp_method OR methodMaskVal (String) */

        /* USERCODE-BEGIN:ConvMaskToImplementation */
        /* get mask parameter values */
        double n_thresh = Double.valueOf(n_threshMaskVal.getValue()); /* speed threshold */
        double p = Double.valueOf(pMaskVal.getValue()); /* number of pole pairs */
        double Q = Double.valueOf(QMaskVal.getValue()); /* Q-factor */
        // double n_max = Double.valueOf(n_maxMaskVal.getValue()); /* speed scaling */

        double ts_fact = Double.valueOf(ts_factMaskVal.getValue()).intValue();

        /* validation */
        if (ts_fact < 1) {
            throw new Exception("Sample time factor must not be smaller than one!");
        }
        if (p < 1) {
            throw new Exception("Number of pole pairs must not be smaller than one!");
        }
        if (n_thresh <= 0) {
            throw new Exception("Speed threshold must be greater than zero!");
        }
        if (Q < 0.5) {
            throw new Exception(
                    "Q-factor must not be smaller than 0.5!"); /* to prevent complex multiplication factors */
        }

        /* calculate multiplication factors for filter coefficients */
        double Ts = ts_fact * this.getDedicatedBlock().getModel().getSampleTime();
        double a_fact = ((((-2 * Math.PI) / 60) * p) / 2 / Q) * Ts;
        double b_fact = ((((-2 * Math.PI) / 60) * p) / 2 / Q) * Ts * Math.sqrt((4 * Q * Q) - 1);
        double c_fact = ((2 * Math.PI) / 60) * p * Ts;

        /* set implementation parameter values */
        n_threshCtrVal.setReal(0, 0, n_thresh);
        a_factCtrVal.setReal(0, 0, a_fact);
        b_factCtrVal.setReal(0, 0, b_fact);
        c_factCtrVal.setReal(0, 0, c_fact);

        /* USERCODE-END:ConvMaskToImplementation */

        /* Implementation Parameter values */
        /* cp_a_fact OR a_factCtrVal (Value) */
        /* cp_b_fact OR b_factCtrVal (Value) */
        /* cp_c_fact OR c_factCtrVal (Value) */
        /* cp_n_thresh OR n_threshCtrVal (Value) */

        if (Double.isNaN(cp_a_fact)) {
            this.setImplementationParameterData("a_fact", a_factCtrVal);
        } else {
            this.setImplementationParameterData("a_fact", new TNumeric(cp_a_fact));
        }
        if (Double.isNaN(cp_b_fact)) {
            this.setImplementationParameterData("b_fact", b_factCtrVal);
        } else {
            this.setImplementationParameterData("b_fact", new TNumeric(cp_b_fact));
        }
        if (Double.isNaN(cp_c_fact)) {
            this.setImplementationParameterData("c_fact", c_factCtrVal);
        } else {
            this.setImplementationParameterData("c_fact", new TNumeric(cp_c_fact));
        }
        if (Double.isNaN(cp_n_thresh)) {
            this.setImplementationParameterData("n_thresh", n_threshCtrVal);
        } else {
            this.setImplementationParameterData("n_thresh", new TNumeric(cp_n_thresh));
        }
    }

    @Override
    public void convertImplementationToMask() throws Exception {
        MaskDouble QMaskVal = (MaskDouble)this.getMaskParameter("Q").getMaskDataType();
        TNumeric QMaskData = (TNumeric)QMaskVal.getData();
        double mp_Q = Double.NaN;
        MaskDouble n_threshMaskVal = (MaskDouble)this.getMaskParameter("n_thresh").getMaskDataType();
        TNumeric n_threshMaskData = (TNumeric)n_threshMaskVal.getData();
        double mp_n_thresh = Double.NaN;
        MaskDouble n_maxMaskVal = (MaskDouble)this.getMaskParameter("n_max").getMaskDataType();
        TNumeric n_maxMaskData = (TNumeric)n_maxMaskVal.getData();
        double mp_n_max = Double.NaN;
        MaskDouble pMaskVal = (MaskDouble)this.getMaskParameter("p").getMaskDataType();
        TNumeric pMaskData = (TNumeric)pMaskVal.getData();
        double mp_p = Double.NaN;
        MaskDouble ts_factMaskVal = (MaskDouble)this.getMaskParameter("ts_fact").getMaskDataType();
        TNumeric ts_factMaskData = (TNumeric)ts_factMaskVal.getData();
        double mp_ts_fact = ts_factMaskData.getReal(0, 0);
        MaskComboBox methodMaskVal = (MaskComboBox)this.getMaskParameter("method").getMaskDataType();
        TString methodMaskData = (TString)methodMaskVal.getData();
        String mp_method = null;

        TNumeric a_factCtrVal = this.getImplementationParameterData("a_fact");
        double cp_a_fact = a_factCtrVal.getReal(0, 0);
        TNumeric b_factCtrVal = this.getImplementationParameterData("b_fact");
        double cp_b_fact = b_factCtrVal.getReal(0, 0);
        TNumeric c_factCtrVal = this.getImplementationParameterData("c_fact");
        double cp_c_fact = c_factCtrVal.getReal(0, 0);
        TNumeric n_threshCtrVal = this.getImplementationParameterData("n_thresh");
        double cp_n_thresh = n_threshCtrVal.getReal(0, 0);

        /* Check if sample time factor is an integer value */
        if (Math.rint(mp_ts_fact) != mp_ts_fact) {
            throw new Exception("Sample time factor must be a whole number");
        }
        /* Check if sample time factor is within the valid range (upper limit defined by unsigned 16-bit integer range) */
        if ((mp_ts_fact < 1.0) || (mp_ts_fact > 65535.0)) {
            throw new Exception("Sample time factor must be at least 1 and at most 65535");
        }
        /* Block sample time */
        double bp_ts = mp_ts_fact * this.getDedicatedBlock().getModel().getSampleTime();
        /* Check if Block sample time has a valid value */
        if (bp_ts <= 0.0) {
            throw new Exception("Block sample time must be greater than zero");
        }

        /* ATTENTION: */
        /* The I2M conversion function may either use the cp_* or *CtrlVal value to get the Implementation Parameter value */
        /* For new or updated implementations, the cp_* value shall be used for all Implementation Parameter data types */
        /* The Mask Parameter assignment variables cp_* shall be used where possible */
        /* Currently, the mp_* variable assignment value cannot be used for setting non-scalar Mask Parameter data */

        /* Implementation Parameter values */
        /* cp_a_fact OR a_factCtrVal (Value) */
        /* cp_b_fact OR b_factCtrVal (Value) */
        /* cp_c_fact OR c_factCtrVal (Value) */
        /* cp_n_thresh OR n_threshCtrVal (Value) */

        /* USERCODE-BEGIN:ConvImplementationToMask */
        if (true) {
            throw new Exception("Implementation-to-Mask conversion not implemented for Block 'AdaptiveNotch', Implementation 'Float32' in Library 'Filter'");
        }
        /* USERCODE-END:ConvImplementationToMask */

        /* Mask parameter values */
        /* mp_Q OR QMaskVal (Number) */
        /* mp_n_thresh OR n_threshMaskVal (Number) */
        /* mp_n_max OR n_maxMaskVal (Number) */
        /* mp_p OR pMaskVal (Number) */
        /* mp_ts_fact OR ts_factMaskVal (Number) */
        /* mp_method OR methodMaskVal (String) */

        if (Double.isNaN(mp_Q)) {
            this.getMaskParameter("Q").setValue(QMaskData.toFullString());
        } else {
            this.getMaskParameter("Q").setValue(new TNumeric(mp_Q).toFullString());
        }
        if (Double.isNaN(mp_n_thresh)) {
            this.getMaskParameter("n_thresh").setValue(n_threshMaskData.toFullString());
        } else {
            this.getMaskParameter("n_thresh").setValue(new TNumeric(mp_n_thresh).toFullString());
        }
        if (Double.isNaN(mp_n_max)) {
            this.getMaskParameter("n_max").setValue(n_maxMaskData.toFullString());
        } else {
            this.getMaskParameter("n_max").setValue(new TNumeric(mp_n_max).toFullString());
        }
        if (Double.isNaN(mp_p)) {
            this.getMaskParameter("p").setValue(pMaskData.toFullString());
        } else {
            this.getMaskParameter("p").setValue(new TNumeric(mp_p).toFullString());
        }
        if (mp_method == null) {
            this.getMaskParameter("method").setValue(methodMaskData.getString());
        } else {
            this.getMaskParameter("method").setValue(mp_method);
        }
    }
}
