// Straight buried type rotor
//console.log("------ start script ------");
// Copyright 2018-2024, Vladimir Kuptsov
include('..\\sDraw.js');

function Rotor()
{
	// Rotor inner radius
	this.Rrinner = motor.rotorInnerRadius;
	// Rotor outer radius
	this.Rrouter = motor.rotorOuterRadius;
	// Number of pole pairs
	this.Npp = motor.rotorNumberPolePairs;
	//Air gap length
	var lag = motor.airGap;
	
	// Default rotor dimensions:
	// Magnet depth:
	this.Dpm = 0.2*(this.Rrouter - this.Rrinner);
	// Magnet inset depth:
	this.Dis = 1.5*this.Dpm;
	if (this.Npp > 2) {
		// Magnet width:
		this.Wpm = 0.6*2*(this.Rrouter - 1.5*this.Dpm)*Math.tan(Math.PI/this.Npp/2);
	}else {
		// Magnet width:
		var gamma = Math.PI/4;
		this.Wpm = 0.3*2*(this.Rrouter - 1.5*this.Dpm)*Math.tan(gamma);
	}
	
	this.Wb = 0;
	
	this.wpm_real = null; //реальная ширина основания магнита
		
};

Rotor.prototype.resetGeometry = function (){
	if(this.t<10){
		this.t = this.t + 1;
		return this.calcGeometry(); //???	
	}
	return;
}

// generate rotor geometry 
// call from с++ code
Rotor.prototype.getGeometry = function (){
	this.wpm_real = this.Wpm;
	this.wb_real = this.Wb;
	this.dpm_real = this.Dpm;
	this.dis_real = this.Dis;
	this.k_1 = false;
	this.t = 0;
	return this.calcGeometry();
}

// generate rotor geometry 
// call from с++ code
Rotor.prototype.calcGeometry = function (){
	// Machine type: InnerRotor | OuterRotor
	var machineType = motor.machineType;
	// Rotor inner radius
	this.Rrinner = motor.rotorInnerRadius;
	// Rotor outer radius
	this.Rrouter = motor.rotorOuterRadius; 
	// Number of pole pairs
	this.Npp = motor.rotorNumberPolePairs;       // исправить число полюсов на число пар полюсов в GUI????
	
	// draw rotor geometry
	this.geomr = new Array;
	
	this.top_iron = false;//необходимость добавления верхнего металла
	
	var point_0 = new QPointF(0, 0);
	
	this.alfa = 360/this.Npp/4; //угол 1-0-10
	//this.alfa_pm = this.alfa*(this.apm_real/180);  //угол 1_0_5
	var s_line_0_10 = sDraw.createLineByPointAngle(0, 0, 90-this.alfa); //правая граница сектора
	
	var circle_Rrouter = sDraw.createCircle(0, 0, this.Rrouter); //внешний круг
	var circle_Rrinner = sDraw.createCircle(0, 0, this.Rrinner); //внутренний круг
		
	// only inner rotor
	
	if(this.Rrouter - this.dis_real < this.Rrinner){
		this.dis_real = this.Rrouter - this.Rrinner;
	}
	if(this.dis_real < this.dpm_real){
		this.dpm_real = this.dis_real;
	}
		
	this.point_4 = new QPointF(0, (this.Rrouter - this.dis_real));
	var s_line_4_5 = sDraw.createLineByPointAngle(this.point_4.x(), this.point_4.y(), 0);
	this.point_5 = new QPointF(this.wpm_real / 2, this.point_4.y());
	var s_line_0_5 = sDraw.createLineBy2Points(0, 0, this.point_5.x(), this.point_5.y());
	//this.point_5 = sDraw.intersection(s_line_0_5, s_line_4_5, 'higher');
	if(Math.abs(90-this.alfa) >= 0.001 ){
		this.point_6x = sDraw.intersection(s_line_0_10, s_line_4_5, 'higher');//точка пересечения линий 4_5 и 0_10
	}else{
		this.point_6x = sDraw.intersection(circle_Rrouter, s_line_4_5, 'right');//точка пересечения внешнего круга и линии 4_5
	}
	if(this.point_6x.x() < this.point_5.x()){
		//т5 = т6 = т11
		this.point_5 = new QPointF(this.point_6x.x(), this.point_6x.y());
		this.point_6 = new QPointF(this.point_6x.x(), this.point_6x.y());
		this.wb_real = 0; //корректируем
		this.top_iron = true;
	}else{
		this.point_5 = sDraw.intersection(s_line_0_5, s_line_4_5, 'higher');
		this.point_6 = new QPointF((this.point_5.x() + this.wb_real), this.point_5.y());
		/*if(this.wb_real == 0){
			this.point_6 = new QPointF(this.point_5.x(), this.point_5.y());*/
		if(this.point_6x !== null && this.point_6.x() >= this.point_6x.x()){
			this.point_6 = new QPointF(this.point_6x.x(), this.point_6x.y());
			this.wb_real = this.point_6.x() - this.point_5.x(); //корректируем
			this.top_iron = true;
		}
	}	
	
	var s_line_0_6 = sDraw.createLineBy2Points(0, 0, this.point_6.x(), this.point_6.y());
	this.angle_0_6 = s_line_0_6.angleDeg;
	

	var s_line_5_2 = sDraw.createLineByPointAngle(this.point_5.x(), this.point_5.y(), 90);
	var s_line_6_3 = sDraw.createLineByPointAngle(this.point_6.x(), this.point_6.y(), 90);
	
	this.point_1 = new QPointF(0, (this.Rrouter - this.dis_real + this.dpm_real));
	var s_line_1_3 = sDraw.createLineByPointAngle(this.point_1.x(), this.point_1.y(), 0);
	this.point_2 = sDraw.intersection(s_line_5_2, s_line_1_3, 'higher');
	this.point_3 = sDraw.intersection(s_line_6_3, s_line_1_3, 'higher');
	this.point_3x = sDraw.intersection(s_line_1_3, circle_Rrouter, 'right'); 
	if(this.point_3x == null){
		this.point_3x = new QPointF(0, this.point_1.y());
	}
	var s_line_0_2;

	if(!this.k_1){
		if(this.point_3x.x() < this.point_3.x() && this.point_3x.x() >= this.point_2.x()){
			//Коррекция воздушной полости при пересечении внешнего диаметра окружности 
			this.point_3 = new QPointF(this.point_3x.x(), this.point_3x.y());
			this.wb_real = this.point_3.x() - this.point_2.x();
			console.warn('Barrier width has been corrected: Wb = '+this.wb_real);
			this.k_1 = true;
			return this.resetGeometry();
		}else if(this.point_3x.x() < this.point_2.x()){
			//Коррекция угла основания магнита при пересечении внешнего диаметра окружности 
			this.point_2 = new QPointF(this.point_3x.x(), this.point_3x.y());
			//s_line_0_5 = sDraw.createLineBy2Points(0, 0, this.point_5.x(), this.point_5.y());
			//this.alfa_pm = 90 - s_line_0_5.angleDeg;
			this.wpm_real = this.point_2.x() * 2;
			this.wb_real = 0;
			console.warn('Magnet width has been corrected: Wpm = '+this.wpm_real);
			this.k_1 = true;
			return this.resetGeometry();
		}
	}else{
		this.top_iron = true;
	}
	s_line_0_2 = sDraw.createLineBy2Points(0, 0, this.point_2.x(), this.point_2.y());
	var arc_10_1 = new Arc(Math.radToDeg(s_line_0_10.angleRad), 90, this.Rrouter, 0, 0);
	var arc_8_7 = new Arc(Math.radToDeg(s_line_0_10.angleRad), 90, this.Rrinner, 0, 0);
	
	this.line_4_5 = new Segment(this.point_4, this.point_5);
	this.line_1_2 = new Segment(this.point_1, this.point_2);
	this.line_5_2 = new Segment(this.point_5, this.point_2);
	this.line_5_6 = new Segment(this.point_5, this.point_6);
	this.line_2_3 = new Segment(this.point_2, this.point_3);
	this.line_6_3 = new Segment(this.point_6, this.point_3);
	
	this.geomr.push(arc_10_1);    // arc 10-1
	this.geomr.push(arc_8_7);    // arc 8-7
	this.geomr.push(this.line_4_5);    // line_4_5
	this.geomr.push(this.line_1_2);    // line_1_2
	this.geomr.push(this.line_5_2);    // line_5_2
	
	if(this.wb_real > 0){
		this.geomr.push(this.line_2_3);    // line_2_3
		this.geomr.push(this.line_5_6);    // line_5_6
		this.geomr.push(this.line_6_3);    // line_6_3
	}
	
	this.k_1 = false;
	return this.geomr;
};

// call from с++ code
Rotor.prototype.getMaterials = function() { // assign material types

	var materials = new Array();
	var delta = 0.001;
	
	// rotor iron
	if (this.Rrinner > 0 || this.point_4.y() > 0) {
		materials.push( new IronMaterial((this.point_4.x() + this.point_6x.x()) / 2 + delta, (this.point_4.y() + this.Rrinner) / 2 - 10*delta));   
	}
	if (Math.abs(this.point_3.x() - this.point_3x.x()) < 0.001 && this.dis_real - this.dpm_real > 0) {  
		materials.push( new IronMaterial(delta, (motor.rotorOuterRadius + (motor.rotorOuterRadius - this.dis_real + this.dpm_real))/2));  
	}
	if (Math.abs(this.point_6.x() - this.point_6x.x()) < 0.001 && this.dpm_real > 0 || this.point_6.y() == 0) {  
		materials.push( new IronMaterial(this.point_3.x() + 10*delta, (this.point_3.y() + this.point_5.y())/2));   
	}
	
	// magnet
	if (this.wpm_real > 0 && this.dpm_real > 0) {   
		var materialMagnet = new MagnetParallelMaterial;
		materialMagnet.pos = new QPointF(delta, motor.rotorOuterRadius - this.dis_real + this.dpm_real/2);
		materialMagnet.angle = 90;
		materials.push(materialMagnet); 
	}
	
	// general
	if (this.wb_real > 0 && this.dpm_real > 0) {
		this.point_general = new QPointF((this.point_5.x() + this.point_6.x()) / 2, this.point_5.y() + this.dpm_real / 2);// point to assign general material
		materials.push( new GeneralMaterial(this.point_general.x(), this.point_general.y())); 
	}  
	
	return materials;
};
	
var rotor = new Rotor();

// rotor widgets constuctor
function RotorUI(ui)
{
	this.ui = ui;

	this.labelDpm = new QLabel("Magnet depth (Dpm):");
	this.labelDpm.toolTip = "Use the ‘? Guide’ button above for explanation on this parameter";
	this.editDpm = new QDoubleSpinBox;
	this.editDpm.objectName = "Dpm";
	this.editDpm.setRange(0.0, Math.abs(motor.rotorOuterRadius - motor.rotorInnerRadius));
	this.editDpm.decimals = 2;
	this.editDpm.singleStep = 1;
	this.editDpm.keyboardTracking = false;
	this.editDpm.value = rotor.Dpm;
	//this.editDpm['valueChanged(double)'].connect(this, function(value){
	//	rotor.Dpm = value;
	//});
	this.unitsDpm = new QLabel("mm");
	
	this.labelWpm = new QLabel("Magnet width (Wpm):");
	this.labelWpm.toolTip = "Use the ‘? Guide’ button above for explanation on this parameter";
	this.editWpm = new QDoubleSpinBox;
	this.editWpm.objectName = "Wpm";
	this.editWpm.setRange(0, motor.rotorOuterRadius * 2);
	this.editWpm.decimals = 1;
	this.editWpm.singleStep = 1;
	this.editWpm.keyboardTracking = false;
	this.editWpm.value = rotor.Wpm;
	//this.editWpm['valueChanged(double)'].connect(this, function(value){
	//	rotor.Wpm = value;
	//});
	this.unitsWpm = new QLabel("mm");
	
	this.labelDis = new QLabel("Magnet inset depth (Dis):");
	this.labelDis.toolTip = "Use the ‘? Guide’ button above for explanation on this parameter";
	this.editDis = new QDoubleSpinBox;
	this.editDis.objectName = "Dis";
	this.editDis.setRange(0, motor.rotorOuterRadius);
	this.editDis.decimals = 1;
	this.editDis.singleStep = 1;
	this.editDis.keyboardTracking = false;
	this.editDis.value = rotor.Dis;
	//this.editDis['valueChanged(double)'].connect(this, function(value){
	//	rotor.Dis = value;
	//});
	this.unitsDis = new QLabel("mm");
	
	this.labelWb = new QLabel("Barrier width (Wb):");
	this.labelWb.toolTip = "Use the ‘? Guide’ button above for explanation on this parameter";
	this.editWb = new QDoubleSpinBox;
	this.editWb.objectName = "Wb";
	this.editWb.setRange(0, Math.abs(motor.rotorOuterRadius - motor.rotorInnerRadius));
	this.editWb.decimals = 2;
	this.editWb.singleStep = 0.1;
	this.editWb.keyboardTracking = false;
	this.editWb.value = rotor.Wb;
	//this.editWb['valueChanged(double)'].connect(this, function(value){
	//	rotor.Wb = value;
	//});
	this.unitsWb = new QLabel("mm");
	
	this.layout = new QGridLayout(ui);
	
	this.layout.addWidget(this.labelDpm, 0, 0);
	this.layout.addWidget(this.editDpm, 0, 1);
	this.layout.addWidget(this.unitsDpm, 0, 2);

	this.layout.addWidget(this.labelWpm, 1, 0);
	this.layout.addWidget(this.editWpm, 1, 1);
	this.layout.addWidget(this.unitsWpm, 1, 2);
				
	this.layout.addWidget(this.labelDis, 3, 0);
	this.layout.addWidget(this.editDis, 3, 1);
	this.layout.addWidget(this.unitsDis, 3, 2);
		
	this.layout.addWidget(this.labelWb, 2, 0);
	this.layout.addWidget(this.editWb, 2, 1);
	this.layout.addWidget(this.unitsWb, 2, 2);
};

// update rotor widgets
RotorUI.prototype.updateUI = function()
{
	//console.log("rotorUI.updateUI");
}

// call this function from c++ code
createRotorUI = function(ui)
{
	rotorUI = new RotorUI(ui);
	rotorUI.updateUI();
}

	

	
	
	
