1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2018 Google LLC
//
// 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.

//! Implements a [Texture] that always returns the given value.
//!
//! [Texture]: crate::core::texture::Texture
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Result;

use crate::core::interaction::SurfaceInteraction;
use crate::core::paramset::TextureParams;
use crate::core::spectrum::Spectrum;
use crate::core::texture::Texture;
use crate::core::transform::Transform;
use crate::Float;

#[derive(Clone)]
/// Implements trait [Texture] to return the given `value`.
///
/// [Texture]: crate::core::texture::Texture
pub struct ConstantTexture<T>
where
    T: Debug,
{
    value: T,
}

/// Creates new `ConstantTexture` from the given `TextureParams` with `Float` as the data type.
///
/// # Examples
/// ```
/// use pbrt::core::paramset::testutils::make_float_param_set;
/// use pbrt::core::paramset::TextureParams;
/// use pbrt::core::texture::Texture;
/// use pbrt::core::transform::Transform;
/// use pbrt::textures::constant::create_constant_float_texture;
///
/// let tp = TextureParams::new(
///     make_float_param_set("value", vec![10.]),
///     Default::default(),
///     Default::default(),
///     Default::default(),
/// );
/// let t = create_constant_float_texture(&Transform::identity(), &tp);
/// assert_eq!(10., t.evaluate(&Default::default()));
/// ```
pub fn create_constant_float_texture(
    _tex2world: &Transform,
    tp: &TextureParams,
) -> ConstantTexture<Float> {
    ConstantTexture {
        value: tp.find_float("value", 1.),
    }
}

/// Creates new `ConstantTexture` from the given `TextureParams` with `Spectrum` as the data type.
///
/// # Examples
/// ```
/// use pbrt::core::paramset::testutils::make_spectrum_param_set;
/// use pbrt::core::paramset::TextureParams;
/// use pbrt::core::spectrum::Spectrum;
/// use pbrt::core::texture::Texture;
/// use pbrt::core::transform::Transform;
/// use pbrt::textures::constant::create_constant_spectrum_texture;
///
/// let tp = TextureParams::new(
///     make_spectrum_param_set("value", vec![Spectrum::from_rgb([1., 0., 0.])]),
///     Default::default(),
///     Default::default(),
///     Default::default(),
/// );
/// let t = create_constant_spectrum_texture(&Transform::identity(), &tp);
/// assert_eq!(
///     Spectrum::from_rgb([1., 0., 0.]),
///     t.evaluate(&Default::default())
/// );
/// ```
pub fn create_constant_spectrum_texture(
    _tex2world: &Transform,
    tp: &TextureParams,
) -> ConstantTexture<Spectrum> {
    ConstantTexture {
        value: tp.find_spectrum("value", Spectrum::from(1.)),
    }
}

impl<T> ConstantTexture<T>
where
    T: Debug,
{
    /// Create a new `ConstantTexture` with the given constant `value`.
    ///
    /// # Examples
    /// ```
    /// use pbrt::core::spectrum::Spectrum;
    /// use pbrt::core::texture::Texture;
    /// use pbrt::textures::constant::ConstantTexture;
    ///
    /// let t = ConstantTexture::new(10.);
    /// assert_eq!(10., t.evaluate(&Default::default()));
    ///
    /// let t = ConstantTexture::new(Spectrum::from_rgb([1., 0., 0.]));
    /// assert_eq!(
    ///     Spectrum::from_rgb([1., 0., 0.]),
    ///     t.evaluate(&Default::default())
    /// );
    /// ```
    pub fn new(value: T) -> ConstantTexture<T> {
        ConstantTexture { value }
    }
}

impl<T> Texture<T> for ConstantTexture<T>
where
    T: Clone + Debug,
{
    /// Implements [evaluate] that just returns the same value for any `SurfaceInteraction`
    ///
    /// [evaluate]: crate::core::texture::Texture
    fn evaluate(&self, _si: &SurfaceInteraction) -> T {
        self.value.clone()
    }
}

impl<T> Debug for ConstantTexture<T>
where
    T: Debug,
{
    /// Implements [fmt] that surfaces the relevant details for the `ConstantTexture`.
    ///
    /// [fmt]: std::fmt::Debug::fmt
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        write!(f, "ConstantTexture{{{:?}}}", &self.value)
    }
}