1 package com.neuronrobotics.bowlerkernel.Bezier3d;
4 import com.neuronrobotics.bowlerstudio.physics.TransformFactory;
5 import com.neuronrobotics.sdk.addons.kinematics.math.RotationNR;
6 import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
7 import com.neuronrobotics.sdk.common.Log;
8 import javafx.geometry.Point3D;
9 import javafx.application.Platform;
10 import javafx.event.EventHandler;
11 import javafx.event.EventType;
12 import javafx.scene.input.MouseButton;
13 import javafx.scene.input.MouseEvent;
14 import javafx.scene.paint.Color;
15 import javafx.scene.transform.Affine;
16 import javafx.scene.transform.Rotate;
17 import com.google.gson.Gson;
18 import com.google.gson.GsonBuilder;
19 import com.google.gson.reflect.TypeToken;
20 import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.lang.reflect.Type;
27 import java.nio.charset.Charset;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
33 import org.apache.commons.io.FileUtils;
34 import org.apache.commons.io.IOUtils;
35 import org.eclipse.jgit.api.errors.GitAPIException;
36 import org.eclipse.jgit.api.errors.InvalidRemoteException;
37 import org.eclipse.jgit.api.errors.TransportException;
39 import eu.mihosoft.vrl.v3d.*;
40 import javafx.scene.shape.Line;
43 Type TT_mapStringString =
new TypeToken<HashMap<String, HashMap<String, List<Double>>>>() {
45 Gson gson =
new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
58 HashMap<String, HashMap<String, List<Double>>> database;
59 boolean updating =
false;
63 private ArrayList<BezierEditor>
nextBez =
new ArrayList<>();
70 throws InvalidRemoteException, TransportException, GitAPIException, IOException {
78 String jsonString =
null;
79 boolean loaded =
false;
81 if (cachejson.exists()) {
82 InputStream inPut =
null;
83 inPut = FileUtils.openInputStream(cachejson);
84 jsonString = IOUtils.toString(inPut);
85 database = gson.fromJson(jsonString, TT_mapStringString);
87 List<Double> cp1in = (List<Double>) database.get(
"bezier").get(
"control one");
88 List<Double> cp2in = (List<Double>) database.get(
"bezier").get(
"control two");
89 List<Double> ep = (List<Double>) database.get(
"bezier").get(
"end point");
90 List<Double> st = (List<Double>) database.get(
"bezier").get(
"start point");
94 cp1.
setX(cp1in.get(0));
95 cp1.
setY(cp1in.get(1));
96 cp1.
setZ(cp1in.get(2));
97 cp2.
setX(cp2in.get(0));
98 cp2.
setY(cp2in.get(1));
99 cp2.
setZ(cp2in.get(2));
101 strt.
setX(st.get(0));
102 strt.
setY(st.get(1));
103 strt.
setZ(st.get(2));
106 }
catch (Throwable t) {
122 database =
new HashMap<>();
152 ArrayList<CSG> parts =
new ArrayList<>();
153 for (
int i = 0; i < numPoints; i++) {
167 ArrayList<CSG> back =
new ArrayList<>();
169 back.addAll(cp1Manip.
get());
170 back.addAll(cp2Manip.
get());
176 public ArrayList<Object>
get() {
178 ArrayList<Object> back =
new ArrayList<>();
195 Platform.runLater(() -> {
208 double cp1XDiff = m.
getX() - p.
getX();
209 double cp1Ydiff = m.
getY() - p.
getY();
210 double cp1ZDiff = m.
getZ() - p.
getZ();
211 double distCP1 = Math.sqrt(Math.pow(cp1XDiff, 2) + Math.pow(cp1Ydiff, 2) + Math.pow(cp1ZDiff, 2));
213 double newX = vect.
getX() + p.
getX();
214 double newY = vect.
getY() + p.
getY();
215 double newZ = vect.
getZ() + p.
getZ();
216 m.
set(newX, newY, newZ);
224 double cp1XDiff = m.
getX() - p.
getX();
225 double cp1Ydiff = m.
getY() - p.
getY();
226 double cp1ZDiff = m.
getZ() - p.
getZ();
229 double distCP1 = Math.sqrt(Math.pow(cp1XDiff, 2) + Math.pow(cp1Ydiff, 2) + Math.pow(cp1ZDiff, 2));
230 double xyRot = Math.toDegrees(Math.atan2(cp1Ydiff, cp1XDiff)) - 90;
236 double xzRot = Math.toDegrees(Math.atan2(reorented.
getZ(), reorented.
getY()));
240 Platform.runLater(() -> {
251 public void setEnd(
double newX,
double newY,
double newZ) {
256 public void setStart(
double newX,
double newY,
double newZ) {
262 public void setCP1(
double newX,
double newY,
double newZ) {
263 cp1Manip.
set(newX, newY, newZ);
268 public void setCP2(
double newX,
double newY,
double newZ) {
269 cp2Manip.
set(newX, newY, newZ);
300 for (
int i = 0; i < tf.size(); i++) {
316 HashMap<String, List<Double>> bezData =
new HashMap<>();
318 bezData.put(
"control one", Arrays.asList(cp1Manip.
getX(), cp1Manip.
getY(), cp1Manip.
getZ()));
319 bezData.put(
"control two", Arrays.asList(cp2Manip.
getX(), cp2Manip.
getY(), cp2Manip.
getZ()));
321 bezData.put(
"start point",
323 bezData.put(
"number of points", Arrays.asList((
double)
getNumParts()));
324 database.put(
"bezier", bezData);
327 System.out.println(
"Saving to file " + cachejson.getAbsolutePath());
328 String writeOut = gson.toJson(database, TT_mapStringString);
333 }
catch (Exception e) {
338 if (!cachejson.exists())
340 cachejson.createNewFile();
341 } catch (IOException e) {
345 OutputStream out =
null;
347 out = FileUtils.openOutputStream(cachejson,
false);
348 IOUtils.write(writeOut, out);
352 }
catch (IOException e) {
356 IOUtils.closeQuietly(out);
377 if (this.start !=
null)
387 cp1Line.getTransforms().clear();
CartesianManipulator getEndManip()
TransformNR updateLines(CartesianManipulator m, CartesianManipulator p, Line l, Affine poseAF)
ArrayList< Transform > transforms()
BezierEditor(String URL, String file, int numPoints)
void setEnd(Vector3d point)
void setStart(Vector3d point)
CartesianManipulator start
void setStartManip(CartesianManipulator start)
void setCP2(double newX, double newY, double newZ)
ArrayList< BezierEditor > nextBez
ArrayList< CSG > getCSG()
void addBezierToTheEnd(BezierEditor b)
ArrayList< CSG > getPartsInternal()
CartesianManipulator getStartManip()
void setCP2(Vector3d point)
void setEnd(double newX, double newY, double newZ)
CartesianManipulator endManip
void setCP1(Vector3d point)
void setPartsInternal(ArrayList< CSG > partsInternal)
void setStart(double newX, double newY, double newZ)
BezierEditor(File data, int numPoints)
ArrayList< CSG > partsInternal
void setCP1(double newX, double newY, double newZ)
void addEventListener(Runnable r)
void addSaveListener(Runnable r)
void addDependant(CartesianManipulator r)
Affine manipulationMatrix
void set(double newX, double newY, double newZ)
static void pushCodeToGit(String id, String branch, String FileName, String content, String commitMessage)
static String getFullBranch(String remoteURI)
static File fileFromGit(String remoteURI, String fileInRepo)
CSG roty(Number degreesToRotate)
static ArrayList< Transform > bezierToTransforms(Vector3d controlA, Vector3d controlB, Vector3d endPoint, int iterations)
static Vector3d y(double y)
static Vector3d z(double z)
static Vector3d x(double x)