Code Monkey home page Code Monkey logo

miniply-swift's Introduction

miniply-swift

miniply-swift is a Swift wrapper for the miniply C++ library, designed to simplify the process of loading .ply files in your Swift projects.

Warning

This wrapper is not complete. The only functionality implemented is loading of basic property types (int, float, double, etc.) from a .ply file. List property types are not supported.

Features

  • Swift-friendly API for loading .ply files.
  • Leverages the power and efficiency of the miniply C++ library.
  • Easy to integrate into your Swift projects.

Installation

Warning

These instructions are untested. Please let me know if you encounter any issues.

You can install miniply-swift using the Swift Package Manager. Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/Netruk44/miniply-swift", from: "0.1.0")
]

In addition, you will need to change your Swift project's build settings to use Objective-C++. To do this, go to your project's settings and go to the "Build Settings" tab. Search for "Interop" to find the C++ and Objective-C Interoperability setting, and set it to "C++ / Objective-C++".

Usage

Here's a basic example of how to use miniply-swift to load a .ply file:

import miniply_swift

// For example purposes, throw strings.
extension String : Error { }

class MyClass {

  private struct VertexData {
        // Note: ALL properties must be of the same type (e.g. Float).
        //       If you need to load multiple types, you will need to create
        //       separate structs for each type.

        var x: Float = 0
        var y: Float = 0
        var z: Float = 0
        
        var scaleX: Float = 0
        var scaleY: Float = 0
        var scaleZ: Float = 0
        
        var orientationX: Float = 0
        var orientationY: Float = 0
        var orientationZ: Float = 0
        var orientationW: Float = 0
        
        var colorR: Float = 0
        var colorG: Float = 0
        var colorB: Float = 0
        var opacity: Float = 0
        
        // Elements of this array MUST follow the same ordering as this struct
        // Otherwise the memory won't be copied into this object correctly.
        //
        // You will need to update these strings for your specific .ply file.
        static let requiredProperties = [
            "x", "y", "z",
            "scale_0", "scale_1", "scale_2",
            "rot_0", "rot_1", "rot_2", "rot_3",
            "colorR", "colorG", "colorB", "colorA"
        ]
    }

    func loadPlyFile(plyFilePath: String) {
        let reader = PlyReader(fromFilePath: plyFile)
        guard reader.isValid() else { throw "Invalid .ply file." }

        while reader.hasElement() {
          // Look for the vertex element
          guard reader.currentElementIs(ofType: .VertexElement) else {
            reader.nextElement()
            continue
          }

          guard reader.loadElement() else { throw "Failed to load vertex element." }

          guard let foundIndices = reader.findPropertyIndices(forProperties: VertexData.requiredProperties) else {
            throw "Failed to find required properties."
          }

          guard var loadedData: [VertexData] = reader.retrieveProperties(forIndices: foundIndices, ofType: .Float) else {
            throw "Failed to retrieve properties."
          }

          // TODO: Do something with the loaded data.
        }
    }
}

License

miniply-swift is released under the MIT license. See license.md for details.

miniply-swift's People

Contributors

netruk44 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.