Revision: 40128
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=40128
Author:   ben2610
Date:     2011-09-11 14:43:53 +0000 (Sun, 11 Sep 2011)
Log Message:
-----------
Recast: forgot the new files in last commit.

Added Paths:
-----------
    trunk/blender/source/blender/blenkernel/BKE_navmesh_conversion.h
    trunk/blender/source/blender/blenkernel/intern/navmesh_conversion.cpp

Removed Paths:
-------------
    trunk/blender/source/blender/editors/include/ED_navmesh_conversion.h
    trunk/blender/source/blender/editors/util/navmesh_conversion.cpp

Added: trunk/blender/source/blender/blenkernel/BKE_navmesh_conversion.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_navmesh_conversion.h            
                (rev 0)
+++ trunk/blender/source/blender/blenkernel/BKE_navmesh_conversion.h    
2011-09-11 14:43:53 UTC (rev 40128)
@@ -0,0 +1,64 @@
+/**
+* $Id$ 
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#ifndef BKE_NAVMESH_CONVERSION_H
+#define BKE_NAVMESH_CONVERSION_H
+
+struct DerivedMesh;
+
+/* navmesh_conversion.cpp */
+bool buildNavMeshDataByDerivedMesh(DerivedMesh *dm, int& vertsPerPoly, 
+                                                                  int &nverts, 
float *&verts,
+                                                                  int &ndtris, 
unsigned short *&dtris,
+                                                                  int& npolys, 
unsigned short *&dmeshes,
+                                                                  unsigned 
short*& polys, int *&dtrisToPolysMap,
+                                                                  int 
*&dtrisToTrisMap, int *&trisToFacesMap);
+
+bool buildRawVertIndicesData(DerivedMesh* dm, int &nverts, float *&verts, 
+                               int &ntris, unsigned short *&tris, int 
*&trisToFacesMap,
+                               int *&recastData);
+
+bool buildNavMeshData(const int nverts, const float* verts, 
+                                         const int ntris, const unsigned short 
*tris, 
+                                         const int* recastData, const int* 
trisToFacesMap,
+                                         int &ndtris, unsigned short *&dtris,
+                                         int &npolys, unsigned short 
*&dmeshes, unsigned short *&polys,
+                                         int &vertsPerPoly, int 
*&dtrisToPolysMap, int *&dtrisToTrisMap);
+
+bool buildPolygonsByDetailedMeshes(const int vertsPerPoly, const int npolys, 
+                               unsigned short* polys, const unsigned short* 
dmeshes, 
+                               const float* verts, const unsigned short* 
dtris, 
+                               const int* dtrisToPolysMap);
+
+int polyNumVerts(const unsigned short* p, const int vertsPerPoly);
+bool polyIsConvex(const unsigned short* p, const int vertsPerPoly, const 
float* verts);
+int polyFindVertex(const unsigned short* p, const int vertsPerPoly, unsigned 
short vertexIdx);
+float distPointToSegmentSq(const float* point, const float* a, const float* b);
+
+#endif //NAVMESH_CONVERSION_H


Property changes on: 
trunk/blender/source/blender/blenkernel/BKE_navmesh_conversion.h
___________________________________________________________________
Added: svn:keywords
   + Author Date Id Revision
Added: svn:eol-style
   + native

Added: trunk/blender/source/blender/blenkernel/intern/navmesh_conversion.cpp
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/navmesh_conversion.cpp       
                        (rev 0)
+++ trunk/blender/source/blender/blenkernel/intern/navmesh_conversion.cpp       
2011-09-11 14:43:53 UTC (rev 40128)
@@ -0,0 +1,464 @@
+/**
+* $Id$ 
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#include <math.h>
+#include "Recast.h"
+
+extern "C"{
+#include "BKE_navmesh_conversion.h"
+
+#include "DNA_meshdata_types.h"
+#include "BKE_cdderivedmesh.h"
+#include "BLI_math.h"
+}
+
+inline float area2(const float* a, const float* b, const float* c)
+{
+       return (b[0] - a[0]) * (c[2] - a[2]) - (c[0] - a[0]) * (b[2] - a[2]);
+}
+
+inline bool left(const float* a, const float* b, const float* c)
+{
+       return area2(a, b, c) < 0;
+}
+
+int polyNumVerts(const unsigned short* p, const int vertsPerPoly)
+{
+       int nv = 0;
+       for (int i=0; i<vertsPerPoly; i++)
+       {
+               if (p[i]==0xffff)
+                       break;
+               nv++;
+       }
+       return nv;
+}
+
+bool polyIsConvex(const unsigned short* p, const int vertsPerPoly, const 
float* verts)
+{
+       int nv = polyNumVerts(p, vertsPerPoly);
+       if (nv<3)
+               return false;
+       for (int j=0; j<nv; j++)
+       {
+               const float* v = &verts[3*p[j]];
+               const float* v_next = &verts[3*p[(j+1)%nv]];
+               const float* v_prev = &verts[3*p[(nv+j-1)%nv]];
+               if (!left(v_prev, v, v_next))
+                       return false;
+
+       }
+       return true;
+}
+
+float distPointToSegmentSq(const float* point, const float* a, const float* b)
+{
+       float abx[3], dx[3];
+       vsub(abx, b,a);
+       vsub(dx, point,a);
+       float d = abx[0]*abx[0]+abx[2]*abx[2];
+       float t = abx[0]*dx[0]+abx[2]*dx[2];
+       if (d > 0)
+               t /= d;
+       if (t < 0)
+               t = 0;
+       else if (t > 1)
+               t = 1;
+       dx[0] = a[0] + t*abx[0] - point[0];
+       dx[2] = a[2] + t*abx[2] - point[2];
+       return dx[0]*dx[0] + dx[2]*dx[2];
+}
+
+bool buildRawVertIndicesData(DerivedMesh* dm, int &nverts, float *&verts, 
+                                                                       int 
&ntris, unsigned short *&tris, int *&trisToFacesMap,
+                                                                       int 
*&recastData)
+{
+       nverts = dm->getNumVerts(dm);
+       if (nverts>=0xffff)
+       {
+               printf("Converting navmesh: Error! Too many vertices. Max 
number of vertices %d\n", 0xffff);
+               return false;
+       }
+       verts = new float[3*nverts];
+       dm->getVertCos(dm, (float(*)[3])verts);
+
+       //flip coordinates
+       for (int vi=0; vi<nverts; vi++)
+       {
+               SWAP(float, verts[3*vi+1], verts[3*vi+2]);
+       }
+
+       //calculate number of tris
+       int nfaces = dm->getNumFaces(dm);
+       MFace *faces = dm->getFaceArray(dm);
+       ntris = nfaces;
+       for (int fi=0; fi<nfaces; fi++)
+       {
+               MFace* face = &faces[fi];
+               if (face->v4)
+                       ntris++;
+       }
+
+       //copy and transform to triangles (reorder on the run)
+       trisToFacesMap = new int[ntris];
+       tris = new unsigned short[3*ntris];
+       unsigned short* tri = tris;
+       int triIdx = 0;
+       for (int fi=0; fi<nfaces; fi++)
+       {
+               MFace* face = &faces[fi];
+               tri[3*triIdx+0] = (unsigned short) face->v1;
+               tri[3*triIdx+1] = (unsigned short) face->v3;
+               tri[3*triIdx+2] = (unsigned short) face->v2;
+               trisToFacesMap[triIdx++]=fi;
+               if (face->v4)
+               {
+                       tri[3*triIdx+0] = (unsigned short) face->v1;
+                       tri[3*triIdx+1] = (unsigned short) face->v4;
+                       tri[3*triIdx+2] = (unsigned short) face->v3;
+                       trisToFacesMap[triIdx++]=fi;
+               }
+       }
+
+       //carefully, recast data is just reference to data in derived mesh
+       recastData = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
+       return true;
+}
+
+bool buildPolygonsByDetailedMeshes(const int vertsPerPoly, const int npolys, 
+                                                                               
  unsigned short* polys, const unsigned short* dmeshes, 
+                                                                               
  const float* verts, const unsigned short* dtris, 
+                                                                               
  const int* dtrisToPolysMap)
+{
+       int capacity = vertsPerPoly;
+       unsigned short* newPoly =  new unsigned short[capacity];
+       memset(newPoly, 0xff, sizeof(unsigned short)*capacity);
+       for (int polyidx=0; polyidx<npolys; polyidx++)
+       {
+               int nv = 0;
+               //search border 
+               int btri = -1;
+               int bedge = -1;
+               int dtrisNum = dmeshes[polyidx*4+3];
+               int dtrisBase = dmeshes[polyidx*4+2];
+               unsigned char *traversedTris = new unsigned char[dtrisNum];
+               memset(traversedTris, 0, dtrisNum*sizeof(unsigned char));
+               for (int j=0; j<dtrisNum && btri==-1;j++)
+               {
+                       int curpolytri = dtrisBase+j;
+                       for (int k=0; k<3; k++)
+                       {
+                               unsigned short neighbortri = 
dtris[curpolytri*3*2+3+k];
+                               if ( neighbortri==0xffff || 
dtrisToPolysMap[neighbortri]!=polyidx+1)
+                               {
+                                       btri = curpolytri;
+                                       bedge = k;
+                                       break;
+                               }
+                       }                                                       
+               }
+               if (btri==-1 || bedge==-1)
+               {
+                       //can't find triangle with border edge
+                       return false;
+               }
+
+               newPoly[nv++] = dtris[btri*3*2+bedge];
+               int tri = btri;
+               int edge = (bedge+1)%3;
+               traversedTris[tri-dtrisBase] = 1;
+               while (tri!=btri || edge!=bedge)
+               {
+                       int neighbortri = dtris[tri*3*2+3+edge];
+                       if (neighbortri==0xffff || 
dtrisToPolysMap[neighbortri]!=polyidx+1)
+                       {
+                               if (nv==capacity)
+                               {
+                                       capacity += vertsPerPoly;
+                                       unsigned short* newPolyBig =  new 
unsigned short[capacity];
+                                       memset(newPolyBig, 0xff, 
sizeof(unsigned short)*capacity);
+                                       memcpy(newPolyBig, newPoly, 
sizeof(unsigned short)*nv);
+                                       delete newPoly;
+                                       newPoly = newPolyBig;                   
+                               }
+                               newPoly[nv++] = dtris[tri*3*2+edge];
+                               //move to next edge                             
        
+                               edge = (edge+1)%3;
+                       }
+                       else
+                       {
+                               //move to next tri
+                               int twinedge = -1;
+                               for (int k=0; k<3; k++)
+                               {
+                                       if (dtris[neighbortri*3*2+3+k] == tri)
+                                       {
+                                               twinedge = k;
+                                               break;
+                                       }
+                               }
+                               if (twinedge==-1)
+                               {
+                                       printf("Converting navmesh: Error! 
Can't find neighbor edge - invalid adjacency info\n");
+                                       goto returnLabel;                       
                
+                               }
+                               tri = neighbortri;
+                               edge = (twinedge+1)%3;
+                               traversedTris[tri-dtrisBase] = 1;
+                       }
+               }
+
+               unsigned short* adjustedPoly = new unsigned short[nv];
+               int adjustedNv = 0;
+               for (size_t i=0; i<(size_t)nv; i++)
+               {
+                       unsigned short prev = newPoly[(nv+i-1)%nv];
+                       unsigned short cur = newPoly[i];
+                       unsigned short next = newPoly[(i+1)%nv];
+                       float distSq = distPointToSegmentSq(&verts[3*cur], 
&verts[3*prev], &verts[3*next]);
+                       static const float tolerance = 0.001f;
+                       if (distSq>tolerance)
+                               adjustedPoly[adjustedNv++] = cur;
+               }
+               memcpy(newPoly, adjustedPoly, adjustedNv*sizeof(unsigned 
short));
+               delete adjustedPoly;
+               nv = adjustedNv;
+
+               bool allBorderTraversed = true;
+               for (size_t i=0; i<(size_t)dtrisNum; i++)
+               {

@@ Diff output truncated at 10240 characters. @@
_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to