OSG_BASE_DLLMAPPING void extend(SphereVolume &srcVol, const SphereVolume &vol) { Pnt3f min, max, min1, max1, min2, max2, c; Real32 r; if((!srcVol.isValid () && !srcVol.isEmpty()) || srcVol.isInfinite() || srcVol.isStatic () ) { return; } if(!vol.isValid()) return; if(srcVol.isEmpty()) { if(vol.isEmpty()) { return; } else { srcVol = vol; return; } } else if(vol.isEmpty()) { return; } srcVol.getBounds(min, max ); vol .getBounds(min1, max1); min2 = Pnt3f(osgMin(min.x(), min1.x()), osgMin(min.y(), min1.y()), osgMin(min.z(), min1.z())); max2 = Pnt3f(osgMax(max.x(), max1.x()), osgMax(max.y(), max1.y()), osgMax(max.z(), max1.z())); c = Pnt3f((min2.x() + max2.x()) * 0.5f, (min2.y() + max2.y()) * 0.5f, (min2.z() + max2.z()) * 0.5f); r = ((max2 - min2).length()) * 0.5f; srcVol.setValue(c, r); return; }
bool Line::intersect(const SphereVolume &sphere, Real &enter, Real &exit ) const { Vec3r v; Pnt3r center; sphere.getCenter(center); Real radius; Real h; Real b; Real d; Real t1; Real t2; radius = sphere.getRadius(); v = center - _pos; h = (v.dot(v))-(radius * radius); b = (v.dot(_dir)); if(h >= 0.f && b <= 0.f) return false; d = b * b - h; if(d < 0.f) return false; d = osgSqrt(d); t1 = b - d; // if (t1 > 1) // return false; t2 = b + d; if( t1 < TypeTraits<Real>::getDefaultEps() ) { if( t2 < TypeTraits<Real>::getDefaultEps() /*|| t2 > 1*/) { return false; } } enter = t1; exit = t2; return true; }
OSG_BASE_DLLMAPPING bool intersect(const SphereVolume &sphere, const FrustumVolume &frustum) { const Plane *frust = frustum.getPlanes(); //check the center of the sphere with each plane of the frustum for(Int32 i = 0; i < 6; i++) { if(frust[i].distance(sphere.getCenter()) < -sphere.getRadius()) return false; } return true; }
OSG_BASE_DLLMAPPING void extend(BoxVolume &srcVol, const SphereVolume &vol) { Pnt3f min, max; if((!srcVol.isValid () && !srcVol.isEmpty()) || srcVol.isInfinite() || srcVol.isStatic () ) { return; } if(!vol.isValid()) return; if(srcVol.isEmpty()) { if(vol.isEmpty()) { return; } else { vol .getBounds(min, max); srcVol.setBounds(min, max); return; } } else if(vol.isEmpty()) { return; } vol.getBounds(min, max); srcVol.setBounds(osgMin(min.x(), srcVol.getMin().x()), osgMin(min.y(), srcVol.getMin().y()), osgMin(min.z(), srcVol.getMin().z()), osgMax(max.x(), srcVol.getMax().x()), osgMax(max.y(), srcVol.getMax().y()), osgMax(max.z(), srcVol.getMax().z())); if(vol.isInfinite()) srcVol.setInfinite(true); return; }
OSG_BASE_DLLMAPPING void extend(SphereVolume &srcVol, const Volume &vol) { const Volume *v = &vol; const SphereVolume *sphere; #ifndef OSG_2_PREP const DynamicVolume *dynamic = dynamic_cast<const DynamicVolume *>(v); if(dynamic) { v = &(dynamic->getInstance()); } #endif if((sphere = dynamic_cast<const SphereVolume *>(v)) != NULL) { OSG::extend(srcVol, *sphere); } else { SphereVolume localSphere; Pnt3f min, max, c; Real32 r; v->getBounds(min, max); c = Pnt3f((min.x() + max.x()) * 0.5f, (min.y() + max.y()) * 0.5f, (min.z() + max.z()) * 0.5f); r = ((max - min).length()) * 0.5f; localSphere.setValue(c, r); OSG::extend(srcVol, localSphere); } return; }
OSG_BASE_DLLMAPPING bool intersect(const BoxVolume &box, const SphereVolume &sphere) { // source: // J. Arvo. A simple method for box-sphere intersection testing. // In A. Glassner, editor, Graphics Gems, pp. 335-339, // Academic Press, Boston, MA, 1990 bool retCode; if(box.isEmpty() == true || sphere.isEmpty() == true) { retCode = false; } else if(box.isInfinite() == true || sphere.isInfinite() == true) { retCode = true; } else { Real32 s; Real32 d = 0.f; //find the square of the distance from the sphere to the box for(Int32 i = 0; i < 3; i++) { if(sphere.getCenter()[i] < box.getMin()[i]) { s = sphere.getCenter()[i] - box.getMin()[i]; d += s * s; } else if(sphere.getCenter()[i] > box.getMax()[i]) { s = sphere.getCenter()[i] - box.getMax()[i]; d += s * s; } } retCode = (d <= (sphere.getRadius() * sphere.getRadius())); } return retCode; }
OSG_BASE_DLLMAPPING bool intersect(const SphereVolume &sphere1, const SphereVolume &sphere2) { bool retCode = false; Real32 dist = (sphere2.getCenter() - sphere1.getCenter()).length(); if(sphere1.isEmpty() || sphere2.isEmpty()) { retCode = false; } else if(sphere1.isInfinite() || sphere2.isInfinite()) { retCode = true; } else if(dist < sphere1.getRadius() + sphere2.getRadius()) { // the distance between the center of the 2 spheres is bigger // than the sum of the 2 radiuses retCode = true; } return retCode; }
bool Frustum::intersectsWith(const SphereVolume& sphere) const { return checkSphere(sphere.center(),sphere.radius())!=NotInFrustum; }
OSG_BASE_DLLMAPPING bool intersect(const SphereVolume &sphere, const CylinderVolume &cylinder) { bool retCode; Pnt3f apos; Vec3f adir; cylinder.getAxis(apos, adir); if(sphere.isEmpty() || cylinder.isEmpty()) { retCode = false; } else if(sphere.isInfinite() || cylinder.isInfinite()) { retCode = true; } else { Real32 d = 0.f, s1 = 0.f, s2 = 0.f; Pnt3f c; Vec3f u, u1, u2; //get the distance between the upper and lower point of the cylinder // and the sphere center s1 = (apos - sphere.getCenter()).length(); s2 = (apos + adir - sphere.getCenter()).length(); if ((s1<=DBL_EPSILON) || (s2<=DBL_EPSILON)) return true; //check the smallest distance and set the vector coordinate if(s1 <= s2) { d = s1; c = apos; } else { d = s2; c = apos + adir; } // decompose the vector in u1 and u2 which are parallel and // perpendicular to the cylinder axis respectively u = ((d - sphere.getRadius()) / d) * (c - sphere.getCenter()); u1 = (u[0] * adir[0] + u[1] * adir[1] + u[2] * adir[2]) / (adir.length() * adir.length()) * adir; u2 = u - u1; if(u2.length() <= 10e-6) { retCode = (d <= sphere.getRadius()); } else { retCode = (u2.length() <= cylinder.getRadius()); } } return retCode; }
OSG_BASE_DLLMAPPING void extend(CylinderVolume &srcVol, const SphereVolume &vol) { Pnt3f min, max, min1, max1, min2, max2, apos; Vec2f p; Vec3f adir; Real32 r; if((!srcVol.isValid () && !srcVol.isEmpty()) || srcVol.isInfinite() || srcVol.isStatic () ) { return; } if(!vol.isValid()) return; if(srcVol.isEmpty()) { if(vol.isEmpty()) { return; } else { r = vol.getRadius(); apos = Pnt3f(vol.getCenter().x() - r, vol.getCenter().y() - r, vol.getCenter().z() - r); adir = Vec3f(vol.getCenter().x() + r - apos.x(), vol.getCenter().y() + r - apos.y(), vol.getCenter().z() + r - apos.z()); srcVol.setValue(apos, adir, r); return; } } else if(vol.isEmpty()) { return; } srcVol.getBounds(min, max); vol .getBounds(min1, max1); min2 = Pnt3f(osgMin(min.x(), min1.x()), osgMin(min.y(), min1.y()), osgMin(min.z(), min1.z())); max2 = Pnt3f(osgMax(max.x(), max1.x()), osgMax(max.y(), max1.y()), osgMax(max.z(), max1.z())); p = Vec2f(max2.x() - min2.x(), max2.y() - min2.y()); r = (p.length()) * 0.5f; adir = Vec3f(0.f, 0.f, max2.z() - min2.z()); apos = Pnt3f(p.x(), p.y(), min2.z()); srcVol.setValue(apos, adir, r); return; }
int main (int argc, char **argv) { Real32 ent, ex; int i; //Lines: const int nlines = 10; Line lines[nlines]; Pnt3f pnts[nlines * 2] = { Pnt3f(0,0,0), Pnt3f(0,1,0), Pnt3f(0,0,0), Pnt3f(2,1,0), Pnt3f(2,0,0), Pnt3f(2,1,0), Pnt3f(-2,0,0), Pnt3f(0,2,0), Pnt3f(-4,2,0), Pnt3f(0,2,0), Pnt3f(-3,0,0), Pnt3f(-2,1,0), Pnt3f(3,4,0), Pnt3f(1,3,0), Pnt3f(-1,0,0), Pnt3f(-1,1,0), Pnt3f(-4,0,0), Pnt3f(-3,1,0), Pnt3f(-4,6,0), Pnt3f(0,6,0) }; for ( i = 0; i < nlines; i++ ) lines[i].setValue( pnts[i*2], pnts[i*2+1] ); BoxVolume b; float xmin, ymin, zmin, xmax, ymax, zmax; xmin = 0; ymin = 0.5; zmin = 0; xmax = 1; ymax = .5; zmax = 1; b.setBounds(xmin, ymin, zmin, xmax, ymax, zmax); std::cout << std::endl; b.dump(); std::cout << std::endl; for ( i = 0 ; i < nlines; i++ ) { std::cout << "Line: (" << lines[i].getPosition() << ") (" << lines[i].getDirection() << ")" << std::endl; bool res = lines[i].intersect( b, ent, ex ); Pnt3f ep = lines[i].getPosition() + ent * lines[i].getDirection(), xp = lines[i].getPosition() + ex * lines[i].getDirection(); std::cout << "Result: " << res; if ( res ) { std::cout << " enter " << ent << "=(" << ep << ") "; bool es = ( b.isOnSurface( ep ) || b.intersect( ep ) ), xs = b.isOnSurface( xp ); if ( ( res && es ) || !res ) std::cout << "ok"; else std::cout << "**BAD**"; std::cout << "; exit " << ex << "=(" << xp << ") "; if ( ( res && xs ) || !res ) std::cout << "ok"; else std::cout << "**BAD**"; } std::cout << std::endl; } return 0; SphereVolume s; float radius; Vec3f center; center[0] = 0; center[1] = 0; center[2] = 0; radius = 1; s.setCenter(center); s.setRadius(radius); std::cout << std::endl; s.dump(); std::cout << std::endl; for ( i = 0 ; i < nlines; i++ ) { std::cout << "Line: (" << lines[i].getPosition() << ") (" << lines[i].getDirection() << ")" << std::endl; bool res = lines[i].intersect( s, ent, ex ); Pnt3f ep = lines[i].getPosition() + ent * lines[i].getDirection(), xp = lines[i].getPosition() + ex * lines[i].getDirection(); std::cout << "Result: " << res; if ( res ) { std::cout << " enter " << ent << "=(" << ep << ") "; bool es = ( s.isOnSurface( ep ) || s.intersect( ep ) ), xs = s.isOnSurface( xp ); if ( ( res && es ) || !res ) std::cout << "ok"; else std::cout << "**BAD**"; std::cout << "; exit " << ex << "=(" << xp << ") "; if ( ( res && xs ) || !res ) std::cout << "ok"; else std::cout << "**BAD**"; } std::cout << std::endl; } // Intersect the line with a cylinder. Pnt3f p; Vec3f d; float rad; p[0] = 0; p[1] = 1; p[2] = 0; d[0] = 0; d[1] = 4; d[2] = 0; rad = 2; CylinderVolume c; c.setValue(p, d, rad); // c.setAxis(p, d); // c.setRadius(rad); std::cout << std::endl; c.dump(); std::cout << std::endl; for ( i = 0 ; i < nlines; i++ ) { std::cout << "Line: (" << lines[i].getPosition() << ") (" << lines[i].getDirection() << ")" << std::endl; bool res = lines[i].intersect( c, ent, ex ); Pnt3f ep = lines[i].getPosition() + ent * lines[i].getDirection(), xp = lines[i].getPosition() + ex * lines[i].getDirection(); std::cout << "Result: " << res; if ( res ) { std::cout << " enter " << ent << "=(" << ep << ") "; bool es = ( c.isOnSurface( ep ) || c.intersect( ep ) ), xs = c.isOnSurface( xp ); if ( ( res && es ) || !res ) std::cout << "ok"; else std::cout << "**BAD**"; std::cout << "; exit " << ex << "=(" << xp << ") "; if ( ( res && xs ) || !res ) std::cout << "ok"; else std::cout << "**BAD**"; } std::cout << std::endl; } //### volume intersection ############################################## std::cout << "### volume intersection test ###" << std::endl; BoxVolume box(-1,-1,-1,1,1,1); BoxVolume boxOut (5,5,5,10,10,10); BoxVolume boxIn(-1,-1,-1,2,2,2); SphereVolume sphere(Pnt3f(0,0,0),2); SphereVolume sphereOut(Pnt3f(2,2,2),1); SphereVolume sphereIn(Pnt3f(1,0,0),1); CylinderVolume cylinder(Pnt3f(0,0,0),Vec3f(1,1,1),1); CylinderVolume cylinderOut(Pnt3f(0,9,9),Vec3f(0,0,1),1); CylinderVolume cylinderIn(Pnt3f(1,0,0),Vec3f(0,1,0),1); // Frustum defined by normal vector and distance Plane pnear(Vec3f(0,0,-1),2); Plane pfar(Vec3f(0,0,1),7); Plane pright(Vec3f(-0.7071,0,-0.7071),0); Plane pleft(Vec3f(0.7071,0,-0.7071),0); Plane ptop(Vec3f(0,-0.7071,-0.7071),0); Plane pbottom(Vec3f(0,0.7071,-0.7071),0); FrustumVolume frustum(pnear, pfar, pleft, pright, ptop, pbottom); //Frustum defined by a clipMatrix Matrix matrix; matrix.setValue(0.7071,0,0,0, 0,0.7071,0,0, 0,0,-1.27,-3.959, 0,0,-0.7071,0); FrustumVolume frustum1; frustum1.setPlanes(matrix); // Frustum defined by 8 points Pnt3f nlt(-2,2,-2); Pnt3f nlb(-2,-2,-2); Pnt3f nrt(2,2,-2); Pnt3f nrb(2,-2,-2); Pnt3f flt(-7,7,-7); Pnt3f flb(-7,-7,-7); Pnt3f frt(7,7,-7); Pnt3f frb(7,-7,-7); FrustumVolume frustum2; frustum2.setPlanes(nlt, nlb, nrt, nrb, flt, flb, frt, frb); //Tests std::cout << "Box/box outside test: " << std::flush; std::cout << (box.intersect(boxOut) ? "**BAD**" : "ok") << std::endl; std::cout << "Box/box inside test: " << std::flush; std::cout << (box.intersect(boxIn) ? "ok" : "**BAD**") << std::endl; std::cout << "Box/sphere outside test: " << std::flush; std::cout << (box.intersect(sphereOut) ? "**BAD**" : "ok") << std::endl; std::cout << "Box/sphere inside test: " << std::flush; std::cout << (box.intersect(sphereIn) ? "ok" : "**BAD**")<< std::endl; std::cout << "Sphere/sphere outside test: " << std::flush; std::cout << (sphere.intersect(sphereOut) ? "**BAD**" : "ok") << std::endl; std::cout << "Sphere/sphere inside test: " << std::flush; std::cout << (sphere.intersect(sphereIn) ? "ok" : "**BAD**") << std::endl; std::cout << "Box/cylinder outside test: " << std::flush; std::cout << (box.intersect(cylinderOut) ? "**BAD**" : "ok") << std::endl; std::cout << "Box/cylinder inside test: " << std::flush; std::cout << (box.intersect(cylinderIn) ? "ok" : "**BAD**") << std::endl; std::cout << "Sphere/cylinder outside test: " << std::flush; std::cout << (sphere.intersect(cylinderOut) ? "**BAD**" : "ok") << std::endl; std::cout << "Sphere/cylinder inside test: " << std::flush; std::cout << (sphere.intersect(cylinderIn) ? "ok" : " **BAD**") << std::endl; std::cout << "Cylinder/cylinder outside test: "<<std::flush; std::cout << (cylinder.intersect(cylinderOut) ? "**BAD**" : "ok")<<std::endl; std::cout << "Cylinder/cylinder inside test: "<<std::flush; std::cout << (cylinder.intersect(cylinderIn) ? "ok" : " **BAD** ")<<std::endl; std::cout << "Box/Frustum outside test : " << std::flush; std::cout << (boxOut.intersect(frustum) ? "**BAD**" : "ok") << std::endl; std::cout << "Box/Frustum inside test : " << std::flush; std::cout << (boxIn.intersect(frustum) ? "ok" : "**BAD**") << std::endl; std::cout << "Sphere/Frustum outside test : " << std::flush; std::cout << (sphereOut.intersect(frustum) ? "**BAD**" : "ok") << std::endl; std::cout << "Sphere/Frustum inside test : " << std::flush; std::cout << (sphereIn.intersect(frustum) ? "ok" : "**BAD**") << std::endl; std::cout << "Cylinder/Frustum outside test : " << std::flush; std::cout << (cylinderOut.intersect(frustum) ? "**BAD**" : "ok") << std::endl; std::cout << "Cylinder/Frustum inside test : " << std::flush; std::cout << (cylinderIn.intersect(frustum) ? "ok" : "**BAD**") << std::endl; std::cout << "Cylinder/Sphere outside test: " << std::flush; std::cout << (cylinderOut.intersect(sphere) ? "**BAD**" : "ok") << std::endl; //###VOLUME EXTENSION################################################ std::cout << "### volume extension test ###" << std::endl; Pnt3f min,max; //Box extension extend(box, boxIn); box.getBounds(min,max); std::cout<< "min of the box : " <<min<<std::endl; std::cout<< "max of the box : " <<max<<std::endl; extend(box,sphere); box.getBounds(min,max); std::cout<< "min of the box : " <<min<<std::endl; std::cout<< "max of the box : " <<max<<std::endl; extend(box,cylinder); box.getBounds(min,max); std::cout<< "min of the box : " <<min<<std::endl; std::cout<< "max of the box : " <<max<<std::endl; //Sphere extension extend(sphere, box); std::cout<<"Center of the sphere : " <<sphere.getCenter()<<std::endl; std::cout<<"Radius of the sphere : " <<sphere.getRadius()<<std::endl; extend(sphere, sphereOut); std::cout<<"Center of the sphere : " <<sphere.getCenter()<<std::endl; std::cout<<"Radius of the sphere : " <<sphere.getRadius()<<std::endl; extend(sphere, cylinder); std::cout<<"Center of the sphere : " <<sphere.getCenter()<<std::endl; std::cout<<"Radius of the sphere : " <<sphere.getRadius()<<std::endl; //Cylinder extension extend (cylinder, box); cylinder.getBounds(min,max); std::cout<< "min of the cylinder : " <<min<<std::endl; std::cout<< "max of the cylinder : " <<max<<std::endl; extend (cylinder, sphere); cylinder.getBounds(min,max); std::cout<< "min of the cylinder : " <<min<<std::endl; std::cout<< "max of the cylinder : " <<max<<std::endl; extend (cylinder, cylinder); cylinder.getBounds(min,max); std::cout<< "min of the cylinder : " <<min<<std::endl; std::cout<< "max of the cylinder : " <<max<<std::endl; return 0; }