[Bf-blender-cvs] [12fbd19ee73] soc-2020-io-performance: Add support for curves to be exported as nurbs

Ankit Meel noreply at git.blender.org
Thu Jun 25 14:25:51 CEST 2020


Commit: 12fbd19ee7358349fa7016bdbb46cb99c4f05ff1
Author: Ankit Meel
Date:   Tue Jun 23 16:57:34 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB12fbd19ee7358349fa7016bdbb46cb99c4f05ff1

Add support for curves to be exported as nurbs

===================================================================

M	source/blender/io/wavefront_obj/intern/wavefront_obj.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.hh

===================================================================

diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
index 25a6afd9a0a..e9e67712452 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj.hh
@@ -23,11 +23,13 @@
 
 #include "BKE_context.h"
 #include "BKE_lib_id.h"
+#include "BKE_curve.h"
 #include "BKE_mesh.h"
 #include "BKE_object.h"
 
 #include "BLI_vector.hh"
 
+#include "DNA_curve_types.h"
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
@@ -158,8 +160,14 @@ class OBJMesh {
 };
 
 class OBJNurbs {
-  /*TODO ankitm to be done*/
  public:
+  bContext *C;
+  const OBJExportParams *export_params;
+
+  Object *object;
+  Curve *curve;
+  void calc_vertex_coords(float coord[3], uint point_index);
+  const char *get_curve_info(int *nurbs_degree, int *curv_num);
 };
 
 }  // namespace obj
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
index e781ccba642..b72eaafe59d 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
@@ -257,6 +257,25 @@ void OBJMesh::calc_edge_vert_indices(uint r_vert_indices[2], uint edge_index)
   }
 }
 
+  void OBJNurbs::calc_vertex_coords(float coords[3], uint vert_index)
+  {
+    Nurb *nu = (Nurb *)curve->nurb.first;
+    BPoint *bpoint = nu->bp;
+    bpoint += vert_index;
+    copy_v3_v3(coords, bpoint->vec);
+  }
+  
+  const char *OBJNurbs::get_curve_info(int *nurbs_degree, int *curv_num)
+  {
+    Nurb *nurb = (Nurb *)curve->nurb.first;
+    *nurbs_degree = nurb->orderu - 1;
+    *curv_num = nurb->pntsv * nurb->pntsu;
+    if (nurb->flagu & CU_NURB_CYCLIC) {
+      *curv_num += *nurbs_degree;
+    }
+    return object->id.name + 2;
+  }
+
 /**
  * Traverses over and exports a single frame to a single OBJ file.
  */
@@ -273,8 +292,15 @@ static void export_frame(bContext *C, const OBJExportParams *export_params, cons
         exportable_meshes.append(OBJMesh(C, export_params, object_in_layer));
         break;
       case OB_CURVE:
-        /* TODO (ankitm) Conditionally push to export_nurbs too. */
+        if (export_params->export_curves_as_nurbs) {
+          export_nurbs.append(OBJNurbs());
+          export_nurbs.last().object = object_in_layer;
+          export_nurbs.last().export_params = export_params;
+          export_nurbs.last().C = C;
+        }
+        else {
         exportable_meshes.append(OBJMesh(C, export_params, object_in_layer));
+        }
       default:
         break;
     }
@@ -310,6 +336,12 @@ static void export_frame(bContext *C, const OBJExportParams *export_params, cons
 
     mesh_to_export.destruct();
   }
+  for (uint ob_iter = 0; ob_iter < export_nurbs.size(); ob_iter++) {
+    OBJNurbs &nurbs_to_export = export_nurbs[ob_iter];
+    nurbs_to_export.curve = (Curve *)nurbs_to_export.object->data;
+    frame_writer.write_nurbs_info(nurbs_to_export);
+  }
+  frame_writer.close_file();
 }
 
 /**
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
index 4da224410c1..6388d978e3a 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
@@ -212,6 +212,48 @@ void OBJWriter::write_curve_edges(OBJMesh &obj_mesh_data)
   }
 }
 
+void OBJWriter::write_nurbs_info(OBJNurbs &ob_nurbs)
+{
+  // todo object name max is 64.
+  Nurb *nurb = (Nurb *)ob_nurbs.curve->nurb.first;
+  uint tot_points = nurb->pntsv * nurb->pntsu;
+  float point_coord[3];
+  for (uint point_idx = 0; point_idx < tot_points; point_idx++) {
+    ob_nurbs.calc_vertex_coords(point_coord, point_idx);
+    fprintf(outfile, "v %f %f %f\n", point_coord[0], point_coord[1], point_coord[2]);
+  }
+
+  const char *nurbs_name;
+  int nurbs_degree;
+  /** Number of vertices in the curve + degree of the curve if it is cyclic. */
+  int curv_num;
+  nurbs_name = ob_nurbs.get_curve_info(&nurbs_degree, &curv_num);
+
+  fprintf(outfile, "g %s\ncstype bspline\n deg %d\n", nurbs_name, nurbs_degree);
+
+  /**
+   * curv_num refers to the vertices above written in relative indices.
+   * 0.0 1.0 -1 -2 -3 -4 for a non-cyclic curve with 4 points.
+   * 0.0 1.0 -1 -2 -3 -4 -1 -2 -3 for a cyclic curve with 4 points.
+   */
+  fprintf(outfile, "curv 0.0 1.0 ");
+  for (int i = 0; i < curv_num; i++) {
+    fprintf(outfile, "%d ", -1 * ((i % tot_points) + 1));
+  }
+  fprintf(outfile, "\n");
+
+  /**
+   * In parm u, between 0 and 1, curv_num + 2 equidistant numbers are inserted.
+   */
+  fprintf(outfile, "parm u 0.000000 ");
+  for (int i = 1; i <= curv_num + 2; i++) {
+    fprintf(outfile, "%f ", 1.0f * i / (curv_num + 2 + 1));
+  }
+  fprintf(outfile, "1.000000\n");
+
+  fprintf(outfile, "end\n");
+}
+
 /** When there are multiple objects in a frame, the indices of previous objects' coordinates or
  * normals add up.
  */
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.hh
index fa11d70336f..c0f74c24fdc 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.hh
@@ -70,6 +70,9 @@ class OBJWriter {
   /** Define and write an edge of a curve converted to mesh or a primitive circle as l v1 v2 */
   void write_curve_edges(OBJMesh &obj_mesh_data);
 
+  /** Write one nurb of a curve. */
+  void write_nurbs_info(OBJNurbs &ob_nurbs);
+
  private:
   /** Destination OBJ file for one frame, and one writer instance. */
   FILE *_outfile;



More information about the Bf-blender-cvs mailing list