rlucene 0.1.0-alpha.3

A Rust port of Apache Lucene, a full-text search engine library
Documentation
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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
 *
 *     http://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.
 */
use crate::core::geo::circle::Circle;
use crate::core::geo::component2d::{
  Component2D, WithinRelation, contains_point, disjoint, point_in_triangle, within,
};
use crate::core::geo::geo_utils::GeoUtils;
use crate::core::geo::rectangle::Rectangle;
use crate::core::geo::xy_circle::XYCircle;
use crate::core::geo::xy_rectangle::XYRectangle;
use crate::core::index::point_values::Relation;
use crate::core::util::SloppyMath;
use crate::core::util::core_helper::CoreHelper;
use crate::core::util::error::lucene_error::Result;

/// 2D circle implementation containing spatial logic
pub struct Circle2D<T> {
  calculator: T,
}
impl<T> Circle2D<T> {
  pub(crate) fn new(calculator: T) -> Self {
    Self { calculator }
  }
}
impl<T> Component2D for Circle2D<T>
where
  T: DistanceCalculator,
{
  fn get_min_x(&self) -> f64 {
    self.calculator.get_min_x()
  }

  fn get_max_x(&self) -> f64 {
    self.calculator.get_max_x()
  }

  fn get_min_y(&self) -> f64 {
    self.calculator.get_min_y()
  }

  fn get_max_y(&self) -> f64 {
    self.calculator.get_max_y()
  }

  fn contains(&self, x: f64, y: f64) -> bool {
    self.calculator.contains(x, y)
  }

  fn relate(&self, min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> Result<Relation> {
    if self.calculator.disjoint(min_x, max_x, min_y, max_y) {
      return Ok(Relation::CellOutsideQuery);
    }
    if self.calculator.within(min_x, max_x, min_y, max_y) {
      return Ok(Relation::CellCrossesQuery);
    }
    self.calculator.relate(min_x, max_x, min_y, max_y)
  }

  fn intersects_line(
    &self,
    min_x: f64,
    max_x: f64,
    min_y: f64,
    max_y: f64,
    a_x: f64,
    a_y: f64,
    b_x: f64,
    b_y: f64,
  ) -> bool {
    if self.calculator.disjoint(min_x, max_x, min_y, max_y) {
      return false;
    }
    self.contains(a_x, a_y)
      || self.contains(b_x, b_y)
      || self.calculator.intersects_line(a_x, a_y, b_x, b_y)
  }

  fn intersects_triangle(
    &self,
    min_x: f64,
    max_x: f64,
    min_y: f64,
    max_y: f64,
    a_x: f64,
    a_y: f64,
    b_x: f64,
    b_y: f64,
    c_x: f64,
    c_y: f64,
  ) -> bool {
    if self.calculator.disjoint(min_x, max_x, min_y, max_y) {
      return false;
    }
    self.contains(a_x, a_y)
      || self.contains(b_x, b_y)
      || self.contains(c_x, c_y)
      || point_in_triangle(
        min_x,
        max_x,
        min_y,
        max_y,
        self.calculator.get_x(),
        self.calculator.get_y(),
        a_x,
        a_y,
        b_x,
        b_y,
        c_x,
        c_y,
      )
      || self.calculator.intersects_line(a_x, a_y, b_x, b_y)
      || self.calculator.intersects_line(b_x, b_y, c_x, c_y)
      || self.calculator.intersects_line(c_x, c_y, a_x, a_y)
  }

  fn contains_line(
    &self,
    min_x: f64,
    max_x: f64,
    min_y: f64,
    max_y: f64,
    a_x: f64,
    a_y: f64,
    b_x: f64,
    b_y: f64,
  ) -> bool {
    if self.calculator.disjoint(min_x, max_x, min_y, max_y) {
      return false;
    }
    self.contains(a_x, a_y) && self.contains(b_x, b_y)
  }

  fn contains_triangle(
    &self,
    min_x: f64,
    max_x: f64,
    min_y: f64,
    max_y: f64,
    a_x: f64,
    a_y: f64,
    b_x: f64,
    b_y: f64,
    c_x: f64,
    c_y: f64,
  ) -> bool {
    if self.calculator.disjoint(min_x, max_x, min_y, max_y) {
      return false;
    }
    self.contains(a_x, a_y) && self.contains(b_x, b_y) && self.contains(c_x, c_y)
  }

  fn within_point(
    &self,
    x: f64,
    y: f64,
  ) -> crate::core::util::error::lucene_error::Result<WithinRelation> {
    Ok(if self.contains(x, y) {
      WithinRelation::NotWithin
    } else {
      WithinRelation::Disjoint
    })
  }

  fn within_line(
    &self,
    min_x: f64,
    max_x: f64,
    min_y: f64,
    max_y: f64,
    a_x: f64,
    a_y: f64,
    ab: bool,
    b_x: f64,
    b_y: f64,
  ) -> crate::core::util::error::lucene_error::Result<WithinRelation> {
    if self.calculator.disjoint(min_x, max_x, min_y, max_y) {
      return Ok(WithinRelation::Disjoint);
    }
    if self.contains(a_x, a_y) || self.contains(b_x, b_y) {
      return Ok(WithinRelation::NotWithin);
    }
    if ab && self.calculator.intersects_line(a_x, a_y, b_x, b_y) {
      return Ok(WithinRelation::NotWithin);
    }
    Ok(WithinRelation::Disjoint)
  }

  fn within_triangle(
    &self,
    min_x: f64,
    max_x: f64,
    min_y: f64,
    max_y: f64,
    a_x: f64,
    a_y: f64,
    ab: bool,
    b_x: f64,
    b_y: f64,
    bc: bool,
    c_x: f64,
    c_y: f64,
    ca: bool,
  ) -> crate::core::util::error::lucene_error::Result<WithinRelation> {
    if self.calculator.disjoint(min_x, max_x, min_y, max_y) {
      return Ok(WithinRelation::Disjoint);
    }

    if self.contains(a_x, a_y) || self.contains(b_x, b_y) || self.contains(c_x, c_y) {
      return Ok(WithinRelation::NotWithin);
    }

    if ab && self.calculator.intersects_line(a_x, a_y, b_x, b_y) {
      return Ok(WithinRelation::NotWithin);
    }
    if bc && self.calculator.intersects_line(b_x, b_y, c_x, c_y) {
      return Ok(WithinRelation::NotWithin);
    }
    if ca && self.calculator.intersects_line(c_x, c_y, a_x, a_y) {
      return Ok(WithinRelation::NotWithin);
    }

    if point_in_triangle(
      min_x,
      max_x,
      min_y,
      max_y,
      self.calculator.get_x(),
      self.calculator.get_y(),
      a_x,
      a_y,
      b_x,
      b_y,
      c_x,
      c_y,
    ) {
      return Ok(WithinRelation::Candidate);
    }
    Ok(WithinRelation::Disjoint)
  }
}

pub trait DistanceCalculator {
  /// check if the point is within a distance
  fn contains(&self, x: f64, y: f64) -> bool;

  /// check if the line is within a distance
  fn intersects_line(&self, a_x: f64, a_y: f64, b_x: f64, b_y: f64) -> bool;

  /// Relates this calculator to the provided bounding box
  fn relate(&self, min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> Result<Relation>;

  /// check if the bounding box is disjoint with this calculator bounding box
  fn disjoint(&self, min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> bool;

  /// check if the bounding box is contains this calculator bounding box
  fn within(&self, min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> bool;

  /// get min X of this calculator
  fn get_min_x(&self) -> f64;

  /// get max X of this calculator
  fn get_max_x(&self) -> f64;

  /// get min Y of this calculator
  fn get_min_y(&self) -> f64;

  /// get max Y of this calculator
  fn get_max_y(&self) -> f64;

  /// get center X
  fn get_x(&self) -> f64;

  /// get center Y
  fn get_y(&self) -> f64;
}
/// Builds a XYCircle2D from XYCircle. Distance calculations are performed using cartesian distance.
pub fn create_from_xy_circle(circle: &XYCircle) -> Result<Circle2D<CartesianDistance>> {
  let calculator = CartesianDistance::new(circle.get_x(), circle.get_y(), circle.get_radius())?;
  Ok(Circle2D::new(calculator))
}
/// Builds a Circle2D from Circle. Distance calculations are performed using haversin distance.
pub fn create_from_circle(circle: &Circle) -> Result<Circle2D<HaversinDistance>> {
  let calculator = HaversinDistance::new(circle.get_lon(), circle.get_lat(), circle.get_radius())?;
  Ok(Circle2D::new(calculator))
}
pub struct CartesianDistance {
  center_x: f64,
  center_y: f64,
  radius_squared: f64,
  rectangle: XYRectangle,
}
impl CartesianDistance {
  pub(crate) fn new(center_x: f32, center_y: f32, radius: f32) -> Result<Self> {
    let rectangle = XYRectangle::from_point_distance(center_x, center_y, radius)?;
    let radius_squared = radius as f64 * radius as f64;
    Ok(Self {
      center_x: center_x as f64,
      center_y: center_y as f64,
      radius_squared,
      rectangle,
    })
  }
}
impl DistanceCalculator for CartesianDistance {
  fn contains(&self, x: f64, y: f64) -> bool {
    if contains_point(
      x,
      y,
      self.rectangle.min_x as f64,
      self.rectangle.max_x as f64,
      self.rectangle.min_y as f64,
      self.rectangle.max_y as f64,
    ) {
      let diff_x = x - self.center_x;
      let diff_y = y - self.center_y;
      return diff_x * diff_x + diff_y * diff_y <= self.radius_squared;
    }
    false
  }

  fn intersects_line(&self, a_x: f64, a_y: f64, b_x: f64, b_y: f64) -> bool {
    intersects_line(self.center_x, self.center_y, a_x, a_y, b_x, b_y, self)
  }

  fn relate(&self, min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> Result<Relation> {
    if contains_point(self.center_x, self.center_y, min_x, max_x, min_y, max_y) {
      if self.contains(min_x, min_y)
        && self.contains(max_x, min_y)
        && self.contains(max_x, max_y)
        && self.contains(min_x, max_y)
      {
        // we are fully enclosed, collect everything within this subtree
        return Ok(Relation::CellInsideQuery);
      }
    } else {
      let mut sum_of_squared_diffs = 0.0f64;
      if self.center_x < min_x {
        let diff = min_x - self.center_x;
        sum_of_squared_diffs += diff * diff;
      } else if self.center_x > max_x {
        let diff = max_x - self.center_x;
        sum_of_squared_diffs += diff * diff;
      }

      if self.center_y < min_y {
        let diff = min_y - self.center_y;
        sum_of_squared_diffs += diff * diff;
      } else if self.center_y > max_y {
        let diff = max_y - self.center_y;
        sum_of_squared_diffs += diff * diff;
      }

      if sum_of_squared_diffs > self.radius_squared {
        return Ok(Relation::CellOutsideQuery);
      }
    }
    Ok(Relation::CellCrossesQuery)
  }

  fn disjoint(&self, min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> bool {
    disjoint(
      self.rectangle.min_x as f64,
      self.rectangle.max_x as f64,
      self.rectangle.min_y as f64,
      self.rectangle.max_y as f64,
      min_x,
      max_x,
      min_y,
      max_y,
    )
  }

  fn within(&self, min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> bool {
    within(
      self.rectangle.min_x as f64,
      self.rectangle.max_x as f64,
      self.rectangle.min_y as f64,
      self.rectangle.max_y as f64,
      min_x,
      max_x,
      min_y,
      max_y,
    )
  }

  fn get_min_x(&self) -> f64 {
    self.rectangle.min_x as f64
  }

  fn get_max_x(&self) -> f64 {
    self.rectangle.max_x as f64
  }

  fn get_min_y(&self) -> f64 {
    self.rectangle.min_y as f64
  }

  fn get_max_y(&self) -> f64 {
    self.rectangle.max_y as f64
  }

  fn get_x(&self) -> f64 {
    self.center_x
  }

  fn get_y(&self) -> f64 {
    self.center_y
  }
}
fn intersects_line<T>(
  center_x: f64,
  center_y: f64,
  a_x: f64,
  a_y: f64,
  b_x: f64,
  b_y: f64,
  calculator: &T,
) -> bool
where
  T: DistanceCalculator,
{
  let vector_apx = center_x - a_x;
  let vector_apy = center_y - a_y;

  let vector_abx = b_x - a_x;
  let vector_aby = b_y - a_y;

  let magnitude_ab = vector_abx * vector_abx + vector_aby * vector_aby;
  let dot_product = vector_apx * vector_abx + vector_apy * vector_aby;

  let distance = dot_product / magnitude_ab;

  if distance < 0.0 || distance > 1.0 {
    return false;
  }

  let p_x = a_x + vector_abx * distance;
  let p_y = a_y + vector_aby * distance;

  let min_x = CoreHelper::min_f64(a_x, b_x);
  let min_y = CoreHelper::min_f64(a_y, b_y);
  let max_x = CoreHelper::max_f64(a_x, b_x);
  let max_y = CoreHelper::max_f64(a_y, b_y);

  if p_x >= min_x && p_x <= max_x && p_y >= min_y && p_y <= max_y {
    return calculator.contains(p_x, p_y);
  }
  false
}
pub struct HaversinDistance {
  center_lat: f64,
  center_lon: f64,
  sort_key: f64,
  axis_lat: f64,
  rectangle: Rectangle,
  crosses_dateline: bool,
}
impl HaversinDistance {
  pub fn new(center_lon: f64, center_lat: f64, radius: f64) -> Result<Self> {
    let sort_key = GeoUtils::distance_query_sort_key(radius);
    let axis_lat = Rectangle::axis_lat(center_lat, radius);
    let rectangle = Rectangle::from_point_distance(center_lat, center_lon, radius)?;
    let crosses_dateline = rectangle.min_lon > rectangle.max_lon;
    Ok(Self {
      center_lat,
      center_lon,
      sort_key,
      axis_lat,
      rectangle,
      crosses_dateline,
    })
  }
}
impl DistanceCalculator for HaversinDistance {
  fn contains(&self, x: f64, y: f64) -> bool {
    if self.crosses_dateline {
      if contains_point(
        x,
        y,
        self.rectangle.min_lon,
        GeoUtils::MAX_LON_INCL,
        self.rectangle.min_lat,
        self.rectangle.max_lat,
      ) || contains_point(
        x,
        y,
        GeoUtils::MIN_LON_INCL,
        self.rectangle.max_lon,
        self.rectangle.min_lat,
        self.rectangle.max_lat,
      ) {
        return SloppyMath::haversin_sort_key(y, x, self.center_lat, self.center_lon)
          <= self.sort_key;
      }
    } else if contains_point(
      x,
      y,
      self.rectangle.min_lon,
      self.rectangle.max_lon,
      self.rectangle.min_lat,
      self.rectangle.max_lat,
    ) {
      return SloppyMath::haversin_sort_key(y, x, self.center_lat, self.center_lon)
        <= self.sort_key;
    }
    false
  }

  fn intersects_line(&self, a_x: f64, a_y: f64, b_x: f64, b_y: f64) -> bool {
    if intersects_line(self.center_lon, self.center_lat, a_x, a_y, b_x, b_y, self) {
      return true;
    }
    if self.crosses_dateline {
      let new_center_lon = if self.center_lon > 0.0 {
        self.center_lon - 360.0
      } else {
        self.center_lon + 360.0
      };
      return intersects_line(new_center_lon, self.center_lat, a_x, a_y, b_x, b_y, self);
    }
    false
  }

  fn relate(&self, min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> Result<Relation> {
    GeoUtils::relate(
      min_y,
      max_y,
      min_x,
      max_x,
      self.center_lat,
      self.center_lon,
      self.sort_key,
      self.axis_lat,
    )
  }

  fn disjoint(&self, min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> bool {
    if self.crosses_dateline {
      disjoint(
        self.rectangle.min_lon,
        GeoUtils::MAX_LON_INCL,
        self.rectangle.min_lat,
        self.rectangle.max_lat,
        min_x,
        max_x,
        min_y,
        max_y,
      ) && disjoint(
        GeoUtils::MIN_LON_INCL,
        self.rectangle.max_lon,
        self.rectangle.min_lat,
        self.rectangle.max_lat,
        min_x,
        max_x,
        min_y,
        max_y,
      )
    } else {
      disjoint(
        self.rectangle.min_lon,
        self.rectangle.max_lon,
        self.rectangle.min_lat,
        self.rectangle.max_lat,
        min_x,
        max_x,
        min_y,
        max_y,
      )
    }
  }

  fn within(&self, min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> bool {
    if self.crosses_dateline {
      within(
        self.rectangle.min_lon,
        GeoUtils::MAX_LON_INCL,
        self.rectangle.min_lat,
        self.rectangle.max_lat,
        min_x,
        max_x,
        min_y,
        max_y,
      ) || within(
        GeoUtils::MIN_LON_INCL,
        self.rectangle.max_lon,
        self.rectangle.min_lat,
        self.rectangle.max_lat,
        min_x,
        max_x,
        min_y,
        max_y,
      )
    } else {
      within(
        self.rectangle.min_lon,
        self.rectangle.max_lon,
        self.rectangle.min_lat,
        self.rectangle.max_lat,
        min_x,
        max_x,
        min_y,
        max_y,
      )
    }
  }

  fn get_min_x(&self) -> f64 {
    if self.crosses_dateline {
      GeoUtils::MIN_LON_INCL
    } else {
      self.rectangle.min_lon
    }
  }

  fn get_max_x(&self) -> f64 {
    if self.crosses_dateline {
      GeoUtils::MAX_LON_INCL
    } else {
      self.rectangle.max_lon
    }
  }

  fn get_min_y(&self) -> f64 {
    self.rectangle.min_lat
  }

  fn get_max_y(&self) -> f64 {
    self.rectangle.max_lat
  }

  fn get_x(&self) -> f64 {
    self.center_lon
  }

  fn get_y(&self) -> f64 {
    self.center_lat
  }
}