Two in one:
[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         /* don't send position of verse vertex to verse server, because it could create
471          * new vertex */
472         if(vvert->flag & VERT_RECEIVED && !(vvert->flag & VERT_DELETED)) {
473                 if(vvert->flag & VERT_LOCKED) {
474                         /* when position of verse vert was sent to verse server
475                          * and it wasn't received yet, then mark sent position
476                          * as obsolete ... blender will automaticaly send actual
477                          * position, when old will be received */
478                         vvert->flag |= VERT_POS_OBSOLETE;
479 /*                      printf("\tsend_versevert_pos: %d mark OBSOLETE\n", vvert->id);*/
480                 }
481                 else {
482                         struct EditVert *eve = (EditVert*)vvert->vertex;
483                         /* send position to verse server, when it is different from actual position */
484                         if(eve && (eve->co[0]!=vvert->co[0] || eve->co[1]!=vvert->co[1] || eve->co[2]!=vvert->co[2])) {
485                                 /* lock vertex and send its position to verse server */
486                                 vvert->flag |= VERT_LOCKED;
487                                 VECCOPY(vvert->co, eve->co);
488 /*                              printf("\tsend_versevert_pos: %d send and LOCK \n", vvert->id);*/
489                                 send_verse_vertex(vvert);
490                         }
491                 }
492         }
493
494         if(!(vvert->flag & VERT_RECEIVED) && (vvert->flag & VERT_LOCKED)) {
495                 struct EditVert *eve = (EditVert*)vvert->vertex;
496                 if(eve && (eve->co[0]!=vvert->co[0] || eve->co[1]!=vvert->co[1] || eve->co[2]!=vvert->co[2])) {
497 /*                      printf("\tsend_versevert_pos: %d mark VERT_POS_OBSOLETE\n", vvert->id); */
498                         vvert->flag |= VERT_POS_OBSOLETE;
499                 }
500         }
501
502         verse_callback_update(0);
503 }
504
505 /*
506  * create new VerseVert due to information about EditVert,
507  * put VerseVert to queue ... send to verse host (server)
508  */
509 void createVerseVert(EditVert *eve)
510 {
511         if(G.editMesh->vnode) {
512                 struct VLayer *vlayer = find_verse_layer_type((VGeomData*)((VNode*)G.editMesh->vnode)->data, VERTEX_LAYER);
513                 createVerseVertNL(eve, (VNode*)G.editMesh->vnode, vlayer);
514         }
515         else eve->vvert = NULL;
516 }
517
518 /*
519  * create new VerseVert due to information about EditVert,
520  * put VerseVert to queue ... send to verse host (server)
521  * NL version of function (infomration about verse node and
522  * layer is known ... optimalisation)
523  */
524 void createVerseVertNL(EditVert *eve, VNode *vnode, VLayer *vlayer)
525 {
526         struct VerseVert *vvert;
527
528         vvert = create_verse_vertex(vlayer, vlayer->counter, eve->co[0], eve->co[1], eve->co[2]);
529
530         vvert->vertex = (void*)eve;
531         eve->vvert = (void*)vvert;
532
533         vvert->flag |= VERT_LOCKED;
534
535 /*      printf("\tsend_versevert_pos: %d create and LOCK \n", vvert->id);*/
536
537         /* add vvert to sending queue */
538         add_item_to_send_queue(&(vlayer->queue), (void*)vvert, VERSE_VERT);
539 }
540
541 /*
542  * create new verse vertexes due to all vertexes and send all of them to verse server 
543  */
544 static void createAllVerseVerts(VNode *vnode, VLayer *vlayer)
545 {
546         if(G.obedit) {
547                 struct EditMesh *em;
548                 struct EditVert *eve;
549
550                 em = G.editMesh;
551                 eve = em->verts.first;
552
553                 /* push all EditVertexes to the verse server */
554                 while(eve){
555                         createVerseVertNL(eve, vnode, vlayer);
556                         eve = eve->next;
557                 }
558         }
559 }
560
561 /*
562  * unsubscribe from verse geometry layers of verse geometry node
563  * and clear bindings between verse node and mesh
564  */
565 void unsubscribe_from_geom_node(VNode *vnode)
566 {
567         struct VerseSession *session = vnode->session;
568         
569         struct VLayer *vlayer = ((VGeomData*)vnode->data)->layers.lb.first;
570         
571         if(vnode->type != V_NT_GEOMETRY) return;
572         
573         /* free bindings between verse node and blender mesh*/  
574         if(((VGeomData*)vnode->data)->mesh) {
575                 ((Mesh*)((VGeomData*)vnode->data)->mesh)->vnode = NULL;
576                 ((VGeomData*)vnode->data)->mesh = NULL;
577         }
578
579         /* free bindings between verse node and blender editmesh */
580         if(((VGeomData*)vnode->data)->editmesh) {
581                 ((EditMesh*)((VGeomData*)vnode->data)->editmesh)->vnode = NULL;
582                 ((VGeomData*)vnode->data)->editmesh = NULL;
583         }
584         
585         /* free all verse layer data and unsubscribe from all layers */
586         while(vlayer) {
587                 BLI_dlist_reinit(&(vlayer->dl));
588                 BLI_freelistN(&(vlayer->queue));
589                 BLI_freelistN(&(vlayer->orphans));
590
591                 if(session->flag & VERSE_CONNECTED)
592                         verse_send_g_layer_unsubscribe(vnode->id, vlayer->id);
593
594                 vlayer = vlayer->next;
595         }
596
597 }
598
599 /* ===================================================================================
600  *
601  *             Function executed after execution of callback functions
602  *
603  * ===================================================================================*/
604
605 /*
606  * Actions executed after new VerseLayer is created
607  */
608 void post_layer_create(struct VLayer *vlayer)
609 {
610         /* if we are owners of VerseNode, then push geometry to verse server */
611         if(vlayer->vnode->owner_id == VN_OWNER_MINE) {
612                 switch(vlayer->type){
613                         case VN_G_LAYER_VERTEX_XYZ:
614 /*                              if(vlayer->id==0) createAllVerseVerts(vlayer->vnode, vlayer);
615                                 break;*/
616                         case VN_G_LAYER_POLYGON_CORNER_UINT32:
617 /*                              if(vlayer->id==1) createAllVerseFaces(vlayer->vnode, vlayer);
618                                 break;*/
619                         case VN_G_LAYER_VERTEX_UINT32:
620                         case VN_G_LAYER_VERTEX_REAL:
621                         case VN_G_LAYER_POLYGON_CORNER_REAL:
622                         case VN_G_LAYER_POLYGON_FACE_UINT8:
623                         case VN_G_LAYER_POLYGON_FACE_UINT32:
624                         case VN_G_LAYER_POLYGON_FACE_REAL:
625                                 break;
626                 }
627         }
628         else {
629                 switch(vlayer->type) {
630                         case VN_G_LAYER_VERTEX_XYZ:
631                         case VN_G_LAYER_POLYGON_CORNER_UINT32:
632                         case VN_G_LAYER_VERTEX_UINT32:
633                         case VN_G_LAYER_VERTEX_REAL:
634                         case VN_G_LAYER_POLYGON_CORNER_REAL:
635                         case VN_G_LAYER_POLYGON_FACE_UINT8:
636                         case VN_G_LAYER_POLYGON_FACE_UINT32:
637                         case VN_G_LAYER_POLYGON_FACE_REAL:
638                                 break;
639                 }
640         }
641 }
642
643 /*
644  * Actions after destroying of VerseLayer
645  */
646 void post_layer_destroy(struct VLayer *vlayer)
647 {
648 }
649
650 /*
651  * Actions executed after creating of new VerseVert, when object is in edit
652  * mode, and this client didn't create this VerseVert (vvert->vertex is NULL),
653  * then new editvert is created
654  */
655 void post_vertex_create(VerseVert *vvert)
656 {
657         struct VNode *obj_vnode;
658         struct VNode *geom_vnode = vvert->vlayer->vnode;
659         struct EditMesh *em=NULL;
660
661         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
662                 em = (EditMesh*)((VGeomData*)geom_vnode->data)->editmesh;
663         }
664
665         /* when vert was changed during sending to verse server, then
666          * we have to send it to verse server again */
667         if(vvert->flag & VERT_POS_OBSOLETE) {
668                 vvert->flag &= ~VERT_POS_OBSOLETE;
669                 
670                 if(em && (vvert->vertex)) {
671                         struct EditVert *eve = (EditVert*)vvert->vertex;
672                         VECCOPY(vvert->co, eve->co);
673 /*                      printf("\tpost_vertex_create: %d send and NOT_OBSOLETE\n", vvert->id);*/
674                         send_verse_vertex(vvert);
675                         verse_callback_update(0);
676
677                         return;
678                 }
679         }
680         
681         if(em && !(vvert->vertex)) {
682                 struct EditVert *eve;
683
684                 /* to prevent never ending loop of sending and receiving
685                  * vertexes, because addvertlist() sends new vertex to verse
686                  * server if em->vnode isn't NULL */
687                 em->vnode = NULL;
688                 eve = addvertlist(vvert->co, NULL);
689                 em->vnode = (void*)geom_vnode;
690
691                 eve->vvert = (void*)vvert;
692                 vvert->vertex = (void*)eve;
693         
694                 countall();
695
696                 recalc_editnormals();
697         }
698
699         if(((VGeomData*)geom_vnode->data)->vlink) {
700                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
701                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
702
703                 allqueue(REDRAWVIEW3D, 1);
704         }
705 }
706
707 /*
708  * Actions executed, when position of VerseVert was changed
709  * position of EditVert is changed in edit mode
710  */
711 void post_vertex_set_xyz(VerseVert *vvert)
712 {
713         struct VNode *obj_vnode;
714         struct VNode *geom_vnode = vvert->vlayer->vnode;
715         struct EditVert *eve = NULL;
716
717         /* when vert was changed during sending to verse server, then
718          * we have to send it to verse server again */
719         if(vvert->flag & VERT_POS_OBSOLETE) {
720                 if(vvert->vertex) {
721                         vvert->flag &= ~VERT_POS_OBSOLETE;
722                         vvert->flag |= VERT_LOCKED;
723
724                         eve = (EditVert*)vvert->vertex;
725                         VECCOPY(vvert->co, eve->co);
726 /*                      printf("\tpost_vertex_set_xyz: %d send and NOT_OBSOLETE\n", vvert->id); */
727                         send_verse_vertex(vvert);
728                         verse_callback_update(0);
729                 }
730                 else {
731                         printf("\terror: vvert->vertex shouldn't be NULL\n");
732                 }
733
734                 return;
735         }
736
737         /* when shared object is in edit mode, then update editmesh */  
738         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
739                 if(vvert->vertex) {
740                         eve = (EditVert*)vvert->vertex;
741 /*                      printf("\tupdate pos of edit vert %d\n", vvert->id); */
742                         VECCOPY(eve->co, vvert->co);
743                         recalc_editnormals();
744                 }
745                 else {
746                         printf("\terror: vvert->vertex shouldn't be NULL\n");
747                 }
748         }
749
750         if(((VGeomData*)geom_vnode->data)->vlink) {
751                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
752                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
753
754                 allqueue(REDRAWVIEW3D, 1);
755         }
756 }
757
758 /*
759  * Actions executed after deleting of VerseVert
760  */
761 void post_vertex_delete(VerseVert *vvert)
762 {
763         struct VNode *obj_vnode;
764         struct VNode *geom_vnode = vvert->vlayer->vnode;
765         struct EditMesh *em = NULL;
766         struct EditEdge *ed, *edn;
767         struct EditVert *eve = NULL;
768
769         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
770                 em = (EditMesh*)((VGeomData*)geom_vnode->data)->editmesh;
771                 eve = (EditVert*)vvert->vertex;
772         }
773
774         if(em && eve) {
775                 /*printf("\tPOST_VERTEX_DELETE()\n");*/
776
777                 /* delete all edges needing eve vertex */
778                 ed = em->edges.first;
779                 while(ed) {
780                         edn = ed->next;
781                         if(ed->v1==eve || ed->v2==eve) {
782                                 remedge(ed);
783                                 free_editedge(ed);
784                         }
785                         ed = edn;
786                 }
787
788                 eve->vvert = NULL;
789                 BLI_remlink(&em->verts, eve);
790                 free_editvert(eve);
791                 vvert->vertex = NULL;
792         
793                 countall();
794
795                 recalc_editnormals();
796         }
797         
798         if(((VGeomData*)geom_vnode->data)->vlink) {
799                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
800                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
801
802                 allqueue(REDRAWVIEW3D, 1);
803         }
804 }
805
806 /*
807  * free constraint between VerseVert and EditVert
808  */
809 void post_vertex_free_constraint(VerseVert *vvert)
810 {
811         if(vvert->vertex) {
812                 ((EditVert*)vvert->vertex)->vvert=NULL;
813                 vvert->vertex=NULL;
814         }
815 }
816
817 /*
818  * Action executed after setting up uint8 value of polygon
819  */
820 void post_polygon_set_uint8(VerseFace *vface)
821 {
822 }
823
824 /*
825  * Action executed after creating of new VerseFace
826  */
827 void post_polygon_create(VerseFace *vface)
828 {
829         struct VNode *obj_vnode;
830         struct VNode *geom_vnode = vface->vlayer->vnode;
831         struct EditMesh *em = NULL;
832
833         /* if verse face was set as deleted during sending to verse server, then send
834          * delete command to verse server now ... we know verse face id */
835 /*      if(vface->flag & FACE_DELETED) {
836                 send_verse_face_delete(vface);
837                 return;
838         }*/
839         
840         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
841                 em = (EditMesh*)((VGeomData*)geom_vnode->data)->editmesh;
842         }
843
844         /* when face was changed during sending to verse server, then
845          * we have to send it to verse server again */
846         if(vface->flag & FACE_OBSOLETE) {
847                 vface->flag &= ~FACE_OBSOLETE;
848                 sync_verseface_with_editface(vface->vlayer, vface);
849                 return;
850         }
851
852         if(em && !(vface->face) && (vface->counter==0)) {
853                 struct VLayer *vlayer;
854                 struct VerseVert *vvert;
855                 struct EditFace *efa;
856                 struct EditVert *eves[4]={NULL, NULL, NULL, NULL};
857                 uint32 vert_ids[4]={vface->v0, vface->v1, vface->v2, vface->v3};
858                 int i;
859
860                 /*printf("\tPOST_POLYGON_CREATE()\n");*/
861
862                 vlayer = find_verse_layer_type((VGeomData*)geom_vnode->data, VERTEX_LAYER);
863
864                 for(i=0; i<4; i++) {
865                         if(vert_ids[i] != -1) {
866                                 vvert = BLI_dlist_find_link(&(vlayer->dl), vert_ids[i]);
867                                 if(vvert) eves[i] = (EditVert*)vvert->vertex;
868                         }
869                 }
870
871                 /* to prevent never ending loop of sending and receiving
872                  * faces, because addfacelist() sends new face to verse
873                  * server if em->vnode isn't NULL */
874                 em->vnode = NULL;
875                 efa = addfacelist(eves[0], eves[1], eves[2], eves[3], NULL, NULL);
876                 em->vnode = geom_vnode;
877
878                 if(efa) {
879                         efa->vface = vface;
880                         vface->face = efa;
881                 }
882         
883                 countall();
884
885                 recalc_editnormals();
886         }
887
888         if(((VGeomData*)geom_vnode->data)->vlink) {
889                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
890                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
891
892                 allqueue(REDRAWVIEW3D, 1);
893         }
894 }
895
896 /*
897  * Action executed after changes of VerseFace
898  * ... order of vertexes was fliped, etc.
899  */
900 void post_polygon_set_corner(VerseFace *vface)
901 {
902         struct VNode *obj_vnode;
903         struct VNode *geom_vnode = vface->vlayer->vnode;
904         struct EditMesh *em = NULL;
905         struct EditFace *efa = NULL;
906         struct EditEdge *eed, *eedn;
907
908         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
909                 em = (EditMesh*)((VGeomData*)geom_vnode->data)->editmesh;
910                 efa = (EditFace*)vface->face;
911         }
912
913         if(em && efa) {
914
915                 /* when face was changed during sending to verse server, then
916                  * we have to send it to verse server again */
917                 if(vface->flag & FACE_OBSOLETE) {
918                         vface->flag &= ~FACE_OBSOLETE;
919                         sync_verseface_with_editface(vface->vlayer, vface);
920                         return;
921                 }
922
923                 /* mark all edges, which are part of face efa */
924                 efa->e1->f2 = 1;
925                 efa->e2->f2 = 1;
926                 efa->e3->f2 = 1;
927                 if(efa->e4) efa->e4->f2 = 1;
928
929                 /* change pointers at EdtitVerts and decrease counters of "old"
930                  * VerseVertexes reference ... less VerseFaces will need them */
931                 if(vface->vvert0 != efa->v1->vvert)
932                         efa->v1 = (EditVert*)vface->vvert0->vertex;
933                 if(vface->vvert1 != efa->v2->vvert)
934                         efa->v2 = (EditVert*)vface->vvert1->vertex;
935                 if(vface->vvert2 != efa->v3->vvert)
936                         efa->v3 = (EditVert*)vface->vvert2->vertex;
937                 if(efa->v4) {
938                         if(!vface->vvert3)
939                                 efa->v4 = NULL;
940                         else if(vface->vvert3 != efa->v4->vvert)
941                                 efa->v4 = (EditVert*)vface->vvert3->vertex;
942                 }
943
944                 /* change pointers at EditEdges */
945
946                 /* 1st edge */
947                 eed = findedgelist(efa->v1, efa->v2);
948                 if(eed) efa->e1 = eed;
949                 else efa->e1 = addedgelist(efa->v1, efa->v2, NULL);
950
951                 /* 2nd edge */
952                 eed = findedgelist(efa->v2, efa->v3);
953                 if(eed) efa->e2 = eed;
954                 else efa->e2 = addedgelist(efa->v2, efa->v3, NULL);
955
956                 if(efa->v4) {
957                 /* 3th edge */
958                         eed = findedgelist(efa->v2, efa->v3);
959                         if(eed) efa->e3 = eed;
960                         else efa->e3 = addedgelist(efa->v2, efa->v3, NULL);
961                         /* 4th edge */
962                         eed = findedgelist(efa->v4, efa->v1);
963                         if(eed) efa->e4 = eed;
964                         else efa->e4 = addedgelist(efa->v4, efa->v1, NULL);
965                 }
966                 else {
967                 /* 3th edge */
968                         eed = findedgelist(efa->v3, efa->v1);
969                         if(eed) efa->e3 = eed;
970                         else efa->e3 = addedgelist(efa->v3, efa->v1, NULL);
971                         /* 4th edge */
972                         efa->e4 = NULL;
973                 }
974
975                 /* unmark needed edges */
976                 efa = em->faces.first;
977                 while(efa) {
978                         efa->e1->f2 = 0;
979                         efa->e2->f2 = 0;
980                         efa->e3->f2 = 0;
981                         if(efa->e4) efa->e4->f2 = 0;
982                         efa = efa->next;
983                 }
984
985                 /* delete all unneeded edges */
986                 eed = em->edges.first;
987                 while(eed) {
988                         eedn = eed->next;
989                         if(eed->f2) {
990                                 remedge(eed);
991                                 free_editedge(eed);
992                         }
993                         eed = eedn;
994                 }
995
996                 countall();
997
998                 recalc_editnormals();
999         }
1000
1001         if(((VGeomData*)geom_vnode->data)->vlink) {
1002                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
1003                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
1004
1005                 allqueue(REDRAWVIEW3D, 1);
1006         }
1007 }
1008
1009 /*
1010  * Action executed after deleting of VerseFace
1011  */
1012 void post_polygon_delete(VerseFace *vface)
1013 {
1014         struct VNode *obj_vnode;
1015         struct VNode *geom_vnode = vface->vlayer->vnode;
1016         struct EditMesh *em = NULL;
1017         struct EditFace *efa = NULL;
1018         struct EditEdge *eed, *eedn;
1019
1020         if(G.obedit && (((Mesh*)G.obedit->data)->vnode==geom_vnode)) {
1021                 em = (EditMesh*)((VGeomData*)geom_vnode->data)->editmesh;
1022                 efa = (EditFace*)vface->face;
1023         }
1024
1025         if(em && efa) {
1026                 /*printf("\tPOST_POLYGON_DELETE()\n");*/
1027
1028                 /* mark all edges, which are part of face efa */
1029                 efa->e1->f2 = 1;
1030                 efa->e2->f2 = 1;
1031                 efa->e3->f2 = 1;
1032                 if(efa->e4) efa->e4->f2 = 1;
1033
1034                 efa->vface = NULL;
1035                 BLI_remlink(&em->faces, efa);
1036                 free_editface(efa);
1037                 vface->face = NULL;
1038
1039                 /* following two crazy loops wouldn't be neccessary if verse spec
1040                  * would support edges */
1041
1042                 /* unmark needed edges */
1043                 efa = em->faces.first;
1044                 while(efa) {
1045                         efa->e1->f2 = 0;
1046                         efa->e2->f2 = 0;
1047                         efa->e3->f2 = 0;
1048                         if(efa->e4) efa->e4->f2 = 0;
1049                         efa = efa->next;
1050                 }
1051
1052                 /* delete all unneeded edges */
1053                 eed = em->edges.first;
1054                 while(eed) {
1055                         eedn = eed->next;
1056                         if(eed->f2) {
1057                                 remedge(eed);
1058                                 free_editedge(eed);
1059                         }
1060                         eed = eedn;
1061                 }
1062         
1063                 countall();
1064         }
1065
1066         if(((VGeomData*)geom_vnode->data)->vlink) {
1067                 obj_vnode = ((VGeomData*)geom_vnode->data)->vlink->source;
1068                 DAG_object_flush_update(G.scene, (Object*)((VObjectData*)obj_vnode->data)->object, OB_RECALC_DATA);
1069
1070                 allqueue(REDRAWVIEW3D, 1);
1071         }
1072 }
1073
1074 /*
1075  * free constraint between VerseFace and EditFace
1076  */
1077 void post_polygon_free_constraint(VerseFace *vface)
1078 {
1079         if(vface->face) {
1080                 ((EditFace*)vface->face)->vface = NULL;
1081                 vface->face = NULL;
1082         }
1083 }
1084
1085 /*
1086  * free constraint between VGeomData, EditMesh and Mesh
1087  */
1088 void post_geometry_free_constraint(VNode *vnode)
1089 {
1090         if(((VGeomData*)vnode->data)->editmesh) {
1091                 G.editMesh->vnode = NULL;
1092                 ((VGeomData*)vnode->data)->editmesh = NULL;
1093         }
1094         if(((VGeomData*)vnode->data)->mesh) {
1095                 ((Mesh*)((VGeomData*)vnode->data)->mesh)->vnode = NULL;
1096                 ((VGeomData*)vnode->data)->mesh = NULL;
1097         }
1098 }
1099
1100 /* =========================================================================
1101  *
1102  *              Functions influencing whole EditMesh or VerseMesh
1103  *
1104  * ========================================================================= */
1105
1106 /*
1107  * free all bindings between EditMesh and "verse mesh" ... it is called between
1108  * restorng editmesh from undo stack
1109  */
1110 void destroy_versemesh(VNode *vnode)
1111 {
1112         struct VLayer *vert_vlayer, *face_vlayer;
1113         struct VerseVert *vvert;
1114         struct VerseFace *vface;
1115
1116         if(vnode->type != V_NT_GEOMETRY) return;
1117
1118         vert_vlayer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
1119         face_vlayer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
1120
1121
1122         /* send delete command to all received verse faces */
1123         vface = face_vlayer->dl.lb.first;
1124         while(vface) {
1125                 if(vface->face) ((EditFace*)vface->face)->vface = NULL;
1126                 vface->face = NULL;
1127                 send_verse_face_delete(vface);
1128                 vface = vface->next;
1129         }
1130         /* send delete command to all verse faces waiting in orphan list */
1131         vface = face_vlayer->orphans.first;
1132         while(vface) {
1133                 if(vface->face) ((EditFace*)vface->face)->vface = NULL;
1134                 vface->face = NULL;
1135                 send_verse_face_delete(vface);
1136                 vface = vface->next;
1137         }
1138         /* mark all verse faces waiting in sending queue as deleted,
1139          * send delete command, when this verse face was changed */
1140         vface = face_vlayer->queue.first;
1141         while(vface) {
1142                 if(vface->face) ((EditFace*)vface->face)->vface = NULL;
1143                 vface->face = NULL;
1144                 if(vface->flag & FACE_CHANGED)
1145                         send_verse_face_delete(vface);
1146                 else {
1147                         vface->flag |= FACE_DELETED;
1148                 }
1149                 vface = vface->next;
1150         }
1151         
1152         /* send delete command to all received verse vertexes */
1153         vvert = vert_vlayer->dl.lb.first;
1154         while(vvert) {
1155                 if(vvert->vertex) ((EditVert*)vvert->vertex)->vvert = NULL;
1156                 vvert->vertex = NULL;
1157                 send_verse_vertex_delete(vvert);
1158                 vvert = vvert->next;
1159         }
1160         /* mark all verse vertexes waiting in sending queue as deleted
1161          * ... verse vertexes will be deleted, when received */
1162         vvert = vert_vlayer->queue.first;
1163         while(vvert) {
1164                 if(vvert->vertex) ((EditVert*)vvert->vertex)->vvert = NULL;
1165                 vvert->vertex = NULL;
1166                 vvert = vvert->next;
1167         }
1168 }
1169
1170 /*
1171  * duplicate geometry verse node, this can be handy, when you duplicate some
1172  * object or make object single user
1173  */
1174 VNode *create_geom_vnode_from_geom_vnode(VNode *vnode)
1175 {
1176         struct VNode *n_vnode;                          /* new verse geometry node */
1177         struct VGeomData *geom_data;                    /* new geometry data */
1178         struct VLayer *n_vert_vlayer, *n_face_vlayer;   /* new vertex and polygon layer */
1179         struct VLayer *vert_vlayer, *face_vlayer;
1180         struct VerseVert *n_vvert, *vvert;
1181         struct VerseFace *n_vface, *vface;
1182         int i;
1183
1184         if(!vnode) return NULL;
1185
1186         if(vnode->type != V_NT_GEOMETRY) return NULL;
1187         
1188         /* create new verse node, when no one exist */
1189         n_vnode= create_verse_node(vnode->session, -1 , V_NT_GEOMETRY, VN_OWNER_MINE);
1190         /* create new geometry data */
1191         geom_data = create_geometry_data();
1192         n_vnode->data = (void*)geom_data;
1193         
1194         /* set up name of VerseNode */
1195         n_vnode->name = (char*)MEM_mallocN(sizeof(char*)*(strlen(vnode->name)-1), "new geom node name");
1196         n_vnode->name[0] = '\0';
1197         strcat(n_vnode->name, vnode->name);
1198         
1199         /* add node to sending queue */
1200         add_item_to_send_queue(&(vnode->session->queue), n_vnode, VERSE_NODE);
1201         
1202         /* create vertex verse layer */
1203         n_vert_vlayer = create_verse_layer(n_vnode, 0, "vertex", VN_G_LAYER_VERTEX_XYZ, 0, 0);
1204         add_item_to_send_queue(&(geom_data->queue), n_vert_vlayer, VERSE_LAYER);
1205
1206         /* create polygon verse layer */
1207         n_face_vlayer = create_verse_layer(n_vnode, 1, "polygon", VN_G_LAYER_POLYGON_CORNER_UINT32, 0, 0);
1208         add_item_to_send_queue(&(geom_data->queue), n_face_vlayer, VERSE_LAYER);
1209
1210         /* find vertex layer of old verse node */
1211         vert_vlayer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
1212         /* find polygon layer of old verse node */
1213         face_vlayer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
1214
1215         /* duplicate verse vertexes */
1216         for(i=0, vvert = (VerseVert*)vert_vlayer->dl.lb.first; vvert; vvert = vvert->next, i++) {
1217                 n_vvert = create_verse_vertex(n_vert_vlayer, i, vvert->co[0], vvert->co[1], vvert->co[2]);
1218                 vvert->tmp.vvert = n_vvert;
1219                 add_item_to_send_queue(&(n_vert_vlayer->queue), n_vvert, VERSE_VERT);
1220         }
1221
1222         /* duplicate verse faces (polyons) */
1223         for(i=0, vface = (VerseFace*)face_vlayer->dl.lb.first; vface; vface = vface->next, i++) {
1224                 n_vface = create_verse_face(n_face_vlayer, i, -1, -1, -1, -1);
1225                 n_vface->vvert0 = vface->vvert0->tmp.vvert;
1226                 n_vface->vvert1 = vface->vvert1->tmp.vvert;
1227                 n_vface->vvert2 = vface->vvert2->tmp.vvert;
1228                 if(vface->vvert3)
1229                         n_vface->vvert3 = vface->vvert3->tmp.vvert;
1230                 else
1231                         n_vface->vvert3 = NULL;
1232                 add_item_to_send_queue(&(n_face_vlayer->queue), n_vface, VERSE_FACE);
1233         }
1234
1235         return n_vnode;
1236 }
1237
1238 /*
1239  * create new geometry node, make bindings between geometry node and editmesh,
1240  * make bindings between editverts and verseverts, make bindings between editfaces
1241  * and versefaces
1242  */
1243 VNode *create_geom_vnode_data_from_editmesh(VerseSession *session, EditMesh *em)
1244 {
1245         struct VNode *vnode;
1246         struct Mesh *me;
1247         struct VGeomData *geom_data;
1248         struct VLayer *vert_vlayer, *face_vlayer;
1249         
1250         if(!session) return NULL;
1251         
1252         if(!em) return NULL;
1253
1254         /* some verse geometry node already exists */
1255         if(em->vnode) return NULL;
1256
1257         /* we will need mesh too (mesh name, creating bindings between verse node, etc.) */
1258         me = get_mesh(G.obedit);
1259         
1260         /* create new verse node, when no one exist */
1261         vnode = create_verse_node(session, -1 , V_NT_GEOMETRY, VN_OWNER_MINE);
1262         /* create new geometry data */
1263         geom_data = create_geometry_data();
1264         vnode->data = (void*)geom_data;
1265         
1266         /* set up name of VerseNode */
1267         vnode->name = (char*)MEM_mallocN(sizeof(char*)*(strlen(me->id.name)-1), "geom node name");
1268         vnode->name[0] = '\0';
1269         strcat(vnode->name, me->id.name+2);
1270
1271         /* set up bindings */
1272         me->vnode = (void*)vnode;
1273         em->vnode = (void*)vnode;
1274         geom_data->mesh = (void*)me;
1275         geom_data->editmesh = (void*)em;
1276
1277         /* add node to sending queue */
1278         add_item_to_send_queue(&(session->queue), vnode, VERSE_NODE);
1279
1280         /* create vertex verse layer */
1281         vert_vlayer = create_verse_layer(vnode, 0, "vertex", VN_G_LAYER_VERTEX_XYZ, 0, 0);
1282         add_item_to_send_queue(&(geom_data->queue), vert_vlayer, VERSE_LAYER);
1283         
1284         /* create polygon verse layer */
1285         face_vlayer = create_verse_layer(vnode, 1, "polygon", VN_G_LAYER_POLYGON_CORNER_UINT32, 0, 0);
1286         add_item_to_send_queue(&(geom_data->queue), face_vlayer, VERSE_LAYER);
1287
1288         /* create all verse verts and add them to sending queue */
1289         createAllVerseVerts(vnode, vert_vlayer);
1290
1291         /* create all verse faces and add tehm to sending queue */
1292         createAllVerseFaces(vnode, face_vlayer);
1293
1294         return vnode;
1295 }
1296
1297 /*
1298  * create new geometry node, make bindings between geometry node and mesh and 
1299  * fill geometry node with new data based at mesh data
1300  */
1301 VNode *create_geom_vnode_data_from_mesh(VerseSession *session, Mesh *me)
1302 {
1303         struct VNode *vnode;
1304         struct VGeomData *geom_data;
1305         struct VLayer *vert_vlayer, *face_vlayer;
1306         struct VerseVert *vvert, **vverts;
1307         struct VerseFace *vface;
1308         struct MVert *mvert;
1309         struct MFace *mface;
1310         int i;
1311
1312         if(!session) return NULL;
1313         
1314         if(!me) return NULL;
1315
1316         /* some verse geometry node already exists */
1317         if(me->vnode) return NULL;
1318         
1319         /* create new verse node, when no one exist */
1320         vnode = create_verse_node(session, -1 , V_NT_GEOMETRY, VN_OWNER_MINE);
1321         /* create new geometry data */
1322         geom_data = create_geometry_data();
1323         vnode->data = (void*)geom_data;
1324         
1325         /* set up name of VerseNode */
1326         vnode->name = (char*)MEM_mallocN(sizeof(char*)*(strlen(me->id.name)-1), "geom node name");
1327         vnode->name[0] = '\0';
1328         strcat(vnode->name, me->id.name+2);
1329
1330         /* set up bindings */
1331         me->vnode = (void*)vnode;
1332         geom_data->mesh = (void*)me;
1333         
1334         /* add node to sending queue */
1335         add_item_to_send_queue(&(session->queue), vnode, VERSE_NODE);
1336
1337         /* create vertex verse layer */
1338         vert_vlayer = create_verse_layer(vnode, 0, "vertex", VN_G_LAYER_VERTEX_XYZ, 0, 0);
1339         add_item_to_send_queue(&(geom_data->queue), vert_vlayer, VERSE_LAYER);
1340         
1341         /* create polygon verse layer */
1342         face_vlayer = create_verse_layer(vnode, 1, "polygon", VN_G_LAYER_POLYGON_CORNER_UINT32, 0, 0);
1343         add_item_to_send_queue(&(geom_data->queue), face_vlayer, VERSE_LAYER);
1344
1345         /* temporary array of VerseVerts */
1346         vverts = (VerseVert**)MEM_mallocN(sizeof(VerseVert*)*me->totvert,"temp array of vverts");
1347         
1348         /* "fill vertex layer with vertexes" and add them to sending queue (send them to verse server) */
1349         for(i=0, mvert=me->mvert; i<me->totvert; i++, mvert++) {
1350                 vverts[i] = vvert = create_verse_vertex(vert_vlayer, i, mvert->co[0], mvert->co[1], mvert->co[2]);
1351                 add_item_to_send_queue(&(vert_vlayer->queue), vvert, VERSE_VERT);
1352         }
1353
1354         /* "fill face/polygon layer with faces" and them to sending queue */
1355         for(i=0, mface = me->mface; i<me->totface; i++, mface++) {
1356                 if(mface->v4) {
1357                         vface = create_verse_face(face_vlayer, i, mface->v1, mface->v2, mface->v3, mface->v4);
1358                         vface->vvert0 = vverts[mface->v1];
1359                         vface->vvert1 = vverts[mface->v2];
1360                         vface->vvert2 = vverts[mface->v3];
1361                         vface->vvert3 = vverts[mface->v4];
1362                         vface->counter = 4;
1363                 }
1364                 else {
1365                         vface = create_verse_face(face_vlayer, i, mface->v1, mface->v2, mface->v3, -1);
1366                         vface->vvert0 = vverts[mface->v1];
1367                         vface->vvert1 = vverts[mface->v2];
1368                         vface->vvert2 = vverts[mface->v3];
1369                         vface->counter = 3;
1370                 }
1371                 add_item_to_send_queue(&(face_vlayer->queue), vface, VERSE_FACE);
1372         }
1373
1374         MEM_freeN(vverts);
1375
1376         return vnode;
1377 }
1378
1379 /*
1380  * creates Mesh from verse geometry node and create bindings
1381  * between them
1382  */
1383 Mesh *create_mesh_from_geom_node(VNode *vnode)
1384 {
1385         struct Mesh *me;
1386
1387         if(vnode->type != V_NT_GEOMETRY) return NULL;
1388
1389         /* add new empty mesh*/
1390         me = add_mesh();
1391         /* set up bindings between mesh and verse node */
1392         me->vnode = (void*)vnode;
1393         ((VGeomData*)vnode->data)->mesh = (void*)me;
1394
1395         return me;
1396 }
1397
1398 /*
1399  * create mesh from verse mesh
1400  */
1401 void create_meshdata_from_geom_node(Mesh *me, VNode *vnode)
1402 {
1403         struct VLayer *vert_vlayer, *face_vlayer;
1404         struct VerseVert *vvert;
1405         struct VerseFace *vface;
1406         struct MVert *mvert;
1407         struct MFace *mface;
1408         struct EdgeHash *edges;
1409         int index;
1410
1411         if(!me || !vnode) return;
1412
1413         if(vnode->type != V_NT_GEOMETRY) return;
1414
1415         vert_vlayer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
1416         face_vlayer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
1417
1418         CustomData_free(&me->vdata, me->totvert);
1419         CustomData_free(&me->edata, me->totedge);
1420         CustomData_free(&me->fdata, me->totface);
1421         mesh_update_customdata_pointers(me);
1422
1423         if(me->mselect) {
1424                 MEM_freeN(me->mselect);
1425                 me->mselect = NULL;
1426         }
1427
1428         me->totvert = vert_vlayer->dl.da.count;
1429         me->totface = face_vlayer->dl.da.count;
1430         me->totselect = 0;
1431
1432         CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, me->totvert);
1433         CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, NULL, me->totface);
1434         mesh_update_customdata_pointers(me);
1435
1436         mvert = me->mvert;
1437         mface = me->mface;
1438
1439         index = 0;
1440         vvert = vert_vlayer->dl.lb.first;
1441         while(vvert) {
1442                 VECCOPY(mvert->co, vvert->co);
1443                 VECCOPY(mvert->no, vvert->no);
1444                 mvert->flag = 0;
1445                 mvert->mat_nr = 0;
1446                 vvert->tmp.index = index++;
1447                 vvert = vvert->next;
1448                 mvert++;
1449         }
1450
1451         edges = BLI_edgehash_new();
1452         vface = face_vlayer->dl.lb.first;
1453         while(vface) {
1454                 mface->v1 = vface->vvert0->tmp.index;
1455                 mface->v2 = vface->vvert1->tmp.index;
1456                 mface->v3 = vface->vvert2->tmp.index;
1457
1458                 if(!BLI_edgehash_haskey(edges, mface->v1, mface->v2))
1459                         BLI_edgehash_insert(edges, mface->v1, mface->v2, NULL);
1460                 if(!BLI_edgehash_haskey(edges, mface->v2, mface->v3))
1461                         BLI_edgehash_insert(edges, mface->v2, mface->v3, NULL);
1462                 if(vface->vvert3) {
1463                         mface->v4 = vface->vvert3->tmp.index;
1464                         if(!BLI_edgehash_haskey(edges, mface->v3, mface->v4))
1465                                 BLI_edgehash_insert(edges, mface->v3, mface->v4, NULL);
1466                         if(!BLI_edgehash_haskey(edges, mface->v4, mface->v1))
1467                                 BLI_edgehash_insert(edges, mface->v4, mface->v1, NULL);
1468                 } else {
1469                         mface->v4 = 0;
1470                         if(!BLI_edgehash_haskey(edges, mface->v3, mface->v1))
1471                                 BLI_edgehash_insert(edges, mface->v3, mface->v1, NULL);
1472                 }
1473
1474                 mface->flag = 0;
1475                 mface->pad = 0;
1476                 mface->mat_nr = 0;
1477                 mface->edcode = 0;
1478
1479                 /* index 0 isn't allowed at location 3 or 4 */
1480                 test_index_face(mface, NULL, 0, vface->vvert3?4:3);
1481 /*              printf("\t mface: %d, %d, %d, %d\n", mface->v1, mface->v2, mface->v3, mface->v4);*/
1482                 
1483                 vface = vface->next;
1484                 mface++;
1485         }
1486         
1487         me->totedge = BLI_edgehash_size(edges);
1488
1489         if(me->totedge) {
1490                 EdgeHashIterator *i;
1491                 MEdge *medge = me->medge = CustomData_add_layer(&me->edata, CD_MEDGE, CD_CALLOC, NULL, me->totedge);
1492
1493                 for(i = BLI_edgehashIterator_new(edges); !BLI_edgehashIterator_isDone(i); BLI_edgehashIterator_step(i), ++medge) {
1494                         BLI_edgehashIterator_getKey(i, (int*)&medge->v1, (int*)&medge->v2);
1495                         medge->crease = medge->pad = medge->flag = 0;
1496                 }
1497                 BLI_edgehashIterator_free(i);
1498         }
1499
1500         BLI_edgehash_free(edges, NULL);
1501
1502         mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
1503 }
1504
1505 /*
1506  * Create EditMesh from VerseMesh and keep system in consitant state, this
1507  * function is called, when edit mode is entered ... edit mesh is generated
1508  * from verse mesh (not from Mesh: (Mesh*)ob->data)
1509  */
1510 void create_edit_mesh_from_geom_node(VNode *vnode)
1511 {
1512         struct VLayer *vert_layer, *face_layer;
1513         struct VerseVert *vvert;
1514         struct VerseFace *vface;
1515         struct Mesh *me;
1516         struct EditVert *eve, *eve0, *eve1, *eve2, *eve3;
1517         struct EditFace *efa;
1518         unsigned int keyindex;
1519
1520         if(!(G.obedit && G.obedit->type==OB_MESH)) return;
1521         me = (Mesh*)G.obedit->data;
1522         if(vnode!=(VNode*)me->vnode || vnode->type!=V_NT_GEOMETRY) return;
1523
1524         vert_layer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
1525         face_layer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
1526
1527         if(!(vert_layer && face_layer)) return;
1528
1529         waitcursor(1);
1530
1531         /* free old editMesh */
1532         free_editMesh(G.editMesh);
1533         
1534         G.editMesh->vnode = NULL;
1535
1536         vvert = vert_layer->dl.lb.first;
1537
1538         keyindex = 0;
1539
1540         /* create all EditVerts */
1541         while(vvert) {
1542                 eve = addvertlist(vvert->co, NULL);
1543
1544                 eve->f = 0;
1545                 eve->h = 0;
1546                 eve->data = NULL;
1547                 eve->keyindex = keyindex;
1548                 eve->vvert = (void*)vvert;
1549
1550                 vvert->vertex = (void*)eve;
1551
1552                 keyindex++;
1553                 vvert = vvert->next;
1554         }
1555
1556         vface = face_layer->dl.lb.first;
1557
1558         /* create all EditFaces and EditEdges */
1559         while(vface) {
1560                 if(vface->vvert0) eve0= vface->vvert0->vertex;
1561                 else eve0 = NULL;
1562                 if(vface->vvert1) eve1= vface->vvert1->vertex;
1563                 else eve1 = NULL;
1564                 if(vface->vvert2) eve2= vface->vvert2->vertex;
1565                 else eve2 = NULL;
1566                 if(vface->vvert3) eve3= vface->vvert3->vertex;
1567                 else eve3 = NULL;
1568
1569                 efa= addfacelist(eve0, eve1, eve2, eve3, NULL, NULL);
1570                 if(efa) {
1571                         efa->f = 0;
1572                         efa->h = 0;
1573                         efa->vface = (void*)vface;
1574                         vface->face = (void*)efa;
1575                 }
1576                 vface = vface->next;
1577         }
1578         
1579         countall();
1580
1581         recalc_editnormals();
1582
1583         G.editMesh->vnode = (void*)vnode;
1584         ((VGeomData*)vnode->data)->editmesh = (void*)G.editMesh;
1585
1586         waitcursor(0);
1587 }
1588
1589 /*
1590  * destroy bindings between EditMesh and VerseMesh and send delete commands
1591  * for all VerseVerts and VerseFaces to verse server, Verse Node has to be
1592  * geometry node
1593  */
1594
1595 void destroy_verse_mesh(VNode *vnode)
1596 {
1597         struct VLayer *vert_vlayer, *face_vlayer;
1598         struct VerseFace *vface;
1599         struct VerseVert *vvert;
1600
1601         if(vnode->type != V_NT_GEOMETRY) return;
1602
1603         face_vlayer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
1604         vface = face_vlayer->dl.lb.first;
1605
1606         while(vface) {
1607                 ((EditFace*)vface->face)->vface = NULL;
1608                 vface->face = NULL;     
1609                 vface = vface->next;
1610         }
1611
1612         vert_vlayer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
1613         vvert = vert_vlayer->dl.lb.first;
1614
1615         while(vvert) {
1616                 ((EditVert*)vvert->vertex)->vvert = NULL;
1617                 vvert->vertex = NULL;
1618                 vvert = vvert->next;
1619         }
1620
1621         destroy_geometry(vnode);
1622 }
1623
1624 #endif
1625