Skip to content

Commit ebacbf7

Browse files
committed
Adding a helper class to make loading in Groovy easier
1 parent 60f5119 commit ebacbf7

2 files changed

Lines changed: 87 additions & 19 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.mujoco;
2+
3+
import java.io.File;
4+
5+
import org.mujoco.MuJoCoLib.mjData;
6+
import org.mujoco.MuJoCoLib.mjData_;
7+
import org.mujoco.MuJoCoLib.mjModel;
8+
import org.mujoco.MuJoCoLib.mjModel_;
9+
import org.mujoco.MuJoCoLib.mjVFS;
10+
11+
public class MuJoCoModelManager {
12+
MuJoCoLib lib = new MuJoCoLib();
13+
14+
private mjModel m;
15+
private mjData d;
16+
private mjModel_ maccessable;
17+
private mjData_ daccessable;
18+
19+
public MuJoCoModelManager(File config){
20+
byte[] error = new byte[1000];
21+
int error_sz = 0;
22+
m = MuJoCoLib.mj_loadXML(
23+
config.getAbsolutePath(),(mjVFS) null, error,
24+
error_sz);
25+
if(m==null)
26+
throw new RuntimeException("Model File Failed to load "+new String(error));
27+
System.out.println("Humanoid model loaded " + m);
28+
d = MuJoCoLib.mj_makeData(m);
29+
setModel(new mjModel_(m));
30+
setData(new mjData_(d));
31+
}
32+
33+
public void close() {
34+
MuJoCoLib.mj_deleteData(d);
35+
MuJoCoLib.mj_deleteModel(m);
36+
}
37+
38+
/**
39+
* @return the maccessable
40+
*/
41+
public mjModel_ getModel() {
42+
return maccessable;
43+
}
44+
45+
/**
46+
* @param maccessable the maccessable to set
47+
*/
48+
private void setModel(mjModel_ maccessable) {
49+
this.maccessable = maccessable;
50+
}
51+
52+
/**
53+
* @return the daccessable
54+
*/
55+
public mjData_ getData() {
56+
return daccessable;
57+
}
58+
59+
/**
60+
* @param daccessable the daccessable to set
61+
*/
62+
public void setData(mjData_ daccessable) {
63+
this.daccessable = daccessable;
64+
}
65+
66+
public void stepOne() {
67+
MuJoCoLib.mj_step1(m, d);
68+
}
69+
public void stepTwo() {
70+
MuJoCoLib.mj_step2(m, d);
71+
}
72+
}

src/test/java/mujoco/java/MuJoColibTest.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import org.mujoco.MuJoCoLib.mjModel;
1111
import org.mujoco.MuJoCoLib.mjModel_;
1212
import org.mujoco.MuJoCoLib.mjVFS;
13+
import org.mujoco.MuJoCoModelManager;
1314

1415
import static org.junit.Assert.*;
1516

17+
import java.io.File;
1618
import java.util.logging.Level;
1719
import java.util.logging.Logger;
1820

@@ -25,34 +27,28 @@ public class MuJoColibTest {
2527
public void mujocoJNILoadTest() {
2628
System.out.println(System.getProperty("org.bytedeco.javacpp.logger.debug"));
2729
System.setProperty("org.bytedeco.javacpp.logger.debug", "true");
28-
MuJoCoLib lib = new MuJoCoLib();
2930

3031
System.out.println("Starting " + MuJoCoLib.mj_versionString().getString());
31-
byte[] error = new byte[100];
32-
int error_sz = 0;
33-
mjModel m = MuJoCoLib.mj_loadXML(
34-
"/home/hephaestus/git/mujoco-java/src/main/resources/mujoco/java/humanoid/humanoid.xml", null, error,
35-
error_sz);
36-
System.out.println("Humanoid model loaded " + m);
37-
mjData d = MuJoCoLib.mj_makeData(m);
32+
33+
MuJoCoModelManager manager = new MuJoCoModelManager(
34+
new File("/home/hephaestus/git/mujoco-java/src/main/resources/mujoco/java/humanoid/humanoid.xml"));
35+
3836
try {
39-
mjModel_ Maccessable = new mjModel_(m);
40-
try (mjData_ accessable = new mjData_(d)) {
41-
System.out.println("Run model for 10 seconds");
42-
while (accessable.time() < 10) {
43-
MuJoCoLib.mj_step(m, d);
44-
Thread.sleep(1);
45-
46-
}
37+
mjModel_ Maccessable = manager.getModel();
38+
mjData_ accessable = manager.getData();
39+
System.out.println("Run model for 10 seconds");
40+
while (accessable.time() < 10) {
41+
manager.stepOne();
42+
manager.stepTwo();
43+
Thread.sleep(1);
4744

4845
}
46+
4947
} catch (Exception e) {
5048
// TODO Auto-generated catch block
5149
e.printStackTrace();
5250
}
5351
System.out.println("Clean up data objects");
54-
55-
MuJoCoLib.mj_deleteData(d);
56-
MuJoCoLib.mj_deleteModel(m);
52+
manager.close();
5753
}
5854
}

0 commit comments

Comments
 (0)