Skip to content

Commit

Permalink
Merge pull request #63 from opensim-org/visualize_multiple_models
Browse files Browse the repository at this point in the history
Visualize multiple models
  • Loading branch information
aymanhab authored Feb 21, 2017
2 parents 447b82d + b650c88 commit 1db6113
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.opensim.modeling.Model;
import org.opensim.view.pub.OpenSimDB;
import org.opensim.utils.FileUtils;
import org.opensim.view.pub.ViewDB;

@ActionID(
category = "File",
Expand All @@ -29,39 +30,39 @@
displayName = "#CTL_ExportSceneToThreeJsAction"
)
@ActionReference(path = "Menu/File", position = 1429)
@Messages("CTL_ExportSceneToThreeJsAction=Export model to json")
@Messages("CTL_ExportSceneToThreeJsAction=Export all models to json format")
public final class ExportSceneToThreeJsAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {

Model model = OpenSimDB.getInstance().getCurrentModel();
if (model == null) return; // Nothing to export
String fileName = FileUtils.getInstance().browseForFilenameToSave(
FileUtils.getFileFilter(".json", "Threejs scene file"),
true, model.getInputFileName().replace(".osim", ".json"));

exportCurrentModelToJson(fileName, model);
exportAllModelsToJson(fileName);
}

public static VisualizationJson exportCurrentModelToJson(String fileName, Model model) {
public static ModelVisualizationJson exportAllModelsToJson(String fileName) {
BufferedWriter out = null;
VisualizationJson vizJson = null;
ModelVisualizationJson vizJson = null;
try {

JSONObject jsonTop = ViewDB.getInstance().getJsondb();
int numModels = OpenSimDB.getInstance().getNumModels();
// Create Json rep for model
vizJson = new VisualizationJson(model);
JSONObject jsonTop = vizJson.getJson();
StringWriter outString = new JSONWriter();
jsonTop.writeJSONString(outString);
String jsonText = outString.toString();
out = new BufferedWriter(new FileWriter(fileName, false));
out.write(jsonText);
out.flush();
out.close();
for (int i=0; i<numModels; i++){
Model model = OpenSimDB.getInstance().getModelByIndex(i);
vizJson = new ModelVisualizationJson(jsonTop, model);
ViewDB.getInstance().addModelVisuals(model, vizJson);
}
JSONUtilities.writeJsonFile(jsonTop, fileName);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} finally {
}
return vizJson;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static String convertLocationStringToPropertyFormat(JSONObject jsonObject) {
JSONObject positionObj = (JSONObject) jsonObject.get("position");
String returnString = "";
double x, y, z;
double relativeScale = VisualizationJson.getVisScaleFactor();
double relativeScale = ModelVisualizationJson.getVisScaleFactor();
x = (Double) positionObj.get("x")/relativeScale;
returnString = returnString.concat(String.valueOf(x));
returnString = returnString.concat(" ");
Expand Down
47 changes: 47 additions & 0 deletions Gui/opensim/view/src/org/opensim/threejs/JSONUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

package org.opensim.threejs;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.UUID;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.opensim.modeling.Rotation;
Expand All @@ -18,6 +23,23 @@
*/
public class JSONUtilities {

static public JSONObject createTopLevelJson() {
JSONObject topLevelJson = new JSONObject();
topLevelJson.put("geometries", new JSONArray());
topLevelJson.put("materials", new JSONArray());
/*
JSONObject models_json = new JSONObject();
models_json.put("uuid", UUID.randomUUID().toString());
models_json.put("type", "Group");
models_json.put("name", "Models");
//System.out.println(model_json.toJSONString());
JSONArray models_json_arr = new JSONArray();
models_json.put("children", models_json_arr);
topLevelJson.put("object", models_json);
*/
return topLevelJson;
}

static String mapColorToRGBA(Vec3 color) {
int r = (int) (color.get(0) * 255);
int g = (int) (color.get(1) * 255);
Expand Down Expand Up @@ -52,5 +74,30 @@ static JSONArray createMatrixFromTransform(Transform xform, Vec3 scaleFactors, d
ret.add(retTransform[i]);
return ret;
}
// Method to remove Json of Model from Scene on Model closing
public static void removeModelJson(JSONObject jsondb, UUID modelUUID) {
// Find models node and remove modelUUID from list of children
JSONObject models_json = ((JSONObject) jsondb.get("object"));
JSONArray models_children = (JSONArray) models_json.get("children");

for (int index = 0; index < models_children.size(); index++){
JSONObject nextModelJson = (JSONObject) models_children.get(index);
String nextModelUUID = (String) nextModelJson.get("uuid");
if (nextModelUUID.equals(modelUUID.toString())){
models_children.remove(index);
break;
}
}
}

public static void writeJsonFile(JSONObject jsonTop, String fileName) throws IOException {
BufferedWriter out;
StringWriter outString = new JSONWriter();
jsonTop.writeJSONString(outString);
out = new BufferedWriter(new FileWriter(fileName, false));
out.write(outString.toString());
out.flush();
out.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/
package org.opensim.threejs;

import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -15,8 +14,6 @@
import java.util.UUID;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.openide.util.Exceptions;
import org.opensim.modeling.ArrayDecorativeGeometry;
import org.opensim.modeling.BodyList;
import org.opensim.modeling.Body;
Expand All @@ -42,9 +39,8 @@
*
* @author Ayman
*/
public class VisualizationJson {
public class ModelVisualizationJson extends JSONObject {
private State state;
private final JSONObject topJson;
private final HashMap<Integer, PhysicalFrame> mapBodyIndicesToFrames = new HashMap<Integer, PhysicalFrame>();
private final HashMap<Integer, JSONObject> mapBodyIndicesToJson = new HashMap<Integer, JSONObject>();
private final static double visScaleFactor = 1000.0;
Expand All @@ -59,39 +55,41 @@ public class VisualizationJson {
private ModelDisplayHints mdh;
private DecorativeGeometryImplementationJS dgimp = null;
private static String boneSuffix = "_Bone";
private JSONArray json_geometries;
private JSONArray json_materials;
private JSONObject model_object;
private UUID modelUUID;

public VisualizationJson(Model model) {
topJson = createJsonForModel(model);
public ModelVisualizationJson(JSONObject jsonTopIn, Model model) {
// implicit super()
createModelJsonNode(); // Model node
createJsonForModel(model);
}
private JSONObject createJsonForModel(Model model) {
private void createJsonForModel(Model model) {
state = model.getWorkingState();
mdh = model.getDisplayHints();
ComponentsList mcList = model.getComponentsList();
muscleList = model.getMuscleList();
ComponentIterator mcIter = mcList.begin();
// Load template
JSONObject jsonTop = createTopLevelJson(model);

BodyList bodies = model.getBodyList();
BodyIterator body = bodies.begin();
mapBodyIndicesToFrames.put(0, model.getGround());

JSONArray json_geometries = (JSONArray) jsonTop.get("geometries");
JSONArray json_materials = (JSONArray) jsonTop.get("materials");
JSONObject sceneObject = (JSONObject) jsonTop.get("object");
JSONArray json_scene_children = (JSONArray) sceneObject.get("children");
JSONArray json_model_children = (JSONArray) ((JSONObject) get("object")).get("children");

JSONObject model_json = new JSONObject();
json_scene_children.add(model_json);
JSONObject model_ground_json = new JSONObject();
// create model node
model_json.put("uuid", UUID.randomUUID().toString());
model_json.put("type", "Group");
model_json.put("opensimtype", "Frame");
model_json.put("name", model.getGround().getAbsolutePathName());
model_json.put("model_ground", true);
model_ground_json.put("uuid", UUID.randomUUID().toString());
model_ground_json.put("type", "Group");
model_ground_json.put("opensimtype", "Frame");
model_ground_json.put("name", model.getGround().getAbsolutePathName());
model_ground_json.put("model_ground", true);
json_model_children.add(model_ground_json);
//System.out.println(model_json.toJSONString());
JSONArray bodies_json = new JSONArray();
model_json.put("children", bodies_json);
mapBodyIndicesToJson.put(0, model_json);
model_ground_json.put("children", bodies_json);
mapBodyIndicesToJson.put(0, model_ground_json);
while (!body.equals(bodies.end())) {
int id = body.getMobilizedBodyIndex();
mapBodyIndicesToFrames.put(id, body.__deref__());
Expand Down Expand Up @@ -124,7 +122,6 @@ private JSONObject createJsonForModel(Model model) {
}
mcIter.next();
}
return jsonTop;
}

private void processDecorativeGeometry(ArrayDecorativeGeometry adg, Component comp,
Expand Down Expand Up @@ -162,23 +159,21 @@ private void processDecorativeGeometry(ArrayDecorativeGeometry adg, Component co

}

private JSONObject createTopLevelJson(Model model) {
JSONObject topLevelJson = new JSONObject();
JSONObject modelJson = new JSONObject();
createOneModelJson(modelJson, model);
topLevelJson.put("object", modelJson);
topLevelJson.put("geometries", new JSONArray());
topLevelJson.put("materials", new JSONArray());
return topLevelJson;
}
private void createModelJsonNode() {
modelUUID = UUID.randomUUID();
model_object = new JSONObject();
model_object.put("uuid", modelUUID.toString());
model_object.put("type", "Group");
model_object.put("opensimtype", "Model");
model_object.put("name", "OpenSimModel");
model_object.put("children", new JSONArray());
model_object.put("matrix", JSONUtilities.createMatrixFromTransform(new Transform(), new Vec3(1.), 1.0));
put("object", model_object);
json_geometries = new JSONArray();
put("geometries", json_geometries);
json_materials = new JSONArray();
put("materials", json_materials);

private void createOneModelJson(JSONObject modelJson, Model model) {
modelJson.put("uuid", UUID.randomUUID().toString());
modelJson.put("type", "Model");
modelJson.put("opensimtype", "Model");
modelJson.put("name", "OpenSimModel");
modelJson.put("matrix", JSONUtilities.createMatrixFromTransform(new Transform(), new Vec3(1.), 1.0));
modelJson.put("children", new JSONArray());
}

private UUID addtoFrameJsonObject(DecorativeGeometry dg, String geomName, UUID uuid, UUID uuid_mat, JSONArray mobody_objects) {
Expand All @@ -193,15 +188,7 @@ private UUID addtoFrameJsonObject(DecorativeGeometry dg, String geomName, UUID u
obj_json.put("castShadow", false);
mobody_objects.add(obj_json);
return mesh_uuid;
}


/**
* @return the topJson
*/
public JSONObject getJson() {
return topJson;
}
}

private JSONObject createBodyJson(Body body){
JSONObject bdyJson = new JSONObject();
Expand Down Expand Up @@ -285,12 +272,18 @@ public static double getVisScaleFactor() {
return visScaleFactor;
}

public JSONObject createCloseModelJson(Model model) {
public JSONObject createCloseModelJson() {
JSONObject guiJson = new JSONObject();
guiJson.put("UUID", modelUUID.toString());
guiJson.put("Op", "CloseModel");
return guiJson;
}


public JSONObject createOpenModelJson() {
JSONObject guiJson = new JSONObject();
/*
UUID obj_uuid = findUUIDForObject(model);
guiJson.put("UUID", obj_uuid.toString());
guiJson.put("Op", "CloseModel"); */
guiJson.put("UUID", modelUUID.toString());
guiJson.put("Op", "OpenModel");
return guiJson;
}

Expand Down Expand Up @@ -427,4 +420,18 @@ private UUID addPathPointGeometryToParent(PathPoint pathPoint, JSONArray json_ge
children.add(bpptInBodyJson);
return ppoint_uuid;
}

/**
* @return the modelUUID
*/
public UUID getModelUUID() {
return modelUUID;
}

public JSONObject createSetCurrentModelJson() {
JSONObject guiJson = new JSONObject();
guiJson.put("UUID", modelUUID.toString());
guiJson.put("Op", "SetCurrentModel");
return guiJson;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ public final class VisualizerWindowAction implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
// launch server and open page in fixed location to be used as a view

VisualizationJson json = ExportSceneToThreeJsAction.exportCurrentModelToJson(JettyMain.getServerRootDir()+"/threejs/editor/model.json", OpenSimDB.getInstance().getCurrentModel());
ViewDB.getInstance().startVisualizationServer();
BrowserLauncher.openURL("http://localhost:"+JettyMain.getServerPort()+JettyMain.getPathToStartPage()+"index.html");
ViewDB.getInstance().setJson(json);
// launch server and open page in fixed location to be used as a view
openVisualizerWindow();
}

public static void openVisualizerWindow() {
ViewDB.getInstance().startVisualizationServer();
BrowserLauncher.openURL("http://localhost:"+JettyMain.getServerPort()+JettyMain.getPathToStartPage()+"index.html");
}

}
4 changes: 4 additions & 0 deletions Gui/opensim/view/src/org/opensim/view/pub/OpenSimDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ else if (currentCloseModelDefaultAction ==CloseModelDefaultAction.SAVE)
closeAction="save";
Preferences.userNodeForPackage(TheApp.class).put("DefaultCloseAction", closeAction);
}

public Model getModelByIndex(int i) {
return models.get(i);
}
public enum CloseModelDefaultAction {
SAVE,
DISCARD,
Expand Down
Loading

0 comments on commit 1db6113

Please sign in to comment.