VectSharp  2.2.1
A light library for C# vector graphics
Scene.cs
1 /*
2  VectSharp - A light library for C# vector graphics.
3  Copyright (C) 2020-2022 Giorgio Bianchini
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, version 3.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 using System;
19 using System.Collections.Generic;
20 
21 namespace VectSharp.ThreeD
22 {
23  /// <summary>
24  /// Represents a 3D scene.
25  /// </summary>
26  public interface IScene
27  {
28  /// <summary>
29  /// The <see cref="Element3D"/>s constituting the scene.
30  /// </summary>
31  IEnumerable<Element3D> SceneElements { get; }
32 
33  /// <summary>
34  /// Adds the specified <paramref name="element"/> to the scene.
35  /// </summary>
36  /// <param name="element">The <see cref="Element3D"/> to add.</param>
37  void AddElement(Element3D element);
38 
39  /// <summary>
40  /// Adds the specified <paramref name="elements"/> to the scene.
41  /// </summary>
42  /// <param name="elements">A collection of <see cref="Element3D"/>s to add.</param>
43  void AddRange(IEnumerable<Element3D> elements);
44 
45  /// <summary>
46  /// Replaces each element in the scene with the element returned by the <paramref name="replacementFunction"/>.
47  /// </summary>
48  /// <param name="replacementFunction">A function replacing each <see cref="Element3D"/> in the scene with another <see cref="Element3D"/>.</param>
49  void Replace(Func<Element3D, Element3D> replacementFunction);
50 
51  /// <summary>
52  /// Replaces each element in the scene with the element(s) returned by the <paramref name="replacementFunction"/>.
53  /// </summary>
54  /// <param name="replacementFunction">A function replacing each <see cref="Element3D"/> in the scene with 0 or more <see cref="Element3D"/>s.</param>
55  void Replace(Func<Element3D, IEnumerable<Element3D>> replacementFunction);
56 
57  /// <summary>
58  /// An object used to synchronise multithreaded rendering of the same scene.
59  /// </summary>
60  object SceneLock { get; }
61  }
62 
63  /// <summary>
64  /// Represents a 3D scene.
65  /// </summary>
66  public class Scene : IScene
67  {
68  /// <inheritdoc/>
69  public object SceneLock { get; }
70 
71  private List<Element3D> sceneElements;
72 
73  /// <inheritdoc/>
74  public IEnumerable<Element3D> SceneElements => sceneElements;
75 
76  /// <summary>
77  /// Creates a new <see cref="Scene"/>.
78  /// </summary>
79  public Scene()
80  {
81  sceneElements = new List<Element3D>();
82  this.SceneLock = new object();
83  }
84 
85  /// <inheritdoc/>
86  public void AddElement(Element3D element)
87  {
88  this.sceneElements.Add(element);
89  }
90 
91  /// <inheritdoc/>
92  public void AddRange(IEnumerable<Element3D> elements)
93  {
94  this.sceneElements.AddRange(elements);
95  }
96 
97  /// <inheritdoc/>
98  public void Replace(Func<Element3D, Element3D> replacementFunction)
99  {
100  for (int i = 0; i < sceneElements.Count; i++)
101  {
102  sceneElements[i] = replacementFunction(sceneElements[i]);
103  }
104  }
105 
106  /// <inheritdoc/>
107  public void Replace(Func<Element3D, IEnumerable<Element3D>> replacementFunction)
108  {
109  List<Element3D> newElements = new List<Element3D>(sceneElements.Count);
110 
111  for (int i = 0; i < this.sceneElements.Count; i++)
112  {
113  newElements.AddRange(replacementFunction(sceneElements[i]));
114  }
115 
116  newElements.TrimExcess();
117 
118  this.sceneElements = newElements;
119  }
120  }
121 }
VectSharp.ThreeD.IScene.SceneLock
object SceneLock
An object used to synchronise multithreaded rendering of the same scene.
Definition: Scene.cs:60
VectSharp.ThreeD.Scene.AddRange
void AddRange(IEnumerable< Element3D > elements)
Adds the specified elements to the scene.
Definition: Scene.cs:92
VectSharp.ThreeD.Scene.Replace
void Replace(Func< Element3D, IEnumerable< Element3D >> replacementFunction)
Replaces each element in the scene with the element(s) returned by the replacementFunction .
Definition: Scene.cs:107
VectSharp.ThreeD.Scene.Scene
Scene()
Creates a new Scene.
Definition: Scene.cs:79
VectSharp.ThreeD
Definition: Lights.cs:23
VectSharp.ThreeD.IScene.AddElement
void AddElement(Element3D element)
Adds the specified element to the scene.
VectSharp.ThreeD.Scene.SceneLock
object SceneLock
Definition: Scene.cs:69
VectSharp.ThreeD.Scene.SceneElements
IEnumerable< Element3D > SceneElements
Definition: Scene.cs:74
VectSharp.ThreeD.IScene.SceneElements
IEnumerable< Element3D > SceneElements
The Element3Ds constituting the scene.
Definition: Scene.cs:31
VectSharp.ThreeD.Scene.Replace
void Replace(Func< Element3D, Element3D > replacementFunction)
Replaces each element in the scene with the element returned by the replacementFunction .
Definition: Scene.cs:98
VectSharp.ThreeD.Scene.AddElement
void AddElement(Element3D element)
Adds the specified element to the scene.
Definition: Scene.cs:86
VectSharp.ThreeD.IScene.Replace
void Replace(Func< Element3D, Element3D > replacementFunction)
Replaces each element in the scene with the element returned by the replacementFunction .
VectSharp.ThreeD.IScene
Represents a 3D scene.
Definition: Scene.cs:27
VectSharp.ThreeD.IScene.Replace
void Replace(Func< Element3D, IEnumerable< Element3D >> replacementFunction)
Replaces each element in the scene with the element(s) returned by the replacementFunction .
VectSharp.ThreeD.Scene
Represents a 3D scene.
Definition: Scene.cs:67
VectSharp.ThreeD.IScene.AddRange
void AddRange(IEnumerable< Element3D > elements)
Adds the specified elements to the scene.