// Rotor with bread loaf magnets
//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);
	if (this.Npp > 2) {
		// Magnet width:
		this.Wpm = 0.7*2*(this.Rrouter - this.Dpm)*Math.tan(Math.PI/this.Npp/2);
	}else {
		// Magnet width:
		var gamma = Math.PI/4;
		this.Wpm = 0.5*2*(this.Rrouter - this.Dpm)*Math.tan(gamma);
	}
	// Magnet radius
	this.Rpm = this.Rrouter;
	
	this.Fpm = 0;
			
	this.Dis = 0;
	
};


// generate rotor geometry 
// call from с++ code
Rotor.prototype.getGeometry = 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;
	// Air gap length
	this.lag = motor.airGap;
	
	// draw rotor geometry
	this.geomr = new Array;
	
	var point_0 = new QPointF(0, 0);
	
	this.alfa = 360/this.Npp/4; //angle 1-0-6
	
	var s_line_0_3 = sDraw.createLineByPointAngle(0, 0, 90-this.alfa); // rigth sector border	
	
	var circle_Rrouter = sDraw.createCircle(0, 0, this.Rrouter); // outer circle
	var circle_Rrinner = sDraw.createCircle(0, 0, this.Rrinner); // inner circle
	
	// only inner rotor
	
	//Mechanical angle of the magnet related to (0; 0)
	this.alfa_pm = Math.radToDeg(Math.atan(this.Wpm / 2 / (this.Rrouter - this.Dpm)));   //angle 1_0_5
	
	this.point_4 = new QPointF(0, (this.Rrouter - this.Dpm));
	var s_line_4_5 = sDraw.createLineByPointAngle(this.point_4.x(), this.point_4.y(), 0);
	
	this.alfa_pm_1_0_2 = Math.radToDeg(Math.asin((this.Wpm / 2) / this.Rrouter));
	
	var s_line_0_2 = sDraw.createLineByPointAngle(0, 0, 90-this.alfa_pm_1_0_2);
	var s_line_0_5 = sDraw.createLineByPointAngle(0, 0, 90-this.alfa_pm);
		
	var point_1 = new QPointF(0, this.Rrouter);
	this.point_2 = sDraw.intersection(s_line_0_2, circle_Rrouter, 'higher');   

	this.point_5 = new QPointF(this.point_4.x() + this.Wpm / 2, this.point_4.y());
	this.point_5x = new QPointF(this.point_5.x(), this.point_5.y()+this.Dis);
	var s_line_0_5x = sDraw.createLineBy2Points(0, 0, this.point_5x.x(), this.point_5x.y());
	
	this.line_4_5 = new Segment(this.point_4, this.point_5);
	var arc_8_7 = new Arc(Math.radToDeg(s_line_0_3.angleRad), 90, this.Rrinner, 0, 0);
	this.arc_2_1 = new Arc(Math.radToDeg(s_line_0_2.angleRad), 90, this.Rrouter, 0, 0);
	
	var delta = 0.01;
	var circle_Rpm = sDraw.createCircle(0, this.Rrouter-this.Rpm-delta, this.Rpm); // outer magnet border circle
	var s_line_5_2 = sDraw.createLineBy2Points(this.point_5.x(), this.point_5.y(), this.point_2.x(), this.point_2.y());
	this.point_2Rpm = sDraw.intersection(circle_Rpm, s_line_5_2, 'higher');   
	var s_line_2Rpm = sDraw.createLineBy2Points(0, this.Rrouter-this.Rpm-delta, this.point_2Rpm.x(), this.point_2Rpm.y());
	
	this.line_5_2Rpm = new Segment(this.point_5, this.point_2Rpm);
	this.arc_2Rpm_1 = new Arc(Math.radToDeg(s_line_2Rpm.angleRad), 90, this.Rpm, 0, this.Rrouter-this.Rpm-delta);
	
	this.arc_6_5x = new Arc(90-this.alfa, Math.radToDeg(s_line_0_5x.angleRad), Math.sqrt((this.Wpm/2)*(this.Wpm/2) + (this.Rrouter-this.Dpm+this.Dis)*(this.Rrouter-this.Dpm+this.Dis)), 0, 0);
	
	if (this.Fpm == 0) {
		this.geomr.push(this.line_5_2Rpm);
		this.geomr.push(this.arc_2Rpm_1);
	}
	else {
		var g1 = couplingSegments(this.line_5_2Rpm, this.arc_2Rpm_1, this.Fpm);
		this.geomr.push(g1[0]);
		this.geomr.push(g1[1]);
		this.geomr.push(g1[2]);
	}
	this.geomr.push(this.line_4_5);    // line_4_5
	this.geomr.push(arc_8_7);    // arc 8-7
	this.geomr.push(this.arc_6_5x);
	
	this.geomr.push(this.arc_3_1);
	
	return this.geomr;
}



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

	var materials = new Array();
	var delta = 0.01;
	
	materials.push( new IronMaterial(delta,(motor.rotorOuterRadius - this.Dpm + motor.rotorInnerRadius) / 2));   // rotor iron
	
	var materialMagnet = new MagnetRadialMaterial;
	materialMagnet.pos = new QPointF(delta, motor.rotorOuterRadius - this.Dpm/2);
	materialMagnet.center = new QPointF(0, 0);
	materialMagnet.direction = Direction.From;
	materials.push(materialMagnet); 
	
	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, motor.rotorOuterRadius);
	this.editDpm.decimals = 2;
	this.editDpm.singleStep = 1;
	this.editDpm.keyboardTracking = false;
	this.editDpm.value = rotor.Dpm;
	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.unitsWpm = new QLabel("mm");
	
	this.labelRpm = new QLabel("Magnet radius (Rpm):");
	this.labelRpm.toolTip = "Use the ‘? Guide’ button above for explanation on this parameter";
	this.editRpm = new QDoubleSpinBox;
	this.editRpm.objectName = "Rpm";
	this.editRpm.setRange(0, motor.rotorOuterRadius);
	this.editRpm.decimals = 1;
	this.editRpm.singleStep = 1;
	this.editRpm.keyboardTracking = false;
	this.editRpm.value = rotor.Rpm;
	this.unitsRpm = new QLabel("mm");
	
	this.labelFpm = new QLabel("Magnet fillet radius (Fpm):");
	this.labelFpm.toolTip = "Use the ‘? Guide’ button above for explanation on this parameter";
	this.editFpm = new QDoubleSpinBox;
	this.editFpm.objectName = "Fpm";
	this.editFpm.setRange(0.0, Math.abs(motor.rotorOuterRadius - motor.rotorInnerRadius));
	this.editFpm.decimals = 2;
	this.editFpm.singleStep = 0.1;
	this.editFpm.keyboardTracking = false;
	this.editFpm.value = rotor.Fpm;
	this.unitsFpm = 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.unitsDis = 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.labelRpm, 2, 0);
	this.layout.addWidget(this.editRpm, 2, 1);
	this.layout.addWidget(this.unitsRpm, 2, 2);
	
	this.layout.addWidget(this.labelFpm, 3, 0);
	this.layout.addWidget(this.editFpm, 3, 1);
	this.layout.addWidget(this.unitsFpm, 3, 2);
			
	this.layout.addWidget(this.labelDis, 4, 0);
	this.layout.addWidget(this.editDis, 4, 1);
	this.layout.addWidget(this.unitsDis, 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();
}

	

	
	
	
