Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DrawBBox for displaz #165

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

DrawBBox for displaz #165

wants to merge 5 commits into from

Conversation

mrosen
Copy link

@mrosen mrosen commented Mar 29, 2017

Here's a start at an application that allow a displaz user to interactively define a bounding box. You can set the center point and grow the box by 50% in x, y and z directions by pressing the corresponding keys. Others may find it useful as a starting point.

Copy link
Owner

@c42f c42f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making the time to submit this :-)

I wonder where best to put this. The test directory is probably better suited to mundane test data, whereas this is a usage example. Perhaps moving it to a new directory doc/examples would make more sense?

Would you consider using the standard PEP-8 for the code style? At the most basic, this means function names should look like make_bbox, and using four spaces per indentation level consistently. (The other python code in this repo (bindings/python) more or less uses PEP-8, I hope :-) )

I've put a bunch of line comments for things which could be a bit neater, if you have the time to do some tweaking.

test/DrawBBox.py Outdated

# write out the .ply file and have Displaz render it
def UpdateDisplaz(v, T):
t = open(PLY_FILE, 'w')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use the python standard library tempfile module to do this without hard coding the PLY_FILE name:

t = tempfile.NamedTemporaryFile(suffix=".ply", delete=False)

test/DrawBBox.py Outdated
vertices = [numpy.add(v_center, xuv) for xuv in xfm_unit_vectors]

# write out resulting vertices
map(lambda v: f.write("%s\n" % " ".join(str(elem) for elem in v)), vertices)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a small nicety, this might be somewhat clearer as a simple for loop (map would normally be used to gather output, but here it's being used more as a foreach, which is something python lacks):

v_center = np.array(v_center)
for uvec in unit_vectors:
    vertex = v_center + transform.dot(uvec)
    f.write("%.17g %.17g %.17g\n" % tuple(vertex))

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, I'm using %.17g here, because this will exactly preserve all the binary digits of any floating point number, while keeping the string short if possible.

test/DrawBBox.py Outdated
PLY_FILE="/tmp/displaz_bbox.ply"

# write .ply file representing a box with specfied center and transform to f
def MakeBBox(v_center, transform, f):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you see the python bindings inside the bindings/python directory? They're pretty basic, but some ply writing utilities like this one could arguably go in there rather than in this example. Just a thought really, I don't mind having this here, for the moment.

test/DrawBBox.py Outdated
f.write("property float z\n")
f.write("element face 2\n")
f.write("property list uint int vertex_index\n")
f.write("end_header\n")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor note - writing literal blocks of text like this to file can be done neatly with the standard textwrap module (see textwrap.dedent to remove pesky indentation from a big block of text in a function).

test/DrawBBox.py Outdated
t.truncate()
MakeBBox(v, T, t)
t.close()
p = subprocess.Popen(["displaz -script -rmtemp " + PLY_FILE], stdout=subprocess.PIPE, shell=True, bufsize=1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that you use the default shell=False, and pass the arguments as a list ["displaz", "-script", "-rmtemp", PLY_FILE] when calling Popen. This is more annoying to type, but is far more reliable whenever you have arguments which would need to be quoted when passed to the shell, for example, empty arguments, file names with whitespace, etc.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, how about labelling your dataset? If you use the -label Box option, it will show up in the dataset view with a more readable name, rather than the temporary file name.

test/DrawBBox.py Outdated
print " Shift Click will set the 3D cursor and put a 1 x 1 x 1 BBox there.\n"
print " Do this first.\n"
print " Then you can press x, y and z to grow it.\n"
print " Remember that these keystrokes go into the Displayz application (needs focus)"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: Displayz

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the docs!

test/DrawBBox.py Outdated
f.write("4 4 5 6 7\n")

# write out the .ply file and have Displaz render it
def UpdateDisplaz(v, T):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation here is inconsistent with the other functions. Personally I prefer the standard PEP-8 style, with four spaces per indentation level.

@mrosen
Copy link
Author

mrosen commented Mar 30, 2017 via email

@c42f
Copy link
Owner

c42f commented Mar 31, 2017

Great!

@mrosen
Copy link
Author

mrosen commented Jul 19, 2017

Sorry for the long delay here. There were internal company issues as well as other distractions. I think the new code is PEP-8 compliant and certainly does a lot more. I hope others find this useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants