math.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. import * as THREE from "../../../libs/three.js/build/three.module.js";
  2. import searchRings from "./searchRings.js";
  3. import {ExtendView} from '../../viewer/ExtendView.js'
  4. var math = {
  5. getBaseLog(x, y) {//返回以 x 为底 y 的对数(即 logx y) . Math.log 返回一个数的自然对数
  6. return Math.log(y) / Math.log(x);
  7. }
  8. ,
  9. convertVector : {
  10. ZupToYup: function(e){//navvis -> 4dkk
  11. return new THREE.Vector3(e.x,e.z,-e.y)
  12. },
  13. YupToZup: function(e){//4dkk -> navvis
  14. return new THREE.Vector3(e.x,-e.z,e.y)
  15. },
  16. },
  17. convertQuaternion: {
  18. ZupToYup: function(e){//navvis -> 4dkk //不同于convertVisionQuaternion
  19. let rotation = new THREE.Euler(-Math.PI/2,0,0)
  20. let quaternion = new THREE.Quaternion().setFromEuler(rotation)
  21. return e.clone().premultiply(quaternion)
  22. //return new THREE.Quaternion(e.x,e.z,-e.y,e.w).multiply((new THREE.Quaternion).setFromAxisAngle(new THREE.Vector3(1,0,0), THREE.Math.degToRad(90)))
  23. },
  24. YupToZup: function(e){//4dkk -> navvis
  25. let rotation = new THREE.Euler(Math.PI/2,0,0)
  26. let quaternion = new THREE.Quaternion().setFromEuler(rotation)
  27. return e.clone().premultiply(quaternion)
  28. },
  29. },
  30. convertVisionQuaternion: function(e) {
  31. return new THREE.Quaternion(e.x,e.z,-e.y,e.w).multiply((new THREE.Quaternion).setFromAxisAngle(new THREE.Vector3(0,1,0), THREE.Math.degToRad(90)))
  32. },
  33. invertVisionQuaternion : function(e) {//反转给算法部
  34. var a = e.clone().multiply((new THREE.Quaternion).setFromAxisAngle(new THREE.Vector3(0,1,0), THREE.Math.degToRad(-90)))
  35. return new THREE.Quaternion(a.x,-a.z,a.y,a.w)
  36. },
  37. //------------
  38. getVec2Angle : function(dir1,dir2){
  39. return Math.acos( THREE.Math.clamp(this.getVec2Cos(dir1,dir2), -1,1) )
  40. },
  41. getVec2Cos : function(dir1,dir2){
  42. return dir1.dot(dir2) / dir1.length() / dir2.length()
  43. },
  44. getAngle:function(vec1, vec2, axis='z'){//带方向的角度 vector3
  45. if(!vec1.isVector3){
  46. vec1 = new THREE.Vector3(vec1.x, vec1.y, 0)
  47. vec2 = new THREE.Vector3(vec2.x, vec2.y, 0)
  48. }
  49. var angle = vec1.angleTo(vec2)
  50. var axis_ = vec1.clone().cross(vec2);
  51. if(typeof axis == 'string'){
  52. if(axis_[axis] < 0){
  53. angle *= -1
  54. }
  55. }else{//vector3
  56. if(axis_.dot(axis)< 0){
  57. angle *= -1
  58. }
  59. }
  60. return angle
  61. },
  62. closeTo : function(a,b, precision=1e-6){
  63. let f = (a,b)=>{
  64. return Math.abs(a-b) < precision;
  65. }
  66. if(typeof (a) == 'number'){
  67. return f(a, b);
  68. }else{
  69. let judge = (name)=>{
  70. if(a[name] == void 0)return true //有值就判断,没值就不判断
  71. else return f(a[name],b[name])
  72. }
  73. return judge('x') && judge('y') && judge('z') && judge('w')
  74. }
  75. },
  76. toPrecision: function (e, t) {//xzw change 保留小数
  77. var f = function (e, t) {
  78. var i = Math.pow(10, t);
  79. return Math.round(e * i) / i
  80. }
  81. if (e instanceof Array) {
  82. for (var s = 0; s < e.length; s++) {
  83. e[s] = f(e[s], t);
  84. }
  85. return e;
  86. } else if (e instanceof Object) {
  87. for (var s in e) {
  88. e[s] = f(e[s], t);
  89. }
  90. return e;
  91. } else if(typeof e == 'number'){
  92. return f(e, t)
  93. }else{
  94. return e
  95. }
  96. },
  97. isEmptyQuaternion: function(e) {
  98. return 0 === Math.abs(e.x) && 0 === Math.abs(e.y) && 0 === Math.abs(e.z) && 0 === Math.abs(e.w)
  99. },
  100. projectPositionToCanvas: function(e, t, i) {
  101. i = i || new THREE.Vector3,
  102. i.copy(e);
  103. var r = .5 * $('#player').width()
  104. , o = .5 * $('#player').height();
  105. return i.project(t),
  106. i.x = i.x * r + r,
  107. i.y = -(i.y * o) + o,
  108. i
  109. },
  110. handelPadResize:false,
  111. /* handelPadding : function () { //去除player左边和上面的宽高,因为pc的player左上有其他element 许钟文
  112. var pads = [];//记录下来避免反复计算
  113. var index = [];
  114. var resetPad = function(){
  115. pads = [];
  116. index = [];
  117. math.handelPadResize = false; //switchview时resized为true
  118. }
  119. if(config.isEdit && !config.isMobile){
  120. window.addEventListener('resize',resetPad);
  121. }
  122. return function(x, y, domE){
  123. if(!config.isEdit || config.isMobile) {
  124. return {
  125. x: x,
  126. y: y
  127. }
  128. }
  129. if(this.handelPadResize)resetPad();
  130. domE = domE || $('#player')[0];
  131. var pad;
  132. var i = index.indexOf(domE);
  133. if (i == -1){
  134. index.push(domE);
  135. pad = {
  136. x: this.getOffset("left", domE),
  137. y: this.getOffset("top", domE)
  138. }
  139. pads.push(pad)
  140. }
  141. else pad = pads[i];
  142. return {
  143. x: x - pad.x,
  144. y: y - pad.y
  145. }
  146. }
  147. }(), */
  148. getOffset: function (type, element, parent) {//获取元素的边距 许钟文
  149. var offset = (type == "left") ? element.offsetLeft : element.offsetTop;
  150. if (!parent) parent = $("body")[0];
  151. while (element = element.offsetParent) {
  152. if (element == parent) break;
  153. offset += (type == "left") ? element.offsetLeft : element.offsetTop;
  154. }
  155. return offset;
  156. }
  157. ,
  158. constrainedTurn: function(e) {
  159. var t = e % (2 * Math.PI);
  160. return t = t > Math.PI ? t -= 2 * Math.PI : t < -Math.PI ? t += 2 * Math.PI : t
  161. },
  162. getFOVDotThreshold: function(e) {
  163. return Math.cos(THREE.Math.degToRad(e / 2))
  164. },
  165. transform2DForwardVectorByCubeFace: function(e, t, i, n) {
  166. switch (e) {
  167. case GLCubeFaces.GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  168. i.set(1, t.y, t.x);
  169. break;
  170. case GLCubeFaces.GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  171. i.set(-1, t.y, -t.x);
  172. break;
  173. case GLCubeFaces.GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  174. i.set(-t.x, 1, -t.y);
  175. break;
  176. case GLCubeFaces.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  177. i.set(-t.x, -1, t.y);
  178. break;
  179. case GLCubeFaces.GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  180. i.set(-t.x, t.y, 1);
  181. break;
  182. case GLCubeFaces.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  183. i.set(t.x, t.y, -1)
  184. }
  185. n && i.normalize()
  186. },
  187. getFootPoint : function(oldPos, p1, p2, restricInline){ //找oldPos在线段p1, p2上的垂足
  188. /* if(isWorld){//输出全局坐标 需要考虑meshGroup.position
  189. p1 = p1.clone();
  190. p2 = p2.clone();
  191. p1.y += mainDesign.meshGroup.position.y;
  192. p2.y += mainDesign.meshGroup.position.y;
  193. } */
  194. if(p1.equals(p2))return p1.clone()
  195. var op1 = oldPos.clone().sub(p1);
  196. var p1p2 = p1.clone().sub(p2)
  197. var p1p2Len = p1p2.length()
  198. var leftLen = op1.dot(p1p2) / p1p2Len;
  199. var pos = p1.clone().add(p1p2.multiplyScalar( leftLen/p1p2Len ));
  200. if(restricInline && pos.clone().sub(p1).dot( pos.clone().sub(p2) ) > 0){//foot不在线段上
  201. if(pos.distanceTo(p1) < pos.distanceTo(p2)) pos = p1.clone();
  202. else pos = p2.clone();
  203. }
  204. return pos;
  205. },
  206. /**
  207. * 计算多边形的重心
  208. * @param {*} points
  209. */
  210. getCenterOfGravityPoint : function(mPoints){
  211. var area = 0.0;//多边形面积
  212. var Gx = 0.0, Gy = 0.0;// 重心的x、y
  213. for (var i = 1; i <= mPoints.length; i++) {
  214. var ix = mPoints[i % mPoints.length].x;
  215. var iy = mPoints[i % mPoints.length].y;
  216. var nx = mPoints[i - 1].x;
  217. var ny = mPoints[i - 1].y;
  218. var temp = (ix * ny - iy * nx) / 2.0;
  219. area += temp;
  220. Gx += temp * (ix + nx) / 3.0;
  221. Gy += temp * (iy + ny) / 3.0;
  222. }
  223. Gx = Gx / area;
  224. Gy = Gy / area;
  225. return { x: Gx, y: Gy };
  226. },
  227. getBound : function(ring){
  228. var bound = new THREE.Box2();
  229. for(var j=0,len = ring.length; j<len; j++){
  230. bound.expandByPoint(ring[j])
  231. }
  232. return bound;
  233. },
  234. isPointInArea : function(ring, holes, point, ifAtLine){//判断点是否在某个环内, 若传递了holes代表还要不能在内环内
  235. var bound = this.getBound(ring);
  236. if(point.x < bound.min.x || point.x > bound.max.x || point.y < bound.min.y || point.y > bound.max.y)return false;
  237. var inside = false;
  238. var x = point.x,
  239. y = point.y;
  240. for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) {
  241. var xi = ring[i].x,
  242. yi = ring[i].y;
  243. var xj = ring[j].x,
  244. yj = ring[j].y;
  245. if((xi - x)*(yj - y) == (xi - x)*(yi - y) && x>=Math.min(xi,xj) && x<=Math.max(xi,xj)//xzw add
  246. && y>=Math.min(yi,yj) && y<=Math.max(yi,yj)
  247. ){
  248. //return !!ifAtLine;//在线段上,则判断为…… (默认在外)
  249. return {atLine:true}
  250. }
  251. if (((yi > y) != (yj > y)) &&
  252. (x < (xj - xi) * (y - yi) / (yj - yi) + xi)
  253. ) {
  254. inside = !inside;
  255. }
  256. }
  257. if(inside && holes){
  258. return !holes.some(ring=>this.isPointInArea(ring, null, point, ifAtLine) ) //不能存在于任何一个二级内环内
  259. }else{
  260. return inside;
  261. }
  262. },
  263. getArea : function (ring) { //求面积 顺时针为正 来自three shape
  264. for (var t = ring.length, i = 0, n = t - 1, r = 0; r < t; n = r++)
  265. i += ring[n].x * ring[r].y - ring[r].x * ring[n].y;
  266. return -.5 * i
  267. },
  268. getVolume:function(faceArr){//求三角多面体的体积。和求面积同理都用鞋带计算法 https://blog.csdn.net/weixin_43414513/article/details/123758897
  269. //问题:怎么确定方向
  270. //每个三角形的顺序必须是右手螺旋法则指向物体外(外向里看为逆时针),分别为ABC,则每个三角形和原点O构成的三棱锥体积为 (OA cross OB) dot(OC) / 6 . 结果一般朝向原点的为负,反之为正
  271. },
  272. isInBetween : function(a, b, c, precision) {
  273. // 如果b几乎等于a或c,返回false.为了避免浮点运行时两值几乎相等,但存在相差0.00000...0001的这种情况出现使用下面方式进行避免
  274. /* if (Math.abs(a - b) < 0.000001 || Math.abs(b - c) < 0.000001) {
  275. return false;
  276. }
  277. return (a <= b && b <= c) || (c <= b && b <= a);*/
  278. //更改:如果b和a或c中一个接近 就算在a和c之间
  279. return (a <= b && b <= c) || (c <= b && b <= a) || this.closeTo(a,b,precision) || this.closeTo(b,c,precision);
  280. },
  281. ifPointAtLineBound:function(point, linePoints, precision){
  282. //待验证 横线和竖线比较特殊
  283. return math.isInBetween(linePoints[0].x, point.x, linePoints[1].x, precision) && math.isInBetween(linePoints[0].y, point.y, linePoints[1].y, precision)
  284. }
  285. ,
  286. isLineIntersect: function (line1, line2, notSegment, precision) {//线段和线段是否有交点. notSegment代表是直线而不是线段
  287. var a1 = line1[1].y - line1[0].y;
  288. var b1 = line1[0].x - line1[1].x;
  289. var c1 = a1 * line1[0].x + b1 * line1[0].y;
  290. //转换成一般式: Ax+By = C
  291. var a2 = line2[1].y - line2[0].y;
  292. var b2 = line2[0].x - line2[1].x;
  293. var c2 = a2 * line2[0].x + b2 * line2[0].y;
  294. // 计算交点
  295. var d = a1 * b2 - a2 * b1;
  296. // 当d==0时,两线平行
  297. if (d == 0) {
  298. return false;
  299. } else {
  300. var x = (b2 * c1 - b1 * c2) / d;
  301. var y = (a1 * c2 - a2 * c1) / d;
  302. // 检测交点是否在两条线段上
  303. /* if (notSegment || (isInBetween(line1[0].x, x, line1[1].x) || isInBetween(line1[0].y, y, line1[1].y)) &&
  304. (isInBetween(line2[0].x, x, line2[1].x) || isInBetween(line2[0].y, y, line2[1].y))) {
  305. return {x,y};
  306. } */
  307. if (notSegment || math.ifPointAtLineBound({x,y}, line1, precision) && math.ifPointAtLineBound({x,y}, line2, precision)){
  308. return {x,y};
  309. }
  310. }
  311. },
  312. getNormal2d : function(o={} ){//获取二维法向量 方向向内
  313. var x,y, x1,y1;
  314. //line2d的向量
  315. if(o.vec){
  316. x1 = o.vec.x; y1 = o.vec.y
  317. }else{
  318. x1 = o.p1.x - o.p2.x;
  319. y1 = o.p1.y - o.p2.y;
  320. }
  321. //假设法向量的x或y固定为1或-1
  322. if(y1 != 0){
  323. x = 1;
  324. y = - (x1 * x) / y1;
  325. }else if(x1 != 0){//y如果为0,正常情况x不会是0
  326. y = 1;
  327. x = - (y1 * y) / x1;
  328. }else{
  329. console.log("两个点一样");
  330. return null;
  331. }
  332. //判断方向里或者外:
  333. var vNormal = new THREE.Vector3(x, 0, y);
  334. var vLine = new THREE.Vector3(x1, 0, y1);
  335. var vDir = vNormal.cross(vLine);
  336. if(vDir.y>0){
  337. x *= -1;
  338. y *= -1;
  339. }
  340. return new THREE.Vector2(x, y).normalize();
  341. },
  342. getQuaBetween2Vector:function(oriVec, newVec, upVec){ //获取从oriVec旋转到newVec可以应用的quaternion
  343. var angle = oriVec.angleTo(newVec);
  344. var axis = oriVec.clone().cross( newVec).normalize();//两个up之间
  345. if(axis.length() == 0){//当夹角为180 或 0 度时,得到的axis为(0,0,0),故使用备用的指定upVec
  346. return new THREE.Quaternion().setFromAxisAngle( upVec, angle );
  347. }
  348. return new THREE.Quaternion().setFromAxisAngle( axis, angle );
  349. } ,
  350. /*
  351. getQuaBetween2Vector2 : function(oriVec, newVec ){//not camera
  352. var _ = (new THREE.Matrix4).lookAt( oriVec, new THREE.Vector3, new THREE.Vector3(0,1,0))
  353. var aimQua = (new THREE.Quaternion).setFromRotationMatrix(_)
  354. var _2 = (new THREE.Matrix4).lookAt( newVec, new THREE.Vector3, new THREE.Vector3(0,1,0))
  355. var aimQua2 = (new THREE.Quaternion).setFromRotationMatrix(_2)
  356. return aimQua2.multiply(aimQua.clone().inverse())
  357. } */
  358. getQuaByAim: function (aim, center=new THREE.Vector3) {
  359. let forward = new THREE.Vector3(0, 1, 0)
  360. let qua1 = new THREE.Quaternion().setFromUnitVectors(forward, aim.clone().sub(center).normalize())
  361. /* var _ = (new THREE.Matrix4).lookAt(center,aim, new THREE.Vector3(0,1,0));
  362. let qua2 = (new THREE.Quaternion).setFromRotationMatrix(_);
  363. let rot1 = new THREE.Euler().setFromQuaternion(qua1)
  364. let rot2 = new THREE.Euler().setFromQuaternion(qua2) //奇怪,qua2怎么都不对
  365. console.log(rot1,rot2) */
  366. return qua1
  367. },
  368. getAimByQua: function (quaternion, center=new THREE.Vector3) {
  369. return new THREE.Vector3(0, 0, -1).applyQuaternion(quaternion).add(center)
  370. },
  371. getScaleForConstantSize : function(){ //获得规定二维大小的mesh的scale值。可以避免因camera的projection造成的mesh视觉大小改变。 来源:tag.updateDisc
  372. var w;
  373. var i = new THREE.Vector3, o = new THREE.Vector3, l = new THREE.Vector3, c = new THREE.Vector3, h = new THREE.Vector3
  374. return function(op={}){
  375. if(op.width2d) w = op.width2d //如果恒定二维宽度
  376. else{//否则考虑上距离,加一丢丢近大远小的效果
  377. var currentDis, nearBound, farBound
  378. if(op.camera.type == "OrthographicCamera"){
  379. currentDis = 200 / op.camera.zoom //(op.camera.right - op.camera.left) / op.camera.zoom
  380. }else{
  381. currentDis = op.position.distanceTo(op.camera.position);
  382. }
  383. w = op.maxSize - ( op.maxSize - op.minSize) * THREE.Math.smoothstep(currentDis, op.nearBound, op.farBound);
  384. //maxSize : mesh要表现的最大像素宽度; nearBound: 最近距离,若比nearBound近,则使用maxSize
  385. }
  386. i.copy(op.position).project(op.camera); //tag中心在屏幕上的二维坐标
  387. o.set(op.resolution.x / 2, op.resolution.y / 2, 1).multiply(i); //转化成px -w/2 到 w/2的范围
  388. l.set(w / 2, 0, 0).add(o); //加上tag宽度的一半
  389. c.set(2 / op.resolution.x, 2 / op.resolution.y, 1).multiply(l); //再转回 -1 到 1的范围
  390. h.copy(c).unproject(op.camera);//再转成三维坐标,求得tag边缘的位置
  391. var g = h.distanceTo(op.position)//就能得到tag的三维半径
  392. //这里使用的都是resolution2, 好处是手机端不会太小, 坏处是pc更改网页显示百分比时显示的大小会变(或许可以自己算出设备真实的deviceRatio, 因window.screen是不会改变的),但考虑到用户可以自行调节字大小也许是好的
  393. return g //可能NAN 当相机和position重叠时
  394. }
  395. }()
  396. ,
  397. //W , H, left, top分别是rect的宽、高、左、上
  398. getCrossPointAtRect : function(p1, aim, W , H, left, top){//求射线p1-aim在rect边界上的交点,其中aim在rect范围内,p1则不一定(交点在aim这边的延长线上)
  399. var x,y, borderX;
  400. var r = (aim.x - p1.x) / (aim.y - p1.y);//根据相似三角形原理先求出这个比值
  401. var getX = function(y){
  402. return r * (y-p1.y) + p1.x;
  403. }
  404. var getY = function(x){
  405. return 1/r * (x-p1.x) + p1.y;
  406. }
  407. if(aim.x >= p1.x){
  408. borderX = W+left;
  409. }else{
  410. borderX = left;
  411. }
  412. x = borderX;
  413. y = getY(x);
  414. if(y < top || y > top+H){
  415. if(y < top){
  416. y = top;
  417. }else{
  418. y = top+H;
  419. }
  420. x = getX(y)
  421. }
  422. return new THREE.Vector2(x, y);
  423. },
  424. getDirFromUV : function(uv){ //获取dir 反向计算 - - 二维转三维比较麻烦
  425. var dirB; //所求 单位向量
  426. var y = Math.cos(uv.y * Math.PI); //uv中纵向可以直接确定y, 根据上面getUVfromDir的反向计算
  427. // 故 uv.y * Math.PI 就是到垂直线(向上)的夹角
  428. var angle = 2 * Math.PI * uv.x - Math.PI //x/z代表的是角度
  429. var axisX, axisZ; //axis为1代表是正,-1是负数
  430. if (-Math.PI <= angle && angle < 0) {
  431. axisX = -1 //下半圆
  432. } else {
  433. axisX = 1 //上半圆
  434. }
  435. if (-Math.PI / 2 <= angle && angle < Math.PI / 2) {
  436. axisZ = 1 //右半圆
  437. } else {
  438. axisZ = -1 //左半圆
  439. }
  440. var XDivideZ = Math.tan(angle);
  441. var z = Math.sqrt((1 - y * y) / (1 + XDivideZ * XDivideZ));
  442. var x = XDivideZ * z
  443. if (z * axisZ < 0) { //异号
  444. z *= -1;
  445. x *= -1;
  446. if (x * axisX < 0) {
  447. // console.log("wrong!!!!!??????????")
  448. }
  449. }
  450. x *= -1 //计算完成后这里不能漏掉 *= -1
  451. dirB = this.convertVector.YupToZup(new THREE.Vector3(x, y, z))
  452. //理想状态下x和z和anotherDir相同
  453. return dirB
  454. },
  455. getUVfromDir : function(dir) { //获取UV 同shader里的计算
  456. var dir = this.convertVector.ZupToYup(dir)
  457. dir.x *= -1; //计算前这里不能漏掉 *= -1 见shader
  458. var tx = Math.atan2(dir.x, dir.z) / (Math.PI * 2.0) + 0.5; //atan2(y,x) 返回从 X 轴正向逆时针旋转到点 (x,y) 时经过的角度。区间是-PI 到 PI 之间的值
  459. var ty = Math.acos(dir.y) / Math.PI;
  460. return new THREE.Vector2(tx, ty)
  461. //理想状态下tx相同
  462. },
  463. getDirByLonLat : function(lon,lat){
  464. var dir = new THREE.Vector3
  465. var phi = THREE.Math.degToRad(90 - lat);
  466. var theta = THREE.Math.degToRad(lon);
  467. dir.x = Math.sin(phi) * Math.cos(theta);
  468. dir.y = Math.cos(phi);
  469. dir.z = Math.sin(phi) * Math.sin(theta);
  470. return dir
  471. } //0,0 => (1,0,0) 270=>(0,0,-1)
  472. ,
  473. projectPointAtPlane:function(o={}){//获取一个点在一个面上的投影 {facePoints:[a,b,c], point:}
  474. var plane = new THREE.Plane().setFromCoplanarPoints(...o.facePoints)
  475. return plane.projectPoint(o.point, new THREE.Vector3() )
  476. }
  477. ,
  478. getPolygonsMixedRings:function( polygons, onlyGetOutRing){//{points:[vector2,...],holes:[[],[]]}
  479. let points = []
  480. let lines = []
  481. let i = 0
  482. polygons.forEach(e=> points.push(...e.map(a=>new THREE.Vector2().copy(a) )) )
  483. polygons.forEach((ps,j)=>{
  484. let length = ps.length;
  485. let index = 0;
  486. while(index<length){
  487. lines.push({p1:index+i,p2:(index+1)%length+i});
  488. index ++;
  489. }
  490. i+=length
  491. })
  492. points.forEach((p,j)=>{p.id = j})
  493. let rings = searchRings({
  494. points,
  495. lines,
  496. onlyGetOutRing
  497. })
  498. //console.log(rings)
  499. rings = rings.filter(e=>e.closetParent == void 0)// 子环不加,被外环包含了
  500. return rings
  501. },
  502. getQuaFromPosAim( position, target ){
  503. /* let matrix = (new THREE.Matrix4).lookAt(position, target, new THREE.Vector3(0,0,1)) //这里垂直的话会默认给一个右向所以不这么写
  504. return (new THREE.Quaternion).setFromRotationMatrix(matrix) */
  505. let view = new ExtendView()
  506. view.direction = new THREE.Vector3().subVectors(target,position)
  507. return view.quaternion
  508. },
  509. getBoundByPoints(points, minSize){
  510. var bound = new THREE.Box3
  511. points.forEach(point=>{
  512. bound.expandByPoint(point)
  513. })
  514. let center = bound.getCenter(new THREE.Vector3)
  515. if(minSize){
  516. let minBound = (new THREE.Box3()).setFromCenterAndSize(center, minSize)
  517. bound.union(minBound)
  518. }
  519. return {
  520. bounding:bound,
  521. size: bound.getSize(new THREE.Vector3),
  522. center,
  523. }
  524. },
  525. /* linearClamp(value, x1,x2, y1, y2){//x为bound.min, bound.max
  526. value = THREE.Math.clamp(value, x1,x2)
  527. return y1 + ( y2 - y1) * (value - x1) / (x2 - x1)
  528. } */
  529. linearClamp(value, xArr , yArr){ //xArr需要按顺序从小到大,yArr对应xArr中的值
  530. let len = xArr.length
  531. if(value <= xArr[0]) return yArr[0]
  532. if(value >= xArr[len - 1]) return yArr[len - 1]
  533. let i = 0
  534. while(++i < len ){
  535. if(value < xArr[i]){
  536. let x1 = xArr[i-1], x2 = xArr[i], y1 = yArr[i-1], y2 = yArr[i]
  537. value = y1 + ( y2 - y1) * (value - x1) / (x2 - x1)
  538. break
  539. }
  540. }
  541. return value
  542. }
  543. };
  544. /*
  545. 如何将若干个点拟合出线段
  546. // 假设你有一个点数组,每个点表示为一个包含x和y坐标的对象
  547. const points = [
  548. { x: 1, y: 2 },
  549. { x: 2, y: 3 },
  550. { x: 3, y: 4 },
  551. { x: 4, y: 5 },
  552. // ... 更多点
  553. ];
  554. // 定义用于计算最小二乘法参数的函数
  555. function leastSquares( ) {
  556. let sumX = 0;
  557. let sumY = 0;
  558. let sumXY = 0;
  559. let sumX2 = 0;
  560. let n = points.length;
  561. for (const point of points) {
  562. sumX += point.x;
  563. sumY += point.y;
  564. sumXY += point.x * point.y;
  565. sumX2 += point.x * point.x;
  566. }
  567. const k = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX); //不知道为何这样算,自己解不出来(使点到直线距离之和平方最小),但测了下似乎是准的
  568. const b = (sumY - k * sumX) / n;
  569. return { k, b };
  570. }
  571. // 使用上述函数计算拟合直线的参数
  572. const { k, b } = leastSquares(points);
  573. // 现在你可以使用这些参数来表示拟合出的线段
  574. console.log(`拟合线段的方程为: y = ${k}x + ${b}`);
  575. */
  576. Potree.math = math
  577. export default math