@@ -249,8 +249,73 @@ private static CSG optimize(
249249 code = code .replace ("$edgeApprox$" , "" + edgeApprox );
250250 code = code .replace ("$edgeTriangleQuality$" , "" + edgeTriangleQuality );
251251
252- // code = code.replace("$edgeApprox$", "" + edgeApprox);
253- // code = code.replace("$edgeTriangleQuality$", "" + edgeTriangleQuality);
252+ Shell .execute (tmpDir .toFile (), code ).print ().waitFor ();
253+
254+ return STL .file (stlFile );
255+
256+ } catch (IOException e ) {
257+ e .printStackTrace (System .err );
258+ throw new RuntimeException (
259+ "optimization failed due to io exception" , e );
260+ }
261+ }
262+
263+ /**
264+ * Repairs the specified csg mesh object.
265+ *
266+ * <b>Note: </b>the size of the
267+ * object during repairs can have a high impact on the overall
268+ * repair quality. Therefore, this method allows the specification of
269+ * the size at which the repair algorithm is performed. After repairing
270+ * the object is returned at original size.
271+ *
272+ * @param csg csg to repair
273+ * @param size object size at which to perform the repair operation (minimum
274+ * dimension)
275+ * @param tol default tolerance
276+ * @param maxTol maximum tolerance
277+ * @return repaired csg mesh object
278+ */
279+ public static CSG repair (CSG csg , double size , double tol ,
280+ double maxTol ) {
281+ return scaleMinDimensionTo (csg , size ,
282+ (csgObj ) -> repair (csgObj , tol , maxTol ));
283+ }
284+
285+ /**
286+ * Repairs the specified csg mesh object.
287+ *
288+ * @param csg csg to repair
289+ * @param tol default tolerance
290+ * @param maxTol maximum tolerance
291+ * @return repaired csg mesh object
292+ */
293+ private static CSG repair (
294+ CSG csg , double tol ,
295+ double maxTol ) {
296+ try {
297+
298+ Path tmpDir = Files .createTempDirectory ("jcsgmeshopt" );
299+ Path stlFile = Paths .get (tmpDir .toAbsolutePath ().toString (),
300+ "csg.stl" );
301+
302+ System .out .println ("mesh-ext: csg file: " + stlFile );
303+
304+ Files .write (stlFile , csg .toStlString ().getBytes ());
305+
306+ String code = read ("repair.lua" );
307+
308+ String pathVariable = stlFile .toAbsolutePath ().toString ();//
309+
310+ if (System .getProperty ("os.name" ).toLowerCase ().contains ("windows" )) {
311+ pathVariable = pathVariable .replace ("\\ " , "\\ \\ " );
312+ }
313+
314+ code = code .replace ("$fileName$" , "\" "
315+ + pathVariable + "\" " );
316+ code = code .replace ("$removeDoublesTOL$" , "" + tol );
317+ code = code .replace ("$resolveTOL$" , "" + maxTol );
318+
254319 Shell .execute (tmpDir .toFile (), code ).print ().waitFor ();
255320
256321 return STL .file (stlFile );
0 commit comments