Commit 71c1fae6 authored by legoc's avatar legoc
Browse files

Calculate the zero values from reference axes

parent 251204bb
......@@ -69,11 +69,11 @@ import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.paint.Color;
/**
* Menu bar of the Nomad3DEditor.getInstance().
*
* @author dages
*/
public class EditorMenuBar extends MenuBar {
......@@ -261,6 +261,10 @@ public class EditorMenuBar extends MenuBar {
Label yPosField = new Label("-");
Label zPosField = new Label("-");
Label valueText = new Label("Current Value : ");
valueText.setAlignment(Pos.CENTER_LEFT);
valueText.setText(currentValueText(selectedAxis.getType()));
// List of reference axes.
ObservableList<String> references = FXCollections.observableArrayList("");
references.addAll(ReferenceAxisList.getInstance().getNames());
......@@ -317,10 +321,36 @@ public class EditorMenuBar extends MenuBar {
}
});
Button resetButton = new Button("Reset Movement");
resetButton.setOnAction(medianEvent -> {
try {
selectedAxis.resetMovement();
} catch (Exception e) {
// Invalid input, nothing to do
}
});
///////////////////////////////////////////////////////////////////////////////////////
// Reference for first zero value
Label zeroValueReference1Label = new Label("Zero Value A : ");
Label zeroValueReference1StatusLabel = new Label("");
zeroValueReference1Label.setTextFill(selectedAxis.calculateZeroValueReferenceAxis1Status() == Axis.Status.NOK ? Color.RED : Color.BLACK);
CheckBox reversedDirection1Check = new CheckBox("Reversed");
reversedDirection1Check.setSelected(selectedAxis.isZeroValueReferenceAxis1Reversed());
reversedDirection1Check.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
// Set the selected axis with the reversed value.
selectedAxis.setZeroValueReferenceAxis1Reversed(newValue);
// Calculate the zero value if possible.
calculateZeroValueFromReferences(selectedAxis);
// Refresh the zero value.
zeroValueField.setText(String.format("%.3f", selectedAxis.getZeroValue()));
}
});
ComboBox<String> zeroValueReference1Box = new ComboBox<>(references);
zeroValueReference1Label.setVisible(selectedAxis.getType() == Axis.Type.ROTATION || selectedAxis.getType() == Axis.Type.TRANSLATION);
zeroValueReference1Box.setVisible(selectedAxis.getType() == Axis.Type.ROTATION || selectedAxis.getType() == Axis.Type.TRANSLATION);
......@@ -341,7 +371,7 @@ public class EditorMenuBar extends MenuBar {
selectedAxis.setZeroValueReferenceAxis1(referenceAxis);
// Set the status.
zeroValueReference1StatusLabel.setText(convertStatus(selectedAxis.calculateZeroValueReferenceAxis1Status()));
zeroValueReference1Label.setTextFill(selectedAxis.calculateZeroValueReferenceAxis1Status() == Axis.Status.NOK ? Color.RED : Color.BLACK);
// Calculate the zero value if possible.
calculateZeroValueFromReferences(selectedAxis);
......@@ -354,10 +384,28 @@ public class EditorMenuBar extends MenuBar {
///////////////////////////////////////////////////////////////////////////////////////
// Reference for second zero value
Label zeroValueReference2Label = new Label("Zero Value B : ");
Label zeroValueReference2StatusLabel = new Label("");
zeroValueReference2Label.setTextFill(selectedAxis.calculateZeroValueReferenceAxis2Status() == Axis.Status.NOK ? Color.RED : Color.BLACK);
CheckBox reversedDirection2Check = new CheckBox("Reversed");
reversedDirection2Check.setSelected(selectedAxis.isZeroValueReferenceAxis2Reversed());
reversedDirection2Check.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
// Set the selected axis with the reversed value.
selectedAxis.setZeroValueReferenceAxis2Reversed(newValue);
// Calculate the zero value if possible.
calculateZeroValueFromReferences(selectedAxis);
// Refresh the zero value.
zeroValueField.setText(String.format("%.3f", selectedAxis.getZeroValue()));
}
});
ComboBox<String> zeroValueReference2Box = new ComboBox<>(references);
zeroValueReference2Label.setVisible(selectedAxis.getType() == Axis.Type.ROTATION);
zeroValueReference2Box.setVisible(selectedAxis.getType() == Axis.Type.ROTATION);
reversedDirection2Check.setVisible(selectedAxis.getType() == Axis.Type.ROTATION);
// Select the reference axis.
if (selectedAxis.getZeroValueReferenceAxis2() != null) {
......@@ -375,7 +423,7 @@ public class EditorMenuBar extends MenuBar {
selectedAxis.setZeroValueReferenceAxis2(referenceAxis);
// Set the status.
zeroValueReference2StatusLabel.setText(convertStatus(selectedAxis.calculateZeroValueReferenceAxis2Status()));
zeroValueReference2Label.setTextFill(selectedAxis.calculateZeroValueReferenceAxis2Status() == Axis.Status.NOK ? Color.RED : Color.BLACK);
// Calculate the zero value if possible.
calculateZeroValueFromReferences(selectedAxis);
......@@ -402,8 +450,8 @@ public class EditorMenuBar extends MenuBar {
selectedAxis.setRotationDirection(newValue);
// Set the status.
zeroValueReference1StatusLabel.setText(convertStatus(selectedAxis.calculateZeroValueReferenceAxis1Status()));
zeroValueReference2StatusLabel.setText(convertStatus(selectedAxis.calculateZeroValueReferenceAxis2Status()));
zeroValueReference1Label.setTextFill(selectedAxis.calculateZeroValueReferenceAxis1Status() == Axis.Status.NOK ? Color.RED : Color.BLACK);
zeroValueReference2Label.setTextFill(selectedAxis.calculateZeroValueReferenceAxis2Status() == Axis.Status.NOK ? Color.RED : Color.BLACK);
// Calculate the zero value if possible.
calculateZeroValueFromReferences(selectedAxis);
......@@ -438,6 +486,10 @@ public class EditorMenuBar extends MenuBar {
zeroValueReference2Label.setVisible(newValue == Axis.Type.ROTATION);
zeroValueReference2Box.setVisible(newValue == Axis.Type.ROTATION);
reversedDirection2Check.setVisible(newValue == Axis.Type.ROTATION);
// Set the current value label text.
valueText.setText(currentValueText(selectedAxis.getType()));
}
});
......@@ -499,8 +551,8 @@ public class EditorMenuBar extends MenuBar {
selectedAxis.setRotationDirection(Axis.RotationDirection.CCW);
// Set the status.
zeroValueReference1StatusLabel.setText(convertStatus(selectedAxis.calculateZeroValueReferenceAxis1Status()));
zeroValueReference2StatusLabel.setText(convertStatus(selectedAxis.calculateZeroValueReferenceAxis2Status()));
zeroValueReference1Label.setTextFill(selectedAxis.calculateZeroValueReferenceAxis1Status() == Axis.Status.NOK ? Color.RED : Color.BLACK);
zeroValueReference2Label.setTextFill(selectedAxis.calculateZeroValueReferenceAxis2Status() == Axis.Status.NOK ? Color.RED : Color.BLACK);
// Calculate the zero value if possible.
calculateZeroValueFromReferences(selectedAxis);
......@@ -522,23 +574,6 @@ public class EditorMenuBar extends MenuBar {
///////////////////////////////////////////////////////////////////////////////////////
// Current value
Label unitLabel = new Label();
Label zeroUnitLabel = new Label();
switch (selectedAxis.getType()) {
case TRANSLATION:
unitLabel.setText("mm");
zeroUnitLabel.setText("mm");
break;
case ROTATION:
unitLabel.setText("");
zeroUnitLabel.setText("");
break;
default:
break;
}
Label valueText = new Label("Current Value : ");
valueText.setAlignment(Pos.CENTER_LEFT);
TextField valueField = new TextField(Double.toString(selectedAxis.getValue()));
valueField.setAlignment(Pos.CENTER_RIGHT);
valueField.setOnAction(valueEvent -> {
......@@ -610,7 +645,9 @@ public class EditorMenuBar extends MenuBar {
selectedAxis.getMinValue(),
selectedAxis.getMaxValue(),
zeroValueReferenceAxis1Name,
reversedDirection1Check.isSelected(),
zeroValueReferenceAxis2Name,
reversedDirection2Check.isSelected(),
selectedAxis.getZeroValue(),
controllerName));
}
......@@ -663,10 +700,11 @@ public class EditorMenuBar extends MenuBar {
calibrationPane.setHgap(10);
calibrationPane.setVgap(5);
calibrationPane.setAlignment(Pos.TOP_LEFT);
calibrationPane.addRow(0, valueText, valueField, unitLabel, setZeroButton);
calibrationPane.addRow(1, zeroValueText, zeroValueField, zeroUnitLabel, moveToZeroButton);
calibrationPane.addRow(2, zeroValueReference1Label, zeroValueReference1Box, zeroValueReference1StatusLabel);
calibrationPane.addRow(3, zeroValueReference2Label, zeroValueReference2Box, zeroValueReference2StatusLabel);
calibrationPane.addRow(0, valueText, valueField, setZeroButton);
calibrationPane.addRow(1, zeroValueText, zeroValueField);
calibrationPane.addRow(2, zeroValueReference1Label, zeroValueReference1Box, reversedDirection1Check);
calibrationPane.addRow(3, zeroValueReference2Label, zeroValueReference2Box, reversedDirection2Check);
calibrationPane.addRow(4, resetButton, moveToZeroButton);
addPane.addRow(2, calibrationPane);
......@@ -1639,23 +1677,6 @@ public class EditorMenuBar extends MenuBar {
this.getMenus().addAll(this.file, this.edit, this.view, this.selection, this.help);
}
/**
* Convert the axis status into text.
* @param status The status.
* @return The string result.
*/
private String convertStatus(Axis.Status status) {
if (status == Axis.Status.UNDEFINED) {
return "";
}
else if (status == Axis.Status.OK) {
return "OK";
}
else {
return "!OK";
}
}
/**
* Calculates the zero value of an axis from its reference axes.
* @param selectedAxis The axis to calculate the zero value.
......@@ -1667,13 +1688,54 @@ public class EditorMenuBar extends MenuBar {
if (selectedAxis.getType() == Axis.Type.ROTATION && reference1 != null && reference2 != null) {
System.out.println("Calculating new zero value for rotation");
// Get the vector A.
Point3D A = reference1.getDirection();
if (selectedAxis.isZeroValueReferenceAxis1Reversed()) {
A = A.multiply(-1.0);
}
// Get the vector B.
Point3D B = reference2.getDirection();
if (selectedAxis.isZeroValueReferenceAxis2Reversed()) {
B = B.multiply(-1.0);
}
// Get the vector C.
Point3D C = selectedAxis.getDirection();
// Check the relative position of A and B.
Point3D AB = A.crossProduct(B);
// Calculate the angle.
double angle = A.angle(B);
selectedAxis.setZeroValue(reference1.getDirection().angle(reference2.getDirection()));
// Revert if needed.
if (AB.dotProduct(C) < 0) {
angle *= -1.0;
}
// Set the zero value.
selectedAxis.setZeroValue(angle);
}
else if (selectedAxis.getType() == Axis.Type.TRANSLATION && reference1 != null) {
System.out.println("Calculating new zero value for translation");
// Get the vector A.
Point3D A = reference1.getDirection();
// Get the vector C.
Point3D C = selectedAxis.getDirection();
// Calculate the length of the projection of A onto C.
double translation = A.dotProduct(C) / C.magnitude();
if (selectedAxis.isZeroValueReferenceAxis1Reversed()) {
translation *= -1.0;
}
// Set the zero value.
selectedAxis.setZeroValue(translation);
}
else {
// Reset the zero value.
......@@ -1681,6 +1743,26 @@ public class EditorMenuBar extends MenuBar {
}
}
/**
* Returns the text for the current value label.
* @param type The input type.
* @return The string result.
*/
private String currentValueText(Axis.Type type) {
String text = "Current Value ";
if (type == Axis.Type.ROTATION) {
text += "";
}
else if (type == Axis.Type.TRANSLATION) {
text += "mm";
}
text += " :";
return text;
}
/**
* Selects the root component of the selection.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment