Three.js Source Note (12) math/box3.js

Source: Internet
Author: User
Tags function prototype scalar

Commercial Territory (http://blog.csdn.net/omni360/)

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Reprint Please keep this sentence: Business Domain-this blog focuses on Agile development and mobile and IoT device research: Data visualization, Golang, HTML5, WEBGL, three. JS, Otherwise, from this blog article refused to reprint or reprint, thank you for your cooperation.

I was just beginning to learn, a lot of things are definitely wrong and please forgive me.


The following code is a comment for the math/box3.js file in the three.js source 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/westlangl The constructor of the EY *//*///box3 object. Used to create a cube boundary object within a three-dimensional space. The function function of the Box3 object is implemented using a function prototype object defined by///. Note: If there is no parameter min, Max initializes the cube boundary to infinity, Infinity//////usage: var min = new Vector3 (0,0,0), max = new Vector3 (1,1,1); var box = new Box3 (Min,max);///////Create a cube boundary object with two Vector3 (three-dimensional vector) Min,max .*////<summary>box3</summary>///< param name = "min" type= "Vector3" > The minimum coordinate value of the boundary </param>///<param name = "Max" type= "Vector3" > The maximum coordinate value of the boundary </ Param>three. Box3 = function (min, max) {this.min = (min!== undefined)? min:new three. Vector3 (Infinity, Infinity, Infinity);//infinity positive Infinity This.max = (max!== undefined)? Max:new three. Vector3 (-Infinity,-Infinity,-Infinity);//-Infinity negative Infinity};/******************************************** The following are the function functions provided by the Box3 object. ****************************************/three. Box3.prototype = {Constructor:three. box3,//constructor that returns a reference to the BOX3 function that created this object/*///set method is used to set the starting point of the cube boundary from the newBeam point, Min,max coordinate value. and returns the cube boundary of the new coordinate value. *////<summary>set</summary>///<param name = "min" type= "Vector3" > The minimum coordinate value of the boundary </param>///<param name = "Max" type= "Vector3" > The 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;//returns the new coordinates The cube boundary of the value},/*///setfrompoints method sets the minimum, maximum, Min,max coordinate values of the cube boundary by a points array of Vector3 objects. and returns the cube boundary of the new coordinate value .*////< Summary>setfrompoints</summary>///<param name = "Points" type= "Vector3array" >vector3 object consisting of points array </param>///<returns type= "Box3" > returns 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])//called. Expandbypoi The NT () method, gets the smallest, largest coordinate in the points array, and then extends the bounds.} Return this;//the cube boundary of the new coordinate value},/*///setfromcenterandsize method re-sets the minimum, maximum, Min,max coordinate values of the cube boundary by the center point, the bounding dimension mode. and returns the cube boundary of the new coordinate value. *////<summary>setfromcenterandsize</summary>///<pAram name = "Center" type= "Vector3" >vector3 object, center point coordinate </param>///<param name = "Size" type= "number" > Boundary dimension </param>///<returns type= "Box3" > returns 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). Multiplyscalar (0.5); This.min.copy (center). S UB (halfsize); this.max.copy (center). Add (halfsize); return this;//returns the cube boundary of the new coordinate value}; (), the/*///setfromobject method sets the minimum, maximum, Min,max coordinate value of the cube boundary by getting the endpoint of the Parameter object. and returns the cube boundary of the new coordinate value .*////<summary> Setfromobject</summary>///<param name = "Object" type= "Object3d" >object3d object </param>///< Returns type= "Box3" > returns 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// Transform the world coordinate system by getting the endpoint of the Object3d object (including the child object) to set the cube boundary var v1 = new three. Vector3 (); return function (object) {var scope = tHis;object.updatematrixworld (TRUE);//Set global transformations, and object and sub-objects apply transformations.//todo:updatematrixworld () method not 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 Expandbypoint () method to reset Square Boundary}}); return this;//returns the cube boundary of the new coordinate value}; (), the/*///copy method is used to copy the minimum, maximum, and min,max coordinate values of the cube boundary. and returns the cube boundary of the new coordinate value. *////<summary>copy</summary>///<param name = "box" type= "Box3" > Cube boundary </param>///<returns type= "Box3" > return the new coordinate value of the cube boundary </returns>copy: function (box) {this.min.copy (box.min); this.max.copy (Box.max); return this;//returns 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;//initializes the cube boundary to Infinity, infinity this.max.x = this.max.y = This.max.z =-Infinit y;//initializes the cube boundary to infinity, and the Infinity return this;//returns the cube boundary of the new coordinate value},/*///empty method is used to determine if x, y of the maximum value of the cube boundary is less than the minimum value of x,y.///Note: Returns true if this box includes 0 points at its bounds. Note: A box has the largest, smallest boundary, and the maximum minimum boundary is represented by a single point, which is shared by two boundaries.///The Todo:empty method is not understood. When to use. *////<summary>empty</ Summary>///<returns type= "Boolean" > Returns TRUE or False</returns>empty:function () {//This was a more robust CH Eck for empty than (volume <= 0) because volume can get positive with both negative axesreturn (this.max.x < this. min.x) | | (This.max.y < THIS.MIN.Y) | | (This.max.z < THIS.MIN.Z);//returns TRUE or False},/*///center method used to return the midpoint of the cube boundary *////<summary>center</summary >///<param name = "Optionaltarget" type= "Vector3" > Optional parameters, receive return result, midpoint of boundary </param>///<returns type= " Vector3 "> returns the midpoint </returns>center:function of the cube boundary (optionaltarget) {var result = Optionaltarget | | new three. Vector3 (); return resUlt.addvectors (This.min, This.max). Multiplyscalar (0.5);//Returns the midpoint},/*///size method of the cube boundary to return the vector of the cube boundary dimension *////<summary >size</summary>///<param name = "Optionaltarget" type= "Vector3" > Optional parameters, receive return results, vector of boundary dimensions </param>// /<returns type= "Vector3" > returns the vector of cube boundary dimensions </returns>size:function (optionaltarget) {var result = Optionaltarget | | New three. Vector3 (); return result.subvectors (This.max, this.min);//The vector},/*///expandbypoint method that returns the cube boundary dimension via the Vector3 object (point parameter) Extends the minimum, maximum, and min,max coordinate values of the cube boundary. and returns the cube boundary of the new coordinate value.///Note:expandbypoint method and Expandbyvector method both pass a Vector3 object, The Expandbypoint method compares the maximum value of the current boundary with the x, Y coordinates of the minimum value to obtain a new boundary, but the Expandbyvector method adds the maximum value of the cube boundary to the parameter vector, the minimum value minus the parameter vector,*////< Summary>expandbypoint</summary>///<param name = "Points" type= "Vector3" >vector3 object </param>/// <returns type= "Box3" > returns the cube boundary of the new coordinate value </returns>expandbypoint:function (point) {this.min.min (point); This.max.max (point); return this;//a cube boundary that returns a new coordinate value},/*///expandbyvector method extends the cube by Vector3 object (vector parameter)The minimum, maximum, Min,max coordinate value of the boundary. and returns the cube boundary of the new coordinate value.///Note:expandbyvector method differs from Expandbyscalar method in that Expandbyvector () receives a vector, The Expandbyscalar () method receives a scalar. *////<summary>expandbyvector</summary>///<param name = "scalar" type= " Number > Value Object </param>///<returns type= "Box3" > returns the cube boundary of the new coordinate value </returns>expandbyvector:function ( Vector) {this.min.sub (vector); this.max.add (vector); return this;//returns the cube boundary of the new coordinate value},/*///expandbyscalar method through the Vector3 object ( Scalar parameter) extends the minimum, maximum, and min,max coordinate values of the cube boundary. and returns the cube boundary of the new coordinate value.///Note:expandbyscalar method differs from Expandbyvector method expandbyvector () receives a vector, the Expandbyscalar () method receives a scalar. *////<summary>expandbyscalar</summary>///<param name = "Scalar" Type= "Number" > Value object </param>///<returns type= "Box3" > returns the cube boundary </returns>expandbyscalar for the new coordinate value: function (scalar) {this.min.addScalar (-scalar); this.max.addScalar (scalar); return this;},/*/// The Containspoint method is used to get the parameter point (a Vector3 's three-dimensional dot coordinate) within the current cube boundary. *////<summary>containspoint</summary>/// <param Name= "point" type= "Vector3" > A Vector3 of three-dimensional points coordinates </param>///<returns type= "Boolean" > returns 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;//not within the bounds, return False}return true;//within the bounds, return True},/*///containsbox method to get the parameter box ( A Box3 cube boundary) is within the current cube boundary. *////<summary>containsbox</summary>///<param name = "box" type= "Box3" > A Box3 cube boundary </param>///<returns type= "Boolean" > Returns 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;//within the bounds, return True}return false;//not within the bounds, return False},/*///getparameter method to get the parameter point (a Vector3 of three-dimensional dot coordinates) InThe length aspect ratio of the current cube boundary.///Example: var point = new Vector3 (3,2,3),///var min= new Vector3 (1,1,1), max = new Vector3 (5,5,5);///var box = New Box3 (Min,max); A 4x4 border///var ot = new Vector3 ();////Box.getparameter (Point,ot);///Ot= 3/4,1/2,3/4*////<summary>containsbox </summary>///<param name = "box" type= "Box3" > a Box3 cube border </param>///<param name = " Optionaltarget "type=" Vector3 "> Optional parameters, receive return results, three-dimensional vector with long aspect ratio </param>///<returns type=" Vector3 "> Returns a three-dimensional vector that contains the aspect ratio. </returns>getparameter:function (Point, Optionaltarget) {//This can potentially has a divide by ze Ro if the box//has a size dimension of 0.//Note: There may be a divisor of 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},/*///isintersectionbox method with a long aspect ratio to obtain the parameter box (a Box3 cube boundary) is intersected with the current cube boundary .*////<summary>isintersectionbox</summary>///<param name = "box" type= "Box3" > a Box3 cube boundary </param>///<returns type= "Boolean" > Returns True or false</returns>isintersectionbox:function (box) {//using 6 splitting planes to the 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;//if not intersected, return False}return true;//if intersect, return true.},/*/// The Clamppoint method is used to limit the parameter point to the cube boundary. If point is less than Min, return min, or return max if it is greater than Max, returns point*////<summary>clamppoint</ Summary>///<param name = "point" type= "Vector3" > a Vector3 three-dimensional dot coordinate </param>///<param name = " Optionaltarget "type=" Vector3 "> Optional parameters, receive returned results, return trimmed boundary points </param>///<returns type=" Vector3 "> Return trimmed boundary points. </returns>clamppoint:function (Point, Optionaltarget) {var result = Optionaltarget | | new three. Vector3 (); return result.copy (point). Clamp (this.min, This.max);//returns trimmed boundary points},/*///distaThe Ncetopoint method is used to obtain a point to the minimum boundary within the boundary, the length of the maximum boundary (the length of box's 12 edges) .*////<summary>distancetopoint</summary>///< param name = "point" type= "Vector3" > three-dimensional dot coordinates of Vector3 within a boundary </param>///<returns type= "number" > Returns a point to the minimum boundary within the bounds, the length of the maximum boundary (the length of box's 12 edges). </returns>distancetopoint:function () {var v1 = new three. Vector3 (); return function (point) {var clampedpoint = v1.copy (point). Clamp (this.min, This.max); return Clampedpoint.s UB (point) Length ();//returns the minimum boundary within the boundary, the length of the maximum boundary (the length of box's 12 sides).};} (), the/*///getboundingsphere method returns the spherical boundary of the current cube boundary (which should be tangent to a sphere within the cube boundary) *////<summary>getboundingsphere</summary >///<param name = "Optionaltarget" type= "three. Sphere () "> Optional parameters, three. The Sphere () Sphere object, which is used to receive the return value </param>///<returns type= "three. Sphere () "> returns the spherical boundary of the current cube boundary (this should be tangent to a sphere in the cube boundary) </returns>getboundingsphere:function () {var v1 = new three. Vector3 (); return function (optionaltarget) {var result = Optionaltarget | | new three. Sphere (); result.center = This.center ();//sets the center of the sphere boundary to the current cube center result. Radius = this.size (v1). Length () * 0.5;//Sets the radius of the sphere boundary return result;//returns the spherical boundary of the current cube boundary (which should be tangent to a sphere in the cube boundary)}; (), the/*///intersect method is used to find the intersection of the current cube boundary and the parameter box by shrinking the current cube boundary. *////<summary>intersect</summary>///<param name = "box" type= "Box3" > a 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;//returns the intersection of the current cube boundary and the parameter box The},/*///intersect method is used to enclose the parameter box in the current cube boundary by extending the current cube boundary. Is to take two boundary of the set *////<summary>intersect</summary>/// <param name = "box" type= "Box3" > a Box3 cube boundary </param>///<returns type= "Boolean" > returns the two-border set </ Returns>union:function (Box) {this.min.min (box.min); This.max.max (Box.max); return this;//returns the set of two boundaries},/*/// The ApplyMatrix4 method applies transformations to the 8 corners of the current cube object by passing the matrix (rotation, scaling, moving, and so on) to a transform matrix. *////<summary>applymatrix4</summary>/// <param name = "Matrix" type= "Matrix4" > (rotation, scaling, moving and other transformation matrices </param>///<returns type= "Boolean" > Returns the cube boundary after the transformation.</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 represents 8 corner points by 3 bits. points[0].set (this.min.x, THIS.MIN.Y, this.min.z). applyMatrix4 (matrix); 000points[1].set (this.min.x, THIS.MIN.Y, this.max.z). applyMatrix4 (matrix); 001points[2].set (this.min.x, this.max.y, this.min.z). applyMatrix4 (matrix); 010points[3].set (this.min.x, this.max.y, this.max.z). applyMatrix4 (matrix); 011points[4].set (this.max.x, THIS.MIN.Y, this.min.z). applyMatrix4 (matrix); 100points[5].set (this.max.x, THIS.MIN.Y, this.max.z). applyMatrix4 (matrix); 101points[6].set (this.max.x, this.max.y, this.min.z). applyMatrix4 (matrix); 110points[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;//returns the transformed cube boundary.}; (), the/*///translate method is used to move the position of the current cube boundary through the parameter offset. *////<summary>translate</summary>///<param name = " Offset "type=" Vector3 "> Offset </param>///<returns type=" Boolean "> returns the cube boundary of the new coordinate value </returns> Translate:function (offset) {this.min.add (offset); This.max.add (offset); return this;//returns the cube boundary of the new coordinate value},/*/// The Equals method is used to get the parameter box (a Box3 cube boundary) that is exactly equal to the current cube boundary. *////<summary>equals</summary>///<param name = "box" Type= "Box3" > a Box3 cube border </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///c The lone method clones a cube boundary object. *////<summary>clone</summary>///<returns type= "Box3" > Return cube Boundary Object </returns >clone:function () {return new three. Box3 (). copy (this);//Return cube boundary object}};


Commercial Territory (http://blog.csdn.net/omni360/)

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Reprint Please keep this sentence: Business Domain-this blog focuses on Agile development and mobile and IoT device research: Data visualization, Golang, HTML5, WEBGL, three. JS, Otherwise, from this blog article refused to reprint or reprint, thank you for your cooperation.


The following code is a comment for the math/box3.js file in the three.js source file.

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

Three.js Source Note (12) math/box3.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.