*/}}

la_tns_mesh.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 <math.h>
  20. extern tnsMain *T;
  21. tnsEdgeHash* tnsCreateEdgeHash(int OptionalInitialVertCount){
  22. tnsEdgeHash*eh=memAcquireSimple(sizeof(tnsEdgeHash));
  23. eh->max=OptionalInitialVertCount;
  24. if(eh->max<16) eh->max=16;
  25. arrInitLength(&eh->vl,eh->max,&eh->max, sizeof(tnsEdgeHashVert));
  26. return eh;
  27. }
  28. void tnsDestroyEdgeHash(tnsEdgeHash* eh){
  29. for(int i=0;i<eh->max;i++){
  30. tnsEdgeHashVert* ehv=&eh->vl[i];
  31. if(ehv->e) free(ehv->e);
  32. }
  33. if(eh->vl) free(eh->vl);
  34. memFree(eh);
  35. }
  36. void tnsEdgeHashAddVertPair(tnsEdgeHash* eh, int v1, int v2){
  37. if(v1==v2) return; if(v1>v2) LA_SWAP(int, v1,v2); if(v1>=eh->max) return;
  38. tnsEdgeHashVert* ehv=&eh->vl[v1];
  39. if(!ehv->e) arrInitLength(&ehv->e,1,&ehv->max, sizeof(tnsEdgeHashEdge));
  40. else arrEnsureLength(&ehv->e, ehv->next, &ehv->max, sizeof(tnsEdgeHashEdge));
  41. for(int i=0;i<ehv->next;i++){ if(ehv->e[i].tv==v2) return; }
  42. ehv->e[ehv->next].tv=v2;
  43. ehv->next++;
  44. }
  45. tnsEdgeHashEdge* tnsEdgeHashGetEdge(tnsEdgeHash* eh, int v1, int v2){
  46. if(v1==v2) return 0; if(v1>v2) LA_SWAP(int, v1,v2); if(v1>=eh->max) return 0;
  47. tnsEdgeHashVert* ehv=&eh->vl[v1];
  48. for(int i=0;i<ehv->next;i++){ if(ehv->e[i].tv==v2) return &ehv->e[i]; }
  49. return 0;
  50. }
  51. void tnsInitMesh(tnsMeshObject* mo, int Initialv, int Initiale, int Initialf){
  52. arrInitLength(&mo->v, Initialv, &mo->maxv, sizeof(tnsVert));
  53. arrInitLength(&mo->e, Initiale, &mo->maxe, sizeof(tnsEdge));
  54. arrInitLength(&mo->f, Initialf, &mo->maxf, sizeof(tnsFace));
  55. }
  56. tnsVert* tnsFillVertI(tnsMeshObject* mo, int index, real x, real y, real z){
  57. arrEnsureLength(&mo->v, index, &mo->maxv, sizeof(tnsVert));
  58. mo->v[index].p[0]=x;
  59. mo->v[index].p[1]=y;
  60. mo->v[index].p[2]=z;
  61. return &mo->v[index];
  62. }
  63. tnsVert* tnsFillVert(tnsMeshObject* mo, real x, real y, real z){
  64. int index=mo->totv; mo->totv++;
  65. return tnsFillVertI(mo, index, x,y,z);
  66. }
  67. tnsFace* tnsFillFace(tnsMeshObject* mo, int vertcount, ...){
  68. arrEnsureLength(&mo->f, mo->totf, &mo->maxf, sizeof(tnsFace));
  69. arrInitLength(&mo->f[mo->totf].loop, vertcount, &mo->f[mo->totf].looplen, sizeof(int));
  70. va_list list; va_start(list, vertcount); int id=va_arg(list, int); if(id==-1){ va_end(list); mo->totf++; return &mo->f[mo->totf-1]; }
  71. for(int i=0;i<vertcount;i++){ mo->f[mo->totf].loop[i]=id; id=va_arg(list, int); }
  72. va_end(list);
  73. mo->totf++; return &mo->f[mo->totf-1];
  74. }
  75. void tnsFillFaceLoop(tnsFace* f, int i, int v){ f->loop[i]=v; }
  76. void tnsFillFaceNormal(tnsFace* f, real* normal){ tnsVectorSet3v(f->n, normal); }
  77. void tnsFillVertNormal(tnsVert* v, real* normal){ tnsVectorSet3v(v->n, normal); }
  78. int tnsMergeMeshObjects(tnsMeshObject* into, tnsMeshObject* mo){
  79. if(into->Base.Type!=TNS_OBJECT_MESH||mo->Base.Type!=TNS_OBJECT_MESH) return 0;
  80. if(!mo->totv){ tnsDestroyObject(mo); return 1; }
  81. tnsVector3d v,vf; tnsMatrix44d inv; tnsInverse44d(inv, into->Base.GlobalTransform);
  82. for(int i=0;i<mo->totv;i++){
  83. tnsVectorSet3v(v,mo->v[i].p); tnsApplyTransform43d(vf, mo->Base.GlobalTransform, v);
  84. tnsApplyTransform43d(v, inv, vf); tnsVectorSet3v(mo->v[i].p,v);
  85. }
  86. for(int i=0;i<mo->tote;i++){ mo->e[i].l+=into->totv; mo->e[i].r+=into->totv; }
  87. for(int i=0;i<mo->totf;i++){ for(int l=0;l<mo->f[i].looplen;l++){ mo->f[i].loop[l]+=into->totv; } }
  88. int origv=into->totv, orige=into->tote, origf=into->totf;
  89. into->totv+=mo->totv; into->tote+=mo->tote; into->totf+=mo->totf;
  90. arrEnsureLength(&into->v, into->totv, &into->maxv, sizeof(tnsVert)); if(mo->totv) memcpy(&into->v[origv],mo->v,sizeof(tnsVert)*mo->totv);
  91. arrEnsureLength(&into->e, into->tote, &into->maxe, sizeof(tnsEdge)); if(mo->tote) memcpy(&into->e[orige],mo->e,sizeof(tnsEdge)*mo->tote);
  92. arrEnsureLength(&into->f, into->totf, &into->maxf, sizeof(tnsFace)); if(mo->totf) memcpy(&into->f[origf],mo->f,sizeof(tnsFace)*mo->totf);
  93. tnsDestroyObject(mo); return 1;
  94. }
  95. tnsMeshObject* tnsDuplicateMeshObjects(tnsMeshObject* from){
  96. if(from->Base.Type!=TNS_OBJECT_MESH) return 0;
  97. tnsMeshObject* to = memAcquireHyper(sizeof(tnsMeshObject));
  98. tnsInitObjectBase(to, from->Base.ParentObject?from->Base.ParentObject:from->Base.InRoot, from->Base.Name->Ptr, TNS_OBJECT_MESH,0,0,0,0,0,0,0,0,1);
  99. tnsCopyObjectTransformationsLocal(to,from);
  100. if(!from->totv){ return to; }
  101. to->totv=from->totv; to->tote=from->tote; to->totf=from->totf;
  102. arrInitLength(&to->v, to->totv, &to->maxv, sizeof(tnsVert)); if(from->totv) memcpy(to->v,from->v,sizeof(tnsVert)*from->totv);
  103. arrInitLength(&to->e, to->tote, &to->maxe, sizeof(tnsEdge)); if(from->tote) memcpy(to->e,from->e,sizeof(tnsEdge)*from->tote);
  104. arrInitLength(&to->f, to->totf, &to->maxf, sizeof(tnsFace)); if(from->totf) memcpy(to->f,from->f,sizeof(tnsFace)*from->totf);
  105. for(int i=0;i<to->totf;i++){ to->f[i].loop=0; arrInitLength(&to->f[i].loop, to->f[i].looplen, &to->f[i].looplen, sizeof(int));
  106. for(int l=0;l<to->f[i].looplen;l++){ to->f[i].loop[l]=from->f[i].loop[l]; }
  107. }
  108. return to;
  109. }
  110. void tnsInitMeshPlane(tnsMeshObject* mo, real size){
  111. tnsInitMesh(mo, 4,0,1);
  112. tnsFillVert(mo, size, size,0); tnsFillVert(mo,-size, size,0);
  113. tnsFillVert(mo,-size,-size,0); tnsFillVert(mo, size,-size,0);
  114. tnsFace* f=tnsFillFace(mo, 4, 0,1,2,3); tnsVector3d n={0,0,1}; tnsFillFaceNormal(f,n);
  115. mo->v[0].flags|=TNS_MESH_FLAG_SELECTED;
  116. mo->v[1].flags|=TNS_MESH_FLAG_SELECTED;
  117. mo->v[2].flags|=TNS_MESH_FLAG_SELECTED;
  118. mo->v[3].flags|=TNS_MESH_FLAG_SELECTED;
  119. tnsMMeshEnsureSelectionFromVerts(mo);
  120. }
  121. void tnsAddMMeshPlane(tnsMeshObject* mo, real size){
  122. tnsMVert *mv1,*mv2,*mv3,*mv4;
  123. mv1=tnsMMeshNewVert(mo); mv1->p[0]=size; mv1->p[1]=size; mv1->flags|=TNS_MESH_FLAG_SELECTED;
  124. mv2=tnsMMeshNewVert(mo); mv2->p[0]=-size; mv2->p[1]=size; mv2->flags|=TNS_MESH_FLAG_SELECTED;
  125. mv3=tnsMMeshNewVert(mo); mv3->p[0]=-size; mv3->p[1]=-size; mv3->flags|=TNS_MESH_FLAG_SELECTED;
  126. mv4=tnsMMeshNewVert(mo); mv4->p[0]=size; mv4->p[1]=-size; mv4->flags|=TNS_MESH_FLAG_SELECTED;
  127. tnsMMeshMakeFace4v(mo,mv1,mv2,mv3,mv4);
  128. }
  129. void tnsTrangulateFaceSimple(tnsMeshObject* mo, int fi, tnsFace* f, int* ebuf){
  130. for(int i=0;i<f->looplen-2;i++){
  131. ebuf[i*3]=f->loop[i];
  132. ebuf[i*3+1]=f->loop[i+1];
  133. ebuf[i*3+2]=fi+mo->totv; //f->loop[i+2];
  134. }
  135. }
  136. void tnsTrangulateFaceSimpleM(tnsMeshObject* mo, int fi, tnsMFace* mf, int* ebuf){
  137. tnsMVert* mv=0,*mvs; int i=0;
  138. for(laListItemPointer* lip=mf->l.pFirst;lip;lip=lip->pNext){
  139. laListItemPointer* next=lip->pNext; if(!next) next=mf->l.pFirst; tnsMEdge* me0=lip->p, *me1=next->p;
  140. if(next==mf->l.pLast){ break; }
  141. mvs=tnsMMeshEdgeStartingVert(me0,me1);
  142. if(!mv) mv=mvs;
  143. ebuf[i*3]=mvs->i; tnsMVert*mm=tnsMMeshEdgeAnotherVert(me0,mvs);
  144. ebuf[i*3+1]=mm->i;
  145. ebuf[i*3+2]=fi+mo->totmv; /*tnsMMeshEdgeAnotherVert(me1,mm)->i;*/ i++;
  146. }
  147. }
  148. tnsMVert* tnsGetMFaceLastVert(tnsMFace* mf){
  149. laListItemPointer* lip=mf->l.pLast; laListItemPointer* next=mf->l.pFirst; tnsMEdge* me0=lip->p, *me1=next->p;
  150. return tnsMMeshEdgeStartingVert(me0,me1);
  151. }
  152. int* tnsGetTriangulatedBatch(tnsMeshObject* mo, int* totelem){
  153. int tottri=0;
  154. if(mo->Mode==TNS_MESH_OBJECT_MODE){ for(int i=0;i<mo->totf;i++){ tottri+=(mo->f[i].looplen-2); } }
  155. else{ for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ tottri+=mf->looplen-2; } }
  156. if(!tottri) return 0;
  157. int* ebuf=calloc(1,sizeof(int)*tottri*3); int* pebuf=ebuf;
  158. if(mo->Mode==TNS_MESH_OBJECT_MODE){ for(int i=0;i<mo->totf;i++){ tnsTrangulateFaceSimple(mo, i, &mo->f[i], pebuf); pebuf+=(mo->f[i].looplen-2)*3; } }
  159. else{ int i=0; for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ tnsTrangulateFaceSimpleM(mo, i, mf, pebuf); pebuf+=(mf->looplen-2)*3; i++; } }
  160. *totelem = tottri;
  161. return ebuf;
  162. }
  163. float* tnsGetDrawingVertArray(tnsMeshObject* mo, int* r_tot_render_v, float** r_normals, float** idcolors, float** editcolors, int** edgeelems, int* r_tote){
  164. if(!mo->totv&&!mo->totmv) return 0;
  165. int objmode=mo->Mode==TNS_MESH_OBJECT_MODE;
  166. int totv=objmode?mo->totv:mo->totmv;
  167. int tote=objmode?mo->tote:mo->totme; *r_tote=tote;
  168. int totf=objmode?mo->totf:mo->totmf;
  169. int extraverts=objmode?mo->totf:(mo->totme*2+mo->totmf);
  170. float* p=calloc(1,(totv+extraverts)*3*sizeof(float)); *r_tot_render_v=(totv+extraverts);
  171. float* n=calloc(1,(totv+extraverts)*3*sizeof(float)); *r_normals = n;
  172. if(objmode){
  173. for(int i=0;i<totv;i++){ tnsVectorSet3v(&p[i*3],mo->v[i].p); tnsVectorSet3v(&n[i*3],mo->v[i].n); }
  174. int start=mo->totv*3; for(int i=0;i<totf;i++){
  175. tnsVectorSet3v(&p[start+i*3],mo->v[mo->f[i].loop[mo->f[i].looplen-1]].p); tnsVectorSet3v(&n[start+i*3],mo->f[i].n);
  176. }
  177. }else{
  178. (*idcolors)=calloc(1,(totv+extraverts)*3*sizeof(float));
  179. (*edgeelems)=calloc(1,(extraverts)*2*sizeof(int));
  180. (*editcolors)=calloc(1,totv*4*sizeof(float)*extraverts);
  181. for(tnsMVert*mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ int i=mv->i;
  182. tnsVectorSet3v(&p[i*3],mv->p); tnsVectorSet3v(&n[i*3],mv->n);
  183. int id=i+1; real r=(real)((id & 0x000000FF)>>0)/255.0; real g=(real)((id & 0x0000FF00)>>8)/255.0; real b=(real)((id & 0x00FF0000)>>16)/255.0;
  184. (*idcolors)[i*3]=r; (*idcolors)[i*3+1]=g; (*idcolors)[i*3+2]=b;
  185. real* c=(mv->flags&TNS_MESH_FLAG_SELECTED)?laAccentColor(LA_BT_SVERTEX):laAccentColor(LA_BT_VERTEX);
  186. (*editcolors)[i*4]=c[0]; (*editcolors)[i*4+1]=c[1]; (*editcolors)[i*4+2]=c[2]; (*editcolors)[i*4+3]=c[3];
  187. }
  188. for(tnsMEdge*me=mo->me.pFirst;me;me=me->Item.pNext){ int ei=me->i;
  189. (*edgeelems)[ei*2]=mo->totmv+ei*2; (*edgeelems)[ei*2+1]=mo->totmv+ei*2+1;
  190. float* eidcolor1=&(*idcolors)[(*edgeelems)[ei*2]*3], *eidcolor2=&(*idcolors)[(*edgeelems)[ei*2+1]*3];
  191. int id=ei+1; real r=(real)((id & 0x000000FF)>>0)/255.0; real g=(real)((id & 0x0000FF00)>>8)/255.0; real b=(real)((id & 0x00FF0000)>>16)/255.0;
  192. tnsVectorSet3(eidcolor1,r,g,b); tnsVectorSet3(eidcolor2,r,g,b);
  193. int se1=(*edgeelems)[ei*2]; tnsVectorSet3v(&p[se1*3],me->vl->p);
  194. int se2=(*edgeelems)[ei*2+1]; tnsVectorSet3v(&p[se2*3],me->vr->p);
  195. float* eedcolor1=&(*editcolors)[(*edgeelems)[ei*2]*4], *eedcolor2=&(*editcolors)[(*edgeelems)[ei*2+1]*4];
  196. real* c=(me->flags&TNS_MESH_FLAG_SELECTED)?laAccentColor(LA_BT_SEDGE):laAccentColor(LA_BT_EDGE);
  197. tnsVectorSet4v(eedcolor1,c); tnsVectorSet4v(eedcolor2,c);
  198. }
  199. int start=mo->totmv*3; int fi=0; for(tnsMFace*mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){
  200. tnsMVert* sv=tnsGetMFaceLastVert(mf); tnsVectorSet3v(&p[start+fi*3],sv->p); tnsVectorSet3v(&n[start+fi*3],mf->n); fi++;
  201. }
  202. }
  203. return p;
  204. }
  205. int* tnsGetEdgeBatch(tnsMeshObject* mo){
  206. if(!mo->totme) return 0;
  207. int* ebuf=calloc(1,sizeof(int)*mo->totme*2); int* pebuf=ebuf; int i=0;
  208. for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ ebuf[i*2]=me->vl->i; ebuf[i*2+1]=me->vr->i; i++; }
  209. return ebuf;
  210. }
  211. void tnsInvalidateMeshBatch(tnsMeshObject* mo){
  212. if(mo->Base.Type!=TNS_OBJECT_MESH) return;
  213. if(mo->Batch) tnsDeleteBatch(mo->Batch); mo->Batch=0;
  214. }
  215. void tnsRegenerateMeshBatch(tnsMeshObject* mo){
  216. if(!mo) return;
  217. if(mo->Batch) tnsDeleteBatch(mo->Batch); mo->Batch=0;
  218. real meshcolor[4]={0.8,0.8,0.8,1};
  219. int tottri; int* elem = tnsGetTriangulatedBatch(mo, &tottri);
  220. float* idcolors=0,*editcolors=0; int docolors=mo->Mode==TNS_MESH_EDIT_MODE;
  221. int totv,tote; int* eelems=0; float* n=0;
  222. float* v = tnsGetDrawingVertArray(mo,&totv,&n,&idcolors,&editcolors,&eelems,&tote);
  223. if(!v){ if(elem){free(elem);} if(eelems){free(eelems);} return; }
  224. mo->Batch = tnsCreateBatch(totv, 3, v, 3, n, 4, editcolors);
  225. tnsBatchCommand*c=tnsCreateCommand(mo->Batch, "body", tottri, 3, GL_TRIANGLES, elem, 0);
  226. tnsCommandUseUniformColor(c,meshcolor);
  227. free(elem); free(v); free(n);
  228. if(mo->Mode==TNS_MESH_EDIT_MODE){
  229. elem=tnsGetEdgeBatch(mo); if(elem) {
  230. c= tnsCreateCommand(mo->Batch, "lines", mo->totme, 2, GL_LINES, elem, 1); free(elem);
  231. //tnsCommandUseUniformColor(c, laAccentColor(LA_BT_EDGE));
  232. }
  233. c= tnsCreateCommand(mo->Batch, "verts", mo->totmv, 3, GL_POINTS, 0, 1);
  234. c= tnsCreateCommand(mo->Batch, "verts_select", mo->totmv, 3, GL_POINTS, 0, 1);
  235. tnsCommandOverrideColorArray(c, mo->Batch->NumVert, 3, idcolors);
  236. c= tnsCreateCommand(mo->Batch, "edges_select", mo->totme, 2, GL_LINES, eelems, 1);
  237. tnsCommandOverrideColorArray(c, mo->Batch->NumVert, 3, idcolors);
  238. c= tnsCreateCommand(mo->Batch, "edges", mo->totme, 2, GL_LINES, eelems, 1);
  239. tnsCommandOverrideColorArray(c, mo->Batch->NumVert, 4, editcolors);
  240. //for(int i=0;i<mo->totme*2;i++){ printf("%d ",eelems[i]); } printf("\n");
  241. }
  242. if(idcolors) free(idcolors); if(editcolors) free(editcolors); if(eelems) free(eelems);
  243. }
  244. void tnsEnsureMeshBatch(tnsMeshObject* mo){
  245. if(mo->Base.Type!=TNS_OBJECT_MESH) return;
  246. if(mo->Batch) return;
  247. tnsRegenerateMeshBatch(mo);
  248. }
  249. void tnsDrawMeshObject(tnsMeshObject* mo, int DrawAsObjectSelection, int MeshSelectionMode, tnsMeshObject* Active){
  250. tnsEnsureMeshBatch(mo);
  251. if(DrawAsObjectSelection){
  252. if(mo->Base.Flags&TNS_OBJECT_FLAGS_SELECTED && mo->Mode!=TNS_MESH_EDIT_MODE){
  253. real* color=(Active==mo)?laAccentColor(LA_BT_TEXT):laAccentColor(LA_BT_NORMAL);
  254. tnsDrawBatch(mo->Batch, "body", color, 0);
  255. }
  256. }else{
  257. if(T->BindedShader==T->SelectionShader){
  258. int i=mo->Base.SelectID; real color[4]={0,0,0,1}; TNS_ID_TO_COLOR(color,i);
  259. tnsDrawBatch(mo->Batch,"body",color,0);
  260. }else{
  261. tnsUseNormal(1);
  262. tnsDrawBatch(mo->Batch,"body",0,0);
  263. tnsUseNormal(0);
  264. if(mo->Mode==TNS_MESH_EDIT_MODE){
  265. if(MeshSelectionMode==LA_CANVAS_SELECT_MODE_VERTS){
  266. tnsDrawBatch(mo->Batch,"verts",0,0); tnsDrawBatch(mo->Batch,"lines",0,0);
  267. }else{
  268. tnsDrawBatch(mo->Batch,"edges",0,0);
  269. }
  270. }
  271. }
  272. }
  273. }
  274. tnsMFace* tnsMMeshNewFace(tnsMeshObject* mo){ tnsMFace* mf=memAcquireSimple(sizeof(tnsMFace)); mf->i=mo->totmf; mo->totmf++; lstAppendItem(&mo->mf,mf); return mf; }
  275. tnsMEdge* tnsMMeshNewEdge(tnsMeshObject* mo){ tnsMEdge* me=memAcquireSimple(sizeof(tnsMEdge)); me->i=mo->totme; mo->totme++; lstAppendItem(&mo->me,me); return me; }
  276. tnsMVert* tnsMMeshNewVert(tnsMeshObject* mo){ tnsMVert* mv=memAcquireSimple(sizeof(tnsMVert)); mv->i=mo->totmv; mo->totmv++; lstAppendItem(&mo->mv,mv); return mv; }
  277. void tnsMMeshCalculateNormalFrom(tnsMFace* mf){
  278. tnsVector3d accum={0}; tnsVector3d d0,d1; tnsVector3d naccum={0},nn={0};if(mf->flags&TNS_MESH_FLAG_PICKED) return;
  279. for(laListItemPointer* lip=mf->l.pFirst;lip;lip=lip->pNext){
  280. laListItemPointer* next=lip->pNext; if(!next) next=mf->l.pFirst; tnsMEdge* me0=lip->p, *me1=next->p;
  281. tnsMVert* v0=tnsMMeshEdgeStartingVert(me0,me1); tnsMVert* v1=tnsMMeshEdgeShareVert(me0,me1); tnsMVert* v2=tnsMMeshEdgeAnotherVert(me1, v1);
  282. tnsVectorAccum3d(accum, v0->p); tnsVectorAccum3d(accum, v1->p); tnsVectorAccum3d(accum, v2->p);
  283. tnsVectorMinus3d(d0,v0->p,v1->p); tnsVectorMinus3d(d1,v2->p,v1->p); real len=tnsVectorCross3d(nn,d0,d1); tnsVectorMultiSelf3d(nn, len);
  284. tnsVectorAccum3d(naccum, nn);
  285. //if(v0==me0->vr){ me0->flags|=TNS_MESH_FLAG_LOOP_REVERSE; } // TODO: consistency not implemented.
  286. }
  287. tnsNormalize3d(mf->n, naccum); tnsVectorMulti3d(mf->c, accum, 1.0/(mf->looplen+2));
  288. mf->flags|=TNS_MESH_FLAG_PICKED;
  289. if(mf->flags&TNS_MESH_FLAG_SELECTED){
  290. for(laListItemPointer* lip=mf->l.pFirst;lip;lip=lip->pNext){
  291. tnsMEdge* me=lip->p; tnsMFace* of=((mf==me->fl)?me->fr:me->fl);
  292. if(of) tnsMMeshCalculateNormalFrom(of);
  293. }
  294. }
  295. }
  296. void tnsMMeshCalculateFaceVertNormal(tnsMFace* mf){
  297. tnsVector3d accum={0};
  298. for(laListItemPointer* lip=mf->l.pFirst;lip;lip=lip->pNext){
  299. laListItemPointer* next=lip->pNext; if(!next) next=mf->l.pFirst; tnsMEdge* me0=lip->p, *me1=next->p;
  300. tnsMVert* mv=tnsMMeshEdgeStartingVert(me0,me1); int fcount=0;
  301. for(laListItemPointer* el=mv->elink.pFirst;el;el=el->pNext){ tnsMEdge* ve=lip->p;
  302. if(ve->fl){ tnsVectorAccum3d(accum, ve->fl->n); fcount++; }
  303. if(ve->fr){ tnsVectorAccum3d(accum, ve->fr->n); fcount++; }
  304. }
  305. if(!fcount) accum[2]=1; else tnsNormalizeSelf3d(accum);
  306. tnsVectorSet3v(mv->n, accum);
  307. }
  308. }
  309. int tnsMMeshCalculateNormal(tnsMeshObject* mo){
  310. tnsMMeshClearExtraFlags(mo); int ran=0;
  311. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ if((!(mf->flags&TNS_MESH_FLAG_SELECTED))||(mf->flags&TNS_MESH_FLAG_PICKED)) continue;
  312. tnsMMeshCalculateNormalFrom(mf); ran=1;
  313. }
  314. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ if(!(mf->flags&TNS_MESH_FLAG_PICKED)) continue;
  315. tnsMMeshCalculateFaceVertNormal(mf); ran=1;
  316. }
  317. return ran;
  318. }
  319. void tnsMMeshEdgeAssignVerts(tnsMEdge* me,tnsMVert* mv1,tnsMVert* mv2){
  320. if(me->vl||me->vr){ return; } //if((me->vl==mv1&&me->vr=mv2) || (me->vl==mv2&&me->vr=mv1))
  321. me->vl=mv1; me->vr=mv2;
  322. lstAppendPointer(&me->vl->elink,me); lstAppendPointer(&me->vr->elink,me);
  323. }
  324. tnsMEdge* tnsMMeshVertShareEdge(tnsMVert* mv0, tnsMVert* mv1){
  325. for(laListItemPointer*lip=mv0->elink.pFirst;lip;lip=lip->pNext){ tnsMEdge* me=lip->p; if(tnsMMeshEdgeAnotherVert(me, mv0)==mv1) return me; } return 0;
  326. }
  327. int tnsMMeshEdgeHasVert(tnsMEdge* me, tnsMVert* mv){
  328. if(me->vl==mv || me->vr==mv) return 1; return 0;
  329. }
  330. tnsMVert* tnsMMeshEdgeShareVert(tnsMEdge* me0, tnsMEdge* me1){
  331. if(me0->vl==me1->vl || me0->vl==me1->vr) return me0->vl;
  332. if(me0->vr==me1->vl || me0->vr==me1->vr) return me0->vr;
  333. return 0;
  334. }
  335. tnsMVert* tnsMMeshEdgeAnotherVert(tnsMEdge* me, tnsVert* v){
  336. if(me->vl==v) return me->vr; if(me->vr==v) return me->vl;
  337. return 0;
  338. }
  339. tnsMVert* tnsMMeshEdgeStartingVert(tnsMEdge* me0, tnsMEdge* me1){
  340. tnsMVert* sv=tnsMMeshEdgeShareVert(me0,me1); if(!sv) return 0;
  341. return tnsMMeshEdgeAnotherVert(me0, sv);
  342. }
  343. void tnsMMeshFaceAddEdge(tnsMFace* mf, tnsMEdge* me){
  344. lstAppendPointer(&mf->l, me); mf->looplen++;
  345. if(!me->fl) me->fl=mf; elif(!me->fr) me->fr=mf;
  346. }
  347. tnsMFace* tnsMMeshFaceHasVert(tnsMFace* mf, tnsMVert* mv){
  348. if(!mf||!mv) return 0; for(laListItemPointer*lip=mf->l.pFirst;lip;lip=lip->pNext){ if(tnsMMeshEdgeAnotherVert(lip->p, mv)) return mf; }
  349. return 0;
  350. }
  351. tnsMFace* tnsMMeshVertsShareFace(tnsMVert* v1, tnsMVert* v2){
  352. tnsMFace* mf=0; for(laListItemPointer*lip=v1->elink.pFirst;lip;lip=lip->pNext){
  353. tnsMEdge* me=lip->p; if((mf=tnsMMeshFaceHasVert(me->fl, v2)) || (mf=tnsMMeshFaceHasVert(me->fr, v2))) return mf;
  354. } return 0;
  355. }
  356. int tnsMMeshSplitFace(tnsMeshObject* mo, tnsMFace* mf, tnsMEdge* me, tnsMFace** r_f1, tnsMFace** r_f2){
  357. tnsMEdge* NextE; laListItemPointer* NextLip, *StartLip=0, *EndLip=0; int guard=0;
  358. for(laListItemPointer*lip=mf->l.pFirst;lip;lip=NextLip){
  359. NextLip=lip->pNext?lip->pNext:mf->l.pFirst; NextE=NextLip->p;
  360. if(tnsMMeshEdgeHasVert(me,tnsMMeshEdgeShareVert(NextE,lip->p))){
  361. if(!StartLip) StartLip=NextLip;
  362. else{EndLip=NextLip; break;} }
  363. guard++; if(guard>mf->looplen) return 0; // me is not across mf.
  364. }
  365. tnsMFace* f1=tnsMMeshNewFace(mo);
  366. for(laListItemPointer*lip=StartLip;lip;lip=NextLip){ NextLip=lip->pNext?lip->pNext:mf->l.pFirst;
  367. if(lip==EndLip){ tnsMMeshFaceAddEdge(f1, me); break; } tnsMMeshFaceAddEdge(f1, lip->p);
  368. }
  369. tnsMFace* f2=tnsMMeshNewFace(mo);
  370. for(laListItemPointer*lip=EndLip;lip;lip=NextLip){ NextLip=lip->pNext?lip->pNext:mf->l.pFirst;
  371. if(lip==StartLip){ tnsMMeshFaceAddEdge(f2, me); break; } tnsMMeshFaceAddEdge(f2, lip->p);
  372. }
  373. tnsMMeshRemoveFaceOnly(mo, mf);
  374. if(r_f1){ *r_f1=f1; } if(r_f2){ *r_f2=f2; }
  375. return 1;
  376. }
  377. tnsMEdge* tnsMMeshMakeEdge(tnsMeshObject* mo, tnsMVert* v1, tnsMVert* v2){
  378. for(laListItemPointer*lip=v1->elink.pFirst;lip;lip=lip->pNext){ if(tnsMMeshEdgeAnotherVert(lip->p, v1)==v2) return lip->p; }
  379. // for(laListItemPointer*lip=v2->elink.pFirst;lip;lip=lip->pNext){ if(tnsMMeshEdgeAnotherVert(lip->p, v2)==v1) return lip->p; } shouldn't need.
  380. tnsMFace* mf=tnsMMeshVertsShareFace(v1,v2);
  381. tnsMEdge* me=tnsMMeshNewEdge(mo); tnsMMeshEdgeAssignVerts(me, v1, v2);
  382. if(mf){ tnsMMeshSplitFace(mo, mf, me, 0,0); }
  383. return me;
  384. }
  385. int tnsMMeshFaceMatchesN(tnsMFace* mf, int ecount, laListHandle* eip){
  386. if(!mf||mf->looplen!=ecount) return 0; laListItemPointer* lipe=eip->pFirst;
  387. for(int i=0;i<ecount;i++){
  388. tnsMEdge* me=lipe->p; int found=0; lipe=lipe->pNext;
  389. for(laListItemPointer* lip=mf->l.pFirst;lip;lip=lip->pNext){
  390. if(lip->p==me){ found=1; break; }
  391. }
  392. if(!found){ return 0; }
  393. }
  394. return 1;
  395. }
  396. int tnsMMeshFaceMatches(tnsMFace* mf, int ecount, ...){
  397. if(!mf||mf->looplen!=ecount) return 0;
  398. va_list list; va_start(list, ecount);
  399. for(int i=0;i<ecount;i++){
  400. tnsMEdge* me=va_arg(list, tnsMEdge*); int found=0;
  401. for(laListItemPointer* lip=mf->l.pFirst;lip;lip=lip->pNext){
  402. if(lip->p==me){ found=1; break; }
  403. }
  404. if(!found){ va_end(list); return 0; }
  405. }
  406. va_end(list); return 1;
  407. }
  408. tnsMFace* tnsMMeshMakeFaceN(tnsMeshObject* mo, int count, laListHandle* vip, tnsMEdge** r_fallback_me){
  409. if(count<2){ return 0; }
  410. if(count==2){ tnsMEdge* me=tnsMMeshMakeEdge(mo,((laListItemPointer*)vip->pFirst)->p,((laListItemPointer*)vip->pLast)->p); if(r_fallback_me)*r_fallback_me=me; return 0; }
  411. laListHandle el={0};
  412. for(laListItemPointer*lip=vip->pFirst;lip;lip=lip->pNext){
  413. tnsMVert* mv=lip->p; laListItemPointer*nlip=lip->pNext?((laListItemPointer*)lip->pNext):vip->pFirst; tnsMVert* nextmv=nlip->p;
  414. lstAppendPointer(&el,tnsMMeshMakeEdge(mo,mv,nextmv));
  415. }
  416. tnsMEdge* e1=((laListItemPointer*)el.pFirst)->p;
  417. if(tnsMMeshFaceMatchesN(e1->fl, count, &el))return e1->fl; if(tnsMMeshFaceMatchesN(e1->fr, count, &el))return e1->fr;
  418. for(laListItemPointer* lip=el.pFirst;lip;lip=lip->pNext){ tnsMEdge*me=lip->p; if(me->fl&&me->fr) return 0; }
  419. tnsMFace* mf=tnsMMeshNewFace(mo);
  420. for(laListItemPointer* lip=el.pFirst;lip;lip=lip->pNext){ tnsMEdge*me=lip->p; tnsMMeshFaceAddEdge(mf, me); }
  421. while(lstPopPointer(&el));
  422. return mf;
  423. }
  424. tnsMFace* tnsMMeshMakeFace4v(tnsMeshObject* mo, tnsVert* v1,tnsVert* v2,tnsVert* v3,tnsVert* v4){
  425. tnsMEdge* e1=tnsMMeshMakeEdge(mo,v1,v2); tnsMEdge* e2=tnsMMeshMakeEdge(mo,v2,v3);
  426. tnsMEdge* e3=tnsMMeshMakeEdge(mo,v3,v4); tnsMEdge* e4=tnsMMeshMakeEdge(mo,v4,v1);
  427. if(tnsMMeshFaceMatches(e1->fl,4,e1,e2,e3,e4)) return e1->fl; if(tnsMMeshFaceMatches(e1->fr,4,e1,e2,e3,e4)) return e1->fr; //should not need more
  428. if((e1->fl&&e1->fr) || (e2->fl&&e2->fr) || (e3->fl&&e3->fr) || (e4->fl&&e4->fr)) return 0;
  429. tnsMFace* mf=tnsMMeshNewFace(mo);
  430. tnsMMeshFaceAddEdge(mf,e1); tnsMMeshFaceAddEdge(mf,e2);
  431. tnsMMeshFaceAddEdge(mf,e3); tnsMMeshFaceAddEdge(mf,e4);
  432. return mf;
  433. }
  434. int tnsMMeshLoopIsInverted(laListItemPointer* l){
  435. tnsMEdge* me=l->p; if(l->pNext){ tnsMEdge*next=((laListItemPointer*)l->pNext)->p; if(me->vr==tnsMMeshEdgeShareVert(me,next))return 0; return 1; }
  436. else{ tnsMEdge*prev=((laListItemPointer*)l->pPrev)->p; if(me->vl==tnsMMeshEdgeShareVert(prev,me))return 0; return 1; }
  437. }
  438. int tnsMMeshEdgeInsertVert(tnsMeshObject* mo, tnsMEdge* me, tnsMVert* mv, tnsMVert* ref_e1v_optional, tnsMEdge** r_e1, tnsMEdge** r_e2){
  439. if(mv->elink.pFirst||mv->elink.pLast||me->vl==mv||me->vl==mv) return 0;
  440. tnsMEdge* me1=tnsMMeshNewEdge(mo),*me2=tnsMMeshNewEdge(mo);
  441. me1->fl=me2->fl=me->fl; me1->fr=me2->fr=me->fr;
  442. tnsMMeshEdgeAssignVerts(me1,me->vl,mv); tnsMMeshEdgeAssignVerts(me2,mv,me->vr);
  443. laListItemPointer* lipa1=memAcquireSimple(sizeof(laListItemPointer)),*lipa2=memAcquireSimple(sizeof(laListItemPointer)); lipa1->p=me1; lipa2->p=me2;
  444. laListItemPointer* lipb1=memAcquireSimple(sizeof(laListItemPointer)),*lipb2=memAcquireSimple(sizeof(laListItemPointer)); lipb1->p=me1; lipb2->p=me2;
  445. if(me->fl) for(laListItemPointer* lip=me->fl->l.pFirst;lip;lip=lip->pNext){
  446. tnsMEdge*ie=lip->p; if(ie!=me) continue; if(tnsMMeshLoopIsInverted(lip)){ LA_SWAP(laListItemPointer*,lipa1,lipa2); }
  447. lstInsertItemBefore(&me->fl->l,lipa1,lip); lstInsertItemBefore(&me->fl->l,lipa2,lip); lstRemoveItem(&me->fl->l, lip); memLeave(lip); me->fl->looplen++;
  448. }
  449. if(me->fr) for(laListItemPointer* lip=me->fr->l.pFirst;lip;lip=lip->pNext){
  450. tnsMEdge*ie=lip->p; if(ie!=me) continue; if(tnsMMeshLoopIsInverted(lip)){ LA_SWAP(laListItemPointer*,lipb1,lipb2); }
  451. lstInsertItemBefore(&me->fr->l,lipb1,lip); lstInsertItemBefore(&me->fr->l,lipb2,lip); lstRemoveItem(&me->fr->l, lip); memLeave(lip); me->fr->looplen++;
  452. }
  453. me->fl=me->fr=0; tnsMMeshRemoveEdgeFace(mo,me);
  454. if(ref_e1v_optional&&tnsMMeshEdgeShareVert(me1, ref_e1v_optional)){ if(r_e1)*r_e1=me1; if(r_e2)*r_e2=me2; }else{ if(r_e1)*r_e1=me2; if(r_e2)*r_e2=me1; }
  455. return 1;
  456. }
  457. tnsMVert* tnsMMeshEdgeInsertVertAt(tnsMeshObject* mo, tnsMEdge* me, real at, tnsMVert* ref_e1v_optional, tnsMEdge** r_e1, tnsMEdge** r_e2){
  458. if(!me||at<=0||at>=1) return 0; tnsMVert* mv=tnsMMeshNewVert(mo);
  459. tnsInterpolate3dv(me->vl->p, me->vr->p, at, mv->p);
  460. if(tnsMMeshEdgeInsertVert(mo,me,mv,ref_e1v_optional,r_e1,r_e2)) return mv; return 0;
  461. }
  462. void tnsMMeshRemoveFaceOnly(tnsMeshObject* mo, tnsMFace* mf){
  463. if(!mf) return; tnsMEdge* me;
  464. while(me=lstPopPointerLeave(&mf->l)){ if(me->fl==mf) me->fl=0; elif(me->fr==mf) me->fr=0; }
  465. lstRemoveItem(&mo->mf,mf); memLeave(mf); mo->totmf--;
  466. }
  467. void tnsMMeshRemoveEdgeFace(tnsMeshObject* mo, tnsMEdge* me){
  468. if(!me) return;
  469. tnsMMeshRemoveFaceOnly(mo, me->fl); tnsMMeshRemoveFaceOnly(mo, me->fr);
  470. lstRemovePointerLeave(&me->vl->elink, me); lstRemovePointerLeave(&me->vr->elink, me);
  471. lstRemoveItem(&mo->me,me); memLeave(me); mo->totme--;
  472. }
  473. void tnsMMeshRemoveVertEdgeFace(tnsMeshObject* mo, tnsMVert* mv){
  474. if(!mv) return; tnsMEdge* me;
  475. while(me=lstPopPointerLeave(&mv->elink)){ tnsMMeshRemoveEdgeFace(mo,me); }
  476. lstRemoveItem(&mo->mv,mv); memLeave(mv); mo->totmv--;
  477. }
  478. void tnsMMeshRefreshIndex(tnsMeshObject* mo){
  479. int i;
  480. i=0; for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ mv->i=i; i++; } mo->totmv=i;
  481. i=0; for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ me->i=i; i++; } mo->totme=i;
  482. i=0; for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ mf->i=i; i++; } mo->totmf=i;
  483. }
  484. void tnsClearMesh(tnsMeshObject* mo){
  485. arrFree(&mo->v, &mo->maxv); mo->totv=0;
  486. arrFree(&mo->e, &mo->maxe); mo->tote=0;
  487. for(int i=0;i<mo->totf;i++){ if(mo->f[i].loop) free(mo->f[i].loop); }
  488. arrFree(&mo->f, &mo->maxf); mo->totf=0;
  489. }
  490. void tnsClearMMesh(tnsMeshObject* mo){
  491. tnsMFace* mf; tnsMEdge* me; tnsMVert* mv;
  492. while(mf=lstPopItem(&mo->mf)){ while(lstPopPointerLeave(&mf->l)); memLeave(mf); }
  493. while(me=lstPopItem(&mo->me)){ memLeave(me); }
  494. while(mv=lstPopItem(&mo->mv)){ while(lstPopPointerLeave(&mv->elink)); memLeave(mv); }
  495. mo->totmv=mo->totme=mo->totmf=0;
  496. }
  497. void tnsMMeshFromMesh(tnsMeshObject* mo){
  498. tnsClearMMesh(mo);
  499. tnsEdgeHash* eh=tnsCreateEdgeHash(mo->totv); //mo->totmv=mo->totv; mo->totme=mo->tote; mo->totmf=mo->totf;
  500. for(int i=0;i<mo->totf;i++){
  501. tnsFace* f=&mo->f[i];
  502. for(int j=0;j<f->looplen-1;j++){ tnsEdgeHashAddVertPair(eh, f->loop[j], f->loop[j+1]); }
  503. tnsEdgeHashAddVertPair(eh, f->loop[f->looplen-1], f->loop[0]);
  504. }
  505. for(int i=0;i<eh->max;i++){
  506. tnsEdgeHashVert* ehv=&eh->vl[i];
  507. for(int j=0;j<ehv->next;j++){ tnsMEdge*me=tnsMMeshNewEdge(mo); ehv->e[j].me=me; }
  508. }
  509. for(int i=0;i<mo->totv;i++){ tnsVert*v=&mo->v[i]; tnsMVert*mv=tnsMMeshNewVert(mo); eh->vl[i].mv=mv;
  510. tnsVectorSet3v(mv->p,mo->v[i].p); mv->flags=mo->v[i].flags; tnsVectorSet3v(mv->n,v->n); }
  511. for(int i=0;i<mo->totf;i++){
  512. tnsFace* f=&mo->f[i]; tnsMFace* mf=tnsMMeshNewFace(mo); mf->flags=f->flags;
  513. for(int j=0;j<f->looplen;j++){ int v2=j+1; if(j==f->looplen-1) v2=0;
  514. tnsEdgeHashEdge* ehe=tnsEdgeHashGetEdge(eh,f->loop[j],f->loop[v2]);
  515. tnsMEdge* me=ehe->me; tnsMMeshEdgeAssignVerts(me,eh->vl[f->loop[j]].mv,eh->vl[f->loop[v2]].mv);
  516. tnsMMeshFaceAddEdge(mf,me); tnsVectorSet3v(mf->n,f->n);
  517. }
  518. }
  519. tnsMMeshEnsureSelectionFromVerts(mo);
  520. tnsDestroyEdgeHash(eh);
  521. }
  522. void tnsMeshFromMMesh(tnsMeshObject* mo){
  523. tnsClearMesh(mo);
  524. tnsInitMesh(mo, mo->totmv, 0, mo->totmf); int i=0;
  525. /* Vertex index should already correct. */
  526. //for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ mv->i=i; i++; }
  527. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ tnsVert* v=tnsFillVert(mo, LA_COLOR3(mv->p)); tnsFillVertNormal(v,mv->n); v->flags=mv->flags; }
  528. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ tnsFace* f=tnsFillFace(mo, mf->looplen, -1); f->flags=mf->flags;
  529. int j=0; for(laListItemPointer* lip=mf->l.pFirst;lip;lip=lip->pNext){
  530. laListItemPointer* next=lip->pNext; if(!next) next=mf->l.pFirst; tnsMEdge* me0=lip->p, *me1=next->p;
  531. tnsFillFaceLoop(f, j, tnsMMeshEdgeStartingVert(me0,me1)->i); j++;
  532. }
  533. tnsFillFaceNormal(f,mf->n);
  534. }
  535. mo->totv=mo->totmv; mo->totf=mo->totmf;
  536. mo->maxv=mo->totv; mo->maxe=mo->tote; mo->maxf=mo->totf;
  537. if((!mo->maxv) && mo->v) arrFree(&mo->v, &mo->maxv);
  538. if((!mo->maxe) && mo->e) arrFree(&mo->e, &mo->maxe);
  539. if((!mo->maxf) && mo->f) arrFree(&mo->f, &mo->maxf);
  540. tnsClearMMesh(mo);
  541. }
  542. void tnsMeshEnterEditMode(tnsMeshObject* mo){
  543. if(mo->Mode==TNS_MESH_EDIT_MODE) return;
  544. tnsMMeshFromMesh(mo);
  545. mo->Mode = TNS_MESH_EDIT_MODE;
  546. tnsInvalidateMeshBatch(mo);
  547. }
  548. void tnsMeshLeaveEditMode(tnsMeshObject* mo){
  549. if(mo->Mode==TNS_MESH_OBJECT_MODE) return;
  550. tnsMeshFromMMesh(mo);
  551. mo->Mode = TNS_MESH_OBJECT_MODE;
  552. tnsInvalidateMeshBatch(mo);
  553. }
  554. int tnsMMeshAnySelected(tnsMeshObject* mo){
  555. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(mv->flags&TNS_MESH_FLAG_SELECTED) return 1; }
  556. for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ if(me->flags&TNS_MESH_FLAG_SELECTED) return 1; }
  557. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ if(mf->flags&TNS_MESH_FLAG_SELECTED) return 1; } return 0;
  558. }
  559. void tnsMMeshClearExtraFlags(tnsMeshObject* mo){
  560. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ mv->flags&=(~TNS_MESH_FLAG_PICKED); }
  561. for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ me->flags&=(~TNS_MESH_FLAG_PICKED); /*me->flags&=(~TNS_MESH_FLAG_LOOP_REVERSE);*/ }
  562. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ mf->flags&=(~TNS_MESH_FLAG_PICKED); }
  563. }
  564. void tnsMMeshDeselectAll(tnsMeshObject* mo){
  565. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ mv->flags&=(~TNS_MESH_FLAG_SELECTED); }
  566. for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ me->flags&=(~TNS_MESH_FLAG_SELECTED); }
  567. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ mf->flags&=(~TNS_MESH_FLAG_SELECTED); }
  568. }
  569. void tnsMMeshSelectAll(tnsMeshObject* mo){
  570. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ mv->flags|=TNS_MESH_FLAG_SELECTED; }
  571. for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ me->flags|=TNS_MESH_FLAG_SELECTED; }
  572. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ mf->flags|=TNS_MESH_FLAG_SELECTED; }
  573. }
  574. void tnsMMeshSelectVert(tnsMeshObject* mo, tnsMVert* mv, int select, int toggle){
  575. if(!mo) return;
  576. if(toggle) tnsMMeshSelectVert(mo,mv,(mv->flags&TNS_MESH_FLAG_SELECTED?0:1),0);
  577. elif(select) mv->flags|=TNS_MESH_FLAG_SELECTED; else mv->flags&=(~TNS_MESH_FLAG_SELECTED);
  578. }
  579. void tnsMMeshSelectEdge(tnsMeshObject* mo, tnsMEdge* me, int select, int toggle){
  580. if(!mo) return;
  581. if(toggle) tnsMMeshSelectEdge(mo,me,(me->flags&TNS_MESH_FLAG_SELECTED?0:1),0);
  582. elif(select) me->flags|=TNS_MESH_FLAG_SELECTED; else me->flags&=(~TNS_MESH_FLAG_SELECTED);
  583. }
  584. void tnsMMeshEnsureSelectionFromVerts(tnsMeshObject* mo){
  585. for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ me->flags&=(~TNS_MESH_FLAG_SELECTED);
  586. if(me->vl->flags&me->vr->flags&TNS_MESH_FLAG_SELECTED) me->flags|=TNS_MESH_FLAG_SELECTED;
  587. }
  588. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ int sel=1; mf->flags&=(~TNS_MESH_FLAG_SELECTED);
  589. for(laListItemPointer* lip=mf->l.pFirst;lip;lip=lip->pNext){ tnsMEdge*me=lip->p; if(!(me->flags&TNS_MESH_FLAG_SELECTED)){ sel=0; break; } }
  590. if(sel){ mf->flags|=TNS_MESH_FLAG_SELECTED; }
  591. }
  592. }
  593. void tnsMMeshEnsureSelectionFromEdges(tnsMeshObject* mo){
  594. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ mv->flags&=(~TNS_MESH_FLAG_SELECTED);
  595. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){ tnsMEdge*me=lip->p; if(me->flags&TNS_MESH_FLAG_SELECTED){ mv->flags|=TNS_MESH_FLAG_SELECTED; break; } }
  596. }
  597. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){ int sel=1; mf->flags&=(~TNS_MESH_FLAG_SELECTED);
  598. for(laListItemPointer* lip=mf->l.pFirst;lip;lip=lip->pNext){ tnsMEdge*me=lip->p; if(!(me->flags&TNS_MESH_FLAG_SELECTED)){ sel=0; break; } }
  599. if(sel){ mf->flags|=TNS_MESH_FLAG_SELECTED; }
  600. }
  601. }
  602. void tnsMMeshEnsureSelection(tnsMeshObject* mo, int SelectMode){
  603. if(SelectMode==LA_CANVAS_SELECT_MODE_VERTS){ tnsMMeshEnsureSelectionFromVerts(mo); }
  604. elif(SelectMode==LA_CANVAS_SELECT_MODE_EDGES){ tnsMMeshEnsureSelectionFromEdges(mo); }
  605. }
  606. tnsMeshObject *tnsCreateMeshEmpty(tnsObject *under, char *Name, real AtX, real AtY, real AtZ){
  607. tnsMeshObject *mo = memAcquireHyper(sizeof(tnsMeshObject));
  608. tnsInitObjectBase(&mo->Base, under, Name, TNS_OBJECT_MESH, AtX, AtY, AtZ, 0, 0, 0, 1.0f, TNS_ROTATION_XYZ_EULER, 1.0f);
  609. return mo;
  610. }
  611. tnsMeshObject *tnsCreateMeshPlane(tnsObject *under, char *Name, real AtX, real AtY, real AtZ, real size){
  612. tnsMeshObject *mo=tnsCreateMeshEmpty(under, Name, AtX, AtY, AtZ);
  613. tnsInitMeshPlane(mo, size);
  614. tnsInvalidateMeshBatch(mo);
  615. return mo;
  616. }