Просмотр исходного кода

Working in python. Tests added

Ashwini Jha 5 лет назад
Родитель
Сommit
3511f65caf

+ 3 - 2
panda/src/navigation/navMesh.h

@@ -19,17 +19,18 @@
 #include "DetourNavMesh.h"
 #include "typedWritableReferenceCount.h"
 
-class NavMesh: public TypedWritableReferenceCount
+class EXPCL_NAVIGATION NavMesh: public TypedWritableReferenceCount
 {
 PUBLISHED:
   NavMesh(dtNavMesh *nav_mesh);
   void set_nav_mesh(dtNavMesh *m) { _nav_mesh = m; }
+  NavMesh();
 
 private:
   dtNavMesh *_nav_mesh;
   
 public:
-  NavMesh();
+  
   
   ~NavMesh();
   

+ 34 - 9
panda/src/navigation/navMeshBuilder.cxx

@@ -52,6 +52,7 @@ NavMeshBuilder::NavMeshBuilder() :
   _normals(0),
   _vert_count(0),
   _tri_count(0) {
+  index_temp = 0;
   vertex_map.clear();
   vertex_vector.clear();
   face_vector.clear();
@@ -108,12 +109,28 @@ void NavMeshBuilder::add_triangle(int a, int b, int c, int &cap) {
   _tri_count++;
 }
 
+bool NavMeshBuilder::from_geom(PT(Geom) geom) {
+  int vcap = 0;
+  int tcap = 0;
+  //CPT(geom_pt) = &geom;
+  process_geom(geom, vcap, tcap);
+  _loaded = true;
+  return true;
+}
+
 bool NavMeshBuilder::from_node_path(NodePath node) {
   NodePathCollection geom_node_collection = node.find_all_matches("**/+GeomNode");
 
   int vcap = 0;
   int tcap = 0;
 
+  if (node.node()->is_of_type(GeomNode::get_class_type())) {
+    PT(GeomNode) g = DCAST(GeomNode, node.node());
+    process_geom_node(g, vcap, tcap);
+    _loaded = true;
+    return true;
+  }
+
   for (size_t i = 0; i < geom_node_collection.get_num_paths(); ++i) {
 
     PT(GeomNode) g = DCAST(GeomNode, geom_node_collection.get_path(i).node());
@@ -141,6 +158,7 @@ void NavMeshBuilder::process_primitive(const GeomPrimitive *orig_prim, const Geo
       vertex.set_row(a);
       v = vertex.get_data3();
       a = vertex_map[v];
+
       int b = prim->get_vertex(s + 1);
       vertex.set_row(b);
       v = vertex.get_data3();
@@ -155,6 +173,7 @@ void NavMeshBuilder::process_primitive(const GeomPrimitive *orig_prim, const Geo
       LVector3 xvx = { float(a + 1), float(b + 1), float(c + 1) };
       face_vector.push_back(xvx);
       add_triangle(a, b, c, tcap);
+
     }
     else if (e - s > 3) {
 
@@ -177,6 +196,7 @@ void NavMeshBuilder::process_primitive(const GeomPrimitive *orig_prim, const Geo
         LVector3 xvx = { float(a + 1), float(b + 1), float(c + 1) };
         face_vector.push_back(xvx);
         add_triangle(a, b, c, tcap);
+
       }
     }
     else continue;
@@ -196,17 +216,21 @@ void NavMeshBuilder::process_vertex_data(const GeomVertexData *vdata, int &vcap)
     y = v[1];
     z = v[2];
     if (vertex_map.find(v) == vertex_map.end()) {
-      //add_vertex(x, z, -y, vcap); //if input model is originally z-up
-      add_vertex(x, y, z, vcap); //if input model is originally y-up
-      vertex_map[v] = index_temp++;
+      add_vertex(x, z, -y, vcap); //if input model is originally z-up
+      //add_vertex(x, y, z, vcap); //if input model is originally y-up
+
+      //vertex_map[v] = index_temp++;
       LVector3 xvx = { v[0],v[2],-v[1] };
-      vertex_vector.push_back(xvx);
+      vertex_map[v] = index_temp++;
+      vertex_vector.push_back(v);
     }
 
   }
   return;
 }
 
+
+
 void NavMeshBuilder::process_geom(CPT(Geom) geom, int& vcap, int& tcap) {
 
   CPT(GeomVertexData) vdata = geom->get_vertex_data();
@@ -401,7 +425,9 @@ PT(NavMesh) NavMeshBuilder::build() {
   // If your input data is multiple meshes, you can transform them here, calculate
   // the are type for each of the meshes and rasterize them.
   memset(_triareas, 0, ntris * sizeof(unsigned char));
+  
   rcMarkWalkableTriangles(_ctx, _cfg.walkableSlopeAngle, verts, nverts, tris, ntris, _triareas);
+
   if (!rcRasterizeTriangles(_ctx, verts, nverts, tris, _triareas, ntris, *_solid, _cfg.walkableClimb)) {
     _ctx->log(RC_LOG_ERROR, "buildNavigation: Could not rasterize triangles.");
     std::cout << "\nbuildNavigation: Could not rasterize triangles.\n";
@@ -572,7 +598,6 @@ PT(NavMesh) NavMeshBuilder::build() {
   //
   // (Optional) Step 8. Create Detour data from Recast poly mesh.
   //
-  std::cout << "\nSample_SoloMesh::handleBuild() : (Optional) Step 8. Create Detour data from Recast poly mesh.\n";
   // The GUI may allow more max points per polygon than Detour can handle.
   // Only build the detour navmesh if we do not exceed the limit.
   if (_cfg.maxVertsPerPoly <= DT_VERTS_PER_POLYGON) {
@@ -699,10 +724,10 @@ PT(GeomNode) NavMeshBuilder::draw_poly_mesh_geom() {
     const float y = orig[1] + v[1] * ch;
     const float z = orig[2] + v[2] * cs;
 
-    //vertex.add_data3(x, -z, y); //if origingally model is z-up
-    vertex.add_data3(x, y, z); //if originally model is y-up
+    vertex.add_data3(x, -z, y); //if origingally model is z-up
+    //vertex.add_data3(x, y, z); //if originally model is y-up
     colour.add_data4((float)rand() / RAND_MAX, (float)rand() / RAND_MAX, (float)rand() / RAND_MAX, 1);
-    std::cout << "index: " << i / 3 << "\t" << x << "\t" << y << "\t" << z << "\n";
+    //std::cout << "index: " << i / 3 << "\t" << x << "\t" << y << "\t" << z << "\n";
 
   }
 
@@ -732,7 +757,7 @@ PT(GeomNode) NavMeshBuilder::draw_poly_mesh_geom() {
         // The edge beginning with this vertex connects to 
         // polygon p[j + nvp].
       }
-      std::cout << "p[j]: " << p[j] << std::endl;
+      //std::cout << "p[j]: " << p[j] << std::endl;
 
     }
     prim->close_primitive();

+ 14 - 7
panda/src/navigation/navMeshBuilder.h

@@ -62,20 +62,28 @@ struct BuildSettings {
 };
 
 
-class NavMeshBuilder {
+class EXPCL_NAVIGATION NavMeshBuilder {
 PUBLISHED:
-  float get_agent_radius() { return _agent_radius; }
-  float get_agent_height() { return _agent_height; }
-  float get_agent_climb() { return _agent_max_climb; }
+  float get_actor_radius() { return _agent_radius; }
+  float get_actor_height() { return _agent_height; }
+  float get_actor_climb() { return _agent_max_climb; }
   void set_actor_height(float h) { _agent_height = h; }
   void set_actor_radius(float r) { _agent_radius = r; }
   void set_actor_climb(float c) { _agent_max_climb = c; }
   void set_partition_type(std::string p);
   void reset_common_settings();
   bool from_node_path(NodePath node);
+  bool from_geom(PT(Geom) geom);
   PT(GeomNode) draw_poly_mesh_geom();
 
   PT(NavMesh) build();
+  NavMeshBuilder();
+
+  int get_vert_count() const { return _vert_count; }
+  int get_tri_count() const { return _tri_count; }
+  int get_pmesh_vert_count() { return _pmesh->nverts; }
+  int get_pmesh_poly_count() { return _pmesh->npolys; }
+  int get_pmesh_max_poly_count() { return _pmesh->maxpolys; }
 private:
   std::string _filename;
   float _scale;
@@ -142,7 +150,7 @@ protected:
 
 public:
 
-  NavMeshBuilder();
+  
   ~NavMeshBuilder();
   void set_context(rcContext *ctx) { _ctx = ctx; }
 
@@ -156,8 +164,7 @@ public:
   const float *get_verts() const { return _verts; }
   const float *get_normals() const { return _normals; }
   const int *get_tris() const { return _tris; }
-  int get_vert_count() const { return _vert_count; }
-  int get_tri_count() const { return _tri_count; }
+  
   const std::string& get_file_name() const { return _filename; }
   bool loaded_geom() { return _loaded; }
   

+ 1 - 1
panda/src/navigation/navMeshNode.h

@@ -21,7 +21,7 @@
 #include "navMesh.h"
 #include <string>
 
-class NavMeshNode: public PandaNode
+class EXPCL_NAVIGATION NavMeshNode: public PandaNode
 {
 PUBLISHED:
   NavMeshNode(const std::string &name, PT(NavMesh) nav_mesh);

+ 1 - 1
panda/src/navigation/navMeshQuery.h

@@ -18,7 +18,7 @@
 #include "DetourNavMeshQuery.h"
 #include "navMeshBuilder.h"
 
-class NavMeshQuery
+class EXPCL_NAVIGATION NavMeshQuery
 {
 PUBLISHED:
   void set_nav_query(NavMeshBuilder *nav_mesh) { _nav_query = nav_mesh->get_nav_query(); }

+ 85 - 0
tests/navigation/test_navigation.py

@@ -0,0 +1,85 @@
+from direct.showbase.ShowBase import ShowBase
+from panda3d import navigation
+from panda3d import core
+from panda3d.core import NodePath
+
+def test_input_geom():
+	
+    data = core.GeomVertexData("", core.GeomVertexFormat.get_v3(), core.Geom.UH_static)
+    vertex = core.GeomVertexWriter(data, "vertex")
+    vertex.add_data3((0, 0, 0))
+    vertex.add_data3((100, 1000, 0))
+    vertex.add_data3((0, 100, 2))
+    vertex.add_data3((500, 200, 2))
+    vertex.add_data3((170, 20, 2))
+    vertex.add_data3((100, 100, 100))
+
+    prim = core.GeomTriangles(core.Geom.UH_static)
+    prim.add_vertices(0, 1, 4)
+    prim.closePrimitive()
+
+    geom = core.Geom(data)
+    geom.add_primitive(prim)
+
+    prim.add_vertices(2,3,5)
+    prim.closePrimitive()
+    geom.add_primitive(prim)
+
+    node = core.GeomNode('gnode')
+    node.addGeom(geom)
+
+    scene = NodePath(node)
+    # Defining navmesh as object of NavMehsBuilder class.
+    nav = navigation.NavMeshBuilder()
+
+    # Extracting geoms from 'scene' and calculating vertices and triangles.
+    nav.fromNodePath(scene)
+    # Finding number of vertices.
+    vcount = nav.get_vert_count()
+    # Finding number of triangles.
+    tcount = nav.get_tri_count()
+    assert vcount == 6
+    assert tcount == 4
+
+def test_poly_mesh():
+	
+    data = core.GeomVertexData("", core.GeomVertexFormat.get_v3(), core.Geom.UH_static)
+    vertex = core.GeomVertexWriter(data, "vertex")
+    vertex.add_data3((0, 0, 0))
+    vertex.add_data3((100, 1000, 0))
+    vertex.add_data3((0, 100, 2))
+    vertex.add_data3((500, 200, 2))
+    vertex.add_data3((170, 20, 2))
+    vertex.add_data3((100, 100, 100))
+
+
+    prim = core.GeomTriangles(core.Geom.UH_static)
+    prim.add_vertices(0, 1, 2)
+    prim.closePrimitive()
+
+    geom = core.Geom(data)
+    geom.add_primitive(prim)
+
+    prim.add_vertices(2,3,5)
+    prim.closePrimitive()
+    geom.add_primitive(prim)
+
+    node = core.GeomNode('gnode')
+    node.addGeom(geom)
+
+    scene = NodePath(node)
+    # Defining navmesh as object of NavMehsBuilder class.
+    nav = navigation.NavMeshBuilder()
+
+    # Extracting geoms from 'scene' and calculating vertices and triangles.
+    nav.fromNodePath(scene)
+    
+    # Building the navmesh
+    navmesh = nav.build()
+    
+    nverts = nav.getPmeshVertCount()
+    npolys = nav.getPmeshPolyCount()
+    maxpolys = nav.getPmeshMaxPolyCount()
+    assert nverts == 230
+    assert npolys == 112
+    assert maxpolys == 226