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
use std::rc::Rc;
use std::sync::Arc;
use std::cell::RefCell;

use math::*;

use crate::core::geometry::*;
use crate::core::resource::*;
use crate::util::uuid::OsRandNewV4;

#[derive(Debug)]
pub struct FanGeometry
{
	uuid: uuid::Uuid,
	radius_top:f32,
	radius_bottom:f32,
	segments:u32,
	top_length:f32,
	bottom_length:f32,
	vertices:float3s,
	normals:float3s,
	texcoords:float2s,
	indices:Vec<u16>,
}

impl FanGeometry 
{
	pub fn new(top_radius:f32, bottom_radius:f32, segments:u32, top_length:f32, bottom_length:f32) -> Self 
	{
		let mut _vertices = Vec::new();
		let mut _indices = Vec::new();
		let mut _normals = Vec::new();
		let mut _texcoords = Vec::new();

		let top_start = -(f32::pi() + bottom_length) * 0.5;
		let top_segment = top_length / segments as f32;

		let bottom_start = -(f32::pi() + top_length) * 0.5;;
		let bottom_segment = bottom_length / segments as f32;

		for i in 0..segments + 1
		{
			let sin = f32::sin(bottom_start + i as f32 * bottom_segment);
			let cos = f32::cos(bottom_start + i as f32 * bottom_segment);

			let v = float!(bottom_radius * cos, -bottom_radius * sin, 0.0);

			_vertices.push(v);
			_normals.push(float!(0.0,0.0,1.0));
			_texcoords.push(float!(i as f32 / segments as f32, 1.0));
		}

		for i in 0..segments + 1
		{
			let sin = f32::sin(top_start + i as f32 * top_segment);
			let cos = f32::cos(top_start + i as f32 * top_segment);

			let v = float!(top_radius * cos, -top_radius * sin, 0.0);

			_vertices.push(v);
			_normals.push(float!(0.0,0.0,1.0));
			_texcoords.push(float!(i as f32 / segments as f32, 0.0));
		}

		for i in 0..segments
		{
			let v1 = i;
			let v3 = i + 1 + segments;
			let v4 = i + 2 + segments;

			_indices.push(v1 as u16);
			_indices.push(v3 as u16);
			_indices.push(v4 as u16);
		}

		for i in 0..segments
		{
			let v1 = i;
			let v2 = i + 1;
			let v4 = i + 2 + segments;

			_indices.push(v1 as u16);
			_indices.push(v4 as u16);
			_indices.push(v2 as u16);
		}

		Self
		{
			uuid:uuid::Uuid::new_v4_osrng(),
			radius_top:top_radius,
			radius_bottom:bottom_radius,
			segments:segments,
			top_length:top_length,
			bottom_length:bottom_length,
			vertices:_vertices,
			normals:_normals,
			texcoords:_texcoords,
			indices:_indices,
		}
	}

	pub fn new_1(radius_top:f32, radius_bottom:f32, segments:u32) -> Self
	{
		FanGeometry::new(radius_top, radius_bottom, segments, f32::pi(), f32::pi())
	}

	pub fn top_radius(&self) -> f32
	{
		self.radius_top
	}

	pub fn top_length(&self) -> f32
	{
		self.top_length
	}

	pub fn bottom_radius(&self) -> f32
	{
		self.radius_top
	}

	pub fn bottom_length(&self) -> f32
	{
		self.bottom_length
	}

	pub fn segments(&self) -> u32
	{
		self.segments
	}
}

impl Geometry for FanGeometry
{
	fn vertices(&self) -> &[float3]
	{
		&self.vertices[..]
	}

	fn normals(&self) -> &[float3]
	{
		&self.normals[..]
	}

	fn texcoords(&self) -> &[float2]
	{
		&self.texcoords[..]
	}

	fn indices(&self) -> &[u16]
	{
		&self.indices[..]
	}
}

impl Resource for FanGeometry
{
	#[inline]
	fn uuid(&self) -> &uuid::Uuid
	{
		&self.uuid
	}
}

impl From<FanGeometry> for Rc<Geometry + 'static>
{
	fn from(shape:FanGeometry) -> Self
	{
		Rc::new(shape)
	}
}

impl From<FanGeometry> for Arc<Geometry + 'static>
{
	fn from(shape:FanGeometry) -> Self
	{
		Arc::new(shape)
	}
}

impl From<FanGeometry> for Rc<RefCell<Geometry + 'static>>
{
	fn from(shape:FanGeometry) -> Self
	{
		Rc::new(RefCell::new(shape))
	}
}

impl From<FanGeometry> for Arc<RefCell<Geometry + 'static>>
{
	fn from(shape:FanGeometry) -> Self
	{
		Arc::new(RefCell::new(shape))
	}
}