光线追踪锥.判别式给出 -ve 值,因此没有交集

Ray tracing cone. The discriminant gives -ve values so no intersections

本文关键字:判别式 -ve 光线追踪      更新时间:2023-10-16

我正在C++中实现光线追踪。球体工作正常,圆柱体几乎工作正常。但是光线追踪器无法找到与圆锥体的交点。经过计算,根据此处的链接,我创建了一个交集函数。这是我的圆锥类的查找交集函数。 可变比率为 -1 * 半径 * 半径/(高度 * 高度);

virtual double findIntersection(Ray ray){

                Vect ray_origin = ray.getRayOrigin();
                double ray_origin_x = ray_origin.getVectX();
                double ray_origin_y = ray_origin.getVectY();
                double ray_origin_z = ray_origin.getVectZ();
                Vect ray_direction = ray.getRayDirection();
                double ray_direction_x = ray_direction.getVectX();
                double ray_direction_y = ray_direction.getVectY();
                double ray_direction_z = ray_direction.getVectZ();
                Vect cone_center  = center;
                double cone_center_x = center.getVectX();
                double cone_center_y = center.getVectY();
                double cone_center_z = center.getVectZ();
        Vect diff(cone_center_x - ray_origin_x, cone_center_y - ray_origin_y + height, cone_center_z - ray_origin_z);
        //Derive the coefficients of the quadratic equation
        double a = pow(ray_direction_x, 2) + pow(ray_direction_y, 2) * ratio + pow(ray_direction_z, 2);
        double b = diff.getVectX() * ray_direction_x +  diff.getVectY() * ratio * ray_direction_y + diff.getVectZ() * ray_direction_z;
        double c = diff.getVectX() * diff.getVectX() + ratio * diff.getVectY() * diff.getVectY() + diff.getVectZ() * diff.getVectZ();
        double discriminant = b * b - a * c;
        cout << discriminant << "n";
        if (discriminant > 0){
            //The ray intersects this cone. Find the lower root
            double root_1 = (-1 * b - discriminant)/2 - 0.000001;
                       if (root_1 > 0) {
                                // the first root is the smallest positive root
                                return root_1;
                        }
                        else {
                                // the second root is the smallest positive root
                                double root_2 = ((sqrt(discriminant) - b)/2) - 0.000001;
                                return root_2;
                        }
                }
                else {
                        // the ray missed the cone
                        return -1;
        }
    }

问题在于变量判别的计算。它们出来是负的,所以没有返回交叉点。此程序中的交点函数返回从射线原点到交点的距离。

请有人看看锥体计算,并告诉我我是否做错了什么。

问候莫伊拉

我无法点击该链接,但您的"判别式"看起来不对。

假设ratio(不管是什么)是 1。然后我们有

A = 射线2
b = 差分 . 射线
c = 差异2

判别式 = b * b - a * c = (差分 . 射线)2 - 差异 2 射线2

第一学期最多 |差异|||(当射线差异平行时),因此判别式最多为零。