inline
				ValueType
				cell( const typename BaseType::PreparedWidget& widget,
					  int32 i, int32 j, int32 k,
					  ValueType pX, ValueType pY, ValueType pZ,
					  ValueType nX, ValueType nY, ValueType nZ ) const
				{
					uint32	s = morton( i, j, k ) + widget.seed;
					
					if( s == 0 )
						s = 1;

					PrngType	prng( s );
					
					ValueType	numberOfImpulsesPerCell = widget.impulseDensity * widget.kernelRadius * widget.kernelRadius / ValueType( 2.0 );
					uint32		numberOfImpulses = prng.poisson( numberOfImpulsesPerCell );
					ValueType	noise = ValueType( 0.0 );

					Projection< ValueType >	projection( nX, nY, nZ,
														ValueType( 1.0 ), ValueType( 0.0 ), ValueType( 0.0 ),
														pX, pY, pZ );

					for( uint32 i = 0; i < numberOfImpulses; ++i)
					{
						ValueType	xi = prng.uniformNormalized();
						ValueType	yi = prng.uniformNormalized();
						ValueType	zi = prng.uniformNormalized();
						ValueType	F0 = prng.uniformRange( widget.frequencyRangeStart, widget.frequencyRangeEnd );
						ValueType	omega0 = prng.uniformRange( widget.angularRangeStart, widget.angularRangeEnd );

						projection.project( xi, yi, zi );
						zi = ValueType( 1.0 ) - fabs( zi );

						if( ((xi * xi) + (yi * yi)) < ValueType( 1.0 ) &&
							zi >= ValueType( 0.0 ) )
						{
							noise += zi * gabor( widget.K, widget.a, F0, omega0, xi * widget.kernelRadius, yi * widget.kernelRadius ); // anisotropic
						}
					}

					return noise;
				}
Example #2
0
/*
 * The function that builds the quad-tree
 */
void build(const value_type* const x, const value_type* const y, const value_type* const mass, const int N, const int k, value_type* xsorted, value_type* ysorted, value_type* mass_sorted, Node* tree, int depth){

    // Allocate the index array and compute and store the morton indices in it.
    unsigned int *index = new unsigned int[N];
    double xmin, ymin, ext;
    extent(N, x, y, xmin, ymin, ext);

    morton(N, x, y, xmin, ymin, ext, index, depth);

    // Sort the indices and store the corresponding permutation in keys
    unsigned int *keys = new unsigned int[N];

    // Put the values in keys
    for (int j = 0; j < N; ++j) {
        keys[j] = j;
    }

    sort(N, index, keys);

    // Sort the remaining arrays with the same permutation
    reorder(N, keys, x, y, mass, xsorted, ysorted, mass_sorted);

    // Build the tree

    // Compute the center of mass and the total mass
    value_type xCom = 0, yCom = 0, total_mass = 0;
    for (int i = 0; i < N; ++i) {
        xCom += mass_sorted[i] * xsorted[i];
        yCom += mass_sorted[i] * ysorted[i];
        total_mass += mass_sorted[i];
    }
    xCom /= total_mass;
    yCom /= total_mass;
    
    // leave r initialized to zero.
    // this attribute will never be needed on the first level 
    
    // Allocate variable to contain the index of the next free spot in the nodes array
    int newNodeIndex = 1; // since the root node lies at index 0

    // Create the root node (initially a leaf node)
    tree[0] = Node {0,          // root is at level 0
                    0,          // morton index
                    -1,         // child_id
                    0,          // part_start
                    N-1,        // part_end
                    total_mass, // node mass
                    xCom,       // x center of mass
                    yCom,       // y center of mass
                    NAN,          // radius of node (not used)
                    NULL,       // real part of multipole expansion (not used)
                    NULL        // imaginary part of multipole expansion (not used)
    };

    // Subdivide as long as there are more than k particles in the cells
    if (N > k && depth > 0) {
        // There are more than k particles in the node and we haven't reached the maximum depth so split it in four
        // TODO use omp tasking to parallelize this.
        split(tree, tree, depth, index, xsorted, ysorted, mass_sorted, k, &newNodeIndex);
    }
    else {
        // There are less than or equal to k particles in the node
        // or we reached the maximum depth => the node is a leaf
        std::cout << "The root node was not split." << std::endl;
        std::cout << "Particles in root node: " << N << ". Stopping criterion particle number: " << k << std::endl;
        std::cout << "depth = " << depth << std::endl;
    }
    
    delete[] keys;
    delete[] index;
}
Example #3
0
File: klbvh.cpp Project: sunf71/BVH
void Bvh_Builder::build(const Bbox<Vector3f> bbox,PrimitiveIterator begin, PrimitiveIterator end, BVH& bvh)
{
	//std::vector<std::pair<uint32,Bbox3f>> map;
	std::vector<std::pair<uint32,uint32>> indexMap;
	morton_functor<uint32> morton(bbox);
	uint32 t=0;
	//assign morton code
	for( PrimitiveIterator itr = begin; itr!= end; itr++)
	{
		/*m_keys.push_back(morton(itr->center));
		m_leaveboxes.push_back(itr->bbox);*/
		uint32 key = morton(itr->center);
		/*map.push_back(std::pair<uint32,Bbox3f>(key,itr->bbox));*/
		indexMap.push_back(std::pair<uint32,uint32>(key,t++));
		bvh.leaf_Boxes.push_back(itr->bbox);
	}
	/*m_keys.push_back(1);
	m_keys.push_back(2);
	m_keys.push_back(4);
	m_keys.push_back(5);
	m_keys.push_back(19);
	m_keys.push_back(24);
	m_keys.push_back(25);
	m_keys.push_back(30);
	m_keys.push_back(35);
	m_keys.push_back(38);
	m_keys.push_back(42);
	m_keys.push_back(45);*/

	//sort
	//std::sort(m_keys.begin(),m_keys.end());
	/*std::sort(map.begin(),map.end(),pairCompare);*/
	std::sort(indexMap.begin(),indexMap.end());
	/*for( std::vector<std::pair<uint32,Bbox3f>>::const_iterator itr= map.begin(); itr!= map.end(); itr++)
	{
		m_keys.push_back(itr->first);
		bvh.leaf_Boxes.push_back(itr->second);
		
	}*/
	for( std::vector<std::pair<uint32,uint32>>::const_iterator itr= indexMap.begin(); itr!= indexMap.end(); itr++)
	{
		m_keys.push_back(itr->first);
		bvh.indices.push_back(itr->second);
		
	}

	/*std::cout<<"sorted keys"<<std::endl;
	for(int i=0; i<m_keys.size(); i++)
	{
		std::cout<<m_keys[i]<<" ";
		std::bitset<32> bitvec((int)m_keys[i]);
		std::cout<<bitvec<<std::endl;;
	}*/

	/*std::cout<<"sorted leaf boxes"<<std::endl;
	for(int i=0; i<bvh.leaf_Boxes.size(); i++)
		printBbox3f(bvh.leaf_Boxes[i]);*/

	m_size = end - begin;

	bvh.nodes.resize(m_size-1);
	bvh.leafs.resize(m_size);

	for(uint32 i=0; i<m_size-1; i++)
	{
		int d = sign(theta(i,i+1)-theta(i,i-1));
		int tMin = theta(i,i-d);
		uint32 lMax = 2;
		while(theta(i,i+lMax*d) > tMin)
			lMax *= 2;

		uint32 l = 0;
		for(uint32 t = lMax/2; t>=1; t/=2)
		{
			if (theta(i,i+(t+l)*d) > tMin)
				l += t;
		}
		uint32 j = i + l*d;
		uint32 tNode = theta(i,j);
		uint32 s = 0;
		for(uint32 t = ceil(l/2.0); t!=1; t=ceil(t/2.0))
		{
			if (theta(i,i+(s+t)*d) > tNode)
				s = s+t;
		}
		if (theta(i,i+(s+1)*d) > tNode)
				s = s+1;
		uint32 gama  = i + s*d + min(d,0);
		bvh.nodes[i].childIdx = gama;
		bvh.nodes[i].id = i;
		bvh.nodes[i].isLeaf = false;
		bvh.nodes[i].leafStart = min(i,j);
		bvh.nodes[i].leafEnd  = max(i,j);
		if(min(i,j) == gama) 
		{
			bvh.nodes[i].l_isleaf = true; 		
			bvh.leafs[gama].parentIdx = i;
			bvh.leafs[gama].isLeaf = true;
			bvh.leafs[gama].id = gama;
		}
		else
		{
			bvh.nodes[i].l_isleaf = false; 	
			bvh.nodes[gama].parentIdx = i;
		}
		if (max(i,j) == gama +1)
		{
			bvh.nodes[i].r_isleaf = true;	
			bvh.leafs[gama+1].parentIdx = i;
			bvh.leafs[gama+1].isLeaf = true;
			bvh.leafs[gama+1].id = gama+1;
		}
		else
		{
			bvh.nodes[i].r_isleaf = false;
			bvh.nodes[gama+1].parentIdx = i;
		}
	}

	assignAABBs(bvh);

	/*std::cout<<"node boxes"<<std::endl;
	for(int i =0; i< bvh.node_Boxes.size(); i++)
		printBbox3f(bvh.node_Boxes[i]);*/


}