11package org .mujoco ;
22
33import java .io .File ;
4- import java .io .FileOutputStream ;
5- import java .io .IOException ;
6- import java .io .InputStream ;
7- import java .net .URL ;
4+ import java .util .HashMap ;
85
96import org .bytedeco .javacpp .BytePointer ;
7+ import org .bytedeco .javacpp .DoublePointer ;
108import org .bytedeco .javacpp .IntPointer ;
119import org .mujoco .MuJoCoLib .mjData ;
1210import org .mujoco .MuJoCoLib .mjData_ ;
1311import org .mujoco .MuJoCoLib .mjModel ;
1412import org .mujoco .MuJoCoLib .mjModel_ ;
1513import org .mujoco .MuJoCoLib .mjOption_ ;
16- import org .mujoco .MuJoCoLib .mjVFS ;
1714
1815public class MuJoCoModelManager {
1916 MuJoCoLib lib = new MuJoCoLib ();
@@ -30,16 +27,11 @@ public class MuJoCoModelManager {
3027 private IntPointer jointNameIndexes ;
3128
3229 private IntPointer bodyNameIndex ;
30+ private boolean connected =false ;
3331 public MuJoCoModelManager (File config ){
3432 loadFromFile (config );
3533 }
36- public MuJoCoModelManager (String jarpath ) throws IOException {
37- File config = new File ("model/" +jarpath );
3834
39- System .out .println ("Searching for models in " +config .getAbsolutePath ());
40- loadFromFile (config );
41-
42- }
4335 private void loadFromFile (File config ) {
4436 if (!config .exists ())
4537 throw new RuntimeException ("Config File does not exist " +config );
@@ -57,14 +49,22 @@ private void loadFromFile(File config) {
5749 setModelNames (model .names ());
5850 jointNameIndexes = model .name_jntadr ();
5951 bodyNameIndex = model .name_bodyadr ();
52+ connected =true ;
53+ }
54+ private void check () {
55+ if (!connected )
56+ throw new RuntimeException ("MuJoCo Model may not be accessed when disconnected" );
6057 }
6158 public double getCurrentSimulationTimeSeconds () {
59+ check ();
6260 return data .time ();
6361 }
6462 public int getNumberOfJoints () {
63+ check ();
6564 return model .njnt ();
6665 }
6766 public String getJointName (int i ) {
67+ check ();
6868 if (i <0 )
6969 throw new IndexOutOfBoundsException ("Joint index must be positive or zero" );
7070 if (i >=getNumberOfJoints ()) {
@@ -74,9 +74,15 @@ public String getJointName(int i) {
7474 return byp .getString ();
7575 }
7676 public int getNumberOfBodys () {
77+ check ();
7778 return model .nbody ();
7879 }
80+ public int getNumberOfGeometrys () {
81+ check ();
82+ return model .ngeom ();
83+ }
7984 public String getBodyName (int i ) {
85+ check ();
8086 if (i <0 )
8187 throw new IndexOutOfBoundsException ("Body index must be positive or zero" );
8288 if (i >=getNumberOfBodys ()) {
@@ -86,14 +92,106 @@ public String getBodyName(int i) {
8692 return byp .getString ();
8793 }
8894
95+ public String getGeometryName (int i ) {
96+ check ();
97+ if (i <0 )
98+ throw new IndexOutOfBoundsException ("Body index must be positive or zero" );
99+ if (i >=getNumberOfBodys ()) {
100+ throw new IndexOutOfBoundsException ("Body index must be less than " +i );
101+ }
102+ IntPointer GeomIndex = model .name_geomadr ();
103+ BytePointer byp = modelNames .getPointer (GeomIndex .getPointer (i ).get ());
104+ return byp .getString ();
105+ }
106+
107+ /**
108+ *
109+ * @param cartesianPositions pointer to positions
110+ * @param cartesianQuaturnions pointer to quaturnions
111+ * @param i index within the pointer space
112+ * @return cartesian pose, Xm, Ym, Zm, QuatW, QuatX, QuatY, QuatZ
113+ */
114+ double [] convert (DoublePointer cartesianPositions ,DoublePointer cartesianQuaturnions , int i ) {
115+ check ();
116+ DoublePointer coords =cartesianPositions .getPointer (i *3 );
117+ double x = coords .getPointer (0 ).get ();
118+ double y = coords .getPointer (1 ).get ();
119+ double z = coords .getPointer (2 ).get ();
120+
121+ DoublePointer quat =cartesianQuaturnions .getPointer (i *4 );
122+ double qw = quat .getPointer (0 ).get ();
123+ double qx = quat .getPointer (1 ).get ();
124+ double qy = quat .getPointer (2 ).get ();
125+ double qz = quat .getPointer (3 ).get ();
126+
127+ return new double [] {x ,y ,z ,qw , qx , qy , qz };
128+ }
129+ private HashMap <String , Integer > bodyNameIndexMap = null ;
130+ private HashMap <String , Integer > geometryNameIndexMap = null ;
131+
132+ public int getBodyIndex (String name ) {
133+ check ();
134+ if (bodyNameIndexMap ==null ) {
135+ bodyNameIndexMap =new HashMap <>();
136+ for (int i =0 ;i <getNumberOfBodys ();i ++) {
137+ bodyNameIndexMap .put (getBodyName (i ), i );
138+ }
139+ }
140+ Integer i = bodyNameIndexMap .get (name );
141+ if (i !=null )
142+ return i ;
143+ throw new RuntimeException ("Body named " +name +" not found" );
144+ }
145+
146+
147+ public int getGeometryIndex (String name ) {
148+ check ();
149+ if (geometryNameIndexMap ==null ) {
150+ geometryNameIndexMap =new HashMap <>();
151+ for (int i =0 ;i <getNumberOfGeometrys ();i ++) {
152+ geometryNameIndexMap .put (getGeometryName (i ), i );
153+ }
154+ }
155+ Integer i = geometryNameIndexMap .get (name );
156+ if (i !=null )
157+ return i ;
158+ throw new RuntimeException ("Geometry named " +name +" not found" );
159+ }
160+
161+ /**
162+ *
163+ * @param name of a body
164+ * @return cartesian pose, Xm, Ym, Zm, QuatW, QuatX, QuatY, QuatZ
165+ */
166+ public double [] getBodyPose (String name ) {
167+ DoublePointer cartesianQuaturnions = data .xquat ();
168+ DoublePointer cartesianPositions = data .xpos ();
169+ return convert (cartesianPositions ,cartesianQuaturnions ,getBodyIndex (name ));
170+ }
171+ /**
172+ *
173+ * @param name of a geometry
174+ * @return cartesian pose, Xm, Ym, Zm, QuatW, QuatX, QuatY, QuatZ
175+ */
176+ public double [] getGeometryPose (String name ) {
177+ DoublePointer geomPos = model .geom_pos ();
178+ DoublePointer geomQuat = model .geom_quat ();
179+ return convert (geomPos ,geomQuat ,getGeometryIndex (name ));
180+ }
181+
182+ public String getBodyNameOfAGeometry (String geomName ) {
183+
184+ }
89185
90186 public double getTimestepSeconds () {
187+
91188 return getOpt ().timestep ();
92189 }
93190 public long getTimestepMilliSeconds () {
94191 return (long )(getTimestepSeconds ()*1000 );
95192 }
96193 public void close () {
194+ connected =false ;
97195 MuJoCoLib .mj_deleteData (d );
98196 MuJoCoLib .mj_deleteModel (m );
99197 }
@@ -116,6 +214,7 @@ private void setModel(mjModel_ maccessable) {
116214 * @return the daccessable
117215 */
118216 public mjData_ getData () {
217+ check ();
119218 return data ;
120219 }
121220
@@ -141,6 +240,7 @@ public void stepTwo() {
141240 * @return the opt
142241 */
143242 public mjOption_ getOpt () {
243+ check ();
144244 return opt ;
145245 }
146246 /**
@@ -165,12 +265,13 @@ public void setController(IMujocoController controller) {
165265 * @return the modelNames
166266 */
167267 public BytePointer getModelNames () {
268+ check ();
168269 return modelNames ;
169270 }
170271 /**
171272 * @param modelNames the modelNames to set
172273 */
173- public void setModelNames (BytePointer modelNames ) {
274+ private void setModelNames (BytePointer modelNames ) {
174275 this .modelNames = modelNames ;
175276 }
176277}
0 commit comments