unsigned int RedBlackTree<T>::CalculateHeight(Node<T>* node) const { if (node != NULL) { // Calculate the left and right height of every node recursively, // and then take the largest one of them and return it. int leftHeight = 1 + CalculateHeight(node->left); int rightHeight = 1 + CalculateHeight(node->right); if (leftHeight > rightHeight) { return leftHeight; } else { return rightHeight; } } else { // Add 0 to the total height once we reach the NULL pointer. return 0; } }
unsigned int CAbstractTextEditorLayout::GetHeight () { if (height == 0) height = CalculateHeight (); return height; }
unsigned int RedBlackTree<T>::Height() const { int HeightOfTree = CalculateHeight(root); // Calls a helper method to calculate the height. if (HeightOfTree > 0) { // This will make sure that an empty tree or a tree with root node only // will have same height of 0. Otherwise, it will decrement the hight by 1 // to disregard the root's node from the height. --HeightOfTree; } return HeightOfTree; }
//////////////////// // Create event int CScrollbar::DoCreate() { CWidget::DoCreate(); if (iDirection == scrVertical) Resize(getX(), getY(), CalculateWidth(), getHeight()); else Resize(getX(), getY(), getWidth(), CalculateHeight()); return WID_PROCESSED; }
void xrSaveNodes(LPCSTR N, LPCSTR out_name) { Msg ("NS: %d, CNS: %d, ratio: %f%%",sizeof(vertex),sizeof(CLevelGraph::CVertex),100*float(sizeof(CLevelGraph::CVertex))/float(sizeof(vertex))); Msg ("Renumbering nodes..."); string_path fName; strconcat (sizeof(fName),fName,N,out_name); IWriter *fs = FS.w_open(fName); // Header Status ("Saving header..."); hdrNODES H; H.version = XRAI_CURRENT_VERSION; H.count = g_nodes.size(); H.size = g_params.fPatchSize; H.size_y = CalculateHeight(H.aabb); H.guid = generate_guid(); fs->w (&H,sizeof(H)); // fs->w_u32 (g_covers_palette.size()); // for (u32 j=0; j<g_covers_palette.size(); ++j) // fs->w (&g_covers_palette[j],sizeof(g_covers_palette[j])); // All nodes Status ("Saving nodes..."); for (u32 i=0; i<g_nodes.size(); ++i) { vertex &N = g_nodes[i]; NodeCompressed NC; Compress (NC,N,H); compressed_nodes.push_back(NC); } xr_vector<u32> sorted; xr_vector<u32> renumbering; CNodeRenumberer A(compressed_nodes,sorted,renumbering); for (u32 i=0; i<g_nodes.size(); ++i) { fs->w (&compressed_nodes[i],sizeof(NodeCompressed)); Progress (float(i)/float(g_nodes.size())); } // Stats u32 SizeTotal = fs->tell(); Msg ("%dK saved",SizeTotal/1024); FS.w_close (fs); }
void CXTPPopupItem::FitToContent() { if (m_pRichRender) { CWindowDC dc(NULL); CSize sz = m_pRichRender->GetTextExtent(&dc, m_rcItem.Width()); m_rcItem.right = m_rcItem.left + sz.cx; m_rcItem.bottom = m_rcItem.top + sz.cy; return; } if (m_pUIElement) { XTPMarkupSetDefaultFont(m_pControl->GetMarkupContext(), (HFONT)GetTextFont()->GetSafeHandle(), COLORREF_NULL); CSize sz = XTPMarkupMeasureElement(m_pUIElement, m_rcItem.Width()); m_rcItem.right = m_rcItem.left + sz.cx; m_rcItem.bottom = m_rcItem.top + sz.cy; return; } CalculateWidth(); CalculateHeight(); }
int RedBlackTree<T>::CalculateHeight(Node<T>* node) const { if (node == NULL) { return 0; } else if (node->left == NULL&&node->right == NULL) { return 1; } else if (node->left != NULL && node->right == NULL) { return 1 + CalculateHeight(node->left); } else if (node->left == NULL && node->right != NULL) { return 1 + CalculateHeight(node->right); } if (CalculateHeight(node->left) > CalculateHeight(node->right)) { return 1 + CalculateHeight(node->left); } else { return 1 + CalculateHeight(node->right); } }
S3DModel* CAssParser::Load(const std::string& modelFilePath) { LOG_S(LOG_SECTION_MODEL, "Loading model: %s", modelFilePath.c_str() ); const std::string modelPath = FileSystem::GetDirectory(modelFilePath); const std::string modelName = FileSystem::GetBasename(modelFilePath); //! LOAD METADATA //! Load the lua metafile. This contains properties unique to Spring models and must return a table std::string metaFileName = modelFilePath + ".lua"; if (!CFileHandler::FileExists(metaFileName, SPRING_VFS_ZIP)) { //! Try again without the model file extension metaFileName = modelPath + '/' + modelName + ".lua"; } LuaParser metaFileParser(metaFileName, SPRING_VFS_MOD_BASE, SPRING_VFS_ZIP); if (!CFileHandler::FileExists(metaFileName, SPRING_VFS_ZIP)) { LOG_S(LOG_SECTION_MODEL, "No meta-file '%s'. Using defaults.", metaFileName.c_str()); } else if (!metaFileParser.Execute()) { LOG_SL(LOG_SECTION_MODEL, L_ERROR, "'%s': %s. Using defaults.", metaFileName.c_str(), metaFileParser.GetErrorLog().c_str()); } //! Get the (root-level) model table const LuaTable& metaTable = metaFileParser.GetRoot(); if (metaTable.IsValid()) { LOG_S(LOG_SECTION_MODEL, "Found valid model metadata in '%s'", metaFileName.c_str()); } //! LOAD MODEL DATA //! Create a model importer instance Assimp::Importer importer; //! Create a logger for debugging model loading issues Assimp::DefaultLogger::create("",Assimp::Logger::VERBOSE); const unsigned int severity = Assimp::Logger::Debugging|Assimp::Logger::Info|Assimp::Logger::Err|Assimp::Logger::Warn; Assimp::DefaultLogger::get()->attachStream( new AssLogStream(), severity ); //! Give the importer an IO class that handles Spring's VFS importer.SetIOHandler( new AssVFSSystem() ); //! Speed-up processing by skipping things we don't need importer.SetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, aiComponent_CAMERAS|aiComponent_LIGHTS|aiComponent_TEXTURES|aiComponent_ANIMATIONS); #ifndef BITMAP_NO_OPENGL //! Optimize VBO-Mesh sizes/ranges GLint maxIndices = 1024; GLint maxVertices = 1024; glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); glGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &maxVertices); //FIXME returns not optimal data, at best compute it ourself! (pre-TL cache size!) importer.SetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT, maxVertices); importer.SetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT, maxIndices/3); #endif //! Read the model file to build a scene object LOG_S(LOG_SECTION_MODEL, "Importing model file: %s", modelFilePath.c_str() ); const aiScene* scene = importer.ReadFile( modelFilePath, ASS_POSTPROCESS_OPTIONS ); if (scene != NULL) { LOG_S(LOG_SECTION_MODEL, "Processing scene for model: %s (%d meshes / %d materials / %d textures)", modelFilePath.c_str(), scene->mNumMeshes, scene->mNumMaterials, scene->mNumTextures ); } else { LOG_SL(LOG_SECTION_MODEL, L_ERROR, "Model Import: %s", importer.GetErrorString()); } SAssModel* model = new SAssModel; model->name = modelFilePath; model->type = MODELTYPE_ASS; model->scene = scene; //model->meta = &metaTable; //! Gather per mesh info CalculatePerMeshMinMax(model); //! Assign textures //! The S3O texture handler uses two textures. //! The first contains diffuse color (RGB) and teamcolor (A) //! The second contains glow (R), reflectivity (G) and 1-bit Alpha (A). if (metaTable.KeyExists("tex1")) { model->tex1 = metaTable.GetString("tex1", "default.png"); } else { //! Search for a texture std::vector<std::string> files = CFileHandler::FindFiles("unittextures/", modelName + ".*"); for(std::vector<std::string>::iterator fi = files.begin(); fi != files.end(); ++fi) { model->tex1 = FileSystem::GetFilename(*fi); break; //! there can be only one! } } if (metaTable.KeyExists("tex2")) { model->tex2 = metaTable.GetString("tex2", ""); } else { //! Search for a texture std::vector<std::string> files = CFileHandler::FindFiles("unittextures/", modelName + "2.*"); for(std::vector<std::string>::iterator fi = files.begin(); fi != files.end(); ++fi) { model->tex2 = FileSystem::GetFilename(*fi); break; //! there can be only one! } } model->flipTexY = metaTable.GetBool("fliptextures", true); //! Flip texture upside down model->invertTexAlpha = metaTable.GetBool("invertteamcolor", true); //! Reverse teamcolor levels //! Load textures LOG_S(LOG_SECTION_MODEL, "Loading textures. Tex1: '%s' Tex2: '%s'", model->tex1.c_str(), model->tex2.c_str()); texturehandlerS3O->LoadS3OTexture(model); //! Load all pieces in the model LOG_S(LOG_SECTION_MODEL, "Loading pieces from root node '%s'", scene->mRootNode->mName.data); LoadPiece(model, scene->mRootNode, metaTable); //! Update piece hierarchy based on metadata BuildPieceHierarchy( model ); //! Simplified dimensions used for rough calculations model->radius = metaTable.GetFloat("radius", model->radius); model->height = metaTable.GetFloat("height", model->height); model->relMidPos = metaTable.GetFloat3("midpos", model->relMidPos); model->mins = metaTable.GetFloat3("mins", model->mins); model->maxs = metaTable.GetFloat3("maxs", model->maxs); //! Calculate model dimensions if not set if (!metaTable.KeyExists("mins") || !metaTable.KeyExists("maxs")) CalculateMinMax( model->rootPiece ); if (model->radius < 0.0001f) CalculateRadius( model ); if (model->height < 0.0001f) CalculateHeight( model ); //! Verbose logging of model properties LOG_SL(LOG_SECTION_MODEL, L_DEBUG, "model->name: %s", model->name.c_str()); LOG_SL(LOG_SECTION_MODEL, L_DEBUG, "model->numobjects: %d", model->numPieces); LOG_SL(LOG_SECTION_MODEL, L_DEBUG, "model->radius: %f", model->radius); LOG_SL(LOG_SECTION_MODEL, L_DEBUG, "model->height: %f", model->height); LOG_SL(LOG_SECTION_MODEL, L_DEBUG, "model->mins: (%f,%f,%f)", model->mins[0], model->mins[1], model->mins[2]); LOG_SL(LOG_SECTION_MODEL, L_DEBUG, "model->maxs: (%f,%f,%f)", model->maxs[0], model->maxs[1], model->maxs[2]); LOG_S(LOG_SECTION_MODEL, "Model %s Imported.", model->name.c_str()); return model; }
bool RedBlackTree<T>::Remove(T item) { Node<T>* x = NULL; Node<T>* y = NULL; Node<T>* z = getNodeFromTree(root, item); // The node to be removed (it's value // will be gone, and it is going to be replaced // by the predecessor's value if a predecessor exists // for this node, and the predecessor's node will be // deleted, or delete this node if no predecessor exists). if (z == NULL) { // If no such item was found in the tree, then return false. return false; } if (z->left == NULL || z->right == NULL) { // If z has at most one child. y = z; } else { // z has two children. y = Predecessor(z); } if (y != NULL && y->left != NULL) { // The two conditions below are to // find whether is y's only child // is left or right. x = y->left; } else { if (y != NULL) { x = y->right; } } bool xIsLeft = false; Node<T>* xParent = NULL; if (x != NULL && y != NULL) { // If x is not NULL, detach x from y. x->p = y->p; xParent = y->p; } if (y != NULL && y->p == NULL) { // Check if y is root (i.e. it has no parent). root = x; } else { // Attach x to y's parent. if (y == y->p->left) { // y is a left child. y->p->left = x; xIsLeft = true; } else { // y is a right child. y->p->right = x; xIsLeft = false; } } if (y != NULL && z != NULL && y != z) { // Check to see if y has been moved up. z->data = y->data; } if (y != NULL && y->is_black == true) { if (x == NULL) { if (xParent == NULL) { if (root != NULL && root->right != NULL && root->left != NULL && CalculateHeight(root->right) > CalculateHeight(root->left)) { x = root->right; xParent = root; xIsLeft = false; } else if (root != NULL && root->right != NULL && root->left != NULL && CalculateHeight(root->left) > CalculateHeight(root->right)) { x = root->left; xParent = root; xIsLeft = true; } else if (root != NULL && root->left == NULL) { x = root->right; xParent = root; xIsLeft = false; } else if (root != NULL && root->right == NULL) { x = root->left; xParent = root; xIsLeft = true; } } } RBDeleteFixUp(x, xParent, xIsLeft); } delete y; // It can be the original predecessor's node, since its // value has been moved up, or it can be z itself. --size; // Decrement the size counter. if (root != NULL && root->left != NULL && root->right == NULL) { if (root->left->left == NULL && root->left->right == NULL) { root->is_black = true; root->left->is_black = false; } } else if (root != NULL && root->right != NULL && root->left == NULL) { if (root->right->left == NULL && root->right->right == NULL) { root->is_black = true; root->right->is_black = false; } } return true; }
int RedBlackTree<T>::Height() const { //only want the int part eg. >2 log3 >1 use 1 return CalculateHeight(root); }