Three. js source code annotation (12) Math/Box3.js, three. jsbox3.js

Source: Internet
Author: User

Three. js source code annotation (12) Math/Box3.js, three. jsbox3.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/Box3.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/Box3.js/*** @ author bhouston/http://exocortex.com * @ author WestLangley/http://github.com/WestLangley * // The constructor of the Box3 object. creates a cube boundary object in a 3D space. the function functions of the Box3 object are implemented by using the function prototype object constructed. /// NOTE: If the parameter min is not specified, max initializes the cube boundary to Infinity, Infinity // usage: var min = new Vector3 (, 0 ), max = new Vector3 (, 1); var box = new Box3 (min, max); // create a cube boundary object by using two Vector3 (3D vector) min. * //// <summary> Box3 </summary> /// <Param name = "min" type = "Vector3"> Minimum coordinate value of a boundary </param> // <param name = "max" type = "Vector3"> maximum coordinate value </param> THREE. box3 = function (min, max) {this. min = (min! = Undefined )? Min: new THREE. Vector3 (Infinity, Infinity, Infinity); // Infinity is positive Infinity this. max = (max! = Undefined )? Max: new THREE. vector3 (-Infinity,-Infinity,-Infinity); //-Infinity, negative Infinity }; /*************************************** * ***** the following are the functions provided by the Box3 object. **************************************** /THREE. box3.prototype = {constructor: THREE. box3, // constructor, returns a reference to the Box3 function that creates this object/* // set method used to set the starting point, ending point, min, max coordinate value. return the cube boundary of the new coordinate value. * //// <summary> set </summary> /// <param name = "min" type = "Vector3"> Minimum coordinate value of the boundary </param> // /<param Name = "max" type = "Vector3"> maximum coordinate value of the boundary </param> // <returns type = "Box3"> returns the cube boundary of the new coordinate value </returns> set: function (min, max) {this. min. copy (min); this. max. copy (max); return this; // return the cube boundary of the new coordinate value},/* // setFromPoints method reset the minimum value of the cube boundary through the points array composed of Vector3 objects, maximum value, min, and max coordinate value. return the cube boundary of the new coordinate value. * //// <summary> setFromPoints </summary> // A points array composed of <param name = "points" type = "Vector3Array"> Vector3 objects </param>/ // <returns type = "Box3"> return the cube boundary of the new coordinate value </returns> setFromPoints: function (points) {this. makeEmpty (); for (var I = 0, il = points. length; I <il; I ++) {this. expandByPoint (points [I]) // call. expandByPoint () method, obtain the minimum and maximum coordinates in the points array, and then extend the boundary .} return this; // return the cube boundary of the new coordinate value},/* // setFromCenterAndSize method reset the minimum value, maximum value, min, of the cube boundary by means of the center point and boundary size, max coordinate value. return the cube boundary of the new coordinate value. * //// <summary> setFromCenterAndSize </summary> /// <param name = "center" Type = "Vector3"> Vector3 object, center Coordinate </param> /// <param name = "size" type = "Number"> boundary size </param> /// <returns type = "Box3"> return the cube boundary of the new coordinate value </returns> setFromCenterAndSize: function () {var v1 = new THREE. vector3 (); return function (center, size) {var halfSize = v1.copy (size ). multi plyscalar (0.5); this. min. copy (center ). sub (halfSize); this. max. copy (center ). add (halfSize); return this; // return the cube boundary of the new coordinate value };}(),/*// The/setFromObject method obtains the object endpoint and resets the minimum, maximum, min, and max coordinate values of the cube boundary. return the cube boundary of the new coordinate value. * //// <summary> setFromObject </summary> // <param name = "object" type = "Object3D"> Object3D object </param> /// <returns type = "Box3"> return the cube boundary of the new coordinate value </returns> setFromObject: function () {// Computes the world-axis-aligned bounding box of an object (including its children), // accounting for both the object's, and childrens ', world transforms // Change the world coordinate system, and set the cube boundary var v1 = new THREE by obtaining the endpoint of the Object3D object (including sub-objects. vector3 (); return function (object) {var scope = this; object. updateMatrixWorld (true); // sets the global transformation. All objects and sub-objects are transformed. // TODO: The updateMatrixWorld () method is not detailed yet. this. makeEmpty (); // call the Box3.makeEmpty () method to set the cube boundary to infinity. object. traverse (function (node) {if (node. geometry! = Undefined & node. geometry. vertices! = Undefined) {var vertices = node. geometry. vertices; for (var I = 0, il = vertices. length; I <il; I ++) {v1.copy (vertices [I]); v1.applyMatrix4 (node. matrixWorld); scope. expandByPoint (v1); // call the expandByPoint () method to reset the cube boundary }}); return this; // return the cube boundary of the new coordinate value };}(), // copy the minimum, maximum, min, and max coordinate values of the cube boundary. return the cube boundary of the new coordinate value. * //// <summary> copy </summary> /// <param name = "box" type = "Box3"> cube boundary </param> /// <ret Urns type = "Box3"> returns the cube boundary of the new coordinate value </returns> copy: function (box) {this. min. copy (box. min); this. max. copy (box. max); return this; // return the cube boundary of the new coordinate value},/* // The makeEmpty method is used to initialize the cube boundary to Infinity, infinity * // <summary> makeEmpty </summary> // <returns type = "Box3"> returns the cube boundary of the new coordinate value </returns> makeEmpty: function () {this. min. x = this. min. y = this. min. z = Infinity; // initialize the cube boundary to Infinity, Infinity this. max. x = this. max. y = this. max. z =-Inf Inity; // initialize the cube boundary to Infinity, Infinity return this; // return the cube boundary of the new coordinate value }, /* // The empty method is used to determine whether x and y of the maximum value of the cube boundary are smaller than x and y of the minimum value. /// NOTE: If this box contains zero points, return true at its boundary. /// NOTE: A box has the maximum and minimum boundary. the maximum and minimum boundary are represented by one vertex, which is shared by two boundary. /// TODO: The empty method is not clear. when to use. * //// <summary> empty </summary> /// <returns type = "Boolean"> returns true or false </returns> empty: function () {// this is a more robust check for empty than (volume <= 0) because volume can get positive with two negative axesreturn (this. max. x <this. min. x) | (this. max. y <this. min. y) | (this. max. z <this. min. z); // return true or false }, /* // The center method is used to return the midpoint of the cube boundary * // <summary> center </summary> /// <param name = "optionalTarget" type = "Vector3 "> optional parameters, receive the returned result. The midpoint of the boundary </param> /// <returns type = "Vector3"> returns the midpoint of the cube boundary </returns> center: function (optionalTarget) {var result = optionalTarget | new THREE. vector3 (); return result. addVectors (this. min, this. max ). multiplyScalar (0.5); // returns the midpoint of the cube boundary }, /* // The size method is used to return the vector of the cube boundary size * // <summary> size </summary> /// <param name = "optionalTarget" type =" vector3 "> optional parameters, receive the returned result. The vector of the boundary size </param> // <returns type = "Vector3"> returns the vector of the cube boundary size </returns> size: function (optionalTarget) {var result = optionalTarget | new THREE. vector3 (); return result. subVectors (this. max, this. min); // returns the cube boundary size vector},/* // expandByPoint method extends the minimum value, maximum value, min, max coordinate value. return the cube boundary of the new coordinate value. /// NOTE: Both the expandByPoint method and the expandByVector method pass a Vector3 object. The expandByPoint method compares the maximum and minimum values of the x and y coordinates of the current boundary to obtain the new boundary, however, the expandByVector method adds the maximum value of the cube boundary to the parameter vector, and the minimum value minus the parameter vector, * //// <summary> expandByPoint </summary> /// <param name = "points" type = "Vector3"> Vector3 object </param> /// <returns type = "Box3"> return the cube boundary of the new coordinate value </returns> expandByPoint: function (point) {this. min. min (point); this. max. max (point); return this; // return the cube boundary of the new coordinate value}, // * // The expandByVector method extends the minimum and maximum values of the cube boundary through the Vector3 object (vector parameter, min, max coordinate value. return the cube boundary of the new coordinate value. /// NOTE: The expandByVector method is different from the expandByScalar method. expandByVector () receives a vector, and expandByScalar () method receives a scalar. * //// <summary> expandByVector </summary> /// <param name = "scalar" type = "Number"> value object </param> /// <returns type = "Box3"> return the cube boundary of the new coordinate value </returns> expandByVector: function (vector) {this. min. sub (vector); this. max. add (vector); return this; // return the cube boundary of the new coordinate value},/* // expandByScalar method extends the minimum and maximum values of the cube boundary through the Vector3 object (Scalar parameter, min, max coordinate value. return the cube boundary of the new coordinate value. /// NOTE: The expandByScalar method is different from the expandByVector method. expandByVector () receives a vector, and expandByScalar () method receives a scalar. * //// <summary> expandByScalar </summary> /// <param name = "scalar" type = "Number"> value object </param> /// <returns type = "Box3"> return the cube boundary of the new coordinate value </returns> expandByScalar: function (scalar) {this. min. addScalar (-scalar); this. max. addScalar (scalar); return this ;},/* // The containsPoint method is used to obtain whether the parameter point (the three-dimensional coordinate of A Vector3) is within the boundary of the current cube. * //// <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) {if (point. x <this. min. x | point. x> this. max. x | point. y <this. min. y | point. y> this. max. y | point. z <this. min. z | point. z> this. max. z) {return false; // returns false if it is not within the boundary} return true; // returns true if it is within the boundary }, /* // The containsBox method is used to obtain whether the parameter box (Box3 cube boundary) is within the current cube boundary. * //// <summary> containsBox </summary> /// <param name = "box" type = "Box3"> Box3 cube boundary </param> // /<returns type = "Boolean"> return true or false </returns> containsBox: function (box) {if (this. min. x <= box. min. x) & (box. max. x <= this. max. x) & (this. min. y <= box. min. y) & (box. max. y <= this. max. y) & (this. min. z <= box. min. z) & (box. max. z <= this. max. z) {return true; // return true} return false within the boundary; // return false if not within the boundary }, // * // The getParameter method is used to obtain the aspect ratio of the parameter point (the three-dimensional coordinate of A Vector3) at the boundary of the current cube. /// example: var point = new Vector3 (, 3); // var min = new Vector3 (, 1), max = new Vector3 (, 5 ); /// var box = new Box3 (min, max); // a 4x4 boundary // var ot = new Vector3 (); // box. getParameter (point, ot); /// ot = 3/4, 1/2, 3/4 * /// <summary> containsBox </summary> /// <param name = "box" type = "Box3"> Box3 cube boundary </param>/ // <param name = "optionalTarget" type = "Vector3"> optional parameter, returns a three-dimensional vector containing the aspect ratio. </param> // <returns type = "Vector3"> returns a three-dimensional vector containing the aspect ratio. </returns> getParameter: function (point, optionalTarget) {// This can potentially have a divide by zero if the box // has a size dimension of 0. // NOTE: the divisor 0.var result = optionalTarget | new THREE. vector3 (); return result. set (point. x-this. min. x)/(this. max. x-this. min. x), (point. y-this. min. y)/(this. max. y-this. min. y), (point. z-this. min. z)/(this. max. z-this. min. z); // returns a three-dimensional vector that contains the aspect ratio.}, // * // The isIntersectionBox method is used to obtain whether the parameter box (A Box3 cube boundary) matches the current cube boundary. * //// <summary> isIntersectionBox </summary> /// <param name = "box" type = "Box3"> Box3 cube boundary </param> // /<returns type = "Boolean"> returns true or false </returns> isIntersectionBox: function (box) {// using 6 splitting planes to rule out intersections. if (box. max. x <this. min. x | box. min. x> this. max. x | box. max. y <this. min. y | box. min. y> this. max. y | box. max. z <this. min. z | box. min. z> this. max. z) {return false; // return false} return true if the intersection exists; // return true if the intersection exists .}, /* // The clampPoint method is used to restrict the parameter point within the cube boundary. if the point is less than min, min is returned. If the point is greater than max, max is returned, otherwise, the system returns the three-dimensional coordinate of point * /// <summary> clampPoint </summary> /// <param name = "point" type = "Vector3"> 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 result = optionalTarget | new THREE. vector3 (); return result. copy (point ). clamp (this. min, this. max); // return the cropped boundary point},/* // The distanceToPoint method is used to obtain the point to the minimum boundary within the boundary, the maximum boundary length (the length of 12 sides of the box ). * //// <summary> distanceToPoint </summary> // <param name = "point" type = "Vector3"> three-dimensional coordinate of Vector3 within a boundary </ param> /// <returns type = "Number"> returns a point to the minimum boundary within the boundary, the maximum boundary length (the length of 12 sides of the box ). </returns> distanceToPoint: function () {var v1 = new THREE. vector3 (); return function (point) {var clampedPoint = v1.copy (point ). clamp (this. min, this. max); return clampedPoint. sub (point ). length (); // return the length from one point to the smallest boundary within the boundary ).};} (), // * // The getBoundingSphere method returns the spherical boundary of the current cube boundary (a sphere that must be cut inside the cube boundary) * //// <summary> getBoundingSphere </summary> /// <param name = "optionalTarget" type = "THREE. sphere () "> optional parameter, THREE. sphere () Sphere object, used to receive returned values </param> /// <returns type = "THREE. sphere () "> returns the Sphere boundary of the current cube boundary (a Sphere that is cut inside the boundingsphere) </returns> getBoundingSphere: function () {var v1 = new THREE. vector3 (); return function (optionalTarget) {var result = optionalTarget | new THREE. sphere (); result. center = this. center (); // set the sphere boundary center to the current cube center result. radius = this. size (v1 ). length () * 0.5; // set the return result radius of the sphere boundary; // return the spherical boundary of the current cube boundary (a sphere that is cut inside the cube boundary here )};} (), // * // intersect is used to calculate the intersection between the current cube boundary and the parameter box by shrinking the boundary of the current cube. * //// <summary> intersect </summary> /// <param name = "box" type = "Box3"> Box3 cube boundary </param> // /<returns type = "Boolean"> returns the intersection of the current cube boundary and the parameter box </returns> intersect: function (box) {this. min. max (box. min); this. max. min (box. max); return this; // return the intersection of the current cube boundary and the parameter box},/* // The intersect method is used to expand the boundary of the current cube, enclose the parameter box in the boundary of the current cube. is to take the union of two boundaries * /// <summary> intersect </summary> /// <param name = "box" type = "Box3"> Box3 cube Boundary </param> /// <returns type = "Boolean"> returns the union of two boundaries </returns> union: function (box) {this. min. min (box. min); this. max. max (box. max); return this; // returns the union of the two boundary},/* // The applyMatrix4 method passes the matrix (rotation, scaling, moving, and other transformation matrices) apply the transformation to the eight vertices of the current cube object. * //// <summary> applyMatrix4 </summary> /// <param name = "matrix" type = "Matrix4"> (rotate, scale, move the transformation matrix </param> /// <returns type = "Boolean"> returns the transformed cube boundary. </returns> applyMatrix4: function () {var points = [new THREE. vector3 (), new THREE. vector3 (), new THREE. vector3 (), new THREE. vector3 (), new THREE. vector3 (), new THREE. vector3 (), new THREE. vector3 (), new THREE. vector3 ()]; return function (matrix) {// NOTE: I am using a binary pattern to specify all 2 ^ 3 combinations below // NOTE: the author uses three binary bits to represent eight vertices. points [0]. set (this. min. x, this. min. y, this. min. z ). applyMatrix4 (matrix); // 000 points [1]. set (this. min. x, this. min. y, this. max. z ). applyMatrix4 (matrix); // 001 points [2]. set (this. min. x, this. max. y, this. min. z ). applyMatrix4 (matrix); // 010 points [3]. set (this. min. x, this. max. y, this. max. z ). applyMatrix4 (matrix); // 011 points [4]. set (this. max. x, this. min. y, this. min. z ). applyMatrix4 (matrix); // 100 points [5]. set (this. max. x, this. min. y, this. max. z ). applyMatrix4 (matrix); // 101 points [6]. set (this. max. x, this. max. y, this. min. z ). applyMatrix4 (matrix); // 110 points [7]. set (this. max. x, this. max. y, this. max. z ). applyMatrix4 (matrix); // 111this. makeEmpty (); this. setFromPoints (points); // call the setFromPoints () method to reset the cube boundary. return this; // return the transformed cube boundary .};} (), // * // The translate method is used to move the position of the current cube boundary through the offset parameter. * //// <summary> translate </summary> // <param name = "offset" type = "Vector3"> offset </param> // <returns type = "Boolean"> return the cube boundary of the new coordinate value </returns> translate: function (offset) {this. min. add (offset); this. max. add (offset); return this; // return the cube boundary of the new coordinate value}, and the/* // equals method is used to obtain the box parameter (A Box3 cube boundary) whether it is completely equal to the boundary of the current cube. * //// <summary> equals </summary> /// <param name = "box" type = "Box3"> Box3 cube boundary </param> // /<returns type = "Boolean"> returns true or false </returns> equals: function (box) {return box. min. equals (this. min) & box. max. equals (this. max); // return true or false},/* clone method // clone method to clone a cube boundary object. * //// <summary> clone </summary> /// <returns type = "Box3"> returns the cube boundary object </returns> clone: function () {return new THREE. box3 (). copy (this); // return the cube boundary 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/Box3.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.