1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2019 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Asher Elmquist
13 // =============================================================================
14 //
15 // Benchmark for testing changes to rendering algorimths
16 //
17 // =============================================================================
18 
19 #include "chrono/assets/ChTriangleMeshShape.h"
20 #include "chrono/assets/ChVisualMaterial.h"
21 #include "chrono/assets/ChVisualization.h"
22 #include "chrono/geometry/ChTriangleMeshConnected.h"
23 #include "chrono/physics/ChBodyEasy.h"
24 #include "chrono/physics/ChSystemNSC.h"
25 #include "chrono/utils/ChUtilsCreators.h"
26 #include "chrono_thirdparty/filesystem/path.h"
27 
28 #include "chrono_sensor/sensors/ChCameraSensor.h"
29 #include "chrono_sensor/ChSensorManager.h"
30 #include "chrono_sensor/filters/ChFilterAccess.h"
31 #include "chrono_sensor/filters/ChFilterGrayscale.h"
32 #include "chrono_sensor/filters/ChFilterSave.h"
33 #include "chrono_sensor/filters/ChFilterVisualize.h"
34 
35 using namespace chrono;
36 using namespace chrono::geometry;
37 using namespace chrono::sensor;
38 
39 float end_time = 10.0f;
40 
main(int argc,char * argv[])41 int main(int argc, char* argv[]) {
42     // for (int q = 0; q < 5; q++) {
43     GetLog() << "Copyright (c) 2019 projectchrono.org\nChrono version: " << CHRONO_VERSION << "\n\n";
44 
45     // -----------------
46     // Create the system
47     // -----------------
48     ChSystemNSC mphysicalSystem;
49 
50     auto reflective_color = std::make_shared<ChVisualMaterial>();
51     reflective_color->SetDiffuseColor({.5f, .5f, .5f});
52     reflective_color->SetSpecularColor({.99f, .99f, .99f});
53     reflective_color->SetFresnelMin(1);
54     reflective_color->SetRoughness(0);
55 
56     auto red_color = std::make_shared<ChVisualMaterial>();
57     red_color->SetDiffuseColor({1.f, 0.f, 0.f});
58     red_color->SetSpecularColor({0.f, 0.f, 0.f});
59     red_color->SetFresnelMin(0.0f);
60     red_color->SetRoughness(1.0f);
61 
62     auto mirror_left = chrono_types::make_shared<ChBodyEasyBox>(50, .1, 5, 1000, true, false);
63     mirror_left->SetPos({0, -1, 0});
64     mirror_left->SetBodyFixed(true);
65     mphysicalSystem.Add(mirror_left);
66     std::dynamic_pointer_cast<ChVisualization>(mirror_left->GetAssets()[0])->material_list.push_back(reflective_color);
67 
68     auto mirror_right = chrono_types::make_shared<ChBodyEasyBox>(50, .1, 5, 1000, true, false);
69     mirror_right->SetPos({0, 1, 0});
70     mirror_right->SetBodyFixed(true);
71     mphysicalSystem.Add(mirror_right);
72     std::dynamic_pointer_cast<ChVisualization>(mirror_right->GetAssets()[0])->material_list.push_back(reflective_color);
73 
74     auto ball = chrono_types::make_shared<ChBodyEasySphere>(.25, 1000, true, false);
75     ball->SetPos({4, 0, 0});
76     ball->SetBodyFixed(true);
77     mphysicalSystem.Add(ball);
78     std::dynamic_pointer_cast<ChVisualization>(ball->GetAssets()[0])->material_list.push_back(red_color);
79 
80     auto cam_body = chrono_types::make_shared<ChBodyEasyBox>(.01, .01, .01, 1000, false, false);
81     cam_body->SetBodyFixed(true);
82     mphysicalSystem.Add(cam_body);
83 
84     // -----------------------
85     // Create a sensor manager
86     // -----------------------
87     auto manager = std::make_shared<ChSensorManager>(&mphysicalSystem);
88     manager->SetVerbose(false);
89     manager->SetRayRecursions(21);
90     float intensity = 1.f;
91     manager->scene->AddPointLight({0, 0, 100}, {intensity, intensity, intensity}, 1000);
92     manager->scene->AddPointLight({0, 0, -100}, {intensity, intensity, intensity}, 1000);
93 
94     // ------------------------------------------------
95     // Create a camera and add it to the sensor manager
96     // ------------------------------------------------
97     auto cam = std::make_shared<ChCameraSensor>(
98         cam_body,                                                            // body camera is attached to
99         50.0f,                                                               // update rate in Hz
100         chrono::ChFrame<double>({-10, 0, 0}, Q_from_AngAxis(0, {0, 1, 0})),  // offset pose
101         1920,                                                                // image width
102         1080,                                                                // image height
103         (float)CH_C_PI / 1.2f                                                // FOV
104     );
105     cam->SetName("Camera Sensor");
106     cam->PushFilter(std::make_shared<ChFilterVisualize>(1920, 1080));
107     // cam->PushFilter(std::make_shared<ChFilterRGBA8Access>());
108 
109     // add sensor to the manager
110     manager->AddSensor(cam);
111 
112     // ---------------
113     // Simulate system
114     // ---------------
115     // float orbit_radius = (2 * x_instances) * x_spread + 0.f;
116     // float orbit_rate = 0.5;
117     float ch_time = 0.0;
118 
119     std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
120 
121     while (ch_time < end_time) {
122         // cam->SetOffsetPose(chrono::ChFrame<double>(
123         //     {-orbit_radius * cos(ch_time * orbit_rate), -orbit_radius * sin(ch_time * orbit_rate), 1},
124         //     Q_from_AngAxis(ch_time * orbit_rate, {0, 0, 1})));
125 
126         manager->Update();
127         mphysicalSystem.DoStepDynamics(0.001);
128 
129         ch_time = (float)mphysicalSystem.GetChTime();
130     }
131     std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
132     std::chrono::duration<double> wall_time = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
133     std::cout << "Simulation time: " << ch_time << "s, wall time: " << wall_time.count() << "s.\n";
134     std::cout << "Frames: " << 100 * ch_time << ", Mean FPS: " << 100 * ch_time / wall_time.count() << "\n";
135     return 0;
136 }
137