svn merge ^/trunk/blender -r43092:43092
[blender.git] / source / blender / editors / mesh / mesh_navmesh.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2011 by Blender Foundation
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Benoit Bolsee,
24  *                 Nick Samarin
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <math.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_scene_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_meshdata_types.h"
36 #include "DNA_modifier_types.h"
37 #include "DNA_ID.h"
38
39 #include "BKE_library.h"
40 #include "BKE_depsgraph.h"
41 #include "BKE_context.h"
42 #include "BKE_main.h"
43 #include "BKE_mesh.h"
44 #include "BKE_modifier.h"
45 #include "BKE_scene.h"
46 #include "BKE_DerivedMesh.h"
47 #include "BKE_cdderivedmesh.h"
48 #include "BKE_report.h"
49 #include "BKE_tessmesh.h"
50
51 #include "BLI_editVert.h"
52 #include "BLI_listbase.h"
53 #include "BLI_utildefines.h"
54 #include "BLI_math_vector.h"
55 #include "BLI_linklist.h"
56
57 #include "ED_object.h"
58 #include "ED_mesh.h"
59 #include "ED_screen.h"
60
61 #include "RNA_access.h"
62
63 #include "WM_api.h"
64 #include "WM_types.h"
65
66 #include "mesh_intern.h"
67 #include "recast-capi.h"
68
69 static void createVertsTrisData(bContext *C, LinkNode* obs, int *nverts_r, float **verts_r, int *ntris_r, int **tris_r)
70 {
71         MVert *mvert;
72         int nfaces= 0, *tri, i, curnverts, basenverts, curnfaces;
73         MFace *mface;
74         float co[3], wco[3];
75         Object *ob;
76         LinkNode *oblink, *dmlink;
77         DerivedMesh *dm;
78         Scene* scene= CTX_data_scene(C);
79         LinkNode* dms= NULL;
80
81         int nverts, ntris, *tris;
82         float *verts;
83
84         nverts= 0;
85         ntris= 0;
86
87         /* calculate number of verts and tris */
88         for(oblink= obs; oblink; oblink= oblink->next) {
89                 ob= (Object*) oblink->link;
90                 dm= mesh_create_derived_no_virtual(scene, ob, NULL, CD_MASK_MESH);
91                 BLI_linklist_append(&dms, (void*)dm);
92
93                 nverts+= dm->getNumVerts(dm);
94                 nfaces= dm->getNumTessFaces(dm);
95                 ntris+= nfaces;
96
97                 /* resolve quad faces */
98                 mface= dm->getTessFaceArray(dm);
99                 for(i= 0; i<nfaces; i++) {
100                         MFace* mf= &mface[i];
101                         if(mf->v4)
102                                 ntris+=1;
103                 }
104         }
105
106         /* create data */
107         verts= MEM_mallocN(sizeof(float)*3*nverts, "createVertsTrisData verts");
108         tris= MEM_mallocN(sizeof(int)*3*ntris, "createVertsTrisData faces");
109
110         basenverts= 0;
111         tri= tris;
112         for(oblink= obs, dmlink= dms; oblink && dmlink;
113                         oblink= oblink->next, dmlink= dmlink->next) {
114                 ob= (Object*) oblink->link;
115                 dm= (DerivedMesh*) dmlink->link;
116
117                 curnverts= dm->getNumVerts(dm);
118                 mvert= dm->getVertArray(dm);
119
120                 /* copy verts */
121                 for(i= 0; i<curnverts; i++) {
122                         MVert *v= &mvert[i];
123
124                         copy_v3_v3(co, v->co);
125                         mul_v3_m4v3(wco, ob->obmat, co);
126
127                         verts[3*(basenverts+i)+0]= wco[0];
128                         verts[3*(basenverts+i)+1]= wco[2];
129                         verts[3*(basenverts+i)+2]= wco[1];
130                 }
131
132                 /* create tris */
133                 curnfaces= dm->getNumTessFaces(dm);
134                 mface= dm->getTessFaceArray(dm);
135
136                 for(i= 0; i<curnfaces; i++) {
137                         MFace* mf= &mface[i];
138
139                         tri[0]= basenverts + mf->v1;
140                         tri[1]= basenverts + mf->v3;
141                         tri[2]= basenverts + mf->v2;
142                         tri += 3;
143
144                         if(mf->v4) {
145                                 tri[0]= basenverts + mf->v1;
146                                 tri[1]= basenverts + mf->v4;
147                                 tri[2]= basenverts + mf->v3;
148                                 tri += 3;
149                         }
150                 }
151
152                 basenverts+= curnverts;
153         }
154
155         /* release derived mesh */
156         for(dmlink= dms; dmlink; dmlink= dmlink->next) {
157                 dm= (DerivedMesh*) dmlink->link;
158                 dm->release(dm);
159         }
160
161         BLI_linklist_free(dms, NULL);
162
163         *nverts_r= nverts;
164         *verts_r= verts;
165         *ntris_r= ntris;
166         *tris_r= tris;
167 }
168
169 static int buildNavMesh(const RecastData *recastParams, int nverts, float *verts, int ntris, int *tris,
170                                                                  struct recast_polyMesh **pmesh, struct recast_polyMeshDetail **dmesh)
171 {
172         float bmin[3], bmax[3];
173         struct recast_heightfield *solid;
174         unsigned char *triflags;
175         struct recast_compactHeightfield* chf;
176         struct recast_contourSet *cset;
177         int width, height, walkableHeight, walkableClimb, walkableRadius;
178         int minRegionArea, mergeRegionArea, maxEdgeLen;
179         float detailSampleDist, detailSampleMaxError;
180
181         recast_calcBounds(verts, nverts, bmin, bmax);
182
183         /* ** Step 1. Initialize build config ** */
184         walkableHeight= (int)ceilf(recastParams->agentheight/ recastParams->cellheight);
185         walkableClimb= (int)floorf(recastParams->agentmaxclimb / recastParams->cellheight);
186         walkableRadius= (int)ceilf(recastParams->agentradius / recastParams->cellsize);
187         minRegionArea= (int)(recastParams->regionminsize * recastParams->regionminsize);
188         mergeRegionArea= (int)(recastParams->regionmergesize * recastParams->regionmergesize);
189         maxEdgeLen= (int)(recastParams->edgemaxlen/recastParams->cellsize);
190         detailSampleDist= recastParams->detailsampledist< 0.9f ? 0 :
191                         recastParams->cellsize * recastParams->detailsampledist;
192         detailSampleMaxError= recastParams->cellheight * recastParams->detailsamplemaxerror;
193
194         /* Set the area where the navigation will be build. */
195         recast_calcGridSize(bmin, bmax, recastParams->cellsize, &width, &height);
196
197         /* ** Step 2: Rasterize input polygon soup ** */
198         /* Allocate voxel heightfield where we rasterize our input data to */
199         solid= recast_newHeightfield();
200
201         if(!recast_createHeightfield(solid, width, height, bmin, bmax, recastParams->cellsize, recastParams->cellheight)) {
202                 recast_destroyHeightfield(solid);
203
204                 return 0;
205         }
206
207         /* Allocate array that can hold triangle flags */
208         triflags= MEM_callocN(sizeof(unsigned char)*ntris, "buildNavMesh triflags");
209
210         /* Find triangles which are walkable based on their slope and rasterize them */
211         recast_markWalkableTriangles(RAD2DEG(recastParams->agentmaxslope), verts, nverts, tris, ntris, triflags);
212         recast_rasterizeTriangles(verts, nverts, tris, triflags, ntris, solid);
213         MEM_freeN(triflags);
214
215         /* ** Step 3: Filter walkables surfaces ** */
216         recast_filterLowHangingWalkableObstacles(walkableClimb, solid);
217         recast_filterLedgeSpans(walkableHeight, walkableClimb, solid);
218         recast_filterWalkableLowHeightSpans(walkableHeight, solid);
219
220         /* ** Step 4: Partition walkable surface to simple regions ** */
221
222         chf= recast_newCompactHeightfield();
223         if(!recast_buildCompactHeightfield(walkableHeight, walkableClimb, solid, chf)) {
224                 recast_destroyHeightfield(solid);
225                 recast_destroyCompactHeightfield(chf);
226
227                 return 0;
228         }
229
230         recast_destroyHeightfield(solid);
231         solid = NULL;
232
233         if (!recast_erodeWalkableArea(walkableRadius, chf)) {
234                 recast_destroyCompactHeightfield(chf);
235
236                 return 0;
237         }
238
239         /* Prepare for region partitioning, by calculating distance field along the walkable surface */
240         if(!recast_buildDistanceField(chf)) {
241                 recast_destroyCompactHeightfield(chf);
242
243                 return 0;
244         }
245
246         /* Partition the walkable surface into simple regions without holes */
247         if(!recast_buildRegions(chf, 0, minRegionArea, mergeRegionArea)) {
248                 recast_destroyCompactHeightfield(chf);
249
250                 return 0;
251         }
252
253         /* ** Step 5: Trace and simplify region contours ** */
254         /* Create contours */
255         cset= recast_newContourSet();
256
257         if(!recast_buildContours(chf, recastParams->edgemaxerror, maxEdgeLen, cset)) {
258                 recast_destroyCompactHeightfield(chf);
259                 recast_destroyContourSet(cset);
260
261                 return 0;
262         }
263
264         /* ** Step 6: Build polygons mesh from contours ** */
265         *pmesh= recast_newPolyMesh();
266         if(!recast_buildPolyMesh(cset, recastParams->vertsperpoly, *pmesh)) {
267                 recast_destroyCompactHeightfield(chf);
268                 recast_destroyContourSet(cset);
269                 recast_destroyPolyMesh(*pmesh);
270
271                 return 0;
272         }
273
274
275         /* ** Step 7: Create detail mesh which allows to access approximate height on each polygon ** */
276
277         *dmesh= recast_newPolyMeshDetail();
278         if(!recast_buildPolyMeshDetail(*pmesh, chf, detailSampleDist, detailSampleMaxError, *dmesh)) {
279                 recast_destroyCompactHeightfield(chf);
280                 recast_destroyContourSet(cset);
281                 recast_destroyPolyMesh(*pmesh);
282                 recast_destroyPolyMeshDetail(*dmesh);
283
284                 return 0;
285         }
286
287         recast_destroyCompactHeightfield(chf);
288         recast_destroyContourSet(cset);
289
290         return 1;
291 }
292
293 static Object* createRepresentation(bContext *C, struct recast_polyMesh *pmesh, struct recast_polyMeshDetail *dmesh, Base* base)
294 {
295         float co[3], rot[3];
296         BMEditMesh *em;
297         int i,j, k;
298         unsigned short* v;
299         int face[3];
300         Scene *scene= CTX_data_scene(C);
301         Object* obedit;
302         int createob= base==NULL;
303         int nverts, nmeshes, nvp;
304         unsigned short *verts, *polys;
305         unsigned int *meshes;
306         float bmin[3], cs, ch, *dverts;
307         unsigned char *tris;
308
309         zero_v3(co);
310         zero_v3(rot);
311
312         if(createob) {
313                 /* create new object */
314                 obedit= ED_object_add_type(C, OB_MESH, co, rot, FALSE, 1);
315         }
316         else {
317                 obedit= base->object;
318                 scene_select_base(scene, base);
319                 copy_v3_v3(obedit->loc, co);
320                 copy_v3_v3(obedit->rot, rot);
321         }
322
323         ED_object_enter_editmode(C, EM_DO_UNDO|EM_IGNORE_LAYER);
324         em= (((Mesh *)obedit->data))->edit_btmesh;
325
326         if(!createob) {
327                 /* clear */
328                 EDBM_ClearMesh(em);
329         }
330
331         /* create verts for polygon mesh */
332         verts= recast_polyMeshGetVerts(pmesh, &nverts);
333         recast_polyMeshGetBoundbox(pmesh, bmin, NULL);
334         recast_polyMeshGetCell(pmesh, &cs, &ch);
335
336         for(i= 0; i<nverts; i++) {
337                 v= &verts[3*i];
338                 co[0]= bmin[0] + v[0]*cs;
339                 co[1]= bmin[1] + v[1]*ch;
340                 co[2]= bmin[2] + v[2]*cs;
341                 SWAP(float, co[1], co[2]);
342                 BM_Make_Vert(em->bm, co, NULL);
343         }
344
345         /* create custom data layer to save polygon idx */
346         CustomData_add_layer_named(&em->bm->pdata, CD_RECAST, CD_CALLOC, NULL, 0, "createRepresentation recastData");
347         
348         /* create verts and faces for detailed mesh */
349         meshes= recast_polyMeshDetailGetMeshes(dmesh, &nmeshes);
350         polys= recast_polyMeshGetPolys(pmesh, NULL, &nvp);
351         dverts= recast_polyMeshDetailGetVerts(dmesh, NULL);
352         tris= recast_polyMeshDetailGetTris(dmesh, NULL);
353
354         for(i= 0; i<nmeshes; i++) {
355                 int uniquevbase= em->bm->totvert;
356                 unsigned int vbase= meshes[4*i+0];
357                 unsigned short ndv= meshes[4*i+1];
358                 unsigned short tribase= meshes[4*i+2];
359                 unsigned short trinum= meshes[4*i+3];
360                 const unsigned short* p= &polys[i*nvp*2];
361                 int nv= 0;
362
363                 for(j= 0; j < nvp; ++j) {
364                         if(p[j]==0xffff) break;
365                         nv++;
366                 }
367
368                 /* create unique verts  */
369                 for(j= nv; j<ndv; j++) {
370                         copy_v3_v3(co, &dverts[3*(vbase + j)]);
371                         SWAP(float, co[1], co[2]);
372                         BM_Make_Vert(em->bm, co, NULL);
373                 }
374
375                 EDBM_init_index_arrays(em, 1, 0, 0);
376
377                 /* create faces */
378                 for(j= 0; j<trinum; j++) {
379                         unsigned char* tri= &tris[4*(tribase+j)];
380                         BMFace* newFace;
381                         int* polygonIdx;
382
383                         for(k= 0; k<3; k++) {
384                                 if(tri[k]<nv)
385                                         face[k]= p[tri[k]]; /* shared vertex */
386                                 else
387                                         face[k]= uniquevbase+tri[k]-nv; /* unique vertex */
388                         }
389                         newFace= BM_Make_Face_QuadTri(em->bm, EDBM_get_vert_for_index(em, face[0]), EDBM_get_vert_for_index(em, face[2]),
390                                                                         EDBM_get_vert_for_index(em, face[1]), NULL, NULL, 0);
391
392                         /* set navigation polygon idx to the custom layer */
393                         polygonIdx= (int*)CustomData_bmesh_get(&em->bm->pdata, newFace->head.data, CD_RECAST);
394                         *polygonIdx= i+1; /* add 1 to avoid zero idx */
395                 }
396                 
397                 EDBM_free_index_arrays(em);
398         }
399
400         recast_destroyPolyMesh(pmesh);
401         recast_destroyPolyMeshDetail(dmesh);
402
403         DAG_id_tag_update((ID*)obedit->data, OB_RECALC_DATA);
404         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
405
406
407         ED_object_exit_editmode(C, EM_FREEDATA); 
408         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
409
410         if(createob) {
411                 obedit->gameflag&= ~OB_COLLISION;
412                 obedit->gameflag|= OB_NAVMESH;
413                 obedit->body_type= OB_BODY_TYPE_NAVMESH;
414                 rename_id((ID *)obedit, "Navmesh");
415         }
416
417         BKE_mesh_ensure_navmesh(obedit->data);
418
419         return obedit;
420 }
421
422 static int create_navmesh_exec(bContext *C, wmOperator *op)
423 {
424         Scene* scene= CTX_data_scene(C);
425         LinkNode* obs= NULL;
426         Base* navmeshBase= NULL;
427
428         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
429                 if (base->object->type == OB_MESH) {
430                         if (base->object->body_type==OB_BODY_TYPE_NAVMESH) {
431                                 if (!navmeshBase || base == scene->basact) {
432                                         navmeshBase= base;
433                                 }
434                         }
435                         else {
436                                 BLI_linklist_append(&obs, (void*)base->object);
437                         }
438                 }
439         }
440         CTX_DATA_END;
441
442         if (obs) {
443                 struct recast_polyMesh *pmesh= NULL;
444                 struct recast_polyMeshDetail *dmesh= NULL;
445
446                 int nverts= 0, ntris= 0;
447                 int *tris= 0;
448                 float *verts= NULL;
449
450                 createVertsTrisData(C, obs, &nverts, &verts, &ntris, &tris);
451                 BLI_linklist_free(obs, NULL);
452                 buildNavMesh(&scene->gm.recastData, nverts, verts, ntris, tris, &pmesh, &dmesh);
453                 createRepresentation(C, pmesh, dmesh, navmeshBase);
454
455                 MEM_freeN(verts);
456                 MEM_freeN(tris);
457
458                 return OPERATOR_FINISHED;
459         }
460         else {
461                 BKE_report(op->reports, RPT_ERROR, "No mesh objects found");
462
463                 return OPERATOR_CANCELLED;
464         }
465 }
466
467 void MESH_OT_navmesh_make(wmOperatorType *ot)
468 {
469         /* identifiers */
470         ot->name= "Create navigation mesh";
471         ot->description= "Create navigation mesh for selected objects";
472         ot->idname= "MESH_OT_navmesh_make";
473
474         /* api callbacks */
475         ot->exec= create_navmesh_exec;
476
477         /* flags */
478         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
479 }
480
481 static int navmesh_face_copy_exec(bContext *C, wmOperator *op)
482 {
483         Object *obedit= CTX_data_edit_object(C);
484         BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
485
486         /* do work here */
487         BMFace *efa_act= BM_get_actFace(em->bm, 0);
488
489         if(efa_act) {
490                 if(CustomData_has_layer(&em->bm->pdata, CD_RECAST)) {
491                         BMFace *efa;
492                         BMIter iter;
493                         int targetPolyIdx= *(int*)CustomData_bmesh_get(&em->bm->pdata, efa_act->head.data, CD_RECAST);
494                         targetPolyIdx= targetPolyIdx>=0? targetPolyIdx : -targetPolyIdx;
495
496                         if(targetPolyIdx > 0) {
497                                 /* set target poly idx to other selected faces */
498                                 BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
499                                         if(BM_TestHFlag(efa, BM_SELECT) && efa != efa_act)  {
500                                                 int* recastDataBlock= (int*)CustomData_bmesh_get(&em->bm->pdata, efa->head.data, CD_RECAST);
501                                                 *recastDataBlock= targetPolyIdx;
502                                         }
503                                 }
504                         }
505                         else {
506                                 BKE_report(op->reports, RPT_ERROR, "Active face has no index set");
507                         }
508                 }
509         }
510
511         DAG_id_tag_update((ID*)obedit->data, OB_RECALC_DATA);
512         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
513
514         return OPERATOR_FINISHED;
515 }
516
517 void MESH_OT_navmesh_face_copy(struct wmOperatorType *ot)
518 {
519         /* identifiers */
520         ot->name= "NavMesh Copy Face Index";
521         ot->description= "Copy the index from the active face";
522         ot->idname= "MESH_OT_navmesh_face_copy";
523
524         /* api callbacks */
525         ot->poll= ED_operator_editmesh;
526         ot->exec= navmesh_face_copy_exec;
527
528         /* flags */
529         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
530 }
531
532 static int compare(const void * a, const void * b)
533 {
534         return ( *(int*)a - *(int*)b );
535 }
536
537 static int findFreeNavPolyIndex(BMEditMesh* em)
538 {
539         /* construct vector of indices */
540         int numfaces= em->bm->totface;
541         int* indices= MEM_callocN(sizeof(int)*numfaces, "findFreeNavPolyIndex(indices)");
542         BMFace* ef;
543         BMIter iter;
544         int i, idx= em->bm->totface-1, freeIdx= 1;
545
546         /*XXX this originally went last to first, but that isn't possible anymore*/
547         BM_ITER(ef, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
548                 int polyIdx= *(int*)CustomData_bmesh_get(&em->bm->pdata, ef->head.data, CD_RECAST);
549                 indices[idx]= polyIdx;
550                 idx--;
551         }
552
553         qsort(indices, numfaces, sizeof(int), compare);
554
555         /* search first free index */
556         freeIdx= 1;
557         for(i= 0; i<numfaces; i++) {
558                 if(indices[i]==freeIdx)
559                         freeIdx++;
560                 else if(indices[i]>freeIdx)
561                         break;
562         }
563
564         MEM_freeN(indices);
565
566         return freeIdx;
567 }
568
569 static int navmesh_face_add_exec(bContext *C, wmOperator *UNUSED(op))
570 {
571         Object *obedit= CTX_data_edit_object(C);
572         BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
573         BMFace *ef;
574         BMIter iter;
575         
576         if(CustomData_has_layer(&em->bm->pdata, CD_RECAST)) {
577                 int targetPolyIdx= findFreeNavPolyIndex(em);
578
579                 if(targetPolyIdx>0) {
580                         /* set target poly idx to selected faces */
581                         /*XXX this originally went last to first, but that isn't possible anymore*/
582                         
583                         BM_ITER(ef, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
584                                 if(BM_TestHFlag(ef, BM_SELECT)) {
585                                         int *recastDataBlock= (int*)CustomData_bmesh_get(&em->bm->pdata, ef->head.data, CD_RECAST);
586                                         *recastDataBlock= targetPolyIdx;
587                                 }
588                         }
589                 }
590         }
591
592         DAG_id_tag_update((ID*)obedit->data, OB_RECALC_DATA);
593         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
594
595         return OPERATOR_FINISHED;
596 }
597
598 void MESH_OT_navmesh_face_add(struct wmOperatorType *ot)
599 {
600         /* identifiers */
601         ot->name= "NavMesh New Face Index";
602         ot->description= "Add a new index and assign it to selected faces";
603         ot->idname= "MESH_OT_navmesh_face_add";
604
605         /* api callbacks */
606         ot->poll= ED_operator_editmesh;
607         ot->exec= navmesh_face_add_exec;
608
609         /* flags */
610         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
611 }
612
613 static int navmesh_obmode_data_poll(bContext *C)
614 {
615         Object *ob = ED_object_active_context(C);
616         if (ob && (ob->mode == OB_MODE_OBJECT) && (ob->type == OB_MESH)) {
617                 Mesh *me= ob->data;
618                 return CustomData_has_layer(&me->fdata, CD_RECAST);
619         }
620         return FALSE;
621 }
622
623 static int navmesh_obmode_poll(bContext *C)
624 {
625         Object *ob = ED_object_active_context(C);
626         if (ob && (ob->mode == OB_MODE_OBJECT) && (ob->type == OB_MESH)) {
627                 return TRUE;
628         }
629         return FALSE;
630 }
631
632 static int navmesh_reset_exec(bContext *C, wmOperator *UNUSED(op))
633 {
634         Object *ob = ED_object_active_context(C);
635         Mesh *me= ob->data;
636
637         CustomData_free_layers(&me->fdata, CD_RECAST, me->totface);
638
639         BKE_mesh_ensure_navmesh(me);
640
641         DAG_id_tag_update(&me->id, OB_RECALC_DATA);
642         WM_event_add_notifier(C, NC_GEOM|ND_DATA, &me->id);
643
644         return OPERATOR_FINISHED;
645 }
646
647 void MESH_OT_navmesh_reset(struct wmOperatorType *ot)
648 {
649         /* identifiers */
650         ot->name= "NavMesh Reset Index Values";
651         ot->description= "Assign a new index to every face";
652         ot->idname= "MESH_OT_navmesh_reset";
653
654         /* api callbacks */
655         ot->poll= navmesh_obmode_poll;
656         ot->exec= navmesh_reset_exec;
657
658         /* flags */
659         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
660 }
661
662 static int navmesh_clear_exec(bContext *C, wmOperator *UNUSED(op))
663 {
664         Object *ob = ED_object_active_context(C);
665         Mesh *me= ob->data;
666
667         CustomData_free_layers(&me->fdata, CD_RECAST, me->totface);
668
669         DAG_id_tag_update(&me->id, OB_RECALC_DATA);
670         WM_event_add_notifier(C, NC_GEOM|ND_DATA, &me->id);
671
672         return OPERATOR_FINISHED;
673 }
674
675 void MESH_OT_navmesh_clear(struct wmOperatorType *ot)
676 {
677         /* identifiers */
678         ot->name= "NavMesh Clear Data";
679         ot->description= "Remove navmesh data from this mesh";
680         ot->idname= "MESH_OT_navmesh_clear";
681
682         /* api callbacks */
683         ot->poll= navmesh_obmode_data_poll;
684         ot->exec= navmesh_clear_exec;
685
686         /* flags */
687         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
688 }