Skip to content

Commit

Permalink
rotation bug fix(#3), scaling vertices/edges
Browse files Browse the repository at this point in the history
+fixed a bug with setting new rotation anchor points when having multiple objects on the scene

+the width of vertices/edges now scales with the max distance of vertices
  • Loading branch information
esontak committed May 7, 2015
1 parent e324de0 commit e5b7f98
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 90 deletions.
Binary file modified .gradle/2.0/taskArtifacts/cache.properties.lock
Binary file not shown.
Binary file modified .gradle/2.0/taskArtifacts/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/2.0/taskArtifacts/fileSnapshots.bin
Binary file not shown.
Binary file modified .gradle/2.0/taskArtifacts/outputFileStates.bin
Binary file not shown.
Binary file modified .gradle/2.0/taskArtifacts/taskArtifacts.bin
Binary file not shown.
185 changes: 98 additions & 87 deletions src/main/java/edu/gcsc/jfx3d/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public class Main extends Application{
private boolean firstRun = true;

EventHandler mouseB;
private ArrayList<EventHandler> mouseBehaviorList = new ArrayList<>();

File file;

Expand Down Expand Up @@ -273,7 +274,7 @@ public Group createContent() {

ugxSubsetCount = ugxr.getNumberOfSubsets();

return root;
return enableNewFocusPoint(dragDrop(root,renderedUGXGeometries.size()),renderedUGXGeometries.size());
}

private void handleKeyboard(Scene scene,PerspectiveCamera camera){
Expand Down Expand Up @@ -478,33 +479,15 @@ private VBox addGuiElements(){

fileOpenNewFile.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
for (int i = (ultraRoot.getChildren().size()-1); i > 1 ; i--) {
ultraRoot.getChildren().remove(i);
}
renderedUGXGeometries.clear();
mousePlaneList.clear();
mousePlaneList.add(new Rectangle(800, 800, Color.TRANSPARENT));
Rectangle rect = setupMousePlane(0);
renderedUGXGeometries.add(new Group(enableNewFocusPoint(dragDrop(createContent(),0))));

ultraRoot.getChildren().add(rect);
ultraRoot.getChildren().add(renderedUGXGeometries.get(0));

addGeometryToScene(true);
}
});

fileOpenAdditionalFile.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
int size = renderedUGXGeometries.size();
mousePlaneList.add(new Rectangle(800, 800, Color.TRANSPARENT));
Rectangle rect = setupMousePlane(size);
Group newG = new Group(enableNewFocusPoint(dragDrop(createContent(),size)));
renderedUGXGeometries.add(newG);

ultraRoot.getChildren().addAll(newG,rect);

addGeometryToScene(false);
}
});

Expand Down Expand Up @@ -595,107 +578,104 @@ private SubScene createScene3D(Group group,PerspectiveCamera camera) {

private Group dragDrop(Node node,int index){


node.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent event) {
if (event.isSecondaryButtonDown()) {
node.setMouseTransparent(true); // node will not be picked
mousePlaneList.get(index).setMouseTransparent(false); // mousePlane will be pickable
node.startFullDrag(); // this redirects drag events from the origin (node) to the picked target (which will be mousePlane)
}
}
});


@Override
public void handle(MouseEvent event) {
if (event.isSecondaryButtonDown()) {
node.setMouseTransparent(true); // node will not be picked
mousePlaneList.get(index).setMouseTransparent(false); // mousePlane will be pickable
node.startFullDrag(); // this redirects drag events from the origin (node) to the picked target (which will be mousePlane)
}
}
});

node.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent event) {
if (!event.isSecondaryButtonDown()) {
node.setMouseTransparent(false);
mousePlaneList.get(index).setMouseTransparent(true);
}
}
});
node.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (!event.isSecondaryButtonDown()) {
node.setMouseTransparent(false);
mousePlaneList.get(index).setMouseTransparent(true);
}
}
});

mousePlaneList.get(index).setOnMouseDragOver(new EventHandler<MouseDragEvent>() {
@Override public void handle(MouseDragEvent event) {
@Override
public void handle(MouseDragEvent event) {
if (event.isSecondaryButtonDown()) {
Point3D coords = event.getPickResult().getIntersectedPoint();
double x = coords.getX();
double y = coords.getY();
double z = coords.getZ();

coords = mousePlaneList.get(index).localToParent(new Point3D(x, y, z)); //mouseplane has the same parent as the real plane and objects like cube

node.setTranslateX(coords.getX());
node.setTranslateY(coords.getY());
node.setTranslateZ(coords.getZ());
}
}
});

return (Group) node;

}

private Rectangle setupMousePlane(int index) {

mousePlaneList.get(index).setLayoutX(-800 / 2);
mousePlaneList.get(index).setLayoutY(-800 / 2);
mousePlaneList.get(index).setOpacity(0.7);
mousePlaneList.get(index).setMouseTransparent(true);
mousePlaneList.get(index).setDepthTest(DepthTest.DISABLE); // this makes the plane to be picked even if there are objects closer to the camera

mousePlaneList.get(index).setOnMouseDragOver(new EventHandler<MouseDragEvent>() {
@Override
public void handle(MouseDragEvent me) {
if (me.isSecondaryButtonDown() && me.isAltDown()) {
//do nothing, we are rotating on the gizmo
} else if (me.isSecondaryButtonDown()) {
Point3D coords = me.getPickResult().getIntersectedPoint();
mouseOldX = mousePosX;
mouseOldY = mousePosY;
mousePosX = coords.getX();
mousePosY = coords.getY();

double z = coords.getZ();

coords = mousePlaneList.get(index).localToParent(new Point3D(mousePosX, mousePosY, z)); //mouseplane has the same parent as the real plane and objects like cube
mousePosX = coords.getX();
mousePosY = coords.getY();
mouseDeltaX = (mousePosX - mouseOldX);
mouseDeltaY = (mousePosY - mouseOldY);
mousePlaneList.get(index).setLayoutX(-800 / 2);
mousePlaneList.get(index).setLayoutY(-800 / 2);
mousePlaneList.get(index).setOpacity(0.7);
mousePlaneList.get(index).setMouseTransparent(true);
mousePlaneList.get(index).setDepthTest(DepthTest.DISABLE); // this makes the plane to be picked even if there are objects closer to the camera

mousePlaneList.get(index).setOnMouseDragOver(new EventHandler<MouseDragEvent>() {
@Override
public void handle(MouseDragEvent me) {
if (me.isSecondaryButtonDown() && me.isAltDown()) {
//do nothing, we are rotating on the gizmo
} else if (me.isSecondaryButtonDown()) {
Point3D coords = me.getPickResult().getIntersectedPoint();
mouseOldX = mousePosX;
mouseOldY = mousePosY;
mousePosX = coords.getX();
mousePosY = coords.getY();

double z = coords.getZ();

coords = mousePlaneList.get(index).localToParent(new Point3D(mousePosX, mousePosY, z)); //mouseplane has the same parent as the real plane and objects like cube
mousePosX = coords.getX();
mousePosY = coords.getY();
mouseDeltaX = (mousePosX - mouseOldX);
mouseDeltaY = (mousePosY - mouseOldY);

}
}
}
});
return mousePlaneList.get(index);
}

});
return mousePlaneList.get(index);
}


private Group enableNewFocusPoint(Group node){
private Group enableNewFocusPoint(Group node, int index){

//incase a new object was added to the scene, the mouse behavior will be applied to it
mouseB = new MouseBehaviorImpl1(node, MouseButton.PRIMARY, Rotate.X_AXIS, Rotate.Y_AXIS);
node.addEventHandler(MouseEvent.ANY, mouseB);
mouseBehaviorList.add(new MouseBehaviorImpl1(node, MouseButton.PRIMARY, Rotate.X_AXIS, Rotate.Y_AXIS)) ;
node.addEventHandler(MouseEvent.ANY, mouseBehaviorList.get(mouseBehaviorList.size()-1));

node.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {

if (event.isAltDown() && event.getButton().equals(MouseButton.PRIMARY)) {

if (event.isAltDown() && event.getButton().equals(MouseButton.PRIMARY) ) {
//remove old rotation anchor, so it wont rotate around multiple points
try {
node.removeEventHandler(MouseEvent.ANY, mouseB);
} catch (Exception e) {
}


node.removeEventHandler(MouseEvent.ANY, mouseBehaviorList.get(index));
//set new rotation anchor point
Point3D pt = event.getPickResult().getIntersectedPoint();

mouseB = new MouseBehaviorImpl1(node, MouseButton.PRIMARY, Rotate.X_AXIS, Rotate.Y_AXIS, pt);

node.addEventHandler(MouseEvent.ANY, mouseB);


mouseBehaviorList.set(index, mouseB);

if (debugMode) {
System.out.println("New rotation anchor for " + node.toString() + " was set at \n"+ pt.toString());
}
Expand All @@ -708,4 +688,35 @@ private Group enableNewFocusPoint(Group node){
return node;
}

private void addGeometryToScene(boolean replaceOldScene){

if (replaceOldScene) {
for (int i = (ultraRoot.getChildren().size()-1); i > 1 ; i--) { //remove every node exept the light
ultraRoot.getChildren().remove(i);
}
mouseBehaviorList.clear();
renderedUGXGeometries.clear();
mousePlaneList.clear();
mousePlaneList.add(new Rectangle(800, 800, Color.TRANSPARENT));
Rectangle rect = setupMousePlane(0);
renderedUGXGeometries.add(new Group(createContent()));

ultraRoot.getChildren().add(rect);
ultraRoot.getChildren().add(renderedUGXGeometries.get(0));
}else{

int size = renderedUGXGeometries.size();
mousePlaneList.add(new Rectangle(800, 800, Color.TRANSPARENT));
Rectangle rect = setupMousePlane(size);
Group newG = new Group(createContent());

renderedUGXGeometries.add(newG);

ultraRoot.getChildren().addAll(newG,rect);


}

}

}
6 changes: 3 additions & 3 deletions src/main/java/edu/gcsc/jfx3d/UGXReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public Group xbuildUGX (){
if (highResolution) { //high resolution vertex visualization
Sphere[] sphereArray = new Sphere[ssVertices.length];
for (int j = 0; j < ssVertices.length; j++) {
sphereArray[j] = new Sphere(0.075, 4);
sphereArray[j] = new Sphere(0.03*ugxfile.getLogBiggestDistanceBetweenVertices(), 4);

float x = vertices[ssVertices[j] * 3];
float y = vertices[ssVertices[j] * 3 + 1];
Expand All @@ -272,7 +272,7 @@ public Group xbuildUGX (){
TriangleMesh vertexMesh = new TriangleMesh();
vertexMesh.getTexCoords().addAll(0, 0);

float width = 0.03f;
float width = (float) (0.02f * ugxfile.getLogBiggestDistanceBetweenVertices());

for (int j = 0; j < ssVertices.length; j++) {

Expand Down Expand Up @@ -335,7 +335,7 @@ public Group xbuildUGX (){
TriangleMesh edgesMesh = new TriangleMesh();
edgesMesh.getTexCoords().addAll(0, 0);

float width = 0.02f;
float width = (float) (0.01f * ugxfile.getLogBiggestDistanceBetweenVertices());

PhongMaterial edgeMat = new PhongMaterial(new Color(ugxfile.getSubset_handler().get(0).getSubsets().get(i).getColor()[0],
ugxfile.getSubset_handler().get(0).getSubsets().get(i).getColor()[1],
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/edu/gcsc/jfx3d/UGXfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import java.awt.List;
import java.util.ArrayList;
import javafx.geometry.Point3D;


/**
Expand Down Expand Up @@ -53,6 +54,7 @@ public class UGXfile {
private boolean containsHexahedrons = false;
private boolean containsPrisms = false;
private boolean containsPyramids = false;
public double biggestDistanceBetweenVertices = 0;


private ArrayList<Float> globalVertices = new ArrayList<Float>();
Expand Down Expand Up @@ -428,6 +430,7 @@ public void convertReaderStringToData(){
} catch (Exception e) {
System.out.println("No pyramids were found.");
}
calcBiggestDistance();
}

public float[] getGlobalVerticesArray() {
Expand Down Expand Up @@ -490,6 +493,54 @@ public int getGeometryCounter2D(){
return geometryCounter2D;
}

/**Calculates the biggest distance between two vertices in the geometry
*/
private void calcBiggestDistance(){

if (globalVertices.size() > 10000) {
for (int i = 0; i < globalVertices.size() - 6; i += 6) {
Point3D p1 = new Point3D(globalVertices.get(i), globalVertices.get(i + 1), globalVertices.get(i + 2));

Point3D p2 = new Point3D(globalVertices.get((i + 3)), globalVertices.get((i + 4)), globalVertices.get((i + 5)));

if (p1.distance(p2) > biggestDistanceBetweenVertices) {
biggestDistanceBetweenVertices = p1.distance(p2);
}

}

} else {
Point3D p1;
Point3D p2;

for (int i = 0; i < globalVertices.size()-3; i+=3) {
p1 = new Point3D(globalVertices.get(i), globalVertices.get(i+1), globalVertices.get(i+2));

for (int j = 0; j < globalVertices.size()-3; j+=3) {

if (j != i) {
p2 = new Point3D(globalVertices.get((j)), globalVertices.get((j + 1)), globalVertices.get((j + 2)));

if (p1.distance(p2) > biggestDistanceBetweenVertices) {
biggestDistanceBetweenVertices = p1.distance(p2);
}

}

}
}

}

}

/** Returns the log of the biggest distance between two vertices in the geometry
* @return the log of the biggest distance between two vertices
*/
public double getLogBiggestDistanceBetweenVertices() {
return Math.log(biggestDistanceBetweenVertices);
}



}

0 comments on commit e5b7f98

Please sign in to comment.