This is a conversion to C# from C++ incase some needs it, thanks to ChatGPT:
private void RunScript(Brep x, Brep y, ref object A)
{
//example with boxes
Box box1 = Box.Unset;
Plane xPlane = Plane.Unset;
x.Surfaces[0].FrameAt(0, 0, out xPlane);
x.GetBoundingBox(xPlane, out box1);
Box box2 = Box.Unset;
Plane yPlane = Plane.Unset;
y.Surfaces[0].FrameAt(0, 0, out yPlane);
y.GetBoundingBox(yPlane, out box2);
A = GetCollision(box1,box2);
//example with bounding boxes
// A = GetCollision(x.GetBoundingBox(true), y.GetBoundingBox(true));
}
// <Custom additional code>
// define the operations to be used in our 3D vertices
// define the operations to be used in our 3D vertices
public struct Vec3
{
public float x, y, z;
public Vec3(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
}
public static Vec3 operator -(Vec3 lhs, Vec3 rhs)
{
return new Vec3 { x = lhs.x - rhs.x, y = lhs.y - rhs.y, z = lhs.z - rhs.z };
}
public static float operator *(Vec3 lhs, Vec3 rhs)
{
return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
}
public static Vec3 operator ^(Vec3 lhs, Vec3 rhs)
{
return new Vec3 { x = lhs.y * rhs.z - lhs.z * rhs.y, y = lhs.z * rhs.x - lhs.x * rhs.z, z = lhs.x * rhs.y - lhs.y * rhs.x };
}
public static Vec3 operator *(Vec3 lhs, float rhs)
{
return new Vec3 { x = lhs.x * rhs, y = lhs.y * rhs, z = lhs.z * rhs };
}
}
// set the relevant elements of our oriented bounding box
public struct OBB
{
public Vec3 Pos, AxisX, AxisY, AxisZ, Half_size;
public OBB(BoundingBox bbox)
{
// Calculate the center position of the bounding box
Pos = new Vec3((float) (bbox.Min.X + bbox.Max.X) / 2f,
(float) (bbox.Min.Y + bbox.Max.Y) / 2f,
(float) (bbox.Min.Z + bbox.Max.Z) / 2f);
// Calculate the size of the bounding box in each dimension
float sizeX = (float) (bbox.Max.X - bbox.Min.X) / 2f;
float sizeY = (float) (bbox.Max.Y - bbox.Min.Y) / 2f;
float sizeZ = (float) (bbox.Max.Z - bbox.Min.Z) / 2f;
// Set the axis vectors
AxisX = new Vec3(1f, 0f, 0f);
AxisY = new Vec3(0f, 1f, 0f);
AxisZ = new Vec3(0f, 0f, 1f);
// Set the half size of the bounding box
Half_size = new Vec3(sizeX, sizeY, sizeZ);
}
public OBB(Box box)
{
// Calculate the center position of the box
var center_points = box.PointAt(0.5, 0.5, 0.5);
Pos = new Vec3((float) (center_points.X),
(float) (center_points.Y),
(float) (center_points.Z));
// Calculate the axis vectors
AxisX = new Vec3((float) (box.Plane.XAxis.X),
(float) (box.Plane.XAxis.Y),
(float) (box.Plane.XAxis.Z));
AxisY = new Vec3((float) (box.Plane.YAxis.X),
(float) (box.Plane.YAxis.Y),
(float) (box.Plane.YAxis.Z));
AxisZ = new Vec3((float) (box.Plane.ZAxis.X),
(float) (box.Plane.ZAxis.Y),
(float) (box.Plane.ZAxis.Z));
// Set the half size of the box
Half_size = new Vec3((float) (box.X.Length) / 2f,
(float) (box.Y.Length) / 2f,
(float) (box.Z.Length) / 2f);
}
}
// check if there's a separating plane in between the selected axes
public static bool GetSeparatingPlane(Vec3 RPos, Vec3 Plane, OBB box1, OBB box2)
{
return Math.Abs(RPos * Plane) >
(Math.Abs((box1.AxisX * box1.Half_size.x) * Plane) +
Math.Abs((box1.AxisY * box1.Half_size.y) * Plane) +
Math.Abs((box1.AxisZ * box1.Half_size.z) * Plane) +
Math.Abs((box2.AxisX * box2.Half_size.x) * Plane) +
Math.Abs((box2.AxisY * box2.Half_size.y) * Plane) +
Math.Abs((box2.AxisZ * box2.Half_size.z) * Plane));
}
// test for separating planes in all 15 axes
public static int GetCollision(BoundingBox _box1, BoundingBox _box2) // -1_outside_0_intersected_1_inside
{
OBB box1 = new OBB(_box1);
OBB box2 = new OBB(_box2);
if(_box1.Contains(_box2))
return 1;
Vec3 RPos;
RPos = box2.Pos - box1.Pos;
bool result = !(GetSeparatingPlane(RPos, box1.AxisX, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisY, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisZ, box1, box2) ||
GetSeparatingPlane(RPos, box2.AxisX, box1, box2) ||
GetSeparatingPlane(RPos, box2.AxisY, box1, box2) ||
GetSeparatingPlane(RPos, box2.AxisZ, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisX ^ box2.AxisX, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisX ^ box2.AxisY, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisX ^ box2.AxisZ, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisY ^ box2.AxisX, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisY ^ box2.AxisY, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisY ^ box2.AxisZ, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisZ ^ box2.AxisX, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisZ ^ box2.AxisY, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisZ ^ box2.AxisZ, box1, box2));
if(result)
return 0;
else
return -1;
}
public static int GetCollision(Box _box1, Box _box2) // -1_outside_0_intersected_1_inside
{
OBB box1 = new OBB(_box1);
OBB box2 = new OBB(_box2);
if(_box1.Contains(_box2))
return 1;
Vec3 RPos;
RPos = box2.Pos - box1.Pos;
bool result = !(GetSeparatingPlane(RPos, box1.AxisX, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisY, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisZ, box1, box2) ||
GetSeparatingPlane(RPos, box2.AxisX, box1, box2) ||
GetSeparatingPlane(RPos, box2.AxisY, box1, box2) ||
GetSeparatingPlane(RPos, box2.AxisZ, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisX ^ box2.AxisX, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisX ^ box2.AxisY, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisX ^ box2.AxisZ, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisY ^ box2.AxisX, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisY ^ box2.AxisY, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisY ^ box2.AxisZ, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisZ ^ box2.AxisX, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisZ ^ box2.AxisY, box1, box2) ||
GetSeparatingPlane(RPos, box1.AxisZ ^ box2.AxisZ, box1, box2));
if(result)
return 0;
else
return -1;
}