/* Copyright (c) 2018 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include #include #include #include #include "3dmodelio/MeshData.h" #include "binaryserialization/BinarySerializationContext.h" #include "binaryserialization/BinarySerializationShared.h" #include "jsonserialization/JSONSerializationContext.h" #include "jsonserialization/JSONSerializationShared.h" #include "serialization/DeserializationTypeDetector.h" static const char * inFileName, * outFileName; static bool writeBinary, uniqVertices; static void printUsage() { fprintf(stderr, "Usage: meshutils -o outfile [--binary | --json] [--uniq] infile\n"); } static void parseArgs(int argc, char ** argv) { int argIndex; for (argIndex = 1; argIndex < argc; argIndex++) { if (!strcmp(argv[argIndex], "--help")) { printUsage(); exit(EXIT_SUCCESS); } else if (!strcmp(argv[argIndex], "-o")) { if (argIndex >= argc - 1) { fprintf(stderr, "Error: -o specified at end of argv\n"); printUsage(); exit(EXIT_FAILURE); } argIndex++; outFileName = argv[argIndex]; } else if (!strcmp(argv[argIndex], "--binary")) { writeBinary = true; } else if (!strcmp(argv[argIndex], "--json")) { writeBinary = false; } else if (!strcmp(argv[argIndex], "--uniq")) { uniqVertices = true; } else { if (inFileName != NULL) { fprintf(stderr, "Couldn't understand argument \"%s\" (infile already read as \"%s\")\n", argv[argIndex], inFileName); printUsage(); exit(EXIT_FAILURE); } inFileName = argv[argIndex]; } } } static void removeDuplicateVertices(MeshData * meshData) { float * vertices = meshData->vertices; uint32_t * indexes = meshData->indexes; unsigned int componentCount = meshData->vertexFormat->componentCountTotal; for (unsigned int vertexIndex = 0; vertexIndex < meshData->vertexCount; vertexIndex++) { for (unsigned int compareVertexIndex = vertexIndex + 1; compareVertexIndex < meshData->vertexCount; compareVertexIndex++) { bool match = true; for (unsigned int componentIndex = 0; componentIndex < componentCount; componentIndex++) { if (vertices[vertexIndex * componentCount + componentIndex] != vertices[compareVertexIndex * componentCount + componentIndex]) { match = false; break; } } if (match) { meshData->vertexCount--; for (unsigned int vertexIndex2 = compareVertexIndex; vertexIndex2 < meshData->vertexCount; vertexIndex2++) { vertices[vertexIndex2] = vertices[vertexIndex2 + 1]; } for (unsigned int indexIndex = 0; indexIndex < meshData->indexCount; indexIndex++) { if (indexes[indexIndex] == compareVertexIndex) { indexes[indexIndex] = vertexIndex; } else if (indexes[indexIndex] > compareVertexIndex) { indexes[indexIndex]--; } } } } } } int main(int argc, char ** argv) { parseArgs(argc, argv); if (inFileName == NULL || outFileName == NULL) { fprintf(stderr, "Error: Must specify both input and output file names\n"); printUsage(); return EXIT_FAILURE; } DeserializationTypeDetector * typeDetector = DeserializationTypeDetector_create(); DeserializationTypeDetector_registerDetectionCallback(typeDetector, JSONSerialization_typeDetector); DeserializationTypeDetector_registerDetectionCallback(typeDetector, BinarySerialization_typeDetector); DeserializationContext * context = DeserializationTypeDetector_createDeserializationContextWithFile(typeDetector, inFileName); if (context == NULL) { fprintf(stderr, "Couldn't open or determine type of \"%s\" (errno = %d)\n", inFileName, errno); return EXIT_FAILURE; } MeshConfigurationRegistry * meshConfigurationRegistry = MeshConfigurationRegistry_create(); MeshConfigurationRegistry_registerBuiltInConfigurations(meshConfigurationRegistry); MeshData * meshData = MeshData_deserialize(context, NULL, meshConfigurationRegistry); if (meshData == NULL) { fprintf(stderr, "Couldn't load input mesh\n"); } if (uniqVertices) { removeDuplicateVertices(meshData); } if (writeBinary) { BinarySerializationContext * context = BinarySerializationContext_create(false); MeshData_serialize(meshData, context); BinarySerializationContext_writeToFile(context, outFileName); BinarySerializationContext_dispose(context); } else { JSONSerializationContext * context = JSONSerializationContext_create(); MeshData_serialize(meshData, context); JSONSerializationContext_writeToFile(context, JSONEmitterFormat_multiLine, outFileName, NULL); JSONSerializationContext_dispose(context); } return EXIT_SUCCESS; }