*/}}

la_modelling.c 94 KB

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