Page 1 of 1

Scale Tool: Manually setting marker weights (3.3)

Posted: Fri Oct 26, 2018 8:58 am
by p4t0u
Hi,

I've been trying to make the scale tool work within my c++ code.

I've managed to make a simple example work by feeding an OpenSim generated scale file (.xml) to the Scale Tool. This scale file loads the .trc and applies correctly to the .osim model.

Now, I had to remove that .xml scale file dependency and replace it with extra code, which essentially just sets weights for every marker:

Code: Select all

	
	OpenSim::Set<OpenSim::MarkerWeight> markerWeights;
	markerWeights.setSize(3);
	scaleTool.getMarkerPlacer().getIKTaskSet().createMarkerWeightSet(markerWeights);
	
	OpenSim::MarkerWeight markerWeight1;
	markerWeight.setName("Marker1");
	markerWeight.setWeight(1);
	markerWeights.set(0, markerWeight1, false);
	
	OpenSim::MarkerWeight markerWeight2;
	markerWeight.setName("Marker2");
	markerWeight.setWeight(2);
	markerWeights.set(1, markerWeight2, false);

	OpenSim::MarkerWeight markerWeight3;
	markerWeight.setName("Marker3");
	markerWeight.setWeight(2);
	markerWeights.set(2, markerWeight3, false);
... but when I run

Code: Select all

scaleTool.getMarkerPlacer().processModel(myState, &myModel, folder_containing_files);
, I get the following error:

Code: Select all

AssemblySolver::assemble() Failed: SimTK Exception thrown at assembler.cpp:227:
  Error detected by Simbody method AssemblerSystem::objectiveFunc(): calcGoal() method of assembly condition Markers returned a negative or non-finite value -nan(ind).
  (Required condition 'goalValue >= 0' was not met.)
  
I tried to load the exact same model + .trc file inside OpenSim's Scale Tool (without specifying a .xml scale file) and it runs! Is there anything more I need to do?

Re: Scale Tool: Manually setting marker weights (3.3)

Posted: Fri Oct 26, 2018 9:16 am
by p4t0u
Here is my entire code:

Code: Select all

	Model myModel = Model::Model(osimFullFilePath, true);
	State myState = myModel.initSystem();
	
	ScaleTool scaleTool = ScaleTool();
	scaleTool.getGenericModelMaker().setModelFileName(osimFullFilePath);
	
	Array <double> TimeArray;
	TimeArray.set(0, 0.0);
	TimeArray.set(1, 0.0);
	
	scaleTool.getModelScaler().setApply(1);
	scaleTool.getModelScaler().setMarkerFileName("marker.trc");
	scaleTool.getModelScaler().setOutputScaleFileName("scale_result.xml");
	
	scaleTool.getModelScaler().processModel(myState, &myModel, osimFolderPath, 0); // This executes
	
	scaleTool.getMarkerPlacer().setApply(1);
	scaleTool.getMarkerPlacer().setTimeRange(TimeArray);
	scaleTool.getMarkerPlacer().setStaticPoseFileName("marker.trc");
	scaleTool.getMarkerPlacer().setOutputModelFileName("scaledModel.osim");
	scaleTool.getMarkerPlacer().setOutputMotionFileName("outputmotion.mot");
	scaleTool.setPrintResultFiles(true);
	scaleTool.getMarkerPlacer().setMoveModelMarkers(true);
	
	// Weights
	OpenSim::Set<OpenSim::MarkerWeight> markerWeights;
	markerWeights.setSize(3);
	scaleTool.getMarkerPlacer().getIKTaskSet().createMarkerWeightSet(markerWeights);
	
	OpenSim::MarkerWeight markerWeight1;
	markerWeight1.setName("Marker1");
	markerWeight1.setWeight(1);
	markerWeights.set(0, markerWeight1, false);
	
	OpenSim::MarkerWeight markerWeight2;
	markerWeight2.setName("Marker2");
	markerWeight2.setWeight(1);
	markerWeights.set(1, markerWeight2, false);

	OpenSim::MarkerWeight markerWeight3;
	markerWeight3.setName("Marker3");
	markerWeight3.setWeight(1);
	markerWeights.set(2, markerWeight3, false);
	
	
	scaleTool.getMarkerPlacer().processModel(myState, &myModel, osimFolderPath); // This crashes

Re: Scale Tool: Manually setting marker weights (3.3)

Posted: Sun Oct 28, 2018 6:56 pm
by p4t0u
My code works if I instantiate the ScaleTool class using the overloaded constructor that asks for a .xml scale file. Are there any methods I can use to generate that file in C++?

Re: Scale Tool: Manually setting marker weights (3.3)

Posted: Sun Oct 28, 2018 7:24 pm
by tkuchida
My code works if I instantiate the ScaleTool class using the overloaded constructor that asks for a .xml scale file. Are there any methods I can use to generate that file in C++?
print(). MATLAB example:

Code: Select all

myScaleTool = ScaleTool();
myScaleTool.print('setup_scale.xml');