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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
//! Inspection and manipulation of geometry models.
//!
//! There are two model types:
//! 1. [`OccModel`](crate::model::OccModel), which uses the `OpenCASCADE` geometry kernel.
//! 2. [`GeoModel`](crate::model::GeoModel), which uses the built-in `Gmsh` geometry kernel.
//!
//! Either should suffice for most projects.
//!
//! ## Creating a model
//!
//! The only way to get a model is through a [`Gmsh`](crate::Gmsh) context object.
//! ```
//! # use rgmsh::{Gmsh, GmshResult, GeoModel, OccModel};
//! # fn main() -> GmshResult<()> {
//! let gmsh = Gmsh::initialize()?;
//!
//! let mut geo: GeoModel = gmsh.create_native_model("geo")?;
//!
//! let mut occ: OccModel = gmsh.create_occ_model("occ")?;
//! # Ok(())
//! # }
//! ```
//!
//! The model is only valid for the lifetime of `Gmsh`.
//! ```compile_fail
//! # use rgmsh::{Gmsh, GmshResult. OccModel};
//! # fn main() -> GmshResult<()> {
//! let gmsh = Gmsh::initialize()?;
//! let mut geom: OccModel = gmsh.create_occ_model("model")?;
//!
//! // -- do some stuff with geom
//!
//! // drop the Gmsh context
//! std::mem::drop(gmsh);
//! // try to use the model afterwards
//! geom.add_point(0., 0., 0.)?; // won't compile
//! # Ok(())
//! # }
//! ```
//!
//! ## Create, modify and delete shapes
//! You can define points, lines, 2D surfaces and 3D volumes.
//! After defining a shape, you'll get a geometry tag to identify[^unique] it.
//! ```
//! # use rgmsh::{Gmsh, GmshResult};
//! # use rgmsh::model::{PointTag, CurveTag};
//! # fn main() -> GmshResult<()> {
//! # let gmsh = Gmsh::initialize()?;
//! // make a model using the default geometry kernel and call it `model`.
//! let mut geom = gmsh.create_native_model("model")?;
//!
//! // make a point
//! let p1: PointTag = geom.add_point(0., 0., 0.)?;
//! // and another
//! let p2: PointTag = geom.add_point(1., 1., 0.)?;
//!
//! // create a line from the two points
//! let l1: CurveTag = geom.add_line(p1, p2)?;
//! # Ok(())
//! # }
//! ```
//!
//! There are two ways to make geometries in Gmsh: top-down and bottom-up.
//!
//! ### Top-down geometry with the `OpenCASCADE` kernel
//! With the `OpenCASCADE` kernel, you can directly specify the shape you want to make.
//! ```
//! # use rgmsh::{Gmsh, GmshResult};
//! # use rgmsh::model::{PointTag, CurveTag};
//! # fn main() -> GmshResult<()> {
//! # let gmsh = Gmsh::initialize()?;
//! let mut geom = gmsh.create_occ_model("model")?;
//!
//! // make a box starting at (0, 0, 0) with side extents (1, 1, 1)
//! let b = geom.add_box((0., 0., 0.), (1., 1., 1.))?;
//!
//! // make a sphere centered at (10, 10, 10) with radius 2.5
//! let s = geom.add_sphere((10., 10., 10.), 2.5)?;
//!
//! // make a torus centered at (-1, -2, -3) with major radius 5 and minor radius 2
//! let t = geom.add_torus((-1., -2., -3.), (5., 2.))?;
//!
//! # Ok(())
//! # }
//! ```
//!
//! ### Bottom-up geometries with either the `OpenCASCADE` or built-in kernel
//!
//!
//! ## Differences between the CAD engines
//! `OpenCASCADE` is a widely-used CAD engine, so it's a good default choice. You can directly define larger shapes without making their smaller components first.
//! You also get access to powerful Boolean geometry operations for making complex shapes.
//!
//! The [Gmsh manual](http://gmsh.info/doc/texinfo/gmsh.html#Geometry-module)
//! has more information on the differences between the two kernels:
//!
//! > The built-in CAD kernel provides a simple CAD engine based on a bottom-up boundary representation approach:
//! > you need to first define points, then curves, then surfaces and finally volumes.
//!
//! > The `OpenCASCADE` kernel allows one to build models in the same bottom-up manner, or by using a
//! > constructive solid geometry approach where solids are defined first.
//! > Boolean operations can then be performed to modify them.
//!
//! ## Errors
//! Nearly all model functions can fail. Fallible functions will result a `GmshResult`.
//!
//! You can use the `?` operator for terse error handling.
//! ```
//! # use rgmsh::{Gmsh, GmshResult};
//! fn main() -> GmshResult<()> {
//!     let gmsh = Gmsh::initialize()?;
//!     let mut geom = gmsh.create_native_model("model")?;
//!
//!     let p1 = geom.add_point(0., 0., 0.)?;
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Geometry tags
//! Geometry tags are used for:
//! * accessing shape information,
//! * making more complex shapes (like a line from two points),
//! * removing a shape from the model
//!
//! The different geometry tags are:
//! * `PointTag`
//! * `CurveTag`
//! * `WireTag`
//! * `SurfaceTag`
//! * `ShellTag`
//! * `VolumeTag`
//!
//!
//! Since tags can only be created from successful geometry operations, you can't
//! use raw integers for tags.
//! ```compile_fail
//! # use rgmsh::{Gmsh, GmshResult};
//! # use rgmsh::model::{PointTag, CurveTag};
//! # fn main() -> GmshResult<()> {
//! # let gmsh = Gmsh::initialize()?;
//! # let geom = gmsh.create_native_model("model")?;
//! // try to make a point from a raw integer
//! let p1 = PointTag(1); // won't compile
//! // try to make a line from two raw integers
//! let l1 = CurveTag(1, 2); // won't compile
//! # Ok(())
//! # }
//! ```
//!
//! This design differs from other Gmsh API
//! implementations. For example, using the `C++` API, the following example will
//! compile but cause a runtime error.
//! ```cpp
//! #include "gmsh.h"
//! int main() {
//!     gmsh::initialize();
//!     gmsh::model::geo::addLine(1, 2); // (!)
//!     gmsh::finalize();
//! }
//! ```
//! The Rust API avoids such bugs for a single model by only making tags available through API functions.
//!
//! ## Model shadowing
//!
//! Since two models can have identical tag values, tags from one can be used on the other.
//! It's your responsibility to make sure tags are used with the right model.
//!
//! If you're lucky, using the wrong tags will cause a runtime error.
//! ```
//! # use rgmsh::{Gmsh, GmshResult};
//! # use std::result::Result;
//! # fn main() -> GmshResult<()> {
//! #  let gmsh = Gmsh::initialize()?;
//! let mut geom_a = gmsh.create_occ_model("jimbo")?;
//! let mut geom_b = gmsh.create_native_model("aircraft-carrier")?;
//!
//! let p_a = geom_a.add_point(0., 0., 0.)?;
//!
//! let p_b1 = geom_b.add_point(0., 1., 0.)?;
//! let p_b2 = geom_b.add_point(1., 1., 0.)?;
//!
//! // points from different models can have the same value
//! assert!(p_a == p_b1, "Point tags are different!");
//!
//! // Bad! Using tags from one model with another.
//! let line = geom_a.add_line(p_b1, p_b2);
//! assert!(line.is_err());
//! #  Ok(())
//! # }
//! ```
//!
//! If you're unlucky, the tags will exist in both models, causing a silent logic error in your program.
//! In the API's eyes, you've given it valid tags, and it's going to go ahead and do what you asked for.
//! ```
//! # use rgmsh::{Gmsh, GmshResult};
//! # use std::result::Result;
//! # fn main() -> GmshResult<()> {
//! #  let gmsh = Gmsh::initialize()?;
//! let mut geom_a = gmsh.create_occ_model("jimbo")?;
//! let p_a1 = geom_a.add_point(0., 0., 0.)?;
//! let p_a2 = geom_a.add_point(1., 0., 0.)?;
//!
//! let mut geom_b = gmsh.create_native_model("aircraft-carrier")?;
//! let p_b1 = geom_b.add_point(0., 1., 1.)?;
//! let p_b2 = geom_b.add_point(0., 1., 1.)?;
//!
//! // Very bad! A silent logic error. You're on your own debugging this one.
//! let line = geom_a.add_line(p_b1, p_b2);
//! assert!(line.is_ok());
//! #  Ok(())
//! # }
//! ```
//!
//!
//! ## Describing shapes using Physical Groups
//! Physical Groups are Gmsh's way to associate information with geometries.
//! Physical Groups only associate a name with geometry entities and it is up to client software
//! to correctly interpret the Physical Group information.
//!
//! Some common uses for Physical Groups are:
//! * Materials
//! * Boundary conditions
//! * Part names
//!
//! [^unique]: In most circumstances, tags are a unique identifier. There are some
//! exceptions:
//! * If tags are removed from a model, they can be used again for other shapes.
//! * One Gmsh context can have many models. It's your responsibility to avoid
//!   using tags from one model in another.
//!

use crate::{check_main_error, check_model_error, get_cstring, Gmsh, GmshError, GmshResult};

use std::ffi::{CStr, CString};
use std::os::raw::c_int;

use std::marker::PhantomData;
use std::ops::Neg;

// gmsh_sys interface
pub use crate::interface::{geo::*, occ::*};

pub mod shapes;
pub use shapes::*;

/// Add points to a geometry model inline.
///
/// You can use `add_points!` to create a series of points inline.
///
/// Both regular points and points with characteristic lengths are supported.
///
/// This macro returns a new `Vec<PointTag>`.
/// ```
/// # use rgmsh::{Gmsh, GmshResult, add_points};
/// # fn main() -> GmshResult<()> {
/// #   let gmsh = Gmsh::initialize()?;
/// let mut geom = gmsh.create_occ_model("model")?;
/// let lc = 1e-2;
/// let rect_pts = add_points![geom,
///                           (0., 0., 0.),      // basic point
///                           (0.1, 0., 0., lc), // point with a target mesh size
///                           (0.1, 0.3, 0., lc),
///                           (0., 0.3, 0.)];
///
/// for pt in rect_pts.iter() {
///     println!("{:?}", pt);
/// }
/// #    Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! add_points {
    // base case
    (@accum, $kernel_name:ident, $vec:ident) => {};
    // point without a characteristic length
    (@accum, $kernel_name:ident, $vec:ident, ($x:expr, $y:expr, $z:expr) $(, $tail:tt)*) => {
        {
            $vec.push($kernel_name.add_point($x, $y, $z)?);
            add_points!(@accum, $kernel_name, $vec $(,$tail)*);
        }
    };
    // point with a characteristic length
    (@accum, $kernel_name:ident, $vec:ident, ($x:expr, $y:expr, $z:expr, $lc:expr) $(, $tail:tt)*) => {
        {
            $vec.push($kernel_name.add_point_with_lc($x, $y, $z, $lc)?);
            add_points!(@accum, $kernel_name, $vec $(,$tail)*);
        }
    };
    // match one more more points
    ($kernel_name:ident, $($points:tt),+) => {
        {
            let mut temp_vec = Vec::new();
            // use internal separator comma at the front
            add_points!(@accum, $kernel_name, temp_vec $(,$points)*);
            temp_vec
       }
    }
}

/// An instance of the built-in geometry kernel.
pub struct GeoModel<'gmsh> {
    /// The model name.
    pub name: &'static str,
    /// The model name used to talk to C.
    pub c_name: CString,
    phantom: PhantomData<&'gmsh Gmsh>,
}

/// An instance of the `OpenCASCADE` geometry kernel.
pub struct OccModel<'gmsh> {
    /// The model name.
    pub name: &'static str,
    /// The model name used to talk to C.
    pub c_name: CString,
    phantom: PhantomData<&'gmsh Gmsh>,
}

// General model methods
macro_rules! impl_model {

    (@kernel_prefix GeoModel, $fn_name: ident) => {
        crate::interface::geo::$fn_name
    };

    (@kernel_prefix OccModel, $fn_name: ident) => {
         crate::interface::occ::$fn_name
    };

    ($model_type: ident) => {
        impl<'gmsh> $model_type<'gmsh> {
            /// Create a new Gmsh model.
            // todo: fix me for setting which model is the current one.
            // idea: keep a list of already used model names and only allow one at once
            #[must_use]
            pub fn create(_: &'gmsh Gmsh, name: &'static str) -> GmshResult<Self> {
                let c_name = get_cstring(name)?;
                unsafe {
                    let mut ierr: c_int = 0;
                    // also sets the added model as the current model
                    gmsh_sys::gmshModelAdd(c_name.as_ptr(), &mut ierr);
                    let model = $model_type {
                        name,
                        c_name,
                        phantom: PhantomData,
                    };
                    check_main_error!(ierr, model)
                }
            }

            /// Remove model from Gmsh.
            pub fn remove(self) -> GmshResult<()> {
                 // first set this model to the current model.
                 self.set_current()?;
                 // now, remove the current model
                 unsafe {
                     let mut ierr: c_int = 0;
                     gmsh_sys::gmshModelRemove(&mut ierr);
                     check_main_error!(ierr, ())
                 }
             }

            /// Set model to current model.
            pub fn set_current(&self) -> GmshResult<()> {
                unsafe {
                    let mut ierr: c_int = 0;
                    gmsh_sys::gmshModelSetCurrent(self.c_name.as_ptr(), &mut ierr);
                    match ierr {
                        0 => Ok(()),
                        _ => Err(GmshError::Execution),
                    }
                }
            }

            /// Synchronize the underlying CAD representation.
            pub fn synchronize(&mut self) -> GmshResult<()> {
                self.set_current()?;
                unsafe {
                    let mut ierr: c_int = 0;
                    let sync_fn = impl_model!(@kernel_prefix $model_type, synchronize);
                    sync_fn(&mut ierr);
                    check_model_error!(ierr, ())
                }
            }

            /// Mesh the model.
            // probably should move this to a dedicated model class
            // with an inner Option(Mesh) and Option(Geo)
            pub fn generate_mesh(&mut self, dim: i32) -> GmshResult<()> {
                self.set_current()?;
                // TODO think about synchronize by default?
                self.synchronize()?;
                unsafe {
                    let mut ierr: c_int = 0;
                    gmsh_sys::gmshModelMeshGenerate(dim, &mut ierr);
                    check_model_error!(ierr, ())
                }
            }
        }
    }
}

impl_model!(GeoModel);
impl_model!(OccModel);

mod geo;
mod occ;

//    #[doc(hidden)]
//    #[must_use]
//    fn add_point_gen(
//        &mut self,
//        coords: (f64, f64, f64),
//        mesh_size: Option<f64>,
//    ) -> GmshResult<PointTag>;
//
//    /// Add a point to the model by specifying its coordinates.
//    #[must_use]
//    fn add_point(&mut self, x: f64, y: f64, z: f64) -> GmshResult<PointTag> {
//        println!("added basic point");
//        self.add_point_gen((x, y, z), None)
//    }
//
//    /// Add a point to the model and specify a target mesh size `lc` there.
//    #[must_use]
//    fn add_point_with_lc(&mut self, x: f64, y: f64, z: f64, lc: f64) -> GmshResult<PointTag> {
//        println!("added point with lc");
//        self.add_point_gen((x, y, z), Some(lc))
//    }
//
//    /// Remove a point from the model.
//    fn remove_point(&mut self, p: PointTag) -> GmshResult<()>;
//
//    /// Add a straight line between two points.
//    fn add_line(&mut self, p1: PointTag, p2: PointTag) -> GmshResult<CurveTag>;
//
//    /// Add a curve loop from a closed set of curves.
//    fn add_curve_loop(&mut self, curves: &[CurveTag]) -> GmshResult<WireTag>;
//
//    /// Add a surface from a WireTag of a closed curve set.
//    fn add_plane_surface(&mut self, closed_curve: WireTag) -> GmshResult<SurfaceTag>;
//
//    /// Add a surface with holes from a WireTag of a boundary and a Wiretags of the holes.
//    fn add_plane_surface_with_holes(&mut self, boundary: WireTag, holes: &[WireTag]) -> GmshResult<SurfaceTag>;
//
//    #[doc(hidden)]
//    fn add_plane_surface_gen(&mut self, curves: &[WireTag]) -> GmshResult<SurfaceTag>;
//
//    #[doc(hidden)]
//    fn curve_or_surface_op<T: Into<CurveOrSurface>>(&mut self, gen_entity: T);
//
//    /// Mesh the model.
//    // probably should move this to a dedicated model class
//    // with an inner Option(Mesh) and Option(Geo)
//    fn generate_mesh(&mut self, dim: i32) -> GmshResult<()> {
//        self.set_current()?;
//        // synchronize by default?
//        self.synchronize()?;
//        unsafe {
//            let mut ierr: c_int = 0;
//            gmsh_sys::gmshModelMeshGenerate(dim, &mut ierr);
//            check_model_error!(ierr, ())
//        }
//    }
//}

// // Implement kernel functions that follow a naming pattern.
// #[doc(hidden)]
// #[macro_export]
// macro_rules! impl_kernel {
//
//     // internal macro rules for prefixing similar geometry kernel functions
//     // Idea adapted from the rust-blas package here:
//     // https://github.com/mikkyang/rust-blas/pull/12
//     (@kernel_prefix Geo, $fn_name: ident) => {
//         crate::interface::geo::$fn_name
//     };
//
//     (@kernel_prefix Occ, $fn_name: ident) => {
//         crate::interface::occ::$fn_name
//     };
//
//     ($kernel_name: ident) => {
//         impl<'a> GeoKernel for $kernel_name<'a> {
//             //-----------------------------------------------------------------
//             // General kernel methods for all kernels
//             //-----------------------------------------------------------------
//
//             fn name(&self) -> &'static str {
//                 self.name
//             }
//
//             fn c_name(&self) -> &CStr {
//                 &self.c_name
//             }
//
//             fn remove(self) -> GmshResult<()> {
//                 // first set this model to the current model.
//                 self.set_current()?;
//                 // now, remove the current model
//                 unsafe {
//                     let mut ierr: c_int = 0;
//                     gmsh_sys::gmshModelRemove(&mut ierr);
//                     check_main_error!(ierr, ())
//                 }
//             }
//
//             //-----------------------------------------------------------------
//             // Prefix methods with a naming pattern for each kernel
//             //-----------------------------------------------------------------
//
//             /// Synchronize the geometry model.
//             fn synchronize(&mut self) -> GmshResult<()> {
//                 self.set_current()?;
//                 unsafe {
//                     let mut ierr: c_int = 0;
//                     let sync_fn = impl_kernel!(@kernel_prefix $kernel_name, synchronize);
//                     sync_fn(&mut ierr);
//                     check_model_error!(ierr, ())
//                 }
//             }
//
//             #[doc(hidden)]
//             #[must_use]
//             fn add_point_gen(
//                 &mut self,
//                 coords: (f64, f64, f64),
//                 mesh_size: Option<f64>,
//             ) -> GmshResult<PointTag> {
//                 self.set_current()?;
//
//                 let (x, y, z) = coords;
//
//                 let lc = mesh_size.unwrap_or(0.);
//                 let auto_number = -1;
//
//                 unsafe {
//                     let mut ierr: c_int = 0;
//                     let add_point_fn = impl_kernel!(@kernel_prefix $kernel_name, add_point);
//                     let out_tag = add_point_fn(x, y, z, lc, auto_number, &mut ierr);
//                     check_model_error!(ierr, PointTag(out_tag))
//                 }
//             }
//
//             /// Delete a point from the Gmsh model.
//             // todo: Genericize this for all GeometryTags
//             fn remove_point(&mut self, p: PointTag) -> GmshResult<()> {
//                 self.set_current()?;
//                 let raw_tag = p.0;
//                 unsafe {
//                     let vec_len = 1;
//                     let is_recursive = 0;
//                     let mut ierr: c_int = 0;
//                     let remove_point_fn = impl_kernel!(@kernel_prefix $kernel_name, remove_point);
//                     remove_point_fn([raw_tag].as_mut_ptr(), vec_len, is_recursive, &mut ierr);
//                     check_model_error!(ierr, ())
//                 }
//             }
//
//             /// Add a straight line between two points.
//             #[must_use]
//             fn add_line(&mut self, p1: PointTag, p2: PointTag) -> GmshResult<CurveTag> {
//                 self.set_current()?;
//                 let auto_number = -1;
//                 unsafe {
//                     let mut ierr: c_int = 0;
//                     let add_line_fn = impl_kernel!(@kernel_prefix $kernel_name, add_line);
//                     let out_tag = add_line_fn(p1.to_raw(), p2.to_raw(), auto_number, &mut ierr);
//                     check_model_error!(ierr, CurveTag(out_tag))
//                 }
//             }
//
//             /// Add a curve loop from a closed set of curves.
//             #[must_use]
//             fn add_curve_loop(&mut self, curves: &[CurveTag]) -> GmshResult<WireTag> {
//                 self.set_current()?;
//                 let mut raw_tags: Vec<_> = curves.iter().map(|c| c.to_raw()).collect();
//                 let auto_number = -1;
//                 unsafe {
//                     let mut ierr: c_int = 0;
//                     let add_curve_loop_fn = impl_kernel!(@kernel_prefix $kernel_name, add_curve_loop);
//                     let out_tag = add_curve_loop_fn(raw_tags.as_mut_ptr(), raw_tags.len() as usize, auto_number, &mut ierr);
//                     check_model_error!(ierr, WireTag(out_tag))
//                 }
//             }
//
//             /// Add a surface from a WireTag of a closed curve set.
//             #[must_use]
//             fn add_plane_surface(&mut self, boundary: WireTag) -> GmshResult<SurfaceTag> {
//                 self.add_plane_surface_gen(&[boundary])
//             }
//
//             /// Add a surface with holes.
//             #[must_use]
//             fn add_plane_surface_with_holes(&mut self, boundary: WireTag, holes: &[WireTag]) -> GmshResult<SurfaceTag> {
//                 self.add_plane_surface_gen(&[&[boundary], holes].concat())
//             }
//
//             #[doc(hidden)]
//             fn add_plane_surface_gen(&mut self, curves: &[WireTag]) -> GmshResult<SurfaceTag> {
//                 self.set_current()?;
//                 let mut raw_tags: Vec<_> = curves.iter().map(|c| c.to_raw()).collect();
//                 let auto_number = -1;
//                 unsafe {
//                     let mut ierr: c_int = 0;
//                     let add_plane_fn = impl_kernel!(@kernel_prefix $kernel_name, add_plane_surface);
//                     let out_tag = add_plane_fn(raw_tags.as_mut_ptr(), raw_tags.len() as usize, auto_number, &mut ierr);
//                     check_model_error!(ierr, SurfaceTag(out_tag))
//                 }
//             }
//
//             // idea for a certain operation that only works for curves and surfaces
//             fn curve_or_surface_op<T: Into<CurveOrSurface>>(&mut self, gen_entity: T) {
//                 let entity = gen_entity.into();
//                 match entity {
//                     CurveOrSurface::Curve(CurveTag(ct)) => println!("Curve with tag {:?}", ct),
//                     CurveOrSurface::Surface(SurfaceTag(ct)) => {
//                         println!("Surface with tag {:?}", ct)
//                     }
//                 }
//             }
//         }
//     };
// }

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
/// A point tag. Points are used to build larger shapes. 0D.
pub struct PointTag(i32);
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
/// A curve tag, built from points. The curve type includes straight lines. 1D.
pub struct CurveTag(i32);

/// Curves have a direction from start to end.
impl Neg for CurveTag {
    type Output = Self;
    /// Reverse the curve's direction.
    fn neg(self) -> Self {
        match self {
            CurveTag(i) => Self(-i),
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
/// A wire tag. Wires are built from curves. Wires are a path of multiple curves. 1.5D.
pub struct WireTag(i32);
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
/// A surface tag. Surfaces are built from closed wires. 2D.
pub struct SurfaceTag(i32);
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
/// A shell tag. Shells are built from surface loops. 2.5D.
pub struct ShellTag(i32);
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
/// A volume tag. Volumes are built from closed shells. 3D.
pub struct VolumeTag(i32);

/// A trait for the different tags used by Gmsh.
trait GmshTag {
    /// The raw tag integer passed to the Gmsh library.
    fn to_raw(&self) -> i32;
}

impl GmshTag for PointTag {
    fn to_raw(&self) -> i32 {
        self.0
    }
}

impl GmshTag for CurveTag {
    fn to_raw(&self) -> i32 {
        self.0
    }
}

impl GmshTag for WireTag {
    fn to_raw(&self) -> i32 {
        self.0
    }
}

impl GmshTag for SurfaceTag {
    fn to_raw(&self) -> i32 {
        self.0
    }
}

impl From<PointTag> for BasicShape {
    fn from(t: PointTag) -> BasicShape {
        BasicShape::Point(t)
    }
}

impl From<CurveTag> for BasicShape {
    fn from(t: CurveTag) -> BasicShape {
        BasicShape::Curve(t)
    }
}

/// Private module for sets of geometries passed and returned from functions.
///
/// Gmsh operations can be on multiple known types. We use enums for a compile-time
/// check that the type is OK to use with that function.
mod geometry_groups {
    use super::*;

    #[derive(Debug, Copy, Clone)]
    /// The basic geometry types (points, curves, surfaces, and volumes).
    pub enum BasicShape {
        Point(PointTag),
        Curve(CurveTag),
        Surface(SurfaceTag),
        Volume(VolumeTag),
    }

    #[derive(Debug, Copy, Clone)]
    /// The full set of geometry types (`BasicGeometries` + wires + shells).
    pub enum GeneralShape {
        Point(PointTag),
        Curve(CurveTag),
        Wire(WireTag),
        Surface(SurfaceTag),
        Shell(ShellTag),
        Volume(VolumeTag),
    }

    #[derive(Debug, Copy, Clone)]
    /// Only curves or surfaces.
    pub enum CurveOrSurface {
        Curve(CurveTag),
        Surface(SurfaceTag),
    }
}

use geometry_groups::BasicShape;
use geometry_groups::CurveOrSurface;

type c_or_s = CurveOrSurface;

impl From<CurveTag> for c_or_s {
    fn from(t: CurveTag) -> c_or_s {
        CurveOrSurface::Curve(t)
    }
}

impl From<SurfaceTag> for CurveOrSurface {
    fn from(t: SurfaceTag) -> CurveOrSurface {
        CurveOrSurface::Surface(t)
    }
}

/// Associated geometry information.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct PhysicalGroupTag(i32);