1 /***************************************
  2  * Copyright 2011, 2012 GlobWeb contributors.
  3  *
  4  * This file is part of GlobWeb.
  5  *
  6  * GlobWeb is free software: you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as published by
  8  * the Free Software Foundation, version 3 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * GlobWeb is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14  * GNU Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with GlobWeb. If not, see <http://www.gnu.org/licenses/>.
 18  ***************************************/
 19 
 20 define( function() {
 21 
 22 /**************************************************************************************************************/
 23 
 24 /** @constructor
 25 	BoundingBox constructor
 26  */
 27 var BoundingBox = function(min,max)
 28 {
 29 	if (min)
 30 	{
 31 		this.min = vec3.create( min );
 32 	}
 33 	if (max)
 34 	{
 35 		this.max = vec3.create( max );
 36 	}
 37 }
 38 
 39 /**************************************************************************************************************/
 40 
 41 /**
 42 	Extent the bounding box with the given point
 43  */
 44 BoundingBox.prototype.extend = function( x, y, z)
 45 {
 46 	if (!this.min)
 47 	{
 48 		this.min = vec3.create();
 49 		this.max = vec3.create();
 50 		
 51 		this.min[0] = x;
 52 		this.min[1] = y;
 53 		this.min[2] = z;
 54 		this.max[0] = x;
 55 		this.max[1] = y;
 56 		this.max[2] = z;
 57 	}
 58 	else
 59 	{
 60 		if ( x < this.min[0] )
 61 		{
 62 			this.min[0] = x;
 63 		}
 64 		if ( y < this.min[1] )
 65 		{
 66 			this.min[1] = y;
 67 		}
 68 		if ( z < this.min[2] )
 69 		{
 70 			this.min[2] = z;
 71 		}
 72 		if ( x > this.max[0] )
 73 		{
 74 			this.max[0] = x;
 75 		}
 76 		if ( y > this.max[1] )
 77 		{
 78 			this.max[1] = y;
 79 		}
 80 		if ( z > this.max[2] )
 81 		{
 82 			this.max[2] = z;
 83 		}
 84 	}
 85 }
 86 /**************************************************************************************************************/
 87 
 88 /**
 89 	Compute the bounding box from an array of vertices
 90  */
 91 BoundingBox.prototype.compute = function(vertices,length,stride)
 92 {
 93 	if (!this.min)
 94 	{
 95 		this.min = vec3.create();
 96 		this.max = vec3.create();
 97 	}
 98 	
 99 	this.min[0] = vertices[0];
100 	this.min[1] = vertices[1];
101 	this.min[2] = vertices[2];
102 	this.max[0] = vertices[0];
103 	this.max[1] = vertices[1];
104 	this.max[2] = vertices[2];
105 	
106 	var st = stride || 3;
107 	var ll = length || vertices.length;
108 	
109 	for (var i=stride; i < ll; i += st)
110 	{
111 		for (var j=0; j < 3; j++)
112 		{
113 			if ( vertices[i+j] < this.min[j] )
114 			{
115 				this.min[j] = vertices[i+j];
116 			}
117 			if ( vertices[i+j] > this.max[j] )
118 			{
119 				this.max[j] = vertices[i+j];
120 			}
121 		}
122 	}
123 }
124 
125 /**************************************************************************************************************/
126 
127 /**
128 	Get the corner of a bounding box
129  */
130 BoundingBox.prototype.getCorner = function(pos)
131 {
132 	return [ pos&1 ? this.max[0] : this.min[0],
133 			pos&2 ? this.max[1] : this.min[1],
134 			pos&4 ? this.max[2] : this.min[2]	];
135 }
136 
137 /**************************************************************************************************************/
138 
139 /**
140 	Get the center of a bounding box
141  */
142 BoundingBox.prototype.getCenter = function()
143 {
144 	return [ (this.max[0] + this.min[0]) * 0.5,
145 			(this.max[1] + this.min[1]) * 0.5,
146 			(this.max[2] + this.min[2]) * 0.5	];
147 }
148 
149 /**************************************************************************************************************/
150 
151 /**
152 	Get the radius of a bounding box
153  */
154 BoundingBox.prototype.getRadius = function()
155 {
156 	var vec = vec3.create();
157 	vec3.subtract( this.max, this.min, vec)
158 	return 0.5 * vec3.length(vec);
159 }
160 
161 /**************************************************************************************************************/
162 
163 return BoundingBox;
164 
165 });
166