Hi
I am coding a C# script in grasshopper. My aim is to find the bouning box either has minimum volume or align with long axis of the object.
One of my issue: For searching the minimum volume, I set a very large number to compare with at first place. I feel there could be a better way. If someone can help, it will be very thankful.
I post the code and attach the file.
bounding box EX.gh (11.0 KB)
private void RunScript(List<GeometryBase> geo, int n, int VorA, ref object A, ref object Box)
{
//make a list of planes for bounding box to align with.
double angle = Math.PI / n;
Plane myPlaneZ = Plane.WorldXY;
List<Plane> myPlanesZ = new List<Plane>();
List<Plane> myPlanesY = new List<Plane>();
List<Plane> myPlanesX = new List<Plane>();
//create a list of rotated planes align z axis.
for (int i = 0; i < n; i++)
{
myPlaneZ.Rotate(angle, Vector3d.ZAxis, Point3d.Origin);
myPlanesZ.Add(myPlaneZ);
}
//rotate each plane align y axis. Planes are structs. You need to get them out of the list (which creates a copy) modify the copy, then put it back into the list.
for (int i = 0; i < myPlanesZ.Count; i++)
{
for (int j = 0; j < n; j++)
{
Plane p = myPlanesZ[i];
p.Rotate(angle, Vector3d.YAxis, Point3d.Origin);
myPlanesZ[i] = p;
myPlanesY.Add(myPlanesZ[i]);
}
}
//rotate each plane align x axis.
for (int i = 0; i < myPlanesY.Count; i++)
{
for (int j = 0; j < n; j++)
{
Plane p = myPlanesY[i];
p.Rotate(angle, Vector3d.XAxis, Point3d.Origin);
myPlanesY[i] = p;
myPlanesX.Add(myPlanesY[i]);
}
}
//create index for searching.
int index = 0;
//search minimum volume and return index.
if (VorA == 0)
{
double minVolume = 100000000000000000000.0;
for (int i = 0; i < myPlanesX.Count; i++)
{
Plane p = myPlanesX[i];
//union bounding box, for bounding each object use code "var bbox = geo.GetBoundingBox(p);"
BoundingBox bboxUnion = BoundingBox.Empty;
foreach (var obj in geo)
{
var bbox = obj.GetBoundingBox(p);
bboxUnion.Union(bbox);
}
//get bounding box volume.
var box = new Box(bboxUnion);
double volume = box.Volume;
//find the minimum.
if (volume <= minVolume)
{
minVolume = volume;
index = i;
}
}
Print("Minimum Volume");
A = minVolume;
}
//search longest axis and return index.
if (VorA == 1)
{
double longestAxis = 0.0;
for (int i = 0; i < myPlanesX.Count; i++)
{
Plane p = myPlanesX[i];
//union bounding box, for bounding each obj use code "var bbox = geo.GetBoundingBox(p);"
BoundingBox bboxUnion = BoundingBox.Empty;
foreach (var obj in geo)
{
var bbox = obj.GetBoundingBox(p);
bboxUnion.Union(bbox);
}
//get box width.
var box = new Box(bboxUnion);
double axis = box.X.Length;
//find the longest.
if (axis >= longestAxis)
{
longestAxis = axis;
index = i;
}
}
Print("Longest Axis");
A = longestAxis;
}
//output bounding box.
Plane plane0 = myPlanesX[index];
BoundingBox bboxUnion0 = BoundingBox.Empty;
foreach (var obj in geo)
{
var bbox = obj.GetBoundingBox(plane0);
bboxUnion0.Union(bbox);
}
//transform the box to worldXY.
var box0 = new Box(bboxUnion0);
box0.Transform(Transform.PlaneToPlane(Plane.WorldXY, plane0));
Box = box0;
}