Пример #1
0
        /**
         * Classifies the face based on the ray trace technique
         *
         * @param object object3d used to compute the face status
         */
        public void rayTraceClassify(Object3D obj)
        {
            //creating a ray starting starting at the face baricenter going to the normal direction
            Point3d p0 = new Point3d();

            p0.x = (v1.x + v2.x + v3.x) / 3d;
            p0.y = (v1.y + v2.y + v3.y) / 3d;
            p0.z = (v1.z + v2.z + v3.z) / 3d;
            Line ray = new Line(getNormal(), p0);

            bool    success;
            double  dotProduct, distance;
            Point3d intersectionPoint;
            Face    closestFace = null;
            double  closestDistance;

            do
            {
                success         = true;
                closestDistance = Double.MaxValue;
                //for each face from the other solid...
                for (int i = 0; i < obj.getNumFaces(); i++)
                {
                    Face face = obj.getFace(i);
                    dotProduct        = face.getNormal().dot(ray.getDirection());
                    intersectionPoint = ray.computePlaneIntersection(face.getNormal(), face.v1.getPosition());

                    //if ray intersects the plane...
                    if (intersectionPoint != null)
                    {
                        distance = ray.computePointToPointDistance(intersectionPoint);

                        //if ray lies in plane...
                        if (Math.Abs(distance) < TOL && Math.Abs(dotProduct) < TOL)
                        {
                            //disturb the ray in order to not lie into another plane
                            ray.perturbDirection();
                            success = false;
                            break;
                        }

                        //if ray starts in plane...
                        if (Math.Abs(distance) < TOL && Math.Abs(dotProduct) > TOL)
                        {
                            //if ray intersects the face...
                            if (face.hasPoint(intersectionPoint))
                            {
                                //faces coincide
                                closestFace     = face;
                                closestDistance = 0;
                                break;
                            }
                        }

                        //if ray intersects plane...
                        else if (Math.Abs(dotProduct) > TOL && distance > TOL)
                        {
                            if (distance < closestDistance)
                            {
                                //if ray intersects the face;
                                if (face.hasPoint(intersectionPoint))
                                {
                                    //this face is the closest face untill now
                                    closestDistance = distance;
                                    closestFace     = face;
                                }
                            }
                        }
                    }
                }
            } while(success == false);

            //none face found: outside face
            if (closestFace == null)
            {
                status = OUTSIDE;
            }
            //face found: test dot product
            else
            {
                dotProduct = closestFace.getNormal().dot(ray.getDirection());

                //distance = 0: coplanar faces
                if (Math.Abs(closestDistance) < TOL)
                {
                    if (dotProduct > TOL)
                    {
                        status = SAME;
                    }
                    else if (dotProduct < -TOL)
                    {
                        status = OPPOSITE;
                    }
                }

                //dot product > 0 (same direction): inside face
                else if (dotProduct > TOL)
                {
                    status = INSIDE;
                }

                //dot product < 0 (opposite direction): outside face
                else if (dotProduct < -TOL)
                {
                    status = OUTSIDE;
                }
            }
        }