UInt32 ParticleBSPTree::doBuild(std::vector<Int32>::iterator  begin, 
                                std::vector<Int32>::iterator  end,
                                     UInt32                   nodeindex,
                                     GeoVectorProperty       *pos)
{
    // reached a leaf?
    
    if(begin + 1 == end)
    {
        _tree[nodeindex].setValue(*begin);
        return nodeindex + 1;
    }
    
    // find the bounding volume of the group
    
    BoxVolume b;
    Pnt3f p;
    
    b.setEmpty();
    
    for(std::vector<Int32>::iterator i = begin; i != end; ++i)
    {
        pos->getValue(p,*i);     
        b.extendBy(p);
    }
    
    // find the axis with the longest extension
    
    Vec3f d = b.getMax() - b.getMin();
    
    UInt8 axis = ParticleBSPNode::X;
    Real32 maxval = d[0];
    
    if(d[1] > maxval)
    {
        axis = ParticleBSPNode::Y;
        maxval = d[1];
    }
    if(d[2] > maxval)
    {
        axis = ParticleBSPNode::Z;
        maxval = d[2];
    }

    // sort in that axis
    ParticleCompare comp(pos, axis);
    
    std::sort(begin,end,comp);
    
    // find median value
    std::vector<Int32>::iterator mid = begin + (end - begin) / 2;
    
    Pnt3f p2;
    pos->getValue(p ,*mid);
    pos->getValue(p2,(*mid)-1);
    _tree[nodeindex].setSplit(axis, (p[axis] + p2[axis]) / 2.f);
    
    return osgMax( doBuild(begin, mid, nodeindex * 2    , pos),
                   doBuild(  mid, end, nodeindex * 2 + 1, pos) );   
}   
Esempio n. 2
0
 void doBuild(SegmentTreeNode* node){
     if(node->start == node->end) return;
     SegmentTreeNode* left = new SegmentTreeNode(node->start, (node->start + node->end) / 2);
     SegmentTreeNode* right = new SegmentTreeNode((node->start + node->end) / 2 + 1, node->end);
     node->left = left;
     node->right = right;
     doBuild(left);
     doBuild(right);
     
 }
Esempio n. 3
0
 TreeNode *doBuild(vector<int> &inorder, int in_b, int in_e, vector<int> &postorder, int post_b, int post_e) {
     if (post_b >= post_e)
         return NULL;
     int mid = postorder[post_e - 1];
     TreeNode *root = new TreeNode(mid);
     for (int i = in_b; i < in_e; i++) {
         if (inorder[i] == mid) {
             root->left = doBuild(inorder, in_b, i, postorder, post_b, post_b + i - in_b);
             root->right = doBuild(inorder, i + 1, in_e, postorder, post_b + i - in_b, post_e - 1);
         }
     }
     return root;
 }
Esempio n. 4
0
 /**
  *@param start, end: Denote an segment / interval
  *@return: The root of Segment Tree
  */
 SegmentTreeNode * build(int start, int end) {
     // write your code here
     if(start > end) return nullptr;
     SegmentTreeNode* root = new SegmentTreeNode(start, end);
     doBuild(root);
     return root;
 }
Esempio n. 5
0
// build Tan(a)
DWPOINT* MathDraw::buildTan(DWPOINT* gPt,int ptCnt)
{
	// if got null array - return null 
	if (gPt==NULL) return NULL;

	return doBuild(gPt,NULL,ptCnt,&MathDraw::calcTan);
}
Esempio n. 6
0
void Constraint::build(double dt,
	MlcpPhysicsProblem* mlcp,
	size_t indexOfRepresentation0,
	size_t indexOfRepresentation1,
	size_t indexOfConstraint)
{
	doBuild(dt, *m_data.get(), mlcp, indexOfRepresentation0, indexOfRepresentation1, indexOfConstraint);
}
Esempio n. 7
0
// common build function for string data
DWPOINT* MathDraw::doBuild(char* str, int n, int ptCnt, char a, double (MathDraw::* func)(double, double))
{
	DWPOINT *pO1=NULL;
	DWPOINT *pO2=NULL;

	// get operands, if any
	if ( getOperands(str, n, &pO1, &pO2, a) ) return NULL;
	return doBuild(pO1,pO2,ptCnt,func);
}
void ParticleBSPTree::build(Particles *core)
{
    _tree.clear();
    
    if(core == NULL)
    {
        FWARNING(("ParticleBSP::build: no core!!\n"));
        return;
    }
    
    GeoVectorProperty * const pos = core->getPositions();
    
    if(pos == NULL)
        return;

    const MFInt32 *indices = core->getMFIndices();
        
    // 1. create list for particles
 
    std::vector<Int32> order;
    order.reserve(pos->size());
    
    for(UInt32 i = 0; i < pos->size32(); ++i )
    {     
        if(indices->size() == pos->size())
        {        
            order.push_back((*indices)[i]);
        }
        else
        {
            order.push_back(i);            
        }
    }
    
    // reserve mem for tree
    
    _tree.resize(osgNextPower2(order.size()) * 2);
    
    // 2. recursively build the tree
    
    UInt32 nnodes = doBuild(order.begin(), order.end(), 1, pos);
    
    // 3. remove the unneeded elements from the end
    
    if(nnodes < _tree.size())
        _tree.erase( _tree.begin() + nnodes, _tree.end());

    // done
}
Esempio n. 9
0
// build (a^b) - powering
DWPOINT* MathDraw::buildPow(char* str, int n, int ptCnt)
{
	return doBuild(str, n, ptCnt, '^', &MathDraw::calcPow);
}
Esempio n. 10
0
// build (a-b)
DWPOINT* MathDraw::buildMinus(char* str, int n, int ptCnt)
{
	return doBuild(str, n, ptCnt, '-', &MathDraw::calcMinus);
}
Esempio n. 11
0
// build (a+b)
DWPOINT* MathDraw::buildPlus(char* str, int n, int ptCnt)
{
	return doBuild(str, n, ptCnt, '+', &MathDraw::calcPlus);
}
Esempio n. 12
0
// build (a/b)
DWPOINT* MathDraw::buildDiv(char* str, int n, int ptCnt)
{
	return doBuild(str, n, ptCnt, '/', &MathDraw::calcDiv);
}
Esempio n. 13
0
// build (a*b)
DWPOINT* MathDraw::buildMltpl(char* str, int n, int ptCnt) //умножаем
{
	return doBuild(str, n, ptCnt, '*', &MathDraw::calcMltpl);
}
Esempio n. 14
0
// build func(X)
DWPOINT* MathDraw::buildX(int ptCnt) 
{
	return doBuild((double)alterStartPt_,ptCnt,&MathDraw::dltX);
}
Esempio n. 15
0
// build func(N)
DWPOINT* MathDraw::buildLine(double dg,int ptCnt) 
{
	return doBuild(dg,ptCnt,&MathDraw::calcLine);
}
Esempio n. 16
0
 TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     return doBuild(inorder, 0, inorder.size(), postorder, 0, postorder.size());
 }