Revision: 73207
          http://sourceforge.net/p/brlcad/code/73207
Author:   starseeker
Date:     2019-05-29 21:59:15 +0000 (Wed, 29 May 2019)
Log Message:
-----------
Start drilling down into why we're getting incorrect normals.

Modified Paths:
--------------
    brlcad/trunk/src/libbrep/cdt.cpp
    brlcad/trunk/src/libbrep/cdt.h
    brlcad/trunk/src/libbrep/cdt_edge.cpp
    brlcad/trunk/src/libbrep/cdt_util.cpp

Modified: brlcad/trunk/src/libbrep/cdt.cpp
===================================================================
--- brlcad/trunk/src/libbrep/cdt.cpp    2019-05-29 19:37:52 UTC (rev 73206)
+++ brlcad/trunk/src/libbrep/cdt.cpp    2019-05-29 21:59:15 UTC (rev 73207)
@@ -44,6 +44,7 @@
     return tdir;
 }
 
+#if 0
 static void
 validate_face_normals(
        struct ON_Brep_CDT_State *s_cdt,
@@ -80,6 +81,7 @@
        }
     }
 }
+#endif
 
 struct on_brep_mesh_data {
     std::vector<ON_3dPoint *> vfpnts;
@@ -310,7 +312,117 @@
     }
 }
 
+static void
+triangles_incorrect_normals(struct ON_Brep_CDT_State *s_cdt, struct 
on_brep_mesh_data *md, int face_index)
+{
+    p2t::CDT *cdt = s_cdt->p2t_faces[face_index];
+    std::map<p2t::Point *, ON_3dPoint *> *pointmap = 
s_cdt->tri_to_on3_maps[face_index];
+    std::map<p2t::Point *, ON_3dPoint *> *normalmap = 
s_cdt->tri_to_on3_norm_maps[face_index];
+    std::vector<p2t::Triangle*> tris = cdt->GetTriangles();
+    for (size_t i = 0; i < tris.size(); i++) {
+       p2t::Triangle *t = tris[i];
+       if (md->tris_degen.size() > 0 && md->tris_degen.find(t) != 
md->tris_degen.end()) {
+           continue;
+       }
 
+
+       int invalid_face_normal = 0;
+       ON_3dVector tdir = p2tTri_Normal(t, pointmap);
+       p2t::Point *p[3];
+       p[0] = t->GetPoint(0);
+       p[1] = t->GetPoint(1);
+       p[2] = t->GetPoint(2);
+       int wnorm[3] = {0, 0, 0};
+       for (size_t j = 0; j < 3; j++) {
+           ON_3dPoint onorm = *(*normalmap)[p[j]];
+           if (s_cdt->brep->m_F[face_index].m_bRev) {
+               onorm = onorm * -1;
+           }
+           if (tdir.Length() > 0 && ON_DotProduct(onorm, tdir) < 0.1) {
+               invalid_face_normal++;
+               wnorm[j] = 1;
+           }
+       }
+
+       if (invalid_face_normal == 3) {
+           int tind[3] = {-1, -1, -1};
+           int edge_pnt_cnt = 0;
+           for (int j = 0; j < 3; j++) {
+               if (s_cdt->p2t_edge_points->find(p[j]) != 
s_cdt->p2t_edge_points->end()) {
+                   tind[j] = (*s_cdt->p2t_edge_points)[p[j]];
+                   edge_pnt_cnt++;
+               }
+           }
+           bu_log("Face %d: 3 normals in wrong direction:\n", face_index);
+           if ((tind[0] != -1) && (tind[0] == tind[1]) && (tind[1] == 
tind[2])) {
+               bu_log("invalid face normal with all points on the same trim 
curve\n");
+               ON_3dPoint tri_cent = (*(*pointmap)[t->GetPoint(0)] + 
*(*pointmap)[t->GetPoint(1)] + *(*pointmap)[t->GetPoint(2)])/3;
+               bu_log("Tri p1: %f %f %f\n", (*pointmap)[t->GetPoint(0)]->x, 
(*pointmap)[t->GetPoint(0)]->y, (*pointmap)[t->GetPoint(0)]->z);
+               bu_log("Tri p2: %f %f %f\n", (*pointmap)[t->GetPoint(1)]->x, 
(*pointmap)[t->GetPoint(1)]->y, (*pointmap)[t->GetPoint(1)]->z);
+               bu_log("Tri p3: %f %f %f\n", (*pointmap)[t->GetPoint(2)]->x, 
(*pointmap)[t->GetPoint(2)]->y, (*pointmap)[t->GetPoint(2)]->z);
+               bu_log("Tri center: %f %f %f\n", tri_cent.x, tri_cent.y, 
tri_cent.z);
+               bu_log("Tri norm: %f %f %f\n", tdir.x, tdir.y, tdir.z);
+               bu_log("edge trim: %d\n", 
(*s_cdt->p2t_edge_points)[t->GetPoint(0)]);
+
+               md->triangle_cnt--;
+               md->tris_degen.insert(t);
+           }
+
+           if (edge_pnt_cnt == 2) {
+               bu_log("problem point from surface:\n");
+               for (int j = 0; j < 3; j++) {
+                   if (tind[j] == -1) {
+                       ON_3dPoint *spt = (*pointmap)[p[j]];
+                       bu_log("%f %f %f\n", spt->x, spt->y, spt->z);
+                   }
+               }
+           }
+           if (edge_pnt_cnt == 1) {
+               bu_log("two problem points from surface:\n");
+               for (int j = 0; j < 3; j++) {
+                   if (tind[j] == -1) {
+                       ON_3dPoint *spt = (*pointmap)[p[j]];
+                       bu_log("%f %f %f\n", spt->x, spt->y, spt->z);
+                   }
+               }
+
+           }
+           if (edge_pnt_cnt == 0) {
+               bu_log("no edge pnts involved:\n");
+               for (int j = 0; j < 3; j++) {
+                   if (tind[j] == -1) {
+                       ON_3dPoint *spt = (*pointmap)[p[j]];
+                       bu_log("%f %f %f\n", spt->x, spt->y, spt->z);
+                   }
+               }
+
+
+           }
+       }
+
+       if (invalid_face_normal == 2) {
+           bu_log("Face %d: 2 normals in wrong direction:\n", face_index);
+           for (int j = 0; j < 3; j++) {
+               if (wnorm[j] != 0) {
+                   ON_3dPoint *spt = (*pointmap)[p[j]];
+                   bu_log("%f %f %f\n", spt->x, spt->y, spt->z);
+               }
+           }
+       }
+       if (invalid_face_normal == 1) {
+           bu_log("Face %d: 1 normal in wrong direction:\n", face_index);
+           for (int j = 0; j < 3; j++) {
+               if (wnorm[j] != 0) {
+                   ON_3dPoint *spt = (*pointmap)[p[j]];
+                   bu_log("%f %f %f\n", spt->x, spt->y, spt->z);
+               }
+           }
+
+       }
+    }
+}
+
+
 static void
 Process_Loop_Edges(
        struct ON_Brep_CDT_State *s_cdt,
@@ -462,6 +574,7 @@
                // map point to last entry to 3d point
                p2t::Point *p = new p2t::Point((brep_loop_points[li])[i].p2d.x, 
(brep_loop_points[li])[i].p2d.y);
                polyline.push_back(p);
+               (*s_cdt->p2t_edge_points)[p] = 
(brep_loop_points[li])[i].trim_ind;
                (*pointmap)[p] = (brep_loop_points[li])[i].p3d;
                (*on3_to_tri)[(brep_loop_points[li])[i].p3d].insert(p);
                (*normalmap)[p] = (brep_loop_points[li])[i].n3d;
@@ -1554,6 +1667,10 @@
        }
     }
 
+    for (int face_index = 0; face_index != s_cdt->brep->m_F.Count(); 
face_index++) {
+       triangles_incorrect_normals(s_cdt, &md, face_index);
+    }
+
     bu_log("tri_cnt_init: %zd\n", md.triangle_cnt);
     for (int face_index = 0; face_index != s_cdt->brep->m_F.Count(); 
face_index++) {
        std::vector<p2t::Triangle *> *tri_add = 
s_cdt->p2t_extra_faces[face_index];
@@ -1565,7 +1682,7 @@
     }
     bu_log("tri_cnt_init+: %zd\n", md.triangle_cnt);
 
-
+#if 0
     // Validate normals using the triangle itself
     for (int face_index = 0; face_index != s_cdt->brep->m_F.Count(); 
face_index++) {
        p2t::CDT *cdt = s_cdt->p2t_faces[face_index];
@@ -1579,6 +1696,7 @@
        }
        validate_face_normals(s_cdt, tri_add, NULL, face_index);
     }
+#endif
 
     // Know how many faces and points now - initialize BoT container.
     *fcnt = (int)md.triangle_cnt;

Modified: brlcad/trunk/src/libbrep/cdt.h
===================================================================
--- brlcad/trunk/src/libbrep/cdt.h      2019-05-29 19:37:52 UTC (rev 73206)
+++ brlcad/trunk/src/libbrep/cdt.h      2019-05-29 21:59:15 UTC (rev 73207)
@@ -114,6 +114,7 @@
 
     /* Poly2Tri data */
     p2t::CDT **p2t_faces;
+    std::map<p2t::Point *, int> *p2t_edge_points;
     std::vector<p2t::Triangle *> **p2t_extra_faces;
     std::map<ON_2dPoint *, ON_3dPoint *> **on2_to_on3_maps;
     std::map<p2t::Point *, ON_3dPoint *> **tri_to_on3_maps;

Modified: brlcad/trunk/src/libbrep/cdt_edge.cpp
===================================================================
--- brlcad/trunk/src/libbrep/cdt_edge.cpp       2019-05-29 19:37:52 UTC (rev 
73206)
+++ brlcad/trunk/src/libbrep/cdt_edge.cpp       2019-05-29 21:59:15 UTC (rev 
73207)
@@ -153,8 +153,8 @@
     fastf_t emindist = (cdt_tol->min_dist < 0.5*loop_min_dist) ? 
cdt_tol->min_dist : 0.5 * loop_min_dist;
     ON_3dPoint trim1_mid_2d, trim2_mid_2d;
     if (edge->m_edge_index == 790) {
-       trim1_mid_2d = get_trim_midpt(&t1, &trim, sbtp1->t, ebtp1->t, 
edge_mid_3d, emindist, 1);
-       trim2_mid_2d = get_trim_midpt(&t2, trim2, sbtp2->t, ebtp2->t, 
edge_mid_3d, emindist, 1);
+       trim1_mid_2d = get_trim_midpt(&t1, &trim, sbtp1->t, ebtp1->t, 
edge_mid_3d, emindist, 0);
+       trim2_mid_2d = get_trim_midpt(&t2, trim2, sbtp2->t, ebtp2->t, 
edge_mid_3d, emindist, 0);
     } else {
        trim1_mid_2d = get_trim_midpt(&t1, &trim, sbtp1->t, ebtp1->t, 
edge_mid_3d, emindist, 0);
        trim2_mid_2d = get_trim_midpt(&t2, trim2, sbtp2->t, ebtp2->t, 
edge_mid_3d, emindist, 0);

Modified: brlcad/trunk/src/libbrep/cdt_util.cpp
===================================================================
--- brlcad/trunk/src/libbrep/cdt_util.cpp       2019-05-29 19:37:52 UTC (rev 
73206)
+++ brlcad/trunk/src/libbrep/cdt_util.cpp       2019-05-29 21:59:15 UTC (rev 
73207)
@@ -182,6 +182,7 @@
     cdt->face_degen_pnts = (std::set<p2t::Point *> 
**)bu_calloc(brep->m_F.Count(), sizeof(std::set<p2t::Point *> *), "degenerate 
edge points");
 
     cdt->p2t_faces = (p2t::CDT **)bu_calloc(brep->m_F.Count(), sizeof(p2t::CDT 
*), "poly2tri triangulations");
+    cdt->p2t_edge_points = new std::map<p2t::Point *, int>;
     cdt->p2t_extra_faces = (std::vector<p2t::Triangle *> 
**)bu_calloc(brep->m_F.Count(), sizeof(std::vector<p2t::Triangle *> *), "extra 
p2t faces");
     cdt->on2_to_on3_maps = (std::map<ON_2dPoint *, ON_3dPoint *> 
**)bu_calloc(brep->m_F.Count(), sizeof(std::map<ON_2dPoint *, ON_3dPoint *> *), 
"ON_2dPoint to ON_3dPoint maps");
     cdt->tri_to_on3_maps = (std::map<p2t::Point *, ON_3dPoint *> 
**)bu_calloc(brep->m_F.Count(), sizeof(std::map<p2t::Point *, ON_3dPoint *> *), 
"poly2tri point to ON_3dPoint maps");
@@ -241,6 +242,8 @@
     delete s_cdt->min_edge_seg_len;
     delete s_cdt->max_edge_seg_len;
 
+    delete s_cdt->p2t_edge_points;
+
     delete s_cdt->w3dpnts;
     delete s_cdt->vert_pnts;
     delete s_cdt->w3dnorms;

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