r3-legacy/src/r3-api-quaternion-points.js

303 lines
7.2 KiB
JavaScript
Raw Normal View History

2019-07-24 14:45:42 +02:00
/**
* R3.API.Quaternion.Points
2019-10-06 21:11:18 +02:00
* @param apiComponent
2019-07-24 14:45:42 +02:00
* @constructor
*/
2019-10-06 21:11:18 +02:00
R3.API.Quaternion.Points = function(
apiComponent
) {
__API_COMPONENT__;
if (R3.Utils.UndefinedOrNull(apiComponent.vectors)) {
apiComponent.vectors = [];
}
this.vectors = apiComponent.vectors;
2018-04-09 09:35:04 +02:00
};
2019-10-06 21:11:18 +02:00
R3.API.Quaternion.Points.prototype = Object.create(R3.API.Component.prototype);
R3.API.Quaternion.Points.prototype.constructor = R3.API.Quaternion.Points;
2019-07-24 08:08:02 +02:00
R3.API.Quaternion.Points.prototype.add = function(vector) {
2018-04-09 09:35:04 +02:00
if (vector instanceof R3.API.Vector3) {
vector = new R3.API.Quaternion(
2019-10-06 21:11:18 +02:00
{
parent : this,
register : true,
x : vector.x,
y : vector.y,
z : vector.z,
w : 1
}
2018-04-09 09:35:04 +02:00
)
}
if (!vector instanceof R3.API.Quaternion) {
console.warn("Vector needs to be of type Quaternion");
throw new Error("Vector needs to be of type Quaternion");
}
this.vectors.push(vector);
return this;
};
2019-07-24 08:08:02 +02:00
R3.API.Quaternion.Points.prototype.copy = function() {
2018-04-09 09:35:04 +02:00
var vectors = [];
for (var i = 0; i < this.vectors.length; i++) {
vectors.push(this.vectors[i].copy());
}
return vectors;
};
2019-07-24 08:08:02 +02:00
R3.API.Quaternion.Points.prototype.maximizeXDistance = function(grain) {
2018-04-09 09:35:04 +02:00
// console.log("vectors (before): " + JSON.stringify(this.vectors, null, 2));
var multiplier = 0;
2019-10-06 21:11:18 +02:00
var rotationMatrixY = new R3.API.Matrix4(
{
parent : this
}
).rotationMatrixY(grain);
2018-04-09 09:35:04 +02:00
var totalRadians = 0;
var backupVectors = this.copy();
var maxXDistance = 0;
for (var i = 0; i < Math.PI * 2; i += grain) {
multiplier++;
for (var j = 0; j < this.vectors.length; j++) {
this.vectors[j] = rotationMatrixY.multiply(this.vectors[j]);
}
var distances = this.distances();
if (distances.x > maxXDistance) {
maxXDistance = distances.x;
totalRadians = multiplier * grain;
}
}
this.vectors = backupVectors;
// console.log("distance: " + maxXDistance + " radians : " + totalRadians);
2019-10-06 21:11:18 +02:00
var maxRotationMatrix = new R3.API.Matrix4(
{
parent : this,
register : true
}
).rotationMatrixY(totalRadians);
2018-04-09 09:35:04 +02:00
for (var k = 0; k < this.vectors.length; k++) {
this.vectors[k] = maxRotationMatrix.multiply(this.vectors[k]);
}
// console.log("vectors (after): " + JSON.stringify(this.vectors, null, 2));
};
2019-07-24 08:08:02 +02:00
R3.API.Quaternion.Points.prototype.maximizeYDistance = function(grain) {
2018-04-09 09:35:04 +02:00
// console.log("vectors (before): " + JSON.stringify(this.vectors, null, 2));
var multiplier = 0;
2019-10-06 21:11:18 +02:00
var rotationMatrixX = new R3.API.Matrix4(
{
parent : this,
register : true
}
).rotationMatrixX(grain);
2018-04-09 09:35:04 +02:00
var totalRadians = 0;
var backupVectors = this.copy();
var maxYDistance = 0;
for (var i = 0; i < Math.PI * 2; i += grain) {
multiplier++;
for (var j = 0; j < this.vectors.length; j++) {
this.vectors[j] = rotationMatrixX.multiply(this.vectors[j]);
}
var distances = this.distances();
if (distances.y > maxYDistance) {
maxYDistance = distances.y;
totalRadians = multiplier * grain;
}
}
this.vectors = backupVectors;
// console.log("distance: " + maxYDistance + " radians : " + totalRadians);
2019-10-06 21:11:18 +02:00
var maxRotationMatrix = new R3.API.Matrix4(
{
parent : this,
register : true
}
).rotationMatrixX(totalRadians);
2018-04-09 09:35:04 +02:00
for (var k = 0; k < this.vectors.length; k++) {
this.vectors[k] = maxRotationMatrix.multiply(this.vectors[k]);
}
// console.log("vectors (after): " + JSON.stringify(this.vectors, null, 2));
};
2019-07-24 08:08:02 +02:00
R3.API.Quaternion.Points.prototype.lookAt = function(at, up) {
2018-04-09 09:35:04 +02:00
var polyCenter = this.average();
console.log("poly center : " + JSON.stringify(polyCenter));
2019-10-06 21:11:18 +02:00
var lookAtMatrix = new R3.API.Matrix4({
parent : this
}).lookAt(polyCenter, at, up);
lookAtMatrix.rows[0] = new R3.API.Quaternion(
{
parent : this,
register : true,
x : 1,
y : 0,
z : 0,
w : 0
}
);
2018-04-09 09:35:04 +02:00
2019-10-06 21:11:18 +02:00
lookAtMatrix.rows[1] = new R3.API.Quaternion(
{
parent : this,
register : true,
x : 0,
y : 0,
z : 1,
w : 0
}
);
lookAtMatrix.rows[2] = new R3.API.Quaternion(
{
parent : this,
register : true,
x : 0,
y : 1,
z : 0,
w : 0
}
);
2018-04-09 09:35:04 +02:00
console.log("look at matrix : " + JSON.stringify(lookAtMatrix, null, 2));
for (var i = 0; i < this.vectors.length; i++) {
console.log("vector " + i + " (before): " + JSON.stringify(this.vectors[i]));
this.vectors[i] = lookAtMatrix.multiply(this.vectors[i]);
console.log("vector " + i + " (after) : " + JSON.stringify(this.vectors[i]));
}
};
2019-07-24 08:08:02 +02:00
R3.API.Quaternion.Points.prototype.distances = function() {
2018-04-09 09:35:04 +02:00
var minX = this.vectors[0].x;
var minY = this.vectors[0].y;
var minZ = this.vectors[0].z;
var maxX = this.vectors[0].x;
var maxY = this.vectors[0].y;
var maxZ = this.vectors[0].z;
for (var i = 0; i < this.vectors.length; i++) {
if (this.vectors[i].x < minX) {
minX = this.vectors[i].x;
}
if (this.vectors[i].y < minY) {
minY = this.vectors[i].y;
}
if (this.vectors[i].z < minZ) {
minZ = this.vectors[i].z;
}
if (this.vectors[i].x > maxX) {
maxX = this.vectors[i].x;
}
if (this.vectors[i].y > maxY) {
maxY = this.vectors[i].y;
}
if (this.vectors[i].z > maxZ) {
maxZ = this.vectors[i].z;
}
}
return new R3.API.Vector3(
2019-10-06 21:11:18 +02:00
{
parent : this,
x : Math.abs(maxX - minX),
y : Math.abs(maxY - minY),
z : Math.abs(maxY - minZ)
}
2018-04-09 09:35:04 +02:00
)
};
2019-07-24 08:08:02 +02:00
R3.API.Quaternion.Points.prototype.average = function() {
2019-10-06 21:11:18 +02:00
2018-04-09 09:35:04 +02:00
var averageX = 0;
var averageY = 0;
var averageZ = 0;
for (var i = 0; i < this.vectors.length; i++) {
averageX += this.vectors[i].x;
averageY += this.vectors[i].y;
averageZ += this.vectors[i].z;
}
return new R3.API.Vector3(
2019-10-06 21:11:18 +02:00
{
parent : this,
register : true,
x : averageX / this.vectors.length,
y : averageY / this.vectors.length,
z : averageZ / this.vectors.length
}
2018-04-09 09:35:04 +02:00
);
};
2019-07-24 08:08:02 +02:00
R3.API.Quaternion.Points.prototype.negate = function() {
2018-04-09 09:35:04 +02:00
for (var i = 0; i < this.vectors.length; i++) {
this.vectors[i].x *= -1;
this.vectors[i].y *= -1;
this.vectors[i].z *= -1;
}
return this;
};
2019-07-24 08:08:02 +02:00
R3.API.Quaternion.Points.prototype.toOrigin = function() {
2018-04-09 09:35:04 +02:00
var distanceFromOrigin = this.average().negate();
for (var i = 0; i < this.vectors.length; i++) {
this.vectors[i].translate(distanceFromOrigin);
}
};