Simplified SkeletonBounds AABB computation.

This commit is contained in:
NathanSweet 2013-09-24 15:37:46 +02:00
parent dda304faa0
commit 8cec4eb354
5 changed files with 50 additions and 146 deletions

View File

@ -36,78 +36,24 @@ using System.Collections.Generic;
namespace Spine { namespace Spine {
public class SkeletonBounds { public class SkeletonBounds {
private bool aabb;
private List<Polygon> polygonPool = new List<Polygon>(); private List<Polygon> polygonPool = new List<Polygon>();
private float minX, minY, maxX, maxY;
public List<BoundingBoxAttachment> BoundingBoxes { get; private set; } public List<BoundingBoxAttachment> BoundingBoxes { get; private set; }
public List<Polygon> Polygons { get; private set; } public List<Polygon> Polygons { get; private set; }
public float MinX { get { return minX; } set { minX = value; } }
private float minX; public float MinY { get { return minY; } set { minY = value; } }
public float MinX { public float MaxX { get { return maxX; } set { maxX = value; } }
get { public float MaxY { get { return maxY; } set { maxY = value; } }
if (!aabb) aabbCompute(); public float Width { get { return maxX - minX; } }
return minX; public float Height { get { return maxY - minY; } }
}
private set {
minX = value;
}
}
private float maxX;
public float MaxX {
get {
if (!aabb) aabbCompute();
return maxX;
}
private set {
maxX = value;
}
}
private float minY;
public float MinY {
get {
if (!aabb) aabbCompute();
return minY;
}
private set {
minY = value;
}
}
private float maxY;
public float MaxY {
get {
if (!aabb) aabbCompute();
return maxY;
}
private set {
maxY = value;
}
}
public float Width {
get {
if (!aabb) aabbCompute();
return maxX - minX;
}
}
public float Height {
get {
if (!aabb) aabbCompute();
return maxY - minY;
}
}
public SkeletonBounds () { public SkeletonBounds () {
BoundingBoxes = new List<BoundingBoxAttachment>(); BoundingBoxes = new List<BoundingBoxAttachment>();
Polygons = new List<Polygon>(); Polygons = new List<Polygon>();
} }
public void Update (Skeleton skeleton) { public void Update (Skeleton skeleton, bool updateAabb) {
aabb = false;
List<BoundingBoxAttachment> boundingBoxes = BoundingBoxes; List<BoundingBoxAttachment> boundingBoxes = BoundingBoxes;
List<Polygon> polygons = Polygons; List<Polygon> polygons = Polygons;
List<Slot> slots = skeleton.slots; List<Slot> slots = skeleton.slots;
@ -139,6 +85,8 @@ namespace Spine {
if (polygon.Vertices.Length < count) polygon.Vertices = new float[count]; if (polygon.Vertices.Length < count) polygon.Vertices = new float[count];
boundingBox.ComputeWorldVertices(x, y, slot.bone, polygon.Vertices); boundingBox.ComputeWorldVertices(x, y, slot.bone, polygon.Vertices);
} }
if (updateAabb) aabbCompute();
} }
private void aabbCompute () { private void aabbCompute () {
@ -160,19 +108,16 @@ namespace Spine {
this.minY = minY; this.minY = minY;
this.maxX = maxX; this.maxX = maxX;
this.maxY = maxY; this.maxY = maxY;
aabb = true;
} }
/** Returns true if the axis aligned bounding box contains the point. */ /** Returns true if the axis aligned bounding box contains the point. */
public bool AabbContainsPoint (float x, float y) { public bool AabbContainsPoint (float x, float y) {
if (!aabb) aabbCompute();
return x >= minX && x <= maxX && y >= minY && y <= maxY; return x >= minX && x <= maxX && y >= minY && y <= maxY;
} }
/** Returns true if the axis aligned bounding box intersects the line segment. */ /** Returns true if the axis aligned bounding box intersects the line segment. */
public bool AabbIntersectsSegment (float x1, float y1, float x2, float y2) { public bool AabbIntersectsSegment (float x1, float y1, float x2, float y2) {
if (!aabb) aabbCompute();
float minX = this.minX; float minX = this.minX;
float minY = this.minY; float minY = this.minY;
float maxX = this.maxX; float maxX = this.maxX;
@ -193,14 +138,11 @@ namespace Spine {
/** Returns true if the axis aligned bounding box intersects the axis aligned bounding box of the specified bounds. */ /** Returns true if the axis aligned bounding box intersects the axis aligned bounding box of the specified bounds. */
public bool AabbIntersectsSkeleton (SkeletonBounds bounds) { public bool AabbIntersectsSkeleton (SkeletonBounds bounds) {
if (!aabb) aabbCompute();
if (!bounds.aabb) bounds.aabbCompute();
return minX < bounds.maxX && maxX > bounds.minX && minY < bounds.maxY && maxY > bounds.minY; return minX < bounds.maxX && maxX > bounds.minX && minY < bounds.maxY && maxY > bounds.minY;
} }
/** Returns true if the bounding box attachment contains the point. */ /** Returns true if the polygon contains the point. */
public bool ContainsPoint (int index, float x, float y) { public bool ContainsPoint (Polygon polygon, float x, float y) {
Polygon polygon = Polygons[index];
float[] vertices = polygon.Vertices; float[] vertices = polygon.Vertices;
int nn = polygon.Count; int nn = polygon.Count;
@ -209,7 +151,7 @@ namespace Spine {
for (int ii = 0; ii < nn; ii += 2) { for (int ii = 0; ii < nn; ii += 2) {
float vertexY = vertices[ii + 1]; float vertexY = vertices[ii + 1];
float prevY = vertices[prevIndex + 1]; float prevY = vertices[prevIndex + 1];
if (vertexY < y && prevY >= y || prevY < y && vertexY >= y) { if ((vertexY < y && prevY >= y) || (prevY < y && vertexY >= y)) {
float vertexX = vertices[ii]; float vertexX = vertices[ii];
if (vertexX + (y - vertexY) / (prevY - vertexY) * (vertices[prevIndex] - vertexX) < x) inside = !inside; if (vertexX + (y - vertexY) / (prevY - vertexY) * (vertices[prevIndex] - vertexX) < x) inside = !inside;
} }
@ -219,40 +161,25 @@ namespace Spine {
} }
/** Returns the first bounding box attachment that contains the point, or null. When doing many checks, it is usually more /** Returns the first bounding box attachment that contains the point, or null. When doing many checks, it is usually more
* efficient to only call this method if {@link #aabbContainsPoint(float, float)} return true. */ * efficient to only call this method if {@link #aabbContainsPoint(float, float)} returns true. */
public BoundingBoxAttachment ContainsPoint (float x, float y) { public BoundingBoxAttachment ContainsPoint (float x, float y) {
List<BoundingBoxAttachment> boundingBoxes = BoundingBoxes; List<Polygon> polygons = Polygons;
for (int i = 0, n = boundingBoxes.Count; i < n; i++) for (int i = 0, n = polygons.Count; i < n; i++)
if (ContainsPoint(i, x, y)) return boundingBoxes[i]; if (ContainsPoint(polygons[i], x, y)) return BoundingBoxes[i];
return null; return null;
} }
/** Returns true if the bounding box attachment contains the point. The bounding box must be in the SkeletonBounds. */
public bool containsPoint (BoundingBoxAttachment attachment, float x, float y) {
int index = BoundingBoxes.IndexOf(attachment);
return index == -1 ? false : ContainsPoint(index, x, y);
}
/** Returns the first bounding box attachment that contains the line segment, or null. When doing many checks, it is usually /** Returns the first bounding box attachment that contains the line segment, or null. When doing many checks, it is usually
* more efficient to only call this method if {@link #aabbIntersectsSegment(float, float, float, float)} return true. */ * more efficient to only call this method if {@link #aabbIntersectsSegment(float, float, float, float)} returns true. */
public BoundingBoxAttachment IntersectsSegment (float x1, float y1, float x2, float y2) { public BoundingBoxAttachment IntersectsSegment (float x1, float y1, float x2, float y2) {
List<BoundingBoxAttachment> boundingBoxes = BoundingBoxes; List<Polygon> polygons = Polygons;
for (int i = 0, n = boundingBoxes.Count; i < n; i++) { for (int i = 0, n = polygons.Count; i < n; i++)
BoundingBoxAttachment attachment = boundingBoxes[i]; if (IntersectsSegment(polygons[i], x1, y1, x2, y2)) return BoundingBoxes[i];
if (IntersectsSegment(attachment, x1, y1, x2, y2)) return attachment;
}
return null; return null;
} }
/** Returns true if the bounding box attachment contains the line segment. */ /** Returns true if the polygon contains the line segment. */
public bool IntersectsSegment (BoundingBoxAttachment attachment, float x1, float y1, float x2, float y2) { public bool IntersectsSegment (Polygon polygon, float x1, float y1, float x2, float y2) {
int index = BoundingBoxes.IndexOf(attachment);
return index == -1 ? false : IntersectsSegment(index, x1, y1, x2, y2);
}
/** Returns true if the bounding box attachment contains the line segment. */
public bool IntersectsSegment (int index, float x1, float y1, float x2, float y2) {
Polygon polygon = Polygons[index];
float[] vertices = polygon.Vertices; float[] vertices = polygon.Vertices;
int nn = polygon.Count; int nn = polygon.Count;
@ -274,6 +201,11 @@ namespace Spine {
} }
return false; return false;
} }
public Polygon getPolygon (BoundingBoxAttachment attachment) {
int index = BoundingBoxes.IndexOf(attachment);
return index == -1 ? null : Polygons[index];
}
} }
} }

View File

@ -40,14 +40,11 @@ import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.FloatArray; import com.badlogic.gdx.utils.FloatArray;
public class SkeletonBounds { public class SkeletonBounds {
private boolean aabb;
private float minX, minY, maxX, maxY; private float minX, minY, maxX, maxY;
private Array<BoundingBoxAttachment> boundingBoxes = new Array(); private Array<BoundingBoxAttachment> boundingBoxes = new Array();
private Array<FloatArray> polygons = new Array(); private Array<FloatArray> polygons = new Array();
public void update (Skeleton skeleton) { public void update (Skeleton skeleton, boolean updateAabb) {
aabb = false;
Array<BoundingBoxAttachment> boundingBoxes = this.boundingBoxes; Array<BoundingBoxAttachment> boundingBoxes = this.boundingBoxes;
Array<FloatArray> polygons = this.polygons; Array<FloatArray> polygons = this.polygons;
Array<Slot> slots = skeleton.slots; Array<Slot> slots = skeleton.slots;
@ -75,6 +72,8 @@ public class SkeletonBounds {
boundingBox.computeWorldVertices(x, y, slot.bone, polygon.items); boundingBox.computeWorldVertices(x, y, slot.bone, polygon.items);
} }
} }
if (updateAabb) aabbCompute();
} }
private void aabbCompute () { private void aabbCompute () {
@ -96,18 +95,15 @@ public class SkeletonBounds {
this.minY = minY; this.minY = minY;
this.maxX = maxX; this.maxX = maxX;
this.maxY = maxY; this.maxY = maxY;
aabb = true;
} }
/** Returns true if the axis aligned bounding box contains the point. */ /** Returns true if the axis aligned bounding box contains the point. */
public boolean aabbContainsPoint (float x, float y) { public boolean aabbContainsPoint (float x, float y) {
if (!aabb) aabbCompute();
return x >= minX && x <= maxX && y >= minY && y <= maxY; return x >= minX && x <= maxX && y >= minY && y <= maxY;
} }
/** Returns true if the axis aligned bounding box intersects the line segment. */ /** Returns true if the axis aligned bounding box intersects the line segment. */
public boolean aabbIntersectsSegment (float x1, float y1, float x2, float y2) { public boolean aabbIntersectsSegment (float x1, float y1, float x2, float y2) {
if (!aabb) aabbCompute();
float minX = this.minX; float minX = this.minX;
float minY = this.minY; float minY = this.minY;
float maxX = this.maxX; float maxX = this.maxX;
@ -128,14 +124,20 @@ public class SkeletonBounds {
/** Returns true if the axis aligned bounding box intersects the axis aligned bounding box of the specified bounds. */ /** Returns true if the axis aligned bounding box intersects the axis aligned bounding box of the specified bounds. */
public boolean aabbIntersectsSkeleton (SkeletonBounds bounds) { public boolean aabbIntersectsSkeleton (SkeletonBounds bounds) {
if (!aabb) aabbCompute();
if (!bounds.aabb) bounds.aabbCompute();
return minX < bounds.maxX && maxX > bounds.minX && minY < bounds.maxY && maxY > bounds.minY; return minX < bounds.maxX && maxX > bounds.minX && minY < bounds.maxY && maxY > bounds.minY;
} }
/** Returns the first bounding box attachment that contains the point, or null. When doing many checks, it is usually more
* efficient to only call this method if {@link #aabbContainsPoint(float, float)} returns true. */
public BoundingBoxAttachment containsPoint (float x, float y) {
Array<FloatArray> polygons = this.polygons;
for (int i = 0, n = polygons.size; i < n; i++)
if (containsPoint(polygons.get(i), x, y)) return boundingBoxes.get(i);
return null;
}
/** Returns true if the bounding box attachment contains the point. */ /** Returns true if the bounding box attachment contains the point. */
public boolean containsPoint (int index, float x, float y) { public boolean containsPoint (FloatArray polygon, float x, float y) {
FloatArray polygon = polygons.get(index);
float[] vertices = polygon.items; float[] vertices = polygon.items;
int nn = polygon.size; int nn = polygon.size;
@ -144,7 +146,7 @@ public class SkeletonBounds {
for (int ii = 0; ii < nn; ii += 2) { for (int ii = 0; ii < nn; ii += 2) {
float vertexY = vertices[ii + 1]; float vertexY = vertices[ii + 1];
float prevY = vertices[prevIndex + 1]; float prevY = vertices[prevIndex + 1];
if (vertexY < y && prevY >= y || prevY < y && vertexY >= y) { if ((vertexY < y && prevY >= y) || (prevY < y && vertexY >= y)) {
float vertexX = vertices[ii]; float vertexX = vertices[ii];
if (vertexX + (y - vertexY) / (prevY - vertexY) * (vertices[prevIndex] - vertexX) < x) inside = !inside; if (vertexX + (y - vertexY) / (prevY - vertexY) * (vertices[prevIndex] - vertexX) < x) inside = !inside;
} }
@ -153,41 +155,17 @@ public class SkeletonBounds {
return inside; return inside;
} }
/** Returns the first bounding box attachment that contains the point, or null. When doing many checks, it is usually more
* efficient to only call this method if {@link #aabbContainsPoint(float, float)} return true. */
public BoundingBoxAttachment containsPoint (float x, float y) {
Array<BoundingBoxAttachment> boundingBoxes = this.boundingBoxes;
for (int i = 0, n = boundingBoxes.size; i < n; i++)
if (containsPoint(i, x, y)) return boundingBoxes.get(i);
return null;
}
/** Returns true if the bounding box attachment contains the point. The bounding box must be in the SkeletonBounds. */
public boolean containsPoint (BoundingBoxAttachment attachment, float x, float y) {
int index = boundingBoxes.indexOf(attachment, true);
return index == -1 ? false : containsPoint(index, x, y);
}
/** Returns the first bounding box attachment that contains the line segment, or null. When doing many checks, it is usually /** Returns the first bounding box attachment that contains the line segment, or null. When doing many checks, it is usually
* more efficient to only call this method if {@link #aabbIntersectsSegment(float, float, float, float)} return true. */ * more efficient to only call this method if {@link #aabbIntersectsSegment(float, float, float, float)} returns true. */
public BoundingBoxAttachment intersectsSegment (float x1, float y1, float x2, float y2) { public BoundingBoxAttachment intersectsSegment (float x1, float y1, float x2, float y2) {
Array<BoundingBoxAttachment> boundingBoxes = this.boundingBoxes; Array<FloatArray> polygons = this.polygons;
for (int i = 0, n = boundingBoxes.size; i < n; i++) { for (int i = 0, n = polygons.size; i < n; i++)
BoundingBoxAttachment attachment = boundingBoxes.get(i); if (intersectsSegment(polygons.get(i), x1, y1, x2, y2)) return boundingBoxes.get(i);
if (intersectsSegment(attachment, x1, y1, x2, y2)) return attachment;
}
return null; return null;
} }
/** Returns true if the bounding box attachment contains the line segment. */ /** Returns true if the bounding box attachment contains the line segment. */
public boolean intersectsSegment (BoundingBoxAttachment attachment, float x1, float y1, float x2, float y2) { public boolean intersectsSegment (FloatArray polygon, float x1, float y1, float x2, float y2) {
int index = boundingBoxes.indexOf(attachment, true);
return index == -1 ? false : intersectsSegment(index, x1, y1, x2, y2);
}
/** Returns true if the bounding box attachment contains the line segment. */
public boolean intersectsSegment (int index, float x1, float y1, float x2, float y2) {
FloatArray polygon = polygons.get(index);
float[] vertices = polygon.items; float[] vertices = polygon.items;
int nn = polygon.size; int nn = polygon.size;
@ -211,32 +189,26 @@ public class SkeletonBounds {
} }
public float getMinX () { public float getMinX () {
if (!aabb) aabbCompute();
return minX; return minX;
} }
public float getMinY () { public float getMinY () {
if (!aabb) aabbCompute();
return minY; return minY;
} }
public float getMaxX () { public float getMaxX () {
if (!aabb) aabbCompute();
return maxX; return maxX;
} }
public float getMaxY () { public float getMaxY () {
if (!aabb) aabbCompute();
return maxY; return maxY;
} }
public float getWidth () { public float getWidth () {
if (!aabb) aabbCompute();
return maxX - minX; return maxX - minX;
} }
public float getHeight () { public float getHeight () {
if (!aabb) aabbCompute();
return maxY - minY; return maxY - minY;
} }

View File

@ -101,7 +101,7 @@ public class SkeletonRendererDebug {
if (drawBoundingBoxes) { if (drawBoundingBoxes) {
SkeletonBounds bounds = this.bounds; SkeletonBounds bounds = this.bounds;
bounds.update(skeleton); bounds.update(skeleton, true);
renderer.setColor(aabbColor); renderer.setColor(aabbColor);
renderer.rect(bounds.getMinX(), bounds.getMinY(), bounds.getWidth(), bounds.getHeight()); renderer.rect(bounds.getMinX(), bounds.getMinY(), bounds.getWidth(), bounds.getHeight());
renderer.setColor(boundingBoxColor); renderer.setColor(boundingBoxColor);

View File

@ -133,7 +133,7 @@ namespace Spine {
skeletonRenderer.Draw(skeleton); skeletonRenderer.Draw(skeleton);
skeletonRenderer.End(); skeletonRenderer.End();
bounds.Update(skeleton); bounds.Update(skeleton, true);
MouseState mouse = Mouse.GetState(); MouseState mouse = Mouse.GetState();
headSlot.G = 1; headSlot.G = 1;
headSlot.B = 1; headSlot.B = 1;

View File

@ -116,7 +116,7 @@ namespace Spine {
item.vertexTR.Color.A = a; item.vertexTR.Color.A = a;
float[] vertices = this.vertices; float[] vertices = this.vertices;
regionAttachment.ComputeVertices(x, y, slot.Bone, vertices); regionAttachment.ComputeWorldVertices(x, y, slot.Bone, vertices);
item.vertexTL.Position.X = vertices[RegionAttachment.X1]; item.vertexTL.Position.X = vertices[RegionAttachment.X1];
item.vertexTL.Position.Y = vertices[RegionAttachment.Y1]; item.vertexTL.Position.Y = vertices[RegionAttachment.Y1];
item.vertexTL.Position.Z = 0; item.vertexTL.Position.Z = 0;