29 #ifndef __H3DBOUND_H__
30 #define __H3DBOUND_H__
52 const Vec3f &to ) = 0;
63 template<
class Iterator >
64 static inline Bound *boundUnion( Iterator begin, Iterator end );
69 template<
class Iterator >
70 static inline Bound *SFBoundUnion( Iterator begin, Iterator end );
149 Inst< SFVec3f > _size = 0 ):
153 center->setValue( Vec3f( 0, 0, 0 ) );
154 size->setValue( Vec3f( 0, 0, 0 ) );
160 template<
class InputIterator >
162 InputIterator end ) {
164 center->setValue( Vec3f( 0, 0, 0 ) );
165 size->setValue( Vec3f( 0, 0, 0 ) );
168 InputIterator i = begin;
172 for( ; i != end; ++i ) {
173 if( (*i).x < min_p.x ) min_p.x = (
H3DFloat) (*i).x;
174 if( (*i).y < min_p.y ) min_p.y = (
H3DFloat) (*i).y;
175 if( (*i).z < min_p.z ) min_p.z = (
H3DFloat) (*i).z;
176 if( (*i).x > max_p.x ) max_p.x = (
H3DFloat) (*i).x;
177 if( (*i).y > max_p.y ) max_p.y = (
H3DFloat) (*i).y;
178 if( (*i).z > max_p.z ) max_p.z = (
H3DFloat) (*i).z;
180 Vec3f s = max_p - min_p;
181 center->setValue( min_p + s / 2.0 );
185 template<
class InputIterator >
186 void fitAround2DPoints( InputIterator begin,
187 InputIterator end ) {
189 center->setValue( Vec3f( 0, 0, 0 ) );
190 size->setValue( Vec3f( 0, 0, 0 ) );
193 InputIterator i = begin;
197 for( ; i != end; ++i ) {
198 if( (*i).x < min_p.x ) min_p.x = (*i).x;
199 if( (*i).y < min_p.y ) min_p.y = (*i).y;
200 if( (*i).x > max_p.x ) max_p.x = (*i).x;
201 if( (*i).y > max_p.y ) max_p.y = (*i).y;
203 Vec2f s = max_p - min_p;
204 Vec2f c = min_p + s / 2.0;
205 center->setValue( Vec3f( c.x, c.y, 0.f ) );
206 size->setValue( Vec3f( s.x, s.y, 0.f ) );
211 Vec3f half_size = size->getValue() / 2.0;
212 const Vec3f c = center->getValue();
213 return ( p.x <= ( c.x + half_size.x ) &&
214 p.x >= ( c.x - half_size.x ) &&
215 p.y <= ( c.y + half_size.y ) &&
216 p.y >= ( c.y - half_size.y ) &&
217 p.z <= ( c.z + half_size.z ) &&
218 p.z >= ( c.z - half_size.z ) );
226 Vec3f e = size->getValue() / 2.0;
227 const Vec3f c = center->getValue();
229 Vec3f m = (from + to ) * 0.5f;
236 if(
H3DAbs( m.x ) > e.x + adx )
return false;
238 if(
H3DAbs( m.y ) > e.y + ady )
return false;
240 if(
H3DAbs( m.z ) > e.z + adz )
return false;
242 adx += Constants::f_epsilon;
243 ady += Constants::f_epsilon;
244 adz += Constants::f_epsilon;
246 if(
H3DAbs( m.y * d.z - m.z * d.y ) > e.y * adz + e.z * ady )
248 if(
H3DAbs( m.z * d.x - m.x * d.z ) > e.x * adz + e.z * adx )
250 if(
H3DAbs( m.x * d.y - m.y * d.x ) > e.x * ady + e.y * adx )
258 virtual bool movingSphereIntersect(
const Vec3f &from,
263 const Vec3f &c = center->getValue();
264 const Vec3f &half_s = size->getValue() / 2;
266 Vec3f min = c - half_s;
267 Vec3f max = c + half_s;
272 for(
int i = 0; i < 3; ++i ) {
274 if( v < min[i] ) v = min[i];
275 if( v > max[i] ) v = max[i];
282 virtual void render();
307 virtual void update() {
311 const Matrix4f &_matrix =
327 virtual void update() {
330 const Matrix4f &_matrix =
334 const Vec3f &bb_center = bb->
center->getValue();
335 const Vec3f &half_bb_size = bb->
size->getValue() / 2.0;
337 Vec3f trf_bb = bb_center + half_bb_size;
338 Vec3f llc_bb = bb_center - half_bb_size;
341 vector<Vec3f> corners;
343 Vec3f point = trf_bb;
344 corners.push_back(_matrix * point);
347 corners.push_back(_matrix * point);
350 corners.push_back(_matrix * point);
353 corners.push_back(_matrix * point);
356 corners.push_back(_matrix * point);
359 corners.push_back(_matrix * point);
362 corners.push_back(_matrix * point);
365 corners.push_back(_matrix * point);
367 vector< Vec3f >::iterator i = corners.begin();
371 for (; i != corners.end(); ++i) {
372 if ((*i).x < min.x) min.x = (*i).x;
373 if ((*i).y < min.y) min.y = (*i).y;
374 if ((*i).z < min.z) min.z = (*i).z;
375 if ((*i).x > max.x) max.x = (*i).x;
376 if ((*i).y > max.y) max.y = (*i).y;
377 if ((*i).z > max.z) max.z = (*i).z;
386 Inst< Center > _center = 0,
387 Inst< Size > _size = 0,
388 Inst< SFMatrix4f > _matrix = 0,
389 Inst< SFBoxBound > _boxBound = 0 ) :
409 namespace BoundInternal {
410 template<
class Iterator >
411 inline Bound *SFBoundGetBound(
const Iterator &i ) {
415 template<
class Iterator >
416 inline Bound *boundGetBound(
const Iterator &i ) {
422 template<
class Iterator >
425 Bound *(*getBound) (
const Iterator & ) ) {
427 for( Iterator i = begin; i != end; ++i ) {
428 Bound *b = getBound( i );
436 box_bound->
size->setValue( bb->
size->getValue() );
438 const Vec3f &union_center = box_bound->
center->getValue();
439 const Vec3f &half_union_size = box_bound->
size->getValue()/2.0;
440 const Vec3f &bb_center = bb->
center->getValue();
441 const Vec3f &half_bb_size = bb->
size->getValue()/2.0;
443 Vec3f trf_union = union_center + half_union_size;
444 Vec3f llc_union = union_center - half_union_size;
445 Vec3f trf_bb = bb_center + half_bb_size;
446 Vec3f llc_bb = bb_center - half_bb_size;
449 Vec3f( trf_union.x > trf_bb.x ? trf_union.x: trf_bb.x,
450 trf_union.y > trf_bb.y ? trf_union.y: trf_bb.y,
451 trf_union.z > trf_bb.z ? trf_union.z: trf_bb.z );
454 Vec3f( llc_union.x < llc_bb.x ? llc_union.x: llc_bb.x,
455 llc_union.y < llc_bb.y ? llc_union.y: llc_bb.y,
456 llc_union.z < llc_bb.z ? llc_union.z: llc_bb.z );
457 Vec3f new_size = trf_new - llc_new;
458 Vec3f new_center = llc_new + new_size / 2.0;
459 box_bound->
center->setValue( new_center );
460 box_bound->
size->setValue( new_size );
465 if( box_bound )
delete box_bound;
473 s <<
"Unsupported Bound type "
474 <<
typeid( *(b) ).
name();
490 template<
class Iterator >
493 using namespace BoundInternal;
494 return boundUnionBase( begin, end, boundGetBound< Iterator > );
497 template<
class Iterator >
500 using namespace BoundInternal;
501 return boundUnionBase( begin, end, SFBoundGetBound< Iterator > );
Bound * boundUnionBase(Iterator begin, Iterator end, Bound *(*getBound)(const Iterator &))
Returns a Bound that is the union between all the bounds.
Definition: Bound.h:423
#define H3D_FULL_LOCATION
Contains different templates to modify field behaviour.
Base header file that handles all configuration related settings.
Contains the RefCountSField class.
Contains the SFMatrix4f field class.
Contains the SFVec3f field class.
The Bound class is the abstract base class for all classes specifying bounding objects.
Definition: Bound.h:44
virtual bool isInside(const Vec3f &p)=0
Determines if a given point is inside the bound or not.
virtual bool lineSegmentIntersect(const Vec3f &from, const Vec3f &to)=0
Checks a line segment for intersection with the bound.
static Bound * SFBoundUnion(Iterator begin, Iterator end)
Returns a Bound that is the union between the Bound objects in the SFBound fields specified by the it...
Definition: Bound.h:498
virtual void render()
Render the outline of the bound with OpenGL.
Definition: Bound.h:76
virtual Vec3f closestPoint(const Vec3f &p)=0
Returns the closest point on the bound to the given point.
static Bound * boundUnion(Iterator begin, Iterator end)
Returns a Bound that is the union between all the bounds specified by the iterators.
Definition: Bound.h:491
virtual bool movingSphereIntersect(const Vec3f &from, const Vec3f &to, H3DFloat radius)=0
Checks a moving sphere for intersection with the bound.
The BoxBound is a Bound class that specifies the bound using an axis-aligned bounding box.
Definition: Bound.h:144
BoxBound(Inst< SFVec3f > _center=0, Inst< SFVec3f > _size=0)
Constructor.
Definition: Bound.h:148
virtual Vec3f closestPoint(const Vec3f &p)
Returns the closest point on the bound to the given point.
Definition: Bound.h:262
virtual bool lineSegmentIntersect(const Vec3f &from, const Vec3f &to)
Checks a line segment for intersection with the bound.
Definition: Bound.h:223
virtual bool isInside(const Vec3f &p)
Determines if a given point is inside the bound or not.
Definition: Bound.h:210
void fitAroundPoints(InputIterator begin, InputIterator end)
This function sets the BoxBound to encompass all the points between the begin and end iterators.
Definition: Bound.h:161
H3DUniquePtr< SFVec3f > center
The center point of the bounding box.
Definition: Bound.h:285
H3DUniquePtr< SFVec3f > size
The size of the bounding box.
Definition: Bound.h:287
An EmptyBound is a Bound that encompasses nothing.
Definition: Bound.h:111
virtual bool isInside(const Vec3f &)
Determines if a given point is inside the bound or not.
Definition: Bound.h:114
virtual Vec3f closestPoint(const Vec3f &p)
Returns the closest point on the bound to the given point.
Definition: Bound.h:132
virtual bool lineSegmentIntersect(const Vec3f &, const Vec3f &)
Returns false always.
Definition: Bound.h:119
virtual bool movingSphereIntersect(const Vec3f &, const Vec3f &, H3DFloat)
Checks a moving sphere for intersection with the bound.
Definition: Bound.h:126
An InfiniteBound is a Bound that encompasses everything.
Definition: Bound.h:82
virtual bool movingSphereIntersect(const Vec3f &, const Vec3f &, H3DFloat)
Checks a moving sphere for intersection with the bound.
Definition: Bound.h:97
virtual Vec3f closestPoint(const Vec3f &p)
Returns the closest point on the bound to the given point.
Definition: Bound.h:103
virtual bool isInside(const Vec3f &)
Determines if a given point is inside the bound or not.
Definition: Bound.h:85
virtual bool lineSegmentIntersect(const Vec3f &, const Vec3f &)
Returns true always.
Definition: Bound.h:90
RefCountSField is almost like any SField but it encapsulates a pointer to a RefCountedClass.
Definition: RefCountSField.h:42
virtual RefClass * getValue(int id=0)
Get the value of the field.
Definition: RefCountSField.h:217
The SFMatrix4f field contains a Matrix4f.
Definition: SFMatrix4f.h:41
virtual const Vec3f & getValue(int id=0)
Get the value of the field.
Definition: SField.h:235
Vec3f value
The encapsulated value.
Definition: SField.h:176
A template modifier class for adding type checking on the routes to any Field class.
Definition: TypedField.h:84
H3D API namespace.
Definition: Anchor.h:38