*/}}

la_modelling.c 84 KB

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