implement compute star system coordinates
This commit is contained in:
@@ -20,6 +20,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.trader.model.MarketModel;
|
||||
import ru.trader.model.SystemModel;
|
||||
import ru.trader.model.support.PositionComputer;
|
||||
import ru.trader.view.support.Localization;
|
||||
import ru.trader.view.support.cells.TextFieldCell;
|
||||
|
||||
@@ -83,6 +84,13 @@ public class SystemsEditorController {
|
||||
clnS6.setCellFactory(TextFieldCell.forTableColumn(new DoubleStringConverter()));
|
||||
tblSystems.setItems(FXCollections.observableArrayList());
|
||||
tblSystems.getSelectionModel().setCellSelectionEnabled(true);
|
||||
tblSystems.setSortPolicy(t -> true);
|
||||
system1.valueProperty().addListener((ov, o, n) -> clnS1.setText(n != null ? n.getName() : ""));
|
||||
system2.valueProperty().addListener((ov, o, n) -> clnS2.setText(n != null ? n.getName() : ""));
|
||||
system3.valueProperty().addListener((ov, o, n) -> clnS3.setText(n != null ? n.getName() : ""));
|
||||
system4.valueProperty().addListener((ov, o, n) -> clnS4.setText(n != null ? n.getName() : ""));
|
||||
system5.valueProperty().addListener((ov, o, n) -> clnS5.setText(n != null ? n.getName() : ""));
|
||||
system6.valueProperty().addListener((ov, o, n) -> clnS6.setText(n != null ? n.getName() : ""));
|
||||
init();
|
||||
}
|
||||
|
||||
@@ -111,10 +119,33 @@ public class SystemsEditorController {
|
||||
reset();
|
||||
}
|
||||
|
||||
public void add() {
|
||||
public void add(){
|
||||
tblSystems.getItems().add(new SystemData());
|
||||
}
|
||||
|
||||
public void compute(){
|
||||
SystemModel sys1 = system1.getValue();
|
||||
SystemModel sys2 = system2.getValue();
|
||||
SystemModel sys3 = system3.getValue();
|
||||
SystemModel sys4 = system4.getValue();
|
||||
SystemModel sys5 = system5.getValue();
|
||||
SystemModel sys6 = system6.getValue();
|
||||
|
||||
for (SystemData systemData : tblSystems.getItems()) {
|
||||
if (systemData.name.isEmpty().get()) continue;
|
||||
PositionComputer computer = new PositionComputer();
|
||||
if (sys1 != null) computer.addLandMark(sys1, systemData.s1.get());
|
||||
if (sys2 != null) computer.addLandMark(sys2, systemData.s2.get());
|
||||
if (sys3 != null) computer.addLandMark(sys3, systemData.s3.get());
|
||||
if (sys4 != null) computer.addLandMark(sys4, systemData.s4.get());
|
||||
if (sys5 != null) computer.addLandMark(sys5, systemData.s5.get());
|
||||
if (sys6 != null) computer.addLandMark(sys6, systemData.s6.get());
|
||||
PositionComputer.Coordinates coord = computer.compute();
|
||||
systemData.x.set(coord.getX());
|
||||
systemData.y.set(coord.getY());
|
||||
systemData.z.set(coord.getZ());
|
||||
}
|
||||
}
|
||||
|
||||
private void commit(){
|
||||
for (SystemData systemData : tblSystems.getItems()) {
|
||||
@@ -196,8 +227,12 @@ public class SystemsEditorController {
|
||||
return s6;
|
||||
}
|
||||
|
||||
public boolean isEmpty(){
|
||||
return name.get().isEmpty() || Double.isNaN(x.get()) || Double.isNaN(y.get()) || Double.isNaN(z.get());
|
||||
}
|
||||
|
||||
private void commit(){
|
||||
if (!name.get().isEmpty() && !Double.isNaN(x.get()) && !Double.isNaN(y.get()) && !Double.isNaN(z.get())){
|
||||
if (!isEmpty()){
|
||||
if (system != null){
|
||||
system.setName(name.get());
|
||||
system.setPosition(x.get(), y.get(), z.get());
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package ru.trader.model.support;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.trader.model.SystemModel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PositionComputer {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(PositionComputer.class);
|
||||
|
||||
private final static double MAX_ERROR = 1E-10;
|
||||
private final static double MAX_VALUE = 1000;
|
||||
private final static double MIN_VALUE = -1000;
|
||||
|
||||
private final List<SystemModel> marks = new ArrayList<>(6);
|
||||
private final List<Double> distances = new ArrayList<>(6);
|
||||
|
||||
public PositionComputer() {
|
||||
}
|
||||
|
||||
public void addLandMark(SystemModel system, double distance){
|
||||
marks.add(system);
|
||||
distances.add(distance);
|
||||
}
|
||||
|
||||
|
||||
public Coordinates compute(){
|
||||
return compute(MIN_VALUE, MIN_VALUE, MIN_VALUE);
|
||||
}
|
||||
|
||||
public Coordinates compute(final double x, final double y, final double z){
|
||||
Coordinates res = new Coordinates(Double.NaN, Double.NaN, Double.NaN);
|
||||
Coordinates current = new Coordinates(x, y, z);
|
||||
double xmin = MIN_VALUE, xmax = MAX_VALUE;
|
||||
double ymin = MIN_VALUE, ymax = MAX_VALUE;
|
||||
double zmin = MIN_VALUE, zmax = MAX_VALUE;
|
||||
double step=100, mineps = Double.NaN;
|
||||
|
||||
while (true){
|
||||
double delta = 0;
|
||||
for (int i = 0; i < marks.size(); i++) {
|
||||
delta += Math.abs(marks.get(i).getDistance(current.x, current.y, current.z) - distances.get(i));
|
||||
}
|
||||
LOG.trace("coordinates = {}, delta = {}", current, delta);
|
||||
if (delta < MAX_ERROR) {
|
||||
res = current;
|
||||
LOG.debug("Coordinates found {}", res);
|
||||
break;
|
||||
}
|
||||
if (Double.isNaN(mineps) || delta < mineps){
|
||||
mineps = delta;
|
||||
res = new Coordinates(current);
|
||||
}
|
||||
|
||||
current.x += step;
|
||||
if (current.x >= xmax){
|
||||
current.x = xmin;
|
||||
current.y += step;
|
||||
if (current.y >= ymax){
|
||||
current.y = ymin;
|
||||
current.z += step;
|
||||
if (current.z >= zmax){
|
||||
if (step < MAX_ERROR){
|
||||
LOG.debug("Coordinates not found, last {}, delta {}", res, mineps);
|
||||
break;
|
||||
} else {
|
||||
xmin = res.x - step*2; xmax = res.x + step*2; current.x = xmin;
|
||||
ymin = res.y - step*2; ymax = res.y + step*2; current.y = ymin;
|
||||
zmin = res.z - step*2; zmax = res.z + step*2; current.z = zmin;
|
||||
step = step/2;
|
||||
LOG.trace("Change step to {}", step);
|
||||
mineps = Double.NaN;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
public class Coordinates {
|
||||
private double x;
|
||||
private double y;
|
||||
private double z;
|
||||
|
||||
public Coordinates(double x, double y, double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public Coordinates(Coordinates other) {
|
||||
this.x = other.x;
|
||||
this.y = other.y;
|
||||
this.z = other.z;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("{");
|
||||
sb.append("x=").append(x);
|
||||
sb.append(", y=").append(y);
|
||||
sb.append(", z=").append(z);
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,16 @@
|
||||
<ComboBox fx:id="system5" prefWidth="160" GridPane.rowIndex="1" GridPane.columnIndex="2" />
|
||||
<ComboBox fx:id="system6" prefWidth="160" GridPane.rowIndex="1" GridPane.columnIndex="3" />
|
||||
|
||||
<TableView fx:id="tblSystems" editable="true" prefWidth="725.0" GridPane.rowIndex="2" GridPane.columnSpan="4">
|
||||
<HBox GridPane.rowIndex="2" spacing="4">
|
||||
<Button prefWidth="30" onAction="#add">
|
||||
<graphic><Glyph text="FontAwesome|PLUS"/></graphic>
|
||||
</Button>
|
||||
<Button prefWidth="30" onAction="#compute">
|
||||
<graphic><Glyph text="FontAwesome|CROSSHAIRS"/></graphic>
|
||||
</Button>
|
||||
</HBox>
|
||||
|
||||
<TableView fx:id="tblSystems" editable="true" prefWidth="725.0" GridPane.rowIndex="3" GridPane.columnSpan="4">
|
||||
<columns>
|
||||
<TableColumn fx:id="clnName" minWidth="180.0" text="%market.systems">
|
||||
<cellValueFactory><PropertyValueFactory property="name"/></cellValueFactory>
|
||||
|
||||
Reference in New Issue
Block a user