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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
use na::{self, Isometry2, Point2, Point3, Translation2, UnitComplex, Vector2};

use crate::planar_camera::PlanarCamera;
use crate::resource::{
    PlanarMaterial, PlanarMaterialManager, PlanarMesh, PlanarMeshManager, Texture, TextureManager,
};
use crate::scene::PlanarObject;
use std::cell::{Ref, RefCell, RefMut};
use std::f32;
use std::mem;
use std::path::Path;
use std::rc::Rc;

// XXX: once something like `fn foo(self: Rc<RefCell<PlanarSceneNode>>)` is allowed, this extra struct
// will not be needed any more.
/// The datas contained by a `PlanarSceneNode`.
pub struct PlanarSceneNodeData {
    local_scale: Vector2<f32>,
    local_transform: Isometry2<f32>,
    world_scale: Vector2<f32>,
    world_transform: Isometry2<f32>,
    visible: bool,
    up_to_date: bool,
    children: Vec<PlanarSceneNode>,
    object: Option<PlanarObject>,
    // FIXME: use Weak pointers instead of the raw pointer.
    parent: Option<*const RefCell<PlanarSceneNodeData>>,
}

/// A node of the scene graph.
///
/// This may represent a group of other nodes, and/or contain an object that can be rendered.
#[derive(Clone)]
pub struct PlanarSceneNode {
    data: Rc<RefCell<PlanarSceneNodeData>>,
}

impl PlanarSceneNodeData {
    // XXX: Because `node.borrow_mut().parent = Some(self.data.downgrade())`
    // causes a weird compiler error:
    //
    // ```
    // error: mismatched types: expected `&std::cell::RefCell<scene::scene_node::PlanarSceneNodeData>`
    // but found
    // `std::option::Option<std::rc::Weak<std::cell::RefCell<scene::scene_node::PlanarSceneNodeData>>>`
    // (expe cted &-ptr but found enum std::option::Option)
    // ```
    fn set_parent(&mut self, parent: *const RefCell<PlanarSceneNodeData>) {
        self.parent = Some(parent);
    }

    // XXX: this exists because of a similar bug as `set_parent`.
    fn remove_from_parent(&mut self, to_remove: &PlanarSceneNode) {
        let _ = self.parent.as_ref().map(|p| unsafe {
            let mut bp = (**p).borrow_mut();
            bp.remove(to_remove)
        });
    }

    fn remove(&mut self, o: &PlanarSceneNode) {
        if let Some(i) = self
            .children
            .iter()
            .rposition(|e| std::ptr::eq(&*o.data, &*e.data))
        {
            let _ = self.children.swap_remove(i);
        }
    }

    /// Whether this node contains an `PlanarObject`.
    #[inline]
    pub fn has_object(&self) -> bool {
        self.object.is_some()
    }

    /// Whether this node has no parent.
    #[inline]
    pub fn is_root(&self) -> bool {
        self.parent.is_none()
    }

    /// Render the scene graph rooted by this node.
    pub fn render(&mut self, camera: &mut dyn PlanarCamera) {
        if self.visible {
            self.do_render(&na::one(), &Vector2::from_element(1.0), camera)
        }
    }

    fn do_render(
        &mut self,
        transform: &Isometry2<f32>,
        scale: &Vector2<f32>,
        camera: &mut dyn PlanarCamera,
    ) {
        if !self.up_to_date {
            self.up_to_date = true;
            self.world_transform = *transform * self.local_transform;
            self.world_scale = scale.component_mul(&self.local_scale);
        }

        if let Some(ref o) = self.object {
            o.render(&self.world_transform, &self.world_scale, camera)
        }

        for c in self.children.iter_mut() {
            let mut bc = c.data_mut();
            if bc.visible {
                bc.do_render(&self.world_transform, &self.world_scale, camera)
            }
        }
    }

    /// A reference to the object possibly contained by this node.
    #[inline]
    pub fn object(&self) -> Option<&PlanarObject> {
        self.object.as_ref()
    }

    /// A mutable reference to the object possibly contained by this node.
    #[inline]
    pub fn object_mut(&mut self) -> Option<&mut PlanarObject> {
        self.object.as_mut()
    }

    /// A reference to the object possibly contained by this node.
    ///
    /// # Failure
    /// Fails of this node does not contains an object.
    #[inline]
    pub fn get_object(&self) -> &PlanarObject {
        self.object()
            .expect("This scene node does not contain an PlanarObject.")
    }

    /// A mutable reference to the object possibly contained by this node.
    ///
    /// # Failure
    /// Fails of this node does not contains an object.
    #[inline]
    pub fn get_object_mut(&mut self) -> &mut PlanarObject {
        self.object_mut()
            .expect("This scene node does not contain an PlanarObject.")
    }

    ///////////////////~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HERE
    /* FIXME: the ~Any is kind of problematic here…
    /// Attaches user-defined data to the objects contained by this node and its children.
    #[inline]
    pub fn set_user_data(&mut self, user_data: ~Any) {
        self.apply_to_objects_mut(&mut |o| o.set_user_data(user_data))
    }
    */

    // FIXME: for all those set_stuff, would it be more per formant to add a special case for when
    // we are on a leaf? (to avoid the call to a closure required by the apply_to_*).
    /// Sets the material of the objects contained by this node and its children.
    #[inline]
    pub fn set_material(&mut self, material: Rc<RefCell<Box<dyn PlanarMaterial + 'static>>>) {
        self.apply_to_objects_mut(&mut |o| o.set_material(material.clone()))
    }

    /// Sets the material of the objects contained by this node and its children.
    ///
    /// The material must already have been registered as `name`.
    #[inline]
    pub fn set_material_with_name(&mut self, name: &str) {
        let material = PlanarMaterialManager::get_global_manager(|tm| {
            tm.get(name).unwrap_or_else(|| {
                panic!("Invalid attempt to use the unregistered material: {}", name)
            })
        });

        self.set_material(material)
    }

    /// Sets the width of the lines drawn for the objects contained by this node and its children.
    #[inline]
    pub fn set_lines_width(&mut self, width: f32) {
        self.apply_to_objects_mut(&mut |o| o.set_lines_width(width))
    }

    /// Sets the color of the lines drawn for the objects contained by this node and its children.
    #[inline]
    pub fn set_lines_color(&mut self, color: Option<Point3<f32>>) {
        self.apply_to_objects_mut(&mut |o| o.set_lines_color(color))
    }

    /// Sets the size of the points drawn for the objects contained by this node and its children.
    #[inline]
    pub fn set_points_size(&mut self, size: f32) {
        self.apply_to_objects_mut(&mut |o| o.set_points_size(size))
    }

    /// Activates or deactivates the rendering of the surfaces of the objects contained by this node and its
    /// children.
    #[inline]
    pub fn set_surface_rendering_activation(&mut self, active: bool) {
        self.apply_to_objects_mut(&mut |o| o.set_surface_rendering_activation(active))
    }

    /// Activates or deactivates backface culling for the objects contained by this node and its
    /// children.
    #[inline]
    pub fn enable_backface_culling(&mut self, active: bool) {
        self.apply_to_objects_mut(&mut |o| o.enable_backface_culling(active))
    }

    /// Mutably accesses the vertices of the objects contained by this node and its children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn modify_vertices<F: FnMut(&mut Vec<Point2<f32>>)>(&mut self, f: &mut F) {
        self.apply_to_objects_mut(&mut |o| o.modify_vertices(f))
    }

    /// Accesses the vertices of the objects contained by this node and its children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn read_vertices<F: FnMut(&[Point2<f32>])>(&self, f: &mut F) {
        self.apply_to_objects(&mut |o| o.read_vertices(f))
    }

    /// Mutably accesses the faces of the objects contained by this node and its children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn modify_faces<F: FnMut(&mut Vec<Point3<u16>>)>(&mut self, f: &mut F) {
        self.apply_to_objects_mut(&mut |o| o.modify_faces(f))
    }

    /// Accesses the faces of the objects contained by this node and its children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn read_faces<F: FnMut(&[Point3<u16>])>(&self, f: &mut F) {
        self.apply_to_objects(&mut |o| o.read_faces(f))
    }

    /// Mutably accesses the texture coordinates of the objects contained by this node and its
    /// children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn modify_uvs<F: FnMut(&mut Vec<Point2<f32>>)>(&mut self, f: &mut F) {
        self.apply_to_objects_mut(&mut |o| o.modify_uvs(f))
    }

    /// Accesses the texture coordinates of the objects contained by this node and its children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn read_uvs<F: FnMut(&[Point2<f32>])>(&self, f: &mut F) {
        self.apply_to_objects(&mut |o| o.read_uvs(f))
    }

    /// Get the visibility status of node.
    #[inline]
    pub fn is_visible(&self) -> bool {
        self.visible
    }

    /// Sets the visibility of this node.
    ///
    /// The node and its children are not rendered if it is not visible.
    #[inline]
    pub fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }

    /// Sets the color of the objects contained by this node and its children.
    ///
    /// Colors components must be on the range `[0.0, 1.0]`.
    #[inline]
    pub fn set_color(&mut self, r: f32, g: f32, b: f32) {
        self.apply_to_objects_mut(&mut |o| o.set_color(r, g, b))
    }

    /// Sets the texture of the objects contained by this node and its children.
    ///
    /// The texture is loaded from a file and registered by the global `TextureManager`.
    ///
    /// # Arguments
    ///   * `path` - relative path of the texture on the disk
    ///   * `name` - &str identifier to store this texture under
    #[inline]
    pub fn set_texture_from_file(&mut self, path: &Path, name: &str) {
        let texture = TextureManager::get_global_manager(|tm| tm.add(path, name));

        self.set_texture(texture)
    }

    /// Sets the texture of the objects contained by this node and its children.
    ///
    /// The texture is loaded from a byte slice and registered by the global `TextureManager`.
    ///
    /// # Arguments
    ///   * `path` - relative path of the texture on the disk
    ///   * `image_data` - slice of bytes containing encoded image
    ///   * `name` - &str identifier to store this texture under
    #[inline]
    pub fn set_texture_from_memory(&mut self, image_data: &[u8], name: &str) {
        let texture =
            TextureManager::get_global_manager(|tm| tm.add_image_from_memory(image_data, name));

        self.set_texture(texture)
    }

    /// Sets the texture of the objects contained by this node and its children.
    ///
    /// The texture must already have been registered as `name`.
    #[inline]
    pub fn set_texture_with_name(&mut self, name: &str) {
        let texture = TextureManager::get_global_manager(|tm| {
            tm.get(name).unwrap_or_else(|| {
                panic!("Invalid attempt to use the unregistered texture: {}", name)
            })
        });

        self.set_texture(texture)
    }

    /// Sets the texture of the objects contained by this node and its children.
    pub fn set_texture(&mut self, texture: Rc<Texture>) {
        self.apply_to_objects_mut(&mut |o| o.set_texture(texture.clone()))
    }

    /// Applies a closure to each object contained by this node and its children.
    #[inline]
    pub fn apply_to_objects_mut<F: FnMut(&mut PlanarObject)>(&mut self, f: &mut F) {
        if let Some(ref mut o) = self.object {
            f(o)
        }

        for c in self.children.iter_mut() {
            c.data_mut().apply_to_objects_mut(f)
        }
    }

    /// Applies a closure to each object contained by this node and its children.
    #[inline]
    pub fn apply_to_objects<F: FnMut(&PlanarObject)>(&self, f: &mut F) {
        if let Some(ref o) = self.object {
            f(o)
        }

        for c in self.children.iter() {
            c.data().apply_to_objects(f)
        }
    }

    // FIXME: add folding?

    /// Sets the local scaling factors of the object.
    #[inline]
    pub fn set_local_scale(&mut self, sx: f32, sy: f32) {
        self.invalidate();
        self.local_scale = Vector2::new(sx, sy)
    }

    /// Returns the scaling factors of the object.
    #[inline]
    pub fn local_scale(&self) -> Vector2<f32> {
        self.local_scale
    }

    /// This node local transformation.
    #[inline]
    pub fn local_transformation(&self) -> Isometry2<f32> {
        self.local_transform
    }

    /// Inverse of this node local transformation.
    #[inline]
    pub fn inverse_local_transformation(&self) -> Isometry2<f32> {
        self.local_transform.inverse()
    }

    /// This node world transformation.
    ///
    /// This will force an update of the world transformation of its parents if they have been
    /// invalidated.
    #[inline]
    #[allow(mutable_transmutes)]
    pub fn world_transformation(&self) -> Isometry2<f32> {
        // NOTE: this is to have some kind of laziness without a `&mut self`.
        unsafe {
            let mself: &mut PlanarSceneNodeData = mem::transmute(self);
            mself.update();
        }
        self.world_transform
    }

    /// The inverse of this node world transformation.
    ///
    /// This will force an update of the world transformation of its parents if they have been
    /// invalidated.
    #[inline]
    #[allow(mutable_transmutes)]
    pub fn inverse_world_transformation(&self) -> Isometry2<f32> {
        // NOTE: this is to have some kind of laziness without a `&mut self`.
        unsafe {
            let mself: &mut PlanarSceneNodeData = mem::transmute(self);
            mself.update();
        }
        self.local_transform.inverse()
    }

    /// Appends a transformation to this node local transformation.
    #[inline]
    pub fn append_transformation(&mut self, t: &Isometry2<f32>) {
        self.invalidate();
        self.local_transform = t * self.local_transform
    }

    /// Prepends a transformation to this node local transformation.
    #[inline]
    pub fn prepend_to_local_transformation(&mut self, t: &Isometry2<f32>) {
        self.invalidate();
        self.local_transform *= t;
    }

    /// Set this node local transformation.
    #[inline]
    pub fn set_local_transformation(&mut self, t: Isometry2<f32>) {
        self.invalidate();
        self.local_transform = t
    }

    /// This node local translation.
    #[inline]
    pub fn local_translation(&self) -> Translation2<f32> {
        self.local_transform.translation
    }

    /// The inverse of this node local translation.
    #[inline]
    pub fn inverse_local_translation(&self) -> Translation2<f32> {
        self.local_transform.translation.inverse()
    }

    /// Appends a translation to this node local transformation.
    #[inline]
    pub fn append_translation(&mut self, t: &Translation2<f32>) {
        self.invalidate();
        self.local_transform = t * self.local_transform
    }

    /// Prepends a translation to this node local transformation.
    #[inline]
    pub fn prepend_to_local_translation(&mut self, t: &Translation2<f32>) {
        self.invalidate();
        self.local_transform *= t
    }

    /// Sets the local translation of this node.
    #[inline]
    pub fn set_local_translation(&mut self, t: Translation2<f32>) {
        self.invalidate();
        self.local_transform.translation = t
    }

    /// This node local rotation.
    #[inline]
    pub fn local_rotation(&self) -> UnitComplex<f32> {
        self.local_transform.rotation
    }

    /// The inverse of this node local rotation.
    #[inline]
    pub fn inverse_local_rotation(&self) -> UnitComplex<f32> {
        self.local_transform.rotation.inverse()
    }

    /// Appends a rotation to this node local transformation.
    #[inline]
    pub fn append_rotation(&mut self, r: &UnitComplex<f32>) {
        self.invalidate();
        self.local_transform = r * self.local_transform
    }

    /// Appends a rotation to this node local transformation.
    #[inline]
    pub fn append_rotation_wrt_center(&mut self, r: &UnitComplex<f32>) {
        self.invalidate();
        self.local_transform.append_rotation_wrt_center_mut(r)
    }

    /// Prepends a rotation to this node local transformation.
    #[inline]
    pub fn prepend_to_local_rotation(&mut self, r: &UnitComplex<f32>) {
        self.invalidate();
        self.local_transform *= r
    }

    /// Sets the local rotation of this node.
    #[inline]
    pub fn set_local_rotation(&mut self, r: UnitComplex<f32>) {
        self.invalidate();
        self.local_transform.rotation = r
    }

    fn invalidate(&mut self) {
        self.up_to_date = false;

        for c in self.children.iter_mut() {
            let mut dm = c.data_mut();

            if dm.up_to_date {
                dm.invalidate()
            }
        }
    }

    // FIXME: make this public?
    fn update(&mut self) {
        // NOTE: makin this test
        if !self.up_to_date {
            match self.parent {
                Some(ref mut p) => unsafe {
                    let mut dp = (**p).borrow_mut();

                    dp.update();
                    self.world_transform = self.local_transform * dp.world_transform;
                    self.world_scale = self.local_scale.component_mul(&dp.local_scale);
                    self.up_to_date = true;
                    return;
                },
                None => {}
            }

            // no parent
            self.world_transform = self.local_transform;
            self.world_scale = self.local_scale;
            self.up_to_date = true;
        }
    }
}

impl PlanarSceneNode {
    /// Creates a new scene node that is not rooted.
    pub fn new(
        local_scale: Vector2<f32>,
        local_transform: Isometry2<f32>,
        object: Option<PlanarObject>,
    ) -> PlanarSceneNode {
        let data = PlanarSceneNodeData {
            local_scale,
            local_transform,
            world_transform: local_transform,
            world_scale: local_scale,
            visible: true,
            up_to_date: false,
            children: Vec::new(),
            object,
            parent: None,
        };

        PlanarSceneNode {
            data: Rc::new(RefCell::new(data)),
        }
    }

    /// Creates a new empty, not rooted, node with identity transformations.
    pub fn new_empty() -> PlanarSceneNode {
        PlanarSceneNode::new(Vector2::from_element(1.0), na::one(), None)
    }

    /// Removes this node from its parent.
    pub fn unlink(&mut self) {
        let self_self = self.clone();
        self.data_mut().remove_from_parent(&self_self);
        self.data_mut().parent = None
    }

    /// The data of this scene node.
    pub fn data(&self) -> Ref<PlanarSceneNodeData> {
        self.data.borrow()
    }

    /// The data of this scene node.
    pub fn data_mut(&mut self) -> RefMut<PlanarSceneNodeData> {
        self.data.borrow_mut()
    }

    /*
     *
     * Methods to add objects.
     *
     */
    /// Adds a node without object to this node children.
    pub fn add_group(&mut self) -> PlanarSceneNode {
        let node = PlanarSceneNode::new_empty();

        self.add_child(node.clone());

        node
    }

    /// Adds a node as a child of `parent`.
    ///
    /// # Failures:
    /// Fails if `node` already has a parent.
    pub fn add_child(&mut self, node: PlanarSceneNode) {
        assert!(
            node.data().is_root(),
            "The added node must not have a parent yet."
        );

        let mut node = node;
        node.data_mut().set_parent(&*self.data);
        self.data_mut().children.push(node)
    }

    /// Adds a node containing an object to this node children.
    pub fn add_object(
        &mut self,
        local_scale: Vector2<f32>,
        local_transform: Isometry2<f32>,
        object: PlanarObject,
    ) -> PlanarSceneNode {
        let node = PlanarSceneNode::new(local_scale, local_transform, Some(object));

        self.add_child(node.clone());

        node
    }

    /// Adds a rectangle as a children of this node. The rectangle is initially axis-aligned and centered
    /// at (0, 0).
    ///
    /// # Arguments
    /// * `wx` - the rectangle extent along the x axis
    /// * `wy` - the rectangle extent along the y axis
    pub fn add_rectangle(&mut self, wx: f32, wy: f32) -> PlanarSceneNode {
        let res = self.add_geom_with_name("rectangle", Vector2::new(wx, wy));

        res.expect("Unable to load the default rectangle geometry.")
    }

    /// Adds a circle as a children of this node. The circle is initially centered at (0, 0, 0).
    ///
    /// # Arguments
    /// * `r` - the circle radius
    pub fn add_circle(&mut self, r: f32) -> PlanarSceneNode {
        let res = self.add_geom_with_name("circle", Vector2::new(r * 2.0, r * 2.0));

        res.expect("Unable to load the default circle geometry.")
    }

    /// Adds a 2D capsule as a children of this node. The capsule is initially centered at (0, 0).
    ///
    /// # Arguments
    /// * `r` - the circle radius
    pub fn add_capsule(&mut self, r: f32, h: f32) -> PlanarSceneNode {
        let name = format!("capsule_{}_{}", r, h);

        let mesh = PlanarMeshManager::get_global_manager(|mm| {
            if let Some(geom) = mm.get(&name) {
                geom
            } else {
                // Create the capsule geometry.
                let mut capsule_vtx = vec![Point2::origin()];
                let mut capsule_ids = Vec::new();
                let nsamples = 50;

                for i in 0..=nsamples {
                    let ang = (i as f32) / (nsamples as f32) * f32::consts::PI;
                    capsule_vtx.push(Point2::new(ang.cos() * r, ang.sin() * r + h / 2.0));
                    capsule_ids.push(Point3::new(
                        0,
                        capsule_vtx.len() as u16 - 2,
                        capsule_vtx.len() as u16 - 1,
                    ));
                }

                for i in nsamples..=nsamples * 2 {
                    let ang = (i as f32) / (nsamples as f32) * f32::consts::PI;
                    capsule_vtx.push(Point2::new(ang.cos() * r, ang.sin() * r - h / 2.0));
                    capsule_ids.push(Point3::new(
                        0,
                        capsule_vtx.len() as u16 - 2,
                        capsule_vtx.len() as u16 - 1,
                    ));
                }

                capsule_ids.push(Point3::new(0, capsule_vtx.len() as u16 - 1, 1));

                let capsule = PlanarMesh::new(capsule_vtx, capsule_ids, None, false);
                let mesh = Rc::new(RefCell::new(capsule));
                mm.add(mesh.clone(), &name);
                mesh
            }
        });

        self.add_mesh(mesh, Vector2::repeat(1.0))
    }

    /// Creates and adds a new object using the geometry registered as `geometry_name`.
    pub fn add_geom_with_name(
        &mut self,
        geometry_name: &str,
        scale: Vector2<f32>,
    ) -> Option<PlanarSceneNode> {
        PlanarMeshManager::get_global_manager(|mm| mm.get(geometry_name))
            .map(|g| self.add_mesh(g, scale))
    }

    /// Creates and adds a new object to this node children using a 2D mesh.
    pub fn add_mesh(
        &mut self,
        mesh: Rc<RefCell<PlanarMesh>>,
        scale: Vector2<f32>,
    ) -> PlanarSceneNode {
        let tex = TextureManager::get_global_manager(|tm| tm.get_default());
        let mat = PlanarMaterialManager::get_global_manager(|mm| mm.get_default());
        let object = PlanarObject::new(mesh, 1.0, 1.0, 1.0, tex, mat);

        self.add_object(scale, na::one(), object)
    }

    /// Creates and adds a new object to this node children using a convex polyline
    pub fn add_convex_polygon(
        &mut self,
        polygon: Vec<Point2<f32>>,
        scale: Vector2<f32>,
    ) -> PlanarSceneNode {
        let mut indices = Vec::new();

        for i in 1..polygon.len() - 1 {
            indices.push(Point3::new(0, i as u16, i as u16 + 1));
        }

        let mesh = PlanarMesh::new(polygon, indices, None, false);
        let tex = TextureManager::get_global_manager(|tm| tm.get_default());
        let mat = PlanarMaterialManager::get_global_manager(|mm| mm.get_default());
        let object = PlanarObject::new(Rc::new(RefCell::new(mesh)), 1.0, 1.0, 1.0, tex, mat);

        self.add_object(scale, na::one(), object)
    }

    /// Applies a closure to each object contained by this node and its children.
    #[inline]
    pub fn apply_to_scene_nodes_mut<F: FnMut(&mut PlanarSceneNode)>(&mut self, f: &mut F) {
        f(self);

        for c in self.data_mut().children.iter_mut() {
            c.apply_to_scene_nodes_mut(f)
        }
    }

    /// Applies a closure to each object contained by this node and its children.
    #[inline]
    pub fn apply_to_scene_nodes<F: FnMut(&PlanarSceneNode)>(&self, f: &mut F) {
        f(self);

        for c in self.data().children.iter() {
            c.apply_to_scene_nodes(f)
        }
    }

    //
    //
    // fwd
    //
    //

    /// Render the scene graph rooted by this node.
    pub fn render(&mut self, camera: &mut dyn PlanarCamera) {
        self.data_mut().render(camera)
    }

    /// Sets the material of the objects contained by this node and its children.
    #[inline]
    pub fn set_material(&mut self, material: Rc<RefCell<Box<dyn PlanarMaterial + 'static>>>) {
        self.data_mut().set_material(material)
    }

    /// Sets the material of the objects contained by this node and its children.
    #[inline]
    pub fn set_material_with_name(&mut self, name: &str) {
        self.data_mut().set_material_with_name(name)
    }

    /// Sets the width of the lines drawn for the objects contained by this node and its children.
    #[inline]
    pub fn set_lines_width(&mut self, width: f32) {
        self.data_mut().set_lines_width(width)
    }

    /// Sets the color of the lines drawn for the objects contained by this node and its children.
    #[inline]
    pub fn set_lines_color(&mut self, color: Option<Point3<f32>>) {
        self.data_mut().set_lines_color(color)
    }

    /// Sets the size of the points drawn for the objects contained by this node and its children.
    #[inline]
    pub fn set_points_size(&mut self, size: f32) {
        self.data_mut().set_points_size(size)
    }

    /// Activates or deactivates the rendering of the surfaces of the objects contained by this node and its
    /// children.
    #[inline]
    pub fn set_surface_rendering_activation(&mut self, active: bool) {
        self.data_mut().set_surface_rendering_activation(active)
    }

    /// Activates or deactivates backface culling for the objects contained by this node and its
    /// children.
    #[inline]
    pub fn enable_backface_culling(&mut self, active: bool) {
        self.data_mut().enable_backface_culling(active)
    }

    /// Mutably accesses the vertices of the objects contained by this node and its children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn modify_vertices<F: FnMut(&mut Vec<Point2<f32>>)>(&mut self, f: &mut F) {
        self.data_mut().modify_vertices(f)
    }

    /// Accesses the vertices of the objects contained by this node and its children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn read_vertices<F: FnMut(&[Point2<f32>])>(&self, f: &mut F) {
        self.data().read_vertices(f)
    }

    /// Mutably accesses the faces of the objects contained by this node and its children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn modify_faces<F: FnMut(&mut Vec<Point3<u16>>)>(&mut self, f: &mut F) {
        self.data_mut().modify_faces(f)
    }

    /// Accesses the faces of the objects contained by this node and its children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn read_faces<F: FnMut(&[Point3<u16>])>(&self, f: &mut F) {
        self.data().read_faces(f)
    }

    /// Mutably accesses the texture coordinates of the objects contained by this node and its
    /// children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn modify_uvs<F: FnMut(&mut Vec<Point2<f32>>)>(&mut self, f: &mut F) {
        self.data_mut().modify_uvs(f)
    }

    /// Accesses the texture coordinates of the objects contained by this node and its children.
    ///
    /// The provided closure is called once per object.
    #[inline(always)]
    pub fn read_uvs<F: FnMut(&[Point2<f32>])>(&self, f: &mut F) {
        self.data().read_uvs(f)
    }

    /// Get the visibility status of node.
    #[inline]
    pub fn is_visible(&self) -> bool {
        self.data().is_visible()
    }

    /// Sets the visibility of this node.
    ///
    /// The node and its children are not rendered if it is not visible.
    #[inline]
    pub fn set_visible(&mut self, visible: bool) {
        self.data_mut().set_visible(visible)
    }

    /// Sets the color of the objects contained by this node and its children.
    ///
    /// Colors components must be on the range `[0.0, 1.0]`.
    #[inline]
    pub fn set_color(&mut self, r: f32, g: f32, b: f32) {
        self.data_mut().set_color(r, g, b)
    }

    /// Sets the texture of the objects contained by this node and its children.
    ///
    /// The texture is loaded from a file and registered by the global `TextureManager`.
    ///
    /// # Arguments
    ///   * `path` - relative path of the texture on the disk
    ///   * `name` - &str to identify this texture in `TextureManager`
    #[inline]
    pub fn set_texture_from_file(&mut self, path: &Path, name: &str) {
        self.data_mut().set_texture_from_file(path, name)
    }

    /// Sets the texture of the objects contained by this node and its children.
    ///
    /// The texture is loaded from a file and registered by the global `TextureManager`.
    ///
    /// # Arguments
    ///   * `image_data` - slice of bytes containing encoded image
    ///   * `name` - &str to identify this texture in `TextureManager`

    pub fn set_texture_from_memory(&mut self, image_data: &[u8], name: &str) {
        self.data_mut().set_texture_from_memory(image_data, name)
    }

    /// Sets the texture of the objects contained by this node and its children.
    ///
    /// The texture must already have been registered as `name`.
    #[inline]
    pub fn set_texture_with_name(&mut self, name: &str) {
        self.data_mut().set_texture_with_name(name)
    }

    /// Sets the texture of the objects contained by this node and its children.
    pub fn set_texture(&mut self, texture: Rc<Texture>) {
        self.data_mut().set_texture(texture)
    }

    /// Sets the local scaling factors of the object.
    #[inline]
    pub fn set_local_scale(&mut self, sx: f32, sy: f32) {
        self.data_mut().set_local_scale(sx, sy)
    }

    /// Appends a transformation to this node local transformation.
    #[inline]
    pub fn append_transformation(&mut self, t: &Isometry2<f32>) {
        self.data_mut().append_transformation(t)
    }

    /// Prepends a transformation to this node local transformation.
    #[inline]
    pub fn prepend_to_local_transformation(&mut self, t: &Isometry2<f32>) {
        self.data_mut().prepend_to_local_transformation(t)
    }

    /// Set this node local transformation.
    #[inline]
    pub fn set_local_transformation(&mut self, t: Isometry2<f32>) {
        self.data_mut().set_local_transformation(t)
    }

    /// Appends a translation to this node local transformation.
    #[inline]
    pub fn append_translation(&mut self, t: &Translation2<f32>) {
        self.data_mut().append_translation(t)
    }

    /// Prepends a translation to this node local transformation.
    #[inline]
    pub fn prepend_to_local_translation(&mut self, t: &Translation2<f32>) {
        self.data_mut().prepend_to_local_translation(t)
    }

    /// Sets the local translation of this node.
    #[inline]
    pub fn set_local_translation(&mut self, t: Translation2<f32>) {
        self.data_mut().set_local_translation(t)
    }

    /// Appends a rotation to this node local transformation.
    #[inline]
    pub fn append_rotation(&mut self, r: &UnitComplex<f32>) {
        self.data_mut().append_rotation(r)
    }

    /// Appends a rotation to this node local transformation.
    #[inline]
    pub fn append_rotation_wrt_center(&mut self, r: &UnitComplex<f32>) {
        (*self.data_mut()).append_rotation_wrt_center(r)
    }

    /// Prepends a rotation to this node local transformation.
    #[inline]
    pub fn prepend_to_local_rotation(&mut self, r: &UnitComplex<f32>) {
        self.data_mut().prepend_to_local_rotation(r)
    }

    /// Sets the local rotation of this node.
    #[inline]
    pub fn set_local_rotation(&mut self, r: UnitComplex<f32>) {
        self.data_mut().set_local_rotation(r)
    }
}