Example #1
0
SimTK::ContactGeometry::TriangleMesh* ContactMesh::
    loadMesh(const std::string& filename) const
{
    SimTK::PolygonalMesh mesh;
    std::ifstream file;
    assert (_model);
    const std::string& savedCwd = IO::getCwd();
    bool restoreDirectory = false;
    if ((_model->getInputFileName()!="")
            && (_model->getInputFileName()!="Unassigned")) {
        std::string parentDirectory = IO::getParentDirectory(
                _model->getInputFileName());
        IO::chDir(parentDirectory);
        restoreDirectory=true;
    }
    file.open(filename.c_str());
    if (file.fail()){
        if (restoreDirectory) IO::chDir(savedCwd);
        throw Exception("Error loading mesh file: "+filename+". "
                "The file should exist in same folder with model.\n "
                "Loading is aborted.");
    }
    file.close();
    mesh.loadFile(filename);
    if (restoreDirectory) IO::chDir(savedCwd);
        
    return new SimTK::ContactGeometry::TriangleMesh(mesh);
}
Example #2
0
ContactMesh::ContactMesh(const std::string& filename,
                         const SimTK::Vec3& location,
                         const SimTK::Vec3& orientation,
                         const PhysicalFrame& frame) :
    ContactGeometry(location, orientation, frame)
{
    setNull();
    constructProperties();
    setFilename(filename);
    if (filename != ""){
        std::ifstream file;
        file.open(filename.c_str());
        if (file.fail())
            throw Exception("Error loading mesh file: "+filename+". The file should exist in same folder with model.\n Model loading is aborted.");
        file.close();
        SimTK::PolygonalMesh mesh;
        mesh.loadFile(filename);
        _geometry.reset(new SimTK::ContactGeometry::TriangleMesh(mesh));
    }
}
Example #3
0
void Mesh::extendFinalizeFromProperties() {

    if (!isObjectUpToDateWithProperties()) {
        const Component* rootModel = nullptr;
        if (!hasParent()) {
            std::clog << "Mesh " << get_mesh_file() << " not connected to model..ignoring\n";
            return;   // Orphan Mesh not part of a model yet
        }
        const Component* parent = &getParent();
        while (parent != nullptr) {
            if (dynamic_cast<const Model*>(parent) != nullptr) {
                rootModel = parent;
                break;
            }
            if (parent->hasParent())
                parent = &(parent->getParent()); // traverse up Component tree
            else
                break; // can't traverse up.
        }

        if (rootModel == nullptr) {
            std::clog << "Mesh " << get_mesh_file() << " not connected to model..ignoring\n";
            return;   // Orphan Mesh not descendent of a model
        }
        // Current interface to Visualizer calls generateDecorations on every frame.
        // On first time through, load file and create DecorativeMeshFile and cache it
        // so we don't load files from disk during live drawing/rendering.
        const std::string& file = get_mesh_file();
        bool isAbsolutePath; string directory, fileName, extension;
        SimTK::Pathname::deconstructPathname(file,
            isAbsolutePath, directory, fileName, extension);
        const string lowerExtension = SimTK::String::toLower(extension);
        if (lowerExtension != ".vtp" && lowerExtension != ".obj" && lowerExtension != ".stl") {
            std::clog << "ModelVisualizer ignoring '" << file
                << "'; only .vtp .stl and .obj files currently supported.\n";
            return;
        }

        // File is a .vtp or .obj. See if we can find it.
        Array_<string> attempts;
        const Model& model = dynamic_cast<const Model&>(*rootModel);
        bool foundIt = ModelVisualizer::findGeometryFile(model, file, isAbsolutePath, attempts);

        if (!foundIt) {
            std::clog << "ModelVisualizer couldn't find file '" << file
                << "'; tried\n";
            for (unsigned i = 0; i < attempts.size(); ++i)
                std::clog << "  " << attempts[i] << "\n";
            if (!isAbsolutePath &&
                !Pathname::environmentVariableExists("OPENSIM_HOME"))
                std::clog << "Set environment variable OPENSIM_HOME "
                << "to search $OPENSIM_HOME/Geometry.\n";
            return;
        }

        SimTK::PolygonalMesh pmesh;
        try {
            std::ifstream objFile;
            objFile.open(attempts.back().c_str());
            pmesh.loadFile(attempts.back().c_str());
            // objFile closes when destructed

        }
        catch (const std::exception& e) {
            std::clog << "Visualizer couldn't read "
                << attempts.back() << " because:\n"
                << e.what() << "\n";
            return;
        }

        cachedMesh.reset(new DecorativeMeshFile(attempts.back().c_str()));
    }
}