// Spoke 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.Wpm = 5*lag;
	
	this.Tpm = 20;
	
	this.Dis = this.Wpm;
	
	
	// Magnet width:
	this.Wpm = 0.6*(this.Rrouter - this.Rrinner);
	// Magnet inset depth:
	this.Dis = 1.3*this.Wpm;
	// Bridge depth
	this.Db = 0.2*this.Wpm;
	if (this.Npp > 2) {
		// Magnet thickness:
		this.Tpm = 0.2*2*(this.Rrouter - this.Dis)*Math.tan(Math.PI/this.Npp/2);
	}else {
		// Magnet thickness:
		var gamma = Math.PI/4;
		this.Tpm = 0.05*2*(this.Rrouter - this.Dis)*Math.tan(gamma);
	}
		
	this.Wsi = 0;
	
	this.apm_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.tpm_real = this.Tpm;
	this.wpm_real = this.Wpm;
	this.db_real = this.Db;
	this.wsi_real = this.Wsi;
	this.dis_real = this.Dis;
	this.k_1 = false;
	this.t = 0;
	//console.log(222);
	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-11
	var s_line_0_11 = 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.wsi_real > this.tpm_real){
		//коррекция ширины щели
		this.wsi_real = this.tpm_real;
		console.warn('Bridge slit width is greater than the thickness of the magnet. Bridge slit width has been corrected: Wsi = Tpm');
	}
	
	this.point_1 = new QPointF(0, (this.Rrouter));
	this.point_1y = new QPointF(0, (Math.sqrt(Math.pow(this.Rrouter, 2) - Math.pow(this.wsi_real / 2, 2))));
	this.point_2 = new QPointF(this.wsi_real / 2, this.point_1y.y());
	this.point_3 = new QPointF(this.point_2.x(), (this.point_2.y() - this.db_real));
	this.point_4 = new QPointF(this.tpm_real / 2, this.point_3.y());
	
	this.point_7 = new QPointF(0, (this.point_1.y() - this.dis_real));
	
	this.point_5 = new QPointF(0, (this.point_7.y() + this.wpm_real));
	this.point_8 = new QPointF(this.tpm_real / 2, this.point_7.y());
	this.point_6 = new QPointF(this.point_8.x(), this.point_5.y());
	//console.log(this.point_7);
	//Находим точку 8y
	var s_line_8_6 = sDraw.createLineByPointAngle(this.point_8.x(), this.point_8.y(), 90);
	this.point_8y = sDraw.intersection(s_line_0_11, s_line_8_6, 'right'); //точка пересечения линий 0_11 и 8_6
	
	//Находим точку 8x и корректируем соответственно Tpm
	var s_line_7_8 = sDraw.createLineByPointAngle(this.point_7.x(), this.point_7.y(), 0);
	this.point_8x11 = sDraw.intersection(s_line_0_11, s_line_7_8, 'right'); //точка пересечения линий 0_11 и 7_8
	this.point_8xRo = sDraw.intersection(circle_Rrouter, s_line_7_8, 'right'); //точка пересечения внешнего радиуса и линии 7_8
	if(this.Npp == 1 || this.point_8xRo.x() < this.point_8x11.x()){
		this.point_8x = new QPointF(this.point_8xRo.x(), this.point_8xRo.y());
	}else{
		this.point_8x = new QPointF(this.point_8x11.x(), this.point_8x11.y());
	}
	if(this.point_8x.x() < this.point_8.x()){
		this.point_8 = new QPointF(this.point_8x.x(), this.point_8x.y());
		//корректировка
		this.tpm_real = 2 * this.point_8.x(); //скорректировали
		console.warn('Magnet thickness does not fit rotor dimensions. Magnet thickness has been corrected: Tpm = ' + this.tpm_real);
		return this.resetGeometry();
	}
	
	//Находим точку 4x
	var s_line_3_4 = sDraw.createLineByPointAngle(this.point_3.x(), this.point_3.y(), 0);
	this.point_4x = sDraw.intersection(circle_Rrouter, s_line_3_4, 'right');//точка пересечения внешнего круга и линии 3_4
	
	//Находим точку 4y и корректируем соответственно величину Db
	this.point_4y = sDraw.intersection(circle_Rrouter, s_line_8_6, 'higher'); //точка пересечения внешнего радиуса и линии 8_6
	if(this.point_4y.y() < this.point_8.y()){
		this.point_4y = new QPointF(this.point_8.x(), this.point_8.y());
	}
	
	if(this.point_4.y() - this.point_4y.y() > 0.0000001){
		this.point_4 = new QPointF(this.point_4y.x(), this.point_4y.y());
		//корректировка
		this.db_real = this.point_1y.y() - this.point_4.y(); //скорректировали
		console.warn('Bridge depth has been corrected: Db = ' + this.db_real);
		return this.resetGeometry();
	}else if(this.point_8.y() > this.point_4.y()){
		this.point_4 = new QPointF(this.point_8.x(), this.point_8.y());
		//корректировка
		this.db_real = this.point_1y.y() - this.point_4.y(); //скорректировали
		console.warn('Bridge depth has been corrected: Db = ' + this.db_real);
		return this.resetGeometry();
	}
	
	//Корректируем Wpm
	if(this.point_4.y() < this.point_6.y()){
		this.point_6 = new QPointF(this.point_4.x(), this.point_4.y());
		//корректировка
		this.wpm_real = this.point_6.y() - this.point_8.y(); //скорректировали
		console.warn('Magnet width has been corrected: Wpm = ' + this.wpm_real);
		return this.resetGeometry();
	}
	
	var s_line_0_2 = sDraw.createLineBy2Points(0, 0, this.point_2.x(), this.point_2.y());
	
	var arc_11_2 = new Arc(Math.radToDeg(s_line_0_11.angleRad), Math.radToDeg(s_line_0_2.angleRad), this.Rrouter, 0, 0);
	var arc_1_2 = new Arc(Math.radToDeg(s_line_0_2.angleRad), 90, this.Rrouter, 0, 0);
	var arc_10_9 = new Arc(Math.radToDeg(s_line_0_11.angleRad), 90, this.Rrinner, 0, 0);
	this.line_7_8 = new Segment(this.point_7, this.point_8);
	this.line_5_6 = new Segment(this.point_5, this.point_6);
	this.line_3_4 = new Segment(this.point_3, this.point_4);
	this.line_3_2 = new Segment(this.point_3, this.point_2);
	this.line_6_4 = new Segment(this.point_6, this.point_4);
	this.line_8_6 = new Segment(this.point_8, this.point_6);
	
	this.geomr.push(arc_11_2);    // arc_11_2
	this.geomr.push(arc_1_2);    // arc_1_2
	this.geomr.push(arc_10_9);    // arc_10_9
	this.geomr.push(this.line_7_8);    // line_7_8
	this.geomr.push(this.line_5_6);    // line_5_6
	this.geomr.push(this.line_8_6);    // line_8_6
	this.geomr.push(this.line_6_4);    // line_6_4
	this.geomr.push(this.line_3_4);    // line_3_4
	if(this.wsi_real > 0){
		this.geomr.push(this.line_3_2);    // line_3_2
	}
	
	this.k_1 = false;
	this.t = 0;
	//console.dir(this.geomr);
	return this.geomr;
};

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

	var materials = new Array();
	var delta = 0.001;
	
	// rotor iron
	if(Math.abs(this.point_8.x() - this.point_8x.x()) < 0.001 || this.point_8.y() == 0){
		materials.push( new IronMaterial(this.point_8.x() + delta, (this.point_4.y() + this.point_8.y()) / 2)); 
	}
	if(Math.abs(this.point_4.x() - this.point_4x.x()) < 0.001
		&& this.point_3.x() < this.point_4.x()){
		materials.push( new IronMaterial((this.point_3.x() + this.point_4x.x()) / 2, (this.point_2.y() + this.point_3.y()) / 2)); 
	}
	if(this.Rrinner > 0 || this.point_7.y() > 0){
		materials.push( new IronMaterial((this.point_7.x() + this.point_8x.x()) / 2, (this.point_7.y() + this.Rrinner) / 2 - 10*delta)); 
	}
	
	// magnet
	if (this.wpm_real > 0 && this.tpm_real > 0) {  
		var materialMagnet = new MagnetParallelMaterial;
		materialMagnet.pos = new QPointF(delta, motor.rotorOuterRadius - this.dis_real + this.wpm_real / 2);
		materialMagnet.angle = 0;
		materials.push(materialMagnet);
	}
	
	// general
	if (this.wsi_real > 0 && this.db_real > 0) {
		materials.push( new GeneralMaterial(this.point_5.x() + delta, (this.point_1.y() + this.point_5.y()) / 2)); 
	}else if(this.point_4.y() > this.point_6.y()) {
		materials.push( new GeneralMaterial(this.point_5.x() + delta, (this.point_4.y() + this.point_5.y()) / 2)); 
	}  
	
	return materials;
};
	
var rotor = new Rotor();

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

	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.0, Math.abs(motor.rotorOuterRadius - motor.rotorInnerRadius));
	this.editWpm.decimals = 2;
	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.labelTpm = new QLabel("Magnet thickness (Tpm):");
	this.labelTpm.toolTip = "Use the ‘? Guide’ button above for explanation on this parameter";
	this.editTpm = new QDoubleSpinBox;
	this.editTpm.objectName = "Tpm";
	this.editTpm.setRange(0, motor.rotorOuterRadius * 2);
	this.editTpm.decimals = 1;
	this.editTpm.singleStep = 1;
	this.editTpm.keyboardTracking = false;
	this.editTpm.value = rotor.Tpm;
	//this.editTpm['valueChanged(double)'].connect(this, function(value){
	//	rotor.Tpm = value;
	//});
	this.unitsTpm = 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, Math.abs(motor.rotorOuterRadius - motor.rotorInnerRadius));
	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.labelDb = new QLabel("Bridge depth (Db):");
	this.labelDb.toolTip = "Use the ‘? Guide’ button above for explanation on this parameter";
	this.editDb = new QDoubleSpinBox;
	this.editDb.objectName = "Db";
	this.editDb.setRange(0, Math.abs(motor.rotorOuterRadius - motor.rotorInnerRadius));
	this.editDb.decimals = 2;
	this.editDb.singleStep = 0.1;
	this.editDb.keyboardTracking = false;
	this.editDb.value = rotor.Db;
	//this.editDb['valueChanged(double)'].connect(this, function(value){
	//	rotor.Db = value;
	//});
	this.unitsDb = new QLabel("mm");
	
	this.labelWsi = new QLabel("Bridge slit width (Wsi):");
	this.labelWsi.toolTip = "Use the ‘? Guide’ button above for explanation on this parameter";
	this.editWsi = new QDoubleSpinBox;
	this.editWsi.objectName = "Wsi";
	this.editWsi.setRange(0, motor.rotorOuterRadius * 2);
	this.editWsi.decimals = 2;
	this.editWsi.singleStep = 0.1;
	this.editWsi.keyboardTracking = false;
	this.editWsi.value = rotor.Wsi;
	//this.editWsi['valueChanged(double)'].connect(this, function(value){
	//	rotor.Wsi = value;
	//});
	this.unitsWsi = new QLabel("mm");
	
	this.layout = new QGridLayout(ui);
	
	this.layout.addWidget(this.labelWpm, 0, 0);
	this.layout.addWidget(this.editWpm, 0, 1);
	this.layout.addWidget(this.unitsWpm, 0, 2);

	this.layout.addWidget(this.labelTpm, 1, 0);
	this.layout.addWidget(this.editTpm, 1, 1);
	this.layout.addWidget(this.unitsTpm, 1, 2);
				
	this.layout.addWidget(this.labelDis, 2, 0);
	this.layout.addWidget(this.editDis, 2, 1);
	this.layout.addWidget(this.unitsDis, 2, 2);
		
	this.layout.addWidget(this.labelDb, 3, 0);
	this.layout.addWidget(this.editDb, 3, 1);
	this.layout.addWidget(this.unitsDb, 3, 2);
	
	this.layout.addWidget(this.labelWsi, 4, 0);
	this.layout.addWidget(this.editWsi, 4, 1);
	this.layout.addWidget(this.unitsWsi, 4, 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();
}

	

	
	
	
