- support for fake verse edges (subsurf modifier should work now)
[blender.git] / source / blender / src / verse_mesh.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * Contributor(s): Jiri Hnidek.
24  *
25  * ***** END GPL/BL DUAL LICENSE BLOCK *****
26  */
27
28 #ifdef WITH_VERSE
29
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "mydevice.h"
35
36 #include "DNA_object_types.h"
37 #include "DNA_mesh_types.h"
38
39 #include "BLI_dynamiclist.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_edgehash.h"
42 #include "BLI_editVert.h"
43
44 #include "BKE_customdata.h"
45 #include "BKE_depsgraph.h"
46 #include "BKE_global.h"
47 #include "BKE_mesh.h"
48 #include "BKE_utildefines.h"
49 #include "BKE_verse.h"
50
51 #include "BIF_verse.h"
52 #include "BIF_editmesh.h"
53 #include "BIF_space.h"
54 #include "BIF_screen.h"
55
56 #include "BSE_edit.h"
57
58 #include "editmesh.h"
59
60 #include "verse.h"
61
62 /* prototypes of static functions */
63
64 static void mark_changed_face_obsolete(struct VerseFace *vface);
65 static void sync_verseface_with_editface(struct VLayer *vlayer, struct VerseFace *vface);
66
67 void createVerseVertNL(struct EditVert *ev, struct VNode *vnode, struct VLayer *vlayer);
68 static void createAllVerseVerts(struct VNode *vnode, struct VLayer *vlayer);
69 void createVerseFaceNL(struct EditFace *efa, struct VNode *vnode, struct VLayer *vlayer);
70 static void createAllVerseFaces(struct VNode *vnode, struct VLayer *vlayer);
71
72 /*=============================================================================
73  *
74  *                  functions handling verse/blender FACES
75  *
76  *===========================================================================*/
77
78 /*
79  * create new VerseFace (polygon), due to information about EditFace
80  * put VerseFace to queue ... send to verse host (server)
81  */
82 void createVerseFace(EditFace *efa)
83 {
84         if(G.editMesh->vnode) {
85                 struct VLayer *vlayer = find_verse_layer_type((VGeomData*)((VNode*)G.editMesh->vnode)->data, POLYGON_LAYER);
86                 createVerseFaceNL(efa, (VNode*)G.editMesh->vnode, vlayer);
87         }
88         else efa->vface = NULL;
89 }
90
91 /*
92  * create new VerseFace (polygon), due to information about EditFace
93  * put VerseFace to queue ... send to verse host (server)
94  * NL version of function (infomration about verse node and
95  * layer is known ... optimalisation)
96  */
97 void createVerseFaceNL(EditFace *efa, VNode *vnode, VLayer *vlayer)
98 {
99         struct VerseFace *vface;
100
101         vface = (VerseFace*)create_verse_face(vlayer, vlayer->counter, -1, -1, -1, -1);
102         
103         vface->face = (void*)efa;
104         efa->vface = (void*)vface;
105
106         vface->flag |= FACE_SEND_READY;
107
108         /* EditVert #1 */
109         if(efa->v1) {
110                 if(efa->v1->vvert) {
111                         vface->vvert0 = (VerseVert*)efa->v1->vvert;
112                         if(((VerseVert*)efa->v1->vvert)->flag & VERT_RECEIVED) {
113                                 vface->v0 = ((VerseVert*)efa->v1->vvert)->id;
114                                 vface->counter--;
115                         }
116                         else
117                                 vface->flag &= ~FACE_SEND_READY;
118                 }
119         }
120         else
121                 vface->counter--;
122
123         /* EditVert #2 */
124         if(efa->v2) {
125                 if(efa->v2->vvert) {
126                         vface->vvert1 = (VerseVert*)efa->v2->vvert;
127                         if(((VerseVert*)efa->v2->vvert)->flag & VERT_RECEIVED) {
128                                 vface->v1 = ((VerseVert*)efa->v2->vvert)->id;
129                                 vface->counter--;
130                         }
131                         else
132                                 vface->flag &= ~FACE_SEND_READY;
133                 }
134         }
135         else
136                 vface->counter--;
137         /* EditVert #3 */
138         if(efa->v3) {
139                 if(efa->v3->vvert) {
140                         vface->vvert2 = (VerseVert*)efa->v3->vvert;
141                         if(((VerseVert*)efa->v3->vvert)->flag & VERT_RECEIVED) {
142                                 vface->v2 = ((VerseVert*)efa->v3->vvert)->id;
143                                 vface->counter--;
144                         }
145                         else
146                                 vface->flag &= ~FACE_SEND_READY;
147                 }
148         }
149         else
150                 vface->counter--;
151         /* EditVert #4 */
152         if(efa->v4) {
153                 if(efa->v4->vvert) {
154                         vface->vvert3 = (VerseVert*)efa->v4->vvert;
155                         if(((VerseVert*)efa->v4->vvert)->flag & VERT_RECEIVED) {
156                                 vface->v3 = ((VerseVert*)efa->v4->vvert)->id;
157                                 vface->counter--;
158                         }
159                         else
160                                 vface->flag &= ~FACE_SEND_READY;
161                 }
162         }
163         else
164                 vface->counter--;
165
166         add_item_to_send_queue(&(vlayer->queue), (void*)vface, VERSE_FACE);
167 }
168
169 /*
170  * creates new verse faces, add all of then to queue ... send to verse server
171  */
172 static void createAllVerseFaces(VNode *vnode, VLayer *vlayer)
173 {
174         if(G.obedit) {
175                 struct EditMesh *em;
176                 struct EditFace *efa;
177
178                 em = G.editMesh;
179
180                 efa = em->faces.first;
181                 /* push all EditFaces to the verse server */
182                 while(efa){
183                         createVerseFaceNL(efa, vnode, vlayer);
184                         efa = efa->next;
185                 }
186         }
187 }
188
189 /*
190  * When verse face is changed during sending/receiving from verse server, then
191  * this verse face is marked as obsolete and it will be send again, when it
192  * will be received from verse server 
193  */
194 static void mark_changed_face_obsolete(VerseFace *vface)
195 {
196         struct EditFace *efa = (EditFace*)vface->face;
197
198         if(efa) {
199                 if(vface->vvert0->vertex != efa->v1) vface->flag |= FACE_OBSOLETE;
200                 if(vface->vvert1->vertex != efa->v2) vface->flag |= FACE_OBSOLETE;
201                 if(vface->vvert2->vertex != efa->v3) vface->flag |= FACE_OBSOLETE;
202                 if(vface->vvert3 && (vface->vvert3->vertex != efa->v4)) vface->flag |= FACE_OBSOLETE;
203         }
204 }
205
206 /*
207  * this function will sync EditFace and VerseFace and it will send changes to
208  * verse server too
209  */
210 static void sync_verseface_with_editface(VLayer *vlayer, VerseFace *vface)
211 {
212         struct EditFace *efa = (EditFace*)vface->face;
213         int dosend = 0;
214
215         /* edit face and probably verse face was deleted */
216         if(!efa || (vface->flag & FACE_DELETED)) return;
217
218         /* blender nor verse don't support such crazy things */
219         if(!(vface->vvert0) || !(vface->vvert1) || !(vface->vvert2)) {
220                 printf("\tERROR: vface->vvert0: %p, vface->vvert1: %p, vface->vvert2: %p\n", vface->vvert0, vface->vvert1, vface->vvert2);
221                 return;
222         }
223
224         /* initialize verse face flag */
225         vface->flag |= FACE_SEND_READY;
226
227         /* debug print */
228 #if 0
229         printf("\n");
230         if(efa->v4) {
231                 printf("\tefa->v1,v2,v3,v4->vvert->id: %d, %d, %d, %d\n",
232                                 ((VerseVert*)efa->v1->vvert)->id,
233                                 ((VerseVert*)efa->v2->vvert)->id,
234                                 ((VerseVert*)efa->v3->vvert)->id,
235                                 ((VerseVert*)efa->v4->vvert)->id);
236                 printf("\tefa->v1,v2,v3,v4->vvert: %p, %p, %p, %p\n",
237                                 efa->v1->vvert,
238                                 efa->v2->vvert,
239                                 efa->v3->vvert,
240                                 efa->v4->vvert);
241         }
242         else {
243                 printf("\tefa->v1,v2,v3->vvert->id: %d, %d, %d, NULL\n",
244                                 ((VerseVert*)efa->v1->vvert)->id,
245                                 ((VerseVert*)efa->v2->vvert)->id,
246                                 ((VerseVert*)efa->v3->vvert)->id);
247                 printf("\tefa->v1,v2,v3,v4->vvert: %p, %p, %p, NULL\n",
248                                 efa->v1->vvert,
249                                 efa->v2->vvert,
250                                 efa->v3->vvert);
251         }
252         printf("\tvface->vvert0, vvvert1, vvvert2, vvvert3: %p, %p, %p, %p\n",
253                         vface->vvert0,
254                         vface->vvert1,
255                         vface->vvert2,
256                         vface->vvert3);
257
258         printf("\tefa->v1, v2, v3, v4: %p, %p, %p, %p\n",
259                         efa->v1,
260                         efa->v2,
261                         efa->v3,
262                         efa->v4);
263         if(vface->vvert3) {
264                 printf("\tvface->v0,v1,v2,v3: %d, %d, %d, %d\n",
265                                 vface->v0,
266                                 vface->v1,
267                                 vface->v2,
268                                 vface->v3);
269                 printf("\tvface->vvert0,vvvert1,vvvert2,vvvert3->vertex: %p, %p, %p, %p\n",
270                                 vface->vvert0->vertex,
271                                 vface->vvert1->vertex,
272                                 vface->vvert2->vertex,
273                                 vface->vvert3->vertex);
274         }
275         else {
276                 printf("\tvface->v0,v1,v2,v3: %d, %d, %d, NULL\n",
277                                 vface->v0,
278                                 vface->v1,
279                                 vface->v2);
280                 printf("\tvface->vvert0,vvvert1,vvvert2->vertex: %p, %p, %p, NULL\n",
281                                 vface->vvert0->vertex,
282                                 vface->vvert1->vertex,
283                                 vface->vvert2->vertex);
284         }
285 #endif
286
287         /* initialize counter of unreceived vertexes */
288         vface->counter = 4;
289
290         /* 1st vertex */
291         if(vface->vvert0->vertex != efa->v1) {
292                 dosend = 1;
293                 vface->vvert0->counter--;
294                 vface->vvert0 = (VerseVert*)efa->v1->vvert;
295                 vface->v0 = vface->vvert0->id;
296                 if(vface->vvert0->flag & VERT_RECEIVED)
297                         vface->counter--;
298                 else
299                         vface->flag &= ~FACE_SEND_READY;
300         }
301         else
302                 vface->counter--;
303
304         /* 2nd vertex */
305         if(vface->vvert1->vertex != efa->v2) {
306                 dosend = 1;
307                 vface->vvert1->counter--;
308                 vface->vvert1 = (VerseVert*)efa->v2->vvert;
309                 vface->v1 = vface->vvert1->id;
310                 if(vface->vvert1->flag & VERT_RECEIVED)
311                         vface->counter--;
312                 else
313                         vface->flag &= ~FACE_SEND_READY;
314         }
315         else
316                 vface->counter--;
317
318         /* 3th vertex */
319         if(vface->vvert2->vertex != efa->v3) {
320                 dosend = 1;
321                 vface->vvert2->counter--;
322                 vface->vvert2 = (VerseVert*)efa->v3->vvert;
323                 vface->v2 = vface->vvert2->id;
324                 if(vface->vvert2->flag & VERT_RECEIVED)
325                         vface->counter--;
326                 else
327                         vface->flag &= ~FACE_SEND_READY;
328         }
329         else
330                 vface->counter--;
331
332         /* 4th vertex */
333         if(vface->vvert3 && ((vface->vvert3->vertex != efa->v4) || (vface->vvert3 && !efa->v4) || (vface->v3 != vface->vvert3->id))) {
334                 dosend = 1;
335                 if(efa->v4) {
336                         vface->vvert3->counter--;
337                         vface->vvert3 = (VerseVert*)efa->v4->vvert;
338                         vface->v3 = vface->vvert3->id;
339                         if(vface->vvert3->flag & VERT_RECEIVED)
340                                 vface->counter--;
341                         else
342                                 vface->flag &= ~FACE_SEND_READY;
343                 }
344                 else {
345                         vface->vvert3->counter--;
346                         vface->vvert3 = NULL;
347                         vface->v3 = -1;
348                         vface->counter--;
349                 }
350         }
351         /* verse face has 4 vertexes now, not 3 vertexes as in past */
352         else if(!(vface->vvert3) && efa->v4) {
353                 dosend = 1;
354                 vface->vvert3 = (VerseVert*)efa->v4->vvert;
355                 vface->v3 = vface->vvert3->id;
356                 if(vface->vvert3->flag & VERT_RECEIVED)
357                         vface->counter--;
358                 else
359                         vface->flag &= ~FACE_SEND_READY;
360         }
361         else
362                 vface->counter--;
363
364         if(dosend) {
365                 /* printf("\tsending CHANGED FACE\n");
366                 printf("\t\tnew: %d %d %d %d\n", vface->v0, vface->v1, vface->v2, vface->v3);*/
367                 vface->flag |= FACE_CHANGED;
368                 /* remove verse face from list of received faces */
369                 BLI_dlist_rem_item(&(vlayer->dl), vface->id);
370                 /* and add verse face again to sending queue */
371                 add_item_to_send_queue(&(vlayer->queue), (void*)vface, VERSE_FACE);
372         }
373 }
374
375 /*
376  * this function will sync all VerseFaces with coresponding EditFaces,
377  * this is useful, when some editmesh tool has changed editface pointers at
378  * vertexes (edges), parameter of this function is geometry node
379  */
380 void sync_all_versefaces_with_editfaces(VNode *vnode)
381 {
382         struct VLayer *vlayer;
383         struct VerseFace *vface, *nvface;
384
385         if(vnode->type != V_NT_GEOMETRY) return;
386
387         vlayer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
388
389         /* mark changed verse faces in sending queue as obsolete at the first time */
390         vface = vlayer->queue.first;
391         while(vface) {
392                 mark_changed_face_obsolete(vface);
393                 vface = vface->next;
394         }
395         
396         /* send all received and changed verse face again to verse server */
397         vface = vlayer->dl.lb.first;
398         while(vface) {
399                 nvface = vface->next;
400                 sync_verseface_with_editface(vlayer, vface);
401                 vface = nvface;
402         }
403 }
404
405 /*
406  * send delete polygon command to verse server
407  */
408 void b_verse_send_face_delete(EditFace *efa)
409 {
410         ((VerseFace*)efa->vface)->face = NULL;
411         send_verse_face_delete((VerseFace*)efa->vface);
412         efa->vface = NULL;
413 }
414
415 /*=============================================================================
416  *
417  *                   functions handling verse/blender VERTEXES
418  *
419  *===========================================================================*/
420
421 /*
422  * this function will sync position of all VerseVerts with EditVerts
423  * this function is called after actions: Smooth, Noise and To Sphere,
424  * because position of vertexes isn't managed by transform system
425  */
426 void sync_all_verseverts_with_editverts(VNode *vnode)
427 {
428         struct VLayer *vlayer;
429         struct VerseVert *vvert;
430
431         if(vnode->type != V_NT_GEOMETRY) return;
432
433         vlayer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
434
435         vvert = vlayer->dl.lb.first;
436
437         /* sync all received vertexes */
438         while(vvert) {
439                 send_versevert_pos(vvert);
440                 vvert = vvert->next;
441         }
442
443         vvert = vlayer->queue.first;
444         
445         /* sync all unreceived vertexes (mar pos as obsolete, when
446          * actual position was changed) */
447         while(vvert) {
448                 send_versevert_pos(vvert);
449                 vvert = vvert->next;
450         }
451
452         verse_callback_update(0);
453 }
454
455 /*
456  * send delete vertex command to verse server
457  */
458 void b_verse_send_vertex_delete(EditVert *eve)
459 {
460         ((VerseVert*)eve->vvert)->vertex = NULL;
461         send_verse_vertex_delete((VerseVert*)eve->vvert);
462         eve->vvert = NULL;
463 }
464
465 /*
466  * send position of verse vertex to verse server
467  */
468 void send_versevert_pos(VerseVert *vvert)
469 {
470         /* delete command was sent to verse server ... sending one
471          * more position command would create new vertex */
472         if ((vvert->flag & VERT_DELETED) | (vvert->flag & VERT_OBSOLETE)) return;
473         
474         /* don't send position of verse vertex to verse server, because it could create
475          * new vertex */
476         if(vvert->flag & VERT_RECEIVED) {
477                 if(vvert->flag & VERT_LOCKED) {
478                         /* when position of verse vert was sent to verse server
479                          * and it wasn't received yet, then mark sent position
480                          * as obsolete ... blender will automaticaly send actual
481                          * position, when old will be received */
482                         vvert->flag |= VERT_POS_OBSOLETE;
483                 }
484                 else {
485                         struct EditVert *eve = (EditVert*)vvert->vertex;
486                         /* send position to verse server, when it is different from actual position */
487                         if(eve && (eve->co[0]!=vvert->co[0] || eve->co[1]!=vvert->co[1] || eve->co[2]!=vvert->co[2])) {
488                                 /* lock vertex and send its position to verse server,
489                                  * locking of vertex prevents from sending too many
490                                  * informations about vertex position during draging
491                                  * of vertex */
492                                 vvert->flag |= VERT_LOCKED;
493                                 VECCOPY(vvert->co, eve->co);
494                                 send_verse_vertex(vvert);
495                         }
496                 }
497         }
498         else {
499                 /* we created this vertex and we sent new position to verse server, but "confirmation" command about
500                  * position of vertex didn't arrived yet, then we can't send new position of vertex ... we only mark
501                  * position of vertex as obsolete and new position will be sent to verse server, when confirmation
502                  * command will arive */
503                 struct EditVert *eve = (EditVert*)vvert->vertex;
504                 if(eve && (eve->co[0]!=vvert->co[0] || eve->co[1]!=vvert->co[1] || eve->co[2]!=vvert->co[2])) {
505                         vvert->flag |= VERT_POS_OBSOLETE;
506                 }
507         }
508         
509         verse_callback_update(0);
510 }
511
512 /*
513  * create new VerseVert due to information about EditVert,
514  * put VerseVert to queue ... send to verse host (server)
515  */
516 void createVerseVert(EditVert *eve)
517 {
518         if(G.editMesh->vnode) {
519                 struct VLayer *vlayer = find_verse_layer_type((VGeomData*)((VNode*)G.editMesh->vnode)->data, VERTEX_LAYER);
520                 createVerseVertNL(eve, (VNode*)G.editMesh->vnode, vlayer);
521         }
522         else eve->vvert = NULL;
523 }
524
525 /*
526  * create new VerseVert due to information about EditVert,
527  * put VerseVert to queue ... send to verse host (server)
528  * NL version of function (infomration about verse node and
529  * layer is known ... optimalisation)
530  */
531 void createVerseVertNL(EditVert *eve, VNode *vnode, VLayer *vlayer)
532 {
533         struct VerseVert *vvert;
534
535         vvert = create_verse_vertex(vlayer, vlayer->counter, eve->co[0], eve->co[1], eve->co[2]);
536
537         vvert->vertex = (void*)eve;
538         eve->vvert = (void*)vvert;
539
540         vvert->flag |= VERT_LOCKED;
541
542 /*      printf("\tsend_versevert_pos: %d create and LOCK \n", vvert->id);*/
543
544         /* add vvert to sending queue */
545         add_item_to_send_queue(&(vlayer->queue), (void*)vvert, VERSE_VERT);
546 }
547
548 /*
549  * create new verse vertexes due to all vertexes and send all of them to verse server 
550  */
551 static void createAllVerseVerts(VNode *vnode, VLayer *vlayer)
552 {
553         if(G.obedit) {
554                 struct EditMesh *em;
555                 struct EditVert *eve;
556
557                 em = G.editMesh;
558                 eve = em->verts.first;
559
560                 /* push all EditVertexes to the verse server */
561                 while(eve){
562                         createVerseVertNL(eve, vnode, vlayer);
563                         eve = eve->next;
564                 }
565         }
566 }
567
568 /*
569  * unsubscribe from verse geometry layers of verse geometry node
570  * and clear bindings between verse node and mesh
571  */
572 void unsubscribe_from_geom_node(VNode *vnode)
573 {
574         struct VerseSession *session = vnode->session;
575         
576         struct VLayer *vlayer = ((VGeomData*)vnode->data)->layers.lb.first;
577         
578         if(vnode->type != V_NT_GEOMETRY) return;
579         
580         /* free bindings between verse node and blender mesh*/  
581         if(((VGeomData*)vnode->data)->mesh) {
582                 ((Mesh*)((VGeomData*)vnode->data)->mesh)->vnode = NULL;
583                 ((VGeomData*)vnode->data)->mesh = NULL;
584         }
585
586         /* free bindings between verse node and blender editmesh */
587         if(((VGeomData*)vnode->data)->editmesh) {
588                 ((EditMesh*)((VGeomData*)vnode->data)->editmesh)->vnode = NULL;
589                 ((VGeomData*)vnode->data)->editmesh = NULL;
590         }
591         
592         /* free all verse layer data and unsubscribe from all layers */
593         while(vlayer) {
594                 BLI_dlist_reinit(&(vlayer->dl));
595                 BLI_freelistN(&(vlayer->queue));
596                 BLI_freelistN(&(vlayer->orphans));
597
598                 if(session->flag & VERSE_CONNECTED)
599                         verse_send_g_layer_unsubscribe(vnode->id, vlayer->id);
600
601                 vlayer = vlayer->next;
602         }
603
604 }
605
606 /* ===================================================================================
607  *
608  *             Function executed after execution of callback functions
609  *
610  * ===================================================================================*/
611
612 /*
613  * Actions executed after new VerseLayer is created
614  */
615 void post_layer_create(struct VLayer *vlayer)
616 {
617         /* if we are owners of VerseNode, then push geometry to verse server */
618         if(vlayer->vnode->owner_id == VN_OWNER_MINE) {
619                 switch(vlayer->type){
620                         case VN_G_LAYER_VERTEX_XYZ:
621 /*                              if(vlayer->id==0) createAllVerseVerts(vlayer->vnode, vlayer);
622                                 break;*/
623                         case VN_G_LAYER_POLYGON_CORNER_UINT32:
624 /*                              if(vlayer->id==1) createAllVerseFaces(vlayer->vnode, vlayer);
625                                 break;*/
626                         case VN_G_LAYER_VERTEX_UINT32:
627                         case VN_G_LAYER_VERTEX_REAL:
628                         case VN_G_LAYER_POLYGON_CORNER_REAL:
629                         case VN_G_LAYER_POLYGON_FACE_UINT8:
630                         case VN_G_LAYER_POLYGON_FACE_UINT32:
631                         case VN_G_LAYER_POLYGON_FACE_REAL:
632                                 break;
633                 }
634         }
635         else {
636                 switch(vlayer->type) {
637                         case VN_G_LAYER_VERTEX_XYZ:
638                         case VN_G_LAYER_POLYGON_CORNER_UINT32:
639                         case VN_G_LAYER_VERTEX_UINT32:
640                         case VN_G_LAYER_VERTEX_REAL:
641                         case VN_G_LAYER_POLYGON_CORNER_REAL:
642                         case VN_G_LAYER_POLYGON_FACE_UINT8:
643                         case VN_G_LAYER_POLYGON_FACE_UINT32:
644                         case VN_G_LAYER_POLYGON_FACE_REAL:
645                                 break;
646                 }
647         }
648 }
649
650 /*
651  * Actions after destroying of VerseLayer
652  */
653 void post_layer_destroy(struct VLayer *vlayer)
654 {
655 }
656
657 /*
658  * Actions executed after creating of new VerseVert, when object is in edit
659  * mode, and this client didn't create this VerseVert (vvert->vertex is NULL),
660  * then new editvert is created
661  */
662 void post_vertex_create(VerseVert *vvert)
663 {
664         struct VNode *obj_vnode;
665         struct VNode *geom_vnode = vvert->vlayer->vnode;
666         struct EditMesh *em=NULL;
667
668         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
669                 em = (EditMesh*)((VGeomData*)geom_vnode->data)->editmesh;
670         }
671
672         /* when vert was changed during sending to verse server, then
673          * we have to send it to verse server again */
674         if(vvert->flag & VERT_POS_OBSOLETE) {
675                 vvert->flag &= ~VERT_POS_OBSOLETE;
676                 
677                 if(em && (vvert->vertex)) {
678                         struct EditVert *eve = (EditVert*)vvert->vertex;
679                         VECCOPY(vvert->co, eve->co);
680 /*                      printf("\tpost_vertex_create: %d send and NOT_OBSOLETE\n", vvert->id);*/
681                         send_verse_vertex(vvert);
682                         verse_callback_update(0);
683
684                         return;
685                 }
686         }
687         
688         if(em && !(vvert->vertex)) {
689                 struct EditVert *eve;
690
691                 /* to prevent never ending loop of sending and receiving
692                  * vertexes, because addvertlist() sends new vertex to verse
693                  * server if em->vnode isn't NULL */
694                 em->vnode = NULL;
695                 eve = addvertlist(vvert->co, NULL);
696                 em->vnode = (void*)geom_vnode;
697
698                 eve->vvert = (void*)vvert;
699                 vvert->vertex = (void*)eve;
700         
701                 countall();
702
703                 recalc_editnormals();
704         }
705
706         if(((VGeomData*)geom_vnode->data)->vlink) {
707                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
708                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
709
710                 allqueue(REDRAWVIEW3D, 1);
711         }
712 }
713
714 /*
715  * Actions executed, when position of VerseVert was changed
716  * position of EditVert is changed in edit mode
717  */
718 void post_vertex_set_xyz(VerseVert *vvert)
719 {
720         struct VNode *obj_vnode;
721         struct VNode *geom_vnode = vvert->vlayer->vnode;
722         struct EditVert *eve = NULL;
723
724         /* when vert was changed during sending to verse server, then
725          * we have to send it to verse server again */
726         if(vvert->flag & VERT_POS_OBSOLETE) {
727                 if(vvert->vertex) {
728                         vvert->flag &= ~VERT_POS_OBSOLETE;
729                         vvert->flag |= VERT_LOCKED;
730
731                         eve = (EditVert*)vvert->vertex;
732                         VECCOPY(vvert->co, eve->co);
733                         send_verse_vertex(vvert);
734                         verse_callback_update(0);
735                 }
736                 else {
737                         printf("\terror: vvert->vertex shouldn't be NULL\n");
738                 }
739
740                 return;
741         }
742         
743         /* when shared object is in edit mode, then update editmesh */  
744         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
745                 if(vvert->vertex) {
746                         eve = (EditVert*)vvert->vertex;
747                         VECCOPY(eve->co, vvert->co);
748                         recalc_editnormals();
749                 }
750                 else {
751                         printf("\terror: vvert->vertex shouldn't be NULL\n");
752                 }
753         }
754
755         if(((VGeomData*)geom_vnode->data)->vlink) {
756                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
757                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
758
759                 allqueue(REDRAWVIEW3D, 1);
760         }
761 }
762
763 /*
764  * Actions executed after deleting of VerseVert
765  */
766 void post_vertex_delete(VerseVert *vvert)
767 {
768         struct VNode *obj_vnode;
769         struct VNode *geom_vnode = vvert->vlayer->vnode;
770         struct EditMesh *em = NULL;
771         struct EditEdge *ed, *edn;
772         struct EditVert *eve = NULL;
773
774         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
775                 em = (EditMesh*)((VGeomData*)geom_vnode->data)->editmesh;
776                 eve = (EditVert*)vvert->vertex;
777         }
778
779         if(em && eve) {
780                 /*printf("\tPOST_VERTEX_DELETE()\n");*/
781
782                 /* delete all edges needing eve vertex */
783                 ed = em->edges.first;
784                 while(ed) {
785                         edn = ed->next;
786                         if(ed->v1==eve || ed->v2==eve) {
787                                 remedge(ed);
788                                 free_editedge(ed);
789                         }
790                         ed = edn;
791                 }
792
793                 eve->vvert = NULL;
794                 BLI_remlink(&em->verts, eve);
795                 free_editvert(eve);
796                 vvert->vertex = NULL;
797         
798                 countall();
799
800                 recalc_editnormals();
801         }
802         
803         if(((VGeomData*)geom_vnode->data)->vlink) {
804                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
805                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
806
807                 allqueue(REDRAWVIEW3D, 1);
808         }
809 }
810
811 /*
812  * free constraint between VerseVert and EditVert
813  */
814 void post_vertex_free_constraint(VerseVert *vvert)
815 {
816         if(vvert->vertex) {
817                 ((EditVert*)vvert->vertex)->vvert=NULL;
818                 vvert->vertex=NULL;
819         }
820 }
821
822 /*
823  * Action executed after setting up uint8 value of polygon
824  */
825 void post_polygon_set_uint8(VerseFace *vface)
826 {
827 }
828
829 /*
830  * Action executed after creating of new VerseFace
831  */
832 void post_polygon_create(VerseFace *vface)
833 {
834         struct VNode *obj_vnode;
835         struct VNode *geom_vnode = vface->vlayer->vnode;
836         struct EditMesh *em = NULL;
837
838         /* if verse face was set as deleted during sending to verse server, then send
839          * delete command to verse server now ... we know verse face id */
840 /*      if(vface->flag & FACE_DELETED) {
841                 send_verse_face_delete(vface);
842                 return;
843         }*/
844         
845         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
846                 em = (EditMesh*)((VGeomData*)geom_vnode->data)->editmesh;
847         }
848
849         /* when face was changed during sending to verse server, then
850          * we have to send it to verse server again */
851         if(vface->flag & FACE_OBSOLETE) {
852                 vface->flag &= ~FACE_OBSOLETE;
853                 sync_verseface_with_editface(vface->vlayer, vface);
854                 return;
855         }
856
857         if(em && !(vface->face) && (vface->counter==0)) {
858                 struct VLayer *vlayer;
859                 struct VerseVert *vvert;
860                 struct EditFace *efa;
861                 struct EditVert *eves[4]={NULL, NULL, NULL, NULL};
862                 uint32 vert_ids[4]={vface->v0, vface->v1, vface->v2, vface->v3};
863                 int i;
864
865                 /*printf("\tPOST_POLYGON_CREATE()\n");*/
866
867                 vlayer = find_verse_layer_type((VGeomData*)geom_vnode->data, VERTEX_LAYER);
868
869                 for(i=0; i<4; i++) {
870                         if(vert_ids[i] != -1) {
871                                 vvert = BLI_dlist_find_link(&(vlayer->dl), vert_ids[i]);
872                                 if(vvert) eves[i] = (EditVert*)vvert->vertex;
873                         }
874                 }
875
876                 /* to prevent never ending loop of sending and receiving
877                  * faces, because addfacelist() sends new face to verse
878                  * server if em->vnode isn't NULL */
879                 em->vnode = NULL;
880                 efa = addfacelist(eves[0], eves[1], eves[2], eves[3], NULL, NULL);
881                 em->vnode = geom_vnode;
882
883                 if(efa) {
884                         efa->vface = vface;
885                         vface->face = efa;
886                 }
887         
888                 countall();
889
890                 recalc_editnormals();
891         }
892
893         if(((VGeomData*)geom_vnode->data)->vlink) {
894                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
895                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
896
897                 allqueue(REDRAWVIEW3D, 1);
898         }
899 }
900
901 /*
902  * Action executed after changes of VerseFace
903  * ... order of vertexes was fliped, etc.
904  */
905 void post_polygon_set_corner(VerseFace *vface)
906 {
907         struct VNode *obj_vnode;
908         struct VNode *geom_vnode = vface->vlayer->vnode;
909         struct EditMesh *em = NULL;
910         struct EditFace *efa = NULL;
911         struct EditEdge *eed, *eedn;
912
913         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
914                 em = (EditMesh*)((VGeomData*)geom_vnode->data)->editmesh;
915                 efa = (EditFace*)vface->face;
916         }
917
918         if(em && efa) {
919
920                 /* when face was changed during sending to verse server, then
921                  * we have to send it to verse server again */
922                 if(vface->flag & FACE_OBSOLETE) {
923                         vface->flag &= ~FACE_OBSOLETE;
924                         sync_verseface_with_editface(vface->vlayer, vface);
925                         return;
926                 }
927
928                 /* mark all edges, which are part of face efa */
929                 efa->e1->f2 = 1;
930                 efa->e2->f2 = 1;
931                 efa->e3->f2 = 1;
932                 if(efa->e4) efa->e4->f2 = 1;
933
934                 /* change pointers at EdtitVerts and decrease counters of "old"
935                  * VerseVertexes reference ... less VerseFaces will need them */
936                 if(vface->vvert0 != efa->v1->vvert)
937                         efa->v1 = (EditVert*)vface->vvert0->vertex;
938                 if(vface->vvert1 != efa->v2->vvert)
939                         efa->v2 = (EditVert*)vface->vvert1->vertex;
940                 if(vface->vvert2 != efa->v3->vvert)
941                         efa->v3 = (EditVert*)vface->vvert2->vertex;
942                 if(efa->v4) {
943                         if(!vface->vvert3)
944                                 efa->v4 = NULL;
945                         else if(vface->vvert3 != efa->v4->vvert)
946                                 efa->v4 = (EditVert*)vface->vvert3->vertex;
947                 }
948
949                 /* change pointers at EditEdges */
950
951                 /* 1st edge */
952                 eed = findedgelist(efa->v1, efa->v2);
953                 if(eed) efa->e1 = eed;
954                 else efa->e1 = addedgelist(efa->v1, efa->v2, NULL);
955
956                 /* 2nd edge */
957                 eed = findedgelist(efa->v2, efa->v3);
958                 if(eed) efa->e2 = eed;
959                 else efa->e2 = addedgelist(efa->v2, efa->v3, NULL);
960
961                 if(efa->v4) {
962                 /* 3th edge */
963                         eed = findedgelist(efa->v2, efa->v3);
964                         if(eed) efa->e3 = eed;
965                         else efa->e3 = addedgelist(efa->v2, efa->v3, NULL);
966                         /* 4th edge */
967                         eed = findedgelist(efa->v4, efa->v1);
968                         if(eed) efa->e4 = eed;
969                         else efa->e4 = addedgelist(efa->v4, efa->v1, NULL);
970                 }
971                 else {
972                 /* 3th edge */
973                         eed = findedgelist(efa->v3, efa->v1);
974                         if(eed) efa->e3 = eed;
975                         else efa->e3 = addedgelist(efa->v3, efa->v1, NULL);
976                         /* 4th edge */
977                         efa->e4 = NULL;
978                 }
979
980                 /* unmark needed edges */
981                 efa = em->faces.first;
982                 while(efa) {
983                         efa->e1->f2 = 0;
984                         efa->e2->f2 = 0;
985                         efa->e3->f2 = 0;
986                         if(efa->e4) efa->e4->f2 = 0;
987                         efa = efa->next;
988                 }
989
990                 /* delete all unneeded edges */
991                 eed = em->edges.first;
992                 while(eed) {
993                         eedn = eed->next;
994                         if(eed->f2) {
995                                 remedge(eed);
996                                 free_editedge(eed);
997                         }
998                         eed = eedn;
999                 }
1000
1001                 countall();
1002
1003                 recalc_editnormals();
1004         }
1005
1006         if(((VGeomData*)geom_vnode->data)->vlink) {
1007                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
1008                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
1009
1010                 allqueue(REDRAWVIEW3D, 1);
1011         }
1012 }
1013
1014 /*
1015  * Action executed after deleting of VerseFace
1016  */
1017 void post_polygon_delete(VerseFace *vface)
1018 {
1019         struct VNode *obj_vnode;
1020         struct VNode *geom_vnode = vface->vlayer->vnode;
1021         struct EditMesh *em = NULL;
1022         struct EditFace *efa = NULL;
1023         struct EditEdge *eed, *eedn;
1024
1025         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
1026                 em = (EditMesh*)((VGeomData*)geom_vnode->data)->editmesh;
1027                 efa = (EditFace*)vface->face;
1028         }
1029
1030         if(em && efa) {
1031                 /*printf("\tPOST_POLYGON_DELETE()\n");*/
1032
1033                 /* mark all edges, which are part of face efa */
1034                 efa->e1->f2 = 1;
1035                 efa->e2->f2 = 1;
1036                 efa->e3->f2 = 1;
1037                 if(efa->e4) efa->e4->f2 = 1;
1038
1039                 efa->vface = NULL;
1040                 BLI_remlink(&em->faces, efa);
1041                 free_editface(efa);
1042                 vface->face = NULL;
1043
1044                 /* following two crazy loops wouldn't be neccessary if verse spec
1045                  * would support edges */
1046
1047                 /* unmark needed edges */
1048                 efa = em->faces.first;
1049                 while(efa) {
1050                         efa->e1->f2 = 0;
1051                         efa->e2->f2 = 0;
1052                         efa->e3->f2 = 0;
1053                         if(efa->e4) efa->e4->f2 = 0;
1054                         efa = efa->next;
1055                 }
1056
1057                 /* delete all unneeded edges */
1058                 eed = em->edges.first;
1059                 while(eed) {
1060                         eedn = eed->next;
1061                         if(eed->f2) {
1062                                 remedge(eed);
1063                                 free_editedge(eed);
1064                         }
1065                         eed = eedn;
1066                 }
1067         
1068                 countall();
1069         }
1070
1071         if(((VGeomData*)geom_vnode->data)->vlink) {
1072                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
1073                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
1074
1075                 allqueue(REDRAWVIEW3D, 1);
1076         }
1077 }
1078
1079 /*
1080  * free constraint between VerseFace and EditFace
1081  */
1082 void post_polygon_free_constraint(VerseFace *vface)
1083 {
1084         if(vface->face) {
1085                 ((EditFace*)vface->face)->vface = NULL;
1086                 vface->face = NULL;
1087         }
1088 }
1089
1090 /*
1091  * free constraint between VGeomData, EditMesh and Mesh
1092  */
1093 void post_geometry_free_constraint(VNode *vnode)
1094 {
1095         if(((VGeomData*)vnode->data)->editmesh) {
1096                 G.editMesh->vnode = NULL;
1097                 ((VGeomData*)vnode->data)->editmesh = NULL;
1098         }
1099         if(((VGeomData*)vnode->data)->mesh) {
1100                 ((Mesh*)((VGeomData*)vnode->data)->mesh)->vnode = NULL;
1101                 ((VGeomData*)vnode->data)->mesh = NULL;
1102         }
1103 }
1104
1105 /* =========================================================================
1106  *
1107  *              Functions influencing whole EditMesh or VerseMesh
1108  *
1109  * ========================================================================= */
1110
1111 /*
1112  * free all bindings between EditMesh and "verse mesh" ... it is called between
1113  * restorng editmesh from undo stack
1114  */
1115 void destroy_versemesh(VNode *vnode)
1116 {
1117         struct VLayer *vert_vlayer, *face_vlayer;
1118         struct VerseVert *vvert;
1119         struct VerseFace *vface;
1120
1121         if(vnode->type != V_NT_GEOMETRY) return;
1122
1123         vert_vlayer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
1124         face_vlayer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
1125
1126
1127         /* send delete command to all received verse faces */
1128         vface = face_vlayer->dl.lb.first;
1129         while(vface) {
1130                 if(vface->face) ((EditFace*)vface->face)->vface = NULL;
1131                 vface->face = NULL;
1132                 send_verse_face_delete(vface);
1133                 vface = vface->next;
1134         }
1135         /* send delete command to all verse faces waiting in orphan list */
1136         vface = face_vlayer->orphans.first;
1137         while(vface) {
1138                 if(vface->face) ((EditFace*)vface->face)->vface = NULL;
1139                 vface->face = NULL;
1140                 send_verse_face_delete(vface);
1141                 vface = vface->next;
1142         }
1143         /* mark all verse faces waiting in sending queue as deleted,
1144          * send delete command, when this verse face was changed */
1145         vface = face_vlayer->queue.first;
1146         while(vface) {
1147                 if(vface->face) ((EditFace*)vface->face)->vface = NULL;
1148                 vface->face = NULL;
1149                 if(vface->flag & FACE_CHANGED)
1150                         send_verse_face_delete(vface);
1151                 else {
1152                         vface->flag |= FACE_DELETED;
1153                 }
1154                 vface = vface->next;
1155         }
1156         
1157         /* send delete command to all received verse vertexes */
1158         vvert = vert_vlayer->dl.lb.first;
1159         while(vvert) {
1160                 if(vvert->vertex) ((EditVert*)vvert->vertex)->vvert = NULL;
1161                 vvert->vertex = NULL;
1162                 send_verse_vertex_delete(vvert);
1163                 vvert = vvert->next;
1164         }
1165         /* mark all verse vertexes waiting in sending queue as deleted
1166          * ... verse vertexes will be deleted, when received */
1167         vvert = vert_vlayer->queue.first;
1168         while(vvert) {
1169                 if(vvert->vertex) ((EditVert*)vvert->vertex)->vvert = NULL;
1170                 vvert->vertex = NULL;
1171                 vvert = vvert->next;
1172         }
1173 }
1174
1175 /*
1176  * duplicate geometry verse node, this can be handy, when you duplicate some
1177  * object or make object single user
1178  */
1179 VNode *create_geom_vnode_from_geom_vnode(VNode *vnode)
1180 {
1181         struct VNode *n_vnode;                          /* new verse geometry node */
1182         struct VGeomData *geom_data;                    /* new geometry data */
1183         struct VLayer *n_vert_vlayer, *n_face_vlayer;   /* new vertex and polygon layer */
1184         struct VLayer *vert_vlayer, *face_vlayer;
1185         struct VerseVert *n_vvert, *vvert;
1186         struct VerseFace *n_vface, *vface;
1187         int i;
1188
1189         if(!vnode) return NULL;
1190
1191         if(vnode->type != V_NT_GEOMETRY) return NULL;
1192         
1193         /* create new verse node, when no one exist */
1194         n_vnode= create_verse_node(vnode->session, -1 , V_NT_GEOMETRY, VN_OWNER_MINE);
1195         /* create new geometry data */
1196         geom_data = create_geometry_data();
1197         n_vnode->data = (void*)geom_data;
1198         
1199         /* set up name of VerseNode */
1200         n_vnode->name = (char*)MEM_mallocN(sizeof(char*)*(strlen(vnode->name)-1), "new geom node name");
1201         n_vnode->name[0] = '\0';
1202         strcat(n_vnode->name, vnode->name);
1203         
1204         /* add node to sending queue */
1205         add_item_to_send_queue(&(vnode->session->queue), n_vnode, VERSE_NODE);
1206         
1207         /* create vertex verse layer */
1208         n_vert_vlayer = create_verse_layer(n_vnode, 0, "vertex", VN_G_LAYER_VERTEX_XYZ, 0, 0);
1209         add_item_to_send_queue(&(geom_data->queue), n_vert_vlayer, VERSE_LAYER);
1210
1211         /* create polygon verse layer */
1212         n_face_vlayer = create_verse_layer(n_vnode, 1, "polygon", VN_G_LAYER_POLYGON_CORNER_UINT32, 0, 0);
1213         add_item_to_send_queue(&(geom_data->queue), n_face_vlayer, VERSE_LAYER);
1214
1215         /* find vertex layer of old verse node */
1216         vert_vlayer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
1217         /* find polygon layer of old verse node */
1218         face_vlayer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
1219
1220         /* duplicate verse vertexes */
1221         for(i=0, vvert = (VerseVert*)vert_vlayer->dl.lb.first; vvert; vvert = vvert->next, i++) {
1222                 n_vvert = create_verse_vertex(n_vert_vlayer, i, vvert->co[0], vvert->co[1], vvert->co[2]);
1223                 vvert->tmp.vvert = n_vvert;
1224                 add_item_to_send_queue(&(n_vert_vlayer->queue), n_vvert, VERSE_VERT);
1225         }
1226
1227         /* duplicate verse faces (polyons) */
1228         for(i=0, vface = (VerseFace*)face_vlayer->dl.lb.first; vface; vface = vface->next, i++) {
1229                 n_vface = create_verse_face(n_face_vlayer, i, -1, -1, -1, -1);
1230                 n_vface->vvert0 = vface->vvert0->tmp.vvert;
1231                 n_vface->vvert1 = vface->vvert1->tmp.vvert;
1232                 n_vface->vvert2 = vface->vvert2->tmp.vvert;
1233                 if(vface->vvert3)
1234                         n_vface->vvert3 = vface->vvert3->tmp.vvert;
1235                 else
1236                         n_vface->vvert3 = NULL;
1237                 add_item_to_send_queue(&(n_face_vlayer->queue), n_vface, VERSE_FACE);
1238         }
1239
1240         return n_vnode;
1241 }
1242
1243 /*
1244  * create new geometry node, make bindings between geometry node and editmesh,
1245  * make bindings between editverts and verseverts, make bindings between editfaces
1246  * and versefaces
1247  */
1248 VNode *create_geom_vnode_data_from_editmesh(VerseSession *session, EditMesh *em)
1249 {
1250         struct VNode *vnode;
1251         struct Mesh *me;
1252         struct VGeomData *geom_data;
1253         struct VLayer *vert_vlayer, *face_vlayer;
1254         
1255         if(!session) return NULL;
1256         
1257         if(!em) return NULL;
1258
1259         /* some verse geometry node already exists */
1260         if(em->vnode) return NULL;
1261
1262         /* we will need mesh too (mesh name, creating bindings between verse node, etc.) */
1263         me = get_mesh(G.obedit);
1264         
1265         /* create new verse node, when no one exist */
1266         vnode = create_verse_node(session, -1 , V_NT_GEOMETRY, VN_OWNER_MINE);
1267         /* create new geometry data */
1268         geom_data = create_geometry_data();
1269         vnode->data = (void*)geom_data;
1270         
1271         /* set up name of VerseNode */
1272         vnode->name = (char*)MEM_mallocN(sizeof(char*)*(strlen(me->id.name)-1), "geom node name");
1273         vnode->name[0] = '\0';
1274         strcat(vnode->name, me->id.name+2);
1275
1276         /* set up bindings */
1277         me->vnode = (void*)vnode;
1278         em->vnode = (void*)vnode;
1279         geom_data->mesh = (void*)me;
1280         geom_data->editmesh = (void*)em;
1281
1282         /* add node to sending queue */
1283         add_item_to_send_queue(&(session->queue), vnode, VERSE_NODE);
1284
1285         /* create vertex verse layer */
1286         vert_vlayer = create_verse_layer(vnode, 0, "vertex", VN_G_LAYER_VERTEX_XYZ, 0, 0);
1287         add_item_to_send_queue(&(geom_data->queue), vert_vlayer, VERSE_LAYER);
1288         
1289         /* create polygon verse layer */
1290         face_vlayer = create_verse_layer(vnode, 1, "polygon", VN_G_LAYER_POLYGON_CORNER_UINT32, 0, 0);
1291         add_item_to_send_queue(&(geom_data->queue), face_vlayer, VERSE_LAYER);
1292
1293         /* create all verse verts and add them to sending queue */
1294         createAllVerseVerts(vnode, vert_vlayer);
1295
1296         /* create all verse faces and add tehm to sending queue */
1297         createAllVerseFaces(vnode, face_vlayer);
1298
1299         return vnode;
1300 }
1301
1302 /*
1303  * create new geometry node, make bindings between geometry node and mesh and 
1304  * fill geometry node with new data based at mesh data
1305  */
1306 VNode *create_geom_vnode_data_from_mesh(VerseSession *session, Mesh *me)
1307 {
1308         struct VNode *vnode;
1309         struct VGeomData *geom_data;
1310         struct VLayer *vert_vlayer, *face_vlayer;
1311         struct VerseVert *vvert, **vverts;
1312         struct VerseFace *vface;
1313         struct MVert *mvert;
1314         struct MFace *mface;
1315         int i;
1316
1317         if(!session) return NULL;
1318         
1319         if(!me) return NULL;
1320
1321         /* some verse geometry node already exists */
1322         if(me->vnode) return NULL;
1323         
1324         /* create new verse node, when no one exist */
1325         vnode = create_verse_node(session, -1 , V_NT_GEOMETRY, VN_OWNER_MINE);
1326         /* create new geometry data */
1327         geom_data = create_geometry_data();
1328         vnode->data = (void*)geom_data;
1329         
1330         /* set up name of VerseNode */
1331         vnode->name = (char*)MEM_mallocN(sizeof(char*)*(strlen(me->id.name)-1), "geom node name");
1332         vnode->name[0] = '\0';
1333         strcat(vnode->name, me->id.name+2);
1334
1335         /* set up bindings */
1336         me->vnode = (void*)vnode;
1337         geom_data->mesh = (void*)me;
1338         
1339         /* add node to sending queue */
1340         add_item_to_send_queue(&(session->queue), vnode, VERSE_NODE);
1341
1342         /* create vertex verse layer */
1343         vert_vlayer = create_verse_layer(vnode, 0, "vertex", VN_G_LAYER_VERTEX_XYZ, 0, 0);
1344         add_item_to_send_queue(&(geom_data->queue), vert_vlayer, VERSE_LAYER);
1345         
1346         /* create polygon verse layer */
1347         face_vlayer = create_verse_layer(vnode, 1, "polygon", VN_G_LAYER_POLYGON_CORNER_UINT32, 0, 0);
1348         add_item_to_send_queue(&(geom_data->queue), face_vlayer, VERSE_LAYER);
1349
1350         /* temporary array of VerseVerts */
1351         vverts = (VerseVert**)MEM_mallocN(sizeof(VerseVert*)*me->totvert,"temp array of vverts");
1352         
1353         /* "fill vertex layer with vertexes" and add them to sending queue (send them to verse server) */
1354         for(i=0, mvert=me->mvert; i<me->totvert; i++, mvert++) {
1355                 vverts[i] = vvert = create_verse_vertex(vert_vlayer, i, mvert->co[0], mvert->co[1], mvert->co[2]);
1356                 add_item_to_send_queue(&(vert_vlayer->queue), vvert, VERSE_VERT);
1357         }
1358
1359         /* "fill face/polygon layer with faces" and them to sending queue */
1360         for(i=0, mface = me->mface; i<me->totface; i++, mface++) {
1361                 if(mface->v4) {
1362                         vface = create_verse_face(face_vlayer, i, mface->v1, mface->v2, mface->v3, mface->v4);
1363                         vface->vvert0 = vverts[mface->v1];
1364                         vface->vvert1 = vverts[mface->v2];
1365                         vface->vvert2 = vverts[mface->v3];
1366                         vface->vvert3 = vverts[mface->v4];
1367                         vface->counter = 4;
1368                 }
1369                 else {
1370                         vface = create_verse_face(face_vlayer, i, mface->v1, mface->v2, mface->v3, -1);
1371                         vface->vvert0 = vverts[mface->v1];
1372                         vface->vvert1 = vverts[mface->v2];
1373                         vface->vvert2 = vverts[mface->v3];
1374                         vface->counter = 3;
1375                 }
1376                 add_item_to_send_queue(&(face_vlayer->queue), vface, VERSE_FACE);
1377         }
1378
1379         MEM_freeN(vverts);
1380
1381         return vnode;
1382 }
1383
1384 /*
1385  * creates Mesh from verse geometry node and create bindings
1386  * between them
1387  */
1388 Mesh *create_mesh_from_geom_node(VNode *vnode)
1389 {
1390         struct Mesh *me;
1391
1392         if(vnode->type != V_NT_GEOMETRY) return NULL;
1393
1394         /* add new empty mesh*/
1395         me = add_mesh("Mesh");
1396         /* set up bindings between mesh and verse node */
1397         me->vnode = (void*)vnode;
1398         ((VGeomData*)vnode->data)->mesh = (void*)me;
1399
1400         return me;
1401 }
1402
1403 /*
1404  * create mesh from verse mesh
1405  */
1406 void create_meshdata_from_geom_node(Mesh *me, VNode *vnode)
1407 {
1408         struct VLayer *vert_vlayer, *face_vlayer;
1409         struct VerseVert *vvert;
1410         struct VerseFace *vface;
1411         struct MVert *mvert;
1412         struct MFace *mface;
1413         struct EdgeHash *edges;
1414         int index;
1415
1416         if(!me || !vnode) return;
1417
1418         if(vnode->type != V_NT_GEOMETRY) return;
1419
1420         vert_vlayer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
1421         face_vlayer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
1422
1423         CustomData_free(&me->vdata, me->totvert);
1424         CustomData_free(&me->edata, me->totedge);
1425         CustomData_free(&me->fdata, me->totface);
1426         mesh_update_customdata_pointers(me);
1427
1428         if(me->mselect) {
1429                 MEM_freeN(me->mselect);
1430                 me->mselect = NULL;
1431         }
1432
1433         me->totvert = vert_vlayer ? vert_vlayer->dl.da.count : 0;
1434         me->totface = face_vlayer ? face_vlayer->dl.da.count : 0;
1435         me->totselect = 0;
1436
1437         CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, me->totvert);
1438         CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, NULL, me->totface);
1439         mesh_update_customdata_pointers(me);
1440
1441         mvert = me->mvert;
1442         mface = me->mface;
1443
1444         index = 0;
1445         vvert = vert_vlayer ? vert_vlayer->dl.lb.first : NULL;
1446         while(vvert) {
1447                 VECCOPY(mvert->co, vvert->co);
1448                 VECCOPY(mvert->no, vvert->no);
1449                 mvert->flag = 0;
1450                 mvert->mat_nr = 0;
1451                 vvert->tmp.index = index++;
1452                 vvert = vvert->next;
1453                 mvert++;
1454         }
1455
1456         edges = BLI_edgehash_new();
1457         vface = face_vlayer ? face_vlayer->dl.lb.first : NULL;
1458         while(vface) {
1459                 mface->v1 = vface->vvert0->tmp.index;
1460                 mface->v2 = vface->vvert1->tmp.index;
1461                 mface->v3 = vface->vvert2->tmp.index;
1462
1463                 if(!BLI_edgehash_haskey(edges, mface->v1, mface->v2))
1464                         BLI_edgehash_insert(edges, mface->v1, mface->v2, NULL);
1465                 if(!BLI_edgehash_haskey(edges, mface->v2, mface->v3))
1466                         BLI_edgehash_insert(edges, mface->v2, mface->v3, NULL);
1467                 if(vface->vvert3) {
1468                         mface->v4 = vface->vvert3->tmp.index;
1469                         if(!BLI_edgehash_haskey(edges, mface->v3, mface->v4))
1470                                 BLI_edgehash_insert(edges, mface->v3, mface->v4, NULL);
1471                         if(!BLI_edgehash_haskey(edges, mface->v4, mface->v1))
1472                                 BLI_edgehash_insert(edges, mface->v4, mface->v1, NULL);
1473                 } else {
1474                         mface->v4 = 0;
1475                         if(!BLI_edgehash_haskey(edges, mface->v3, mface->v1))
1476                                 BLI_edgehash_insert(edges, mface->v3, mface->v1, NULL);
1477                 }
1478
1479                 mface->flag = 0;
1480                 mface->pad = 0;
1481                 mface->mat_nr = 0;
1482                 mface->edcode = 0;
1483
1484                 /* index 0 isn't allowed at location 3 or 4 */
1485                 test_index_face(mface, NULL, 0, vface->vvert3?4:3);
1486 /*              printf("\t mface: %d, %d, %d, %d\n", mface->v1, mface->v2, mface->v3, mface->v4);*/
1487                 
1488                 vface = vface->next;
1489                 mface++;
1490         }
1491         
1492         me->totedge = BLI_edgehash_size(edges);
1493
1494         if(me->totedge) {
1495                 EdgeHashIterator *i;
1496                 MEdge *medge = me->medge = CustomData_add_layer(&me->edata, CD_MEDGE, CD_CALLOC, NULL, me->totedge);
1497
1498                 for(i = BLI_edgehashIterator_new(edges); !BLI_edgehashIterator_isDone(i); BLI_edgehashIterator_step(i), ++medge) {
1499                         BLI_edgehashIterator_getKey(i, (int*)&medge->v1, (int*)&medge->v2);
1500                         medge->crease = medge->pad = medge->flag = 0;
1501                 }
1502                 BLI_edgehashIterator_free(i);
1503         }
1504
1505         BLI_edgehash_free(edges, NULL);
1506
1507         mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
1508 }
1509
1510 /*
1511  * Create EditMesh from VerseMesh and keep system in consitant state, this
1512  * function is called, when edit mode is entered ... edit mesh is generated
1513  * from verse mesh (not from Mesh: (Mesh*)ob->data)
1514  */
1515 void create_edit_mesh_from_geom_node(VNode *vnode)
1516 {
1517         struct VLayer *vert_layer, *face_layer;
1518         struct VerseVert *vvert;
1519         struct VerseFace *vface;
1520         struct Mesh *me;
1521         struct EditVert *eve, *eve0, *eve1, *eve2, *eve3;
1522         struct EditFace *efa;
1523         unsigned int keyindex;
1524
1525         if(!(G.obedit && G.obedit->type==OB_MESH)) return;
1526         me = (Mesh*)G.obedit->data;
1527         if(vnode!=(VNode*)me->vnode || vnode->type!=V_NT_GEOMETRY) return;
1528
1529         vert_layer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
1530         face_layer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
1531
1532         if(!(vert_layer && face_layer)) return;
1533
1534         waitcursor(1);
1535
1536         /* free old editMesh */
1537         free_editMesh(G.editMesh);
1538         
1539         G.editMesh->vnode = NULL;
1540
1541         vvert = vert_layer->dl.lb.first;
1542
1543         keyindex = 0;
1544
1545         /* create all EditVerts */
1546         while(vvert) {
1547                 eve = addvertlist(vvert->co, NULL);
1548
1549                 eve->f = 0;
1550                 eve->h = 0;
1551                 eve->data = NULL;
1552                 eve->keyindex = keyindex;
1553                 eve->vvert = (void*)vvert;
1554
1555                 vvert->vertex = (void*)eve;
1556
1557                 keyindex++;
1558                 vvert = vvert->next;
1559         }
1560
1561         vface = face_layer->dl.lb.first;
1562
1563         /* create all EditFaces and EditEdges */
1564         while(vface) {
1565                 if(vface->vvert0) eve0= vface->vvert0->vertex;
1566                 else eve0 = NULL;
1567                 if(vface->vvert1) eve1= vface->vvert1->vertex;
1568                 else eve1 = NULL;
1569                 if(vface->vvert2) eve2= vface->vvert2->vertex;
1570                 else eve2 = NULL;
1571                 if(vface->vvert3) eve3= vface->vvert3->vertex;
1572                 else eve3 = NULL;
1573
1574                 efa= addfacelist(eve0, eve1, eve2, eve3, NULL, NULL);
1575                 if(efa) {
1576                         efa->f = 0;
1577                         efa->h = 0;
1578                         efa->vface = (void*)vface;
1579                         vface->face = (void*)efa;
1580                 }
1581                 vface = vface->next;
1582         }
1583         
1584         countall();
1585
1586         recalc_editnormals();
1587
1588         G.editMesh->vnode = (void*)vnode;
1589         ((VGeomData*)vnode->data)->editmesh = (void*)G.editMesh;
1590
1591         waitcursor(0);
1592 }
1593
1594 /*
1595  * destroy bindings between EditMesh and VerseMesh and send delete commands
1596  * for all VerseVerts and VerseFaces to verse server, Verse Node has to be
1597  * geometry node
1598  */
1599
1600 void destroy_verse_mesh(VNode *vnode)
1601 {
1602         struct VLayer *vert_vlayer, *face_vlayer;
1603         struct VerseFace *vface;
1604         struct VerseVert *vvert;
1605
1606         if(vnode->type != V_NT_GEOMETRY) return;
1607
1608         face_vlayer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
1609         vface = face_vlayer->dl.lb.first;
1610
1611         while(vface) {
1612                 ((EditFace*)vface->face)->vface = NULL;
1613                 vface->face = NULL;     
1614                 vface = vface->next;
1615         }
1616
1617         vert_vlayer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
1618         vvert = vert_vlayer->dl.lb.first;
1619
1620         while(vvert) {
1621                 ((EditVert*)vvert->vertex)->vvert = NULL;
1622                 vvert->vertex = NULL;
1623                 vvert = vvert->next;
1624         }
1625
1626         destroy_geometry(vnode);
1627 }
1628
1629 #endif
1630