Changeset 1877 for GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
- Timestamp:
- 12/11/06 08:31:01 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
r1876 r1877 27 27 28 28 29 // warning: Should not count origin object for sampling30 // because it disturbs heuristics31 #define SAMPLE_ORIGIN_OBJECTS 0 // matt temp32 29 33 30 // $$JB HACK … … 1871 1868 1872 1869 if (evaluateFilter) { 1873 ObjectPvs filteredPvs = viewcell->GetPvs();1870 ObjectPvs filteredPvs; 1874 1871 PvsFilterStatistics fstat = ApplyFilter2(viewcell, 1875 1876 1877 1872 false, 1873 2.0f, 1874 filteredPvs); 1878 1875 1879 1876 float filteredCost = filteredPvs.EvalPvsCost(); 1880 1877 stat.avgFilteredPvs += filteredCost; 1881 1878 stat.avgFilterContribution += filteredCost - pvsCost; 1882 1879 1883 1880 stat.avgFilterRadius += fstat.mAvgFilterRadius; 1884 1881 int sum = fstat.mGlobalFilterCount + fstat.mLocalFilterCount; … … 1887 1884 (float) sum; 1888 1885 } 1889 1890 1886 1891 1887 } else { … … 2012 2008 2013 2009 2014 float ViewCellsManager::ComputeSampleContribution(VssRay &ray, 2010 2011 #define PVS_ADD_DIRTY 1 2012 2013 float 2014 ViewCellsManager::ComputeSampleContribution(VssRay &ray, 2015 2015 const bool addRays, 2016 2016 const bool storeViewCells) … … 2018 2018 ray.mPvsContribution = 0; 2019 2019 ray.mRelativePvsContribution = 0.0f; 2020 2020 2021 2021 if (ray.mTerminationObject==NULL) 2022 2022 return 0.0f; … … 2026 2026 static Ray hray; 2027 2027 hray.Init(ray); 2028 //hray.mFlags |= Ray::CULL_BACKFACES; 2029 //Ray hray(ray); 2030 2028 2031 2029 float tmin = 0, tmax = 1.0; 2032 2030 … … 2039 2037 ViewCell::NewMail(); 2040 2038 2041 // if (ray.mPdf!=1.0f)2042 // cout<<ray.mPdf<<" ";2043 2044 2045 2039 // traverse the view space subdivision 2046 2040 CastLineSegment(origin, termination, viewcells); 2047 2041 2048 2042 if (storeViewCells) 2049 { // copy viewcells memory efficiently 2043 { 2044 // copy viewcells memory efficiently 2050 2045 ray.mViewCells.reserve(viewcells.size()); 2051 2046 ray.mViewCells = viewcells; 2052 2047 } 2053 2054 Intersectable *obj = GetIntersectable(ray, true);2055 2048 2056 2049 ViewCellContainer::const_iterator it = viewcells.begin(); 2057 2050 2051 Intersectable *terminationObj = GetIntersectable(ray, true); 2052 2053 2058 2054 for (; it != viewcells.end(); ++ it) 2059 2055 { 2060 2056 ViewCell *viewcell = *it; 2061 // tests if view cell is in valid view space2057 2062 2058 if (viewcell->GetValid()) 2063 { 2059 { // if ray not outside of view space 2064 2060 float contribution; 2065 2061 2066 if (ray.mTerminationObject) { 2067 if (viewcell->GetPvs().GetSampleContribution(obj, 2068 ray.mPdf, 2069 contribution)) { 2070 ++ ray.mPvsContribution; 2062 if (terminationObj) 2063 { 2064 // todo: maybe not correct for kd node pvs 2065 if (viewcell->GetPvs().GetSampleContribution( 2066 terminationObj, ray.mPdf, contribution)) 2067 { 2068 ++ ray.mPvsContribution; 2069 if (addRays) { 2070 #if PVS_ADD_DIRTY 2071 viewcell->GetPvs().AddSampleDirtyCheck(terminationObj, ray.mPdf); 2072 if (viewcell->GetPvs().RequiresResort()) { 2073 viewcell->GetPvs().SimpleSort(); 2074 } 2075 #else 2076 viewcell->GetPvs().AddSample(terminationObj, ray.mPdf); 2077 #endif 2078 } 2079 } 2080 2081 ray.mRelativePvsContribution += contribution; 2071 2082 } 2072 2073 ray.mRelativePvsContribution += contribution; 2074 // $$ test of different contribution measure 2075 // ray.mRelativePvsContribution += 1.0f/(viewcell->GetPvs().GetSize() + 10.0f); 2076 2077 } 2078 2079 #if SAMPLE_ORIGIN_OBJECTS 2080 2081 // for directional sampling it is important to count only contributions 2082 // made in one direction!!! 2083 // the other contributions of this sample will be counted for the oposite ray! 2084 2085 if (ray.mOriginObject && 2086 viewcell->GetPvs().GetSampleContribution(ray.mOriginObject, 2087 ray.mPdf, 2088 contribution)) 2089 { 2090 ++ ray.mPvsContribution; 2091 ray.mRelativePvsContribution += contribution; 2092 } 2093 #endif 2094 } 2095 } 2096 2097 // if true, the sampled entities are stored in the pvs 2098 if (addRays) 2099 { 2100 for (it = viewcells.begin(); it != viewcells.end(); ++ it) 2101 { 2102 ViewCell *viewcell = *it; 2103 2104 if (viewcell->GetValid()) 2105 { 2106 // if view point is valid, add new object to the pvs 2107 if (ray.mTerminationObject) 2108 { 2109 viewcell->GetPvs().AddSample(obj, ray.mPdf); 2110 } 2111 #if SAMPLE_ORIGIN_OBJECTS 2112 if (ray.mOriginObject) 2113 { 2114 viewcell->GetPvs().AddSample(ray.mOriginObject, ray.mPdf); 2115 } 2116 #endif 2117 } 2118 } 2119 } 2120 2121 return ABS_CONTRIBUTION_WEIGHT*ray.mPvsContribution + 2122 (1.0f - ABS_CONTRIBUTION_WEIGHT)*ray.mRelativePvsContribution; 2123 } 2083 } 2084 } 2085 2086 return ABS_CONTRIBUTION_WEIGHT*ray.mPvsContribution + 2087 (1.0f - ABS_CONTRIBUTION_WEIGHT)*ray.mRelativePvsContribution; 2088 } 2089 2124 2090 2125 2091 … … 2627 2593 2628 2594 2629 #if 12630 2595 PvsFilterStatistics 2631 2596 ViewCellsManager::ApplyFilter2(ViewCell *viewCell, 2632 2633 2634 2635 2636 2637 { 2638 2597 const bool useViewSpaceFilter, 2598 const float filterSize, 2599 ObjectPvs &pvs, 2600 vector<AxisAlignedBox3> *filteredBoxes 2601 ) 2602 { 2603 //cout<<"y"; 2639 2604 PvsFilterStatistics stats; 2640 2605 … … 2642 2607 Vector3 center = vbox.Center(); 2643 2608 // copy the PVS 2609 Intersectable::NewMail(); 2644 2610 ObjectPvs basePvs = viewCell->GetPvs(); 2645 Intersectable::NewMail();2646 2647 2611 ObjectPvsIterator pit = basePvs.GetIterator(); 2648 2612 2613 pvs.Reserve(viewCell->GetFilteredPvsSize()); 2614 2649 2615 #if !USE_KD_PVS 2650 // first mark all object from this pvs2616 // first mark all objects from this pvs 2651 2617 while (pit.HasMoreEntries()) { 2652 2653 2654 2655 2618 ObjectPvsEntry entry = pit.Next(); 2619 2620 Intersectable *object = entry.mObject; 2621 object->Mail(); 2656 2622 } 2657 2623 #endif 2658 2624 2659 2625 int pvsSize = 0; 2660 2626 int nPvsSize = 0; … … 2681 2647 2682 2648 if (useViewSpaceFilter) { 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2649 // float radius = Max(viewCellRadius/100.0f, avgRadius - viewCellRadius); 2650 float radius = viewCellRadius/100.0f; 2651 vbox.Enlarge(radius); 2652 cout<<"vbox = "<<vbox<<endl; 2653 ViewCellContainer viewCells; 2654 ComputeBoxIntersections(vbox, viewCells); 2655 2656 ViewCellContainer::const_iterator it = viewCells.begin(), 2657 it_end = viewCells.end(); 2658 int i = 0; 2659 for (i=0; it != it_end; ++ it, ++ i) 2660 if ((*it) != viewCell) { 2661 //cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl; 2662 basePvs.MergeInPlace((*it)->GetPvs()); 2663 } 2664 2665 // update samples and globalC 2666 samples = (float)pvs.GetSamples(); 2667 // cout<<"neighboring viewcells = "<<i-1<<endl; 2668 // cout<<"Samples' = "<<samples<<endl; 2703 2669 } 2704 2670 2705 2671 // Minimal number of samples so that filtering takes place 2706 2672 #define MIN_SAMPLES 50 2707 2673 2708 2674 if (samples > MIN_SAMPLES) { 2709 float globalC = 2.0f*filterSize/sqrt(samples); 2710 2711 pit = basePvs.GetIterator(); 2712 2713 ObjectContainer objects; 2714 2715 while (pit.HasMoreEntries()) 2716 { 2717 ObjectPvsEntry entry = pit.Next(); 2718 2719 Intersectable *object = entry.mObject; 2720 // compute filter size based on the distance and the numebr of samples 2721 AxisAlignedBox3 box = object->GetBox(); 2722 2723 float distance = Distance(center, box.Center()); 2724 float globalRadius = distance*globalC; 2725 2726 int objectSamples = (int)entry.mData.mSumPdf; 2727 float localRadius = MAX_FLOAT; 2728 2729 localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/ 2730 sqrt((float)objectSamples); 2731 2732 // cout<<"lr="<<localRadius<<" gr="<<globalRadius<<endl; 2733 2734 // now compute the filter size 2735 float radius; 2736 2675 float globalC = 2.0f*filterSize/sqrt(samples); 2676 2677 pit = basePvs.GetIterator(); 2678 2679 ObjectContainer objects; 2680 2681 while (pit.HasMoreEntries()) { 2682 ObjectPvsEntry entry = pit.Next(); 2683 2684 Intersectable *object = entry.mObject; 2685 // compute filter size based on the distance and the numebr of samples 2686 AxisAlignedBox3 box = object->GetBox(); 2687 2688 float distance = Distance(center, box.Center()); 2689 float globalRadius = distance*globalC; 2690 2691 int objectSamples = (int)entry.mData.mSumPdf; 2692 float localRadius = MAX_FLOAT; 2693 2694 localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/ 2695 sqrt((float)objectSamples); 2696 2697 // cout<<"lr="<<localRadius<<" gr="<<globalRadius<<endl; 2698 2699 // now compute the filter size 2700 float radius; 2701 2737 2702 #if 0 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2703 if (objectSamples <= 1) { 2704 if (localRadius > globalRadius) { 2705 radius = 0.5flRadius; 2706 stats.mLocalFilterCount++; 2707 } else { 2708 radius = globalRadius; 2709 stats.mGlobalFilterCount++; 2710 } 2711 } else { 2712 radius = localRadius; 2713 stats.mLocalFilterCount++; 2714 } 2750 2715 #else 2751 2752 2753 2716 radius = 0.5f*globalRadius + 0.5f*localRadius; 2717 stats.mLocalFilterCount++; 2718 stats.mGlobalFilterCount++; 2754 2719 #endif 2755 2756 stats.mAvgFilterRadius += radius; 2757 2758 // cout<<"box = "<<box<<endl; 2759 // cout<<"distance = "<<distance<<endl; 2760 // cout<<"radiues = "<<radius<<endl; 2761 2762 box.Enlarge(Vector3(radius)); 2763 if (filteredBoxes) 2764 filteredBoxes->push_back(box); 2765 objects.clear(); 2766 // $$ warning collect objects takes only unmailed ones! 2767 CollectObjects(box, objects); 2768 // cout<<"collected objects="<<objects.size()<<endl; 2769 ObjectContainer::const_iterator noi = objects.begin(); 2770 for (; noi != objects.end(); ++ noi) { 2771 Intersectable *o = *noi; 2772 // $$ JB warning: pdfs are not correct at this point! 2773 pvs.AddSampleDirty(o, Limits::Small); 2774 } 2775 } 2776 stats.mAvgFilterRadius /= (stats.mLocalFilterCount + stats.mGlobalFilterCount); 2720 2721 stats.mAvgFilterRadius += radius; 2722 2723 // cout<<"box = "<<box<<endl; 2724 // cout<<"distance = "<<distance<<endl; 2725 // cout<<"radiues = "<<radius<<endl; 2726 2727 box.Enlarge(Vector3(radius)); 2728 2729 if (filteredBoxes) 2730 filteredBoxes->push_back(box); 2731 2732 objects.clear(); 2733 // $$ warning collect objects takes only unmailed ones! 2734 CollectObjects(box, objects); 2735 // cout<<"collected objects="<<objects.size()<<endl; 2736 ObjectContainer::const_iterator noi = objects.begin(); 2737 for (; noi != objects.end(); ++ noi) { 2738 Intersectable *o = *noi; 2739 // $$ JB warning: pdfs are not correct at this point! 2740 pvs.AddSampleDirty(o, Limits::Small); 2741 } 2742 } 2743 stats.mAvgFilterRadius /= (stats.mLocalFilterCount + stats.mGlobalFilterCount); 2777 2744 } 2778 2745 2779 2746 Debug<<" nPvs size = "<<pvs.GetSize()<<endl; 2780 2747 2781 2748 #if !USE_KD_PVS 2782 2749 // copy the base pvs to the new pvs 2783 2750 pit = basePvs.GetIterator(); 2784 2751 while (pit.HasMoreEntries()) { 2785 2786 2752 ObjectPvsEntry entry = pit.Next(); 2753 pvs.AddSampleDirty(entry.mObject, entry.mData.mSumPdf); 2787 2754 } 2788 2755 #endif 2756 2757 pvs.SimpleSort(); 2789 2758 viewCell->SetFilteredPvsSize(pvs.GetSize()); 2790 2759 … … 2792 2761 return stats; 2793 2762 } 2794 #else2795 PvsFilterStatistics2796 ViewCellsManager::ApplyFilter2(ViewCell *viewCell,2797 const bool useViewSpaceFilter,2798 const float filterSize,2799 ObjectPvs &pvs2800 )2801 {2802 cout << "x";2803 PvsFilterStatistics stats;2804 2805 AxisAlignedBox3 vbox = GetViewCellBox(viewCell);2806 Vector3 center = vbox.Center();2807 // copy the PVS2808 ObjectPvs basePvs;// = viewCell->GetPvs();2809 Intersectable::NewMail();2810 2811 ObjectPvsIterator pit = viewCell->GetPvs().GetIterator();2812 2813 #if !USE_KD_PVS2814 // first mark all object from this pvs2815 while (pit.HasMoreEntries())2816 {2817 ObjectPvsEntry entry = pit.Next();2818 2819 Intersectable *object = entry.mObject;2820 object->Mail();2821 }2822 #endif2823 2824 int pvsSize = 0;2825 int nPvsSize = 0;2826 float samples = (float)viewCell->GetPvs().GetSamples();2827 2828 Debug<<"f #s="<<samples<<"pvs size = "<<viewCell->GetPvs().GetSize();2829 // cout<<"Filter size = "<<filterSize<<endl;2830 // cout<<"vbox = "<<vbox<<endl;2831 // cout<<"center = "<<center<<endl;2832 2833 2834 // Minimal number of local samples to take into account2835 // the local sampling density.2836 // The size of the filter is a minimum of the conservative2837 // local sampling density estimate (#rays intersecting teh viewcell and2838 // the object)2839 // and gobal estimate for the view cell2840 // (total #rays intersecting the viewcell)2841 #define MIN_LOCAL_SAMPLES 52842 2843 float viewCellRadius = 0.5f*Magnitude(vbox.Diagonal());2844 2845 // now compute the filter box around the current viewCell2846 2847 if (useViewSpaceFilter)2848 {2849 // float radius = Max(viewCellRadius/100.0f, avgRadius - viewCellRadius);2850 float radius = viewCellRadius/100.0f;2851 vbox.Enlarge(radius);2852 cout<<"vbox = "<<vbox<<endl;2853 ViewCellContainer viewCells;2854 ComputeBoxIntersections(vbox, viewCells);2855 2856 MergeViewCellsEfficient(basePvs, viewCells);2857 cout << "basepvs size " << basePvs.GetSize() << endl;2858 // update samples and globalC2859 samples = (float)pvs.GetSamples();2860 // cout<<"neighboring viewcells = "<<i-1<<endl;2861 // cout<<"Samples' = "<<samples<<endl;2862 }2863 else2864 {2865 basePvs = viewCell->GetPvs();2866 }2867 2868 // Minimal number of samples so that filtering takes place2869 #define MIN_SAMPLES 1002870 if (samples > MIN_SAMPLES)2871 {2872 float globalC = 2.0f*filterSize/sqrt(samples);2873 2874 pit = basePvs.GetIterator();2875 2876 ObjectContainer objects;2877 2878 while (pit.HasMoreEntries())2879 {2880 ObjectPvsEntry entry = pit.Next();2881 2882 Intersectable *object = entry.mObject;2883 // compute filter size based on the distance and the numebr of samples2884 AxisAlignedBox3 box = object->GetBox();2885 2886 float distance = Distance(center, box.Center());2887 float globalRadius = distance*globalC;2888 2889 int objectSamples = (int)entry.mData.mSumPdf;2890 float localRadius = MAX_FLOAT;2891 2892 if (objectSamples > MIN_LOCAL_SAMPLES)2893 {2894 localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/2895 sqrt((float)objectSamples);2896 }2897 2898 // cout<<"lr="<<localRadius<<" gr="<<globalRadius<<endl;2899 2900 // now compute the filter size2901 float radius;2902 2903 if (localRadius < globalRadius)2904 {2905 radius = localRadius;2906 stats.mLocalFilterCount++;2907 }2908 else2909 {2910 radius = globalRadius;2911 stats.mGlobalFilterCount++;2912 }2913 2914 stats.mAvgFilterRadius += radius;2915 2916 // cout<<"box = "<<box<<endl;2917 // cout<<"distance = "<<distance<<endl;2918 // cout<<"radiues = "<<radius<<endl;2919 2920 box.Enlarge(Vector3(radius));2921 objects.clear();2922 2923 // $$ warning collect objects takes only unmailed ones!2924 CollectObjects(box, objects);2925 2926 // cout<<"collected objects="<<objects.size()<<endl;2927 ObjectContainer::const_iterator noi = objects.begin();2928 for (; noi != objects.end(); ++ noi)2929 {2930 Intersectable *o = *noi;2931 // $$ JB warning: pdfs are not correct at this point!2932 pvs.AddSampleDirty(o, Limits::Small);2933 }2934 }2935 2936 stats.mAvgFilterRadius /= (stats.mLocalFilterCount + stats.mGlobalFilterCount);2937 }2938 2939 Debug<<" nPvs size = "<<pvs.GetSize()<<endl;2940 2941 #if !USE_KD_PVS2942 // copy the base pvs to the new pvs2943 pit = basePvs.GetIterator();2944 while (pit.HasMoreEntries())2945 {2946 ObjectPvsEntry entry = pit.Next();2947 pvs.AddSampleDirty(entry.mObject, entry.mData.mSumPdf);2948 }2949 #endif2950 viewCell->SetFilteredPvsSize(pvs.GetSize());2951 2952 Intersectable::NewMail();2953 2954 return stats;2955 }2956 #endif2957 2763 2958 2764 … … 6043 5849 6044 5850 6045 #define PVS_ADD_DIRTY 16046 6047 float6048 VspOspViewCellsManager::ComputeSampleContribution(VssRay &ray,6049 const bool addRays,6050 const bool storeViewCells)6051 {6052 ray.mPvsContribution = 0;6053 ray.mRelativePvsContribution = 0.0f;6054 6055 if (ray.mTerminationObject==NULL)6056 return 0.0f;6057 6058 ViewCellContainer viewcells;6059 6060 static Ray hray;6061 hray.Init(ray);6062 6063 float tmin = 0, tmax = 1.0;6064 6065 if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax))6066 return 0;6067 6068 Vector3 origin = hray.Extrap(tmin);6069 Vector3 termination = hray.Extrap(tmax);6070 6071 ViewCell::NewMail();6072 6073 // traverse the view space subdivision6074 CastLineSegment(origin, termination, viewcells);6075 6076 if (storeViewCells)6077 {6078 // copy viewcells memory efficiently6079 ray.mViewCells.reserve(viewcells.size());6080 ray.mViewCells = viewcells;6081 }6082 6083 ViewCellContainer::const_iterator it = viewcells.begin();6084 6085 Intersectable *terminationObj = GetIntersectable(ray, true);6086 6087 #if SAMPLE_ORIGIN_OBJECTS6088 Intersectable *originObj = GetIntersectable(ray, false);6089 #endif6090 6091 for (; it != viewcells.end(); ++ it)6092 {6093 ViewCell *viewcell = *it;6094 6095 if (viewcell->GetValid())6096 { // if ray not outside of view space6097 float contribution;6098 6099 if (terminationObj)6100 {6101 // todo: maybe not correct for kd node pvs6102 if (viewcell->GetPvs().GetSampleContribution(6103 terminationObj, ray.mPdf, contribution))6104 {6105 ++ ray.mPvsContribution;6106 }6107 6108 ray.mRelativePvsContribution += contribution;6109 }6110 6111 ////////////////6112 //-- for directional sampling it is important to count6113 //-- only contributions made in one direction!6114 //-- the other contributions of this sample will be counted for the opposite ray!6115 6116 #if SAMPLE_ORIGIN_OBJECTS6117 6118 if (originObj &&6119 viewcell->GetPvs().GetSampleContribution(originObj,6120 ray.mPdf,6121 contribution))6122 {6123 ++ ray.mPvsContribution;6124 ray.mRelativePvsContribution += contribution;6125 }6126 #endif6127 }6128 }6129 6130 if (!addRays) {6131 return ray.mRelativePvsContribution;6132 }6133 6134 // sampled objects are stored in the pvs6135 for (it = viewcells.begin(); it != viewcells.end(); ++ it) {6136 ViewCell *viewCell = *it;6137 6138 if (!viewCell->GetValid())6139 break;6140 6141 //$$JB hack6142 6143 #if PVS_ADD_DIRTY6144 viewCell->GetPvs().AddSampleDirtyCheck(terminationObj, ray.mPdf);6145 #else6146 viewCell->GetPvs().AddSample(terminationObj, ray.mPdf);6147 #endif6148 6149 #if SAMPLE_ORIGIN_OBJECTS6150 #if PVS_ADD_DIRTY6151 viewCell->GetPvs().AddSampleDirtyCheck(originObj, ray.mPdf);6152 #else6153 viewCell->GetPvs().AddSample(originObj, ray.mPdf);6154 #endif6155 #endif6156 if (viewCell->GetPvs().RequiresResort()) {6157 viewCell->GetPvs().Sort();6158 }6159 }6160 6161 return ABS_CONTRIBUTION_WEIGHT*ray.mPvsContribution +6162 (1.0f - ABS_CONTRIBUTION_WEIGHT)*ray.mRelativePvsContribution;6163 }6164 5851 6165 5852
Note: See TracChangeset
for help on using the changeset viewer.