Spaces:
Sleeping
Sleeping
| // Recursive function to traverse entities and count faces with materials | |
| void TraverseEntities(SUEntitiesRef entities, int &totalFaces, | |
| int &texturedFaces, int &indentation) { | |
| size_t num_faces = 0; | |
| SUEntitiesGetNumFaces(entities, &num_faces); | |
| totalFaces += num_faces; | |
| if (num_faces > 0) { | |
| std::vector<SUFaceRef> faces(num_faces); | |
| SUEntitiesGetFaces(entities, num_faces, &faces[0], &num_faces); | |
| for (size_t i = 0; i < num_faces; i++) { | |
| SUMaterialRef front_mat = SU_INVALID; | |
| SUMaterialRef back_mat = SU_INVALID; | |
| SUFaceGetFrontMaterial(faces[i], &front_mat); | |
| SUFaceGetBackMaterial(faces[i], &back_mat); | |
| if (SUIsValid(front_mat) || SUIsValid(back_mat)) { | |
| texturedFaces++; | |
| } | |
| } | |
| } | |
| size_t num_instances = 0; | |
| SUEntitiesGetNumInstances(entities, &num_instances); | |
| if (num_instances > 0) { | |
| std::vector<SUComponentInstanceRef> instances(num_instances); | |
| SUEntitiesGetInstances(entities, num_instances, &instances[0], | |
| &num_instances); | |
| for (size_t i = 0; i < num_instances; i++) { | |
| SUComponentDefinitionRef def = SU_INVALID; | |
| SUComponentInstanceGetDefinition(instances[i], &def); | |
| SUEntitiesRef sub_entities = SU_INVALID; | |
| SUComponentDefinitionGetEntities(def, &sub_entities); | |
| TraverseEntities(sub_entities, totalFaces, texturedFaces, indentation); | |
| } | |
| } | |
| size_t num_groups = 0; | |
| SUEntitiesGetNumGroups(entities, &num_groups); | |
| if (num_groups > 0) { | |
| std::vector<SUGroupRef> groups(num_groups); | |
| SUEntitiesGetGroups(entities, num_groups, &groups[0], &num_groups); | |
| for (size_t i = 0; i < num_groups; i++) { | |
| SUEntitiesRef sub_entities = SU_INVALID; | |
| SUGroupGetEntities(groups[i], &sub_entities); | |
| TraverseEntities(sub_entities, totalFaces, texturedFaces, indentation); | |
| } | |
| } | |
| } | |
| int main(int argc, char *argv[]) { | |
| if (argc < 2) { | |
| std::cerr << "Usage: " << argv[0] << " <skp_file>" << std::endl; | |
| return 1; | |
| } | |
| SUInitialize(); | |
| SUModelRef model = SU_INVALID; | |
| SUResult res = SUModelCreateFromFile(&model, argv[1]); | |
| if (res != SU_ERROR_NONE) { | |
| std::cerr << "Failed to load model: " << argv[1] << std::endl; | |
| SUTerminate(); | |
| return 1; | |
| } | |
| std::cout << "Successfully loaded: " << argv[1] << std::endl; | |
| // Check Materials | |
| size_t count = 0; | |
| SUModelGetNumMaterials(model, &count); | |
| std::cout << "Total Materials Definitions: " << count << std::endl; | |
| if (count > 0) { | |
| std::vector<SUMaterialRef> materials(count); | |
| SUModelGetMaterials(model, count, &materials[0], &count); | |
| for (size_t i = 0; i < count; i++) { | |
| SUMaterialRef mat = materials[i]; | |
| SUStringRef name = SU_INVALID; | |
| SUStringCreate(&name); | |
| SUMaterialGetName(mat, &name); | |
| size_t name_len = 0; | |
| SUStringGetUTF8Length(name, &name_len); | |
| std::vector<char> name_buf(name_len + 1); | |
| SUStringGetUTF8(name, name_len + 1, &name_buf[0], &name_len); | |
| std::cout << " Material " << i << ": " << &name_buf[0]; | |
| SUStringRelease(&name); | |
| SUTextureRef texture = SU_INVALID; | |
| SUMaterialGetTexture(mat, &texture); | |
| if (SUIsValid(texture)) { | |
| SUStringRef texName = SU_INVALID; | |
| SUStringCreate(&texName); | |
| SUTextureGetFileName(texture, &texName); | |
| size_t tex_len = 0; | |
| SUStringGetUTF8Length(texName, &tex_len); | |
| std::vector<char> tex_buf(tex_len + 1); | |
| SUStringGetUTF8(texName, tex_len + 1, &tex_buf[0], &tex_len); | |
| std::cout << " (Texture: " << &tex_buf[0] << ")"; | |
| SUStringRelease(&texName); | |
| size_t width = 0, height = 0; | |
| double s_scale = 0, t_scale = 0; | |
| SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale); | |
| std::cout << " [Dim: " << width << "x" << height << "]"; | |
| } else { | |
| std::cout << " (No Texture)"; | |
| } | |
| std::cout << std::endl; | |
| } | |
| } | |
| // Check Geometry Usage | |
| SUEntitiesRef entities = SU_INVALID; | |
| SUModelGetEntities(model, &entities); | |
| int totalFaces = 0; | |
| int texturedFaces = 0; | |
| int indentation = 0; | |
| TraverseEntities(entities, totalFaces, texturedFaces, indentation); | |
| std::cout << "Geometry Stats:" << std::endl; | |
| std::cout << " Total Faces: " << totalFaces << std::endl; | |
| std::cout << " Faces with Material assigned: " << texturedFaces << std::endl; | |
| SUModelRelease(&model); | |
| SUTerminate(); | |
| return 0; | |
| } | |