PanoBoxFrame.js 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167
  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. const convertTool = {
  8. getQuaByAim: function (aim, center=new THREE.Vector3, forward=new THREE.Vector3(0, 0, -1)){ //z朝上的坐标系是 forward = new THREE.Vector3(0, 1, 0)
  9. let qua1 = new THREE.Quaternion().setFromUnitVectors(forward, aim.clone().sub(center).normalize())
  10. //或var _ = (new THREE.Matrix4).lookAt(pano.position, aim, new THREE.Vector3(0,1,0)); aimQua = (new THREE.Quaternion).setFromRotationMatrix(_);
  11. return qua1
  12. },
  13. }
  14. //----------------------复制以下内容---------------------------------
  15. let player,
  16. skyBoxTight,
  17. meshGroup,
  18. modelBound = new THREE.Box3(),
  19. ray = new THREE.Raycaster(),
  20. groundPlane = new THREE.Plane(),
  21. groundY,
  22. safeBound,
  23. boundConfirmed, //安全区域应该在扣除每种类型柜子大概的长宽的一半
  24. hue = 0,
  25. startTime,
  26. isLaser,
  27. boxesSolid = []
  28. const MinBoxInitialScore = 0.68 //找不到匹配时,若box分数低于该值,不createSinglePano
  29. let standards = {
  30. cabinet: {
  31. widthNormal: { min: 0.55, max: 1.05 /* max: 0.65 */ }, //widthNormal是不计宽还是厚度的平均宽度 //个别场景如S9yepREK8Jl 宽0.8米
  32. height: { min: 0.3, max: 2.5, standard: 2 },
  33. closeRatio: 0.7, //数值越小越容易findRest。一般在墙上的位置不准要设置大些,扎堆放置的设置小些
  34. },
  35. fire: {
  36. widthNormal: { min: 0.12, max: 0.16 },
  37. height: { min: 0.4, max: 0.58 },
  38. widthSame: true, //长宽相等
  39. closeRatio: 4,
  40. tinyXZ: true, //可以通过它近似确定地面高度
  41. },
  42. air: {
  43. widthNormal: { min: 0.35, max: 0.7 },
  44. width: { min: 0.48, max: 0.75 }, //因为总是斜着放所以范围较大
  45. thick: { min: 0.33, max: 0.5 },
  46. height: { min: 1.2, max: 2.2, standard: 1.8 },
  47. atWall: 0.8,
  48. closeRatio: 1.1,
  49. },
  50. airSmart: {
  51. widthNormal: { min: 0.35, max: 0.7 },
  52. width: { min: 0.48, max: 0.75 }, //因为总是斜着放所以范围较大
  53. thick: { min: 0.33, max: 0.5 },
  54. height: { min: 1.2, max: 2.2, standard: 1.8 },
  55. atWall: 0.8,
  56. closeRatio: 1.1,
  57. },
  58. 'air-hanging': {
  59. widthNormal: { min: 0.3, max: 1 },
  60. width: { min: 0.8, max: 1.1 },
  61. thick: { min: 0.2, max: 0.3 },
  62. height: { min: 0.3, max: 0.5, standard: 0.4 }, //standard是通常出现的最高高度. 有这个值的在离地的时候直接使用该高度
  63. bottom: { min: 0.8, max: 2.0 }, // 不绝对,大部分
  64. atWall: 1, //在墙壁的可能性
  65. closeRatio: 1.5,
  66. },
  67. battery: {
  68. widthNormal: { min: 0.45, max: 1.35 },
  69. width: { min: 0.7, max: 1.4 },
  70. thick: { min: 0.35, max: 0.5 },
  71. height: { min: 0.3, max: 2.5 }, //maxHeight
  72. //有的电池很小。考虑是否追加battery-little 并且限制大电池的长宽高比例
  73. atWall: 0.9,
  74. closeRatio: 0.9,
  75. },
  76. groundBar: {
  77. widthNormal: { min: 0.06, max: 0.4 },
  78. width: { min: 0.3, max: 0.5 },
  79. thick: { min: 0.05, max: 0.08 },
  80. height: { min: 0.1, max: 0.2 },
  81. bottom: { min: 1.2, max: 3 },
  82. atWall: 1,
  83. closeRatio: 2,
  84. },
  85. hlkcWindow: {
  86. widthNormal: { min: 0.08, max: 0.5 },
  87. width: { min: 0.35, max: 0.5 },
  88. thick: { min: 0.03, max: 0.06 },
  89. height: { min: 0.35, max: 0.5 },
  90. bottom: { min: 1.2, max: 3 },
  91. atWall: 1,
  92. closeRatio: 2,
  93. },
  94. electric: {
  95. widthNormal: { min: 0.2, max: 0.7 },
  96. width: { min: 0.5, max: 0.65 },
  97. thick: { min: 0.2, max: 0.3 },
  98. height: { min: 0.5, max: 1 }, //maxHeight 4GqaqNdyjGf一米高
  99. bottom: { min: 0.8, max: 1.8 },
  100. atWall: 1,
  101. closeRatio: 2,
  102. },
  103. monitor: {
  104. widthNormal: { min: 0.08, max: 0.11 },
  105. height: { min: 0.1, max: 0.2, standard: 0.15 }, //maxHeight
  106. bottom: { min: 1.2, max: 3 },
  107. atWall: 1,
  108. closeRatio: 4,
  109. tiny: true, //因为较小且无方向,所以近似一个点,用射线算出的位置比墙面还准,所以其最终位置可用于expandModelBound
  110. },
  111. rowBigBox: {
  112. widthNormal: { min: 0.55, max: Infinity },
  113. height: { min: 0.7, max: 2.4, standard: 2 },
  114. },
  115. /*"cabling-wall":{
  116. },
  117. "cabling-ceil":{
  118. atCeil:1
  119. } */
  120. }
  121. /* const typeNames = {
  122. cabinet : 'cabinet', //标准机柜
  123. air : 'air', //普通空调柜式
  124. battery : 'battery', //蓄电池组
  125. }
  126. */
  127. const typeNames = {
  128. fire: 'extinguisher', //灭火器
  129. monitor: 'surveillance_camera', //监控摄像机 放第一个,用于继续确定边界
  130. hlkcWindow: 'hlkc', //馈线窗
  131. groundBar: 'grounding_bar', //接地排
  132. cabinet: 'equipment_cabinet', //标准机柜
  133. battery: 'accumulator', //蓄电池组
  134. /* ac : 'ac_switchboard', //交流配电柜 //这两种合并,因为差别太小了,见QlJau21WP8G的,在全景图两侧识别的竟然一个ac一个dc
  135. dc : 'dc_distribution', //直流配电设备 */
  136. electric: ['ac_switchboard', 'dc_distribution'],
  137. air: 'sdkt', //普通空调柜式
  138. airSmart: 'ventilation_installation', //智能通风设备
  139. //cabling : 'cabling_rack', //单层走线架 因为在天花板的走线有点复杂,经常断开,无法确定方向所以放弃
  140. }
  141. /* const typeNamesReverse = {} //为了方便访问
  142. for(let i in typeNames){
  143. typeNamesReverse[typeNames[i]] = i
  144. }
  145. */
  146. let addLine = (origin, dir, len, color) => {
  147. return
  148. if (version != 'vision') return
  149. var line1 = LineDraw.createLine([origin, origin.clone().add(dir.clone().multiplyScalar(len || 1))], { color })
  150. //console.log(origin.toArray(), dir.toArray())
  151. meshGroup.add(line1)
  152. return line1
  153. }
  154. let addLabel = (pos, text, { bgcolor, a } = {}) => {
  155. if (version != 'vision') return
  156. let shift = new THREE.Vector3(0, -0.2, 0)
  157. bgcolor = bgcolor ? new THREE.Color(bgcolor) : { r: 1, g: 1, b: 1 }
  158. //let endPos = new THREE.Vector3().addVectors(pos,shift);
  159. text instanceof Array || (text = [text])
  160. let lineCount = Math.round(Math.random() * 6) + 1
  161. let lines = []
  162. while (lineCount-- > 0) {
  163. lines.push('|')
  164. }
  165. text = [...text, ...lines, 'o']
  166. let textMesh = new TextSprite({
  167. text,
  168. textColor: { r: 0, g: 0, b: 0, a: 1 },
  169. backgroundColor: { r: bgcolor.r * 255, g: bgcolor.g * 255, b: bgcolor.b * 250, a: a || 0 },
  170. textBorderColor: { r: bgcolor.r * 255, g: bgcolor.g * 255, b: bgcolor.b * 250, a: a || 0.9 },
  171. textBorderThick: 2,
  172. margin: { x: 0, y: 0 },
  173. borderRadius: 0,
  174. player: player,
  175. sizeInfo: { minSize: 90, maxSize: 300, nearBound: 1, farBound: 7 },
  176. })
  177. textMesh.position.copy(pos)
  178. //textMesh.scale.set(0.3, 0.3, 0.3)
  179. meshGroup.add(textMesh)
  180. textMesh.sprite.position.y += textMesh.sprite.scale.y * 0.4
  181. return textMesh
  182. }
  183. let getBoxFinalPos = info => {
  184. //创建solidbox时的position
  185. let position
  186. let center = getBoxPos(info)
  187. if (standards[info.boxType].bottom) {
  188. //悬挂
  189. position = center
  190. } else {
  191. position = center.clone().setY(groundY + info.size.y / 2) //使着地
  192. }
  193. return position
  194. }
  195. const axises = [
  196. new THREE.Vector3(-1, 1, -1),
  197. new THREE.Vector3(1, 1, -1),
  198. new THREE.Vector3(1, 1, 1),
  199. new THREE.Vector3(-1, 1, 1),
  200. new THREE.Vector3(-1, -1, -1),
  201. new THREE.Vector3(1, -1, -1),
  202. new THREE.Vector3(1, -1, 1),
  203. new THREE.Vector3(-1, -1, 1),
  204. ]
  205. let traverse = (info, fun) => {
  206. //忽略.infos的row 的信息
  207. fun(info)
  208. info.list && info.list.forEach(a => traverse(a, fun))
  209. info.mixedFrom && info.mixedFrom.forEach(a => traverse(a, fun))
  210. }
  211. class Box {
  212. //结果
  213. constructor(info) {
  214. //preDealBox(info)
  215. this.setFromInfo(info)
  216. this.buildFromData || (this.name = this.boxType + '-' + this.name)
  217. if (version == 'vision' && (this.buildFromData || boundConfirmed)) this.draw()
  218. boxesSolid.push(this)
  219. }
  220. setFromInfo(info) {
  221. for (let i in info) {
  222. this[i] = info[i]
  223. }
  224. /* let h = info.size.y
  225. let standardH = standards[info.boxType].height.standard
  226. if (h > standardH) {
  227. h = standardH + Math.log(1 + (h - standardH) / 2) //Math.log2: 以2为底的对数 ,Math.log:自然对数
  228. info.size.y = h
  229. } */
  230. this.position = this.buildFromData ? this.center : getBoxFinalPos(this)
  231. let bound = new THREE.Box3().setFromCenterAndSize(this.position, this.size)
  232. this.bound = bound
  233. }
  234. draw() {
  235. hue += 0.23
  236. var color = new THREE.Color().setHSL(hue, 0.9, 0.85)
  237. this.boxHelper = new THREE.Box3Helper(this.bound, color)
  238. this.boxHelper.material.depthTest = false
  239. this.boxHelper.material.transparent = true
  240. this.boxHelper.renderOrder = 30
  241. /* let { warnStr, exStr } = this
  242. warnStr && (exStr += `【${warnStr}】`) */
  243. this.label = addLabel(this.position, /* exStr ? [this.name, exStr] : */ this.score ? [this.name, this.score.toFixed(1)] : this.name, { bgcolor: color })
  244. /* //color = new THREE.Color('#fff')
  245. let box = new THREE.Mesh(new THREE.BoxBufferGeometry(), new THREE.MeshBasicMaterial({color, opacity:0.5, transparent:true}))
  246. box.position.copy(this.position)
  247. box.scale.copy(this.size)
  248. meshGroup.add(box) */
  249. meshGroup.add(this.boxHelper)
  250. }
  251. dispose() {
  252. let index = boxesSolid.indexOf(this)
  253. if (index > -1) {
  254. boxesSolid.splice(index, 1)
  255. if (version == 'vision' && boundConfirmed) {
  256. this.label.sprite.material.opacity = 0.3
  257. this.boxHelper.material.opacity = 0.2
  258. }
  259. }
  260. }
  261. traversePair(fun) {
  262. traverse(this, fun)
  263. }
  264. getDirection() {
  265. //获得正面朝向。 需要全部box都创建完再调用
  266. let xProp = this.xProp
  267. let dir
  268. //哪边pano多朝哪边
  269. if (this.boxType == 'cabinet' && this.name.includes('row')) {
  270. if (this.infos.rowboxs.length > 1) {
  271. let k = this.infos.reduce((w, c) => {
  272. return w + c.k
  273. }, 0)
  274. xProp = k > 1 ? 'width' : 'thick'
  275. } else {
  276. //直接使用别的多box的row的方向 ,大多数都相同(会有例外,无所谓了)
  277. let rows = boxesSolid.filter(e => e.boxType == 'cabinet' && e.name.includes('row') && e.dirQua)
  278. rows.sort((a, b) => b.infos.rowboxs.length - a.infos.rowboxs.length)
  279. let box = rows[0]
  280. if (box) {
  281. return (this.dirQua = box.dirQua)
  282. }
  283. }
  284. }
  285. if (!xProp) {
  286. //根据房间的长宽 散落的cabinet。 fire monitor
  287. let { xWidthPossible, yWidthPossible } = getBoxDirProp(this, true)
  288. xProp = this.xProp
  289. /* if(Math.abs(xWidthPossible - yWidthPossible) < 0.3){
  290. let size = new THREE.Vector3()
  291. safeBound.getSize(size)
  292. xWidthPossible += (size.x - size.z) * 0.5
  293. if(xWidthPossible > yWidthPossible){
  294. xProp = 'width'
  295. }
  296. } */
  297. }
  298. if (xProp) {
  299. if (!this.panosDir) getPanosDir(this)
  300. if (xProp == 'width') {
  301. if (Math.abs(this.panosDir['z+']) < Math.abs(this.panosDir['z-'])) {
  302. //朝-z//也就是新坐标系的y
  303. //this.dirQua='下'
  304. dir = new THREE.Vector3(0, 1, 0)
  305. } else {
  306. //朝+z
  307. //this.dirQua='上'
  308. dir = new THREE.Vector3(0, -1, 0)
  309. }
  310. } else {
  311. if (Math.abs(this.panosDir['x+']) < Math.abs(this.panosDir['x-'])) {
  312. //朝-x
  313. //this.dirQua='右'
  314. dir = new THREE.Vector3(-1, 0, 0)
  315. } else {
  316. //朝+x
  317. //this.dirQua='左'
  318. dir = new THREE.Vector3(1, 0, 0)
  319. }
  320. }
  321. }
  322. //addLabel(this.position, this.dirQua)
  323. this.dirQua = convertTool.getQuaByAim(dir, new THREE.Vector3(), new THREE.Vector3(0, 1, 0))
  324. return this.dirQua
  325. /*飞到俯视图查看(不旋转视图,x朝右,z朝下)。以下四个qua是四个墙壁每个墙壁上的dirQua。
  326. _x: 0, _y: -0, _z: 1, _w: 0
  327. _x: 0, _y: 0, _z: -0.707, _w: 0.707 _x: 0, _y: -0, _z: 0.707, _w: 0.707
  328. x: 0, _y: 0, _z: 0, _w: 1
  329. */
  330. }
  331. toJson() {
  332. //转出的坐标系是z朝上的
  333. let category = typeNames[this.boxType]
  334. if (category instanceof Array) {
  335. let scoreMap = new Map()
  336. category = category.slice(0)
  337. category.forEach(e => scoreMap.set(e, 0)) //初始化
  338. //判断boxType: 寻找所使用的box总分最高的boxType
  339. let add = box => {
  340. if (!box) return
  341. let score = scoreMap.get(box.category) + box.score
  342. scoreMap.set(box.category, score)
  343. }
  344. this.traversePair(e => {
  345. add(e.box0)
  346. add(e.box1)
  347. })
  348. category = category.sort((a, b) => {
  349. return scoreMap.get(b) - scoreMap.get(a)
  350. })
  351. category = category[0] //最高分
  352. }
  353. this.getDirection()
  354. let json = {
  355. points: axises.map(axis => math.invertVisionVector(new THREE.Vector3().addVectors(this.position, this.size.clone().multiply(axis).multiplyScalar(0.5))).toArray()),
  356. category,
  357. type: this.boxType,
  358. sid: this.name,
  359. quaternion: this.dirQua.toArray(),
  360. }
  361. return json
  362. }
  363. }
  364. // 2d坐标转3d坐标
  365. let getDirByUV = (uv, pano) => {
  366. // 计算方向向量
  367. let yaw = -uv.x * (Math.PI * 2)
  368. let pitch = Math.PI / 2 - uv.y * Math.PI
  369. let dir = new THREE.Vector3()
  370. dir.copy(Vectors.RIGHT).applyAxisAngle(Vectors.BACK, pitch).applyAxisAngle(Vectors.UP, yaw).applyQuaternion(pano.quaternion)
  371. return dir
  372. }
  373. let getCenterDir = box => {
  374. if (box.centerDir) return
  375. //假设不存在在box中间拍摄的情况,所以y不会横跨两边
  376. let bbox = box.bbox2
  377. let center = { x: getBbox2center(bbox[0], bbox[2]), y: (bbox[1] + bbox[3]) / 2 }
  378. box.bbox2CenterX = center.x
  379. let dir = getDirByUV(center, box.pano)
  380. box.centerDir = dir
  381. let centerTop = { x: center.x, y: bbox[1] }
  382. box.centerTopDir = getDirByUV(centerTop, box.pano)
  383. let centerBtm = { x: center.x, y: bbox[3] }
  384. box.centerBtmDir = getDirByUV(centerBtm, box.pano)
  385. let leftBtm = { x: bbox[0], y: bbox[3] }
  386. box.leftBtmDir = getDirByUV(leftBtm, box.pano)
  387. let rightBtm = { x: bbox[2], y: bbox[3] }
  388. box.rightBtmDir = getDirByUV(rightBtm, box.pano)
  389. }
  390. let getOtherPos = box => {
  391. if (!box.boxType) return
  392. let config = standards[box.boxType]
  393. if (!boundConfirmed) {
  394. if (!config.bottom) {
  395. ray.set(box.pano.position, box.centerBtmDir)
  396. box.btmPosPredict = ray.ray.intersectPlane(groundPlane, new THREE.Vector3()) //没有的话就在空中 (部分air-hanging也会有)。 fire的这个值会较大误差,因为groundY还不确定,但不影响,因只用它射线交点的位置。
  397. if (box.btmPosPredict) {
  398. let dir2d = new THREE.Vector2(box.centerBtmDir.x, box.centerBtmDir.z).normalize()
  399. let { min, max } = standards[box.boxType].widthNormal
  400. min = min * 0.4
  401. max = max * 0.4
  402. let minA = Math.min(Math.abs(dir2d.x), Math.abs(dir2d.y))
  403. const depth = math.linearClamp(minA, 0, 1, min, max)
  404. /* const depth = box.category == typeNames.cabinet ? 0.5 : 0.4
  405. */
  406. dir2d = dir2d.clone().multiplyScalar(depth)
  407. box.btmPosPredict.x += dir2d.x
  408. box.btmPosPredict.z += dir2d.y
  409. //addLabel(box.btmPosPredict,'b_'+box.category+"_"+box.sid, {bgcolor:'#6ff',a:0.1})
  410. //box.btmPosPredict.clamp(safeBound.min, safeBound.max)
  411. }
  412. }
  413. return
  414. }
  415. if (!box.posAtWall && config.atWall > 0) {
  416. //console.log(box.sid, 'getPosWall')
  417. const shrink = config.thick ? config.thick.min : config.widthNormal.min
  418. ray.set(box.pano.position, box.centerDir)
  419. let o = ray.intersectObjects([skyBoxTight])
  420. if (o[0]) box.posAtWall = new THREE.Vector3().addVectors(box.pano.position, box.centerDir.clone().multiplyScalar(o[0].distance - shrink))
  421. //因墙壁不准确,所以还是尽量不用墙的位置
  422. /* if(box.btmPos){
  423. let wallRatio = 0.5;
  424. if(new THREE.Vector3().subVectors(box.btmPos, box.posAtWall).setY(0).length() > 1 )wallRatio = 0.2 //可能墙壁位置不准,靠后了
  425. box.predictCenter = new THREE.Vector3().addVectors(box.btmPos.clone().multiplyScalar(1-wallRatio), box.posAtWall.clone().multiplyScalar(wallRatio))
  426. //box.predictCenter = new THREE.Vector3().addVectors(box.posAtWall, box.btmPos).multiplyScalar(0.5) //也许能当中心点? 虽然y会低一些
  427. addLabel(box.predictCenter, box.sid+'-preC')
  428. } */
  429. }
  430. if (!box.btmPos) {
  431. getBoxBtm(box)
  432. }
  433. if (!box.topPos) {
  434. getBoxTop(box)
  435. }
  436. }
  437. let getUVs = (box, imageWidth, imageHeight) => {
  438. if (box.bbox2) return
  439. let uvs = []
  440. if (!imageWidth) {
  441. imageWidth = global.boxFrame.datas[box.pano.id].imageWidth
  442. imageHeight = global.boxFrame.datas[box.pano.id].imageHeight
  443. }
  444. if (imageWidth != imageWidth || imageHeight != imageHeight) {
  445. console.log(imageWidth, imageHeight)
  446. }
  447. box.bbox2 = box.bbox.map((e, i) => {
  448. //(x1,y1,x2,y2)
  449. return i % 2 == 0 ? e / imageWidth /* + 0.25 */ : e / imageHeight
  450. })
  451. }
  452. let getBoxBase = (box, imageWidth, imageHeight) => {
  453. getBoxType(box)
  454. getUVs(box, imageWidth, imageHeight)
  455. getCenterDir(box)
  456. getOtherPos(box)
  457. }
  458. let getBbox2Diff = (x1, x2) => {
  459. //获取x1-x2,如果x1在x2右边则为正
  460. if (Math.abs(x1 - x2) < 0.5) return x1 - x2
  461. else {
  462. if (x1 > x2) x1 -= 1
  463. else x2 -= 1
  464. return x1 - x2
  465. }
  466. }
  467. let getBbox2center = (x1, x2) => {
  468. //找中间位置
  469. if (Math.abs(x1 - x2) > 0.5) {
  470. //永远找小于180度的那一边
  471. return (x1 + x2 + 1) / 2 //另外半边
  472. } else {
  473. return (x1 + x2) / 2
  474. }
  475. }
  476. let getBoxTop = info => {
  477. /* if(info.sid == 'pano0-11(mix4,8)'){
  478. console.log(5)
  479. } */
  480. if (info.box1) {
  481. let o2 = getIntersect2(info.box0.pano.position, info.box0.centerTopDir, info.box1.pano.position, info.box1.centerTopDir)
  482. info.topPos = o2.pos3d
  483. info.diffHeight = o2.mid2 ? o2.mid2.distanceTo(o2.mid1) : 1
  484. if (info.box0.topPos && info.box1.topPos) {
  485. info.topPos.y = (info.box0.topPos.y + info.box1.topPos.y) / 2 //原先的不准
  486. }
  487. } else {
  488. //取btm上方对应的位置 ( 因为和skybox的交点会因离墙远而偏上或偏下)
  489. let box = info.box0 || info
  490. let btm = box.btmPos
  491. if (!btm) {
  492. btm = getBoxBtm(info)
  493. }
  494. box.topPos = btm.clone() //xz同btm,要求y
  495. let xDelta = btm.x - box.pano.position.x
  496. let zDelta = btm.z - box.pano.position.z
  497. let yDelta
  498. //因为pano有旋转所以btm和top的xz其实是不一样的,所以会有误差。 故这里选择delta较大的
  499. if (Math.abs(xDelta) < Math.abs(zDelta)) {
  500. yDelta = (zDelta * box.centerTopDir.y) / box.centerTopDir.z
  501. /* console.log('use z', box)
  502. if(Math.abs(xDelta)<0.1)console.error('!!!!!!!!!!!!!!!!!!!!!!! xDelta',xDelta, box.sid ) */
  503. } else {
  504. yDelta = (xDelta * box.centerTopDir.y) / box.centerTopDir.x
  505. // console.log('use x', box)
  506. }
  507. box.topPos.y = yDelta + box.pano.position.y
  508. let minHeight = /* info.boxType ? standards[info.boxType].height.min : box.category == typeNames.air ? 0.5 : */ standards[getBoxType(box)].height.min
  509. let diffH = Math.max(box.topPos.y - btm.y, minHeight)
  510. box.topPos.y = btm.y + diffH
  511. info.topPos = box.topPos
  512. /* if (box.sid == 'pano2-1') {
  513. addLabel(box.topPos,'t_'+box.sid,{bgcolor:'#ff4399'})
  514. addLine(box.pano.position,box.centerTopDir, 20)
  515. } */
  516. }
  517. return info.topPos
  518. }
  519. let getBoxBtm = info => {
  520. if (info.box1) {
  521. let o2 = getIntersect2(info.box0.pano.position, info.box0.centerBtmDir, info.box1.pano.position, info.box1.centerBtmDir)
  522. info.btmPos = o2.pos3d
  523. //info.btmPos.y = (info.box0.btmPos.y + info.box1.btmPos.y)/2 //原先的不准
  524. } else {
  525. let box = info.box0 || info
  526. if (!box.btmPos) {
  527. if (box.sid == 'pano0-7') {
  528. console.log(3)
  529. addLine(box.pano.position, box.centerBtmDir, 20)
  530. }
  531. if (!boundConfirmed) {
  532. return box.btmPosPredict
  533. }
  534. ray.set(box.pano.position, box.centerBtmDir)
  535. let o = ray.intersectObjects([skyBoxTight]) //如果skybound有问题,位置就会错
  536. box.btmPosOri = o[0].point.clone()
  537. let depth //缩进 //let depth = Math.abs(o[0].face.normal.y) > 0.9 ? 0.4 : -0.4
  538. getBoxType(box)
  539. let dir2d = new THREE.Vector2(box.centerBtmDir.x, box.centerBtmDir.z).normalize()
  540. if (standards[box.boxType].thick && standards[box.boxType].atWall /* box.boxType == 'battery' */) {
  541. //平贴于墙上,且厚度和宽度相差较大
  542. //注:air-hanging主要用的是posAtWall
  543. let { min, max } = standards[box.boxType].widthNormal
  544. min = min * 0.3
  545. max = max * 0.5
  546. if (!box.xProp) getBoxDirProp(box)
  547. if (box.xProp == 'width') {
  548. depth = math.linearClamp(Math.abs(dir2d.x), 0, 1, min, max)
  549. } else {
  550. depth = math.linearClamp(Math.abs(dir2d.y), 0, 1, min, max)
  551. }
  552. } else {
  553. let w = standards[box.boxType].thick || standards[box.boxType].widthNormal
  554. let w0 = (w.min + w.max) / 2
  555. let min = w0 * 0.5,
  556. max = w0 * 0.8
  557. let minA = Math.min(Math.abs(box.centerBtmDir.x), Math.abs(box.centerBtmDir.z))
  558. depth = math.linearClamp(minA, 0, 0.707, min /* 0.3 */, max /* 0.5 */) //在45度时需要最长的距离。主要针对cabinet
  559. }
  560. if (Math.abs(o[0].face.normal.y) < 0.9) {
  561. if (standards[box.boxType].atWall || o[0].point.y - groundY > 0.3) {
  562. //battery的识别框比较乱,有可能一个电池被识别出好几个,所以
  563. depth *= -1 //at wall
  564. }
  565. }
  566. let dir2d1 = dir2d.clone().multiplyScalar(depth)
  567. box.btmPos = o[0].point.clone()
  568. box.btmPos.x += dir2d1.x
  569. box.btmPos.z += dir2d1.y
  570. //addLabel(box.btmPos,'b_'+box.sid,{bgcolor:'#ff4399'})
  571. }
  572. info.btmPos = box.btmPos
  573. }
  574. return info.btmPos
  575. }
  576. let getIntersect = (pano0Pos, dir0, pano1Pos, dir1) => {
  577. let pos0 = new THREE.Vector3().addVectors(pano0Pos, dir0)
  578. let pos1 = new THREE.Vector3().addVectors(pano1Pos, dir1)
  579. /* var {pos3d, mid1, mid2, behind} = math.getLineIntersect({ A: pano0Pos.clone(), B: pano1Pos.clone(), p1: pos0, p2: pos1 })
  580. */ //三维线若接近平行,算出的交点可能很近,不是实际应该无交点才对
  581. var pos2d = math.isLineIntersect(
  582. [
  583. { x: pano0Pos.x, y: pano0Pos.z },
  584. { x: pos0.x, y: pos0.z },
  585. ],
  586. [
  587. { x: pano1Pos.x, y: pano1Pos.z },
  588. { x: pos1.x, y: pos1.z },
  589. ],
  590. true
  591. ) //优先考虑水平面方向的交点
  592. if (pos2d) {
  593. let y0 = ((pos2d.x - pano0Pos.x) * dir0.y) / dir0.x + pano0Pos.y
  594. let y1 = ((pos2d.x - pano1Pos.x) * dir1.y) / dir1.x + pano1Pos.y
  595. //console.log(y1-y0)
  596. let pos3d = new THREE.Vector3(pos2d.x, (y0 + y1) / 2, pos2d.y)
  597. return { pos3d, diffHeight: Math.abs(y0 - y1) } //diffHeight越小越好
  598. }
  599. }
  600. //究竟哪个比较准 - - 可能两个都判断?
  601. let getIntersect2 = (pano0Pos, dir0, pano1Pos, dir1) => {
  602. let pos0 = new THREE.Vector3().addVectors(pano0Pos, dir0)
  603. let pos1 = new THREE.Vector3().addVectors(pano1Pos, dir1)
  604. let o = math.getLineIntersect2({ A: pano0Pos.clone(), B: pano1Pos.clone(), p1: pos0, p2: pos1, dir0, dir1 }) //不用getLineIntersect,因为这个针对热点写的,当无交点时选用的点不是想要的
  605. if (!o.pos3d) {
  606. console.error('getIntersect2 no result? ?')
  607. }
  608. return o
  609. }
  610. let getBoxPos = info => {
  611. let boxType = getBoxType(info)
  612. return (
  613. (info.preDealRes && info.preDealRes.position) ||
  614. info.center ||
  615. (boxType && (standards[boxType].atWall > 0.5 && standards[boxType].bottom ? info.posAtWall : info.btmPos || info.btmPosPredict)) ||
  616. info.posAtWall
  617. )
  618. }
  619. let isType = (category, type) => {
  620. return type == category || typeNames[type] instanceof Array ? typeNames[type].includes(category) : typeNames[type] == category
  621. }
  622. let getBoxType = info => {
  623. if (info.boxType) return info.boxType
  624. let category = info.category || info.box0.category
  625. if (category == 'rowBigBox') info.boxType = 'rowBigBox'
  626. else {
  627. let type
  628. if (category == 'ac_switchboard') {
  629. console.log(1)
  630. }
  631. for (let i in typeNames) {
  632. /* if (i == category || typeNames[i] instanceof Array ? typeNames[i].includes(category) : typeNames[i] == category) {
  633. type = i
  634. break //type = typeNamesReverse[type]
  635. } */
  636. if (isType(category, i)) {
  637. type = i
  638. break
  639. }
  640. }
  641. /* if(type == 'ac' || type == 'dc'){
  642. type = 'electric' //合并
  643. } */
  644. info.boxType = type
  645. }
  646. return info.boxType
  647. }
  648. /* let getBoxType = info => {
  649. let type = info.category || info.box0.category
  650. if (type == 'air') {
  651. let btm = info.btmPos || info.btmPos //btmPosAtWall
  652. if (!btm) {
  653. btm = getBoxBtm(info)
  654. }
  655. if (!btm) return
  656. let center = info.posAtWall || (info.preDealRes && info.preDealRes.position) || info.center
  657. const s = standards['air-hanging']
  658. if (btm.y - groundY > s.bottom.min) {
  659. let h0 = btm.y - groundY
  660. let h1 = (modelBound.max.y - center.y) / (modelBound.max.y - modelBound.min.y)
  661. let h2 = center.y - btm.y
  662. let score = h0 * 2 - h1 * 3 - h2 * 3
  663. if (score > 0) {
  664. type = 'air-hanging'
  665. }
  666. //console.error( score, h0,h1,h2, info.sid||info.name)
  667. } //注意:如果air被遮住底部,露出的部分只有一点,还是有可能被识别成air-hanging。只能希望
  668. //console.error( type, info.sid||info.name)
  669. }
  670. if (info.box0) {
  671. info.boxType = type
  672. //info.box1 && (info.box1.type = type) //因为box0和box1不一定匹配,所以不能直接赋值
  673. } else {
  674. info.type = type
  675. }
  676. } */
  677. let getPanosDir = (info, center) => {
  678. center = center || getBoxPos(info)
  679. let dirs = { 'x+': 0, 'x-': 0, 'z+': 0, 'z-': 0, got: false }
  680. let getDirs = () => {
  681. //靠墙的在它到墙之间是不会有漫游点的
  682. if (dirs.got || !center) return
  683. player.model.panos.list.forEach(pano => {
  684. let dir = new THREE.Vector3().subVectors(pano.position, center)
  685. if (dir.x > 0) {
  686. dirs['x+'] += dir.x
  687. } else {
  688. dirs['x-'] += dir.x
  689. }
  690. if (dir.z > 0) {
  691. dirs['z+'] += dir.z
  692. } else {
  693. dirs['z-'] += dir.z
  694. }
  695. })
  696. dirs.got = true
  697. }
  698. getDirs()
  699. if (info.panosDir) {
  700. console.error('already has dir')
  701. }
  702. info.panosDir = dirs
  703. return dirs
  704. }
  705. let getBoxDirProp = (info, force) => {
  706. //仅适用于方形单个房间房间,不可以是多边形、两个房间
  707. let xProp, yProp
  708. if (info.name == 'pano10-8&pano12-6') {
  709. console.log(4)
  710. }
  711. //if (info.boxType == 'battery' || info.boxType == 'air-hanging' || info.boxType == 'air' || info.category == 'battery' || info.category == 'air') {
  712. if ((standards[info.boxType].atWall && standards[info.boxType].thick) || force) {
  713. //根据比例判断
  714. /* let r1 = Math.abs((center.x - skyBoxTight.position.x) / (center.z - skyBoxTight.position.z))
  715. let r2 = player.model.size.x / player.model.size.z
  716. if(!math.closeTo(r1,r2, 0.05)){
  717. if (r1<r2) {
  718. ;(xProp = 'width'), (yProp = 'thick') //贴附x(横)墙
  719. } else {
  720. ;(xProp = 'thick'), (yProp = 'width') //贴附y(竖)墙
  721. }
  722. matchInfo.xProp = xProp
  723. matchInfo.yProp = yProp
  724. } */
  725. //根据距离判断
  726. let center = getBoxPos(info)
  727. let bound = safeBound
  728. let minXDiff = Math.min(center.x - bound.min.x, bound.max.x - center.x)
  729. let minYDiff = Math.min(center.z - bound.min.z, bound.max.z - center.z)
  730. let dirs = getPanosDir(info, center)
  731. let noX = dirs['x+'] == 0 || dirs['x-'] == 0,
  732. noZ = dirs['z+'] == 0 || dirs['z-'] == 0
  733. //yi6dsPTWwFM klkcWindow pano0-1&pano2-0
  734. if (dirs['x+'] == 0 && dirs['x-'] != 0) {
  735. minXDiff = bound.max.x - center.x
  736. } else if (dirs['x-'] == 0 && dirs['x+'] != 0) {
  737. minXDiff = center.x - bound.min.x
  738. }
  739. if (dirs['z+'] == 0 && dirs['z-'] != 0) {
  740. minYDiff = bound.max.z - center.z
  741. } else if (dirs['z-'] == 0 && dirs['z+'] != 0) {
  742. minYDiff = center.z - bound.min.z
  743. }
  744. let xWidthPossible = noZ ? 1 : 0,
  745. yWidthPossible = noX ? 1 : 0
  746. xWidthPossible += minXDiff - minYDiff
  747. if (!math.closeTo(xWidthPossible, yWidthPossible, 0.2) && (minXDiff < 0.7 || minYDiff < 0.7)) {
  748. if (xWidthPossible < yWidthPossible) {
  749. ;(xProp = 'thick'), (yProp = 'width') //贴附y(竖)墙
  750. } else {
  751. ;(xProp = 'width'), (yProp = 'thick') //贴附x(横)墙
  752. }
  753. }
  754. if (!xProp) {
  755. if (!force) {
  756. //force的话是fire等无方向的类型
  757. if (info.category) {
  758. //是box
  759. info.pose || (info.pose = getBoxPoseByPos(info, center))
  760. xWidthPossible += info.pose.xWidthPossible
  761. yWidthPossible += info.pose.yWidthPossible
  762. } else {
  763. traverse(info, info => {
  764. if (info.boxposes) {
  765. //是matchInfo
  766. info.boxposes.forEach(e => {
  767. xWidthPossible += e.xWidthPossible
  768. yWidthPossible += e.yWidthPossible
  769. })
  770. }
  771. })
  772. }
  773. }
  774. /* if(noX!=noZ){
  775. if(noX)xWidthPossible
  776. } */
  777. if (xWidthPossible > yWidthPossible) {
  778. ;(xProp = 'width'), (yProp = 'thick') //贴附x(横)墙
  779. } else {
  780. ;(xProp = 'thick'), (yProp = 'width') //贴附y(竖)墙
  781. }
  782. }
  783. xProp && ((info.xProp = xProp), (info.yProp = yProp))
  784. return { xWidthPossible, yWidthPossible }
  785. }
  786. }
  787. let preDealBox = matchInfo => {
  788. if (matchInfo.preDealRes || !matchInfo.center) return
  789. matchInfo.preDealRes = {}
  790. matchInfo.boxType || getBoxType(matchInfo)
  791. let config = standards[matchInfo.boxType]
  792. let minWidth = config.widthNormal.min
  793. let needGetPose
  794. let dis = safeBound.distanceToPoint(matchInfo.center)
  795. if (matchInfo.name == 'pano2-5') {
  796. console.log(4)
  797. }
  798. if (dis > 0.3 && !config.tiny && !config.tinyXZ) {
  799. //Tmo1vLp9Q13: hlkcWindow超出才准确
  800. //tiny的位置优先级高于bound,因为他们可以确定bound
  801. /* matchInfo.str && matchInfo.str.includes('outsideBound') */
  802. //const shrink = minWidth * 0.85
  803. //addLabel(matchInfo.center, '原')
  804. let finalPos = matchInfo.center.clone().clamp(safeBound.min, safeBound.max)
  805. matchInfo.preDealRes.position = finalPos
  806. //addLabel(finalPos, 'finalPos')
  807. getBoxType(matchInfo)
  808. needGetPose = true
  809. }
  810. let center = getBoxPos(matchInfo)
  811. if (needGetPose || !matchInfo.boxposes) {
  812. matchInfo.boxposes = []
  813. ;[matchInfo.box0, matchInfo.box1].forEach(box => {
  814. box && matchInfo.boxposes.push(getBoxPoseByPos(box, center))
  815. })
  816. }
  817. //-----------------
  818. getBoxDirProp(matchInfo)
  819. }
  820. let getBoxPoseByPos = (box, centerPos, addDis = 0) => {
  821. //当得知box的大概位置时,求box在这个角度上的宽度、朝向
  822. //在这个方向看的box的宽度
  823. let config = standards[box.boxType]
  824. let angle = getBbox2Diff(box.bbox2[2], box.bbox2[0]) * Math.PI //角度的一半
  825. let dis = new THREE.Vector3().subVectors(box.pano.position, centerPos).setY(0).length() + addDis
  826. let projectWidth = 2 * Math.tan(angle) * dis //投影宽度 (准确的投影宽度无法求得,只能近似)
  827. let camDir = /* new THREE.Vector2(centerPos.x-box.pano.position.x, centerPos.z-box.pano.position.z).normalize() */ box.centerDir.clone().setY(0).normalize()
  828. let camTangent = math.getNormal({
  829. points: [
  830. { x: 0, y: 0 },
  831. /* camDir */ { x: camDir.x, y: camDir.z },
  832. ],
  833. }) //视线切线方向
  834. camTangent.x = Math.abs(camTangent.x)
  835. camTangent.y = Math.abs(camTangent.y)
  836. /* if (box.sid == 'pano4-6') {
  837. console.log(7)
  838. } */
  839. const maxWidth = config.widthNormal.max
  840. const minWidth = config.widthNormal.min
  841. let minProjectWidth //= (camTangent.x + camTangent.y) * minWidth
  842. let maxProjectWidth //= (camTangent.x + camTangent.y) * maxWidth
  843. let maxX, maxY, minX, minY
  844. if (!standards[box.boxType].thick) {
  845. minProjectWidth = (camTangent.x + camTangent.y) * minWidth
  846. maxProjectWidth = (camTangent.x + camTangent.y) * maxWidth //该角度下该类型允许的最大投影距离
  847. maxX = THREE.MathUtils.clamp((projectWidth - minWidth * camTangent.y) / camTangent.x, minWidth, maxWidth) //可得x的最大值(假设y为最小值)
  848. maxY = THREE.MathUtils.clamp((projectWidth - minWidth * camTangent.x) / camTangent.y, minWidth, maxWidth) //可得y的最大值(假设x为最小值)
  849. minX = THREE.MathUtils.clamp((projectWidth - maxWidth * camTangent.y) / camTangent.x, minWidth, maxWidth) //可得x的最小值(假设y为最大值)
  850. minY = THREE.MathUtils.clamp((projectWidth - maxWidth * camTangent.x) / camTangent.y, minWidth, maxWidth) //可得y的最小值(假设x为最大值)
  851. } else {
  852. const minThick_ = config.thick.min
  853. const minWidth_ = config.width.min
  854. const maxThick_ = config.thick.max
  855. const maxWidth_ = config.width.max
  856. let maxProjectWidth1 = camTangent.x * maxWidth_ + camTangent.y * maxThick_
  857. let maxProjectWidth2 = camTangent.x * maxThick_ + camTangent.y * maxWidth_
  858. let minProjectWidth1 = camTangent.x * minWidth_ + camTangent.y * minThick_
  859. let minProjectWidth2 = camTangent.x * minThick_ + camTangent.y * minWidth_
  860. minProjectWidth = Math.min(minProjectWidth1, minProjectWidth2)
  861. maxProjectWidth = Math.max(maxProjectWidth1, maxProjectWidth2)
  862. let a = (camTangent.x + camTangent.y) * maxWidth
  863. //console.log('diffaaaaaa',maxProjectWidth,a, box.sid)
  864. maxX = THREE.MathUtils.clamp((projectWidth - minThick_ * camTangent.y) / camTangent.x, minThick_, maxWidth_) //可得x的最大值(假设y为最小值)
  865. maxY = THREE.MathUtils.clamp((projectWidth - minThick_ * camTangent.x) / camTangent.y, minThick_, maxWidth_) //可得y的最大值(假设x为最小值)
  866. minX = THREE.MathUtils.clamp((projectWidth - maxWidth_ * camTangent.y) / camTangent.x, minThick_, maxWidth_)
  867. minY = THREE.MathUtils.clamp((projectWidth - maxWidth_ * camTangent.x) / camTangent.y, minThick_, maxWidth_)
  868. }
  869. /*
  870. let maxX = projectWidth / camTangent.x //可得x的最大值(假设y为0)
  871. let maxY = projectWidth / camTangent.y //可得y的最大值(假设x为0)
  872. */
  873. //判断方向
  874. let o = { box, projectWidth, camTangent, maxProjectWidth, minProjectWidth, dis, maxX, maxY, minX, minY }
  875. if (config.atWall > 0 /* isType(box.category,battery) || isType(box.category,air) */) {
  876. //为了获取朝向
  877. o.xWidthPossible = -Math.abs(projectWidth - camTangent.x * maxWidth - camTangent.y * minWidth)
  878. o.yWidthPossible = -Math.abs(projectWidth - camTangent.x * minWidth - camTangent.y * maxWidth)
  879. //在接近45度时容易不准。另外如果被遮住一部分更会错,因此尽量不让被遮住的匹配
  880. }
  881. return o
  882. }
  883. let getPoseScore = (boxposes, boxType) => {
  884. let score = 0
  885. const minDis = 1.5
  886. /* if (boxposes[0].box.sid == 'pano12-13' && boxposes[1].box.sid == 'pano8-6') {
  887. console.log(4)
  888. } */
  889. boxposes.forEach(pose => {
  890. //pose.lowR = pose.dis < minDis ? Math.pow(THREE.MathUtils.smoothstep(pose.dis / minDis, 0, 1),2) : 1 //太近的话误差大
  891. pose.lowR = pose.dis < minDis ? Math.pow(pose.dis / minDis, 1.4) : 1 //太近的话误差大
  892. if (pose.projectWidth > pose.maxProjectWidth) {
  893. score += Math.pow((pose.projectWidth / pose.maxProjectWidth - 1) * pose.lowR, 2) * 500 //超过的话数字较大所以乘的数小一些
  894. } else if (/* isSingle && */ pose.projectWidth < pose.minProjectWidth) {
  895. score += Math.pow((pose.minProjectWidth / pose.projectWidth - 1) * pose.lowR, 2) * 500
  896. }
  897. let { min, max } = standards[boxType].widthNormal
  898. if ((standards[boxType].atWall == 1 && min < 0.3) || (boxposes.length == 2 && boxType == 'battery')) {
  899. let { min, max } = standards[boxType].widthNormal
  900. //let r = Math.max(0.001, (pose.projectWidth - min) / (max - min))
  901. let diff = max - min
  902. let r = math.linearClamp(pose.projectWidth, min, min + diff * 0.5, 600, 0)
  903. //if(boxposes.length == 2 && boxType == 'battery'){//由于battery经常出现遮挡,导致方向出错,因此尽量不匹配projectWidth较短的,尽管这本有可能是对的
  904. score += r
  905. //}
  906. /* if (min < 0.3 && r < 0.5) {
  907. //从贴近墙面的位置看侧面的话,容易被挡住,不准,尤其是电箱
  908. score += ((max - min) / r / min) * 5
  909. } */
  910. }
  911. })
  912. score = Math.min(score, 1300) //压低一点,因为得的宽度可能不准
  913. if (boxposes.length == 2) {
  914. //每一个方向对应有四个方向(每个象限一个)看到的projectWidth应该接近。
  915. //先把camTangent转化为第一个象限的
  916. let camTangent0 = new THREE.Vector2(Math.abs(boxposes[0].camTangent.x), Math.abs(boxposes[0].camTangent.y))
  917. let camTangent1 = new THREE.Vector2(Math.abs(boxposes[1].camTangent.x), Math.abs(boxposes[1].camTangent.y))
  918. let a = camTangent0.dot(camTangent1)
  919. if (a > 0.8) {
  920. //WcLVXvmV9AU
  921. //0.9: 25度之内. 0.8: 36.8度之内
  922. let diff = Math.abs(boxposes[0].projectWidth - boxposes[1].projectWidth)
  923. boxposes.score2 = a * diff * 1300 * boxposes[0].lowR * boxposes[1].lowR
  924. score += boxposes.score2
  925. //console.warn('在同一个方向看到的projectWidth应该接近。 ', diff)
  926. }
  927. boxposes.camTangentCos = a
  928. }
  929. //要不要加上minX等的差距?
  930. score = Math.min(score, 1200)
  931. return -score
  932. }
  933. let getBoxSize = info => {
  934. if (info.boxType == 'groundBar') {
  935. console.log(1)
  936. }
  937. if (info.size) return
  938. //console.warn('开始算 ' + info.name)
  939. let exStr = '',
  940. warnStr = ''
  941. let x, y //求对角线的向量 x>0,y>0
  942. //假设盒子的长宽为x,y (x>0,y>0),视线切线单位向量为(k,m),投影距离:x'k+y'm.(x'是正负x,y'是正负y)
  943. //由于盒子的对角线有四个可选方向,(类似四个象限) 则需要能使投影距离最长的一个对角线向量。
  944. //如,当k<0,m>0时,要使xk+ym 最大,必有x<0,y>0. 故 x = -x', y = y', 故 投影距离:x'k+y'm = x(-k)+ym 。
  945. //故无论km的符号如何,只要变为正数,再去联立方程即可得xy。(相当于切线转到第一象限)
  946. //注:但是因为无法获取准确的投影距离(角平分线左右两边的端点到角平分线的距离不相等,垂足也无法确定),所以所算的误差非常大。
  947. if (info.name == 'pano6-13&pano4-13') {
  948. console.log(6)
  949. }
  950. let center = getBoxPos(info)
  951. let oriX, oriY
  952. if (info.predictSize) {
  953. ;(x = oriX = info.predictSize.x), (y = oriY = info.predictSize.y)
  954. } else {
  955. if (info.box1) {
  956. let x1 = info.boxposes[0].camTangent.x,
  957. x2 = info.boxposes[1].camTangent.x,
  958. y1 = info.boxposes[0].camTangent.y,
  959. y2 = info.boxposes[1].camTangent.y,
  960. w1 = /* info.boxposes[0].projectWidth, */ THREE.MathUtils.clamp(info.boxposes[0].projectWidth, info.boxposes[0].minProjectWidth, info.boxposes[0].maxProjectWidth * 1.1), //校准。如果projectwidth不准那算出来更不准. 但XswQxwmn2ZC的里侧电池是前者更准
  961. w2 = /* info.boxposes[1].projectWidth */ THREE.MathUtils.clamp(info.boxposes[1].projectWidth, info.boxposes[1].minProjectWidth, info.boxposes[1].maxProjectWidth * 1.1)
  962. //如果识别到柜门上,(柜体被遮住了),整体中心就会在柜门上,且厚度小于真实值。
  963. if (x1 == 0) {
  964. y = w1
  965. x = (w2 - y2 * y) / x2
  966. } else {
  967. //联立方程得:
  968. y = (w2 - (x2 / x1) * w1) / (y2 - (x2 / x1) * y1)
  969. x = (w1 - y1 * y) / x1
  970. }
  971. //console.log('xy', { x, y })
  972. ;(x < 0.3 || x > 1.4) && (exStr += ' x:' + math.toPrecision(x, 2))
  973. ;(y < 0.3 || y > 1.4) && (exStr += ' y:' + math.toPrecision(y, 2))
  974. if (y < 0 || x < 0) {
  975. //console.log('<0 ?????????')
  976. warnStr = x < 0 ? 'x<0!' : 'y<0!'
  977. }
  978. ;(oriX = x), (oriY = y)
  979. } else {
  980. //single pano data
  981. //将maxX maxY 限定在标准范围内
  982. if (info.xProp) {
  983. let widthValue = standards[info.boxType].width
  984. let thickValue = standards[info.boxType].thick
  985. let maxX, maxY, minX, minY
  986. if (info.xProp == 'width') {
  987. /* x = oriX = THREE.MathUtils.clamp(info.boxposes[0].maxX, widthValue.min, widthValue.max)
  988. y = oriY = THREE.MathUtils.clamp(info.boxposes[0].maxY, thickValue.min, thickValue.max)
  989. */
  990. maxX = THREE.MathUtils.clamp(info.boxposes[0].maxX, widthValue.min, widthValue.max) //AG0bi2fhb3 需要将得到的minX等这四项都clamp后再平均
  991. maxY = THREE.MathUtils.clamp(info.boxposes[0].maxY, thickValue.min, thickValue.max)
  992. minX = THREE.MathUtils.clamp(info.boxposes[0].minX, widthValue.min, widthValue.max)
  993. minY = THREE.MathUtils.clamp(info.boxposes[0].minY, thickValue.min, thickValue.max)
  994. } else {
  995. /* x = oriX = THREE.MathUtils.clamp(info.boxposes[0].maxX, thickValue.min, thickValue.max)
  996. y = oriY = THREE.MathUtils.clamp(info.boxposes[0].maxY, widthValue.min, widthValue.max)
  997. */
  998. maxX = THREE.MathUtils.clamp(info.boxposes[0].maxX, thickValue.min, thickValue.max)
  999. maxY = THREE.MathUtils.clamp(info.boxposes[0].maxY, widthValue.min, widthValue.max)
  1000. minX = THREE.MathUtils.clamp(info.boxposes[0].minX, thickValue.min, thickValue.max)
  1001. minY = THREE.MathUtils.clamp(info.boxposes[0].minY, widthValue.min, widthValue.max)
  1002. }
  1003. x = oriX = (maxX + minX) / 2
  1004. y = oriY = (maxY + minY) / 2
  1005. } else {
  1006. //x = oriX = y = oriY = (min+max)/2
  1007. let standard = standards[info.boxType].widthNormal
  1008. x = oriX = THREE.MathUtils.clamp(info.boxposes[0].maxX, standard.min, standard.max)
  1009. y = oriY = THREE.MathUtils.clamp(info.boxposes[0].maxY, standard.min, standard.max)
  1010. }
  1011. }
  1012. //按正常来说,得到的x,y都应>0,但是由于箱子会被遮挡,导致投影宽度比真实的小,算出的也不准,可能是负数
  1013. //所以手动将过小的宽度矫正
  1014. }
  1015. /* if(info.name == "pano2-6"){
  1016. console.log(7)
  1017. } */
  1018. let height
  1019. if (standards[info.boxType].bottom) {
  1020. //悬挂的
  1021. //挂式空调最好把长宽固定。 不过极少出错
  1022. //center.y -= 0.1 //很可能过高
  1023. height = standards[info.boxType].height.standard
  1024. getBoxBtm(info)
  1025. if (height) {
  1026. let d = center.y - info.btmPos.y
  1027. center.y -= THREE.MathUtils.clamp((d - height / 2) / 2, -0.1, 0.1) //如果中心点到底部的距离和height的一半不同,中心点移动差值的一半
  1028. } else {
  1029. let btmY = info.btmPos.y
  1030. /* let ys = [info.btmPos.y]//info.btmPos.y蛮不准的
  1031. if(info.box0){
  1032. ys.push(info.box0.btmPos.y)
  1033. }
  1034. if(info.box1){
  1035. ys.push(info.box1.btmPos.y)
  1036. }
  1037. btmY = ys.reduce((w,c)=>{return w+c},0)
  1038. btmY/=ys.length //平均 */
  1039. height = (center.y - btmY) * 2
  1040. }
  1041. } else {
  1042. if (!info.topPos) getBoxTop(info)
  1043. height = info.topPos.y - groundY
  1044. }
  1045. let o = restrictSize(x, height, y, info)
  1046. ;(x = o.x), (height = o.y), (y = o.z)
  1047. if (standards[info.boxType].widthSame) {
  1048. x = y = (x + y) / 2
  1049. }
  1050. info.size = new THREE.Vector3(x, height, y)
  1051. info.sizeAdjust = Math.pow(Math.abs(x - oriX), 1.3) + Math.pow(Math.abs(y - oriY), 1.3) //计算得到的值和标准值之间的差距,可以反映该info的匹配分值
  1052. if (info.sizeAdjust) info.score = (info.score || 0) - Math.min(info.sizeAdjust * 100, 400)
  1053. ;(info.size.oriX = oriX), (info.size.oriY = oriY)
  1054. ;(info.exStr = exStr), (info.warnStr = warnStr)
  1055. }
  1056. let restrictSize = (x, y, z, info) => {
  1057. let s
  1058. let { xProp, yProp } = info
  1059. if (xProp != void 0) {
  1060. var { min, max } = standards[info.boxType][xProp]
  1061. x = THREE.MathUtils.clamp(x, min, max)
  1062. var { min, max } = standards[info.boxType][yProp]
  1063. z = THREE.MathUtils.clamp(z, min, max)
  1064. s = true
  1065. }
  1066. if (!s) {
  1067. var { min, max } = standards[info.boxType].widthNormal
  1068. x = THREE.MathUtils.clamp(x, min, max)
  1069. z = THREE.MathUtils.clamp(z, min, max)
  1070. }
  1071. var { min, max } = standards[info.boxType].height
  1072. y = THREE.MathUtils.clamp(y, min, max)
  1073. return { x, y, z }
  1074. }
  1075. let getMixBox = (box0, box1) => {
  1076. //重叠部分
  1077. let box = new THREE.Box2()
  1078. box.min.set(Math.max(box0.min.x, box1.min.x), Math.max(box0.min.y, box1.min.y))
  1079. box.max.set(Math.min(box0.max.x, box1.max.x), Math.min(box0.max.y, box1.max.y))
  1080. return box
  1081. }
  1082. let getLeftRight = boxArr => {
  1083. //获取pano的boxes中最左和最右的bbox.x
  1084. let lefts = boxArr.map(e => e.bbox2[0])
  1085. let rights = boxArr.map(e => e.bbox2[2])
  1086. lefts.sort((a, b) => getBbox2Diff(a, b))
  1087. rights.sort((a, b) => getBbox2Diff(b, a))
  1088. let leftX = lefts[0] //最左
  1089. let rightX = rights[0] //最右
  1090. return {
  1091. leftX,
  1092. rightX,
  1093. }
  1094. }
  1095. ;(global.searchCount1 = 0), (global.escapeCount1 = 0)
  1096. let searchPair = (beginItem, group0_, group1_, parentPairs, resultPairs, evaluateFun, minScore) => {
  1097. //配对结果个数为n!,其中n是每组的元素个数。注意当n=10时,已经有40320个,非常恐怖。
  1098. let pair = [],
  1099. parentExit = !!parentPairs
  1100. let removeParent = () => {
  1101. //元结点裂变出多个,来装新的pair
  1102. if (parentExit) {
  1103. let i = resultPairs.indexOf(parentPairs)
  1104. resultPairs.splice(i, 1)
  1105. parentExit = false
  1106. }
  1107. }
  1108. if (!parentPairs) {
  1109. //首次
  1110. if (group0_.length == 0 || group1_.length == 0) return
  1111. //保证第一个的个数<=第二个,否则第一组多出来的永远匹配不上
  1112. if (group0_.length > group1_.length) {
  1113. let t = group0_
  1114. group0_ = group1_
  1115. group1_ = t
  1116. }
  1117. beginItem = group0_[0]
  1118. let complex = Object.keys(global.boxFrame.datas).length
  1119. if (complex < 10) evaluateFun = null
  1120. else minScore = math.linearClamp(complex, 10, 80, -4000, -500)
  1121. //console.log('searchPair length',group0_.length,group1_.length)
  1122. }
  1123. searchCount1++
  1124. for (let j = 0; j < group1_.length; j++) {
  1125. pair = [beginItem, group1_[j]]
  1126. //if(pair[0].sid == 'void' || pair[1].sid == 'void')continue
  1127. let evaluate
  1128. if (evaluateFun /* && !(pair[0].sid == 'void' || pair[1].sid == 'void') */) {
  1129. evaluate = evaluateFun(pair[0], pair[1])
  1130. if (evaluate == void 0 || evaluate.score <= minScore) {
  1131. //console.log('因为评估出匹配可能性低所以跳过',pair[0],pair[1],evaluate)
  1132. escapeCount1++
  1133. continue
  1134. }
  1135. }
  1136. let newPairs //用来存放该组pair
  1137. if (parentPairs) {
  1138. removeParent()
  1139. newPairs = parentPairs.slice(0) //复制
  1140. newPairs.push(pair)
  1141. } else {
  1142. newPairs = [pair] //新的容器
  1143. }
  1144. resultPairs.push(newPairs)
  1145. let newGroup0 = group0_.slice(0)
  1146. let newGroup1 = group1_.slice(0)
  1147. let index = newGroup0.indexOf(pair[0])
  1148. newGroup0.splice(index, 1)
  1149. index = newGroup1.indexOf(pair[1])
  1150. newGroup1.splice(index, 1)
  1151. if (newGroup0.length > 0 && newGroup1.length > 0) {
  1152. searchPair(newGroup0[0], newGroup0, newGroup1, newPairs, resultPairs, evaluateFun, minScore)
  1153. }
  1154. }
  1155. }
  1156. //如果第一个元素就和后面所有的都不匹配,就直接返回了怎么办?
  1157. export default class PanoBoxFrame extends THREE.Group {
  1158. constructor(player_, ifAnalyze, dataList) {
  1159. super()
  1160. this.clear()
  1161. player = player_
  1162. player.model.add(this)
  1163. global.boxFrame = this
  1164. this.ifAnalyze = ifAnalyze
  1165. this.wireframes = new THREE.Object3D()
  1166. this.wireframes.name = 'wireframes'
  1167. this.add(this.wireframes)
  1168. this.matchScoreMap = {}
  1169. this.bindEvents()
  1170. meshGroup = new THREE.Object3D()
  1171. meshGroup.name = 'testBox'
  1172. this.add(meshGroup)
  1173. this.compute(dataList)
  1174. }
  1175. async compute(dataList) {
  1176. startTime = Date.now()
  1177. this.datas = {}
  1178. this.datasMixed = {}
  1179. this.boxesSolid = boxesSolid
  1180. /* let metadata = player.$app.store.getValue('metadata') //await player.$app.resource.metadata()
  1181. isLaser = metadata.sceneFrom == 'laser' */
  1182. let compu = 0
  1183. let beginCompute = () => {
  1184. //获取匹配分数
  1185. let getMatchScore = (box0, box1, { isSingle, center, onlyGet, dontCheckDis } = {}) => {
  1186. let name0 = box0.sid + '&' + box1.sid
  1187. let name1 = box1.sid + '&' + box0.sid
  1188. let boxType = getBoxType(box0)
  1189. let matchInfo0 = this.matchScoreMap[boxType][name0]
  1190. let matchInfo1 = this.matchScoreMap[boxType][name1]
  1191. let matchInfo = matchInfo0 || matchInfo1
  1192. if (onlyGet) return matchInfo
  1193. let name
  1194. if (!matchInfo) {
  1195. name = name0
  1196. matchInfo = { name, box0, box1, center }
  1197. this.matchScoreMap[boxType][name] = matchInfo
  1198. } else {
  1199. return matchInfo
  1200. }
  1201. if (name == 'pano6-9&pano8-4') {
  1202. console.log(5)
  1203. }
  1204. getBoxBase(box0)
  1205. getBoxBase(box1)
  1206. let A = box0.pano.position.clone()
  1207. let B = box1.pano.position.clone()
  1208. let AB = new THREE.Vector3().subVectors(B, A)
  1209. let AB2d = new THREE.Vector2(AB.x, AB.z).normalize()
  1210. 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()
  1211. 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()
  1212. let angleA = Math.acos(AB2d.dot(AP12d))
  1213. let angleB = Math.PI - Math.acos(AB2d.dot(BP22d))
  1214. let score = 100,
  1215. str = []
  1216. if (angleA + angleB > Math.PI + 0.2) {
  1217. //无交点(比180大是因为中心角度有误差,所以给一定的容错)
  1218. //console.log(`${panoId0}的第${box0.index}个与${panoId1}的第${box1.index}个因角度大于180度 不匹配`)
  1219. return Object.assign(matchInfo, { score: -5000, str: ['angle>180'] })
  1220. }
  1221. if (box0.type != box1.type) {
  1222. return Object.assign(matchInfo, { score: -5000, str: ['typeNotSame'] })
  1223. }
  1224. if (matchInfo.dirAngleXZ == void 0) {
  1225. matchInfo.dirAngleXZ = THREE.MathUtils.radToDeg(Math.acos(AP12d.dot(BP22d))) //需要尽量接近90度算出来的交点会比较准
  1226. matchInfo.minAng = Math.min(180 - matchInfo.dirAngleXZ, matchInfo.dirAngleXZ) //角度小的getIntersect2容易算不准
  1227. if (isSingle) {
  1228. let bestDisSquared = 2
  1229. //单个匹配单个,而非多个匹配多个(没有固定到两个漫游点),所以可以直接寻找最优角度
  1230. score += Math.sin(THREE.MathUtils.degToRad(matchInfo.dirAngleXZ)) * 300
  1231. score += matchInfo.dirAngleXZ //另外角度越大越不容易偏向一边
  1232. score -= Math.abs(getBoxPos(box0).distanceToSquared(box0.pano.position) - bestDisSquared) * 10
  1233. score -= Math.abs(getBoxPos(box1).distanceToSquared(box1.pano.position) - bestDisSquared) * 10
  1234. }
  1235. }
  1236. let shinkRatio = 1
  1237. let btmPos0 = box0.btmPos || box0.btmPosPredict //fire类型先用btmPosPredict,测定groundY后才有btmPos
  1238. let btmPos1 = box1.btmPos || box1.btmPosPredict
  1239. if (!dontCheckDis) {
  1240. //let r = box0.boxType == 'air' ? 1 : box0.boxType == 'cabinet' ? 0.9 : 0.7 //随着宽度增加而降低
  1241. let r = THREE.MathUtils.clamp(0.8 / standards[boxType].widthNormal.max, 0.6, 2) //随着宽度增加而降低 UWrshepp0G5的fire
  1242. if (!standards[boxType].bottom && btmPos0 && btmPos1) {
  1243. //注:挂空调不应使用btmPosPredict
  1244. let d = btmPos0.distanceToSquared(btmPos1)
  1245. matchInfo.btmPosPreDis = d
  1246. score -= d * 1300 * r * shinkRatio
  1247. if (box1.topPos) {
  1248. let a = box0.topPos.distanceToSquared(box1.topPos)
  1249. matchInfo.topPosPreDis = a
  1250. let u = a * 700 * r * shinkRatio
  1251. let AP0 = new THREE.Vector2(btmPos0.x - A.x, btmPos0.z - A.z).lengthSq()
  1252. let AP1 = new THREE.Vector2(btmPos1.x - B.x, btmPos1.z - B.z).lengthSq()
  1253. if (AP0 < 0.4 || AP1 < 0.4) u *= 0.3 //太近
  1254. score -= u
  1255. }
  1256. } else if (box0.posAtWall && box1.posAtWall) {
  1257. //
  1258. let d = box0.posAtWall.distanceToSquared(box1.posAtWall)
  1259. matchInfo.wallPosPreDis = d
  1260. score -= d * 200 * r //墙面不准所以分低 ftMTQIrs79
  1261. d = box0.btmPosOri.distanceToSquared(box1.btmPosOri) //还是加一下
  1262. matchInfo.btmPosPreDis = d
  1263. score -= d * 200 * r * shinkRatio
  1264. /* let h0 = box0.topPos.y - box0.btmPos.y
  1265. let h1 = box1.topPos.y - box1.btmPos.y
  1266. score -= Math.abs(h0-h1) * 3000 * r * shinkRatio //高度差 倾斜角度大的不准
  1267. */
  1268. }
  1269. }
  1270. if (!matchInfo.center) {
  1271. let o = getIntersect2(A, box0.centerDir, B, box1.centerDir)
  1272. matchInfo.center = o.pos3d.clone()
  1273. /* let o2 = math.getLineIntersect({ A, B, p1: A.clone().add(box0.centerDir), p2:B.clone().add(box1.centerDir) })
  1274. matchInfo.center2 = o2.pos3d.clone() */
  1275. /* if (name == "pano16-4&pano18-5") {
  1276. addLine(A, box0.centerBtmDir, 10), addLine(B, box1.centerBtmDir, 10)
  1277. } */
  1278. //验证是否漫游点到中心点的方向和centerDir一样
  1279. let dir0 = new THREE.Vector3().subVectors(o.pos3d, A).normalize()
  1280. let dir1 = new THREE.Vector3().subVectors(o.pos3d, B).normalize()
  1281. let sum = dir0.dot(box0.centerDir) + dir1.dot(box1.centerDir)
  1282. let wrongDir = sum < 1.95
  1283. score -= (2 - sum) * 10000
  1284. if (wrongDir) {
  1285. str.push('wrongDir')
  1286. return Object.assign(matchInfo, { score: score - 5000, str })
  1287. }
  1288. if (!dontCheckDis && !standards[boxType].bottom && box0.btmPos && box1.btmPos) {
  1289. //墙壁位置不准所以不用 KK-ftMTQIrs79
  1290. let p0 = new THREE.Vector2(box0.btmPos.x, box0.btmPos.z)
  1291. let p1 = new THREE.Vector2(box1.btmPos.x, box1.btmPos.z)
  1292. let p = new THREE.Vector2(matchInfo.center.x, matchInfo.center.z)
  1293. let dis = p0.distanceToSquared(p) + p1.distanceToSquared(p)
  1294. let s = math.linearClamp(matchInfo.minAng, 0, 20, 0, 1)
  1295. score -= dis * 1500 * s //如果距离较远就说明算的center误差大,不可信。可能有一个框不准确
  1296. matchInfo.centerDrift = dis
  1297. }
  1298. getBoxBtm(matchInfo)
  1299. let cr //getIntersect2结果的权重
  1300. if (standards[box0.boxType].atWall == 1) {
  1301. cr = math.linearClamp(matchInfo.minAng, 2, 15, 0, 1) //墙壁误差大,所以尽量完全依赖cIntersect。。。后期如果该墙壁校准了可以调整
  1302. } else {
  1303. cr = math.linearClamp(matchInfo.minAng, 2, 20, 0, 0.4)
  1304. }
  1305. let predict0 = (!standards[boxType].bottom && box0.btmPos) || box0.posAtWall || matchInfo.topPos //在墙上的除非角度小,否则不考虑cIntersect
  1306. let predict1 = (!standards[boxType].bottom && box1.btmPos) || box1.posAtWall || matchInfo.topPos
  1307. //相对来说btmPos要比center准一点?因为center有在两个维度上的误差
  1308. const btmRatio = 0.5
  1309. let cIntersect = new THREE.Vector3().addVectors(matchInfo.center.clone().multiplyScalar(1 - btmRatio), matchInfo.btmPos.clone().multiplyScalar(btmRatio)).setY(o.pos3d.y)
  1310. if (predict0 && predict1) {
  1311. matchInfo.center = new THREE.Vector3()
  1312. .addVectors(
  1313. cIntersect.clone().multiplyScalar(cr),
  1314. predict0
  1315. .clone()
  1316. .add(predict1)
  1317. .multiplyScalar((1 / 2) * (1 - cr))
  1318. )
  1319. .setY(o.pos3d.y)
  1320. } else {
  1321. matchInfo.center = cIntersect
  1322. }
  1323. matchInfo.cIntersect = cIntersect
  1324. //addLabel(matchInfo.center, matchInfo.name + '-c')
  1325. }
  1326. getBoxType(matchInfo)
  1327. /* if (matchInfo.boxType != box0.type || matchInfo.boxType != box1.type) {
  1328. ;(score -= 1000), str.push('typeNotSame2')
  1329. } */
  1330. {
  1331. let vec0 = new THREE.Vector3().subVectors(box0.pano.position, matchInfo.center)
  1332. let vec1 = new THREE.Vector3().subVectors(box1.pano.position, matchInfo.center)
  1333. if (vec0.x * vec1.x > 0 && vec0.z * vec1.z > 0) {
  1334. //同一个象限(center会偏向一侧)
  1335. score -= 200
  1336. }
  1337. }
  1338. if (!safeBound.containsPoint(matchInfo.center)) {
  1339. let dis = safeBound.distanceToPoint(matchInfo.center)
  1340. //console.log(`${panoId0}的第${box0.index}个与${panoId1}的第${box1.index}个因中心点在bounding外不匹配`, center, 'dis: ' + dis)
  1341. ;(score -= 1000 * dis * dis), str.push('outsideBound')
  1342. Object.assign(matchInfo, { score, str, center: matchInfo.center, disToBound: dis })
  1343. if (dis > 0.5) return matchInfo
  1344. }
  1345. //检查宽度
  1346. let boxposes
  1347. let checkWidth = () => {
  1348. boxposes = []
  1349. ;[box0, box1].forEach(box => {
  1350. let pose = getBoxPoseByPos(box, matchInfo.center)
  1351. boxposes.push(pose)
  1352. //如果超出标准,基本上这二者不匹配;但过小的话,有可能是被遮挡所以残缺,因此不予过滤
  1353. })
  1354. }
  1355. checkWidth()
  1356. score += getPoseScore(boxposes, boxType /* isSingle */) //根据投影信息预测的长度再得匹配分数
  1357. compu++
  1358. return Object.assign(matchInfo, { score: score, str, /* diffHalfHeight, */ boxposes })
  1359. }
  1360. this.rows = {}
  1361. /* let getchainNext = (left, end, chain, boxes) => {
  1362. chain.push(left)
  1363. if (left == end) return boxes.chains.push(chain)
  1364. let nodes = boxes.relationships.filter(pair => pair.includes(left))
  1365. let rights = nodes.map(pair => pair.find(e => e != left))
  1366. rights = rights.filter(e => !chain.includes(e) && boxes.indexOf(e) > boxes.indexOf(left))
  1367. rights.forEach(right => {
  1368. getchainNext(right, end, chain.slice(), boxes)
  1369. })
  1370. } */
  1371. let getPanoBigRowBox = (panoBoxes, { reason = 'row' } = {}) => {
  1372. //将一个pano中的所有boxes分组,识别哪些是一排的。也可用于识别融合
  1373. let pano = panoBoxes[0].pano
  1374. const category = panoBoxes[0].category
  1375. let type = category + '|' + reason
  1376. this.rows[type] || (this.rows[type] = {})
  1377. if (this.rows[type][pano.id]) return this.rows[type][pano.id]
  1378. let bigBoxes
  1379. let bigBox = {
  1380. sid: 'pano' + pano.id + (reason == 'mix' ? '-mix' : '-row'),
  1381. pano,
  1382. category: reason == 'mix' ? category : 'rowBigBox',
  1383. boxType: reason == 'mix' ? panoBoxes[0].boxType : 'rowBigBox',
  1384. }
  1385. let rows = []
  1386. for (let i = 0; i < panoBoxes.length; i++) {
  1387. let box0 = panoBoxes[i]
  1388. getBoxBase(box0)
  1389. let [left0, right0] = [box0.bbox2[0], box0.bbox2[2]]
  1390. for (let j = i + 1; j < panoBoxes.length; j++) {
  1391. let box1 = panoBoxes[j]
  1392. getBoxBase(box1)
  1393. if (box0.boxType != box0.boxType) continue //类型不同
  1394. let [left1, right1] = [box1.bbox2[0], box1.bbox2[2]]
  1395. let d1 = getBbox2Diff(left1, right0),
  1396. d2 = getBbox2Diff(left1, left0),
  1397. d3 = getBbox2Diff(left0, right1)
  1398. if (box0.sid == 'pano2-4' && box1.sid == 'pano2-5') {
  1399. console.log(9)
  1400. }
  1401. const min = reason == 'mix' ? 0.004 : 0.003 //mix代表寻找分裂的重新融合到一起
  1402. if ((d1 <= min && d2 >= min) || (d3 <= min && d2 <= min)) {
  1403. //边框交接
  1404. let atEdgeMight = left1 < 0.002 && right0 > 0.998 ? [left1, right0] : left0 < 0.002 && right1 > 0.998 ? [left0, right1] : null //有在全景图的边界的可能性
  1405. //if (reason == 'mix' && box0.category == typeNames.cabinet && !atEdgeMight) continue //柜子容易并排,尽量不融合 //再看,啥意思,没懂
  1406. if (reason == 'mix' && !atEdgeMight) continue //不在边缘
  1407. const { max } = standards[box0.boxType].widthNormal //standards[box0.btmPos ? category : 'air-hanging'].widthNormal
  1408. const tolerate = max * max * (reason == 'mix' ? 0.7 : 1.8) //yDCiaTQvRYn:row不能低于1.5
  1409. let p0 = standards[box0.boxType].atWall == 1 ? box0.posAtWall : reason == 'mix' ? box0.btmPosOri || box0.btmPosPredict : box0.btmPos
  1410. let p1 = standards[box1.boxType].atWall == 1 ? box1.posAtWall : reason == 'mix' ? box1.btmPosOri || box1.btmPosPredict : box1.btmPos
  1411. //let p0 = box0.type == 'air-hanging' ? box0.posAtWall : reason == 'mix' ? box0.btmPosOri : box0.btmPos
  1412. //let p1 = box1.type == 'air-hanging' ? box1.posAtWall : reason == 'mix' ? box1.btmPosOri : box1.btmPos
  1413. let dis = p0.distanceToSquared(p1)
  1414. if (reason == 'mix') {
  1415. let allY = box0.bbox2[3] - box0.bbox2[1] + (box1.bbox2[3] - box1.bbox2[1]) //各自高度和
  1416. let wholeY = Math.max(box0.bbox2[3], box1.bbox2[3]) - Math.min(box0.bbox2[1], box1.bbox2[1]) //总跨越高度
  1417. let coverY = allY - wholeY //重合区域的y高度,可为负数
  1418. let disY = (1 - coverY / wholeY + (wholeY - coverY) * 3) * 4 * tolerate //既要考虑占比也要考虑差值
  1419. dis += disY //SGyhEzZNGP9案例:虽然是atEdge但并不应该融合,便通过disY来阻挡 ; MW6MEeCOy9Y:pano16-15
  1420. //console.log('disY',disY, box0.sid, box1.sid )
  1421. let atEdgePossib = atEdgeMight ? 0.002 / (atEdgeMight[0] + (1 - atEdgeMight[1])) : 0 // 两条线越接近越可能融合
  1422. atEdgePossib = Math.min(6, atEdgePossib) //原本计算得 min:1, max:Infinity
  1423. dis -= atEdgePossib * tolerate //给点点优势
  1424. }
  1425. /* if(box0.sid == "pano2-0"){
  1426. console.log('dis',dis,'tolerate',tolerate,[box0, box1],disY)
  1427. }
  1428. if (box0.sid == 'pano2-1' && box1.sid == 'pano2-4') {
  1429. reason == 'mix' && console.log(dis, tolerate, box0.sid, box1.sid)
  1430. }*/
  1431. if (dis < tolerate) {
  1432. //reason == 'mix' && console.log('-------------------')
  1433. common.pushToGroupAuto([box0, box1], rows)
  1434. }
  1435. }
  1436. }
  1437. }
  1438. //一排箱子的角度范围不可超过180度,因为不可能站在箱子上拍,所以超过的话肯定有边缘的不在这一排中。
  1439. //可判断边缘箱子的是否角度偏大,一般中间的被遮挡所以偏小
  1440. rows.forEach(boxes => {
  1441. //从左到右排序
  1442. boxes.sort((a, b) => {
  1443. //但因有的box跨越到别的box区域,所以这个顺序不准确
  1444. return getBbox2Diff(a.bbox2CenterX, b.bbox2CenterX)
  1445. })
  1446. })
  1447. //去除不在一条直线上的连接. 当bound超出后就断开
  1448. if (reason == 'row') {
  1449. rows.slice(0).forEach(boxes => {
  1450. if (boxes.length >= 2) {
  1451. let removes = [],
  1452. bound = new THREE.Box2(),
  1453. size = new THREE.Vector2(),
  1454. maxW = 0.6
  1455. for (let i = 0, j = boxes.length; i < j; i++) {
  1456. let box = boxes[i]
  1457. let pos2d = new THREE.Vector2(box.btmPos.x, box.btmPos.z)
  1458. bound.expandByPoint(pos2d)
  1459. bound.getSize(size)
  1460. let min = Math.min(size.x, size.y)
  1461. if (min > maxW) {
  1462. removes.push([boxes[i], boxes[i - 1]])
  1463. bound = new THREE.Box2()
  1464. bound.expandByPoint(pos2d)
  1465. //console.log('removes', size)
  1466. }
  1467. //console.log('removes',k, box1.sid)
  1468. }
  1469. if (removes.length) {
  1470. /* console.log(
  1471. '去除错误row连接',
  1472. removes.map(e => e.map(a => a.sid))
  1473. ) */
  1474. let { newGroups } = common.disconnectGroup(removes, rows)
  1475. //if(newGroups.length>1){//分裂成多组了,重新计算
  1476. // console.log(newGroups)
  1477. //}
  1478. }
  1479. }
  1480. })
  1481. rows.forEach(boxes => {
  1482. //从左到右重新排序
  1483. boxes.sort((a, b) => {
  1484. //但因有的box跨越到别的box区域,所以这个顺序不准确
  1485. return getBbox2Diff(a.bbox2CenterX, b.bbox2CenterX)
  1486. })
  1487. })
  1488. }
  1489. rows.sort((a, b) => {
  1490. return b.length - a.length
  1491. }) //箱子数量从大到小排序
  1492. bigBoxes = rows.map((boxes, i) => {
  1493. let { leftX, rightX } = getLeftRight(boxes) //最左
  1494. let topY = boxes.slice().sort((a, b) => a.bbox2[1] - b.bbox2[1])[0].bbox2[1]
  1495. let btmY = boxes.slice().sort((a, b) => b.bbox2[3] - a.bbox2[3])[0].bbox2[3]
  1496. let rowBigBox = Object.assign({}, bigBox, {
  1497. boxes,
  1498. bbox2: [leftX, topY, rightX, btmY], //整排的bbox
  1499. left: boxes.find(e => e.bbox2[0] == leftX),
  1500. right: boxes.find(e => e.bbox2[2] == rightX),
  1501. })
  1502. let p0 = getBoxPos(rowBigBox.left)
  1503. let p1 = getBoxPos(rowBigBox.right)
  1504. let vec = new THREE.Vector2(p0.x - p1.x, p0.z - p1.z)
  1505. rowBigBox.k = Math.abs(vec.x / vec.y)
  1506. rowBigBox.predictLen = (rowBigBox.k > 1 ? Math.abs(vec.x) : Math.abs(vec.y)) + 0.6 //加入一个宽度
  1507. /* if(boxes.length <= boxes.relationships.length){//多条链(为了识别一个box嵌套多个的情况。不过后来在开头时处理了一部分)
  1508. boxes.chains = []
  1509. getchainNext(left,right,[], boxes )
  1510. let aveAngle = (getBbox2Diff(left.bbox2[2], left.bbox2[0]) + getBbox2Diff(right.bbox2[2], right.bbox2[0]) ) / 2 -0.01 //首尾的angle平均数。但如果这两个不准那就导致整体出错了
  1511. let middleAngle = getBbox2Diff(right.bbox2[0], left.bbox2[2])
  1512. let counts = boxes.chains.map(e=>e.length)
  1513. counts.sort((a,b)=>a-b)
  1514. let min = counts[0],max = counts[counts.length-1]
  1515. let r = [], cur = min;
  1516. while(cur<=max){
  1517. r.push({cur, diff:Math.abs((middleAngle / (cur-2) - aveAngle)}) //加 0.01是因为增加边缘
  1518. cur++
  1519. }
  1520. r.sort((a,b)=>a.diff-b.diff)
  1521. rowBigBox.predictBoxCount = r[0].cur
  1522. //---------
  1523. let goodCountChains = boxes.chains.filter(e=>e.length == rowBigBox.predictBoxCount)
  1524. if(goodCountChains.length == 1) rowBigBox.bestChain = goodCountChains[0]
  1525. else{
  1526. goodCountChains = goodCountChains.map((chain,i)=>{
  1527. let j = 1, diff=0 //中间的box的angle的方差
  1528. while(j<rowBigBox.predictBoxCount){
  1529. let angle = getBbox2Diff(chain[j].bbox2[2], chain[j].bbox2[0])
  1530. diff += Math.pow(angle - aveAngle, 2)
  1531. j++;
  1532. }
  1533. return {diff, chain}
  1534. })
  1535. goodCountChains.sort((a,b)=>a.diff-b.diff)
  1536. rowBigBox.bestChain = goodCountChains[0].chain
  1537. }
  1538. console.log('getChains',boxes.chains, 'predictBoxCount',rowBigBox.predictBoxCount, r)
  1539. } */
  1540. return rowBigBox
  1541. })
  1542. if (reason != 'mix') {
  1543. panoBoxes.forEach(box => {
  1544. //加入单个的
  1545. if (!rows.some(row => row.includes(box))) {
  1546. let boxBig = Object.assign({}, bigBox, {
  1547. bbox2: box.bbox2,
  1548. boxes: [box],
  1549. left: box,
  1550. right: box,
  1551. })
  1552. bigBoxes.push(boxBig)
  1553. }
  1554. })
  1555. }
  1556. //mix的之前的btm因pose错误而延伸了不对的depth所以不准
  1557. bigBoxes.forEach(bigBox => {
  1558. bigBox.sid += '-' + bigBox.boxes.map(e => e.index).join(',')
  1559. /* if (bigBox.sid == 'pano0-rowBigBox-1,0,2,4') {
  1560. console.log(3)
  1561. } */
  1562. if (reason == 'row') {
  1563. //取平均值
  1564. if (bigBox.boxes[0].btmPos) {
  1565. bigBox.btmPos = bigBox.boxes.reduce((w, c) => w.add(c.btmPos), new THREE.Vector3()).multiplyScalar(1 / bigBox.boxes.length)
  1566. //addLabel(bigBox.btmPos,'b_'+bigBox.sid, {bgcolor:'#f93',a:0.4})
  1567. }
  1568. if (bigBox.boxes[0].topPos) {
  1569. bigBox.topPos = bigBox.boxes.reduce((w, c) => w.add(c.topPos), new THREE.Vector3()).multiplyScalar(1 / bigBox.boxes.length)
  1570. }
  1571. if (bigBox.boxes[0].posAtWall) {
  1572. bigBox.posAtWall = bigBox.boxes.reduce((w, c) => w.add(c.posAtWall), new THREE.Vector3()).multiplyScalar(1 / bigBox.boxes.length)
  1573. }
  1574. }
  1575. //mix的需要合并后计算才准确
  1576. })
  1577. this.rows[type][pano.id] = bigBoxes //当前pano的所有row
  1578. return bigBoxes
  1579. }
  1580. /* let getPanoBoxAngleTrend = rowBox => {
  1581. //顺时针方向该pano的box角度范围是越来越大还是越来越小
  1582. let diffs = []
  1583. let angles = rowBox.boxes.map(box => getBbox2Diff(box.bbox2[2], box.bbox2[0]))
  1584. for (let i = 0, j = angles.length; i < j - 1; i++) {
  1585. //得所有相邻之间的差
  1586. let a0 = angles[i],
  1587. a1 = angles[i + 1]
  1588. diffs.push(a1 - a0)
  1589. }
  1590. diffs.sort((a, b) => a - b)
  1591. return diffs[Math.floor(diffs.length / 2)] //中位数
  1592. } */
  1593. /* let getBoxCount = (rowBigBox)=>{
  1594. return rowBigBox.predictBoxCount || rowBigBox.boxes.length
  1595. } */
  1596. let getReverseInfo = (rowBigBox0, rowBigBox1) => {
  1597. //两个row的方向对应
  1598. let reversed = false
  1599. let lefts = [rowBigBox0.left, rowBigBox1.left]
  1600. let rights = [rowBigBox0.right, rowBigBox1.right]
  1601. let dis0 = lefts[0].btmPos.distanceToSquared(lefts[1].btmPos)
  1602. let dis1 = rights[0].btmPos.distanceToSquared(rights[1].btmPos)
  1603. let dis2 = lefts[0].btmPos.distanceToSquared(rights[1].btmPos)
  1604. let dis3 = rights[0].btmPos.distanceToSquared(lefts[1].btmPos)
  1605. let posLeft2, posRight2
  1606. if (dis0 + dis1 > dis2 + dis3) {
  1607. //距离近的代表是同一端
  1608. reversed = true
  1609. posLeft2 = new THREE.Vector3().addVectors(lefts[0].btmPos, rights[1].btmPos).multiplyScalar(0.5)
  1610. posRight2 = new THREE.Vector3().addVectors(rights[0].btmPos, lefts[1].btmPos).multiplyScalar(0.5)
  1611. } else {
  1612. posLeft2 = new THREE.Vector3().addVectors(lefts[0].btmPos, lefts[1].btmPos).multiplyScalar(0.5)
  1613. posRight2 = new THREE.Vector3().addVectors(rights[0].btmPos, rights[1].btmPos).multiplyScalar(0.5)
  1614. }
  1615. let vec = new THREE.Vector2(posLeft2.x - posRight2.x, posLeft2.z - posRight2.z)
  1616. let k = Math.abs(vec.x / vec.y) //这个算斜率更准,但位置容易偏向一侧(可能用边缘的bbox算会好些?)
  1617. return { reversed, k }
  1618. }
  1619. let searchByRow = (groups, datas) => {
  1620. //先查找row,匹配row,再slice row的方法
  1621. this.matchScoreMap['rowBigBox'] = {}
  1622. let rowInfos = []
  1623. let getRowMatchInfo = (rowBigBox0, rowBigBox1, ignoreCountMatch) => {
  1624. //获取row间的匹配信息
  1625. //获取bigBox位置。由于一排的盒子比较长,中心方向误差大,所以采用先获取两边位置,再求中点的方法
  1626. //if (rowBigBox0.boxes.length != rowBigBox1.boxes.length && !ignoreCountMatch) return //太难了,不算不一样的情况了
  1627. let name = rowBigBox0.sid + '&' + rowBigBox1.sid
  1628. if (name in rowInfos) {
  1629. return rowInfos[name]
  1630. }
  1631. if (name == 'pano4-row-2,11,10,8,6&pano6-row-2,5,8,11') {
  1632. console.log(3)
  1633. }
  1634. if (rowBigBox0.boxes.length != rowBigBox1.boxes.length && !ignoreCountMatch) return
  1635. //if (getBoxCount(rowBigBox0) != getBoxCount(rowBigBox1) && getBoxCount(rowBigBox0) != 1 && getBoxCount(rowBigBox1) != 1)return
  1636. /* if (rowBigBox0.sid == 'pano0-row-1,2,6' && rowBigBox1.sid == 'pano2-row-0,2,6') {
  1637. console.log(4)
  1638. } */
  1639. let rowInfo
  1640. if (rowBigBox0.boxes.length > 1 && rowBigBox1.boxes.length > 1) {
  1641. //多对多,可以求两端的位置
  1642. let lefts = [rowBigBox0.left, rowBigBox1.left]
  1643. let rights = [rowBigBox0.right, rowBigBox1.right]
  1644. let leftInfo
  1645. let rightInfo
  1646. let info2 = getReverseInfo(rowBigBox0, rowBigBox1)
  1647. let len0 = rowBigBox0.predictLen, //长度应该接近
  1648. len1 = rowBigBox1.predictLen
  1649. let overLen = Math.abs(len0 - len1) /* / (rowBigBox0.boxes.length + rowBigBox1.boxes.length) * 5 */
  1650. if (overLen > 1) {
  1651. //console.warn('overLen> 1', overLen, rowBigBox0.sid, '和', rowBigBox1.sid)
  1652. return done()
  1653. }
  1654. if (info2.reversed) {
  1655. leftInfo = getMatchScore(lefts[0], rights[1], { isSingle: true })
  1656. rightInfo = getMatchScore(rights[0], lefts[1], { isSingle: true })
  1657. } else {
  1658. leftInfo = getMatchScore(lefts[0], lefts[1], { isSingle: true })
  1659. rightInfo = getMatchScore(rights[0], rights[1], { isSingle: true })
  1660. }
  1661. let posLeft = getBoxPos(leftInfo)
  1662. let posRight = getBoxPos(rightInfo)
  1663. if (!posLeft || !posRight || leftInfo.score < -4000 || rightInfo.score < -4000) {
  1664. return done() //漫游点重合、>180度会导致此问题
  1665. }
  1666. preDealBox(leftInfo)
  1667. //getBoxSize(leftInfo)
  1668. preDealBox(rightInfo)
  1669. //getBoxSize(rightInfo)
  1670. posLeft = getBoxPos(leftInfo)
  1671. posRight = getBoxPos(rightInfo)
  1672. //验证是否是垂直或水平
  1673. let vec = new THREE.Vector2(posLeft.x - posRight.x, posLeft.z - posRight.z)
  1674. let k = Math.abs(vec.x / vec.y)
  1675. if ((info2.k > 1 && k < 1) || (info2.k < 1 && k > 1)) {
  1676. console.error('请检查!info2.k > 1 && k < 1 || info2.k < 1 && k > 1', rowBigBox0.sid, '和', rowBigBox1.sid) //绘制的方向错误,尺寸错误
  1677. return done()
  1678. }
  1679. let wrongK = 0
  1680. if ((rowBigBox0.k > 1 && rowBigBox1.k < 1) || (rowBigBox0.k < 1 && rowBigBox1.k > 1)) {
  1681. wrongK = rowBigBox0.k / rowBigBox1.k
  1682. if (wrongK < 1) wrongK = 1 / wrongK
  1683. }
  1684. /*
  1685. let trend0 = getPanoBoxAngleTrend(rowBigBox0)
  1686. let trend1 = getPanoBoxAngleTrend(rowBigBox1)
  1687. let judgeReverse = () => { //这个方法有时不准
  1688. let disLeftSquared0 = new THREE.Vector2(posLeft.x - rowBigBox0.pano.position.x, posLeft.z - rowBigBox0.pano.position.z).lengthSq()
  1689. let disRightSquared0 = new THREE.Vector2(posRight.x - rowBigBox0.pano.position.x, posRight.z - rowBigBox0.pano.position.z).lengthSq()
  1690. let a = trend0 * (disLeftSquared0 - disRightSquared0)
  1691. if (a < 0 && Math.abs(a) > 0.1) return true
  1692. let posLeft2 = reversed ? posRight : posLeft, //反向过的对第二个漫游点来说左右是反的
  1693. posRight2 = reversed ? posLeft : posRight
  1694. let disLeftSquared1 = new THREE.Vector2(posLeft2.x - rowBigBox1.pano.position.x, posLeft2.z - rowBigBox1.pano.position.z).lengthSq()
  1695. let disRightSquared1 = new THREE.Vector2(posRight2.x - rowBigBox1.pano.position.x, posRight2.z - rowBigBox1.pano.position.z).lengthSq()
  1696. let b = trend1 * (disLeftSquared1 - disRightSquared1)
  1697. if (b < 0 && Math.abs(b) > 0.1) return true
  1698. }
  1699. if (leftInfo.score < -2000 || rightInfo.score < -2000 || judgeReverse()) {
  1700. //反向试试
  1701. leftInfo = getMatchScore(lefts[0], rights[1], { isSingle: true })
  1702. rightInfo = getMatchScore(rights[0], lefts[1], { isSingle: true })
  1703. posLeft = getBoxPos(leftInfo)
  1704. posRight = getBoxPos(rightInfo)
  1705. reversed = true //rowBigBox1 反向了
  1706. }
  1707. if (leftInfo.score < -2000 || rightInfo.score < -2000 || judgeReverse()) {
  1708. return console.log('getCenter ;两个方向都不符合', rowBigBox0.sid, rowBigBox1.sid)
  1709. } */
  1710. /*const maxK = Math.max(0.6 / Math.sqrt(rowBigBox0.boxes.length), 0.2) // 最大斜率
  1711. if (k < maxK && k > 1 / maxK) {
  1712. return //console.log('放弃,斜率', k)
  1713. } */
  1714. //横的话,按x从小到大,竖的按z从小到大
  1715. if ((k < 1 && posLeft.z > posRight.z) || (k > 1 && posLeft.x > posRight.x)) {
  1716. let temp = posRight
  1717. ;(posRight = posLeft), (posLeft = temp)
  1718. }
  1719. //addLabel(posLeft, 'left-' + rowBigBox0.pano.id + '&' + rowBigBox1.pano.id, { a: 0.1 })
  1720. //addLabel(posRight, 'right-' + rowBigBox0.pano.id + '&' + rowBigBox1.pano.id, { a: 0.1 })
  1721. /* if (rowBigBox0.pano.id + '&' + rowBigBox1.pano.id == '22&26') {
  1722. console.log(777)
  1723. } */
  1724. /* var line1 = LineDraw.createLine([posLeft, posRight])
  1725. meshGroup.add(line1) */
  1726. //根据btmPos矫正一下中心位置, 否则容易偏漫游点这一侧
  1727. let center = new THREE.Vector3().addVectors(posLeft, posRight).multiplyScalar(0.5)
  1728. center
  1729. .add(rowBigBox0.btmPos)
  1730. .add(rowBigBox1.btmPos)
  1731. .multiplyScalar(1 / 3)
  1732. let axis = k > 1 ? 'z' : 'x'
  1733. //posLeft[axis] = center[axis], posRight[axis] = center[axis]
  1734. let match = getMatchScore(rowBigBox0, rowBigBox1, { isSingle: true, center }) //是否预先传送center ?
  1735. //rowInfo.minAngs = [leftInfo.minAng , rightInfo.minAng]
  1736. /* if (match.name == 'pano4-row-6,4,1,3&pano0-row-0,1,4,5') {
  1737. console.log(8)
  1738. } */
  1739. let sc = match.score - overLen * 1000 - wrongK * 100 + leftInfo.score + rightInfo.score
  1740. if (sc < -4000) {
  1741. //console.log('放弃,匹配分过低,可能不是一组', rowBigBox0.sid, '和', rowBigBox1.sid, sc)
  1742. return done()
  1743. }
  1744. //console.log('getcenter', rowBigBox0.sid, '和', rowBigBox1.sid, overLen, match.score + overLen * 1000 + leftInfo.score + rightInfo.score)
  1745. rowInfo = {
  1746. rowBigBox0,
  1747. rowBigBox1,
  1748. match,
  1749. k,
  1750. posLeft,
  1751. posRight,
  1752. score: sc / 3 + 500, //700 + match.score*0.7 + (leftInfo.score + rightInfo.score)*0.3 ,
  1753. reversed: info2.reversed,
  1754. }
  1755. } else {
  1756. if (rowBigBox0.boxes.length == 1 && rowBigBox1.boxes.length == 1) {
  1757. rowInfo = getMatchScore(rowBigBox0.boxes[0], rowBigBox1.boxes[0]) //直接匹配box
  1758. } else {
  1759. //一对多。getMatchScore计算误差大(长度越长中心误差越大、宽度计算也误差大)所以再写点限制。直接使用btm来预测长度和位置似乎更准
  1760. let mulBoxRow = rowBigBox0.boxes.length > 1 ? rowBigBox0 : rowBigBox1
  1761. let singleBox = rowBigBox0.boxes.length == 1 ? rowBigBox0 : rowBigBox1
  1762. /* if(rowBigBox0.sid == "pano12-row-3" && rowBigBox1.sid == "pano0-row-3,1,0" ){
  1763. console.log(5)
  1764. } */
  1765. rowInfo = getMatchScore(rowBigBox0, rowBigBox1, { dontCheckDis: true }) //一对多 也可以根据方向检查距离,如仅检查z
  1766. rowInfo.k = mulBoxRow.k
  1767. if (rowInfo.center) {
  1768. rowInfo.center.add(getBoxPos(mulBoxRow)).multiplyScalar(0.5)
  1769. }
  1770. if (rowInfo.name == 'pano0-row-14&pano4-row-8,11') {
  1771. addLabel(rowInfo.center, 'c')
  1772. }
  1773. rowInfo.predictSize = rowInfo.k > 1 ? { x: mulBoxRow.predictLen, y: 0.6 } : { y: mulBoxRow.predictLen, x: 0.6 }
  1774. //单个的应该和多个的其中一端一样,且是离单个漫游点近的那端(也就是要走到箱子一端看不见其他箱子才行)
  1775. let dis0 = getBoxPos(mulBoxRow.left).distanceToSquared(singleBox.pano.position)
  1776. let dis1 = getBoxPos(mulBoxRow.right).distanceToSquared(singleBox.pano.position)
  1777. let near = dis0 < dis1 ? mulBoxRow.left : mulBoxRow.right
  1778. let p0 = getBoxPos(singleBox)
  1779. let dis = getBoxPos(near).distanceToSquared(p0)
  1780. rowInfo.score -= dis * 1000
  1781. let p1 = getBoxPos(mulBoxRow)
  1782. let vec1 = new THREE.Vector2(p1.x - singleBox.pano.position.x, p1.z - singleBox.pano.position.z).normalize() //看向中心的方向
  1783. let vec2 = new THREE.Vector2(singleBox.centerDir.x, singleBox.centerDir.z).normalize()
  1784. rowInfo.score += (vec1.dot(vec2) - 1) * 1000 //同一个方向是最好
  1785. }
  1786. }
  1787. function done(rowInfo) {
  1788. rowInfo && rowInfos.push(rowInfo)
  1789. rowInfos[name] = rowInfo
  1790. }
  1791. done(rowInfo)
  1792. return rowInfo
  1793. }
  1794. let matchGroups = []
  1795. let allRelations = []
  1796. let getK = info => {
  1797. let k
  1798. if (info.left) {
  1799. let vec = new THREE.Vector2(info.left.x - info.right.x, info.left.z - info.right.z)
  1800. k = Math.abs(vec.x / vec.y)
  1801. } else {
  1802. k = Math.abs(Math.max(info.size.x, 0.6) / Math.max(info.size.z, 0.6))
  1803. }
  1804. return k
  1805. }
  1806. let ignoreCountMatch = groups.filter(e => e.length > 1).length == 1 //是否不同数量box的row也能匹配
  1807. let minScore = boundConfirmed ? -2000 : -800
  1808. let match = searchType => {
  1809. if (searchType == 'second') ignoreCountMatch = true
  1810. for (let i = 0; i < groups.length - 1; i++) {
  1811. let rowBigBoxes_0 = getPanoBigRowBox(groups[i])
  1812. let pano0 = groups[i][0].pano
  1813. if (searchType == 'second') rowBigBoxes_0 = rowBigBoxes_0.filter(e => !matchGroups.some(u => u.includes(e)))
  1814. for (let j = i + 1; j < groups.length; j++) {
  1815. let rowBigBoxes_1 = getPanoBigRowBox(groups[j])
  1816. if (searchType == 'second') rowBigBoxes_1 = rowBigBoxes_1.filter(e => !matchGroups.some(u => u.includes(e)))
  1817. let pano1 = groups[j][0].pano
  1818. if (pano0.id == 54 && pano1.id == 56) {
  1819. console.log(2)
  1820. }
  1821. let resultPairs = []
  1822. let evaluateFun = (row0, row1) => {
  1823. return getRowMatchInfo(row0, row1, ignoreCountMatch)
  1824. }
  1825. searchPair(null /* bigBoxes_0[0] */, rowBigBoxes_0.slice(), rowBigBoxes_1.slice(), null, resultPairs, evaluateFun)
  1826. resultPairs = resultPairs.map(pairs => {
  1827. let infos = pairs.map(pair => (pair.some(e => e.sid == 'void') ? null : getRowMatchInfo(pair[0], pair[1], ignoreCountMatch)))
  1828. //infos.sort((a,b)=>{return a.score-b.score});
  1829. let score = infos.reduce((s, e) => {
  1830. return s + (e && e.score > minScore ? e.score : minScore / 2) //只考虑组成功的分数
  1831. }, 0)
  1832. return {
  1833. pairs,
  1834. infos,
  1835. score,
  1836. name: pairs.map(pair => pair.map(item => item.sid).join(' & ')),
  1837. }
  1838. })
  1839. resultPairs.sort((a, b) => b.score - a.score)
  1840. /* if (resultPairs[0].name[0].includes('pano8') && resultPairs[0].name[0].includes('pano0')) {
  1841. console.log(111)
  1842. } */
  1843. resultPairs[0] &&
  1844. resultPairs[0].pairs.forEach((pair, i) => {
  1845. let info = resultPairs[0].infos[i]
  1846. if (info && info.score > minScore) {
  1847. allRelations.push(info)
  1848. let items = pair.filter(e => e.sid != 'void')
  1849. common.pushToGroupAuto(items, matchGroups, null, atGroup => {
  1850. //需要朝向一致才行
  1851. if (!info.k) return true //(box识别的宽高识别不准所以不需要)
  1852. let onePair = atGroup.relationships[0]
  1853. let name = onePair[0].sid + '&' + onePair[1].sid
  1854. if (!rowInfos[name].k) return true //不过不应该有这种情况,否则匹配不到一起才对
  1855. if ((rowInfos[name].k < 1 && info.k < 1) || (rowInfos[name].k > 1 && info.k > 1)) {
  1856. return true
  1857. } else {
  1858. console.log('k不一致无法匹配', info, atGroup)
  1859. }
  1860. })
  1861. //根据目前的规则应该是有端点的和有端点的匹配,box和box匹配
  1862. }
  1863. })
  1864. //console.log(resultPairs[0])
  1865. }
  1866. }
  1867. }
  1868. match()
  1869. ignoreCountMatch || match('second') //再次将剩余的匹配一下,这次允许个数不同的row匹配
  1870. //console.log('matchGroups', matchGroups)
  1871. //识别出来的多组,可能有重复的,因为box个数不同所以才没到一组
  1872. //整理一下,每个组整理出一个info,同时重新检查一下,挑去每组中和其他成员非常不同的
  1873. let groupInfo = []
  1874. let getGroupInfo = group => {
  1875. let left = new THREE.Vector3(),
  1876. right = new THREE.Vector3(),
  1877. pointsLen = 0
  1878. let bigBoxes = []
  1879. let info = {}
  1880. group.relationships.forEach(pair => {
  1881. let name = pair[0].sid + '&' + pair[1].sid
  1882. let matchInfo = rowInfos[name] //this.matchScoreMap["rowBigBox"][name] || this.matchScoreMap["cabinet"][name];
  1883. if (matchInfo.posLeft) {
  1884. left.add(matchInfo.posLeft), right.add(matchInfo.posRight), pointsLen++
  1885. } else {
  1886. bigBoxes.push(matchInfo)
  1887. preDealBox(matchInfo)
  1888. getBoxSize(matchInfo)
  1889. }
  1890. })
  1891. let index = groupInfo.length
  1892. if (pointsLen > 0) {
  1893. left.multiplyScalar(1 / pointsLen)
  1894. right.multiplyScalar(1 / pointsLen)
  1895. //addLabel(left, 'Left' + index, { bgcolor: '#F00', a: 0.2 })
  1896. //addLabel(right, 'Right' + index, { bgcolor: '#F00', a: 0.2 })
  1897. let center = new THREE.Vector3().addVectors(left, right).multiplyScalar(0.5)
  1898. //addLabel(center, 'center' + index, { bgcolor: '#F00', a: 0.3 })
  1899. ;(info.left = left), (info.right = right), (info.center = center)
  1900. info.pointsLen = pointsLen
  1901. }
  1902. if (bigBoxes.length > 0) {
  1903. let getAve = bigBoxes => {
  1904. let center1 = new THREE.Vector3(),
  1905. size = new THREE.Vector3()
  1906. bigBoxes.forEach(box => {
  1907. let center0 = getBoxPos(box)
  1908. center1.add(center0)
  1909. size.add(box.size)
  1910. })
  1911. if (pointsLen > 0) {
  1912. let size0 = new THREE.Vector3(Math.abs(left.x - right.x), size.y, Math.abs(left.z - right.z))
  1913. size.add(size0.multiplyScalar(pointsLen)).multiplyScalar(1 / (pointsLen + bigBoxes.length))
  1914. center1.add(info.center.clone().multiplyScalar(pointsLen)).multiplyScalar(1 / (pointsLen + bigBoxes.length))
  1915. } else {
  1916. size.multiplyScalar(1 / bigBoxes.length)
  1917. center1.multiplyScalar(1 / bigBoxes.length)
  1918. }
  1919. return { center1, size }
  1920. }
  1921. let getScores = (center, size) => {
  1922. //获得相对于center,size的差别分数
  1923. bigBoxes.forEach(box => {
  1924. box.sc = -box.center.distanceToSquared(center1) - size.distanceToSquared(box.size) * 0.5
  1925. })
  1926. }
  1927. let { center1, size } = getAve(bigBoxes)
  1928. //console.log(center1, size)
  1929. getScores(center1, size)
  1930. bigBoxes.sort((a, b) => b.sc - a.sc)
  1931. let midItem = bigBoxes[Math.floor(bigBoxes.length / 2)] //中位数
  1932. getScores(midItem.center, midItem.size)
  1933. const minScore = -8
  1934. let removes = bigBoxes.filter(e => {
  1935. return e.sc < minScore
  1936. })
  1937. if (removes.length) {
  1938. let { newGroups } = common.disconnectGroup(
  1939. removes.map(e => [e.box0, e.box1]),
  1940. matchGroups
  1941. )
  1942. console.log('去除错误数据', removes)
  1943. if (newGroups.length > 1) {
  1944. //分裂成多组了,重新计算
  1945. newGroups.forEach(e => {
  1946. getGroupInfo(e)
  1947. })
  1948. return
  1949. }
  1950. bigBoxes = bigBoxes.filter(e => {
  1951. return e.sc >= minScore
  1952. })
  1953. }
  1954. if (bigBoxes.length) {
  1955. let o = getAve(bigBoxes) //again
  1956. ;(info.center = o.center1), (info.size = o.size)
  1957. }
  1958. }
  1959. info.k = getK(info)
  1960. info.bigBoxes = bigBoxes
  1961. info.group = group
  1962. groupInfo.push(info)
  1963. }
  1964. matchGroups.slice(0).forEach(group => {
  1965. getGroupInfo(group)
  1966. })
  1967. let getLength = c => {
  1968. //获取bigbox长度
  1969. 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
  1970. }
  1971. let getLeft = (group, k) => {
  1972. let dirAxis = (k || group.k) > 1 ? 'x' : 'z'
  1973. return group.left ? group.left[dirAxis] - 0.3 : group.center[dirAxis] - group.size[dirAxis] / 2 //left和right加减半个宽度
  1974. }
  1975. let getRight = (group, k) => {
  1976. let dirAxis = (k || group.k) > 1 ? 'x' : 'z'
  1977. return group.right ? group.right[dirAxis] + 0.3 : group.center[dirAxis] + group.size[dirAxis] / 2
  1978. }
  1979. //识别是否group之间有一样的, 去重
  1980. {
  1981. let realGroups = []
  1982. let getAveWidth = (infos, len) => {
  1983. //获取这些infos最合适的箱子平均宽度和个数
  1984. let boxCounts = []
  1985. infos.forEach(e => {
  1986. boxCounts.push(...e.group.map(bigBox => bigBox.boxes.length))
  1987. })
  1988. boxCounts.sort((a, b) => a - b)
  1989. let midCounts = []
  1990. let r0 = 0.3,
  1991. r1 = 0.7 //取中间这部分的算最适合的个数,结果不一定是中位数
  1992. boxCounts.slice(Math.floor(boxCounts.length * r0), Math.floor(boxCounts.length * r1) + 1).forEach(c => {
  1993. if (!midCounts.includes(c)) midCounts.push(c)
  1994. })
  1995. let { min, max } = standards.cabinet.widthNormal
  1996. let standardW = (min + max) / 2
  1997. let aveWs = midCounts.map(e => {
  1998. return { aveW: len / e, count: e }
  1999. })
  2000. aveWs.sort((a, b) => Math.abs(a.aveW - standardW) - Math.abs(b.aveW - standardW))
  2001. let aveW = aveWs[0].aveW
  2002. let count = aveWs[0].count
  2003. if (aveW > max || aveW < min) {
  2004. let w = THREE.MathUtils.clamp(aveW, min, max)
  2005. //console.warn(`box aveW宽度不太对,从${aveW}修改到${w}`)
  2006. aveW = w
  2007. }
  2008. return { aveW, count }
  2009. }
  2010. let getBox2 = (center, len, thick, k) => {
  2011. let box2 = new THREE.Box2()
  2012. box2.expandByPoint(new THREE.Vector2(center.x, center.z))
  2013. let sizeVec = k > 1 ? new THREE.Vector2(len / 2, thick / 2) : new THREE.Vector2(thick / 2, len / 2)
  2014. box2.expandByVector(sizeVec)
  2015. return box2
  2016. }
  2017. const standardW = 0.6 //两排之间最小距离
  2018. for (let m = 0; m < groupInfo.length - 1; m++) {
  2019. let group0 = groupInfo[m]
  2020. for (let n = m + 1; n < groupInfo.length; n++) {
  2021. let group1 = groupInfo[n]
  2022. if (group1.bigBoxes[0]?.name == 'pano4-row-4&pano6-row-5,1' && group0.bigBoxes[0]?.name == 'pano4-row-2,5&pano6-row-3') {
  2023. console.log(4)
  2024. }
  2025. /* if (group0.k == 4.242560016595383 || group1.k == 0.8571428571428572) {
  2026. console.log(2)
  2027. } */
  2028. const maxR = 2.3
  2029. if (((group0.k > 1 && group1.k < 1) || (group0.k < 1 && group1.k > 1)) && getLength(group0) > 1.5 && getLength(group1) > 1.5) continue //如果是方块状的无视k
  2030. //间距
  2031. let spaceAxis = (group0.k + group1.k) / 2 > 1 ? 'z' : 'x'
  2032. let spaceDis = Math.abs(group0.center[spaceAxis] - group1.center[spaceAxis])
  2033. if (spaceDis > standardW * 1.5) continue
  2034. let o0 = getAveWidth([group0], getLength(group0)) //因为有可能长度和箱子个数不匹配,所以需要得到限制后的宽度再比较
  2035. let o1 = getAveWidth([group1], getLength(group1))
  2036. let len0 = (group0.predictLen = o0.aveW * o0.count)
  2037. let len1 = (group1.predictLen = o1.aveW * o1.count)
  2038. const minR = 0.5 //不可限制太死,因为有的框个数识别少了,导致len短。但可通过重叠面积来判断
  2039. // if( len0 / len1 < minR || len0 / len1 > 1/minR) continue
  2040. let area0 = (group0.area = len0 * o0.aveW)
  2041. let area1 = (group1.area = len1 * o1.aveW)
  2042. let getBoxMixArea = (expandRatio1, expandRatio2) => {
  2043. let box0 = getBox2(group0.center, len0 + expandRatio1, o0.aveW + expandRatio2, group0.k)
  2044. let box1 = getBox2(group1.center, len1 + expandRatio1, o1.aveW + expandRatio2, group1.k)
  2045. let mixBox = getMixBox(box0, box1) //重叠部分
  2046. let s = mixBox.getSize(new THREE.Vector2())
  2047. return { box0, box1, areaMix: Math.max(0, s.x) * Math.max(0, s.y) } //可能是0
  2048. }
  2049. let areaMixExpand = getBoxMixArea(0.1, 0.3).areaMix
  2050. if (areaMixExpand / area0 < 0.65 && areaMixExpand / area1 < 0.65) continue //包含的可以通过
  2051. /*let areaMix = getBoxMixArea(0, 0).areaMix //实际重合面积
  2052. group0.contains = group0.contains || []
  2053. group1.contains = group1.contains || []
  2054. group0.contains.push({ group: group1, selfPercent: areaMix / area0, percent2: areaMix / area1, areaMix })
  2055. group1.contains.push({ group: group0, selfPercent: areaMix / area1, percent2: areaMix / area0, areaMix })
  2056. */
  2057. //console.log('两个合并', group0, group1)
  2058. common.pushToGroupAuto([group0, group1], realGroups)
  2059. //包含的直接合并吧 - - ,这样会使结果偏移,不过没办法了,多个重叠面积太难算了
  2060. }
  2061. }
  2062. //但没合并前样本数量少,包含关系可能有错 - -
  2063. /*for(let m=0; m<groupInfo.length;m++){
  2064. let group = groupInfo[m]
  2065. let contains = group.contains.filter(e=> e.percent2 > 0.8) //所有包含的
  2066. contains.reduce
  2067. } */
  2068. groupInfo.forEach(info => {
  2069. //加入单个的
  2070. if (!realGroups.some(groups => groups.includes(info))) {
  2071. realGroups.push([info])
  2072. }
  2073. })
  2074. //console.log('realGroups', realGroups)
  2075. //get boxes
  2076. realGroups.forEach((infos, i) => {
  2077. const sampleCount = infos.reduce((w, c) => {
  2078. return (w += c.pointsLen || c.bigBoxes.length)
  2079. }, 0)
  2080. let k
  2081. /* {
  2082. //const k = infos.reduce((w, c) => (w += c.k), 0) / infos.length
  2083. let ks = infos.map(e => e.k)
  2084. ks.sort((a, b) => a - b)
  2085. let min = ks[0],
  2086. max = ks[ks.length - 1]
  2087. if (min < 1 && max > 1) {
  2088. //比较最小和最大,选取更极端的那个
  2089. let min_ = 1 / min
  2090. if (min_ < max) k = max
  2091. else k = min
  2092. } else k = (min + max) / 2
  2093. } */
  2094. {
  2095. //看>1的和<1的平均数哪个多用哪个
  2096. let ks = { '<1': { count: 0, sum: 0 }, '>1': { count: 0, sum: 0 } }
  2097. infos.forEach(e => {
  2098. if (e.k < 1) {
  2099. ks['<1'].count++, (ks['<1'].sum += 1 / e.k)
  2100. } else {
  2101. ks['>1'].count++, (ks['>1'].sum += e.k)
  2102. }
  2103. })
  2104. ks['<1'].count && (ks['<1'].ave = ks['<1'].sum / ks['<1'].count)
  2105. ks['>1'].count && (ks['>1'].ave = ks['>1'].sum / ks['>1'].count)
  2106. if (ks['<1'].ave > ks['>1'].ave) {
  2107. k = 1 / ks['<1'].ave
  2108. } else {
  2109. k = ks['>1'].ave
  2110. }
  2111. }
  2112. let centerPos = infos
  2113. .reduce((w, c) => {
  2114. return w.add(c.center.clone().multiplyScalar(c.pointsLen || c.bigBoxes.length))
  2115. }, new THREE.Vector3())
  2116. .multiplyScalar(1 / sampleCount) //预得中心点
  2117. //获取左右端点(需要排除可能的误差,所以采用最靠近端点的三个点。但无法排除前三个点中万一含有包含box的、或者误差大的端点)
  2118. let lefts = infos
  2119. .map(e => getLeft(e, k))
  2120. .sort((a, b) => a - b)
  2121. .filter(a => a < centerPos[k > 1 ? 'x' : 'z'])
  2122. .slice(0, 3)
  2123. let rights = infos
  2124. .map(e => getRight(e, k))
  2125. .sort((a, b) => b - a)
  2126. .filter(a => a > centerPos[k > 1 ? 'x' : 'z'])
  2127. .slice(0, 3)
  2128. let left = 0,
  2129. right = 0
  2130. let c0 = ((lefts.length + 1) * lefts.length) / 2
  2131. lefts.forEach((e, i) => {
  2132. //越靠近最外侧权重越高。
  2133. left += e * ((lefts.length - i) / c0)
  2134. })
  2135. c0 = ((rights.length + 1) * rights.length) / 2
  2136. rights.forEach((e, i) => {
  2137. right += e * ((rights.length - i) / c0)
  2138. })
  2139. centerPos[k > 1 ? 'x' : 'z'] = (left + right) / 2
  2140. let len = right - left //加一点值是因为之前计算长度,用的是最外box的中心点,会少box一半宽度
  2141. let infos2 = infos.filter(e => {
  2142. return !e.predictLen || e.predictLen / len > 0.7
  2143. })
  2144. if (infos2.length == 0) {
  2145. infos2 = infos.sort((a, b) => b.predictLen - a.predictLen).slice(0, 1)
  2146. }
  2147. let { aveW, count } = getAveWidth(infos2, len)
  2148. //长宽比中心点的误差更大,尤其是box类型的、或样本少的
  2149. //获取高度
  2150. let heights = []
  2151. {
  2152. let pairs = [],
  2153. heightss
  2154. /* infos.forEach(e => {
  2155. pairs.push(...e.group.relationships.filter(pair => pair[0].boxes.length == count && pair[1].boxes.length == count))
  2156. })
  2157. if (pairs.length) {
  2158. heightss = pairs.map(pair => {
  2159. let boxes = pair.map(e => {
  2160. return e.boxes.slice()
  2161. })
  2162. let match = rowInfos[pair[0].sid + '&' + pair[1].sid]
  2163. let ifReverse = match.reversed
  2164. if (match.reversed == void 0 && pair[0].boxes.length > 1 && pair[1].boxes.length > 1) {
  2165. let { reversed } = getReverseInfo(pair[0], pair[1])
  2166. ifReverse = reversed
  2167. }
  2168. if (ifReverse) {
  2169. boxes[1].reverse()
  2170. }
  2171. let heights1 = []
  2172. let topPoss = []
  2173. for (let i = 0; i < count; i++) {
  2174. let match1 = getMatchScore(boxes[0][i], boxes[1][i], { onlyGet: true })
  2175. let topPos = match1 && match1.topPos
  2176. if (!topPos) {
  2177. topPos = getBoxTop({ box0: boxes[0][i], box1: boxes[1][i] })
  2178. }
  2179. heights1.push(topPos.y - groundY)
  2180. if (topPos.y - groundY < 0) {
  2181. console.log('?')
  2182. }
  2183. topPoss.push(topPos)
  2184. }
  2185. if ((k < 1 && topPoss[0].z > topPoss[count - 1].z) || (k > 1 && topPoss[0].x > topPoss[count - 1].x)) {
  2186. heights1.reverse()
  2187. }
  2188. return heights1
  2189. })
  2190. } else { */
  2191. let bigBoxes = []
  2192. infos.forEach(e => {
  2193. bigBoxes.push(...e.group.filter(e => !bigBoxes.includes(e) && e.boxes.length == count))
  2194. })
  2195. heightss = bigBoxes.map(bigBox => {
  2196. let topPoss = bigBox.boxes.map(box => {
  2197. let a = { box0: box }
  2198. getBoxTop(a)
  2199. return a.topPos
  2200. })
  2201. if ((k < 1 && topPoss[0].z > topPoss[count - 1].z) || (k > 1 && topPoss[0].x > topPoss[count - 1].x)) {
  2202. topPoss.reverse()
  2203. }
  2204. let heights1 = topPoss.map(topPos => topPos.y - groundY)
  2205. return heights1
  2206. })
  2207. //}
  2208. heightss.forEach(arr => {
  2209. for (let i = 0; i < count; i++) {
  2210. heights[i] = (heights[i] || 0) + arr[i]
  2211. }
  2212. })
  2213. heights = heights.map(e => {
  2214. let h = e / heightss.length
  2215. return h
  2216. })
  2217. //console.log('heightss',heightss, pairs, heights)
  2218. }
  2219. //拆分成小box
  2220. let size = new THREE.Vector3(aveW, 2, aveW)
  2221. let c = 0
  2222. infos.rowboxs = []
  2223. while (c < count) {
  2224. let center
  2225. if (k > 1) {
  2226. let startX = centerPos.x - ((count - 1) / 2) * aveW
  2227. center = new THREE.Vector3(startX + c * aveW, centerPos.y, centerPos.z)
  2228. } else {
  2229. let startZ = centerPos.z - ((count - 1) / 2) * aveW
  2230. center = new THREE.Vector3(centerPos.x, centerPos.y, startZ + c * aveW)
  2231. }
  2232. let size1 = heights[c] ? size.clone().setY(heights[c]) : size //如1*3的是得不到height的
  2233. let box = new Box({ name: 'row' + i + '-' + c, center, size: size1, boxType: 'cabinet', infos })
  2234. c++
  2235. infos.rowboxs.push(box)
  2236. }
  2237. })
  2238. return realGroups.length > 0
  2239. }
  2240. }
  2241. let removeContain = arr => {
  2242. //去除嵌套
  2243. let len = arr.length
  2244. if (len < 2) return
  2245. for (let i = 0; i < len - 1; i++) {
  2246. let box0 = arr[i]
  2247. getBoxBase(box0)
  2248. box0.contains = box0.contains || []
  2249. for (let j = i + 1; j < len; j++) {
  2250. let box1 = arr[j]
  2251. /* if (box0.sid == 'pano2-1' && box1.sid == 'pano2-7') {
  2252. console.log(6)
  2253. } */
  2254. getBoxBase(box1)
  2255. box1.contains = box1.contains || []
  2256. let d3 = Math.abs(box1.bbox2CenterX - box0.bbox2CenterX) //限制d3是因为在相差180度两端可能也符合
  2257. //d4 = Math.abs(box1.bbox2[3] - box0.bbox2[3])
  2258. if (d3 > 0.4 /* || d4 > 0.01 */) continue
  2259. let d0 = getBbox2Diff(box1.bbox2[0], box0.bbox2[0]),
  2260. d1 = getBbox2Diff(box0.bbox2[2], box1.bbox2[2]),
  2261. w0 = getBbox2Diff(box0.bbox2[2], box0.bbox2[0]),
  2262. w1 = getBbox2Diff(box1.bbox2[2], box1.bbox2[0])
  2263. let min = Math.min(0.005, Math.min(w0, w1) * 0.2) //如果本身w0,w1宽度就小,差距更要小
  2264. if ((d1 >= 0 && Math.abs(d0) < min) || (d0 >= 0 && Math.abs(d1) < min) || (d1 >= 0 && d0 >= 0)) {
  2265. box0.contains.push(box1)
  2266. } else if ((d0 <= 0 && Math.abs(d1) < min) || (d1 <= 0 && Math.abs(d0) < min) || (d1 <= 0 && d0 <= 0)) {
  2267. box1.contains.push(box0)
  2268. }
  2269. }
  2270. }
  2271. let getWidthScore = (box, type) => {
  2272. const addDis = 0.1 //因为用的是btm的pos,比中心点近了一些,所以加上一段距离
  2273. let o = getBoxPoseByPos(box, getBoxPos(box), addDis)
  2274. let boxPjW = o.projectWidth
  2275. let standardPjW = (o.maxProjectWidth + o.minProjectWidth) / 2
  2276. let s = type == 'out' ? boxPjW - standardPjW : standardPjW - boxPjW
  2277. return -Math.pow(s, 2) * 10
  2278. }
  2279. let minChildCount = arr[0].boxType == 'battery' ? 1 : 2
  2280. arr.slice().forEach(box => {
  2281. if (box.contains.length >= minChildCount) {
  2282. //假设不存在第二层嵌套, 假设每个只能被一个嵌套
  2283. //决定留大还是留小
  2284. //先只去掉包含两个以上的,且角度范围一致
  2285. //尽量保留内层,除非内层太小
  2286. let { leftX, rightX } = getLeftRight(box.contains)
  2287. if (box.contains.length > 1 && (Math.abs(getBbox2Diff(box.bbox2[0], leftX)) > 0.005 || Math.abs(getBbox2Diff(box.bbox2[2], rightX)) > 0.005)) return //范围不一致
  2288. let remainChild = true
  2289. if (box.contains.length == 1) {
  2290. //cdi6xzNiNdS 电池包含单个 感觉一般都是留大
  2291. remainChild = false
  2292. } else {
  2293. let outScore = getWidthScore(box, 'out')
  2294. let childrenScores = box.contains.map(e => getWidthScore(e, 'in'))
  2295. let childAve = childrenScores.reduce((w, c) => w + c, 0) / childrenScores.length
  2296. if (childAve < -4) {
  2297. //let outScore = getWidthScore(box, 'out')
  2298. remainChild = childAve > outScore
  2299. }
  2300. }
  2301. if (!remainChild) {
  2302. box.contains.forEach(e => {
  2303. ;(e.state = '因被嵌套被删除'), (e.containBy = box)
  2304. arr.splice(arr.indexOf(e), 1)
  2305. console.log('因被嵌套被删除', ...box.contains)
  2306. })
  2307. } else {
  2308. box.state = '因嵌套其他被删除'
  2309. console.log('因嵌套其他被删除', box)
  2310. arr.splice(arr.indexOf(box), 1)
  2311. }
  2312. }
  2313. })
  2314. }
  2315. let waitFindRest = []
  2316. let Search = type => {
  2317. console.error('开始search', type)
  2318. let matchScoreMap = (this.matchScoreMap[type] = {})
  2319. let datas = {}
  2320. let panoIds = []
  2321. for (let id in this.datas) {
  2322. if (!this.datas[id]) continue
  2323. datas[id] = this.datas[id].shapes.filter(e => isType(e.category, type))
  2324. datas[id].length && panoIds.push(id)
  2325. }
  2326. for (let id in this.datas) {
  2327. //对data预处理
  2328. //(之后如果还出现不同类型重叠在一起的,需要先识别摘除下。 )4GqaqNdyjGf
  2329. if (!this.datas[id]) continue
  2330. removeContain(datas[id]) //去除线框中的嵌套,主要是一个嵌套两个的。案例:KK-1Zjm9Rbl47
  2331. if (datas[id].length) {
  2332. //融合。很多box被一分为二了,基本都是在全景图左右边界处。
  2333. let bigBoxes = getPanoBigRowBox(datas[id], { reason: 'mix' })
  2334. bigBoxes.forEach(bigBox => {
  2335. if (bigBox.boxes.length > 1) {
  2336. bigBox.boxes.forEach(box => {
  2337. ;(box.state = '被删除'), (box.mixTo = bigBox)
  2338. let i = datas[id].indexOf(box)
  2339. datas[id].splice(i, 1)
  2340. if (version == 'vision') {
  2341. i = this.datasMixed[id].shapes.findIndex(e => e.sid == box.sid)
  2342. this.datasMixed[id].shapes.splice(i, 1)
  2343. }
  2344. })
  2345. //console.log('因融合而删除', bigBox.boxes)
  2346. datas[id].push(bigBox)
  2347. if (version == 'vision') {
  2348. this.datasMixed[id].shapes.push(bigBox)
  2349. }
  2350. let shapes = this.datas[id].shapes
  2351. bigBox.index = shapes.length > 1 ? shapes[shapes.length - 2].index + 1 : 0
  2352. {
  2353. let a = bigBox.sid.split('mix-')
  2354. bigBox.sid = a[0] + bigBox.index + '(mix' + a[1] + ')' //"pano20-mix-1,2"
  2355. }
  2356. }
  2357. })
  2358. }
  2359. }
  2360. if (panoIds.length == 0) {
  2361. if (standards[type].tinyXZ) {
  2362. //fire 调试:nZrBdvRaDuC
  2363. this.expandModelBound()
  2364. }
  2365. return
  2366. }
  2367. panoIds.sort((a, b) => {
  2368. return datas[b].length - datas[a].length
  2369. })
  2370. let groups = panoIds.map(e => datas[e])
  2371. //console.log('按box个数排序:', groups.slice())
  2372. let group0 = groups[0],
  2373. len0 = group0.length
  2374. if (groups.length == 1) {
  2375. //只有一个全景里有数据
  2376. if (standards[type].tinyXZ) {
  2377. //fire. 无法match以获取groundY, 就取消。 调试: eGhyf5QdVHA
  2378. this.expandModelBound(type)
  2379. }
  2380. group0.forEach(e => createSinglePano(e))
  2381. return combines(type)
  2382. }
  2383. let finish = groups => {
  2384. waitFindRest.push({ type, args: [groups] }) //等待最后检查遗漏
  2385. if (standards[type].tinyXZ) {
  2386. //fire 调试:nZrBdvRaDuC
  2387. this.confirmGroundY(type)
  2388. }
  2389. if (standards[type].tiny) {
  2390. //monitor 调试:S9yepREK8Jl
  2391. this.expandModelBound2(type)
  2392. }
  2393. }
  2394. if (len0 == 1) {
  2395. //最多的也只有一个box。此情况大部分是空调
  2396. panoIds.forEach(e => getBoxBase(datas[e][0]))
  2397. let maxAngle
  2398. //找出centerDir夹角最大的两个pano
  2399. for (let i = 0; i < panoIds.length; i++) {
  2400. let box0 = datas[panoIds[i]][0]
  2401. for (let j = i + 1; j < panoIds.length; j++) {
  2402. let box1 = datas[panoIds[j]][0]
  2403. getMatchScore(box0, box1, { isSingle: true })
  2404. }
  2405. }
  2406. let list = Object.keys(matchScoreMap)
  2407. list.sort((a, b) => {
  2408. return matchScoreMap[b].score - matchScoreMap[a].score
  2409. })
  2410. let match = matchScoreMap[list[0]]
  2411. preDealBox(match)
  2412. if (match.score > -100) {
  2413. getBoxSize(match)
  2414. if (match.score > 0 && match.sizeAdjust < 0.1) {
  2415. new Box(matchScoreMap[list[0]])
  2416. //waitFindRest.push({ type, args: [groups] }) //等待最后检查遗漏
  2417. finish(groups)
  2418. return
  2419. }
  2420. }
  2421. //根据分数重排序,前两个已匹配的pano放在第一第二(reMatchLowScores会跳过),获得groups2
  2422. let panoIds2 = []
  2423. list.forEach(e => {
  2424. let info = matchScoreMap[e]
  2425. if (!panoIds2.includes(info.box0.pano.id)) panoIds2.push(info.box0.pano.id)
  2426. if (!panoIds2.includes(info.box1.pano.id)) panoIds2.push(info.box1.pano.id)
  2427. })
  2428. let groups2 = panoIds2.map(e => datas[e])
  2429. //继续match
  2430. reMatchLowScores([match], groups2)
  2431. finish(groups2)
  2432. return
  2433. }
  2434. {
  2435. //重新根据距离排序,挑选离所有box距离最近的两个pano (远的可能看不到box,或者得到的线框计算的位置不准。不过其实太近也不准-,-)
  2436. let counts = {}
  2437. groups.forEach(e => {
  2438. e.forEach(a => getBoxBase(a))
  2439. counts[e.length] || (counts[e.length] = [])
  2440. counts[e.length].push(e)
  2441. })
  2442. groups = []
  2443. let atWall = standards[type].atWall
  2444. let nums = Object.keys(counts)
  2445. let bestDisSquared = 2
  2446. nums.reverse()
  2447. nums.forEach(count => {
  2448. let groups_ = counts[count]
  2449. if (groups_.length > 1) {
  2450. groups_.forEach(e => {
  2451. e.disSc = e.reduce((w, c) => {
  2452. let pos = getBoxPos(c)
  2453. //let s = atWall ? Math.pow(Math.abs(c.centerDir.y), 3)*100 : 0 //在墙上的话尽量平视
  2454. //console.log(c.sid,s)
  2455. return w + Math.abs(c.pano.position.distanceToSquared(pos) - bestDisSquared) //+ s
  2456. }, 0)
  2457. })
  2458. groups_.sort((a, b) => a.disSc - b.disSc)
  2459. }
  2460. groups.push(...groups_)
  2461. })
  2462. //console.log('按距离和个数排序:', groups)
  2463. group0 = groups[0]
  2464. len0 = group0.length
  2465. }
  2466. if (type == 'cabinet') {
  2467. //转化为分组
  2468. if (searchByRow(groups, datas)) {
  2469. waitFindRest.push({ type, args: [groups, 0] }) //等待最后检查遗漏
  2470. return
  2471. }
  2472. }
  2473. //零散匹配。
  2474. let match2Group = (group0, group1, { fake }) => {
  2475. let len0 = group0.length,
  2476. len1 = group1.length
  2477. for (let i = 0; i < len0; i++) {
  2478. //复杂度:n的平方次
  2479. for (let j = 0; j < len1; j++) {
  2480. let box1 = group0[i]
  2481. let box2 = group1[j]
  2482. let result = getMatchScore(box1, box2)
  2483. }
  2484. }
  2485. //寻找最佳配对 n!种组合(是否要限制个数多的情况?) 超过8个就很恐怖
  2486. //仅先查找选中的两个pano配对
  2487. let resultPairs = []
  2488. let newGroup0 = group0.slice(0)
  2489. let newGroup1 = group1.slice(0)
  2490. /* while (newGroup1.length < newGroup0.length) {
  2491. newGroup1.push({ sid: 'void' }) //为了使排列正确,补个空,使左右两边个数相等,过后和void匹配的不会计算box
  2492. } */
  2493. let evaluateFun = getMatchScore.bind(this)
  2494. searchPair(/* group0[0] */ null, newGroup0, newGroup1, null, resultPairs, evaluateFun)
  2495. /* console.log(
  2496. 'resultPairs',
  2497. resultPairs.map(pairs => pairs.map(pair => pair.map(item => item.sid).join(' & ')))
  2498. ) */
  2499. resultPairs = resultPairs.map(pairs => {
  2500. let infos = pairs.map(pair => matchScoreMap[pair[0].sid + '&' + pair[1].sid])
  2501. let score = infos.reduce((s, e) => {
  2502. return s + (e ? e.score : 0)
  2503. }, 0)
  2504. let o = {
  2505. infos,
  2506. score,
  2507. pairs,
  2508. name: pairs.map(pair => pair.map(item => item.sid).join(' & ')),
  2509. }
  2510. return o
  2511. })
  2512. //console.log('resultPairs', resultPairs.slice())
  2513. console.log(
  2514. 'resultPairs按分数高低',
  2515. resultPairs.sort((a, b) => b.score - a.score)
  2516. )
  2517. //console.log('compu',compu)
  2518. let minScore = boundConfirmed ? -2000 : -800
  2519. let noMatches = [] //和void匹配的,需要和其他pano的重新匹配
  2520. let mayHaventMatched = []
  2521. let lowScores = []
  2522. if (resultPairs[0]) {
  2523. resultPairs[0].infos.forEach((info, i) => {
  2524. if (!info) {
  2525. noMatches.push(resultPairs[0].pairs[i].find(e => e.sid != 'void'))
  2526. return //match with void
  2527. }
  2528. if (info.score < minScore || (standards[info.boxType].bottom && info.minAng < 5)) {
  2529. lowScores.push(info)
  2530. return
  2531. }
  2532. preDealBox(info)
  2533. getBoxSize(info)
  2534. if ((info.sizeAdjust && standards[info.boxType].tiny) || info.sizeAdjust > 0.2) {
  2535. //或者识别下悬挂的且线的角度较小
  2536. lowScores.push(info)
  2537. return
  2538. }
  2539. if (info.box0.category == typeNames.battery && info.size.x < 1 && info.size.z < 1) {
  2540. //宽度较小
  2541. let vec0 = new THREE.Vector3().subVectors(info.box0.pano.position, getBoxPos(info))
  2542. let vec1 = new THREE.Vector3().subVectors(info.box1.pano.position, getBoxPos(info))
  2543. /* let k0 = Math.abs(vec0.x / vec0.z), k1 = Math.abs(vec1.x / vec1.z), maxR = 6
  2544. if(info.name == '') */
  2545. if (vec0.x * vec1.x > 0 && vec0.z * vec1.z > 0 /* || k0/k1 > maxR || k0/k1 < 1/maxR */) {
  2546. //同一个象限 或 偏向一侧
  2547. lowScores.push(info)
  2548. return
  2549. }
  2550. }
  2551. info.fake = fake
  2552. let box = new Box(info)
  2553. })
  2554. }
  2555. if (noMatches.length) {
  2556. reMatchLowScores(
  2557. noMatches.map(e => {
  2558. return { box0: e }
  2559. }),
  2560. groups,
  2561. { fake }
  2562. )
  2563. }
  2564. reMatchLowScores(lowScores, groups, { fake })
  2565. }
  2566. {//KJ-iUrPonbhCQo 大的场景有很多柜子时,groups的前两个元素很可能相隔很远,导致匹配不到柜子。
  2567. let disScores = [], bestDisSquared = 2
  2568. for (let i = 0; i < groups.length - 1; i++) {
  2569. let dis = groups[i][0].pano.position.distanceToSquared(groups[i+1][0].pano.position)
  2570. let score = 1/Math.max(dis - bestDisSquared, 0.05) + (groups[i].length + groups[i+1].length)
  2571. disScores.push()
  2572. }
  2573. for (let i = 0; i < groups.length - 1; i++) {
  2574. match2Group(groups[i], groups[i + 1], { fake: i > 0 })
  2575. if (!standards[type].tinyXZ /* && !browser.urlHasValue('many') */ || groups[i + 1].length < 2) {
  2576. break
  2577. }
  2578. }
  2579. }
  2580. finish(groups)
  2581. }
  2582. function blocked(box) {
  2583. //该box是否被实体box遮挡
  2584. //WcLVXvmV9AU:pano2-2 和 pano12-4 ; 5yhlMduTHL8:pano2-10
  2585. /* if (box.sid == 'pano6-7') {
  2586. console.log(1)
  2587. addLine(box.pano.position, box.centerBtmDir, 20)
  2588. } */
  2589. if (!standards[box.boxType].bottom) {
  2590. //当底部每个方向都有遮挡物时,其位置很可能不准。但若是部分遮住,还是有可能识别对的
  2591. if (!box.blocked) {
  2592. box.blocked = {
  2593. centerBtmDir: null,
  2594. leftBtmDir: null,
  2595. rightBtmDir: null,
  2596. }
  2597. }
  2598. for (let dir in box.blocked) {
  2599. if (!box.blocked[dir]) {
  2600. let block = boxesSolid.find(boxSolid => {
  2601. ray.set(box.pano.position, box[dir])
  2602. let o = ray.ray.intersectsBox(boxSolid.bound)
  2603. if (o) {
  2604. // 遮挡
  2605. return true
  2606. }
  2607. })
  2608. if (block) {
  2609. //若不存在,即都遮挡了,就算真的遮挡
  2610. box.blocked[dir] = block
  2611. } else {
  2612. box.blocked[dir] = false
  2613. return false //找到一个方向无遮挡则可返回
  2614. }
  2615. }
  2616. }
  2617. return true
  2618. //暂定,若某个方向已得到遮挡了,就不计算。但若没有,就再搜一遍(如果会重复搜之后再增加一个stamp啥的,比如记录下当前所有solidBoxes的name,如果改变了就重新搜下)
  2619. } else {
  2620. //若中心方向已经有同类型的box,很可能它就是自身(因为挂墙的预测位置很不准,所以需要这一步。越tiny的越能通过此来筛选)
  2621. let block = boxesSolid.find(boxSolid => {
  2622. if (boxSolid.boxType != box.boxType) return
  2623. ray.set(box.pano.position, box.centerDir)
  2624. let o = ray.ray.intersectsBox(boxSolid.bound)
  2625. if (o) {
  2626. // 遮挡
  2627. return true
  2628. }
  2629. })
  2630. if (block) {
  2631. box.blocked = block
  2632. return true
  2633. }
  2634. }
  2635. }
  2636. //调试 O540aEVF3b7 jQUQlER160 n4z0yd5tQaF WcLVXvmV9AU 8czlwsbSe5 NlUM8yGve9
  2637. function findRest(groups /* ,startIndex=2 */) {
  2638. //查找是否有遗漏。
  2639. //1 可能有距离较远的box不在头两个pano的附近导致被漏掉。(概率很小)
  2640. //2 被剩余的 (包括低分匹配中放弃的,这种需要的距离识别度高)
  2641. const tolerateWidth = { min: 0.1, max: 0.5 }
  2642. let boxes = [],
  2643. scores = new Map(),
  2644. bestDisSquared = 2
  2645. groups.forEach(g => {
  2646. boxes.push(...g)
  2647. })
  2648. boxes = boxes.filter(box => !used(box))
  2649. boxes.forEach(box => {
  2650. getBoxBase(box)
  2651. let p1 = new THREE.Vector2(box.pano.position.x, box.pano.position.z)
  2652. let p2 = new THREE.Vector2(box.btmPos.x, box.btmPos.z)
  2653. let dis = p1.distanceToSquared(p2)
  2654. let s1 = -Math.abs(dis - bestDisSquared) //将距离最佳的排前面,距离远的得到的位置不准确,也容易被遮
  2655. if (!box.pose) box.pose = getBoxPoseByPos(box, getBoxPos(box))
  2656. let s2 = getPoseScore([box.pose], box.boxType) + THREE.MathUtils.clamp(box.pose.projectWidth, box.pose.minProjectWidth, box.pose.maxProjectWidth) * 7 //projectWidth长的更安全
  2657. scores.set(box, { s1, s2, sum: s1 + s2 })
  2658. })
  2659. //console.log(boxes[0].boxType,scores)
  2660. boxes = boxes.sort((a, b) => {
  2661. return scores.get(b).sum - scores.get(a).sum
  2662. }) //FXcq5PI9QGv
  2663. //console.log(boxes)
  2664. boxes.forEach(box => {
  2665. if (box.sid == 'pano0-4') {
  2666. console.log(3)
  2667. }
  2668. if (!used(box) && !blocked(box)) {
  2669. //如果和现有的box的距离都很远,很可能是漏掉的
  2670. let near = boxesSolid.find(solidBox => {
  2671. if (solidBox.name == 'air-pano16-7&pano14-13') {
  2672. console.log(3)
  2673. }
  2674. if (solidBox.boxType != box.boxType && (solidBox.boxType == 'air-hanging' || box.boxType == 'air-hanging')) return //挂空调一般不会撞到地面上的
  2675. let p1 = getBoxPos(solidBox)
  2676. let p2 = getBoxPos(box)
  2677. let p1_ = new THREE.Vector2(p1.x, p1.z)
  2678. let p2_ = new THREE.Vector2(p2.x, p2.z)
  2679. let maxWidth = standards[box.boxType].widthNormal.max / 2
  2680. maxWidth = THREE.MathUtils.clamp(maxWidth, tolerateWidth.min, tolerateWidth.max) //因为场景精度存在较大误差,所以maxWidth不能过小,否则像灭火器摄像头都容易findRest多个
  2681. let dis = solidBox.bound.distanceToPoint(p2)
  2682. //let r0 = solidBox.boxType == 'air' ? 2 : solidBox.boxType == 'battery' ? 1.1 : 1 //空调最不容易扎堆放置,所以范围设置广一些
  2683. let r0 = standards[box.boxType].closeRatio
  2684. let r1 = math.linearClamp(box.pano.position.distanceTo(p2), 3, 20, 1, 5) //距离远的话识别、计算都会更不准确,给一定的容错. 远的尽量不findRest,即尽量>0
  2685. let ra = (solidBox.boxType == box.boxType ? 1 : 0.5) * r0 * r1 //数字越小限制越大
  2686. /* let a = maxWidth * maxWidth * ra - p1_.distanceToSquared(p2_)
  2687. let b = -dis * dis * 0.7
  2688. let c = a + b*/
  2689. let c = maxWidth * ra - dis * 1.3
  2690. /* if (c > 0) {
  2691. //太近 不创建
  2692. console.log(1)
  2693. } */
  2694. return c > 0
  2695. })
  2696. if (!near) {
  2697. reMatchLowScores([{ box0: box, log: 'findRest' }], groups, { startIndex: 0 })
  2698. } else {
  2699. //console.log('find near', near.name, box.sid)
  2700. }
  2701. }
  2702. })
  2703. }
  2704. function used(box) {
  2705. //是否已经使用过
  2706. let has = e => {
  2707. return e.box0 == box || e.box1 == box
  2708. }
  2709. let traverse = e => {
  2710. return (
  2711. has(e) ||
  2712. (e.list && e.list.some(a => traverse(a))) ||
  2713. (e.mixedFrom && e.mixedFrom.some(a => traverse(a))) ||
  2714. (e.infos && e.infos.some(u => u.group.some(r => r.boxes.some(b => b == box))))
  2715. ) //row
  2716. }
  2717. return boxesSolid.some(e => traverse(e))
  2718. }
  2719. function reMatchLowScores(lowScores, groups, { startIndex = 2, fake } = {}) {
  2720. let matched = [],
  2721. tooLows = []
  2722. let minScore = boundConfirmed ? -2000 : -800
  2723. let isSameMatch = (match0, match1) => {
  2724. //是否对应同一个box实体(不一定准),通过两两box之间是否都match来判断
  2725. //如果是相同pano但不同的box肯定不是对应同一个box实体
  2726. let ifWrong = (box0, box1) => {
  2727. if (box0 == box1) return
  2728. if (box0.pano == box1.pano) return true
  2729. let match2 = getMatchScore(box0, box1, { isSingle: true, restMatch: true })
  2730. if (match2.score < minScore) return true
  2731. }
  2732. if (ifWrong(match0.box0, match1.box0) || ifWrong(match0.box1, match1.box1) || ifWrong(match0.box0, match1.box1) || ifWrong(match0.box1, match1.box0)) return
  2733. return true
  2734. }
  2735. if (lowScores.length) {
  2736. //console.warn(lowScores[0].log || (lowScores[0].box1 ? '低分重新匹配' : '剩余匹配'), lowScores[0].box0.boxType, ...lowScores)
  2737. if (lowScores[0].box1) {
  2738. lowScores.sort((a, b) => {
  2739. //低分优先
  2740. return a.score - b.score
  2741. })
  2742. }
  2743. lowScores.forEach(info => {
  2744. //info中的box0和box1分别向后寻找其他的配对。选择分数高的配对。 但box0和box1可能是错误配对,会导致找到了替代的也可能遗漏。
  2745. /* if (info.name == 'pano0-3&pano2-3') {
  2746. console.log(1)
  2747. } */
  2748. let box01 = info.box0
  2749. let box02 = info.box1
  2750. let bigGroup = []
  2751. box02 && bigGroup.push(info)
  2752. let got
  2753. for (let cur = startIndex; cur < groups.length; cur++) {
  2754. let thirdGroup = groups[cur]
  2755. let scores0 = [],
  2756. scores1 = []
  2757. thirdGroup.forEach(box1 => {
  2758. //if (matched.includes(box1.sid)) return
  2759. if (used(box1)) return //会不会太严格?
  2760. if (box1.pano != box01.pano && box1 != box02) {
  2761. let r1 = getMatchScore(box01, box1, { isSingle: true, restMatch: true })
  2762. r1.score > minScore * 1.5 && scores0.push(r1)
  2763. }
  2764. if (box02 && box1.pano != box02.pano && box1 != box01) {
  2765. let r2 = getMatchScore(box02, box1, { isSingle: true, restMatch: true })
  2766. r2.score > minScore * 1.5 && scores1.push(r2)
  2767. }
  2768. })
  2769. scores0.sort((a, b) => {
  2770. return b.score - a.score
  2771. })
  2772. scores1.sort((a, b) => {
  2773. return b.score - a.score
  2774. })
  2775. scores0[0] && bigGroup.push(scores0[0])
  2776. scores1[0] && bigGroup.push(scores1[0])
  2777. }
  2778. bigGroup.sort((a, b) => {
  2779. return b.score - a.score
  2780. })
  2781. let goodList = bigGroup.slice(0, 10).map(e => {
  2782. if (!getBoxPos(e)) return e
  2783. preDealBox(e)
  2784. getBoxSize(e)
  2785. return e
  2786. })
  2787. let goodList2 = goodList
  2788. .sort((a, b) => {
  2789. return b.score - a.score
  2790. })
  2791. .slice(0, 3)
  2792. if (goodList2.length == 0) {
  2793. return fake || createSinglePano(box01, 0.6) //minScorePercent原因:剩余匹配时位置不太好的案例: AhMgXXjM15
  2794. }
  2795. if (goodList2[0].score > minScore * 0.65) {
  2796. goodList2 = goodList2.filter(e => e.score > minScore * 0.65)
  2797. } else {
  2798. /* goodList2 = [goodList2[0]] // 最高分已经过小
  2799. if (goodList2[0].score < -1500) { */
  2800. if (!box02) {
  2801. if (info.log == 'findRest' || getBoxPos(box01).distanceTo(box01.pano.position) < 2.5) {
  2802. //远距离不准,留到过后findRest
  2803. fake || createSinglePano(box01)
  2804. }
  2805. return
  2806. }
  2807. //console.warn('分数过低,是否有匹配错误?', goodList2[0])
  2808. return tooLows.push(goodList2[0])
  2809. //}
  2810. }
  2811. if (goodList2.length) {
  2812. //需要确认两两之间是配对的,也就是都对应同一个box
  2813. let subGroups = [],
  2814. boxes = []
  2815. for (let i = 0, len = goodList2.length; i < len; i++) {
  2816. //向后选择队友
  2817. let match0 = goodList2[i]
  2818. if (subGroups.some(e => e.includes(match0))) continue //被挑选了的没有选择权
  2819. let gr = [match0]
  2820. for (let j = i + 1; j < len; j++) {
  2821. let match1 = goodList2[j]
  2822. if (isSameMatch(match0, match1)) {
  2823. //可能不是同一个,所以需要检验
  2824. gr.push(match1)
  2825. }
  2826. }
  2827. //if(gr.length>1){
  2828. subGroups.push(gr)
  2829. //}
  2830. }
  2831. //console.log('lowScores subGroups', subGroups)
  2832. subGroups.forEach(pair => {
  2833. boxes.push(mixMatchBox(pair, lowScores[0].log, fake))
  2834. })
  2835. fake || combineBoxes(boxes) //很可能其实还是同一个,需要检验是否要融合
  2836. }
  2837. })
  2838. //改为之后 findRest, 因为两者都single的可能性低
  2839. /* let judge = box => {
  2840. if (!used(box)) {
  2841. matched.push(box)
  2842. createSinglePano(box)
  2843. }
  2844. }
  2845. tooLows.forEach(e => {
  2846. judge(e.box0)
  2847. judge(e.box1)
  2848. }) */
  2849. }
  2850. }
  2851. function mixMatchBox(list, log, fake) {
  2852. let center = new THREE.Vector3(),
  2853. size = new THREE.Vector3(),
  2854. name,
  2855. bound = new THREE.Box3()
  2856. list.forEach(e => {
  2857. let _bound = new THREE.Box3().setFromCenterAndSize(getBoxFinalPos(e), e.size)
  2858. bound.union(_bound)
  2859. })
  2860. //bound.getCenter(center) //这两种获得center的方法哪个准?
  2861. bound.getSize(size)
  2862. list.forEach(e => center.add(getBoxPos(e)))
  2863. center.multiplyScalar(1 / list.length)
  2864. let { xProp, yProp } = list.find(e => e.xProp) || {}
  2865. if (xProp && list.find(e => e.xProp && e.xProp != xProp)) {
  2866. //如果有不同的话
  2867. xProp = yProp = null
  2868. }
  2869. let prefix = log == 'findRest' ? 'rest:' : 'low:'
  2870. let object = {
  2871. name: prefix + list.map(e => e.name),
  2872. boxType: list[0].boxType,
  2873. center,
  2874. size,
  2875. list,
  2876. xProp,
  2877. yProp,
  2878. }
  2879. xProp || getBoxDirProp(object)
  2880. let o = restrictSize(size.x, size.y, size.z, object)
  2881. size.x = o.x
  2882. size.y = o.y
  2883. size.z = o.z
  2884. object.fake = fake
  2885. let box = new Box(object)
  2886. //console.log('mixMatchBox', box)
  2887. return box
  2888. }
  2889. function combineBoxes(boxes, typeCount) {
  2890. //调试 tY4ot33f8vT UWrshepp0G5的一高一低的电箱
  2891. //判断这些实体boxes是否需要合并 主要用于重复识别(重叠面积较大) 电池还可能是拼接
  2892. const group = []
  2893. if (boxes.length > 1) {
  2894. let boxType = boxes[0].boxType
  2895. let { min, max } = standards[boxType].widthNormal
  2896. for (let i = 0, len = boxes.length; i < len - 1; i++) {
  2897. let box0 = boxes[i]
  2898. if (box0.fake) continue
  2899. for (let j = i + 1; j < len; j++) {
  2900. let box1 = boxes[j]
  2901. if (box1.fake) continue
  2902. let bound = box0.bound.clone().union(box1.bound)
  2903. let size = bound.getSize(new THREE.Vector3())
  2904. let intersect = box0.bound.intersectsBox(box1.bound)
  2905. if (box0.boxType == 'electric') {
  2906. console.log(1)
  2907. }
  2908. let minX = min,
  2909. minZ = min,
  2910. maxX = max,
  2911. maxZ = max,
  2912. maxY = standards[boxType].height.max
  2913. if (box0.xProp && box1.xProp && box0.xProp == box1.xProp) {
  2914. maxX = standards[boxType][box0.xProp].max
  2915. maxZ = standards[boxType][box0.yProp].max
  2916. /* minX = standards[boxType][box0.xProp].min
  2917. minZ = standards[boxType][box0.yProp].min */
  2918. if (box0.boxType != box1.boxType) {
  2919. //air & airSmart
  2920. maxX = Math.max(maxX, standards[box1.boxType][box1.xProp].max)
  2921. maxZ = Math.max(maxZ, standards[box1.boxType][box1.yProp].max)
  2922. /* minX = Math.min(minX, standards[box1.boxType][box1.xProp].min)
  2923. minZ = Math.min(minZ, standards[box1.boxType][box1.yProp].min) */
  2924. }
  2925. }
  2926. maxX = Math.max(maxX, box0.size.x, box1.size.x) //必须大于各自的size,否则无法去除本身就oversize的box中包含的
  2927. maxZ = Math.max(maxZ, box0.size.z, box1.size.z)
  2928. maxY = Math.max(maxY, box0.size.y, box1.size.y)
  2929. let r = intersect ? 1.5 : 1.3 /* / standards[box1.boxType].closeRatio */ //如果是没有交集,限制更大些
  2930. //若需要更精确的结果,可以getMixBox算出重叠面积,重叠少,且各自都不太小,就不合并。但考虑到电池边界很模糊,合并了也无大碍。
  2931. if (box0.boxType == 'battery') {
  2932. r *= 1.3 //比较可能扎堆
  2933. }
  2934. let maxDiff = 0.4,
  2935. maxDiffX = maxDiff,
  2936. maxDiffZ = maxDiff,
  2937. rx = r,
  2938. rz = r
  2939. if (standards[box0.boxType].atWall == 1) {
  2940. //在墙面上不可能叠放,所以厚度限制可放宽
  2941. let s = 4
  2942. if (box0.yProp == 'width') {
  2943. maxDiffX *= s
  2944. rx *= s
  2945. } else if (box0.xProp == 'width') {
  2946. maxDiffZ *= s
  2947. rz *= s
  2948. }
  2949. }
  2950. //如果某个点位含有类似和这俩相近的box: A9rCPzp2UD9两个fire合并了。好难写,算了
  2951. /* if(box0.boxType){
  2952. this.datasMixed
  2953. rx = rz = 1.1
  2954. } */
  2955. if (size.x - maxX < maxDiffX && size.x < maxX * rx && size.z < maxZ * rz && size.z - maxZ < maxDiffZ && size.y < maxY * r && size.y - maxY < maxDiff) {
  2956. //总size不会太大
  2957. common.pushToGroupAuto([box0, box1], group)
  2958. }
  2959. }
  2960. }
  2961. if (group.length) {
  2962. //虽然如果三个以上可能会超出maxWidth。 不过3个的概率很低,且可以限制宽度
  2963. group.forEach(pair => {
  2964. let boxTypes = []
  2965. let bound = new THREE.Box3()
  2966. pair.forEach(e => {
  2967. bound.union(e.bound)
  2968. e.dispose()
  2969. if (typeCount > 1) {
  2970. //判断boxType: 寻找所使用的box总分最高的boxType
  2971. let a = boxTypes.find(a => a.boxType == e.boxType)
  2972. let score = 0
  2973. let add = box => {
  2974. box && (score += box.score)
  2975. }
  2976. e.traversePair(e => {
  2977. add(e.box0)
  2978. add(e.box1)
  2979. })
  2980. if (a) {
  2981. a.score += score
  2982. } else {
  2983. boxTypes.push({ score, boxType: e.boxType })
  2984. }
  2985. }
  2986. })
  2987. if (typeCount > 1) {
  2988. boxTypes.sort((a, b) => {
  2989. return b.score - a.score
  2990. })
  2991. boxType = boxTypes[0].boxType
  2992. }
  2993. let size = bound.getSize(new THREE.Vector3())
  2994. let center = bound.getCenter(new THREE.Vector3())
  2995. let { xProp, yProp } = pair.find(e => e.xProp) || {}
  2996. if (xProp && pair.find(e => e.xProp && e.xProp != xProp)) {
  2997. //如果有不同的话
  2998. xProp = yProp = null
  2999. }
  3000. let info = {
  3001. name: 'mix:' + pair.map(e => ' ' + e.name),
  3002. mixedFrom: pair,
  3003. boxType,
  3004. center,
  3005. size,
  3006. xProp,
  3007. yProp,
  3008. }
  3009. xProp || getBoxDirProp(info)
  3010. let o = restrictSize(size.x, size.y, size.z, info)
  3011. size.x = o.x
  3012. size.y = o.y
  3013. size.z = o.z
  3014. let box = new Box(info)
  3015. console.error('混合', boxType, pair, box)
  3016. })
  3017. }
  3018. }
  3019. }
  3020. function combines(types) {
  3021. //合并boxSolids . battery经常嵌套
  3022. if (!(types instanceof Array)) types = [types]
  3023. let boxes = boxesSolid.filter(e => types.includes(e.boxType))
  3024. combineBoxes(boxes, types.length)
  3025. }
  3026. let createSinglePano = (box, minScorePercent = 1) => {
  3027. //仅用一个pano中的data来创建。 悬挂于墙上的准确性依赖于墙的准确性。
  3028. if (box.score < MinBoxInitialScore) {
  3029. //如Xszq2fv03b的电池pano8-0其实是纸箱、 WZQoMbNmNTu的pano14-0分数0.649
  3030. return console.error('取消createSinglePano: 线框识别分数低,可能错误', box.sid, box)
  3031. }
  3032. getBoxBase(box)
  3033. let center = getBoxPos(box)
  3034. if (safeBound.distanceToPoint(center) > 0) {
  3035. return console.log('取消createSinglePano:超出safebound', box) //可能是错误的线框,如H7pg1tO9oeJ pano8-1
  3036. }
  3037. let info = {
  3038. name: box.sid,
  3039. box0: box,
  3040. center,
  3041. topPos: box.topPos,
  3042. btmPos: box.btmPos,
  3043. }
  3044. preDealBox(info)
  3045. const minScore = -500 * minScorePercent //调试 3MnIWabM6ne Tmo1vLp9Q13
  3046. let a = getPoseScore(info.boxposes, box.boxType /* true */)
  3047. if (!standards[box.boxType].bottom && box.btmPos) {
  3048. a -= (box.btmPos.y - groundY) * 3000 //底部被遮住一部分(但是像vHC1GfkdKtD在户外,容易底部变高到bound外去怎么办)
  3049. }
  3050. let failed = a < minScore
  3051. console.log('createSinglePano', failed ? '失败' : '成功', 'pose score:', a, box.sid, box)
  3052. if (failed) return
  3053. //addLabel(center, 'center', { a: 0.3 })
  3054. //info.topPos && addLabel(info.topPos, 'topPos', { a: 0.3 })
  3055. info.score = a
  3056. getBoxSize(info)
  3057. new Box(info)
  3058. }
  3059. //去除挨得很近的漫游点,因为两个接近的点match出的值误差很大
  3060. {
  3061. this.removedDatas = {}
  3062. let panoIds = Object.keys(this.datas)
  3063. let panoIds = Object.keys(this.datas).filter(id=>{
  3064. if(!player.model.panos.index[id]){//排除数据错误
  3065. this.removedDatas[id] = this.datas[id]
  3066. delete this.datas[id]
  3067. return
  3068. }
  3069. return true
  3070. })
  3071. let len = panoIds.length
  3072. for (let i = 0; i < len; i++) {
  3073. let pano0 = player.model.panos.index[panoIds[i]]
  3074. for (let j = i + 1; j < len; j++) {
  3075. let pano1 = player.model.panos.index[panoIds[j]]
  3076. if (pano0.position.distanceToSquared(pano1.position) < 0.01) {
  3077. //离的很近。保留shape多的那个data
  3078. let remove
  3079. if (this.datas[panoIds[i]].shapes.length > this.datas[panoIds[j]].shapes.length) {
  3080. remove = panoIds[j]
  3081. } else {
  3082. remove = panoIds[i]
  3083. }
  3084. console.log(`删除pano${remove}的data,因pano${panoIds[i]}和pano${panoIds[j]}很近`)
  3085. this.removedDatas[remove] = this.datas[remove]
  3086. delete this.datas[remove]
  3087. }
  3088. }
  3089. }
  3090. }
  3091. //this.expandModelBound()
  3092. // if (version == 'vision') this.datasMixed = common.CloneObject(this.datas, null, [player.model.panos.list[0].constructor])
  3093. if (version == 'vision')
  3094. this.datasMixed = common.CloneObject(this.datas, null, undefined, data => {
  3095. return data['category']
  3096. })
  3097. /* Search('cabinet')
  3098. Search('air')
  3099. Search('battery') */
  3100. for (let i in typeNames) {
  3101. Search(i)
  3102. }
  3103. console.log('----FindRest----')
  3104. waitFindRest.forEach(e => {
  3105. findRest(...e.args)
  3106. if (e.type != 'air' && e.type != 'airSmart' && e.type != 'cabinet') combines(e.type)
  3107. })
  3108. combines(['air', 'airSmart']) //这两种合在一起combine,因为太像了容易识别出多个 nZrBdvRaDuC
  3109. console.log('cost:', Date.now() - startTime, 'ms, boxSolid:', this.boxesSolid.map(e=>e.position), '共'+this.boxesSolid.length+'个')
  3110. }
  3111. /* let getSid = (function(){
  3112. let sid = 0
  3113. return function(){
  3114. return sid++
  3115. }
  3116. })() */
  3117. let done = () => {
  3118. for (let panoId in this.datas) {
  3119. if (!this.datas[panoId]) continue
  3120. this.datas[panoId].shapes = this.datas[panoId].shapes.map((shape, i) => {
  3121. return Object.assign(
  3122. {
  3123. sid: 'pano' + panoId + '-' + i,
  3124. category: shape.category, //提前 便于调试
  3125. pano: player.model.panos.index[panoId],
  3126. index: i,
  3127. },
  3128. shape
  3129. )
  3130. })
  3131. }
  3132. if (this.ifAnalyze) {
  3133. this.panoBound = new THREE.Box3()
  3134. player.model.chunks.forEach(e => {
  3135. modelBound.union(e.geometry.boundingBox) //注:不用model.boundingBox是 因为union了pano的position的
  3136. })
  3137. //针对部分模型错误,只有底面的,union一下pano.position
  3138. let minY = Infinity,
  3139. minYs = []
  3140. let panos = player.model.panos.list.filter(e => e.isAligned())
  3141. panos.forEach(e => {
  3142. let bound = new THREE.Box3().setFromCenterAndSize(e.position, new THREE.Vector3(0.1, 0.1, 0.1))
  3143. modelBound.union(bound)
  3144. this.panoBound.union(bound)
  3145. minY = Math.min(e.floorPosition.y, minY)
  3146. //avePanoFY += e.floorPosition.y
  3147. minYs.push(e.floorPosition.y)
  3148. })
  3149. groundY = modelBound.min.y
  3150. minYs.sort((a, b) => {
  3151. return a - b
  3152. })
  3153. //console.log(minYs)
  3154. let midFloorY = minYs[Math.floor(minYs.length / 2)]
  3155. console.error('minY', minY, 'midFloorY', midFloorY, '原groundY', groundY)
  3156. this.minY = minY
  3157. //部分模型底部高度错误
  3158. /* if (minY > groundY) {
  3159. console.error('minY > groundY', minY, groundY)
  3160. groundY = modelBound.min.y = midFloorY //案例nZrBdvRaDuC
  3161. } else {
  3162. if (groundY - minY > 0.05) console.warn('minY', minY, 'groundY', groundY)
  3163. groundY = modelBound.min.y = midFloorY , document.title += ' new' //修改以后未必更好所以暂时不修改 变更好的:eGhyf5QdVHA
  3164. } */
  3165. groundY = modelBound.min.y = midFloorY //这个y可能不准。需要通过fire的btmPos.y来确定
  3166. safeBound = this.safeBound = modelBound
  3167. groundPlane.setFromNormalAndCoplanarPoint(new THREE.Vector3(0, 1, 0), new THREE.Vector3(0, groundY, 0))
  3168. beginCompute()
  3169. }
  3170. if (version == 'vision') this.load(player.currentPano.id)
  3171. }
  3172. async function load(panoId) {
  3173. let data = await http.post('/service/scene/sceneMarkShape/getInfo', { num: player.$app.config.num, imagePath: panoId + '.jpg' })
  3174. // console.error(data)
  3175. if (!data.data || !data.success) return (this.datas[panoId] = null)
  3176. this.datas[panoId] = data.data
  3177. if (Object.keys(this.datas).length == panosCount && !player.model.panos.list.some(e => e.isAligned() && !(panoId in this.datas))) {
  3178. done()
  3179. }
  3180. }
  3181. async function loadAll() {
  3182. let data = await http.post('/service/shapes/sceneMarkShape/getInfos', { num: player.$app.config.num })
  3183. if (!data.data || !data.success) return
  3184. if (browser.urlHasValue('ext')) {
  3185. //利用算好的toJson的数据展示框
  3186. //console.log('box8',data.data)
  3187. data.data.box8.dataSet.boxes.forEach(e => {
  3188. let point1 = math.convertVisionVector(new THREE.Vector3().fromArray(e.points[0]))
  3189. let point2 = math.convertVisionVector(new THREE.Vector3().fromArray(e.points[1]))
  3190. let point3 = math.convertVisionVector(new THREE.Vector3().fromArray(e.points[2]))
  3191. let point4 = math.convertVisionVector(new THREE.Vector3().fromArray(e.points[4]))
  3192. let size = new THREE.Vector3(point1.distanceTo(point2), point1.y - point4.y, point3.distanceTo(point2))
  3193. let center = new THREE.Vector3().addVectors(point3, point4).multiplyScalar(0.5) //对角线中点
  3194. //可能需要考虑上rotation,现暂时不需要
  3195. let box = new Box({
  3196. buildFromData: true,
  3197. center,
  3198. size,
  3199. boxType: e.type,
  3200. name: e.sid,
  3201. })
  3202. })
  3203. this.ifAnalyze = false
  3204. }
  3205. let box4 = Array.isArray(data.data) ? data.data : data.data.box4 // 兼容旧数据
  3206. box4.forEach(e => {
  3207. let panoId = e.imagePath.split('.jpg')[0]
  3208. this.datas[panoId] = e
  3209. })
  3210. done()
  3211. }
  3212. let panosCount = 0
  3213. if (!dataList) {
  3214. /* player.model.panos.list.forEach(e => {
  3215. if (!e.isAligned()) return
  3216. panosCount++
  3217. load.bind(this)(e.id)
  3218. }) */
  3219. loadAll.bind(this)() //测试环境
  3220. } else {
  3221. //when version == 'output'
  3222. dataList.forEach(e => {
  3223. let panoId = e.imagePath.split('.jpg')[0]
  3224. this.datas[panoId] = e
  3225. })
  3226. done()
  3227. }
  3228. }
  3229. expandModelBound() {
  3230. //有的模型太窄,容易将一排的柜体当做墙壁。所以可以根据box位置扩展bound. (如果因为点位太少,导致内部的电池远超模型范围,就不管了。R7xZsmm9FsG)
  3231. const maxDis0 = 1, //最终位置不会超过这个距离
  3232. maxDis1 = 3 //搜寻范围。不可扩展太宽,否则不准确的框会飘很远,甚至多画多个box,如R7xZsmm9FsG
  3233. let newBound = modelBound.clone()
  3234. let list = []
  3235. for (let panoId in this.datas) {
  3236. if (!this.datas[panoId]) continue
  3237. let { imageWidth, imageHeight } = this.datas[panoId]
  3238. this.datas[panoId].shapes.forEach(box => {
  3239. getBoxBase(box, imageWidth, imageHeight)
  3240. if (box.sid == 'pano2-10') {
  3241. console.log(4)
  3242. }
  3243. if (box.btmPosPredict) {
  3244. let far = box.pano.position.distanceToSquared(box.btmPosPredict)
  3245. if (far > 20) return //太远不准
  3246. let dis = modelBound.distanceToPoint(box.btmPosPredict)
  3247. if (dis > 0 && dis < maxDis1) {
  3248. //maxDis1用来防air-hanging和一些错误的框
  3249. list.push({ box, dis })
  3250. }
  3251. }
  3252. })
  3253. }
  3254. list.sort((a, b) => a.dis - b.dis)
  3255. //let mid = list[Math.floor(list.length/2)]
  3256. let mid = list.length // /2
  3257. for (let i = 0; i < mid; i++) {
  3258. let box = list[i].box
  3259. let pos = box.btmPosPredict
  3260. if (list[i].dis > maxDis0) {
  3261. let p1 = pos.clone().clamp(modelBound.min, modelBound.max)
  3262. let vec = new THREE.Vector3().subVectors(pos, p1).normalize().multiplyScalar(maxDis0)
  3263. pos = new THREE.Vector3().addVectors(p1, vec)
  3264. }
  3265. let marginBound = new THREE.Box3().setFromCenterAndSize(pos, new THREE.Vector3(0.2, 0, 0.2))
  3266. newBound.union(marginBound)
  3267. }
  3268. skyBoxTight = new BoundingMesh(
  3269. newBound,
  3270. new THREE.MeshBasicMaterial({
  3271. side: THREE.DoubleSide,
  3272. wireframe: true,
  3273. transparent: true,
  3274. opacity: 0.05,
  3275. }),
  3276. 0
  3277. )
  3278. //skyBoxTight.visible = false
  3279. skyBoxTight.updateMatrixWorld() //不update的话raycaster是错的
  3280. meshGroup.add(skyBoxTight)
  3281. this.skyBoxTight = skyBoxTight
  3282. this.safeBound = this.safeBoundFirstVer = safeBound = newBound
  3283. boundConfirmed = true
  3284. console.log('bound1:', this.safeBound.min.toArray(), this.safeBound.max.toArray())
  3285. }
  3286. expandModelBound2(type) {
  3287. let material = skyBoxTight.material
  3288. skyBoxTight.geometry.dispose()
  3289. boxesSolid.forEach(e => {
  3290. if (e.boxType != type) return
  3291. this.safeBound.union(e.bound)
  3292. })
  3293. skyBoxTight = new BoundingMesh(this.safeBound, material, 0)
  3294. //skyBoxTight.visible = false
  3295. skyBoxTight.updateMatrixWorld() //不update的话raycaster是错的
  3296. meshGroup.add(skyBoxTight)
  3297. this.skyBoxTight = skyBoxTight
  3298. console.log('bound2:', this.safeBound.min.toArray(), this.safeBound.max.toArray())
  3299. }
  3300. /* adjustModelBound(type){
  3301. if(standards[type].atWall != 1)return //必须是挂墙的
  3302. this.lastSafeBound = this.safeBound.clone()
  3303. let list = []
  3304. let add = e => {
  3305. if (e.btmPos && e.box1 && e.minAng>10 ) {
  3306. //有 box1才能确定是getIntersect得到的pos
  3307. list.includes(e) || e.push(e)
  3308. }
  3309. }
  3310. this.boxesSolid.forEach((solidBox)=>{
  3311. if(solidBox.boxType != type)return
  3312. solidBox.traversePair(add)
  3313. })
  3314. let wallSides = {'x+': 0, 'x-': 0, 'z+': 0, 'z-': 0}
  3315. let getBoxSide = (e)=>{
  3316. for(let i in e.panosDir){
  3317. if(e.panosDir[i] != 0){
  3318. wallSides[i] += 1
  3319. }
  3320. }
  3321. }
  3322. if(list.length){
  3323. this.safeBound = this.panoBound.clone()
  3324. list.forEach((e)=>{
  3325. this.safeBound.union(e.bound)//还是说使用cIntersect来expand?
  3326. getBoxSide()
  3327. })
  3328. }
  3329. skyBoxTight = new BoundingMesh(this.safeBound, material, 0)
  3330. //skyBoxTight.visible = false
  3331. skyBoxTight.updateMatrixWorld() //不update的话raycaster是错的
  3332. meshGroup.add(skyBoxTight)
  3333. this.skyBoxTight = skyBoxTight
  3334. //主要目的,针对那些模型比room大得多的情况,比如多了门外的部分,造成墙面不准。
  3335. //monitor的可以完全决定位置。其他的只能收缩(但是不能收缩超过panos和第一次expand的那些btm的bound里)
  3336. //太麻烦了,且考虑到即使墙面完全准确也可能反倒把它变的不准的风险。
  3337. } */
  3338. /* 最低点使用fire的吗
  3339. MW6MEeCOy9Y 只有最近三个点和fire接近
  3340. 3MnIWabM6ne fire combine了
  3341. ov5NTEImzhW fire多个
  3342. n4z0yd5tQaF fire多两个错位离谱的。。 位置和显示的不一样。。
  3343. 5BaiXkK6Ag1 fire算的不太准 低了0.1 样本数仅两个
  3344. */
  3345. confirmGroundY(type) {
  3346. //利用fire来确定地面高度(会稍低于地板,但box框不打滑且xz更准,估计因box框比box大)。调试:3MnIWabM6ne up9PPZkx1px 4GqaqNdyjGfs GFbQi1LiSij
  3347. if (boundConfirmed) return
  3348. let btmYs = [],
  3349. pairs = [],
  3350. needCount = 5,
  3351. maxDis = 3,
  3352. btmY = 0
  3353. let add = e => {
  3354. if (e.btmPos && e.box1 /* && e.score >= minScore */) {
  3355. //有 box1才能确定是getIntersect得到的pos
  3356. pairs.push(e)
  3357. }
  3358. }
  3359. boxesSolid.forEach(e => {
  3360. if (e.boxType != type) return
  3361. e.traversePair(add)
  3362. })
  3363. let usePairs = pairs
  3364. if (pairs.length > needCount) {
  3365. //调试:Y8czF2Z3h9m
  3366. let disMap = new Map()
  3367. pairs.forEach(e => {
  3368. disMap.set(e, Math.max(e.box0.pano.position.distanceTo(e.box0.btmPosPredict), e.box1.pano.position.distanceTo(e.box1.btmPosPredict)))
  3369. })
  3370. pairs.sort((a, b) => {
  3371. return disMap.get(a) - disMap.get(b)
  3372. }) //距离从近到远 . 远处的高度可能偏离严重,就不管了,而且框也不一定准
  3373. usePairs = pairs.slice(0, needCount)
  3374. for (let i = needCount; i < pairs.length; i++) {
  3375. if (disMap.get(pairs[i]) < maxDis) {
  3376. usePairs.push(pairs[i])
  3377. }
  3378. }
  3379. }
  3380. usePairs.forEach(e => {
  3381. //有 box1才能确定是getIntersect得到的pos
  3382. let btmY_ = e.btmPos.y
  3383. const width = e.size.x / 2
  3384. let h0 = width / Math.tan(Math.acos(-e.box0.centerBtmDir.y)) //|centerBtmDir.y| 即俯视角度的cos
  3385. let h1 = width / Math.tan(Math.acos(-e.box1.centerBtmDir.y))
  3386. let h = Math.min(h0, h1) //选个小的吧,因浅的会更快接触到中心,虽然交点是两条射线最近点 不一定在fire中心
  3387. //如果毫无误差,且centerBtmDir.y相同,只要两条线centerBtmDir的xz相同,交点就是和地面的交点,而只要不同,交点就是和地面的交点,而只要不同,交点必然是在fire中心之下。随着minAng和centerBtmDir.y 交点在这中间变化
  3388. let r = math.linearClamp(e.minAng, 0, 90, 0, 1)
  3389. btmY_ += h * r
  3390. btmYs.push(btmY_)
  3391. btmY += btmY_
  3392. })
  3393. btmY /= btmYs.length
  3394. if (btmYs.length) {
  3395. console.error(
  3396. 'confirmGroundY',
  3397. btmY,
  3398. /* '样本数', btmYs.length, */ btmYs,
  3399. usePairs.map(e => e.name),
  3400. pairs
  3401. )
  3402. /* if(btmYs.length == 1 && pairs[0].minAngle<12 && ){
  3403. } */
  3404. groundY = this.safeBound.min.y = this.panoBound.min.y = btmY // = -1.1
  3405. groundPlane.setFromNormalAndCoplanarPoint(new THREE.Vector3(0, 1, 0), new THREE.Vector3(0, groundY, 0))
  3406. }
  3407. let { min, max } = standards[type].height
  3408. boxesSolid.slice().forEach(e => {
  3409. if (e.boxType != type) return
  3410. if (e.fake) {
  3411. e.dispose()
  3412. return
  3413. }
  3414. let topY = e.topPos
  3415. ? e.topPos.y
  3416. : e.list.reduce((w, c) => {
  3417. return w + c.topPos.y
  3418. }, 0) / e.list.length
  3419. e.size.y = THREE.MathUtils.clamp(topY - groundY, min, max)
  3420. e.setFromInfo(e)
  3421. if (version == 'vision') e.draw()
  3422. })
  3423. this.panoBound
  3424. this.expandModelBound()
  3425. }
  3426. bindEvents() {
  3427. if (version != 'vision') return
  3428. player.on(PlayerEvents.FlyingStarted, e => {
  3429. //if(e.mode == 'dollhouse')return
  3430. // 点位跳转前清除已有线框
  3431. this.traverse(obj => {
  3432. if (obj.isMesh) {
  3433. obj.geometry.dispose()
  3434. obj.material.dispose()
  3435. }
  3436. })
  3437. this.wireframes.clear()
  3438. this.currentId = null
  3439. })
  3440. player.on(PlayerEvents.FlyingEnded, () => {
  3441. // 点位跳转后加载线框
  3442. if (player.mode != Viewmode.PANORAMA) return // 只有PANORAMA模式下需要加载
  3443. this.load(player.currentPano.id)
  3444. })
  3445. if (this.ifAnalyze) {
  3446. setTimeout(() => {
  3447. {
  3448. let btn = document.createElement('button')
  3449. btn.innerHTML = '点击切换box显示'
  3450. btn.onclick = () => {
  3451. this.boxesSolid.forEach(e => ((e.boxHelper.visible = !e.boxHelper.visible), (e.label.visible = !e.label.visible)))
  3452. }
  3453. document.querySelector('#app').appendChild(btn)
  3454. btn.id = 'boxWire'
  3455. btn.style.position = 'fixed'
  3456. btn.style['z-index'] = '100'
  3457. btn.style.background = '#e00472'
  3458. btn.style.padding = '10px'
  3459. btn.style.bottom = '80px'
  3460. }
  3461. {
  3462. let btn = document.createElement('button')
  3463. btn.innerHTML = '点击切换矩形框显示'
  3464. btn.onclick = () => {
  3465. this.wireframes.visible = !this.wireframes.visible
  3466. }
  3467. document.querySelector('#app').appendChild(btn)
  3468. btn.id = 'wireframes'
  3469. btn.style.position = 'fixed'
  3470. btn.style['z-index'] = '100'
  3471. btn.style.background = '#419aff'
  3472. btn.style.padding = '10px'
  3473. btn.style.bottom = '130px'
  3474. }
  3475. }, 1000)
  3476. }
  3477. }
  3478. /**
  3479. * 加载点位标记数据
  3480. * @param {*} panoId
  3481. */
  3482. load(panoId) {
  3483. let data = this.datasMixed[panoId] || this.datas[panoId]
  3484. if (!data) {
  3485. if (!(panoId in this.datas))
  3486. setTimeout(() => {
  3487. this.load(panoId)
  3488. }, 100) //否则无数据
  3489. return
  3490. }
  3491. if (player.currentPano.id != panoId || player.flying || this.currentId == panoId) return // 防止连续跳转点位时,clear后才load好上一点位的数据,导致出现之前的标记
  3492. this.currentId = panoId
  3493. let { shapes, imageHeight, imageWidth } = data //data.data
  3494. let allShapes = shapes.slice()
  3495. shapes.forEach(e => {
  3496. if (e.boxes) allShapes.push(...e.boxes)
  3497. })
  3498. let fireIndex = 0
  3499. allShapes.forEach(shape => {
  3500. // 填充色和线框色
  3501. // let { fill_color, line_color } = shape
  3502. getUVs(shape)
  3503. getCenterDir(shape, player.currentPano)
  3504. let { fill_color, color = [56, 56, 255] } = shape
  3505. let line_color = [...color, 255]
  3506. if (!fill_color) fill_color = [255, 255, 255, 0]
  3507. if (!line_color) line_color = [255, 0, 0, 255]
  3508. if (shape.boxes) {
  3509. line_color = [20, 205, 255, 255]
  3510. }
  3511. let pos = getBoxPos(shape)
  3512. let dis = pos ? shape.pano.position.distanceTo(getBoxPos(shape)) : 1
  3513. let labelShift = (shape.boxType == 'fire' ? 0 : -0.2) / dis
  3514. let name = shape.category + '-' + shape.sid
  3515. //if(shape.score < MinBoxInitialScore){
  3516. name = [name, 'sc: ' + math.toPrecision(shape.score, 3)]
  3517. //}
  3518. this.showSignalFrom2d(
  3519. name,
  3520. shape.bbox2,
  3521. imageWidth,
  3522. imageHeight,
  3523. {
  3524. fill: {
  3525. color: new THREE.Color().setRGB(fill_color[0] / 255, fill_color[1] / 255, fill_color[2] / 255),
  3526. opacity: fill_color[3] / 255,
  3527. },
  3528. line: {
  3529. color: new THREE.Color().setRGB(line_color[0] / 255, line_color[1] / 255, line_color[2] / 255),
  3530. opacity: shape.category == 'cabling_rack' ? 0.4 : line_color[3] / 255, //走线架太绕,扰乱视线
  3531. },
  3532. },
  3533. shape.centerDir,
  3534. labelShift,
  3535. shape.state == '被删除'
  3536. )
  3537. })
  3538. // })
  3539. // .catch(err => console.log(`点位${panoId}无标记数据或数据出错:`, err))
  3540. }
  3541. /**
  3542. * 根据坐标标记全景图
  3543. *
  3544. * 存在的问题:如果要准确复现全景图上的线框,上下边框会变为弧形。而按顶点连直线的话,180度以上会出bug。
  3545. * 解决方式:目前150度以下只画出4个顶点然后连直线,150度以上准确画出全景图线框。
  3546. */
  3547. showSignalFrom2d(name, rect, w, h, options, centerDir, labelShift, removed) {
  3548. // 目前rect给的是矩形对角的两个点坐标,将它扩展成四个顶点
  3549. let cornerArr = [new THREE.Vector2(rect[0], rect[1]), new THREE.Vector2(rect[2], rect[1]), new THREE.Vector2(rect[2], rect[3]), new THREE.Vector2(rect[0], rect[3])]
  3550. // 根据四个顶点,填充中间点
  3551. let pointArr = []
  3552. for (let i = 0; i < cornerArr.length; i++) {
  3553. let corner1 = cornerArr[i]
  3554. let corner2 = cornerArr[(i + 1) % cornerArr.length]
  3555. pointArr.push(corner1)
  3556. /* // 横向角度超过150度时,3d中边框的弧线已经不太明显,准确画出全景图线框
  3557. if ((rect[2] - rect[0]) / w < 5 / 12 && i % 2 == 0) continue
  3558. const vec = [corner2[0] - corner1[0], corner2[1] - corner1[1]]
  3559. let length = Math.sqrt(vec[0] * vec[0] + vec[1] * vec[1])
  3560. let num = length / 150
  3561. for (let j = 1; j <= num; j++) {
  3562. pointArr.push([corner1[0] + (vec[0] / num) * j, corner1[1] + (vec[1] / num) * j])
  3563. } */
  3564. }
  3565. //pointArr.push(cornerArr[0], cornerArr[2], cornerArr[1], cornerArr[3]) //对角线
  3566. let points = []
  3567. pointArr.forEach(uv => {
  3568. let dir = getDirByUV(uv, player.currentPano)
  3569. // points.push(dir.sub(centerVec)) // 计算其他点相对于中点的坐标,方便旋转平移等
  3570. points.push(dir)
  3571. })
  3572. // 线框
  3573. const lineGeometry = new THREE.BufferGeometry().setFromPoints(points)
  3574. const lineMaterial = new THREE.LineBasicMaterial({ color: options.line.color, opacity: options.line.opacity, transparent: true, depthTest: false })
  3575. const wireframe = new THREE.LineLoop(lineGeometry, lineMaterial)
  3576. // wireframe.position.copy(centerVec) // 将中点作为线框坐标
  3577. wireframe.renderOrder = 100
  3578. // 填充颜色
  3579. const fillGeometry = lineGeometry.clone().setIndex(new THREE.BufferAttribute(new Uint16Array([0, 1, 3, 2, 3, 1]), 1))
  3580. const fillMaterial = new THREE.MeshBasicMaterial({ color: options.fill.color, opacity: options.fill.opacity, transparent: true, side: THREE.DoubleSide, depthTest: false })
  3581. const plane = new THREE.Mesh(fillGeometry, fillMaterial)
  3582. plane.renderOrder = wireframe.renderOrder - 1
  3583. wireframe.add(plane)
  3584. // 名称
  3585. const textMesh = new TextSprite({
  3586. text: name,
  3587. backgroundColor: { r: options.line.color.r * 255, g: options.line.color.g * 255, b: options.line.color.b * 255, a: options.line.opacity * 0.5 },
  3588. textColor: { r: 255, g: 255, b: 255, a: options.line.opacity * 1.1 },
  3589. borderRadius: 15,
  3590. renderOrder: wireframe.renderOrder + 1,
  3591. player: player,
  3592. })
  3593. //const shift = new THREE.Vector3(0, labelShift , 0)
  3594. textMesh.position.copy(centerDir /* .clone().add(shift).normalize() */)
  3595. textMesh.lookAt(0, 0, 0) // 看向相机
  3596. textMesh.scale.set(0.12, 0.12, 0.12)
  3597. /* let line = addLine(centerDir, shift, null, options.line.color)
  3598. line.material.opacity = options.line.opacity */
  3599. let group = new THREE.Group()
  3600. group.position.copy(player.currentPano.position)
  3601. group.add(wireframe)
  3602. group.add(textMesh)
  3603. //group.add(line)
  3604. this.wireframes.add(group)
  3605. if (removed) {
  3606. textMesh.sprite.material.opacity = 0.4
  3607. lineMaterial.opacity *= 0.6
  3608. lineMaterial.color.set('#efe')
  3609. }
  3610. }
  3611. clear() {
  3612. //清除上一次的结果
  3613. ;(skyBoxTight = null), (meshGroup = null), (modelBound = new THREE.Box3()), (groundY = null), (safeBound = null), (boundConfirmed = null), (hue = 0), (boxesSolid = [])
  3614. }
  3615. }