Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move out common functions to utils.py
PiperOrigin-RevId: 361885696
- Loading branch information
1 parent
5a584e4
commit d8e7c04
Showing
3 changed files
with
86 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2020 The TensorFlow Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Tests for common util functions.""" | ||
|
||
from absl.testing import parameterized | ||
import tensorflow as tf | ||
|
||
from tensorflow_graphics.rendering import utils | ||
from tensorflow_graphics.util import test_case | ||
|
||
|
||
class UtilsTest(test_case.TestCase): | ||
|
||
@parameterized.named_parameters( | ||
('non-batched xyz', False), | ||
('batched xyz', True), | ||
) | ||
def test_transform_homogeneous_shapes(self, do_batched): | ||
num_vertices = 10 | ||
batch_size = 3 | ||
num_channels = 3 | ||
vertices_shape = ([batch_size, num_vertices, num_channels] | ||
if do_batched else [num_vertices, num_channels]) | ||
vertices = tf.ones(vertices_shape, dtype=tf.float32) | ||
matrices = tf.eye(4, dtype=tf.float32) | ||
if do_batched: | ||
matrices = tf.tile(matrices[tf.newaxis, ...], [batch_size, 1, 1]) | ||
|
||
transformed = utils.transform_homogeneous(matrices, vertices) | ||
|
||
expected_shape = ([batch_size, num_vertices, 4] | ||
if do_batched else [num_vertices, 4]) | ||
self.assertEqual(transformed.shape, expected_shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2020 The TensorFlow Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Various util functions common for all rasterizers.""" | ||
|
||
import tensorflow as tf | ||
|
||
|
||
def transform_homogeneous(matrices, vertices): | ||
"""Applies 4x4 homogenous matrix transformations to xyz vertices. | ||
The vertices are input and output as as row-major, but are interpreted as | ||
column vectors multiplied on the right-hand side of the matrices. More | ||
explicitly, this function computes (MV^T)^T where M represents transformation | ||
matrices and V stands for vertices. | ||
Since input vertices are xyz they are extended to xyzw with w=1. | ||
Args: | ||
matrices: A tensor of shape `[batch, 4, 4]` containing batches of view | ||
projection matrices. | ||
vertices: A tensor of shape `[batch, num_vertices, 3]` containing batches of | ||
vertices, each defined by a 3D point. | ||
Returns: | ||
A [batch, N, 4] Tensor of xyzw vertices. | ||
""" | ||
homogeneous_coord = tf.ones_like(vertices[..., 0:1]) | ||
vertices = tf.concat([vertices, homogeneous_coord], -1) | ||
|
||
return tf.matmul(vertices, matrices, transpose_b=True) |