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

Viewer py add magnum text #1862

Merged
merged 32 commits into from
Sep 27, 2022
Merged

Viewer py add magnum text #1862

merged 32 commits into from
Sep 27, 2022

Conversation

jrreyna
Copy link
Contributor

@jrreyna jrreyna commented Sep 13, 2022

Motivation and Context

Using the magnum text code merged in PR #1853 to add text to the viewer.py app. I had to update the settings.py code. For some reason, it was drastically different from what I originally used for my the code in my #1838 PR so it wasn't working. It couldn't find "habitat_sim.utils.settings" so I had to change it to "examples.settings." I think the former refers to the settings file used for viewer.cpp

How Has This Been Tested

Locally, and with the CI tests. Here are two screenshots that show the mouse mode changing. We don't have python bindings yet to expose frame profiler functionality

Screenshot from 2022-09-26 15-01-21
Screenshot from 2022-09-26 15-03-12

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have completed my CLA (see CONTRIBUTING)
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Sep 13, 2022
@jrreyna jrreyna self-assigned this Sep 13, 2022
@Skylion007
Copy link
Contributor

Seems like you accidentally reverting some submodules? Like pybind11?

@jrreyna
Copy link
Contributor Author

jrreyna commented Sep 13, 2022

Seems like you accidentally reverting some submodules? Like pybind11?

Yes I have no idea what is happening. I made a branch from origin/main, and it says I am up to date, but the magnum-bindings submodule is also not correct. I am missing several files and changes that I see were pushed in the #1853 PR. I also tried the command git submodule update src/deps/magnum-bindings but it is not doing anything

@Skylion007
Copy link
Contributor

Seems like you accidentally reverting some submodules? Like pybind11?

Yes I have no idea what is happening. I made a branch from origin/main, and it says I am up to date, but the magnum-bindings submodule is also not correct. I am missing several files and changes that I see were pushed in the #1853 PR. I also tried the command git submodule update src/deps/magnum-bindings but it is not doing anything

You need to cd into those folders, git checkout the proper commits (and then git add the submodule folder) (or git checkout the .submodule file directly and then do the git submodule update).

examples/viewer.py Outdated Show resolved Hide resolved
examples/viewer.py Outdated Show resolved Hide resolved
examples/viewer.py Outdated Show resolved Hide resolved
examples/settings.py Outdated Show resolved Hide resolved
examples/settings.py Outdated Show resolved Hide resolved
@ykarmesh ykarmesh linked an issue Sep 14, 2022 that may be closed by this pull request
Comment on lines 806 to 811
Last {self.num_frames_to_track} frames:
Frame time: {placeholder} ms
CPU duration: {placeholder} ms
GPU duration: {placeholder} µs
Vertex fetch ratio: {placeholder}
Primitives clipped: {placeholder} %
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah yes, I should expose the FrameProfiler to Python as well. Adding a TODO for myself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah thank you, I was looking into the gl module in Python to see if there were any profiling options, but just decided to bring it up later with the team

self._shader.draw(self._window_text.mesh)

def get_sensor_subtype_string(self, sensor_subtype):
if sensor_subtype == SensorSubType.PINHOLE:
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not quite as pretty, but can't we just figure out a way to stringify the name of the enum? Makes it easier to extend. Something like

    return str(sensor_subtype.value)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If that is possible in Python, that would be sweet. I'll look into it. I just remember trying to do that in C++ one time and it was tough

examples/viewer.py Outdated Show resolved Hide resolved
@johnrreyna
Copy link

johnrreyna commented Sep 15, 2022

Where are these extra fonts coming from? Are they included on purpose?

Yeah, I just added them to this directory in case the font directory changes and the default font is no longer in the right place. I want a default font loaded so that the user doesn't always have to provide one in the command line, and right now it just loads it from the current directory. I can remove them and just provide the relative path of the font in a fonts directory to the function that loads it in _init_. The viewer.cpp app has a font file in its directory as well, so I did the same. But I amenable to whatever. In general, it's probably a bad idea to duplicate files

jrreyna and others added 3 commits September 16, 2022 16:10
…t is in viewer.cpp, also removed some unnecessary fonts from the examples directory
change hardcoded glyphs to ascii imputs

Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
@Skylion007
Copy link
Contributor

Why can't we use the same proggytff file that C++ user is using btw?

@johnrreyna
Copy link

johnrreyna commented Sep 20, 2022

Why can't we use the same proggytff file that C++ user is using btw?

That proggy ttf file was copied directly into the same directory as viewer.cpp, so I did something similar and copied the file into the same directory as viewer.py. Though I think the best solution is to have a fonts directory and specify the relative path to it. I just wanted to be safe and put the ttf file in the same directory for now in case the font directory moves

@@ -1,4 +1,4 @@
group=fonts

[file]
filename=ProggyClean.ttf
filename=../../../data/fonts/ProggyClean.ttf
Copy link
Collaborator

Choose a reason for hiding this comment

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

To avoid having to hardcode the ugly relative path in viewer.cpp, add an alias here (docs):

Suggested change
filename=../../../data/fonts/ProggyClean.ttf
filename=../../../data/fonts/ProggyClean.ttf
alias=ProggyClean.ttf

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oo that's slick. Thanks, I wasn't exactly sure how the resources.conf file worked

@@ -899,7 +899,8 @@ Viewer::Viewer(const Arguments& arguments)

Cr::Utility::Resource rs{"fonts"};
font_ = fontManager_.loadAndInstantiate("TrueTypeFont");
if (!font_ || !font_->openData(rs.getRaw("ProggyClean.ttf"), fontSize))
std::string font_relative_path = "../../../data/fonts/ProggyClean.ttf";
if (!font_ || !font_->openData(rs.getRaw(font_relative_path), fontSize))
Copy link
Collaborator

Choose a reason for hiding this comment

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

... and this change then isn't needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you know if I can do something similar in viewer.py to avoid hardcoding the ugly relative path?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the viewer.py meant to be packaged / installed in some way (setup.py, pip etc.)? I know very little about Python packaging, but if it is, I'd put the font file into some "well-known package data location" (if there's such a thing) and reference it from there. If not, then I'm afraid the path has to be hardcoded that way. But I'd make it relative to __file__, at least, so it doesn't depend on CWD.

Another idea I had was that we embed the font directly inside the Habitat libraries instead of the C++ viewer, and expose that array to Python. Such as

Containers::ArrayView<const char> fontData() {
    Cr::Utility::Resource rs{"fonts"};
    return rs.getRaw("ProggyClean.ttf");
}

and then, if the above would be exposed as habitat.font_data() to Python, you'd do

self.display_font.open_data(habitat.font_data(), size)

instead of self.display_font.open_file("that/ugly/path.ttf", size). A downside is that the binary now embeds a font file that's only needed if using a GUI viewer, but as long as we stay with that tiny 40 kB font file, I think the convenience wins over memory use :)

…ly relative path in viewer.cpp to refer to a font
@ykarmesh ykarmesh marked this pull request as ready for review September 27, 2022 00:09
Copy link
Contributor

@ykarmesh ykarmesh left a comment

Choose a reason for hiding this comment

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

@aclegg3 @johnrreyna I am approving this PR.

relative_path_to_font = "../data/fonts/ProggyClean.ttf"
self.display_font.open_file(
os.path.join(os.path.dirname(__file__), relative_path_to_font),
180,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
180,
13,

)

# Glyphs we need to render everything
self.glyph_cache = text.GlyphCache(mn.Vector2i(2048))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self.glyph_cache = text.GlyphCache(mn.Vector2i(2048))
self.glyph_cache = text.GlyphCache(mn.Vector2i(256))

... we discussed this on Slack some weeks ago, so just a reminder :) It'll also avoid unnecessary GPU memory use, and since the font is a bitmap one, rendering it at 10x the size will not make it any prettier.

* HabitatSimInteractiveViewer.TEXT_DELTA_FROM_CENTER,
)
)
self.shader = shaders.DistanceFieldVectorGL2D()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self.shader = shaders.DistanceFieldVectorGL2D()
self.shader = shaders.VectorGL2D()

Since you aren't really using any outline anyway, let's use a simpler shader. (The same is used in the viewer.cpp.)

Comment on lines 817 to 818
self.shader.outline_color = [1.0, 1.0, 1.0]
self.shader.outline_range = (0.45, 0.40)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self.shader.outline_color = [1.0, 1.0, 1.0]
self.shader.outline_range = (0.45, 0.40)

Those aren't defined in the simpler non-distance-field shader.

@@ -0,0 +1,364 @@
----------------------------------------------------------------------
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd remove everything from this directory except for the ProggyClean.ttf. Unnecessary and irrelevant now that the font file is hardcoded anyway. Tho it might be good to credit

ProggyClean.ttf by Tristan Grimmer

somewhere, although I don't know if there's even such a place in the docs or README.

…into viewer-py-add-magnum-text

updating submodules
@Skylion007
Copy link
Contributor

Do we need the other font files besides proggy?

@jrreyna
Copy link
Contributor Author

jrreyna commented Sep 27, 2022

Do we need the other font files besides proggy?

I'm not sure, I just used the same file that was in the viewer.cpp file. Those fonts were in a directory in the imgui submodule, so I don't even think those font files are on main, right?

@johnrreyna
Copy link

Do we need the other font files besides proggy?

Just talked to mosra, probably not. Will delete them for now

…xample of how to use the font in python with magnum. Also changed the text shader from DistanceFieldVectorGL2D() to VectorGL2D() to make things simpler
…into viewer-py-add-magnum-text

pull and merge main into this branch
@jrreyna jrreyna merged commit 8b8a098 into main Sep 27, 2022
@Skylion007 Skylion007 deleted the viewer-py-add-magnum-text branch September 27, 2022 23:07
@Skylion007
Copy link
Contributor

@johnrreyna Please squash your commits next time when merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Do not delete this pull request or issue due to inactivity.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GUI Text for viewer.py
8 participants