Skip to content

Commit 34d3ea6

Browse files
committed
added List of Points
Lsit of Points is a List<Point2d> Added DAO Added OutputPoints and InputPoints Added PrintPoints Node Added TurtleToPoints Node Added GridOfPoints Node
1 parent 4bceb6c commit 34d3ea6

File tree

11 files changed

+229
-3
lines changed

11 files changed

+229
-3
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ It pairs really well with Marlin-polargraph, the code in the brain of the robot
569569
Github/Jitpack/etc can find the copy published by Jitpack.
570570
I have to do this dance every time.
571571
-->
572-
<groupId>com.github.marginallyclever</groupId>
572+
<groupId>com.marginallyclever</groupId>
573573
<artifactId>nodegraphcore</artifactId>
574574
<version>1.1.0</version>
575575
</dependency>
@@ -581,7 +581,7 @@ It pairs really well with Marlin-polargraph, the code in the brain of the robot
581581
Github/Jitpack/etc can find the copy published by Jitpack.
582582
I have to do this dance every time.
583583
-->
584-
<groupId>com.github.marginallyclever</groupId>
584+
<groupId>com.marginallyclever</groupId>
585585
<artifactId>donatello</artifactId>
586586
<version>1.4.3</version>
587587
</dependency>

src/main/java/com/marginallyclever/makelangelo/donatelloimpl/MakelangeloRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.marginallyclever.makelangelo.donatelloimpl;
22

3+
import com.marginallyclever.makelangelo.donatelloimpl.nodes.points.PointsDAO4JSON;
34
import com.marginallyclever.makelangelo.donatelloimpl.nodes.turtle.TurtleDAO4JSON;
45
import com.marginallyclever.makelangelo.turtle.Turtle;
56
import com.marginallyclever.nodegraphcore.DAO4JSONFactory;
@@ -32,5 +33,6 @@ public void registerNodes() {
3233
public void registerDAO() {
3334
logger.info("Registering makelangelo-software DAOs");
3435
DAO4JSONFactory.registerDAO(new TurtleDAO4JSON());
36+
DAO4JSONFactory.registerDAO(new PointsDAO4JSON());
3537
}
3638
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.marginallyclever.makelangelo.donatelloimpl.nodes.points;
2+
3+
import com.marginallyclever.donatello.ports.InputDouble;
4+
import com.marginallyclever.donatello.ports.InputInt;
5+
import com.marginallyclever.nodegraphcore.Node;
6+
7+
import javax.vecmath.Point2d;
8+
import java.util.stream.IntStream;
9+
10+
/**
11+
* Create a grid of points controlled by the number (quantity) and spacing (distance between points).
12+
*/
13+
public class GridOfPoints extends Node {
14+
private final InputInt numberAcross = new InputInt("x count",10);
15+
private final InputDouble spaceAcross = new InputDouble("x spacing",10d);
16+
private final InputInt numberDown = new InputInt("y count",10);
17+
private final InputDouble spaceDown = new InputDouble("y spacing",10d);
18+
private final OutputPoints output = new OutputPoints("output");
19+
20+
public GridOfPoints() {
21+
super("GridOfPoints");
22+
addVariable(numberAcross);
23+
addVariable(spaceAcross);
24+
addVariable(numberDown);
25+
addVariable(spaceDown);
26+
addVariable(output);
27+
}
28+
29+
@Override
30+
public void update() {
31+
var nx = Math.max(1,numberAcross.getValue());
32+
var ny = Math.max(1,numberDown.getValue());
33+
var dx = spaceAcross.getValue();
34+
var dy = spaceDown.getValue();
35+
36+
var list = new ListOfPoints();
37+
IntStream.range(0,ny).forEach(y->{
38+
IntStream.range(0,nx).forEach(x->{
39+
list.add(new Point2d(x*dx,y*dy));
40+
});
41+
});
42+
43+
output.send(list);
44+
}
45+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.marginallyclever.makelangelo.donatelloimpl.nodes.points;
2+
3+
import com.marginallyclever.nodegraphcore.port.Input;
4+
5+
/**
6+
* {@link Input} for a {@link ListOfPoints}.
7+
*/
8+
public class InputPoints extends Input<ListOfPoints> {
9+
public InputPoints(String _name) throws IllegalArgumentException {
10+
super(_name, ListOfPoints.class, new ListOfPoints());
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.marginallyclever.makelangelo.donatelloimpl.nodes.points;
2+
3+
import javax.vecmath.Point2d;
4+
import java.util.ArrayList;
5+
6+
/**
7+
* A list of 2D points with double precision.
8+
*/
9+
public class ListOfPoints extends ArrayList<Point2d> {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.marginallyclever.makelangelo.donatelloimpl.nodes.points;
2+
3+
import com.marginallyclever.nodegraphcore.port.Output;
4+
5+
/**
6+
* {@link Output} for a {@link ListOfPoints}.
7+
*/
8+
public class OutputPoints extends Output<ListOfPoints> {
9+
public OutputPoints(String _name) throws IllegalArgumentException {
10+
super(_name, ListOfPoints.class, new ListOfPoints());
11+
}
12+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.marginallyclever.makelangelo.donatelloimpl.nodes.points;
2+
3+
import com.marginallyclever.nodegraphcore.AbstractDAO4JSON;
4+
import org.json.JSONArray;
5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
8+
import javax.vecmath.Point2d;
9+
import java.util.stream.IntStream;
10+
11+
/**
12+
* DAO for ListOfPoints
13+
*/
14+
public class PointsDAO4JSON extends AbstractDAO4JSON<ListOfPoints> {
15+
public PointsDAO4JSON() {
16+
super(ListOfPoints.class);
17+
}
18+
19+
@Override
20+
public Object toJSON(Object object) throws JSONException {
21+
JSONArray array = new JSONArray();
22+
ListOfPoints points = (ListOfPoints)object;
23+
// for a complete snapshot, capture all the instance details, too.
24+
for(var p : points) {
25+
JSONObject point = new JSONObject();
26+
point.put("x", p.getX());
27+
point.put("y", p.getY());
28+
array.put(point);
29+
}
30+
31+
return array;
32+
}
33+
34+
@Override
35+
public ListOfPoints fromJSON(Object object) throws JSONException {
36+
JSONArray array = (JSONArray)object;
37+
// for a complete snapshot, restore all the instance details, too.
38+
var list = new ListOfPoints();
39+
IntStream.range(0, array.length()).forEach(i ->{
40+
JSONObject point = array.getJSONObject(i);
41+
list.add(new Point2d(
42+
point.getDouble("x"),
43+
point.getDouble("y")
44+
));
45+
});
46+
return list;
47+
}
48+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.marginallyclever.makelangelo.donatelloimpl.nodes.points;
2+
3+
import com.marginallyclever.donatello.graphview.GraphViewPanel;
4+
import com.marginallyclever.donatello.ports.InputColor;
5+
import com.marginallyclever.donatello.ports.InputInt;
6+
import com.marginallyclever.nodegraphcore.Node;
7+
import com.marginallyclever.nodegraphcore.PrintWithGraphics;
8+
9+
import java.awt.*;
10+
import java.util.concurrent.locks.ReentrantLock;
11+
12+
/**
13+
* Draw a list of points.
14+
*/
15+
public class PrintPoints extends Node implements PrintWithGraphics {
16+
private final InputPoints input = new InputPoints("points");
17+
private final InputInt radius = new InputInt("radius",5);
18+
private final InputColor color = new InputColor("color",Color.WHITE);
19+
private final ReentrantLock lock = new ReentrantLock();
20+
private ListOfPoints list;
21+
22+
public PrintPoints() {
23+
super("PrintPoints");
24+
addVariable(input);
25+
addVariable(radius);
26+
addVariable(color);
27+
}
28+
29+
@Override
30+
public void update() {
31+
lock.lock();
32+
try {
33+
list = input.getValue();
34+
} finally {
35+
lock.unlock();
36+
}
37+
}
38+
39+
@Override
40+
public void print(Graphics g) {
41+
if(list==null || list.isEmpty()) return;
42+
lock.lock();
43+
try {
44+
45+
Graphics2D g2 = (Graphics2D)g.create();
46+
GraphViewPanel.setHints(g2);
47+
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
48+
g2.setStroke(new BasicStroke(radius.getValue()));
49+
g2.setColor(color.getValue());
50+
51+
list.stream().parallel().forEach(p -> {
52+
g2.drawLine((int)p.x, (int)p.y, (int)p.x, (int)p.y);
53+
});
54+
} finally {
55+
lock.unlock();
56+
}
57+
}
58+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.marginallyclever.makelangelo.donatelloimpl.nodes.points;
2+
3+
import com.marginallyclever.makelangelo.donatelloimpl.ports.InputTurtle;
4+
import com.marginallyclever.makelangelo.turtle.MovementType;
5+
import com.marginallyclever.nodegraphcore.Node;
6+
7+
import javax.vecmath.Point2d;
8+
9+
/**
10+
* Converts a {@link com.marginallyclever.makelangelo.turtle.Turtle} to a {@link ListOfPoints}.
11+
*/
12+
public class TurtleToPoints extends Node {
13+
private final InputTurtle turtle = new InputTurtle("Turtle");
14+
private final OutputPoints points = new OutputPoints("points");
15+
16+
public TurtleToPoints() {
17+
super("TurtleToPoints");
18+
addVariable(turtle);
19+
addVariable(points);
20+
}
21+
22+
@Override
23+
public void update() {
24+
var in = turtle.getValue();
25+
var out = new ListOfPoints();
26+
27+
for( var move : in.history ) {
28+
if(move.type != MovementType.TOOL_CHANGE) {
29+
out.add(new Point2d(move.x,move.y));
30+
}
31+
}
32+
33+
points.send(out);
34+
}
35+
}

src/main/java/com/marginallyclever/makelangelo/donatelloimpl/nodes/turtle/TurtleDAO4JSON.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ public Object toJSON(Object object) throws JSONException {
1515
JSONObject json = new JSONObject();
1616
Turtle turtle = (Turtle)object;
1717
// for a complete snapshot, capture all the instance details, too.
18+
// TODO save turtle to JSON.
19+
1820
return json;
1921
}
2022

2123
@Override
2224
public Turtle fromJSON(Object object) throws JSONException {
2325
JSONObject json = (JSONObject)object;
2426
// for a complete snapshot, restore all the instance details, too.
25-
return new Turtle();
27+
Turtle turtle = new Turtle();
28+
// TODO load turtle from JSON
29+
return turtle;
2630
}
2731
}

0 commit comments

Comments
 (0)