Skip to content

Commit 2754435

Browse files
committed
Makelangelo 5 frame is now drawing again
1 parent 81ba492 commit 2754435

15 files changed

+578
-399
lines changed

WeavingRadonJava/app.layout

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" ?>
2+
<app-layout>
3+
<layout main-frame="true" location="487,73" size="1208,922" state="0">
4+
<westToolbar>
5+
</westToolbar>
6+
<eastToolbar>
7+
</eastToolbar>
8+
<southToolbar>
9+
</southToolbar>
10+
<split orientation="1" divider-proportion="0.7118786573410034">
11+
<left>
12+
<tabbed>
13+
<selectedTab persistentID="8e50154c-a149-4e95-9db5-4611d24cc0cc">
14+
</selectedTab>
15+
<tab persistentID="8e50154c-a149-4e95-9db5-4611d24cc0cc">
16+
<properties></properties>
17+
</tab>
18+
<tab persistentID="e675ab55-fea9-49a5-a04d-51daa9cd31e6">
19+
<properties></properties>
20+
</tab>
21+
</tabbed>
22+
</left>
23+
<right>
24+
<tabbed>
25+
<selectedTab persistentID="f2308391-8388-4f90-89f0-61caca03eb18">
26+
</selectedTab>
27+
<tab persistentID="f2308391-8388-4f90-89f0-61caca03eb18">
28+
<properties></properties>
29+
</tab>
30+
<tab persistentID="eb1ea92b-fc66-4c54-a5a9-3e4f8203bd5d">
31+
<properties></properties>
32+
</tab>
33+
</tabbed>
34+
</right>
35+
</split>
36+
</layout>
37+
<undocked>
38+
</undocked>
39+
</app-layout>

WeavingRadonJava/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@
9393
<artifactId>vecmath</artifactId>
9494
<version>1.5.2</version>
9595
</dependency>
96+
<dependency>
97+
<groupId>org.junit.jupiter</groupId>
98+
<artifactId>junit-jupiter</artifactId>
99+
<version>5.8.1</version>
100+
<scope>test</scope>
101+
</dependency>
96102
</dependencies>
97103

98104
</project>

WeavingRadonJava/src/main/java/com/marginallyclever/weavingradon/WeavingApp.java

+46-21
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,43 @@
1010
import java.util.ArrayList;
1111
import java.util.Arrays;
1212

13+
import ModernDocking.app.AppState;
1314
import ModernDocking.app.Docking;
1415
import ModernDocking.app.RootDockingPanel;
16+
import ModernDocking.exception.DockingLayoutException;
1517
import ModernDocking.ext.ui.DockingUI;
1618
import com.formdev.flatlaf.FlatLaf;
1719
import com.formdev.flatlaf.FlatLightLaf;
18-
import com.marginallyclever.weavingradon.core.MulticolorThreader;
19-
import com.marginallyclever.weavingradon.core.Loom;
20-
import com.marginallyclever.weavingradon.core.RenderHintHelper;
21-
import com.marginallyclever.weavingradon.core.MonochromaticThreader;
20+
import com.marginallyclever.weavingradon.core.*;
2221
import com.marginallyclever.weavingradon.ui.*;
2322

2423
public class WeavingApp {
2524
public static final int DIAMETER = 800;
25+
public static final int NUM_NAILS = 188;
2626
public static final int TITLEBAR_HEIGHT = 30;
2727
public static final int DOCKING_TAB_HEIGHT = 30;
28-
public static final int ALPHA = 127;
28+
public static final int ALPHA = 64;
2929
public static final int TOOLBAR_HEIGHT = 30;
3030

3131
private final JFrame frame;
3232
private final ArrayList<DockingPanel> windows = new ArrayList<>();
3333
private final JFileChooser fileChooser;
3434

35-
private final Loom loom = new Loom(DIAMETER/2, 100);
35+
private final Loom loom = new Loom(DIAMETER/2, NUM_NAILS);
3636
private final LoomPanel loomPanel;
3737
private final RadonPanel radonPanel;
3838

39-
//public final RadonThreader myThreader = new SingleThreader(Color.WHITE);
40-
public final MulticolorThreader myThreader = new MulticolorThreader();
41-
public final MonochromaticThreader radonThreaderC = new MonochromaticThreader(new Color( 0,255,255, ALPHA));
42-
public final MonochromaticThreader radonThreaderM = new MonochromaticThreader(new Color(255, 0,255, ALPHA));
43-
public final MonochromaticThreader radonThreaderY = new MonochromaticThreader(new Color(255,255, 0, ALPHA));
44-
public final MonochromaticThreader radonThreaderK = new MonochromaticThreader(new Color( 0, 0, 0, ALPHA));
45-
public final MonochromaticThreader radonThreaderW = new MonochromaticThreader(new Color(255,255,255, ALPHA));
39+
private final RadonThreader myThreader;
40+
/*
41+
private final MonochromaticThreader radonThreaderC = new MonochromaticThreader(new Color( 0,255,255, ALPHA));
42+
private final MonochromaticThreader radonThreaderM = new MonochromaticThreader(new Color(255, 0,255, ALPHA));
43+
private final MonochromaticThreader radonThreaderY = new MonochromaticThreader(new Color(255,255, 0, ALPHA));
44+
private final MonochromaticThreader radonThreaderK = new MonochromaticThreader(new Color( 0, 0, 0, ALPHA));
45+
private final MonochromaticThreader radonThreaderW = new MonochromaticThreader(new Color(255,255,255, ALPHA));*/
4646

4747
private final OneLineOnImage singleLine;
4848
private final RadonPanel singleRadon;
49-
public final MonochromaticThreader radonThreaderB = new MonochromaticThreader(new Color(255,255,255));
49+
private final MonochromaticThreader radonThreaderB = new MonochromaticThreader(new Color(255,255,255));
5050

5151
public static void main(String[] args) {
5252
SwingUtilities.invokeLater(WeavingApp::new);
@@ -61,6 +61,23 @@ public WeavingApp() {
6161
//frame.setLocationByPlatform(true);
6262
frame.setLocationRelativeTo(null);
6363

64+
myThreader = new MonochromaticThreader(new Color(255,255,255, ALPHA));
65+
/*
66+
MulticolorThreader mct = new MulticolorThreader();
67+
mct.addColor(new Color( 0,255,255, ALPHA));
68+
mct.addColor(new Color(255, 0,255, ALPHA));
69+
mct.addColor(new Color(255,255, 0, ALPHA));
70+
mct.addColor(new Color( 0, 0, 0, ALPHA));
71+
mct.addColor(new Color(255,255,255, ALPHA));
72+
myThreader = mct;
73+
*/
74+
/*
75+
mct.addThreader(radonThreaderC);
76+
mct.addThreader(radonThreaderM);
77+
mct.addThreader(radonThreaderY);
78+
mct.addThreader(radonThreaderK);
79+
mct.addThreader(radonThreaderW);
80+
*/
6481
// create panels
6582
loomPanel = new LoomPanel();
6683
radonPanel = new RadonPanel(loomPanel);
@@ -70,16 +87,11 @@ public WeavingApp() {
7087
singleRadon = new RadonPanel(singleLine);
7188
singleLine.setRadon(radonThreaderB,singleRadon);
7289

73-
myThreader.addThreader(radonThreaderC);
74-
myThreader.addThreader(radonThreaderM);
75-
myThreader.addThreader(radonThreaderY);
76-
myThreader.addThreader(radonThreaderK);
77-
myThreader.addThreader(radonThreaderW);
78-
7990
// setup the docking system and dock the panels.
8091
initDocking();
8192
createDefaultLayout();
8293
resetDefaultLayout();
94+
saveAndRestoreLayout();
8395
frame.setJMenuBar(new MainMenu(this));
8496

8597
// create a file chooser for images
@@ -152,6 +164,19 @@ private void resetDefaultLayout() {
152164
//Docking.dock(windows.get(1), windows.get(3), DockingRegion.CENTER);
153165
}
154166

167+
private void saveAndRestoreLayout() {
168+
// now that the main frame is set up with the defaults, we can restore the layout
169+
AppState.setPersistFile(new File("app.layout"));
170+
AppState.setAutoPersist(true);
171+
172+
try {
173+
AppState.restore();
174+
} catch (DockingLayoutException e) {
175+
// something happened trying to load the layout file, record it here
176+
e.printStackTrace();
177+
}
178+
}
179+
155180
public void openFile(ActionEvent actionEvent) {
156181
// show the file chooser dialog
157182
if(fileChooser.showOpenDialog(frame)== JFileChooser. APPROVE_OPTION) {
@@ -163,7 +188,7 @@ public void openFile(ActionEvent actionEvent) {
163188
loom.reset();
164189
// build a radon transform for every thread, based on the color filter for that threader.
165190
myThreader.setLoomAndImage(loom,square);
166-
myThreader.maskCurrentRadonByAllThreads();
191+
myThreader.maskRadonTransformByAllThreads();
167192

168193
loomPanel.setLoomAndImage(loom,square);
169194
radonPanel.setRadonTransform(myThreader.getRadonTransform());

WeavingRadonJava/src/main/java/com/marginallyclever/weavingradon/core/Loom.java

+34-24
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Loom {
2020

2121
public final List<Vector2d> nails = new ArrayList<>();
2222
public final List<LoomThread> selectedThreads = new ArrayList<>();
23-
public final List<LoomThread> potentialThreads = new ArrayList<>();
23+
public final List<LoomThread> allThreads = new ArrayList<>();
2424

2525
public Loom(int radius,int numNails) {
2626
this.radius = radius;
@@ -55,8 +55,8 @@ private void createNails() {
5555
for(int i = 0; i< numNails; ++i) {
5656
double angle = i * Math.PI * 2 / numNails;
5757
nails.add(new Vector2d(
58-
radius + Math.sin(angle) * radius,
59-
radius + Math.cos(angle) * radius
58+
Math.sin(angle) * radius,
59+
Math.cos(angle) * radius
6060
));
6161
}
6262
}
@@ -67,66 +67,76 @@ private void createNails() {
6767
private void createThreads() {
6868
System.out.println("createThreads");
6969
selectedThreads.clear();
70-
potentialThreads.clear();
70+
allThreads.clear();
7171

72-
double maxR = 0;
7372
for (int i = 0; i < numNails; i++) {
7473
Vector2d start = nails.get(i);
75-
double sx = start.x - radius;
76-
double sy = start.y - radius;
74+
double sx = start.x;
75+
double sy = start.y;
7776

7877
for (int j = i + 1; j < numNails; j++) {
7978
Vector2d end = nails.get(j);
80-
double dx = end.x - start.x;
81-
double dy = end.y - start.y;
82-
double len = Math.sqrt(dx * dx + dy * dy);
79+
double dx = end.x - sx;
80+
double dy = end.y - sy;
8381

84-
int theta = (int)Math.toDegrees(Math.atan2(-dx, dy));
82+
double theta = Math.toDegrees(Math.atan2(-dx, dy));
8583

8684
// Ensure theta is within [0-180)
8785
if(theta < 0) theta += 180;
8886
if(theta >= 180) theta -= 180;
8987

9088
double angle = Math.toRadians(theta);
9189
int r = (int)(sx * Math.cos(angle)
92-
+ sy * Math.sin(angle));
90+
+ sy * Math.sin(angle));
9391

9492
//System.out.println("theta="+theta+" r="+r);
95-
maxR = Math.max(maxR, Math.abs(r));
96-
LoomThread thread = new LoomThread(start, end, new ThetaR(theta, r,0), new Color(255, 255, 255, WeavingApp.ALPHA),len);
97-
potentialThreads.add(thread);
93+
LoomThread thread = new LoomThread(start, end, theta, r, new Color(255,255,255, WeavingApp.ALPHA));
94+
allThreads.add(thread);
9895
}
9996
}
10097
}
10198

102-
public void addNextBestThread(LoomThread bestThread) {
99+
public void selectThread(LoomThread bestThread) {
103100
if (bestThread == null) return;
104-
potentialThreads.remove(bestThread);
101+
//potentialThreads.remove(bestThread);
105102
selectedThreads.add(bestThread);
106103
}
107104

108105
public LoomThread findThreadClosestToThetaR(ThetaR target) {
109106
LoomThread nearestThread = null;
110107
double minDistance = Double.MAX_VALUE;
111108

112-
for (LoomThread thread : potentialThreads) {
113-
double x = thread.thetaR.theta - target.theta;
114-
double y = thread.thetaR.r - target.r;
115-
double distanceSquared = ( x*x + y*y );
109+
for (LoomThread thread : allThreads) {
110+
double dTheta = thread.thetaR.theta - target.theta;
111+
double dr = thread.thetaR.r - target.r;
112+
double distanceSquared = Math.sqrt( dTheta*dTheta + dr*dr );
116113
if (distanceSquared < minDistance) {
117114
minDistance = distanceSquared;
118115
nearestThread = thread;
119116
}
120117
}
121118

122-
//if(minDistance>2) return null;
123-
//System.out.println("matches "+ nearestThread );
119+
if(minDistance>2) {
120+
System.out.println("near hit "+target + " vs "+ nearestThread );/*
121+
double previous = minDistance;
122+
123+
for (LoomThread thread : selectedThreads) {
124+
double dTheta = thread.thetaR.theta - target.theta;
125+
double dr = thread.thetaR.r - target.r;
126+
double distanceSquared = Math.sqrt( dTheta*dTheta + dr*dr );
127+
if (distanceSquared < minDistance) {
128+
minDistance = distanceSquared;
129+
nearestThread = thread;
130+
}
131+
}
132+
System.out.println("difference "+ nearestThread + " "+previous+"->"+minDistance);*/
133+
}
124134
//System.out.println(threads.size() +"/"+remainingThreads.size());
125135
return nearestThread;
126136
}
127137

128138
public boolean shouldStop() {
129-
return potentialThreads.size() <= numNails*0.2;
139+
return allThreads.size() <= numNails*0.2;
130140
}
131141

132142
public int getNumNails() {

WeavingRadonJava/src/main/java/com/marginallyclever/weavingradon/core/LoomThread.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import javax.vecmath.Vector2d;
44
import java.awt.*;
5-
import java.awt.image.BufferedImage;
65

76
/**
87
* Thread on a Loom.
@@ -11,29 +10,23 @@ public class LoomThread {
1110
public final Vector2d start; // xy
1211
public final Vector2d end; // xy
1312
public final ThetaR thetaR;
14-
public final double length;
1513
public Color col; // rgba
1614

17-
public LoomThread(Vector2d start, Vector2d end, ThetaR thetaR, Color col, double length) {
15+
public LoomThread(Vector2d start, Vector2d end, double theta, int r, Color col) {
1816
this.start = start;
1917
this.end = end;
20-
this.thetaR = thetaR;
18+
this.thetaR = new ThetaR(theta, r,0);
2119
this.col = col;
22-
this.length = length;
2320
}
2421

2522
public LoomThread(LoomThread b) {
2623
this.start = new Vector2d(b.start);
2724
this.end = new Vector2d(b.end);
2825
this.thetaR = new ThetaR(b.thetaR);
2926
this.col = new Color(b.col.getRGB());
30-
this.length = b.length;
3127
}
3228

33-
public void display(BufferedImage image) {
34-
Graphics2D g2 = image.createGraphics();
35-
RenderHintHelper.setRenderHints(g2);
36-
29+
public void display(Graphics g2) {
3730
g2.setColor(col);
3831
g2.drawLine((int)start.x, (int)start.y, (int)end.x, (int)end.y);
3932
g2.dispose();

0 commit comments

Comments
 (0)