Revision: 46691
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=46691
Author:   gaiaclary
Date:     2012-05-16 11:21:03 +0000 (Wed, 16 May 2012)
Log Message:
-----------
fix [#31320] Collada now supports import/export of loose edges (edges not 
attached to faces)

Modified Paths:
--------------
    trunk/blender/source/blender/collada/GeometryExporter.cpp
    trunk/blender/source/blender/collada/GeometryExporter.h
    trunk/blender/source/blender/collada/MeshImporter.cpp
    trunk/blender/source/blender/collada/MeshImporter.h

Modified: trunk/blender/source/blender/collada/GeometryExporter.cpp
===================================================================
--- trunk/blender/source/blender/collada/GeometryExporter.cpp   2012-05-16 
10:25:03 UTC (rev 46690)
+++ trunk/blender/source/blender/collada/GeometryExporter.cpp   2012-05-16 
11:21:03 UTC (rev 46691)
@@ -127,6 +127,7 @@
                createVertexColorSource(geom_id, me);
 
        // <vertices>
+
        COLLADASW::Vertices verts(mSW);
        verts.setId(getIdBySemantics(geom_id, 
COLLADASW::InputSemantic::VERTEX));
        COLLADASW::InputList &input_list = verts.getInputList();
@@ -134,6 +135,8 @@
        input_list.push_back(input);
        verts.add();
 
+       createLooseEdgeList(ob, me, geom_id, norind);
+
        // XXX slow             
        if (ob->totcol) {
                for (int a = 0; a < ob->totcol; a++)    {
@@ -149,7 +152,7 @@
        if (me->flag & ME_TWOSIDED) {
                mSW->appendTextBlock("<extra><technique 
profile=\"MAYA\"><double_sided>1</double_sided></technique></extra>");
        }
-       
+
        closeGeometry();
 
        if (this->export_settings->apply_modifiers)
@@ -157,11 +160,65 @@
                BKE_libblock_free_us(&(G.main->mesh), me);
        }
 
+
 #if 0
        dm->release(dm);
 #endif
 }
 
+
+void GeometryExporter::createLooseEdgeList(Object *ob,
+                                            Mesh   *me,
+                                            std::string& geom_id,
+                                            std::vector<Face>& norind)
+{
+
+       MEdge *medges = me->medge;
+       int totedges  = me->totedge;
+       int edges_in_linelist = 0;
+       std::vector<unsigned int> edge_list;
+       int index;
+
+       // Find all loose edges in Mesh 
+       // and save vertex indices in edge_list
+       for (index = 0; index < totedges; index++) 
+       {
+               MEdge *edge = &medges[index];
+
+               if (edge->flag & ME_LOOSEEDGE)
+               {
+                       edges_in_linelist += 1;
+                       edge_list.push_back(edge->v1);
+                       edge_list.push_back(edge->v2);
+               }
+       }
+
+       if (edges_in_linelist > 0)
+       {
+               // Create the list of loose edges
+               COLLADASW::Lines lines(mSW);
+
+               lines.setCount(edges_in_linelist);
+
+
+               COLLADASW::InputList &til = lines.getInputList();
+                       
+               // creates <input> in <lines> for vertices 
+               COLLADASW::Input input1(COLLADASW::InputSemantic::VERTEX, 
getUrlBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX), 0);
+               til.push_back(input1);
+
+               lines.prepareToAppendValues();
+
+               for (index = 0; index < edges_in_linelist; index++) 
+               {
+                       lines.appendValues(edge_list[2*index+1]);
+                       lines.appendValues(edge_list[2*index]);
+               }
+               lines.finish();
+       }
+
+}
+
 // powerful because it handles both cases when there is material and when 
there's not
 void GeometryExporter::createPolylist(short material_index,
                                        bool has_uvs,
@@ -247,6 +304,8 @@
                
        // performs the actual writing
        polylist.prepareToAppendValues();
+       
+
                
        // <p>
        int texindex = 0;

Modified: trunk/blender/source/blender/collada/GeometryExporter.h
===================================================================
--- trunk/blender/source/blender/collada/GeometryExporter.h     2012-05-16 
10:25:03 UTC (rev 46690)
+++ trunk/blender/source/blender/collada/GeometryExporter.h     2012-05-16 
11:21:03 UTC (rev 46691)
@@ -64,6 +64,11 @@
 
        void operator()(Object *ob);
 
+       void createLooseEdgeList(Object *ob,
+                                                    Mesh   *me,
+                                                    std::string& geom_id,
+                                                    std::vector<Face>& norind);
+
        // powerful because it handles both cases when there is material and 
when there's not
        void createPolylist(short material_index,
                                                bool has_uvs,

Modified: trunk/blender/source/blender/collada/MeshImporter.cpp
===================================================================
--- trunk/blender/source/blender/collada/MeshImporter.cpp       2012-05-16 
10:25:03 UTC (rev 46690)
+++ trunk/blender/source/blender/collada/MeshImporter.cpp       2012-05-16 
11:21:03 UTC (rev 46691)
@@ -52,6 +52,7 @@
 #include "BLI_listbase.h"
 #include "BLI_math.h"
 #include "BLI_string.h"
+#include "BLI_edgehash.h"
 
 #include "MEM_guardedalloc.h"
 }
@@ -262,7 +263,7 @@
 }
 #endif
 
-bool MeshImporter::is_nice_mesh(COLLADAFW::Mesh *mesh) // checks if mesh has 
supported primitive types: polylist, triangles, triangle_fans
+bool MeshImporter::is_nice_mesh(COLLADAFW::Mesh *mesh) // checks if mesh has 
supported primitive types: lines, polylist, triangles, triangle_fans
 {
        COLLADAFW::MeshPrimitiveArray& prim_arr = mesh->getMeshPrimitives();
 
@@ -291,6 +292,12 @@
                        }
                                
                }
+
+               else if ( type == COLLADAFW::MeshPrimitive::LINES )
+               {
+                       // TODO: Add Checker for line syntax here
+               }
+
                else if (type != COLLADAFW::MeshPrimitive::TRIANGLES && type!= 
COLLADAFW::MeshPrimitive::TRIANGLE_FANS) {
                        fprintf(stderr, "Primitive type %s is not 
supported.\n", type_str);
                        return false;
@@ -409,14 +416,202 @@
        return tottri;
 }
 
+// =====================================================================
+// condition 1: The Primitive has normals
+// condition 2: The number of normals equals the number of faces.
+// return true if both conditions apply.
+// return false otherwise.
+// =====================================================================
+bool MeshImporter::primitive_has_useable_normals(COLLADAFW::MeshPrimitive *mp) 
{
+
+       bool has_useable_normals = false;
+
+       int normals_count = mp->getNormalIndices().getCount();
+       if (normals_count > 0) {
+               int index_count   = mp->getPositionIndices().getCount();
+               if (index_count == normals_count) 
+                       has_useable_normals = true;
+               else {
+                       fprintf(stderr,
+                               "Warning: Number of normals %d is different 
from the number of vertices %d, skipping normals\n",
+                               normals_count, index_count );   
+               }
+       }
+
+       return has_useable_normals;
+
+}
+
+// =====================================================================
+// Assume that only TRIANGLES, TRIANGLE_FANS, POLYLIST and POLYGONS
+// have faces. (to be verified)
+// =====================================================================
+bool MeshImporter::primitive_has_faces(COLLADAFW::MeshPrimitive *mp) {
+
+       bool has_faces = false;
+       int type = mp->getPrimitiveType();
+       switch (type) {
+               case COLLADAFW::MeshPrimitive::TRIANGLES:
+               case COLLADAFW::MeshPrimitive::TRIANGLE_FANS:
+               case COLLADAFW::MeshPrimitive::POLYLIST:
+               case COLLADAFW::MeshPrimitive::POLYGONS: {
+                       has_faces = true;
+                       break;
+               }
+               default: {
+                       has_faces = false; 
+                       break;
+               }
+       }
+       return has_faces;
+}
+
+// =================================================================
+// Return the number of faces by summing up 
+// the facecounts of the parts.
+// hint: This is done because mesh->getFacesCount() does
+// count loose edges as extra faces, which is not what we want here.
+// =================================================================
+void MeshImporter::allocate_face_data(COLLADAFW::Mesh *mesh, Mesh *me, int 
new_tris) {
+       COLLADAFW::MeshPrimitiveArray& prim_arr = mesh->getMeshPrimitives();
+       int total_facecount = 0;
+
+       // collect edge_count and face_count from all parts
+       for (int i = 0; i < prim_arr.getCount(); i++) {         
+               COLLADAFW::MeshPrimitive *mp = prim_arr[i];
+               int type = mp->getPrimitiveType();
+               switch (type) {
+                       case COLLADAFW::MeshPrimitive::TRIANGLES:
+                       case COLLADAFW::MeshPrimitive::TRIANGLE_FANS:
+                       case COLLADAFW::MeshPrimitive::POLYLIST:
+                       case COLLADAFW::MeshPrimitive::POLYGONS: {
+                               size_t prim_totface = mp->getFaceCount();
+                               total_facecount += prim_totface;
+                               break;
+                       }
+                       default: break;
+               }
+       }
+
+       // allocate space for faces
+       if (total_facecount > 0) {
+               me->totface = total_facecount + new_tris;
+               me->mface   = (MFace*)CustomData_add_layer(&me->fdata, 
CD_MFACE, CD_CALLOC, NULL, me->totface);
+       }
+}
+
+unsigned int MeshImporter::get_loose_edge_count(COLLADAFW::Mesh *mesh) {
+       COLLADAFW::MeshPrimitiveArray& prim_arr = mesh->getMeshPrimitives();
+       int loose_edge_count = 0;
+
+       // collect edge_count and face_count from all parts
+       for (int i = 0; i < prim_arr.getCount(); i++) {         
+               COLLADAFW::MeshPrimitive *mp = prim_arr[i];
+               int type = mp->getPrimitiveType();
+               switch (type) {
+                       case COLLADAFW::MeshPrimitive::LINES: {
+                               size_t prim_totface = mp->getFaceCount();
+                               loose_edge_count += prim_totface;
+                               break;
+                       }
+                       default: break;
+               }
+       }
+       return loose_edge_count;
+}
+
+// =================================================================
+// This functin is copied from source/blender/editors/mesh/mesh_data.c
+//
+// TODO: (As discussed with sergey-) :
+// Maybe move this function to blenderkernel/intern/mesh.c 
+// and add definition to BKE_mesh.c
+// =================================================================
+void MeshImporter::mesh_add_edges(Mesh *mesh, int len)
+{
+       CustomData edata;
+       MEdge *medge;
+       int i, totedge;
+
+       if (len == 0)
+               return;
+
+       totedge = mesh->totedge + len;
+
+       /* update customdata  */
+       CustomData_copy(&mesh->edata, &edata, CD_MASK_MESH, CD_DEFAULT, 
totedge);
+       CustomData_copy_data(&mesh->edata, &edata, 0, 0, mesh->totedge);
+
+       if (!CustomData_has_layer(&edata, CD_MEDGE))
+               CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, 
totedge);
+
+       CustomData_free(&mesh->edata, mesh->totedge);
+       mesh->edata = edata;
+       mesh_update_customdata_pointers(mesh, FALSE); /* new edges don't change 
tessellation */
+
+       /* set default flags */
+       medge = &mesh->medge[mesh->totedge];
+       for (i = 0; i < len; i++, medge++)
+               medge->flag = ME_EDGEDRAW | ME_EDGERENDER | SELECT;
+
+       mesh->totedge = totedge;
+}
+
+// =================================================================
+// Read all loose edges.
+// Important: This function assumes that all edges from existing 
+// faces have allready been generated and added to me->medge
+// So this function MUST be called after read_faces() (see below)
+// =================================================================
+void MeshImporter::read_lines(COLLADAFW::Mesh *mesh, Mesh *me)
+{
+       unsigned int loose_edge_count = get_loose_edge_count(mesh);
+       if(loose_edge_count > 0) {
+
+               unsigned int face_edge_count  = me->totedge;
+               unsigned int total_edge_count = loose_edge_count + 
face_edge_count;
+               
+               mesh_add_edges(me, loose_edge_count);
+               MEdge *med = me->medge + face_edge_count;
+
+               COLLADAFW::MeshPrimitiveArray& prim_arr = 
mesh->getMeshPrimitives();
+
+               for (int i = 0; i < prim_arr.getCount(); i++) {
+                       
+                       COLLADAFW::MeshPrimitive *mp = prim_arr[i];
+
+                       int type = mp->getPrimitiveType();
+                       if (type == COLLADAFW::MeshPrimitive::LINES)
+                       {
+                               unsigned int edge_count  = mp->getFaceCount();
+                               unsigned int *indices    = 
mp->getPositionIndices().getData();
+                               
+                               for (int i = 0; i < edge_count; i++, med++) {
+                                       med->bweight= 0;
+                                       med->crease = 0;
+                                       med->flag   = 0;
+                                       med->v1     = indices[ 2*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