*/}}

la_modelling.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  1. /*
  2. * LaGUI: A graphical application framework.
  3. * Copyright (C) 2022-2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "../la_5.h"
  19. extern LA MAIN;
  20. extern struct _tnsMain *T;
  21. void la_ReadGLocation(tnsOffscreen* off, int x, int y,float* xyz0){
  22. glBindFramebuffer(GL_READ_FRAMEBUFFER, off->FboHandle);
  23. glReadBuffer(GL_COLOR_ATTACHMENT2); glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  24. glReadPixels(x,y,1,1, GL_RGBA, GL_FLOAT, xyz0);
  25. }
  26. int OPINV_SetCursor(laOperator *a, laEvent *e){
  27. if(!a->This || !a->This->EndInstance){ return 0; }
  28. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  29. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root || root->Type!=TNS_OBJECT_ROOT) return 0;
  30. float pos[4]; la_ReadGLocation(ex->OffScr, e->x-ui->L,ui->B-e->y,pos);
  31. if(pos[0]>-1e20){
  32. tnsVectorSet3v(root->GLocation,pos); laNotifyUsers("tns.world"); return LA_FINISHED_PASS;
  33. }else
  34. {
  35. if(!ex->ViewingCamera){ return LA_FINISHED_PASS; }
  36. real vv[4], gp[4]; tnsMatrix44d proj; tnsMatrix44d inv;
  37. real focus=ex->ViewingCamera->FocusDistance, _near=ex->ViewingCamera->ZMin, _far=ex->ViewingCamera->ZMax;
  38. tnsGetCameraViewProjection(proj,ui->R-ui->L,ui->B-ui->U,ex->ViewingCamera); tnsInverse44d(inv,proj);
  39. vv[0]=(real)(e->x-ui->L)/(real)(ui->R-ui->L)*2-1; vv[0]*=focus;
  40. vv[1]=(real)(ui->B-e->y)/(real)(ui->B-ui->U)*2-1; vv[1]*=focus;
  41. vv[2]=tnsGetRatiod(1/_near,1/_far,1/focus)*focus;
  42. vv[3]=focus;
  43. tnsApplyTransform44dTrue(gp, inv, vv);
  44. tnsVectorMultiSelf3d(gp,1/gp[3]);
  45. real vv0[3]={0,0,-1}, vv1[3], ray[3], p0[3]; real t; tnsMatrix44d inv2;
  46. tnsSelfTransformValueChanged(ex->ViewingCamera);tnsInverse44d(inv2,ex->ViewingCamera->Base.GlobalTransform);
  47. tnsApplyRotation43d(vv1,inv2,vv0);
  48. tnsVectorMulti3d(vv0,vv1,focus);
  49. tnsVectorAccum3d(vv0,ex->ViewingCamera->Base.GLocation);
  50. tnsVectorMinus3d(ray, gp, ex->ViewingCamera->Base.GLocation);
  51. tnsNormalizeSelf3d(ray);
  52. if(tnsIntersectPlaneRay(vv1,vv0,ex->ViewingCamera->Base.GLocation,ray,&t)){
  53. tnsVectorMultiSelf3d(ray,t); tnsVectorPlus3d(gp,ray,ex->ViewingCamera->Base.GLocation);
  54. }
  55. tnsVectorSet3v(root->GLocation,gp); laNotifyUsers("tns.world"); return LA_FINISHED_PASS;
  56. }
  57. return LA_FINISHED_PASS;
  58. }
  59. int OPCHK_ThereIsActiveObject(laPropPack *This, laStringSplitor *ss){
  60. if(!This || !This->EndInstance){ return 0; }
  61. laCanvasExtra* ex=This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  62. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  63. tnsMeshObject* mo=root->Active; if(!mo) return 0; return 1;
  64. }
  65. int OPINV_ToggleEdit(laOperator *a, laEvent *e){
  66. if(!a->This || !a->This->EndInstance){ return 0; }
  67. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  68. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  69. tnsMeshObject* mo=root->Active; if(!mo) return 0;
  70. if(mo->Base.Type!=TNS_OBJECT_MESH) return LA_CANCELED;
  71. if(mo->Mode==TNS_MESH_EDIT_MODE) tnsMeshLeaveEditMode(mo); else tnsMeshEnterEditMode(mo);
  72. laRecordInstanceDifferences(mo, "tns_mesh_object"); laPushDifferences("Toggle edit mode", TNS_HINT_GEOMETRY);
  73. laNotifyUsers("tns.world");
  74. return LA_FINISHED_PASS;
  75. }
  76. STRUCTURE(MSelectData){
  77. tnsOffscreen* FBO;
  78. tnsTexture* Color;
  79. void** RefsV; int nextV,maxV;
  80. void** RefsE; int nextE,maxE;
  81. };
  82. MSelectData* la_InitSelectData(int w, int h, tnsCamera* camera){
  83. MSelectData* sd=memAcquireSimple(sizeof(MSelectData));
  84. if (!sd->FBO || sd->FBO->pColor[0]->Height != h || sd->FBO->pColor[0]->Width != w){
  85. if (sd->FBO) tnsDelete2DOffscreen(sd->FBO);
  86. sd->FBO = tnsCreate2DOffscreen(GL_RGB, w, h, 0, 1, 0); sd->Color=sd->FBO->pColor[0];
  87. }
  88. tnsDrawToOffscreen(sd->FBO, 1, 0);
  89. return sd;
  90. }
  91. void la_AssignObjectSelectIDRecursive(tnsObject* root, MSelectData* sd){
  92. for(laListItemPointer*lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  93. tnsObject* o=lip->p; if(!o) continue;
  94. arrEnsureLength(&sd->RefsV, sd->nextV, &sd->maxV, sizeof(tnsObject*));
  95. sd->RefsV[sd->nextV]=o; o->SelectID=sd->nextV; sd->nextV++;
  96. if(o->ChildObjects.pFirst){ la_AssignObjectSelectIDRecursive(o,sd); }
  97. }
  98. }
  99. void la_PopulateSelectDataObjects(MSelectData* sd, tnsObject* root, tnsCamera* camera){
  100. arrEnsureLength(&sd->RefsV,0,&sd->maxV,sizeof(tnsObject*));
  101. sd->nextV++; // starting from 1;
  102. la_AssignObjectSelectIDRecursive(root, sd);
  103. if(sd->nextV==1) return; int w=sd->Color->Width, h=sd->Color->Height;
  104. tnsUnbindTexture(); tnsUniformUseTexture(T->immShader,0,0); tnsUseMultiplyColor(0);
  105. tnsEnableShaderv(T->SelectionShader);
  106. glDisableVertexAttribArray(T->SelectionShader->iColor); glVertexAttrib4f(T->SelectionShader->iColor,0,0,0,0);
  107. tnsViewportWithScissor(0,0,w,h);tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  108. tnsApplyCameraView(w,h,camera);
  109. glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  110. glEnable(GL_DEPTH_TEST);
  111. tnsInvalidateEvaluation(root); tnsSetObjectTreeEvaluationArgs(root,root->Active,1,1); tnsEvaluateObjectTree(root,0);
  112. tnsDrawObjectTree(root,TNS_EVAL_LAYER_SELECTION,0);
  113. glDisable(GL_DEPTH_TEST);
  114. tnsEnableShaderv(T->immShader);
  115. }
  116. void la_PopulateSelectVerts(MSelectData* sd, tnsMeshObject* mo){
  117. arrEnsureLength(&sd->RefsV,0,&sd->maxV,sizeof(tnsMVert*));
  118. if(mo->Base.Type!=TNS_OBJECT_MESH||!mo->mv.pFirst){ return; }
  119. for(tnsMVert* v=mo->mv.pFirst;v;v=v->Item.pNext){
  120. arrEnsureLength(&sd->RefsV, v->i, &sd->maxV, sizeof(tnsObject*));
  121. sd->RefsV[v->i]=v; sd->nextV=TNS_MAX2(v->i, sd->nextV);
  122. }
  123. sd->nextV++;
  124. }
  125. void la_PopulateSelectEdges(MSelectData* sd, tnsMeshObject* mo){
  126. arrEnsureLength(&sd->RefsE,0,&sd->maxE,sizeof(tnsMEdge*));
  127. if(mo->Base.Type!=TNS_OBJECT_MESH||!mo->mv.pFirst){ return; }
  128. for(tnsMEdge* e=mo->me.pFirst;e;e=e->Item.pNext){
  129. arrEnsureLength(&sd->RefsE, e->i, &sd->maxE, sizeof(tnsObject*));
  130. sd->RefsE[e->i]=e; sd->nextE=TNS_MAX2(e->i, sd->nextE);
  131. }
  132. sd->nextE++;
  133. }
  134. void la_PopulateSelectDataPrimitives(MSelectData* sd, tnsMeshObject* mo, tnsCamera* camera, int WhatPrim, int SelectThrough){
  135. int DoVerts=(WhatPrim==LA_CANVAS_SELECT_MODE_VERTS),DoEdges=(WhatPrim==LA_CANVAS_SELECT_MODE_EDGES);
  136. int Knife=(WhatPrim==LA_CANVAS_SELECT_MODE_KNIFE);
  137. if(DoVerts || Knife){ la_PopulateSelectVerts(sd,mo); }
  138. if(DoEdges || Knife){ la_PopulateSelectEdges(sd,mo); }
  139. int w=sd->Color->Width, h=sd->Color->Height;
  140. tnsUnbindTexture(); tnsUniformUseTexture(T->immShader,0,0); tnsUseMultiplyColor(0);
  141. tnsEnableShaderv(T->SelectionShader);
  142. tnsViewportWithScissor(0,0,w,h);tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  143. tnsApplyCameraView(w,h,camera);
  144. glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  145. tnsPushMatrix(); tnsApplyModelMatrix(mo->Base.GlobalTransform);glEnable(GL_DEPTH_TEST);
  146. if(!SelectThrough){
  147. glDepthMask(GL_TRUE); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  148. tnsUniformUseOffset(T->SelectionShader,0);
  149. tnsDrawBatch(mo->Batch,"body",0,0);
  150. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_FALSE);
  151. }
  152. tnsUniformUseOffset(T->SelectionShader,1);
  153. if(DoEdges || Knife){ tnsDrawBatch(mo->Batch, "edges_select",0,0); }
  154. if(DoVerts || Knife){ tnsDrawBatch(mo->Batch, "verts_select",0,0); }
  155. tnsUniformUseOffset(T->SelectionShader,0);
  156. tnsPopMatrix();
  157. glDisable(GL_DEPTH_TEST);
  158. }
  159. void la_PadSelectionBuffer(uint8_t* buf, int w, int h, int sx, int sy, int ex, int ey, int real_endx){
  160. if(!sx&&!sy&&!ex&&!ey) return;
  161. uint8_t* pad=calloc(1,sizeof(uint8_t)*4*w*h);
  162. for(int i=0;i<h;i++){
  163. if(i<sy || i>=h-ey){ memset(&pad[i*w*4],0,w*4); continue; }
  164. for(int j=0;j<w;j++){
  165. if(j<sx || j>=w-sx){ memset(&pad[(i*w+j)*4],0,4); continue; }
  166. memcpy(&pad[(i*w+j)*4], &buf[((i-sy)*(real_endx-sx)+j-sx)*4], 4);
  167. }
  168. }
  169. memcpy(buf,pad,sizeof(uint8_t)*4*w*h);
  170. free(pad);
  171. }
  172. uint8_t* la_ReadSelection(MSelectData* sd, u8bit* buf, int x, int y, int w, int h){
  173. glFlush(); glFinish(); glGetError();
  174. glBindFramebuffer(GL_READ_FRAMEBUFFER, sd->FBO->FboHandle);
  175. glReadBuffer(GL_COLOR_ATTACHMENT0);
  176. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  177. glReadPixels(x,y,w,h, GL_RGBA, GL_UNSIGNED_BYTE, buf);
  178. return buf;
  179. }
  180. uint8_t* la_ReadSelectionRadius(MSelectData* sd, int uix, int uiy, int radius){
  181. int w=radius*2; int vh=sd->Color->Height,vw=sd->Color->Width;
  182. uint8_t* buf=calloc(1,sizeof(uint8_t)*4*w*w);
  183. int startx=uix-radius, starty=vh-uiy-radius, endx=uix+radius, endy=vh-uiy+radius;
  184. TNS_CLAMP(startx,0,vw);TNS_CLAMP(endx,0,vw);TNS_CLAMP(starty,0,vh);TNS_CLAMP(endy,0,vh);
  185. int _startx=TNS_MAX2(startx,0), _starty=TNS_MAX2(starty,0), _endx=TNS_MIN2(endx,vw), _endy=TNS_MIN2(endy,vh);
  186. la_ReadSelection(sd, buf, _startx, _starty, _endx-_startx, _endy-_starty);
  187. la_PadSelectionBuffer(buf, w, w, _startx-startx, _starty-starty, endx-_endx, endy-_endy,_endx);
  188. return buf;
  189. }
  190. uint8_t* la_ReadSelectionBox(MSelectData* sd, int uix, int uiy, int uix2, int uiy2){
  191. int vh=sd->Color->Height,vw=sd->Color->Width;
  192. if(uix2<uix) LA_SWAP(int,uix,uix2); if(uiy2<uiy) LA_SWAP(int,uiy,uiy2);
  193. TNS_CLAMP(uix,0,vw);TNS_CLAMP(uix2,0,vw);TNS_CLAMP(uiy,0,vh);TNS_CLAMP(uiy2,0,vh);
  194. int w=uix2-uix, h=uiy2-uiy;
  195. uint8_t* buf=calloc(1,sizeof(uint8_t)*4*w*h);
  196. int startx=uix, starty=vh-uiy2, endx=uix+w, endy=starty+h;
  197. int _startx=TNS_MAX2(startx,0), _starty=TNS_MAX2(starty,0), _endx=TNS_MIN2(endx,vw), _endy=TNS_MIN2(endy,vh);
  198. la_ReadSelection(sd, buf, _startx, _starty, _endx-_startx, _endy-_starty);
  199. la_PadSelectionBuffer(buf, w, h, _startx-startx, _starty-starty, endx-_endx, endy-_endy,_endx);
  200. return buf;
  201. }
  202. int la_SelectGetClosest(MSelectData* sd, int uix, int uiy, int radius, int *ElemType){
  203. *ElemType=0; uint8_t* buf=la_ReadSelectionRadius(sd, uix, uiy, radius); if(!buf) return 0;
  204. int w=radius*2; int MinD=INT_MAX,MinDe=INT_MAX; int MinID=0,MinIDe=0, d, elemtype=0;
  205. for(int i=0;i<w;i++){
  206. for(int j=0;j<w;j++){
  207. uint8_t* p=&buf[(i*w+j)*4]; int id=(p[0])|(p[1]<<8)|((p[2]<<16)&(~TNS_MMESH_TYPE_BIT)); if(!id){continue;}
  208. d=tnsDistIdv2(i,j, radius, radius); elemtype=((p[2]<<16)&TNS_MMESH_TYPE_BIT);
  209. if(elemtype==TNS_MMESH_EDGE_BIT){ if(d<MinDe){ MinDe=d; MinIDe=id; } }
  210. elif(d<MinD){ MinD=d; MinID=id; }
  211. //printf("%d ",buf[(i*w+j)*4]);
  212. }
  213. //printf("\n");
  214. }
  215. printf("%d %d\n",MinID, MinIDe);
  216. free(buf);
  217. if(MinID && MinIDe){ if(MinD<MinDe*5){ *ElemType=0; return MinID; } }
  218. if(MinIDe){ *ElemType=TNS_MMESH_EDGE_BIT; return MinIDe; }
  219. if(MinID){ *ElemType=0; return MinID; }
  220. *ElemType=0; return 0;
  221. }
  222. int* la_SelectGetBox(MSelectData* sd, int uix, int uiy, int uix2, int uiy2, int* r_length){
  223. uint8_t* buf=la_ReadSelectionBox(sd, uix, uiy, uix2, uiy2); if(!buf) return 0;
  224. int vh=sd->Color->Height,vw=sd->Color->Width;
  225. if(uix2<uix) LA_SWAP(int,uix,uix2); if(uiy2<uiy) LA_SWAP(int,uiy,uiy2);
  226. TNS_CLAMP(uix,0,vw);TNS_CLAMP(uix2,0,vw);TNS_CLAMP(uiy,0,vh);TNS_CLAMP(uiy2,0,vh);
  227. int w=uix2-uix, h=uiy2-uiy;
  228. int* ids=0; int next=0,max=0;
  229. arrEnsureLength(&ids, next, &max, sizeof(int));
  230. for(int i=0;i<h;i++){
  231. for(int j=0;j<w;j++){
  232. uint8_t* p=&buf[(i*w+j)*4]; int id=(p[0])|(p[1]<<8)|((p[2]<<16)&(~TNS_MMESH_TYPE_BIT));
  233. if(id){ int found=0;
  234. for(int a=0;a<next;a++){ if(ids[a]==id){ found=1; break; } }
  235. if(!found){
  236. arrEnsureLength(&ids, next, &max, sizeof(int)); ids[next]=id; next++; }
  237. }
  238. }
  239. }
  240. free(buf);
  241. *r_length=next;
  242. return ids;
  243. }
  244. void la_FreeSelectData(MSelectData* sd){
  245. tnsDelete2DOffscreen(sd->FBO);
  246. free(sd->RefsV);
  247. free(sd->RefsE);
  248. memFree(sd);
  249. }
  250. void* la_SelectGetRef(MSelectData* sd, int id, int elemtype){
  251. if(!elemtype){ if(id>=0 && id<sd->nextV){ return sd->RefsV[id]; } }
  252. elif(elemtype==TNS_MMESH_EDGE_BIT){ if(id>=0 && id<sd->nextE){ return sd->RefsE[id]; } }
  253. return 0;
  254. }
  255. int OPCHK_ViewportAndSceneExists(laPropPack *This, laStringSplitor *ss){
  256. if(!This || !This->EndInstance){ return 0; } laCanvasExtra* ex=This->EndInstance;
  257. laUiItem* ui=ex->ParentUi; tnsObject* root=ui?ui->PP.EndInstance:0;
  258. if(!ex->ViewingCamera || !root){ return 0; }
  259. return 1;
  260. }
  261. void la_DoObjectSelect(tnsObject* root, tnsObject* o, laCanvasExtra* e, int DeselectAll, int Select, int Toggle){
  262. if(DeselectAll){ tnsDeselectAllObjects(root); memAssignRef(root,&root->Active,0); }
  263. if(o){ tnsSelectObject(o, Select, o==root->Active?0:Toggle); memAssignRef(root,&root->Active,o); }
  264. }
  265. void la_DoMeshSelect(tnsMeshObject* mo, void* p, int WhatPrim, int DeselectAll, int Select, int Toggle){
  266. if(DeselectAll){ tnsMMeshDeselectAll(mo); }
  267. if(p){ if(WhatPrim==LA_CANVAS_SELECT_MODE_VERTS) tnsMMeshSelectVert(mo,p,Select,Toggle);
  268. elif(WhatPrim==LA_CANVAS_SELECT_MODE_EDGES) tnsMMeshSelectEdge(mo,p,Select,Toggle); }
  269. }
  270. #define LA_SELECT_MODE_BOX 1
  271. STRUCTURE(MSelectExtra){
  272. MSelectData* sd;
  273. tnsObject* root;
  274. tnsObject* mo;
  275. tnsCamera* cam;
  276. int Mode;
  277. int InSelect;
  278. laListHandle KnifeElements;
  279. void* PendingElem; int PendingElemType, BatchDirty;
  280. int IsLoop;
  281. };
  282. int OPINV_Select(laOperator *a, laEvent *e){
  283. if(!a->This || !a->This->EndInstance){ return 0; }
  284. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  285. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  286. tnsMeshObject* mo=root->Active;
  287. int is_geo=0, SelectMode=ex->SelectMode, ring_band=0;
  288. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "toggle")){
  289. if(mo && mo->Base.Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){
  290. if(tnsMMeshAnySelected(mo)) tnsMMeshDeselectAll(mo); else tnsMMeshSelectAll(mo);
  291. tnsInvalidateMeshBatch(mo); is_geo=1;
  292. }else{
  293. if(tnsAnyObjectsSelected(root)) tnsDeselectAllObjects(root); else tnsSelectAllObjects(root);
  294. }
  295. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world", "Toggle selection",is_geo?TNS_HINT_GEOMETRY:TNS_HINT_TRANSFORM);
  296. return LA_FINISHED;
  297. }
  298. int spk=e->SpecialKeyBit & (LA_KEY_CTRL|LA_KEY_ALT);
  299. if(spk==LA_KEY_ALT){ ring_band=1; SelectMode=LA_CANVAS_SELECT_MODE_EDGES; }
  300. elif(spk==(LA_KEY_CTRL|LA_KEY_ALT)){ ring_band=2; SelectMode=LA_CANVAS_SELECT_MODE_EDGES; }
  301. MSelectData* sd=la_InitSelectData(ex->OffScr->pColor[0]->Width, ex->OffScr->pColor[0]->Height, c);
  302. int DeselectAll=1;
  303. int Append=e->SpecialKeyBit&LA_KEY_SHIFT; if(Append) DeselectAll=0;
  304. if(mo && mo->Base.Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){
  305. la_PopulateSelectDataPrimitives(sd, mo, ex->ViewingCamera, SelectMode, ex->SelectThrough);
  306. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "box")){
  307. MSelectExtra* se=memAcquire(sizeof(MSelectExtra));
  308. ex->OnX=e->x; ex->OnX=e->y;
  309. a->CustomData=se; se->sd=sd; se->Mode=LA_SELECT_MODE_BOX; se->mo=mo; se->cam=c; ex->DrawCursor=1; se->root=root; return LA_RUNNING;
  310. }
  311. int elemtype,id=la_SelectGetClosest(sd, e->x-ui->L, e->y-ui->U, LA_RH,&elemtype)-1;
  312. void* p=la_SelectGetRef(sd,id,elemtype);
  313. la_DoMeshSelect(mo, p, SelectMode, DeselectAll, 1, 1); tnsMMeshEnsureSelection(mo,ex->SelectMode);
  314. if(ring_band && p){
  315. tnsMMeshSelectRingBandFrom(mo,p,ring_band,1,Append); tnsMMeshEnsureSelection(mo,ex->SelectMode);
  316. }
  317. tnsInvalidateMeshBatch(mo);
  318. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world","Mesh selection",TNS_HINT_GEOMETRY);
  319. }else{
  320. la_PopulateSelectDataObjects(sd,root,ex->ViewingCamera);
  321. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "box")){
  322. MSelectExtra* se=memAcquire(sizeof(MSelectExtra));
  323. ex->OnX=e->x; ex->OnX=e->y;
  324. a->CustomData=se; se->sd=sd; se->Mode=LA_SELECT_MODE_BOX; se->cam=c; ex->DrawCursor=1; se->root=root; return LA_RUNNING;
  325. }
  326. int elemtype,id=la_SelectGetClosest(sd, e->x-ui->L, e->y-ui->U, LA_RH*2,&elemtype);
  327. void* p=la_SelectGetRef(sd,id,elemtype); if(p){ la_DoObjectSelect(root, p, ex, DeselectAll, 1, 1); }
  328. else{ la_DoObjectSelect(root, 0, ex, DeselectAll, 1, 1); }
  329. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world","Object selection",TNS_HINT_TRANSFORM);
  330. }
  331. la_FreeSelectData(sd);
  332. return LA_FINISHED_PASS;
  333. }
  334. int OPMOD_Select(laOperator *a, laEvent *e){
  335. if(!a->This || !a->This->EndInstance){ return 0; }
  336. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  337. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  338. tnsMeshObject* mo=root->Active;
  339. MSelectExtra* se=a->CustomData;
  340. if(e->Type==LA_L_MOUSE_DOWN){ se->InSelect=1; ex->DrawCursor=2; ex->ClickedX=e->x; ex->ClickedY=e->y; laRedrawCurrentPanel(); }
  341. if(e->Type&LA_MOUSE_EVENT){ ex->OnX=e->x; ex->OnY=e->y; laRedrawCurrentPanel(); }
  342. if(e->Type==LA_R_MOUSE_DOWN || (e->Type == LA_KEY_DOWN && e->key==LA_KEY_ESCAPE)){
  343. ex->DrawCursor=0; la_FreeSelectData(se->sd); memFree(se); laNotifyUsers("tns.world"); return LA_FINISHED;
  344. }
  345. int DeselectAll=1;
  346. int Append=e->SpecialKeyBit&LA_KEY_SHIFT; if(Append) DeselectAll=0;
  347. int Remove=e->SpecialKeyBit&LA_KEY_ALT; if(Remove) DeselectAll=0;
  348. int is_geo=0;
  349. if(se->InSelect && e->Type==LA_L_MOUSE_UP){
  350. if(mo && mo->Base.Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){
  351. la_DoMeshSelect(mo, 0, ex->SelectMode, DeselectAll, 0, 0);
  352. int len; int* ids=la_SelectGetBox(se->sd, ex->ClickedX-ui->L, ex->ClickedY-ui->U, e->x-ui->L, e->y-ui->U, &len);
  353. for(int i=0;i<len;i++){
  354. int id=ids[i]-1; void* p=la_SelectGetRef(se->sd,id,ex->SelectMode==LA_CANVAS_SELECT_MODE_EDGES?TNS_MMESH_EDGE_BIT:0);
  355. la_DoMeshSelect(mo, p, ex->SelectMode, 0, !Remove, 0);
  356. }
  357. tnsMMeshEnsureSelection(mo,ex->SelectMode);
  358. tnsInvalidateMeshBatch(mo); is_geo=1;
  359. }else{
  360. la_DoObjectSelect(se->root, 0, ex, DeselectAll, 0, 0);
  361. int len; int* ids=la_SelectGetBox(se->sd, ex->ClickedX, ex->ClickedY, e->x-ui->L, e->y-ui->U, &len);
  362. for(int i=0;i<len;i++){
  363. int id=ids[i]; void* p=la_SelectGetRef(se->sd,id,0);
  364. if(p){ la_DoObjectSelect(se->root, p, ex, 0, !Remove, 0); }
  365. }
  366. }
  367. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world","Box selection",is_geo?TNS_HINT_GEOMETRY:TNS_HINT_TRANSFORM);
  368. ex->DrawCursor=0;
  369. la_FreeSelectData(se->sd); memFree(se);
  370. laRedrawCurrentPanel();
  371. return LA_FINISHED;
  372. }
  373. return LA_RUNNING;
  374. }
  375. #define LA_TRANSFORM_MODE_GRAB 1
  376. #define LA_TRANSFORM_MODE_ROTATE 2
  377. #define LA_TRANSFORM_MODE_SCALE 3
  378. #define LA_TRANSFORM_RESTORE_LOC 1
  379. #define LA_TRANSFORM_RESTORE_ROT 2
  380. #define LA_TRANSFORM_RESTORE_SCA 3
  381. STRUCTURE(MTOrigObject){
  382. tnsObject* o;
  383. tnsMatrix44d Global;
  384. tnsMatrix44d Local;
  385. int Discard;
  386. };
  387. STRUCTURE(MTOrigMVert){
  388. tnsVector3d p;
  389. tnsVector3d origp;
  390. tnsMVert* mv;
  391. };
  392. STRUCTURE(MTransformData){
  393. tnsMatrix44d Delta;
  394. tnsMatrix44d ViewProjection;
  395. tnsVector4d Up,Right,Foward;
  396. tnsVector4d TCenter;
  397. int CenterX,CenterY; real Initial;
  398. tnsObject* mo; tnsMatrix44d obmatinv,deltainv;
  399. tnsCamera* c; tnsObject* root;
  400. int w,h;
  401. void* Originals; int next,max;
  402. int mode;
  403. int LockAxis[3];
  404. int UseLocal;
  405. real DeltaVal, UserDeltaVal;
  406. laStringEdit* Entry; int UseUserDelta;
  407. int CanvasDeltaMode;
  408. };
  409. MTransformData* la_InitTransformData(int w, int h, tnsCamera* c, int CanvasDeltaMode){
  410. MTransformData* td=memAcquireSimple(sizeof(MTransformData));
  411. tnsVector4d pu={0,1,0,0}, pr={1,0,0,0}, pf={0,0,1};
  412. tnsGetCameraMovingDeltas(c,w,h,1,0,pr); pr[2]=0; pr[3]=0;
  413. tnsGetCameraMovingDeltas(c,w,h,0,-1,pu); pu[2]=0; pu[3]=0;
  414. tnsMatrix44d inv; tnsInverse44d(inv,c->Base.GlobalTransform);
  415. tnsApplyRotation43d(td->Up,inv,pu);
  416. tnsApplyRotation43d(td->Right,inv,pr);
  417. tnsApplyRotation43d(td->Foward,inv,pf);
  418. tnsGetCameraViewProjection(td->ViewProjection, w,h,c);
  419. td->c=c; td->w=w; td->h=h; td->CanvasDeltaMode=CanvasDeltaMode;
  420. strBeginEdit(&td->Entry, "");
  421. return td;
  422. }
  423. void la_GetTransformInitialScale(MTransformData* td, laUiItem* ui, int x, int y){ td->Initial=tnsDistIdv2(x-ui->L,y-ui->U,td->CenterX,td->CenterY); }
  424. void la_GetTransformInitialRotation(MTransformData* td, laUiItem* ui, int x, int y){ td->Initial=atan2(y-ui->U-td->CenterY,x-ui->L-td->CenterX); }
  425. void la_GetTransformCenter2D(MTransformData* td){
  426. tnsVector4d vp; tnsApplyTransform44d(vp,td->ViewProjection,td->TCenter);
  427. if(td->c->CameraType==TNS_CAMERA_PERSP){ tnsVectorMultiSelf3d(vp, 1/vp[3]); }
  428. td->CenterX = (vp[0]/2+0.5f)*td->w; td->CenterY=(-vp[1]/2+0.5f)*td->h;
  429. if(td->CenterX<0||td->CenterY<0) { td->CenterX=td->w/2; td->CenterY=td->h/2; }
  430. }
  431. int la_AddTransformObjectsRecursive(MTransformData* td, tnsObject*root){
  432. int any=0; for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  433. tnsObject* o=lip->p; if(o->ChildObjects.pFirst) any+=la_AddTransformObjectsRecursive(td,o);
  434. if(!(o->Flags&TNS_OBJECT_FLAGS_SELECTED)) continue;
  435. arrEnsureLength(&td->Originals, td->next, &td->max, sizeof(MTOrigObject));
  436. MTOrigObject* to=arrElement(td->Originals, td->next, sizeof(MTOrigObject)); td->next++; to->o=o;
  437. memcpy(to->Global, o->GlobalTransform, sizeof(tnsMatrix44d));
  438. memcpy(to->Local, o->SelfTransform, sizeof(tnsMatrix44d));
  439. tnsVectorAccum3d(td->TCenter,o->GLocation); any++;
  440. int discard=0; tnsObject* po=o; while(po=po->ParentObject){ if(po->Flags&TNS_OBJECT_FLAGS_SELECTED){ discard=1; break;} } if(discard) to->Discard=1;
  441. }
  442. return any;
  443. }
  444. int la_PopulateTransformObjects(MTransformData* td, tnsObject* root){
  445. arrEnsureLength(&td->Originals, 0, &td->max, sizeof(MTOrigObject));
  446. int any=la_AddTransformObjectsRecursive(td,root);
  447. tnsVectorMultiSelf3d(td->TCenter, 1.0f/any);
  448. la_GetTransformCenter2D(td);
  449. return any;
  450. }
  451. int la_PopulateTransformVerticies(MTransformData* td, tnsMeshObject* mo){
  452. int any=0; td->mo=mo;
  453. arrEnsureLength(&td->Originals, 0, &td->max, sizeof(MTOrigMVert));
  454. tnsInverse44d(td->obmatinv, mo->Base.GlobalTransform);
  455. tnsInverse44d(td->deltainv, mo->Base.DeltaTransform);
  456. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){
  457. if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  458. //printf("v %d ",mv->i);
  459. arrEnsureLength(&td->Originals, td->next, &td->max, sizeof(MTOrigMVert));
  460. MTOrigMVert* to=arrElement(td->Originals, td->next, sizeof(MTOrigMVert)); td->next++; to->mv=mv;
  461. tnsApplyTransform43d(to->p, mo->Base.GlobalTransform, mv->p);
  462. memcpy(to->origp, mv->p, sizeof(tnsVector3d)); any++;
  463. tnsVectorAccum3d(td->TCenter,to->p);
  464. }
  465. //printf(" [totmv %d]\n",mo->totmv);
  466. tnsVectorMultiSelf3d(td->TCenter, 1.0f/any);
  467. la_GetTransformCenter2D(td);
  468. return any;
  469. }
  470. void la_ApplyTranslation(MTransformData* td, int x, int y){
  471. tnsMatrix44d trans; tnsVector3d deltay,delta; tnsVector3d gp;
  472. tnsVectorMulti3d(delta, td->Right, x); tnsVectorMulti3d(deltay, td->Up, y); tnsVectorAccum3d(delta, deltay);
  473. tnsVector3d use_delta={LA_COLOR3(delta)}; real len;
  474. if(td->LockAxis[0]||td->LockAxis[1]||td->LockAxis[2]){ len=tnsLength3d(delta); }
  475. if(td->LockAxis[0]>0){ use_delta[1]=use_delta[2]=0; real l=fabs(use_delta[0]); use_delta[0]=l?use_delta[0]*len/l:1e-7; }
  476. if(td->LockAxis[1]>0){ use_delta[0]=use_delta[2]=0; real l=fabs(use_delta[1]); use_delta[1]=l?use_delta[1]*len/l:1e-7; }
  477. if(td->LockAxis[2]>0){ use_delta[0]=use_delta[1]=0; real l=fabs(use_delta[2]); use_delta[2]=l?use_delta[2]*len/l:1e-7; }
  478. if(td->LockAxis[0]<0){ use_delta[0]=0; real l=tnsLength3d(use_delta); tnsVectorMultiSelf3d(use_delta, l?len/l*len/l:0); }
  479. if(td->LockAxis[1]<0){ use_delta[1]=0; real l=tnsLength3d(use_delta); tnsVectorMultiSelf3d(use_delta, l?len/l*len/l:0); }
  480. if(td->LockAxis[2]<0){ use_delta[2]=0; real l=tnsLength3d(use_delta); tnsVectorMultiSelf3d(use_delta, l?len/l*len/l:0); }
  481. td->DeltaVal=tnsLength3d(use_delta);
  482. if(td->UseUserDelta){
  483. tnsVectorMultiSelf3d(use_delta,1/tnsLength3d(use_delta)*td->UserDeltaVal);
  484. tnsVector3d lock={ td->LockAxis[0], td->LockAxis[1], td->LockAxis[2] };
  485. real dir=tnsDot3d(use_delta, lock, 0); tnsVectorMultiSelf3d(use_delta,(td->UserDeltaVal*dir<=0)?-1:1);
  486. td->DeltaVal=td->UserDeltaVal;
  487. }
  488. if(!td->mo){
  489. for(int i=0;i<td->next;i++){
  490. MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject)); memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  491. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  492. if(td->CanvasDeltaMode) tnsGlobalMatrixChangedForDelta(to->o, 0); else tnsGlobalMatrixChanged(to->o, 0);
  493. if(td->UseLocal){
  494. if(td->CanvasDeltaMode) tnsMoveObjectDelta(to->o, LA_COLOR3(use_delta));
  495. else tnsMoveObjectLocal(to->o, LA_COLOR3(use_delta));
  496. }else{
  497. if(td->CanvasDeltaMode) tnsMoveObjectGlobalForDelta(to->o, LA_COLOR3(use_delta));
  498. else tnsMoveObjectGlobal(to->o, LA_COLOR3(use_delta));
  499. }
  500. }
  501. }else{
  502. tnsMakeTranslationMatrix44d(trans, LA_COLOR3(use_delta)); tnsMatrix44d final;
  503. if(!td->UseLocal) tnsMultiply44d(final,td->obmatinv,trans);
  504. else tnsMultiply44d(final,trans,td->obmatinv);
  505. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert));
  506. tnsApplyTransform43d(to->mv->p, final, to->p);
  507. }
  508. tnsInvalidateMeshBatch(td->mo); tnsMMeshCalculateNormal(td->mo);
  509. }
  510. }
  511. void la_ApplyScale(MTransformData* td, int uix, int uiy){
  512. tnsMatrix44d trans; real d=tnsDistIdv2(uix,uiy,td->CenterX,td->CenterY); if(!td->Initial){ td->Initial=100; }
  513. real s=d/td->Initial; tnsVector3d gp;
  514. td->DeltaVal=s; if(td->UseUserDelta) td->DeltaVal=s=td->UserDeltaVal;
  515. if(!td->mo){
  516. for(int i=0;i<td->next;i++){
  517. MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject)); memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  518. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  519. if(td->CanvasDeltaMode) tnsGlobalMatrixChangedForDelta(to->o, 0); else tnsGlobalMatrixChanged(to->o, 0);
  520. if(td->CanvasDeltaMode) tnsScaleObjectDelta(to->o,s,LA_COLOR3(td->TCenter));
  521. else tnsScaleObject(to->o, s, LA_COLOR3(td->TCenter));
  522. }
  523. }else{
  524. tnsVector3d use_delta={s,s,s};
  525. if(td->LockAxis[0]>0){ use_delta[1]=use_delta[2]=1;}
  526. if(td->LockAxis[1]>0){ use_delta[0]=use_delta[2]=1;}
  527. if(td->LockAxis[2]>0){ use_delta[0]=use_delta[1]=1;}
  528. if(td->LockAxis[0]<0){ use_delta[0]=1; }
  529. if(td->LockAxis[1]<0){ use_delta[1]=1; }
  530. if(td->LockAxis[2]<0){ use_delta[2]=1; }
  531. tnsMakeScaleMatrix44d(trans,LA_COLOR3(use_delta));
  532. tnsMatrix44d t1,t2,t3; tnsMakeTranslationMatrix44d(t1,LA_COLOR3(td->TCenter)); tnsInverse44d(t2,t1);
  533. tnsMatrix44d final; tnsMultiply44d(t3,t1,trans); tnsMultiply44d(t1,t3,t2);
  534. if(!td->UseLocal) tnsMultiply44d(final,td->obmatinv,t1);
  535. else tnsMultiply44d(final,t1,td->obmatinv);
  536. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert));
  537. tnsApplyTransform43d(to->mv->p, final, to->p);
  538. }
  539. tnsInvalidateMeshBatch(td->mo); tnsMMeshCalculateNormal(td->mo);
  540. }
  541. }
  542. void la_ApplyRotation(MTransformData* td, int uix, int uiy){
  543. tnsMatrix44d trans; real a=atan2(uiy-td->CenterY,uix-td->CenterX);
  544. real angle=a-td->Initial; tnsVector3d gp; tnsVector3d LimFoward={0}; real* use_forward=td->Foward;
  545. if(td->LockAxis[0]||td->LockAxis[1]||td->LockAxis[2]){ use_forward=LimFoward; }
  546. if(td->LockAxis[0]){ LimFoward[0]=1; }
  547. if(td->LockAxis[1]){ LimFoward[1]=1; }
  548. if(td->LockAxis[2]){ LimFoward[2]=1; }
  549. if(td->UseUserDelta) angle=rad(td->UserDeltaVal); td->DeltaVal=deg(angle);
  550. if(!td->mo){
  551. for(int i=0;i<td->next;i++){
  552. MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject)); memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  553. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  554. if(td->CanvasDeltaMode) tnsGlobalMatrixChangedForDelta(to->o, 0); else tnsGlobalMatrixChanged(to->o, 0);
  555. if(td->UseLocal){
  556. if(td->CanvasDeltaMode) tnsRotateObjectDelta(to->o,LA_COLOR3(use_forward),angle,LA_COLOR3(td->TCenter));
  557. else tnsRotateObjectLocal(to->o,LA_COLOR3(use_forward),angle,LA_COLOR3(td->TCenter));
  558. }else{
  559. if(td->CanvasDeltaMode) tnsRotateObjectGlobalForDelta(to->o,LA_COLOR3(use_forward),angle,LA_COLOR3(td->TCenter));
  560. else tnsRotateObjectGlobal(to->o,LA_COLOR3(use_forward),angle,LA_COLOR3(td->TCenter));
  561. }
  562. }
  563. }else{
  564. tnsMakeRotationMatrix44d(trans, angle, LA_COLOR3(use_forward));
  565. tnsMatrix44d t1,t2,t3; tnsMakeTranslationMatrix44d(t1,LA_COLOR3(td->TCenter)); tnsInverse44d(t2,t1);
  566. tnsMatrix44d final; tnsMultiply44d(t3,t1,trans); tnsMultiply44d(t1,t3,t2);
  567. if(!td->UseLocal) tnsMultiply44d(final,td->obmatinv,t1);
  568. else tnsMultiply44d(final,t1,td->obmatinv);
  569. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert));
  570. tnsApplyTransform43d(to->mv->p, final, to->p);
  571. }
  572. tnsInvalidateMeshBatch(td->mo); tnsMMeshCalculateNormal(td->mo);
  573. }
  574. }
  575. void la_CancelTransformObjects(MTransformData* td){
  576. if(!td->mo){
  577. for(int i=0;i<td->next;i++){ MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject));
  578. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  579. memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  580. if(td->CanvasDeltaMode) tnsGlobalMatrixChangedForDelta(to->o, 1);
  581. else tnsGlobalMatrixChanged(to->o, 1);
  582. }
  583. }else{
  584. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert)); tnsVectorCopy3d(to->origp,to->mv->p); }
  585. tnsInvalidateMeshBatch(td->mo);
  586. }
  587. }
  588. void la_RecordTransformDifferences(MTransformData* td){
  589. if(!td->mo){
  590. for(int i=0;i<td->next;i++){ MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject));
  591. laRecordInstanceDifferences(to->o, "tns_object");
  592. } laPushDifferences(td->mode==LA_TRANSFORM_MODE_GRAB?"Moved objects":td->mode==LA_TRANSFORM_MODE_ROTATE?"Rotated objects":"Scaled objects", TNS_HINT_TRANSFORM);
  593. }else{
  594. laRecordInstanceDifferences(td->mo, "tns_mesh_object");
  595. laPushDifferences(td->mode==LA_TRANSFORM_MODE_GRAB?"Moved primitives":td->mode==LA_TRANSFORM_MODE_ROTATE?"Rotated primitives":"Scaled primitives", TNS_HINT_GEOMETRY);
  596. tnsInvalidateMeshBatch(td->mo);
  597. }
  598. }
  599. void la_FreeTransformData(MTransformData* td){
  600. free(td->Originals);
  601. strEndEdit(&td->Entry, 1);
  602. memFree(td);
  603. }
  604. void la_MakeTransformOperatorHint(laOperator* a, MTransformData* td){
  605. strSafeDestroy(&a->RuntimeHint);
  606. strSafePrint(&a->RuntimeHint, "%s ",
  607. td->mode==LA_TRANSFORM_MODE_GRAB?"Grab":td->mode==LA_TRANSFORM_MODE_ROTATE?"Rotate":td->mode==LA_TRANSFORM_MODE_SCALE?"Scale":"");
  608. char* entry=strGetEditString(td->Entry,0);
  609. strSafePrint(&a->RuntimeHint, "Delta: %.3lf [🔢 %s] 🆇🆈🆉 Lock axis: %s 🈳 %s ", td->DeltaVal, (entry&&entry[0])?entry:"Type...",
  610. td->LockAxis[0]?"X":td->LockAxis[1]?"Y":td->LockAxis[2]?"Z":"None",
  611. td->UseLocal?"Global/[Local]":"[Global]/Local");
  612. free(entry);
  613. if(td->mode==LA_TRANSFORM_MODE_GRAB){ strSafePrint(&a->RuntimeHint, "🡅🆇🆈🆉 Reverse: %s ",
  614. (td->LockAxis[0]<0||td->LockAxis[1]<0||td->LockAxis[2]<0)?"Yes":"No"); }
  615. if(td->mode!=LA_TRANSFORM_MODE_GRAB){ strSafePrint(&a->RuntimeHint,"🅶 Grab "); }
  616. if(td->mode!=LA_TRANSFORM_MODE_SCALE){ strSafePrint(&a->RuntimeHint,"🆂 Scale "); }
  617. if(td->mode!=LA_TRANSFORM_MODE_ROTATE){ strSafePrint(&a->RuntimeHint,"🆁 Rotate "); }
  618. }
  619. void la_RestoreTransform(tnsObject* ob, int restore_type, int restore_delta){
  620. if(restore_delta){
  621. if(restore_type==LA_TRANSFORM_RESTORE_LOC){ tnsVectorSet3(ob->DLocation,0,0,0); }
  622. elif(restore_type==LA_TRANSFORM_RESTORE_ROT){ tnsVectorSet3(ob->DRotation,0,0,0); }
  623. elif(restore_type==LA_TRANSFORM_RESTORE_SCA){ ob->DScale=1; }
  624. tnsDeltaTransformValueChanged(ob);
  625. }else{
  626. if(restore_type==LA_TRANSFORM_RESTORE_LOC){ tnsVectorSet3(ob->Location,0,0,0); }
  627. elif(restore_type==LA_TRANSFORM_RESTORE_ROT){ tnsVectorSet3(ob->Rotation,0,0,0); }
  628. elif(restore_type==LA_TRANSFORM_RESTORE_SCA){ ob->Scale=1; }
  629. tnsSelfTransformValueChanged(ob);
  630. }
  631. }
  632. int la_InitTransform(laOperator* a, laEvent* e, int mode, int restore_type, int restore_delta){
  633. if(!a->This || !a->This->EndInstance){ return 0; }
  634. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  635. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  636. tnsMeshObject* mo=root->Active;
  637. MTransformData* td=la_InitTransformData(ex->OffScr->pColor[0]->Width, ex->OffScr->pColor[0]->Height, c, ex->DeltaMode);
  638. a->CustomData = td;
  639. td->mode=mode;
  640. td->root=root;
  641. int ret=0;
  642. if(mo && mo->Base.Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){
  643. if(la_PopulateTransformVerticies(td, mo)){ ex->ClickedX=e->x; ex->ClickedY=e->y; ret=1; }
  644. }else{
  645. if(la_PopulateTransformObjects(td,root)){ ex->ClickedX=e->x; ex->ClickedY=e->y; ret=1; }
  646. if(ret && restore_type){
  647. for(int i=0;i<td->next;i++){ MTOrigObject* ob=arrElement(td->Originals, i, sizeof(MTOrigObject));
  648. la_RestoreTransform(ob->o,restore_type,restore_delta);
  649. }
  650. la_RecordTransformDifferences(td); laNotifyUsers("tns.world"); la_FreeTransformData(td);
  651. return 0;
  652. }
  653. }
  654. if(ret){
  655. if(mode==LA_TRANSFORM_MODE_SCALE){ la_GetTransformInitialScale(td,ui,e->x,e->y); ex->DrawCursor=LA_CANVAS_CURSOR_ARROW; }
  656. elif(mode==LA_TRANSFORM_MODE_ROTATE){ la_GetTransformInitialRotation(td,ui,e->x,e->y); ex->DrawCursor=LA_CANVAS_CURSOR_ARROW; }
  657. ex->TargetX=td->CenterX+ui->L; ex->TargetY=td->CenterY+ui->U; ex->OnX=e->x; ex->OnY=e->y;
  658. la_MakeTransformOperatorHint(a, td);
  659. laNotifyUsers("tns.world"); return 1;
  660. }
  661. return 0;
  662. }
  663. int OPINV_Grab(laOperator *a, laEvent *e){
  664. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_GRAB,0,0)) return LA_RUNNING; return LA_FINISHED_PASS;
  665. }
  666. int OPINV_Scale(laOperator *a, laEvent *e){
  667. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_SCALE,0,0)) return LA_RUNNING; return LA_FINISHED_PASS;
  668. }
  669. int OPINV_Rotate(laOperator *a, laEvent *e){
  670. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_ROTATE,0,0)) return LA_RUNNING; return LA_FINISHED_PASS;
  671. }
  672. int OPMOD_Transformation(laOperator *a, laEvent *e){
  673. if(!a->This || !a->This->EndInstance){ return 0; }
  674. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  675. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  676. tnsMeshObject* mo=root->Active;
  677. MTransformData* td=a->CustomData;
  678. if (e->Input=='x'||e->Input=='y'||e->Input=='z'||e->Input=='g'||e->Input=='s'||e->Input=='r'||
  679. e->Input=='X'||e->Input=='Y'||e->Input=='Z'||e->Input==' '){ /*pass*/ }
  680. else{ la_ProcessTextEdit(e, td->Entry, 0); }
  681. char* entered;
  682. if(entered=strGetEditString(td->Entry,0)){ int status;
  683. if((status=sscanf(entered,"%lf",&td->UserDeltaVal)) && status!=EOF) td->UseUserDelta=1; else td->UseUserDelta=0;
  684. td->UserDeltaVal=fabs(td->UserDeltaVal); for(char*pc=entered;*pc;pc++){ if(*pc=='-') td->UserDeltaVal=-td->UserDeltaVal; } free(entered);
  685. }
  686. if(e->Type==LA_KEY_DOWN){
  687. int Other=1; if(e->SpecialKeyBit&LA_KEY_SHIFT){ Other=-1; }
  688. if(e->key=='x'){ td->LockAxis[0]=Other*((Other<0&&td->LockAxis[0])?td->LockAxis[0]:!td->LockAxis[0]); td->LockAxis[1]=td->LockAxis[2]=0; }
  689. if(e->key=='y'){ td->LockAxis[1]=Other*((Other<0&&td->LockAxis[1])?td->LockAxis[1]:!td->LockAxis[1]); td->LockAxis[0]=td->LockAxis[2]=0; }
  690. if(e->key=='z'){ td->LockAxis[2]=Other*((Other<0&&td->LockAxis[2])?td->LockAxis[2]:!td->LockAxis[2]); td->LockAxis[0]=td->LockAxis[1]=0; }
  691. if(e->key=='g' && td->mode!=LA_TRANSFORM_MODE_GRAB){ td->mode=LA_TRANSFORM_MODE_GRAB; ex->DrawCursor=0; }
  692. if(e->key=='s' && td->mode!=LA_TRANSFORM_MODE_SCALE){ td->mode=LA_TRANSFORM_MODE_SCALE; la_GetTransformInitialScale(td,ui,e->x,e->y);ex->DrawCursor=LA_CANVAS_CURSOR_ARROW; }
  693. if(e->key=='r' && td->mode!=LA_TRANSFORM_MODE_ROTATE){ td->mode=LA_TRANSFORM_MODE_ROTATE; la_GetTransformInitialRotation(td,ui,e->x,e->y);ex->DrawCursor=LA_CANVAS_CURSOR_ARROW; }
  694. if(e->key==' '){ td->UseLocal=!td->UseLocal; }
  695. }
  696. if(e->Type==LA_MOUSEMOVE || e->Type==LA_KEY_DOWN){
  697. switch(td->mode){
  698. case LA_TRANSFORM_MODE_GRAB: la_ApplyTranslation(td,e->x-ex->ClickedX, e->y-ex->ClickedY); break;
  699. case LA_TRANSFORM_MODE_SCALE: la_ApplyScale(td,e->x-ui->L, e->y-ui->U); break;
  700. case LA_TRANSFORM_MODE_ROTATE: la_ApplyRotation(td,e->x-ui->L, e->y-ui->U); break;
  701. default: break;
  702. }
  703. ex->OnX=e->x; ex->OnY=e->y;
  704. laNotifyUsers("tns.world");
  705. }
  706. if(e->Type==LA_L_MOUSE_DOWN || (e->Type==LA_KEY_DOWN && e->key==LA_KEY_ENTER)){ ex->DrawCursor=0;
  707. la_RecordTransformDifferences(td);
  708. laNotifyUsers("tns.world"); la_FreeTransformData(td); return LA_FINISHED;
  709. }
  710. if(e->Type==LA_R_MOUSE_DOWN || e->Type==LA_ESCAPE_DOWN){ ex->DrawCursor=0;
  711. la_CancelTransformObjects(td); laNotifyUsers("tns.world"); la_FreeTransformData(td); return LA_FINISHED;
  712. }
  713. la_MakeTransformOperatorHint(a, td);
  714. return LA_RUNNING;
  715. }
  716. int OPINV_ClearTransformation(laOperator *a, laEvent *e){
  717. laCanvasExtra* ex=a->This->EndInstance; int restore=0;
  718. char* channel=strGetArgumentString(a->ExtraInstructionsP,"channel");
  719. if(channel){
  720. if(channel[0]=='L' || channel[0]=='l'){ restore=LA_TRANSFORM_RESTORE_LOC; }
  721. if(channel[0]=='R' || channel[0]=='r'){ restore=LA_TRANSFORM_RESTORE_ROT; }
  722. if(channel[0]=='S' || channel[0]=='s'){ restore=LA_TRANSFORM_RESTORE_SCA; }
  723. }else{
  724. laEnableOperatorPanel(a,a->This,e->x,e->y,150,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING;
  725. }
  726. char* dt=strGetArgumentString(a->ExtraInstructionsP,"delta");
  727. int delta=dt?strSame(dt,"true"):ex->DeltaMode;
  728. la_InitTransform(a,e,0,restore,delta);
  729. return LA_FINISHED;
  730. }
  731. void laui_ClearTransformation(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  732. laColumn* c=laFirstColumn(uil), *cl,*cr; laSplitColumn(uil,c,0.6); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  733. laShowItemFull(uil,cl,pp,"_this_M_clear_transformations",0,"channel=loc;text=Location",0,0);
  734. laShowItemFull(uil,cl,pp,"_this_M_clear_transformations",0,"channel=rot;text=Rotation",0,0);
  735. laShowItemFull(uil,cl,pp,"_this_M_clear_transformations",0,"channel=sca;text=Scale",0,0);
  736. laShowItemFull(uil,cr,pp,"_this_M_clear_transformations",0,"channel=loc;text=Delta;delta=true",0,0);
  737. laShowItemFull(uil,cr,pp,"_this_M_clear_transformations",0,"channel=rot;text=Delta;delta=true",0,0);
  738. laShowItemFull(uil,cr,pp,"_this_M_clear_transformations",0,"channel=sca;text=Delta;delta=true",0,0);
  739. }
  740. int la_ParentableRecursive(tnsObject* root, tnsObject* parent){
  741. for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  742. tnsObject* o=lip->p; if((!o) || (!(o->Flags&TNS_OBJECT_FLAGS_SELECTED)) || (o==parent)) continue;
  743. if(!tnsCheckParentable(o,parent)) return 0;
  744. if(!la_ParentableRecursive(o,parent)) return 0;
  745. }return 1;
  746. }
  747. void la_MakeParentExecuteRecursive(tnsObject* root, tnsObject* parent, int Unparent, int KeepTransform){
  748. laListItemPointer* NextLip;
  749. for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=NextLip){ NextLip=lip->pNext;
  750. tnsObject* o=lip->p; if((!o) || (!(o->Flags&TNS_OBJECT_FLAGS_SELECTED)) || (o==parent)) continue;
  751. if(Unparent) tnsUnparentObject(o, KeepTransform);
  752. else tnsParentObject(o, parent, KeepTransform);
  753. la_MakeParentExecuteRecursive(o,parent,Unparent,KeepTransform);
  754. }
  755. laNotifyUsers("tns.world");
  756. }
  757. int OPINV_MakeParent(laOperator *a, laEvent *e){
  758. if(!a->This || !a->This->EndInstance){ return 0; }
  759. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  760. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  761. tnsObject* mo=root->Active;
  762. int Unparent=0,KeepTransform=1;
  763. char* action=strGetArgumentString(a->ExtraInstructionsP,"action");
  764. char* keep=strGetArgumentString(a->ExtraInstructionsP,"keep_transform");
  765. if(strSame(action,"unparent")){ Unparent=1; }
  766. if(strSame(keep,"false")){ KeepTransform=0; }
  767. if(!Unparent){ if((!mo) || (!(mo->Flags&TNS_OBJECT_FLAGS_SELECTED))) return LA_FINISHED;
  768. if(!la_ParentableRecursive(root,mo)){ laEnableMessagePanel(0,0,"It didn't work","There are loops in parenting",e->x,e->y,0,e); return LA_FINISHED; }
  769. }
  770. if(keep){
  771. la_MakeParentExecuteRecursive(root,mo,Unparent,KeepTransform);
  772. laRecordInstanceDifferences(&T->World, "tns_world"); laPushDifferences(Unparent?"Unparent":"Parent", TNS_HINT_TRANSFORM);
  773. return LA_FINISHED;
  774. }
  775. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e);
  776. return LA_RUNNING;
  777. }
  778. void laui_MakeParent(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  779. laColumn* c=laFirstColumn(uil);
  780. laShowItemFull(uil,c,pp,"_this_M_make_parent",0,"action=parent;keep_transform=true;text=Keep transform",0,0);
  781. laShowItemFull(uil,c,pp,"_this_M_make_parent",0,"action=parent;keep_transform=false;text=Directly",0,0);
  782. }
  783. void laui_Unparent(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  784. laColumn* c=laFirstColumn(uil);
  785. laShowItemFull(uil,c,pp,"_this_M_unparent",0,"action=unparent;keep_transform=true;text=Keep transform",0,0);
  786. laShowItemFull(uil,c,pp,"_this_M_unparent",0,"action=unparent;keep_transform=false;text=Directly",0,0);
  787. }
  788. STRUCTURE(MEDupVert){
  789. int oi; tnsMVert* nmv; tnsMVert* omv; int IsBorder;
  790. };
  791. STRUCTURE(MEDupEdge){
  792. int oi; tnsMEdge* nme; tnsMEdge* ome; int IsBorder;
  793. };
  794. STRUCTURE(MEDupFace){
  795. int oi; tnsMFace* nmf; tnsMFace* omf;
  796. };
  797. STRUCTURE(MExtrudeExtra){
  798. MEDupVert* dv; int nextv,maxv;
  799. MEDupEdge* de; int nexte,maxe;
  800. MEDupFace* df; int nextf,maxf;
  801. tnsMeshObject* mo;
  802. int RemoveOriginalFaces;
  803. };
  804. int la_IsSelectionBorderVertex(tnsMVert* mv){
  805. if(!mv->elink.pFirst)
  806. return 0;
  807. int NearSelected=0, NearUnselected=0;
  808. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  809. tnsMEdge* me=lip->p; if(!(me->fl&&me->fr)) return 1;
  810. tnsMVert* av=tnsMMeshEdgeAnotherVert(me,mv);
  811. if(me->fl->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { return 1; }
  812. if(me->fr->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { return 1; }
  813. if(NearUnselected && NearSelected) return 1;
  814. }
  815. return 0;
  816. }
  817. int la_IsSelectionBorderEdge(tnsMEdge* me){
  818. if(me->fl&&me->fr){
  819. if ((me->fl->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fr->flags&TNS_MESH_FLAG_SELECTED))||
  820. (me->fr->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fl->flags&TNS_MESH_FLAG_SELECTED))) return 1;
  821. else return (me->fr->flags==me->fl->flags&&me->fl->flags==0);
  822. } return 1;
  823. }
  824. MExtrudeExtra* la_InitExtrude(tnsMeshObject* mo){
  825. MExtrudeExtra* ee=memAcquireSimple(sizeof(MExtrudeExtra));
  826. arrEnsureLength(&ee->dv, ee->nextv, &ee->maxv, sizeof(MEDupVert));
  827. arrEnsureLength(&ee->de, ee->nexte, &ee->maxe, sizeof(MEDupEdge));
  828. arrEnsureLength(&ee->df, ee->nextf, &ee->maxf, sizeof(MEDupFace));
  829. ee->mo=mo;
  830. return ee;
  831. }
  832. void la_ExtrudeMakeDuplication(MExtrudeExtra* ee){
  833. tnsMeshObject* mo=ee->mo;
  834. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  835. arrEnsureLength(&ee->dv, ee->nextv, &ee->maxv, sizeof(MEDupVert));
  836. MEDupVert* dv=&ee->dv[ee->nextv];
  837. tnsMVert* nmv=tnsMMeshNewVert(mo); tnsVectorCopy3d(mv->p, &nmv->p); dv->oi=mv->i; mv->i=ee->nextv; dv->nmv=nmv; dv->omv=mv;
  838. dv->IsBorder=la_IsSelectionBorderVertex(mv);
  839. ee->nextv++;
  840. }
  841. for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ if(!(me->flags&TNS_MESH_FLAG_SELECTED)) continue;
  842. arrEnsureLength(&ee->de, ee->nexte, &ee->maxe, sizeof(MEDupEdge));
  843. MEDupEdge* de=&ee->de[ee->nexte];
  844. tnsMEdge* nme=tnsMMeshNewEdge(mo); de->oi=me->i; me->i=ee->nexte; de->nme=nme; de->ome=me; de->IsBorder=la_IsSelectionBorderEdge(me);
  845. tnsMMeshEdgeAssignVerts(nme, ee->dv[me->vl->i].nmv, ee->dv[me->vr->i].nmv);
  846. if(de->IsBorder&&me->fl&&me->fr){ ee->RemoveOriginalFaces=1; }
  847. ee->nexte++;
  848. }
  849. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){
  850. if(!(mf->flags&TNS_MESH_FLAG_SELECTED)) continue;
  851. arrEnsureLength(&ee->df, ee->nextf, &ee->maxf, sizeof(MEDupFace));
  852. MEDupFace* df=&ee->df[ee->nextf];
  853. tnsMFace* nmf=tnsMMeshNewFace(mo); df->oi=mf->i; mf->i=ee->nextf; df->nmf=nmf; df->omf=mf;
  854. for(laListItemPointer*lip=mf->l.pFirst;lip;lip=lip->pNext){
  855. tnsMEdge* ome=lip->p; tnsMMeshFaceAddEdge(nmf,ee->de[ome->i].nme);
  856. }
  857. ee->nextf++;
  858. }
  859. for(int i=0;i<ee->nextv;i++){ ee->dv[i].omv->i=ee->dv[i].oi; ee->dv[i].omv->flags&=(~TNS_MESH_FLAG_SELECTED); ee->dv[i].nmv->flags|=TNS_MESH_FLAG_SELECTED; }
  860. for(int i=0;i<ee->nexte;i++){ ee->de[i].ome->i=ee->de[i].oi; ee->de[i].ome->flags&=(~TNS_MESH_FLAG_SELECTED); ee->de[i].nme->flags|=TNS_MESH_FLAG_SELECTED; }
  861. for(int i=0;i<ee->nextf;i++){
  862. ee->df[i].omf->i=ee->df[i].oi;
  863. ee->df[i].omf->flags&=(~TNS_MESH_FLAG_SELECTED);
  864. ee->df[i].nmf->flags|=TNS_MESH_FLAG_SELECTED; }
  865. }
  866. void la_RemoveOriginalFaces(MExtrudeExtra* ee){
  867. tnsMeshObject* mo=ee->mo;
  868. if(ee->RemoveOriginalFaces){
  869. for(int i=0;i<ee->nextf;i++){ tnsMMeshRemoveFaceOnly(mo, ee->df[i].omf); }
  870. for(int i=0;i<ee->nexte;i++){ if(ee->de[i].IsBorder) continue; tnsMMeshRemoveEdgeFace(mo, ee->de[i].ome); }
  871. for(int i=0;i<ee->nextv;i++){ if(ee->dv[i].IsBorder) continue; tnsMMeshRemoveVertEdgeFace(mo, ee->dv[i].omv); }
  872. }
  873. }
  874. void la_ReconnectFaces(MExtrudeExtra* ee){
  875. tnsMeshObject* mo=ee->mo;
  876. for(int i=0;i<ee->nexte;i++){
  877. if(!ee->de[i].IsBorder) continue; MEDupEdge*de=&ee->de[i];
  878. tnsMMeshMakeFace4v(mo, de->ome->vl, de->ome->vr, de->nme->vr, de->nme->vl);
  879. }
  880. }
  881. void la_FinishExtrude(MExtrudeExtra* ee, int PushDifferences){
  882. tnsMMeshRefreshIndex(ee->mo);
  883. tnsInvalidateMeshBatch(ee->mo);
  884. if(PushDifferences){
  885. laRecordInstanceDifferences(ee->mo, "tns_mesh_object"); laPushDifferences("Extruded", TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  886. }
  887. free(ee->dv); free(ee->de); free(ee->df); memFree(ee);
  888. }
  889. int OPINV_Extrude(laOperator *a, laEvent *e){
  890. if(!a->This || !a->This->EndInstance){ return 0; }
  891. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  892. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  893. tnsMeshObject* mo=root->Active; if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE) return 0;
  894. if(!tnsMMeshAnySelected(mo)) return LA_FINISHED;
  895. MExtrudeExtra* ee=la_InitExtrude(mo);
  896. la_ExtrudeMakeDuplication(ee);
  897. if(strSame(strGetArgumentString(a->ExtraInstructionsP,"duplicate_only"), "true")){
  898. la_FinishExtrude(ee, 1);
  899. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_GRAB,0,0)) return LA_RUNNING; return LA_FINISHED;
  900. }
  901. la_RemoveOriginalFaces(ee);
  902. la_ReconnectFaces(ee);
  903. la_FinishExtrude(ee, 1);
  904. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_GRAB,0,0)) return LA_RUNNING; return LA_FINISHED;
  905. return LA_FINISHED;
  906. }
  907. int la_EdgeShouldDeleteVert(tnsMVert* mv){
  908. if(!mv->elink.pFirst) return 0;
  909. int NearSelected=0, NearUnselected=0;
  910. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  911. tnsMEdge* me=lip->p;
  912. tnsMVert* av=tnsMMeshEdgeAnotherVert(me,mv);
  913. if(av->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { NearUnselected=1; }
  914. if(NearUnselected && NearSelected) return 0;
  915. }
  916. if(NearSelected&&(!NearUnselected)) return 1; return 0;
  917. }
  918. int la_FaceShouldDeleteVert(tnsMVert* mv){
  919. if(!mv->elink.pFirst) return 0;
  920. int NearSelected=0, NearUnselected=0, IsBorder=0;
  921. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  922. tnsMEdge* me=lip->p;
  923. if(!(me->fl&&me->fr)){ IsBorder=1; }
  924. if(me->fl){ if(me->fl->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { NearUnselected=1; } }
  925. if(me->fr){ if(me->fr->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { NearUnselected=1; } }
  926. if(NearUnselected && NearSelected) return 0;
  927. }
  928. if((!NearUnselected) && IsBorder) return 1;
  929. return 1;
  930. }
  931. int la_FaceShouldDeleteEdge(tnsMEdge* me){
  932. if(me->fl&&me->fr){
  933. if ((me->fl->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fr->flags&TNS_MESH_FLAG_SELECTED))||
  934. (me->fr->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fl->flags&TNS_MESH_FLAG_SELECTED))) return 0;
  935. else return (me->fr->flags==me->fl->flags&&me->fl->flags==1);
  936. }else{
  937. if(me->fl&&me->fl->flags&TNS_MESH_FLAG_SELECTED&&!me->fr) return 1;
  938. if(me->fr&&me->fr->flags&TNS_MESH_FLAG_SELECTED&&!me->fl) return 1;
  939. } return 0;
  940. }
  941. void la_DeleteVertices(tnsMeshObject* mo){
  942. tnsMVert* nextmv; for(tnsMVert*mv=mo->mv.pFirst;mv;mv=nextmv){ nextmv=mv->Item.pNext; if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  943. tnsMMeshRemoveVertEdgeFace(mo, mv);
  944. }
  945. }
  946. void la_DeleteEdges(tnsMeshObject* mo){
  947. laListHandle lv={0};
  948. for(tnsMVert*mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  949. if(la_EdgeShouldDeleteVert(mv)) lstAppendPointer(&lv,mv);
  950. }
  951. tnsMEdge* nextme; for(tnsMEdge*me=mo->me.pFirst;me;me=nextme){ nextme=me->Item.pNext; if(!(me->flags&TNS_MESH_FLAG_SELECTED)) continue;
  952. tnsMMeshRemoveEdgeFace(mo, me);
  953. }
  954. tnsMVert* mv; while(mv=lstPopPointer(&lv)){ tnsMMeshRemoveVertEdgeFace(mo,mv); }
  955. }
  956. void la_DeleteFaces(tnsMeshObject* mo, int OnlyFaces){
  957. laListHandle lv={0}, le={0};
  958. if(OnlyFaces){
  959. tnsMFace* nextmf; for(tnsMFace*mf=mo->mf.pFirst;mf;mf=nextmf){ nextmf=mf->Item.pNext; if(!(mf->flags&TNS_MESH_FLAG_SELECTED)) continue;
  960. tnsMMeshRemoveFaceOnly(mo, mf);
  961. }
  962. }else{
  963. for(tnsMVert*mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  964. if(la_FaceShouldDeleteVert(mv)) lstAppendPointer(&lv,mv);
  965. }
  966. for(tnsMEdge*me=mo->me.pFirst;me;me=me->Item.pNext){ if(!(me->flags&TNS_MESH_FLAG_SELECTED)) continue;
  967. if(la_FaceShouldDeleteEdge(me)) lstAppendPointer(&le,me);
  968. }
  969. tnsMFace* nextmf; for(tnsMFace*mf=mo->mf.pFirst;mf;mf=nextmf){ nextmf=mf->Item.pNext; if(!(mf->flags&TNS_MESH_FLAG_SELECTED)) continue;
  970. tnsMMeshRemoveFaceOnly(mo, mf);
  971. }
  972. tnsMEdge* me; while(me=lstPopPointer(&le)){ tnsMMeshRemoveEdgeFace(mo,me); }
  973. tnsMVert* mv; while(mv=lstPopPointer(&lv)){ tnsMMeshRemoveVertEdgeFace(mo,mv); }
  974. }
  975. }
  976. int la_DeleteSelectedObjectsRecursive(tnsObject* root){
  977. int any=0; for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){ if(!lip->p) continue;
  978. tnsObject* o=lip->p; la_DeleteSelectedObjectsRecursive(lip->p);
  979. if(o->Flags&TNS_OBJECT_FLAGS_SELECTED){ tnsDestroyObject(o); any++; }
  980. }
  981. return any;
  982. }
  983. int OPINV_Delete(laOperator *a, laEvent *e){
  984. if(!a->This || !a->This->EndInstance){ return 0; }
  985. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  986. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  987. tnsMeshObject* mo=root->Active; if(!mo) return 0;
  988. if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){
  989. if(la_DeleteSelectedObjectsRecursive(root)){
  990. laRecordInstanceDifferences(&T->World, "tns_world"); laPushDifferences("Deleted objects", TNS_HINT_TRANSFORM); laNotifyUsers("tns.world");
  991. }
  992. }else{
  993. if(!tnsMMeshAnySelected(mo)) return LA_FINISHED;
  994. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"vertices")){
  995. la_DeleteVertices(mo);
  996. }elif(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"edges")){
  997. la_DeleteEdges(mo);
  998. }elif(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"faces")){
  999. la_DeleteFaces(mo,0);
  1000. }elif(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"only_faces")){
  1001. la_DeleteFaces(mo,1);
  1002. }else{
  1003. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e);
  1004. return LA_RUNNING;
  1005. }
  1006. tnsMMeshDeselectAll(mo);
  1007. tnsMMeshRefreshIndex(mo);
  1008. tnsInvalidateMeshBatch(mo);
  1009. laRecordInstanceDifferences(mo, "tns_mesh_object"); laPushDifferences("Deleted primitives", TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1010. }
  1011. return LA_FINISHED;
  1012. }
  1013. void laui_Delete(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  1014. laColumn* c=laFirstColumn(uil);
  1015. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=vertices;text=Vertices",0,0);
  1016. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=edges;text=Edges",0,0);
  1017. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=faces;text=Faces",0,0);
  1018. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=only_faces;text=Only Faces",0,0);
  1019. }
  1020. STRUCTURE(MIslandInfo){
  1021. laListItem Item;
  1022. laListHandle v,e,f;int numv,nume,numf;
  1023. int Paired,HasBranches;
  1024. };
  1025. STRUCTURE(MMakeData){
  1026. laListHandle Islands; int NumIslands;
  1027. };
  1028. #define M_SHOULD_INCL_PRIM(m) \
  1029. ((!(m->flags&TNS_MESH_FLAG_PICKED)) && (m->flags&TNS_MESH_FLAG_SELECTED))
  1030. #define M_SHOULD_USE_OE(oe,sf) \
  1031. (((!oe->fl)&&((!sf)||(oe->fr!=sf)))||((!oe->fr)&&((!sf)||(oe->fl!=sf))))
  1032. MIslandInfo* la_NewMIsland(MMakeData* md){ MIslandInfo* ii=memAcquireSimple(sizeof(MIslandInfo)); lstAppendItem(&md->Islands, ii); md->NumIslands++; return ii; }
  1033. void la_FillIslandFromVert(MIslandInfo* ii, tnsMVert* mv, int SelectMode){
  1034. mv->flags|=TNS_MESH_FLAG_PICKED; lstAppendPointer(&ii->v,mv); ii->numv++; int connections=0;
  1035. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){ tnsMEdge* oe=lip->p; tnsMVert* ov=tnsMMeshEdgeAnotherVert(oe,mv);
  1036. if(ov->flags&TNS_MESH_FLAG_SELECTED) connections++; else continue;
  1037. if(SelectMode==LA_CANVAS_SELECT_MODE_EDGES){ if(!(oe->flags&TNS_MESH_FLAG_SELECTED)) continue; }
  1038. if(M_SHOULD_INCL_PRIM(ov)){ la_FillIslandFromVert(ii,ov,SelectMode); } else { continue; }
  1039. if(M_SHOULD_INCL_PRIM(oe)){ lstAppendPointer(&ii->e,oe); ii->nume++; oe->flags|=TNS_MESH_FLAG_PICKED; }
  1040. if(oe->fl&&M_SHOULD_INCL_PRIM(oe->fl)){ lstAppendPointer(&ii->f,oe->fl); ii->numf++; oe->fl->flags|=TNS_MESH_FLAG_PICKED; }
  1041. if(oe->fr&&M_SHOULD_INCL_PRIM(oe->fr)){ lstAppendPointer(&ii->f,oe->fr); ii->numf++; oe->fr->flags|=TNS_MESH_FLAG_PICKED; }
  1042. }
  1043. if(connections>2) ii->HasBranches=1;
  1044. }
  1045. void la_GetSelectionIslands(tnsMeshObject* mo, MMakeData* md, int SelectMode){
  1046. tnsMMeshClearExtraFlags(mo);
  1047. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){
  1048. if(M_SHOULD_INCL_PRIM(mv)){ MIslandInfo* ii=la_NewMIsland(md); la_FillIslandFromVert(ii,mv,SelectMode); }
  1049. }
  1050. }
  1051. void la_ClearIslands(MMakeData* md){ MIslandInfo* ii; while(ii=lstPopItem(&md->Islands)){ while(lstPopPointer(&ii->v)); memFree(ii); } }
  1052. tnsMFace* la_MakeFacesFrom1Vert(tnsMeshObject* mo, tnsMVert* mv){
  1053. tnsMEdge* oe1=0,*oe2=0; tnsMVert* ov1,*ov2;
  1054. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  1055. tnsMEdge* oe=lip->p; if(oe->flags&TNS_MESH_FLAG_SELECTED) continue; if((!oe->fl)||(!oe->fr)){ if(!oe1)oe1=oe;elif(!oe2)oe2=oe;else return 0; /* more than 2 empty edges connected */ }
  1056. } if(!oe1||!oe2) return 0;
  1057. ov1=tnsMMeshEdgeAnotherVert(oe1,mv); ov2=tnsMMeshEdgeAnotherVert(oe2,mv);
  1058. laListHandle vl={0}; lstAppendPointer(&vl,ov1); lstAppendPointer(&vl,mv); lstAppendPointer(&vl,ov2);
  1059. tnsMFace* f=tnsMMeshMakeFaceN(mo, 3, &vl, 0);
  1060. ov1->flags|=TNS_MESH_FLAG_SELECTED;ov2->flags|=TNS_MESH_FLAG_SELECTED; mv->flags&=(~TNS_MESH_FLAG_SELECTED);
  1061. tnsMMeshEnsureSelectionFromVerts(mo);
  1062. while(lstPopPointer(&vl)); return f;
  1063. }
  1064. tnsMFace* la_MakeFacesFrom2Verts(tnsMeshObject* mo, tnsMVert* mv1, tnsMVert* mv2){
  1065. tnsMEdge* oe1=0,*oe2=0; tnsMVert* ov1,*ov2; tnsMFace* sf=0;
  1066. tnsMEdge* se=tnsMMeshVertsShareEdge(mv1,mv2); if(se->fl && se->fr) return 0; sf=se->fl?se->fl:se->fr;
  1067. for(laListItemPointer* lip=mv1->elink.pFirst;lip;lip=lip->pNext){
  1068. tnsMEdge* oe=lip->p; if(oe->flags&TNS_MESH_FLAG_SELECTED) continue; if(M_SHOULD_USE_OE(oe,sf)){ if(!oe1)oe1=oe;else return 0; /* more than 1 empty edge connected */ }
  1069. }
  1070. for(laListItemPointer* lip=mv2->elink.pFirst;lip;lip=lip->pNext){
  1071. tnsMEdge* oe=lip->p; if(oe->flags&TNS_MESH_FLAG_SELECTED) continue; if(M_SHOULD_USE_OE(oe,sf)){ if(!oe2)oe2=oe;else return 0; /* more than 1 empty edge connected */ }
  1072. }
  1073. if(!oe1||!oe2) return 0;
  1074. ov1=tnsMMeshEdgeAnotherVert(oe1,mv1); ov2=tnsMMeshEdgeAnotherVert(oe2,mv2);
  1075. ov1->flags|=TNS_MESH_FLAG_SELECTED;ov2->flags|=TNS_MESH_FLAG_SELECTED; mv1->flags&=(~TNS_MESH_FLAG_SELECTED);mv2->flags&=(~TNS_MESH_FLAG_SELECTED);
  1076. laListHandle vl={0}; int vcount=3;
  1077. lstAppendPointer(&vl,ov1); lstAppendPointer(&vl,mv1); lstAppendPointer(&vl,mv2); if(ov2!=ov1){ lstAppendPointer(&vl,ov2); vcount=4; }
  1078. tnsMFace* f=tnsMMeshMakeFaceN(mo, vcount, &vl, 0); tnsMMeshEnsureSelectionFromVerts(mo);
  1079. while(lstPopPointer(&vl)); return f;
  1080. }
  1081. int la_IsEndingVert(tnsMVert* mv){
  1082. int sel=0; for(laListItemPointer*lip=mv->elink.pFirst;lip;lip=lip->pNext){ tnsMEdge* me=lip->p;
  1083. if(tnsMMeshEdgeAnotherVert(me,mv)->flags&TNS_MESH_FLAG_SELECTED){ sel++; if(sel>1) return 0; }
  1084. }
  1085. if(sel==1) return 1; return 0;
  1086. }
  1087. void la_EnsureIslandVertsSequence(MIslandInfo* ii){
  1088. laListHandle l={0}; tnsMVert* startv=((laListItemPointer*)ii->v.pFirst)->p; laListItemPointer* nextlip;
  1089. for(laListItemPointer*lip=ii->v.pFirst;lip;lip=lip->pNext){ if(la_IsEndingVert(lip->p)){ startv=lip->p; break; } } // otherwise a loop, doesn't matter.
  1090. lstRemovePointer(&ii->v, startv); lstAppendPointer(&l, startv);
  1091. while(ii->v.pFirst){
  1092. for(laListItemPointer*lip=ii->v.pFirst;lip;lip=lip->pNext){ nextlip=lip->pNext;
  1093. if(tnsMMeshVertsShareEdge(startv, lip->p)){ startv=lip->p; lstRemoveItem(&ii->v,lip); lstAppendItem(&l, lip); break; }
  1094. }
  1095. }
  1096. memcpy(&ii->v, &l, sizeof(laListHandle));
  1097. }
  1098. MIslandInfo* la_GetNeighborIsland(tnsMVert* from, MMakeData* md){
  1099. int found=0; real dist=1e10; MIslandInfo* rii=0; for(MIslandInfo* ii=md->Islands.pFirst;ii;ii=ii->Item.pNext){ if(ii->Paired) continue;
  1100. found=1; tnsMVert* mv1=((laListItemPointer*)ii->v.pFirst)->p,*mv2=((laListItemPointer*)ii->v.pLast)->p;
  1101. real d1=tnsDist3dv(mv1->p, from->p),d2=tnsDist3dv(mv2->p, from->p);
  1102. if(d2<d1 && d2<dist){ lstReverse(&ii->v); } if(d1<dist || d2<dist){ rii=ii; }
  1103. }
  1104. if(rii) rii->Paired=1;
  1105. return rii;
  1106. }
  1107. int la_MakeFacesFromIslands(tnsMeshObject* mo, MMakeData* md){
  1108. int success=0;
  1109. if(!md->Islands.pFirst) return 0;
  1110. if(md->Islands.pFirst==md->Islands.pLast){
  1111. MIslandInfo* ii=md->Islands.pFirst; if(ii->HasBranches) return 0;
  1112. if(ii->numv==1){ laListItemPointer*lip=ii->v.pFirst; if(la_MakeFacesFrom1Vert(mo,lip->p)) success++; }
  1113. elif(ii->numv==2){ laListItemPointer*lip=ii->v.pFirst,*lip2=ii->v.pLast; if(la_MakeFacesFrom2Verts(mo,lip->p, lip2->p)) success++; }
  1114. else{ la_EnsureIslandVertsSequence(ii); if(tnsMMeshMakeFaceN(mo, ii->numv, &ii->v, 0)) success++; }
  1115. }else{
  1116. for(MIslandInfo* ii=md->Islands.pFirst;ii;ii=ii->Item.pNext){ if(ii->HasBranches) return 0; la_EnsureIslandVertsSequence(ii); }
  1117. laListHandle final={0}; int vcount=0;
  1118. MIslandInfo* ii=md->Islands.pFirst; ii->Paired=1; for(laListItemPointer* lip=ii->v.pFirst;lip;lip=lip->pNext){ lstAppendPointer(&final, lip->p); vcount++; }
  1119. while((ii=la_GetNeighborIsland(((laListItemPointer*)ii->v.pLast)->p, md))){
  1120. for(laListItemPointer* lip=ii->v.pFirst;lip;lip=lip->pNext){ lstAppendPointer(&final, lip->p); vcount++; }
  1121. }
  1122. tnsMEdge* fallback_e=0;
  1123. if(tnsMMeshMakeFaceN(mo,vcount,&final, &fallback_e) || fallback_e) success++;
  1124. while(lstPopPointer(&final));
  1125. }
  1126. return success;
  1127. }
  1128. int OPINV_Make(laOperator *a, laEvent *e){
  1129. if(!a->This || !a->This->EndInstance){ return 0; }
  1130. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1131. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1132. tnsMeshObject* mo=root->Active;
  1133. if(!mo||mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1134. MMakeData md={0};
  1135. la_GetSelectionIslands(mo,&md,ex->SelectMode);
  1136. int success=la_MakeFacesFromIslands(mo,&md); if(success){ tnsMMeshCalculateNormal(mo); }
  1137. la_ClearIslands(&md);
  1138. tnsMMeshRefreshIndex(mo);
  1139. tnsMMeshEnsureSelection(mo,ex->SelectMode);
  1140. tnsInvalidateMeshBatch(mo);
  1141. if(laRecordInstanceDifferences(mo, "tns_mesh_object")) laPushDifferences("Make primitives", TNS_HINT_GEOMETRY);
  1142. laNotifyUsers("tns.world");
  1143. return LA_FINISHED;
  1144. }
  1145. int OPINV_Subdiv(laOperator *a, laEvent *e){
  1146. if(!a->This || !a->This->EndInstance){ return 0; }
  1147. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1148. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1149. tnsMeshObject* mo=root->Active;
  1150. if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1151. laListHandle pending={0}; for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ if(me->flags&TNS_MESH_FLAG_SELECTED) lstAppendPointer(&pending, me); }
  1152. if(!pending.pFirst) return LA_FINISHED;
  1153. tnsMEdge* me; while(me=lstPopPointer(&pending)){ tnsMVert* mv=tnsMMeshEdgeInsertVertAt(mo,me,0.5,0,0,0); mv->flags|=TNS_MESH_FLAG_SELECTED; }
  1154. tnsMMeshRefreshIndex(mo);
  1155. tnsMMeshEnsureSelection(mo,ex->SelectMode);
  1156. tnsInvalidateMeshBatch(mo);
  1157. if(laRecordInstanceDifferences(mo, "tns_mesh_object")) laPushDifferences("Subdivide edges", TNS_HINT_GEOMETRY);
  1158. laNotifyUsers("tns.world");
  1159. return LA_FINISHED;
  1160. }
  1161. #define LA_ADD_CTX_OBJECT 0
  1162. #define LA_ADD_CTX_MESH 1
  1163. STRUCTURE(laObjectAddData){
  1164. int Context;
  1165. };
  1166. int OPINV_Add(laOperator *a, laEvent *e){
  1167. if(!a->This || !a->This->EndInstance){ return 0; }
  1168. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1169. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1170. tnsMeshObject* mo=root->Active; int ran=0; tnsObject* no=0;
  1171. laObjectAddData *ad=memAcquire(sizeof(laObjectAddData));a->CustomData=ad;
  1172. char* str=strGetArgumentString(a->ExtraInstructionsP, "mode");
  1173. if((!mo) || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ ad->Context=LA_ADD_CTX_OBJECT;
  1174. if(strSame(str,"PLANE")){ tnsDeselectAllObjects(root);
  1175. no=tnsCreateMeshPlane(root, "Plane",0,0,0,10); no->Flags|=TNS_OBJECT_FLAGS_SELECTED; memAssignRef(root,&root->Active,no); ran=1; }
  1176. elif(strSame(str,"INSTANCER")){ tnsDeselectAllObjects(root);
  1177. no=tnsCreateInstancer(root, "Instancer",0,0,0); no->Flags|=TNS_OBJECT_FLAGS_SELECTED; memAssignRef(root,&root->Active,no); ran=1; }
  1178. else{ laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING; }
  1179. if(ran){ laRecordAndPush(0,"tns.world","Add object",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world"); }
  1180. }else{ ad->Context=LA_ADD_CTX_MESH;
  1181. if(strSame(str,"PLANE")){
  1182. tnsMMeshDeselectAll(mo); tnsAddMMeshPlane(mo, 10); tnsMMeshEnsureSelection(mo,ex->SelectMode); ran=1;
  1183. }else{ laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING; }
  1184. if(ran){
  1185. tnsMMeshRefreshIndex(mo); tnsInvalidateMeshBatch(mo);
  1186. laRecordInstanceDifferences(mo, "tns_mesh_object"); laPushDifferences("Add primitives", TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1187. }
  1188. }
  1189. return LA_FINISHED;
  1190. }
  1191. void laui_Add(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  1192. laColumn* c=laFirstColumn(uil);
  1193. laUiItem* b=laOnConditionThat(uil,c,laEqual(laPropExpression(actinst,"context"),laIntExpression(LA_ADD_CTX_OBJECT)));{
  1194. laShowLabel(uil,c,"Empty",0,0)->Flags|=LA_TEXT_MONO|LA_UI_FLAGS_DISABLED;
  1195. laShowItemFull(uil,c,pp,"_this_M_add",0,"mode=INSTANCER;text=Instancer",0,0);
  1196. }laEndCondition(uil,b);
  1197. laShowLabel(uil,c,"Primitives",0,0)->Flags|=LA_TEXT_MONO|LA_UI_FLAGS_DISABLED;
  1198. laShowItemFull(uil,c,pp,"_this_M_add",0,"mode=PLANE;text=Plane",0,0);
  1199. }
  1200. int OPINV_Separate(laOperator *a, laEvent *e){
  1201. if(!a->This || !a->This->EndInstance){ return 0; }
  1202. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1203. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1204. tnsMeshObject* mo=root->Active; int ran=0;
  1205. if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){
  1206. return LA_CANCELED;
  1207. }
  1208. if(!tnsMMeshAnySelected(mo)) return LA_CANCELED;
  1209. MExtrudeExtra* ee=la_InitExtrude(mo);
  1210. la_ExtrudeMakeDuplication(ee);
  1211. ee->RemoveOriginalFaces=1;la_RemoveOriginalFaces(ee);
  1212. tnsMeshObject* no=tnsCreateMeshEmpty(mo->Base.ParentObject?mo->Base.ParentObject:mo->Base.InRoot, mo->Base.Name->Ptr, 0,0,0);
  1213. tnsCopyObjectTransformationsLocal(no,mo);
  1214. no->Mode=TNS_MESH_EDIT_MODE;
  1215. tnsMVert* nmv; for(tnsMVert* mv=mo->mv.pFirst;mv;mv=nmv){ nmv=mv->Item.pNext; if(!(mv->flags&TNS_MESH_FLAG_SELECTED))continue;
  1216. lstRemoveItem(&mo->mv, mv); lstAppendItem(&no->mv, mv); no->totmv++; mo->totmv--; }
  1217. tnsMEdge* nme; for(tnsMEdge* me=mo->me.pFirst;me;me=nme){ nme=me->Item.pNext; if(!(me->flags&TNS_MESH_FLAG_SELECTED))continue;
  1218. lstRemoveItem(&mo->me, me); lstAppendItem(&no->me, me); no->totme++; mo->totme--; }
  1219. tnsMFace* nmf; for(tnsMFace* mf=mo->mf.pFirst;mf;mf=nmf){ nmf=mf->Item.pNext; if(!(mf->flags&TNS_MESH_FLAG_SELECTED))continue;
  1220. lstRemoveItem(&mo->mf, mf); lstAppendItem(&no->mf, mf); no->totmf++; mo->totmf--; }
  1221. tnsMMeshRefreshIndex(no); tnsMeshLeaveEditMode(no);
  1222. la_FinishExtrude(ee, 0);
  1223. tnsMMeshRefreshIndex(mo); tnsInvalidateMeshBatch(mo);
  1224. laRecordAndPush(0,"tns.world","Separate mesh parts",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1225. return LA_FINISHED;
  1226. }
  1227. void la_PopulateSelectedMeshObjects(tnsObject* root, laListHandle* l){
  1228. if(root->Type==TNS_OBJECT_MESH && root->Flags&TNS_OBJECT_FLAGS_SELECTED){ lstAppendPointer(l,root); }
  1229. for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  1230. la_PopulateSelectedMeshObjects(lip->p, l);
  1231. }
  1232. }
  1233. int OPINV_Combine(laOperator *a, laEvent *e){
  1234. if(!a->This || !a->This->EndInstance){ return 0; }
  1235. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1236. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1237. tnsMeshObject* mo=root->Active; int ran=0;
  1238. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode==TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1239. laListHandle pending={0}; la_PopulateSelectedMeshObjects(root,&pending);
  1240. tnsMeshObject* o; while(o=lstPopPointer(&pending)){ if(o==mo || o->Mode==TNS_MESH_EDIT_MODE) continue;
  1241. if(tnsMergeMeshObjects(mo, o)) ran++;
  1242. }
  1243. if(ran){
  1244. tnsMMeshRefreshIndex(mo); tnsInvalidateMeshBatch(mo);
  1245. laRecordAndPush(0,"tns.world","Merge mesh objects",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1246. }
  1247. return LA_FINISHED;
  1248. }
  1249. int OPINV_Duplicate(laOperator *a, laEvent *e){
  1250. if(!a->This || !a->This->EndInstance){ return 0; }
  1251. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1252. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1253. tnsMeshObject* mo=root->Active; int ran=0;
  1254. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode==TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1255. laListHandle pending={0}; la_PopulateSelectedMeshObjects(root,&pending);
  1256. tnsMeshObject* o; tnsMeshObject* no;while(o=lstPopPointer(&pending)){ if(o->Mode==TNS_MESH_EDIT_MODE) continue;
  1257. if(no=tnsDuplicateMeshObjects(o)){ no->Base.Flags|=TNS_OBJECT_FLAGS_SELECTED; o->Base.Flags&=(~TNS_OBJECT_FLAGS_SELECTED);
  1258. if(mo==o){ memAssignRef(root,&root->Active,no); } ran++;
  1259. }
  1260. }
  1261. if(ran){
  1262. laRecordAndPush(0,"tns.world","Merge mesh objects",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1263. if(la_InitTransform(a,e,LA_TRANSFORM_MODE_GRAB,0,0)) return LA_RUNNING; return LA_FINISHED;
  1264. }
  1265. return LA_FINISHED;
  1266. }
  1267. int OPINV_RecalculateNormals(laOperator *a, laEvent *e){
  1268. if(!a->This || !a->This->EndInstance){ return 0; }
  1269. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1270. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1271. tnsMeshObject* mo=root->Active; int ran=0;
  1272. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1273. ran=tnsMMeshCalculateNormal(mo);
  1274. if(ran){ tnsInvalidateMeshBatch(mo); laRecordAndPush(0,"tns.world","Recalculate Normals",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world"); }
  1275. return LA_FINISHED;
  1276. }
  1277. STRUCTURE(MKnifeElement){
  1278. laListItem Item;
  1279. void* p;
  1280. int Type;
  1281. };
  1282. void la_KnifeUpdateToolBatch(MSelectExtra* se,tnsMeshObject* o){
  1283. if(o->ExtraBatch) tnsDeleteBatch(o->ExtraBatch); o->ExtraBatch=0;
  1284. int count=lstCountElements(&se->KnifeElements); if((!count) && (!se->PendingElem)) return;
  1285. float* points=calloc((count+1)*3,sizeof(real));
  1286. float* p=points; real tmp[3],trans[4];
  1287. for(MKnifeElement* ke=se->KnifeElements.pFirst;ke;ke=ke->Item.pNext){
  1288. if(ke->Type==TNS_MMESH_EDGE_BIT){ tnsMEdge* me=ke->p; tnsVectorSet3v(tmp,me->vl->p); tnsVectorAccum3d(tmp,me->vr->p);
  1289. tnsVectorMultiSelf3d(tmp,0.5); tnsApplyTransform43d(trans,o->Base.GlobalTransform,tmp); tnsVectorSet3v(p,trans); }
  1290. else{ tnsMVert* mv=ke->p; tnsApplyTransform43d(trans,o->Base.GlobalTransform,mv->p); tnsVectorSet3v(p,trans); }
  1291. p+=3;
  1292. }
  1293. if(se->PendingElem){
  1294. if(se->PendingElemType==TNS_MMESH_EDGE_BIT){ tnsMEdge* me=se->PendingElem; tnsVectorSet3v(tmp,me->vl->p); tnsVectorAccum3d(tmp,me->vr->p);
  1295. tnsVectorMultiSelf3d(tmp,0.5); tnsApplyTransform43d(trans,o->Base.GlobalTransform,tmp); tnsVectorSet3v(p,trans); }
  1296. else{ tnsMVert* mv=se->PendingElem; tnsApplyTransform43d(trans,o->Base.GlobalTransform,mv->p); tnsVectorSet3v(p,trans); }
  1297. }elif(count){
  1298. tnsVectorSet3v(p,p-3);
  1299. }
  1300. uint32_t elem=count;
  1301. tnsBatch* batch=tnsCreateBatch(count+1,3,points,0,0,0,0); tnsBatchCommand*c;
  1302. c=tnsCreateCommand(batch, "hovering_point", 1, 3, GL_POINTS, &elem, 0);
  1303. tnsCommandUseUniformColor(c,laAccentColor(LA_BT_SVERTEX));
  1304. tnsCommandUseWidth(c, 8);
  1305. if(count){
  1306. c=tnsCreateCommand(batch, "edges", count+(se->IsLoop?0:1), 3, se->IsLoop?GL_LINE_LOOP:GL_LINE_STRIP, 0, 0);
  1307. tnsCommandUseUniformColor(c,laAccentColor(LA_BT_NORMAL));
  1308. tnsCommandUseWidth(c, 2);
  1309. c=tnsCreateCommand(batch, "points", count, 3, GL_POINTS, 0, 0);
  1310. tnsCommandUseUniformColor(c,laAccentColor(LA_BT_NORMAL));
  1311. tnsCommandUseWidth(c, 6);
  1312. }
  1313. o->ExtraBatch=batch;
  1314. free(points);
  1315. }
  1316. int la_KnifeIsDuplicated(MSelectExtra* se, void* ref){
  1317. for(MKnifeElement* ke=se->KnifeElements.pFirst;ke;ke=ke->Item.pNext){
  1318. if(ke->Type==TNS_MMESH_EDGE_BIT && ke->p==ref){ return 1; }
  1319. }
  1320. return 0;
  1321. }
  1322. void la_KnifeAppendCut(MSelectExtra* se){
  1323. MKnifeElement* ke=lstAppendPointerSized(&se->KnifeElements,se->PendingElem,sizeof(MKnifeElement));
  1324. ke->Type=se->PendingElemType;
  1325. }
  1326. int la_KnifeRegisterCuts(MSelectExtra* se, tnsMeshObject* mo, int TryClose){
  1327. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE) return 0;
  1328. if(!se->KnifeElements.pFirst) return 0;
  1329. tnsMVert* lastv=0,*newv=0,*firstv=0; tnsMEdge* newme=0; int changed=0; tnsMFace* mf=0;
  1330. for(MKnifeElement* ke=se->KnifeElements.pFirst;ke;ke=ke->Item.pNext){
  1331. if(ke->Type==TNS_MMESH_EDGE_BIT){
  1332. newv=tnsMMeshEdgeInsertVertAt(mo,ke->p,0.5,0,0,0); changed=1;
  1333. }
  1334. else{ newv=ke->p; }
  1335. if(lastv){ if(tnsMMeshVertsShareFace(lastv,newv)) newme=tnsMMeshMakeEdge(mo, lastv, newv); changed=1; }
  1336. lastv=newv; if(!firstv) firstv=newv; newv->flags|=TNS_MESH_FLAG_SELECTED;
  1337. if(newme){ newme->flags|=TNS_MESH_FLAG_SELECTED; }
  1338. }
  1339. if(TryClose){ if((mf=tnsMMeshVertsShareFace(lastv,firstv))&&mf->looplen==6) newme=tnsMMeshMakeEdge(mo, lastv, firstv); changed=1; }
  1340. if(changed){ tnsMMeshRefreshIndex(mo); tnsMMeshCalculateNormal(mo); }
  1341. return changed;
  1342. }
  1343. void la_KnifeFinish(MSelectExtra* se, tnsMeshObject*o){
  1344. if(o->ExtraBatch) tnsDeleteBatch(o->ExtraBatch); o->ExtraBatch=0;
  1345. while(lstPopPointer(&se->KnifeElements));
  1346. la_FreeSelectData(se->sd); memFree(se);
  1347. }
  1348. void la_KnifeRefreshLoopCuts(MSelectExtra* se, tnsObject* o,tnsMEdge* me){
  1349. if(!me) return;
  1350. tnsMMeshClearExtraFlags(o);
  1351. laListHandle lst={0}; lstAppendPointer(&lst,me); me->flags|=TNS_MESH_FLAG_PICKED;
  1352. tnsMMeshExpandBandList(o, me, &lst);
  1353. if(lst.pFirst==lst.pLast){ while(lstPopPointer(&lst)); return; }
  1354. tnsMEdge* mme; while((mme=lstPopPointer(&lst))){
  1355. MKnifeElement* ke=lstAppendPointerSized(&se->KnifeElements,mme,sizeof(MKnifeElement));
  1356. ke->Type=TNS_MMESH_EDGE_BIT;
  1357. }
  1358. }
  1359. void la_KnifeSortLoopCuts(MSelectExtra* se){
  1360. laListHandle lst={0};MKnifeElement* ke=se->KnifeElements.pFirst,*NextKe;
  1361. if(!ke) return;
  1362. lstRemoveItem(&se->KnifeElements,ke); lstAppendItem(&lst,ke);
  1363. for(ke=se->KnifeElements.pFirst;ke;ke=NextKe){
  1364. NextKe=ke->Item.pNext; tnsMEdge* me=ke->p,*firste=((MKnifeElement*)lst.pFirst)->p,*laste=((MKnifeElement*)lst.pLast)->p;
  1365. if(tnsMMeshEdgeShareFace(me,firste) && (!tnsMMeshEdgeShareVert(me,firste))){
  1366. lstRemoveItem(&se->KnifeElements,ke); lstPushItem(&lst,ke); ke=se->KnifeElements.pFirst; continue; }
  1367. if(tnsMMeshEdgeShareFace(me,laste) && (!tnsMMeshEdgeShareVert(me,laste))){
  1368. lstRemoveItem(&se->KnifeElements,ke); lstAppendItem(&lst,ke); ke=se->KnifeElements.pFirst; continue; }
  1369. }
  1370. if(se->KnifeElements.pFirst){ /* something went wrong */ while(lstPopPointer(&se->KnifeElements)); while(lstPopPointer(&lst)); }
  1371. se->KnifeElements.pFirst=lst.pFirst; se->KnifeElements.pLast=lst.pLast;
  1372. }
  1373. int OPINV_Knife(laOperator *a, laEvent *e){
  1374. if(!a->This || !a->This->EndInstance){ return 0; }
  1375. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1376. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1377. tnsMeshObject* mo=root->Active; int ran=0;
  1378. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1379. MSelectExtra* se=memAcquire(sizeof(MSelectExtra));
  1380. MSelectData* sd=la_InitSelectData(ex->OffScr->pColor[0]->Width, ex->OffScr->pColor[0]->Height, c);
  1381. a->CustomData=se; se->sd=sd; se->root=root;
  1382. int SelectMode=LA_CANVAS_SELECT_MODE_KNIFE;
  1383. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "loop_cut")){
  1384. se->IsLoop=1; SelectMode=LA_CANVAS_SELECT_MODE_EDGES; }
  1385. la_PopulateSelectDataPrimitives(sd,mo,c,SelectMode,ex->SelectThrough);
  1386. if(se->IsLoop){ strSafePrint(&a->RuntimeHint,"◧ Cut ◨ Cancel"); }
  1387. else{ strSafePrint(&a->RuntimeHint,"◧ Place Cut ◨ Cancel ⮨ Confirm"); }
  1388. return LA_RUNNING;
  1389. }
  1390. int OPMOD_Knife(laOperator *a, laEvent *e){
  1391. if(!a->This || !a->This->EndInstance || !a->CustomData){ return 0; }
  1392. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1393. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1394. MSelectExtra* se=a->CustomData;
  1395. tnsMeshObject* mo=root->Active;
  1396. int changed=0;
  1397. if(e->Type==LA_R_MOUSE_DOWN || (e->Type == LA_KEY_DOWN && e->key==LA_KEY_ESCAPE)){
  1398. la_KnifeFinish(se,mo); laNotifyUsers("tns.world"); return LA_FINISHED;
  1399. }
  1400. if(e->Type&LA_MOUSE_EVENT){
  1401. int elemtype,id=la_SelectGetClosest(se->sd, e->x-ui->L, e->y-ui->U, LA_RH,&elemtype)-1;
  1402. void* p=la_SelectGetRef(se->sd,id,elemtype);
  1403. if(se->IsLoop){
  1404. if(se->PendingElem!=p){ changed=1; while(lstPopPointer(&se->KnifeElements));
  1405. se->PendingElem=p; la_KnifeRefreshLoopCuts(se, mo, se->PendingElem);
  1406. la_KnifeSortLoopCuts(se);
  1407. }
  1408. }else{
  1409. if(la_KnifeIsDuplicated(se,p)) p=0;
  1410. if(se->PendingElem!=p){ changed=1; }
  1411. se->PendingElem=p; se->PendingElemType=elemtype;
  1412. if(e->Type==LA_L_MOUSE_DOWN && p){ la_KnifeAppendCut(se); changed=1; }
  1413. }
  1414. }
  1415. if((e->Type==LA_KEY_DOWN && e->key==LA_KEY_ENTER) || (se->IsLoop && e->Type==LA_L_MOUSE_DOWN)){
  1416. if(la_KnifeRegisterCuts(se,mo,se->IsLoop)){ tnsMMeshEnsureSelection(mo,ex->SelectMode);
  1417. tnsInvalidateMeshBatch(mo); laNotifyUsers("tns.world");
  1418. laRecordAndPush(0,"tns.world",se->IsLoop?"Loop Cut":"Knife Cut",TNS_HINT_GEOMETRY);
  1419. }
  1420. la_KnifeFinish(se,mo); return LA_FINISHED;
  1421. }
  1422. if(changed){
  1423. la_KnifeUpdateToolBatch(se,mo); laRedrawCurrentPanel();
  1424. }
  1425. return LA_RUNNING;
  1426. }
  1427. #define LA_MERGE_MODE_CENTER 0
  1428. #define LA_MERGE_MODE_FIRST 1
  1429. #define LA_MERGE_MODE_LAST 2
  1430. int la_MergeGetCenter(tnsMeshObject* mo, int SelectMode, int MergeMode, real* center){
  1431. real pos[3]={0},tmp[3]={0}; int count=0;
  1432. tnsMVert* usev=0; tnsMEdge* usee=0;
  1433. if(MergeMode!=LA_MERGE_MODE_CENTER){
  1434. if(MergeMode==LA_MERGE_MODE_FIRST){ usev=mo->FirstSelectV; usee=mo->FirstSelectE; }
  1435. elif(MergeMode==LA_MERGE_MODE_LAST){ usev=mo->LastSelectV; usee=mo->LastSelectE; }
  1436. if(SelectMode==LA_CANVAS_SELECT_MODE_VERTS && usev){ tnsVectorCopy3d(usev->p,center); return 1; }
  1437. elif(SelectMode==LA_CANVAS_SELECT_MODE_EDGES && usee){
  1438. tnsVectorAccum3d(pos,usee->vl->p);tnsVectorAccum3d(pos,usee->vr->p);
  1439. tnsVectorMultiSelf3d(pos,0.5f); tnsVectorCopy3d(pos,center); return 1;
  1440. }
  1441. }
  1442. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  1443. tnsVectorAccum3d(pos,mv->p); count++;
  1444. }
  1445. if(!count) return 0;
  1446. tnsVectorMultiSelf3d(pos,1.0f/count); tnsVectorCopy3d(pos,center);
  1447. return 1;
  1448. }
  1449. void la_MergeSelected(tnsMeshObject* mo, real* center, int* count_success, int* count_fail){
  1450. tnsMVert* fmv=0,*NextMv=0; int success=0,fail=0,has_success=0;
  1451. do{ fail=0; has_success=0;
  1452. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=NextMv){ NextMv=mv->Item.pNext;
  1453. if((!(mv->flags&TNS_MESH_FLAG_SELECTED))||mv==fmv) continue; if(!fmv){ fmv=mv; success++; continue; }
  1454. if(tnsMMeshMergeVerts(mo,fmv,mv)){ success++; has_success=1; }else{ fail++; }
  1455. }
  1456. }while(has_success&&fail);
  1457. if(count_success){ *count_success=success; } if(count_fail){ *count_fail=fail; }
  1458. if(!fmv) return; tnsVectorCopy3d(center,fmv->p);
  1459. tnsMMeshRefreshIndex(mo);
  1460. }
  1461. int OPINV_Merge(laOperator *a, laEvent *e){
  1462. if(!a->This || !a->This->EndInstance){ return 0; }
  1463. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1464. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1465. tnsMeshObject* mo=root->Active; int ran=0;
  1466. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1467. char* arg=strGetArgumentString(a->ExtraInstructionsP, "towards");
  1468. if(arg){
  1469. int MergeMode=LA_MERGE_MODE_CENTER; real center[3];
  1470. if(strSame(arg,"FIRST")){ MergeMode=LA_MERGE_MODE_FIRST; }
  1471. elif(strSame(arg,"LAST")){ MergeMode=LA_MERGE_MODE_LAST; }
  1472. if(!(la_MergeGetCenter(mo,ex->SelectMode,MergeMode,center))){
  1473. laEnableMessagePanel(a, 0, "Can't perform merging:", "No vertices selected.", e->x, e->y, 0, e);
  1474. return LA_FINISHED;
  1475. }
  1476. int success,fail;
  1477. la_MergeSelected(mo,center,&success,&fail); //printf("succeeded: %d failed:%d\n",success,fail);
  1478. if(fail){
  1479. char* msg[256]; sprintf(msg,"Succeeded: %d vertices, failed: %d vertices.",success,fail);
  1480. laEnableMessagePanel(a, 0, "Merging partially succeeded.", msg, e->x, e->y, 0, e);
  1481. }
  1482. if(success){
  1483. tnsMMeshEnsureSelection(mo,ex->SelectMode); tnsInvalidateMeshBatch(mo); laNotifyUsers("tns.world");
  1484. laRecordAndPush(0,"tns.world","Merge vertices",TNS_HINT_GEOMETRY);
  1485. }
  1486. }else{
  1487. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING;
  1488. }
  1489. return LA_FINISHED;
  1490. }
  1491. void laui_Merge(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  1492. laColumn* c=laFirstColumn(uil);
  1493. laShowItemFull(uil,c,pp,"_this_M_merge",0,"towards=CENTER;text=Center",0,0);
  1494. laShowItemFull(uil,c,pp,"_this_M_merge",0,"towards=FIRST;text=First",0,0);
  1495. laShowItemFull(uil,c,pp,"_this_M_merge",0,"towards=LAST;text=Last",0,0);
  1496. }
  1497. void la_RegisterModellingOperators(){
  1498. laPropContainer *pc; laProp *p;
  1499. laOperatorType *at;
  1500. laEnumProp *ep;
  1501. laCreateOperatorType("M_set_cursor", "Set Cursor", "Set cursor in the viewport", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_SetCursor, 0, 0, LA_EXTRA_TO_PANEL);
  1502. laCreateOperatorType("M_toggle_edit_mode", "Toggle Edit Mode", "Toggle edit mode of the active object", OPCHK_ThereIsActiveObject, 0, 0, OPINV_ToggleEdit, 0, 0, 0);
  1503. laCreateOperatorType("M_select", "Select", "Select things in the viewport", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Select, OPMOD_Select, 0, LA_EXTRA_TO_PANEL);
  1504. laCreateOperatorType("M_grab", "Grab", "Grab things and move around", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Grab, OPMOD_Transformation, 0, LA_EXTRA_TO_PANEL);
  1505. laCreateOperatorType("M_scale", "Scale", "Scale selected things", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Scale, OPMOD_Transformation, 0, LA_EXTRA_TO_PANEL);
  1506. laCreateOperatorType("M_rotate", "Rotate", "Rotation selected things", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Rotate, OPMOD_Transformation, 0, LA_EXTRA_TO_PANEL);
  1507. at=laCreateOperatorType("M_clear_transformations", "Clear Transformations", "Clear object transformations", 0, 0, 0, OPINV_ClearTransformation, OPMOD_FinishOnData, 0, 0);
  1508. at->UiDefine=laui_ClearTransformation;
  1509. at=laCreateOperatorType("M_make_parent", "Make Parent", "Parent objects to active objects or unparent selected ones", 0, 0, 0, OPINV_MakeParent, OPMOD_FinishOnData, 0, 0);
  1510. at->UiDefine = laui_MakeParent;
  1511. at=laCreateOperatorType("M_unparent", "Unparent", "Unparent selected objects", 0, 0, 0, OPINV_MakeParent, OPMOD_FinishOnData, 0, 0);
  1512. at->UiDefine = laui_Unparent;
  1513. laCreateOperatorType("M_extrude", "Extrude", "Extrude parts of the mesh", 0, 0, 0, OPINV_Extrude, OPMOD_Transformation, 0, 0);
  1514. at=laCreateOperatorType("M_delete", "Delete", "Delete parts of the mesh", 0, 0, 0, OPINV_Delete, OPMOD_FinishOnData, 0, 0);
  1515. at->UiDefine=laui_Delete;
  1516. laCreateOperatorType("M_make", "Make", "Make mesh primitive from selected ones", 0, 0, 0, OPINV_Make, 0, 0, 0);
  1517. laCreateOperatorType("M_subdiv", "Subdiv", "Subdivide edges", 0, 0, 0, OPINV_Subdiv, 0, 0, 0);
  1518. at=laCreateOperatorType("M_add", "Add", "Add mesh or primitives", 0, 0, 0, OPINV_Add, OPMOD_FinishOnData, 0, 0);
  1519. at->UiDefine=laui_Add; pc = laDefineOperatorProps(at, 2);
  1520. p=laAddEnumProperty(pc,"context","Context","Context of adding",0,0,0,0,0,offsetof(laObjectAddData,Context),0,0,0,0,0,0,0,0,0,0);
  1521. laAddEnumItemAs(p,"OBJECT","Object","Object context",LA_ADD_CTX_OBJECT,0);
  1522. laAddEnumItemAs(p,"MESH","Mesh","Mesh context",LA_ADD_CTX_MESH,0);
  1523. laCreateOperatorType("M_separate", "Separate", "Separate mesh parts", 0, 0, 0, OPINV_Separate, 0, 0, 0);
  1524. laCreateOperatorType("M_combine", "Combine", "Combine mesh objects", 0, 0, 0, OPINV_Combine, 0, 0, 0);
  1525. laCreateOperatorType("M_duplicate", "Duplicate", "Duplicate objects", 0, 0, 0, OPINV_Duplicate, OPMOD_Transformation, 0, 0);
  1526. laCreateOperatorType("M_recalculate_normals", "Recalculate Normals", "Recalculate normals", 0, 0, 0, OPINV_RecalculateNormals, 0, 0, 0);
  1527. laCreateOperatorType("M_knife", "Knife", "Cut through edges", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Knife, OPMOD_Knife, 0, LA_EXTRA_TO_PANEL);
  1528. at=laCreateOperatorType("M_merge", "Merge", "Merge vertices", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Merge, OPMOD_FinishOnData, 0, LA_EXTRA_TO_PANEL);
  1529. at->UiDefine=laui_Merge;
  1530. }