[2588] | 1 | |
---|
| 2 | #include "SamplingStrategy.h" |
---|
| 3 | #include "Intersectable.h" |
---|
| 4 | #include "DifferenceSampling.h" |
---|
| 5 | #include "ViewCell.h" |
---|
| 6 | #include "ViewCellsManager.h" |
---|
| 7 | #include "Preprocessor.h" |
---|
| 8 | #ifdef USE_PERFTIMER |
---|
| 9 | #include "Timer/PerfTimer.h" |
---|
| 10 | #endif |
---|
| 11 | |
---|
| 12 | #ifdef GTP_INTERNAL |
---|
| 13 | #include "ArchModeler2MLRT.hxx" |
---|
| 14 | #endif |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | #ifdef USE_PERFTIMER |
---|
| 18 | PerfTimer sFilterTimer; |
---|
| 19 | #endif |
---|
| 20 | |
---|
| 21 | namespace GtpVisibilityPreprocessor { |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | /** Collects the pvs of the objects which are |
---|
| 25 | in the first but not in the second pvs. |
---|
| 26 | */ |
---|
| 27 | static void GetPvsDifference(const ObjectPvs &largerPvs, |
---|
| 28 | const ObjectPvs &smallerPvs, |
---|
| 29 | ObjectContainer &pvsDifference) |
---|
| 30 | { |
---|
| 31 | Intersectable::NewMail(); |
---|
| 32 | Intersectable *obj; |
---|
| 33 | |
---|
| 34 | ObjectPvsIterator bit = smallerPvs.GetIterator(); |
---|
| 35 | |
---|
| 36 | // mail smaller pvs |
---|
| 37 | while (bit.HasMoreEntries()) |
---|
| 38 | { |
---|
| 39 | obj = bit.Next(); |
---|
| 40 | obj->Mail(); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | ObjectPvsIterator ait = largerPvs.GetIterator(); |
---|
| 44 | |
---|
| 45 | // write differece pvs |
---|
| 46 | while (ait.HasMoreEntries()) |
---|
| 47 | { |
---|
| 48 | obj = ait.Next(); |
---|
| 49 | pvsDifference.push_back(obj); |
---|
| 50 | |
---|
| 51 | //if (!obj->Mailed()) |
---|
| 52 | // c.AddSampleDirty(obj, 0); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | DifferenceSampling::DifferenceSampling(Preprocessor &preprocessor): |
---|
| 58 | SamplingStrategy(preprocessor), mCurrentViewCell(0), mNumSamples(0) |
---|
| 59 | {} |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | void DifferenceSampling::ComputeDifferenceSet(ViewCell *vc, |
---|
| 63 | ObjectContainer &differenceSet) |
---|
| 64 | { |
---|
| 65 | const ObjectPvs &pvs = vc->GetPvs(); |
---|
| 66 | const float filterSize = 100; |
---|
| 67 | ObjectPvs filteredPvs; |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | #ifdef USE_PERFTIMER |
---|
| 71 | sFilterTimer.Entry(); |
---|
| 72 | #endif |
---|
| 73 | |
---|
| 74 | PvsFilterStatistics pvsStats = mPreprocessor. |
---|
| 75 | mViewCellsManager->ApplyFilter2(vc, |
---|
| 76 | false, |
---|
| 77 | filterSize, |
---|
| 78 | filteredPvs); |
---|
| 79 | |
---|
| 80 | #ifdef USE_PERFTIMER |
---|
| 81 | sFilterTimer.Exit(); |
---|
| 82 | #endif |
---|
| 83 | |
---|
| 84 | //mDifferencePvs.Clear(false); |
---|
| 85 | //mDifferencePvs.Reserve(filteredPvs.GetSize()); |
---|
| 86 | mPvsDifference.clear(); |
---|
| 87 | mPvsDifference.reserve(filteredPvs.GetSize()); |
---|
| 88 | |
---|
| 89 | GetPvsDifference(filteredPvs, pvs, mPvsDifference); |
---|
| 90 | |
---|
| 91 | cerr << "found " << mPvsDifference.size() << " different objects" << endl; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | bool DifferenceSampling::GenerateSample(SimpleRay &ray) |
---|
| 96 | { |
---|
| 97 | if (mNumSamples <= 0) |
---|
| 98 | { |
---|
| 99 | // choose random view cell |
---|
| 100 | ViewCellContainer &viewCells = mPreprocessor.mViewCellsManager->GetViewCells(); |
---|
| 101 | const int vcIdx = (int)RandomValue(0, (float)viewCells.size() - 0.5f); |
---|
| 102 | |
---|
| 103 | mCurrentViewCell = viewCells[vcIdx]; |
---|
| 104 | |
---|
| 105 | if (mCurrentViewCell->GetPvs().Empty()) |
---|
| 106 | return false; |
---|
| 107 | |
---|
| 108 | cout << "computing new difference set" << endl; |
---|
| 109 | |
---|
| 110 | ComputeDifferenceSet(mCurrentViewCell, mPvsDifference); |
---|
| 111 | |
---|
| 112 | // n samples per object |
---|
| 113 | const int n = 50; |
---|
| 114 | mNumSamples = (int)mPvsDifference.size() * n; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if (mPvsDifference.empty()) |
---|
| 118 | return false; |
---|
| 119 | |
---|
| 120 | -- mNumSamples; |
---|
| 121 | |
---|
| 122 | cout << "x"; |
---|
| 123 | Vector3 origin, direction; |
---|
| 124 | Vector3 point; |
---|
| 125 | Vector3 normal, vcnormal; |
---|
| 126 | |
---|
| 127 | // get a new object from the difference set |
---|
| 128 | const int objIdx = (int)RandomValue(0, (float)mPvsDifference.size() - 0.5f); |
---|
| 129 | Intersectable *object = mPvsDifference[objIdx]; |
---|
| 130 | |
---|
| 131 | // cast a ray from the view cells border towards the object |
---|
| 132 | object->GetRandomSurfacePoint(point, normal); |
---|
| 133 | mCurrentViewCell->GetRandomEdgePoint(origin, vcnormal); |
---|
| 134 | |
---|
| 135 | direction = point - origin; |
---|
| 136 | |
---|
| 137 | // $$ jb the pdf is yet not correct for all sampling methods! |
---|
| 138 | const float c = Magnitude(direction); |
---|
| 139 | |
---|
| 140 | if ((c <= Limits::Small) /*|| (DotProd(direction, normal) < 0)*/) |
---|
| 141 | { |
---|
| 142 | return false; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | // $$ jb the pdf is yet not correct for all sampling methods! |
---|
| 146 | const float pdf = 1.0f; |
---|
| 147 | //cout << "p: " << point << " "; |
---|
| 148 | direction *= 1.0f / c; |
---|
| 149 | ray = SimpleRay(origin, direction, DIFFERENCE_SAMPLING_BASED_DISTRIBUTION, pdf); |
---|
| 150 | |
---|
| 151 | //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl; |
---|
| 152 | |
---|
| 153 | return true; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | } |
---|