diff --git a/misc/octave-core b/misc/octave-core deleted file mode 100644 index 453a3d15..00000000 Binary files a/misc/octave-core and /dev/null differ diff --git a/misc/perp b/misc/perp deleted file mode 100644 index 9d4b48f4..00000000 --- a/misc/perp +++ /dev/null @@ -1,27 +0,0 @@ -pkg load optim; - -function y = f (x) - - y = [ - ( - (x(3) - x(1)) * (x(7) - x(5)) + - (x(4) - x(2)) * (x(8) - x(6)) - ) ^ 2, - ( - (x(11) - x(9)) * (x(7) - x(5)) + - (x(12) - x(10)) * (x(8) - x(6)) - ) ^ 2]; - y(3) = 0;y(4) = 0;y(5) = 0;y(6) = 0;y(7) = 0;y(8) = 0; - y(9) = 0;y(10) = 0;y(11) = 0;y(12) = 0; -endfunction - -x0 = [100, 100, 600, 600, 700, 600, 900, 100, 1100, 100, 1600, 600]; -#x = fminunc(@f, reshape(x0, 12,1)); -x = fsolve(@f, x0); #WORKS! - -#x = x0; -l1 = [x(1), x(2); x(3), x(4)]; -l2 = [x(5), x(6); x(7), x(8)]; -l3 = [x(9), x(10); x(11), x(12)]; - -plot(l1(:,1), l1(:,2), l2(:,1), l2(:,2), l3(:,1), l3(:,2),'-'); diff --git a/misc/proto.m b/misc/proto.m deleted file mode 100644 index 0bcc6080..00000000 --- a/misc/proto.m +++ /dev/null @@ -1,72 +0,0 @@ -pkg load optim; - -l1p1x = 1; -l1p1y = 2; -l1p2x = 3; -l1p2y = 4; -l2p1x = 5; -l2p1y = 6; -l2p2x = 7; -l2p2y = 8; - -function out = g (x) - -l1p1x = 1; -l1p1y = 2; -l1p2x = 3; -l1p2y = 4; -l2p1x = 5; -l2p1y = 6; -l2p2x = 7; -l2p2y = 8; - p = x{1}; - out = [ - (p(l2p1y) - p(l2p2y)); - -(p(l2p1y) - p(l2p2y)); - (p(l2p1x) - p(l2p2x)); - -(p(l2p1x) - p(l2p2x)); - (p(l1p1y) - p(l1p2y)); - -(p(l1p1y) - p(l1p2y)); - (p(l1p1x) - p(l1p2x)); - -(p(l1p1x) - p(l1p2x)); - ]; - -endfunction - -function out = phi (x) - -l1p1x = 1; -l1p1y = 2; -l1p2x = 3; -l1p2y = 4; -l2p1x = 5; -l2p1y = 6; -l2p2x = 7; -l2p2y = 8; - p = x{1}; - dx1 = (p(l1p1x) - p(l1p2x)); - dy1 = (p(l1p1y) - p(l1p2y)); - dx2 = (p(l2p1x) - p(l2p2x)); - dy2 = (p(l2p1y) - p(l2p2y)); - #dot product shows how the lines off to be perpendicular - off = dx1 * dx2 + dy1 * dy2; - out = off * off; -endfunction - - - -l1 = [100, 100; 300, 600]; -l2 = [400, 600; 600, 100]; - - -x0 = [l1(1,1);l1(1,2);l1(2,1);l1(2,2); l2(1,1);l2(1,2);l2(2,1);l2(2,2)]; - -#[x, obj, info, iter, nf, lambda] = sqp (x0, @phi, @g, []); - -[a,b,c] = cg_min (@phi, @g, x0); - - -#plot([l1(1), l1(2)], [l1(3), l1(4)], '-'); -#plot(reshape(l2, 2, 2)); -plot(l1(:,1), l1(:,2), l2(:,1), l2(:,2), '-'); - diff --git a/misc/simple.m b/misc/simple.m deleted file mode 100644 index 7c449e13..00000000 --- a/misc/simple.m +++ /dev/null @@ -1,31 +0,0 @@ -function y = angle (x) - -a = [x(3) - x(1); x(4) - x(2)]; -b = [x(7) - x(5); x(8) - x(6)]; - -y = acos(dot(a, b) / (norm(a) * norm(b))) / pi * 180; - -endfunction - -function y = f (x) - y(1) = ( - (x(3) - x(1)) * (x(7) - x(5)) + - (x(4) - x(2)) * (x(8) - x(6)) - ) ^ 2; -endfunction - -x0 = [100, 100, 600, 600, 700, 600, 900, 100]; -x = sqp(x0, @f); - -#pkg load optim; -#x = bfgsmin('f', {reshape(x0, 8,1)}); #WORKS! - - -l1 = [x(1), x(2); x(3), x(4)]; -l2 = [x(5), x(6); x(7), x(8)]; - -plot(l1(:,1), l1(:,2), l2(:,1), l2(:,2), '-'); - - -#d = (x(3) - x(1)) * (x(7) - x(5)) + (x(4) - x(2)) * (x(8) - x(6)) ; -disp("Angle: "), disp(angle(x)); diff --git a/oce-build.txt b/oce-build.txt deleted file mode 100644 index 0ed71d49..00000000 --- a/oce-build.txt +++ /dev/null @@ -1,22 +0,0 @@ -cd build_oce/ -cmake -D OCE_VISUALISATION:BOOLEAN=FALSE -D OCE_DISABLE_X11:BOOLEAN=TRUE -D OCE_USE_PCH:BOOLEAN=TRUE ../oce -make -j 4 - -export DESTDIR=/home/xibyte/git/build_oce/out && make -j4 install - -export LD_LIBRARY_PATH=/home/xibyte/git/build_oce/out/usr/local/lib/ - -official/6.7.0 - - 1071 sudo apt-get install g++ - 1073 sudo apt-get install tcl - 1074 sudo apt-get install tclsudo - 1075 sudo apt-get install cmake cmake-curses-gui g++ build-essential - 1078 sudo apt-get install tcl-dev - 1084 sudo apt-get install automake - 1090 sudo apt-get install libtool - - 1085 ./build_configure - 1092 ./configure - 1093 make - diff --git a/src/cad/SolveServer.java b/src/cad/SolveServer.java deleted file mode 100644 index 668936d4..00000000 --- a/src/cad/SolveServer.java +++ /dev/null @@ -1,160 +0,0 @@ -package cad; - -import cad.gcs.Constraint; -import cad.gcs.GlobalSolver; -import cad.gcs.Param; -import cad.gcs.Solver; -import cad.gcs.constr.Equal; -import cad.gcs.constr.EqualsTo; -import cad.gcs.constr.P2LDistance; -import cad.gcs.constr.P2PDistance; -import cad.gcs.constr.Parallel; -import cad.gcs.constr.Perpendicular; -import org.eclipse.jetty.server.Request; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.handler.AbstractHandler; -import org.eclipse.jetty.server.handler.HandlerList; -import org.eclipse.jetty.server.handler.ResourceHandler; -import org.json.JSONArray; -import org.json.JSONObject; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.BufferedReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class SolveServer { - - public static void main(String[] args) throws Exception { - Server server = new Server(8080); - - HandlerList handlers = new HandlerList(); - handlers.addHandler(new SolveHandler()); - - ResourceHandler rh = new ResourceHandler(); - rh.setDirectoriesListed(true); - rh.setResourceBase("/home/xibyte/Dropbox/project/cadit/web"); - handlers.addHandler(rh); - - server.setHandler(handlers); - server.start(); -// server.dumpStdErr(); - server.join(); - } -} - -class SolveHandler extends AbstractHandler { - - volatile boolean busy = false; - - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - - if (!request.getRequestURI().startsWith("/solve")) { - return; - } - - baseRequest.setHandled(true); - - if (busy) { - response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); - return; - } - synchronized (this) { - if (busy) return; - busy = true; - try { - BufferedReader reader = request.getReader(); - String jsonStr = new Scanner(reader).useDelimiter("\\A").next(); - System.out.println("REQUEST: " + jsonStr); - - JSONObject json = new JSONObject(jsonStr); - - JSONObject solved = solve(json); - System.out.println("RESPONSE: " + solved); - - response.setContentType("application/json;charset=utf-8"); - response.setStatus(HttpServletResponse.SC_OK); - - response.getWriter().println(solved.toString()); - } finally { - busy = false; - } - } - } - - private JSONObject solve(JSONObject req) { - List constraints = new ArrayList<>(); - JSONObject system = req.getJSONObject("system"); - JSONArray params = system.getJSONArray("params"); - JSONArray constrs = system.getJSONArray("constraints"); - JSONArray locked = system.getJSONArray("locked"); - -// TIntObjectMap paramsDict = new TIntObjectHashMap(); - List paramsDict = new ArrayList<>(params.length()); - for (int i = 0; i < params.length(); i++) { - paramsDict.add(null); - } - class ParamHelper { - private JSONArray refs; - public Param get(int pos) { - int ref = refs.getInt(pos); - Param param = paramsDict.get(ref); - if (param == null) { - param = new Param(params.getDouble(ref)); - paramsDict.set(ref, param); - } - return param; - } - } - - ParamHelper h = new ParamHelper(); - - for (int i = 0; i < constrs.length(); i++) { - JSONArray constr = constrs.getJSONArray(i); - String functional = constr.getString(0); - h.refs = constr.getJSONArray(1); - JSONArray constants = constr.getJSONArray(2); - - switch (functional) { - case "equal": - constraints.add(new Equal(h.get(0), h.get(1))); - break; - case "perpendicular": - constraints.add(new Perpendicular(h.get(0), h.get(1), h.get(2), h.get(3), h.get(4), h.get(5), h.get(6), h.get(7))); - break; - case "parallel": - constraints.add(new Parallel(h.get(0), h.get(1), h.get(2), h.get(3), h.get(4), h.get(5), h.get(6), h.get(7))); - break; - case "P2LDistance": - constraints.add(new P2LDistance(constants.getDouble(0), h.get(0), h.get(1), h.get(2), h.get(3), h.get(4), h.get(5))); - break; - case "P2PDistance": - constraints.add(new P2PDistance(h.get(0), h.get(1), h.get(2), h.get(3), constants.getDouble(0))); - break; - } - } - - for (int i = 0; i < locked.length(); i++) { - Param param = paramsDict.get(locked.getInt(i)); -// param.setLocked(true); - constraints.add(new EqualsTo(param, param.get())); - } - - Solver.SubSystem subSystem = new Solver.SubSystem(constraints); - GlobalSolver.globalSolve(subSystem, () -> {}); - - - JSONObject response = new JSONObject(); - response.put("reqId", req.getInt("reqId")); - JSONArray paramsJson = new JSONArray(); - response.put("params", paramsJson); - for (Param param : paramsDict) { - paramsJson.put(param.get()); - } - return response; - } -} \ No newline at end of file diff --git a/src/cad/fx/App.java b/src/cad/fx/App.java deleted file mode 100644 index e9827077..00000000 --- a/src/cad/fx/App.java +++ /dev/null @@ -1,22 +0,0 @@ -package cad.fx; - -import javafx.application.Application; -import javafx.fxml.FXMLLoader; -import javafx.scene.Scene; -import javafx.stage.Stage; - -public class App extends Application { - - public static void main(String[] args) { - System.setProperty("prism.dirtyopts", "false"); - launch(args); - } - - @Override - public void start(Stage primaryStage) throws Exception { - Scene scene = new Scene(FXMLLoader.load(AppCtrl.class.getResource("app.fxml")), 1024, 1100); - primaryStage.setTitle("Solid CAD"); - primaryStage.setScene(scene); - primaryStage.show(); - } -} diff --git a/src/cad/fx/App2D.java b/src/cad/fx/App2D.java deleted file mode 100644 index 229fd0e2..00000000 --- a/src/cad/fx/App2D.java +++ /dev/null @@ -1,22 +0,0 @@ -package cad.fx; - -import javafx.application.Application; -import javafx.fxml.FXMLLoader; -import javafx.scene.Scene; -import javafx.stage.Stage; - -public class App2D extends Application { - - public static void main(String[] args) { - System.setProperty("prism.dirtyopts", "false"); - launch(args); - } - - @Override - public void start(Stage primaryStage) throws Exception { - Scene scene = new Scene(FXMLLoader.load(AppCtrl.class.getResource("app2d.fxml")), 1024, 1100); - primaryStage.setTitle("Sketch"); - primaryStage.setScene(scene); - primaryStage.show(); - } -} diff --git a/src/cad/fx/App2DCtrl.java b/src/cad/fx/App2DCtrl.java deleted file mode 100644 index 0ffa4884..00000000 --- a/src/cad/fx/App2DCtrl.java +++ /dev/null @@ -1,328 +0,0 @@ -package cad.fx; - -import cad.gcs.Constraint; -import cad.gcs.Figures; -import cad.gcs.GlobalSolver; -import cad.gcs.Param; -import cad.gcs.Solver; -import cad.gcs.constr.P2LDistance; -import cad.gcs.constr.Parallel; -import cad.gcs.constr.Perpendicular; -import cad.gcs.constr.Reconcilable; -import cad.math.Vector; -import gnu.trove.list.TDoubleList; -import javafx.application.Platform; -import javafx.event.ActionEvent; -import javafx.fxml.Initializable; -import javafx.scene.Group; -import javafx.scene.control.Button; -import javafx.scene.layout.Pane; -import javafx.scene.shape.Line; -import org.apache.commons.math3.analysis.MultivariateFunction; -import org.apache.commons.math3.exception.MathIllegalStateException; -import org.apache.commons.math3.optim.ConvergenceChecker; -import org.apache.commons.math3.optim.InitialGuess; -import org.apache.commons.math3.optim.MaxEval; -import org.apache.commons.math3.optim.MaxIter; -import org.apache.commons.math3.optim.PointValuePair; -import org.apache.commons.math3.optim.PointVectorValuePair; -import org.apache.commons.math3.optim.SimpleBounds; -import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; -import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction; -import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunctionGradient; -import org.apache.commons.math3.optim.nonlinear.scalar.gradient.NonLinearConjugateGradientOptimizer; -import org.apache.commons.math3.optim.nonlinear.vector.ModelFunction; -import org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian; -import org.apache.commons.math3.optim.nonlinear.vector.Target; -import org.apache.commons.math3.optim.nonlinear.vector.Weight; -import org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer; - -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.ResourceBundle; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import static java.util.Arrays.asList; -import static org.apache.commons.math3.optim.nonlinear.scalar.gradient.NonLinearConjugateGradientOptimizer.Formula.FLETCHER_REEVES; - -public class App2DCtrl implements Initializable { - - private final CadContext cadContext = new CadContext(); - - public Pane viewer; - public Button solve; - public Button square; - private Group content; - - @Override - public void initialize(URL location, ResourceBundle resources) { - content = new Group(); - setInitObject(content); - viewer.getChildren().setAll(content); - - - Line l1 = new Line(100, 100, 300, 600); - Line l2 = new Line(400, 600, 600, 100); - Line l3 = new Line(650, 100, 800, 600); - - content.getChildren().addAll(l1, l2, l3); - - square.setOnAction(event -> { - solveFigure(Figures.square(100)); - }); - - - solve.setOnAction(event -> { - - Vector as = new Vector(l1.getStartX(), l1.getStartY()); - Vector ae = new Vector(l1.getEndX(), l1.getEndY()); - Vector bs = new Vector(l2.getStartX(), l2.getStartY()); - Vector be = new Vector(l2.getEndX(), l2.getEndY()); - - - Param l1p1x = new Param(l1.getStartX()); - Param l1p1y = new Param(l1.getStartY()); - Param l1p2x = new Param(l1.getEndX()); - Param l1p2y = new Param(l1.getEndY()); - - Param l2p1x = new Param(l2.getStartX()); - Param l2p1y = new Param(l2.getStartY()); - Param l2p2x = new Param(l2.getEndX()); - Param l2p2y = new Param(l2.getEndY()); - - Param l3p1x = new Param(l3.getStartX()); - Param l3p1y = new Param(l3.getStartY()); - Param l3p2x = new Param(l3.getEndX()); - Param l3p2y = new Param(l3.getEndY()); - -// l2p2x.setLocked(true); -// l2p2y.setLocked(true); -// l2p1x.setLocked(true); -// l2p1y.setLocked(true); - - - Perpendicular perpendicular = new Perpendicular( - l1p1x, - l1p1y, - l1p2x, - l1p2y, - l2p1x, - l2p1y, - l2p2x, - l2p2y - ); - - Parallel parallel = new Parallel( - l3p1x, - l3p1y, - l3p2x, - l3p2y, - l2p1x, - l2p1y, - l2p2x, - l2p2y - ); - - P2LDistance p2l1 = new P2LDistance( - 10, - l3p1x, l3p1y, - l2p1x, l2p1y, - l2p2x, l2p2y - ); - - P2LDistance p2l2 = new P2LDistance( - 10, - l1p2x, l1p2y, - l2p1x, l2p1y, - l2p2x, l2p2y - ); - - - Runnable update = () -> { - System.out.println("ANGLE |- : " + perpendicular.angle()); - System.out.println("ANGLE || : " + parallel.angle()); - System.out.println("DISTANCE : " + p2l1.error()); - -// Constraint2 constr = xy; - -// Constraint constr = perpendicular; -// GradientDescent.solve(constr); -// perpendicular.out(a1, b1, a2, b2); - -// GradientDescent2.solve(constr); - - -// l1.setStartX(as.x); -// l1.setStartY(as.y); -// l1.setEndX(ae.x); -// l1.setEndY(ae.y); -// -// l2.setStartX(bs.x); -// l2.setStartY(bs.y); -// l2.setEndX(be.x); -// l2.setEndY(be.y); - - l1.setStartX(l1p1x.get()); - l1.setStartY(l1p1y.get()); - l1.setEndX(l1p2x.get()); - l1.setEndY(l1p2y.get()); - - l2.setStartX(l2p1x.get()); - l2.setStartY(l2p1y.get()); - l2.setEndX(l2p2x.get()); - l2.setEndY(l2p2y.get()); - - l3.setStartX(l3p1x.get()); - l3.setStartY(l3p1y.get()); - l3.setEndX(l3p2x.get()); - l3.setEndY(l3p2y.get()); - -// scale(l1); -// scale(l2); -// scale(l3); - }; - - - List constrs = Arrays.asList(p2l2, parallel, perpendicular, p2l1); -// List constrs = Arrays.asList(p2l1); - Solver.SubSystem subSystem = new Solver.SubSystem(constrs); -// Solver.optimize(subSystem); -// - - ExecutorService executor = Executors.newSingleThreadExecutor(); - executor.execute(() -> { - GlobalSolver.globalSolve(subSystem, () -> Platform.runLater(update)); - if (true) return; - while (subSystem.errorSquared() > 0.0001) { -// Solver.solve_LM(subSystem); - GlobalSolver.solveLM_COMMONS(subSystem); -// Solver.solve_DL(subSystem); -// Solver.solve_BFGS(subSystem, true); - Platform.runLater(update); - } - }); - }); - } - - private void solveFigure(Figures.Figure square) { - - Solver.SubSystem subSystem = new Solver.SubSystem(square.constraints); - - List lines = new ArrayList<>(); - - for (Param[] line : square.lines) { - Line fxLine = new Line(); - fxLine.setStartX(line[0].get()); - fxLine.setStartY(line[1].get()); - fxLine.setEndX(line[2].get()); - fxLine.setEndY(line[3].get()); - - lines.add(fxLine); - } - - content.getChildren().addAll(lines); - - ExecutorService executor = Executors.newSingleThreadExecutor(); - executor.execute(() -> { - GlobalSolver.globalSolve(subSystem, () -> Platform.runLater(() -> { - for (int i = 0; i < square.lines.length; i++) { - Param[] line = square.lines[i]; - Line fxLine = lines.get(i); - fxLine.setStartX(line[0].get()); - fxLine.setStartY(line[1].get()); - fxLine.setEndX(line[2].get()); - fxLine.setEndY(line[3].get()); - } - })); - }); - } - - - private void solveWorse(Solver.SubSystem subSystem, double eps) { - TDoubleList residuals = subSystem.calcResidual(); - double worseValue = residuals.max(); - if (Math.abs(worseValue) > eps) { - int worseId = residuals.indexOf(worseValue); - Constraint worseConstr = subSystem.constraints.get(worseId); - if (worseConstr instanceof Reconcilable) { - ((Reconcilable) worseConstr).reconcile(); - } else { - Solver.SubSystem worse = new Solver.SubSystem(asList(worseConstr)); - GlobalSolver.solveLM_COMMONS(worse); -// Solver.solve_LM(worse); - - } - - System.out.println("WORSE FIXED ERROR:" + worseConstr.error()); - } - } - - double xxx = 100; - - private void scale(Line l) { - - Vector v = new Vector(l.getEndX() - l.getStartX(), l.getEndY() - l.getStartY()); - v = v.normalize().multi(200); - l.setStartX(xxx += 100.); - l.setStartY(500.); - - l.setEndX(l.getStartX() + v.x); - l.setEndY(l.getStartY() + v.y); - } - - private void solveScalarFunc(final Solver.SubSystem subSystem) { - double eps = 1e-10; - ConvergenceChecker convergenceChecker = new ConvergenceChecker() { - @Override - public boolean converged(int iteration, PointValuePair previous, PointValuePair current) { - return previous.getValue() < eps; - } - }; - NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(FLETCHER_REEVES, convergenceChecker); - double[] lb = new double[subSystem.pSize()]; - double[] ub = new double[subSystem.pSize()]; - - Arrays.fill(lb, -1000); - Arrays.fill(ub, 1000); - - optimizer.optimize( - new MaxEval(10000), - new InitialGuess(subSystem.getParams().toArray()), - GoalType.MINIMIZE, - new SimpleBounds(lb, ub), -// new NonLinearConjugateGradientOptimizer.BracketingStep( 100 ), - getGradient(subSystem), - getScalarFunction(subSystem)); - } - - private ObjectiveFunction getScalarFunction(Solver.SubSystem system) { - return new ObjectiveFunction(point -> { - system.setParams(point); - return system.value(); - }); - } - - private ObjectiveFunctionGradient getGradient(Solver.SubSystem subSystem) { - return new ObjectiveFunctionGradient(point -> { - subSystem.setParams(point); - Constraint constraint = subSystem.constraints.get(0); - double[] out = new double[constraint.pSize()]; - constraint.gradient(out); - return out; - }); - } - - private void solve(ActionEvent e) { - -// UnconstrainedLeastSquares opt = FactoryOptimization.leastSquaresTrustRegion(100, RegionStepType.DOG_LEG_FTF, false); - - } - - private void setInitObject(Group parent) { -// CSG init = new Cube(100).toCSG().difference(new Cylinder(30, 100, 10).toCSG()); -// return new CSGNode(Utils3D.getFXMesh(init), cadContext); - } -} diff --git a/src/cad/fx/AppCtrl.java b/src/cad/fx/AppCtrl.java deleted file mode 100644 index e1218179..00000000 --- a/src/cad/fx/AppCtrl.java +++ /dev/null @@ -1,45 +0,0 @@ -package cad.fx; - -import cad.fx.viewer.Viewer3D; -import javafx.fxml.Initializable; -import javafx.scene.Group; -import javafx.scene.control.Button; - -import java.net.URL; -import java.util.List; -import java.util.ResourceBundle; - -public class AppCtrl implements Initializable { - - private final CadContext cadContext = new CadContext(); - - public Viewer3D viewer; - public Button beginSketching; - public Button endSketching; - public Button pad; - public Button cut; - - @Override - public void initialize(URL location, ResourceBundle resources) { - Group content = new Group(); - setInitObject(content); - viewer.setContent(content); - beginSketching.setOnAction(event -> { - cadContext.beginSketching(); - }); - endSketching.setOnAction(event -> { - cadContext.endSketching(); - }); - pad.setOnAction(event -> { - cadContext.pad(50); - }); - } - - private void setInitObject(Group parent) { - List cube = Utils3D.createCube(100); - parent.getChildren().addAll(cadContext.toNodes(cube)); -// -// CSG init = new Cube(100).toCSG().difference(new Cylinder(30, 100, 10).toCSG()); -// return new CSGNode(Utils3D.getFXMesh(init), cadContext); - } -} diff --git a/src/cad/fx/CSGMesh.java b/src/cad/fx/CSGMesh.java deleted file mode 100644 index f2e3a468..00000000 --- a/src/cad/fx/CSGMesh.java +++ /dev/null @@ -1,11 +0,0 @@ -package cad.fx; - -import gnu.trove.map.TIntObjectMap; -import gnu.trove.map.hash.TIntObjectHashMap; -import javafx.scene.shape.TriangleMesh; - -public class CSGMesh extends TriangleMesh { - - public final TIntObjectMap polygons = new TIntObjectHashMap<>(); - -} diff --git a/src/cad/fx/CSGNode.java b/src/cad/fx/CSGNode.java deleted file mode 100644 index 30e35c3e..00000000 --- a/src/cad/fx/CSGNode.java +++ /dev/null @@ -1,48 +0,0 @@ -package cad.fx; - -import javafx.scene.Group; -import javafx.scene.shape.MeshView; - -import java.util.HashMap; -import java.util.Map; - -public class CSGNode extends MeshView { - - private final CadContext context; - - public CSGNode(CSGMesh mesh, CadContext context) { - super(mesh); - this.context = context; - setMaterial(Utils3D.DEFAULT_MATERIAL); - setOnMouseEntered(e -> { - context.highlightManger.selectExclusively(this); - }); - setOnMouseExited(e -> { - context.highlightManger.getSelection().clear(); - }); - setOnMouseClicked(e -> { - context.clickOnNode(this, e); - }); - } - - private void highlight(eu.mihosoft.vrl.v3d.Polygon poly) { - System.out.println(poly); - } - - private void select(eu.mihosoft.vrl.v3d.Polygon poly) { - System.out.println(poly); - } - - public final Map sketches = new HashMap<>(); - - public Sketch getSketch(Polygon poly) { - Sketch sketch = sketches.get(poly); - if (sketch == null) { - sketch = new Sketch(poly); - ((Group) getParent()).getChildren().add(sketch.drawLayer); - sketches.put(poly, sketch); - } - return sketch; - } - -} diff --git a/src/cad/fx/CadContext.java b/src/cad/fx/CadContext.java deleted file mode 100644 index e944ee0f..00000000 --- a/src/cad/fx/CadContext.java +++ /dev/null @@ -1,153 +0,0 @@ -package cad.fx; - -import cad.math.Vector; -import javafx.scene.Node; -import javafx.scene.input.MouseEvent; -import javafx.scene.input.PickResult; -import javafx.scene.paint.PhongMaterial; -import javafx.scene.shape.MeshView; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static java.util.stream.Collectors.toList; - -public class CadContext { - - public Sketcher sketcher; - public Selection selection; - public final SelectionManager selectionManger = new SelectionManager(); - public final SelectionManager highlightManger = new SelectionManager(); - - - class MaterialChangeListener implements SelectionManager.Listener { - - public final PhongMaterial onSelect; - public final PhongMaterial onDeselect; - private SelectionManager dependency; - - MaterialChangeListener(PhongMaterial onSelect, PhongMaterial onDeselect, SelectionManager dependency) { - this.onSelect = onSelect; - this.onDeselect = onDeselect; - this.dependency = dependency; - } - - public void added(List nodes) { - if (dependency != null) { - nodes = filter(nodes, dependency); - } - setMaterial(nodes, onSelect); - } - - public void removed(List nodes) { - if (dependency != null) { - nodes = filter(nodes, dependency); - } - setMaterial(nodes, onDeselect); - } - - private List filter(List nodes, SelectionManager dependency) { - nodes = new ArrayList<>(nodes); - nodes.removeAll(selectionManger.getSelection()); - return nodes; - } - } - - { - selectionManger.addListener(new MaterialChangeListener(Utils3D.SELECTED_MATERIAL, Utils3D.DEFAULT_MATERIAL, null)); - highlightManger.addListener(new MaterialChangeListener(Utils3D.HIGHLIGHTED_MATERIAL, Utils3D.DEFAULT_MATERIAL, selectionManger)); - } - - private void setMaterial(List nodes, PhongMaterial material) { - for (Node node : nodes) { - if (node instanceof MeshView) { - ((MeshView) node).setMaterial(material); - } - } - } - - public void clickOnNode(CSGNode csgNode, MouseEvent e) { - selectionManger.selectExclusively(csgNode); - PickResult pickResult = e.getPickResult(); - int face = pickResult.getIntersectedFace(); - CSGMesh csgMesh = (CSGMesh) csgNode.getMesh(); - Polygon poly = csgMesh.polygons.get(face); - System.out.println(poly); - if (poly != null) { - if (selection != null) { - boolean isSameNode = selection.sameTo(csgNode, poly); - if (sketcher == null && !isSameNode) { - selection = new Selection(csgNode, poly); - } - if (sketcher != null && isSameNode) { - sketcher.addPoint(pickResult.getIntersectedPoint()); - } - } else { - if (sketcher == null) { - selection = new Selection(csgNode, poly); - } - } - } - } - - public void beginSketching() { - if (sketcher != null || selection == null) { - return; - } - sketcher = new Sketcher(selection.csgNode.getSketch(selection.poly)); - } - - public void endSketching() { - if (sketcher == null) { - return; - } - sketcher.commitOperation(); - sketcher = null; - } - - public void pad(double height) { - if (selection == null) { - return; - } - - Sketch sketch = selection.csgNode.getSketch(selection.poly); - Vector dir = sketch.owner.normal.multi(height); - for (List polygon : sketch.polygons) { - if (polygon.isEmpty()) { - continue; - } - - Polygon poly = new Polygon(sketch.owner.normal, polygon, Collections.emptyList()); - List extruded = Polygon.extrude(poly, dir); - - for (Polygon s : extruded) { - sketch.drawLayer.getChildren().addAll(toNodes(extruded));// fixme - } -// CSG pad = Extrude.points(dir, polygon); - } - } - - public List toNodes(List extruded) { - return extruded.stream().map(this::toNode).collect(toList()); - } - - public CSGNode toNode(Polygon poly) { - return new CSGNode(Utils3D.getMesh(Collections.singletonList(poly)), this); - } - - public static class Selection { - - public final CSGNode csgNode; - public final Polygon poly; - - public Selection(CSGNode csgNode, Polygon poly) { - this.csgNode = csgNode; - this.poly = poly; - } - - public boolean sameTo(CSGNode csgNode, Polygon poly) { - return this.csgNode.equals(csgNode) && this.poly.equals(poly); - } - } -} diff --git a/src/cad/fx/Polygon.java b/src/cad/fx/Polygon.java deleted file mode 100644 index 3a363b94..00000000 --- a/src/cad/fx/Polygon.java +++ /dev/null @@ -1,177 +0,0 @@ -package cad.fx; - -import cad.math.HMath; -import cad.math.Matrix; -import cad.math.Vector; -import org.poly2tri.Poly2Tri; -import org.poly2tri.geometry.polygon.PolygonPoint; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -import static java.util.stream.Collectors.toList; - -public class Polygon { - - public final Vector normal; - public final List shell; - public final List> holes; - - private List triangles; - - public Polygon(List shell) { - this(shell, Collections.emptyList()); - } - - public Polygon(List shell, List> holes) { - this(normalOfCCWSeq(shell.get(0), shell.get(1), shell.get(2)), shell, holes); - } - - public Polygon(Vector normal, List shell, List> holes) { - this.normal = normal.normalize(); - this.shell = shell; - this.holes = holes; - checkPolygon(shell); - for (List hole : holes) { - checkPolygon(hole); - } - } - - public Polygon fixCCW() { - if (!normal.slightlyEqualTo(normalOfCCWSeq(shell.get(0), shell.get(1), shell.get(2)))) { - List shell = new ArrayList<>(this.shell); - Collections.reverse(shell); - return new Polygon(normal, shell, holes); - } - return this; - } - - - public Vector[] someBasis() { - Vector x = shell.get(1).minus(shell.get(0)).normalize(); - Vector y = normal.cross(x).normalize(); - return new Vector[] {x, y, normal}; - } - - private void checkPolygon(List points) { - if (points.size() < 3) { - throw new IllegalArgumentException("Polygon should contain at least 3 point"); - } - } - - public List getTriangles() { - if (triangles == null) { - triangulate(); - } - return triangles; - } - - private void triangulate() { - - Matrix _3dTransformation = new Matrix(someBasis()); - Matrix _2dTransformation = _3dTransformation.invert(); - - List shellPoints = shell.stream() - .map(vector -> HMath.cross(_2dTransformation, vector)) - .map(vector -> new PolygonPoint(vector.x, vector.y, vector.z)) - .collect(toList()); - - org.poly2tri.geometry.polygon.Polygon polygon = new org.poly2tri.geometry.polygon.Polygon(shellPoints); - - for (List hole : holes) { - - List holePoints = hole.stream() - .map(vector -> HMath.cross(_2dTransformation, vector)) - .map(vector -> new PolygonPoint(vector.x, vector.y, vector.z)) - .collect(toList()); - - polygon.addHole(new org.poly2tri.geometry.polygon.Polygon(holePoints)); - } - - Poly2Tri.triangulate(polygon); - - triangles = polygon.getTriangles().stream() - .map(tr -> - new Vector[]{ - HMath.cross(_3dTransformation, new Vector(tr.points[0].getX(), tr.points[0].getY(), tr.points[0].getZ())), - HMath.cross(_3dTransformation, new Vector(tr.points[1].getX(), tr.points[1].getY(), tr.points[1].getZ())), - HMath.cross(_3dTransformation, new Vector(tr.points[2].getX(), tr.points[2].getY(), tr.points[2].getZ())) - }) - .collect(Collectors.toList()); - - setupNormal(triangles, normal); - } - - public static void setupNormal(List triangles, Vector normal) { - - for (Vector[] triangle : triangles) { - if (!normalOfCCWSeq(triangle[0], triangle[1], triangle[2]).slightlyEqualTo(normal)) { - reverse(triangle); - System.out.println(""); - } - } - } - - public static Vector normalOfCCWSeq(Vector v0, Vector v1, Vector v2) { - return v1.minus(v0).cross(v2.minus(v0)).normalize(); - } - - private static void reverse(Vector[] triangle) { - Vector first = triangle[0]; - triangle[0] = triangle[2]; - triangle[2] = first; - } - - public Polygon flip() { - return new Polygon(normal.negate(), shell, holes); - } - - public static List extrude(Polygon source, Vector target) { - - double dotProduct = target.normalize().dot(source.normal); - if (dotProduct == 0) { - return Collections.emptyList(); - } - if (dotProduct > 0) { - source = source.flip(); - } - source = source.fixCCW(); - - List poly = new ArrayList<>(); - poly.add(source); - - Polygon lid = source.shift(target).flip(); - poly.add(lid); - - for (int i = 0; i < source.shell.size(); i++) { - Polygon face = new Polygon(Arrays.asList( - get(source.shell, i - 1), - get(lid.shell, i - 1), - get(lid.shell, i), - get(source.shell, i) - )); - poly.add(face); - } - return poly; - } - - public static T get(List list, int i) { - i = i % list.size(); - if (i < 0) { - i = list.size() + i; - } - return list.get(i); - } - - public Polygon shift(Vector target) { - List shell = this.shell.stream().map(vector -> vector.plus(target)).collect(toList()); - List> holes = new ArrayList<>(); - for (List hole : this.holes) { - holes.add(hole.stream().map(vector -> vector.plus(target)).collect(toList())); - } - return new Polygon(normal, shell, holes); - } -} diff --git a/src/cad/fx/SelectionManager.java b/src/cad/fx/SelectionManager.java deleted file mode 100644 index 1c07e761..00000000 --- a/src/cad/fx/SelectionManager.java +++ /dev/null @@ -1,66 +0,0 @@ -package cad.fx; - -import com.sun.javafx.collections.TrackableObservableList; -import javafx.collections.ListChangeListener; -import javafx.collections.ObservableList; -import javafx.scene.Node; - -import java.util.ArrayList; -import java.util.List; - -public class SelectionManager { - - private final List listeners = new ArrayList<>(); - - public void addListener(Listener listener) { - listeners.add(listener); - } - - private final ObservableList selection = new TrackableObservableList() { - - protected void onChanged(ListChangeListener.Change c) { - while (c.next()) { - if (c.wasAdded()) { - List added = c.getAddedSubList(); - added.forEach((n) -> { - ObservableList styleClass = n.getStyleClass(); - if (!styleClass.contains("selected")) { - styleClass.add("selected"); - } - }); - fireSelected(added); - } else if (c.wasRemoved()) { - List removed = c.getRemoved(); - removed.forEach((n) -> n.getStyleClass().removeAll("selected")); - fireRemoved(removed); - } - } - } - }; - - private void fireRemoved(List removed) { - for (Listener l : listeners) { - l.removed(removed); - } - } - - private void fireSelected(List added) { - for (Listener l : listeners) { - l.added(added); - } - } - - public ObservableList getSelection() { - return selection; - } - - public interface Listener { - void added(List nodes); - void removed(List nodes); - } - - public void selectExclusively(Node node) { - selection.clear(); - selection.add(node); - } -} diff --git a/src/cad/fx/Sketch.java b/src/cad/fx/Sketch.java deleted file mode 100644 index 7c4fc58f..00000000 --- a/src/cad/fx/Sketch.java +++ /dev/null @@ -1,18 +0,0 @@ -package cad.fx; - -import cad.math.Vector; -import javafx.scene.Group; - -import java.util.ArrayList; -import java.util.List; - -public class Sketch { - - public final Polygon owner; - public final List> polygons = new ArrayList<>(); - public final Group drawLayer = new Group(); - - public Sketch(Polygon owner) { - this.owner = owner; - } -} diff --git a/src/cad/fx/Sketcher.java b/src/cad/fx/Sketcher.java deleted file mode 100644 index aafaf22e..00000000 --- a/src/cad/fx/Sketcher.java +++ /dev/null @@ -1,36 +0,0 @@ -package cad.fx; - -import cad.math.Vector; -import eu.mihosoft.vrl.v3d.Vector3d; -import javafx.geometry.Point3D; -import javafx.scene.shape.Line; -import javafx.scene.shape.Sphere; - -import java.util.ArrayList; -import java.util.List; - -public class Sketcher { - - public final Sketch sketch; - - public Sketcher(Sketch sketch) { - this.sketch = sketch; - if (sketch.polygons.isEmpty()) { - sketch.polygons.add(new ArrayList<>()); - } - } - - public void addPoint(Point3D point) { - List poly = sketch.polygons.get(sketch.polygons.size() - 1); - poly.add(new Vector(point.getX(), point.getY(), point.getZ())); - Sphere pt = new Sphere(1); - pt.setTranslateX(point.getX()); - pt.setTranslateY(point.getY()); - pt.setTranslateZ(point.getZ()); - sketch.drawLayer.getChildren().addAll(pt); - } - - public void commitOperation() { - sketch.polygons.add(new ArrayList<>()); - } -} diff --git a/src/cad/fx/Test.java b/src/cad/fx/Test.java deleted file mode 100644 index fc68a3e9..00000000 --- a/src/cad/fx/Test.java +++ /dev/null @@ -1,82 +0,0 @@ -package cad.fx; - -import javafx.application.Application; -import javafx.fxml.FXMLLoader; -import javafx.scene.Scene; -import javafx.scene.canvas.Canvas; -import javafx.scene.control.ScrollPane; -import javafx.scene.layout.Pane; -import javafx.scene.shape.Line; -import javafx.scene.transform.Translate; -import javafx.stage.Stage; -import org.poly2tri.Poly2Tri; -import org.poly2tri.geometry.polygon.Polygon; -import org.poly2tri.geometry.polygon.PolygonPoint; -import org.poly2tri.triangulation.TriangulationPoint; -import org.poly2tri.triangulation.delaunay.DelaunayTriangle; - -import java.util.Arrays; -import java.util.List; - -public class Test extends Application { - - public static void main(String[] args) { - System.setProperty("prism.dirtyopts", "false"); - launch(args); - } - - @Override - public void start(Stage primaryStage) throws Exception { - Pane pane = new Pane(); - Scene scene = new Scene(pane, 600, 600); - - - - - Polygon polygon = new Polygon(Arrays.asList( - new PolygonPoint(0, 0), -// new PolygonPoint(200, 100), - new PolygonPoint(400, 0), - new PolygonPoint(400, 400), - new PolygonPoint(0, 400) - )); - - Polygon hole = new Polygon(Arrays.asList( - new PolygonPoint(50, 50), - new PolygonPoint(50, 100), - new PolygonPoint(100, 100), - new PolygonPoint(100, 50) - )); - - polygon.addHole(hole); - - Poly2Tri.triangulate(polygon); - - - for (DelaunayTriangle triangle : polygon.getTriangles()) { - show(pane, Arrays.asList(triangle.points)); - } - - pane.getTransforms().add(new Translate(10, 10)); - show(pane, polygon); - - primaryStage.setScene(scene); - primaryStage.show(); - } - - private void show(Pane pane, Polygon polygon) { - show(pane, polygon.getPoints()); - } - - private void show(Pane pane, List points) { - - TriangulationPoint first = points.get(0); - TriangulationPoint prev = first; - for (TriangulationPoint point : points.subList(1, points.size())) { - pane.getChildren().addAll(new Line(prev.getX(), prev.getY(), point.getX(), point.getY())); - prev = point; - } - pane.getChildren().addAll(new Line(prev.getX(), prev.getY(), first.getX(), first.getY())); - } - -} diff --git a/src/cad/fx/Utils3D.java b/src/cad/fx/Utils3D.java deleted file mode 100644 index c2093ff9..00000000 --- a/src/cad/fx/Utils3D.java +++ /dev/null @@ -1,178 +0,0 @@ -package cad.fx; - -import cad.math.Vector; -import javafx.scene.paint.Color; -import javafx.scene.paint.PhongMaterial; -import org.poly2tri.triangulation.TriangulationPoint; -import org.poly2tri.triangulation.delaunay.DelaunayTriangle; - -import java.util.Arrays; -import java.util.List; - -public class Utils3D { - - public static final PhongMaterial DEFAULT_MATERIAL = new PhongMaterial(); - public static final PhongMaterial SELECTED_MATERIAL = new PhongMaterial(); - public static final PhongMaterial HIGHLIGHTED_MATERIAL = new PhongMaterial(); - - static { -// DEFAULT_MATERIAL.setDiffuseColor(Color.LIGHTBLUE); -// DEFAULT_MATERIAL.setSpecularColor(Color.WHITE); - - DEFAULT_MATERIAL.setDiffuseColor(Color.LIGHTSTEELBLUE); -// DEFAULT_MATERIAL.setSpecularColor(Color.LIGHTBLUE); - - SELECTED_MATERIAL.setDiffuseColor(Color.AZURE); -// SELECTED_MATERIAL.setSpecularColor(Color.SEAGREEN); //disable reflection - - HIGHLIGHTED_MATERIAL.setDiffuseColor(Color.LIGHTGOLDENRODYELLOW); -// HIGHLIGHTED_MATERIAL.setSpecularColor(Color.GOLD); - -// DEFAULT_MATERIAL.setDiffuseMap(new Image(Utils3D.class.getResource("tex.png").toExternalForm())); - } - - - public static CSGMesh getMesh(List polygons) { - - CSGMesh mesh = new CSGMesh(); - - int faceCounter = 0; - - for (Polygon poly : polygons) { - - - for (Vector[] triangle : poly.getTriangles()) { - - - mesh.getPoints().addAll( - (float) triangle[0].x, - (float) triangle[0].y, - (float) triangle[0].z - ); - - mesh.getTexCoords().addAll(0); // texture (not covered) - mesh.getTexCoords().addAll(0); - - - mesh.getPoints().addAll( - (float) triangle[1].x, - (float) triangle[1].y, - (float) triangle[1].z - ); - - mesh.getTexCoords().addAll(0); // texture (not covered) - mesh.getTexCoords().addAll(0); - - mesh.getPoints().addAll( - (float) triangle[2].x, - (float) triangle[2].y, - (float) triangle[2].z - ); - - - mesh.getTexCoords().addAll(0); // texture (not covered) - mesh.getTexCoords().addAll(0); - - int counter = faceCounter * 3; - mesh.getFaces().addAll( - counter, // first vertex - 0, // texture (not covered) - counter + 1, // second vertex - 0, // texture (not covered) - counter + 2, // third vertex - 0 // texture (not covered) - ); - mesh.polygons.put(faceCounter, poly); - ++faceCounter; - - } // end if #verts >= 3 - - } // end for polygon - - return mesh; - } - - public static CSGMesh getFXMesh(List triangles) { - - CSGMesh mesh = new CSGMesh(); - - int faceCounter = 0; - - for (DelaunayTriangle p : triangles) { - - - TriangulationPoint firstVertex = p.points[0]; - - mesh.getPoints().addAll( - p.points[2].getXf(), - p.points[2].getYf(), - p.points[2].getZf() - ); - - mesh.getTexCoords().addAll(0); // texture (not covered) - mesh.getTexCoords().addAll(0); - - mesh.getPoints().addAll( - p.points[1].getXf(), - p.points[1].getYf(), - p.points[1].getZf() - ); - - mesh.getTexCoords().addAll(0); // texture (not covered) - mesh.getTexCoords().addAll(0); - - mesh.getPoints().addAll( - p.points[0].getXf(), - p.points[0].getYf(), - p.points[0].getZf() - ); - - mesh.getTexCoords().addAll(0); // texture (not covered) - mesh.getTexCoords().addAll(0); - - int counter = faceCounter * 3; - mesh.getFaces().addAll( - counter, // first vertex - 0, // texture (not covered) - counter + 1, // second vertex - 0, // texture (not covered) - counter + 2, // third vertex - 0 // texture (not covered) - ); -// mesh.polygons.put(faceCounter, p); - ++faceCounter; - - } // end for polygon - - return mesh; - } - - public static List createCube(double width) { - Polygon square = createSquare(width); - return Polygon.extrude(square, square.normal.multi(width)); - } - - public static Polygon createSquare(double width) { - - width /= 2; - - List shell = Arrays.asList( - new Vector(-width, -width), - new Vector(width, -width), - new Vector(width, width, 0), - new Vector(-width, width, 0) - ); - -// width /= 3; -// org.poly2tri.geometry.polygon.Polygon hole = new org.poly2tri.geometry.polygon.Polygon(Arrays.asList( -// new PolygonPoint(-width, -width), -// new PolygonPoint(width, -width), -// new PolygonPoint(width, width, 100), -// new PolygonPoint(-width, width, 100) -// )); -// -// polygon.addHole(hole); - - return new Polygon(shell); - } -} diff --git a/src/cad/fx/app.fxml b/src/cad/fx/app.fxml deleted file mode 100644 index b79c912c..00000000 --- a/src/cad/fx/app.fxml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - -