<commit_before>#include "ChCBroadphase.h"
using namespace chrono::collision;

typedef thrust::pair<real3, real3> bbox;
// reduce a pair of bounding boxes (a,b) to a bounding box containing a and b
struct bbox_reduction: public thrust::binary_function<bbox, bbox, bbox> {
		bbox __host__ __device__ operator()(bbox a, bbox b) {
			real3 ll = R3(fmin(a.first.x, b.first.x), fmin(a.first.y, b.first.y), fmin(a.first.z, b.first.z));     // lower left corner
			real3 ur = R3(fmax(a.second.x, b.second.x), fmax(a.second.y, b.second.y), fmax(a.second.z, b.second.z));     // upper right corner
			return bbox(ll, ur);
		}
};

// convert a point to a bbox containing that point, (point) -> (point, point)
struct bbox_transformation: public thrust::unary_function<real3, bbox> {
		bbox __host__ __device__ operator()(real3 point) {
			return bbox(point, point);
		}
};

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ChCBroadphase::ChCBroadphase() {
	number_of_contacts_possible = 0;
	val = 0;
	last_active_bin = 0;
	number_of_bin_intersections = 0;
	numAABB = 0;
	grid_size = I3(20, 20, 20);
	min_body_per_bin = 25;
	max_body_per_bin = 50;
	// TODO: Should make aabb_data organization less confusing, compiler should switch depending on if the user passes a host/device vector
	// TODO: Should be able to tune bins_per_axis, it's nice to have as a parameter though!
	// TODO: As the collision detection is progressing, we should free up vectors that are no longer being used! For example, Bin_Intersections is only used in steps 4&5
	// TODO: Make sure that aabb_data isn't being duplicated for this code (should be a reference to user's code to conserve space)
	// TODO: Fix debug mode to work with compiler settings, add more debugging features (a drawing function would be nice!)
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void ChCBroadphase::setBinsPerAxis(int3 binsPerAxis) {
	grid_size = binsPerAxis;
	cout<<"Set BPA: "<<grid_size.x<<" "<<grid_size.y<<" "<<grid_size.z<<endl;

}
int3 ChCBroadphase::getBinsPerAxis() {
	return grid_size;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//template<class T>
//inline int3 __host__ __device__ HashMax(     //CHANGED: For maximum point, need to check if point lies on edge of bin (TODO: Hmm, fmod still doesn't work completely)
//		const T &A,
//		const real3 & bin_size_vec) {
//	int3 temp;
//	temp.x = A.x / bin_size_vec.x;
//	if (!fmod(A.x, bin_size_vec.x) && temp.x != 0)
//		temp.x--;
//	temp.y = A.y / bin_size_vec.y;
//	if (!fmod(A.y, bin_size_vec.y) && temp.y != 0)
//		temp.y--;
//	temp.z = A.z / bin_size_vec.z;
//	if (!fmod(A.z, bin_size_vec.z) && temp.z != 0)
//		temp.z--;
//
//	//cout << temp.x << " " << temp.y << " " << temp.z << endl;
//	return temp;
//}

template<class T>
inline int3 __host__ __device__ HashMin(const T &A, const real3 &bin_size_vec) {
	int3 temp;
	temp.x = A.x * bin_size_vec.x;
	temp.y = A.y * bin_size_vec.y;
	temp.z = A.z * bin_size_vec.z;
	//cout << temp.x << " " << temp.y << " " << temp.z << endl;
	return temp;
}

template<class T>
inline uint __host__ __device__ Hash_Index(const T &A, int3 grid_size) {
	//return ((A.x * 73856093) ^ (A.y * 19349663) ^ (A.z * 83492791));
	return ((A.z * grid_size.y) * grid_size.x) + (A.y * grid_size.x) + A.x;
}

//Function to Count AABB Bin intersections
inline void __host__ __device__ function_Count_AABB_BIN_Intersection(const uint &index, const real3 *aabb_data, const real3 &bin_size_vec, const uint &number_of_particles, uint *Bins_Intersected) {
	int3 gmin = HashMin(aabb_data[index], bin_size_vec);
	int3 gmax = HashMin(aabb_data[index + number_of_particles], bin_size_vec);
	Bins_Intersected[index] = (gmax.x - gmin.x + 1) * (gmax.y - gmin.y + 1) * (gmax.z - gmin.z + 1);
}

//--------------------------------------------------------------------------
void ChCBroadphase::host_Count_AABB_BIN_Intersection(const real3 *aabb_data, uint *Bins_Intersected) {
#pragma omp parallel for
	for (int i = 0; i < numAABB; i++) {
		function_Count_AABB_BIN_Intersection(i, aabb_data, bin_size_vec, numAABB, Bins_Intersected);
	}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Function to Store AABB Bin Intersections

inline void __host__ __device__ function_Store_AABB_BIN_Intersection(
		const uint &index,
		const real3 *aabb_data,
		const uint *Bins_Intersected,
		const real3 &bin_size_vec,
		const int3 &grid_size,
		const uint &number_of_particles,
		uint *bin_number,
		uint *body_number) {
	uint count = 0, i, j, k;
	int3 gmin = HashMin(aabb_data[index], bin_size_vec);
	int3 gmax = HashMin(aabb_data[index + number_of_particles], bin_size_vec);
	uint mInd = (index == 0) ? 0 : Bins_Intersected[index - 1];
	for (i = gmin.x; i <= gmax.x; i++) {
		for (j = gmin.y; j <= gmax.y; j++) {
			for (k = gmin.z; k <= gmax.z; k++) {
				uint3 location;
				location.x = i;
				location.y = j;
				location.z = k;
				bin_number[mInd + count] = Hash_Index(location,grid_size);
				body_number[mInd + count] = index;
				count++;
			}
		}
	}
}

//--------------------------------------------------------------------------

void ChCBroadphase::host_Store_AABB_BIN_Intersection(const real3 *aabb_data, const uint *Bins_Intersected, uint *bin_number, uint *body_number) {
#pragma omp parallel for
	for (int i = 0; i < numAABB; i++) {
		function_Store_AABB_BIN_Intersection(i, aabb_data, Bins_Intersected, bin_size_vec,grid_size, numAABB, bin_number, body_number);
	}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Function to count AABB AABB intersection

void __host__ __device__ function_Count_AABB_AABB_Intersection(
		const uint index,<commit_msg>Revert "remove inline"<commit_after>#include "ChCBroadphase.h"
using namespace chrono::collision;

typedef thrust::pair<real3, real3> bbox;
// reduce a pair of bounding boxes (a,b) to a bounding box containing a and b
struct bbox_reduction: public thrust::binary_function<bbox, bbox, bbox> {
		bbox __host__ __device__ operator()(bbox a, bbox b) {
			real3 ll = R3(fmin(a.first.x, b.first.x), fmin(a.first.y, b.first.y), fmin(a.first.z, b.first.z));     // lower left corner
			real3 ur = R3(fmax(a.second.x, b.second.x), fmax(a.second.y, b.second.y), fmax(a.second.z, b.second.z));     // upper right corner
			return bbox(ll, ur);
		}
};

// convert a point to a bbox containing that point, (point) -> (point, point)
struct bbox_transformation: public thrust::unary_function<real3, bbox> {
		bbox __host__ __device__ operator()(real3 point) {
			return bbox(point, point);
		}
};

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ChCBroadphase::ChCBroadphase() {
	number_of_contacts_possible = 0;
	val = 0;
	last_active_bin = 0;
	number_of_bin_intersections = 0;
	numAABB = 0;
	grid_size = I3(20, 20, 20);
	min_body_per_bin = 25;
	max_body_per_bin = 50;
	// TODO: Should make aabb_data organization less confusing, compiler should switch depending on if the user passes a host/device vector
	// TODO: Should be able to tune bins_per_axis, it's nice to have as a parameter though!
	// TODO: As the collision detection is progressing, we should free up vectors that are no longer being used! For example, Bin_Intersections is only used in steps 4&5
	// TODO: Make sure that aabb_data isn't being duplicated for this code (should be a reference to user's code to conserve space)
	// TODO: Fix debug mode to work with compiler settings, add more debugging features (a drawing function would be nice!)
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void ChCBroadphase::setBinsPerAxis(int3 binsPerAxis) {
	grid_size = binsPerAxis;
	cout<<"Set BPA: "<<grid_size.x<<" "<<grid_size.y<<" "<<grid_size.z<<endl;

}
int3 ChCBroadphase::getBinsPerAxis() {
	return grid_size;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//template<class T>
//inline int3 __host__ __device__ HashMax(     //CHANGED: For maximum point, need to check if point lies on edge of bin (TODO: Hmm, fmod still doesn't work completely)
//		const T &A,
//		const real3 & bin_size_vec) {
//	int3 temp;
//	temp.x = A.x / bin_size_vec.x;
//	if (!fmod(A.x, bin_size_vec.x) && temp.x != 0)
//		temp.x--;
//	temp.y = A.y / bin_size_vec.y;
//	if (!fmod(A.y, bin_size_vec.y) && temp.y != 0)
//		temp.y--;
//	temp.z = A.z / bin_size_vec.z;
//	if (!fmod(A.z, bin_size_vec.z) && temp.z != 0)
//		temp.z--;
//
//	//cout << temp.x << " " << temp.y << " " << temp.z << endl;
//	return temp;
//}

template<class T>
inline int3 __host__ __device__ HashMin(const T &A, const real3 &bin_size_vec) {
	int3 temp;
	temp.x = A.x * bin_size_vec.x;
	temp.y = A.y * bin_size_vec.y;
	temp.z = A.z * bin_size_vec.z;
	//cout << temp.x << " " << temp.y << " " << temp.z << endl;
	return temp;
}

template<class T>
inline uint __host__ __device__ Hash_Index(const T &A, int3 grid_size) {
	//return ((A.x * 73856093) ^ (A.y * 19349663) ^ (A.z * 83492791));
	return ((A.z * grid_size.y) * grid_size.x) + (A.y * grid_size.x) + A.x;
}

//Function to Count AABB Bin intersections
inline void __host__ __device__ function_Count_AABB_BIN_Intersection(const uint &index, const real3 *aabb_data, const real3 &bin_size_vec, const uint &number_of_particles, uint *Bins_Intersected) {
	int3 gmin = HashMin(aabb_data[index], bin_size_vec);
	int3 gmax = HashMin(aabb_data[index + number_of_particles], bin_size_vec);
	Bins_Intersected[index] = (gmax.x - gmin.x + 1) * (gmax.y - gmin.y + 1) * (gmax.z - gmin.z + 1);
}

//--------------------------------------------------------------------------
void ChCBroadphase::host_Count_AABB_BIN_Intersection(const real3 *aabb_data, uint *Bins_Intersected) {
#pragma omp parallel for
	for (int i = 0; i < numAABB; i++) {
		function_Count_AABB_BIN_Intersection(i, aabb_data, bin_size_vec, numAABB, Bins_Intersected);
	}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Function to Store AABB Bin Intersections

inline void __host__ __device__ function_Store_AABB_BIN_Intersection(
		const uint &index,
		const real3 *aabb_data,
		const uint *Bins_Intersected,
		const real3 &bin_size_vec,
		const int3 &grid_size,
		const uint &number_of_particles,
		uint *bin_number,
		uint *body_number) {
	uint count = 0, i, j, k;
	int3 gmin = HashMin(aabb_data[index], bin_size_vec);
	int3 gmax = HashMin(aabb_data[index + number_of_particles], bin_size_vec);
	uint mInd = (index == 0) ? 0 : Bins_Intersected[index - 1];
	for (i = gmin.x; i <= gmax.x; i++) {
		for (j = gmin.y; j <= gmax.y; j++) {
			for (k = gmin.z; k <= gmax.z; k++) {
				uint3 location;
				location.x = i;
				location.y = j;
				location.z = k;
				bin_number[mInd + count] = Hash_Index(location,grid_size);
				body_number[mInd + count] = index;
				count++;
			}
		}
	}
}

//--------------------------------------------------------------------------

void ChCBroadphase::host_Store_AABB_BIN_Intersection(const real3 *aabb_data, const uint *Bins_Intersected, uint *bin_number, uint *body_number) {
#pragma omp parallel for
	for (int i = 0; i < numAABB; i++) {
		function_Store_AABB_BIN_Intersection(i, aabb_data, Bins_Intersected, bin_size_vec,grid_size, numAABB, bin_number, body_number);
	}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Function to count AABB AABB intersection

inline void __host__ __device__ function_Count_AABB_AABB_Intersection(
		const uint index,
{'fraction_non_alphanumeric': 0.2787830264211369, 'fraction_numerical': 0.013290632506004804, 'mean_word_length': 4.2176274018379285, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3394895', 'n_tokens_mistral': 4518, 'n_tokens_neox': 4189, 'n_words': 1084}