1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2021 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: Radu Serban
13 // =============================================================================
14 //
15 // Shared data structure for the custom multicore collision system.
16 // =============================================================================
17 
18 #pragma once
19 
20 #include <memory>
21 
22 #include "chrono/physics/ChContactContainer.h"
23 
24 #include "chrono/multicore_math/ChMulticoreMath.h"
25 
26 namespace chrono {
27 namespace collision {
28 
29 /// @addtogroup collision_mc
30 /// @{
31 
32 /// Readibility type definition.
33 typedef int shape_type;
34 
35 /// Structure of arrays containing rigid collision shape information.
36 struct shape_container {
37     // All arrays of num_shapes length and indexed by the shape ID.
38 
39     std::vector<short2> fam_rigid;  ///< family information
40     std::vector<uint> id_rigid;     ///< ID of associated body
41     std::vector<int> typ_rigid;     ///< shape type
42     std::vector<int> local_rigid;   ///< local shape index in collision model of associated body
43     std::vector<int> start_rigid;   ///< start index in the appropriate container of dimensions
44     std::vector<int> length_rigid;  ///< usually 1, except for convex
45 
46     std::vector<quaternion> ObR_rigid;  ///< shape rotations
47     std::vector<real3> ObA_rigid;       ///< shape positions
48 
49     std::vector<real3> obj_data_A_global;
50     std::vector<quaternion> obj_data_R_global;
51 
52     std::vector<real> sphere_rigid;      ///< radius for sphere shapes
53     std::vector<real3> box_like_rigid;   ///< dimensions for box-like shapes
54     std::vector<real3> triangle_rigid;   ///< vertices of all triangle shapes (3 per shape)
55     std::vector<real2> capsule_rigid;    ///< radius and half-length for capsule shapes
56     std::vector<real4> rbox_like_rigid;  ///< dimensions and radius for rbox-like shapes
57     std::vector<real3> convex_rigid;     ///< points for convex hull shapes
58 
59     std::vector<real3> triangle_global;  ///< triangle vertices in global frame
60 };
61 
62 /// Structure of arrays containing state data.
63 struct state_container {
state_containerstate_container64     state_container()
65         : num_rigid_bodies(0),
66           num_fluid_bodies(0),
67           pos_rigid(nullptr),
68           rot_rigid(nullptr),
69           active_rigid(nullptr),
70           collide_rigid(nullptr),
71           pos_3dof(nullptr),
72           sorted_pos_3dof(nullptr) {}
73 
74     // Counters
75     uint num_rigid_bodies;  ///< number of rigid bodies in a system
76     uint num_fluid_bodies;  ///< number of fluid bodies in the system
77 
78     // Object data
79     std::vector<real3>* pos_rigid;       ///< [num_rigid_bodies] rigid body positions
80     std::vector<quaternion>* rot_rigid;  ///< [num_rigid_bodies] rigid body rotations
81     std::vector<char>* active_rigid;     ///< [num_rigid_bodies] flags indicating rigid bodies that active
82     std::vector<char>* collide_rigid;    ///< [num_rigid_bodies] flags indicating bodies that participate in collision
83 
84     // Information for 3dof nodes
85     std::vector<real3>* pos_3dof;         ///< [num_fluid_bodies] 3-dof particle positions
86     std::vector<real3>* sorted_pos_3dof;  ///< [num_fluid_bodies] (output) 3-dof particle positions sorted by bin index
87 };
88 
89 /// Global data for the custom Chrono multicore collision system.
90 class ChApi ChCollisionData {
91   public:
ChCollisionData(bool owns_data)92     ChCollisionData(bool owns_data)
93         : owns_state_data(owns_data),
94           //
95           collision_envelope(0),
96           //
97           num_rigid_contacts(0),
98           num_rigid_fluid_contacts(0),
99           num_fluid_contacts(0),
100           num_rigid_shapes(0),
101           //
102           bins_per_axis(vec3(10, 10, 10)),
103           //
104           min_bounding_point(real3(0)),
105           max_bounding_point(real3(0)),
106           global_origin(real3(0)),
107           bin_size(real3(0)),
108           num_bins(0),
109           num_active_bins(0),
110           num_bin_aabb_intersections(0),
111           num_possible_collisions(0),
112           //
113           rigid_min_bounding_point(real3(0)),
114           rigid_max_bounding_point(real3(0)),
115           //
116           p_kernel_radius(real(0.04)),
117           p_collision_envelope(0),
118           p_collision_family(short2(1, 0x7FFF)),
119           //
120           ff_min_bounding_point(real3(0)),
121           ff_max_bounding_point(real3(0)),
122           ff_bins_per_axis(vec3(0)) {
123         if (owns_data) {
124             state_data.pos_rigid = new std::vector<real3>;
125             state_data.rot_rigid = new std::vector<quaternion>;
126             state_data.active_rigid = new std::vector<char>;
127             state_data.collide_rigid = new std::vector<char>;
128 
129             state_data.pos_3dof = new std::vector<real3>;
130             state_data.sorted_pos_3dof = new std::vector<real3>;
131         }
132     }
133 
~ChCollisionData()134     ~ChCollisionData() {
135         if (owns_state_data) {
136             delete state_data.pos_rigid;
137             delete state_data.rot_rigid;
138             delete state_data.active_rigid;
139             delete state_data.collide_rigid;
140 
141             delete state_data.pos_3dof;
142             delete state_data.sorted_pos_3dof;
143         }
144     }
145 
146     bool owns_state_data;  ///< if false, state data set from outside
147 
148     state_container state_data;  ///< state data arrays
149     shape_container shape_data;  ///< shape information data arrays
150 
151     real collision_envelope;  ///< collision envelope for rigid shapes
152 
153     real p_collision_envelope;  ///< collision envelope for 3-dof particles
154     real p_kernel_radius;       ///< 3-dof particle radius
155     short2 p_collision_family;  ///< collision family and family mask for 3-dof particles
156 
157     // Collision detection output data
158     // -------------------------------
159 
160     std::vector<real3> aabb_min;  ///< list of bounding boxes minimum point
161     std::vector<real3> aabb_max;  ///< list of bounding boxes maximum point
162 
163     std::vector<long long> pair_shapeIDs;     ///< shape IDs for each shape pair (encoded in a single long long)
164     std::vector<long long> contact_shapeIDs;  ///< shape IDs for each contact (encoded in a single long long)
165 
166     // Rigid-rigid geometric collision data
167     std::vector<real3> norm_rigid_rigid;  ///< [num_rigid_contacts] normal for each rigid-rigid contact
168     std::vector<real3> cpta_rigid_rigid;  ///< [num_rigid_contacts] point on first shape for each rigid-rigid contact
169     std::vector<real3> cptb_rigid_rigid;  ///< [num_rigid_contacts] point on second shape for each rigid-rigid contact
170     std::vector<real> dpth_rigid_rigid;   ///< [num_rigid_contacts] penetration depth for each rigid-rigid contact
171     std::vector<real> erad_rigid_rigid;  ///< [num_rigid_contacts] effective contact radius for each rigid-rigid contact
172     std::vector<vec2> bids_rigid_rigid;  ///< [num_rigid_contacts] body IDs for each rigid-rigid contact pair
173 
174     // Rigid-particle geometric collision data
175     std::vector<real3> norm_rigid_fluid;    ///< [num_rigid_fluid_contacts]
176     std::vector<real3> cpta_rigid_fluid;    ///< [num_rigid_fluid_contacts]
177     std::vector<real> dpth_rigid_fluid;     ///< [num_rigid_fluid_contacts]
178     std::vector<int> neighbor_rigid_fluid;  ///< [num_rigid_fluid_contacts]
179     std::vector<int> c_counts_rigid_fluid;  ///< [num_fluid_bodies]
180 
181     // 3dof particle neighbor information
182     std::vector<int> neighbor_3dof_3dof;  ///< [num_fluid_contacts]
183     std::vector<int> c_counts_3dof_3dof;  ///< [num_fluid_contacts]
184 
185     // Sorting map for 3dof particles
186     std::vector<int> particle_indices_3dof;
187     std::vector<int> reverse_mapping_3dof;
188 
189     // Broadphase Data
190     vec3 bins_per_axis;               ///< number of slices along each axis of the collision detection grid
191     real3 bin_size;                   ///< bin sizes in each direction
192     real3 inv_bin_size;               ///< bin size reciprocals in each direction
193     real3 min_bounding_point;         ///< LBR (left-bottom-rear) corner of union of all AABBs
194     real3 max_bounding_point;         ///< RTF (right-top-front) corner of union of all AABBs
195     real3 global_origin;              ///< grid zero point (same as LBR)
196     uint num_bins;                    ///< total number of bins
197     uint num_bin_aabb_intersections;  ///< number of bin - shape AABB intersections
198     uint num_active_bins;             ///< number of bins intersecting at least one shape AABB
199     uint num_possible_collisions;     ///< number of candidate collisions from broadphase
200 
201     real3 rigid_min_bounding_point;  ///< LBR (left-bottom-rear) corner of union of rigid AABBs
202     real3 rigid_max_bounding_point;  ///< RTF (right-top-front) corner of union of rigid AABBs
203 
204     vec3 ff_bins_per_axis;        ///< grid resolution for fluid particles
205     real3 ff_min_bounding_point;  ///< LBR (left-bottom-rear) corner of union of fluid AABBs
206     real3 ff_max_bounding_point;  ///< RTF (right-top-front) corner of union of fluid AABBs
207 
208     std::vector<uint> bin_intersections;    ///< [num_rigid_shapes+1] number of bin intersections for each shape AABB
209     std::vector<uint> bin_number;           ///< [num_bin_aabb_intersections] bin index for bin-shape AABB intersections
210     std::vector<uint> bin_aabb_number;      ///< [num_bin_aabb_intersections] shape ID for bin-shape AABB intersections
211     std::vector<uint> bin_active;           ///< [num_active_bins] bin index of active bins (no duplicates)
212     std::vector<uint> bin_start_index;      ///< [num_active_bins+1]
213     std::vector<uint> bin_start_index_ext;  ///< [num_bins+1]
214     std::vector<uint> bin_num_contact;      ///< [num_active_bins+1]
215 
216     // Indexing variables
217     // ------------------
218 
219     uint num_rigid_shapes;          ///< number of collision models in a system
220     uint num_rigid_contacts;        ///< number of contacts between rigid bodies in a system
221     uint num_rigid_fluid_contacts;  ///< number of contacts between rigid and fluid objects
222     uint num_fluid_contacts;        ///< number of contacts between fluid objects
223 };
224 
225 /// @} collision_mc
226 
227 }  // end namespace collision
228 }  // end namespace chrono
229