Three. js source code annotation (19th) Math/Spline. js

Source: Internet
Author: User

Three. js source code annotation (19th) Math/Spline. 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/Spline. JS file in the THREE. js source code file.

 


/*** Spline from Tween. js, slightly optimized (and trashed) * constructor for http://sole.github.com/tween.js/examples/05_spline.html ** @ author mrdoob/http://mrdoob.com/* @ author alteredq/http://alteredqualia.com/* // Spline object. this interface is used to create a spline object through the parameter points (Vector3 array) in 3D space .. ///// definition: A Spline is a series of smooth Curves passing through a given point. Initially, the splines were obtained by means of physical splines, And the lofting staff made elastic wood strips (or organic glass strips ), /// use iron to fix the curve at the fixed value point that should pass through. The curve drawn by the natural bending of the spline is the spline. A Spline is continuous not only by the ordered value points, but also by the first and second derivative at each Value Point, that is, the curve has the characteristics of continuous and uniform curvature variation. // NOTE: Refer to Baidu encyclopedia http://baike.baidu.com/view/1896463.htm? Fr = aladdin // NOTE: For cubic splines, refer to Baidu encyclopedia http://baike.baidu.com/view/2326225.htm? Fr = aladdin // NOTE: For more information about curve interpolation, see Wikipedia http://zh.wikipedia.org/wiki/%E8%B2%9D%E8%8C%B2%E6%9B%B2%E7%B7%9A/// NOTE: For more information about splines, see Wikipedia regression var a = new Vector3 (, 0 ), B = new Vector3 (, 1), c = new Vector3 (, 2); var points = new Array (a, B, c ); var spline = new Spline (points); // create a spline object composed of three points a, B, and c. *////Spline///The points array object THREE composed of Vector3 objects. spline = function (points) {this. points = points; // set the parameter points to the points attribute var c = [], v3 = {x: 0, y: 0, z: 0} of the current spline object }, point, intPoint, weight, w2, w3, pa, pb, pc, pd; /*************************************** * ***** the following are the functions provided by the Spline object. **************************************** // * // The initFromArray method is an array composed of Vector3 objects (parameter ), reset the current spline. *////InitFromArray///Points array object composed of Vector3 objects ///
 
  
Returns a new spline.
 This. initFromArray = function (a) {this. points = []; for (var I = 0; I <. length; I ++) {// obtain the array length and assign the elements in the array to the points attribute of the current spline. this. points [I] = {x: a [I] [0], y: a [I] [1], z: a [I] [2] };}; // The getPoint method regards the current spline as a whole and returns the coordinate points at the k position of the current spline. /// NOTE: In the getPoint () method, the value range of K is 0.0-1. 0. *////GetPoint///Returns the coordinate of a vertex located at k of the current spline ///
 
  
Returns a new spline.
 This. getPoint = function (k) {point = (this. points. length-1) * k; // obtain the number of nodes where k is located, and the result may be decimal (for example, the spline has nine nodes, the parameter k is 0.4. The result 3.2 indicates that the parameter k is located at the third node of the current spline to the fourth node with a length of 20%.) The value is assigned to pointintPoint = Math. floor (point); // call Math. the floor () method returns an integer. The third node is assigned intPointweight = point-intPoint; // or the obtained weight. The value is assigned to weightc [0] = intPoint = 0? IntPoint: intPoint-1; // the starting point of the cubic spline. c [0] c [1] = intPoint; // control point of Cubic Spline c [1] c [2] = intPoint> this. points. length-2? This. points. length-1: intPoint + 1; // control point of Cubic Spline c [2] c [3] = intPoint> this. points. length-3? This. points. length-1: intPoint + 2; // end point of the cubic spline. c [3] pa = this. points [c [0]; pb = this. points [c [1]; pc = this. points [c [2]; pd = this. points [c [3]; w2 = weight * weight; // The squared w3 of the weight value = weight * w2; // The cubic v3.x = interpolate (pa. x, pb. x, pc. x, pd. x, weight, w2, w3); // call the interpolate Method to be a spline interpolation function, and return the x coordinate of the curve point at the parameter value t. v3.y = interpolate (pa. y, pb. y, pc. y, pd. y, weight, w2, w3); // call the interpolate Method to be a spline interpolation function, and return the y coordinate of the curve point at the parameter value t. v3.z = interpolate (pa. z, pb. z, pc. z, pd. z, weight, w2, w3); // call the interpolate Method as a spline interpolation function. return the return value of the zcoordinate return v3 of the curve point at the parameter value t; // returns the coordinate of the vertex at the k position of the current spline};/* // returns the array composed of the coordinate of the current spline node using the getControlPointsArray Method *////GetControlPointsArray///
 
  
Returns an array composed of the coordinates of the current spline node.
 This. getControlPointsArray = function () {var I, p, l = this. points. length, coords = []; for (I = 0; I <l; I ++) {// traverse the points attribute of the current spline p = this. points [I]; coords [I] = [p. x, p. y, p. z]; // assign the points attribute to the coords array} return coords; // return the array composed of the coordinates of the current spline node }; /* // The getLength method returns the object consisting of the nSubDivisions parameter (the number of segments of the current spline) and the total length of the current spline. *////GetLength///Number of segments ///
 
  
Returns the object composed of the nSubDivisions parameter (the number of segments of the current spline) and the total length of the current spline.
 // Approximate length by summing linear segments // obtain the approximate length of a linear line segment. getLength = function (nSubDivisions) {var I, index, nSamples, position, point = 0, intPoint = 0, oldIntPoint = 0, oldPosition = new THREE. vector3 (), tmpVec = new THREE. vector3 (), chunkLengths = [], totalLength = 0; // first point has 0 length // The first start point length is 0 chunkLengths [0] = 0; if (! NSubDivisions) nSubDivisions = 100; nSamples = this. points. length * nSubDivisions; oldPosition. copy (this. points [0]); for (I = 1; I <nSamples; I ++) {index = I/nSamples; position = this. getPoint (index); // call the getPoint () method to obtain the coordinates of the current number of segments. tmpVec. copy (position); totalLength + = tmpVec. distanceTo (oldPosition); // calculates the length of the current segment. oldPosition. copy (position); point = (this. points. length-1) * index; intPoint = Math. floor (point); if (intPoint! = OldIntPoint) {chunkLengths [intPoint] = totalLength; oldIntPoint = intPoint ;}// last point ends with total length // The last end point, returns the total length. chunkLengths [chunkLengths. length] = totalLength; return {chunks: chunkLengths, total: totalLength };}; this. reparametrizeByArcLength = function (samplingCoef) {var I, j, index, indexCurrent, indexNext, linearDistance, realDistance, sampling, position, newpoints = [], tm PVec = new THREE. vector3 (), sl = this. getLength (); newpoints. push (tmpVec. copy (this. points [0]). clone (); for (I = 1; I <this. points. length; I ++) {// tmpVec. copy (this. points [I-1]); // linearDistance = tmpVec. distanceTo (this. points [I]); realDistance = sl. chunks [I]-sl. chunks [I-1]; sampling = Math. ceil (samplingCoef * realDistance/sl. total); indexCurrent = (I-1)/(this. point S. length-1); indexNext = I/(this. points. length-1); for (j = 1; j <sampling-1; j ++) {index = indexCurrent + j * (1/sampling) * (indexNext-indexCurrent ); position = this. getPoint (index); newpoints. push (tmpVec. copy (position ). clone ();} newpoints. push (tmpVec. copy (this. points [I]). clone ();} this. points = newpoints;}; // Catmull-Rom/* // The interpolate Method is the legendary spline interpolation function. Here it is the cubic spline interpolation algorithm, which returns Calculate the curve point of the parameter value t. // NOTE: For cubic spline interpolation, refer to Baidu encyclopedia http://baike.baidu.com/view/2326225.htm? Fr = aladdin // NOTE: For more information about curve interpolation, see Wikipedia http://zh.wikipedia.org/wiki/%E8%B2%9D%E8%8C%B2%E6%9B%B2%E7%B7%9A/// NOTE: For more information about splines, see Wikipedia tips *////Interpolate///Spline start point p0 ///Spline Control Point p1 ///Spline control point p2 ///Spline end point p3 ///T is the parameter value, 0 <= t <= 1 ///T2 is the square of the parameter value t ///T3 is the cube of the parameter value t ///
 
  
Returns the curve point at the parameter value t.
 Function interpolate (p0, p1, p2, p3, t, t2, t3) {var v0 = (p2-p0) * 0.5, v1 = (p3-p1) * 0.5; return (2 * (p1-p2) + v0 + v1) * t3 + (-3*(p1-p2)-2 * v0-v1) * t2 + v0 * t + p1; // return the curve point at the parameter value t };};


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

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

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.