<commit_before>package org.jbox2d.dynamics.contacts;

import java.util.Stack;

import org.jbox2d.callbacks.ContactListener;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.Transform;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.pooling.SingletonPool;
import org.jbox2d.pooling.TLManifold;
import org.jbox2d.pooling.stacks.TLStack;
import org.jbox2d.structs.collision.ContactID;
import org.jbox2d.structs.collision.Manifold;
import org.jbox2d.structs.collision.ManifoldPoint;
import org.jbox2d.structs.collision.WorldManifold;
import org.jbox2d.structs.collision.shapes.ShapeType;
import org.jbox2d.structs.dynamics.contacts.ContactCreator;
import org.jbox2d.structs.dynamics.contacts.ContactEdge;
import org.jbox2d.structs.dynamics.contacts.ContactRegister;

// updated to rev 100
/**
 * The class manages contact between two shapes. A contact exists for each
 * overlapping AABB in the broad-phase (except if filtered). Therefore a contact
 * object may exist that has no contact points.
 * 
 * @author daniel
 */
public abstract class Contact {
	// Flags stored in m_flags
	// Used when crawling contact graph when forming islands.
	public static final int ISLAND_FLAG = 0x0001;
	// Set when the shapes are touching.
	public static final int TOUCHING_FLAG = 0x0002;
	// This contact can be disabled (by user)
	public static final int ENABLED_FLAG = 0x0004;
	// This contact needs filtering because a fixture filter was changed.
	public static final int FILTER_FLAG = 0x0008;
	// This bullet contact had a TOI event
	public static final int BULLET_HIT_FLAG = 0x0010;

	public static final ContactRegister[][] s_registers = new ContactRegister[ShapeType.TYPE_COUNT][ShapeType.TYPE_COUNT];
	public static boolean s_initialized = false;

	public static void addType(ContactCreator creator, ShapeType type1,
			ShapeType type2) {
		ContactRegister register = new ContactRegister();
		register.creator = creator;
		register.primary = true;
		s_registers[type1.intValue][type2.intValue] = register;

		if (type1 != type2) {
			ContactRegister register2 = new ContactRegister();
			register2.creator = creator;
			register2.primary = false;
			s_registers[type2.intValue][type1.intValue] = register2;
		}
	}

	public static void initializeRegisters() {
		addType(new ContactCreator() {
			private final TLStack<Contact> stack = new TLStack<Contact>();

			public void contactDestroyFcn(Contact contact) {
				stack.get().push(contact);
			}

			public Contact contactCreateFcn(Fixture fixtureA, Fixture fixtureB) {
				Stack<Contact> s = stack.get();
				if (s.isEmpty()) {
					s.push(new CircleContact());
					s.push(new CircleContact());
					s.push(new CircleContact());
				}
				Contact c = s.pop();
				c.init(fixtureA, fixtureB);
				return c;
			}
		}, ShapeType.CIRCLE, ShapeType.CIRCLE);
		addType(new ContactCreator() {
			private final TLStack<Contact> stack = new TLStack<Contact>();

			public void contactDestroyFcn(Contact contact) {
				stack.get().push(contact);
			}

			public Contact contactCreateFcn(Fixture fixtureA, Fixture fixtureB) {
				Stack<Contact> s = stack.get();
				if (s.isEmpty()) {
					s.push(new PolygonAndCircleContact());
					s.push(new PolygonAndCircleContact());
					s.push(new PolygonAndCircleContact());
				}
				Contact c = s.pop();
				c.init(fixtureA, fixtureB);
				return c;
			}
		}, ShapeType.POLYGON, ShapeType.CIRCLE);
		addType(new ContactCreator() {
			private final TLStack<Contact> stack = new TLStack<Contact>();

			public void contactDestroyFcn(Contact contact) {
				stack.get().push(contact);
			}

			public Contact contactCreateFcn(Fixture fixtureA, Fixture fixtureB) {
				Stack<Contact> s = stack.get();
				if (s.isEmpty()) {
					s.push(new PolygonContact());
					s.push(new PolygonContact());
					s.push(new PolygonContact());
				}
				Contact c = s.pop();
				c.init(fixtureA, fixtureB);
				return c;
			}
		}, ShapeType.POLYGON, ShapeType.POLYGON);
	}

	public static Contact create(Fixture fixtureA, Fixture fixtureB) {
		if (s_initialized == false) {
			initializeRegisters();
			s_initialized = true;
		}

		ShapeType type1 = fixtureA.getType();
		ShapeType type2 = fixtureB.getType();

		ContactCreator creator = s_registers[type1.intValue][type2.intValue].creator;
		if (creator != null) {
			if (s_registers[type1.intValue][type2.intValue].primary) {
				return creator.contactCreateFcn(fixtureA, fixtureB);
			} else {
				return creator.contactCreateFcn(fixtureB, fixtureA);
			}
		} else {
			return null;
		}
	}

	public static void destroy(Contact contact, ShapeType typeA, ShapeType typeB) {
		// djm: what's here?
	}

	public static void destroy(Contact contact) {
		assert (s_initialized == true);

		if (contact.m_manifold.pointCount > 0) {
			contact.getFixtureA().getBody().setAwake(true);
			contact.getFixtureB().getBody().setAwake(true);
		}

		ShapeType type1 = contact.getFixtureA().getType();
		ShapeType type2 = contact.getFixtureB().getType();

		ContactCreator creator = s_registers[type1.intValue][type2.intValue].creator;
		// creator.contactDestroyFcn(contact);
	}

	public int m_flags;

	// World pool and list pointers.
	public Contact m_prev;
	public Contact m_next;

	// Nodes for connecting bodies.
	public ContactEdge m_nodeA = null;
	public ContactEdge m_nodeB = null;

	public Fixture m_fixtureA;
	public Fixture m_fixtureB;

	protected Manifold m_manifold;

	public float m_toiCount;

	protected Contact() {
		m_fixtureA = null;
		m_fixtureB = null;
		m_nodeA = new ContactEdge();
		m_nodeB = new ContactEdge();

	}

	/** initialization for pooling */
	public void init(Fixture fA, Fixture fB) {
		m_flags = 0;

		m_fixtureA = fA;
		m_fixtureB = fB;

		m_manifold = new Manifold();
		m_manifold.pointCount = 0;

		m_prev = null;
		m_next = null;

		m_nodeA.contact = null;
		m_nodeA.prev = null;
		m_nodeA.next = null;
		m_nodeA.other = null;

		m_nodeB.contact = null;
		m_nodeB.prev = null;
		m_nodeB.next = null;
		m_nodeB.other = null;

		m_toiCount = 0;
	}

	/**
	 * Get the contact manifold. Do not set the point count to zero. Instead
	 * call Disable.
	 */
	public Manifold getManifold() {
		return m_manifold;
	}
<commit_msg>fixed pooling<commit_after>package org.jbox2d.dynamics.contacts;

import java.util.Stack;

import org.jbox2d.callbacks.ContactListener;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.Transform;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.pooling.SingletonPool;
import org.jbox2d.pooling.TLManifold;
import org.jbox2d.pooling.stacks.TLStack;
import org.jbox2d.structs.collision.ContactID;
import org.jbox2d.structs.collision.Manifold;
import org.jbox2d.structs.collision.ManifoldPoint;
import org.jbox2d.structs.collision.WorldManifold;
import org.jbox2d.structs.collision.shapes.ShapeType;
import org.jbox2d.structs.dynamics.contacts.ContactCreator;
import org.jbox2d.structs.dynamics.contacts.ContactEdge;
import org.jbox2d.structs.dynamics.contacts.ContactRegister;

// updated to rev 100
/**
 * The class manages contact between two shapes. A contact exists for each
 * overlapping AABB in the broad-phase (except if filtered). Therefore a contact
 * object may exist that has no contact points.
 * 
 * @author daniel
 */
public abstract class Contact {
	// Flags stored in m_flags
	// Used when crawling contact graph when forming islands.
	public static final int ISLAND_FLAG = 0x0001;
	// Set when the shapes are touching.
	public static final int TOUCHING_FLAG = 0x0002;
	// This contact can be disabled (by user)
	public static final int ENABLED_FLAG = 0x0004;
	// This contact needs filtering because a fixture filter was changed.
	public static final int FILTER_FLAG = 0x0008;
	// This bullet contact had a TOI event
	public static final int BULLET_HIT_FLAG = 0x0010;

	public static final ContactRegister[][] s_registers = new ContactRegister[ShapeType.TYPE_COUNT][ShapeType.TYPE_COUNT];
	public static boolean s_initialized = false;

	public static void addType(ContactCreator creator, ShapeType type1,
			ShapeType type2) {
		ContactRegister register = new ContactRegister();
		register.creator = creator;
		register.primary = true;
		s_registers[type1.intValue][type2.intValue] = register;

		if (type1 != type2) {
			ContactRegister register2 = new ContactRegister();
			register2.creator = creator;
			register2.primary = false;
			s_registers[type2.intValue][type1.intValue] = register2;
		}
	}

	public static void initializeRegisters() {
		addType(new ContactCreator() {
			private final TLStack<Contact> stack = new TLStack<Contact>();

			public void contactDestroyFcn(Contact contact) {
				stack.get().push(contact);
			}

			public Contact contactCreateFcn(Fixture fixtureA, Fixture fixtureB) {
				Stack<Contact> s = stack.get();
				if (s.isEmpty()) {
					s.push(new CircleContact());
					s.push(new CircleContact());
					s.push(new CircleContact());
				}
				Contact c = s.pop();
				c.init(fixtureA, fixtureB);
				return c;
			}
		}, ShapeType.CIRCLE, ShapeType.CIRCLE);
		addType(new ContactCreator() {
			private final TLStack<Contact> stack = new TLStack<Contact>();

			public void contactDestroyFcn(Contact contact) {
				stack.get().push(contact);
			}

			public Contact contactCreateFcn(Fixture fixtureA, Fixture fixtureB) {
				Stack<Contact> s = stack.get();
				if (s.isEmpty()) {
					s.push(new PolygonAndCircleContact());
					s.push(new PolygonAndCircleContact());
					s.push(new PolygonAndCircleContact());
				}
				Contact c = s.pop();
				c.init(fixtureA, fixtureB);
				return c;
			}
		}, ShapeType.POLYGON, ShapeType.CIRCLE);
		addType(new ContactCreator() {
			private final TLStack<Contact> stack = new TLStack<Contact>();

			public void contactDestroyFcn(Contact contact) {
				stack.get().push(contact);
			}

			public Contact contactCreateFcn(Fixture fixtureA, Fixture fixtureB) {
				Stack<Contact> s = stack.get();
				if (s.isEmpty()) {
					s.push(new PolygonContact());
					s.push(new PolygonContact());
					s.push(new PolygonContact());
				}
				Contact c = s.pop();
				c.init(fixtureA, fixtureB);
				return c;
			}
		}, ShapeType.POLYGON, ShapeType.POLYGON);
	}

	public static Contact create(Fixture fixtureA, Fixture fixtureB) {
		if (s_initialized == false) {
			initializeRegisters();
			s_initialized = true;
		}

		ShapeType type1 = fixtureA.getType();
		ShapeType type2 = fixtureB.getType();

		ContactCreator creator = s_registers[type1.intValue][type2.intValue].creator;
		if (creator != null) {
			if (s_registers[type1.intValue][type2.intValue].primary) {
				return creator.contactCreateFcn(fixtureA, fixtureB);
			} else {
				return creator.contactCreateFcn(fixtureB, fixtureA);
			}
		} else {
			return null;
		}
	}

	public static void destroy(Contact contact, ShapeType typeA, ShapeType typeB) {
		// djm: what's here?
	}

	public static void destroy(Contact contact) {
		assert (s_initialized == true);

		if (contact.m_manifold.pointCount > 0) {
			contact.getFixtureA().getBody().setAwake(true);
			contact.getFixtureB().getBody().setAwake(true);
		}

		ShapeType type1 = contact.getFixtureA().getType();
		ShapeType type2 = contact.getFixtureB().getType();

		ContactCreator creator = s_registers[type1.intValue][type2.intValue].creator;
		// creator.contactDestroyFcn(contact);
	}

	public int m_flags;

	// World pool and list pointers.
	public Contact m_prev;
	public Contact m_next;

	// Nodes for connecting bodies.
	public ContactEdge m_nodeA = null;
	public ContactEdge m_nodeB = null;

	public Fixture m_fixtureA;
	public Fixture m_fixtureB;

	protected Manifold m_manifold;

	public float m_toiCount;

	protected Contact() {
		m_fixtureA = null;
		m_fixtureB = null;
		m_nodeA = new ContactEdge();
		m_nodeB = new ContactEdge();
		m_manifold = new Manifold();
	}

	/** initialization for pooling */
	public void init(Fixture fA, Fixture fB) {
		m_flags = 0;

		m_fixtureA = fA;
		m_fixtureB = fB;

		m_manifold.pointCount = 0;

		m_prev = null;
		m_next = null;

		m_nodeA.contact = null;
		m_nodeA.prev = null;
		m_nodeA.next = null;
		m_nodeA.other = null;

		m_nodeB.contact = null;
		m_nodeB.prev = null;
		m_nodeB.next = null;
		m_nodeB.other = null;

		m_toiCount = 0;
	}

	/**
	 * Get the contact manifold. Do not set the point count to zero. Instead
	 * call Disable.
	 */
	public Manifold getManifold() {
		return m_manifold;
	}

{'fraction_non_alphanumeric': 0.10970600080547724, 'fraction_numerical': 0.01143777688280306, 'mean_word_length': 4.041006902151848, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2373815', 'n_tokens_mistral': 4730, 'n_tokens_neox': 4111, 'n_words': 997}