1 // ============================================================================= 2 // PROJECT CHRONO - http://projectchrono.org 3 // 4 // Copyright (c) 2014 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 13 #ifndef CHIRRTOOLS_H 14 #define CHIRRTOOLS_H 15 16 #include <vector> 17 #include <irrlicht.h> 18 19 #include "chrono/core/ChCoordsys.h" 20 #include "chrono/core/ChMatrix.h" 21 #include "chrono/core/ChVector.h" 22 #include "chrono/motion_functions/ChFunction_Base.h" 23 #include "chrono/physics/ChSystem.h" 24 25 #include "chrono_irrlicht/ChApiIrr.h" 26 27 namespace irr { 28 namespace core { 29 30 /// Utility class to easily convert a Chrono vector into an Irrlicht vector3df. 31 class ChApiIrr vector3dfCH : public vector3df { 32 public: vector3dfCH(const chrono::ChVector<> & mch)33 vector3dfCH(const chrono::ChVector<>& mch) { 34 X = ((f32)mch.x()); 35 Y = ((f32)mch.y()); 36 Z = ((f32)mch.z()); 37 } 38 }; 39 40 } // end namespace core 41 } // end namespace irr 42 43 // ----------------------------------------------------------------------------- 44 45 namespace chrono { 46 namespace irrlicht { 47 48 /// @addtogroup irrlicht_module 49 /// @{ 50 51 enum class IrrContactsDrawMode { 52 CONTACT_NORMALS = 0, // draw normals 53 CONTACT_DISTANCES, // draw lines connecting two contact points 54 CONTACT_FORCES_N, // draw contact forces (only normal component) applied to 2nd body 55 CONTACT_FORCES, // draw contact forces applied to 2nd body 56 CONTACT_NONE // draw nothing 57 }; 58 59 enum class IrrContactsLabelMode { 60 CONTACT_DISTANCES_VAL, // print distance 61 CONTACT_FORCES_VAL, // print contact forces modulus 62 CONTACT_FORCES_N_VAL, // print contact forces (only normal component) 63 CONTACT_FORCES_T_VAL, // print contact forces (only tangential component) 64 CONTACT_TORQUES_VAL, // print contact torques modulus 65 CONTACT_TORQUES_S_VAL, // print contact torques modulus (only spinning component) 66 CONTACT_TORQUES_R_VAL, // print contact torques modulus (only rolling component) 67 CONTACT_NONE_VAL // print nothing 68 }; 69 70 enum class IrrLinkDrawMode { 71 LINK_REACT_FORCE = 0, // draw reaction force 72 LINK_REACT_TORQUE, // draw reaction torque 73 LINK_NONE // draw nothing 74 }; 75 76 enum class IrrLinkLabelMode { 77 LINK_REACT_FORCE_VAL = 0, // print reaction force modulus 78 LINK_REACT_FORCE_X, // print reaction force x 79 LINK_REACT_FORCE_Y, // print reaction force y 80 LINK_REACT_FORCE_Z, // print reaction force z 81 LINK_REACT_TORQUE_VAL, // print reaction torque modulus 82 LINK_REACT_TORQUE_X, // print reaction torque x 83 LINK_REACT_TORQUE_Y, // print reaction torque y 84 LINK_REACT_TORQUE_Z, // print reaction torque z 85 LINK_NONE_VAL // draw nothing 86 }; 87 88 namespace tools { 89 90 /// Function to align an Irrlicht object to a Chrono coordsys. 91 ChApiIrr void alignIrrlichtNodeToChronoCsys(irr::scene::ISceneNode* mnode, const ChCoordsys<>& mcoords); 92 93 /// Easy-to-use function which draws contact points used by a ChSystem in the 94 /// current Irrlicht viewer (the IVideoDriver). The contact points are 95 /// visually represented with short lines, of length mlen, aligned to contact 96 /// normals. 97 ChApiIrr int drawAllContactPoints(std::shared_ptr<ChContactContainer> mcontactcontainer, 98 irr::video::IVideoDriver* driver, 99 double mlen = 1.0, 100 IrrContactsDrawMode drawtype = IrrContactsDrawMode::CONTACT_NORMALS); 101 102 /// Easy-to-use function which draws contact informations as labels at the 103 /// contact point 104 ChApiIrr int drawAllContactLabels(std::shared_ptr<ChContactContainer> mcontactcontainer, 105 irr::IrrlichtDevice* device, 106 IrrContactsLabelMode labeltype = IrrContactsLabelMode::CONTACT_FORCES_N_VAL, 107 irr::video::SColor mcol = irr::video::SColor(255, 255, 255, 255)); 108 109 /// Easy-to-use function which draws reaction forces in all contacts in 110 /// current Irrlicht viewer (the IVideoDriver). 111 ChApiIrr int drawAllLinks(ChSystem& mphysicalSystem, 112 irr::video::IVideoDriver* driver, 113 double mlen = 1.0, 114 IrrLinkDrawMode drawtype = IrrLinkDrawMode::LINK_REACT_FORCE); 115 116 /// Easy-to-use function which draws contact informations as labels at the 117 /// contact point 118 ChApiIrr int drawAllLinkLabels(ChSystem& mphysicalSystem, 119 irr::IrrlichtDevice* device, 120 IrrLinkLabelMode labeltype = IrrLinkLabelMode::LINK_REACT_FORCE_X, 121 irr::video::SColor mcol = irr::video::SColor(255, 255, 255, 255)); 122 123 /// Easy-to-use function which draws collision objects bounding boxes for 124 /// rigid bodies - if they have a collision shape. 125 ChApiIrr int drawAllBoundingBoxes(ChSystem& mphysicalSystem, irr::video::IVideoDriver* driver); 126 127 /// Easy-to-use function which draws coordinate systems of ChBody objects. 128 ChApiIrr int drawAllCOGs(ChSystem& mphysicalSystem, irr::video::IVideoDriver* driver, double scale = 0.01); 129 130 /// Easy-to-use function which draws coordinate systems of link frames. 131 ChApiIrr int drawAllLinkframes(ChSystem& mphysicalSystem, irr::video::IVideoDriver* driver, double scale = 0.01); 132 133 /// -- 134 ChApiIrr void drawHUDviolation(irr::video::IVideoDriver* driver, 135 irr::IrrlichtDevice* mdevice, 136 ChSystem& asystem, 137 int mx = 10, 138 int my = 290, 139 int sx = 300, 140 int sy = 100, 141 double spfact = 100.0); 142 143 /// -- 144 ChApiIrr void drawChFunction(irr::IrrlichtDevice* mdevice, 145 ChFunction* fx, 146 double xmin = 0, 147 double xmax = 1, 148 double ymin = -1, 149 double ymax = 1, 150 int mx = 10, 151 int my = 290, 152 int sx = 300, 153 int sy = 100); 154 155 /// Easy-to-use function to draw segment lines in 3D space, with given color. 156 ChApiIrr void drawSegment(irr::video::IVideoDriver* driver, 157 ChVector<> mstart, 158 ChVector<> mend, 159 irr::video::SColor mcol = irr::video::SColor(255, 0, 0, 0), 160 bool use_Zbuffer = false); 161 162 /// Easy-to-use function to draw a polyline in 3D space, given the array of 163 /// points as a std::vector. 164 ChApiIrr void drawPolyline(irr::video::IVideoDriver* driver, 165 std::vector<ChVector<> >& mpoints, 166 irr::video::SColor mcol = irr::video::SColor(255, 0, 0, 0), 167 bool use_Zbuffer = false); 168 169 /// Easy-to-use function to draw a circle line in 3D space, with given color. 170 /// Specify the center as coordsys position. Orientation as coordsys 171 /// quaternion (default in xy plane) 172 ChApiIrr void drawCircle(irr::video::IVideoDriver* driver, 173 double radius, 174 ChCoordsys<> mpos = CSYSNORM, 175 irr::video::SColor mcol = irr::video::SColor(255, 0, 0, 0), 176 int mresolution = 36, 177 bool use_Zbuffer = false); 178 179 /// Easy-to-use function to draw a spring in 3D space, with given color. 180 /// Specify the radius, the end points in absolute space, the resolution (i.e. 181 /// the number of segments approximating the helix) and the number of turns. 182 ChApiIrr void drawSpring(irr::video::IVideoDriver* driver, 183 double radius, 184 ChVector<> start, 185 ChVector<> end, 186 irr::video::SColor mcol = irr::video::SColor(255, 0, 0, 0), 187 int mresolution = 65, 188 double turns = 5, 189 bool use_Zbuffer = false); 190 191 /// Easy-to-use function to draw grids in 3D space, with given orientation, 192 /// color and spacing. 193 ChApiIrr void drawGrid(irr::video::IVideoDriver* driver, 194 double ustep = 0.1, 195 double vstep = 0.1, 196 int nu = 20, 197 int nv = 20, 198 ChCoordsys<> mpos = CSYSNORM, 199 irr::video::SColor mcol = irr::video::SColor(50, 80, 110, 110), 200 bool use_Zbuffer = false); 201 202 /// Easy-to-use function to draw color bar with a color map and 2D legend 203 ChApiIrr void drawColorbar(double vmin, 204 double vmax, 205 const std::string& label, 206 irr::IrrlichtDevice* mdevice, 207 int mx = 740, 208 int my = 20, 209 int sx = 30, 210 int sy = 300); 211 212 /// Draw the collision shapes as wireframe, overlayed to shapes. 213 /// Note: this works only for the Bullet collision system (i.e. not working for Chrono::Multicore) 214 ChApiIrr void drawCollisionShapes(ChSystem& asystem, 215 irr::IrrlichtDevice* mdevice, 216 irr::video::SColor mcol = irr::video::SColor(50, 0, 0, 110)); 217 218 /// -- 219 ChApiIrr void drawPlot3D(irr::video::IVideoDriver* driver, 220 ChMatrixConstRef X, // x of points, in local csys x 221 ChMatrixConstRef Y, // y of points, in local csys y 222 ChMatrixConstRef Z, // z height map of points, in local csys z 223 ChCoordsys<> mpos = CSYSNORM, 224 irr::video::SColor mcol = irr::video::SColor(50, 80, 110, 110), 225 bool use_Zbuffer = false); 226 227 /// Draw run-time profiler infos 228 ChApiIrr void drawProfiler(irr::IrrlichtDevice* device); 229 230 } // end namespace tools 231 232 /// @} irrlicht_module 233 234 } // end namespace irrlicht 235 } // end namespace chrono 236 237 #endif 238