PanoBoxFrame.js 132 KB

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