<commit_before>package h3d.scene;

class Scene extends Object implements h3d.IDrawable implements hxd.SceneEvents.InteractiveScene {

	public var camera : h3d.Camera;
	public var lightSystem : h3d.pass.LightSystem;
	public var renderer : Renderer;
	var prePasses : Array<h3d.IDrawable>;
	var postPasses : Array<h3d.IDrawable>;
	var ctx : RenderContext;
	var interactives : Array<Interactive>;
	@:allow(h3d.scene.Interactive)
	var events : hxd.SceneEvents;
	var hitInteractives : Array<Interactive>;

	public function new() {
		super(null);
		hitInteractives = [];
		interactives = [];
		camera = new h3d.Camera();
		ctx = new RenderContext();
		renderer = new Renderer();
		lightSystem = new h3d.pass.LightSystem();
		postPasses = [];
		prePasses = [];
	}

	@:noCompletion public function setEvents(events) {
		this.events = events;
	}

	public function dispatchListeners(event:hxd.Event) {
	}

	function sortHitPointByCameraDistance( i1 : Interactive, i2 : Interactive ) {
		var z1 = i1.hitPoint.w;
		var z2 = i2.hitPoint.w;
		if( z1 > z2 )
			return -1;
		return 1;
	}

	public function dispatchEvent( event : hxd.Event, to : hxd.SceneEvents.Interactive ) {
		var i : Interactive = cast to;
		// TODO : compute relX/Y/Z
		i.handleEvent(event);
	}

	public function handleEvent( event : hxd.Event, last : hxd.SceneEvents.Interactive ) {

		if( interactives.length == 0 )
			return null;

		if( hitInteractives.length == 0 ) {

			var stage = hxd.Stage.getInstance();
			var screenX = (event.relX / stage.width - 0.5) * 2;
			var screenY = -(event.relY / stage.height - 0.5) * 2;
			var p0 = camera.unproject(screenX, screenY, 0);
			var p1 = camera.unproject(screenX, screenY, 1);
			var r = h3d.col.Ray.fromPoints(p0.toPoint(), p1.toPoint());
			var saveR = r.clone();

			var hitTmp = new h3d.col.Point();			
			for( i in interactives ) {
				
				var p : h3d.scene.Object = i;
				while( p != null && p.visible )
					p = p.parent;
				if( p != null ) continue;
				
				var minv = i.getInvPos();
				r.transform(minv);
				r.normalize();
				var hit = i.shape.rayIntersection(r, hitTmp);
				r.load(saveR);
				if( hit == null ) continue;

				i.hitPoint.x = hit.x;
				i.hitPoint.y = hit.y;
				i.hitPoint.z = hit.z;
				hitInteractives.push(i);
			}

			if( hitInteractives.length == 0 )
				return null;

			if( hitInteractives.length > 1 ) {
				for( i in hitInteractives ) {
					var m = i.invPos;
					var p = i.hitPoint.clone();
					p.transform3x4(i.absPos);
					p.project(camera.m);
					i.hitPoint.w = p.z;
				}
				hitInteractives.sort(sortHitPointByCameraDistance);<commit_msg>skip NaN Interactive (so they don't take all the events)<commit_after>package h3d.scene;

class Scene extends Object implements h3d.IDrawable implements hxd.SceneEvents.InteractiveScene {

	public var camera : h3d.Camera;
	public var lightSystem : h3d.pass.LightSystem;
	public var renderer : Renderer;
	var prePasses : Array<h3d.IDrawable>;
	var postPasses : Array<h3d.IDrawable>;
	var ctx : RenderContext;
	var interactives : Array<Interactive>;
	@:allow(h3d.scene.Interactive)
	var events : hxd.SceneEvents;
	var hitInteractives : Array<Interactive>;

	public function new() {
		super(null);
		hitInteractives = [];
		interactives = [];
		camera = new h3d.Camera();
		ctx = new RenderContext();
		renderer = new Renderer();
		lightSystem = new h3d.pass.LightSystem();
		postPasses = [];
		prePasses = [];
	}

	@:noCompletion public function setEvents(events) {
		this.events = events;
	}

	public function dispatchListeners(event:hxd.Event) {
	}

	function sortHitPointByCameraDistance( i1 : Interactive, i2 : Interactive ) {
		var z1 = i1.hitPoint.w;
		var z2 = i2.hitPoint.w;
		if( z1 > z2 )
			return -1;
		return 1;
	}

	public function dispatchEvent( event : hxd.Event, to : hxd.SceneEvents.Interactive ) {
		var i : Interactive = cast to;
		// TODO : compute relX/Y/Z
		i.handleEvent(event);
	}

	public function handleEvent( event : hxd.Event, last : hxd.SceneEvents.Interactive ) {

		if( interactives.length == 0 )
			return null;

		if( hitInteractives.length == 0 ) {

			var stage = hxd.Stage.getInstance();
			var screenX = (event.relX / stage.width - 0.5) * 2;
			var screenY = -(event.relY / stage.height - 0.5) * 2;
			var p0 = camera.unproject(screenX, screenY, 0);
			var p1 = camera.unproject(screenX, screenY, 1);
			var r = h3d.col.Ray.fromPoints(p0.toPoint(), p1.toPoint());
			var saveR = r.clone();

			var hitTmp = new h3d.col.Point();			
			for( i in interactives ) {
				
				var p : h3d.scene.Object = i;
				while( p != null && p.visible )
					p = p.parent;
				if( p != null ) continue;
				
				var minv = i.getInvPos();
				r.transform(minv);
				r.normalize();
				
				// check for NaN
				if( r.lx != r.lx ) {
					r.load(saveR);
					continue;
				}
				
				var hit = i.shape.rayIntersection(r, hitTmp);
				r.load(saveR);
				if( hit == null ) continue;

				i.hitPoint.x = hit.x;
				i.hitPoint.y = hit.y;
				i.hitPoint.z = hit.z;
				hitInteractives.push(i);
			}

			if( hitInteractives.length == 0 )
				return null;

			if( hitInteractives.length > 1 ) {
				for( i in hitInteractives ) {
					var m = i.invPos;
					var p = i.hitPoint.clone();
					p.transform3x4(i.absPos);
					p.project(camera.m);
					i.hitPoint.w = p.z;
				}
				hitInteractives.sort(sortHitPointByCameraDistance);
{'fraction_non_alphanumeric': 0.1342835708927232, 'fraction_numerical': 0.015003750937734433, 'mean_word_length': 3.0158132530120483, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '2084407', 'n_tokens_mistral': 2281, 'n_tokens_neox': 1932, 'n_words': 452}