forked from enviPath/enviPy
Current Dev State
This commit is contained in:
96
static/js/ketcher2/script/util/box2abs.js
Normal file
96
static/js/ketcher2/script/util/box2abs.js
Normal file
@ -0,0 +1,96 @@
|
||||
/****************************************************************************
|
||||
* Copyright 2017 EPAM Systems
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
***************************************************************************/
|
||||
|
||||
var Vec2 = require('./vec2');
|
||||
|
||||
function Box2Abs() {
|
||||
if (arguments.length == 1 && 'min' in arguments[0] && 'max' in arguments[0]) {
|
||||
this.p0 = arguments[0].min;
|
||||
this.p1 = arguments[0].max;
|
||||
}
|
||||
if (arguments.length == 2) {
|
||||
this.p0 = arguments[0];
|
||||
this.p1 = arguments[1];
|
||||
} else if (arguments.length == 4) {
|
||||
this.p0 = new Vec2(arguments[0], arguments[1]);
|
||||
this.p1 = new Vec2(arguments[2], arguments[3]);
|
||||
} else if (arguments.length == 0) {
|
||||
this.p0 = new Vec2();
|
||||
this.p1 = new Vec2();
|
||||
} else {
|
||||
new Error('Box2Abs constructor only accepts 4 numbers or 2 vectors or no arguments!');
|
||||
}
|
||||
}
|
||||
|
||||
Box2Abs.prototype.toString = function () {
|
||||
return this.p0.toString() + ' ' + this.p1.toString();
|
||||
};
|
||||
|
||||
Box2Abs.fromRelBox = function (relBox) {
|
||||
console.assert(!!relBox);
|
||||
return new Box2Abs(relBox.x, relBox.y, relBox.x + relBox.width, relBox.y + relBox.height);
|
||||
};
|
||||
|
||||
Box2Abs.prototype.clone = function () {
|
||||
return new Box2Abs(this.p0, this.p1);
|
||||
};
|
||||
|
||||
Box2Abs.union = function (/* Box2Abs*/b1, /* Box2Abs*/b2) {
|
||||
console.assert(!!b1);
|
||||
console.assert(!!b2);
|
||||
return new Box2Abs(Vec2.min(b1.p0, b2.p0), Vec2.max(b1.p1, b2.p1));
|
||||
};
|
||||
|
||||
Box2Abs.prototype.extend = function (/* Vec2*/lp, /* Vec2*/rb) {
|
||||
console.assert(!!lp);
|
||||
rb = rb || lp;
|
||||
return new Box2Abs(this.p0.sub(lp), this.p1.add(rb));
|
||||
};
|
||||
|
||||
Box2Abs.prototype.include = function (/* Vec2*/p) {
|
||||
console.assert(!!p);
|
||||
return new Box2Abs(this.p0.min(p), this.p1.max(p));
|
||||
};
|
||||
|
||||
Box2Abs.prototype.contains = function (/* Vec2*/p, /* float*/ext) {
|
||||
ext = (ext || 0) - 0;
|
||||
console.assert(!!p);
|
||||
return p.x >= this.p0.x - ext && p.x <= this.p1.x + ext && p.y >= this.p0.y - ext && p.y <= this.p1.y + ext;
|
||||
};
|
||||
|
||||
Box2Abs.prototype.translate = function (/* Vec2*/d) {
|
||||
console.assert(!!d);
|
||||
return new Box2Abs(this.p0.add(d), this.p1.add(d));
|
||||
};
|
||||
|
||||
Box2Abs.prototype.transform = function (/* function(Vec2):Vec2*/f, options) {
|
||||
console.assert(!!f);
|
||||
return new Box2Abs(f(this.p0, options), f(this.p1, options));
|
||||
};
|
||||
|
||||
Box2Abs.prototype.sz = function () {
|
||||
return this.p1.sub(this.p0);
|
||||
};
|
||||
|
||||
Box2Abs.prototype.centre = function () {
|
||||
return Vec2.centre(this.p0, this.p1);
|
||||
};
|
||||
|
||||
Box2Abs.prototype.pos = function () {
|
||||
return this.p0;
|
||||
};
|
||||
|
||||
module.exports = Box2Abs;
|
||||
27
static/js/ketcher2/script/util/constants.js
Normal file
27
static/js/ketcher2/script/util/constants.js
Normal file
@ -0,0 +1,27 @@
|
||||
/****************************************************************************
|
||||
* Copyright 2017 EPAM Systems
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
***************************************************************************/
|
||||
|
||||
const SgContexts = {
|
||||
Fragment: 'Fragment',
|
||||
Multifragment: 'Multifragment',
|
||||
Bond: 'Bond',
|
||||
Atom: 'Atom',
|
||||
Group: 'Group'
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
SgContexts
|
||||
};
|
||||
157
static/js/ketcher2/script/util/map.js
Normal file
157
static/js/ketcher2/script/util/map.js
Normal file
@ -0,0 +1,157 @@
|
||||
/****************************************************************************
|
||||
* Copyright 2017 EPAM Systems
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
***************************************************************************/
|
||||
|
||||
/* eslint-disable no-underscore-dangle*/
|
||||
|
||||
function Map(obj) {
|
||||
if (typeof (obj) !== 'undefined' && obj.constructor !== Object)
|
||||
throw Error('Passed object is not an instance of "Object"!');
|
||||
this._obj = obj || {};
|
||||
this._count = 0;
|
||||
}
|
||||
|
||||
Map.prototype.each = function (func, context) {
|
||||
var v,
|
||||
value,
|
||||
vInt;
|
||||
|
||||
for (v in this._obj) {
|
||||
vInt = parseInt(v, 10);
|
||||
value = this._obj[v];
|
||||
|
||||
if (!isNaN(vInt))
|
||||
v = vInt;
|
||||
func.call(context, v, value);
|
||||
}
|
||||
};
|
||||
|
||||
Map.prototype.map = function (func, context) {
|
||||
var ret = new Map();
|
||||
this.each(function (v, value) {
|
||||
ret.set(v, func.call(context, v, value));
|
||||
}, this);
|
||||
return ret;
|
||||
};
|
||||
|
||||
Map.prototype.find = function (func, context) {
|
||||
var v,
|
||||
vInt,
|
||||
value;
|
||||
|
||||
for (v in this._obj) {
|
||||
vInt = parseInt(v, 10);
|
||||
value = this._obj[v];
|
||||
|
||||
if (!isNaN(vInt))
|
||||
v = vInt;
|
||||
if (func.call(context, v, value))
|
||||
return v;
|
||||
}
|
||||
};
|
||||
|
||||
Map.prototype.findAll = function (func, context) {
|
||||
var v,
|
||||
vInt,
|
||||
value,
|
||||
vv = [];
|
||||
|
||||
for (v in this._obj) {
|
||||
vInt = parseInt(v, 10);
|
||||
value = this._obj[v];
|
||||
if (!isNaN(vInt))
|
||||
v = vInt;
|
||||
if (func.call(context, v, value))
|
||||
vv.push(v);
|
||||
}
|
||||
return vv;
|
||||
};
|
||||
|
||||
Map.prototype.keys = function () {
|
||||
var keys = [],
|
||||
v;
|
||||
for (v in this._obj)
|
||||
keys.push(v);
|
||||
return keys;
|
||||
};
|
||||
|
||||
Map.prototype.ikeys = function () {
|
||||
var keys = [];
|
||||
for (var v in this._obj)
|
||||
keys.push(v - 0);
|
||||
return keys;
|
||||
};
|
||||
|
||||
Map.prototype.values = function () {
|
||||
var values = [];
|
||||
for (var v in this._obj)
|
||||
values.push(this._obj[v]);
|
||||
return values;
|
||||
};
|
||||
|
||||
Map.prototype.set = function (key, value) {
|
||||
var val;
|
||||
this._count += (typeof value !== 'undefined' ? 1 : 0) - (typeof this._obj[key] !== 'undefined' ? 1 : 0);
|
||||
|
||||
if (typeof value === 'undefined') {
|
||||
val = this._obj[key];
|
||||
delete this._obj[key];
|
||||
return val;
|
||||
}
|
||||
|
||||
this._obj[key] = value;
|
||||
return value;
|
||||
};
|
||||
|
||||
Map.prototype.get = function (key) {
|
||||
if (this._obj[key] !== Object.prototype[key])
|
||||
return this._obj[key];
|
||||
return undefined;
|
||||
};
|
||||
|
||||
Map.prototype.has = function (key) {
|
||||
return (this._obj[key] !== Object.prototype[key]);
|
||||
};
|
||||
|
||||
Map.prototype.unset = function (key) {
|
||||
return this.set(key, undefined);
|
||||
};
|
||||
|
||||
Map.prototype.update = function (object) {
|
||||
for (var v in object)
|
||||
this.set(v, object[v]);
|
||||
};
|
||||
|
||||
Map.prototype.clear = function () {
|
||||
this._obj = {};
|
||||
this._count = 0;
|
||||
};
|
||||
|
||||
Map.prototype.count = function () {
|
||||
return this._count;
|
||||
};
|
||||
|
||||
Map.prototype.idList = function () {
|
||||
return Object.keys(this._obj);
|
||||
};
|
||||
|
||||
Map.prototype.keyOf = function (value) {
|
||||
for (var key in this._obj) {
|
||||
if (this._obj[key] === value)
|
||||
return key;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Map;
|
||||
88
static/js/ketcher2/script/util/pool.js
Normal file
88
static/js/ketcher2/script/util/pool.js
Normal file
@ -0,0 +1,88 @@
|
||||
/****************************************************************************
|
||||
* Copyright 2017 EPAM Systems
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
***************************************************************************/
|
||||
|
||||
/* eslint-disable no-underscore-dangle*/
|
||||
|
||||
var Map = require('./map.js');
|
||||
|
||||
function Pool() {
|
||||
this._map = new Map();
|
||||
this._nextId = 0;
|
||||
}
|
||||
|
||||
Pool.prototype.newId = function () {
|
||||
return this._nextId++;
|
||||
};
|
||||
|
||||
Pool.prototype.add = function (obj) {
|
||||
var id = this._nextId++;
|
||||
this._map.set(id, obj);
|
||||
return id;
|
||||
};
|
||||
|
||||
Pool.prototype.set = function (id, obj) {
|
||||
this._map.set(id, obj);
|
||||
};
|
||||
|
||||
Pool.prototype.get = function (id) {
|
||||
return this._map.get(id);
|
||||
};
|
||||
|
||||
Pool.prototype.has = function (id) {
|
||||
return this._map.has(id);
|
||||
};
|
||||
|
||||
Pool.prototype.remove = function (id) {
|
||||
return this._map.unset(id);
|
||||
};
|
||||
|
||||
Pool.prototype.clear = function () {
|
||||
this._map.clear();
|
||||
};
|
||||
|
||||
Pool.prototype.keys = function () {
|
||||
return this._map.keys();
|
||||
};
|
||||
|
||||
Pool.prototype.ikeys = function () {
|
||||
return this._map.ikeys();
|
||||
};
|
||||
|
||||
Pool.prototype.values = function () {
|
||||
return this._map.values();
|
||||
};
|
||||
|
||||
Pool.prototype.each = function (func, context) {
|
||||
this._map.each(func, context);
|
||||
};
|
||||
|
||||
Pool.prototype.map = function (func, context) {
|
||||
return this._map.map(func, context);
|
||||
};
|
||||
|
||||
Pool.prototype.find = function (func, context) {
|
||||
return this._map.find(func, context);
|
||||
};
|
||||
|
||||
Pool.prototype.count = function () {
|
||||
return this._map.count();
|
||||
};
|
||||
|
||||
Pool.prototype.keyOf = function (value) {
|
||||
return this._map.keyOf(value);
|
||||
};
|
||||
|
||||
module.exports = Pool;
|
||||
29
static/js/ketcher2/script/util/scale.js
Normal file
29
static/js/ketcher2/script/util/scale.js
Normal file
@ -0,0 +1,29 @@
|
||||
/****************************************************************************
|
||||
* Copyright 2017 EPAM Systems
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
***************************************************************************/
|
||||
|
||||
function scaled2obj(v, options) {
|
||||
return v.scaled(1 / options.scale);
|
||||
}
|
||||
|
||||
function obj2scaled(v, options) {
|
||||
return v.scaled(options.scale);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
scaled2obj: scaled2obj,
|
||||
obj2scaled: obj2scaled
|
||||
};
|
||||
|
||||
153
static/js/ketcher2/script/util/set.js
Normal file
153
static/js/ketcher2/script/util/set.js
Normal file
@ -0,0 +1,153 @@
|
||||
/****************************************************************************
|
||||
* Copyright 2017 EPAM Systems
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
***************************************************************************/
|
||||
|
||||
/* global module*/
|
||||
|
||||
var Set = {
|
||||
empty: function () {
|
||||
return {};
|
||||
},
|
||||
|
||||
single: function (item) {
|
||||
var set = {};
|
||||
Set.add(set, item);
|
||||
return set;
|
||||
},
|
||||
|
||||
size: function (set) {
|
||||
var cnt = 0;
|
||||
for (var id in set) {
|
||||
if (set[id] !== Object.prototype[id])
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
},
|
||||
|
||||
contains: function (set, v) {
|
||||
return typeof (set[v]) !== 'undefined' && set[v] !== Object.prototype[v];
|
||||
},
|
||||
|
||||
subset: function (subset, superset) {
|
||||
for (var id in subset) {
|
||||
if (subset[id] !== Object.prototype[id] && superset[id] !== subset[id])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
intersection: function (set1, set2) {
|
||||
var set = {};
|
||||
for (var id in set1) {
|
||||
if (set1[id] !== Object.prototype[id] && set2[id] === set1[id])
|
||||
Set.add(set, id);
|
||||
}
|
||||
return set;
|
||||
},
|
||||
|
||||
disjoint: function (set1, set2) {
|
||||
for (var id in set1) {
|
||||
if (set1[id] !== Object.prototype[id] && set2[id] === set1[id])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
eq: function (set1, set2) {
|
||||
return Set.subset(set1, set2) && Set.subset(set2, set1);
|
||||
},
|
||||
|
||||
each: function (set, func, context) {
|
||||
for (var v in set) {
|
||||
if (set[v] !== Object.prototype[v])
|
||||
func.call(context, set[v]);
|
||||
}
|
||||
},
|
||||
|
||||
filter: function (set, func, context) {
|
||||
var subset = {};
|
||||
for (var v in set) {
|
||||
if (set[v] !== Object.prototype[v] && func.call(context, set[v]))
|
||||
subset[set[v]] = set[v];
|
||||
}
|
||||
return subset;
|
||||
},
|
||||
|
||||
pick: function (set) {
|
||||
for (var v in set) {
|
||||
if (set[v] !== Object.prototype[v])
|
||||
return set[v];
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
list: function (set) {
|
||||
var list = [];
|
||||
for (var v in set) {
|
||||
if (set[v] !== Object.prototype[v])
|
||||
list.push(set[v]);
|
||||
}
|
||||
return list;
|
||||
},
|
||||
|
||||
add: function (set, item) {
|
||||
set[item] = item;
|
||||
},
|
||||
|
||||
mergeIn: function (set, other) {
|
||||
Set.each(other, function (item) {
|
||||
Set.add(set, item);
|
||||
});
|
||||
},
|
||||
|
||||
remove: function (set, item) {
|
||||
var v = set[item];
|
||||
delete set[item];
|
||||
return v;
|
||||
},
|
||||
|
||||
clone: function (other) {
|
||||
var set = {};
|
||||
Set.mergeIn(set, other);
|
||||
return set;
|
||||
},
|
||||
|
||||
fromList: function (list) {
|
||||
var set = {};
|
||||
if (list) {
|
||||
for (var i = 0; i < list.length; ++i)
|
||||
set[list[i] - 0] = list[i] - 0;
|
||||
}
|
||||
return set;
|
||||
},
|
||||
|
||||
keySetInt: function (map) {
|
||||
var set = {};
|
||||
map.each(function (id) {
|
||||
set[id - 0] = id - 0;
|
||||
});
|
||||
return set;
|
||||
},
|
||||
|
||||
find: function (set, func, context) {
|
||||
for (var v in set) {
|
||||
if (set[v] !== Object.prototype[v] && func.call(context, set[v]))
|
||||
return v;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Set;
|
||||
297
static/js/ketcher2/script/util/vec2.js
Normal file
297
static/js/ketcher2/script/util/vec2.js
Normal file
@ -0,0 +1,297 @@
|
||||
/****************************************************************************
|
||||
* Copyright 2017 EPAM Systems
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
***************************************************************************/
|
||||
|
||||
function Vec2(x, y, z) {
|
||||
if (arguments.length == 0) {
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
this.z = 0;
|
||||
} else if (arguments.length == 1) {
|
||||
this.x = parseFloat(x.x || 0);
|
||||
this.y = parseFloat(x.y || 0);
|
||||
this.z = parseFloat(x.z || 0);
|
||||
} else if (arguments.length == 2) {
|
||||
this.x = parseFloat(x || 0);
|
||||
this.y = parseFloat(y || 0);
|
||||
this.z = 0;
|
||||
} else if (arguments.length == 3) {
|
||||
this.x = parseFloat(x);
|
||||
this.y = parseFloat(y);
|
||||
this.z = parseFloat(z);
|
||||
} else {
|
||||
throw new Error('Vec2(): invalid arguments');
|
||||
}
|
||||
}
|
||||
|
||||
Vec2.ZERO = new Vec2(0, 0);
|
||||
Vec2.UNIT = new Vec2(1, 1);
|
||||
|
||||
Vec2.segmentIntersection = function (a, b, c, d) {
|
||||
/* eslint-disable no-mixed-operators*/
|
||||
var dc = (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x);
|
||||
var dd = (a.x - d.x) * (b.y - d.y) - (a.y - d.y) * (b.x - d.x);
|
||||
var da = (c.x - a.x) * (d.y - a.y) - (c.y - a.y) * (d.x - a.x);
|
||||
var db = (c.x - b.x) * (d.y - b.y) - (c.y - b.y) * (d.x - b.x);
|
||||
/* eslint-enable no-mixed-operators*/
|
||||
return dc * dd <= 0 && da * db <= 0;
|
||||
};
|
||||
|
||||
Vec2.prototype.length = function () {
|
||||
/* eslint-disable no-mixed-operators*/
|
||||
return Math.sqrt(this.x * this.x + this.y * this.y);
|
||||
/* eslint-enable no-mixed-operators*/
|
||||
};
|
||||
|
||||
Vec2.prototype.equals = function (v) {
|
||||
console.assert(!!v);
|
||||
return this.x == v.x && this.y == v.y;
|
||||
};
|
||||
|
||||
Vec2.prototype.add = function (v) {
|
||||
console.assert(!!v);
|
||||
return new Vec2(this.x + v.x, this.y + v.y, this.z + v.z);
|
||||
};
|
||||
|
||||
Vec2.prototype.add_ = function (v) { // eslint-disable-line no-underscore-dangle
|
||||
console.assert(!!v);
|
||||
this.x += v.x;
|
||||
this.y += v.y;
|
||||
this.z += v.z;
|
||||
};
|
||||
|
||||
Vec2.prototype.get_xy0 = function () {
|
||||
return new Vec2(this.x, this.y);
|
||||
};
|
||||
|
||||
Vec2.prototype.sub = function (v) {
|
||||
console.assert(!!v);
|
||||
return new Vec2(this.x - v.x, this.y - v.y, this.z - v.z);
|
||||
};
|
||||
|
||||
Vec2.prototype.scaled = function (s) {
|
||||
console.assert(s == 0 || !!s);
|
||||
return new Vec2(this.x * s, this.y * s, this.z * s);
|
||||
};
|
||||
|
||||
Vec2.prototype.negated = function () {
|
||||
return new Vec2(-this.x, -this.y, -this.z);
|
||||
};
|
||||
|
||||
Vec2.prototype.yComplement = function (y1) {
|
||||
y1 = y1 || 0;
|
||||
return new Vec2(this.x, y1 - this.y, this.z);
|
||||
};
|
||||
|
||||
Vec2.prototype.addScaled = function (v, f) {
|
||||
console.assert(!!v);
|
||||
console.assert(f == 0 || !!f);
|
||||
/* eslint-disable no-mixed-operators*/
|
||||
return new Vec2(this.x + v.x * f, this.y + v.y * f, this.z + v.z * f);
|
||||
/* eslint-enable no-mixed-operators*/
|
||||
};
|
||||
|
||||
Vec2.prototype.normalized = function () {
|
||||
return this.scaled(1 / this.length());
|
||||
};
|
||||
|
||||
Vec2.prototype.normalize = function () {
|
||||
var l = this.length();
|
||||
|
||||
if (l < 0.000001)
|
||||
return false;
|
||||
|
||||
this.x /= l;
|
||||
this.y /= l;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
Vec2.prototype.turnLeft = function () {
|
||||
return new Vec2(-this.y, this.x, this.z);
|
||||
};
|
||||
|
||||
Vec2.prototype.coordStr = function () {
|
||||
return this.x.toString() + ' , ' + this.y.toString();
|
||||
};
|
||||
|
||||
Vec2.prototype.toString = function () {
|
||||
return '(' + this.x.toFixed(2) + ',' + this.y.toFixed(2) + ')';
|
||||
};
|
||||
|
||||
Vec2.dist = function (a, b) {
|
||||
console.assert(!!a);
|
||||
console.assert(!!b);
|
||||
return Vec2.diff(a, b).length();
|
||||
};
|
||||
|
||||
Vec2.max = function (v1, v2) {
|
||||
console.assert(!!v1);
|
||||
console.assert(!!v2);
|
||||
return new Vec2(Math.max(v1.x, v2.x), Math.max(v1.y, v2.y), Math.max(v1.z, v2.z));
|
||||
};
|
||||
|
||||
Vec2.min = function (v1, v2) {
|
||||
console.assert(!!v1);
|
||||
console.assert(!!v2);
|
||||
return new Vec2(Math.min(v1.x, v2.x), Math.min(v1.y, v2.y), Math.min(v1.z, v2.z));
|
||||
};
|
||||
|
||||
Vec2.prototype.max = function (v) {
|
||||
console.assert(!!v);
|
||||
return new Vec2.max(this, v); // eslint-disable-line new-cap
|
||||
};
|
||||
|
||||
Vec2.prototype.min = function (v) {
|
||||
console.assert(!!v);
|
||||
return new Vec2.min(this, v); // eslint-disable-line new-cap
|
||||
};
|
||||
|
||||
Vec2.prototype.ceil = function () {
|
||||
return new Vec2(Math.ceil(this.x), Math.ceil(this.y), Math.ceil(this.z));
|
||||
};
|
||||
|
||||
Vec2.prototype.floor = function () {
|
||||
return new Vec2(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z));
|
||||
};
|
||||
|
||||
Vec2.sum = function (v1, v2) {
|
||||
console.assert(!!v1);
|
||||
console.assert(!!v2);
|
||||
return new Vec2(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
|
||||
};
|
||||
|
||||
Vec2.dot = function (v1, v2) {
|
||||
console.assert(!!v1);
|
||||
console.assert(!!v2);
|
||||
/* eslint-disable no-mixed-operators*/
|
||||
return v1.x * v2.x + v1.y * v2.y;
|
||||
/* eslint-enable no-mixed-operators*/
|
||||
};
|
||||
|
||||
Vec2.cross = function (v1, v2) {
|
||||
console.assert(!!v1);
|
||||
console.assert(!!v2);
|
||||
/* eslint-disable no-mixed-operators*/
|
||||
return v1.x * v2.y - v1.y * v2.x;
|
||||
/* eslint-enable no-mixed-operators*/
|
||||
};
|
||||
|
||||
Vec2.prototype.rotate = function (angle) {
|
||||
console.assert(angle == 0 || !!angle);
|
||||
var si = Math.sin(angle);
|
||||
var co = Math.cos(angle);
|
||||
|
||||
return this.rotateSC(si, co);
|
||||
};
|
||||
|
||||
Vec2.prototype.rotateSC = function (si, co) {
|
||||
console.assert(si == 0 || !!si);
|
||||
console.assert(co == 0 || !!co);
|
||||
/* eslint-disable no-mixed-operators*/
|
||||
return new Vec2(this.x * co - this.y * si, this.x * si + this.y * co, this.z);
|
||||
/* eslint-enable no-mixed-operators*/
|
||||
};
|
||||
|
||||
Vec2.angle = function (v1, v2) {
|
||||
console.assert(!!v1);
|
||||
console.assert(!!v2);
|
||||
return Math.atan2(Vec2.cross(v1, v2), Vec2.dot(v1, v2));
|
||||
};
|
||||
|
||||
Vec2.prototype.oxAngle = function () {
|
||||
return Math.atan2(this.y, this.x);
|
||||
};
|
||||
|
||||
Vec2.diff = function (v1, v2) {
|
||||
console.assert(!!v1);
|
||||
console.assert(!!v2);
|
||||
return new Vec2(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
|
||||
};
|
||||
|
||||
// assume arguments v1, f1, v2, f2, v3, f3, etc.
|
||||
// where v[i] are vectors and f[i] are corresponding coefficients
|
||||
Vec2.lc = function () {
|
||||
var v = new Vec2();
|
||||
for (var i = 0; i < arguments.length / 2; ++i)
|
||||
/* eslint-disable no-mixed-operators*/
|
||||
v = v.addScaled(arguments[2 * i], arguments[2 * i + 1]);
|
||||
/* eslint-enable no-mixed-operators*/
|
||||
return v;
|
||||
};
|
||||
|
||||
Vec2.lc2 = function (v1, f1, v2, f2) {
|
||||
console.assert(!!v1);
|
||||
console.assert(!!v2);
|
||||
console.assert(f1 == 0 || !!f1);
|
||||
console.assert(f2 == 0 || !!f2);
|
||||
/* eslint-disable no-mixed-operators*/
|
||||
return new Vec2(v1.x * f1 + v2.x * f2, v1.y * f1 + v2.y * f2, v1.z * f1 + v2.z * f2);
|
||||
/* eslint-enable no-mixed-operators*/
|
||||
};
|
||||
|
||||
Vec2.centre = function (v1, v2) {
|
||||
return new Vec2.lc2(v1, 0.5, v2, 0.5); // eslint-disable-line new-cap
|
||||
};
|
||||
|
||||
// find intersection of a ray and a box and
|
||||
// return the shift magnitude to avoid it
|
||||
Vec2.shiftRayBox = function (/* Vec2*/p, /* Vec2*/d, /* Box2Abs*/bb) { // eslint-disable-line max-statements
|
||||
console.assert(!!p);
|
||||
console.assert(!!d);
|
||||
console.assert(!!bb);
|
||||
// four corner points of the box
|
||||
var b = [bb.p0, new Vec2(bb.p1.x, bb.p0.y),
|
||||
bb.p1, new Vec2(bb.p0.x, bb.p1.y)];
|
||||
var r = b.map(function (v) {
|
||||
return v.sub(p);
|
||||
}); // b relative to p
|
||||
d = d.normalized();
|
||||
var rc = r.map(function (v) {
|
||||
return Vec2.cross(v, d);
|
||||
}); // cross prods
|
||||
var rd = r.map(function (v) {
|
||||
return Vec2.dot(v, d);
|
||||
}); // dot prods
|
||||
|
||||
// find foremost points on the right and on the left of the ray
|
||||
var pid = -1,
|
||||
nid = -1;
|
||||
for (var i = 0; i < 4; ++i) {
|
||||
if (rc[i] > 0) {
|
||||
if (pid < 0 || rd[pid] < rd[i]) pid = i;
|
||||
} else
|
||||
if (nid < 0 || rd[nid] < rd[i]) {
|
||||
nid = i;
|
||||
}
|
||||
}
|
||||
if (nid < 0 || pid < 0) // no intersection, no shift
|
||||
return 0;
|
||||
|
||||
// check the order
|
||||
var id0, id1;
|
||||
if (rd[pid] > rd[nid])
|
||||
id0 = nid, id1 = pid;
|
||||
else
|
||||
id0 = pid, id1 = nid;
|
||||
|
||||
// simple proportion to calculate the shift
|
||||
/* eslint-disable no-mixed-operators*/
|
||||
return rd[id0] + Math.abs(rc[id0]) * (rd[id1] - rd[id0]) /
|
||||
(Math.abs(rc[id0]) + Math.abs(rc[id1]));
|
||||
/* eslint-enable no-mixed-operators*/
|
||||
};
|
||||
|
||||
module.exports = Vec2;
|
||||
Reference in New Issue
Block a user