Revision: 73173
          http://sourceforge.net/p/brlcad/code/73173
Author:   starseeker
Date:     2019-05-24 14:41:08 +0000 (Fri, 24 May 2019)
Log Message:
-----------
refactor the normal check

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

Modified: brlcad/trunk/src/libbrep/cdt.cpp
===================================================================
--- brlcad/trunk/src/libbrep/cdt.cpp    2019-05-24 14:14:09 UTC (rev 73172)
+++ brlcad/trunk/src/libbrep/cdt.cpp    2019-05-24 14:41:08 UTC (rev 73173)
@@ -30,7 +30,55 @@
 
 #define BREP_PLANAR_TOL 0.05
 
+static ON_3dVector
+p2tTri_Normal(p2t::Triangle *t, std::map<p2t::Point *, ON_3dPoint *> *pointmap)
+{
+    ON_3dPoint *p1 = (*pointmap)[t->GetPoint(0)];
+    ON_3dPoint *p2 = (*pointmap)[t->GetPoint(1)];
+    ON_3dPoint *p3 = (*pointmap)[t->GetPoint(2)];
+
+    ON_3dVector e1 = *p2 - *p1;
+    ON_3dVector e2 = *p3 - *p1;
+    ON_3dVector tdir = ON_CrossProduct(e1, e2);
+    tdir.Unitize();
+    return tdir;
+}
+
 static void
+validate_face_normals(
+       struct ON_Brep_CDT_State *s_cdt,
+       std::vector<p2t::Triangle*> *tris,
+       std::set<p2t::Triangle*> *tris_degen,
+       int 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];
+    for (size_t i = 0; i < tris->size(); i++) {
+       p2t::Triangle *t = (*tris)[i];
+       if (tris_degen) {
+           if (tris_degen->size() > 0 && tris_degen->find(t) != 
tris_degen->end()) {
+               continue;
+           }
+       }
+       ON_3dVector tdir = p2tTri_Normal(t, pointmap);
+       for (size_t j = 0; j < 3; j++) {
+           p2t::Point *p = t->GetPoint(j);
+           ON_3dPoint *onorm = (*normalmap)[p];
+           if (tdir.Length() > 0 && ON_DotProduct(*onorm, tdir) < 0) {
+               ON_3dPoint tri_cent = (*(*pointmap)[t->GetPoint(0)] + 
*(*pointmap)[t->GetPoint(1)] + *(*pointmap)[t->GetPoint(2)])/3;
+               bu_log("Normal in wrong direction:\n");
+               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("onorm: %f %f %f\n", onorm->x, onorm->y, onorm->z);
+           }
+       }
+    }
+}
+
+static void
 Process_Loop_Edges(
        struct ON_Brep_CDT_State *s_cdt,
        ON_SimpleArray<BrepTrimPoint> *points,
@@ -920,20 +968,6 @@
    return 0;
 }
 
-static ON_3dVector
-p2tTri_Normal(p2t::Triangle *t, std::map<p2t::Point *, ON_3dPoint *> *pointmap)
-{
-    ON_3dPoint *p1 = (*pointmap)[t->GetPoint(0)];
-    ON_3dPoint *p2 = (*pointmap)[t->GetPoint(1)];
-    ON_3dPoint *p3 = (*pointmap)[t->GetPoint(2)];
-
-    ON_3dVector e1 = *p2 - *p1;
-    ON_3dVector e2 = *p3 - *p1;
-    ON_3dVector tdir = ON_CrossProduct(e1, e2);
-    tdir.Unitize();
-    return tdir;
-}
-
 // Generate a BoT with normals.
 int
 ON_Brep_CDT_Mesh(
@@ -974,7 +1008,6 @@
        triangle_cnt += tris.size();
        for (size_t i = 0; i < tris.size(); i++) {
            p2t::Triangle *t = tris[i];
-           ON_3dVector tdir = p2tTri_Normal(t, pointmap);
            tri_brep_face[t] = face_index;
            ON_3dPoint *tpnts[3] = {NULL, NULL, NULL};
            for (size_t j = 0; j < 3; j++) {
@@ -1036,18 +1069,6 @@
                        }
                    }
                    tpnts[j] = op;
-
-                   // TODO - validate onorm against triangle face
-                   if (tdir.Length() > 0 && ON_DotProduct(*onorm, tdir) < 0) {
-                       ON_3dPoint tri_cent = (*(*pointmap)[t->GetPoint(0)] + 
*(*pointmap)[t->GetPoint(1)] + *(*pointmap)[t->GetPoint(2)])/3;
-                       bu_log("Normal in wrong direction:\n");
-                       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("onorm: %f %f %f\n", onorm->x, onorm->y, 
onorm->z);
-                   }
                    
                } else {
                    bu_log("Face %d: p2t face without proper point info...\n", 
face.m_face_index);
@@ -1512,6 +1533,20 @@
     bu_log("tri_cnt_init+: %zd\n", triangle_cnt);
 
 
+    // 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];
+       std::vector<p2t::Triangle*> tris = cdt->GetTriangles();
+       validate_face_normals(s_cdt, &tris, &tris_degen, face_index);
+    }
+    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];
+       if (!tri_add) {
+           continue;
+       }
+       validate_face_normals(s_cdt, tri_add, NULL, face_index);
+    }
+
     // Know how many faces and points now - initialize BoT container.
     *fcnt = (int)triangle_cnt;
     *faces = (int *)bu_calloc(triangle_cnt*3, sizeof(int), "new faces array");

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