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
// Boost.Polygon library

//          Copyright Andrii Sydorchuk 2010-2012.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

// See http://www.boost.org for updates, documentation, and revision history of C++ code.

// Ported from C++ boost 1.76.0 to Rust in 2020/2021 by Eadf (github.com/eadf)

#![deny(non_camel_case_types)]
#![deny(unused_parens)]
#![deny(non_upper_case_globals)]
#![deny(unused_qualifications)]
#![deny(unused_results)]
#![deny(unused_imports)]
#![deny(bare_trait_objects)]
#![deny(ellipsis_inclusive_range_patterns)]
#![deny(elided_lifetimes_in_paths)]
#![cfg_attr(feature = "map_first_last", feature(map_first_last))]

#[cfg(test)]
extern crate lazy_static;
use core::fmt::Debug;
use extended_exp_fpt as EX;
use extended_int as EI;
use num::{Float, NumCast, PrimInt, Zero};
use std::fmt;
use std::hash::Hash;
use std::marker::PhantomData;
use std::ops::Neg;

mod beach_line;
pub mod builder;
mod circle_event;
mod ctypes;
pub mod diagram;
mod end_point;
// I'd prefer if this module could be pub (crate), but then the documentation examples would not work.
pub mod extended_exp_fpt;
// I'd prefer if this module could be pub (crate), but then the documentation examples would not work.
pub mod extended_int;
pub mod file_reader;
pub(crate) mod predicate;
// I'd prefer if this module could be pub (crate), but then the documentation examples would not work.
pub mod geometry;
pub mod robust_fpt;
mod site_event;
pub mod sync_diagram;
pub mod visual_utils;

/// Debug utility function, formats an id string
pub(crate) fn format_id(value: Option<usize>) -> String {
    if let Some(value) = value {
        value.to_string()
    } else {
        String::from("-")
    }
}

/// A feature gated print(), will only be active when the feature "console_debug" is selected.
#[macro_export]
macro_rules! t {
    ($($arg:tt)*) => ({
     #[cfg(feature = "console_debug")]
     print!($($arg)*)
    });
}

/// A feature gated println(), will only be active when the feature "console_debug" is selected.
#[macro_export]
macro_rules! tln {
    ($($arg:tt)*) => ({
     #[cfg(feature = "console_debug")]
     println!($($arg)*)
    });
}

#[derive(thiserror::Error, Debug)]
pub enum BvError {
    #[error("error: Some error from cpp_map")]
    ListError {
        #[from]
        source: cpp_map::MapError,
    },
    #[error("error: Some error with object id")]
    IdError(String),
    #[error("error: Some error with a value")]
    ValueError(String),
    #[error("error: Some error with the beach-line")]
    BeachLineError(String),
    #[error("error: given value for the radius is less than 0.0.")]
    RadiusLessThanZero,
    #[error("error: vertices should be added before segments")]
    VerticesGoesFirst(String),
    #[error("error: Some error")]
    InternalError(String),
    #[error("Suspected self-intersecting input data")]
    SelfIntersecting(String),
    #[error("Could not cast number")]
    NumberConversion(String),
    #[error(transparent)]
    BvError(#[from] std::io::Error),
}

/// This is the integer input type of the algorithm. Typically i32 or i64.
pub trait InputType:
    fmt::Display
    + Ord
    + PartialOrd
    + Eq
    + PartialEq
    + Hash
    + PrimInt
    + Copy
    + Clone
    + NumCast
    + Debug
    + Zero
    + Default
    + Unpin
{
}

impl<I> InputType for I where
    I: fmt::Display
        + Ord
        + PartialOrd
        + Eq
        + PartialEq
        + Hash
        + PrimInt
        + Copy
        + Clone
        + NumCast
        + Debug
        + Zero
        + Default
        + Unpin
{
}

/// This is the floating point output type of the algorithm. Typically f32 or f64.
pub trait OutputType:
    Float
    + PartialOrd
    + PartialEq
    + NumCast
    + Copy
    + Clone
    + fmt::Display
    + Default
    + Debug
    + Zero
    + std::ops::MulAssign
    + Unpin
{
}

impl<F> OutputType for F where
    F: Float
        + PartialOrd
        + PartialEq
        + NumCast
        + Copy
        + Clone
        + fmt::Display
        + Default
        + Debug
        + Zero
        + std::ops::MulAssign
        + Unpin
        + Neg<Output = F>
{
}

/// Functions for converting the integer input type to other types (i32 i64 etc.)
#[derive(Default)]
pub struct TypeConverter1<I>
where
    I: InputType + Neg<Output = I>,
{
    #[doc(hidden)]
    pdi_: PhantomData<I>,
}

impl<I> TypeConverter1<I>
where
    I: InputType + Neg<Output = I>,
{
    #[inline(always)]
    /// Convert from the input integer type to an extended int
    pub fn i_to_xi(input: I) -> EI::ExtendedInt {
        EI::ExtendedInt::from(num::cast::<I, i64>(input).unwrap())
    }

    #[inline(always)]
    /// Convert from i32 to the input integer type
    pub fn i32_to_i(input: i32) -> I {
        num::cast::<i32, I>(input).unwrap()
    }

    #[inline(always)]
    /// Convert from the input integer type to a i32
    pub fn i_to_i32(input: I) -> i32 {
        num::cast::<I, i32>(input).unwrap()
    }

    #[inline(always)]
    /// Convert from the input integer type to a i64
    pub fn i_to_i64(input: I) -> i64 {
        num::cast::<I, i64>(input).unwrap()
    }

    #[inline(always)]
    /// Convert from the input integer type to a f32
    pub fn i_to_f32(input: I) -> f32 {
        num::cast::<I, f32>(input).unwrap()
    }

    #[inline(always)]
    /// Convert from the input integer type to a f64
    pub fn i_to_f64(input: I) -> f64 {
        NumCast::from(input).unwrap()
    }
}

/// Functions for converting the integer and float input type to other types.
#[derive(Default)]
pub struct TypeConverter2<I, F>
where
    I: InputType + Neg<Output = I>,
    F: OutputType + Neg<Output = F>,
{
    #[doc(hidden)]
    pdf_: PhantomData<F>,
    #[doc(hidden)]
    pdi_: PhantomData<I>,
}

impl<I, F> TypeConverter2<I, F>
where
    I: InputType + Neg<Output = I>,
    F: OutputType + Neg<Output = F>,
{
    #[inline(always)]
    /// Convert from the input integer type to the output float type
    pub fn i_to_f(input: I) -> F {
        num::cast::<I, F>(input).unwrap()
    }

    #[inline(always)]
    /// Convert from the output float type to i32
    pub fn f_to_i32(input: F) -> i32 {
        num::cast::<F, i32>(input).unwrap()
    }

    #[inline(always)]
    /// Try to convert from the output float type to i32
    pub fn try_f_to_i32(input: F) -> Result<i32, BvError> {
        num::cast::<F, i32>(input).ok_or_else(|| {
            BvError::NumberConversion(format!("Could not convert {:?} to int32", input))
        })
    }

    #[inline(always)]
    pub fn f_to_i(input: F) -> I {
        num::cast::<F, I>(input).unwrap()
    }

    #[inline(always)]
    pub fn try_f_to_i(input: F) -> Result<I, BvError> {
        num::cast::<F, I>(input)
            .ok_or_else(|| BvError::NumberConversion(format!("Could not convert {:?} to I", input)))
    }

    #[inline(always)]
    pub fn f_to_f64(input: F) -> f64 {
        num::cast::<F, f64>(input).unwrap()
    }

    #[inline(always)]
    pub fn f_to_f32(input: F) -> f32 {
        num::cast::<F, f32>(input).unwrap()
    }

    #[inline(always)]
    pub fn i32_to_f(input: i32) -> F {
        num::cast::<i32, F>(input).unwrap()
    }

    #[inline(always)]
    pub fn f32_to_f(input: f32) -> F {
        num::cast::<f32, F>(input).unwrap()
    }

    #[inline(always)]
    pub fn f64_to_f(input: f64) -> F {
        num::cast::<f64, F>(input).unwrap()
    }

    #[inline(always)]
    pub fn xi_to_xf(input: &EI::ExtendedInt) -> EX::ExtendedExponentFpt<f64> {
        EX::ExtendedExponentFpt::from(input)
    }
}