void main() { // 初始化节点栈 for(int i = 0; i < LEN; i++) NodeStack[i] = NULL; for(int i = 0; i < LEN; i++) { // 如果遇到的是数字 // 不能使用Expr[i] != '-' || Expr[i] != '+'来识别除了-+以外的字符 // 因为当字符为 '-' 时,前一条规则不满足但后一条满足,所以整个表达式还是返回true // '+'同上 if(Expr[i] == '-' || Expr[i] == '+') { // 以下则是处理 + - 的情况 // 创建一个符号节点 PtrToNode newnode = CreateNewNode(Expr[i]); newnode->Right = PopStack(); newnode->Left = PopStack(); PushStack(newnode); } else // 将非运算符的的字符入栈 PushStack(CreateNewNode(Expr[i])); } // 输出结果 // 使用后序遍历,遍历二叉树 PrintBinaryTree(NodeStack[0]); }
void InsertHash(int data, int index, int pos, HashList* list) { if (list[pos]->next == NULL) { list[pos]->next = CreateNewNode(index, data); } else { HashNode node; node = GetTailNode(list[pos]->next); node->next = CreateNewNode(index, data); } }
int main() { int n,m; scanf("%d%d",&n,&m); CreateNewNode(); for (int i=0;i<m;i++) { int x;scanf("%d",&x); if (x==1) { int u,v,a,b;scanf("%d%d%d%d",&u,&v,&a,&b); InsertItem(1,1,n,u,v,b,a); } else if (x==2) { int u,v,a,b;scanf("%d%d%d%d",&u,&v,&a,&b); InsertTax(1,1,n,u,v,b,a); } else { int pos;scanf("%d",&pos); long long item=0,tax=0; bool has_item=false; FindMaxCost(1,1,n,pos,item,tax,has_item); if (has_item) printf("%lld\n",item+tax); else printf("NA\n"); } } return 0; }
void Insert(int value, struct BinaryNode* node) { if(!root) { root = CreateNewNode(value); return; } if(value > node->value) { if(node->greater) Insert(value, node->greater); else node->greater = CreateNewNode(value); } else if(value < node->value) { if(node->smaller) Insert(value, node->smaller); else node->smaller = CreateNewNode(value); } }
int InfixParserTree::InitializeParser() { InfixParserTreeNode* initial_tree_node = CreateNewNode(InfixParserTreeNode::IRREDUCIBLE_NODE, expression); if(initial_tree_node == NULL) return false; tree_node_list.push_back(initial_tree_node); return true; }
void InsertItem(int o,int L,int R,int u,int v,long long a,long long b) { if ((u<=L)&&(R<=v)) if (!Tree[o].has_item) { Tree[o].has_item=true; Tree[o].a1=a+(long long)(L-u)*b;Tree[o].b1=b; return; } else { long long new_max,old_max; new_max=(long long)(R-u)*b+a; old_max=(long long)(R-L)*Tree[o].b1+Tree[o].a1; if (Tree[o].a1>=a+(long long)(L-u)*b) { if (old_max>=new_max) return; int new_u=GetMaxPos(L,R,Tree[o].a1,Tree[o].b1,a+(long long)(L-u)*b,b,0)+1; a=(long long)(new_u-u)*b+a; u=new_u; } else if (old_max>=new_max) { v=GetMaxPos(L,R,Tree[o].a1,Tree[o].b1,a+(long long)(L-u)*b,b,1); } else { Tree[o].a1=a+(long long)(L-u)*b; Tree[o].b1=b; return; } } int mid=(L+R)/2; if (u<=mid) { if (Tree[o].left==0) Tree[o].left=CreateNewNode(); InsertItem(Tree[o].left,L,mid,u,v,a,b); } if (v>mid) { if (Tree[o].right==0) Tree[o].right=CreateNewNode(); InsertItem(Tree[o].right,mid+1,R,u,v,a,b); } }
mitk::ConnectomicsNetworkCreator::ImageLabelPairType mitk::ConnectomicsNetworkCreator::JustEndPointVerticesNoLabelTest( TractType::Pointer singleTract ) { ImageLabelPairType labelpair; {// Note: .fib image tracts are safed using index coordinates mitk::Point3D firstElementFiberCoord, lastElementFiberCoord; mitk::Point3D firstElementSegCoord, lastElementSegCoord; itk::Index<3> firstElementSegIndex, lastElementSegIndex; if( singleTract->front().Size() != 3 ) { MBI_ERROR << mitk::ConnectomicsConstantsManager::CONNECTOMICS_ERROR_INVALID_DIMENSION_NEED_3; } for( unsigned int index = 0; index < singleTract->front().Size(); index++ ) { firstElementFiberCoord.SetElement( index, singleTract->front().GetElement( index ) ); lastElementFiberCoord.SetElement( index, singleTract->back().GetElement( index ) ); } // convert from fiber index coordinates to segmentation index coordinates FiberToSegmentationCoords( firstElementFiberCoord, firstElementSegCoord ); FiberToSegmentationCoords( lastElementFiberCoord, lastElementSegCoord ); for( int index = 0; index < 3; index++ ) { firstElementSegIndex.SetElement( index, firstElementSegCoord.GetElement( index ) ); lastElementSegIndex.SetElement( index, lastElementSegCoord.GetElement( index ) ); } int firstLabel = 1 * firstElementSegIndex[ 0 ] + 1000 * firstElementSegIndex[ 1 ] + 1000000 * firstElementSegIndex[ 2 ]; int lastLabel = 1 * firstElementSegIndex[ 0 ] + 1000 * firstElementSegIndex[ 1 ] + 1000000 * firstElementSegIndex[ 2 ]; labelpair.first = firstLabel; labelpair.second = lastLabel; // Add property to property map CreateNewNode( firstLabel, firstElementSegIndex, m_UseCoMCoordinates ); CreateNewNode( lastLabel, lastElementSegIndex, m_UseCoMCoordinates ); } return labelpair; }
void InsertTax(int o,int L,int R,int u,int v,long long a,long long b) { if ((u<=L)&&(R<=v)) { Tree[o].a2+=a+(L-u)*b; Tree[o].b2+=b; return; } int mid=(L+R)/2; if (u<=mid) { if (Tree[o].left==0) Tree[o].left=CreateNewNode(); InsertTax(Tree[o].left,L,mid,u,v,a,b); } if (v>mid) { if (Tree[o].right==0) Tree[o].right=CreateNewNode(); InsertTax(Tree[o].right,mid+1,R,u,v,a,b); } }
HashList* InitHashTable(int tableSize) { int i = 0; HashList* list = (HashList*)malloc(sizeof(HashNode) * tableSize); if (list == NULL) { printf("malloc list error\n"); exit(0); } for (i = 0; i < tableSize; i++) { list[i] = CreateNewNode(0, 0); } return list; }
CHAR8 * CFormPkg::IfrBinBufferGet ( IN UINT32 Len ) { CHAR8 *BinBuffer = NULL; SBufferNode *Node = NULL; if ((Len == 0) || (Len > mBufferSize)) { return NULL; } if ((mCurrBufferNode->mBufferFree + Len) <= mCurrBufferNode->mBufferEnd) { BinBuffer = mCurrBufferNode->mBufferFree; mCurrBufferNode->mBufferFree += Len; } else { Node = CreateNewNode (); if (Node == NULL) { return NULL; } if (mBufferNodeQueueTail == NULL) { mBufferNodeQueueHead = mBufferNodeQueueTail = Node; } else { mBufferNodeQueueTail->mNext = Node; mBufferNodeQueueTail = Node; } mCurrBufferNode = Node; // // Now try again. // BinBuffer = mCurrBufferNode->mBufferFree; mCurrBufferNode->mBufferFree += Len; } mPkgLength += Len; return BinBuffer; }
void COctree::CreateNode(CVector3 *pVertices, int numberOfVerts, CVector3 vCenter, float width) { // This is our main function that creates the octree. We will recurse through // this function until we finish subdividing. Either this will be because we // subdivided too many levels or we divided all of the triangles up. // Create a variable to hold the number of triangles int numberOfTriangles = numberOfVerts / 3; // Initialize this node's center point. Now we know the center of this node. m_vCenter = vCenter; // Initialize this nodes cube width. Now we know the width of this current node. m_Width = width; // Add the current node to our debug rectangle list so we can visualize it. // We can now see this node visually as a cube when we render the rectangles. // Since it's a cube we pass in the width for width, height and depth. g_Debug.AddDebugRectangle(vCenter, width, width, width); // Check if we have too many triangles in this node and we haven't subdivided // above our max subdivisions. If so, then we need to break this node into // 8 more nodes (hence the word OCTree). Both must be true to divide this node. if( (numberOfTriangles > g_MaxTriangles) && (g_CurrentSubdivision < g_MaxSubdivisions) ) { // Since we need to subdivide more we set the divided flag to true. // This let's us know that this node does NOT have any vertices assigned to it, // but nodes that perhaps have vertices stored in them (Or their nodes, etc....) // We will querey this variable when we are drawing the octree. m_bSubDivided = true; // Create a list for each new node to store if a triangle should be stored in it's // triangle list. For each index it will be a true or false to tell us if that triangle // is in the cube of that node. Below we check every point to see where it's // position is from the center (I.E. if it's above the center, to the left and // back it's the TOP_LEFT_BACK node). Depending on the node we set the pList // index to true. This will tell us later which triangles go to which node. // You might catch that this way will produce doubles in some nodes. Some // triangles will intersect more than 1 node right? We won't split the triangles // in this tutorial just to keep it simple, but the next tutorial we will. // Create the list of booleans for each triangle index vector<bool> pList1(numberOfTriangles); // TOP_LEFT_FRONT node list vector<bool> pList2(numberOfTriangles); // TOP_LEFT_BACK node list vector<bool> pList3(numberOfTriangles); // TOP_RIGHT_BACK node list vector<bool> pList4(numberOfTriangles); // TOP_RIGHT_FRONT node list vector<bool> pList5(numberOfTriangles); // BOTTOM_LEFT_FRONT node list vector<bool> pList6(numberOfTriangles); // BOTTOM_LEFT_BACK node list vector<bool> pList7(numberOfTriangles); // BOTTOM_RIGHT_BACK node list vector<bool> pList8(numberOfTriangles); // BOTTOM_RIGHT_FRONT node list // Create this variable to cut down the thickness of the code below (easier to read) CVector3 vCtr = vCenter; // Go through all of the vertices and check which node they belong too. The way // we do this is use the center of our current node and check where the point // lies in relationship to the center. For instance, if the point is // above, left and back from the center point it's the TOP_LEFT_BACK node. // You'll see we divide by 3 because there are 3 points in a triangle. // If the vertex index 0 and 1 are in a node, 0 / 3 and 1 / 3 is 0 so it will // just set the 0'th index to TRUE twice, which doesn't hurt anything. When // we get to the 3rd vertex index of pVertices[] it will then be checking the // 1st index of the pList*[] array. We do this because we want a list of the // triangles in the node, not the vertices. for(int i = 0; i < numberOfVerts; i++) { // Create some variables to cut down the thickness of the code (easier to read) CVector3 vPoint = pVertices[i]; // Check if the point lines within the TOP LEFT FRONT node if( (vPoint.x <= vCtr.x) && (vPoint.y >= vCtr.y) && (vPoint.z >= vCtr.z) ) pList1[i / 3] = true; // Check if the point lines within the TOP LEFT BACK node if( (vPoint.x <= vCtr.x) && (vPoint.y >= vCtr.y) && (vPoint.z <= vCtr.z) ) pList2[i / 3] = true; // Check if the point lines within the TOP RIGHT BACK node if( (vPoint.x >= vCtr.x) && (vPoint.y >= vCtr.y) && (vPoint.z <= vCtr.z) ) pList3[i / 3] = true; // Check if the point lines within the TOP RIGHT FRONT node if( (vPoint.x >= vCtr.x) && (vPoint.y >= vCtr.y) && (vPoint.z >= vCtr.z) ) pList4[i / 3] = true; // Check if the point lines within the BOTTOM LEFT FRONT node if( (vPoint.x <= vCtr.x) && (vPoint.y <= vCtr.y) && (vPoint.z >= vCtr.z) ) pList5[i / 3] = true; // Check if the point lines within the BOTTOM LEFT BACK node if( (vPoint.x <= vCtr.x) && (vPoint.y <= vCtr.y) && (vPoint.z <= vCtr.z) ) pList6[i / 3] = true; // Check if the point lines within the BOTTOM RIGHT BACK node if( (vPoint.x >= vCtr.x) && (vPoint.y <= vCtr.y) && (vPoint.z <= vCtr.z) ) pList7[i / 3] = true; // Check if the point lines within the BOTTOM RIGHT FRONT node if( (vPoint.x >= vCtr.x) && (vPoint.y <= vCtr.y) && (vPoint.z >= vCtr.z) ) pList8[i / 3] = true; } // Here we create a variable for each list that holds how many triangles // were found for each of the 8 subdivided nodes. int triCount1 = 0; int triCount2 = 0; int triCount3 = 0; int triCount4 = 0; int triCount5 = 0; int triCount6 = 0; int triCount7 = 0; int triCount8 = 0; // Go through each of the lists and increase the triangle count for each node. for(int i = 0; i < numberOfTriangles; i++) { // Increase the triangle count for each node that has a "true" for the index i. if(pList1[i]) triCount1++; if(pList2[i]) triCount2++; if(pList3[i]) triCount3++; if(pList4[i]) triCount4++; if(pList5[i]) triCount5++; if(pList6[i]) triCount6++; if(pList7[i]) triCount7++; if(pList8[i]) triCount8++; } // Next we do the dirty work. We need to set up the new nodes with the triangles // that are assigned to each node, along with the new center point of the node. // Through recursion we subdivide this node into 8 more nodes. // Create the subdivided nodes if necessary and then recurse through them. // The information passed into CreateNewNode() are essential for creating the // new nodes. We pass the 8 ID's in so it knows how to calculate it's new center. CreateNewNode(pVertices, pList1, numberOfVerts, vCenter, width, triCount1, TOP_LEFT_FRONT); CreateNewNode(pVertices, pList2, numberOfVerts, vCenter, width, triCount2, TOP_LEFT_BACK); CreateNewNode(pVertices, pList3, numberOfVerts, vCenter, width, triCount3, TOP_RIGHT_BACK); CreateNewNode(pVertices, pList4, numberOfVerts, vCenter, width, triCount4, TOP_RIGHT_FRONT); CreateNewNode(pVertices, pList5, numberOfVerts, vCenter, width, triCount5, BOTTOM_LEFT_FRONT); CreateNewNode(pVertices, pList6, numberOfVerts, vCenter, width, triCount6, BOTTOM_LEFT_BACK); CreateNewNode(pVertices, pList7, numberOfVerts, vCenter, width, triCount7, BOTTOM_RIGHT_BACK); CreateNewNode(pVertices, pList8, numberOfVerts, vCenter, width, triCount8, BOTTOM_RIGHT_FRONT); } else { // If we get here we must either be subdivided past our max level, or our triangle // count went below the minimum amount of triangles so we need to store them. // Assign the vertices to this node since we reached the end node. // This will be the end node that actually gets called to be drawn. // We just pass in the vertices and vertex count to be assigned to this node. AssignVerticesToNode(pVertices, numberOfVerts); } }
EFI_VFR_RETURN_CODE CFormPkg::AdjustDynamicInsertOpcode ( IN CHAR8 *LastFormEndAddr, IN CHAR8 *InsertOpcodeAddr ) { SBufferNode *LastFormEndNode; SBufferNode *InsertOpcodeNode; SBufferNode *NewRestoreNodeBegin; SBufferNode *NewRestoreNodeEnd; SBufferNode *NewLastEndNode; SBufferNode *TmpNode; UINT32 NeedRestoreCodeLen; NewRestoreNodeEnd = NULL; LastFormEndNode = GetBinBufferNodeForAddr(LastFormEndAddr); InsertOpcodeNode = GetBinBufferNodeForAddr(InsertOpcodeAddr); if (LastFormEndNode == InsertOpcodeNode) { // // Create New Node to save the restore opcode. // NeedRestoreCodeLen = InsertOpcodeAddr - LastFormEndAddr; gAdjustOpcodeLen = NeedRestoreCodeLen; NewRestoreNodeBegin = CreateNewNode (); if (NewRestoreNodeBegin == NULL) { return VFR_RETURN_OUT_FOR_RESOURCES; } memcpy (NewRestoreNodeBegin->mBufferFree, LastFormEndAddr, NeedRestoreCodeLen); NewRestoreNodeBegin->mBufferFree += NeedRestoreCodeLen; // // Override the restore buffer data. // memmove (LastFormEndAddr, InsertOpcodeAddr, InsertOpcodeNode->mBufferFree - InsertOpcodeAddr); InsertOpcodeNode->mBufferFree -= NeedRestoreCodeLen; memset (InsertOpcodeNode->mBufferFree, 0, NeedRestoreCodeLen); } else { // // Create New Node to save the restore opcode. // NeedRestoreCodeLen = LastFormEndNode->mBufferFree - LastFormEndAddr; gAdjustOpcodeLen = NeedRestoreCodeLen; NewRestoreNodeBegin = CreateNewNode (); if (NewRestoreNodeBegin == NULL) { return VFR_RETURN_OUT_FOR_RESOURCES; } memcpy (NewRestoreNodeBegin->mBufferFree, LastFormEndAddr, NeedRestoreCodeLen); NewRestoreNodeBegin->mBufferFree += NeedRestoreCodeLen; // // Override the restore buffer data. // LastFormEndNode->mBufferFree -= NeedRestoreCodeLen; // // Link the restore data to new node. // NewRestoreNodeBegin->mNext = LastFormEndNode->mNext; // // Count the Adjust opcode len. // TmpNode = LastFormEndNode->mNext; while (TmpNode != InsertOpcodeNode) { gAdjustOpcodeLen += TmpNode->mBufferFree - TmpNode->mBufferStart; TmpNode = TmpNode->mNext; } // // Create New Node to save the last node of restore opcode. // NeedRestoreCodeLen = InsertOpcodeAddr - InsertOpcodeNode->mBufferStart; gAdjustOpcodeLen += NeedRestoreCodeLen; if (NeedRestoreCodeLen > 0) { NewRestoreNodeEnd = CreateNewNode (); if (NewRestoreNodeEnd == NULL) { return VFR_RETURN_OUT_FOR_RESOURCES; } memcpy (NewRestoreNodeEnd->mBufferFree, InsertOpcodeNode->mBufferStart, NeedRestoreCodeLen); NewRestoreNodeEnd->mBufferFree += NeedRestoreCodeLen; // // Override the restore buffer data. // memmove (InsertOpcodeNode->mBufferStart, InsertOpcodeAddr, InsertOpcodeNode->mBufferFree - InsertOpcodeAddr); InsertOpcodeNode->mBufferFree -= InsertOpcodeAddr - InsertOpcodeNode->mBufferStart; // // Insert the last restore data node. // TmpNode = GetNodeBefore (InsertOpcodeNode); if (TmpNode == LastFormEndNode) { NewRestoreNodeBegin->mNext = NewRestoreNodeEnd; } else { TmpNode->mNext = NewRestoreNodeEnd; } // // Connect the dynamic opcode node to the node before last form end node. // LastFormEndNode->mNext = InsertOpcodeNode; } } if (mBufferNodeQueueTail->mBufferFree - mBufferNodeQueueTail->mBufferStart > 2) { // // End form set opcode all in the mBufferNodeQueueTail node. // NewLastEndNode = CreateNewNode (); if (NewLastEndNode == NULL) { return VFR_RETURN_OUT_FOR_RESOURCES; } NewLastEndNode->mBufferStart[0] = 0x29; NewLastEndNode->mBufferStart[1] = 0x02; NewLastEndNode->mBufferFree += 2; mBufferNodeQueueTail->mBufferFree -= 2; mBufferNodeQueueTail->mNext = NewRestoreNodeBegin; if (NewRestoreNodeEnd != NULL) { NewRestoreNodeEnd->mNext = NewLastEndNode; } else { NewRestoreNodeBegin->mNext = NewLastEndNode; } mBufferNodeQueueTail = NewLastEndNode; } else if (mBufferNodeQueueTail->mBufferFree - mBufferNodeQueueTail->mBufferStart == 2) { TmpNode = GetNodeBefore(mBufferNodeQueueTail); TmpNode->mNext = NewRestoreNodeBegin; if (NewRestoreNodeEnd != NULL) { NewRestoreNodeEnd->mNext = mBufferNodeQueueTail; } else { NewRestoreNodeBegin->mNext = mBufferNodeQueueTail; } } return VFR_RETURN_SUCCESS; }
int InfixParserTree::ParseFunction(InfixParserTreeNode* tree_node, int& expression_parseable) { string cur_expression = tree_node->GetExpression(); int found_matching_function = false; string matching_function_identifier = ""; InfixFunctionNode* matching_function_instance = NULL; for(size_t i=0; i<function_manager->GetFunctionInstanceCount(); i++) { InfixFunctionNode* function_instance = function_manager->GetFunctionInstance(i); for(size_t j=0; j<function_instance->GetFunctionIdentifierCount(); j++) { string function_identifier = function_instance->GetFunctionIdentifier(j); string cur_expression_substr = cur_expression.substr(0, function_identifier.length()); if(cur_expression_substr == function_identifier) { found_matching_function = true; matching_function_identifier = function_identifier; matching_function_instance = function_instance; break; } } if(found_matching_function) break; } if(found_matching_function == false) return true; //Check for open/close parenthesis int identifier_length = matching_function_identifier.length(); if(cur_expression.length() < identifier_length + 3) return true; int expr_length = cur_expression.length(); int found_open_parenthesis = (cur_expression[identifier_length] == '(' || cur_expression[identifier_length] == '['); int found_close_parenthesis = (cur_expression[expr_length-1] == ')' || cur_expression[expr_length-1] == ']'); if(!found_open_parenthesis || !found_close_parenthesis) return true; //Find delimiters vector<size_t> delimiter_location_list; delimiter_location_list.push_back(identifier_length); int scope_level = 0; for(size_t i=identifier_length+1; i<cur_expression.length()-1; i++) { if(cur_expression[i] == '(' || cur_expression[i] == '[') scope_level++; if(cur_expression[i] == ')' || cur_expression[i] == ']') scope_level--; if(scope_level == 0 && cur_expression[i] == ',') delimiter_location_list.push_back(i); } delimiter_location_list.push_back(cur_expression.length()-1); if(delimiter_location_list.size() != matching_function_instance->GetInputCount()+1) return true; //for(size_t i=0; i<delimiter_location_list.size()-1; i++) // printf("%s\n", cur_expression.substr(delimiter_location_list[i]+1, delimiter_location_list[i+1] - (delimiter_location_list[i]+1)).c_str()); //printf("\n"); //Create a new function node InfixParserTreeNode* function_node = (InfixParserTreeNode*) matching_function_instance->CreateNewInstance(); if(function_node == NULL) return false; function_node->SetExpression(cur_expression); function_node->SetLogFileManager(logfile_manager); //Create children nodes for(size_t i=0; i<delimiter_location_list.size()-1; i++) { int substr_length = delimiter_location_list[i+1] - (delimiter_location_list[i]+1); string substr = cur_expression.substr(delimiter_location_list[i]+1, substr_length); InfixParserTreeNode* child_node = CreateNewNode(InfixParserTreeNode::IRREDUCIBLE_NODE, substr); if(child_node == NULL) return false; function_node->AppendChildNode(child_node); } //Replace old tree node with new operator node size_t parent_child_index = tree_node->GetParentChildIndex(); InfixParserTreeNode* parent_node = tree_node->GetParentNode(); if(parent_node != NULL) parent_node->SetChildNode(parent_child_index, function_node); //Append all new nodes to tree node list tree_node_list.push_back(function_node); for(size_t i=0; i<function_node->GetChildCount(); i++) tree_node_list.push_back(function_node->GetChild(i)); expression_parseable = true; return true; }
int InfixParserTree::ParseParenthesis(InfixParserTreeNode* tree_node, int& expression_parseable) { string cur_expression = tree_node->GetExpression(); if(cur_expression.length() <= 2) return true; int len = cur_expression.length(); int found_open_parenthesis = (cur_expression[0] == '(' || cur_expression[0] == '['); int found_close_parenthesis = false; //(cur_expression[len-1] == ')' || cur_expression[len-1] == ']'); int scope_level = 1; for(int i=1; i<cur_expression.length(); i++) { if(cur_expression[i] == '(' || cur_expression[i] == '[') scope_level++; else if(cur_expression[i] == ')' || cur_expression[i] == ']') scope_level--; if(scope_level == 0) { if(i == cur_expression.length() - 1) found_close_parenthesis = true; break; } } if(!found_open_parenthesis || !found_close_parenthesis) return true; //Create a new operator node InfixParserTreeNode* parenthesis_node = CreateNewNode(InfixParserTreeNode::OPERATOR_NODE, cur_expression); if(parenthesis_node == NULL) return false; ((InfixOperatorNode*) parenthesis_node)->SetOperatorType(InfixOperatorNode::PARENTHESIS_OPERATOR); //Create children node InfixParserTreeNode* child_node = CreateNewNode(InfixParserTreeNode::IRREDUCIBLE_NODE, cur_expression.substr(1, len-2)); if(child_node == NULL) return false; //Append children to operator node parenthesis_node->AppendChildNode(child_node); //Replace old tree node with new operator node size_t parent_child_index = tree_node->GetParentChildIndex(); InfixParserTreeNode* parent_node = tree_node->GetParentNode(); if(parent_node != NULL) parent_node->SetChildNode(parent_child_index, parenthesis_node); //Append all new nodes to tree node list tree_node_list.push_back(parenthesis_node); tree_node_list.push_back(child_node); expression_parseable = true; return true; }
int InfixParserTree::ParsePower(InfixParserTreeNode* tree_node, int& expression_parseable) { string cur_expression = tree_node->GetExpression(); int operator_index = -1; int scope_level = 0; for(int i=0; i<cur_expression.length() && i<INFIX_PARSER_MAX_EXPR_LENGTH; i++) { if(cur_expression[i] == '(' || cur_expression[i] == '[') scope_level++; if(cur_expression[i] == ')' || cur_expression[i] == ']') scope_level--; if(scope_level == 0 && cur_expression[i] == '^') { operator_index = i; break; } } if(operator_index == -1) return true; //Create a new operator node InfixParserTreeNode* power_node = CreateNewNode(InfixParserTreeNode::OPERATOR_NODE, cur_expression); if(power_node == NULL) return false; ((InfixOperatorNode*) power_node)->SetOperatorType(InfixOperatorNode::POWER_OPERATOR); //Create children nodes string left_expression = cur_expression.substr(0, operator_index); string right_expression = cur_expression.substr(operator_index+1, cur_expression.length() - (operator_index + 1)); //printf("left_expression = %s\n", left_expression.c_str()); //printf("right_expression = %s\n", right_expression.c_str()); InfixParserTreeNode* left_child = CreateNewNode(InfixParserTreeNode::IRREDUCIBLE_NODE, left_expression); InfixParserTreeNode* right_child = CreateNewNode(InfixParserTreeNode::IRREDUCIBLE_NODE, right_expression); if(left_child == NULL || right_child == NULL) return false; //Append children to operator node power_node->AppendChildNode(left_child); power_node->AppendChildNode(right_child); //Replace old tree node with new operator node size_t parent_child_index = tree_node->GetParentChildIndex(); InfixParserTreeNode* parent_node = tree_node->GetParentNode(); if(parent_node != NULL) parent_node->SetChildNode(parent_child_index, power_node); //Append all new nodes to tree node list tree_node_list.push_back(power_node); tree_node_list.push_back(left_child); tree_node_list.push_back(right_child); expression_parseable = true; return true; }
mitk::ConnectomicsNetworkCreator::ImageLabelPairType mitk::ConnectomicsNetworkCreator::EndElementPositionLabelAvoidingWhiteMatter( TractType::Pointer singleTract ) { ImageLabelPairType labelpair; {// Note: .fib image tracts are safed using index coordinates mitk::Point3D firstElementFiberCoord, lastElementFiberCoord; mitk::Point3D firstElementSegCoord, lastElementSegCoord; itk::Index<3> firstElementSegIndex, lastElementSegIndex; if( singleTract->front().Size() != 3 ) { MBI_ERROR << mitk::ConnectomicsConstantsManager::CONNECTOMICS_ERROR_INVALID_DIMENSION_NEED_3; } for( unsigned int index = 0; index < singleTract->front().Size(); index++ ) { firstElementFiberCoord.SetElement( index, singleTract->front().GetElement( index ) ); lastElementFiberCoord.SetElement( index, singleTract->back().GetElement( index ) ); } // convert from fiber index coordinates to segmentation index coordinates FiberToSegmentationCoords( firstElementFiberCoord, firstElementSegCoord ); FiberToSegmentationCoords( lastElementFiberCoord, lastElementSegCoord ); for( int index = 0; index < 3; index++ ) { firstElementSegIndex.SetElement( index, firstElementSegCoord.GetElement( index ) ); lastElementSegIndex.SetElement( index, lastElementSegCoord.GetElement( index ) ); } int firstLabel = m_SegmentationItk->GetPixel(firstElementSegIndex); int lastLabel = m_SegmentationItk->GetPixel(lastElementSegIndex ); // Check whether the labels belong to the white matter (which means, that the fibers ended early) bool extendFront(false), extendEnd(false), retractFront(false), retractEnd(false); extendFront = !IsNonWhiteMatterLabel( firstLabel ); extendEnd = !IsNonWhiteMatterLabel( lastLabel ); retractFront = IsBackgroundLabel( firstLabel ); retractEnd = IsBackgroundLabel( lastLabel ); //if( extendFront || extendEnd ) //{ //MBI_INFO << "Before Start: " << firstLabel << " at " << firstElementSegIndex[ 0 ] << " " << firstElementSegIndex[ 1 ] << " " << firstElementSegIndex[ 2 ] << " End: " << lastLabel << " at " << lastElementSegIndex[ 0 ] << " " << lastElementSegIndex[ 1 ] << " " << lastElementSegIndex[ 2 ]; //} if ( extendFront ) { std::vector< int > indexVectorOfPointsToUse; //Use first two points for direction indexVectorOfPointsToUse.push_back( 1 ); indexVectorOfPointsToUse.push_back( 0 ); // label and coordinate temp storage int tempLabel( firstLabel ); itk::Index<3> tempIndex = firstElementSegIndex; LinearExtensionUntilGreyMatter( indexVectorOfPointsToUse, singleTract, tempLabel, tempIndex ); firstLabel = tempLabel; firstElementSegIndex = tempIndex; } if ( extendEnd ) { std::vector< int > indexVectorOfPointsToUse; //Use last two points for direction indexVectorOfPointsToUse.push_back( singleTract->Size() - 2 ); indexVectorOfPointsToUse.push_back( singleTract->Size() - 1 ); // label and coordinate temp storage int tempLabel( lastLabel ); itk::Index<3> tempIndex = lastElementSegIndex; LinearExtensionUntilGreyMatter( indexVectorOfPointsToUse, singleTract, tempLabel, tempIndex ); lastLabel = tempLabel; lastElementSegIndex = tempIndex; } if ( retractFront ) { // label and coordinate temp storage int tempLabel( firstLabel ); itk::Index<3> tempIndex = firstElementSegIndex; RetractionUntilBrainMatter( true, singleTract, tempLabel, tempIndex ); firstLabel = tempLabel; firstElementSegIndex = tempIndex; } if ( retractEnd ) { // label and coordinate temp storage int tempLabel( lastLabel ); itk::Index<3> tempIndex = lastElementSegIndex; RetractionUntilBrainMatter( false, singleTract, tempLabel, tempIndex ); lastLabel = tempLabel; lastElementSegIndex = tempIndex; } //if( extendFront || extendEnd ) //{ // MBI_INFO << "After Start: " << firstLabel << " at " << firstElementSegIndex[ 0 ] << " " << firstElementSegIndex[ 1 ] << " " << firstElementSegIndex[ 2 ] << " End: " << lastLabel << " at " << lastElementSegIndex[ 0 ] << " " << lastElementSegIndex[ 1 ] << " " << lastElementSegIndex[ 2 ]; //} labelpair.first = firstLabel; labelpair.second = lastLabel; // Add property to property map CreateNewNode( firstLabel, firstElementSegIndex, m_UseCoMCoordinates ); CreateNewNode( lastLabel, lastElementSegIndex, m_UseCoMCoordinates ); } return labelpair; }
void COctree::CreateNode(t3DModel *pWorld, int numberOfTriangles, CVector3 vCenter, float width) { // Initialize this node's center point. Now we know the center of this node. m_vCenter = vCenter; // Initialize this nodes cube width. Now we know the width of this current node. m_Width = width; // Add the current node to our debug rectangle list so we can visualize it. g_Debug.AddDebugRectangle(vCenter, width, width, width); // Check if we have too many triangles in this node and we haven't subdivided // above our max subdivisions. If so, then we need to break this node into // 8 more nodes (hence the word OCTree). Both must be true to divide this node. if( (numberOfTriangles > g_MaxTriangles) && (g_CurrentSubdivision < g_MaxSubdivisions) ) { // Since we need to subdivide more we set the divided flag to true. m_bSubDivided = true; /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // This function pretty much stays the same, except a small twist because // we are dealing with multiple objects for the scene, not just an array of vertices. // In the previous tutorials, we used a vector<> of booleans, but now we use our // tFaceList to store a vector of booleans for each object. // Create the list of tFaceLists for each child node vector<tFaceList> pList1(pWorld->numOfObjects); // TOP_LEFT_FRONT node list vector<tFaceList> pList2(pWorld->numOfObjects); // TOP_LEFT_BACK node list vector<tFaceList> pList3(pWorld->numOfObjects); // TOP_RIGHT_BACK node list vector<tFaceList> pList4(pWorld->numOfObjects); // TOP_RIGHT_FRONT node list vector<tFaceList> pList5(pWorld->numOfObjects); // BOTTOM_LEFT_FRONT node list vector<tFaceList> pList6(pWorld->numOfObjects); // BOTTOM_LEFT_BACK node list vector<tFaceList> pList7(pWorld->numOfObjects); // BOTTOM_RIGHT_BACK node list vector<tFaceList> pList8(pWorld->numOfObjects); // BOTTOM_RIGHT_FRONT node list // Create this variable to cut down the thickness of the code below (easier to read) CVector3 vCtr = vCenter; // Go through every object in the current partition of the world for(int i = 0; i < pWorld->numOfObjects; i++) { // Store a point to the current object t3DObject *pObject = &(pWorld->pObject[i]); // Now, we have a face list for each object, for every child node. // We need to then check every triangle in this current object // to see if it's in any of the child nodes dimensions. We store a "true" in // the face list index to tell us if that's the case. This is then used // in CreateNewNode() to create a new partition of the world for that child node. // Resize the current face list to be the size of this object's face count pList1[i].pFaceList.resize(pObject->numOfFaces); pList2[i].pFaceList.resize(pObject->numOfFaces); pList3[i].pFaceList.resize(pObject->numOfFaces); pList4[i].pFaceList.resize(pObject->numOfFaces); pList5[i].pFaceList.resize(pObject->numOfFaces); pList6[i].pFaceList.resize(pObject->numOfFaces); pList7[i].pFaceList.resize(pObject->numOfFaces); pList8[i].pFaceList.resize(pObject->numOfFaces); // Go through all the triangles for this object for(int j = 0; j < pObject->numOfFaces; j++) { // Check every vertice in the current triangle to see if it's inside a child node for(int whichVertex = 0; whichVertex < 3; whichVertex++) { // Store the current vertex to be checked against all the child nodes CVector3 vPoint = pObject->pVerts[pObject->pFaces[j].vertIndex[whichVertex]]; // Check if the point lies within the TOP LEFT FRONT node if( (vPoint.x <= vCtr.x) && (vPoint.y >= vCtr.y) && (vPoint.z >= vCtr.z) ) pList1[i].pFaceList[j] = true; // Check if the point lies within the TOP LEFT BACK node if( (vPoint.x <= vCtr.x) && (vPoint.y >= vCtr.y) && (vPoint.z <= vCtr.z) ) pList2[i].pFaceList[j] = true; // Check if the point lies within the TOP RIGHT BACK node if( (vPoint.x >= vCtr.x) && (vPoint.y >= vCtr.y) && (vPoint.z <= vCtr.z) ) pList3[i].pFaceList[j] = true; // Check if the point lies within the TOP RIGHT FRONT node if( (vPoint.x >= vCtr.x) && (vPoint.y >= vCtr.y) && (vPoint.z >= vCtr.z) ) pList4[i].pFaceList[j] = true; // Check if the point lies within the BOTTOM LEFT FRONT node if( (vPoint.x <= vCtr.x) && (vPoint.y <= vCtr.y) && (vPoint.z >= vCtr.z) ) pList5[i].pFaceList[j] = true; // Check if the point lies within the BOTTOM LEFT BACK node if( (vPoint.x <= vCtr.x) && (vPoint.y <= vCtr.y) && (vPoint.z <= vCtr.z) ) pList6[i].pFaceList[j] = true; // Check if the point lies within the BOTTOM RIGHT BACK node if( (vPoint.x >= vCtr.x) && (vPoint.y <= vCtr.y) && (vPoint.z <= vCtr.z) ) pList7[i].pFaceList[j] = true; // Check if the point lines within the BOTTOM RIGHT FRONT node if( (vPoint.x >= vCtr.x) && (vPoint.y <= vCtr.y) && (vPoint.z >= vCtr.z) ) pList8[i].pFaceList[j] = true; } } // Here we initialize the face count for each list that holds how many triangles // were found for each of the 8 subdivided nodes. pList1[i].totalFaceCount = 0; pList2[i].totalFaceCount = 0; pList3[i].totalFaceCount = 0; pList4[i].totalFaceCount = 0; pList5[i].totalFaceCount = 0; pList6[i].totalFaceCount = 0; pList7[i].totalFaceCount = 0; pList8[i].totalFaceCount = 0; } // Here we create a variable for each list that holds how many triangles // were found for each of the 8 subdivided nodes. int triCount1 = 0; int triCount2 = 0; int triCount3 = 0; int triCount4 = 0; int triCount5 = 0; int triCount6 = 0; int triCount7 = 0; int triCount8 = 0; // Go through all of the objects of this current partition for(i = 0; i < pWorld->numOfObjects; i++) { // Go through all of the current objects triangles for(int j = 0; j < pWorld->pObject[i].numOfFaces; j++) { // Increase the triangle count for each node that has a "true" for the index i. // In other words, if the current triangle is in a child node, add 1 to the count. // We need to store the total triangle count for each object, but also // the total for the whole child node. That is why we increase 2 variables. if(pList1[i].pFaceList[j]) { pList1[i].totalFaceCount++; triCount1++; } if(pList2[i].pFaceList[j]) { pList2[i].totalFaceCount++; triCount2++; } if(pList3[i].pFaceList[j]) { pList3[i].totalFaceCount++; triCount3++; } if(pList4[i].pFaceList[j]) { pList4[i].totalFaceCount++; triCount4++; } if(pList5[i].pFaceList[j]) { pList5[i].totalFaceCount++; triCount5++; } if(pList6[i].pFaceList[j]) { pList6[i].totalFaceCount++; triCount6++; } if(pList7[i].pFaceList[j]) { pList7[i].totalFaceCount++; triCount7++; } if(pList8[i].pFaceList[j]) { pList8[i].totalFaceCount++; triCount8++; } } } // Next we do the dirty work. We need to set up the new nodes with the triangles // that are assigned to each node, along with the new center point of the node. // Through recursion we subdivide this node into 8 more potential nodes. // Create the subdivided nodes if necessary and then recurse through them. // The information passed into CreateNewNode() are essential for creating the // new nodes. We pass the 8 ID's in so it knows how to calculate it's new center. CreateNewNode(pWorld, pList1, triCount1, vCenter, width, TOP_LEFT_FRONT); CreateNewNode(pWorld, pList2, triCount2, vCenter, width, TOP_LEFT_BACK); CreateNewNode(pWorld, pList3, triCount3, vCenter, width, TOP_RIGHT_BACK); CreateNewNode(pWorld, pList4, triCount4, vCenter, width, TOP_RIGHT_FRONT); CreateNewNode(pWorld, pList5, triCount5, vCenter, width, BOTTOM_LEFT_FRONT); CreateNewNode(pWorld, pList6, triCount6, vCenter, width, BOTTOM_LEFT_BACK); CreateNewNode(pWorld, pList7, triCount7, vCenter, width, BOTTOM_RIGHT_BACK); CreateNewNode(pWorld, pList8, triCount8, vCenter, width, BOTTOM_RIGHT_FRONT); } else { // If we get here we must either be subdivided past our max level, or our triangle // count went below the minimum amount of triangles so we need to store them. // We pass in the current partition of world data to be assigned to this end node AssignTrianglesToNode(pWorld, numberOfTriangles); } /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * }
//--------------------------------------------------------------------------------------- // funkce vytvori inicialni stromovou strukturu, ktera se pouzije pro // otestovani Vami napsaneho kodu Node* InitTree() { Node *root = CreateNewNode("IB002 Algorithms and data structures I", NULL), *algorithms = CreateNewNode("algorithms", root), *iterativeAlgorithms = CreateNewNode("Iterative algorithms", algorithms), *searchingIt = CreateNewNode("searching algorithms", iterativeAlgorithms), *recursiveAlgorithms = CreateNewNode("recursive algorithms", algorithms), *divideAndConquer = CreateNewNode("divide and conquer algorithms", recursiveAlgorithms), *searchingRe = CreateNewNode("searching algorithms", divideAndConquer), *randomizedAlgorithms = CreateNewNode("randomized algorithms", algorithms), *dataStructures = CreateNewNode("data structures", root), *dynamicDataStructures = CreateNewNode("dynamic data structures", dataStructures), *graphs = CreateNewNode("graphs", dynamicDataStructures), *trees = CreateNewNode("trees", dynamicDataStructures), *lists = CreateNewNode("linear data structures", dynamicDataStructures), *staticDataStructures = CreateNewNode("static data structures", dataStructures); CreateNewNode("select sort", searchingIt); CreateNewNode("insert sort", searchingIt); CreateNewNode("bubble sort", searchingIt); CreateNewNode("heap sort", searchingIt); CreateNewNode("fibonacci", iterativeAlgorithms); CreateNewNode("power", iterativeAlgorithms); CreateNewNode("merge sort", searchingRe); CreateNewNode("quick sort", searchingRe); CreateNewNode("monte carlo pi", randomizedAlgorithms); CreateNewNode("weighted graph", graphs); CreateNewNode("unweighted graph", graphs); CreateNewNode("directed graph", graphs); CreateNewNode("undirected graph", graphs); CreateNewNode("bipartited graph", graphs); CreateNewNode("binary search tree", trees); CreateNewNode("red black tree", trees); CreateNewNode("B tree", trees); CreateNewNode("B+ tree", trees); CreateNewNode("list", lists); CreateNewNode("queue", lists); CreateNewNode("stack", lists); CreateNewNode("n-tuple", staticDataStructures); CreateNewNode("array of static length", staticDataStructures); CreateNewNode("primitive data types", staticDataStructures); return root; }