<commit_before>/*******************************************************************************
 * Copyright 2020 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.tests.math.collision;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
import com.badlogic.gdx.graphics.g3d.utils.shapebuilders.BoxShapeBuilder;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Quaternion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;
import com.badlogic.gdx.math.collision.OrientedBoundingBox;
import com.badlogic.gdx.math.collision.Ray;
import com.badlogic.gdx.tests.utils.GdxTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class OrientedBoundingBoxTest extends GdxTest implements ApplicationListener {

	private static final int BOXES = 90;

	private PerspectiveCamera camera;
	private CameraInputController cameraController;
	private ModelBatch modelBatch;

	private static final Color COLOR_STANDARD = Color.BLUE;
	private static final Color COLOR_MOUSE_OVER = Color.GREEN;
	private static final Color COLOR_INTERSECTION = Color.GOLD;

	private List<Box> boxes = new ArrayList<>();

	@Override
	public void create() {
		modelBatch = new ModelBatch();

		setupScene();
		setupCamera();
		InputMultiplexer inputMultiplexer = new InputMultiplexer();
		inputMultiplexer.addProcessor(cameraController);
		inputMultiplexer.addProcessor(this);
		Gdx.input.setInputProcessor(inputMultiplexer);
	}

	private void setupScene() {
		boxes.clear();
		for (int i = 0; i < BOXES; i++) {
			boxes.add(new Box());
		}
	}

	private void setupCamera() {
		camera = new PerspectiveCamera(60f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.near = 0.01f;
		camera.far = 100f;
		camera.position.set(0, 5, -2);
		camera.lookAt(Vector3.Zero);
		camera.update();

		cameraController = new CameraInputController(camera);
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0, 0, 0, 0);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

		cameraController.update();
		checkCollision();

		// Draw Box
		modelBatch.begin(camera);
		for (Box box : boxes) {
			box.update();
			modelBatch.render(box.instance);
		}
		modelBatch.end();
	}

	private void checkCollision() {
		Ray ray = camera.getPickRay(Gdx.input.getX(), Gdx.input.getY());

		// Reset all boxes
		for (Box box : boxes) {
			box.intersects = false;
			box.updateColor(COLOR_STANDARD);
		}

		for (int i = 0; i < boxes.size(); i++) {
			Box box = boxes.get(i);

			for (int j = i + 1; j < boxes.size(); j++) {
				Box anotherBox = boxes.get(j);

				if (box.orientedBoundingBox.intersects(anotherBox.orientedBoundingBox)) {
					if (!box.intersects) {
						box.updateColor(COLOR_INTERSECTION);
						box.intersects = true;
					}

					anotherBox.updateColor(COLOR_INTERSECTION);
					anotherBox.intersects = true;
				}
			}

			boolean mouseOver = Intersector.intersectRayOrientedBoundsFast(ray, box.orientedBoundingBox);
			if (mouseOver) {
				box.updateColor(COLOR_MOUSE_OVER);
				box.intersects = true;
			}
		}

	}

	@Override
	public void dispose() {
		modelBatch.dispose();
		for (Box box : boxes) {
			box.model.dispose();<commit_msg>Display FPS on OrientedBoundingBoxTest<commit_after>/*******************************************************************************
 * Copyright 2020 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.tests.math.collision;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
import com.badlogic.gdx.graphics.g3d.utils.shapebuilders.BoxShapeBuilder;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Quaternion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;
import com.badlogic.gdx.math.collision.OrientedBoundingBox;
import com.badlogic.gdx.math.collision.Ray;
import com.badlogic.gdx.tests.utils.GdxTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class OrientedBoundingBoxTest extends GdxTest implements ApplicationListener {

	private static final int NUM_BOXES = 100;

	private PerspectiveCamera camera;
	private CameraInputController cameraController;
	private ModelBatch modelBatch;

	private SpriteBatch batch;
	private BitmapFont font;

	private static final Color COLOR_STANDARD = Color.BLUE;
	private static final Color COLOR_MOUSE_OVER = Color.GREEN;
	private static final Color COLOR_INTERSECTION = Color.GOLD;

	private List<Box> boxes = new ArrayList<>();

	@Override
	public void create() {
		font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false);
		batch = new SpriteBatch();
		modelBatch = new ModelBatch();

		setupScene();
		setupCamera();
		InputMultiplexer inputMultiplexer = new InputMultiplexer();
		inputMultiplexer.addProcessor(cameraController);
		inputMultiplexer.addProcessor(this);
		Gdx.input.setInputProcessor(inputMultiplexer);
	}

	private void setupScene() {
		boxes.clear();
		for (int i = 0; i < NUM_BOXES; i++) {
			boxes.add(new Box());
		}
	}

	private void setupCamera() {
		camera = new PerspectiveCamera(60f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.near = 0.01f;
		camera.far = 100f;
		camera.position.set(0, 5, -2);
		camera.lookAt(Vector3.Zero);
		camera.update();

		cameraController = new CameraInputController(camera);
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0, 0, 0, 0);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

		// Draw FPS
		batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		batch.begin();
		font.draw(batch, "fps: " + Gdx.graphics.getFramesPerSecond(), 0, 30);
		batch.end();

		cameraController.update();
		checkCollision();

		// Draw Box
		modelBatch.begin(camera);
		for (Box box : boxes) {
			box.update();
			modelBatch.render(box.instance);
		}
		modelBatch.end();
	}

	private void checkCollision() {
		Ray ray = camera.getPickRay(Gdx.input.getX(), Gdx.input.getY());

		// Reset all boxes
		for (Box box : boxes) {
			box.intersects = false;
			box.updateColor(COLOR_STANDARD);
		}

		for (int i = 0; i < boxes.size(); i++) {
			Box box = boxes.get(i);

			for (int j = i + 1; j < boxes.size(); j++) {
				Box anotherBox = boxes.get(j);

				if (box.orientedBoundingBox.intersects(anotherBox.orientedBoundingBox)) {
					if (!box.intersects) {
						box.updateColor(COLOR_INTERSECTION);
						box.intersects = true;
					}

					anotherBox.updateColor(COLOR_INTERSECTION);
					anotherBox.intersects = true;
				}
			}

			boolean mouseOver = Intersector.intersectRayOrientedBoundsFast(ray, box.orientedBoundingBox);
			if (mouseOver) {
				box.updateColor(COLOR_MOUSE_OVER);
				box.intersects = true;
			}
		}

	}

	@Override
	public void dispose() {
		batch.dispose();
		font.dispose();
		modelBatch.dispose();
		for (Box box : boxes) {
			box.model.dispose();
{'fraction_non_alphanumeric': 0.14495907159879806, 'fraction_numerical': 0.010465236763029738, 'mean_word_length': 4.601857225769008, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '21614', 'n_tokens_mistral': 3502, 'n_tokens_neox': 3075, 'n_words': 680}