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 // Authors: Alessandro Tasora
13 // =============================================================================
14 
15 #ifndef CHVISUALIZATIONFEAMESH_H
16 #define CHVISUALIZATIONFEAMESH_H
17 
18 #include "chrono/assets/ChAssetLevel.h"
19 #include "chrono/assets/ChColor.h"
20 #include "chrono/geometry/ChTriangleMeshConnected.h"
21 
22 #include "chrono/fea/ChMesh.h"
23 #include "chrono/fea/ChNodeFEAxyz.h"
24 #include "chrono/fea/ChNodeFEAxyzP.h"
25 #include "chrono/fea/ChNodeFEAxyzrot.h"
26 #include "chrono/fea/ChNodeFEAxyzDD.h"
27 
28 namespace chrono {
29 namespace fea {
30 
31 /// @addtogroup chrono_fea
32 /// @{
33 
34 /// Class for a FEA mesh visualization.
35 /// It converts tetrahedrons, etc. into a colored triangle mesh asset
36 /// of class ChTriangleMeshShape that is contained in its sublevel,
37 /// so that it can be rendered or postprocessed.
38 class ChApi ChVisualizationFEAmesh : public ChAssetLevel {
39   public:
40     enum eChFemDataType {
41         E_PLOT_NONE,
42         E_PLOT_SURFACE,
43         E_PLOT_CONTACTSURFACES,
44         E_PLOT_LOADSURFACES,
45         E_PLOT_NODE_DISP_NORM,
46         E_PLOT_NODE_DISP_X,
47         E_PLOT_NODE_DISP_Y,
48         E_PLOT_NODE_DISP_Z,
49         E_PLOT_NODE_SPEED_NORM,
50         E_PLOT_NODE_SPEED_X,
51         E_PLOT_NODE_SPEED_Y,
52         E_PLOT_NODE_SPEED_Z,
53         E_PLOT_NODE_ACCEL_NORM,
54         E_PLOT_NODE_ACCEL_X,
55         E_PLOT_NODE_ACCEL_Y,
56         E_PLOT_NODE_ACCEL_Z,
57         E_PLOT_ELEM_STRAIN_VONMISES,
58         E_PLOT_ELEM_STRESS_VONMISES,
59         E_PLOT_ELEM_STRAIN_HYDROSTATIC,
60         E_PLOT_ELEM_STRESS_HYDROSTATIC,
61         E_PLOT_ELEM_BEAM_MX,
62         E_PLOT_ELEM_BEAM_MY,
63         E_PLOT_ELEM_BEAM_MZ,
64         E_PLOT_ELEM_BEAM_TX,
65         E_PLOT_ELEM_BEAM_TY,
66         E_PLOT_ELEM_BEAM_TZ,
67         E_PLOT_NODE_P,  // scalar field for Poisson problems (ex. temperature if thermal FEM)
68         E_PLOT_ANCF_BEAM_AX,
69         E_PLOT_ANCF_BEAM_BD,
70     };
71 
72     enum eChFemGlyphs {
73         E_GLYPH_NONE,
74         E_GLYPH_NODE_DOT_POS,
75         E_GLYPH_NODE_CSYS,
76         E_GLYPH_NODE_VECT_SPEED,
77         E_GLYPH_NODE_VECT_ACCEL,
78         E_GLYPH_ELEM_TENS_STRAIN,
79         E_GLYPH_ELEM_TENS_STRESS,
80         E_GLYPH_ELEM_VECT_DP,  // gradient field for Poisson problems (ex. heat flow if thermal FEM)
81     };
82 
83   protected:
84     //
85     // DATA
86     //
87 
88     ChMesh* FEMmesh;
89 
90     eChFemDataType fem_data_type;
91     eChFemGlyphs fem_glyph;
92 
93     double colorscale_min;
94     double colorscale_max;
95 
96     double symbols_scale;
97     double symbols_thickness;
98 
99     bool shrink_elements;
100     double shrink_factor;
101 
102     bool wireframe;
103     bool backface_cull;
104 
105     bool zbuffer_hide;
106 
107     bool smooth_faces;
108 
109     bool undeformed_reference;
110 
111     int beam_resolution;
112     int beam_resolution_section;
113     int shell_resolution;
114 
115     ChColor meshcolor;
116     ChColor symbolscolor;
117 
118     std::vector<int> normal_accumulators;
119 
120   public:
121     //
122     // CONSTRUCTORS
123     //
124 
125     ChVisualizationFEAmesh(ChMesh& mymesh);
126 
~ChVisualizationFEAmesh()127     virtual ~ChVisualizationFEAmesh() {}
128 
129     //
130     // FUNCTIONS
131     //
132 
133     // Access the referenced FEM mesh
GetMesh()134     virtual ChMesh& GetMesh() { return *FEMmesh; }
135 
136     // Returns the current data type to be plotted (speeds, forces, etc.)
GetFEMdataType()137     eChFemDataType GetFEMdataType() { return fem_data_type; }
138 
139     // Set the current data type to be plotted (speeds, forces, etc.)
SetFEMdataType(eChFemDataType mdata)140     void SetFEMdataType(eChFemDataType mdata) { fem_data_type = mdata; }
141 
142     // Returns the current data type to be drawn with glyphs
GetFEMglyphType()143     eChFemGlyphs GetFEMglyphType() { return fem_glyph; }
144 
145     // Set the current data type to be drawn with glyphs
SetFEMglyphType(eChFemGlyphs mdata)146     void SetFEMglyphType(eChFemGlyphs mdata) { fem_glyph = mdata; }
147 
148     // Set upper and lower values of the plotted variable for the colorscale plots.
SetColorscaleMinMax(double mmin,double mmax)149     void SetColorscaleMinMax(double mmin, double mmax) {
150         colorscale_min = mmin;
151         colorscale_max = mmax;
152     }
153 
154     // Set the scale for drawing the vector/tensors/etc. symbols
SetSymbolsScale(double mscale)155     void SetSymbolsScale(double mscale) { this->symbols_scale = mscale; }
GetSymbolsScale()156     double GetSymbolsScale() { return this->symbols_scale; }
157 
158     // Set the thickness of symbols used for drawing the vector/tensors/etc.
SetSymbolsThickness(double mthick)159     void SetSymbolsThickness(double mthick) { this->symbols_thickness = mthick; }
GetSymbolsThickness()160     double GetSymbolsThickness() { return this->symbols_thickness; }
161 
162     /// Set the resolution of beam triangulated drawing, along direction of beam
SetBeamResolution(int mres)163     void SetBeamResolution(int mres) { this->beam_resolution = mres; }
GetBeamResolution()164     int GetBeamResolution() { return this->beam_resolution; }
165 
166     /// Set the resolution of beam triangulated drawing, along the section
167     /// (i.e. for circular section= number of points along the circle)
SetBeamResolutionSection(int mres)168     void SetBeamResolutionSection(int mres) { this->beam_resolution_section = mres; }
GetBeamResolutionSection()169     int GetBeamResolutionSection() { return this->beam_resolution_section; }
170 
171     /// Set the resolution of shell triangulated drawing
SetShellResolution(int mres)172     void SetShellResolution(int mres) { this->shell_resolution = mres; }
GetShellResolution()173     int GetShellResolution() { return this->shell_resolution; }
174 
175     // Set shrinkage of elements during drawing
SetShrinkElements(bool mshrink,double mfact)176     void SetShrinkElements(bool mshrink, double mfact) {
177         this->shrink_elements = mshrink;
178         shrink_factor = mfact;
179     }
180 
181     // Set as wireframe visualization
SetWireframe(bool mwireframe)182     void SetWireframe(bool mwireframe) { this->wireframe = mwireframe; }
183 
184     // Set backface cull speedup (default false, but must be true for shells and in general where already double-sided
185     // twin triangles are used)
SetBackfaceCull(bool mbc)186     void SetBackfaceCull(bool mbc) { this->backface_cull = mbc; }
187 
188     // Set the Z buffer enable/disable (for those rendering systems that can do this)
189     // If hide= false, symbols will appear even if hidden by meshes/geometries. Default true.
SetZbufferHide(bool mhide)190     void SetZbufferHide(bool mhide) { this->zbuffer_hide = mhide; }
191 
192     // Set color for E_PLOT_SURFACE mode (also for wireframe lines)
SetDefaultMeshColor(ChColor mcolor)193     void SetDefaultMeshColor(ChColor mcolor) { this->meshcolor = mcolor; }
194 
195     // Set color for E_GLYPHS_NONE mode or for wireframe lines
SetDefaultSymbolsColor(ChColor mcolor)196     void SetDefaultSymbolsColor(ChColor mcolor) { this->symbolscolor = mcolor; }
197 
198     // Activate Gourad or Phong smoothing for faces of non-straight elements
199     // (with a small performance overhead) -NOTE: experimental
SetSmoothFaces(bool msmooth)200     void SetSmoothFaces(bool msmooth) { this->smooth_faces = msmooth; }
201 
202     // If this flag is turned on, the mesh is drawn as it is
203     // undeformed (the reference position).
SetDrawInUndeformedReference(bool mdu)204     void SetDrawInUndeformedReference(bool mdu) { this->undeformed_reference = mdu; }
205 
206     // Updates the triangle visualization mesh so that it matches with the
207     // FEM mesh (ex. tetrahedrons are converted in 4 surfaces, etc.
208     virtual void Update(ChPhysicsItem* updater, const ChCoordsys<>& coords);
209 
210   private:
211     double ComputeScalarOutput(std::shared_ptr<ChNodeFEAxyz> mnode,
212                                int nodeID,
213                                std::shared_ptr<ChElementBase> melement);
214     double ComputeScalarOutput(std::shared_ptr<ChNodeFEAxyzP> mnode,
215                                int nodeID,
216                                std::shared_ptr<ChElementBase> melement);
217     ChVector<float> ComputeFalseColor(double in);
218     ChColor ComputeFalseColor2(double in);
219     void UpdateBuffers_Hex(std::shared_ptr<ChElementBase> element,
220                            geometry::ChTriangleMeshConnected& trianglemesh,
221                            unsigned int& i_verts,
222                            unsigned int& i_vnorms,
223                            unsigned int& i_vcols,
224                            unsigned int& i_triindex);
225 };
226 
227 /// @} chrono_fea
228 
229 }  // end namespace fea
230 }  // end namespace chrono
231 
232 #endif
233