### Get vertex colors from PLYData object (C++) Source: https://github.com/nmwsharp/happly/blob/master/README.md Retrieves the vertex colors from a PLYData object. Colors are returned as a vector of `std::array`, where each array represents the R, G, and B components (0-255) of a vertex color. The `vertexElementName` parameter allows specifying a non-standard name for the vertex element. ```C++ happly::PLYData ply("input.ply"); std::vector> colors = ply.getVertexColors(); // Or with a custom element name: // std::vector> custom_colors = ply.getVertexColors("my_vertices"); ``` -------------------------------- ### Get vertex positions from PLYData object (C++) Source: https://github.com/nmwsharp/happly/blob/master/README.md Retrieves the vertex positions from a PLYData object. The positions are returned as a vector of `std::array`, where each array represents the x, y, and z coordinates of a vertex. The `vertexElementName` parameter allows specifying a non-standard name for the vertex element. ```C++ happly::PLYData ply("input.ply"); std::vector> positions = ply.getVertexPositions(); // Or with a custom element name: // std::vector> custom_positions = ply.getVertexPositions("my_vertices"); ``` -------------------------------- ### Get face indices from PLYData object (C++) Source: https://github.com/nmwsharp/happly/blob/master/README.md Retrieves the face indices from a PLYData object. These indices typically refer to a vertex list and define the connectivity of faces. The function supports type promotion for index types and naive conversion between signed and unsigned integers. ```C++ happly::PLYData ply("mesh.ply"); // Assuming 'face' element with 'vertex_indices' property auto face_indices = ply.getFaceIndices(); // Example: specify expected index type ``` -------------------------------- ### CMake: Download and Integrate Googletest Source: https://github.com/nmwsharp/happly/blob/master/test/CMakeLists.txt This snippet demonstrates how to download, configure, and build the googletest framework as part of the CMake build process. It includes error handling for the download and build steps and sets the C++ standard for the gtest target. ```cmake ### Download and unpack googletest at configure time # (from the googletest docs) configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . RESULT_VARIABLE result WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) if(result) message(FATAL_ERROR "CMake step for googletest failed: ${result}") endif() execute_process(COMMAND ${CMAKE_COMMAND} --build . RESULT_VARIABLE result WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) if(result) message(FATAL_ERROR "Build step for googletest failed: ${result}") endif() # Prevent overriding the parent project's compiler/linker # settings on Windows set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # Add googletest directly to our build. This defines # the gtest and gtest_main targets. add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src ${CMAKE_CURRENT_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL) set_property(TARGET gtest PROPERTY CXX_STANDARD 14) ``` -------------------------------- ### Common-Case Helpers for Mesh Data Source: https://github.com/nmwsharp/happly/blob/master/README.md Helper functions for efficiently accessing and adding common mesh data like vertex positions and colors, and face indices. ```APIDOC ## Common-Case Helpers for Mesh Data ### Description This section provides helper functions for common mesh data operations, including vertex positions, colors, and face indices. ### Methods - `std::vector> getVertexPositions(std::string vertexElementName = "vertex")` - **Description**: Returns x,y,z vertex positions from an object. `vertexElementName` specifies the name of the element type holding vertices, which is conventionally "vertex". - **Method**: `std::vector> getVertexPositions(std::string vertexElementName)` - **Parameters**: - **vertexElementName** (string) - Optional - The name of the element type holding vertices (defaults to "vertex"). - `void addVertexPositions(std::vector>& vertexPositions)` - **Description**: Adds x,y,z vertex positions to an object, under the element name "vertex". - **Method**: `void addVertexPositions(std::vector>& vertexPositions)` - **Parameters**: - **vertexPositions** (vector>) - Required - A vector of vertex positions to add. - `std::vector> getVertexColors(std::string vertexElementName = "vertex")` - **Description**: Returns r,g,b vertex colors from an object. `vertexElementName` specifies the name of the element type holding vertices, which is conventionally "vertex". - **Method**: `std::vector> getVertexColors(std::string vertexElementName)` - **Parameters**: - **vertexElementName** (string) - Optional - The name of the element type holding vertices (defaults to "vertex"). - `void addVertexColors(std::vector>& vertexColors)` - **Description**: Adds r,g,b vertex colors to an object, under the element name "vertex". - **Method**: `void addVertexColors(std::vector>& vertexColors)` - **Parameters**: - **vertexColors** (vector>) - Required - A vector of vertex colors (0-255) to add. - `void addVertexColors(std::vector>& vertexColors)` - **Description**: Adds r,g,b vertex colors to an object, under the element name "vertex". Assumes input is in [0.0,1.0], and internally converts to 0-255 char values. - **Method**: `void addVertexColors(std::vector>& vertexColors)` - **Parameters**: - **vertexColors** (vector>) - Required - A vector of vertex colors (0.0-1.0) to add. - `std::vector> getFaceIndices()` - **Description**: Returns indices into a vertex list for each face. Usually 0-indexed, but there are no formal rules in the format. Supports type promotion as in `getProperty()`, and furthermore converts signed to unsigned and vice-versa, though the conversion is performed naively. - **Method**: `std::vector> getFaceIndices()` - **Template Parameters**: - **T** - The desired data type for the face indices. ``` -------------------------------- ### CMake: Define Test Executable and Run Tests Source: https://github.com/nmwsharp/happly/blob/master/test/CMakeLists.txt This code defines the main test executable 'ply-test', links it against the googletest library, and specifies include directories. It also enables testing and adds the executable as a CMake test named 'MainTest'. ```cmake # Test executable add_executable(ply-test main_test.cpp ) target_link_libraries(ply-test gtest) target_include_directories(ply-test PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/..") # Add cmake test target ("make test") enable_testing() add_test(MainTest ply-test) ``` -------------------------------- ### PLYData Constructors and File Operations Source: https://github.com/nmwsharp/happly/blob/master/README.md Constructs `PLYData` objects from files or streams, and methods to write `PLYData` objects to files or streams. ```APIDOC ## PLYData Constructors and File Operations ### Description This section covers the initialization of `PLYData` objects and the methods for reading from and writing to PLY files. ### Methods - `PLYData()` - **Description**: Construct an empty `PLYData` object containing no elements or properties. - **Method**: Constructor - `PLYData(std::string filename, bool verbose = false)` - **Description**: Construct a new `PLYData` object from a file, automatically detecting whether the file is plaintext or binary. If `verbose=true`, useful information about the file will be printed to `stdout`. - **Method**: Constructor - **Parameters**: - **filename** (string) - Required - The path to the PLY file. - **verbose** (boolean) - Optional - If true, prints verbose information during file reading. - `PLYData(std::istream& inStream, bool verbose = false)` - **Description**: Like the previous constructor, but reads from an `istream`. - **Method**: Constructor - **Parameters**: - **inStream** (istream) - Required - The input stream to read PLY data from. - **verbose** (boolean) - Optional - If true, prints verbose information during stream reading. - `PLYData::validate()` - **Description**: Perform some basic sanity checks on the object, throwing an exception if any fail. Called internally before writing. - **Method**: `void validate()` - `PLYData::write(std::string filename, DataFormat format = DataFormat::ASCII)` - **Description**: Write the object to a file. Specifying `DataFormat::ASCII`, `DataFormat::Binary`, or `DataFormat::BinaryBigEndian` controls the kind of output file. - **Method**: `void write(std::string filename, DataFormat format)` - **Parameters**: - **filename** (string) - Required - The path to the output file. - **format** (DataFormat) - Optional - The format to write the file in (ASCII, Binary, BinaryBigEndian). - `PLYData::write(std::ostream& outStream, DataFormat format = DataFormat::ASCII)` - **Description**: Like the previous method, but writes to an `ostream`. - **Method**: `void write(std::ostream& outStream, DataFormat format)` - **Parameters**: - **outStream** (ostream) - Required - The output stream to write PLY data to. - **format** (DataFormat) - Optional - The format to write the data in (ASCII, Binary, BinaryBigEndian). ``` -------------------------------- ### CMake: Configure Compiler Flags and Policies Source: https://github.com/nmwsharp/happly/blob/master/test/CMakeLists.txt Sets the minimum CMake version, project name, policy settings, output directories, C++ standard, and compiler extensions. It also defines compiler-specific flags for Clang/GCC and MSVC, including warning levels and optimization options. ```cmake cmake_minimum_required(VERSION 3.1) project(happly-test) ### Policy settings cmake_policy(SET CMP0054 NEW) # don't implicitly dereference inside if() ### Configure output locations set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) ### Compile options set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") # using Clang (linux or apple) or GCC message("Using clang/gcc compiler flags") SET(BASE_CXX_FLAGS "-Wall -Wextra -g3") SET(DISABLED_WARNINGS " -Wno-unused-parameter -Wno-unused-variable -Wno-unused-private-field -Wno-unused-function -Wno-deprecated-declarations") SET(TRACE_INCLUDES " -H -Wno-error=unused-command-line-argument") # clang-specific things if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") SET(BASE_CXX_FLAGS "${BASE_CXX_FLAGS} -ferror-limit=5 -fcolor-diagnostics") endif() SET(CMAKE_CXX_FLAGS "${BASE_CXX_FLAGS} ${DISABLED_WARNINGS}") #SET(CMAKE_CXX_FLAGS_DEBUG "-g3 -fsanitize=address") // tests are crashing in gtest with this on OSX. Is something broken or is the address checker being too aggressive with gtest? SET(CMAKE_CXX_FLAGS_DEBUG "-g3") SET(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG") SET(CMAKE_CXX_FLAGS_RELEASE "-march=native -O3 -DNDEBUG") SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # using Visual Studio C++ message("Using Visual Studio compiler flags") set(BASE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") set(BASE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") # parallel build SET(DISABLED_WARNINGS "${DISABLED_WARNINGS} /wd\"4267\"") # ignore conversion to smaller type (fires more aggressively than the gcc version, which is annoying) SET(DISABLED_WARNINGS "${DISABLED_WARNINGS} /wd\"4244\"") # ignore conversion to smaller type (fires more aggressively than the gcc version, which is annoying) SET(DISABLED_WARNINGS "${DISABLED_WARNINGS} /wd\"4305\"") # ignore truncation on initialization SET(CMAKE_CXX_FLAGS "${BASE_CXX_FLAGS} ${DISABLED_WARNINGS}") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") add_definitions(/D "_CRT_SECURE_NO_WARNINGS") add_definitions (-DNOMINMAX) else() # unrecognized message( FATAL_ERROR "Unrecognized compiler [${CMAKE_CXX_COMPILER_ID}]" ) endif() ``` -------------------------------- ### Miscellaneous Object Options Source: https://github.com/nmwsharp/happly/blob/master/README.md Access to comments and object information lines within the PLY file. ```APIDOC ## Miscellaneous Object Options ### Description This section describes access to comments and `obj_info` lines within the PLY file. ### Properties - `std::vector PLYData::comments` - **Description**: Comments included in the .ply file, one string per line. These are populated after reading and written when writing. - **Type**: `std::vector` - `std::vector PLYData::objInfoComments` - **Description**: Lines prefaced with `obj_info` included in the .ply file, which are effectively a different kind of comment, one string per line. These seem to be an ad-hoc extension to .ply, but they are pretty common, so we support them. - **Type**: `std::vector` ``` -------------------------------- ### Write Basic PLY Data Source: https://github.com/nmwsharp/happly/blob/master/README.md Illustrates how to create and write general elements and properties to a PLY file using the hapPLY library. It covers adding elements, properties, and specifying the output format (binary or ASCII). ```cpp #include "happly.h" // Suppose these hold your data std::vector elementA_prop1; std::vector elementA_prop2; std::vector> elementB_listProp; // Create an empty object happly::PLYData plyOut; // Add elements plyOut.addElement("elementA", 20); plyOut.addElement("elementB", 42); // Add properties to those elements plyOut.getElement("elementA").addProperty("prop1", elementA_prop1); plyOut.getElement("elementA").addProperty("prop2", elementA_prop2); plyOut.getElement("elementB").addListProperty("listprop1", elementB_listProp); // Write the object to file plyOut.write("my_output_file.ply", happly::DataFormat::Binary); ``` -------------------------------- ### Read Basic PLY Data Source: https://github.com/nmwsharp/happly/blob/master/README.md Demonstrates how to read general elements and properties from a PLY file using the hapPLY library. It shows accessing properties as different data types, including automatic type promotion. ```cpp #include "happly.h" // Construct a data object by reading from file happly::PLYData plyIn("my_file.ply"); // Get data from the object std::vector elementA_prop1 = plyIn.getElement("elementA").getProperty("prop1"); std::vector elementA_prop2 = plyIn.getElement("elementA").getProperty("prop1"); std::vector> elementB_listProp = plyIn.getElement("elementB").getListProperty("listprop1"); // Type promotion is automatic for numeric types: even if this property was stored as a float, // we can access it as a double std::vector elementA_prop1_as_double = plyIn.getElement("elementA").getProperty("prop1"); ``` -------------------------------- ### Accessing and Adding Element Data Source: https://github.com/nmwsharp/happly/blob/master/README.md Methods for managing elements within a `PLYData` object, including adding, retrieving, and checking for the existence of elements and their properties. ```APIDOC ## Accessing and Adding Element Data ### Description This section details how to interact with elements and their properties within a `PLYData` object. ### Methods - `void addElement(std::string name, size_t count)` - **Description**: Add a new element type to the object, with the given name and number of elements. - **Method**: `void addElement(std::string name, size_t count)` - **Parameters**: - **name** (string) - Required - The name of the element type to add. - **count** (size_t) - Required - The number of elements of this type. - `Element& getElement(std::string target)` - **Description**: Get a reference to an element type contained in the object. - **Method**: `Element& getElement(std::string target)` - **Parameters**: - **target** (string) - Required - The name of the element to retrieve. - `bool hasElement(std::string target)` - **Description**: Check if an element type is contained in the object. - **Method**: `bool hasElement(std::string target)` - **Parameters**: - **target** (string) - Required - The name of the element to check for. - `std::vector getElementNames()` - **Description**: List of all element names. - **Method**: `std::vector getElementNames()` - `std::vector Element::getProperty(std::string propertyName)` - **Description**: Get a vector of property data for an element. Will automatically promote types if possible, e.g., `getProperty("my_prop")` will succeed even if the object contains `"my_prop"` with type `short`. - **Method**: `std::vector getProperty(std::string propertyName)` - **Parameters**: - **propertyName** (string) - Required - The name of the property to retrieve. - **Template Parameters**: - **T** - The desired data type for the property. - `std::vector> Element::getListProperty(std::string propertyName)` - **Description**: Get a vector of list property data for an element. Supports type promotion just like `getProperty()`. - **Method**: `std::vector> getListProperty(std::string propertyName)` - **Parameters**: - **propertyName** (string) - Required - The name of the list property to retrieve. - **Template Parameters**: - **T** - The desired data type for the elements within the list property. - `void Element::addProperty(std::string propertyName, std::vector& data)` - **Description**: Add a new property to an element type. `data` must be the same length as the number of elements of that type. - **Method**: `void addProperty(std::string propertyName, std::vector& data)` - **Parameters**: - **propertyName** (string) - Required - The name of the property to add. - **data** (vector) - Required - A vector containing the property data. - **Template Parameters**: - **T** - The data type of the property. - `void addListProperty(std::string propertyName, std::vector>& data)` - **Description**: Add a new list property to an element type. `data` must be the same length as the number of elements of that type. - **Method**: `void addListProperty(std::string propertyName, std::vector>& data)` - **Parameters**: - **propertyName** (string) - Required - The name of the list property to add. - **data** (vector>) - Required - A vector of vectors containing the list property data. - **Template Parameters**: - **T** - The data type of the elements within the list property. ``` -------------------------------- ### Write Mesh-like PLY Data Source: https://github.com/nmwsharp/happly/blob/master/README.md Demonstrates how to add and write mesh-specific data, such as vertex positions, colors, and face indices, to a PLY file using the hapPLY library. This is useful for exporting 3D models. ```cpp #include "happly.h" // Suppose these hold your data std::vector> meshVertexPositions; std::vector> meshVertexColors; std::vector> meshFaceIndices; // Create an empty object happly::PLYData plyOut; // Add mesh data (elements are created automatically) plyOut.addVertexPositions(meshVertexPositions); plyOut.addVertexColors(meshVertexColors); plyOut.addFaceIndices(meshFaceIndices); // Write the object to file plyOut.write("my_output_mesh_file.ply", happly::DataFormat::ASCII); ``` -------------------------------- ### Read Mesh-like PLY Data Source: https://github.com/nmwsharp/happly/blob/master/README.md Shows how to extract common mesh-related data, specifically vertex positions and face indices, from a PLY file using hapPLY. This simplifies working with 3D geometry stored in PLY format. ```cpp #include "happly.h" // Construct the data object by reading from file happly::PLYData plyIn("my_mesh_file.ply"); // Get mesh-style data from the object std::vector> vPos = plyIn.getVertexPositions(); std::vector> fInd = plyIn.getFaceIndices(); ``` -------------------------------- ### Access and manipulate PLYData elements and properties (C++) Source: https://github.com/nmwsharp/happly/blob/master/README.md Provides methods to interact with elements and their properties within a PLYData object. This includes adding new elements, retrieving existing ones, checking for their presence, and accessing/adding specific properties (including list properties). Type promotion is supported for property access. ```C++ happly::PLYData ply; // Add element ply.addElement("vertex", 10); // Check for element if (ply.hasElement("vertex")) { // Get element reference happly::Element& vertex_element = ply.getElement("vertex"); // Add property std::vector x_coords = {0.1, 0.2, 0.3}; vertex_element.addProperty("x", x_coords); // Get property (with type promotion) std::vector y_coords_float = vertex_element.getProperty("y"); // Get list property std::vector> face_vertex_indices = vertex_element.getListProperty("face_indices"); } ``` -------------------------------- ### Read PLY file into PLYData object (C++) Source: https://github.com/nmwsharp/happly/blob/master/README.md Constructs a PLYData object by reading from a specified file. The library automatically detects the file format (ASCII or binary). An optional verbose flag can be set to print file information to standard output. Errors during file parsing will be communicated via C++ exceptions. ```C++ happly::PLYData ply("my_input.ply"); happly::PLYData ply_verbose("my_input.ply", true); ``` -------------------------------- ### Read PLY file from an input stream (C++) Source: https://github.com/nmwsharp/happly/blob/master/README.md Constructs a PLYData object by reading from an `std::istream` rather than a file path. This allows for reading PLY data from various sources like network sockets or in-memory buffers. An optional verbose flag can be set for debugging output. ```C++ #include std::string ply_data_string = "ply\nformat ascii 1.0\nelement vertex 2\nproperty double x\nproperty double y\nproperty double z\nend_header\n0 0 0\n1 1 1\n"; std::stringstream ss(ply_data_string); happly::PLYData ply(ss); happly::PLYData ply_verbose(ss, true); ``` -------------------------------- ### Add Face Indices to Object (C++) Source: https://github.com/nmwsharp/happly/blob/master/README.md Adds vertex indices for faces to an object, named 'face' with property 'vertex_indices'. It automatically converts to a 32-bit integer type, matching the input's signedness, and raises an error if conversion fails. ```cpp void addFaceIndices(std::vector>& indices) ``` -------------------------------- ### Add vertex colors to PLYData object (C++) Source: https://github.com/nmwsharp/happly/blob/master/README.md Adds vertex colors to the PLYData object. Supports adding colors as RGB byte arrays (0-255) or as RGB double arrays (0.0-1.0), which are internally converted. Colors are associated with the 'vertex' element by default. ```C++ std::vector> colors_uchar = {{ {255, 0, 0}, {0, 255, 0} }}; happly::PLYData ply; ply.addVertexColors(colors_uchar); std::vector> colors_double = {{ {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0} }}; ply.addVertexColors(colors_double); ``` -------------------------------- ### Write PLYData object to file (C++) Source: https://github.com/nmwsharp/happly/blob/master/README.md Writes the current PLYData object to a specified file. The output format (ASCII or binary) can be controlled by the DataFormat enum. This operation can throw C++ exceptions if there are issues during writing or if the object fails internal validation checks before writing. ```C++ happly::PLYData ply; // ... populate ply object ... ply.write("my_output.ply", DataFormat::ASCII); ply.write("my_output.bin", DataFormat::Binary); ``` -------------------------------- ### Write PLYData object to an output stream (C++) Source: https://github.com/nmwsharp/happly/blob/master/README.md Writes the PLYData object to a specified `std::ostream`. This enables writing PLY data to destinations other than files, such as network connections or memory buffers. The output format can be controlled using the `DataFormat` enum. ```C++ #include happly::PLYData ply; // ... populate ply object ... std::stringstream ss_ascii; ply.write(ss_ascii, DataFormat::ASCII); std::string ascii_output = ss_ascii.str(); std::stringstream ss_binary; ply.write(ss_binary, DataFormat::Binary); std::string binary_output = ss_binary.str(); ``` -------------------------------- ### Add vertex positions to PLYData object (C++) Source: https://github.com/nmwsharp/happly/blob/master/README.md Adds a list of vertex positions, represented as an array of three doubles (x, y, z), to the PLYData object. These are typically added under an element named 'vertex'. The function handles the internal representation of this data for PLY file output. ```C++ std::vector> positions = {{ {0.0, 0.0, 0.0}, {1.0, 0.0, 0.0} }}; happly::PLYData ply; ply.addVertexPositions(positions); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.