diff --git a/src/main/java/org/mujoco/MuJoCoModelManager.java b/src/main/java/org/mujoco/MuJoCoModelManager.java new file mode 100644 index 0000000..b518114 --- /dev/null +++ b/src/main/java/org/mujoco/MuJoCoModelManager.java @@ -0,0 +1,72 @@ +package org.mujoco; + +import java.io.File; + +import org.mujoco.MuJoCoLib.mjData; +import org.mujoco.MuJoCoLib.mjData_; +import org.mujoco.MuJoCoLib.mjModel; +import org.mujoco.MuJoCoLib.mjModel_; +import org.mujoco.MuJoCoLib.mjVFS; + +public class MuJoCoModelManager { + MuJoCoLib lib = new MuJoCoLib(); + + private mjModel m; + private mjData d; + private mjModel_ maccessable; + private mjData_ daccessable; + + public MuJoCoModelManager(File config){ + byte[] error = new byte[1000]; + int error_sz = 0; + m = MuJoCoLib.mj_loadXML( + config.getAbsolutePath(),(mjVFS) null, error, + error_sz); + if(m==null) + throw new RuntimeException("Model File Failed to load "+new String(error)); + System.out.println("Humanoid model loaded " + m); + d = MuJoCoLib.mj_makeData(m); + setModel(new mjModel_(m)); + setData(new mjData_(d)); + } + + public void close() { + MuJoCoLib.mj_deleteData(d); + MuJoCoLib.mj_deleteModel(m); + } + + /** + * @return the maccessable + */ + public mjModel_ getModel() { + return maccessable; + } + + /** + * @param maccessable the maccessable to set + */ + private void setModel(mjModel_ maccessable) { + this.maccessable = maccessable; + } + + /** + * @return the daccessable + */ + public mjData_ getData() { + return daccessable; + } + + /** + * @param daccessable the daccessable to set + */ + public void setData(mjData_ daccessable) { + this.daccessable = daccessable; + } + + public void stepOne() { + MuJoCoLib.mj_step1(m, d); + } + public void stepTwo() { + MuJoCoLib.mj_step2(m, d); + } +} diff --git a/src/test/java/mujoco/java/MuJoColibTest.java b/src/test/java/mujoco/java/MuJoColibTest.java index 8bd8991..7577023 100755 --- a/src/test/java/mujoco/java/MuJoColibTest.java +++ b/src/test/java/mujoco/java/MuJoColibTest.java @@ -10,9 +10,11 @@ import org.mujoco.MuJoCoLib.mjModel; import org.mujoco.MuJoCoLib.mjModel_; import org.mujoco.MuJoCoLib.mjVFS; +import org.mujoco.MuJoCoModelManager; import static org.junit.Assert.*; +import java.io.File; import java.util.logging.Level; import java.util.logging.Logger; @@ -25,34 +27,28 @@ public class MuJoColibTest { public void mujocoJNILoadTest() { System.out.println(System.getProperty("org.bytedeco.javacpp.logger.debug")); System.setProperty("org.bytedeco.javacpp.logger.debug", "true"); - MuJoCoLib lib = new MuJoCoLib(); System.out.println("Starting " + MuJoCoLib.mj_versionString().getString()); - byte[] error = new byte[100]; - int error_sz = 0; - mjModel m = MuJoCoLib.mj_loadXML( - "/home/hephaestus/git/mujoco-java/src/main/resources/mujoco/java/humanoid/humanoid.xml", null, error, - error_sz); - System.out.println("Humanoid model loaded " + m); - mjData d = MuJoCoLib.mj_makeData(m); + + MuJoCoModelManager manager = new MuJoCoModelManager( + new File("/home/hephaestus/git/mujoco-java/src/main/resources/mujoco/java/humanoid/humanoid.xml")); + try { - mjModel_ Maccessable = new mjModel_(m); - try (mjData_ accessable = new mjData_(d)) { - System.out.println("Run model for 10 seconds"); - while (accessable.time() < 10) { - MuJoCoLib.mj_step(m, d); - Thread.sleep(1); - - } + mjModel_ Maccessable = manager.getModel(); + mjData_ accessable = manager.getData(); + System.out.println("Run model for 10 seconds"); + while (accessable.time() < 10) { + manager.stepOne(); + manager.stepTwo(); + Thread.sleep(1); } + } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Clean up data objects"); - - MuJoCoLib.mj_deleteData(d); - MuJoCoLib.mj_deleteModel(m); + manager.close(); } }