Index: /GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.sh
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.sh	(revision 1456)
+++ /GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.sh	(revision 1744)
@@ -1,58 +1,29 @@
 #!/bin/sh
 
-PROGRAM=../bin/release/Preprocessor.exe
-#PROGRAM=../bin/Debug/Preprocessor.exe
+# add bin to path
+export PATH=../bin:$PATH
 
-if [ $# -ne 3 ]
-then
-    echo "Usage:"
-	echo "preprocess_visibility scene_file.x3d viewcells_file.xml number_of_viewcells"
-    exit
-fi
+#TARGET=debug
+TARGET=release
 
+PROGRAM=../bin/$TARGET/Preprocessor.exe
 
-
-SCENE=$1
-VIEWCELLS=$2
-NUM_VIEWCELLS=$3
-NUM_SAMPLE_RAYS=500000
-
-LOG_PREFIX=generate_viewcells
-
+SCENE=vienna_cropped
+LOG_PREFIX=../scripts/$SCENE
 ENVIRONMENT=generate_viewcells.env
 
+echo "starting $TARGET mode for $SCENE scene"
 
-###############################################
+############################################################################
 
-METHOD=avs
-echo "$SCENE $METHOD"
+ METHOD=gradient
+ echo "$SCENE $METHOD"
 
-$PROGRAM $ENVIRONMENT \
--scene_filename=$SCENE \
--view_cells_type=vspBspTree \
--view_cells_construction_samples=0 \
--vsp_bsp_construction_samples=$NUM_SAMPLE_RAYS \
--vsp_bsp_term_max_view_cells=$NUM_VIEWCELLS \
--view_cells_evaluation_stats_prefix=$LOG_PREFIX-$METHOD \
--view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
--vsp_bsp_subdivision_stats=$LOG_PREFIX-$METHOD-subdivisionStats.log \
--view_cells_post_process_merge=false \
--view_cells_active=$NUM_VIEWCELLS \
--view_cells_filename=$VIEWCELLS \
--view_cells_export_pvs=true \
--view_cells_export_bounding_boxes=true \
--vsp_bsp_term_min_pvs=0 \
--view_cells_evaluate=false \
--vsp_bsp_use_cost_heuristics=true \
--vsp_bsp_max_poly_candidates=0 \
--vsp_bsp_use_split_cost_queue=true \
--view_cells_show_visualization=true \
--view_cells_construction_samples=0 \
--view_cells_evaluation_samples=2000000 \
--view_cells_evaluation_samples_per_pass=1000000
-
-
-mv debug.log $LOG_PREFIX-$METHOD-debug.log
-mv merged_view_cells.wrl $LOG_PREFIX-$METHOD-merged_view_cells.wrl
-mv merged_view_cells_pvs.wrl $LOG_PREFIX-$METHOD-merged_view_cells_pvs.wrl
-#mv final_view_cells.x3d $LOG_PREFIX-$METHOD-final_view_cells.x3d
+ $PROGRAM $ENVIRONMENT \
+ -view_cells_filename=$LOG_PREFIX-$METHOD-viewcells.xml.gz \
+  -view_cells_merge_stats=$LOG_PREFIX-$METHOD-mergeStats.log \
+  -vsp_subdivision_stats=$LOG_PREFIX-$METHOD-vsp-subdivisionStats.log \
+  -bvh_subdivision_stats=$LOG_PREFIX-$METHOD-bvh-subdivisionStats.log \
+  -hierarchy_subdivision_stats=$LOG_PREFIX-$METHOD-hierarchy-subdivisionStats.log \
+  -hierarchy_construction_type=2 \
+  -hierarchy_construction_consider_memory=true
Index: /GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.env
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.env	(revision 1455)
+++ /GTP/trunk/Lib/Vis/Preprocessing/scripts/generate_viewcells.env	(revision 1744)
@@ -1,3 +1,3 @@
-#############################################################################
+##############################################################################
 # ERS default configuration file
 # Jiri Bittner 2003
@@ -5,50 +5,48 @@
 
 Scene {
+
 #	filename glasgow1.x3d
 #	filename vienna.x3d
 #	filename ../data/vienna/vienna-simple.x3d
 #	filename ../data/vienna/vienna-buildings.x3d
-	filename ../data/vienna/city1500_flat_1.x3d
-#	filename ../data/vienna/vienna-roads.x3d
-#	filename ../data/vienna/vienna-buildings.x3d;../data/vienna/vienna-roads.x3d;../data/vienna/vienna-roofs.x3d
-#filename ../data/vienna/vienna-buildings.x3d;../data/vienna/vienna-roofs.x3d;../data/vienna/vienna-roads.x3d
-#filename ../data/vienna/city1500_flat_1.x3d;../data/vienna/vienna-roofs.x3d;../data/vienna/vienna-roads.x3d
-#filename ../data/vienna/vienna-buildings.x3d;../data/vienna/vienna-roofs.x3d;../data/vienna/vienna-roads.x3d;../data/vienna/vienna-plane.x3d
-#filename ../data/vienna/city1500_flat_1.x3d;../data/vienna/vienna-roofs.x3d;../data/vienna/vienna-roads.x3d;../data/vienna/vienna-plane.x3d
+#	filename ../data/vienna/vienna-roofs.x3d
+#	filename ../data/vienna/vienna-buildings.x3d;../data/vienna/vienna-roofs.x3d;../data/vienna/vienna-roads.x3d
+#	filename ../data/vienna/vienna-buildings_vienna-roofs_vienna-roads_vienna-plane_cropped.obj
+	filename ../data/vienna/vienna_cropped.obj
+#;../data/vienna/vienna-plane.x3d
+#	filename ../data/vienna/vienna-buildings.x3d;../data/vienna/vienna-roofs.x3d;../data/vienna/vienna-roads.x3d;../data/vienna/vienna-plane.x3d
 #	filename ../data/vienna/viewcells-25-sel.x3d
 #	filename ../data/atlanta/atlanta2.x3d
 #	filename ../data/soda/soda.dat
 #	filename ../data/soda/soda5.dat
-}
+#	filename ../data/artificial/cube_test.x3d
+#	filename ../data/grandcanyon1_RotXmin90.obj
+#	filename ../data/CityModel.obj
+#	filename ../data/arena/arena-low-lods.obj
+}
+
 
 Preprocessor {
-	# stored sample rays
-	samplesFilename rays.out
 	useGlRenderer false
-#	type sampling
 	type vss
-#	type rss
 	detectEmptyViewSpace true
-	loadMeshes true
-	applyVisibilityFilter true
-	applyVisibilitySpatialFilter true
-}
+	loadMeshes false
+	# internal raycaster
+	rayCastMethod 0
+	# intel raycaster
+	#rayCastMethod 1
+	exportVisibility false
+	loadKdTree false
+	exportKdTree false 
+#	exportObj true
+}
+
 
 VssPreprocessor {
-	samplesPerPass  100000
 	initialSamples 0
 	vssSamples 0
-	vssSamplesPerPass 500000
 	useImportanceSampling true
 	loadInitialSamples  false
 	storeInitialSamples false
-	useViewSpaceBox false
-#	testBeamSampling true
-}
-
-
-SamplingPreprocessor {
-	totalSamples 500000
-	samplesPerPass  3
 }
 
@@ -58,5 +56,5 @@
 	epsilon		1e-6
 	
-	maxDepth 	40
+	maxDepth 	2
 	minPvs		30
 	minRays		800
@@ -74,5 +72,5 @@
 	
 	interleaveDirSplits	true
-    dirSplitDepth 0
+    	dirSplitDepth 0
 	
 	numberOfEndPointDomains	10000
@@ -83,63 +81,6 @@
 }
 
-RssPreprocessor {
-	samplesPerPass  100000
-	#initialSamples 500000
-	#vssSamples 10000000
-	#for view cell construction
-	initialSamples 0
-	vssSamples 0
-	vssSamplesPerPass 500000
-	useImportanceSampling true
-
-	directionalSampling true
-	objectBasedSampling false
-
-	Export {
-		pvs false
-		rssTree false
-		rays true
-		numRays 5000
-	}
-
-	useViewcells true
-	updateSubdivision true
-	loadInitialSamples false
-	storeInitialSamples false
-}
-
-
-RssTree {
-
-	epsilon		1e-6
-
-	maxDepth 	40
-	minPvs		3
-	minRays		30
-	minSize		0.001
- 	maxCostRatio 1.0
-	maxRayContribution 0.5
-	maxRays		1000000
-	maxTotalMemory	200
-	maxStaticMemory	100
-
-#	splitType regular
-#	splitType heuristic
-	splitType hybrid
-	splitUseOnlyDrivingAxis true
-	importanceBasedCost false
-
-	interleaveDirSplits	true
-    dirSplitDepth 0
-
-	numberOfEndPointDomains	10000
-	ct_div_ci	0.0
-	randomize	false
-
-	refDirBoxMaxSize	0.1
-}
 
 Limits {
-
        threshold	1e-6
        small		1e-6
@@ -147,10 +88,27 @@
 }
 
+
 Unigraphics {
-		meshGrouping 1
-}
+	meshGrouping 1
+}
+
 
 KdTree {
 	sahUseFaces true
+
+	Termination {
+		minCost 0
+		maxDepth 20
+		
+		maxCostRatio 1.5
+		ct_div_ci 0.5
+	}
+
+#	splitMethod spatialMedian
+	splitMethod SAH
+	splitBorder 0.01
+}
+
+MeshKdTree {
 	Termination {
 		minCost 1
@@ -165,28 +123,16 @@
 }
 
-MeshKdTree {
-	Termination {
-		minCost 1
-		maxDepth 18
-		maxCostRatio 0.9
-		ct_div_ci 0.5
-	}
-
-#	splitMethod spatialMedian
-	splitMethod SAH
-	splitBorder 0.01
-}
-
-
 
 ViewCells {
-	# samples used for view cell construction
+	
+	# samples used for view cell construction 
+	# (after the sampling used for the hierarchy)
 	Construction {
 		samples 0
-		samplesPerPass 1500000
-	}
-
-	#number of active view cells
-	active 200000
+		samplesPerPass 1000000
+	}
+
+	# number of active view cells
+	active 50000
 	maxStaticMemory	40
 
@@ -194,13 +140,9 @@
 	loadFromFile false
 
-	#type kdTree
-	#type vspKdTree
-	#type bspTree
-	type vspBspTree
-	#type sceneDependent
-	
-	height 5.0
-	maxViewCells 3000
-
+	exportPvs false
+
+	# type of view cells
+	type vspOspTree
+	
 	#percentage of total visible objects where pvs is considered invalid
 	maxPvsRatio 1.0
@@ -208,238 +150,138 @@
 	processOnlyValidViewCells false
 
-	#stats viewCellStats.log
-
+	#samplingType object_directional
 	#samplingType directional
 	samplingType box
+	#samplingType reverse_object
+	#samplingType object
 
 	PostProcess {
 		# how much samples are used for post processing
 		samples 0
-		renderCostWeight 1.0
-		maxCostRatio 0.1
-		minViewCells 1
-		avgCostMaxDeviation 0.01
 		maxMergesPerPass 5000
 		useRaysForMerge false
+		refine false
 		compress false
-		merge true
+		merge false
 	}
 
 	Visualization {
 		# how much samples we use for visualization
-		samples 0
-		#colorCode PVS
-		#colorCode MergedLeaves
-		#colorCode MergedTreeDiff
-		colorCode Random
-		exportRays false
+		samples 200
+		exportRays true
 		exportGeometry true
 		exportMergedViewCells false
 		useClipPlane true
 		clipPlaneAxis 1
-		clipPlanePos  0.3
+		clipPlanePos 0.3
+		maxOutput 2
 	}
 
 	showVisualization true
+	#showVisualization false
 	evaluateViewCells false
 	
 	Evaluation {
-		samplesPerPass 1000000
-		samples 1000000
+		samples         80000000
+		samplesForStats 80000000
+		samplesPerPass  2000000
+
+		stepSize	500
+
+		#samplingType object_directional
+		#samplingType reverse_object
+		#samplingType object
+		samplingType box
+
 		statsPrefix ../scripts/viewCells
-		#samplingType directional
-		samplingType box
-		histogram true
-		histoStepSize 5000
-	}
-
-#	filename ../data/atlanta/atlanta_viewcells_large.x3d
-#	filename ../data/vienna/viewcells-25-sel.x3d
-#	filename ../data/vienna/viewcells-25.x3d
-#	filename ../data/vienna/viewcells-large-sel.x3d
-	filename ../data/vienna/vienna-visibility.xml.zip
-#	filename ../scripts/vienna-visibility.xml.zip
-}
-
-
-Simulation {
-	objRenderCost 1.0
-	vcOverhead 1.0
-	# always between 0 and 1
-	moveSpeed 0.0001
-}
-
-
-VspBspTree {
+	}
+}
+
+
+################################
+#
+# View space partitioning kd tree
+#
+
+VspTree {
 	Construction {
-		samples 500000
-		epsilon 0.0000001
-		randomize false
-		renderCostWeight 1.0
-	}
-
-
-	# random polygon       = 1
-	# axis aligned         = 2
-	# least ray splits     = 256
-	# balanced rays        = 512
-	# pvs                  = 1024
-	
-	splitPlaneStrategy 1026
-	
-	# maximal candidates for split planes
-	maxPolyCandidates 0
-
-	usePolygonSplitIfAvailable false
-
-	# maximal tested rays for split cost heuristics
-	maxTests 10000
-	
-	maxTotalMemory	50
-	maxStaticMemory	50
-
-	subdivisionStats ../subdivisionStats.log
-
-	# factors for evaluating split plane costs
-	Factor {
-		leastRaySplits 1.0
-	  	balancedRays 1.0
-	  	pvs 1.0
-	}
-	
-	Termination {
-		# parameters used for autopartition
-		minRays 		-15
-		minPolygons 		-1
-		maxDepth		25
-		minPvs 			0
-		minProbability		0.000001
-		maxRayContribution	1
-		maxCostRatio		0.9
-		missTolerance		6
-		globalCostMissTolerance 4
-		#minGlobalCostRatio      0.0000001
-		minGlobalCostRatio      0.0001
-		maxViewCells		5000
-	
-
-		# used for pvs criterium
-		ct_div_ci 0.0
-		
-		AxisAligned {
-			minRays 		50000
-			maxRayContribution	9.9
-		}
-	}
-	
-	useSplitCostQueue true
-	useCostHeuristics false
-
+		renderCostDecreaseWeight 1.0
+	}
+
+	Termination {
+		minPvs 0
+		maxViewCells 500000
+	}
+
+	useCostHeuristics true
 	splitUseOnlyDrivingAxis false
-	simulateOctree false
-	useRandomAxis false
-	usePolygonSplitIfAvailable false
-	nodePriorityQueueType 0
-
-	Visualization {
-		# x3d visualization of the split planes
-		exportSplits false
-	}
-}
-
-BspTree {
+
+	# maximum number of tests per node
+	maxTests 50000
+}
+
+
+##########################
+#
+# The bounding volume hierarchy
+#
+
+BvHierarchy {
+
+	Construction {		
+		renderCostDecreaseWeight 1.0
+	}
+
+	Termination {
+		maxLeaves 50000000
+	}
+
+	minRaysForVisibility 15
+
+	# use only surface area heuristic	
+	useCostHeuristics true
+	useSah false
+
+	splitUseOnlyDrivingAxis false
+
+	maxTests 50000
+}
+
+
+###############################################################
+#
+# Manages the construction of view space and object space partition
+#
+
+Hierarchy {
+	# the type of object space partition: view space is always vsp (=kd) partition
+	type bvh
+	
 	Construction {
-		samples 100000
-		epsilon 0.005
-	}
-
-
-	# random polygon       = 1
-	# axis aligned         = 2
-	# least splits         = 4
-	# balanced polygons    = 8
-	# balanced view cells  = 16
-	# largest polygon area = 32
-	# vertical axis        = 64
-	# blocked rays         = 128
-	# least ray splits     = 256
-	# balanced rays        = 512
-	# pvs                  = 1024
-
-	# least splits + balanced polygons
-	#splitPlaneStrategy 12
-	
-	#axis aligned + vertical axis
-	#splitPlaneStrategy 66
-	
-	# axis aligned + balanced view cells
-	# splitPlaneStrategy 18
-	
-	# largest polygon area
-	#splitPlaneStrategy 32
-	
-	# axus aligned + balanced polygons
-	#splitPlaneStrategy 72
-	
-	# axis aligned + blocked rays
-	#splitPlaneStrategy 130
-	
-	#splitPlaneStrategy 384
-	#splitPlaneStrategy 130
-	
-	splitPlaneStrategy 32
-	
-	maxPolyCandidates 100
-	maxRayCandidates 0
-	
-	maxTests 10000
-	
-	subdivisionStats ../subDivisionStats.log
-
-	# factors for evaluating split plane costs
-	Factor {
-		verticalSplits 1.0
-	  	largestPolyArea 1.0
-	  	blockedRays 1.0
-	  	leastRaySplits 1.0
-	  	balancedRays 1.0
-	  	pvs 1.0
-	  	leastSplits 1.0
-	  	balancedPolys 1.0
-	  	balancedViewCells 1.0
-	}
-	
-	Termination {
-		# parameters used for autopartition
-		minRays -1
-		minPolygons 0
-		maxDepth 30
-		minPvs -1
-		minProbability 0.00001
-		maxRayContribution 9999
-		maxViewCells 50000
-
-		# used for pvs criterium
-		ct_div_ci 0.0
-	
-		maxCostRatio 0.9
-		
-		# axis aligned splits
-		AxisAligned {
-			minPolys 5000
-			minRays 500
-			minObjects 10
-			ct_div_ci 0.5
-		}
-	}
-	
-	AxisAligned {
-		splitBorder 0.01
-	}
-	
-	
-	Visualization {
-		# x3d visualization of the split planes
-		exportSplits false
-	}
-}
+
+		samples 3000000
+
+		# type 0 = sequential computation, 1 = interleaved, 2 = gradient
+		type 2
+
+		###############################
+		# only for interleaved method
+
+		# minimal steps of same type for gradient method
+		minStepsOfSameType 100
+		# maximum steps of same type for gradient method
+		maxStepsOfSameType 900
+
+		# maximum number of repair steps per gradient step
+		maxRepairs 1000
+	}
+
+	Termination {
+		# maximal number of leaves
+		maxLeaves 100000
+		# maximal memory in MB
+		maxMemory 20
+		# minimum ratio of global cost decrease
+		minGlobalCostRatio -1
+	}
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1727)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1744)
@@ -2542,10 +2542,5 @@
 					optBool,
 					"hierarchy_construction_consider_memory=",
-					"false");
-
-	RegisterOption("Hierarchy.Construction.considerMemory2",
-					optBool,
-					"hierarchy_construction_consider_memory2=",
-					"false");
+					"true");
 
 	RegisterOption("Hierarchy.Construction.repairQueue",
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1743)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1744)
@@ -128,4 +128,5 @@
 	int mObjectSpaceSplits;
 
+	float mPriority;
 
 	float VspOspRatio() const { return (float)mViewSpaceSplits / (float)mObjectSpaceSplits; }
@@ -145,4 +146,5 @@
 		mViewSpaceSplits = 0;
 		mObjectSpaceSplits = 0;
+		mPriority = 0;
 	}
 
@@ -287,5 +289,4 @@
 	inline bool ConsiderMemory() const { return mConsiderMemory; }
 	//inline float GetMemoryConst() const { return mMemoryConst; }
-
 	
 	void EvaluateSubdivision(const VssRayContainer &sampleRays,											 
@@ -625,6 +626,4 @@
 	bool mConsiderMemory;
 
-	bool mConsiderMemory2;
-
 	int mMaxRepairs;
 
@@ -633,4 +632,7 @@
 	friend OspTree;
 	friend BvHierarchy;
+
+	float mPriority;
+
 	friend ViewCellsParseHandlers;
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1737)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1744)
@@ -294,6 +294,7 @@
 	//mMemoryConst = (float)(sizeof(VspLeaf) + sizeof(VspViewCell));
 	//mMemoryConst = (float)sizeof(BvhLeaf);
-mMemoryConst = (float)sizeof(ObjectContainer);
-	cout << "bvh memcost: " << mMemoryConst << endl;
+	mMemoryConst = (float)sizeof(ObjectContainer);
+
+	/*cout << "bvh memcost: " << mMemoryConst << endl;
 	cout << "triangle: " << sizeof(TriangleIntersectable) << endl;
 	cout << "triangle: " << sizeof(Intersectable) << endl;
@@ -301,5 +302,5 @@
 	cout << "triangle: " << sizeof(float) << endl;
 	cout << "triangle: " << sizeof(int) << endl;
-
+*/
     mUseBboxAreaForSah = true;
 
@@ -327,5 +328,5 @@
 	Debug << "minimal rays for visibility: " << mMinRaysForVisibility << endl;
 	Debug << "bvh mem const: " << mMemoryConst << endl;
-
+cout << "here10 " << mRenderCostDecreaseWeight << endl;
 	Debug << endl;
 }
@@ -509,13 +510,4 @@
 		EvaluateLeafStats(tData);
         
-		const bool mStoreRays = true;
-		if (mStoreRays)
-		{
-			BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(currentNode);
-		//	CollectRays(leaf->mObjects, leaf->mVssRays);
-		}
-		
-		//////////////////////////////////////
-		
 		// this leaf is no candidate for splitting anymore
 		// => detach subdivision candidate
@@ -598,5 +590,5 @@
 		{
 			priority = factor * renderCostDecr + (1.0f - factor) * oldRenderCost;
-			if (mHierarchyManager->mConsiderMemory2)
+			if (mHierarchyManager->mConsiderMemory)
 			{
 				priority /= ((float)splitCandidate.GetPvsEntriesIncr() + mMemoryConst);
@@ -605,5 +597,5 @@
 		else
 		{
-			if (!mHierarchyManager->mConsiderMemory2)
+			if (!mHierarchyManager->mConsiderMemory)
 			{
 				priority = factor * renderCostDecr + (1.0f - factor) * oldRenderCost;
@@ -619,4 +611,5 @@
 	}
 
+	//splitCandidate.SetOldCost(oldRenderCost);
   	// compute global decrease in render cost
 	splitCandidate.SetPriority(priority);
@@ -1356,26 +1349,33 @@
 	
 	VssRayContainer rays;
-	rays.reserve(tData.mNumRays);
+	// maximal 2 objects share the same ray
+	rays.reserve(tData.mNumRays * 2);
 	CollectRays(tData.mNode->mObjects, rays);
 
-	const float prop = (float)mMaxTests / (float)tData.mNumRays;
+	const float prop = (float)mMaxTests / ((float)tData.mNumRays + Limits::Small);
 
 	VssRay::NewMail();
 
+	//cout << "here7 " << tData.mNumRays << " " << rays.size() << endl;
+	//cout << "here5 prop: " << prop << " " << mMaxTests << " " << tData.mNumRays << endl;
 	// only use a subset of the rays
 	VssRayContainer::const_iterator rit, rit_end = rays.end();
 
+	int nRays = 0;
+
 	for (rit = rays.begin(); rit != rit_end; ++ rit)
 	{
-		if ((mMaxTests >= tData.mNumRays) || (Random(1.0f) < prop))
+		if ((mMaxTests >= (int)rays.size()) || (Random(1.0f) < prop))
 		{
 			(*rit)->Mail();
-		}
-	}
-	
+			++ nRays;
+		}
+	}
+	
+	//cout << "here99 " << nRays << " obj " << tData.mNode->mObjects.size() << endl;
 	// collect and mark the view cells as belonging to front pvs
 	ViewCellContainer viewCells;
 	CollectViewCells(tData.mNode->mObjects, viewCells, true, true);
-			
+	
 	ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
 	for (vit = viewCells.begin(); vit != vit_end; ++ vit)
@@ -1488,5 +1488,4 @@
 				if (useVisibilityBasedHeuristics)
 				{
-					//cout << "v rays: " << tData.mNumRays << " " << endl;
 					///////////
 					//-- heuristics using objects weighted by view cells volume
@@ -1498,5 +1497,5 @@
 				}
 				else
-				{	//cout << "e rays: " << tData.mNumRays << " ";
+				{	
 					//////////////////
 					//-- view cells not constructed yet	=> use surface area heuristic			
@@ -1710,28 +1709,34 @@
 
 
-void BvHierarchy::CollectViewCells(const ObjectContainer &objects, 
-								   ViewCellContainer &viewCells,
-								   const bool setCounter,
-								   const bool onlyMailedRays) const
+int BvHierarchy::CollectViewCells(const ObjectContainer &objects, 
+								  ViewCellContainer &viewCells,
+								  const bool setCounter,
+								  const bool onlyMailedRays) const
 {
 	ViewCell::NewMail();
 	ObjectContainer::const_iterator oit, oit_end = objects.end();
 
+	int numRays = 0;
 	// loop through all object and collect view cell pvs of this node
 	for (oit = objects.begin(); oit != oit_end; ++ oit)
 	{
 		// always use only mailed objects
-		CollectViewCells(*oit, viewCells, true, setCounter, onlyMailedRays);
-	}
-}
-
-
-void BvHierarchy::CollectViewCells(Intersectable *obj, 
-								   ViewCellContainer &viewCells,
-								   const bool useMailBoxing,
-								   const bool setCounter,
-								   const bool onlyMailedRays) const
+		numRays += CollectViewCells(*oit, viewCells, true, setCounter, onlyMailedRays);
+	}
+	//cout << "here4 " << numRays << " boj: " << objects.size() << " " << onlyMailedRays << endl;
+
+	return numRays;
+}
+
+
+int BvHierarchy::CollectViewCells(Intersectable *obj, 
+								  ViewCellContainer &viewCells,
+								  const bool useMailBoxing,
+								  const bool setCounter,
+								  const bool onlyMailedRays) const
 {
 	VssRayContainer::const_iterator rit, rit_end = obj->GetOrCreateRays()->end();
+
+	int numRays = 0;
 
 	for (rit = obj->GetOrCreateRays()->begin(); rit < rit_end; ++ rit)
@@ -1742,5 +1747,6 @@
 			continue;
 
-		ray->Mail();
+		//ray->Mail();
+		++ numRays;
 
 		ViewCellContainer tmpViewCells;
@@ -1774,4 +1780,6 @@
 		}
 	}
+
+	return numRays;
 }
 
@@ -1833,6 +1841,7 @@
 	ViewCellContainer viewCells;
 	ViewCell::NewMail();
-	CollectViewCells(node->mObjects, viewCells, true, false);
-
+	int numRays = CollectViewCells(node->mObjects, viewCells, true, false);
+
+	//cout << "here6 " << numRays << endl;
 	if (0) cout << "collected " << (int)viewCells.size() << " dirty candidates" << endl;
 	
@@ -2015,7 +2024,8 @@
 
 	ViewCellContainer viewCells;
-	// here we have to account for all view cells that can 
+	
+	// we have to account for all view cells that can 
 	// be seen from the objects
-	CollectViewCells(objects, viewCells, false, false);
+	int numRays = CollectViewCells(objects, viewCells, false, false);
 
 	ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1743)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1744)
@@ -42,6 +42,6 @@
 // HACK
 const static bool SAMPLE_AFTER_SUBDIVISION = true;
-const static bool CLAMP_TO_BOX = false;
-//const static bool CLAMP_TO_BOX = true;
+//const static bool CLAMP_TO_BOX = false;
+const static bool CLAMP_TO_BOX = true;
 
 
@@ -119,5 +119,5 @@
 
 	
-	ObjectPvs pvs1, pvs2, mergedPvs;
+	/*ObjectPvs pvs1, pvs2, mergedPvs;
 
 	pvs1.AddSample((Intersectable *)1, 1);
@@ -172,5 +172,5 @@
 		cout << (int)entry.mObject << " " << entry.mData.mSumPdf << " x ";
 	}
-
+*/
 	// sampling type for view cells construction samples
 	if (strcmp(buf, "object") == 0)
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1732)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1744)
@@ -781,8 +781,8 @@
 	/** Collect view cells which see this bvh leaf.
 	*/
-	void CollectViewCells(const ObjectContainer &objects, 
-						  ViewCellContainer &viewCells,
-						  const bool setCounter,
-						  const bool onlyMailedRays) const;
+	int CollectViewCells(const ObjectContainer &objects, 
+						 ViewCellContainer &viewCells,
+						 const bool setCounter,
+						 const bool onlyUnmailedRays) const;
 
 	/** Counts the view cells of this object. note: only
@@ -797,9 +797,9 @@
 	/** Collects view cells which see an object.
 	*/
-	void CollectViewCells(Intersectable *object, 
-						  ViewCellContainer &viewCells, 
-						  const bool useMailBoxing,
-						  const bool setCounter,
-						  const bool onlyMailedRays) const;
+	int CollectViewCells(Intersectable *object, 
+						 ViewCellContainer &viewCells, 
+						 const bool useMailBoxing,
+						 const bool setCounter,
+						 const bool onlyUnmailedRays) const;
 
 	/** Evaluates increase in pvs size.
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1742)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1744)
@@ -121,14 +121,4 @@
 
 
-/*template<typename T, typename S>
-class PvsIterator
-{
-	Next(PvsEntry<T, S>)
-private:
-	typename vector<PvsEntry<T, S> >::iterator mItCurrent;
-	typename vector<PvsEntry<T, S> >::iterator mItEnd;
-};*/
-
-
 template<typename T, typename S>
 class PvsIterator
@@ -147,5 +137,5 @@
 	}
 
-	PvsEntry<T, S> Next()
+	const PvsEntry<T, S> &Next()
 	{
 		return *(mItCurrent ++);
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1738)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1744)
@@ -462,6 +462,5 @@
 	Environment::GetSingleton()->GetIntValue("VspTree.maxTests", mMaxTests);
 
-	Environment::GetSingleton()->
-		GetFloatValue("VspTree.Construction.renderCostDecreaseWeight", mRenderCostDecreaseWeight);
+	Environment::GetSingleton()->GetFloatValue("VspTree.Construction.renderCostDecreaseWeight", mRenderCostDecreaseWeight);
 	
 	// if only the driving axis is used for axis aligned split
@@ -505,5 +504,5 @@
 
 	Debug << "vsp mem const: " << mMemoryConst << endl;
-
+cout << "here11 " << mRenderCostDecreaseWeight << endl;
 	Debug << endl;
 }
@@ -852,5 +851,5 @@
 	{
 		priority = factor * renderCostDecr + (1.0f - factor) * oldRenderCost;
-		if (mHierarchyManager->mConsiderMemory2)
+		if (mHierarchyManager->mConsiderMemory)
 		{
 			priority /= ((float)splitCandidate.GetPvsEntriesIncr() + mMemoryConst);
@@ -859,5 +858,5 @@
 	else
 	{
-		if (!mHierarchyManager->mConsiderMemory2)
+		if (!mHierarchyManager->mConsiderMemory)
 		{
 			priority = factor * renderCostDecr + (1.0f - factor) * oldRenderCost;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1743)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1744)
@@ -91,4 +91,5 @@
 		<< "#FullMem\n" << mFullMemory << endl
 		<< "#RenderCostDecrease\n" << mRenderCostDecrease << endl
+		<< "#Priority\n" << mPriority << endl
 		<< "#FpsPerMb\n" << FpsPerMb() << endl
 		<< endl;
@@ -142,7 +143,4 @@
 	Environment::GetSingleton()->GetBoolValue(
 		"Hierarchy.Construction.considerMemory", mConsiderMemory);
-
-	Environment::GetSingleton()->GetBoolValue(
-		"Hierarchy.Construction.considerMemory2", mConsiderMemory2);
 
 	Environment::GetSingleton()->GetFloatValue(
@@ -166,5 +164,4 @@
 	Debug << "maximal allowed memory: " << mTermMaxMemory << endl;
 	Debug << "consider memory: " << mConsiderMemory << endl;
-	Debug << "consider memory2: " << mConsiderMemory << endl;
 	Debug << "min steps of same kind: " << mMinStepsOfSameType << endl;
 	Debug << "max steps of same kind: " << mMaxStepsOfSameType << endl;
@@ -266,8 +263,4 @@
 {
 	SubdivisionCandidate *splitCandidate = splitQueue.Top();
-
-	if (splitCandidate->IsDirty())
-		splitCandidate->EvalCandidate();
-
 	splitQueue.Pop();
 
@@ -287,4 +280,5 @@
 		
 	HierarchySubdivisionStats stats;
+
 	stats.mNumSplits = mHierarchyStats.Leaves();
 	stats.mTotalRenderCost = mHierarchyStats.mTotalCost;
@@ -295,5 +289,7 @@
 	stats.mViewSpaceSplits = mVspTree->mVspStats.Leaves();
 	stats.mObjectSpaceSplits = GetObjectSpaceSubdivisionLeaves();
-	
+	stats.mRenderCostDecrease = mHierarchyStats.mRenderCostDecrease;
+	stats.mPriority = mPriority;
+
 	stats.Print(mSubdivisionStats);
 }
@@ -740,9 +736,14 @@
 	const bool success = sc->Apply(splitQueue, terminationCriteriaMet);
 
+	if (sc->IsDirty())
+		cout << "*******************************error!" << endl;
+
 	if (!success) // split was not taken
 	{
+		cout << "x";
 		return false;
 	}
 
+	//cout << "priority: " << sc->GetPriority() << " rc decr: " << sc->GetRenderCostDecrease() << " | ";
 	///////////////
 	//-- split was successful => update stats and queue
@@ -750,5 +751,5 @@
     // cost ratio of cost decrease / totalCost
 	const float costRatio = sc->GetRenderCostDecrease() / mHierarchyStats.mTotalCost;
-	//Debug << "ratio: " << costRatio << " min ratio: " << mTermMinGlobalCostRatio << endl;
+	//cout << "ratio: " << costRatio << " min ratio: " << mTermMinGlobalCostRatio << endl;
 	
 	if (costRatio < mTermMinGlobalCostRatio)
@@ -772,4 +773,6 @@
 	mHierarchyStats.mMemory += (float)ObjectPvs::GetEntrySizeByte() * pvsEntriesIncr;
 	mHierarchyStats.mRenderCostDecrease = sc->GetRenderCostDecrease();
+	
+	mPriority = sc->GetPriority();
 
 	static float memoryCount = 0;
@@ -1391,5 +1394,6 @@
 	{
 		SubdivisionCandidate *candidate = NextSubdivisionCandidate(splitQueue);
-		 // reevaluate local split plane and priority
+		
+		// reevaluate local split plane and priority
 		candidate->EvalCandidate(recomputeSplitPlane);
 		cout << ".";
@@ -1924,5 +1928,5 @@
 	subStats.mViewSpaceSplits = 0;
 	subStats.mObjectSpaceSplits = 0;
-	
+	subStats.mRenderCostDecrease = 0;
 	subStats.Print(stats);
 
