520 Commits

Author SHA1 Message Date
Nikhila Ravi
c75ca04cf7 Bug fix in rendering clipped meshes
Summary:
There was a bug when `z_clip_value` is not None but there are no faces which are actually visible in the image due to culling.  In `rasterize_meshes.py` a function `convert_clipped_rasterization_to_original_faces` is called to convert the clipped face indices etc back to the unclipped versions, but the case where there is no clipping was not handled correctly.

Fixes Github Issue #632

Reviewed By: bottler

Differential Revision: D29116150

fbshipit-source-id: fae82a0b4848c84b3ed7c7b04ef5c9848352cf5c
2021-06-15 07:51:12 -07:00
Nikhila Ravi
bc8361fa47 Lighting broadcasting bug fix
Summary: Fixed multiple issues with shape broadcasting in lighting, shading and blending and updated the tests.

Reviewed By: bottler

Differential Revision: D28997941

fbshipit-source-id: d3ef93f979344076b1d9098a86178b4da63844c8
2021-06-14 11:48:27 -07:00
Patrick Labatut
780e231536 Increase code coverage of subdivide_meshes
Summary: Increase code coverage of subdivide_meshes and re-include it in code coverage test

Reviewed By: bottler

Differential Revision: D29097476

fbshipit-source-id: 3403ae38a90c4b53f24188eed11faae202a235b5
2021-06-14 04:02:59 -07:00
Nikhila Ravi
a0f79318c5 Culling to frustrum bug fix
Summary:
When `z_clip_value = None` and faces are outside the view frustum the shape of one of the tensors in `clip.py` is incorrect.

`faces_num_clipped_verts` should be (F,) but it was (F,3).  Added a new test to ensure this case is handled.

Reviewed By: bottler

Differential Revision: D29051282

fbshipit-source-id: 5f4172ba4d4a75d928404dde9abf48aef18c68bd
2021-06-11 14:33:40 -07:00
Nikhila Ravi
ef16253953 textures dimension check
Summary:
When textures are set on `Meshes` we need to check if the dimensions actually match that of the verts/faces in the mesh. There was a github issue where someone tried to set the attribute after construction of the `Meshes` object and ran into an error when trying to sample textures.

The desired usage is to initialize the class with the textures (not set an attribute afterwards) but in any case we need to check the dimensions match before sampling textures.

Reviewed By: bottler

Differential Revision: D29020249

fbshipit-source-id: 9fb8a5368b83c9ec53652df92b96fc8b2613f591
2021-06-11 13:38:46 -07:00
Patrick Labatut
44508ed0db Make Transform3d.to() not ignore dtype
Summary: Make Transform3d.to() not ignore a different dtype when device is the same and no copy is requested. Fix other methods where dtype is ignored.

Reviewed By: nikhilaravi

Differential Revision: D28981171

fbshipit-source-id: 4528e6092f4a693aecbe8131ede985fca84e84cf
2021-06-09 15:50:09 -07:00
Patrick Labatut
1f9661e150 Tidy uses of torch.device in Volumes
Summary:
Tidy uses of `torch.device` in `Volumes`:
- Allow `str` or `torch.device` in `Volumes.to()` method
- Consistently use `torch.device` for internal type
- Fix comparison of devices

Reviewed By: nikhilaravi

Differential Revision: D28970876

fbshipit-source-id: c640cc22ced684a54cc450ac38a0f4b3435d47be
2021-06-09 15:50:09 -07:00
Patrick Labatut
1db40ac566 Tidy uses of torch.device in Pointclouds
Summary:
Tidy uses of `torch.device` in `Pointclouds`:
- Allow `str` or `torch.device` in `Pointclouds.to()` method
- Consistently use `torch.device` for internal type
- Fix comparison of devices

Reviewed By: nikhilaravi

Differential Revision: D28970221

fbshipit-source-id: 3ca7104d4c0d9b20b0cff4f00e3ce931c5f1528a
2021-06-09 15:50:09 -07:00
Patrick Labatut
633d66f1f0 Tidy uses of torch.device in Meshes
Summary:
Tidy uses of `torch.device` in `Meshes`:
- Allow `str` or `torch.device` in `Meshes.to()` method
- Consistently use `torch.device` for internal type
- Fix comparison of devices

Reviewed By: nikhilaravi

Differential Revision: D28969461

fbshipit-source-id: 16d3c1f5458954bb11fdf0efea88542e94dccd7a
2021-06-09 15:50:09 -07:00
Patrick Labatut
13a0110b69 Tidy uses of torch.device in Transform3d
Summary:
Tidy uses of `torch.device` in `Transforms3d`:
- Allow `str` or `torch.device` in user-facing methods
- Consistently use `torch.device` for internal types
- Fix comparison of devices

Reviewed By: nikhilaravi

Differential Revision: D28929486

fbshipit-source-id: bd1d6cc7ede3d8fd549fd3224a9b07eec53f8164
2021-06-09 15:50:09 -07:00
Nikhila Ravi
a15c33a3cc Alpha channel to return the mask
Summary: Updated the alpha channel in the `hard_rgb_blend` function to return the mask of the pixels which have overlapping mesh faces.

Reviewed By: bottler

Differential Revision: D29001604

fbshipit-source-id: 22a2173d769f2d3ad34892d68ceb628f073bca22
2021-06-09 15:06:53 -07:00
Jeremy Reizenstein
7204a4ca64 cuda 11 problems with test_normal_consistency
Summary:
One test hits problems with CUDA 11.1 and pytorch 1.8. This seems to be a known bug, so we just run that test on the cpu in the problematic cases.

Note - the full test run is much slower with cuda 11.1 than 10.2, but this is known.

Reviewed By: patricklabatut

Differential Revision: D28938933

fbshipit-source-id: cf8ed84cd10a0b52d8f4292edbef7bd4844fea65
2021-06-08 02:52:06 -07:00
Jeremy Reizenstein
36b451a49b Test failures print message in assertNormsClose
Summary: Restore assertNormsClose's printing of its message on failure which I broke in D26233419 (cd9786e787).

Reviewed By: nikhilaravi

Differential Revision: D28799743

fbshipit-source-id: e7a24b2558b68991c731bbd55fb3ca6c1df98f69
2021-06-03 18:30:54 -07:00
Jeremy Reizenstein
070ec550d3 Always print message on test failure
Summary: make assertClose print its failure information even if a message is supplied.

Reviewed By: nikhilaravi

Differential Revision: D28799745

fbshipit-source-id: 787c8c356342420cd8f40fdc0b2aba036142298e
2021-06-03 18:30:54 -07:00
Jeremy Reizenstein
ed6983ea84 Experimental glTF reading
Summary: Experimental data loader for taking the default scene from a GLB file and converting it to a single mesh in PyTorch3D.

Reviewed By: nikhilaravi

Differential Revision: D25900167

fbshipit-source-id: bff22ac00298b83a0bd071ae5c8923561e1d81d7
2021-05-26 04:54:19 -07:00
Jeremy Reizenstein
0e85652f07 ambient lighting
Summary:
Specific object to represent light which is 100% everywhere. Sometimes lighting is irrelevant, for example when viewing a mesh which has lighting already baked in.

This is not primarily aiming for a performance win but I think the test which has changed might be a bit faster.

Reviewed By: theschnitz

Differential Revision: D26405151

fbshipit-source-id: 82eae55de0bee918548a3eaf031b002cb95e726c
2021-05-26 04:54:19 -07:00
Jeremy Reizenstein
61e38de034 rotate_on_spot
Summary: Function to relatively rotate a camera position. Also document how to relatively translate a camera position.

Reviewed By: theschnitz

Differential Revision: D25900166

fbshipit-source-id: 2ddaf06ee7c5e2a2e973c04d7dee6ccb61c6ff84
2021-05-26 04:54:19 -07:00
Jeremy Reizenstein
e12a08133f Deduplicate texture maps when joining
Summary:
If you join several meshes which have TexturesUV textures using join_meshes_as_scene then we amalgamate all the texture images in to a single one. This now checks if some of the images are equal (i.e. the tensors are the same tensor, in the `is` sense; they have the same `id` in Python) and only uses one copy if they are.

I have an example of a massive scene made of several textured meshes with some shared, where this makes the difference between fitting the data on the GPU and not.

Reviewed By: theschnitz

Differential Revision: D25982364

fbshipit-source-id: a8228805f38475c796302e27328a340d9b56c8ef
2021-05-26 04:54:19 -07:00
Jeremy Reizenstein
0ca839cc32 avoid running tests twice
Summary: Avoid test files explicitly importing TestCase objects from each other, because doing so causes the tests to be discovered twice by unittest discover. This means moving a few static functions out of their classes. I noticed this while trying to fix failures from yesterday.

Reviewed By: nikhilaravi

Differential Revision: D28194679

fbshipit-source-id: ac6e6585603bd4ef9c098cdd56891d94f8923ba6
2021-05-07 05:04:08 -07:00
Jeremy Reizenstein
e3624b4e9d test_meshes numeric fixes
Summary:
A couple of tests are failing in open source after my changes yesterday because of numerical issues calculating normals. In particular we have meshes with very few vertices and several faces, where the normals should be zero but end up non-negligible after F.normalize. I have no idea why the different environments produce different results, so that the tests are passing internally.

An example. Consider a mesh with the following faces:
```
tensor([[4, 0, 2],
        [4, 1, 2],
        [3, 1, 0],
        [1, 3, 1],
        [3, 0, 1],
        [4, 0, 0],
        [4, 0, 2]])
```
At vertex 3, there is one zero-area face and there are two other faces, which are back-to-back with each other. This vertex should have zero normal. The open source calculation produces a small but nonzero normal which varies unpredictably with changes in scale/offset, which can cause test failures.

In this diff, the main change is to increase the number of vertices to make this less likely to happen. Also a small change to init_mesh to make it not generate a batch of empty meshes.

Reviewed By: nikhilaravi

Differential Revision: D28220984

fbshipit-source-id: 79fdc62e5f5f8836de5a3a9980cfd6fe44590359
2021-05-07 05:04:08 -07:00
Daisy
5241b7dd4e Handle header has no newline or space after OFF. (#665)
Summary:
Allow a line like `OFF10 10 10` as the start of an OFF file. Such things apparently occur in ModelNet40.

This resolves https://github.com/facebookresearch/pytorch3d/issues/663.

Pull Request resolved: https://github.com/facebookresearch/pytorch3d/pull/665

Test Plan: New test

Reviewed By: nikhilaravi

Differential Revision: D28180006

Pulled By: bottler

fbshipit-source-id: 7f474c6a262e32da012217e09f76e8672a7f0278
2021-05-07 05:01:58 -07:00
Jeremy Reizenstein
6fa66f5534 PLY load normals
Summary: Add ability to load normals when they are present in a PLY file.

Reviewed By: nikhilaravi

Differential Revision: D26458971

fbshipit-source-id: 658270b611f7624eab4f5f62ff438038e1d25723
2021-05-04 05:36:51 -07:00
Jeremy Reizenstein
66b97a0c28 has_verts_normals for Meshes
Summary: Add ability to ask a Meshes if it already has normals. If it does, then requesting normals will not trigger a calculation. MeshesFormatInterpreters will therefore be able to decide whether to save normals.

Reviewed By: theschnitz, nikhilaravi

Differential Revision: D27765261

fbshipit-source-id: 7c87dbf999d5616d20f5eb2c01039ee5ff65a830
2021-05-04 05:36:51 -07:00
Jeremy Reizenstein
2bbca5f2a7 Allow setting verts_normals on Meshes
Summary: Add ability to set the vertex normals when creating a Meshes, so that the pluggable loaders can return them from a file.

Reviewed By: nikhilaravi

Differential Revision: D27765258

fbshipit-source-id: b5ddaa00de3707f636f94d9f74d1da12ecce0608
2021-05-04 05:36:51 -07:00
Jeremy Reizenstein
502f15aca7 avoid recalculating normals for simple move
Summary: If offset_verts_ is used to move meshes with a single vector, the normals won't change so don't need to recalculate. I am planning to allow user-specified vertex normals. This change means that user-specified vertex normals won't get overwritten when they don't need to be.

Reviewed By: nikhilaravi

Differential Revision: D27765256

fbshipit-source-id: f6e4d308ac9ac023030325cb75a18d39b966cf88
2021-05-04 05:36:51 -07:00
Jeremy Reizenstein
e9f4e0d086 PLY color scaling
Summary: When a PLY file contains colors in byte format, these are now scaled from 0..255 to [0,1], as they should be

Reviewed By: gkioxari

Differential Revision: D27765254

fbshipit-source-id: 526b5f5149d5e8cbffd7412b411be52c935fa4ad
2021-05-04 05:36:51 -07:00
Jeremy Reizenstein
6c3fe952d1 PLY TexturesVertex loading
Summary:
Include TexturesVertex colors when loading and saving Meshes to PLY files.

A couple of other improvements to the internals of ply_io, including using `None` instead of empty tensors for some missing data.

Reviewed By: gkioxari

Differential Revision: D27765260

fbshipit-source-id: b9857dc777c244b9d7d6643b608596d31435ecda
2021-05-04 05:36:51 -07:00
JudyYe
eb04a488c5 TexturesVertex._num_verts_per_mesh deep copy (#623)
Summary:
When a list of Meshes is `join_batched()`, the `num_verts_per_mesh` in the list would be unexpectedly modified.

Also some cleanup around `_num_verts_per_mesh`.

Pull Request resolved: https://github.com/facebookresearch/pytorch3d/pull/623

Test Plan: A modification to an existing test checks this.

Reviewed By: nikhilaravi

Differential Revision: D27682104

Pulled By: bottler

fbshipit-source-id: 9d00913dfb4869bd6c7d3f5cc9156b7b6f1aecc9
2021-04-20 03:11:34 -07:00
Jeremy Reizenstein
c18ee9d40a lint fixes
Summary: Lint after recent changes.

Reviewed By: nikhilaravi

Differential Revision: D27682328

fbshipit-source-id: 285d159010d886e4e97902995adbdff875fd3c19
2021-04-12 19:10:18 -07:00
Jeremy Reizenstein
124bb5e391 spelling
Summary: Collection of spelling things, mostly in docs / tutorials.

Reviewed By: gkioxari

Differential Revision: D26101323

fbshipit-source-id: 652f62bc9d71a4ff872efa21141225e43191353a
2021-04-09 09:58:54 -07:00
Rong Rong (AI Infra)
c2e62a5087 Allow tests to be run on GPU with remote execution
Summary: Test path special case

Reviewed By: bottler

Differential Revision: D27566817

fbshipit-source-id: c7b3ac839908c071f1378a37b7013b91ca4e8b18
2021-04-08 20:03:04 -07:00
Rong Rong (AI Infra)
dd8343922e Get rid of duplicate test data directory initialization
Summary: Simplify finding the data directories in the tests.

Reviewed By: nikhilaravi

Differential Revision: D27634293

fbshipit-source-id: dc308a7c86c41e6fae56a2ab58187c9f0335b575
2021-04-08 20:03:04 -07:00
Rong Rong (AI Infra)
1216b5765a Extract finding directories for test data
Summary: Make common functions for finding directories where test data is found, instead of lots of tests using their own `__file__`  while trying to get ./tests/data and the tutorials data.

Reviewed By: nikhilaravi

Differential Revision: D27633701

fbshipit-source-id: 1467bb6018cea16eba3cab097d713116d51071e9
2021-04-08 20:03:04 -07:00
Jeremy Reizenstein
24ee279005 Fix flake exceptions
Summary: flake8 no longer respects the black fmt:off message, so include specific line-length exclusion.

Reviewed By: nikhilaravi

Differential Revision: D27624641

fbshipit-source-id: adcdb6f55b382fbf252eede3f3ddeda0621da883
2021-04-08 09:38:43 -07:00
Jeremy Reizenstein
cc08c6b288 CI fixes
Summary:
Update `main` build to latest CircleCI image - Ubuntu 2020.04.

Avoid torch.logical_or and logical_and for PyTorch 1.4 compatibility.

Also speed up the test run with Pytorch 1.4.0 (which has no ninja) by not setting NVCC_FLAGS for it.

Reviewed By: theschnitz

Differential Revision: D27262327

fbshipit-source-id: ddc359d134b1dc755f8b20bd3f33bb080cb3a0e1
2021-03-23 14:25:25 -07:00
Jeremy Reizenstein
ff9c6612b4 Use old style isfinite
Summary: Avoid using the newish member function isfinite. We use torch.isfinite instead for torch 1.4.0 compatibility.

Reviewed By: nikhilaravi

Differential Revision: D26946672

fbshipit-source-id: 853c3716f40061152f1ea54a39eb60b565de7c2c
2021-03-11 03:13:48 -08:00
Nikhila Ravi
13429640d3 Bug fix for case where aspect ratio is a float
Summary:
- Fix the calculation of the non square NDC range when the H and W are not integer multiples.
- Add test for this case

Reviewed By: gkioxari

Differential Revision: D26613213

fbshipit-source-id: df6763cac602e9f1d516b41b432c4d2cfbaa356d
2021-02-24 10:07:17 -08:00
Jeremy Reizenstein
0345f860d4 Loading/saving meshes to OFF files.
Summary: Implements the ascii OFF file format. This was discussed in https://github.com/facebookresearch/pytorch3d/issues/216

Reviewed By: theschnitz

Differential Revision: D25788834

fbshipit-source-id: c141d1f4ba3bad24e3c1f280a20aee782bfd74d6
2021-02-12 07:05:55 -08:00
Jeremy Reizenstein
5ac2f42184 test & compilation fixes
Summary:
Fixes mostly related to the "main" build on circleci.
-Avoid error to do with tuple copy from initializer_list which is `explicit` on old compiler.
-Add better reporting to copyright test.
-Move to PackedTensorAccessor64 from the deprecated PackedTensorAccessor
-Avoid some warnings about mismatched comparisons.

The "main" build is the only one that runs the test_build stuff. In that area
-Fix my bad copyright fix D26275931 (3463f418b8) / 965c9c
-Add test that all tutorials are valid json.

Reviewed By: nikhilaravi

Differential Revision: D26366466

fbshipit-source-id: c4ab8b7e6647987069f7cb7144aa6ab7c24bcdac
2021-02-11 11:06:08 -08:00
Shubham Goel
e13e63a811 bugfix in cotcurv laplacian loss. closes #551 (#553)
Summary: Pull Request resolved: https://github.com/facebookresearch/pytorch3d/pull/553

Reviewed By: theschnitz

Differential Revision: D26257591

Pulled By: gkioxari

fbshipit-source-id: 899a3f733a77361e8572b0900a34b55764ff08f2
2021-02-10 21:23:17 -08:00
Nikhila Ravi
340662e98e CUDA/C++ Rasterizer updates to handle clipped faces
Summary:
- Updated the C++/CUDA mesh rasterization kernels to handle the clipped faces. In particular this required careful handling of the distance calculation for faces which are cut into a quadrilateral by the image plane and then split into two sub triangles i.e. both sub triangles can't be part of the top K faces.
- Updated `rasterize_meshes.py` to use the utils functions to clip the meshes and convert the fragments back to in terms of the unclipped mesh
- Added end to end tests

Reviewed By: jcjohnson

Differential Revision: D26169685

fbshipit-source-id: d64cd0d656109b965f44a35c301b7c81f451cfa0
2021-02-08 14:32:39 -08:00
Nikhila Ravi
838b73d3b6 Updates to cameras and rasterizer to infer camera type correctly
Summary: Small update to the cameras and rasterizer to correctly infer the type of camera (perspective vs orthographic).

Reviewed By: jcjohnson

Differential Revision: D26267225

fbshipit-source-id: a58ed3bc2ab25553d2a4307c734204c1d41b5176
2021-02-08 14:32:39 -08:00
Nikhila Ravi
23279c5f1d Utils for clipping mesh faces partially behind the image plane
Summary:
Instead of culling faces behind the camera, partially clip them if they intersect with the image plane.

This diff implements the utils functions for clipping.

There are 4 cases for the mesh faces which are all handled:

```
Case 1: the triangle is completely in front of the clipping plane (it is left
        unchanged)
Case 2: the triangle is completely behind the clipping plane (it is culled)
Case 3: the triangle has exactly two vertices behind the clipping plane (it is
        clipped into a smaller triangle)
Case 4: the triangle has exactly one vertex behind the clipping plane (it is clipped
        into a smaller quadrilateral and divided into two triangular faces)
```

Reviewed By: jcjohnson

Differential Revision: D23108673

fbshipit-source-id: 550a8b6a982d06065dff10aba10d47e8b144ae52
2021-02-05 18:30:15 -08:00
Jeremy Reizenstein
cd9786e787 Disable random-dependent tests
Summary:
These two tests fail (with non-small differences) when the seed is changed or if certain environmental changes are made. We disable them pending investigation.

A small change to the tolerance at the failing assertion doesn't help. The change in common_testing helps diagnose this.

Reviewed By: shapovalov

Differential Revision: D26233419

fbshipit-source-id: 357afc1786825256c9bade101fb15707e4dea5ed
2021-02-03 18:24:27 -08:00
Jeremy Reizenstein
e42b0c4f70 Mesh normal consistency when no faces intersect
Summary: Corner case where there's nothing to do in this function.

Reviewed By: nikhilaravi

Differential Revision: D26073476

fbshipit-source-id: eb061683ffe35c1ffa8384c422a1557a636d52cd
2021-01-27 17:32:16 -08:00
Jeremy Reizenstein
7f62eacdb2 Mesh normal consistency when many faces intersect
Summary: We were double counting some pairs in some cases. Specifically if four or more faces share an edge, then some of them were getting double counted. This is a minimal tweak to avoid that.

Reviewed By: nikhilaravi

Differential Revision: D26073477

fbshipit-source-id: a40032acf3044bb98dd91cb29904614ef64d5599
2021-01-27 17:32:16 -08:00
Jeremy Reizenstein
d173a2f8da textures device consistency
Summary: Ensure that `mesh2 = mesh.to(device)` doesn't change the device of `mesh.textures`.

Reviewed By: nikhilaravi

Differential Revision: D25978610

fbshipit-source-id: 0558cd62132965d8693ebeea05e42b8c1d16cfbf
2021-01-25 06:09:56 -08:00
Jeremy Reizenstein
cf9bb7c48c (eye, at, up) extraction function
Summary:
Plotly viewing from a specific camera location requires converting that location in to an (eye, at, up) specification. There may be other reasons to want to do this as well. I create a separate utility function for it.

I envisage more such utility functions for manipulating camera information, so I create a separate camera_utils.py file for such things.

Reviewed By: nikhilaravi

Differential Revision: D25981184

fbshipit-source-id: 0947bf98b212676c021f2fddf775bf436dee3487
2021-01-22 07:33:31 -08:00
Jeremy Reizenstein
ddebdfbcd7 Allow single offset in offset_verts
Summary:
It is common when trying things out to want to move a whole mesh or point cloud by the same amount. Here we allow the offset functions to broadcast.

Also add a sanity check to join_meshes_as_scene which it is easy to call wrongly.

Reviewed By: nikhilaravi

Differential Revision: D25980593

fbshipit-source-id: cdf1568e1317e3b81ad94ed4e608ba7eef81290b
2021-01-22 07:33:31 -08:00
Jeremy Reizenstein
d60c52df4a devices for transform3d
Summary: Make `to` on Transform3D carry its member _transforms.

Reviewed By: nikhilaravi

Differential Revision: D25978611

fbshipit-source-id: 12b39e7a657f28d59ca60800bf9f4193a2c08197
2021-01-21 04:58:40 -08:00