# Path: ldif/training/loss.py
# def bounding_box_constraint_error(samples, box):
# def shape_element_center_magnitude_loss(model_config, _, structured_implicit):
# def element_center_lowres_grid_direct_loss(model_config, training_example,
#                                            structured_implicit):
# def element_center_lowres_grid_squared_loss(model_config, training_example,
#                                             structured_implicit):
# def element_center_lowres_grid_inside_loss(model_config, training_example,
#                                            structured_implicit):
# def smooth_element_center_lowres_grid_inside_loss(model_config,
#                                                   training_example,
#                                                   structured_implicit):
# def center_variance_loss(model_config, training_example, structured_implicit):  # pylint:disable=unused-argument
# def center_nn_loss(model_config, training_example, structured_implicit):  # pylint:disable=unused-argument
# def inside_box_loss(model_config, _, structured_implicit):
# def shape_element_center_loss(model_config, training_example,
#                               structured_implicit):
# def old_shape_element_center_loss(model_config, training_example,
#                                   structured_implicit):
# def weighted_l2_loss(gt_value, pred_value, weights):
# def sample_loss(model_config, gt_sdf, structured_implicit, global_samples, name,
#                 apply_ucf):
# def uniform_sample_loss(model_config, training_example, structured_implicit):
# def overlap_loss(model_config, training_example, structured_implicit):
# def near_surface_sample_loss(model_config, training_example,
#                              structured_implicit):
# def compute_loss(model_config, training_example, structured_implicit):
# def set_loss(model_config, training_example, structured_implicit):


import numpy as np
import tensorflow as tf
from ldif.training import loss
#     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.
# Lint as: python3
"""Refines a prediction according to an observation using gradient descent."""


# LDIF is an internal package, should be imported last.
# pylint: disable=g-bad-import-order
# pylint: enable=g-bad-import-order


def sample_gradients_wrt_placeholders(model_config, training_example,
                                      prediction, samples):
  """Creates gradient ops for refining a quadric prediction."""
  structured_implicit_ph = prediction.structured_implicit.as_placeholder()
  sample_shape = samples.get_shape().as_list()
  target_shape = samples.get_shape().as_list()
  target_shape[-1] = 1  # The target is a function value, not an xyz position.
  samples_ph = tf.placeholder(tf.float32, shape=sample_shape)
  target_f_ph = tf.placeholder(tf.float32, shape=target_shape)

  # TODO(kgenova): One way to test whether trilinear interpolation is a problem
  # or it's some gradient sharing (?) is to replace this with an on-grid
  # evaluation.


# Path: ldif/training/loss.py
# def bounding_box_constraint_error(samples, box):
# def shape_element_center_magnitude_loss(model_config, _, structured_implicit):
# def element_center_lowres_grid_direct_loss(model_config, training_example,
#                                            structured_implicit):
# def element_center_lowres_grid_squared_loss(model_config, training_example,
#                                             structured_implicit):
# def element_center_lowres_grid_inside_loss(model_config, training_example,
#                                            structured_implicit):
# def smooth_element_center_lowres_grid_inside_loss(model_config,
#                                                   training_example,
#                                                   structured_implicit):
# def center_variance_loss(model_config, training_example, structured_implicit):  # pylint:disable=unused-argument
# def center_nn_loss(model_config, training_example, structured_implicit):  # pylint:disable=unused-argument
# def inside_box_loss(model_config, _, structured_implicit):
# def shape_element_center_loss(model_config, training_example,
#                               structured_implicit):
# def old_shape_element_center_loss(model_config, training_example,
#                                   structured_implicit):
# def weighted_l2_loss(gt_value, pred_value, weights):
# def sample_loss(model_config, gt_sdf, structured_implicit, global_samples, name,
#                 apply_ucf):
# def uniform_sample_loss(model_config, training_example, structured_implicit):
# def overlap_loss(model_config, training_example, structured_implicit):
# def near_surface_sample_loss(model_config, training_example,
#                              structured_implicit):
# def compute_loss(model_config, training_example, structured_implicit):
# def set_loss(model_config, training_example, structured_implicit):


# Path: ldif/inference/refine.py
import numpy as np
import tensorflow as tf
from ldif.training import loss

#     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.
# Lint as: python3
"""Refines a prediction according to an observation using gradient descent."""


# LDIF is an internal package, should be imported last.
# pylint: disable=g-bad-import-order
# pylint: enable=g-bad-import-order


def sample_gradients_wrt_placeholders(model_config, training_example,
                                      prediction, samples):
  """Creates gradient ops for refining a quadric prediction."""
  structured_implicit_ph = prediction.structured_implicit.as_placeholder()
  sample_shape = samples.get_shape().as_list()
  target_shape = samples.get_shape().as_list()
  target_shape[-1] = 1  # The target is a function value, not an xyz position.
  samples_ph = tf.placeholder(tf.float32, shape=sample_shape)
  target_f_ph = tf.placeholder(tf.float32, shape=target_shape)

  # TODO(kgenova): One way to test whether trilinear interpolation is a problem
  # or it's some gradient sharing (?) is to replace this with an on-grid
  # evaluation.


  loss_val = loss.sample_loss(