File: LookAtPolygonalMesh.cpp

package info (click to toggle)
simbody 3.7%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 72,896 kB
  • sloc: cpp: 248,827; ansic: 18,240; sh: 29; makefile: 24
file content (141 lines) | stat: -rw-r--r-- 5,708 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* -------------------------------------------------------------------------- *
 *             Simbody(tm) Adhoc test: Look at polygonal mesh                 *
 * -------------------------------------------------------------------------- *
 * This is part of the SimTK biosimulation toolkit originating from           *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody.  *
 *                                                                            *
 * Portions copyright (c) 2014 Stanford University and the Authors.           *
 * Authors: Michael Sherman                                                   *
 * Contributors:                                                              *
 *                                                                            *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License. You may obtain a  *
 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
 *                                                                            *
 * Unless required by applicable law or agreed to in writing, software        *
 * distributed under the License is distributed on an "AS IS" BASIS,          *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 * -------------------------------------------------------------------------- */

/*                      Simbody LookAtPolygonalMesh
Utility that exercises PolygonalMesh's load-from-file methods and then
displays the resulting mesh in the visualizer. It will also try to turn
the mesh into ContactGeometry and report any problems that occur. */

#include "Simbody.h"

#include <cassert>
#include <iostream>
using std::cout; using std::endl;

using namespace SimTK;

class ShowMesh : public DecorationGenerator {
public:
    ShowMesh() {} 

    // This is a shallow reference to the supplied mesh.
    void setMesh(const PolygonalMesh& mesh) {m_mesh=mesh;}

    void generateDecorations(const State&                state, 
                             Array_<DecorativeGeometry>& geometry) override
    {
        const Real TextScale = 0.1;
        DecorativeText info; info.setIsScreenText(true);
        info.setText("Faces/vertices: " + String(m_mesh.getNumFaces()) 
                     + "/" + String(m_mesh.getNumVertices()));
        geometry.push_back(info);
        if (!m_mesh.getNumFaces()) 
            return;

        DecorativeMesh dmesh(m_mesh);
        geometry.push_back(DecorativeMesh(dmesh)
                           .setOpacity(.8).setColor(Cyan));
        geometry.push_back(DecorativeMesh(dmesh)
                           .setRepresentation(DecorativeGeometry::DrawWireframe)
                           .setLineThickness(3)
                           .setColor(Black));
    }
private:
    PolygonalMesh m_mesh;
};

int main() {
  try {    
    // Create a system containing only Ground.   
    MultibodySystem system;
    SimbodyMatterSubsystem matter(system);

    system.setUseUniformBackground(true); // no ground plane in display
    system.realizeTopology();

    Visualizer viz(system);
    viz.setCameraClippingPlanes(.01, 100);
    ShowMesh* sp = new ShowMesh();
    viz.addDecorationGenerator(sp);
    viz.report(system.getDefaultState()); // show default shape

    // The Visualizer caches meshes by their addresses so won't update 
    // properly if memory gets reused in a modified mesh. Since there's a 
    // human in the loop here we can just keep all the meshes around until
    // termination; that way there is no danger of reuse.
    Array_<PolygonalMesh> meshes;
    meshes.push_back(PolygonalMesh::createSphereMesh(1.,3));
    sp->setMesh(meshes.back());
    viz.report(system.getDefaultState());
    viz.zoomCameraToShowAllGeometry();


    std::string line, cwd = Pathname::getCurrentWorkingDirectory();
    std::cout << "Current working directory: " << cwd << std::endl;
    std::cout << "Change working directory (ENTER to keep): ";
    std::getline(std::cin, line);
    if (!line.empty()) cwd = line;

    while(true) {
        viz.report(system.getDefaultState());
        viz.zoomCameraToShowAllGeometry();

        meshes.push_back(); // make a new empty mesh
        PolygonalMesh& mesh = meshes.back();

        printf("mesh file name (or 'end'): ");
        std::getline(std::cin, line);
        if (line=="end")
            break;

        std::string dir,fn,ext;
        bool isAbsolutePath;
        Pathname::deconstructPathname(line,isAbsolutePath,dir,fn,ext);
        if (!isAbsolutePath) line = cwd + "/" + line;

        if (!Pathname::fileExists(line)) {
            if (!line.empty()) printf("'%s' doesn't exist\n", line.c_str());
            sp->setMesh(mesh);
            continue;
        }

        try {
            mesh.loadFile(line);
        } catch(const std::exception& e) {
            cout << "File loader error: " << e.what() << "\n";
            mesh.clear();
        }
        sp->setMesh(mesh);

        try {
            ContactGeometry::TriangleMesh tri(mesh);
            printf("*** Works as contact mesh! ***\n");
        } catch(const std::exception& e) {
            cout << "XXX Can't make contact mesh: " << e.what() << "\n";
        }
    }

  } catch (const std::exception& e) {
    cout << "EXCEPTION: " << e.what() << "\n";
  }
}