// CSG Union object. By John Haggerty (Slime).
// http://www.slimeland.com/
// Created January 21, 2003

// requires objects.js.

function CSGUnion(objects)
{
	this.objects = objects;
	this.setupdefaultmodifiers();
}
CSGUnion.prototype = new Obj();
CSGUnion.prototype.copy = function()
{
	var objectscopy = new Array();
	for (var a=0; a < this.objects.length; a++)
		objectscopy[a] = this.objects[a].copy();
	var toreturn = new CSGUnion(objectscopy);
	toreturn.testboundingbox = this.testboundingbox;
	return this.copymodifiers(new CSGUnion(objectscopy));
}
CSGUnion.prototype.initialize = function()
{
	for (a=0; a < this.objects.length; a++)
	{
		this.objects[a].transform = Transformation.multipletrans([this.objects[a].transform,this.transform]);
		this.objects[a].texture.transform = Transformation.multipletrans([this.objects[a].texture.transform,this.transform])
		this.objects[a].initialize();
	}
	this.transform = Transformation.IdentityTrans.copy();

	if (typeof(this.testboundingbox) == 'undefined') this.testboundingbox = false;
	this.volumeoutsideofbounds = this.objects[0].volumeoutsideofbounds;
	this.infinitebounds = this.objects[0].infinitebounds;
	for (var a=1; a < this.objects.length; a++) {
		if (this.objects[a].volumeoutsideofbounds)
			this.volumeoutsideofbounds = true;
		if (this.objects[a].infinitebounds)
			this.infinitebounds = true;
	}
	if (this.testboundingbox && !this.infinitebounds) {
		var v1 = this.objects[0].boundedby.v1.copy();
		var v2 = this.objects[0].boundedby.v2.copy();
		for (var a=1; a < this.objects.length; a++)
		{
			nv1 = this.objects[a].boundedby.v1;
			nv2 = this.objects[a].boundedby.v2;
			if (nv1.x < v1.x) v1.x = nv1.x;
			if (nv1.y < v1.y) v1.y = nv1.y;
			if (nv1.z < v1.z) v1.z = nv1.z;
			if (nv2.x > v2.x) v2.x = nv2.x;
			if (nv2.y > v2.y) v2.y = nv2.y;
			if (nv2.z > v2.z) v2.z = nv2.z;
		}
		this.boundedby = new Box(v1,v2);
	}
	else if (!this.testboundingbox) {
		this.infinitebounds = true; // avoids any bounds testing on this
	}
	if (this.inversed && !this.infinitebounds)
	{
		this.volumeoutsideofbounds = true;
		// it might be false (for instance, if this is a union containing an inversed sphere, inversed), 
		// but we don't know for sure and it's safer to assume that it's true.
		// i think the case where it changes anything is rare enough that it won't really matter.
	}
}
CSGUnion.prototype.findIntersectionsUntransformed = function(ray)
{
	var toreturn = new Array();
	// find every intersection of every object
	for (var a=0; a < this.objects.length; a++)
	{
		var thisobjectsintersections = this.objects[a].findIntersections(ray);
		for (var b=0; b < thisobjectsintersections.length; b++)
		{
			thisobjectsintersections[b].addobject(this);
			toreturn[toreturn.length] = thisobjectsintersections[b];
		}
	}
	return toreturn.sort(Intersection.closerintersection);
}
CSGUnion.prototype.isPointInsideUntransformed = function(pos) {
	for (var a=0; a < this.objects.length; a++)
		if (this.objects[a].isPointInside(pos)) return true;
	return false;
}