Skip to content

Commit

Permalink
replace TriangleMeshShapeSTL with new formulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiangyu-Hu committed Jul 17, 2024
1 parent 9f12566 commit c227d3b
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 93 deletions.
4 changes: 2 additions & 2 deletions modules/structural_simulation/structural_simulation_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ void StructuralSimulation::createBodyMeshList()
{
std::string relative_input_path_copy = relative_input_path_;
#ifdef __EMSCRIPTEN__
body_mesh_list_.push_back(makeShared<TriangleMeshShapeGeneric>(reinterpret_cast<const uint8_t *>(imported_stl_list_[i].ptr), translation_list_[i], scale_stl_, imported_stl_list_[i].name));
body_mesh_list_.push_back(makeShared<TriangleMeshShapeSTL>(reinterpret_cast<const uint8_t *>(imported_stl_list_[i].ptr), translation_list_[i], scale_stl_, imported_stl_list_[i].name));
#else
body_mesh_list_.push_back(makeShared<TriangleMeshShapeGeneric>(relative_input_path_copy.append(imported_stl_list_[i]), translation_list_[i], scale_stl_, imported_stl_list_[i]));
body_mesh_list_.push_back(makeShared<TriangleMeshShapeSTL>(relative_input_path_copy.append(imported_stl_list_[i]), translation_list_[i], scale_stl_, imported_stl_list_[i]));
#endif
}
}
Expand Down
58 changes: 4 additions & 54 deletions src/for_3D_build/geometries/triangle_mesh_shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,55 +97,6 @@ BoundingBox TriangleMeshShape::findBounds()
return BoundingBox(lower_bound, upper_bound);
}
//=================================================================================================//
TriangleMeshShapeSTL::TriangleMeshShapeSTL(const std::string &filepathname, Vecd translation, Real scale_factor,
const std::string &shape_name)
: TriangleMeshShape(shape_name)
{
if (!fs::exists(filepathname))
{
std::cout << "\n Error: the input file:" << filepathname << " is not exists" << std::endl;
std::cout << __FILE__ << ':' << __LINE__ << std::endl;
throw;
}
SimTK::PolygonalMesh polymesh;
polymesh.loadStlFile(filepathname);
polymesh.scaleMesh(scale_factor);
triangle_mesh_ = generateTriangleMesh(polymesh.transformMesh(SimTKVec3(translation[0], translation[1], translation[2])));
}
//=================================================================================================//
TriangleMeshShapeSTL::TriangleMeshShapeSTL(const std::string &filepathname, Mat3d rotation,
Vec3d translation, Real scale_factor, const std::string &shape_name)
: TriangleMeshShape(shape_name)
{
if (!fs::exists(filepathname))
{
std::cout << "\n Error: the input file:" << filepathname << " is not exists" << std::endl;
std::cout << __FILE__ << ':' << __LINE__ << std::endl;
throw;
}
SimTK::PolygonalMesh polymesh;
polymesh.loadStlFile(filepathname);

polymesh.scaleMesh(scale_factor);
SimTK::Transform_<double> transform(SimTK::Rotation_<double>(SimTKMat33((double)rotation(0, 0), (double)rotation(0, 1), (double)rotation(0, 2),
(double)rotation(1, 0), (double)rotation(1, 1), (double)rotation(1, 2),
(double)rotation(2, 0), (double)rotation(2, 1), (double)rotation(2, 2))),
SimTKVec3((double)translation[0], (double)translation[1], (double)translation[2]));
triangle_mesh_ = generateTriangleMesh(polymesh.transformMesh(transform));
}
//=================================================================================================//
#ifdef __EMSCRIPTEN__
TriangleMeshShapeSTL::TriangleMeshShapeSTL(const uint8_t *buffer, Vecd translation, Real scale_factor,
const std::string &shape_name)
: TriangleMeshShape(shape_name)
{
SimTK::PolygonalMesh polymesh;
polymesh.loadStlBuffer(buffer);
polymesh.scaleMesh(scale_factor);
triangle_mesh_ = generateTriangleMesh(polymesh.transformMesh(SimTKVec3(translation[0], translation[1], translation[2])));
}
#endif
//=================================================================================================//
TriangleMeshShapeBrick::TriangleMeshShapeBrick(Vecd halfsize, int resolution, Vecd translation,
const std::string &shape_name)
: TriangleMeshShape(shape_name)
Expand Down Expand Up @@ -176,9 +127,8 @@ TriangleMeshShapeCylinder::TriangleMeshShapeCylinder(SimTK::UnitVec3 axis, Real
triangle_mesh_ = generateTriangleMesh(polymesh.transformMesh(SimTKVec3(translation[0], translation[1], translation[2])));
}
//=================================================================================================//
TriangleMeshShapeGeneric::
TriangleMeshShapeGeneric(const std::string &filepathname, Vec3d translation, Real scale_factor,
const std::string &shape_name)
TriangleMeshShapeSTL::TriangleMeshShapeSTL(const std::string &filepathname, Vec3d translation,
Real scale_factor, const std::string &shape_name)
: TriangleMeshShape(shape_name)
{
if (!fs::exists(filepathname))
Expand Down Expand Up @@ -213,14 +163,14 @@ TriangleMeshShapeGeneric::
triangle_mesh_distance_.construct(vertices, faces);
}
//=================================================================================================//
bool TriangleMeshShapeGeneric::checkContain(const Vec3d &probe_point, bool BOUNDARY_INCLUDED)
bool TriangleMeshShapeSTL::checkContain(const Vec3d &probe_point, bool BOUNDARY_INCLUDED)
{
Real distance = triangle_mesh_distance_.signed_distance(probe_point).distance;

return distance < 0.0 ? true : false;
}
//=================================================================================================//
Vecd TriangleMeshShapeGeneric::findClosestPoint(const Vecd &probe_point)
Vecd TriangleMeshShapeSTL::findClosestPoint(const Vecd &probe_point)
{
auto closest_pnt = triangle_mesh_distance_.signed_distance(probe_point).nearest_point;
return Vecd(closest_pnt[0], closest_pnt[1], closest_pnt[2]);
Expand Down
28 changes: 5 additions & 23 deletions src/for_3D_build/geometries/triangle_mesh_shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,6 @@ class TriangleMeshShape : public Shape
virtual BoundingBox findBounds() override;
};

/**
* @class TriangleMeshShapeSTL
* @brief Input triangle mesh with stl file.
*/
class TriangleMeshShapeSTL : public TriangleMeshShape
{
public:
explicit TriangleMeshShapeSTL(const std::string &file_path_name, Vec3d translation, Real scale_factor,
const std::string &shape_name = "TriangleMeshShapeSTL");
/** Overloaded to include rotation. */
explicit TriangleMeshShapeSTL(const std::string &file_path_name, Mat3d rotation, Vec3d translation,
Real scale_factor, const std::string &shape_name = "TriangleMeshShapeSTL");
#ifdef __EMSCRIPTEN__
TriangleMeshShapeSTL(const uint8_t *buffer, Vec3d translation, Real scale_factor, const std::string &shape_name = "TriangleMeshShapeSTL");
#endif
virtual ~TriangleMeshShapeSTL(){};
};

/**
* @class TriangleMeshShapeBrick
* @brief Generate a brick triangle mesh using SIMBODy default shape.
Expand Down Expand Up @@ -139,15 +121,15 @@ class TriangleMeshShapeCylinder : public TriangleMeshShape
};

/**
* @class TriangleMeshShapeGeneric
* @class TriangleMeshShapeSTL
* @brief Input triangle mesh with stl file.
*/
class TriangleMeshShapeGeneric : public TriangleMeshShape
class TriangleMeshShapeSTL : public TriangleMeshShape
{
public:
explicit TriangleMeshShapeGeneric(const std::string &file_path_name, Vec3d translation, Real scale_factor,
const std::string &shape_name = "TriangleMeshShapeSTL");
virtual ~TriangleMeshShapeGeneric(){};
explicit TriangleMeshShapeSTL(const std::string &file_path_name, Vec3d translation, Real scale_factor,
const std::string &shape_name = "TriangleMeshShapeSTL");
virtual ~TriangleMeshShapeSTL(){};

/** Here, we use the open source method TriangleMeshDistance library.
* https://github.com/InteractiveComputerGraphics/TriangleMeshDistance/tree/main */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Beam : public ComplexShape
{
std::string fname_ = "./input/beam.stl";
Vecd translation(0.0, 0.0, 0.0);
add<TriangleMeshShapeGeneric>(fname_, translation, 0.001);
add<TriangleMeshShapeSTL>(fname_, translation, 0.001);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Heart : public ComplexShape
explicit Heart(const std::string &shape_name) : ComplexShape(shape_name)
{
Vecd translation(-53.5 * length_scale, -70.0 * length_scale, -32.5 * length_scale);
add<TriangleMeshShapeGeneric>(full_path_to_stl_file, translation, length_scale);
add<TriangleMeshShapeSTL>(full_path_to_stl_file, translation, length_scale);
}
};
using FiberDirectionDiffusionRelaxation =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Heart : public ComplexShape
public:
explicit Heart(const std::string &shape_name) : ComplexShape(shape_name)
{
add<TriangleMeshShapeGeneric>(full_path_to_myocardium, translation, length_scale);
add<TriangleMeshShapeSTL>(full_path_to_myocardium, translation, length_scale);
}
};
/** Set diffusion relaxation method. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ int main(int ac, char *av[])
/** mass properties of the fixed spot. */
SimTK::Body::Rigid fixed_spot_info(SimTK::MassProperties(1, SimTK::Vec3(0), SimTK::UnitInertia(1)));
/** mass properties of the structure. */
StructureSystemForSimbody structure_multibody(structure, makeShared<TriangleMeshShapeGeneric>(stl_structure_path, translation_str, StructureScale));
StructureSystemForSimbody structure_multibody(structure, makeShared<TriangleMeshShapeSTL>(stl_structure_path, translation_str, StructureScale));
SimTK::Body::Rigid structure_info(*structure_multibody.body_part_mass_properties_);
/** Free mobilizer */
SimTK::MobilizedBody::Free tethered_struct(matter.Ground(), SimTK::Transform(SimTK::Vec3(translation_str[0], translation_str[1], translation_str[2])), structure_info, SimTK::Transform(SimTK::Vec3(0.0, 0.0, 0.0)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class FloatingStructure : public ComplexShape
public:
explicit FloatingStructure(const std::string &shape_name) : ComplexShape(shape_name)
{
add<TriangleMeshShapeGeneric>(stl_structure_path, translation_str, StructureScale);
add<TriangleMeshShapeSTL>(stl_structure_path, translation_str, StructureScale);
}
};

Expand Down Expand Up @@ -299,7 +299,7 @@ class WaterBlock : public ComplexShape
Vecd water_pos(0.5 * DW, 0.5 * (DL - EXS), 0.5 * WH);
Transform translation_water(water_pos);
add<TransformShape<GeometricShapeBox>>(Transform(translation_water), halfsize_water);
subtract<TriangleMeshShapeGeneric>(stl_structure_path, translation_str, StructureScale);
subtract<TriangleMeshShapeSTL>(stl_structure_path, translation_str, StructureScale);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SolidBodyFromMesh : public ComplexShape
explicit SolidBodyFromMesh(const std::string &shape_name) : ComplexShape(shape_name)
{
Vecd translation(0.0, 0.0, 0.0);
add<TriangleMeshShapeGeneric>(full_path_to_file, translation, 1.0);
add<TriangleMeshShapeSTL>(full_path_to_file, translation, 1.0);
}
};
//-----------------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class SolidBodyFromMesh : public ComplexShape
public:
explicit SolidBodyFromMesh(const std::string &shape_name) : ComplexShape(shape_name)
{
add<ExtrudeShape<TriangleMeshShapeGeneric>>(4.0 * dp_0, full_path_to_file, translation, scaling);
subtract<TriangleMeshShapeGeneric>(full_path_to_file, translation, scaling);
add<ExtrudeShape<TriangleMeshShapeSTL>>(4.0 * dp_0, full_path_to_file, translation, scaling);
subtract<TriangleMeshShapeSTL>(full_path_to_file, translation, scaling);
}
};
//----------------------------------------------------------------------
Expand Down Expand Up @@ -77,7 +77,7 @@ class SolidBodyFromMesh : public ComplexShape
// public:
// explicit SolidBodyFromMesh(const std::string &shape_name) : ComplexShape(shape_name)
// {
// add<TriangleMeshShapeGeneric>(full_path_to_file, translation, scaling);
// add<TriangleMeshShapeSTL>(full_path_to_file, translation, scaling);
// }
//};
//-----------------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Heart : public ComplexShape
explicit Heart(const std::string &shape_name) : ComplexShape(shape_name)
{
Vecd translation(0.0, 0.0, 0.0);
add<TriangleMeshShapeGeneric>(full_path_to_lv, translation, length_scale);
add<TriangleMeshShapeSTL>(full_path_to_lv, translation, length_scale);
}
};
/** Set diffusion relaxation. */
Expand Down
2 changes: 1 addition & 1 deletion tests/3d_examples/test_3d_self_contact/3d_self_contact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Coil : public ComplexShape
public:
explicit Coil(const std::string &shape_name) : ComplexShape(shape_name)
{
add<TriangleMeshShapeGeneric>(full_path_to_file, Vecd::Zero(), 1.0);
add<TriangleMeshShapeSTL>(full_path_to_file, Vecd::Zero(), 1.0);
}
};
class StationaryPlate : public ComplexShape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ImportedShellModel : public ComplexShape
public:
explicit ImportedShellModel(const std::string &shape_name) : ComplexShape(shape_name)
{
add<TriangleMeshShapeGeneric>(full_path_to_geometry, Vecd::Zero(), 1.0);
add<TriangleMeshShapeSTL>(full_path_to_geometry, Vecd::Zero(), 1.0);
}
};
//--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TEST(BernoulliBeam20x, Pressure)
SharedPtr<SaintVenantKirchhoffSolid> material = makeShared<SaintVenantKirchhoffSolid>(rho_0, Youngs_modulus, poisson);
std::vector<SharedPtr<SaintVenantKirchhoffSolid>> material_model_list = {material};

SharedPtr<TriangleMeshShapeGeneric> specimen = makeShared<TriangleMeshShapeGeneric>("./input/bernoulli_beam_20x.stl", Vec3d::Zero(), scale_stl, "bernoulli_beam_20x");
SharedPtr<TriangleMeshShapeSTL> specimen = makeShared<TriangleMeshShapeSTL>("./input/bernoulli_beam_20x.stl", Vec3d::Zero(), scale_stl, "bernoulli_beam_20x");
BoundingBox fixation = specimen->getBounds();
fixation.second_[0] = fixation.first_[0] + 0.01;

Expand Down

0 comments on commit c227d3b

Please sign in to comment.