-- You received this message because you are subscribed to the Google Groups "ProjectChrono" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/projectchrono/fbd40813-6b61-4dd8-ab39-b215074a2583n%40googlegroups.com.
// ============================================================================= // PROJECT CHRONO - http://projectchrono.org // // Copyright (c) 2014 projectchrono.org // All rights reserved. // // Use of this source code is governed by a BSD-style license that can be found // in the LICENSE file at the top level of the distribution and at // http://projectchrono.org/license-chrono.txt. // // ============================================================================= // Authors: Alessandro Tasora, Radu Serban // ============================================================================= // // Demo code about collisions of triangle meshes // // =============================================================================
#include "chrono/physics/ChSystemNSC.h"
#include "chrono/physics/ChBodyEasy.h"
#include "chrono/physics/ChInertiaUtils.h"
#include "chrono/assets/ChTexture.h"
#include "chrono/assets/ChTriangleMeshShape.h"
#include "chrono/geometry/ChTriangleMeshConnected.h"
#include "chrono/core/ChLog.h"
#include "chrono_thirdparty/filesystem/path.h"
#include "chrono/utils/ChUtilsInputOutput.h"
#include "chrono_irrlicht/ChVisualSystemIrrlicht.h"
#include <omp.h>
#include <fstream>
#include <iomanip>
// Use the namespaces of Chrono
using namespace chrono;
using namespace chrono::geometry;
using namespace chrono::irrlicht;
using std::cout;
using std::endl;
using std::ofstream;
using std::setw;
int main(int argc, char* argv[]) {
GetLog() << "Copyright (c) 2017 projectchrono.org\nChrono version: " <<
CHRONO_VERSION << "\n\n";
// Create a Chrono physical system
ChSystemNSC sys;
// Create all the rigid bodies.
collision::ChCollisionModel::SetDefaultSuggestedEnvelope(0.0025);
collision::ChCollisionModel::SetDefaultSuggestedMargin(0.0025);
// - Create a floor
auto floor_mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
auto floor = chrono_types::make_shared<ChBodyEasyBox>(30, 0.1, 30, 1000,
true, true, floor_mat);
floor->SetPos(ChVector<>(0, -1, 0));
floor->SetBodyFixed(true);
floor->GetVisualShape(0)->SetTexture(GetChronoDataFile("textures/concrete.jpg"));
sys.Add(floor);
sys.SetNumThreads(1, 4);
// - Create a falling item with triangle mesh shape
// Shared contact material for all meshes
auto mat2 = chrono_types::make_shared<ChMaterialSurfaceNSC>();
// Note: one can create easily a colliding shape using the following
// piece of code:
//
// auto falling = chrono_types::make_shared<ChBodyEasyMesh>(
// GetChronoDataFile("models/bulldozer/shoe_view.obj"), // file name
for OBJ Wavefront mesh
// 1000, // density of
the body
// true, //
automatic evaluation of mass, COG position, inertia
// tensor
// true, // attach
visualization asset
// true, //
enable the collision detection
// mat, // surface
contact material
// 0.005 //
radius of 'inflating' of mesh (for more robust
// collision detection)
// );
// falling->SetFrame_REF_to_abs(ChFrame<>(ChVector<>(-0.9 + ChRandom() *
1.4, 0.4 + j * 0.12, -0.9 + ChRandom()
// * 1.4))); sys.Add(falling);
//
// but here we want to show a more low-level control of this process, for
// various reasons, for example: we want to share a single
ChTriangleMeshConnected
// between 15 falling shapes; also we want to call
RepairDuplicateVertexes() on the
// imported mesh; also we want to scale the imported mesh using Transform().
auto mesh = chrono_types::make_shared<ChTriangleMeshConnected>();
mesh->LoadSTLMesh("D:/Chrono/chrono/data/models/bulldozer/rock1.stl", true);
mesh->Transform(ChVector<>(0, 0, 0), ChMatrix33<>(1)); // scale to a
different size
mesh->RepairDuplicateVertexes(1e-9); // if meshes are
not watertight
// compute mass inertia from mesh
double mass;
ChVector<> cog;
ChMatrix33<> inertia;
double density = 1000;
mesh->ComputeMassProperties(true, mass, cog, inertia);
ChMatrix33<> principal_inertia_rot;
ChVector<> principal_I;
ChInertiaUtils::PrincipalInertia(inertia, principal_I,
principal_inertia_rot);
// Create a shared visual model containing a visualizatoin mesh
auto mesh_shape = chrono_types::make_shared<ChTriangleMeshShape>();
mesh_shape->SetMesh(mesh);
mesh_shape->SetMutable(false);
mesh_shape->SetColor(ChColor(1.0f, 0.5f, 0.5f));
mesh_shape->SetBackfaceCull(true);
auto vis_model = chrono_types::make_shared<ChVisualModel>();
vis_model->AddShape(mesh_shape);
double ori[3] = {0, 2, 0};
for (int j = 0; j != 2; ++j)
{
for (int i = 0; i != 5; ++i)
{
for (int k = 0; k != 5; ++k)
{
auto pObj = chrono_types::make_shared<ChBodyAuxRef>();
// Set the COG coordinates to barycenter, without displacing
the REF reference.
// Make the COG frame a principal frame.
pObj->SetFrame_COG_to_REF(ChFrame<>(cog,
principal_inertia_rot));
// Set inertia
pObj->SetMass(mass * density);
pObj->SetInertiaXX(density * principal_I);
// Set the absolute position of the body:
pObj->SetFrame_REF_to_abs(ChFrame<>(ChVector<>(ori[0] + 1.2 * i
+ j*0.6, ori[1] + 0.6 + j*0.6, ori[2] + 1.2 * k + j*0.6)));
sys.Add(pObj);
pObj->GetCollisionModel()->ClearModel();
pObj->GetCollisionModel()->AddTriangleMesh(mat2, mesh, false,
false, VNULL, ChMatrix33<>(1), 0.001);
pObj->GetCollisionModel()->BuildModel();
pObj->SetCollide(true);
pObj->AddVisualModel(vis_model);
}
}
}
//for (int j = 0; j < 15; ++j) {
// auto falling = chrono_types::make_shared<ChBodyAuxRef>();
// // Set the COG coordinates to barycenter, without displacing the REF
reference.
// // Make the COG frame a principal frame.
// falling->SetFrame_COG_to_REF(ChFrame<>(cog, principal_inertia_rot));
// // Set inertia
// falling->SetMass(mass * density);
// falling->SetInertiaXX(density * principal_I);
// // Set the absolute position of the body:
// falling->SetFrame_REF_to_abs(
// ChFrame<>(ChVector<>(-0.9 + ChRandom() * 1.4, 0.4 + j * 0.12,
-0.9 + ChRandom() * 1.4)));
// sys.Add(falling);
// falling->GetCollisionModel()->ClearModel();
// falling->GetCollisionModel()->AddTriangleMesh(mat2, mesh, false,
false, VNULL, ChMatrix33<>(1), 0.001);
// falling->GetCollisionModel()->BuildModel();
// falling->SetCollide(true);
// falling->AddVisualModel(vis_model);
//}
// Shared contact material for falling objects
auto obj_mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
obj_mat->SetFriction(0.2f);
// Create a falling rigid bodies
for (int bi = 0; bi < 20; bi++) {
auto sphereBody = chrono_types::make_shared<ChBodyEasySphere>(0.05,
// radius size
1000,
// density
true,
// visualization?
true,
// collision?
obj_mat);
// contact material
sphereBody->SetPos(ChVector<>(-0.5 + ChRandom() * 1, 1.4, -0.5 +
ChRandom()));
sphereBody->GetVisualShape(0)->SetColor(ChColor(0.3f, 0.3f, 0.6f));
//sys.Add(sphereBody);
}
// Create the Irrlicht visualization system
auto vis = chrono_types::make_shared<ChVisualSystemIrrlicht>();
vis->AttachSystem(&sys);
vis->SetWindowSize(1280, 720);
vis->SetWindowTitle("Collisions between objects");
vis->Initialize();
vis->AddLogo();
vis->AddSkyBox();
vis->AddCamera(ChVector<>(0, 1, -1));
vis->AddTypicalLights();
/*vis->AddLightWithShadow(ChVector<>(1.5, 5.5, -2.5), ChVector<>(0, 0, 0),
3, 2.2, 7.2, 40, 512,
ChColor(0.8f, 0.8f, 1.0f));*/
vis->EnableShadows();
////application.SetContactsDrawMode(ContactsDrawMode::CONTACT_DISTANCES);
// Simulation loop
const std::string out_dir = GetChronoOutputPath() + "_trimesh";
if (filesystem::create_directory(filesystem::path(out_dir))) {
cout << " ...Created output directory" << endl;
} else {
cout << " ...Error creating output directory" << endl;
return 1;
}
ofstream file_out(out_dir + "/outputdata.txt");
int len(30), len_short(20);
bool UsePercent(false);
if (UsePercent)
{
file_out << setw(10) << "t (s)"
<< setw(len_short) << "Total time (%)"
<< setw(len) << "Integration time (%)"
<< setw(len_short) << "LS Setup time (%)"
<< setw(len_short) << "LS Solve time (%)"
<< setw(len) << "Total Collision time (%)"
<< setw(len) << "Broad Collision time (%)"
<< setw(len) << "Narrow Collision time (%)"
<< setw(len_short) << "Calc Jacobian (%)"
<< setw(len_short) << "System Setup (%)"
<< setw(len_short) << "System Update (%)" << endl;
}
else
{
file_out << setw(10) << "t (s)"
<< setw(len_short) << "Total time (s)"
<< setw(len) << "Integration time (s)"
<< setw(len_short) << "LS Setup time (s)"
<< setw(len_short) << "LS Solve time (s)"
<< setw(len) << "Total Collision time (s)"
<< setw(len) << "Broad Collision time (s)"
<< setw(len) << "Narrow Collision time (s)"
<< setw(len_short) << "Calc Jacobian (s)"
<< setw(len_short) << "System Setup (s)"
<< setw(len_short) << "System Update (s)" << endl;
}
double t_curr(0.0);
//while (vis->Run())
while( t_curr<=5.0)
{
vis->BeginScene(true, true, ChColor(0.55f, 0.63f, 0.75f));
vis->Render();
vis->EndScene();
sys.DoStepDynamics(0.005);
t_curr += 0.005;
if(UsePercent)
{
double t_tot(sys.GetTimerStep());
file_out << setw(10) << sys.GetChTime()
<< setw(len_short) << 1.0
<< setw(len) << sys.GetTimerAdvance()/t_tot
<< setw(len_short) << sys.GetTimerLSsetup()/t_tot
<< setw(len_short) << sys.GetTimerLSsolve()/t_tot
<< setw(len) << sys.GetTimerCollision() /t_tot
<< setw(len) << sys.GetTimerCollisionBroad() /t_tot
<< setw(len) << sys.GetTimerCollisionNarrow() /t_tot
<< setw(len_short) << sys.GetTimerJacobian()/t_tot
<< setw(len_short) << sys.GetTimerSetup()/t_tot
<< setw(len_short) << sys.GetTimerJacobian()/t_tot
<< endl;
}
else
{
file_out << setw(10) << sys.GetChTime()
<< setw(len_short) << sys.GetTimerStep()
<< setw(len) << sys.GetTimerAdvance()
<< setw(len_short) << sys.GetTimerLSsetup()
<< setw(len_short) << sys.GetTimerLSsolve()
<< setw(len) << sys.GetTimerCollision()
<< setw(len) << sys.GetTimerCollisionBroad()
<< setw(len) << sys.GetTimerCollisionNarrow()
<< setw(len_short) << sys.GetTimerJacobian()
<< setw(len_short) << sys.GetTimerSetup()
<< setw(len_short) << sys.GetTimerJacobian()
<< endl;
}
}
return 0;
}
rock1.stl
Description: application/vnd.ms-pki.stl
