Ignore:
Timestamp:
01/03/06 18:37:32 (19 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp

    r490 r491  
    3434 
    3535float BspMergeCandidate::sOverallCost = 0; 
     36 
     37 
     38/// Adds object to the pvs of the front and back node 
     39inline void AddObject2Pvs(Intersectable *object, 
     40                                                  const int side, 
     41                                                  int &pvsBack, 
     42                                                  int &pvsFront) 
     43{ 
     44        if (!object) 
     45                return; 
     46 
     47        if (side <= 0) 
     48        { 
     49                if (!object->Mailed() && !object->Mailed(2)) 
     50                { 
     51                        ++ pvsBack; 
     52 
     53                        if (object->Mailed(1)) 
     54                                object->Mail(2); 
     55                        else 
     56                                object->Mail(); 
     57                } 
     58        } 
     59 
     60        if (side >= 0) 
     61        { 
     62                if (!object->Mailed(1) && !object->Mailed(2)) 
     63                { 
     64                        ++ pvsFront; 
     65 
     66                        if (object->Mailed()) 
     67                                object->Mail(2); 
     68                        else 
     69                                object->Mail(1); 
     70                } 
     71        } 
     72} 
     73 
    3674 
    3775/****************************************************************/ 
     
    509547 
    510548        // subdivide polygons 
    511         mStat.splits += SplitPolygons(interior->GetPlane(), 
    512                                                                   *tData.mPolygons, 
    513                                           *frontData.mPolygons, 
    514                                                                   *backData.mPolygons, 
    515                                                                   coincident); 
     549        SplitPolygons(interior->GetPlane(), 
     550                                  *tData.mPolygons, 
     551                      *frontData.mPolygons, 
     552                                  *backData.mPolygons, 
     553                                  coincident); 
    516554 
    517555 
     
    755793 
    756794float VspBspTree::SelectAxisAlignedPlane(Plane3 &plane, 
    757                                                                                  const VspBspTraversalData &tData) 
     795                                                                                 const VspBspTraversalData &tData, 
     796                                                                                 int &bestAxis) 
    758797{ 
    759798        AxisAlignedBox3 box; 
     
    771810        float nPosition[3]; 
    772811        float nCostRatio[3]; 
    773         int bestAxis = -1; 
     812        bestAxis = -1; 
    774813 
    775814        const int sAxis = box.Size().DrivingAxis(); 
     
    815854        Vector3 normal(0,0,0); normal[bestAxis] = 1; 
    816855        plane = Plane3(normal, nPosition[bestAxis]); 
    817  
     856         
    818857        return nCostRatio[bestAxis]; 
    819858} 
     859 
     860 
     861float VspBspTree::EvalCostRatio(const VspBspTraversalData &tData, 
     862                                                                const AxisAlignedBox3 &box, 
     863                                                                const int axis, 
     864                                                                const float position, 
     865                                                                int &raysBack, 
     866                                                                int &raysFront, 
     867                                                                int &pvsBack, 
     868                                                                int &pvsFront) 
     869{ 
     870        raysBack = 0; 
     871        raysFront = 0; 
     872        pvsFront = 0; 
     873        pvsBack = 0; 
     874 
     875        Intersectable::NewMail(3); 
     876 
     877        // eval pvs size 
     878        const int pvsSize = tData.mPvs; 
     879 
     880        // create unique ids for pvs heuristics 
     881        GenerateUniqueIdsForPvs(); 
     882 
     883        // this is the main ray classification loop! 
     884        for(RayInfoContainer::iterator ri = tData.mRays->begin(); 
     885                        ri != tData.mRays->end(); ++ ri) 
     886        { 
     887                if (!(*ri).mRay->IsActive()) 
     888                        continue; 
     889 
     890                // determine the side of this ray with respect to the plane 
     891                float t; 
     892                int side = (*ri).ComputeRayIntersection(axis, position, t); 
     893                         
     894                if (side <= 0) 
     895                        ++ raysBack; 
     896 
     897                if (side >= 0) 
     898                        ++ raysFront; 
     899 
     900                AddObjToPvs((*ri).mRay->mTerminationObject, side, pvsBack, pvsFront); 
     901        } 
     902 
     903        //-- only one of these options should be one 
     904 
     905        if (0) //-- only pvs 
     906        { 
     907                const float sum = float(pvsBack + pvsFront); 
     908                const float oldCost = (float)pvsSize; 
     909 
     910                return sum / oldCost; 
     911        } 
     912 
     913        //-- pvs + probability heuristics 
     914        float pBack, pFront, pOverall; 
     915 
     916        if (0) 
     917        { 
     918                // box length substitute for probability 
     919                const float minBox = box.Min(axis); 
     920                const float maxBox = box.Max(axis); 
     921 
     922                pBack = position - minBox; 
     923                pFront = maxBox - position; 
     924                pOverall = maxBox - minBox; 
     925        } 
     926 
     927        if (1) //-- area substitute for probability 
     928        { 
     929                 
     930                const bool useMidSplit = true; 
     931                //const bool useMidSplit = false; 
     932                         
     933                pOverall = box.SurfaceArea(); 
     934                         
     935                if (!useMidSplit) 
     936                { 
     937                        Vector3 pos = box.Max(); pos[axis] = position; 
     938                        pBack = AxisAlignedBox3(box.Min(), pos).SurfaceArea(); 
     939 
     940                        pos = box.Min(); pos[axis] = position; 
     941                        pFront = AxisAlignedBox3(pos, box.Max()).SurfaceArea(); 
     942                } 
     943                else 
     944                { 
     945                        //-- simplified computation for mid split 
     946                        const int axis2 = (axis + 1) % 3; 
     947                        const int axis3 = (axis + 2) % 3; 
     948 
     949                        const float faceArea =  
     950                                (box.Max(axis2) - box.Min(axis2)) * 
     951                                (box.Max(axis3) - box.Min(axis3)); 
     952 
     953                        pBack = pFront = pOverall * 0.5f + faceArea; 
     954                } 
     955        } 
     956 
     957        //-- ray density substitute for probability 
     958        if (0) 
     959        { 
     960                pBack = (float)raysBack; 
     961                pFront = (float)raysFront; 
     962                pOverall = (float)tData.mRays->size(); 
     963        } 
     964 
     965        //Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl; 
     966        //Debug << pFront << " " << pBack << " " << pOverall << endl; 
     967 
     968        // float sum = raysBack*(position - minBox) + raysFront*(maxBox - position); 
     969        const float newCost = pvsBack * pBack + pvsFront * pFront; 
     970        //  float oldCost = leaf->mRays.size(); 
     971        const float oldCost = (float)pvsSize * pOverall; 
     972 
     973        return  (mCtDivCi + newCost) / oldCost; 
     974} 
     975 
    820976 
    821977 
     
    835991                        return true; 
    836992                } 
    837                 else 
    838                 { 
    839                         //-- choose plane on midpoint of a ray 
    840                         const int candidateIdx = (int)RandomValue(0, (Real)((int)data.mRays->size() - 1)); 
    841  
    842                         const Vector3 minPt = (*data.mRays)[candidateIdx].ExtrapOrigin(); 
    843                         const Vector3 maxPt = (*data.mRays)[candidateIdx].ExtrapTermination(); 
    844  
    845                         const Vector3 pt = (maxPt + minPt) * 0.5; 
    846  
    847                         const Vector3 normal = (*data.mRays)[candidateIdx].mRay->GetDir(); 
    848  
    849                         plane = Plane3(normal, pt); 
    850                         return true; 
    851                 } 
    852  
    853                 return false; 
    854993        } 
    855994 
     
    9381077        // intermediate plane 
    9391078        Plane3 plane; 
     1079        bool useAxisAlignedPlane = false; 
    9401080 
    9411081        const int limit = Min((int)data.mPolygons->size(), mMaxPolyCandidates); 
     
    9671107        } 
    9681108 
    969 #if 0 
    970         //-- choose candidate planes extracted from rays 
    971         //-- different methods are available 
    972         for (int i = 0; i < mMaxRayCandidates; ++ i) 
    973         { 
    974                 plane = ChooseCandidatePlane3(*data.mRays); 
    975                 candidateCost = SplitPlaneCost(plane, data); 
    976  
    977                 if (candidateCost < lowestCost) 
    978                 { 
    979                         bestPlane = plane; 
    980                         lowestCost = candidateCost; 
    981                 } 
    982         } 
    983 #endif 
    984  
    985         // axis aligned splits 
    986         candidateCost = SelectAxisAlignedPlane(plane, data); 
     1109        //-- axis aligned splits 
     1110        int axis; 
     1111        candidateCost = SelectAxisAlignedPlane(plane, data, axis); 
    9871112 
    9881113        if (candidateCost < lowestCost) 
    989         { 
     1114        {        
     1115                if (!useAxisAlignedPlane) 
     1116                { 
     1117                        useAxisAlignedPlane = true; 
     1118                        //! error also computed if cost ratio is missed 
     1119                        ++ mStat.splits[axis]; 
     1120                } 
     1121 
    9901122                bestPlane = plane; 
    9911123                lowestCost = candidateCost; 
     
    10311163        if (mSplitPlaneStrategy & PVS) 
    10321164        { 
     1165// matt: change back!! 
     1166Intersectable::NewMail(3); 
    10331167                // create unique ids for pvs heuristics 
    1034                 GenerateUniqueIdsForPvs(); 
     1168                //GenerateUniqueIdsForPvs(); 
    10351169 
    10361170                if (mPvsUseArea) // use front and back cell areas to approximate volume 
     
    10561190        bool useRand; 
    10571191 
    1058         // choose test polyongs randomly if over threshold 
     1192        // choose test rays randomly if too much 
    10591193        if ((int)data.mRays->size() > mMaxTests) 
    10601194        { 
     
    10951229 
    10961230                        // add the termination object 
    1097                         AddObjToPvs(ray->mTerminationObject, cf, frontPvs, backPvs); 
     1231                        //AddObjToPvs(ray->mTerminationObject, cf, frontPvs, backPvs); 
     1232                        AddObject2Pvs(ray->mTerminationObject, cf, frontPvs, backPvs); 
    10981233                        // add the source object 
    1099                         AddObjToPvs(ray->mOriginObject, cf, frontPvs, backPvs); 
    1100  
    1101                         // use number or length of rays to approximate volume 
     1234                        //AddObjToPvs(ray->mOriginObject, cf, frontPvs, backPvs); 
     1235 
     1236                        // use number of rays to approximate volume 
    11021237                        if (!mPvsUseArea) 
    11031238                        { 
    1104                                 float len = 1; 
    1105  
    1106                                 if (pvsUseLen) // use length of rays 
    1107                                         len = rayInf.SqrSegmentLength(); 
    1108  
    1109                                 pOverall += len; 
    1110  
    1111                                 if (cf == 1) 
    1112                                         pFront += len; 
    1113                                 else if (cf == -1) 
    1114                                         pBack += len; 
    1115                                 else if (cf == 0) 
    1116                                 { 
    1117                                         // use length of rays to approximate volume 
    1118                                         if (pvsUseLen) 
    1119                                         { 
    1120                                                 float newLen = len * 
    1121                                                         (rayInf.GetMaxT() - t) / 
    1122                                                         (rayInf.GetMaxT() - rayInf.GetMinT()); 
    1123  
    1124                                                 if (candidatePlane.Side(rayInf.ExtrapOrigin()) <= 0) 
    1125                                                 { 
    1126                                                         pBack += newLen; 
    1127                                                         pFront += len - newLen; 
    1128                                                 } 
    1129                                                 else 
    1130                                                 { 
    1131                                                         pFront += newLen; 
    1132                                                         pBack += len - newLen; 
    1133                                                 } 
    1134                                         } 
    1135                                         else 
    1136                                         { 
    1137                                                 ++ pFront; 
    1138                                                 ++ pBack; 
    1139                                         } 
    1140                                 } 
     1239                                ++ pOverall; 
     1240 
     1241                                if (cf >= 0) 
     1242                                        ++ pFront; 
     1243                                if (cf <= 0) 
     1244                                        ++ pBack; 
    11411245                        } 
    11421246                } 
     
    12151319        } 
    12161320} 
     1321 
    12171322 
    12181323void VspBspTree::CollectLeaves(vector<BspLeaf *> &leaves) const 
Note: See TracChangeset for help on using the changeset viewer.