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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use std::cmp::Ordering;

use mercator_db::space;
use mercator_db::Properties;

pub use super::types::*;

/**********************************************************************/
/* FORMATTING DATA                                                    */
/**********************************************************************/
#[derive(Clone, Debug)]
pub enum Projection {
    Nifti(String, LiteralSelector, Bag),
    JSON(String, JsonValue, Bag),
}

impl Projection {
    pub fn space(&self) -> &String {
        match self {
            Projection::Nifti(space, _, _) => &space,
            Projection::JSON(space, _, _) => &space,
        }
    }
}

// JSON FORMAT
#[derive(Clone, Debug)]
pub enum JsonValue {
    String(String),
    JsonNumber(LiteralNumber),
    Bool(bool),
    Null,
    Object(Vec<(String, JsonValue)>),
    Array(Vec<JsonValue>),
    Selector(LiteralSelector),
    Aggregation(Aggregation),
}

#[derive(Clone, Debug)]
pub enum Aggregation {
    Count(bool, LiteralSelector),
    Sum(LiteralSelector),
    Min(LiteralSelector),
    Max(LiteralSelector),
}

// NIFTI
#[derive(Clone, Debug)]
struct Transform {
    reference: String,
    offset: Vec<LiteralNumber>,
    rotation: Vec<Vec<LiteralNumber>>,
}

/**********************************************************************/
/* SELECTING / FILTERING DATA                                         */
/**********************************************************************/
#[derive(Clone, Debug)]
pub enum Bag {
    // This is an implicit operator, inserted by the parser. Never to be used directly.
    ViewPort(Box<Bag>),
    // Bags
    Distinct(Box<Bag>),
    Filter(Option<Predicate>, Option<Box<Bag>>),
    Complement(Box<Bag>),
    Intersection(Box<Bag>, Box<Bag>),
    Union(Box<Bag>, Box<Bag>),
    Bag(Vec<Bag>),
    Inside(Shape),
    Outside(Shape),
    //FIXME: ADD A SHAPE VARIANT WHICH JUST RETURNS ALL THE POSITIONS OF THAT SHAPE
    //Shape(Shape),
}

impl Bag {
    pub fn space(&self) -> &String {
        match self {
            Bag::ViewPort(bag) => bag.space(),
            Bag::Distinct(bag) => bag.space(),
            Bag::Filter(_, bag) => match bag {
                None => space::Space::universe().name(),
                Some(b) => b.space(),
            },
            Bag::Complement(bag) => bag.space(),
            Bag::Intersection(lh, _) => {
                // We are assuming lh and rh are in the same space.
                // Checked as part of the validation.
                lh.space()
            }
            Bag::Union(lh, _) => {
                // We are assuming lh and rh are in the same space.
                // Checked as part of the validation.
                lh.space()
            }
            Bag::Bag(_) => {
                // Bags can be defined in different spaces, thus the output is
                // always in the universe space.
                space::Space::universe().name()
            }
            Bag::Inside(shape) => shape.space(),
            Bag::Outside(shape) => shape.space(),
        }
    }
}
/**********************************************************************/
/* BAG OPERATORS                                                      */
/**********************************************************************/
#[derive(Clone, Debug)]
pub enum Predicate {
    Less(Position, LiteralPosition),
    Greater(Position, LiteralPosition),
    Equal(Position, LiteralPosition),
    Not(Box<Predicate>),
    And(Box<Predicate>, Box<Predicate>),
    Or(Box<Predicate>, Box<Predicate>),
}

/**********************************************************************/
/* SPATIAL OPERATORS                                                  */
/**********************************************************************/

/**********************************************************************/
/* SHAPES                                                             */
/**********************************************************************/
#[derive(Clone, Debug)]
pub enum Shape {
    Point(String, LiteralPosition),
    HyperRectangle(String, Vec<LiteralPosition>),
    HyperSphere(String, LiteralPosition, LiteralNumber),
    Label(String, String),
    Nifti(String),
}

impl Shape {
    pub fn space(&self) -> &String {
        match self {
            Shape::Point(space, _) => space,
            Shape::HyperRectangle(space, _) => space,
            Shape::HyperSphere(space, _, _) => space,
            Shape::Label(space, _) => space,
            Shape::Nifti(space) => space,
        }
    }

    pub fn volume(&self) -> f64 {
        match self {
            Shape::Point(_, _) => std::f64::EPSILON, // The smallest non-zero volume possible
            Shape::HyperRectangle(_space, pos) => {
                //TODO: At this time, only aligned to the axes, defined by two points, hyperrectangles are supported.
                assert_eq!(pos.len(), 2);

                // We assume the first position is the low point, the second is
                // the high point, this being true for each dimension. As we add
                // an even number of points per extra dimension, we assume the
                // last group is the high end, and the last position is the
                // highest point.
                let mut volume = 1.0;
                let LiteralPosition(low) = &pos[0];
                let LiteralPosition(high) = &pos[pos.len() - 1];

                // For each dimension, multiply by the length in that dimension
                for i in 0..low.len() {
                    let l = match low[i] {
                        LiteralNumber::Int(x) => x as f64,
                        LiteralNumber::Float(x) => x,
                    };

                    let h = match high[i] {
                        LiteralNumber::Int(x) => x as f64,
                        LiteralNumber::Float(x) => x,
                    };

                    let length = if h > l { h - l } else { l - h };
                    volume *= length;
                }

                volume
            }
            Shape::HyperSphere(_space, pos, radius) => {
                // Formula from https://en.wikipedia.org/wiki/N-sphere#/media/File:N_SpheresVolumeAndSurfaceArea.png
                let LiteralPosition(position) = pos;
                let k = position.len(); // Number of dimensions.

                let radius = match *radius {
                    LiteralNumber::Int(x) => x as f64,
                    LiteralNumber::Float(x) => x,
                };

                let pi = std::f64::consts::PI;
                let factor = 2.0 * pi;

                // Set starting values for the coefficient
                let mut a = 2.0;
                let mut i = if (k % 2) == 0 {
                    a = pi;
                    2
                } else {
                    1
                };

                while i < k {
                    i += 2;
                    a *= factor;
                    a /= i as f64;
                }

                a * radius.powi(i as i32)
            }
            Shape::Label(_, _) => {
                // FIXME: Needs to find a way to figure out the approximate volume of this specific ID, or return MAX or MIN..
                std::f64::EPSILON
            }
            Shape::Nifti(_) => unimplemented!("Nifti"),
        }
    }

    pub fn rasterize<'e>(&self) -> mercator_db::ResultSet<'e> {
        unimplemented!("rasterize")
    }
}

/**********************************************************************/
/* POSITIONS                                                          */
/**********************************************************************/
#[derive(Clone, Debug)]
pub enum Position {
    StrCmp(LiteralSelector, String),
    Selector(LiteralSelector),
    Literal(LiteralPosition),
}

impl Position {
    pub fn value<'e>(
        &self,
        object: (&'e String, &'e space::Position, &'e Properties),
    ) -> LiteralPosition {
        match self {
            Position::Literal(literal) => literal.clone(),
            Position::Selector(selector) => selector.position(object),
            Position::StrCmp(selector, literal) => {
                let x = match (selector.str(object)).cmp(literal) {
                    Ordering::Equal => 0,
                    Ordering::Greater => 1,
                    Ordering::Less => -1,
                };
                LiteralPosition(vec![LiteralNumber::Int(x)])
            }
        }
    }
}

/**********************************************************************/
/* Literals / TOKENS                                                  */
/**********************************************************************/

#[derive(Clone, Debug)]
pub struct Field(pub String, pub Option<usize>);

#[derive(Clone, Debug)]
pub enum LiteralNumber {
    Int(i64),
    Float(f64),
}

impl From<&LiteralNumber> for Vec<f64> {
    fn from(l: &LiteralNumber) -> Self {
        let r = match l {
            LiteralNumber::Int(x) => (*x) as f64,
            LiteralNumber::Float(x) => *x,
        };

        vec![r]
    }
}

impl PartialEq for LiteralNumber {
    fn eq(&self, other: &LiteralNumber) -> bool {
        match self {
            LiteralNumber::Int(l) => match other {
                LiteralNumber::Int(r) => l == r,
                LiteralNumber::Float(_) => false,
            },
            LiteralNumber::Float(l) => match other {
                LiteralNumber::Int(r) => l == &(*r as f64),
                LiteralNumber::Float(r) => l == r,
            },
        }
    }
}

#[derive(Clone, Debug)]
pub struct LiteralPosition(pub Vec<LiteralNumber>);

impl LiteralPosition {
    pub fn get_type(&self) -> LiteralTypes {
        let Self(v) = self;
        let mut t = Vec::new();

        for n in v {
            t.push(match n {
                LiteralNumber::Int(_) => LiteralTypes::Int,
                LiteralNumber::Float(_) => LiteralTypes::Float,
            });
        }

        LiteralTypes::Vector(t)
    }

    pub fn length(&self) -> f64 {
        let LiteralPosition(v) = self;
        let mut a = 0.0;

        for x in v {
            let x = match x {
                LiteralNumber::Int(x) => (*x) as f64,
                LiteralNumber::Float(x) => *x,
            };

            a += x * x;
        }

        a
    }

    pub fn dimensions(&self) -> usize {
        self.0.len()
    }
}

impl From<&LiteralNumber> for f64 {
    fn from(l: &LiteralNumber) -> Self {
        match l {
            LiteralNumber::Int(x) => (*x) as f64,
            LiteralNumber::Float(x) => *x,
        }
    }
}

impl From<&LiteralPosition> for Vec<f64> {
    fn from(l: &LiteralPosition) -> Self {
        let LiteralPosition(v) = l;
        let mut r = Vec::with_capacity(v.len());

        for x in v {
            let x = match x {
                LiteralNumber::Int(x) => (*x) as f64,
                LiteralNumber::Float(x) => *x,
            };
            r.push(x);
        }

        r
    }
}

impl From<&Vec<f64>> for LiteralPosition {
    fn from(v: &Vec<f64>) -> Self {
        let mut lv = Vec::with_capacity(v.len());
        for value in v {
            lv.push(LiteralNumber::Float(*value));
        }

        LiteralPosition(lv)
    }
}
impl From<&space::Position> for LiteralPosition {
    fn from(position: &space::Position) -> Self {
        let lv: Vec<f64> = position.into();
        (&lv).into()
    }
}

impl PartialOrd for LiteralPosition {
    fn partial_cmp(&self, other: &LiteralPosition) -> Option<Ordering> {
        let LiteralPosition(lh) = self;
        let LiteralPosition(rh) = other;

        if lh.len() != rh.len() {
            None
        } else {
            // Order is defined by the geometric length of the vector between the Origin and the point.
            let l = self.length();
            let r = other.length();

            l.partial_cmp(&r)
        }
    }
}

impl PartialEq for LiteralPosition {
    fn eq(&self, other: &LiteralPosition) -> bool {
        let LiteralPosition(lh) = self;
        let LiteralPosition(rh) = other;

        if lh.len() == rh.len() {
            for i in 0..lh.len() {
                if lh[i] != rh[i] {
                    return false;
                }
            }

            true
        } else {
            false
        }
    }
}

#[derive(Clone, Debug)]
pub struct LiteralSelector(pub Vec<Field>);

impl LiteralSelector {
    pub fn get_type(&self) -> LiteralTypes {
        // FIXME: Pretend for now that everything is a number, needs to be actually looked up in data model.
        LiteralTypes::Int
    }

    // FIXME: THIS IS SOOO WRONG
    pub fn position<'e>(
        &self,
        object: (&'e String, &'e space::Position, &'e Properties),
    ) -> LiteralPosition {
        println!("LiteralSelector.position(): {:?}", self);
        object.1.into()
    }

    // FIXME: THIS IS SOOO WRONG
    pub fn str<'e>(&self, object: (&'e String, &'e space::Position, &'e Properties)) -> &'e str {
        let LiteralSelector(v) = self;
        let last = v.last();
        if let Some(Field(name, _)) = last {
            if name == "id" {
                return object.2.id();
            } else if name == "type" {
                return object.2.type_name();
            } else if name == "reference_space" {
                return object.0;
            }
        }

        println!("LiteralSelector.str(): {:?}", self);
        unimplemented!("Unknown Field");
    }
}

//FIXME: HACK FOR NOW; NEED PROPER TYPE CHECKING!
pub fn get_type() -> LiteralTypes {
    LiteralTypes::Vector(vec![
        LiteralTypes::Float,
        LiteralTypes::Float,
        LiteralTypes::Float,
    ])
}