/*!
 * \brief IntersectLinePlane::setUpResult
 * \param point
 * \return
 */
bool IntersectLinePlane::setUpResult(Point &point){

    //get and check line and plane that shall be intersected
    if(!this->inputElements.contains(0) || this->inputElements[0].size() != 1
            || !this->inputElements.contains(1) || this->inputElements[1].size() != 1){
        return false;
    }
    QPointer<Line> line = this->inputElements[0].at(0).line;
    QPointer<Plane> plane = this->inputElements[1].at(0).plane;
    if(line.isNull() || plane.isNull() || !line->getIsSolved() || !plane->getIsSolved()){
        return false;
    }

    //check if line and plane intersect
    if(!this->isIntersection(line, plane)){
        emit this->sendMessage(QString("No intersection between line %1 and plane %2").arg(line->getFeatureName())
                               .arg(plane->getFeatureName()), eWarningMessage);
        return false;
    }

    //perform intersection
    OiVec l_v = line->getDirection().getVector(); //direction vector for line
    OiVec p_n = plane->getDirection().getVector(); //normal vector for plane
    OiVec l_x0 = line->getPosition().getVector(); //position vector for line
    OiVec p_x0 = plane->getPosition().getVector(); //position vector for plane
    p_n.normalize(); //normalize normal vector for plane
    double d = 0.0; //distance plane to origin
    OiVec::dot(d, p_x0, p_n);
    double h1, h2;
    OiVec::dot(h1, l_x0, p_n);
    OiVec::dot(h2, l_v, p_n);
    double lamda = (d - h1) / (h2);
    OiVec s = l_x0 + lamda * l_v;

    //set result
    Position section;
    section.setVector(s);
    point.setPoint(section);

    return true;

}
/*!
 * \brief Register::setUpResult
 * \param sphere
 * \return
 */
bool Register::setUpResult(Sphere &sphere){

    //get and check plane
    if(!this->inputElements.contains(0) || this->inputElements[0].size() != 1){
        return false;
    }
    QPointer<Plane> plane = this->inputElements[0].at(0).plane;
    if(plane.isNull() || !plane->getIsSolved()){
        return false;
    }

    //get the position of the sphere and the plane and the normal vector
    OiVec n_plane = plane->getDirection().getVector();
    n_plane.normalize();
    OiVec x_plane = plane->getPosition().getVector();
    OiVec x_sphere = sphere.getPosition().getVector();

    //calculate the distance of the plane from the origin
    double d;
    OiVec::dot(d, x_plane, n_plane);
    if(d < 0.0){
        n_plane = -1.0 * n_plane;
        d = -d;
    }

    //calculate the distance of the sphere position from the plane
    double s;
    OiVec::dot(s, x_sphere, n_plane);
    s = s - d;

    //project the sphere position into the plane
    x_sphere = x_sphere - s * n_plane;

    //set result
    Position position = sphere.getPosition();
    position.setVector(x_sphere);
    Radius radius = sphere.getRadius();
    sphere.setSphere(position, radius);

    return true;

}