Three. js source code annotation (14) Math/Sphere. js, three. jssphere. js

Source: Internet
Author: User

Three. js source code annotation (14) Math/Sphere. js, three. jssphere. js

Wuji (http://blog.csdn.net/omni360)

This article follows the "signature-non-commercial use-consistency" creation public agreement

Reprinted please keep this sentence: Wuji-this blog focuses on Agile development and mobile and IOT device research: data visualization, GOLANG, Html5, WEBGL, THREE. JS. Otherwise, the post from this blog will not be reprinted or reprinted. Thank you for your cooperation.


I also just started learning. Sorry for the errors in many places.

The following code is a comment on the Math/Sphere. JS file in the THREE. js source code file.

More updates in: https://github.com/omni360/three.js.sourcecode/blob/master/Three.js


// File: src/math/Sphere. js/*** @ author bhouston/http://exocortex.com * @ author mrdoob/http://mrdoob.com/* // Sphere object constructor. creates a sphere object in a 3D space. the function functions of the Sphere object are implemented using a function prototype object constructed by // definition. //// usage: var center = new Vector3 (, 0), radius = 5; var sphere = new Sphere (center, radius ); /// create a sphere with the center of 0 and the radius of 0 being 5. * /// <summary> Sphere </summary> // <param name = "center" type = "Vector3"> center coordinate value </param> // <param name = "rad Ius "type =" Number "> Number Sphere radius </param> THREE. Sphere = function (center, radius) {this. center = (center! = Undefined )? Center: new THREE. Vector3 (); // assign a value or initialize centerthis. radius = (radius! = Undefined )? Radius: 0; // assign values or initialize radius }; /*************************************** * ***** the following are the functions provided by the Sphere object. **************************************** /THREE. sphere. prototype = {constructor: THREE. sphere, // constructor, returns a reference to the Sphere function that creates this object. The/* // set method is used to set the starting point, ending point, center, and radius coordinate values of the Sphere. and returns the sphere with the new radius and coordinate value. * //// <summary> set </summary> /// <param name = "center" type = "Vector3"> center coordinate value </param> // <param name = "radius" type = "Number"> Number sphere radius </Param> // <returns type = "Sphere"> returns the Sphere with the new radius and coordinate value </returns> set: function (center, radius) {this. center. copy (center); this. radius = radius; return this; // return the new radius, sphere of the coordinate value }, /* // setFromPoints: obtains the radius of the sphere from the maximum distance to the center of the circle in the points array composed of Vector3 objects. Optional parameters optionalCenter are used to set the center of the sphere. and returns the sphere with the new radius and coordinate value. /// NOTE: If the optionalCenter parameter is set for the setFromPoints () method, the distance from the value to the center of the points array will change. * //// <summary> setFromPoints </summary> /// <param name = "Points" type = "Vector3Array"> points array composed of Vector3 </param> // <param name = "optionalCenter" type = "Vector3"> optional parameter, receive the returned result. The center of the Sphere </param> /// <returns type = "Sphere"> returns the new radius and coordinate Sphere </returns> setFromPoints: function () {var box = new THREE. box3 (); return function (points, optionalCenter) {var center = this. center; if (optionalCenter! = Undefined) {center. copy (optionalCenter);} else {box. setFromPoints (points ). center (center);} var maxRadiusSq = 0; for (var I = 0, il = points. length; I <il; I ++) {maxRadiusSq = Math. max (maxRadiusSq, center. distanceToSquared (points [I]); // calculate the maximum value to the center of the points array and assign it to maxRadiusSq} this. radius = Math. sqrt (maxRadiusSq); return this; // returns the new radius, the sphere of the coordinate value };}(), // * // copy method is used to copy the center and radius of the sphere, center, radius value. returns the new radius, sphere of the coordinate value * // <summary> copy </summary> /// <param name = "Sphere" type = "sphere"> Sphere </param> // /<returns type = "Sphere"> returns the new radius, coordinate value sphere </returns> copy: function (sphere) {this. center. copy (sphere. center); this. radius = sphere. radius; return this; // return the new radius, sphere of the coordinate value}, and the/* // empty method is used to determine whether the radius of the sphere is less than or equal to 0, used to determine whether the radius of a space is 0 or a sphere smaller than 0. * //// <summary> empty </summary> /// <returns type = "Boolean"> returns true or false </returns> empty: function () {return (this. radius <= 0); // return true or false},/* // The containsPoint method is used to obtain whether the parameter point (the three-dimensional coordinate of A Vector3) is in the current sphere. * //// <summary> containsPoint </summary> /// <param name = "point" type = "Vector3"> three-dimensional coordinate of A Vector3 </param>/ // <returns type = "Boolean"> return true or false </returns> containsPoint: function (point) {return (point. distanceToSquared (this. center) <= (this. radius * this. radius); // return true or false},/* // The distanceToPoint method is used to obtain the minimum length from a point in the three-dimensional space to the surface of the Sphere object. * //// <summary> distanceToPoint </summary> /// <param name = "point" type = "Vector3"> three-dimensional coordinate of Vector3 in a three-dimensional space </param> // <returns type = "Number"> returns the minimum length from a point in the 3D space to the surface of the Sphere object. </returns> distanceToPoint: function (point) {return (point. distanceTo (this. center)-this. radius); // returns the minimum length from a point in the three-dimensional space to the surface of the Sphere object .}, /* // The intersectsSphere method is used to obtain whether the current sphere matches the sphere object, returns true or false * /// <summary> intersectsSphere </summary> /// <param name = "sphere" type = "Sphere"> Sphere sphere Sphere </param >/// <returns type = "Boolean"> return true or false </returns> intersectsSphere: function (sphere) {var radiusSum = this. radius + sphere. radius; return sphere. center. distanceToSquared (this. center) <= (radiusSum * radiusSum); // returns true or false},/* // The clampPoint method is used to compress the sphere through the point parameter. if the point is outside the sphere, force the point to the sphere surface. If the point is inside the sphere, re-set the sphere radius to the distance from the point to the current sphere radius. * //// <summary> clampPoint </summary> /// <param name = "point" type = "Vector3"> three-dimensional coordinate of A Vector3 </param>/ // <param name = "optionalTarget" type = "Vector3"> optional parameter, receive the returned result and return the cropped boundary point </param> // <returns type = "Vector3"> return the cropped boundary point. </returns> clampPoint: function (point, optionalTarget) {var deltaLengthSq = this. center. distanceToSquared (point); var result = optionalTarget | new THREE. vector3 (); result. copy (point); if (deltaLengthSq> (this. radius * this. radius) {result. sub (this. center ). normalize (); result. multiplyScalar (this. radius ). add (this. center);} return result; // return the cropped boundpoint},/* // The getBoundingBox method returns the Box3 Box3 cube boundary of the current sphere (this should be an external cube of the sphere) /// corresponds to the getBoundingSphere () method in the Box3 class. * //// <summary> getBoundingBox </summary> /// <param name = "optionalTarget" type = "THREE. box3 () "> optional parameter, THREE. box3 () Cube object, used to receive returned values </param> /// <returns type = "THREE. sphere () "> return Box3 cube boundary of the current Sphere (a cube of the Sphere should be exceeded here) </returns> getBoundingBox: function (optionalTarget) {var box = optionalTarget | new THREE. box3 (); box. set (this. center, this. center); box. expandByScalar (this. radius); return box; // return the Box3 Box3 cube boundary of the current sphere (a cube of the sphere should be exceeded here)},/* // applyMatrix4 pass the matrix (rotation, to apply the transformation to the center and radius of the current Sphere object. * //// <summary> applyMatrix4 </summary> /// <param name = "matrix" type = "Matrix4"> (rotate, scale, move the transformation matrix </param> /// <returns type = "Boolean"> returns the transformed sphere. </returns> applyMatrix4: function (matrix) {this. center. applyMatrix4 (matrix); this. radius = this. radius * matrix. getMaxScaleOnAxis (); return this; // return the transformed sphere .}, /* // The translate method is used to move the position of the current sphere through the offset parameter. * //// <summary> translate </summary> // <param name = "offset" type = "Vector3"> offset </param> // <returns type = "Boolean"> return the new radius, sphere of coordinate value </returns> translate: function (offset) {this. center. add (offset); return this; // returns the new radius, sphere of the coordinate value}, and the/* // equals method is used to obtain the Sphere parameter (A sphere Sphere) whether it is completely equal to the current sphere, that is, the center and radius are equal. * //// <summary> equals </summary> // <param name = "sphere" type = "Sphere"> A Sphere </param> /// <returns type = "Boolean"> return true or false </returns> equals: function (sphere) {return sphere. center. equals (this. center) & (sphere. radius === this. radius); // return true or false},/* clone method // clone method to clone a sphere object. * //// <summary> clone </summary> /// <returns type = "Sphere"> returns the Sphere object </returns> clone: function () {return new THREE. sphere (). copy (this); // returns the sphere object }};


Wuji (http://blog.csdn.net/omni360)

This article follows the "signature-non-commercial use-consistency" creation public agreement

Reprinted please keep this sentence: Wuji-this blog focuses on Agile development and mobile and IOT device research: data visualization, GOLANG, Html5, WEBGL, THREE. JS. Otherwise, the post from this blog will not be reprinted or reprinted. Thank you for your cooperation.


The following code is a comment on the Math/Line3.js file in the THREE. JS source code file.

More updates in: https://github.com/omni360/three.js.sourcecode/blob/master/Three.js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.