Page 1 of 1

Help Need with addDirToGeometrySearchPaths()

Posted: Tue Jan 21, 2020 5:46 am
by vinaym815
Hello

addDirToGeometrySearchPath() is not working with relative or absolute paths for me. Can you please help me locate the mistake. I am using the following code after initializing the system.

Code: Select all

auto &modelVisualizer = osimModel.updVisualizer();
const std::string geometryDir = "../geometry/";
//const std::string geometryDir = "/home/vinay/Repos/OpenSimExample/OptimizationExample/build/geometry";
modelVisualizer.addDirToGeometrySearchPaths(geometryDir);
The full code is available at https://github.com/vinaym815/OpenSimExa ... n.cpp#L177

Re: Trouble: addDirToGeometrySearchPaths() not working

Posted: Tue Jan 21, 2020 10:04 am
by aymanh
Hi Vinay,

Using relative path makes sense only when you know exactly your working directory (relative to what) otherwise I'd recommend you stay with absolute paths so your code doesn't fail randomly when you run from a different working directory/folder.

Looking at your code, the call to addDirToGeometrySearchPaths must be made before you initialize the model (using initSystem) otherwise it's too late for the paths to be searched and Geometry/mesh filesto be found.

Hope this helps,
-Ayman

Re: Trouble: addDirToGeometrySearchPaths() not working

Posted: Tue Jan 21, 2020 8:45 pm
by vinaym815
aymanh wrote:
Tue Jan 21, 2020 10:04 am
Using relative path makes sense only when you know exactly your working directory (relative to what) otherwise I'd recommend you stay with absolute paths so your code doesn't fail randomly when you run from a different working directory/folder.
Thank you for the advice. I work on different systems which makes it a necessity to use relative paths.
aymanh wrote:
Tue Jan 21, 2020 10:04 am
Looking at your code, the call to addDirToGeometrySearchPaths must be made before you initialize the model (using initSystem) otherwise it's too late for the paths to be searched and Geometry/mesh filesto be found.
Calling osimModel.updVisualizer() before osimModel.initSystem() is not possible https://github.com/opensim-org/opensim- ... del.h#L357.

If I initialize the model once more after adding the directory, the program can find the geometry files, however annoyingly I end up with two visualization windows.

Kind regards
Vinay

Re: Trouble: addDirToGeometrySearchPaths() not working

Posted: Wed Jan 22, 2020 10:16 am
by aymanh
Hi Vinay,

The method to

Code: Select all

addDirToGeometrySearchPaths
is static so you don't need an instance to invoke it.

The order of events would be to addDirToGeometrySearchPaths then instantiate a model and call initSystem on it so that mesh files are found. The visualizer is created within initSystem so if you try to access it before initSystem call, it will throw as expected.

Hope this helps,
-Ayman

Re: Trouble: addDirToGeometrySearchPaths() not working

Posted: Wed Jan 22, 2020 6:44 pm
by vinaym815
aymanh wrote:
Wed Jan 22, 2020 10:16 am
The order of events would be to addDirToGeometrySearchPaths then instantiate a model and call initSystem on it so that mesh files are found. The visualizer is created within initSystem so if you try to access it before initSystem call, it will throw as expected.
Thank you. That was very very very helpful.

Kind Regards
Vinay

Re: Help Need with addDirToGeometrySearchPaths()

Posted: Wed Jan 29, 2020 3:03 am
by idhamari
Here is a complete example to load a model from a file and visualize the simulation, it would be nice if someone add visualizing the motion part

Code: Select all

#include <OpenSim/OpenSim.h>
#include "OpenSim/Common/STOFileAdapter.h"
#include <time.h>
#include <string>
using namespace OpenSim;
using namespace std;
int main(int argc, char* argv[]) {
   std::cout<<"=============================================="<<std::endl;
   std::cout<<"=                           OpenSim Visualizer                                  ="<<std::endl;
   std::cout<<"=============================================="<<std::endl;
    try {
        double finalTime    = 1 ;  //simulation duration
        // change these two lones to be your model path and its geometry folder
        string    inputModelPath               =  "../data/ss.osim"  ;
        string    inputModelGeometryPath =  "../data/geom";

        // create a model from an input file
        Model inputModel(inputModelPath) ;
        inputModel.setUseVisualizer(true);
        inputModel.finalizeConnections();

        // this is a static function, should be called before initializing the system
        ModelVisualizer::addDirToGeometrySearchPaths(inputModelGeometryPath);

        // Initialize the system and get the default state
        SimTK::State& si = inputModel.initSystem();

        // create  visualiser from simody
        SimTK::Visualizer& viz = inputModel.updVisualizer().updSimbodyVisualizer();
        viz.setWindowTitle("OpenSim Visualizer: "+inputModel.getInputFileName());
        viz.setShowSimTime(true);
        viz.setShowFrameNumber(true);
        viz.setBackgroundType(SimTK::Visualizer::GroundAndSky);

        Manager manager(inputModel);
        inputModel.printDetailedInfo(si, std::cout);
        std::cout << "integrating for " << finalTime << " seconds" << std::endl;
        manager.initialize(si);
        manager.integrate(finalTime);
    }catch (OpenSim::Exception ex) {
        std::cout << ex.getMessage() << std::endl;
        return 1;
    }
    time_t my_time = time(NULL);

    // ctime() used to give the present time
    printf(" All OpenSIm tasks are completed on: %s", ctime(&my_time));
}