Source: components/SpaceRelationComponent.js

import { MathUtils } from '../math/MathUtils';
import { BaseComponent } from './BaseComponent';

const __ = {
	private: Symbol('private'),
}

const Relation = {
	Disjoint: 0,
	Intersects: 1,
	Contains: 2
};

/**
 * @class SpaceRelationComponent
 * The space relation component.
 * @memberof THING
 * @extends THING.BaseComponent
 * @public
 */
class SpaceRelationComponent extends BaseComponent {

	/**
	 * The space relation component of object.
	 */
	constructor() {
		super();

		this[__.private] = {};
	}

	/**
	 * contains relation
	 * @param {Object} obj THe relation object.
	 * @param {Boolean} cascade The cascade.
	 * @returns {Boolean}
	 * @example
	 * 	let result = this.contains(obj, false);
	 *  console.log(result);
	 * @public
	 */
	contains(obj, cascade = true) {
		return this._calcRelation(obj, cascade) === Relation.Contains;
	}

	/**
	 * intersects relation
	 * @param {Object} obj THe relation object.
	 * @param {Boolean} cascade The cascade.
	 * @returns {Boolean}
	 * @example
	 * 	let result = this.intersects(obj, false);
	 *  console.log(result);
	 * @public
	 */
	intersects(obj, cascade = true) {
		return this._calcRelation(obj, cascade) === Relation.Intersects;
	}

	/**
	 * disjoint relation
	 * @param {Object} obj THe relation object.
	 * @param {Boolean} cascade The cascade.
	 * @returns {Boolean}
	 * @example
	 * 	let result = this.disjoint(obj, false);
	 *  console.log(result);
	 * @public
	 */
	disjoint(obj, cascade = true) {
		return this._calcRelation(obj, cascade) === Relation.Disjoint;
	}

	_calcRelation(obj, cascade) {
		const cOBB = this.object.getOBB(false);
		const tOBB = obj.getOBB(cascade);

		const tPoints = tOBB.getPoints();
		let containsCount = 0;
		for (let i = 0; i < tPoints.length; i++) {
			const tPoint = tPoints[i];
			if (cOBB.containsPoint(tPoint)) {
				containsCount++;
			}
		}
		if (containsCount === tPoints.length) {
			return Relation.Contains;
		}

		if (cOBB.intersectsOBB(tOBB)) {
			return Relation.Intersects;
		}

		return Relation.Disjoint;
	}

}

export { SpaceRelationComponent }