Hello. How can I make the position and dimensions of the bounding box like the 3D model?
I asked the AI to write me such a script, and it works (I tried it):
import vtk
import slicer
def createBoundingBoxModel(inputModelNode, boundingBoxModelName="BoundingBox"):
"""
Create a model node that represents the bounding box of the input model.
Args:
inputModelNode: The model node whose bounding box to visualize
boundingBoxModelName: Name for the new bounding box model node
Returns:
The created bounding box model node
"""
if not inputModelNode or not inputModelNode.GetPolyData():
raise ValueError("Invalid input model node")
# Get bounding box of the input model
bounds = inputModelNode.GetPolyData().GetBounds()
# bounds = [xmin, xmax, ymin, ymax, zmin, zmax]
# Create a cube source with the bounding box dimensions
cubeSource = vtk.vtkCubeSource()
cubeSource.SetBounds(bounds)
cubeSource.Update()
# Alternative approach: Create cube and transform it
# center = [(bounds[0] + bounds[1])/2, (bounds[2] + bounds[3])/2, (bounds[4] + bounds[5])/2]
# size = [bounds[1] - bounds[0], bounds[3] - bounds[2], bounds[5] - bounds[4]]
# cubeSource.SetCenter(center)
# cubeSource.SetXLength(size[0])
# cubeSource.SetYLength(size[1])
# cubeSource.SetZLength(size[2])
# Create a new model node for the bounding box
boundingBoxModelNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLModelNode", boundingBoxModelName)
boundingBoxModelNode.SetAndObservePolyData(cubeSource.GetOutput())
# Create display node to make it visible
boundingBoxModelNode.CreateDefaultDisplayNodes()
displayNode = boundingBoxModelNode.GetDisplayNode()
# Configure display properties for wireframe representation
displayNode.SetRepresentation(displayNode.WireframeRepresentation)
displayNode.SetColor(1.0, 1.0, 0.0) # Yellow color
displayNode.SetLineWidth(2)
displayNode.SetOpacity(0.8)
return boundingBoxModelNode