<commit_before>#include <iostream>
#include "implicit.h"
#include "test_util.h"

scalar sphere_sdf(const Vec3& p)
{
  return p.norm() - 1.0;
}

auto sphere_grad = gradient_from_sdf(sphere_sdf);

TEST(sphere_implicit)
{
  ImplicitSurface imp(sphere_sdf, sphere_grad, 1.0);
  UniformSampler samp;
  for (int i = 0; i < 100; ++i)
  {
    Vec3 n = cosine_weighted_hemisphere_sample(samp.sample_2d());
    if (n.z > 0)
      n = -n;
    Ray r(Vec3::z_axis * sqrt(3), n);

    scalar t = imp.intersect(r, SCALAR_MAX);
    scalar angle = acos(n.dot(-Vec3::z_axis));
    if (angle < PI/6.0-0.1)
      CHECK(t > 0);
    else if (angle > PI/6.0+0.1)
      CHECK(t < 0);
  }
}<commit_msg>Randomizes testing of sphere implicits<commit_after>#include <iostream>
#include "implicit.h"
#include "test_util.h"
#include "sphere.h"

scalar sphere_sdf(const Vec3& p)
{
  return p.norm() - 1.0;
}

auto sphere_grad = gradient_from_sdf(sphere_sdf);

TEST(sphere_implicit)
{
  ImplicitSurface imp(sphere_sdf, sphere_grad, 1.0);
  Sphere sp(Vec3::zero, 1.0);

  UniformSampler samp;
  for (int i = 0; i < 100; ++i)
  {
    scalar A = rf() * 2 + 0.1;

    Vec3 n = cosine_weighted_hemisphere_sample(samp.sample_2d());

    Ray ray(Vec3::z_axis * -1.0 * (1.0 + A), n);

    scalar crit_angle = asin(1.0 / (1.0 + A));

    scalar angle = acos(n.dot(Vec3::z_axis));

    scalar imp_t = imp.intersect(ray);
    scalar sp_t = sp.intersect(ray);

    if (angle > crit_angle+0.001)
    {
      CHECK(imp_t < 0);
      CHECK(sp_t < 0);
    }
    if (angle < crit_angle - 0.001)
    {
      scalar a = 1.0, b = -2 * n.z * (A + 1.0);
      scalar c = (1.0 + A) * (1.0 + A) - 1;

      scalar t_analytic = qf(a, b, c);

      CHECK_CLOSE(t_analytic, imp_t, EPS);
      CHECK_CLOSE(t_analytic, sp_t, EPS);
    }
  }
}
{'fraction_non_alphanumeric': 0.1639528354856822, 'fraction_numerical': 0.04042672655811342, 'mean_word_length': 2.9078947368421053, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4899423', 'n_tokens_mistral': 817, 'n_tokens_neox': 756, 'n_words': 164}