Ignore:
Timestamp:
12/05/05 04:42:54 (19 years ago)
Author:
mattausch
Message:

fixed bug in VspBspTree?
view cells in VssPreprocessor?
bounding rays for vspkdtree

File:
1 edited

Legend:

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

    r445 r448  
    9898} 
    9999 
    100 Plane3 *BspInterior::GetPlane() 
    101 { 
    102         return &mPlane; 
     100Plane3 BspInterior::GetPlane() const 
     101{ 
     102        return mPlane; 
    103103} 
    104104 
     
    117117} 
    118118 
    119 int BspInterior::SplitPolygons(PolygonContainer &polys,  
    120                                                            PolygonContainer &frontPolys,  
    121                                                            PolygonContainer &backPolys,  
    122                                                            PolygonContainer &coincident) 
    123 { 
    124         int splits = 0; 
    125  
    126 #ifdef _Debug 
    127         Debug << "splitting polygons of node " << this << " with plane " << mPlane << endl; 
    128 #endif 
    129         while (!polys.empty()) 
    130         { 
    131                 Polygon3 *poly = polys.back(); 
    132                 polys.pop_back(); 
    133  
    134                 //Debug << "New polygon with plane: " << poly->GetSupportingPlane() << "\n"; 
    135  
    136                 // classify polygon 
    137                 const int cf = poly->ClassifyPlane(mPlane); 
    138  
    139                 switch (cf) 
    140                 { 
    141                         case Polygon3::COINCIDENT: 
    142                                 coincident.push_back(poly); 
    143                                 break;                   
    144                         case Polygon3::FRONT_SIDE:       
    145                                 frontPolys.push_back(poly); 
    146                                 break; 
    147                         case Polygon3::BACK_SIDE: 
    148                                 backPolys.push_back(poly); 
    149                                 break; 
    150                         case Polygon3::SPLIT: 
    151                                 { 
    152                                         Polygon3 *front_piece = new Polygon3(poly->mParent); 
    153                                         Polygon3 *back_piece = new Polygon3(poly->mParent); 
    154  
    155                                         //-- split polygon into front and back part 
    156                                         poly->Split(mPlane, *front_piece, *back_piece); 
    157                                          
    158                                         ++ splits; // increase number of splits 
    159  
    160                                         //-- inherit rays from parent polygon for blocked ray criterium 
    161                                         poly->InheritRays(*front_piece, *back_piece); 
    162                                         //Debug << "p: " << poly->mPiercingRays.size() << " f: " << front_piece->mPiercingRays.size() << " b: " << back_piece->mPiercingRays.size() << endl; 
    163                                  
    164                                         // check if polygons still valid  
    165                                         if (front_piece->Valid()) 
    166                                                 frontPolys.push_back(front_piece); 
    167                                         else 
    168                                                 DEL_PTR(front_piece); 
    169                                  
    170                                         if (back_piece->Valid()) 
    171                                                 backPolys.push_back(back_piece); 
    172                                         else                             
    173                                                 DEL_PTR(back_piece); 
    174                                  
    175 #ifdef _DEBUG 
    176                                         Debug << "split " << *poly << endl << *front_piece << endl << *back_piece << endl; 
    177 #endif 
    178                                         DEL_PTR(poly); 
    179                                 } 
    180                                 break; 
    181                         default: 
    182                 Debug << "SHOULD NEVER COME HERE\n"; 
    183                                 break; 
    184                 } 
    185         } 
    186  
    187         return splits; 
    188 } 
    189  
    190119/****************************************************************/ 
    191120/*                  class BspLeaf implementation                */ 
    192121/****************************************************************/ 
     122 
     123 
    193124BspLeaf::BspLeaf(): mViewCell(NULL) 
    194125{ 
    195126} 
     127 
    196128 
    197129BspLeaf::BspLeaf(BspViewCell *viewCell):  
     
    199131{ 
    200132} 
     133 
    201134 
    202135BspLeaf::BspLeaf(BspInterior *parent):  
     
    204137{} 
    205138 
     139 
     140 
    206141BspLeaf::BspLeaf(BspInterior *parent, BspViewCell *viewCell):  
    207142BspNode(parent), mViewCell(viewCell) 
     
    222157{  
    223158        return true;  
    224 } 
    225  
    226 void BspLeaf::AddToPvs(const BoundedRayContainer &rays,  
    227                                            int &sampleContributions, 
    228                                            int &contributingSamples) 
    229 { 
    230         sampleContributions = 0; 
    231         contributingSamples = 0; 
    232  
    233     BoundedRayContainer::const_iterator it, it_end = rays.end(); 
    234  
    235         // add contributions from samples to the PVS 
    236         for (it = rays.begin(); it != it_end; ++ it) 
    237         { 
    238                 int contribution = 0; 
    239                 Ray *ray = (*it)->mRay; 
    240                          
    241                 if (!ray->intersections.empty()) 
    242                         contribution += mViewCell->GetPvs().AddSample(ray->intersections[0].mObject); 
    243                  
    244                 if (ray->sourceObject.mObject) 
    245                         contribution += mViewCell->GetPvs().AddSample(ray->sourceObject.mObject); 
    246  
    247                 if (contribution) 
    248                 { 
    249                         sampleContributions += contribution; 
    250                         ++ contributingSamples; 
    251                 } 
    252  
    253                 //if (ray->mFlags & Ray::STORE_BSP_INTERSECTIONS) 
    254                         ray->bspIntersections.push_back(Ray::BspIntersection((*it)->mMinT, this)); 
    255         } 
    256159} 
    257160 
     
    308211        environment->GetIntValue("BspTree.maxTests", mMaxTests); 
    309212 
    310         environment->GetFloatValue("BspTree.Construction.sideTolerance", Vector3::sDistTolerance); 
    311         Vector3::sDistToleranceSqrt = Vector3::sDistTolerance * Vector3::sDistTolerance; 
    312  
    313  
     213        environment->GetFloatValue("BspTree.Construction.epsilon", mEpsilon); 
     214         
    314215    Debug << "BSP max depth: " << mTermMaxDepth << endl; 
    315216        Debug << "BSP min PVS: " << mTermMinPvs << endl; 
     
    347248} 
    348249 
     250 
    349251const BspTreeStatistics &BspTree::GetStatistics() const  
    350252{ 
    351253        return mStat; 
    352254} 
     255 
     256 
     257int BspTree::SplitPolygons(const Plane3 &plane, 
     258                                                   PolygonContainer &polys,  
     259                                                   PolygonContainer &frontPolys,  
     260                                                   PolygonContainer &backPolys,  
     261                                                   PolygonContainer &coincident) const 
     262{ 
     263        int splits = 0; 
     264 
     265#ifdef _Debug 
     266        Debug << "splitting polygons of node " << this << " with plane " << mPlane << endl; 
     267#endif 
     268        while (!polys.empty()) 
     269        { 
     270                Polygon3 *poly = polys.back(); 
     271                polys.pop_back(); 
     272 
     273                //Debug << "New polygon with plane: " << poly->GetSupportingPlane() << "\n"; 
     274 
     275                // classify polygon 
     276                const int cf = poly->ClassifyPlane(plane, mEpsilon); 
     277 
     278                switch (cf) 
     279                { 
     280                        case Polygon3::COINCIDENT: 
     281                                coincident.push_back(poly); 
     282                                break;                   
     283                        case Polygon3::FRONT_SIDE:       
     284                                frontPolys.push_back(poly); 
     285                                break; 
     286                        case Polygon3::BACK_SIDE: 
     287                                backPolys.push_back(poly); 
     288                                break; 
     289                        case Polygon3::SPLIT: 
     290                                { 
     291                                        Polygon3 *front_piece = new Polygon3(poly->mParent); 
     292                                        Polygon3 *back_piece = new Polygon3(poly->mParent); 
     293 
     294                                        //-- split polygon into front and back part 
     295                                        poly->Split(plane,  
     296                                                                *front_piece,  
     297                                                                *back_piece, 
     298                                                                mEpsilon); 
     299                                         
     300                                        ++ splits; // increase number of splits 
     301 
     302                                        //-- inherit rays from parent polygon for blocked ray criterium 
     303                                        poly->InheritRays(*front_piece, *back_piece); 
     304                                 
     305                                        // check if polygons still valid  
     306                                        if (front_piece->Valid(mEpsilon)) 
     307                                                frontPolys.push_back(front_piece); 
     308                                        else 
     309                                                DEL_PTR(front_piece); 
     310                                 
     311                                        if (back_piece->Valid(mEpsilon)) 
     312                                                backPolys.push_back(back_piece); 
     313                                        else                             
     314                                                DEL_PTR(back_piece); 
     315                                 
     316#ifdef _DEBUG 
     317                                        Debug << "split " << *poly << endl << *front_piece << endl << *back_piece << endl; 
     318#endif 
     319                                        DEL_PTR(poly); 
     320                                } 
     321                                break; 
     322                        default: 
     323                Debug << "SHOULD NEVER COME HERE\n"; 
     324                                break; 
     325                } 
     326        } 
     327 
     328        return splits; 
     329} 
     330 
    353331 
    354332void BspTreeStatistics::Print(ostream &app) const 
     
    458436                 
    459437                                // split viewcell polygons with respect to split plane 
    460                                 splits += interior->SplitPolygons(*tData.mPolygons, 
    461                                                                                                   *frontPolys,  
    462                                                                                                   *backPolys, 
    463                                                                                                   coincident); 
     438                                splits += SplitPolygons(interior->GetPlane(), 
     439                                                                                *tData.mPolygons,  
     440                                                                                *frontPolys,  
     441                                                                                *backPolys,  
     442                                                                                coincident); 
    464443                                 
    465444                                // extract view cells associated with the split polygons 
     
    505484                        // cleanup 
    506485                        DEL_PTR(tData.mPolygons); 
     486                        DEL_PTR(tData.mRays); 
    507487                } 
    508488                else 
     
    514494} 
    515495 
    516 int BspTree::AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent) 
     496int BspTree::AddMeshToPolygons(Mesh *mesh,  
     497                                                           PolygonContainer &polys,  
     498                                                           MeshInstance *parent) 
    517499{ 
    518500        FaceContainer::const_iterator fi; 
     
    523505                Polygon3 *poly = new Polygon3((*fi), mesh); 
    524506                 
    525                 if (poly->Valid()) 
     507                if (poly->Valid(mEpsilon)) 
    526508                { 
    527509                        poly->mParent = parent; // set parent intersectable 
     
    684666 
    685667                float minT, maxT; 
    686                 if (BoundRay(*ray, minT, maxT)) 
     668                if (mBox.GetRaySegment(*ray, minT, maxT)) 
    687669                        rays->push_back(new BoundedRay(ray, minT, maxT)); 
    688670        } 
     
    719701 
    720702                float minT, maxT; 
    721                 if (BoundRay(*ray, minT, maxT)) 
     703                if (mBox.GetRaySegment(*ray, minT, maxT)) 
    722704                        rays->push_back(new BoundedRay(ray, minT, maxT)); 
    723705        } 
     
    801783                { 
    802784                        int conSamp = 0, sampCon = 0; 
    803                         leaf->AddToPvs(*tData.mRays, conSamp, sampCon); 
     785                        AddToPvs(leaf, *tData.mRays, conSamp, sampCon); 
    804786                         
    805787                        mStat.contributingSamples += conSamp; 
     
    818800                DEL_PTR(tData.mPolygons); 
    819801                DEL_PTR(tData.mRays); 
    820                          
     802                DEL_PTR(tData.mGeometry); 
     803 
    821804                return leaf; 
    822805        } 
     
    860843        // cleanup 
    861844        DEL_PTR(tData.mNode); 
     845 
    862846        DEL_PTR(tData.mPolygons); 
    863847        DEL_PTR(tData.mRays); 
     
    916900         
    917901 
    918         Debug << "number of rays: " << tData.mRays->size() << endl; 
    919         Debug << "number of polys: " << tData.mPolygons->size() << endl; 
     902        Debug << "number of rays: " << (int)tData.mRays->size() << endl; 
     903        Debug << "number of polys: " << (int)tData.mPolygons->size() << endl; 
    920904 
    921905        startTime = GetTime(); 
     
    928912        startTime = GetTime(); 
    929913        // subdivide polygons with plane 
    930         mStat.splits += interior->SplitPolygons(*tData.mPolygons,  
    931                                                     *frontData.mPolygons,  
    932                                                                                         *backData.mPolygons,  
    933                                                                                         coincident); 
     914        mStat.splits += SplitPolygons(interior->GetPlane(), 
     915                                                                  *tData.mPolygons,  
     916                                          *frontData.mPolygons,  
     917                                                                  *backData.mPolygons,  
     918                                                                  coincident); 
    934919 
    935920        Debug << "time used for polygon splitting: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 
     
    944929                tData.mGeometry->SplitGeometry(*frontData.mGeometry,  
    945930                                                                           *backData.mGeometry,  
    946                                                                            *this,  
    947                                                                            interior->mPlane); 
     931                                                                           interior->mPlane, 
     932                                                                           mBox, 
     933                                                                           mEpsilon); 
    948934         
    949935                 
     
    974960        interior->SetupChildLinks(new BspLeaf(interior), new BspLeaf(interior)); 
    975961         
    976         frontData.mNode = interior->mFront; 
    977         backData.mNode = interior->mBack; 
     962        frontData.mNode = interior->GetFront(); 
     963        backData.mNode = interior->GetBack(); 
    978964         
    979965        //DEL_PTR(leaf); 
     
    12691255 
    12701256                        BoundedRay *bRay = (*rays)[idx[j]]; 
    1271                         pt[j] = chooseMin ? bRay->mRay->Extrap(bRay->mMinT) : bRay->mRay->Extrap(bRay->mMaxT); 
     1257                        pt[j] = chooseMin ? bRay->mRay->Extrap(bRay->mMinT) :  
     1258                                                                bRay->mRay->Extrap(bRay->mMaxT); 
    12721259                }        
    12731260                         
     
    13471334                Polygon3 *poly = polys[testIdx]; 
    13481335 
    1349         const int classification = poly->ClassifyPlane(candidatePlane); 
     1336        const int classification =  
     1337                        poly->ClassifyPlane(candidatePlane, mEpsilon); 
    13501338 
    13511339                if (mSplitPlaneStrategy & BALANCED_POLYS) 
     
    14371425} 
    14381426 
    1439 bool BspTree::BoundRay(const Ray &ray, float &minT, float &maxT) const 
    1440 { 
    1441         maxT = 1e6; 
    1442         minT = 0; 
    1443          
    1444         // test with tree bounding box 
    1445         if (!mBox.GetMinMaxT(ray, &minT, &maxT)) 
    1446                 return false; 
    1447  
    1448         if (minT < 0) // start ray from origin 
    1449                 minT = 0; 
    1450  
    1451         // bound ray or line segment 
    1452         if (//(ray.GetType() == Ray::LOCAL_RAY) &&  
    1453             !ray.intersections.empty() &&  
    1454                 (ray.intersections[0].mT <= maxT)) 
    1455         { 
    1456                 maxT = ray.intersections[0].mT; 
    1457         } 
    1458  
    1459         return true; 
    1460 } 
    14611427 
    14621428inline void BspTree::GenerateUniqueIdsForPvs() 
     
    14991465                        BspNodeGeometry backCell; 
    15001466 
    1501                         cell.SplitGeometry(frontCell, backCell, *this, candidatePlane); 
     1467                        cell.SplitGeometry(frontCell,  
     1468                                                           backCell,  
     1469                                                           candidatePlane, 
     1470                                                           mBox, 
     1471                                                           mEpsilon); 
    15021472                 
    15031473                        pFront = frontCell.GetArea(); 
     
    18031773        float maxt, mint; 
    18041774 
    1805         if (!BoundRay(ray, mint, maxt)) 
     1775        if (!mBox.GetRaySegment(ray, mint, maxt)) 
    18061776                return 0; 
    18071777 
     
    18181788                if (!node->IsLeaf())  
    18191789                { 
    1820                         BspInterior *in = (BspInterior *) node; 
     1790                        BspInterior *in = dynamic_cast<BspInterior *>(node); 
    18211791                         
    1822                         Plane3 *splitPlane = in->GetPlane(); 
    1823  
    1824                         int entSide = splitPlane->Side(entp); 
    1825                         int extSide = splitPlane->Side(extp); 
    1826  
    1827                         Vector3 intersection; 
     1792                        Plane3 splitPlane = in->GetPlane(); 
     1793                        const int entSide = splitPlane.Side(entp); 
     1794                        const int extSide = splitPlane.Side(extp); 
    18281795 
    18291796                        if (entSide < 0) 
     
    18581825                        // find intersection of ray segment with plane 
    18591826                        float t; 
    1860                         extp = splitPlane->FindIntersection(ray.GetLoc(), extp, &t); 
     1827                        extp = splitPlane.FindIntersection(ray.GetLoc(), extp, &t); 
    18611828                        maxt *= t; 
    18621829                         
     
    19341901                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
    19351902 
    1936                         nodeStack.push(interior->mFront); 
    1937                         nodeStack.push(interior->mBack); 
     1903                        nodeStack.push(interior->GetFront()); 
     1904                        nodeStack.push(interior->GetBack()); 
    19381905                } 
    19391906        } 
     
    19891956                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
    19901957 
    1991                         nodeStack.push(interior->mFront); 
    1992                         nodeStack.push(interior->mBack); 
     1958                        nodeStack.push(interior->GetFront()); 
     1959                        nodeStack.push(interior->GetBack()); 
    19931960                } 
    19941961        } 
     
    21012068                { 
    21022069                        BspInterior *interior = dynamic_cast<BspInterior *>(n); 
    2103                         Plane3 halfSpace = *dynamic_cast<BspInterior *>(interior)->GetPlane(); 
    2104  
    2105             if (interior->mFront != lastNode) 
     2070                        Plane3 halfSpace = dynamic_cast<BspInterior *>(interior)->GetPlane(); 
     2071 
     2072            if (interior->GetFront() != lastNode) 
    21062073                                halfSpace.ReverseOrientation(); 
    21072074 
     
    21352102        ExtractHalfSpaces(n, halfSpaces); 
    21362103 
    2137         PolygonContainer candidatePolys; 
     2104        PolygonContainer candidates; 
    21382105 
    21392106        // bounded planes are added to the polygons (reverse polygons 
     
    21432110                Polygon3 *p = GetBoundingBox().CrossSection(halfSpaces[i]); 
    21442111                 
    2145                 if (p->Valid()) 
    2146                 { 
    2147                         candidatePolys.push_back(p->CreateReversePolygon()); 
     2112                if (p->Valid(mEpsilon)) 
     2113                { 
     2114                        candidates.push_back(p->CreateReversePolygon()); 
    21482115                        DEL_PTR(p); 
    21492116                } 
     
    21582125                        vertices.push_back(mBox.GetFace(i).mVertices[j]); 
    21592126 
    2160                 candidatePolys.push_back(new Polygon3(vertices)); 
    2161         } 
    2162  
    2163         for (int i = 0; i < (int)candidatePolys.size(); ++ i) 
     2127                candidates.push_back(new Polygon3(vertices)); 
     2128        } 
     2129 
     2130        for (int i = 0; i < (int)candidates.size(); ++ i) 
    21642131        { 
    21652132                // polygon is split by all other planes 
    2166                 for (int j = 0; (j < (int)halfSpaces.size()) && candidatePolys[i]; ++ j) 
     2133                for (int j = 0; (j < (int)halfSpaces.size()) && candidates[i]; ++ j) 
    21672134                { 
    21682135                        if (i == j) // polygon and plane are coincident 
     
    21722139                        Polygon3 *frontPoly, *backPoly; 
    21732140 
    2174                         const int cf = candidatePolys[i]->ClassifyPlane(halfSpaces[j]); 
     2141                        const int cf = candidates[i]-> 
     2142                                ClassifyPlane(halfSpaces[j], mEpsilon); 
    21752143                         
    21762144                        switch (cf) 
     
    21802148                                        backPoly = new Polygon3(); 
    21812149 
    2182                                         candidatePolys[i]->Split(halfSpaces[j],  
    2183                                                                                          *frontPoly,  
    2184                                                                                          *backPoly); 
    2185  
    2186                                         DEL_PTR(candidatePolys[i]); 
    2187  
    2188                                         if (frontPoly->Valid()) 
    2189                                                 candidatePolys[i] = frontPoly; 
     2150                                        candidates[i]->Split(halfSpaces[j],  
     2151                                                                                 *frontPoly,  
     2152                                                                                 *backPoly, 
     2153                                                                                 mEpsilon); 
     2154 
     2155                                        DEL_PTR(candidates[i]); 
     2156 
     2157                                        if (frontPoly->Valid(mEpsilon)) 
     2158                                                candidates[i] = frontPoly; 
    21902159                                        else 
    21912160                                                DEL_PTR(frontPoly); 
     
    21942163                                        break; 
    21952164                                case Polygon3::BACK_SIDE: 
    2196                                         DEL_PTR(candidatePolys[i]); 
     2165                                        DEL_PTR(candidates[i]); 
    21972166                                        break; 
    21982167                                // just take polygon as it is 
     
    22042173                } 
    22052174                 
    2206                 if (candidatePolys[i]) 
    2207                         cell.push_back(candidatePolys[i]); 
     2175                if (candidates[i]) 
     2176                        cell.push_back(candidates[i]); 
    22082177        } 
    22092178} 
     
    22422211                                { 
    22432212                                        const int cf =  
    2244                                                 Polygon3::ClassifyPlane(neighborCandidate, halfSpaces[i]); 
     2213                                                Polygon3::ClassifyPlane(neighborCandidate,  
     2214                                                                                                halfSpaces[i], 
     2215                                                                                                mEpsilon); 
    22452216 
    22462217                                        if (cf == Polygon3::BACK_SIDE) 
     
    22582229                        BspInterior *interior = dynamic_cast<BspInterior *>(node); 
    22592230         
    2260                         const int cf = Polygon3::ClassifyPlane(cell, interior->mPlane); 
     2231                        const int cf = Polygon3::ClassifyPlane(cell,  
     2232                                                                                                   interior->mPlane, 
     2233                                                                                                   mEpsilon); 
    22612234 
    22622235                        if (cf == Polygon3::FRONT_SIDE) 
    2263                                 nodeStack.push(interior->mFront); 
     2236                                nodeStack.push(interior->GetFront()); 
    22642237                        else 
    22652238                                if (cf == Polygon3::BACK_SIDE) 
    2266                                         nodeStack.push(interior->mBack); 
     2239                                        nodeStack.push(interior->GetBack()); 
    22672240                                else  
    22682241                                { 
    22692242                                        // random decision 
    2270                                         nodeStack.push(interior->mBack); 
    2271                                         nodeStack.push(interior->mFront); 
     2243                                        nodeStack.push(interior->GetBack()); 
     2244                                        nodeStack.push(interior->GetFront()); 
    22722245                                } 
    22732246                } 
     
    23052278                        ConstructGeometry(interior, cell); 
    23062279 
    2307                         const int cf = Polygon3::ClassifyPlane(cell, halfspace); 
     2280                        const int cf = Polygon3::ClassifyPlane(cell,  
     2281                                                                                                   halfspace,  
     2282                                                                                                   mEpsilon); 
    23082283 
    23092284                        if (cf == Polygon3::BACK_SIDE) 
    2310                                 next = interior->mFront; 
     2285                                next = interior->GetFront(); 
    23112286                        else 
    23122287                                if (cf == Polygon3::FRONT_SIDE) 
    2313                                         next = interior->mFront; 
     2288                                        next = interior->GetFront(); 
    23142289                        else  
    23152290                        { 
    23162291                                // random decision 
    23172292                                if (mask & 1) 
    2318                                         next = interior->mBack; 
     2293                                        next = interior->GetBack(); 
    23192294                                else 
    2320                                         next = interior->mFront; 
     2295                                        next = interior->GetFront(); 
    23212296                                mask = mask >> 1; 
    23222297                        } 
     
    23532328                        // random decision 
    23542329                        if (mask & 1) 
    2355                                 nodeStack.push(interior->mBack); 
     2330                                nodeStack.push(interior->GetBack()); 
    23562331                        else 
    2357                                 nodeStack.push(interior->mFront); 
     2332                                nodeStack.push(interior->GetFront()); 
    23582333 
    23592334                        mask = mask >> 1; 
     
    23622337         
    23632338        return NULL; 
     2339} 
     2340 
     2341void BspTree::AddToPvs(BspLeaf *leaf, 
     2342                                           const BoundedRayContainer &rays,  
     2343                                           int &sampleContributions, 
     2344                                           int &contributingSamples) 
     2345{ 
     2346        sampleContributions = 0; 
     2347        contributingSamples = 0; 
     2348 
     2349    BoundedRayContainer::const_iterator it, it_end = rays.end(); 
     2350 
     2351        ViewCell *vc = leaf->GetViewCell(); 
     2352 
     2353        // add contributions from samples to the PVS 
     2354        for (it = rays.begin(); it != it_end; ++ it) 
     2355        { 
     2356                int contribution = 0; 
     2357                Ray *ray = (*it)->mRay; 
     2358                         
     2359                if (!ray->intersections.empty()) 
     2360                        contribution += vc->GetPvs().AddSample(ray->intersections[0].mObject); 
     2361                 
     2362                if (ray->sourceObject.mObject) 
     2363                        contribution += vc->GetPvs().AddSample(ray->sourceObject.mObject); 
     2364 
     2365                if (contribution) 
     2366                { 
     2367                        sampleContributions += contribution; 
     2368                        ++ contributingSamples; 
     2369                } 
     2370 
     2371                //if (ray->mFlags & Ray::STORE_BSP_INTERSECTIONS) 
     2372                //      ray->bspIntersections.push_back(Ray::BspIntersection((*it)->mMinT, this)); 
     2373        } 
    23642374} 
    23652375 
     
    23972407} 
    23982408 
    2399 /************************************************************* 
    2400  *            BspNodeGeometry Implementation                 * 
    2401  *************************************************************/ 
     2409float BspTree::GetEpsilon() const 
     2410{ 
     2411        return mEpsilon; 
     2412} 
     2413 
     2414void BspViewCellsStatistics::Print(ostream &app) const 
     2415{ 
     2416        app << "===== BspViewCells statistics ===============\n"; 
     2417 
     2418        app << setprecision(4); 
     2419 
     2420        //app << "#N_CTIME  ( Construction time [s] )\n" << Time() << " \n"; 
     2421 
     2422        app << "#N_OVERALLPVS ( objects in PVS )\n" << pvs << endl; 
     2423 
     2424        app << "#N_PMAXPVS ( largest PVS )\n" << maxPvs << endl; 
     2425 
     2426        app << "#N_PMINPVS ( smallest PVS )\n" << minPvs << endl; 
     2427 
     2428        app << "#N_PAVGPVS ( average PVS )\n" << AvgPvs() << endl; 
     2429 
     2430        app << "#N_PEMPTYPVS ( view cells with PVS smaller 2 )\n" << emptyPvs << endl; 
     2431 
     2432        app << "#N_VIEWCELLS ( number of view cells)\n" << viewCells << endl; 
     2433 
     2434        app << "#N_AVGBSPLEAVES (average number of BSP leaves per view cell )\n" << AvgBspLeaves() << endl; 
     2435 
     2436        app << "#N_MAXBSPLEAVES ( maximal number of BSP leaves per view cell )\n" << maxBspLeaves << endl; 
     2437         
     2438        app << "===== END OF BspViewCells statistics ==========\n"; 
     2439} 
     2440 
     2441/*************************************************************/ 
     2442/*            BspNodeGeometry Implementation                 */ 
     2443/*************************************************************/ 
    24022444 
    24032445BspNodeGeometry::~BspNodeGeometry() 
     
    24132455void BspNodeGeometry::SplitGeometry(BspNodeGeometry &front, 
    24142456                                                                        BspNodeGeometry &back, 
    2415                                                                         const BspTree &tree,                                              
    2416                                                                         const Plane3 &splitPlane) const 
     2457                                                                        const Plane3 &splitPlane, 
     2458                                                                        const AxisAlignedBox3 &box, 
     2459                                                                        const float epsilon) const 
    24172460{        
    24182461        // get cross section of new polygon 
    2419         Polygon3 *planePoly = tree.GetBoundingBox().CrossSection(splitPlane); 
    2420  
    2421         planePoly = SplitPolygon(planePoly, tree); 
    2422  
    2423         //-- plane poly splits all other cell polygons 
     2462        Polygon3 *planePoly = box.CrossSection(splitPlane); 
     2463 
     2464        // split polygon with all other polygons 
     2465        planePoly = SplitPolygon(planePoly, epsilon); 
     2466 
     2467        //-- new polygon splits all other polygons 
    24242468        for (int i = 0; i < (int)mPolys.size(); ++ i) 
    24252469        { 
    2426                 const int cf = mPolys[i]->ClassifyPlane(splitPlane, 0.00001f); 
     2470                /// don't use epsilon here to get exact split planes 
     2471                const int cf =  
     2472                        mPolys[i]->ClassifyPlane(splitPlane, Limits::Small); 
    24272473                         
    2428                 // split new polygon with all previous planes 
    24292474                switch (cf) 
    24302475                { 
     
    24362481                                        Polygon3 *backPoly = new Polygon3(); 
    24372482                                 
    2438                                         poly->Split(splitPlane, *frontPoly, *backPoly); 
     2483                                        poly->Split(splitPlane,  
     2484                                                                *frontPoly,  
     2485                                                                *backPoly, 
     2486                                                                epsilon); 
    24392487 
    24402488                                        DEL_PTR(poly); 
    24412489 
    2442                                         if (frontPoly->Valid()) 
     2490                                        if (frontPoly->Valid(epsilon)) 
    24432491                                                front.mPolys.push_back(frontPoly); 
    24442492                                        else 
    24452493                                                DEL_PTR(frontPoly); 
    24462494 
    2447                                         if (backPoly->Valid()) 
     2495                                        if (backPoly->Valid(epsilon)) 
    24482496                                                back.mPolys.push_back(backPoly); 
    24492497                                        else 
     
    24672515        } 
    24682516 
    2469         //-- finally add the new polygon to the child cells 
     2517        //-- finally add the new polygon to the child node geometries 
    24702518        if (planePoly) 
    24712519        { 
     
    24812529 
    24822530Polygon3 *BspNodeGeometry::SplitPolygon(Polygon3 *planePoly, 
    2483                                                                                 const BspTree &tree) const 
    2484 { 
     2531                                                                                const float epsilon) const 
     2532{ 
     2533        if (!planePoly->Valid(epsilon)) 
     2534                DEL_PTR(planePoly); 
     2535 
    24852536        // polygon is split by all other planes 
    24862537        for (int i = 0; (i < (int)mPolys.size()) && planePoly; ++ i) 
     
    24882539                Plane3 plane = mPolys[i]->GetSupportingPlane(); 
    24892540 
     2541                /// don't use epsilon here to get exact split planes 
    24902542                const int cf =  
    2491                         planePoly->ClassifyPlane(plane, 0.00001f); 
     2543                        planePoly->ClassifyPlane(plane, Limits::Small); 
    24922544                         
    24932545                // split new polygon with all previous planes 
     
    24992551                                        Polygon3 *backPoly = new Polygon3(); 
    25002552 
    2501                                         planePoly->Split(plane, *frontPoly, *backPoly); 
     2553                                        planePoly->Split(plane,  
     2554                                                                         *frontPoly,  
     2555                                                                         *backPoly, 
     2556                                                                         epsilon); 
    25022557                                         
    25032558                                        // don't need anymore 
     
    25062561 
    25072562                                        // back polygon is belonging to geometry 
    2508                                         if (backPoly->Valid()) 
     2563                                        if (backPoly->Valid(epsilon)) 
    25092564                                                planePoly = backPoly; 
    25102565                                        else 
     
    25252580        return planePoly; 
    25262581} 
    2527  
    2528 void BspViewCellsStatistics::Print(ostream &app) const 
    2529 { 
    2530         app << "===== BspViewCells statistics ===============\n"; 
    2531  
    2532         app << setprecision(4); 
    2533  
    2534         //app << "#N_CTIME  ( Construction time [s] )\n" << Time() << " \n"; 
    2535  
    2536         app << "#N_OVERALLPVS ( objects in PVS )\n" << pvs << endl; 
    2537  
    2538         app << "#N_PMAXPVS ( largest PVS )\n" << maxPvs << endl; 
    2539  
    2540         app << "#N_PMINPVS ( smallest PVS )\n" << minPvs << endl; 
    2541  
    2542         app << "#N_PAVGPVS ( average PVS )\n" << AvgPvs() << endl; 
    2543  
    2544         app << "#N_PEMPTYPVS ( view cells with PVS smaller 2 )\n" << emptyPvs << endl; 
    2545  
    2546         app << "#N_VIEWCELLS ( number of view cells)\n" << viewCells << endl; 
    2547  
    2548         app << "#N_AVGBSPLEAVES (average number of BSP leaves per view cell )\n" << AvgBspLeaves() << endl; 
    2549  
    2550         app << "#N_MAXBSPLEAVES ( maximal number of BSP leaves per view cell )\n" << maxBspLeaves << endl; 
    2551          
    2552         app << "===== END OF BspViewCells statistics ==========\n"; 
    2553 } 
Note: See TracChangeset for help on using the changeset viewer.