球 - 球交点和圆 - 球交点

时间:2014-05-28 18:00:32

标签: java algorithm 3d geometry

我有圆圈交叉的代码。但我需要将它扩展到3-D。你能帮我写一下这些功能吗?

static class Point{

    double x, y, z;
    int dimension;
    Point(double x, double y, double z) {
        this.x = x;
        this.y = y;
        this.z = z;
        dimension = 3;
    }

    Point sub(Point p2) {
        return new Point(x - p2.x, y - p2.y, z - p2.z);
    }
    Point add(Point p2) {
        return new Point(x + p2.x, y + p2.y, z + p2.z);
    }
    double distance(Point p2) {
        return Math.sqrt((x - p2.x)*(x - p2.x) + (y - p2.y)*(y - p2.y) + (z - p2.z)*(z - p2.z));
    }
    Point normal() {
        double length = Math.sqrt(x*x + y*y + z*z);
        return new Point(x/length, y/length, z/length);
    }
    Point scale(double s) {
        return new Point(x*s, y*s, z*s);
    }

    double[] array()
    {
        return new double[]{x,y,z};
    }
}

static class Circle {

    double x, y, r, left;
    Circle(double x, double y, double r) {
        this.x = x;
        this.y = y;
        this.r = r;
        left = x - r;
    }
    Circle(double[] c, double r) {
        this(c[0], c[1], r); 
    }
    Circle(Point c, double r)
    {
        this(c.x, c.y, r);
    }
    Point[] intersections(Circle c) 
    {
        Point P0 = new Point(x, y,0);
        Point P1 = new Point(c.x, c.y,0);
        double d, a, h;
        d = P0.distance(P1);
        a = (r*r - c.r*c.r + d*d)/(2*d);
        h = Math.sqrt(r*r - a*a);
        if(Double.isNaN(h))
            return null;

        Point P2 = P1.sub(P0).scale(a/d).add(P0);
        double x3, y3, x4, y4;
        x3 = P2.x + h*(P1.y - P0.y)/d;
        y3 = P2.y - h*(P1.x - P0.x)/d;
        x4 = P2.x - h*(P1.y - P0.y)/d;
        y4 = P2.y + h*(P1.x - P0.x)/d;

        return new Point[]{new Point(x3, y3, 0), new Point(x4, y4, 0)};
    }

}

static class Sphere
{
    double x,y,z,r,left;
    Sphere(double x, double y, double z, double r)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        this.r = r;
        left = x-r;
    }

    Circle intersection(Sphere s) 
    {
        Point P0 = new Point(x, y, z);
        Point P1 = new Point(s.x, s.y, s.z);

        double d, a, h;
        d = P0.distance(P1);
        a = (r*r - s.r*s.r + d*d)/(2*d);
        h = Math.sqrt(r*r - a*a);
        if(Double.isNaN(h))
            return null;

        Point P2 = P1.sub(P0).scale(a/d).add(P0);
        return new Circle(P2, h);
    }

    Point[] intersections(Circle c)
    {
        Point P0 = new Point(0,0,0);
        Point P1 = new Point(0,0,0);
        //...
        return new Point[]{P0, P1};
    }

}

我已经检查了this链接和this链接,但我无法理解它们背后的逻辑以及如何对它们进行编码。

我尝试使用ProGAL库进行球体 - 球体 - 球体交叉,但生成的坐标是圆形的。我需要精确的结果。

1 个答案:

答案 0 :(得分:2)

可以肯定的是,你需要的是如何交叉3个球体? 如果是这样,您当前的圆形数据结构是无用的。 假设你想要交叉3个球体:s1,s2,s3,s1和s2的交点将是一个圆圈,你将与s3相交以获得最终结果,但请注意圆圈不在XoY平面上,它的中心可能在任何3D坐标上,它将面向3D空间中的方向。存储你需要的这样一个圆圈的信息,centerX,centerY,centerZ,radius和一个叫做法线的3d矢量。

class Circle3D
{
    Point center;
    double r;
    Point normal;
    ...
}

完成此新课程后,您可以使用它来存储有关Sphere-Sphere交叉点的信息。之后你必须实现Sphere-Circle3D交叉点:

Circle3D Intersect(Sphere s1 , Sphere s2)
{
     double d = dist(s1.center , center)
     double x = (d*d + s1.r*s1.r - s2.r*s2.r)/(2*d)
     Point normal = normalize(s2.Center - s1.Center)
     Point center = s1.center + x*normal;
     double radius = sqrt(s1.r*s1.r - x*x)

     return new Circle3D(center , radius , normal);
}
如果数学似乎很混乱,请不要惊慌,请阅读Page

现在它是球 - 圆三维交叉的时间,因为:

point[] Intersect(Sphere s , Circle3D c)
{
    /*
      first we check if sphere even intersects with plane of c
      I assume you know how to implement some if these functions
    */
    if(GetDistanceOfPointFromPlane(s.center , new Plane(c.center , normal))>s.radius)
         return NULL;

    /*
      again we check possibility of avoiding math of intersection
    */
    point dir = Normalize(s.center - c.center);
    if(!DoesRayIntersectWithSphere(s , new Ray(c.center , c.radius*dir)))
        return NULL;

    /*
      this is the ugly part of code that unfortunately is deep trig math.
      you must describe sphere and circle equations in polar system , then
      you must solve sphere = circle
      I hope the link below be helpful 
    */  

}

这是上面的cemments中提到的链接

http://mathworld.wolfram.com/SphericalCoordinates.html