Revision: 65153
          http://sourceforge.net/p/brlcad/code/65153
Author:   ejno
Date:     2015-06-02 15:50:21 +0000 (Tue, 02 Jun 2015)
Log Message:
-----------
refactoring geometry detection (in progress)

Modified Paths:
--------------
    brlcad/trunk/src/libgcv/conv/fastgen4/fastgen4_write.cpp

Modified: brlcad/trunk/src/libgcv/conv/fastgen4/fastgen4_write.cpp
===================================================================
--- brlcad/trunk/src/libgcv/conv/fastgen4/fastgen4_write.cpp    2015-06-02 
14:27:58 UTC (rev 65152)
+++ brlcad/trunk/src/libgcv/conv/fastgen4/fastgen4_write.cpp    2015-06-02 
15:50:21 UTC (rev 65153)
@@ -1017,15 +1017,8 @@
 }
 
 
-// Determines whether the object with the given name is a member of a
-// CCONE-compatible region (typically created by the fastgen4 importer).
-//
-// Sets `new_name` to the name of the CCONE.
-// Sets `ro1`, `ro2`, `ri1`, and `ri2` to the values of
-// the lower/upper outer and inner radii.
-HIDDEN bool
-find_ccone_cutout(db_i &db, const std::string &name, std::string &new_name,
-                 fastf_t &ro1, fastf_t &ro2, fastf_t &ri1, fastf_t &ri2)
+HIDDEN Section::Cone *
+find_ccone_cutout(Section &section, db_i &db, const std::string &name)
 {
     RT_CK_DBI(&db);
 
@@ -1037,7 +1030,7 @@
     AutoFreePtr<db_full_path, db_free_full_path> autofree_path(&path);
 
     if (path.fp_len < 2)
-       return false;
+       return NULL;
 
     rt_db_internal comb_db_internal;
 
@@ -1058,14 +1051,14 @@
 
        if (t.tb_op != OP_SUBTRACT || !t.tb_left || !t.tb_right
            || t.tb_left->tr_op != OP_DB_LEAF || t.tb_right->tr_op != 
OP_DB_LEAF)
-           return false;
+           return NULL;
 
        outer_directory = db_lookup(&db, t.tb_left->tr_l.tl_name, false);
        inner_directory = db_lookup(&db, t.tb_right->tr_l.tl_name, false);
 
        // check for nonexistent members
        if (!outer_directory || !inner_directory)
-           return false;
+           return NULL;
     }
 
     rt_db_internal outer_db_internal, inner_db_internal;
@@ -1088,7 +1081,7 @@
         && outer_db_internal.idb_minor_type != ID_REC)
        || (inner_db_internal.idb_minor_type != ID_TGC
            && inner_db_internal.idb_minor_type != ID_REC))
-       return false;
+       return NULL;
 
     const rt_tgc_internal &outer_tgc = *static_cast<rt_tgc_internal *>
                                       (outer_db_internal.idb_ptr);
@@ -1099,164 +1092,64 @@
 
     // check cone geometry
     if (!tgc_is_ccone(outer_tgc) || !tgc_is_ccone(inner_tgc))
-       return false;
+       return NULL;
 
     if (!VEQUAL(outer_tgc.v, inner_tgc.v) || !VEQUAL(outer_tgc.h, inner_tgc.h))
-       return false;
+       return NULL;
 
     // store results
     --path.fp_len;
-    new_name = AutoFreePtr<char>(db_path_to_string(&path)).ptr;
-    ro1 = MAGNITUDE(outer_tgc.a);
-    ro2 = MAGNITUDE(outer_tgc.b);
-    ri1 = MAGNITUDE(inner_tgc.a);
-    ri2 = MAGNITUDE(inner_tgc.b);
+    const std::string new_name = 
AutoFreePtr<char>(db_path_to_string(&path)).ptr;
+    const fastf_t ro1 = MAGNITUDE(outer_tgc.a);
+    const fastf_t ro2 = MAGNITUDE(outer_tgc.b);
+    const fastf_t ri1 = MAGNITUDE(inner_tgc.a);
+    const fastf_t ri2 = MAGNITUDE(inner_tgc.b);
 
     // check radii
     if (ri1 >= ro1 || ri2 >= ro2)
-       return false;
+       return NULL;
 
-    return true;
+    point_t v2;
+    VADD2(v2, outer_tgc.v, outer_tgc.h);
+    return new Section::Cone(section, new_name, outer_tgc.v, v2, ro1, ro2, ri1,
+                            ri2);
 }
 
 
-// Determines whether the object with the given name is a member of a
-// CSPHERE-compatible region (typically created by the fastgen4 importer).
-//
-// Sets `new_name` to the name of the CSPHERE.
-// Sets `radius` and `thickness` to the radius and thickness of the CSPHERE.
-HIDDEN bool
-find_csphere_cutout(db_i &db, const std::string &name, std::string &new_name,
-                   fastf_t &radius, fastf_t &thickness)
+HIDDEN Section::Sphere *
+find_csphere_cutout(const Section &UNUSED(section), db_i &UNUSED(db),
+                   const std::string &UNUSED(name))
 {
-    RT_CK_DBI(&db);
-
-    db_full_path path;
-
-    if (db_string_to_path(&path, &db, name.c_str()))
-       throw std::logic_error("logic error: invalid path");
-
-    AutoFreePtr<db_full_path, db_free_full_path> autofree_path(&path);
-
-    if (path.fp_len < 2)
-       return false;
-
-    rt_db_internal comb_db_internal;
-
-    if (rt_db_get_internal(&comb_db_internal, path.fp_names[path.fp_len - 2], 
&db,
-                          NULL, &rt_uniresource) < 0)
-       throw std::runtime_error("rt_db_get_internal() failed");
-
-    AutoFreePtr<rt_db_internal, rt_db_free_internal> autofree_comb_db_internal(
-       &comb_db_internal);
-
-    rt_comb_internal &comb_internal = *static_cast<rt_comb_internal *>
-                                     (comb_db_internal.idb_ptr);
-    RT_CK_COMB(&comb_internal);
-
-    const directory *outer_directory, *inner_directory;
-    {
-       const tree::tree_node &t = comb_internal.tree->tr_b;
-
-       if (t.tb_op != OP_SUBTRACT || !t.tb_left || !t.tb_right
-           || t.tb_left->tr_op != OP_DB_LEAF || t.tb_right->tr_op != 
OP_DB_LEAF)
-           return false;
-
-       outer_directory = db_lookup(&db, t.tb_left->tr_l.tl_name, false);
-       inner_directory = db_lookup(&db, t.tb_right->tr_l.tl_name, false);
-
-       // check for nonexistent members
-       if (!outer_directory || !inner_directory)
-           return false;
-    }
-
-    rt_db_internal outer_db_internal, inner_db_internal;
-
-    if (rt_db_get_internal(&outer_db_internal, outer_directory, &db, NULL,
-                          &rt_uniresource) < 0)
-       throw std::runtime_error("rt_db_get_internal() failed");
-
-    AutoFreePtr<rt_db_internal, rt_db_free_internal> 
autofree_outer_db_internal(
-       &outer_db_internal);
-
-    if (rt_db_get_internal(&inner_db_internal, inner_directory, &db, NULL,
-                          &rt_uniresource) < 0)
-       throw std::runtime_error("rt_db_get_internal() failed");
-
-    AutoFreePtr<rt_db_internal, rt_db_free_internal> 
autofree_inner_db_internal(
-       &inner_db_internal);
-
-    if ((outer_db_internal.idb_minor_type != ID_SPH
-        && outer_db_internal.idb_minor_type != ID_ELL)
-       || (inner_db_internal.idb_minor_type != ID_SPH
-           && inner_db_internal.idb_minor_type != ID_ELL))
-       return false;
-
-    const rt_ell_internal &outer_ell = *static_cast<rt_ell_internal *>
-                                      (outer_db_internal.idb_ptr);
-    const rt_ell_internal &inner_ell = *static_cast<rt_ell_internal *>
-                                      (inner_db_internal.idb_ptr);
-    RT_ELL_CK_MAGIC(&outer_ell);
-    RT_ELL_CK_MAGIC(&inner_ell);
-
-    // check cone geometry
-    if (!ell_is_sphere(outer_ell) || !ell_is_sphere(inner_ell))
-       return false;
-
-    if (!VEQUAL(outer_ell.v, inner_ell.v))
-       return false;
-
-    // store results
-    --path.fp_len;
-    new_name = AutoFreePtr<char>(db_path_to_string(&path)).ptr;
-    const fastf_t ro1 = MAGNITUDE(outer_ell.a);
-    const fastf_t ri1 = MAGNITUDE(inner_ell.a);
-
-    // check radii
-    if (ri1 >= ro1)
-       return false;
-
-    radius = ro1;
-    thickness = ro1 - ri1;
-    return true;
+    // TODO
+    return NULL;
 }
 
 
-// Determines whether `bot` is a CHEX1-compatible BOT
-// (typically created by the fastgen4 importer).
-//
-// Stores CHEX1 points in `points`.
-// Stores CHEX1 thickness in `thickness`.
-// Stores centering mode in `grid_centered`.
-// Sets `volume_mode` according to whether the containing
-// Section is in volume or plate mode.
-HIDDEN bool
-bot_is_chex1(const rt_bot_internal &bot, fastf_t points[8][3],
-            fastf_t &thickness, bool &grid_centered, bool &volume_mode)
+HIDDEN Section::Hexahedron *
+get_chex1(Section &section, const std::string &name, const rt_bot_internal 
&bot)
 {
     RT_BOT_CK_MAGIC(&bot);
 
+    fastf_t hex_thickness = 0.0;
+    bool hex_grid_centered = true;
+
     if (bot.num_vertices != 8 || bot.num_faces != 12)
-       return false;
+       return NULL;
 
     if (bot.mode != RT_BOT_SOLID && bot.mode != RT_BOT_PLATE)
-       return false;
+       return NULL;
 
     if (bot.thickness) {
-       thickness = bot.thickness[0];
-       volume_mode = false;
+       hex_thickness = bot.thickness[0];
 
        for (std::size_t i = 1; i < bot.num_faces; ++i)
-           if (!EQUAL(bot.thickness[i], thickness))
+           if (!EQUAL(bot.thickness[i], hex_thickness))
                return false;
-    } else {
-       thickness = 0.0;
-       volume_mode = true;
     }
 
     if (bot.face_mode) {
        const bool face_mode = BU_BITTEST(bot.face_mode, 0);
-       grid_centered = !face_mode;
+       hex_grid_centered = !face_mode;
 
        std::size_t count = 0;
        BU_BITV_LOOP_START(bot.face_mode) {
@@ -1271,8 +1164,6 @@
            if (count)
                return false;
        }
-    } else {
-       grid_centered = true;
     }
 
     const int hex_faces[12][3] = {
@@ -1298,10 +1189,13 @@
            return false;
     }
 
+    fastf_t points[8][3];
+
     for (int i = 0; i < 8; ++i)
        VMOVE(points[i], &bot.vertices[i * 3]);
 
-    return true;
+    return new Section::Hexahedron(section, name, points, hex_thickness,
+                                  hex_grid_centered);
 }
 
 
@@ -1344,21 +1238,13 @@
            if (internal.idb_type != ID_SPH && !ell_is_sphere(ell))
                return false;
 
-           {
-               std::string new_name;
-               fastf_t radius, thickness;
+           Section section(name, true);
 
-               if (find_csphere_cutout(data.m_db, name, new_name, radius, 
thickness)) {
-                   // an imported CSPHERE with cutout
-                   Section section(name, true);
-                   section.add(new Section::Sphere(section, name, ell.v, 
radius, thickness));
-                   section.write(data.m_writer);
-                   break;
-               }
-           }
+           if (Section::Sphere *sphere = find_csphere_cutout(section, 
data.m_db, name))
+               section.add(sphere);
+           else
+               section.add(new Section::Sphere(section, name, ell.v, 
MAGNITUDE(ell.a)));
 
-           Section section(name, true);
-           section.add(new Section::Sphere(section, name, ell.v, 
MAGNITUDE(ell.a)));
            section.write(data.m_writer);
            break;
        }
@@ -1371,28 +1257,17 @@
            if (internal.idb_type != ID_REC && !tgc_is_ccone(tgc))
                return false;
 
-           point_t v2;
-           VADD2(v2, tgc.v, tgc.h);
+           Section section(name, true);
 
-           {
-               std::string new_name;
-               fastf_t ro1, ro2, ri1, ri2;
-
-               if (find_ccone_cutout(data.m_db, name, new_name, ro1, ro2, ri1, 
ri2)) {
-                   // an imported CCONE with cutout
-                   if (!data.m_recorded_ccones.insert(new_name).second)
-                       break; // already written
-
-                   Section section(new_name, true);
-                   section.add(new Section::Cone(section, new_name, tgc.v, v2, 
ro1, ro2, ri1,
-                                                 ri2));
-                   break;
-               }
+           if (Section::Cone *cone = find_ccone_cutout(section, data.m_db, 
name))
+               section.add(cone);
+           else {
+               point_t v2;
+               VADD2(v2, tgc.v, tgc.h);
+               section.add(new Section::Cone(section, name, tgc.v, v2, 
MAGNITUDE(tgc.a),
+                                             MAGNITUDE(tgc.b), 0.0, 0.0));
            }
 
-           Section section(name, true);
-           section.add(new Section::Cone(section, name, tgc.v, v2, 
MAGNITUDE(tgc.a),
-                                         MAGNITUDE(tgc.b), 0.0, 0.0));
            section.write(data.m_writer);
            break;
        }
@@ -1411,23 +1286,13 @@
            const rt_bot_internal &bot = *static_cast<rt_bot_internal 
*>(internal.idb_ptr);
            RT_BOT_CK_MAGIC(&bot);
 
-           {
-               fastf_t points[8][3];
-               fastf_t thickness;
-               bool grid_centered, volume_mode;
+           Section section(name, bot.mode == RT_BOT_SOLID);
 
-               if (bot_is_chex1(bot, points, thickness, grid_centered, 
volume_mode)) {
-                   // an imported CHEX1
-                   Section section(name, volume_mode);
-                   section.add(new Section::Hexahedron(section, name, points, 
thickness,
-                                                       grid_centered));
-                   section.write(data.m_writer);
-                   break;
-               }
-           }
+           if (Section::Hexahedron *hex = get_chex1(section, name, bot))
+               section.add(hex);
+           else
+               write_bot(section, bot);
 
-           Section section(name, bot.mode == RT_BOT_SOLID);
-           write_bot(section, bot);
            section.write(data.m_writer);
            break;
        }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to