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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Project: Approximation and Finite Elements in Isogeometric Problems
 * Author:  Luca Carlon
 * Date:    2021.12.11
 *
 * Copyright (c) 2021 Luca Carlon. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

use crate::core::{RealPoint, RealPoint2d};
use crate::core::Point;
use crate::core::RowVector;
use crate::core::MatElement;
use crate::core::RealRange;

///
/// Generic interface for an evaluatable element from ℝ^DIMDOM to ℝ^DIMCOD.
///
pub trait Mapping<I: MatElement, O: MatElement, const DIMDOM: usize, const DIMCOD: usize> {
    ///
    /// Evaluates the function.
    /// 
    fn evaluate(&self, i: &Point<I, DIMDOM>) -> Point<O, DIMCOD> {
        let mut output = Point::<O, DIMCOD>::origin();
        self.evaluate_fill(&i, &mut output).clone()
    }

    ///
    /// Evaluates the function and assigns the values to an existing point
    /// object.
    /// 
    fn evaluate_fill<'a>(&self, i: &Point<I, DIMDOM>, o: &'a mut Point<O, DIMCOD>) -> &'a mut Point<O, DIMCOD>;
}

///
/// This class is used to automate computations for curves.
/// 
pub struct Evaluator<const DIMIN: usize, const DIMOUT: usize, const C: usize> {}

impl<const DIMIN: usize, const DIMOUT: usize, const C: usize> Evaluator<DIMIN, DIMOUT, C> {
    ///
    /// Computes a geometric element for a sequence of points.
    ///
    pub fn evaluate(element: &impl Mapping<f64, f64, DIMIN, DIMOUT>, values: &Vec<RealPoint<DIMIN>>) -> Vec<RealPoint<DIMOUT>> {
        let mut ret: Vec<RealPoint<DIMOUT>> = Vec::new();
        for p in values.iter() {
            ret.push(element.evaluate(p));
        }
        return ret;
    }

    ///
    /// Evalutes a parametric element as a map from ℝ to ℝ^DIMOUT.
    /// 
    pub fn evaluate_parametric_range1d(element: &impl Mapping<f64, f64, 1, DIMOUT>, from: &f64, to: &f64) -> (Vec<RealPoint<1>>, Vec<RealPoint<DIMOUT>>) {
        let rv = RowVector::<f64, C>::evenly_spaced(from, to);
        let values = rv.row_to_vec(0);
        let mut input: Vec<RealPoint<1>> = Vec::new();
        for v in values {
            let p = Point::<f64, 1>::point1d(v);
            input.push(p);
        }

        let mut output: Vec<RealPoint<DIMOUT>> = Vec::new();
        let mut tmp = RealPoint::<DIMOUT>::origin();
        for p in &input {
            output.push(element.evaluate_fill(&p, &mut tmp).clone());
        }

        return (input, output);
    }

    pub fn evaluate_parametric_range2d(element: &impl Mapping<f64, f64, 2, DIMOUT>, r1: &RealRange, r2: &RealRange) -> (Vec<RealPoint2d>, Vec<RealPoint<DIMOUT>>) {
        let r1seq = RowVector::<f64, C>::evenly_spaced(&r1.a, &r1.b).row_to_vec(0);
        let r2seq = RowVector::<f64, C>::evenly_spaced(&r2.a, &r2.b).row_to_vec(0);
        let mut input = Vec::<RealPoint2d>::new();
        for i in &r1seq {
            for j in &r2seq {
                input.push(RealPoint2d::point2d(*i, *j));
            }
        }

        let mut output: Vec<RealPoint<DIMOUT>> = Vec::new();
        let mut tmp = RealPoint::<DIMOUT>::origin();
        for p in &input {
            output.push(element.evaluate_fill(&p, &mut tmp).clone());
        }

        return (input, output);
    }

    ///
    /// Rearranges coords in arrays.
    /// 
    pub fn split_coords<T: MatElement>(mapx: usize, x: &Vec<Point<T, DIMOUT>>, mapy: usize, y: &Vec<Point<T, DIMOUT>>, mapz: usize, z: &Vec<Point<T, DIMOUT>>) -> (Vec<T>, Vec<T>, Vec<T>) {
        let sizes = vec![x.len(), y.len(), z.len()];
        let count = sizes.iter().min().clone();
        match count {
            None => { return (vec![], vec![], vec![]) },
            Some(c) => {
                let mut xvalues = Vec::new();
                let mut yvalues = Vec::new();
                let mut zvalues = Vec::new();
                for i in 0..*c {
                    xvalues.push(x[i].value(mapx));
                    yvalues.push(y[i].value(mapy));
                    zvalues.push(z[i].value(mapz));
                }

                return (xvalues, yvalues, zvalues);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::core::Evaluator;
    use crate::core::RealPoint;

    #[test]
    fn test() {
        env_logger::init();
        let xvalues = vec![
            RealPoint::<1>::point1d(0f64),
            RealPoint::<1>::point1d(1f64),
            RealPoint::<1>::point1d(2f64)
        ];
        let yvalues = vec![
            RealPoint::<1>::point1d(3f64),
            RealPoint::<1>::point1d(4f64),
            RealPoint::<1>::point1d(5f64)
        ];
        let zvalues = vec![
            RealPoint::<1>::point1d(6f64),
            RealPoint::<1>::point1d(7f64),
            RealPoint::<1>::point1d(8f64)
        ];
        let (x, y, z) = Evaluator::<1, 1, 1>::split_coords(0, &xvalues, 0, &yvalues, 0, &zvalues);
        assert_eq!(x.len(), 3);
        assert_eq!(y.len(), 3);
        assert_eq!(z.len(), 3);
        assert_eq!(x[0], 0f64);
        assert_eq!(x[1], 1f64);
        assert_eq!(x[2], 2f64);
        assert_eq!(y[0], 3f64);
        assert_eq!(y[1], 4f64);
        assert_eq!(y[2], 5f64);
        assert_eq!(z[0], 6f64);
        assert_eq!(z[1], 7f64);
        assert_eq!(z[2], 8f64);
    }
}