Source: objects/Sphere.js

import { Utils } from '../common/Utils'
import { BaseModelObject3D } from './BaseModelObject3D';

function _getParam(radius, param) {
	if (Utils.isObject(radius)) {
		return radius;
	}

	return param;
}

function _buildParam(radius, param) {
	param = _getParam(radius, param);

	param['body'] = param['body'] || {};

	if (!param['body']['localScale']) {
		let fixedRadius = Math.max(Number.EPSILON, Utils.parseFloat(radius, Utils.parseFloat(param['radius'], 0.5)));
		let scale = fixedRadius * 2;

		param['body']['localScale'] = [scale, scale, scale];
	}

	return param;
}

/**
 * @class Sphere
 * The sphere entity object.
 * @memberof THING
 * @extends THING.BaseModelObject3D
 * @public
 */
class Sphere extends BaseModelObject3D {

	static defaultTagArray = ['Geometry'];

	/**
	 * The sphere object in scene.
	 * @param {Number} radius The radius, default is 0.5.
	 * @param {Object} param The initial parameters.
	 */
	constructor(radius, param = {}) {
		super(_buildParam(radius, param));
	}

	// #region Accessor

	/**
	 * Get/Set the radius.
	 * @type {Number}
	 * @public
	 */
	get radius() {
		let size = this.body.scale.reduce((total, num) => {
			return total + num;
		}, 0);

		return (size / 3) * 0.5;
	}
	set radius(value) {
		this._processBodyWithoutPivot((body) => {
			body.scale = [value, value, value];
		});
	}

	// #endregion

	/**
	 * Check whether it's sphere object.
	 * @type {Boolean}
	 */
	get isSphere() {
		return true;
	}

}

export { Sphere }