Skip to content

Commit 8885f17

Browse files
authored
Basic Java/Matlab Interface (#2327)
* Adding basic Java Interface and example. * Add basic matlab example * Update matlab example that generates ellipsoids * Add Matlab README.md * Add docs for Java/Matlab
1 parent 6fed0eb commit 8885f17

15 files changed

+367
-0
lines changed

Examples/Java/JavaExample.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.List;
2+
import java.util.Map;
3+
import edu.utah.sci.shapeworks.ShapeWorksProject;
4+
import java.io.*;
5+
6+
public class JavaExample {
7+
public static void main(String[] args) {
8+
9+
// Create a new ShapeWorksProject object
10+
ShapeWorksProject project = new ShapeWorksProject();
11+
12+
// Define the list of shapes to be processed
13+
List<String> filenames = List.of("meshes/ellipsoid_00.vtk", "meshes/ellipsoid_01.vtk",
14+
"meshes/ellipsoid_02.vtk", "meshes/ellipsoid_03.vtk");
15+
project.setShapes(filenames);
16+
17+
// Define grooming parameters
18+
Map<String, String> groomingParameters = Map.of(
19+
"alignment_enabled", "true",
20+
"alignment_method", "Center"
21+
// Add more parameters as needed
22+
);
23+
project.setGroomingParameters(groomingParameters);
24+
25+
// Define optimization parameters
26+
Map<String, String> optimizationParameters = Map.of(
27+
"number_of_particles", "128",
28+
"initial_relative_weighting", "0.05"
29+
// Add more parameters as needed
30+
);
31+
project.setOptimizationParameters(optimizationParameters);
32+
33+
System.out.println("Saving project...");
34+
project.save("project.swproj");
35+
36+
// demonstrate loading
37+
System.out.println("Loading project...");
38+
project.load("project.swproj");
39+
40+
System.out.println("Running groom...");
41+
project.groom();
42+
System.out.println("Running optimize...");
43+
project.optimize();
44+
System.out.println("Opening ShapeWorksStudio...");
45+
project.analyze();
46+
}
47+
}

Examples/Java/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See http://sciinstitute.github.io/ShapeWorks/latest/java/java.html

Examples/Java/meshes/ellipsoid_00.vtk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:9c8a63680f63e9a445c25a1bbf9c62174d996354c58bb88a0982b6d30e7343ec
3+
size 43309

Examples/Java/meshes/ellipsoid_01.vtk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:0661b7aa3ec4b108d9a2bc3d88b810976ac6446830911a1555db45b8ac4d2688
3+
size 43007

Examples/Java/meshes/ellipsoid_02.vtk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:04c7a56b3c8e895edc6d315557e0cb745beb338ad1c85f18ce419225df4620b9
3+
size 42716

Examples/Java/meshes/ellipsoid_03.vtk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:497e2709aff951fb637e304da966b4879b102643aa7b162af36b03ae2e907a68
3+
size 42908

Examples/Matlab/MatlabExample.m

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
% ShapeWorks Matlab Example
2+
3+
% Add the required directory to MATLAB's dynamic class path
4+
javaaddpath('/Users/amorris/sci/shapeworks/code/Java');
5+
6+
% Add the shapeworks binary directory to the system path
7+
setenv('PATH', [getenv('PATH') ':/Users/amorris/sci/shapeworks/bin/bin']);
8+
9+
10+
import java.util.ArrayList;
11+
import java.util.HashMap;
12+
import edu.utah.sci.shapeworks.ShapeWorksProject;
13+
14+
% Create a new ShapeWorksProject object
15+
project = ShapeWorksProject();
16+
17+
% Create a set of ellipsoid meshes to be processed, save as STL files
18+
for i=0:3
19+
% vary radius using i for one of the ellipsoids directions
20+
[x,y,z]=ellipsoid(0,0,0,50+i*10,50,50);
21+
fv = surf2patch(x, y, z, 'triangles');
22+
TR = triangulation(fv.faces, fv.vertices);
23+
stlwrite(TR, sprintf('ellipsoid_%02d.stl', i));
24+
end
25+
26+
27+
% Define the list of shapes to be processed
28+
filenames = ArrayList();
29+
filenames.add('ellipsoid_00.stl');
30+
filenames.add('ellipsoid_01.stl');
31+
filenames.add('ellipsoid_02.stl');
32+
filenames.add('ellipsoid_03.stl');
33+
project.setShapes(filenames);
34+
35+
% Define grooming parameters (using HashMap)
36+
groomingParameters = HashMap();
37+
groomingParameters.put('alignment_enabled', 'true');
38+
groomingParameters.put('alignment_method', 'Center');
39+
% Add more parameters as needed
40+
project.setGroomingParameters(groomingParameters);
41+
42+
% Define optimization parameters (using HashMap)
43+
optimizationParameters = HashMap();
44+
optimizationParameters.put('number_of_particles', '128');
45+
optimizationParameters.put('initial_relative_weighting', '0.05');
46+
% Add more parameters as needed
47+
project.setOptimizationParameters(optimizationParameters);
48+
49+
% Save the project
50+
disp('Saving project...');
51+
project.save('project.swproj');
52+
53+
% Demonstrate loading the project
54+
disp('Loading project...');
55+
project.load('project.swproj');
56+
57+
% Run the groom, optimize, and analyze steps
58+
disp('Running groom...');
59+
project.groom();
60+
disp('Running optimize...');
61+
project.optimize();
62+
disp('Opening ShapeWorksStudio...');
63+
project.analyze();

Examples/Matlab/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See http://sciinstitute.github.io/ShapeWorks/latest/java/matlab.html

Java/ShapeWorksProject.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package edu.utah.sci.shapeworks;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class ShapeWorksProject {
7+
private List<String> shapes;
8+
private Map<String, String> groomingParameters;
9+
private Map<String, String> optimizationParameters;
10+
11+
public void setShapes(List<String> filenames) {
12+
this.shapes = filenames;
13+
}
14+
15+
public void setGroomingParameters(Map<String, String> groomingParameters) {
16+
this.groomingParameters = groomingParameters;
17+
}
18+
19+
public void setOptimizationParameters(Map<String, String> optimizationParameters) {
20+
this.optimizationParameters = optimizationParameters;
21+
}
22+
23+
public void save(String filename) {
24+
try (PrintWriter writer = new PrintWriter(new FileOutputStream(filename))) {
25+
writer.println("{");
26+
writer.println("\"data\": [");
27+
for (int i = 0; i < shapes.size(); i++) {
28+
writer.println(
29+
" {\"shape_" + (i + 1) + "\": \"" + shapes.get(i) + "\"}" + (i < shapes.size() - 1 ? "," : ""));
30+
}
31+
writer.println("],");
32+
writer.println("\"groom\": {");
33+
writer.println(" \"1\": {");
34+
int i = 0;
35+
for (Map.Entry<String, String> entry : groomingParameters.entrySet()) {
36+
writer.println(" \"" + entry.getKey() + "\": \"" + entry.getValue() + "\""
37+
+ ((i < groomingParameters.size() - 1) ? "," : ""));
38+
i++;
39+
}
40+
writer.println(" }");
41+
writer.println("},");
42+
writer.println("\"optimize\": {");
43+
i = 0;
44+
for (Map.Entry<String, String> entry : optimizationParameters.entrySet()) {
45+
writer.println(" \"" + entry.getKey() + "\": \"" + entry.getValue() + "\""
46+
+ ((i < optimizationParameters.size() - 1) ? "," : ""));
47+
i++;
48+
}
49+
writer.println("},");
50+
writer.println("\"project\": {");
51+
writer.println(" \"version\": \"2\"");
52+
writer.println("}");
53+
writer.println("}");
54+
} catch (IOException e) {
55+
e.printStackTrace();
56+
}
57+
}
58+
59+
public void load(String filename) {
60+
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
61+
StringBuilder jsonBuilder = new StringBuilder();
62+
String line;
63+
while ((line = reader.readLine()) != null) {
64+
jsonBuilder.append(line.trim());
65+
}
66+
String json = jsonBuilder.toString();
67+
68+
// Basic JSON parsing (Note: This is a simplistic parser; real-world scenarios
69+
// should use a complete library)
70+
this.shapes = new ArrayList<>();
71+
this.groomingParameters = new HashMap<>();
72+
this.optimizationParameters = new HashMap<>();
73+
74+
// Parse data
75+
int dataStart = json.indexOf("\"data\": [") + 8;
76+
int dataEnd = json.indexOf("]", dataStart);
77+
String dataContent = json.substring(dataStart, dataEnd);
78+
String[] dataEntries = dataContent.split("},\\s*\\{");
79+
for (String entry : dataEntries) {
80+
shapes.add(entry.split(":")[1].replace("\"", "").replace("}", "").replace("{", "").trim());
81+
}
82+
83+
// Parse groom
84+
int groomStart = json.indexOf("\"groom\": {") + 10;
85+
int groomEnd = json.indexOf("}", groomStart) + 1;
86+
String groomContent = json.substring(groomStart, groomEnd);
87+
String[] groomEntries = groomContent.split(",");
88+
for (String entry : groomEntries) {
89+
String[] keyValue = entry.split(":");
90+
if (keyValue.length == 2) {
91+
groomingParameters.put(keyValue[0].replace("\"", "").trim(), keyValue[1].replace("\"", "").trim());
92+
}
93+
}
94+
// Parse optimize
95+
int optimizeStart = json.indexOf("\"optimize\": {") + 12;
96+
int optimizeEnd = json.indexOf("}", optimizeStart);
97+
String optimizeContent = json.substring(optimizeStart, optimizeEnd);
98+
String[] optimizeEntries = optimizeContent.split(",");
99+
for (String entry : optimizeEntries) {
100+
String[] keyValue = entry.split(":");
101+
if (keyValue.length == 2) {
102+
optimizationParameters.put(keyValue[0].replace("\"", "").trim(),
103+
keyValue[1].replace("\"", "").trim());
104+
}
105+
}
106+
} catch (IOException e) {
107+
e.printStackTrace();
108+
}
109+
}
110+
111+
public void groom() {
112+
runSystemCommand("shapeworks groom --name project.swproj");
113+
}
114+
115+
public void optimize() {
116+
runSystemCommand("shapeworks optimize --name project.swproj");
117+
}
118+
119+
public void analyze() {
120+
runSystemCommand("shapeworksstudio project.swproj");
121+
}
122+
123+
private void runSystemCommand(String command) {
124+
try {
125+
Process process = Runtime.getRuntime().exec(command);
126+
process.waitFor();
127+
} catch (IOException | InterruptedException e) {
128+
e.printStackTrace();
129+
}
130+
}
131+
}

docs/java/java.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Using the ShapeWorks Java Interface
2+
3+
This example Java program demonstrates how to use the ShapeWorks Java interface to perform the following tasks:
4+
5+
1. Create a ShapeWorks project
6+
2. Add a shape to the project
7+
3. Set grooming parameters
8+
4. Set correspondence parameters
9+
5. Save/Load the project
10+
6. Run the ShapeWorks grooming pipeline
11+
7. Run the ShapeWorks correspondence optimization
12+
8. Launch the ShapeWorks Analysis GUI
13+
14+
# Building the ShapeWorks Java Interface
15+
16+
The ShapeWorks Java interface is built using the Java Compiler. To build it:
17+
18+
```
19+
cd shapeworks/Java
20+
javac -d . *.java
21+
```
22+
23+
This will place the compiled classes in the `shapeworks/Java` directory.
24+
25+
# Building the Example Java Program
26+
27+
```
28+
cd shapeworks/Examples/Java
29+
javac -cp ../../Java *.java
30+
```
31+
32+
# Running the Example Java Program
33+
34+
Note: The example Java program assumes that the ShapeWorks executable is in $PATH. Ensure that you can run `shapeworks` from the command line before running the example Java program.
35+
36+
```
37+
java -cp ../../Java:. JavaExample
38+
```
39+
40+
This will run the example Java program. The output should look like this:
41+
42+
```
43+
Saving project...
44+
Loading project...
45+
Running groom...
46+
Running optimize...
47+
Opening ShapeWorksStudio...
48+
```
49+
50+
And the ShapeWorks Analysis GUI will open.
51+
52+
From here you can change to the analysis module and examine the shape model.
53+
54+
![Studio Java Example](java_example.png){: width="600" }
55+
56+

docs/java/java_example.png

902 KB
Loading

docs/java/matlab.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Using the ShapeWorks Matlab Interface
2+
3+
This example Matlab program demonstrates how to use the ShapeWorks Matlab interface via Java to perform the following tasks:
4+
5+
1. Create a ShapeWorks project
6+
2. Generate Ellipsoids
7+
3. Add shapes to the project
8+
4. Set grooming parameters
9+
5. Set correspondence parameters
10+
6. Save/Load the project
11+
7. Run the ShapeWorks grooming pipeline
12+
8. Run the ShapeWorks correspondence optimization
13+
9. Launch the ShapeWorks Analysis GUI
14+
15+
# Building the ShapeWorks Java Interface
16+
17+
The ShapeWorks Java interface is built using the Java Compiler. To build it:
18+
19+
```
20+
cd shapeworks/Java
21+
javac -d . *.java
22+
```
23+
24+
This will place the compiled classes in the `shapeworks/Java` directory.
25+
26+
# Building the Example Java Program
27+
28+
```
29+
cd shapeworks/Examples/Java
30+
javac -cp ../../Java *.java
31+
```
32+
33+
# Running the Example Matlab Program
34+
35+
Note: The example Matlab program uses the Java interface and needs the paths to the ShapeWorks executables. Ensure that you update the paths in the start of the script.
36+
37+
# Running the Example Matlab Program
38+
39+
From the Matlab terminal, run the following:
40+
41+
```
42+
cd shapeworks/Examples/Matlab
43+
javaaddpath('../../Java')
44+
MatlabExample
45+
```
46+
47+
![Matlab Example](matlab_example1.png){: width="600" }
48+
49+
50+
![Studio Matlab Example](matlab_example2.png){: width="600" }
51+
52+

docs/java/matlab_example1.png

1.12 MB
Loading

docs/java/matlab_example2.png

983 KB
Loading

mkdocs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ nav:
131131
- Classes: 'api/Classes/index_classes.md'
132132
- Files: 'api/Files/index_files.md'
133133

134+
- Java/Matlab Interface:
135+
- Java Interface: 'java/java.md'
136+
- Matlab Interface: 'java/matlab.md'
137+
134138
- About:
135139
- 'Meet ShapeWorkers!': 'about/team.md'
136140
- 'Release Notes': 'about/release-notes.md'

0 commit comments

Comments
 (0)