@@ -1498,14 +1498,11 @@ public CSG triangulate(boolean fix) {
1498
1498
if (triangulated )
1499
1499
return this ;
1500
1500
1501
- //// com.neuronrobotics.sdk.common.Log.error("CSG triangulating for " +
1502
- //// name+"..");
1503
- ArrayList <Polygon > toAdd = new ArrayList <Polygon >();
1504
- ArrayList <Polygon > degenerates = new ArrayList <Polygon >();
1505
1501
if (providerOf3d == null && Debug3dProvider .provider != null )
1506
1502
providerOf3d = Debug3dProvider .provider ;
1507
1503
IDebug3dProvider start = Debug3dProvider .provider ;
1508
1504
Debug3dProvider .setProvider (null );
1505
+ //performTriangulation();
1509
1506
if (preventNonManifoldTriangles ) {
1510
1507
for (int i = 0 ; i < 2 ; i ++)
1511
1508
if (isUseGPU ()) {
@@ -1514,49 +1511,24 @@ public CSG triangulate(boolean fix) {
1514
1511
runCPUMakeManifold ();
1515
1512
}
1516
1513
}
1517
- try {
1518
- Stream <Polygon > polygonStream ;
1519
- polygonStream = polygons .stream ();
1520
- // TODO this should work in paralell but throws immpossible NPE's instead.
1521
- // if (getPolygons().size() > 200) {
1522
- // polygonStream = polygons.parallelStream();
1523
- // }
1524
- polygonStream .forEach (p -> updatePolygons (toAdd , degenerates , p ));
1525
- // for (int i = 0; i < polygons.size(); i++) {
1526
- // Polygon p = polygons.get(i);
1527
- // updatePolygons(toAdd, degenerates, p);
1528
- // }
1529
-
1530
- if (degenerates .size () > 0 ) {
1531
- //
1532
- // Debug3dProvider.setProvider(providerOf3d);
1533
-
1534
- if (fix ) {
1535
- Debug3dProvider .clearScreen ();
1536
- Stream <Polygon > degenStreeam ;
1537
- degenStreeam = polygons .stream (); // this operation is read-modify-write and can not be done in
1538
- // parallel
1539
- // com.neuronrobotics.sdk.common.Log.error("Found "+degenerates.size()+"
1540
- // degenerate triangles, Attempting to fix");
1541
- degenStreeam .forEach (p -> fixDegenerates (toAdd , p ));
1542
- } else {
1543
- needsDegeneratesPruned = true ;
1544
- toAdd .addAll (degenerates );
1545
- }
1546
- }
1547
- if (toAdd .size () > 0 ) {
1548
- setPolygons (toAdd );
1549
- }
1550
- // now all polygons are definantly triangles
1551
- triangulated = true ;
1552
- } catch (Throwable t ) {
1553
- t .printStackTrace ();
1554
-
1555
- }
1514
+ performTriangulation ();
1515
+ // now all polygons are definantly triangles
1516
+ triangulated = true ;
1556
1517
Debug3dProvider .setProvider (start );
1557
1518
return this ;
1558
1519
}
1559
1520
1521
+ private void performTriangulation () {
1522
+ ArrayList <Polygon > toAdd = new ArrayList <Polygon >();
1523
+
1524
+ Stream <Polygon > polygonStream ;
1525
+ polygonStream = polygons .stream ();
1526
+ polygonStream .forEach (p -> updatePolygons (toAdd , p ));
1527
+ if (toAdd .size () > 0 ) {
1528
+ setPolygons (toAdd );
1529
+ }
1530
+ }
1531
+
1560
1532
private void runCPUMakeManifold () {
1561
1533
long start = System .currentTimeMillis ();
1562
1534
System .err .println ("Cleaning up the mesh by adding coincident points to the polygons they touch" );
@@ -1706,99 +1678,28 @@ public void run() {
1706
1678
System .out .println ("Data processed!" );
1707
1679
}
1708
1680
1709
- private CSG fixDegenerates (ArrayList <Polygon > toAdd , Polygon p ) {
1710
- Debug3dProvider .clearScreen ();
1711
- Debug3dProvider .addObject (p );
1712
- ArrayList <Vertex > degen = p .getDegeneratePoints ();
1713
- Edge longEdge = p .getLongEdge ();
1714
- ArrayList <Polygon > polygonsSharing = new ArrayList <Polygon >();
1715
- ArrayList <Polygon > polygonsSharingFixed = new ArrayList <Polygon >();
1716
-
1717
- for (Polygon ptoA : toAdd ) {
1718
- ArrayList <Edge > edges = ptoA .edges ();
1719
- for (Edge e : edges ) {
1720
- if (e .equals (longEdge )) {
1721
- //// com.neuronrobotics.sdk.common.Log.error("Degenerate Mate Found!");
1722
- polygonsSharing .add (ptoA );
1723
- Debug3dProvider .addObject (ptoA );
1724
- // TODO inject the points into the found edge
1725
- // upstream reparirs to mesh generation made this code effectivly unreachable
1726
- // in case that turns out to be false, pick up here
1727
- // the points in degen need to be inserted into the matching polygons
1728
- // both list of points should be right hand, but since they are other polygons,
1729
- // that may not be the case, so sorting needs to take place
1730
- ArrayList <Vertex > newpoints = new ArrayList <Vertex >();
1731
- for (Vertex v : ptoA .getVertices ()) {
1732
- newpoints .add (v );
1733
- if (e .isThisPointOneOfMine (v , Plane .EPSILON_Point )) {
1734
- for (Vertex v2 : degen )
1735
- newpoints .add (v2 );
1736
- }
1737
- }
1738
- Polygon e2 = new Polygon (newpoints , ptoA .getStorage ());
1739
- try {
1740
- List <Polygon > t = PolygonUtil .concaveToConvex (e2 );
1741
- for (Polygon poly : t ) {
1742
- if (!poly .isDegenerate ()) {
1743
- polygonsSharingFixed .add (poly );
1744
- }
1745
-
1746
- }
1747
- } catch (Exception ex ) {
1748
- ex .printStackTrace ();
1749
- // retriangulation failed, ok, whatever man, moving on...
1750
- }
1751
- }
1752
- }
1753
- }
1754
- if (polygonsSharing .size () == 0 ) {
1755
- //// com.neuronrobotics.sdk.common.Log.error("Error! Degenerate triangle does
1756
- //// not share edge with any triangle");
1757
- }
1758
- if (polygonsSharingFixed .size () > 0 ) {
1759
- toAdd .removeAll (polygonsSharing );
1760
- toAdd .addAll (polygonsSharingFixed );
1761
- }
1762
- return this ;
1763
- }
1764
-
1765
- private CSG updatePolygons (ArrayList <Polygon > toAdd , ArrayList <Polygon > degenerates , Polygon p ) {
1766
- // p=PolygonUtil.pruneDuplicatePoints(p);
1681
+ private CSG updatePolygons (ArrayList <Polygon > toAdd , Polygon p ) {
1767
1682
if (p == null )
1768
1683
return this ;
1769
- // if(p.isDegenerate()) {
1770
- // degenerates.add(p);
1771
- // return;
1772
- // }
1684
+
1773
1685
1774
1686
if (p .getVertices ().size () == 3 ) {
1775
1687
toAdd .add (p );
1776
1688
} else {
1777
- // //com.neuronrobotics.sdk.common.Log.error("Fixing error in STL " + name + "
1778
- // polygon# " + i + "
1779
- // number of vertices " + p.vertices.size());
1689
+
1780
1690
try {
1781
- List <Polygon > triangles = PolygonUtil .concaveToConvex (p );
1782
- for (Polygon poly : triangles ) {
1783
- toAdd .add (poly );
1691
+ if (!p .areAllPointsCollinear ()) {
1692
+ List <Polygon > triangles = PolygonUtil .concaveToConvex (p );
1693
+ for (Polygon poly : triangles ) {
1694
+ toAdd .add (poly );
1695
+ }
1696
+ }else {
1697
+ System .err .println ("Polygon is colinear, removing " +p );
1784
1698
}
1785
1699
} catch (Throwable ex ) {
1786
- //ex.printStackTrace();
1700
+ System .err .println ("Failed to triangulate " +p );
1701
+ ex .printStackTrace ();
1787
1702
progressMoniter .progressUpdate (1 , 1 , "Pruning bad polygon CSG::updatePolygons " + p , null );
1788
- // try {PolygonUtil.concaveToConvex(p);} catch (Throwable ex2) {
1789
- // ex2.printStackTrace();
1790
- // }
1791
- // Debug3dProvider.setProvider(providerOf3d);
1792
- // //ex.printStackTrace();
1793
- // Debug3dProvider.clearScreen();
1794
- // Debug3dProvider.addObject(p);
1795
- // try {
1796
- // List<Polygon> triangles = PolygonUtil.concaveToConvex(p);
1797
- // toAdd.addAll(triangles);
1798
- // }catch(java.lang.IllegalStateException ise) {
1799
- // ise.printStackTrace();
1800
- // }
1801
- // Debug3dProvider.setProvider(null);
1802
1703
}
1803
1704
1804
1705
}
@@ -3439,4 +3340,9 @@ public static boolean isUseGPU() {
3439
3340
public static void setUseGPU (boolean useGPU ) {
3440
3341
CSG .useGPU = useGPU ;
3441
3342
}
3343
+
3344
+ public boolean isBoundsTouching (CSG incoming ) {
3345
+ return getBounds ().isBoundsTouching (incoming .getBounds ());
3346
+ }
3347
+
3442
3348
}
0 commit comments