Skip to content

Commit

Permalink
Fix bug with Part.BoundingBox (Fix #370)
Browse files Browse the repository at this point in the history
  • Loading branch information
djungelorm committed Feb 21, 2017
1 parent 1cea21f commit 4b5a9d0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions service/SpaceCenter/CHANGES.txt
Expand Up @@ -4,6 +4,7 @@ v0.3.8
* Add SpaceCenter.UIVisible to show/hide the UI (#355)
* Fix bug with DockingPort.Rotation (#371)
* Fix bug with Part.Rotation (#371)
* Fix bug with Part.BoundingBox (#370)

v0.3.7
* Add custom reference frames (ReferenceFrame.CreateRelative and CreateHybrid) (#252)
Expand Down
13 changes: 7 additions & 6 deletions service/SpaceCenter/src/ExtensionMethods/PartExtensions.cs
Expand Up @@ -128,13 +128,14 @@ public static Vector3d CenterOfMass (this Part part)
/// </remarks>
public static Bounds GetBounds (this Part part, ReferenceFrame referenceFrame)
{
var colliders = part.GetComponentsInChildren<MeshCollider> ();
var bounds = new Bounds (referenceFrame.PositionFromWorldSpace (part.WCoM), Vector3.zero);
foreach (var collider in colliders) {
var vertices = collider.sharedMesh.bounds.ToVertices ();
for (int i = 0; i < vertices.Length; i++) {
// part space -> world space -> reference frame space
var vertex = referenceFrame.PositionFromWorldSpace (collider.transform.TransformPoint (vertices [i]));
var meshes = part.GetComponentsInChildren<MeshFilter> ();
for (int i = 0; i < meshes.Length; i++) {
var mesh = meshes [i];
var vertices = mesh.mesh.bounds.ToVertices ();
for (int j = 0; j < vertices.Length; j++) {
// mesh space -> world space -> reference frame space
var vertex = referenceFrame.PositionFromWorldSpace (mesh.transform.TransformPoint (vertices [j]));
bounds.Encapsulate (vertex);
}
}
Expand Down
10 changes: 8 additions & 2 deletions service/SpaceCenter/test/test_parts_part.py
Expand Up @@ -61,8 +61,8 @@ def test_root_part(self):
modules.extend(['FARBasicDragModel', 'FARControlSys'])
self.assertItemsEqual(modules, [x.name for x in part.modules])
box = part.bounding_box(part.reference_frame)
self.assertAlmostEqual((-1.19, -0.49, -1.16), box[0], places=2)
self.assertAlmostEqual((1.19, 1.16, 1.16), box[1], places=2)
self.assertAlmostEqual((-1.223, -0.574, -1.223), box[0], places=2)
self.assertAlmostEqual((1.223, 1.273, 1.223), box[1], places=2)
self.assertIsNone(part.cargo_bay)
self.assertIsNone(part.control_surface)
self.assertIsNone(part.decoupler)
Expand Down Expand Up @@ -375,6 +375,9 @@ def test_landing_gear(self):
modules.append('FARBasicDragModel')
self.assertItemsEqual(modules, [x.name for x in part.modules])
self.assertIsNotNone(part.landing_gear)
box = part.bounding_box(part.reference_frame)
self.assertAlmostEqual((-0.495, -1.122, -0.569), box[0], places=2)
self.assertAlmostEqual((0.495, 0.232, 0.679), box[1], places=2)

def test_landing_leg(self):
part = self.parts.with_title('LT-1 Landing Struts')[0]
Expand Down Expand Up @@ -410,6 +413,9 @@ def test_landing_leg(self):
modules.append('FARBasicDragModel')
self.assertItemsEqual(modules, [x.name for x in part.modules])
self.assertIsNotNone(part.landing_leg)
box = part.bounding_box(part.reference_frame)
self.assertAlmostEqual((-0.150, -1.016, -0.279), box[0], places=2)
self.assertAlmostEqual((0.150, 0.239, 0.377), box[1], places=2)

def test_launch_clamp(self):
part = self.parts.with_title('TT18-A Launch Stability Enhancer')[0]
Expand Down

0 comments on commit 4b5a9d0

Please sign in to comment.