Skip to content

Commit

Permalink
Export an STL even when a polygon is broken.
Browse files Browse the repository at this point in the history
  • Loading branch information
madhephaestus committed Dec 21, 2023
1 parent 29be425 commit a17aa17
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 35 deletions.
2 changes: 1 addition & 1 deletion JCSG
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

import javafx.application.Platform;

@SuppressWarnings("restriction")
public interface IInteractiveUIElementProvider {

default void runLater(Runnable r) {
Platform.runLater(r);
new Thread(()->{
Platform.runLater(() -> {
try {
r.run();
} catch (Throwable t) {
t.printStackTrace();
}
});
}).start();
}

default TransformNR getCamerFrame() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javafx.scene.paint.Color;
import javafx.scene.transform.Affine;
import javafx.scene.paint.PhongMaterial;

public class manipulation {
HashMap<EventType<MouseEvent>, EventHandler<MouseEvent>> map = new HashMap<>();
double startx = 0;
Expand All @@ -38,41 +39,46 @@ public class manipulation {
public TransformNR currentPose;
private PhongMaterial color;// = new PhongMaterial(getColor());
private PhongMaterial highlight = new PhongMaterial(Color.GOLD);
private enum DragState{
IDLE,
Dragging

private enum DragState {
IDLE, Dragging
}

private DragState state = DragState.IDLE;

public void addEventListener(Runnable r) {
if (eventListeners.contains(r))
return;
eventListeners.add(r);
}

public void addDependant(manipulation r) {
if (dependants.contains(r))
return;
dependants.add(r);
}

public void addSaveListener(Runnable r) {
if (saveListeners.contains(r))
return;
saveListeners.add(r);
}

public void clearListeners() {
// TODO Auto-generated method stub
saveListeners.clear();
eventListeners.clear();
}


private void fireMove( TransformNR trans, TransformNR camFrame2) {

private void fireMove(TransformNR trans, TransformNR camFrame2) {
for (manipulation R : dependants) {
R.performMove(trans,camFrame2);
R.performMove(trans, camFrame2);
}
for (Runnable R : eventListeners) {
R.run();
}
}

private void fireSave() {
new Thread(() -> {
for (Runnable R : saveListeners) {
Expand All @@ -85,18 +91,22 @@ public manipulation(Affine mm, Vector3d o, CSG m, TransformNR p) {
this.manipulationMatrix = mm;
this.orintation = o;
this.manip = m;
color=new PhongMaterial(m.getColor());
color = new PhongMaterial(m.getColor());
this.globalPose = p;
currentPose=p.copy();
currentPose = p.copy();
getUi().runLater(() -> {
TransformFactory.nrToAffine(globalPose, manipulationMatrix);
try {
TransformFactory.nrToAffine(globalPose, manipulationMatrix);
} catch (Throwable t) {
t.printStackTrace();
}
});

map.put(MouseEvent.ANY, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
String name = event.getEventType().getName();
switch(name) {
switch (name) {
case "MOUSE_PRESSED":
pressed(event);
break;
Expand All @@ -108,25 +118,25 @@ public void handle(MouseEvent event) {
break;
case "MOUSE_MOVED":
// ignore
break;
break;
case "MOUSE_ENTERED":
m.getMesh().setMaterial(highlight);
break;
break;
case "MOUSE_EXITED":
if(state==DragState.IDLE)
if (state == DragState.IDLE)
m.getMesh().setMaterial(color);
break;
break;
default:
//System.out.println("UNKNOWN! Mouse event "+name);
// System.out.println("UNKNOWN! Mouse event "+name);
break;
}

}
});
manip.getStorage().set("manipulator", map);
manip.setManipulator(manipulationMatrix);
}

private void pressed(MouseEvent event) {
state = DragState.Dragging;
new Thread(() -> {
Expand All @@ -141,27 +151,31 @@ private void pressed(MouseEvent event) {
}
}).start();
}

private void release(MouseEvent event) {
mouseRelease(event);
for (manipulation R : dependants)
for (manipulation R : dependants)
R.mouseRelease(event);
state = DragState.IDLE;
manip.getMesh().setMaterial(color);
}

private void dragged(MouseEvent event) {
getUi().runLater(() -> {
setDragging(event);
double deltx = (startx - event.getScreenX());
double delty = (starty - event.getScreenY());
TransformNR trans = new TransformNR(deltx / depth, delty / depth, 0, new RotationNR());

performMove( trans,camFrame);
performMove(trans, camFrame);
});
event.consume();
}

public boolean isMoving() {
return state==DragState.Dragging;
return state == DragState.Dragging;
}

private void mouseRelease(MouseEvent event) {
if (dragging) {
dragging = false;
Expand All @@ -172,6 +186,7 @@ private void mouseRelease(MouseEvent event) {
fireSave();
}
}

private void setDragging(MouseEvent event) {
if (dragging == false) {
startx = event.getScreenX();
Expand All @@ -182,11 +197,12 @@ private void setDragging(MouseEvent event) {
R.setDragging(event);
}
}
private void performMove( TransformNR trans, TransformNR camFrame2) {

private void performMove(TransformNR trans, TransformNR camFrame2) {
TransformNR globalTMP = camFrame2.copy();
globalTMP.setX(0);
globalTMP.setY(0);
globalTMP.setZ(0);
globalTMP.setZ(0);
TransformNR global = globalTMP.times(trans);
newx = (global.getX() * orintation.x + globalPose.getX());
newy = (global.getY() * orintation.y + globalPose.getY());
Expand All @@ -199,8 +215,9 @@ private void performMove( TransformNR trans, TransformNR camFrame2) {
setGlobal(global);
// System.out.println(" drag "+global.getX()+" , "+global.getY()+" ,
// "+global.getZ()+" "+deltx+" "+delty);
fireMove(trans,camFrame2);
fireMove(trans, camFrame2);
}

private void setGlobal(TransformNR global) {
currentPose.setX(newx);
currentPose.setY(newy);
Expand All @@ -209,27 +226,27 @@ private void setGlobal(TransformNR global) {
TransformFactory.nrToAffine(global, manipulationMatrix);
});
}

public static IInteractiveUIElementProvider getUi() {
return ui;
}

public static void setUi(IInteractiveUIElementProvider ui) {
manipulation.ui = ui;
}

public void set(double newX, double newY, double newZ) {
newx=newX;
newy=newY;
newz=newZ;
newx = newX;
newy = newY;
newz = newZ;
globalPose.setX(newX);
globalPose.setY(newY);
globalPose.setZ(newZ);
setGlobal(new TransformNR(newX,newY,newZ,new RotationNR()));
setGlobal(new TransformNR(newX, newY, newZ, new RotationNR()));
for (Runnable R : eventListeners) {
R.run();
}

}


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ public ArrayList<File> generateManufacturingParts(List<CSG> totalAssembly , File
nameBase = dir.getAbsolutePath()+"/"+name;
index++;
if(part.getExportFormats()==null){
allCadStl.add(makeStl(nameBase,manufactured));// default to stl
try {
allCadStl.add(makeStl(nameBase,manufactured));// default to stl
}catch(Throwable t) {
System.err.println("Failed to generate "+part.getName());
t.printStackTrace();
}
}else{

for(String format:part.getExportFormats()){
Expand Down

0 comments on commit a17aa17

Please sign in to comment.