Three. js source code annotation (14th) Math/Sphere. js

Source: Internet
Author: User

Three. js source code annotation (14th) Math/Sphere. 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. *////Sphere///Center coordinate value ///Number Sphere radius 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. *////Set///Center coordinate value ///Number sphere radius ///
 
  
Returns the sphere with the new radius and coordinate value.
 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. *////SetFromPoints///Points array composed of Vector3 objects ///Optional parameter. The center of the sphere that receives the returned result ///
 
  
Returns the sphere with the new radius and coordinate value.
 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, the sphere of the coordinate value *////Copy///Sphere ///
 
  
Returns the sphere with the new radius and coordinate value.
 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. *////Empty///
 
  
Returns true or false.
 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. *////ContainsPoint///Three-dimensional Coordinate of A Vector3 ///
 
  
Returns true or false.
 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. *////DistanceToPoint///Three-dimensional Coordinate of Vector3 in a 3D space ///
 
  
Returns the minimum length from a point in a 3D space to the surface of a Sphere object.
 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 returns true or false if the current sphere is intersecting with the sphere object *////IntersectsSphere///A Sphere ///
 
  
Returns true or false.
 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. *////ClampPoint///Three-dimensional Coordinate of A Vector3 ///Optional parameter: receives the returned result and returns the cropped boundary point ///
 
  
Returns the cropped boundary point.
 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. *////GetBoundingBox///Optional parameter, which is a THREE. Box3 () Cube object. It is used to receive the return value ///
 
  
Returns the Box3 cube boundary of the current sphere (a cube of the sphere should be cut out here)
 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. *////ApplyMatrix4///(Rotation, scaling, moving, and other transformation matrices ///
 
  
Returns the transformed sphere.
 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. *////Translate///Offset ///
 
  
Returns the sphere with the new radius and coordinate value.
 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. *////Equals///A Sphere ///
 
  
Returns true or false.
 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. *////Clone///
 
  
Returns a sphere object.
 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

Related Article

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.