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