Code Monkey home page Code Monkey logo

pyobb's Introduction

pyobb

Build Status PyPI version

OBB implementation in Python (using numpy)

This is basically a port of the code found on James' Blog, which in turn is a C++ implementation (using CGAL) of the ideas found in Stefan Gottschalk's PhD thesis. The central idea of this OBB contruction is to compute a covariance matrix for a point set and then find the eigenvectors of this covariance matrix.


Installation

Simply run

pip install pyobb

Usage

The pyobb package contains a single class: OBB. An OBB has the following attributes:

  • centroid: the OBB center
  • min: the OBB point with the smallest XYZ components in the local frame (i.e., -[width/2, height/2, depth/2])
  • max: the OBB point with the largest XYZ components in the local frame (i.e., [width/2, height/2, depth/2])
  • points: the 8 points of the OBB
  • extents: the extents of the OBB in the XYZ-axis (i.e., the scaled unit vectors of the global frame)
  • rotation: the rotation matrix of the OBB

You have three different ways to build an OBB: using a covariance matrix, using a point set and using a triangle mesh. Those ways are respectively implemented by the methods:

  • OBB.build_from_covariance_matrix(covariance_matrix, points): expects a 3x3 covariance matrix and a set of 3D points
  • OBB.build_from_points(points): expects a set of 3D points
  • OBB.build_from_triangles(points, triangles): expects a set of 3D points and a flat list of indices refering those points for which every 3-uple would form a triangle

For instance, you can create an OBB from the points of a lat/lon sphere

from math import pi, cos, sin, sqrt
from pyobb.obb import OBB

# creates a lat/lon sphere with a given radius and centered at a given point
def sphere(radius, center, num_slices=30):
    theta_step = 2.0 * pi / (num_slices - 1)
    phi_step = pi / (num_slices - 1.0)
    theta = 0.0
    vertices = []
    for i in range(0, num_slices):
        cos_theta = cos(theta)
        sin_theta = sin(theta)
        phi = 0.0
        for j in range(0, num_slices):
            x = -sin(phi) * cos_theta
            y = -cos(phi)
            z = -sin(phi) * sin_theta
            n = sqrt(x * x + y * y + z * z)
            if n < 0.99 or n > 1.01:
                x /= n
                y /= n
                z /= n
            vertices.append((x * radius + center[0],
                             y * radius + center[1],
                             z * radius + center[2]))
            phi += phi_step
        theta += theta_step
    return vertices

obb = OBB.build_from_points(sphere(1, (0, 0, 0)))

Which gives you this OBB:

You can also create an OBB from the vertices and faces of OBJ models

from pyobb.obb import OBB
from objloader import OBJ  # source: http://www.pygame.org/wiki/OBJFileLoader

obj = OBJ(filename='bunny.obj')  # stanford bunny
# obj = OBJ(filename='killeroo.obj')  # killeroo
indices = []
for face in obj.faces:
    indices.append(face[0][0] - 1)
    indices.append(face[0][1] - 1)
    indices.append(face[0][2] - 1)
obb = OBB.build_from_triangles(obj.vertices, indices)

Which gives you something like this:

or this:

pyobb's People

Contributors

genevievebuckley avatar pboechat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

pyobb's Issues

How to visualize with obb

Hi, I am new learner of 3d geometry. Your tool helps me a a lot to get oriented bounding box. But I don't know how to visualize it like what to show in the README.md. Could you give me some hints or url sources to guide me how to visualize it?

Incorrect obb vertex

snapshot

I noticed that in certain cases, one of the vertices is wrong.

From the code below, I have represented in yellow an array of coordinates of points while the vertices of the obb are green.
spond to the points I got from the obb object constructed from the yellow dots
The last vertex is not correct.

Please find attached a MWE
Sorry for the length of the code.

`
import numpy as np
from numpy import array
from mayavi import mlab
from pyobb.obb import OBB
Pts =
array([[ 0.3 , 0.6012 , -0.5 ],
[ 0.22981333, 0.66547876, -0.5 ],
[ 0.05209445, 0.69968078, -0.5 ],
[-0.15 , 0.68780254, -0.5 ],
[-0.28190779, 0.63540201, -0.5 ],
[-0.28190779, 0.56699799, -0.5 ],
[-0.15 , 0.51459746, -0.5 ],
[ 0.05209445, 0.50271922, -0.5 ],
[ 0.22981333, 0.53692124, -0.5 ],
[ 0.3 , 0.6012 , -0.5 ],
[ 0.3 , 0.6012 , -0.36200274],
[ 0.22981333, 0.66547876, -0.36200274],
[ 0.05209445, 0.69968078, -0.36200274],
[-0.15 , 0.68780254, -0.36200274],
[-0.28190779, 0.63540201, -0.36200274],
[-0.28190779, 0.56699799, -0.36200274],
[-0.15 , 0.51459746, -0.36200274],
[ 0.05209445, 0.50271922, -0.36200274],
[ 0.22981333, 0.53692124, -0.36200274],
[ 0.3 , 0.6012 , -0.36200274],
[ 0.3 , 0.6012 , -0.2441701 ],
[ 0.22981333, 0.66547876, -0.2441701 ],
[ 0.05209445, 0.69968078, -0.2441701 ],
[-0.15 , 0.68780254, -0.2441701 ],
[-0.28190779, 0.63540201, -0.2441701 ],
[-0.28190779, 0.56699799, -0.2441701 ],
[-0.15 , 0.51459746, -0.2441701 ],
[ 0.05209445, 0.50271922, -0.2441701 ],
[ 0.22981333, 0.53692124, -0.2441701 ],
[ 0.3 , 0.6012 , -0.2441701 ],
[ 0.3 , 0.6012 , -0.14074074],
[ 0.22981333, 0.66547876, -0.14074074],
[ 0.05209445, 0.69968078, -0.14074074],
[-0.15 , 0.68780254, -0.14074074],
[-0.28190779, 0.63540201, -0.14074074],
[-0.28190779, 0.56699799, -0.14074074],
[-0.15 , 0.51459746, -0.14074074],
[ 0.05209445, 0.50271922, -0.14074074],
[ 0.22981333, 0.53692124, -0.14074074],
[ 0.3 , 0.6012 , -0.14074074],
[ 0.3 , 0.6012 , -0.04595336],
[ 0.22981333, 0.66547876, -0.04595336],
[ 0.05209445, 0.69968078, -0.04595336],
[-0.15 , 0.68780254, -0.04595336],
[-0.28190779, 0.63540201, -0.04595336],
[-0.28190779, 0.56699799, -0.04595336],
[-0.15 , 0.51459746, -0.04595336],
[ 0.05209445, 0.50271922, -0.04595336],
[ 0.22981333, 0.53692124, -0.04595336],
[ 0.3 , 0.6012 , -0.04595336],
[ 0.3 , 0.6012 , 0.04595336],
[ 0.22981333, 0.66547876, 0.04595336],
[ 0.05209445, 0.69968078, 0.04595336],
[-0.15 , 0.68780254, 0.04595336],
[-0.28190779, 0.63540201, 0.04595336],
[-0.28190779, 0.56699799, 0.04595336],
[-0.15 , 0.51459746, 0.04595336],
[ 0.05209445, 0.50271922, 0.04595336],
[ 0.22981333, 0.53692124, 0.04595336],
[ 0.3 , 0.6012 , 0.04595336],
[ 0.3 , 0.6012 , 0.14074074],
[ 0.22981333, 0.66547876, 0.14074074],
[ 0.05209445, 0.69968078, 0.14074074],
[-0.15 , 0.68780254, 0.14074074],
[-0.28190779, 0.63540201, 0.14074074],
[-0.28190779, 0.56699799, 0.14074074],
[-0.15 , 0.51459746, 0.14074074],
[ 0.05209445, 0.50271922, 0.14074074],
[ 0.22981333, 0.53692124, 0.14074074],
[ 0.3 , 0.6012 , 0.14074074],
[ 0.3 , 0.6012 , 0.2441701 ],
[ 0.22981333, 0.66547876, 0.2441701 ],
[ 0.05209445, 0.69968078, 0.2441701 ],
[-0.15 , 0.68780254, 0.2441701 ],
[-0.28190779, 0.63540201, 0.2441701 ],
[-0.28190779, 0.56699799, 0.2441701 ],
[-0.15 , 0.51459746, 0.2441701 ],
[ 0.05209445, 0.50271922, 0.2441701 ],
[ 0.22981333, 0.53692124, 0.2441701 ],
[ 0.3 , 0.6012 , 0.2441701 ],
[ 0.3 , 0.6012 , 0.36200274],
[ 0.22981333, 0.66547876, 0.36200274],
[ 0.05209445, 0.69968078, 0.36200274],
[-0.15 , 0.68780254, 0.36200274],
[-0.28190779, 0.63540201, 0.36200274],
[-0.28190779, 0.56699799, 0.36200274],
[-0.15 , 0.51459746, 0.36200274],
[ 0.05209445, 0.50271922, 0.36200274],
[ 0.22981333, 0.53692124, 0.36200274],
[ 0.3 , 0.6012 , 0.36200274],
[ 0.3 , 0.6012 , 0.5 ],
[ 0.22981333, 0.66547876, 0.5 ],
[ 0.05209445, 0.69968078, 0.5 ],
[-0.15 , 0.68780254, 0.5 ],
[-0.28190779, 0.63540201, 0.5 ],
[-0.28190779, 0.56699799, 0.5 ],
[-0.15 , 0.51459746, 0.5 ],
[ 0.05209445, 0.50271922, 0.5 ],
[ 0.22981333, 0.53692124, 0.5 ],
[ 0.3 , 0.6012 , 0.5 ]])

mlab.points3d(Pts[:,0],
Pts[:,1],
Pts[:,2],
color = (1.,1.,0.))
obb = OBB.build_from_points(Pts)
"""
get the vertices of the obb
"""
vertices = np.concatenate(obb.points).reshape(-1,3)
mlab.points3d(vertices[:,0],
vertices[:,1],
vertices[:,2],
color = (0.,1.,0.))
`

Local frame of reference or global frame of reference

For all these following:
centroid: the OBB center
min: the OBB point with the smallest XYZ components
max: the OBB point with the largest XYZ components
points: the 8 points of the OBB
extents: the extents of the OBB in the XYZ-axis
Are they based on the local frame of reference, or the global frame of reference?
I am kinda confused.
And should I use rotation.T as the base vector of OBB, and take the centroid as the origin of the local frame of reference?

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.