Exemplo n.º 1
0
unsigned
GeoMath::intersectLineWithPlane(const osg::Vec3d& p0,
                                const osg::Vec3d& p1,
                                const osg::Plane& plane,
                                osg::Vec3d&       out_p)
{
    osg::Vec3d V = p1-p0;
    V.normalize();
    double denom = plane.dotProductNormal(V);

    // if N*V == 0, line is parallel to the plane
    if ( osg::equivalent(denom, 0.0) )
    {
        // if p0 lies on the plane, line is coincident with plane
        // and intersections are infinite
        if ( osg::equivalent(plane.distance(p0), 0.0) )
        {
            out_p = p0;
            return 2;
        }
        else
        {
            // line does not intersect plane.
            return 0;
        }
    }
    else
    {
        // one intersection:
        double t = -(plane.dotProductNormal(p0) + plane[3])/denom;
        out_p = p0 + V*t;
        return 1;
    }
}
        PolytopeIntersection(unsigned int index, const CandList_t& cands, const osg::Plane &referencePlane) :
            _maxDistance(-1.0), _index(index-1), _numPoints(0)
        {
            Vec3_type center;
            for (CandList_t::const_iterator it=cands.begin(); it!=cands.end(); ++it)
            {
                PlaneMask mask = it->first;
                if (mask==0) continue;

                _points[_numPoints++] = it->second;
                center += it->second;
                value_type distance = referencePlane.distance(it->second);
                if (distance > _maxDistance) _maxDistance = distance;
                if (_numPoints==MaxNumIntesections) break;
            }
            center /= value_type(_numPoints);
            _distance = referencePlane.distance( center );
        }