PanoBoxFrame.js 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790
  1. import * as THREE from 'three'
  2. import math from './util/math.js'
  3. import common from './util/common.js'
  4. import BoundingMesh from './util/BoundingMesh.js'
  5. import Vectors from './util/Vectors.js'
  6. const version = 'output'
  7. //----------------------复制以下内容---------------------------------
  8. //----------------------复制以下内容---------------------------------
  9. let player,
  10. skyBoxTight,
  11. meshGroup,
  12. ray = new THREE.Raycaster(),
  13. groundPlane = new THREE.Plane()
  14. let groundY, safeBound //安全区域应该在扣除每种类型柜子大概的长宽的一半
  15. let hue = 0
  16. let boxes = []
  17. let standards = {
  18. cabinet: {
  19. widthNormal: { min: 0.6, max: 0.7 }, //widthNormal是不计宽还是厚度的平均宽度
  20. height:{min: 0.7, max:2.5, standard:2},
  21. },
  22. air: {
  23. widthNormal: { min: 0.35, max: 0.7 },
  24. width: { min: 0.6, max: 0.75 },
  25. thick: { min: 0.3, max: 0.4 },
  26. height: {min:1.4, max:1.95, standard: 1.8}, //standard是通常出现的最高高度
  27. },
  28. 'air-hanging': {
  29. widthNormal: { min: 0.3, max: 1},
  30. width: { min: 0.8, max: 1.1 },
  31. thick: { min: 0.2, max: 0.3 },
  32. height: { min: 0.3, max: 0.5, standard: 0.4},
  33. bottom: { min: 0.8, max: 2.0 }, // 不绝对,大部分
  34. },
  35. battery: {
  36. widthNormal: { min: 0.4, max: 1.2},
  37. width: { min: 0.7, max: 1.4 },
  38. thick: { min: 0.35, max: 0.5 },
  39. height:{min:0.3, max:1.3, standard:1.1 }, //maxHeight
  40. //有的电池很小。考虑是否追加battery-little 并且限制大电池的长宽高比例
  41. },
  42. rowBigBox: {
  43. widthNormal: { min: 0.5, max: Infinity },
  44. height: {min: 0.7, max:2.4, standard:2},
  45. },
  46. }
  47. let addLabel = (pos, text, { bgcolor, a } = {}) => {
  48. if(version != 'vision')return
  49. bgcolor = bgcolor ? new THREE.Color(bgcolor) : { r: 1, g: 1, b: 1 }
  50. let textMesh = new TextSprite({
  51. text: text || 'aaaaaa',
  52. textColor: { r: 0, g: 0, b: 0, a: 1 },
  53. backgroundColor: { r: bgcolor.r * 255, g: bgcolor.g * 255, b: bgcolor.b * 250, a: a || 0.8 },
  54. margin: { x: 5, y: 5 },
  55. borderRadius: 0,
  56. player: player,
  57. })
  58. textMesh.position.copy(pos)
  59. textMesh.scale.set(0.3, 0.3, 0.3)
  60. meshGroup.add(textMesh)
  61. return textMesh
  62. }
  63. let getBoxPos = info => {
  64. return (info.preDealRes && info.preDealRes.position) || info.center || (info.type == 'air-hanging' ? info.posAtWall : info.btmPosPredict) || info.posAtWall
  65. }
  66. let updateBoxType = info => {
  67. let type = info.category || info.box0.category
  68. if(info.sid == "pano0-2"){
  69. console.log(3)
  70. }
  71. if (type == 'air') {
  72. let btm = info.btmPos || info.btmPosPredict //btmPosAtWall
  73. if(!btm){
  74. btm = getBoxBottom(info.box0 ? info : {box0:info})
  75. }
  76. if (!btm) return
  77. /* let top = info.topPos
  78. if(!top){
  79. top = getBoxTop(info.box0 ? info : {box0:info})
  80. } */
  81. let center = info.posAtWall || (info.preDealRes && info.preDealRes.position) || info.center
  82. const s = standards['air-hanging']
  83. if (btm.y - groundY > s.bottom.min){
  84. let h0 = btm.y - groundY
  85. let h1 = (player.model.boundingBox.max.y - center.y) /(player.model.size.y)
  86. let h2 = center.y-btm.y
  87. let score = h0 * 2 - h1*3 - h2 * 3
  88. if(score > 0){
  89. type = 'air-hanging'
  90. }
  91. //console.error( score, h0,h1,h2, info.sid||info.name)
  92. } //注意:如果air被遮住底部,露出的部分只有一点,还是有可能被识别成air-hanging。只能希望
  93. //console.error( type, info.sid||info.name)
  94. }
  95. if (info.box0) {
  96. info.boxType = /* info.box0.type = */ type
  97. //info.box1 && (info.box1.type = type) //因为box0和box1不一定匹配,所以不能直接赋值
  98. } else {
  99. info.type = type
  100. }
  101. }
  102. let preDealBox = matchInfo => {
  103. if (matchInfo.preDealRes || !matchInfo.center) return
  104. matchInfo.preDealRes = {}
  105. matchInfo.boxType || updateBoxType(matchInfo)
  106. let minWidth = standards[matchInfo.boxType].widthNormal.min
  107. let needGetPose
  108. if (matchInfo.str && matchInfo.str.includes('outsideBound')) {
  109. const shrink = minWidth * 0.85
  110. //addLabel(matchInfo.center, '原')
  111. let finalPos = matchInfo.center.clone().clamp(safeBound.min, safeBound.max)
  112. matchInfo.preDealRes.position = finalPos
  113. //addLabel(finalPos, 'finalPos')
  114. updateBoxType(matchInfo)
  115. needGetPose = true
  116. }
  117. let center = getBoxPos(matchInfo)
  118. if (needGetPose || !matchInfo.boxposes) {
  119. matchInfo.boxposes = []
  120. ;[matchInfo.box0, matchInfo.box1].forEach(box => {
  121. box && matchInfo.boxposes.push(getBoxPoseByPos(box, center))
  122. })
  123. }
  124. //-----------------
  125. /* if(matchInfo.name == "pano0-4&pano2-1"){
  126. console.log(4)
  127. } */
  128. let xProp, yProp
  129. if (matchInfo.boxType == 'battery' || matchInfo.boxType == 'air-hanging' || matchInfo.boxType == 'air') {
  130. //根据比例判断
  131. /* let r1 = Math.abs((center.x - skyBoxTight.position.x) / (center.z - skyBoxTight.position.z))
  132. let r2 = player.model.size.x / player.model.size.z
  133. if(!math.closeTo(r1,r2, 0.05)){
  134. if (r1<r2) {
  135. ;(xProp = 'width'), (yProp = 'thick') //贴附x(横)墙
  136. } else {
  137. ;(xProp = 'thick'), (yProp = 'width') //贴附y(竖)墙
  138. }
  139. matchInfo.xProp = xProp
  140. matchInfo.yProp = yProp
  141. } */
  142. //根据距离判断
  143. if(matchInfo.name == 'pano16-3&pano14-6'){
  144. console.log(5)
  145. }
  146. let bound = player.model.boundingBox
  147. let minXDiff = Math.min(center.x - bound.min.x, bound.max.x - center.x)
  148. let minYDiff = Math.min(center.z - bound.min.z, bound.max.z - center.z)
  149. if(!math.closeTo(minXDiff,minYDiff, 0.3) && (minXDiff < 0.8 || minYDiff < 0.8)){
  150. if(minXDiff<minYDiff){
  151. ;(xProp = 'thick'), (yProp = 'width') //贴附y(竖)墙
  152. }else{
  153. ;(xProp = 'width'), (yProp = 'thick') //贴附x(横)墙
  154. }
  155. matchInfo.xProp = xProp
  156. matchInfo.yProp = yProp
  157. }
  158. }
  159. }
  160. let getPoseScore = (boxposes, isSingle) => {
  161. let score = 0
  162. boxposes.forEach(pose => {
  163. if (pose.projectWidth > pose.maxProjectWidth) {
  164. score += Math.pow((pose.projectWidth - pose.maxProjectWidth) * 8, 2)
  165. } else if (/* isSingle && */pose.projectWidth < pose.minProjectWidth) {
  166. score += Math.pow((pose.minProjectWidth - pose.projectWidth) * 20, 2)
  167. }
  168. })
  169. score = Math.min(score,600) //压低一点,因为得的宽度可能不准
  170. return -score
  171. }
  172. let getBoxSize = info => {
  173. if (info.size) return
  174. //console.warn('开始算 ' + info.name)
  175. let exStr = '',
  176. warnStr = ''
  177. let x, y //求对角线的向量 x>0,y>0
  178. //假设盒子的长宽为x,y (x>0,y>0),视线切线单位向量为(k,m),投影距离:x'k+y'm.
  179. //由于盒子的对角线有四个可选方向,(类似四个象限) 则需要能使投影距离最长的一个对角线向量。
  180. //如,当k<0,m>0时,要使xk+ym 最大,必有x<0,y>0. 故 x = -x', y = y', 故 投影距离:x'k+y'm = x(-k)+ym 。
  181. //故无论km的符号如何,只要变为正数,再去联立方程即可得xy。
  182. //注:但是因为无法获取准确的投影距离(角平分线左右两边的端点到角平分线的距离不相等,垂足也无法确定),所以所算的误差非常大。
  183. let center = getBoxPos(info)
  184. let oriX, oriY
  185. if(info.predictSize){
  186. x = oriX = info.predictSize.x, y = oriY = info.predictSize.y
  187. }else{
  188. if (info.box1) {
  189. let x1 = info.boxposes[0].camTangent.x,
  190. x2 = info.boxposes[1].camTangent.x,
  191. y1 = info.boxposes[0].camTangent.y,
  192. y2 = info.boxposes[1].camTangent.y,
  193. w1 = info.boxposes[0].projectWidth, //Math.max(minProjectWidth1, this.boxposes[0].projectWidth),
  194. w2 = info.boxposes[1].projectWidth //Math.max(minProjectWidth2, this.boxposes[1].projectWidth)
  195. //如果识别到柜门上,(柜体被遮住了),整体中心就会在柜门上,且厚度小于真实值。
  196. if (x1 == 0) {
  197. y = w1
  198. x = (w2 - y2 * y) / x2
  199. } else {//联立方程得:
  200. y = (w2 - (x2 / x1) * w1) / (y2 - (x2 / x1) * y1)
  201. x = (w1 - y1 * y) / x1
  202. }
  203. //console.log('xy', { x, y })
  204. ;(x < 0.3 || x > 1.4) && (exStr += ' x:' + math.toPrecision(x, 2))
  205. ;(y < 0.3 || y > 1.4) && (exStr += ' y:' + math.toPrecision(y, 2))
  206. if (y < 0 || x < 0) {
  207. //console.log('<0 ?????????')
  208. warnStr = x < 0 ? 'x<0!' : 'y<0!'
  209. }
  210. ;(oriX = x), (oriY = y)
  211. } else {
  212. //single pano data
  213. x = oriX = info.boxposes[0].maxX //姑且取最大值
  214. y = oriY = info.boxposes[0].maxY
  215. }
  216. let o = restrictSize(x, y, info)
  217. ;(x = o.x), (y = o.y)
  218. //按正常来说,得到的x,y都应>0,但是由于箱子会被遮挡,导致投影宽度比真实的小,算出的也不准,可能是负数
  219. //所以手动将过小的宽度矫正
  220. }
  221. /* if(info.name == "pano2-6"){
  222. console.log(7)
  223. } */
  224. let height
  225. if (info.boxType == 'air-hanging') {
  226. //挂式空调最好把长宽固定。 不过极少出错
  227. //center.y -= 0.1 //很可能过高
  228. height = standards[info.boxType].height.standard
  229. getBoxBottom(info)
  230. let d = center.y - info.btmPos.y
  231. center.y -= THREE.MathUtils.clamp((d - height/2)/2,-0.1,0.1) //如果中心点到底部的距离和height的一半不同,中心点移动差值的一半
  232. } else {
  233. if (!info.topPos) getBoxTop(info)
  234. let {min,max} = standards[info.boxType].height
  235. height = THREE.MathUtils.clamp(info.topPos.y - groundY, min, max)
  236. }
  237. info.size = new THREE.Vector3(x, height, y)
  238. info.sizeAdjust = Math.pow(x - oriX, 2) + Math.pow(y - oriY, 2) //计算得到的值和标准值之间的差距,可以反映该info的匹配分值
  239. if (info.sizeAdjust) info.score = (info.score || 0) - Math.min(info.sizeAdjust * 200, 800)
  240. info.size.oriX = oriX, info.size.oriY = oriY
  241. /* if (info.name == 'pano6-0&pano4-4') {
  242. console.log(6)
  243. } */
  244. ;(info.exStr = exStr), (info.warnStr = warnStr)
  245. }
  246. let restrictSize = (x, y, info) => {
  247. let s
  248. if (info.boxType == 'battery' || info.boxType == 'air-hanging' || info.boxType == 'air') {
  249. let { xProp, yProp } = info
  250. if(xProp != void 0){
  251. var { min, max } = standards[info.boxType][xProp]
  252. x = THREE.MathUtils.clamp(x, min, max)
  253. var { min, max } = standards[info.boxType][yProp]
  254. y = THREE.MathUtils.clamp(y, min, max)
  255. s = true
  256. }
  257. }
  258. if(!s){
  259. var { min, max } = standards[info.boxType].widthNormal
  260. x = THREE.MathUtils.clamp(x, min, max)
  261. y = THREE.MathUtils.clamp(y, min, max)
  262. if(info.boxposes){
  263. x = Math.min(x, ...info.boxposes.map(e=>e.maxX))
  264. y = Math.min(y, ...info.boxposes.map(e=>e.maxY))
  265. }
  266. }
  267. return { x, y }
  268. }
  269. let getMixBox = (box0, box1) => {
  270. //重叠部分
  271. let box = new THREE.Box2()
  272. box.min.set(Math.max(box0.min.x, box1.min.x), Math.max(box0.min.y, box1.min.y))
  273. box.max.set(Math.min(box0.max.x, box1.max.x), Math.min(box0.max.y, box1.max.y))
  274. return box
  275. }
  276. const axises = [new THREE.Vector3(-1,1,-1),new THREE.Vector3(1,1,-1),new THREE.Vector3(1,1,1),new THREE.Vector3(-1,1,1),
  277. new THREE.Vector3(-1,-1,-1),new THREE.Vector3(1,-1,-1),new THREE.Vector3(1,-1,1),new THREE.Vector3(-1,-1,1)]
  278. class Box {//结果
  279. constructor(info) {
  280. //preDealBox(info)
  281. this.setFromInfo(info)
  282. if(version == 'vision') this.draw()
  283. boxes.push(this)
  284. }
  285. setFromInfo(info) {
  286. for (let i in info) {
  287. this[i] = info[i]
  288. }
  289. let h = info.size.y
  290. let standardH = standards[info.boxType].height.standard
  291. if (h > standardH) {
  292. h = standardH + Math.log(1 + (h - standardH) / 2) //Math.log2: 以2为底的对数 ,Math.log:自然对数
  293. info.size.y = h
  294. }
  295. this.name = this.boxType + '-' + this.name
  296. let position
  297. let center = getBoxPos(this)
  298. if (this.boxType == 'air-hanging') {
  299. position = center
  300. } else {
  301. position = center.clone().setY(groundY + this.size.y / 2) //使着地
  302. }
  303. this.position = position
  304. let bound = new THREE.Box3()
  305. bound.expandByPoint(position)
  306. bound.expandByVector(new THREE.Vector3(this.size.x / 2, this.size.y / 2, this.size.z / 2))
  307. this.bound = bound
  308. }
  309. draw() {
  310. hue += 0.06
  311. var color = new THREE.Color().setHSL(hue, 1, 0.75)
  312. this.boxHelper = new THREE.Box3Helper(this.bound, color)
  313. this.boxHelper.material.depthTest = false
  314. this.boxHelper.material.transparent = true
  315. this.boxHelper.renderOrder = 30
  316. let { warnStr, exStr } = this
  317. warnStr && (exStr += `【${warnStr}】`)
  318. this.label = addLabel(this.position, exStr ? [this.name, exStr] : this.name, { bgcolor: color })
  319. meshGroup.add(this.boxHelper)
  320. }
  321. dispose() {
  322. let index = boxes.indexOf(this)
  323. if (index > -1) {
  324. boxes.splice(index, 1)
  325. if(version == 'vision'){
  326. this.label.sprite.material.opacity = 0.2
  327. this.boxHelper.material.opacity = 0.1
  328. }
  329. }
  330. }
  331. toJson(){
  332. let json = {
  333. points: axises.map(axis=>new THREE.Vector3().addVectors(this.position,this.size.clone().multiply(axis).multiplyScalar(0.5)).toArray()),
  334. category:this.boxType,
  335. sid: this.name
  336. }
  337. return json
  338. }
  339. }
  340. class cabinet extends Box {
  341. constructor() {
  342. super()
  343. }
  344. }
  345. // 2d坐标转3d坐标
  346. let getDirByUV = (uv, pano) => {
  347. // 计算方向向量
  348. let yaw = -uv.x * (Math.PI * 2)
  349. let pitch = Math.PI / 2 - uv.y * Math.PI
  350. let dir = new THREE.Vector3()
  351. dir.copy(Vectors.RIGHT).applyAxisAngle(Vectors.BACK, pitch).applyAxisAngle(Vectors.UP, yaw).applyQuaternion(pano.quaternion)
  352. return dir
  353. }
  354. let getCenterDir = box => {
  355. if (box.centerDir) return
  356. //假设不存在在box中间拍摄的情况,所以y不会横跨两边
  357. let bbox = box.bbox2
  358. let center = { x: getBbox2center(bbox[0], bbox[2]), y: (bbox[1] + bbox[3]) / 2 }
  359. box.bbox2CenterX = center.x
  360. let dir = getDirByUV(center, box.pano)
  361. box.centerDir = dir
  362. let centerTop = { x: center.x, y: bbox[1] }
  363. box.centerTopDir = getDirByUV(centerTop, box.pano)
  364. let centerBtm = { x: center.x, y: bbox[3] }
  365. box.centerBtmDir = getDirByUV(centerBtm, box.pano)
  366. if(box.sid == 'pano0-6'){
  367. console.log(5)
  368. }
  369. if (!box.btmPosPredict) {
  370. ray.set(box.pano.position, box.centerBtmDir)
  371. /*box.btmPosPredict = ray.ray.intersectPlane(groundPlane, new THREE.Vector3()) //没有的话就在空中 (部分air-hanging也会有)
  372. if(box.btmPosPredict){
  373. const depth = 0.4
  374. let dir2d = new THREE.Vector2(box.centerBtmDir.x, box.centerBtmDir.z).normalize().multiplyScalar(depth)
  375. box.btmPosPredict.x += dir2d.x
  376. box.btmPosPredict.z += dir2d.y
  377. box.btmPosPredict.clamp(safeBound.min, safeBound.max)
  378. } */
  379. //if(box.category == 'air'){
  380. let o = ray.intersectObjects([skyBoxTight]) //如果skybound有问题,位置就会错
  381. box.btmPosPredict/* btmPosAtWall */ = o[0].point
  382. const depth = Math.abs(o[0].face.normal.y) > 0.9 ? 0.4 : -0.4
  383. //前进距离。交点在地面上的话, btm往往相比中心更靠近pano;而在墙上则需要移近
  384. let dir2d = new THREE.Vector2(box.centerBtmDir.x, box.centerBtmDir.z).normalize().multiplyScalar(depth)
  385. box.btmPosPredict.x += dir2d.x
  386. box.btmPosPredict.z += dir2d.y
  387. //}
  388. //box.pano.id == '4' && addLabel(box.btmPosPredict, 'btmPre:' + box.sid, { bgcolor: '#e00', a:0.4 })
  389. /* if (box.sid == "pano10-6"|| box.sid == "pano8-1") {
  390. console.log(1)
  391. let color = '#fe1'
  392. var line1 = LineDraw.createLine([box.pano.position, box.pano.position.clone().add(box.centerBtmDir.clone().multiplyScalar(40))], { color })
  393. meshGroup.add(line1)
  394. addLabel(box.btmPosPredict, 'btmPre')
  395. } */
  396. }
  397. if (!box.posAtWall && (box.category == 'battery' || box.category == 'air')) {
  398. const shrink = 0.4
  399. ray.set(box.pano.position, box.centerDir)
  400. let o = ray.intersectObjects([skyBoxTight])
  401. box.posAtWall = new THREE.Vector3().addVectors(box.pano.position, box.centerDir.clone().multiplyScalar(o[0].distance - shrink))
  402. //因墙壁不准确,所以还是尽量不用墙的位置
  403. /* if(box.btmPosPredict){
  404. let wallRatio = 0.5;
  405. if(new THREE.Vector3().subVectors(box.btmPosPredict, box.posAtWall).setY(0).length() > 1 )wallRatio = 0.2 //可能墙壁位置不准,靠后了
  406. box.predictCenter = new THREE.Vector3().addVectors(box.btmPosPredict.clone().multiplyScalar(1-wallRatio), box.posAtWall.clone().multiplyScalar(wallRatio))
  407. //box.predictCenter = new THREE.Vector3().addVectors(box.posAtWall, box.btmPosPredict).multiplyScalar(0.5) //也许能当中心点? 虽然y会低一些
  408. addLabel(box.predictCenter, box.sid+'-preC')
  409. } */
  410. }
  411. updateBoxType(box)
  412. }
  413. let getUVs = box => {
  414. if (box.bbox2) return
  415. let uvs = []
  416. box.bbox2 = box.bbox.map((e, i) => {
  417. return i % 2 == 0 ? e / 4096 /* + 0.25 */ : e / 2048
  418. })
  419. }
  420. let getBoxBase = box => {
  421. getUVs(box)
  422. getCenterDir(box)
  423. }
  424. let getBbox2Diff = (x1, x2) => {//获取x1-x2,如果x1在x2右边则为正
  425. if (Math.abs(x1 - x2) < 0.5) return x1 - x2
  426. else {
  427. if (x1 > x2) x1 -= 1
  428. else x2 -= 1
  429. return x1 - x2
  430. }
  431. }
  432. let getBbox2center = (x1, x2) => {
  433. //找中间位置
  434. if (Math.abs(x1 - x2) > 0.5) {
  435. //永远找小于180度的那一边
  436. return (x1 + x2 + 1) / 2 //另外半边
  437. } else {
  438. return (x1 + x2) / 2
  439. }
  440. }
  441. let getBoxPoseByPos = (box, centerPos, addDis = 0) => {
  442. //当得知box的大概位置时,求box在这个角度上的宽度、朝向
  443. //在这个方向看的box的宽度
  444. let angle = getBbox2Diff(box.bbox2[2], box.bbox2[0]) * Math.PI //角度的一半
  445. let dis = new THREE.Vector3().subVectors(box.pano.position, centerPos).setY(0).length() + addDis
  446. let projectWidth = 2 * Math.tan(angle) * dis //投影宽度 (准确的投影宽度无法求得,只能近似)
  447. let camDir = /* new THREE.Vector2(centerPos.x-box.pano.position.x, centerPos.z-box.pano.position.z).normalize() */ box.centerDir.clone().setY(0).normalize()
  448. let camTangent = math.getNormal({
  449. points: [
  450. { x: 0, y: 0 },
  451. /* camDir */ { x: camDir.x, y: camDir.z },
  452. ],
  453. }) //视线切线方向
  454. camTangent.x = Math.abs(camTangent.x)
  455. camTangent.y = Math.abs(camTangent.y)
  456. let maxWidth = standards[box.type].widthNormal.max
  457. let minWidth = standards[box.type].widthNormal.min
  458. let maxProjectWidth = (camTangent.x + camTangent.y) * maxWidth //该角度下该类型允许的最大投影距离
  459. let minProjectWidth = (camTangent.x + camTangent.y) * minWidth
  460. let maxX = projectWidth / camTangent.x //可得x的最大值
  461. let maxY = projectWidth / camTangent.y //可得y的最大值
  462. return { projectWidth, camTangent, maxProjectWidth, minProjectWidth, maxX, maxY }
  463. }
  464. let getBoxTop = info => {
  465. if(info.box1){
  466. let o2 = getIntersect2(info.box0.pano.position, info.box0.centerTopDir, info.box1.pano.position, info.box1.centerTopDir)
  467. info.topPos = o2.pos3d
  468. info.diffHeight = o2.mid2 ? o2.mid2.distanceTo(o2.mid1) : 1
  469. }else{
  470. //取btm上方对应的位置 ( 因为和skybox的交点会因离墙远而偏上或偏下)
  471. let btm = info.box0.btmPosPredict || info.btmPos //top.x
  472. if(!btm){
  473. btm = getBoxBottom(info)
  474. }
  475. info.topPos = btm.clone() //xz同btm,要求y
  476. let xDelta = btm.x - info.box0.pano.position.x
  477. let yDelta = xDelta * info.box0.centerTopDir.y / info.box0.centerTopDir.x
  478. info.topPos.y = yDelta + info.box0.pano.position.y
  479. }
  480. return info.topPos
  481. }
  482. let getBoxBottom = info => {
  483. if (info.box1) {
  484. let o2 = getIntersect2(info.box0.pano.position, info.box0.centerBtmDir, info.box1.pano.position, info.box1.centerBtmDir)
  485. info.btmPos = o2.pos3d
  486. } else {
  487. ray.set(info.box0.pano.position, info.box0.centerBtmDir)
  488. let o = ray.intersectObjects([skyBoxTight])
  489. info.btmPos = o[0].point.clone()
  490. }
  491. return info.btmPos
  492. }
  493. let getIntersect = (pano0Pos, dir0, pano1Pos, dir1) => {
  494. let pos0 = new THREE.Vector3().addVectors(pano0Pos, dir0)
  495. let pos1 = new THREE.Vector3().addVectors(pano1Pos, dir1)
  496. /* var {pos3d, mid1, mid2, behind} = math.getLineIntersect({ A: pano0Pos.clone(), B: pano1Pos.clone(), p1: pos0, p2: pos1 })
  497. */ //三维线若接近平行,算出的交点可能很近,不是实际应该无交点才对
  498. var pos2d = math.isLineIntersect(
  499. [
  500. { x: pano0Pos.x, y: pano0Pos.z },
  501. { x: pos0.x, y: pos0.z },
  502. ],
  503. [
  504. { x: pano1Pos.x, y: pano1Pos.z },
  505. { x: pos1.x, y: pos1.z },
  506. ],
  507. true
  508. ) //优先考虑水平面方向的交点
  509. if (pos2d) {
  510. let y0 = ((pos2d.x - pano0Pos.x) * dir0.y) / dir0.x + pano0Pos.y
  511. let y1 = ((pos2d.x - pano1Pos.x) * dir1.y) / dir1.x + pano1Pos.y
  512. //console.log(y1-y0)
  513. let pos3d = new THREE.Vector3(pos2d.x, (y0 + y1) / 2, pos2d.y)
  514. return { pos3d, diffHeight: Math.abs(y0 - y1) } //diffHeight越小越好
  515. }
  516. }
  517. //究竟哪个比较准 - - 可能两个都判断?
  518. let getIntersect2 = (pano0Pos, dir0, pano1Pos, dir1) => {
  519. let pos0 = new THREE.Vector3().addVectors(pano0Pos, dir0)
  520. let pos1 = new THREE.Vector3().addVectors(pano1Pos, dir1)
  521. let o = math.getLineIntersect2({ A: pano0Pos.clone(), B: pano1Pos.clone(), p1: pos0, p2: pos1, dir0, dir1 })//不用getLineIntersect,因为这个针对热点写的,当无交点时选用的点不是想要的
  522. if (!o.pos3d) {
  523. console.error('getIntersect2 no result? ?')
  524. }
  525. return o
  526. }
  527. let searchPair = (beginItem, group0_, group1_, parentPairs, resultPairs) => {
  528. let pair = [] //只能是来自group0的
  529. if (parentPairs) {
  530. //元结点裂变出多个,来装新的pair
  531. let i = resultPairs.indexOf(parentPairs)
  532. resultPairs.splice(i, 1)
  533. }
  534. for (let j = 0; j < group1_.length; j++) {
  535. pair = [beginItem, group1_[j]]
  536. let newPairs //用来存放该组pair
  537. if (parentPairs) {
  538. newPairs = parentPairs.slice(0) //复制
  539. newPairs.push(pair)
  540. } else {
  541. newPairs = [pair] //新的容器
  542. }
  543. resultPairs.push(newPairs)
  544. let newGroup0 = group0_.slice(0)
  545. let newGroup1 = group1_.slice(0)
  546. let index = newGroup0.indexOf(pair[0])
  547. newGroup0.splice(index, 1)
  548. index = newGroup1.indexOf(pair[1])
  549. newGroup1.splice(index, 1)
  550. if (newGroup0.length > 0 && newGroup1.length > 0) {
  551. searchPair(newGroup0[0], newGroup0, newGroup1, newPairs, resultPairs)
  552. }
  553. }
  554. }
  555. let getLeftRight = boxArr => {//获取pano的boxes中最左和最右的bbox.x
  556. let lefts = boxArr.map(e => e.bbox2[0])
  557. let rights = boxArr.map(e => e.bbox2[2])
  558. lefts.sort((a, b) => getBbox2Diff(a, b))
  559. rights.sort((a, b) => getBbox2Diff(b, a))
  560. let leftX = lefts[0] //最左
  561. let rightX = rights[0] //最右
  562. return {
  563. leftX,
  564. rightX,
  565. }
  566. }
  567. export default class PanoBoxFrame extends THREE.Group {
  568. constructor(player_, ifAnalyze, dataList) {
  569. super()
  570. player = player_
  571. player.model.add(this)
  572. this.ifAnalyze = ifAnalyze
  573. this.wireframes = new THREE.Object3D()
  574. this.wireframes.name = 'wireframes'
  575. this.add(this.wireframes)
  576. this.matchScoreMap = {}
  577. this.bindEvents()
  578. meshGroup = new THREE.Object3D()
  579. meshGroup.name = 'testBox'
  580. this.add(meshGroup)
  581. skyBoxTight = new BoundingMesh(player.model.boundingBox, undefined, 0)
  582. skyBoxTight.material.side = 2
  583. skyBoxTight.material.wireframe = true
  584. skyBoxTight.visible = false
  585. skyBoxTight.updateMatrixWorld()//不update的话raycaster是错的
  586. this.skyBoxTight = skyBoxTight
  587. this.compute(dataList)
  588. }
  589. async compute(dataList) {
  590. this.datas = {}
  591. this.boxes = boxes
  592. let compu = 0
  593. let beginCompute = () => {
  594. //获取匹配分数
  595. let getMatchScore = (box0, box1, { isSingle, center, onlyGet, dontCheckDis } = {}) => {
  596. let name = box0.sid + '&' + box1.sid
  597. let matchInfo = this.matchScoreMap[box1.category][name]
  598. if (onlyGet) return matchInfo
  599. if (!matchInfo) {
  600. matchInfo = { name, box0, box1, center }
  601. this.matchScoreMap[box1.category][name] = matchInfo
  602. }else{
  603. return matchInfo
  604. }
  605. if (name == 'pano0-3&pano4-4') {
  606. console.log(1)
  607. }
  608. getBoxBase(box0)
  609. getBoxBase(box1)
  610. let A = box0.pano.position.clone()
  611. let B = box1.pano.position.clone()
  612. let AB = new THREE.Vector3().subVectors(B, A)
  613. let AB2d = new THREE.Vector2(AB.x, AB.z).normalize()
  614. let AP12d = center ? new THREE.Vector2(center.x - A.x, center.z - A.z).normalize() : new THREE.Vector2(box0.centerDir.x, box0.centerDir.z).normalize()
  615. let BP22d = center ? new THREE.Vector2(center.x - B.x, center.z - B.z).normalize() : new THREE.Vector2(box1.centerDir.x, box1.centerDir.z).normalize()
  616. let angleA = Math.acos(AB2d.dot(AP12d))
  617. let angleB = Math.PI - Math.acos(AB2d.dot(BP22d))
  618. let score = 100,
  619. str = []
  620. if (name == 'pano2-1&pano4-2') {
  621. console.log(1)
  622. var line1 = LineDraw.createLine([A, A.clone().add(box0.centerDir.clone().multiplyScalar(40))])
  623. var line2 = LineDraw.createLine([B, B.clone().add(box1.centerDir.clone().multiplyScalar(40))])
  624. meshGroup.add(line1)
  625. meshGroup.add(line2)
  626. }
  627. if (angleA + angleB > Math.PI + 0.2) {//无交点(比180大是因为中心角度有误差,所以给一定的容错)
  628. //console.log(`${panoId0}的第${box0.index}个与${panoId1}的第${box1.index}个因角度大于180度 不匹配`)
  629. return Object.assign(matchInfo, { score: -5000, str: ['angle>180'] })
  630. }
  631. if (box0.type != box1.type) {
  632. return Object.assign(matchInfo, { score: -5000, str: ['typeNotSame'] })
  633. }
  634. if (matchInfo.dirAngleXZ == void 0) {
  635. matchInfo.dirAngleXZ = THREE.MathUtils.radToDeg(Math.acos(AP12d.dot(BP22d))) //需要尽量接近90度算出来的交点会比较准
  636. matchInfo.minAng = Math.min(180 - matchInfo.dirAngleXZ, matchInfo.dirAngleXZ)
  637. if (isSingle) {
  638. //单个匹配单个,而非多个匹配多个,所以可以直接寻找最优角度
  639. score += Math.sin(THREE.MathUtils.degToRad(matchInfo.minAng)) * 500
  640. }
  641. }
  642. if(!dontCheckDis){
  643. if(box0.type != 'air-hanging' && box0.btmPosPredict && box1.btmPosPredict) { //注:挂空调不应使用btmPosPredict
  644. let d = box0.btmPosPredict.distanceToSquared(box1.btmPosPredict)
  645. matchInfo.btmPosPreDis = d
  646. score -= d * 2000
  647. } else if (box0.posAtWall && box1.posAtWall) {
  648. let d = box0.posAtWall.distanceToSquared(box1.posAtWall)
  649. matchInfo.wallPosPreDis = d
  650. score -= d * 500 //墙面不准所以分低
  651. }
  652. }
  653. //检查从两处看高度是否一致
  654. getBoxTop(matchInfo)
  655. score -= Math.pow(matchInfo.diffHeight * 10, 2) // 0.1处是1
  656. //检查高度的一半(从 顶部到中心 与 中心到地板 的距离是否差别太大)
  657. /* let diffHalfHeight
  658. if (box0.type == 'air-hanging') {
  659. //diffHalfHeight = 0
  660. //if(matchInfo.topPos.y - center.y > 0.5)score -= Math.pow((matchInfo.topPos.y - center.y )*5, 4)
  661. } else {
  662. diffHalfHeight = Math.abs(matchInfo.topPos.y - matchInfo.center.y - (matchInfo.center.y - groundY))
  663. score -= Math.pow(diffHalfHeight * 3, 4)
  664. } */
  665. if (!matchInfo.center) {
  666. let o = getIntersect2(A, box0.centerDir, B, box1.centerDir)
  667. matchInfo.center = o.pos3d.clone()
  668. //addLabel(matchInfo.center, matchInfo.name + '-c')
  669. let dir0 = new THREE.Vector3().subVectors(o.pos3d, A).normalize()
  670. let dir1 = new THREE.Vector3().subVectors(o.pos3d, B).normalize()
  671. let sum = dir0.dot(box0.centerDir) + dir1.dot(box1.centerDir)
  672. let wrongDir = sum < 1.9
  673. score -= (2 - sum) * 5000
  674. if (wrongDir) {
  675. str.push('wrongDir')
  676. return Object.assign(matchInfo, { score: score - 5000, str })
  677. }
  678. //靠墙的有这些属性 可以帮助预测中心点
  679. let predict0 = box0.type != 'air-hanging' && box0.btmPosPredict || matchInfo.topPos
  680. let predict1 = box1.type != 'air-hanging' && box1.btmPosPredict || matchInfo.topPos
  681. if (predict0 && predict1) {
  682. matchInfo.center
  683. .add(predict0)
  684. .add(predict1)
  685. .multiplyScalar(1 / 3)
  686. .setY(o.pos3d.y)
  687. }
  688. //addLabel(matchInfo.center, matchInfo.name + '-c')
  689. }
  690. updateBoxType(matchInfo)
  691. if(matchInfo.boxType != box0.type || matchInfo.boxType != box1.type){
  692. score -= 1000, str.push('typeNotSame2')
  693. }
  694. if (!safeBound.containsPoint(matchInfo.center)) {
  695. let dis = safeBound.distanceToPoint(matchInfo.center)
  696. //console.log(`${panoId0}的第${box0.index}个与${panoId1}的第${box1.index}个因中心点在bounding外不匹配`, center, 'dis: ' + dis)
  697. ;(score -= 1000 * dis * dis), (str.push('outsideBound'))
  698. Object.assign(matchInfo, { score, str, center: matchInfo.center, disToBound: dis })
  699. if (dis > 0.5) return matchInfo
  700. }
  701. /* if (name == 'pano6-rowBigBox-3,2&pano2-0') {
  702. addLabel(matchInfo.center, 'center')
  703. addLabel(matchInfo.topPos, 'topCenter')
  704. } */
  705. /* if (name == 'pano2-rowBigBox-5,1,8&pano6-rowBigBox-4') {
  706. console.log(1)
  707. let color = '#fe1'
  708. var line1 = LineDraw.createLine([A, A.clone().add(box0.centerTopDir.clone().multiplyScalar(40))], { color })
  709. var line2 = LineDraw.createLine([B, B.clone().add(box1.centerTopDir.clone().multiplyScalar(40))], { color })
  710. meshGroup.add(line1)
  711. meshGroup.add(line2)
  712. addLabel(matchInfo.topPos, 'topPos')
  713. } */
  714. //检查宽度
  715. let boxposes
  716. let checkWidth = () => {
  717. boxposes = []
  718. ;[box0, box1].forEach(box => {
  719. let pose = getBoxPoseByPos(box, matchInfo.center)
  720. boxposes.push(pose)
  721. //如果超出标准,基本上这二者不匹配;但过小的话,有可能是被遮挡所以残缺,因此不予过滤
  722. })
  723. }
  724. checkWidth()
  725. /* let overMaxs = boxposes.filter(e => e.projectWidth > e.maxProjectWidth)
  726. if (
  727. // matchInfo.dirAngleXZ > 160 &&
  728. (overMaxs.length == 1 && Math.abs(boxposes[0].maxProjectWidth - boxposes[0].projectWidth - (boxposes[1].maxProjectWidth - boxposes[1].projectWidth)) > 0.8) ||
  729. (matchInfo.dirAngleXZ < 20 && overMaxs.length == 2)
  730. ) {
  731. //一大一小
  732. //中心偏向一点,如果能移回一些就好了,现在暂时先用topPos的xz。 因为向上的话,侧面角度大,水平角度差影响会小。但如果有一边不准也是不准。
  733. ;(matchInfo.center.x = matchInfo.topPos.x), (matchInfo.center.z = matchInfo.topPos.z) //y要变吗
  734. //console.log('修改中心')
  735. checkWidth()
  736. }*/
  737. score += getPoseScore(boxposes, isSingle) //根据投影信息预测的长度再得匹配分数
  738. compu++
  739. return Object.assign(matchInfo, { score: score, str, /* diffHalfHeight, */ boxposes })
  740. /* preDealBox(matchInfo)
  741. getBoxSize(matchInfo) */
  742. }
  743. this.rows = {}
  744. /* let getchainNext = (left, end, chain, boxes) => {
  745. chain.push(left)
  746. if (left == end) return boxes.chains.push(chain)
  747. let nodes = boxes.relationships.filter(pair => pair.includes(left))
  748. let rights = nodes.map(pair => pair.find(e => e != left))
  749. rights = rights.filter(e => !chain.includes(e) && boxes.indexOf(e) > boxes.indexOf(left))
  750. rights.forEach(right => {
  751. getchainNext(right, end, chain.slice(), boxes)
  752. })
  753. } */
  754. let getPanoBigRowBox = (panoBoxes, { reason='row' } = {}) => {//将一个pano中的所有boxes分组,识别哪些是一排的。也可用于识别融合
  755. let pano = panoBoxes[0].pano
  756. const category = panoBoxes[0].category
  757. let type = category+'|'+reason
  758. this.rows[type] || (this.rows[type] = {})
  759. if (this.rows[type][pano.id]) return this.rows[type][pano.id]
  760. let bigBoxes
  761. let bigBox = {
  762. sid: 'pano' + pano.id + (reason == 'mix' ? '-mix' : '-row'),
  763. pano,
  764. category: 'rowBigBox',
  765. type: 'rowBigBox',
  766. }
  767. let rows = []
  768. for (let i = 0; i < panoBoxes.length; i++) {
  769. let box0 = panoBoxes[i]
  770. getBoxBase(box0)
  771. let [left0, right0] = [box0.bbox2[0], box0.bbox2[2]]
  772. for (let j = i + 1; j < panoBoxes.length; j++) {
  773. let box1 = panoBoxes[j]
  774. getBoxBase(box1)
  775. if (box0.type != box0.type ) continue //类型不同
  776. let [left1, right1] = [box1.bbox2[0], box1.bbox2[2]]
  777. let atEdge = (left1 < 0.00025 && right0 > 0.9997) || (left0 < 0.00025 && right1 > 0.9997) //在全景图的边界必会分两半
  778. if (reason == 'mix' && box0.category == 'cabinet' && !atEdge) continue //柜子容易并排,尽量不融合
  779. let d1 = getBbox2Diff(left1, right0),
  780. d2 = getBbox2Diff(left1, left0),
  781. d3 = getBbox2Diff(left0, right1)
  782. /* if(box0.sid == "pano10-1"){
  783. console.log(9)
  784. } */
  785. const min = reason == 'mix' ? 0.004 : 0.003 //mix代表寻找分裂的重新融合到一起
  786. if ((d1 <= min && d2 >= min) || (d3 <= min && d2 <= min)) { //边框交接
  787. const { min, max } = standards[box0.btmPosPredict ? category : 'air-hanging'].widthNormal
  788. const w = (min + max) / 2
  789. const tolerate = w * w * (reason == 'mix' ? 0.8 : 2.1)
  790. let p0 = getBoxPos(box0)
  791. let p1 = getBoxPos(box1)
  792. let dis = p0.distanceToSquared(p1)
  793. let disY
  794. if (reason == 'mix'){
  795. if(atEdge) {
  796. dis -= tolerate * 0.8 //给many优势
  797. }else{
  798. disY = (Math.abs(box0.bbox2[3] - box1.bbox2[3]) + Math.abs(box0.bbox2[1] - box1.bbox2[1])) * 5 //边框纵向差异越大越不可能融合(虽然是可能有包含的情况啦,留到最后再combine。因为在侧面看,近处的box很容易嵌套远处的box,但它们不该融合)
  799. if (disY > 0.6) continue
  800. dis += disY
  801. let atEdgeMight = (left1 < 0.001 && right0 > 0.999) ? [left1,right0] : (left0 < 0.001 && right1 > 0.999) ? [left0,right1] : null //有在全景图的边界的可能性
  802. let atEdgePossib = atEdgeMight? 0.0003/ (atEdgeMight[0] + (1-atEdgeMight[1])) : 0 // 两条线越接近越可能融合
  803. dis -= atEdgePossib //给点点优势
  804. }
  805. }
  806. /* if(box0.sid == "pano2-0"){
  807. console.log('dis',dis,'tolerate',tolerate,[box0, box1],disY)
  808. } */
  809. if (dis < tolerate) {
  810. //console.log('dis',dis,'tolerate',tolerate,[box0, box1],disY)
  811. common.pushToGroupAuto([box0, box1], rows)
  812. }
  813. }
  814. }
  815. }
  816. //一排箱子的角度范围不可超过180度,因为不可能站在箱子上拍,所以超过的话肯定有边缘的不在这一排中。
  817. //可判断边缘箱子的是否角度偏大,一般中间的被遮挡所以偏小
  818. /* rows.slice(0).forEach(boxes => {
  819. //有的其实是分离的但也在边缘连上了,可以根据底部的差来判断,过大的很可能是分离的
  820. if (boxes.length >= 2) {
  821. let diffs = []
  822. for (let i = 0, j = boxes.length; i < j - 1; i++) {
  823. let box0 = boxes[i],
  824. box1 = boxes[i + 1]
  825. let p0 = box0.predictCenter || box0.btmPosPredict || box0.posAtWall
  826. let p1 = box1.predictCenter || box1.btmPosPredict || box1.posAtWall
  827. diffs.push(p0.distanceToSquared(p1))
  828. }
  829. let minW = standards[category].widthNormal.min
  830. if(!boxes[0].btmPosPredict)minW = standards['air-hanging'].widthNormal.min
  831. let mid = minW * minW
  832. let removes = []
  833. diffs.forEach((e, i) => {
  834. if (e > mid * 2) {
  835. removes.push([boxes[i], boxes[i + 1]])
  836. }
  837. })
  838. if (removes.length) {
  839. console.log(
  840. '去除错误row连接',
  841. removes.map(e => e.map(a => a.sid))
  842. )
  843. let { newGroups } = common.disconnectGroup(removes, rows)
  844. //if(newGroups.length>1){//分裂成多组了,重新计算
  845. // console.log(newGroups)
  846. //}
  847. }
  848. }
  849. }) */
  850. rows.forEach(boxes => {
  851. //从左到右排序
  852. boxes.sort((a, b) => {
  853. //但因有的box跨越到别的box区域,所以这个顺序不准确
  854. return getBbox2Diff(a.bbox2CenterX, b.bbox2CenterX)
  855. })
  856. })
  857. rows.sort((a, b) => {
  858. return b.length - a.length
  859. }) //箱子数量从大到小排序
  860. bigBoxes = rows.map((boxes, i) => {
  861. let { leftX, rightX } = getLeftRight(boxes) //最左
  862. let btmY = boxes.slice().sort((a, b) => a.bbox2[3] - b.bbox2[3])[0].bbox2[3]
  863. let topY = boxes.slice().sort((a, b) => b.bbox2[1] - a.bbox2[1])[0].bbox2[1]
  864. let rowBigBox = Object.assign({}, bigBox, {
  865. boxes,
  866. bbox2: [leftX, topY, rightX, btmY], //整排的bbox
  867. left: boxes.find(e => e.bbox2[0] == leftX),
  868. right: boxes.find(e => e.bbox2[2] == rightX),
  869. })
  870. let p0 = getBoxPos(rowBigBox.left)
  871. let p1 = getBoxPos(rowBigBox.right)
  872. let vec = new THREE.Vector2(p0.x - p1.x, p0.z - p1.z)
  873. rowBigBox.k = Math.abs(vec.x / vec.y)
  874. rowBigBox.predictLen = (rowBigBox.k > 1 ? Math.abs(vec.x) : Math.abs(vec.y)) + 0.6 //加入一个宽度
  875. /* if(boxes.length <= boxes.relationships.length){//多条链
  876. boxes.chains = []
  877. getchainNext(left,right,[], boxes )
  878. let aveAngle = (getBbox2Diff(left.bbox2[2], left.bbox2[0]) + getBbox2Diff(right.bbox2[2], right.bbox2[0]) ) / 2 -0.01 //首尾的angle平均数。但如果这两个不准那就导致整体出错了
  879. let middleAngle = getBbox2Diff(right.bbox2[0], left.bbox2[2])
  880. let counts = boxes.chains.map(e=>e.length)
  881. counts.sort((a,b)=>a-b)
  882. let min = counts[0],max = counts[counts.length-1]
  883. let r = [], cur = min;
  884. while(cur<=max){
  885. r.push({cur, diff:Math.abs((middleAngle / (cur-2) - aveAngle)}) //加 0.01是因为增加边缘
  886. cur++
  887. }
  888. r.sort((a,b)=>a.diff-b.diff)
  889. rowBigBox.predictBoxCount = r[0].cur
  890. //---------
  891. let goodCountChains = boxes.chains.filter(e=>e.length == rowBigBox.predictBoxCount)
  892. if(goodCountChains.length == 1) rowBigBox.bestChain = goodCountChains[0]
  893. else{
  894. goodCountChains = goodCountChains.map((chain,i)=>{
  895. let j = 1, diff=0 //中间的box的angle的方差
  896. while(j<rowBigBox.predictBoxCount){
  897. let angle = getBbox2Diff(chain[j].bbox2[2], chain[j].bbox2[0])
  898. diff += Math.pow(angle - aveAngle, 2)
  899. j++;
  900. }
  901. return {diff, chain}
  902. })
  903. goodCountChains.sort((a,b)=>a.diff-b.diff)
  904. rowBigBox.bestChain = goodCountChains[0].chain
  905. }
  906. console.log('getChains',boxes.chains, 'predictBoxCount',rowBigBox.predictBoxCount, r)
  907. } */
  908. return rowBigBox
  909. })
  910. if(reason != 'mix')panoBoxes.forEach(box => {
  911. //加入单个的
  912. if (!rows.some(row => row.includes(box))) {
  913. let boxBig = Object.assign({}, bigBox, {
  914. bbox2: box.bbox2,
  915. boxes: [box],
  916. left: box,
  917. right: box,
  918. })
  919. bigBoxes.push(boxBig)
  920. }
  921. })
  922. bigBoxes.forEach(bigBox => {
  923. bigBox.sid += '-' + bigBox.boxes.map(e => e.index).join(',')
  924. /* if (bigBox.sid == 'pano0-rowBigBox-1,0,2,4') {
  925. console.log(3)
  926. } */
  927. //取平均值
  928. /* if(bigBox.boxes[0].type == 'air-hanging'){
  929. bigBox.btmPosAtWall = bigBox.boxes.reduce((w, c) => w.add(c.btmPosAtWall), new THREE.Vector3()).multiplyScalar(1 / bigBox.boxes.length)
  930. }else */if (bigBox.boxes[0].btmPosPredict) {
  931. bigBox.btmPosPredict = bigBox.boxes.reduce((w, c) => w.add(c.btmPosPredict), new THREE.Vector3()).multiplyScalar(1 / bigBox.boxes.length)
  932. }
  933. if (bigBox.boxes[0].posAtWall) {
  934. bigBox.posAtWall = bigBox.boxes.reduce((w, c) => w.add(c.posAtWall), new THREE.Vector3()).multiplyScalar(1 / bigBox.boxes.length)
  935. }
  936. })
  937. this.rows[type][pano.id] = bigBoxes //当前pano的所有row
  938. return bigBoxes
  939. }
  940. /* let getPanoBoxAngleTrend = rowBox => {
  941. //顺时针方向该pano的box角度范围是越来越大还是越来越小
  942. let diffs = []
  943. let angles = rowBox.boxes.map(box => getBbox2Diff(box.bbox2[2], box.bbox2[0]))
  944. for (let i = 0, j = angles.length; i < j - 1; i++) {
  945. //得所有相邻之间的差
  946. let a0 = angles[i],
  947. a1 = angles[i + 1]
  948. diffs.push(a1 - a0)
  949. }
  950. diffs.sort((a, b) => a - b)
  951. return diffs[Math.floor(diffs.length / 2)] //中位数
  952. } */
  953. /* let getBoxCount = (rowBigBox)=>{
  954. return rowBigBox.predictBoxCount || rowBigBox.boxes.length
  955. } */
  956. let getReverseInfo = (rowBigBox0, rowBigBox1) => {//两个row的方向对应
  957. let reversed = false
  958. let lefts = [rowBigBox0.left, rowBigBox1.left]
  959. let rights = [rowBigBox0.right, rowBigBox1.right]
  960. let dis0 = lefts[0].btmPosPredict.distanceToSquared(lefts[1].btmPosPredict)
  961. let dis1 = rights[0].btmPosPredict.distanceToSquared(rights[1].btmPosPredict)
  962. let dis2 = lefts[0].btmPosPredict.distanceToSquared(rights[1].btmPosPredict)
  963. let dis3 = rights[0].btmPosPredict.distanceToSquared(lefts[1].btmPosPredict)
  964. let posLeft2, posRight2
  965. if (dis0 + dis1 > dis2 + dis3) {//距离近的代表是同一端
  966. reversed = true
  967. posLeft2 = new THREE.Vector3().addVectors(lefts[0].btmPosPredict, rights[1].btmPosPredict).multiplyScalar(0.5)
  968. posRight2 = new THREE.Vector3().addVectors(rights[0].btmPosPredict, lefts[1].btmPosPredict).multiplyScalar(0.5)
  969. } else {
  970. posLeft2 = new THREE.Vector3().addVectors(lefts[0].btmPosPredict, lefts[1].btmPosPredict).multiplyScalar(0.5)
  971. posRight2 = new THREE.Vector3().addVectors(rights[0].btmPosPredict, rights[1].btmPosPredict).multiplyScalar(0.5)
  972. }
  973. let vec = new THREE.Vector2(posLeft2.x - posRight2.x, posLeft2.z - posRight2.z)
  974. let k = Math.abs(vec.x / vec.y) //这个算斜率更准,但位置容易偏向一侧(可能用边缘的bbox算会好些?)
  975. return { reversed, k }
  976. }
  977. let searchByRow = (groups, datas) => { //先查找row,匹配row,再slice row的方法
  978. this.matchScoreMap['rowBigBox'] = {}
  979. let rowInfos = []
  980. let getCenter = (rowBigBox0, rowBigBox1, ignoreCountMatch) => {//获取row间的匹配信息
  981. //获取bigBox位置。由于一排的盒子比较长,中心方向误差大,所以采用先获取两边位置,再求中点的方法
  982. //if (rowBigBox0.boxes.length != rowBigBox1.boxes.length && !ignoreCountMatch) return //太难了,不算不一样的情况了
  983. if (rowBigBox0.boxes.length != rowBigBox1.boxes.length && !ignoreCountMatch) return
  984. //if (getBoxCount(rowBigBox0) != getBoxCount(rowBigBox1) && getBoxCount(rowBigBox0) != 1 && getBoxCount(rowBigBox1) != 1)return
  985. /*if (rowBigBox0.sid == 'pano6-rowBigBox-4,6' && rowBigBox1.sid == 'pano14-rowBigBox-0,1,4,8') {
  986. console.log(4)
  987. } */
  988. let rowInfo
  989. if (rowBigBox0.boxes.length > 1 && rowBigBox1.boxes.length > 1) {//多对多,可以求两端的位置
  990. let lefts = [rowBigBox0.left, rowBigBox1.left]
  991. let rights = [rowBigBox0.right, rowBigBox1.right]
  992. let leftInfo
  993. let rightInfo
  994. let info2 = getReverseInfo(rowBigBox0, rowBigBox1)
  995. let len0 = rowBigBox0.predictLen, //长度应该接近
  996. len1 = rowBigBox1.predictLen
  997. let overLen = Math.abs(len0 - len1) /* / (rowBigBox0.boxes.length + rowBigBox1.boxes.length) * 5 */
  998. if (overLen > 1) return console.error('overLen> 1', overLen, rowBigBox0.sid, '和', rowBigBox1.sid)
  999. if (info2.reversed) {
  1000. leftInfo = getMatchScore(lefts[0], rights[1], { isSingle: true })
  1001. rightInfo = getMatchScore(rights[0], lefts[1], { isSingle: true })
  1002. } else {
  1003. leftInfo = getMatchScore(lefts[0], lefts[1], { isSingle: true })
  1004. rightInfo = getMatchScore(rights[0], rights[1], { isSingle: true })
  1005. }
  1006. let posLeft = getBoxPos(leftInfo)
  1007. let posRight = getBoxPos(rightInfo)
  1008. if (!posLeft || !posRight || leftInfo.score < -4000 || rightInfo.score < -4000) return //漫游点重合、>180度会导致此问题
  1009. preDealBox(leftInfo)
  1010. //getBoxSize(leftInfo)
  1011. preDealBox(rightInfo)
  1012. //getBoxSize(rightInfo)
  1013. posLeft = getBoxPos(leftInfo)
  1014. posRight = getBoxPos(rightInfo)
  1015. //验证是否是垂直或水平
  1016. let vec = new THREE.Vector2(posLeft.x - posRight.x, posLeft.z - posRight.z)
  1017. let k = Math.abs(vec.x / vec.y)
  1018. if ((info2.k > 1 && k < 1) || (info2.k < 1 && k > 1)) {
  1019. return console.error('请检查!info2.k > 1 && k < 1 || info2.k < 1 && k > 1', rowBigBox0.sid, '和', rowBigBox1.sid) //绘制的方向错误,尺寸错误
  1020. }
  1021. /*
  1022. let trend0 = getPanoBoxAngleTrend(rowBigBox0)
  1023. let trend1 = getPanoBoxAngleTrend(rowBigBox1)
  1024. let judgeReverse = () => { //这个方法有时不准
  1025. let disLeftSquared0 = new THREE.Vector2(posLeft.x - rowBigBox0.pano.position.x, posLeft.z - rowBigBox0.pano.position.z).lengthSq()
  1026. let disRightSquared0 = new THREE.Vector2(posRight.x - rowBigBox0.pano.position.x, posRight.z - rowBigBox0.pano.position.z).lengthSq()
  1027. let a = trend0 * (disLeftSquared0 - disRightSquared0)
  1028. if (a < 0 && Math.abs(a) > 0.1) return true
  1029. let posLeft2 = reversed ? posRight : posLeft, //反向过的对第二个漫游点来说左右是反的
  1030. posRight2 = reversed ? posLeft : posRight
  1031. let disLeftSquared1 = new THREE.Vector2(posLeft2.x - rowBigBox1.pano.position.x, posLeft2.z - rowBigBox1.pano.position.z).lengthSq()
  1032. let disRightSquared1 = new THREE.Vector2(posRight2.x - rowBigBox1.pano.position.x, posRight2.z - rowBigBox1.pano.position.z).lengthSq()
  1033. let b = trend1 * (disLeftSquared1 - disRightSquared1)
  1034. if (b < 0 && Math.abs(b) > 0.1) return true
  1035. }
  1036. if (leftInfo.score < -2000 || rightInfo.score < -2000 || judgeReverse()) {
  1037. //反向试试
  1038. leftInfo = getMatchScore(lefts[0], rights[1], { isSingle: true })
  1039. rightInfo = getMatchScore(rights[0], lefts[1], { isSingle: true })
  1040. posLeft = getBoxPos(leftInfo)
  1041. posRight = getBoxPos(rightInfo)
  1042. reversed = true //rowBigBox1 反向了
  1043. }
  1044. if (leftInfo.score < -2000 || rightInfo.score < -2000 || judgeReverse()) {
  1045. return console.log('getCenter ;两个方向都不符合', rowBigBox0.sid, rowBigBox1.sid)
  1046. } */
  1047. /*const maxK = Math.max(0.6 / Math.sqrt(rowBigBox0.boxes.length), 0.2) // 最大斜率
  1048. if (k < maxK && k > 1 / maxK) {
  1049. return //console.log('放弃,斜率', k)
  1050. } */
  1051. //横的话,按x从小到大,竖的按z从小到大
  1052. if ((k < 1 && posLeft.z > posRight.z) || (k > 1 && posLeft.x > posRight.x)) {
  1053. let temp = posRight
  1054. ;(posRight = posLeft), (posLeft = temp)
  1055. }
  1056. addLabel(posLeft, 'left-' + rowBigBox0.pano.id + '&' + rowBigBox1.pano.id, { a: 0.1 })
  1057. addLabel(posRight, 'right-' + rowBigBox0.pano.id + '&' + rowBigBox1.pano.id, { a: 0.1 })
  1058. if (rowBigBox0.pano.id + '&' + rowBigBox1.pano.id == '22&26') {
  1059. console.log(777)
  1060. }
  1061. /* var line1 = LineDraw.createLine([posLeft, posRight])
  1062. meshGroup.add(line1) */
  1063. let center = new THREE.Vector3().addVectors(posLeft, posRight).multiplyScalar(0.5)
  1064. let match = getMatchScore(rowBigBox0, rowBigBox1, { isSingle: true, center }) //是否预先传送center ?
  1065. //rowInfo.minAngs = [leftInfo.minAng , rightInfo.minAng]
  1066. /* if(match.name == 'pano0-row-6,2,1,0&pano6-row-0,1,2,5'){
  1067. console.log(8)
  1068. } */
  1069. if (match.score + overLen * 1000 + leftInfo.score + rightInfo.score < -5000) {
  1070. return console.log('放弃,匹配分过低,可能不是一组', rowBigBox0.sid, '和', rowBigBox1.sid)
  1071. }
  1072. //console.log('getcenter', rowBigBox0.sid, '和', rowBigBox1.sid, overLen, match.score + overLen * 1000 + leftInfo.score + rightInfo.score)
  1073. rowInfo = {
  1074. rowBigBox0,
  1075. rowBigBox1,
  1076. match,
  1077. k,
  1078. posLeft,
  1079. posRight,
  1080. score: 500 + match.score,
  1081. reversed: info2.reversed,
  1082. }
  1083. } else {
  1084. if (rowBigBox0.boxes.length == 1 && rowBigBox1.boxes.length == 1) {
  1085. rowInfo = getMatchScore(rowBigBox0.boxes[0], rowBigBox1.boxes[0] /* , { isSingle: true } */) //直接匹配box
  1086. } else {
  1087. //一对多。getMatchScore计算误差大(长度越长中心误差越大、宽度计算也误差大)所以再写点限制。直接使用btm来预测长度和位置似乎更准
  1088. let mulBoxRow = rowBigBox0.boxes.length>1 ? rowBigBox0 : rowBigBox1
  1089. let singleBox = rowBigBox0.boxes.length==1 ? rowBigBox0 : rowBigBox1
  1090. /* if(rowBigBox0.sid == "pano12-row-3" && rowBigBox1.sid == "pano0-row-3,1,0" ){
  1091. console.log(5)
  1092. } */
  1093. rowInfo = getMatchScore(rowBigBox0, rowBigBox1, { dontCheckDis:true }) //一对多
  1094. /* if(rowInfo.name == "pano12-row-3&pano0-row-3,1,0"){
  1095. console.log(5)
  1096. } */
  1097. rowInfo.k = mulBoxRow.k
  1098. if(rowInfo.center){
  1099. rowInfo.center.add(getBoxPos(mulBoxRow)).multiplyScalar(0.5)
  1100. }
  1101. rowInfo.predictSize = rowInfo.k > 1 ? {x : mulBoxRow.predictLen, y: 0.6} : {y : mulBoxRow.predictLen, x: 0.6}
  1102. //单个的应该和多个的其中一端一样
  1103. let dis0 = getBoxPos(mulBoxRow.left).distanceToSquared(getBoxPos(singleBox))
  1104. let dis1 = getBoxPos(mulBoxRow.right).distanceToSquared(getBoxPos(singleBox))
  1105. let dis = Math.min(dis0, dis1)
  1106. rowInfo.score -= dis * 1000
  1107. }
  1108. }
  1109. rowInfos.push(rowInfo)
  1110. rowInfos[rowBigBox0.sid + '&' + rowBigBox1.sid] = rowInfo
  1111. return rowInfo
  1112. }
  1113. let matchGroups = []
  1114. let allRelations = []
  1115. let getK = info => {
  1116. let k
  1117. if (info.left) {
  1118. let vec = new THREE.Vector2(info.left.x - info.right.x, info.left.z - info.right.z)
  1119. k = Math.abs(vec.x / vec.y)
  1120. } else {
  1121. k = Math.abs(Math.max(info.size.x,0.6) / Math.max(info.size.z,0.6))
  1122. }
  1123. return k
  1124. }
  1125. let ignoreCountMatch = groups.filter(e => e.length > 1).length == 1
  1126. let match = searchType => {
  1127. if (searchType == 'second') ignoreCountMatch = true
  1128. for (let i = 0; i < groups.length - 1; i++) {
  1129. let rowBigBoxes_0 = getPanoBigRowBox(groups[i])
  1130. let pano0 = groups[i][0].pano
  1131. if (searchType == 'second') rowBigBoxes_0 = rowBigBoxes_0.filter(e => !matchGroups.some(u => u.includes(e)))
  1132. for (let j = i + 1; j < groups.length; j++) {
  1133. let rowBigBoxes_1 = getPanoBigRowBox(groups[j])
  1134. if (searchType == 'second') rowBigBoxes_1 = rowBigBoxes_1.filter(e => !matchGroups.some(u => u.includes(e)))
  1135. let pano1 = groups[j][0].pano
  1136. let resultPairs = []
  1137. let bigBoxes_0, bigBoxes_1
  1138. if (rowBigBoxes_0.length < rowBigBoxes_1.length) {
  1139. bigBoxes_0 = rowBigBoxes_1.slice()
  1140. bigBoxes_1 = rowBigBoxes_0.slice()
  1141. } else {
  1142. bigBoxes_0 = rowBigBoxes_0.slice()
  1143. bigBoxes_1 = rowBigBoxes_1.slice()
  1144. }
  1145. while (bigBoxes_1.length < bigBoxes_0.length) {
  1146. bigBoxes_1.push({ sid: 'void' }) //为了使排列正确,补个空,使左右两边个数相等,过后和void匹配的不会计算box
  1147. }
  1148. if (!bigBoxes_0[0]) continue
  1149. searchPair(bigBoxes_0[0], bigBoxes_0, bigBoxes_1, null, resultPairs)
  1150. resultPairs = resultPairs.map(pairs => {
  1151. let infos = pairs.map(pair => (pair.some(e => e.sid == 'void') ? null : getCenter(pair[0], pair[1], ignoreCountMatch)))
  1152. //infos.sort((a,b)=>{return a.score-b.score});
  1153. let score = infos.reduce((s, e) => {
  1154. return s + (e && e.score > -2000 ? e.score : 0) //只考虑组成功的分数
  1155. }, 0)
  1156. return {
  1157. pairs,
  1158. infos,
  1159. score,
  1160. name: pairs.map(pair => pair.map(item => item.sid).join(' & ')),
  1161. }
  1162. })
  1163. resultPairs.sort((a, b) => b.score - a.score)
  1164. /* if (resultPairs[0].name[0].includes('pano8') && resultPairs[0].name[0].includes('pano0')) {
  1165. console.log(111)
  1166. } */
  1167. resultPairs[0].pairs.forEach((pair, i) => {
  1168. let info = resultPairs[0].infos[i]
  1169. if (info && info.score > -2000) {
  1170. allRelations.push(info)
  1171. let items = pair.filter(e => e.sid != 'void')
  1172. common.pushToGroupAuto(items, matchGroups, null, atGroup => {
  1173. //需要朝向一致才行
  1174. if (!info.k) return true //(box识别的宽高识别不准所以不需要)
  1175. let onePair = atGroup.relationships[0]
  1176. let name = onePair[0].sid + '&' + onePair[1].sid
  1177. if (!rowInfos[name].k) return true //不过不应该有这种情况,否则匹配不到一起才对
  1178. if ((rowInfos[name].k < 1 && info.k < 1) || (rowInfos[name].k > 1 && info.k > 1)) {
  1179. return true
  1180. } else {
  1181. console.log('k不一致无法匹配', info, atGroup)
  1182. }
  1183. })
  1184. //根据目前的规则应该是有端点的和有端点的匹配,box和box匹配
  1185. }
  1186. })
  1187. //console.log(resultPairs[0])
  1188. }
  1189. }
  1190. }
  1191. match()
  1192. ignoreCountMatch || match('second') //再次将剩余的匹配一下,这次允许个数不同的row匹配
  1193. console.log('matchGroups', matchGroups)
  1194. //识别出来的多组,可能有重复的,因为box个数不同所以才没到一组
  1195. //整理一下,每个组整理出一个info,同时重新检查一下,挑去每组中和其他成员非常不同的
  1196. let groupInfo = []
  1197. let getGroupInfo = group => {
  1198. let left = new THREE.Vector3(),
  1199. right = new THREE.Vector3(),
  1200. pointsLen = 0
  1201. let bigBoxes = []
  1202. let info = {}
  1203. group.relationships.forEach(pair => {
  1204. let name = pair[0].sid + '&' + pair[1].sid
  1205. let matchInfo = rowInfos[name] //this.matchScoreMap["rowBigBox"][name] || this.matchScoreMap["cabinet"][name];
  1206. if (matchInfo.posLeft) {
  1207. left.add(matchInfo.posLeft), right.add(matchInfo.posRight), pointsLen++
  1208. } else {
  1209. bigBoxes.push(matchInfo)
  1210. preDealBox(matchInfo)
  1211. getBoxSize(matchInfo)
  1212. }
  1213. })
  1214. let index = groupInfo.length
  1215. if (pointsLen > 0) {
  1216. left.multiplyScalar(1 / pointsLen)
  1217. right.multiplyScalar(1 / pointsLen)
  1218. addLabel(left, 'Left' + index, { bgcolor: '#F00', a: 0.35 })
  1219. addLabel(right, 'Right' + index, { bgcolor: '#F00', a: 0.35 })
  1220. let center = new THREE.Vector3().addVectors(left, right).multiplyScalar(0.5)
  1221. addLabel(center, 'center' + index, { bgcolor: '#F00', a: 0.4 })
  1222. ;(info.left = left), (info.right = right), (info.center = center)
  1223. info.pointsLen = pointsLen
  1224. }
  1225. if (bigBoxes.length > 0) {
  1226. let getAve = bigBoxes => {
  1227. let center1 = new THREE.Vector3(),
  1228. size = new THREE.Vector3()
  1229. bigBoxes.forEach(box => {
  1230. let center0 = getBoxPos(box)
  1231. center1.add(center0)
  1232. size.add(box.size)
  1233. })
  1234. if (pointsLen > 0) {
  1235. let size0 = new THREE.Vector3(Math.abs(left.x - right.x), size.y, Math.abs(left.z - right.z))
  1236. size.add(size0.multiplyScalar(pointsLen)).multiplyScalar(1 / (pointsLen + bigBoxes.length))
  1237. center1.add(info.center.clone().multiplyScalar(pointsLen)).multiplyScalar(1 / (pointsLen + bigBoxes.length))
  1238. } else {
  1239. size.multiplyScalar(1 / bigBoxes.length)
  1240. center1.multiplyScalar(1 / bigBoxes.length)
  1241. }
  1242. return { center1, size }
  1243. }
  1244. let { center1, size } = getAve(bigBoxes)
  1245. //console.log(center1, size)
  1246. let getScores = (center, size) => {
  1247. //获得相对于center,size的差别分数
  1248. bigBoxes.forEach(box => {
  1249. box.sc = -box.center.distanceToSquared(center1) - size.distanceToSquared(box.size) * 0.5
  1250. })
  1251. }
  1252. getScores(center1, size)
  1253. bigBoxes.sort((a, b) => b.sc - a.sc)
  1254. let midItem = bigBoxes[Math.floor(bigBoxes.length / 2)] //中位数
  1255. getScores(midItem.center, midItem.size)
  1256. const minScore = -8
  1257. let removes = bigBoxes.filter(e => {
  1258. return e.sc < minScore
  1259. })
  1260. if (removes.length) {
  1261. let { newGroups } = common.disconnectGroup(
  1262. removes.map(e => [e.box0, e.box1]),
  1263. matchGroups
  1264. )
  1265. console.log('去除错误数据', removes)
  1266. if (newGroups.length > 1) {
  1267. //分裂成多组了,重新计算
  1268. newGroups.forEach(e => {
  1269. getGroupInfo(e)
  1270. })
  1271. return
  1272. }
  1273. bigBoxes = bigBoxes.filter(e => {
  1274. return e.sc >= minScore
  1275. })
  1276. }
  1277. if (bigBoxes.length) {
  1278. let o = getAve(bigBoxes) //again
  1279. ;(info.center = o.center1), (info.size = o.size)
  1280. }
  1281. }
  1282. info.k = getK(info)
  1283. info.bigBoxes = bigBoxes
  1284. info.group = group
  1285. groupInfo.push(info)
  1286. }
  1287. matchGroups.slice(0).forEach(group => {
  1288. getGroupInfo(group)
  1289. })
  1290. let getLength = c => {
  1291. //获取bigbox长度
  1292. return c.size ? (c.k > 1 ? c.size.x : c.size.z) : (c.k > 1 ? c.right.x - c.left.x : c.right.z - c.left.z + 0.6)
  1293. }
  1294. let getLeft = (group, k) => {
  1295. let dirAxis = (k || group.k) > 1 ? 'x' : 'z'
  1296. return group.left ? group.left[dirAxis] - 0.3 : group.center[dirAxis] - group.size[dirAxis] / 2 //left和right加减半个宽度
  1297. }
  1298. let getRight = (group, k) => {
  1299. let dirAxis = (k || group.k) > 1 ? 'x' : 'z'
  1300. return group.right ? group.right[dirAxis] + 0.3 : group.center[dirAxis] + group.size[dirAxis] / 2
  1301. }
  1302. //识别是否group之间有一样的, 去重
  1303. {
  1304. let realGroups = []
  1305. let getAveWidth = (infos, len) => {
  1306. let boxCounts = []
  1307. infos.forEach(e => {
  1308. boxCounts.push(...e.group.map(bigBox => bigBox.boxes.length))
  1309. })
  1310. /* infos.forEach(e => {
  1311. boxCounts.push(...e.group.relationships.map(pair =>
  1312. Math.max(pair[0].predictBoxCount || pair[0].boxes.length, pair[1].predictBoxCount || pair[1].boxes.length)))
  1313. }) //根据匹配到的来得个数 */
  1314. /* infos.forEach(e => {
  1315. e.group.relationships.forEach(pair => {
  1316. let counts = pair.map(e => e.boxes.length)
  1317. if (counts.filter(e => e == 1).length == 1) {
  1318. //如果是1和其他数字匹配,取数字大的。如1和3的话是3个箱子。
  1319. let c = counts.find(e => e != 1)
  1320. boxCounts.push(c)
  1321. } else {
  1322. boxCounts.push(...counts) //否则两个数字都取
  1323. }
  1324. })
  1325. }) //根据匹配到的来得个数 */
  1326. boxCounts.sort((a, b) => a - b)
  1327. /*let midCounts = [] //这一排的箱子个数中位数
  1328. midCounts.push(boxCounts[Math.floor(boxCounts.length / 2)])
  1329. if (boxCounts.length % 2 == 0) {
  1330. let a = boxCounts[Math.floor(boxCounts.length / 2) - 1]
  1331. let b = midCounts[0]
  1332. let c = b - 1
  1333. while (c >= a) {
  1334. midCounts.push(c)
  1335. c--
  1336. }
  1337. //加入a到b中所有的整数
  1338. } */
  1339. let midCounts = []
  1340. let r0=0.3, r1=0.7 //取中间这部分的算最适合的个数,结果不一定是中位数
  1341. boxCounts.slice(Math.floor(boxCounts.length*r0), Math.floor(boxCounts.length*r1)+1).forEach((c)=>{
  1342. if(!midCounts.includes(c)) midCounts.push(c)
  1343. })
  1344. let { min, max } = standards.cabinet.widthNormal
  1345. let standardW = (min+max)/2
  1346. let aveWs = midCounts.map(e => {
  1347. return { aveW: len / e, count: e }
  1348. })
  1349. aveWs.sort((a, b) => Math.abs(a.aveW - standardW) - Math.abs(b.aveW - standardW))
  1350. let aveW = aveWs[0].aveW
  1351. let count = aveWs[0].count
  1352. if (aveW > max || aveW < min) {
  1353. let w = THREE.MathUtils.clamp(aveW, min, max)
  1354. //console.warn(`box aveW宽度不太对,从${aveW}修改到${w}`)
  1355. aveW = w
  1356. }
  1357. return { aveW, count }
  1358. }
  1359. let getBox2 = (center, len, thick, k) => {
  1360. let box2 = new THREE.Box2()
  1361. box2.expandByPoint(new THREE.Vector2(center.x, center.z))
  1362. let sizeVec = k > 1 ? new THREE.Vector2(len / 2, thick / 2) : new THREE.Vector2(thick / 2, len / 2)
  1363. box2.expandByVector(sizeVec)
  1364. return box2
  1365. }
  1366. const standardW = 0.6 //两排之间最小距离
  1367. /* let getBoxLen = (group)=>{
  1368. if( group.bigBoxes.length == 0) return 0
  1369. let max = 0
  1370. group.bigBoxes.forEach(e=>{
  1371. max = Math.max(max, e.box0.boxes ? e.box0.boxes.length : 1 , e.box1.boxes ? e.box1.boxes.length : 1 )
  1372. })
  1373. return max
  1374. } */
  1375. for (let m = 0; m < groupInfo.length - 1; m++) {
  1376. let group0 = groupInfo[m]
  1377. for (let n = m + 1; n < groupInfo.length; n++) {
  1378. let group1 = groupInfo[n]
  1379. /* if(group0.k == 5.135329840006164 && group1.k ==4.494597962707433 || group0.k ==4.494597962707433 && group1.k == 5.135329840006164){
  1380. console.log(4)
  1381. } */
  1382. if (group0.k == 4.242560016595383 || group1.k == 0.8571428571428572) {
  1383. console.log(2)
  1384. }
  1385. const maxR = 2.3
  1386. if (
  1387. ((group0.k > 1 && group1.k < 1) || (group0.k < 1 && group1.k > 1)) &&
  1388. //getBoxLen(group0) != 1 && getBoxLen(group1) != 1 )continue //且没有其中一个是单个的box
  1389. //(group0.k > maxR || group0.k < 1/maxR ) && (group1.k > maxR || group1.k < 1/maxR )) continue //如果是方块状的无视k
  1390. getLength(group0) > 1.5 &&
  1391. getLength(group1) > 1.5
  1392. )
  1393. continue //如果是方块状的无视k
  1394. //间距
  1395. let spaceAxis = (group0.k + group1.k) / 2 > 1 ? 'z' : 'x'
  1396. let spaceDis = Math.abs(group0.center[spaceAxis] - group1.center[spaceAxis])
  1397. if (spaceDis > standardW * 1.5) continue
  1398. let o0 = getAveWidth([group0], getLength(group0)) //因为有可能长度和箱子个数不匹配,所以需要得到限制后的宽度再比较
  1399. let o1 = getAveWidth([group1], getLength(group1))
  1400. let len0 = (group0.predictLen = o0.aveW * o0.count)
  1401. let len1 = (group1.predictLen = o1.aveW * o1.count)
  1402. const minR = 0.5 //不可限制太死,因为有的框个数识别少了,导致len短。但可通过重叠面积来判断
  1403. // if( len0 / len1 < minR || len0 / len1 > 1/minR) continue
  1404. let area0 = (group0.area = len0 * o0.aveW)
  1405. let area1 = (group1.area = len1 * o1.aveW)
  1406. let getBoxMixArea = (expandRatio1, expandRatio2) => {
  1407. let box0 = getBox2(group0.center, len0 + expandRatio1, o0.aveW + expandRatio2, group0.k)
  1408. let box1 = getBox2(group1.center, len1 + expandRatio1, o1.aveW + expandRatio2, group1.k)
  1409. let mixBox = getMixBox(box0, box1) //重叠部分
  1410. let s = mixBox.getSize(new THREE.Vector2())
  1411. return { box0, box1, areaMix: Math.max(0, s.x) * Math.max(0, s.y) } //可能是0
  1412. }
  1413. let areaMixExpand = getBoxMixArea(0.1, 0.6).areaMix
  1414. if (areaMixExpand / area0 < 0.5 && areaMixExpand / area1 < 0.5) continue //包含的可以通过
  1415. let areaMix = getBoxMixArea(0, 0).areaMix //实际重合面积
  1416. group0.contains = group0.contains || []
  1417. group1.contains = group1.contains || []
  1418. group0.contains.push({ group: group1, selfPercent: areaMix / area0, percent2: areaMix / area1, areaMix })
  1419. group1.contains.push({ group: group0, selfPercent: areaMix / area1, percent2: areaMix / area0, areaMix })
  1420. console.log('两个合并', group0, group1)
  1421. common.pushToGroupAuto([group0, group1], realGroups)
  1422. //包含的直接合并吧 - - ,这样会使结果偏移,不过没办法了,多个重叠面积太难算了
  1423. }
  1424. }
  1425. //但没合并前样本数量少,包含关系可能有错 - -
  1426. /*for(let m=0; m<groupInfo.length;m++){
  1427. let group = groupInfo[m]
  1428. let contains = group.contains.filter(e=> e.percent2 > 0.8) //所有包含的
  1429. contains.reduce
  1430. } */
  1431. groupInfo.forEach(info => {
  1432. //加入单个的
  1433. if (!realGroups.some(groups => groups.includes(info))) {
  1434. realGroups.push([info])
  1435. }
  1436. })
  1437. console.log('realGroups', realGroups)
  1438. //get boxes
  1439. realGroups.forEach((infos, i) => {
  1440. const sampleCount = infos.reduce((w, c) => {
  1441. return (w += c.pointsLen || c.bigBoxes.length)
  1442. }, 0)
  1443. const k = infos.reduce((w, c) => (w += c.k), 0) / infos.length
  1444. let centerPos = infos
  1445. .reduce((w, c) => {
  1446. return w.add(c.center.clone().multiplyScalar(c.pointsLen || c.bigBoxes.length))
  1447. }, new THREE.Vector3())
  1448. .multiplyScalar(1 / sampleCount) //预得中心点
  1449. //获取左右端点(需要排除可能的误差,所以采用最靠近端点的三个点。但无法排除前三个点中万一含有包含box的、或者误差大的端点)
  1450. let lefts = infos
  1451. .map(e => getLeft(e, k))
  1452. .sort((a, b) => a - b)
  1453. .filter(a => a < centerPos[k > 1 ? 'x' : 'z'])
  1454. .slice(0, 3)
  1455. let rights = infos
  1456. .map(e => getRight(e, k))
  1457. .sort((a, b) => b - a)
  1458. .filter(a => a > centerPos[k > 1 ? 'x' : 'z'])
  1459. .slice(0, 3)
  1460. let left = 0,
  1461. right = 0
  1462. let c0 = ((lefts.length + 1) * lefts.length) / 2
  1463. lefts.forEach((e, i) => { //越靠近最外侧权重越高。
  1464. left += e * ((lefts.length - i) / c0)
  1465. })
  1466. c0 = ((rights.length + 1) * rights.length) / 2
  1467. rights.forEach((e, i) => {
  1468. right += e * ((rights.length - i) / c0)
  1469. })
  1470. centerPos[k > 1 ? 'x' : 'z'] = (left + right) / 2
  1471. let len = right - left //加一点值是因为之前计算长度,用的是最外box的中心点,会少box一半宽度
  1472. let infos2 = infos.filter(e => {
  1473. return !e.predictLen || e.predictLen / len > 0.7
  1474. })
  1475. if(infos2.length == 0){
  1476. infos2 = infos.sort((a,b)=>b.predictLen - a.predictLen).slice(0,1)
  1477. }
  1478. let { aveW, count } = getAveWidth(infos2, len)
  1479. //长宽比中心点的误差更大,尤其是box类型的、或样本少的
  1480. //获取高度
  1481. let heights = []
  1482. {
  1483. let pairs = [] , heightss
  1484. infos.forEach(e => {
  1485. pairs.push(...e.group.relationships.filter(pair => pair[0].boxes.length == count && pair[1].boxes.length == count))
  1486. })
  1487. if(pairs.length){
  1488. heightss = pairs.map(pair => {
  1489. let boxes = pair.map(e => {
  1490. /* if (e.boxes.length == count) */ return e.boxes.slice()
  1491. /* return e.bestChain.slice() */
  1492. })
  1493. let match = rowInfos[pair[0].sid + '&' + pair[1].sid]
  1494. let ifReverse = match.reversed
  1495. if (match.reversed == void 0 && pair[0].boxes.length > 1 && pair[1].boxes.length > 1) {
  1496. let { reversed } = getReverseInfo(pair[0], pair[1])
  1497. ifReverse = reversed
  1498. }
  1499. if (ifReverse) {
  1500. boxes[1].reverse()
  1501. }
  1502. let heights1 = []
  1503. let topPoss = []
  1504. for (let i = 0; i < count; i++) {
  1505. let match1 = getMatchScore(boxes[0][i], boxes[1][i], { onlyGet: true })
  1506. let topPos = match1 && match1.topPos
  1507. if (!topPos) {
  1508. topPos = getBoxTop({ box0: boxes[0][i], box1: boxes[1][i] })
  1509. }
  1510. heights1.push(topPos.y - groundY)
  1511. topPoss.push(topPos)
  1512. }
  1513. if ((k < 1 && topPoss[0].z > topPoss[count - 1].z) || (k > 1 && topPoss[0].x > topPoss[count - 1].x)) {
  1514. heights1.reverse()
  1515. }
  1516. return heights1
  1517. })
  1518. }else{
  1519. let bigBoxes = []
  1520. infos.forEach(e => {
  1521. bigBoxes.push(...e.group.filter(e=>e.boxes.length == count))
  1522. })
  1523. heightss = bigBoxes.map(bigBox=>{
  1524. let topPoss = bigBox.boxes.map(box=>{
  1525. let a = {box0:box}
  1526. getBoxTop(a)
  1527. return a.topPos
  1528. })
  1529. if ((k < 1 && topPoss[0].z > topPoss[count - 1].z) || (k > 1 && topPoss[0].x > topPoss[count - 1].x)) {
  1530. topPoss.reverse()
  1531. }
  1532. let heights1 = topPoss.map(topPos=>topPos.y - groundY)
  1533. return heights1
  1534. })
  1535. }
  1536. heightss.forEach(arr => {
  1537. for (let i = 0; i < count; i++) {
  1538. heights[i] = (heights[i] || 0) + arr[i]
  1539. }
  1540. })
  1541. heights = heights.map(e => {
  1542. let h = e / heightss.length
  1543. return h
  1544. })
  1545. //console.log('heightss',heightss, pairs, heights)
  1546. }
  1547. //拆分成小box
  1548. let size = new THREE.Vector3(aveW, 2, aveW)
  1549. let c = 0
  1550. infos.box = []
  1551. while (c < count) {
  1552. let center
  1553. if (k > 1) {
  1554. let startX = centerPos.x - ((count - 1) / 2) * aveW
  1555. center = new THREE.Vector3(startX + c * aveW, centerPos.y, centerPos.z)
  1556. } else {
  1557. let startZ = centerPos.z - ((count - 1) / 2) * aveW
  1558. center = new THREE.Vector3(centerPos.x, centerPos.y, startZ + c * aveW)
  1559. }
  1560. let size1 = heights[c] ? size.clone().setY(heights[c]) : size //如1*3的是得不到height的
  1561. let box = new Box({ name: 'row' + i + '-' + c, center, size: size1, boxType: 'cabinet', infos })
  1562. c++
  1563. infos.box.push(box)
  1564. }
  1565. })
  1566. return realGroups.length > 0
  1567. }
  1568. }
  1569. let removeContain = arr => {
  1570. //去除嵌套
  1571. let len = arr.length
  1572. if (len < 2) return
  1573. for (let i = 0; i < len - 1; i++) {
  1574. let box0 = arr[i]
  1575. getBoxBase(box0)
  1576. box0.contains = box0.contains || []
  1577. for (let j = i + 1; j < len; j++) {
  1578. let box1 = arr[j]
  1579. getBoxBase(box1)
  1580. box1.contains = box1.contains || []
  1581. let d3 = Math.abs(box1.bbox2CenterX - box0.bbox2CenterX) //限制d3是因为在相差180度两端可能也符合
  1582. //d4 = Math.abs(box1.bbox2[3] - box0.bbox2[3])
  1583. if (d3 > 0.4 /* || d4 > 0.01 */) continue
  1584. let d0 = getBbox2Diff(box1.bbox2[0], box0.bbox2[0]),
  1585. d1 = getBbox2Diff(box0.bbox2[2], box1.bbox2[2])
  1586. let min = 0.005
  1587. if ((d1 >= 0 && Math.abs(d0) < min) || (d0 >= 0 && Math.abs(d1) < min) || (d1 >= 0 && d0 >= 0)) {
  1588. box0.contains.push(box1)
  1589. } else if ((d0 <= 0 && Math.abs(d1) < min) || (d1 <= 0 && Math.abs(d0) < min) || (d1 <= 0 && d0 <= 0)) {
  1590. box1.contains.push(box0)
  1591. }
  1592. }
  1593. }
  1594. let getWidthScore = (box, type) => {
  1595. const addDis = 0.1 //因为用的是btm的pos,比中心点近了一些,所以加上一段距离
  1596. let o = getBoxPoseByPos(box, getBoxPos(box), addDis)
  1597. let boxPjW = o.projectWidth
  1598. let standardPjW = (o.maxProjectWidth + o.minProjectWidth) / 2
  1599. let s = type == 'out' ? boxPjW - standardPjW : standardPjW - boxPjW
  1600. return -Math.pow(s, 2) * 10
  1601. }
  1602. arr.slice().forEach(box => {
  1603. if (box.contains.length > 1) {
  1604. //假设不存在第二层嵌套, 假设每个只能被一个嵌套
  1605. //决定留大还是留小
  1606. //先只去掉包含两个以上的,且角度范围一致
  1607. //尽量保留内层,除非内层太小
  1608. let { leftX, rightX } = getLeftRight(box.contains)
  1609. if (Math.abs(getBbox2Diff(box.bbox2[0], leftX)) > 0.005 || Math.abs(getBbox2Diff(box.bbox2[2], rightX)) > 0.005) return //范围不一致
  1610. let remainChild = true
  1611. let childrenScores = box.contains.map(e => getWidthScore(e, 'in'))
  1612. let childAve = childrenScores.reduce((w, c) => w + c, 0) / childrenScores.length
  1613. if (childAve < -4) {
  1614. let outScore = getWidthScore(box, 'out')
  1615. remainChild = childAve > outScore
  1616. }
  1617. if (!remainChild) {
  1618. box.contains.forEach(e => {
  1619. ;(e.state = '因被嵌套被删除'), (e.containBy = box)
  1620. arr.splice(arr.indexOf(e), 1)
  1621. console.log('因被嵌套被删除', ...box.contains)
  1622. })
  1623. } else {
  1624. box.state = '因嵌套其他被删除'
  1625. console.log('因嵌套其他被删除', box)
  1626. arr.splice(arr.indexOf(box), 1)
  1627. }
  1628. }
  1629. })
  1630. }
  1631. let waitFindRest = []
  1632. let Search = type => {
  1633. console.error('开始search', type)
  1634. let matchScoreMap = (this.matchScoreMap[type] = {})
  1635. let datas = {}
  1636. let panoIds = []
  1637. for (let id in this.datas) {
  1638. datas[id] = this.datas[id].shapes.filter(e => e.category == type)
  1639. datas[id].length && panoIds.push(id)
  1640. //对data预处理
  1641. //(之后如果还出现不同类型重叠在一起的,需要先识别摘除下。 )4GqaqNdyjGf
  1642. removeContain(datas[id]) //去除线框中的嵌套,主要是一个嵌套两个的。案例:KK-1Zjm9Rbl47
  1643. if (datas[id].length) {//融合。很多box被一分为二了,基本都是在全景图左右边界处。
  1644. let bigBoxes = getPanoBigRowBox(datas[id], { reason: 'mix' })
  1645. bigBoxes.forEach(bigBox => {
  1646. if (bigBox.boxes.length > 1) {
  1647. bigBox.boxes.forEach(box => {
  1648. ;(box.state = '被删除'), (box.mixTo = bigBox)
  1649. let i = datas[id].indexOf(box)
  1650. datas[id].splice(i, 1)
  1651. })
  1652. console.log('因融合而删除', bigBox.boxes)
  1653. datas[id].push(bigBox)
  1654. bigBox.index = datas[id].length>1 ? datas[id][datas[id].length-2].index+1 : 0
  1655. bigBox.category = type //更正
  1656. }
  1657. })
  1658. }
  1659. }
  1660. if (panoIds.length == 0) return
  1661. panoIds.sort((a, b) => {
  1662. return datas[b].length - datas[a].length
  1663. })
  1664. let groups = panoIds.map(e => datas[e])
  1665. console.log('按box个数排序:', groups.slice())
  1666. let group0 = groups[0],
  1667. len0 = group0.length
  1668. if (groups.length == 1) {
  1669. //只有一个全景里有数据
  1670. group0.forEach(e => createSinglePano(e))
  1671. return combines(type)
  1672. }
  1673. if (len0 == 1) {//最多的也只有一个box。此情况大部分是空调
  1674. panoIds.forEach(e => getBoxBase(datas[e][0]))
  1675. let maxAngle
  1676. //找出centerDir夹角最大的两个pano
  1677. for (let i = 0; i < panoIds.length; i++) {
  1678. let box0 = datas[panoIds[i]][0]
  1679. for (let j = i + 1; j < panoIds.length; j++) {
  1680. let box1 = datas[panoIds[j]][0]
  1681. getMatchScore(box0, box1, { isSingle: true })
  1682. }
  1683. }
  1684. let list = Object.keys(matchScoreMap)
  1685. list.sort((a, b) => {
  1686. return matchScoreMap[b].score - matchScoreMap[a].score
  1687. })
  1688. let match = matchScoreMap[list[0]]
  1689. preDealBox(match)
  1690. //根据分数重排序,前两个已匹配的pano放在第一第二(之后会被跳过),获得groups2
  1691. let panoIds2 = []
  1692. list.forEach(e => {
  1693. let info = matchScoreMap[e]
  1694. if (!panoIds2.includes(info.box0.pano.id)) panoIds2.push(info.box0.pano.id)
  1695. if (!panoIds2.includes(info.box1.pano.id)) panoIds2.push(info.box1.pano.id)
  1696. })
  1697. let groups2 = panoIds2.map(e => datas[e])
  1698. if (match.score > -100) {
  1699. getBoxSize(match)
  1700. if (match.score > 0 && match.sizeAdjust < 0.1) {
  1701. new Box(matchScoreMap[list[0]])
  1702. waitFindRest.push({type, args:[groups2]} ) //等待最后检查遗漏
  1703. return
  1704. }
  1705. }
  1706. //继续match
  1707. reMatchLowScores([match], groups2)
  1708. waitFindRest.push({type, args:[groups2]} ) //等待最后检查遗漏
  1709. return
  1710. }
  1711. if (type == 'cabinet') {
  1712. //转化为分组
  1713. if (searchByRow(groups, datas)){
  1714. waitFindRest.push({type, args:[groups,0]} ) //等待最后检查遗漏
  1715. return
  1716. }
  1717. }
  1718. //零散匹配。
  1719. {
  1720. //重新根据距离排序,挑选离所有box距离最近的两个pano (远的可能看不到box,或者得到的线框计算的位置不准。不过其实太近也不准-,-)
  1721. let counts = {}
  1722. groups.forEach(e => {
  1723. e.forEach(a => getBoxBase(a))
  1724. counts[e.length] || (counts[e.length] = [])
  1725. counts[e.length].push(e)
  1726. })
  1727. groups = []
  1728. let nums = Object.keys(counts)
  1729. nums.reverse()
  1730. nums.forEach(count => {
  1731. let groups_ = counts[count]
  1732. if (groups_.length > 1) {
  1733. groups_.forEach(e => {
  1734. e.disSc = e.reduce((w, c) => {
  1735. let pos = getBoxPos(c)
  1736. return w + c.pano.position.distanceToSquared(pos)
  1737. }, 0)
  1738. })
  1739. groups_.sort((a, b) => a.disSc - b.disSc)
  1740. }
  1741. groups.push(...groups_)
  1742. })
  1743. console.log('按距离排序:', groups)
  1744. group0 = groups[0]
  1745. }
  1746. let group1 = groups[1],
  1747. len1 = group1.length
  1748. for (let i = 0; i < len0; i++) {
  1749. //复杂度:n的平方次
  1750. for (let j = 0; j < len1; j++) {
  1751. let box1 = group0[i]
  1752. let box2 = group1[j]
  1753. let result = getMatchScore(box1, box2)
  1754. }
  1755. }
  1756. //寻找最佳配对 n!种组合(是否要限制个数多的情况?) 超过8个就很恐怖
  1757. //仅先查找选中的两个pano配对
  1758. let resultPairs = []
  1759. let newGroup0 = group0.slice(0)
  1760. let newGroup1 = group1.slice(0)
  1761. while (newGroup1.length < newGroup0.length) {
  1762. newGroup1.push({ sid: 'void' }) //为了使排列正确,补个空,使左右两边个数相等,过后和void匹配的不会计算box
  1763. }
  1764. searchPair(group0[0], newGroup0, newGroup1, null, resultPairs)
  1765. console.log(
  1766. 'resultPairs',
  1767. resultPairs.map(pairs => pairs.map(pair => pair.map(item => item.sid).join(' & ')))
  1768. )
  1769. resultPairs = resultPairs.map(pairs => {
  1770. let infos = pairs.map(pair => matchScoreMap[pair[0].sid + '&' + pair[1].sid])
  1771. let score = infos.reduce((s, e) => {
  1772. return s + (e ? e.score : 0)
  1773. }, 0)
  1774. //if(infos[0].score < -1000){
  1775. //score -= infos[0].score
  1776. //}
  1777. let o = {
  1778. infos,
  1779. score,
  1780. pairs,
  1781. name: pairs.map(pair => pair.map(item => item.sid).join(' & ')),
  1782. }
  1783. return o
  1784. })
  1785. console.log('resultPairs', resultPairs.slice())
  1786. console.log(
  1787. 'resultPairs按分数高低',
  1788. resultPairs.sort((a, b) => b.score - a.score)
  1789. )
  1790. //console.log('compu',compu)
  1791. let noMatches = [] //和void匹配的,需要和其他pano的重新匹配
  1792. let mayHaventMatched = [];
  1793. let lowScores = []
  1794. if (resultPairs[0]) {
  1795. resultPairs[0].infos.forEach((info, i) => {
  1796. if (!info) {
  1797. noMatches.push(resultPairs[0].pairs[i].find(e => e.sid != 'void'))
  1798. return //match with void
  1799. }
  1800. if (info.score < -2000) {
  1801. lowScores.push(info)
  1802. return
  1803. }
  1804. preDealBox(info)
  1805. getBoxSize(info)
  1806. if (info.sizeAdjust > 0.1) {
  1807. lowScores.push(info)
  1808. return
  1809. }
  1810. let box = new Box(info)
  1811. })
  1812. }
  1813. if (noMatches.length) {
  1814. reMatchLowScores(
  1815. noMatches.map(e => {
  1816. return { box0: e }
  1817. }),
  1818. groups
  1819. )
  1820. }
  1821. reMatchLowScores(lowScores, groups)
  1822. waitFindRest.push({type, args:[groups]} )
  1823. //combines(type)
  1824. }
  1825. function findRest(groups/* ,startIndex=2 */){
  1826. //查找是否有遗漏。
  1827. //1 可能有距离较远的box不在头两个pano的附近导致被漏掉。(概率很小)
  1828. //2 被剩余的
  1829. for(let i=0;i<groups.length;i++){
  1830. groups[i].forEach(box=>{
  1831. if(!used(box)){
  1832. //如果和现有的box的距离都很远,很可能是漏掉的
  1833. let near = boxes.find(solidBox =>{
  1834. if(solidBox.boxType != box.type && (solidBox.boxType == 'air-hanging' || box.type == 'air-hanging')) return; //挂空调一般不会撞到地面上的
  1835. let p1 = getBoxPos(solidBox)
  1836. let p2 = getBoxPos(box)
  1837. let p1_ = new THREE.Vector2(p1.x, p1.z);
  1838. let p2_ = new THREE.Vector2(p2.x, p2.z);
  1839. let maxWidth = standards[solidBox.boxType].widthNormal.max
  1840. if(solidBox.boxType != box.type){
  1841. maxWidth = Math.min(standards[box.type].widthNormal.max, maxWidth)
  1842. }
  1843. /* let r = solidBox.boxType == box.type ? 1.2 : 1 //种类不同的话要更严格,所以数值更小
  1844. return p1.distanceToSquared(p2) < maxWidth * maxWidth * r
  1845. */
  1846. let dis = solidBox.bound.distanceToPoint(p2)
  1847. let ra = solidBox.boxType == box.type ? 0.9 : 0.6
  1848. let rb = solidBox.boxType == box.type ? 0.4 : 0.3
  1849. let a = maxWidth * maxWidth * ra - p1_.distanceToSquared(p2_)
  1850. let b = maxWidth * rb - dis
  1851. let c = a + b
  1852. if(c>0){
  1853. console.log(1)
  1854. }
  1855. return c>0
  1856. })
  1857. if(!near){
  1858. reMatchLowScores([{box0:box, log:"findRest"}], groups,0 )
  1859. }else{
  1860. //console.log('find near', near.name, box.sid)
  1861. }
  1862. }
  1863. })
  1864. }
  1865. }
  1866. function used(box){
  1867. let has = (e)=>{
  1868. return e.box0 == box || e.box1 == box
  1869. }
  1870. return /* matched.includes(box) || */ boxes.some(e => /* e.boxType == box.type && */ has(e) || (e.list && e.list.some(a => has(a)))
  1871. || e.infos && e.infos.some(u=>u.group.some(r=> r.boxes.some(b=>b==box) )) //row
  1872. )
  1873. }
  1874. function reMatchLowScores(lowScores, groups, startIndex=2 ) {
  1875. let matched = [],
  1876. tooLows = []
  1877. let isSameMatch = (match0, match1) => {
  1878. //是否对应同一个box实体(不一定准),通过两两box之间是否都match来判断
  1879. //如果是相同pano但不同的box肯定不是对应同一个box实体
  1880. let ifWrong = (box0, box1) => {
  1881. if (box0 == box1) return
  1882. if (box0.pano == box1.pano) return true
  1883. let match2 = getMatchScore(box0, box1, { isSingle: true, restMatch: true })
  1884. if (match2.score < -2000) return true
  1885. }
  1886. if (ifWrong(match0.box0, match1.box0) || ifWrong(match0.box1, match1.box1) || ifWrong(match0.box0, match1.box1) || ifWrong(match0.box1, match1.box0)) return
  1887. return true
  1888. }
  1889. if (lowScores.length) {
  1890. console.warn(lowScores[0].log || (lowScores[0].box1 ? '低分重新匹配' : '剩余匹配'), lowScores)
  1891. if (lowScores[0].box1) {
  1892. lowScores.sort((a, b) => {
  1893. //低分优先
  1894. return a.score - b.score
  1895. })
  1896. }
  1897. lowScores.forEach(info => {//info中的box0和box1分别向后寻找其他的配对。选择分数高的配对。 但box0和box1可能是错误配对,会导致找到了替代的也可能遗漏。
  1898. /* if (info.name == 'pano0-3&pano2-3') {
  1899. console.log(1)
  1900. } */
  1901. let box01 = info.box0
  1902. let box02 = info.box1
  1903. let bigGroup = []
  1904. box02 && bigGroup.push(info)
  1905. let got
  1906. for (let cur = startIndex; cur < groups.length; cur++) {
  1907. let thirdGroup = groups[cur]
  1908. let scores0 = [],
  1909. scores1 = []
  1910. thirdGroup.forEach(box1 => {
  1911. //if (matched.includes(box1.sid)) return
  1912. if (used(box1)) return //会不会太严格?
  1913. if(box1.pano != box01.pano){
  1914. let r1 = getMatchScore(box01, box1, { isSingle: true, restMatch: true })
  1915. r1.score > -4000 && scores0.push(r1)
  1916. }
  1917. if (box02 && box1.pano != box02.pano) {
  1918. let r2 = getMatchScore(box02, box1, { isSingle: true, restMatch: true })
  1919. r2.score > -4000 && scores1.push(r2)
  1920. }
  1921. })
  1922. scores0.sort((a, b) => {
  1923. return b.score - a.score
  1924. })
  1925. scores1.sort((a, b) => {
  1926. return b.score - a.score
  1927. })
  1928. scores0[0] && bigGroup.push(scores0[0])
  1929. scores1[0] && bigGroup.push(scores1[0])
  1930. }
  1931. bigGroup.sort((a, b) => {
  1932. return b.score - a.score
  1933. })
  1934. let goodList = bigGroup.slice(0, 10).map(e => {
  1935. if (!getBoxPos(e)) return e
  1936. preDealBox(e)
  1937. getBoxSize(e)
  1938. return e
  1939. })
  1940. let goodList2 = goodList
  1941. .sort((a, b) => {
  1942. return b.score - a.score
  1943. })
  1944. .slice(0, 3)
  1945. if (goodList2.length == 0) {
  1946. return createSinglePano(box01)
  1947. }
  1948. if (goodList2[0].score > -1000) {
  1949. goodList2 = goodList2.filter(e => e.score > -1000)
  1950. } else {
  1951. goodList2 = [goodList2[0]] // 最高分已经过小
  1952. if (goodList2[0].score < -1200) {
  1953. if (!box02) return createSinglePano(box01)
  1954. console.warn('分数过低,是否有匹配错误?', goodList2[0])
  1955. return tooLows.push(goodList2[0])
  1956. }
  1957. }
  1958. if (goodList2.length) {
  1959. //需要确认两两之间是配对的,也就是都对应同一个box
  1960. let subGroups = []
  1961. for (let i = 0, len = goodList2.length; i < len; i++) {
  1962. //向后选择队友
  1963. let match0 = goodList2[i]
  1964. if (subGroups.some(e => e.includes(match0))) continue //被挑选了的没有选择权
  1965. let gr = [match0]
  1966. for (let j = i + 1; j < len; j++) {
  1967. let match1 = goodList2[j]
  1968. if (isSameMatch(match0, match1)) {
  1969. gr.push(match1)
  1970. }
  1971. }
  1972. //if(gr.length>1){
  1973. subGroups.push(gr)
  1974. //}
  1975. }
  1976. subGroups.forEach(pair => {
  1977. mixMatchBox(pair)
  1978. })
  1979. //console.log('lowScores getBox ', box, goodList2)
  1980. }
  1981. })
  1982. //改为之后 findRest, 因为两者都single的可能性低
  1983. /* let judge = box => {
  1984. if (!used(box)) {
  1985. matched.push(box)
  1986. createSinglePano(box)
  1987. }
  1988. }
  1989. tooLows.forEach(e => {
  1990. judge(e.box0)
  1991. judge(e.box1)
  1992. }) */
  1993. }
  1994. }
  1995. function mixMatchBox(list) {
  1996. let center = new THREE.Vector3(),
  1997. size = new THREE.Vector3(),
  1998. name
  1999. list.forEach(e => {
  2000. center.add(getBoxPos(e))
  2001. size.add(e.size)
  2002. //matched.push(e.box1)
  2003. })
  2004. center.multiplyScalar(1 / list.length)
  2005. size.multiplyScalar(1 / list.length)
  2006. let box = new Box({
  2007. name: 'low:' + list.map(e => e.name),
  2008. boxType: list[0].boxType,
  2009. center,
  2010. size,
  2011. list,
  2012. xProp: list[0].xProp,
  2013. yProp: list[0].yProp,
  2014. })
  2015. }
  2016. function combines(type) {
  2017. //合并boxSolids . battery经常嵌套
  2018. if (type == 'battery') {
  2019. const group = []
  2020. /* let airs = boxes.filter(e => e.boxType == 'air')
  2021. let hangings = boxes.filter(e => e.boxType == 'air-hanging') */
  2022. let batterys = boxes.filter(e => e.boxType == 'battery')
  2023. ;[batterys].forEach(boxes => {
  2024. if (boxes.length > 1) {
  2025. const boxType = boxes[0].boxType
  2026. let { min, max } = standards[boxType].widthNormal
  2027. for (let i = 0, len = boxes.length; i < len - 1; i++) {
  2028. let box0 = boxes[i]
  2029. for (let j = i + 1; j < len; j++) {
  2030. let box1 = boxes[j]
  2031. let bound = box0.bound.clone().union(box1.bound)
  2032. let size = bound.getSize(new THREE.Vector3())
  2033. let intersect = box0.bound.intersectsBox(box1.bound)
  2034. let minX = min,
  2035. minZ = min,
  2036. maxX = max,
  2037. maxZ = max
  2038. let xProp = box1.xProp || box0.xProp
  2039. let yProp = box1.yProp || box0.yProp
  2040. if (xProp) {
  2041. if (box0.xProp && box1.xProp && box0.xProp != box1.xProp) continue
  2042. maxX = standards[boxType][xProp].max
  2043. maxZ = standards[boxType][yProp].max
  2044. }
  2045. maxX = Math.max(maxX, box0.size.x, box1.size.x) //必须大于各自的size,否则无法去除本身就oversize的box中包含的
  2046. maxZ = Math.max(maxZ, box0.size.z, box1.size.z)
  2047. let r = intersect ? 1.3 : 1.1 //如果是没有交集,限制更大些
  2048. if (size.x < maxX * r && size.z < maxZ * r ) {
  2049. common.pushToGroupAuto([box0, box1], group)
  2050. }
  2051. }
  2052. }
  2053. if (group.length) {
  2054. //虽然如果三个以上可能会超出maxWidth。 不过3个的概率很低,且可以限制宽度
  2055. group.forEach(pair => {
  2056. let bound = new THREE.Box3()
  2057. pair.forEach(e => {
  2058. bound.union(e.bound)
  2059. e.dispose()
  2060. })
  2061. let size = bound.getSize(new THREE.Vector3())
  2062. let center = bound.getCenter(new THREE.Vector3())
  2063. let o = restrictSize(size.x, size.z, {boxType:pair[0].boxType})
  2064. size.x = o.x
  2065. size.z = o.y
  2066. let box = new Box({
  2067. name: 'mix:' + pair.map(e => e.name + ','),
  2068. mixedFrom: pair,
  2069. boxType,
  2070. center,
  2071. size,
  2072. })
  2073. console.error('混合',pair, box)
  2074. })
  2075. }
  2076. }
  2077. })
  2078. }
  2079. }
  2080. let createSinglePano = box => {
  2081. //仅用一个pano中的data来创建
  2082. getBoxBase(box)
  2083. let center = getBoxPos(box)
  2084. let info = {
  2085. name: box.sid,
  2086. box0: box,
  2087. center,
  2088. }
  2089. //getBoxTop(info)
  2090. preDealBox(info)
  2091. let a = getPoseScore(info.boxposes,true)
  2092. let failed = a < -50
  2093. console.log('createSinglePano', failed?'失败':'成功' , 'pose score:',a, box )
  2094. if(failed)return
  2095. addLabel(center, 'center', { a: 0.3 })
  2096. info.topPos && addLabel(info.topPos, 'topPos', { a: 0.3 })
  2097. getBoxSize(info)
  2098. new Box(info)
  2099. }
  2100. Search('cabinet')
  2101. Search('air')
  2102. Search('battery')
  2103. console.log('----FindRest----')
  2104. waitFindRest.forEach((e)=>{
  2105. findRest(...e.args)
  2106. combines(e.type)
  2107. })
  2108. }
  2109. /* let getSid = (function(){
  2110. let sid = 0
  2111. return function(){
  2112. return sid++
  2113. }
  2114. })() */
  2115. let done = ()=>{
  2116. for(let panoId in this.datas){
  2117. this.datas[panoId].shapes = this.datas[panoId].shapes.map((shape, i) => {
  2118. return Object.assign(
  2119. {
  2120. sid: 'pano' + panoId + '-' + i,
  2121. pano: player.model.panos.index[panoId],
  2122. index: i,
  2123. },
  2124. shape
  2125. )
  2126. })
  2127. }
  2128. groundY = player.model.boundingBox.min.y
  2129. groundPlane.setFromNormalAndCoplanarPoint(new THREE.Vector3(0, 1, 0), new THREE.Vector3(0, groundY, 0))
  2130. const w = 0.5 //这一版的模型bound常常太小,把箱子当墙面,所以扩展一个箱子宽度
  2131. safeBound = player.model.boundingBox.clone().expandByVector(new THREE.Vector3(w, w, w))
  2132. this.ifAnalyze && beginCompute()
  2133. if(version == 'vision')this.load(player.currentPano.id)
  2134. }
  2135. async function load(panoId) {
  2136. let data = await http.post('/service/scene/sceneMarkShape/getInfo', { num: player.$app.config.num, imagePath: panoId + '.jpg' })
  2137. // console.error(data)
  2138. if (!data.data || !data.success) return (this.datas[panoId] = null)
  2139. this.datas[panoId] = data.data
  2140. if (Object.keys(this.datas).length == panosCount && !player.model.panos.list.some(e => e.isAligned() && !(panoId in this.datas))) {
  2141. done()
  2142. }
  2143. }
  2144. let panosCount = 0
  2145. if(!dataList){
  2146. player.model.panos.list.forEach(e => {
  2147. if (!e.isAligned()) return
  2148. panosCount++
  2149. load.bind(this)(e.id)
  2150. })
  2151. }else{ //when version == 'output'
  2152. dataList.forEach(e=>{
  2153. let panoId = e.imagePath.split('.jpg')[0]
  2154. this.datas[panoId] = e
  2155. })
  2156. done()
  2157. }
  2158. }
  2159. bindEvents() {
  2160. if(version != 'vision')return
  2161. player.on(PlayerEvents.FlyingStarted, e => {
  2162. //if(e.mode == 'dollhouse')return
  2163. // 点位跳转前清除已有线框
  2164. this.traverse(obj => {
  2165. if (obj.isMesh) {
  2166. obj.geometry.dispose()
  2167. obj.material.dispose()
  2168. }
  2169. })
  2170. this.wireframes.clear()
  2171. })
  2172. player.on(PlayerEvents.FlyingEnded, () => {
  2173. // 点位跳转后加载线框
  2174. if (player.mode != Viewmode.PANORAMA) return // 只有PANORAMA模式下需要加载
  2175. this.load(player.currentPano.id)
  2176. })
  2177. window.panoWireframe = this
  2178. if(this.ifAnalyze ){
  2179. setTimeout(() => {
  2180. {
  2181. let btn = document.createElement('button')
  2182. btn.innerHTML = '点击切换box显示'
  2183. btn.onclick = () => {
  2184. this.boxes.forEach(e => ((e.boxHelper.visible = !e.boxHelper.visible), (e.label.visible = !e.label.visible)))
  2185. }
  2186. document.querySelector('#app').appendChild(btn)
  2187. btn.id = 'boxWire'
  2188. btn.style.position = 'fixed'
  2189. btn.style['z-index'] = '100'
  2190. btn.style.background = '#e00472'
  2191. btn.style.padding = '10px'
  2192. btn.style.bottom = '80px'
  2193. }
  2194. {
  2195. let btn = document.createElement('button')
  2196. btn.innerHTML = '点击切换矩形框显示'
  2197. btn.onclick = () => {
  2198. this.wireframes.visible = !this.wireframes.visible
  2199. }
  2200. document.querySelector('#app').appendChild(btn)
  2201. btn.id = 'wireframes'
  2202. btn.style.position = 'fixed'
  2203. btn.style['z-index'] = '100'
  2204. btn.style.background = '#419aff'
  2205. btn.style.padding = '10px'
  2206. btn.style.bottom = '130px'
  2207. }
  2208. }, 1000)
  2209. }
  2210. }
  2211. /**
  2212. * 加载点位标记数据
  2213. * @param {*} panoId
  2214. */
  2215. /* async */ load(panoId) {
  2216. // fetch(texture.getImageURL('images/points/' + panoId + '.json'))
  2217. // .then(data => data.json())
  2218. // .then(data => {
  2219. //let data = await http.post('/service/scene/sceneMarkShape/getInfo', { num: player.$app.config.num, imagePath: panoId + '.jpg' })
  2220. // console.error(data)
  2221. let data = this.datas[panoId]
  2222. if (!data) {
  2223. if (!(panoId in this.datas))
  2224. setTimeout(() => {
  2225. this.load(panoId)
  2226. }, 100) //否则无数据
  2227. return
  2228. } //if (!data.data || !data.success) return
  2229. if (player.currentPano.id != panoId || player.flying) return // 防止连续跳转点位时,clear后才load好上一点位的数据,导致出现之前的标记
  2230. let { shapes, imageHeight, imageWidth } = data //data.data
  2231. shapes.forEach((shape, i) => {
  2232. // 填充色和线框色
  2233. // let { fill_color, line_color } = shape
  2234. getUVs(shape)
  2235. getCenterDir(shape, player.currentPano)
  2236. let { fill_color, color = [56, 56, 255] } = shape
  2237. let line_color = [...color, 255]
  2238. if (!fill_color) fill_color = [255, 255, 255, 0]
  2239. if (!line_color) line_color = [255, 0, 0, 255]
  2240. this.showSignalFrom2d(
  2241. `${i} : ${shape.category}`,
  2242. shape.bbox,
  2243. imageWidth,
  2244. imageHeight,
  2245. {
  2246. fill: {
  2247. color: new THREE.Color().setRGB(fill_color[0] / 255, fill_color[1] / 255, fill_color[2] / 255),
  2248. opacity: fill_color[3] / 255,
  2249. },
  2250. line: {
  2251. color: new THREE.Color().setRGB(line_color[0] / 255, line_color[1] / 255, line_color[2] / 255),
  2252. opacity: line_color[3] / 255,
  2253. },
  2254. },
  2255. shape.centerDir
  2256. )
  2257. })
  2258. // })
  2259. // .catch(err => console.log(`点位${panoId}无标记数据或数据出错:`, err))
  2260. }
  2261. /**
  2262. * 根据坐标标记全景图
  2263. *
  2264. * 存在的问题:如果要准确复现全景图上的线框,上下边框会变为弧形。而按顶点连直线的话,180度以上会出bug。
  2265. * 解决方式:目前150度以下只画出4个顶点然后连直线,150度以上准确画出全景图线框。
  2266. */
  2267. showSignalFrom2d(name, rect, w, h, options, centerDir) {
  2268. // 目前rect给的是矩形对角的两个点坐标,将它扩展成四个顶点
  2269. let cornerArr = [
  2270. [rect[0], rect[1]],
  2271. [rect[2], rect[1]],
  2272. [rect[2], rect[3]],
  2273. [rect[0], rect[3]],
  2274. ]
  2275. // 2d坐标转3d坐标
  2276. let transform2dTo3d = point => {
  2277. // 计算方向向量
  2278. let x = point[0],
  2279. y = point[1]
  2280. let yaw = (-x / w) * (Math.PI * 2)
  2281. let pitch = Math.PI / 2 - (y / h) * Math.PI
  2282. let dir = new THREE.Vector3()
  2283. dir.copy(Vectors.RIGHT).applyAxisAngle(Vectors.BACK, pitch).applyAxisAngle(Vectors.UP, yaw).applyQuaternion(player.currentPano.quaternion)
  2284. return dir
  2285. }
  2286. // // 计算矩形线框中点坐标(取x、y的平均值)
  2287. // let center = [cornerArr.reduce((a, b) => [a[0] + b[0], 0])[0] / cornerArr.length, cornerArr.reduce((a, b) => [0, a[1] + b[1]])[1] / cornerArr.length]
  2288. // let centerVec = transform2dTo3d(center) // 计算中点3d坐标
  2289. // 根据四个顶点,填充中间点
  2290. let pointArr = []
  2291. for (let i = 0; i < cornerArr.length; i++) {
  2292. let corner1 = cornerArr[i]
  2293. let corner2 = cornerArr[(i + 1) % cornerArr.length]
  2294. pointArr.push(corner1)
  2295. /* // 横向角度超过150度时,3d中边框的弧线已经不太明显,准确画出全景图线框
  2296. if ((rect[2] - rect[0]) / w < 5 / 12 && i % 2 == 0) continue
  2297. const vec = [corner2[0] - corner1[0], corner2[1] - corner1[1]]
  2298. let length = Math.sqrt(vec[0] * vec[0] + vec[1] * vec[1])
  2299. let num = length / 150
  2300. for (let j = 1; j <= num; j++) {
  2301. pointArr.push([corner1[0] + (vec[0] / num) * j, corner1[1] + (vec[1] / num) * j])
  2302. } */
  2303. }
  2304. //pointArr.push(cornerArr[0], cornerArr[2], cornerArr[1], cornerArr[3]) //对角线
  2305. let points = []
  2306. pointArr.forEach(point => {
  2307. let dir = transform2dTo3d(point)
  2308. // points.push(dir.sub(centerVec)) // 计算其他点相对于中点的坐标,方便旋转平移等
  2309. points.push(dir)
  2310. })
  2311. // 线框
  2312. const lineGeometry = new THREE.BufferGeometry().setFromPoints(points)
  2313. const lineMaterial = new THREE.LineBasicMaterial({ color: options.line.color, opacity: options.line.opacity, transparent: true, depthTest: false })
  2314. const wireframe = new THREE.LineLoop(lineGeometry, lineMaterial)
  2315. // wireframe.position.copy(centerVec) // 将中点作为线框坐标
  2316. wireframe.renderOrder = 100
  2317. // 填充颜色
  2318. const fillGeometry = lineGeometry.clone().setIndex(new THREE.BufferAttribute(new Uint16Array([0, 1, 3, 2, 3, 1]), 1))
  2319. const fillMaterial = new THREE.MeshBasicMaterial({ color: options.fill.color, opacity: options.fill.opacity, transparent: true, side: THREE.DoubleSide, depthTest: false })
  2320. const plane = new THREE.Mesh(fillGeometry, fillMaterial)
  2321. plane.renderOrder = wireframe.renderOrder - 1
  2322. wireframe.add(plane)
  2323. // 名称
  2324. const textMesh = new TextSprite({
  2325. text: name,
  2326. backgroundColor: { r: options.line.color.r * 255, g: options.line.color.g * 255, b: options.line.color.b * 255, a: 0.4 },
  2327. textColor: { r: 255, g: 255, b: 255, a: 1 },
  2328. borderRadius: 15,
  2329. renderOrder: wireframe.renderOrder + 1,
  2330. player: player,
  2331. })
  2332. textMesh.position.copy(centerDir.clone().add(new THREE.Vector3(0, 0.1, 0)))
  2333. textMesh.lookAt(0, 0, 0) // 看向相机
  2334. textMesh.scale.set(0.12, 0.12, 0.12)
  2335. let group = new THREE.Group()
  2336. group.position.copy(player.currentPano.position)
  2337. group.add(wireframe)
  2338. group.add(textMesh)
  2339. this.wireframes.add(group)
  2340. }
  2341. }