124124
125125@ SuppressWarnings ("restriction" )
126126public class CSG implements IuserAPI , Serializable {
127+ private static int MinPolygonsForOffloading = 200 ;
127128 private static final long serialVersionUID = 4071874097772427063L ;
128129 private static IDebug3dProvider providerOf3d = null ;
129130 private static int numFacesInOffset = 15 ;
@@ -147,11 +148,11 @@ public class CSG implements IuserAPI, Serializable {
147148 private static String defaultcolor = "#007956" ;
148149
149150 /** The color. */
150- //private Color color = getDefaultColor();
151- private double r = getDefaultColor ().getRed ();
152- private double g = getDefaultColor ().getGreen ();
153- private double b = getDefaultColor ().getBlue ();
154- private double o = getDefaultColor ().getOpacity ();
151+ // private Color color = getDefaultColor();
152+ private double r = getDefaultColor ().getRed ();
153+ private double g = getDefaultColor ().getGreen ();
154+ private double b = getDefaultColor ().getBlue ();
155+ private double o = getDefaultColor ().getOpacity ();
155156 /** The manipulator. */
156157 private Affine manipulator ;
157158 private Bounds bounds ;
@@ -228,10 +229,10 @@ public Color getColor() {
228229 * @param color the new color
229230 */
230231 public CSG setColor (Color color ) {
231- r = color .getRed ();
232- g = color .getGreen ();
233- b = color .getBlue ();
234- o = color .getOpacity ();
232+ r = color .getRed ();
233+ g = color .getGreen ();
234+ b = color .getBlue ();
235+ o = color .getOpacity ();
235236 for (Polygon p : polygons )
236237 p .setColor (color );
237238 return this ;
@@ -800,15 +801,16 @@ public CSG optimization(OptType type) {
800801 * @return union of this csg and the specified csg
801802 */
802803 public CSG union (CSG csg ) {
803- if (CSGClient .isRunning ()) {
804- ArrayList <CSG > go =new ArrayList <CSG >(Arrays .asList (this ));
805- try {
806- return CSGClient .getClient ().union (go ).get (0 );
807- } catch (Exception e ) {
808- // TODO Auto-generated catch block
809- e .printStackTrace ();
804+ if (this .polygons .size () > getMinPolygonsForOffloading () || csg .polygons .size () > getMinPolygonsForOffloading ())
805+ if (CSGClient .isRunning ()) {
806+ ArrayList <CSG > go = new ArrayList <CSG >(Arrays .asList (this ));
807+ try {
808+ return CSGClient .getClient ().union (go ).get (0 );
809+ } catch (Exception e ) {
810+ // TODO Auto-generated catch block
811+ e .printStackTrace ();
812+ }
810813 }
811- }
812814// triangulate();
813815// csg.triangulate();
814816 switch (getOptType ()) {
@@ -956,19 +958,28 @@ public static CSG unionAll(CSG... csgs) {
956958 }
957959
958960 public static CSG unionAll (List <CSG > csgs ) {
959- if (CSGClient .isRunning ()) {
960- List <CSG > back ;
961- try {
962- back = CSGClient .getClient ().union (csgs );
963- return back .get (0 );
964- } catch (Exception e ) {
965- // TODO Auto-generated catch block
966- e .printStackTrace ();
961+
962+ if (CSGClient .isRunning ()) {
963+ boolean offload = false ;
964+ for (int i = 0 ; i < csgs .size (); i ++)
965+ if (csgs .get (i ).polygons .size () > getMinPolygonsForOffloading ()) {
966+ offload = true ;
967+ break ;
968+ }
969+ if (offload ) {
970+ List <CSG > back ;
971+ try {
972+ back = CSGClient .getClient ().union (csgs );
973+ return back .get (0 );
974+ } catch (Exception e ) {
975+ // TODO Auto-generated catch block
976+ e .printStackTrace ();
977+ }
967978 }
968979 }
969980 CSG first = csgs .get (0 );
970981 return first .union (csgs .stream ().skip (1 ).collect (Collectors .toList ()));
971-
982+
972983 }
973984
974985 public static CSG hullAll (CSG ... csgs ) {
@@ -1157,7 +1168,15 @@ private CSG _unionNoOpt(CSG csg) {
11571168 * @return difference of this csg and the specified csgs
11581169 */
11591170 public CSG difference (List <CSG > csgs ) {
1160-
1171+ if (CSGClient .isRunning ()) {
1172+ ArrayList <CSG > go = new ArrayList <CSG >(csgs );
1173+ try {
1174+ return CSGClient .getClient ().difference (go ).get (0 );
1175+ } catch (Exception e ) {
1176+ // TODO Auto-generated catch block
1177+ e .printStackTrace ();
1178+ }
1179+ }
11611180 if (csgs .isEmpty ()) {
11621181 return this .clone ();
11631182 }
@@ -1233,15 +1252,16 @@ public CSG difference(CSG... csgs) {
12331252 * @return difference of this csg and the specified csg
12341253 */
12351254 public CSG difference (CSG csg ) {
1236- if (CSGClient .isRunning ()) {
1237- ArrayList <CSG > go =new ArrayList <CSG >(Arrays .asList (this ,csg ));
1238- try {
1239- return CSGClient .getClient ().difference (go ).get (0 );
1240- } catch (Exception e ) {
1241- // TODO Auto-generated catch block
1242- e .printStackTrace ();
1255+ if (this .polygons .size () > getMinPolygonsForOffloading () || csg .polygons .size () > getMinPolygonsForOffloading ())
1256+ if (CSGClient .isRunning ()) {
1257+ ArrayList <CSG > go = new ArrayList <CSG >(Arrays .asList (this , csg ));
1258+ try {
1259+ return CSGClient .getClient ().difference (go ).get (0 );
1260+ } catch (Exception e ) {
1261+ // TODO Auto-generated catch block
1262+ e .printStackTrace ();
1263+ }
12431264 }
1244- }
12451265// triangulate();
12461266// csg.triangulate();
12471267 try {
@@ -1392,15 +1412,16 @@ private CSG _differenceNoOpt(CSG csg) {
13921412 * @return intersection of this csg and the specified csg
13931413 */
13941414 public CSG intersect (CSG csg ) {
1395- if (CSGClient .isRunning ()) {
1396- ArrayList <CSG > go =new ArrayList <CSG >(Arrays .asList (this ,csg ));
1397- try {
1398- return CSGClient .getClient ().intersect (go ).get (0 );
1399- } catch (Exception e ) {
1400- // TODO Auto-generated catch block
1401- e .printStackTrace ();
1415+ if (this .polygons .size () > getMinPolygonsForOffloading () || csg .polygons .size () > getMinPolygonsForOffloading ())
1416+ if (CSGClient .isRunning ()) {
1417+ ArrayList <CSG > go = new ArrayList <CSG >(Arrays .asList (this , csg ));
1418+ try {
1419+ return CSGClient .getClient ().intersect (go ).get (0 );
1420+ } catch (Exception e ) {
1421+ // TODO Auto-generated catch block
1422+ e .printStackTrace ();
1423+ }
14021424 }
1403- }
14041425// triangulate();
14051426// csg.triangulate();
14061427 Node a = new Node (this .clone ().getPolygons ());
@@ -1447,7 +1468,15 @@ public CSG intersect(CSG csg) {
14471468 * @return intersection of this csg and the specified csgs
14481469 */
14491470 public CSG intersect (List <CSG > csgs ) {
1450-
1471+ if (CSGClient .isRunning ()) {
1472+ ArrayList <CSG > go = new ArrayList <CSG >(csgs );
1473+ try {
1474+ return CSGClient .getClient ().intersect (go ).get (0 );
1475+ } catch (Exception e ) {
1476+ // TODO Auto-generated catch block
1477+ e .printStackTrace ();
1478+ }
1479+ }
14511480 if (csgs .isEmpty ()) {
14521481 return this .clone ();
14531482 }
@@ -1544,27 +1573,28 @@ public CSG triangulate(boolean fix) {
15441573 triangulated = false ;
15451574 if (triangulated )
15461575 return this ;
1547- if (CSGClient .isRunning ()) {
1548- ArrayList <CSG > go =new ArrayList <CSG >(Arrays .asList (this ));
1549- try {
1550- return CSGClient .getClient ().triangulate (go ).get (0 );
1551- } catch (Exception e ) {
1552- // TODO Auto-generated catch block
1553- e .printStackTrace ();
1576+ if (this .polygons .size () > getMinPolygonsForOffloading ())
1577+ if (CSGClient .isRunning ()) {
1578+ ArrayList <CSG > go = new ArrayList <CSG >(Arrays .asList (this ));
1579+ try {
1580+ return CSGClient .getClient ().triangulate (go ).get (0 );
1581+ } catch (Exception e ) {
1582+ // TODO Auto-generated catch block
1583+ e .printStackTrace ();
1584+ }
15541585 }
1555- }
15561586 if (providerOf3d == null && Debug3dProvider .provider != null )
15571587 providerOf3d = Debug3dProvider .provider ;
15581588 IDebug3dProvider start = Debug3dProvider .provider ;
15591589 Debug3dProvider .setProvider (null );
1560- //performTriangulation();
1590+ // performTriangulation();
15611591 if (preventNonManifoldTriangles ) {
1562- //for (int i = 0; i < 1; i++)
1563- if (isUseGPU ()) {
1564- runGPUMakeManifold ();
1565- } else {
1566- runCPUMakeManifold ();
1567- }
1592+ // for (int i = 0; i < 1; i++)
1593+ if (isUseGPU ()) {
1594+ runGPUMakeManifold ();
1595+ } else {
1596+ runCPUMakeManifold ();
1597+ }
15681598 }
15691599 performTriangulation ();
15701600 // now all polygons are definantly triangles
@@ -1576,22 +1606,23 @@ public CSG triangulate(boolean fix) {
15761606 private void performTriangulation () {
15771607 ArrayList <Polygon > toAdd = new ArrayList <Polygon >();
15781608 int failedPolys = 0 ;
1579- for (int i = 0 ; i < polygons .size ();i ++) {
1609+ for (int i = 0 ; i < polygons .size (); i ++) {
15801610 Polygon p = polygons .get (i );
15811611 CSG ret = updatePolygons (toAdd , p );
1582- if (ret ==null )
1612+ if (ret == null )
15831613 failedPolys ++;
15841614 }
1585- if (failedPolys > 0 )
1586- System .out .println ("Pruned " + failedPolys + " polygons from CSG " + getName ());
1615+ if (failedPolys > 0 )
1616+ System .out .println ("Pruned " + failedPolys + " polygons from CSG " + getName ());
15871617 if (toAdd .size () > 0 ) {
15881618 setPolygons (toAdd );
15891619 }
15901620 }
15911621
15921622 private void runCPUMakeManifold () {
15931623 long start = System .currentTimeMillis ();
1594- //System.err.println("Cleaning up the mesh by adding coincident points to the polygons they touch");
1624+ // System.err.println("Cleaning up the mesh by adding coincident points to the
1625+ // polygons they touch");
15951626
15961627 int totalAdded = 0 ;
15971628 double tOL = 1.0e-11 ;
@@ -1653,7 +1684,7 @@ private void runCPUMakeManifold() {
16531684 }
16541685 totalAdded += 32 ;
16551686 threads .clear ();
1656- if (threadIndex / 32 % 50 == 0 || j == polygons .size () - 1 ) {
1687+ if (threadIndex / 32 % 50 == 0 || j == polygons .size () - 1 ) {
16571688 progressMoniter .progressUpdate (j , polygons .size (),
16581689 "STL Processing Polygons for Manifold Vertex, #" + totalAdded + " added so far" , this );
16591690 }
@@ -1669,7 +1700,8 @@ private void runCPUMakeManifold() {
16691700 // Auto-generated catch block
16701701 e .printStackTrace ();
16711702 }
1672- //progressMoniter.progressUpdate(polygons.size(),polygons.size(),"Manifold fix took " + (System.currentTimeMillis() - start),this);
1703+ // progressMoniter.progressUpdate(polygons.size(),polygons.size(),"Manifold fix
1704+ // took " + (System.currentTimeMillis() - start),this);
16731705 }
16741706
16751707 private void runGPUMakeManifold () {
@@ -1742,19 +1774,18 @@ private CSG updatePolygons(ArrayList<Polygon> toAdd, Polygon p) {
17421774 if (p == null )
17431775 return this ;
17441776
1745-
17461777 if (p .getVertices ().size () == 3 ) {
17471778 toAdd .add (p );
17481779 } else {
17491780
17501781 try {
1751- if (!p .areAllPointsCollinear ()) {
1782+ if (!p .areAllPointsCollinear ()) {
17521783 List <Polygon > triangles = PolygonUtil .concaveToConvex (p );
17531784 for (Polygon poly : triangles ) {
17541785 toAdd .add (poly );
17551786 }
1756- }else {
1757- System .err .println ("Polygon is colinear, removing " + p );
1787+ } else {
1788+ System .err .println ("Polygon is colinear, removing " + p );
17581789 return null ;
17591790 }
17601791 } catch (Throwable ex ) {
@@ -2214,6 +2245,15 @@ public ArrayList<CSG> mink(CSG travelingShape) {
22142245 * @return
22152246 */
22162247 public ArrayList <CSG > minkowskiHullShape (CSG travelingShape ) {
2248+ if (CSGClient .isRunning ()) {
2249+ ArrayList <CSG > go = new ArrayList <CSG >(Arrays .asList (this ,travelingShape ));
2250+ try {
2251+ return CSGClient .getClient ().minkowskiHullShape (go );
2252+ } catch (Exception e ) {
2253+ // TODO Auto-generated catch block
2254+ e .printStackTrace ();
2255+ }
2256+ }
22172257 ArrayList <CSG > bits = new ArrayList <>();
22182258 for (Polygon p : this .getPolygons ()) {
22192259 List <Vector3d > plist = new ArrayList <>();
@@ -2404,7 +2444,7 @@ public CSG historySync(CSG dyingCSG) {
24042444 }
24052445 if (getName ().length () == 0 )
24062446 setName (dyingCSG .getName ());
2407- setColor ( dyingCSG .getColor ());
2447+ setColor (dyingCSG .getColor ());
24082448 return this ;
24092449 }
24102450
@@ -2533,8 +2573,9 @@ public CSG setRegenerate(IRegenerate function) {
25332573 regenerate = function ;
25342574 return this ;
25352575 }
2576+
25362577 public IRegenerate getRegenerate () {
2537- return regenerate ;
2578+ return regenerate ;
25382579 }
25392580
25402581 public CSG regenerate () {
@@ -3113,7 +3154,7 @@ public boolean isHole() {
31133154
31143155 public CSG syncProperties (CSG dying ) {
31153156 getStorage ().syncProperties (dying .getStorage ());
3116- regenerate = dying .regenerate ;
3157+ regenerate = dying .regenerate ;
31173158 return this ;
31183159 }
31193160
@@ -3403,4 +3444,12 @@ public boolean isBoundsTouching(CSG incoming) {
34033444 return getBounds ().isBoundsTouching (incoming .getBounds ());
34043445 }
34053446
3447+ public static int getMinPolygonsForOffloading () {
3448+ return MinPolygonsForOffloading ;
3449+ }
3450+
3451+ public static void setMinPolygonsForOffloading (int minPolygonsForOffloading ) {
3452+ MinPolygonsForOffloading = minPolygonsForOffloading ;
3453+ }
3454+
34063455}
0 commit comments