<commit_before>/*
 * $Id: transport.c,v 1.8 2004/08/15 05:19:39 syoyo Exp $
 *
 * Light transport routine.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "vector.h"
#include "log.h"
#include "memory.h"
#include "render.h"
#include "raytrace.h"
#include "reflection.h"
#include "random.h"
#include "transport.h"
#include "shading.h"

static void trace_path(ri_render_t *render, ri_ray_t *ray, ri_transport_info_t *result);

static ri_light_t *get_light(ri_render_t *render);


/*
 * Function: ri_transport_sample
 *
 *     Triggers light transporter and computes radiance along the ray.
 *
 * Parameters:
 *
 *     *render - The global renderer data.
 *     *ray    - The ray originating camera which throughs pixel plane.
 *     *resut  - Light transport result(including radiance).
 *
 * Returns:
 *
 *     Always 1.
 */
int
ri_transport_sample(
    ri_render_t         *render,
    const ri_ray_t      *ray,
    ri_transport_info_t *result)
{
    ri_ray_t newray;

    memcpy(&newray, ray, sizeof(ri_ray_t));

    ri_vector_setzero(result->radiance);
    result->nbound_diffuse = 0;
    result->nbound_specular = 0;
    result->state.inside = 0;

    trace_path(render, &newray, result);

    return 1;
}


/* --- private functions --- */

/*
 * Function: get_light
 *
 *     Returns the light in the scene. In current implementation, only one
 *     light is considered.
 *
 * Parameters:
 *
 *     *render - The global renderer data.
 *
 * Returns:
 *
 *     An light information.
 */
static ri_light_t *
get_light(ri_render_t *render)
{
    ri_light_t *light;

    if (!ri_list_first(render->scene->light_list)) {
        ri_log(LOG_WARN, "no light exist.\n");
        return NULL;
    }

    light = (ri_light_t *)ri_list_first(render->scene->light_list)->data;

    return light;
}

/*
 * Function: trace_path
 *
 *     Samples light transport path in recursive manner. In curent
 *     implementation, <trace_path> acts as distribution ray tracing.
 *
 * Parameters:
 *
 *     *render - The global renderer data.
 *     *ray    - The ray to be traced.
 *     *resut  - Light transport result(including radiance).
 *
 * Returns:
 *
 *     None.
 */
static void
trace_path(ri_render_t *render, ri_ray_t *ray, ri_transport_info_t *result)
{
#if 0 // TODO
    int hit, lighthit;
    int hasfresnel;
    ri_vector_t col;
    ri_vector_t transmit_ray;
    ri_vector_t reflect_ray;
    ri_vector_t offset;
    ri_vector_t raydir;
    ri_vector_t rayorg;
    ri_vector_t refrad, trasrad;
    ri_vector_t normal;<commit_msg>Refacor source.<commit_after>/*
 * $Id: transport.c,v 1.8 2004/08/15 05:19:39 syoyo Exp $
 *
 * Light transport routine.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "vector.h"
#include "log.h"
#include "memory.h"
#include "render.h"
#include "raytrace.h"
#include "reflection.h"
#include "random.h"
#include "transport.h"
#include "shading.h"

static void          trace_path(
    ri_render_t         *render,
    ri_ray_t            *ray, 
    ri_transport_info_t *result);

static ri_light_t   *get_light(
    ri_render_t         *render);


/*
 * Function: ri_transport_sample
 *
 *     Triggers light transporter and computes radiance along the ray.
 *
 * Parameters:
 *
 *     *render - The global renderer data.
 *     *ray    - The ray originating camera which throughs pixel plane.
 *     *resut  - Light transport result(including radiance).
 *
 * Returns:
 *
 *     Always 1.
 */
int
ri_transport_sample(
    ri_render_t         *render,
    const ri_ray_t      *ray,
    ri_transport_info_t *result)
{
    ri_ray_t newray;

    memcpy(&newray, ray, sizeof(ri_ray_t));     /* copy */

    /*
     * Initialize
     */
    {
        ri_vector_setzero(result->radiance);
        result->nbound_diffuse  = 0;
        result->nbound_specular = 0;
        result->state.inside    = 0;
    }    


    trace_path(render, &newray, result);

    return 1;
}


/* ---------------------------------------------------------------------------
 *
 * Private functions 
 *
 * ------------------------------------------------------------------------ */

/*
 * Function: get_light
 *
 *     Returns the light in the scene. In current implementation, only one
 *     light is considered.
 *
 * Parameters:
 *
 *     *render - The global renderer data.
 *
 * Returns:
 *
 *     An light information.
 */
static ri_light_t *
get_light(ri_render_t *render)
{
    ri_light_t *light;

    if (!ri_list_first(render->scene->light_list)) {
        ri_log(LOG_WARN, "no light exist.\n");
        return NULL;
    }

    light = (ri_light_t *)ri_list_first(render->scene->light_list)->data;

    return light;
}

/*
 * Function: trace_path
 *
 *     Samples light transport path in recursive manner. In curent
 *     implementation, <trace_path> acts as distribution ray tracing.
 *
 * Parameters:
 *
 *     *render - The global renderer data.
 *     *ray    - The ray to be traced.
 *     *resut  - Light transport result(including radiance).
 *
 * Returns:
 *
 *     None.
 */
static void
trace_path(ri_render_t *render, ri_ray_t *ray, ri_transport_info_t *result)
{
    int max_nbound_specular = 10;

    if (result->nbound_specular > max_nbound_specular) {
        /* Too much reflection, terminate.  */
        return;
    }

#if 0 // TODO
    int hit, lighthit;
    int hasfresnel;
    ri_vector_t col;
    ri_vector_t transmit_ray;
    ri_vector_t reflect_ray;
    ri_vector_t offset;
    ri_vector_t raydir;
    ri_vector_t rayorg;
    ri_vector_t refrad, trasrad;
    ri_vector_t normal;
{'fraction_non_alphanumeric': 0.1382156656129259, 'fraction_numerical': 0.008078679311556024, 'mean_word_length': 2.5460772104607723, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, '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': '6928113', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1915, 'n_words': 477}