Three. js source code annotation (15th) Math/Plane. js

Source: Internet
Author: User

Three. js source code annotation (15th) Math/Plane. js

 

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/Plane. JS file in the THREE. js source code file.

 

// File: src/math/Plane. js/*** @ author bhouston/http://exocortex.com * // The constructor of the Plane object. creates a two-dimensional plane object with a normal vector and a constant distance from the origin to the plane. the functional functions of the Plane object are implemented using a function prototype object constructed. //// usage: var normal = new Vector3 (5.5, 0), constant =; var Plane = new Plane (normal, constant ); /// create a two-dimensional plane with a normal vector of 0 to 0 and a distance from the 0 origin to the plane of 5.5. *////Plane///Plane Normal Vector ///Number the distance between the two-dimensional Plane and the origin. THREE. Plane = function (normal, constant) {this. normal = (normal! = Undefined )? Normal: new THREE. Vector3 (1, 0, 0); // assign a value or initialize normalthis. constant = (constant! = Undefined )? Constant: 0; // assign values or initialize constant }; /*************************************** * ***** the following are the functions provided by the Plane object. **************************************** /THREE. plane. prototype = {constructor: THREE. plane, // constructor, returns a reference to the Plane function that creates this object/* // set method to reset the normal vector of the Two-dimensional Plane, the constant distance from the origin point to the plane, and a new two-dimensional plane is returned. *////Set///Plane Normal Vector ///Number the distance from the two-dimensional plane to the origin ///
 
  
Returns a new two-dimensional plane.
 Set: function (normal, constant) {this. normal. copy (normal); this. constant = constant; return this; // returns a new two-dimensional plane},/* // setComponents is used through the x, y, z, w component reset the normal vector of the Two-dimensional plane, the distance from the origin point to the plane constant, and return the new two-dimensional plane. *////SetComponents///Plane Normal Vector ///Plane Normal Vector x coordinate ///Y coordinate of the Plane Normal Vector ///Zcoordinate of the Plane Normal Vector ///Number the distance from the two-dimensional plane to the origin w ///
 
  
Returns a new two-dimensional plane.
 SetComponents: function (x, y, z, w) {this. normal. set (x, y, z); this. constant = w; return this; // return a new two-dimensional plane}, // * // setFromNormalAndCoplanarPoint is used by using the normal (Plane normal Vector) and the point (collocated point) parameters) reset the normal vector of the Two-dimensional plane, the distance from the origin to the plane constant, and return the new two-dimensional plane. *////SetFromNormalAndCoplanarPoint///Plane Normal Vector ///Points in total ///
 
  
Returns a new two-dimensional plane.
 SetFromNormalAndCoplanarPoint: function (normal, point) {this. normal. copy (normal); // the following point. the dot () method only receives this. normal, do not receive normal, this. normal is normalized, which is the unit vector this. constant =-point. dot (this. normal); // must be this. normal, not normal, as this. normal is normalizedreturn this; // and return a new two-dimensional plane},/* // setFromCoplanarPoints is used through the collocated vertices a, B, c. Reset the normal vector of the Two-dimensional plane, and return the new two-dimensional plane from the origin point to the plane. /// NOTE: The setFromCoplanarPoints method accepts three vertices a, B, and c, which need to be passed in the counterclockwise order to determine the direction of discovery. *////SetFromCoplanarPoints///A in total ///Point B in total ///Common points c ///
 
  
Returns a new two-dimensional plane.
 SetFromCoplanarPoints: function () {var v1 = new THREE. vector3 (); var v2 = new THREE. vector3 (); return function (a, B, c) {var normal = v1.subVectors (c, B ). cross (v2.subVectors (a, B )). normalize (); // first obtain the difference between vectors c and B, and obtain the cross product of the difference between vectors a and B through the cross method (the cross product is perpendicular to the vector, b), and then call the normalize () method to obtain the unit vector. // Q: shocould an error be thrown if normal is zero (e.g. degenerate plane )? // NOTE: if the normal vector is 0, an invalid flat object is generated. this. setFromNormalAndCoplanarPoint (normal, a); // The setFromNormalAndCoplanarPoint method is used to reset the normal vector of the Two-dimensional plane through the normal (Plane normal Vector) parameter and the point (common point) parameter, the constant distance from the origin point to the plane, and a new two-dimensional plane is returned. return this; // return a new two-dimensional plane};} (), // * // copy is used to copy the normal vector of the Two-dimensional plane, and the constant value from the origin to the plane. returns a new two-dimensional plane. // TODO: What is the difference between the copy method and the clone method? *////Copy///Two-dimensional plane ///
 
  
Returns a new two-dimensional plane.
 Copy: function (plane) {this. normal. copy (plane. normal); this. constant = plane. constant; return this; // returns a new two-dimensional plane}. The/* // normalize method is used to normalize the normal vector and adjust the value of the constant (obtain the unit plane ). *////Normalize///
 
  
Returns the normalized two-dimensional plane (Unit plane)
 Normalize: function () {// Note: will lead to a divide by zero if the plane is invalid. // NOTE: if the plane is invalid, the divisor is 0. var inverseNormalLength = 1.0/this. normal. length (); this. normal. multiplyScalar (inverseNormalLength); this. constant * = inverseNormalLength; return this; // returns the normalized two-dimensional plane (to obtain the unit plane)}. // The negate method is used to flip the normal to obtain the back of the current plane, *////Negate///
 
  
Returns the back of the current plane.
 Negate: function () {this. constant * =-1; this. normal. negate (); // flip the normal. If the coordinate value of the current three-dimensional vector (x, y, z) is negative, a positive number is returned. if the coordinate value of the current 3D vector (x, y, z) is positive, a negative number is returned. return this; // return the back of the Current Plane},/* // The distanceToPoint method is used to obtain the minimum length from a point in a 3D space to the surface of a Plane 2D Plane object. *////DistanceToPoint///Three-dimensional Coordinate of Vector3 in a 3D space ///
 
  
Returns the minimum length of a point in 3D space to the surface of a Plane 2D Plane object.
 DistanceToPoint: function (point) {return this. normal. dot (point) + this. constant; // returns the minimum length of a point in a 3D space to the surface of a Plane 2D Plane object }, /* // The distanceToPoint method is used to obtain the minimum length of a Plane 2D Plane object to a sphere surface in a three-dimensional space. ()*////DistanceToPoint///Sphere object in a 3D space ///
 
  
Returns the minimum length of a Plane 2D Plane object to a sphere surface in a 3D space.
 DistanceToSphere: function (sphere) {return this. distanceToPoint (sphere. center)-sphere. radius; // returns the minimum length of a Plane 2D Plane object in a 3D space to a sphere surface in a 3D space }, // The projectPoint method returns the projection from the first point in the 3D space to the current plane. point-to-Surface projection is equal to the vertical foot from the point parameter to the plane, so draw a line from the vertical foot to the point perpendicular to the plane. *////ProjectPoint///Vector3 3D vector ///Optional parameter, receiving returned results ///
 
  
Return Point-to-plane projection
 ProjectPoint: function (point, optionalTarget) {return this. orthoPoint (point, optionalTarget ). sub (point ). negate (); // call the orthoPoint () method, subtract the point, and return the reverse result }, /* // The orthoPoint method returns a vector (vertical) with the same direction as the normal vector of the current two-dimensional plane object and equal distance from the point to the plane ). if the optionalTarget parameter is set, the result is saved in optionalTarget. *////OrthoPoint///Vector3 3D vector ///Optional parameter, receiving returned results ///
 
  
Returns a vector (vertical) with the same direction as the normal vector of the current two-dimensional plane object and equal distance from the point to the plane ).
 OrthoPoint: function (point, optionalTarget) {var perpendicularmagntasks = this. distanceToPoint (point); // obtain the distance from the plane to the parameter point, and assign the value to prependicularMagnitudevar result = optionalTarget | new THREE. vector3 (); // The Life variable resault, used to store the return result. copy (this. normal ). multiplyScalar (perpendicularmagnular); // call the multiplyScalar (perpendicularMagnitude) method to multiply the component x, y, and z of the normal vector in the current two-dimensional plane by the distance from the plane to the parameter point. finally, the calculation result is returned .}, /* // The isIntersectionLine method is used to obtain whether the current two-dimensional plane is intersecting with the line parameter. true or false is returned *////IsIntersectionLine///Line3 in 3D space ///
 
  
Returns true or false.
 IsIntersectionLine: function (line) {// Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it. // NOTE: isIntersectionLine () is used to test whether the line and surface are intersecting. It is not mistakenly assumed that the line and surface are co-located. var startSign = this. distanceToPoint (line. start); var endSign = this. distanceToPoint (line. end); return (startSign <0 & endSign> 0) | (endSign <0 & startSign> 0); // return true or false }, // The intersectLine method is used to obtain the intersection of the current two-dimensional plane and the line parameter. If the two-dimensional plane does not intersection the Line, undefined is returned. If the line and the current two-dimensional plane common plane return the starting point of the Line. *////IsIntersectionLine///Line3 in 3D space ///Optional parameter, receiving returned results ///
 
  
Returns the intersection of the current two-dimensional plane and the line parameter. If the two-dimensional plane does not interwork with the Line parameter or is unknown, undefined is returned. If the line and the current two-dimensional plane common plane return the starting point of the Line.
 IntersectLine: function () {var v1 = new THREE. vector3 (); return function (line, optionalTarget) {var result = optionalTarget | new THREE. vector3 (); var direction = line. delta (v1); var denominator = this. normal. dot (direction); if (denominator = 0) {// line is coplanar, return origin // if the line and the current two-dimensional plane common surface return line start point if (this. distanceToPoint (line. start) = 0) {return result. copy (line. start);} // Unsure if this is the correct method to handle this case. // undefinedreturn undefined;} var t =-(line. start. dot (this. normal) + this. constant)/denominator; if (t <0 | t> 1) {return undefined; // return undefined} return result if it does not intersection with the Line parameter. copy (direction ). multiplyScalar (t ). add (line. start); // returns the intersection of the current two-dimensional plane and the line parameter };}(), /* // The coplanarPoint method returns the normal vector of the current two-dimensional plane to the current two-dimensional plane projection (the vertices of the vertical plane and the current plane ). /// TODO: I haven't figured it out here. I have time to figure it out. I forgot about the high school ry. however, we know that this is called in the following application transformation. *////CoplanarPoint///Optional parameter, receiving returned results ///
 
  
Returns the vertices in total.
 CoplanarPoint: function (optionalTarget) {var result = optionalTarget | new THREE. vector3 (); return result. copy (this. normal ). multiplyScalar (-this. constant); // returns a total of vertices},/* // The applyMatrix4 method passes the matrix (rotation, scaling, moving, and other transformation matrices) apply the transformation to the normal and normal vectors of the current Plane two-dimensional Plane object. *////ApplyMatrix4///(Rotation, scaling, moving, and other transformation matrices ///Optional parameter. If this parameter is set, the normal will be applied (rotation, scaling, moving, and other transformation matrices ///
 
  
Returns the transformed two-dimensional plane.
 ApplyMatrix4: function () {var v1 = new THREE. vector3 (); var v2 = new THREE. vector3 (); var m1 = new THREE. matrix3 (); return function (matrix, optionalNormalMatrix) {// compute new normal based on theory here: // http://www.songho.ca/opengl/gl_normaltransform.htmlvar normalMatrix = optionalNormalMatrix | m1.getNormalMatrix (matrix ); var newNormal = v1.copy (this. normal ). applyMatrix3 (normalMatrix); var newCoplanarPoint = this. coplanarPoint (v2); // obtain the newCoplanarPoint in total. applyMatrix4 (matrix); this. setFromNormalAndCoplanarPoint (newNormal, newCoplanarPoint); // The setFromNormalAndCoplanarPoint method is used to reset the normal vector of two-dimensional planes through the normal (Plane normal Vector) and the point (common point) parameters, the constant distance from the origin point to the plane, and a new two-dimensional plane is returned. return this; // return the transformed two-dimensional plane};} (), // * // The translate method is used to move the position of the current two-dimensional plane through the offset parameter. *////Translate///Offset ///
 
  
Returns a new two-dimensional plane.
 Translate: function (offset) {this. constant = this. constant-offset. dot (this. normal); return this; // returns a new two-dimensional Plane}. The/* // equals method is used to obtain the parameter Plane (a two-dimensional Plane of Plane) whether it is completely equal to the current two-dimensional plane, that is, the normal and radius of the normal vector are equal. *////Equals///Two-dimensional Plane of a Plane ///
 
  
Returns true or false.
 Equals: function (plane) {return plane. normal. equals (this. normal) & (plane. constant = this. constant); // returns true or false},/* clone method // clone method to clone a two-dimensional plane object. *////Clone///
 
  
Returns a two-dimensional plane object.
 Clone: function () {return new THREE. Plane (). copy (this); // returns a two-dimensional Plane 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/Plane. 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.