@@ -111,3 +111,64 @@ impl From<Circle> for Mesh {
111111 Mesh :: from ( RegularPolygon :: from ( circle) )
112112 }
113113}
114+
115+ #[ cfg( test) ]
116+ mod tests {
117+ use crate :: mesh:: shape:: RegularPolygon ;
118+ use crate :: mesh:: { Mesh , VertexAttributeValues } ;
119+
120+ /// Sin/cos and multiplication computations result in numbers like 0.4999999.
121+ /// Round these to numbers we expect like 0.5.
122+ fn fix_floats < const N : usize > ( points : & mut [ [ f32 ; N ] ] ) {
123+ for point in points. iter_mut ( ) {
124+ for coord in point. iter_mut ( ) {
125+ let round = ( * coord * 2. ) . round ( ) / 2. ;
126+ if ( * coord - round) . abs ( ) < 0.00001 {
127+ * coord = round;
128+ }
129+ }
130+ }
131+ }
132+
133+ #[ test]
134+ fn test_regular_polygon ( ) {
135+ let mut mesh = Mesh :: from ( RegularPolygon {
136+ radius : 7. ,
137+ sides : 4 ,
138+ } ) ;
139+
140+ let Some ( VertexAttributeValues :: Float32x3 ( mut positions) ) =
141+ mesh. remove_attribute ( Mesh :: ATTRIBUTE_POSITION )
142+ else {
143+ panic ! ( "Expected positions f32x3" ) ;
144+ } ;
145+ let Some ( VertexAttributeValues :: Float32x2 ( mut uvs) ) =
146+ mesh. remove_attribute ( Mesh :: ATTRIBUTE_UV_0 )
147+ else {
148+ panic ! ( "Expected uvs f32x2" ) ;
149+ } ;
150+ let Some ( VertexAttributeValues :: Float32x3 ( normals) ) =
151+ mesh. remove_attribute ( Mesh :: ATTRIBUTE_NORMAL )
152+ else {
153+ panic ! ( "Expected normals f32x3" ) ;
154+ } ;
155+
156+ fix_floats ( & mut positions) ;
157+ fix_floats ( & mut uvs) ;
158+
159+ assert_eq ! (
160+ [
161+ [ 0.0 , 7.0 , 0.0 ] ,
162+ [ 7.0 , 0.0 , 0.0 ] ,
163+ [ 0.0 , -7.0 , 0.0 ] ,
164+ [ -7.0 , 0.0 , 0.0 ] ,
165+ ] ,
166+ & positions[ ..]
167+ ) ;
168+
169+ // Note V coordinate increases in the opposite direction to the Y coordinate.
170+ assert_eq ! ( [ [ 0.5 , 0.0 ] , [ 1.0 , 0.5 ] , [ 0.5 , 1.0 ] , [ 0.0 , 0.5 ] ] , & uvs[ ..] ) ;
171+
172+ assert_eq ! ( & [ [ 0.0 , 0.0 , 1.0 ] ; 4 ] , & normals[ ..] ) ;
173+ }
174+ }
0 commit comments