@@ -340,9 +340,8 @@ public CSG processPolygonsToTriangles(ArrayList<Polygon> polygons) throws Coline
340340 * matching the BSP's behavior.
341341 */
342342 private static int intern (Vertex v , Map <String , Integer > index , List <Vector3d > list ) {
343- // 1. Lower the precision slightly. 1e9 is too high for 'double' stability
344- // after multiple CSG operations. 1e7 (0.1 nanometer) is the "sweet spot".
345- double precision = 1e9 ;
343+
344+ double precision = 0.0001 ;// 0.1d/Plane.getEPSILON();
346345
347346 long x = Math .round (v .pos .x * precision );
348347 long y = Math .round (v .pos .y * precision );
@@ -352,8 +351,6 @@ private static int intern(Vertex v, Map<String, Integer> index, List<Vector3d> l
352351
353352 return index .computeIfAbsent (key , k -> {
354353 int idx = list .size ();
355- // Use the RAW position for the first instance of this vertex.
356- // This keeps the geometry as true to the BSP as possible.
357354 list .add (v .pos .clone ());
358355 return idx ;
359356 });
@@ -1874,7 +1871,9 @@ public String toStlString() {
18741871 * @return the specified string builder
18751872 */
18761873 public StringBuilder toStlString (StringBuilder sb ) {
1874+
18771875 try {
1876+ makeManifold ();
18781877 sb .append ("solid v3d.csg\n " );
18791878 for (Polygon p : generatePolygonsFromMesh ()) {
18801879 try {
@@ -1892,21 +1891,11 @@ public StringBuilder toStlString(StringBuilder sb) {
18921891 }
18931892 }
18941893
1895- public CSG triangulate () {
1896- // return triangulate(false);
1897- return this ;
1898- }
1899-
1900- public CSG triangulate (boolean fix ) {
1901- // return triangulate(fix, false);
1902- return this ;
1903- }
1904-
19051894 // public CSG snapPoints() throws ColinearPointsException {
19061895 // return triangulate(false, true);
19071896 // }
19081897
1909- public CSG triangulate ( boolean fix , boolean justSnap ) throws ColinearPointsException {
1898+ public CSG makeManifold ( ) throws ColinearPointsException {
19101899 // if (fix && needsDegeneratesPruned)
19111900 // triangulated = false;
19121901 // if (triangulated)
@@ -1933,10 +1922,11 @@ public CSG triangulate(boolean fix, boolean justSnap) throws ColinearPointsExcep
19331922 int added = 0 ;
19341923 int itr = 1 ;
19351924 if (preventNonManifoldTriangles ) {
1925+
1926+ ArrayList <Polygon > polygons = generatePolygonsFromMesh ();
19361927 do {
19371928 long np = vertCount ;
19381929 long numberOfPolygons = getNumberOfTriangles ();
1939- ArrayList <Polygon > polygons = generatePolygonsFromMesh ();
19401930
19411931 int extraSpace = ExtraSpace ;
19421932 long longLength = 1 + np + ((numberOfPolygons + 1 ) * extraSpace );
@@ -1946,7 +1936,7 @@ public CSG triangulate(boolean fix, boolean justSnap) throws ColinearPointsExcep
19461936 System .err .println ("Processing Mesh Manifold with " + longLength * 4 + " byte buffer" );
19471937 added = 0 ;
19481938 try {
1949- added = runGPUMakeManifold (itr , np , (int ) longLength , numberOfPolygons , justSnap , polygons );
1939+ added = runGPUMakeManifold (itr , np , (int ) longLength , numberOfPolygons , polygons );
19501940 } catch (Exception ex ) {
19511941 ex .printStackTrace ();
19521942 }
@@ -1958,10 +1948,16 @@ public CSG triangulate(boolean fix, boolean justSnap) throws ColinearPointsExcep
19581948 }
19591949 }
19601950 } while (added < 0 && itr ++ < 51 );
1951+ try {
1952+ processPolygonsToTriangles (polygons );
1953+ } catch (ColinearPointsException e ) {
1954+ // TODO Auto-generated catch block
1955+ e .printStackTrace ();
1956+ }
19611957 }
19621958 // }
1963- if (! justSnap )
1964- performTriangulation ();
1959+
1960+
19651961 // System.out.println("Complete Triangulation \n\n");
19661962 // now all polygons are definantly triangles
19671963 // triangulated = true;
@@ -1980,7 +1976,7 @@ private void performTriangulation() {
19801976 // triangulation is performed when loading
19811977 }
19821978
1983- private int runGPUMakeManifold (int iteration , long np , int longLength , long numPoly , boolean justSnap ,
1979+ private int runGPUMakeManifold (int iteration , long np , int longLength , long numPoly ,
19841980 ArrayList <Polygon > polygons ) {
19851981 if (iteration < 0 || np <= 0 || longLength <= 0 || numPoly <= 0 )
19861982 throw new RuntimeException ("Error none of the dataa lengths can be negative nor 0" );
@@ -2075,15 +2071,15 @@ public void run() {
20752071 }
20762072 };
20772073
2078- gpuRun (numberOfPoints , snapPointsToDistance , done , "Snap Points Itr(" + iteration + ")" , () -> {
2079- tp [0 ] += snapChunk ;
2080- tp [1 ] += snapChunk ;
2081- if (tp [1 ] > polySizes .length )
2082- tp [1 ] = polySizes .length ;
2083- if (tp [0 ] < polySizes .length )
2084- return true ;
2085- return false ;
2086- }, iteration , polySizes .length / snapChunk );
2074+ // gpuRun(numberOfPoints, snapPointsToDistance, done, "Snap Points Itr(" + iteration + ")", () -> {
2075+ // tp[0] += snapChunk;
2076+ // tp[1] += snapChunk;
2077+ // if (tp[1] > polySizes.length)
2078+ // tp[1] = polySizes.length;
2079+ // if (tp[0] < polySizes.length)
2080+ // return true;
2081+ // return false;
2082+ // }, iteration, polySizes.length / snapChunk);
20872083 tp [0 ] = 0 ;
20882084 tp [1 ] = testPointChunk ;
20892085 HashSet <Integer > unique = new HashSet <Integer >();
@@ -2267,7 +2263,7 @@ public void run() {
22672263 }// Run method
22682264 };
22692265 pointsAdded = 0 ;
2270- if (! justSnap ) {
2266+
22712267 gpuRun (numberOfPolygons , findNonManifoldPoints , done , "Manifold Itr(" + iteration + ")" , () -> {
22722268 // for (int tp = 0; tp < uniquePoints.length; tp++)
22732269 // Iterate through each of the test points in host thread
@@ -2299,7 +2295,7 @@ public void run() {
22992295 }
23002296 return false ;
23012297 }, iteration , uniquePoints .length / testPointChunk );
2302- }
2298+
23032299 ArrayList <Polygon > newPoly = new ArrayList <>();
23042300 for (int i = 0 ; i < getNumberOfTriangles (); i ++) {
23052301 Polygon polygon = polygons .get (i );
@@ -2338,7 +2334,7 @@ public void run() {
23382334 polygon .getPoints ().clear ();
23392335 }
23402336 polygons .clear ();
2341- polygons = newPoly ;
2337+ polygons . addAll ( newPoly ) ;
23422338 return pointsAdded ;
23432339 }
23442340
@@ -2571,7 +2567,12 @@ public CSG color(Color c) {
25712567 * @throws ColinearPointsException
25722568 */
25732569 public StringBuilder toObjString (StringBuilder sb ) {
2574- triangulate (true );
2570+ try {
2571+ makeManifold ();
2572+ } catch (ColinearPointsException e ) {
2573+ // TODO Auto-generated catch block
2574+ e .printStackTrace ();
2575+ }
25752576 sb .append ("# Group" ).append ("\n " );
25762577 sb .append ("g v3d.csg\n " );
25772578 sb .append ("o " + (name == null || name .length () == 0 ? "CSG Export" : getName ()) + "\n " );
0 commit comments