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
//! Change to origin.

use crate::{
    grid3::*,
    range::RangeBoundsPlus,
};
use mint::Vector3;

/// <0, 0> in the inner grid will be new_origin in this grid.
pub struct Grid3NewOrigin<G> 
where
    G: Grid3,
    <G as Grid3>::XBound: RangeBoundsPlus,
    <G as Grid3>::YBound: RangeBoundsPlus,
    <G as Grid3>::ZBound: RangeBoundsPlus,
{
    inner: G,
    new_origin: Vector3<i32>,
    
    // cache these (arbitrary decision)
    new_xbound: <<G as Grid3>::XBound as RangeBoundsPlus>::Output,
    new_ybound: <<G as Grid3>::YBound as RangeBoundsPlus>::Output,
    new_zbound: <<G as Grid3>::ZBound as RangeBoundsPlus>::Output,
}

impl<G> Grid3NewOrigin<G> 
where
    G: Grid3,
    <G as Grid3>::XBound: RangeBoundsPlus,
    <G as Grid3>::YBound: RangeBoundsPlus,
    <G as Grid3>::ZBound: RangeBoundsPlus,
{
    pub fn new<I>(inner: G, new_origin: I) -> Self 
    where
        I: Into<Vector3<i32>>,
    {
        let new_origin = new_origin.into();
        let new_xbound = inner.x_bound().plus(new_origin.x);
        let new_ybound = inner.y_bound().plus(new_origin.y);
        let new_zbound = inner.z_bound().plus(new_origin.z);
        
        Grid3NewOrigin {
            inner,
            new_origin,
            new_xbound,
            new_ybound,
            new_zbound,
        }
    }
    
    pub fn new_origin<I>(&self) -> I 
    where
        I: From<Vector3<i32>>
    {
        I::from(self.new_origin)
    }
    
    pub fn adjust_coord<I>(&self, coord: I) -> I 
    where
        I: From<Vector3<i32>> + Into<Vector3<i32>>
    {
        let mut coord = coord.into();
        coord.x -= self.new_origin.x;
        coord.y -= self.new_origin.y;
        coord.z -= self.new_origin.z;
        I::from(coord)
    }
}

impl<G> Grid3 for Grid3NewOrigin<G> 
where
    G: Grid3,
    <G as Grid3>::XBound: RangeBoundsPlus,
    <G as Grid3>::YBound: RangeBoundsPlus,
    <G as Grid3>::ZBound: RangeBoundsPlus,
{
    type Item = <G as Grid3>::Item;
    type XBound = <<G as Grid3>::XBound as RangeBoundsPlus>::Output;
    type YBound = <<G as Grid3>::YBound as RangeBoundsPlus>::Output;
    type ZBound = <<G as Grid3>::ZBound as RangeBoundsPlus>::Output;
    
    fn x_bound(&self) -> Self::XBound { self.new_xbound.clone() }
    fn y_bound(&self) -> Self::YBound { self.new_ybound.clone() }
    fn z_bound(&self) -> Self::ZBound { self.new_zbound.clone() }
}

impl<G> Grid3Get for Grid3NewOrigin<G> 
where
    G: Grid3 + Grid3Get,
    <G as Grid3>::XBound: RangeBoundsPlus,
    <G as Grid3>::YBound: RangeBoundsPlus,
    <G as Grid3>::ZBound: RangeBoundsPlus,
{
    fn get<I>(&self, coord: I) -> Self::Item
    where
        I: Into<Vector3<i32>>
    {
        let coord = self.adjust_coord(coord.into());
        self.inner.get(coord)
    }
}

impl<G> Grid3Set for Grid3NewOrigin<G> 
where
    G: Grid3 + Grid3Set,
    <G as Grid3>::XBound: RangeBoundsPlus,
    <G as Grid3>::YBound: RangeBoundsPlus,
    <G as Grid3>::ZBound: RangeBoundsPlus,
{
    fn set<I>(&mut self, coord: I, elem: Self::Item)
    where
        I: Into<Vector3<i32>>
    {
        let coord = self.adjust_coord(coord.into());
        self.inner.set(coord, elem);
    }
}

impl<G> Grid3Ref for Grid3NewOrigin<G> 
where
    G: Grid3 + Grid3Ref,
    <G as Grid3>::XBound: RangeBoundsPlus,
    <G as Grid3>::YBound: RangeBoundsPlus,
    <G as Grid3>::ZBound: RangeBoundsPlus,
{
    fn idx<I>(&self, coord: I) -> &Self::Item
    where
        I: Into<Vector3<i32>>
    {
        let coord = self.adjust_coord(coord.into());
        self.inner.idx(coord)
    }
}

impl<G> Grid3Mut for Grid3NewOrigin<G> 
where
    G: Grid3 + Grid3Mut,
    <G as Grid3>::XBound: RangeBoundsPlus,
    <G as Grid3>::YBound: RangeBoundsPlus,
    <G as Grid3>::ZBound: RangeBoundsPlus,
{
    fn midx<I>(&mut self, coord: I) -> &mut Self::Item
    where
        I: Into<Vector3<i32>>
    {
        let coord = self.adjust_coord(coord.into());
        self.inner.midx(coord)
    }
}