commit 33584688448cd34c7e92be1d27269e171845e2d7
parent d630e99171576f765361549d3ec09bcd193ace57
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 29 Oct 2015 08:18:03 +0100
Add the cylinder mesh
Add the function schiff_mesh_init_cylinder that initialises a
schiff_mesh with the geometry of a cylinder discretized in N slices,
with N is a user defined parameter.
Diffstat:
2 files changed, 82 insertions(+), 1 deletion(-)
diff --git a/src/schiff_mesh.c b/src/schiff_mesh.c
@@ -143,6 +143,81 @@ error:
goto exit;
}
+res_T
+schiff_mesh_init_cylinder
+ (struct mem_allocator* allocator,
+ struct schiff_mesh* cylinder,
+ const unsigned nsteps)
+{
+ const double step = 2*PI / (double)nsteps;
+ size_t nverts;
+ size_t ntris;
+ unsigned i;
+ res_T res = RES_OK;
+ ASSERT(allocator && cylinder && nsteps);
+
+ darray_float_init(allocator, &cylinder->vertices);
+ darray_uint_init(allocator, &cylinder->indices);
+
+ nverts = nsteps*2/* #contour verts */ + 2/* #polar verts */;
+ ntris = nsteps*2/* #contour tris */ + 2*nsteps/* #caop tris */;
+
+ res = darray_float_resize(&cylinder->vertices, nverts*3/*#coords per vert*/);
+ if(res != RES_OK) goto error;
+ res = darray_uint_resize(&cylinder->indices, ntris*3/*#indices per tri*/);
+ if(res != RES_OK) goto error;
+
+ /* Generate the vertex coordinates */
+ FOR_EACH(i, 0, nsteps) {
+ const float theta = (float)(i* step);
+ const float x = (float)cos(theta);
+ const float y = (float)sin(theta);
+ f3(darray_float_data_get(&cylinder->vertices) + (i*2 + 0)*3, x, y, 0.f);
+ f3(darray_float_data_get(&cylinder->vertices) + (i*2 + 1)*3, x, y, 1.f);
+ }
+
+ /* "Polar" vertices */
+ f3(darray_float_data_get(&cylinder->vertices) + (i*2 + 0)*3, 0.f, 0.f, 0.f);
+ f3(darray_float_data_get(&cylinder->vertices) + (i*2 + 1)*3, 0.f, 0.f, 1.f);
+
+ /* Contour primitives */
+ FOR_EACH(i, 0, nsteps) {
+ const unsigned id = i * 2;
+ unsigned* iprim = darray_uint_data_get(&cylinder->indices) + id*3;
+
+ iprim[0] = (id + 0);
+ iprim[1] = (id + 1);
+ iprim[2] = (id + 2) % (nsteps*2);
+
+ iprim += 3;
+
+ iprim[0] = (id + 2) % (nsteps*2);
+ iprim[1] = (id + 1);
+ iprim[2] = (id + 3) % (nsteps*2);
+ }
+
+ /* Cap primitives */
+ FOR_EACH(i, 0, nsteps) {
+ const unsigned id = i* 2;
+ unsigned* iprim = darray_uint_data_get(&cylinder->indices) + (nsteps + i)*6;
+
+ iprim[0] = (nsteps * 2);
+ iprim[1] = (id + 0);
+ iprim[2] = (id + 2) % (nsteps*2);
+
+ iprim += 3;
+
+ iprim[0] = (nsteps * 2) + 1;
+ iprim[1] = (id + 3) % (nsteps*2);
+ iprim[2] = (id + 1);
+ }
+exit:
+ return res;
+error:
+ schiff_mesh_release(cylinder);
+ goto exit;
+}
+
void
schiff_mesh_release(struct schiff_mesh* mesh)
{
diff --git a/src/schiff_mesh.h b/src/schiff_mesh.h
@@ -39,10 +39,16 @@ struct schiff_mesh {
extern LOCAL_SYM res_T
schiff_mesh_init_sphere
- (struct mem_allocator* alocator,
+ (struct mem_allocator* allocator,
struct schiff_mesh* mesh,
const unsigned nthetas); /* # discret points along 2PI */
+extern LOCAL_SYM res_T
+schiff_mesh_init_cylinder
+ (struct mem_allocator* allocator,
+ struct schiff_mesh* mesh,
+ const unsigned nslices);
+
extern LOCAL_SYM void
schiff_mesh_release
(struct schiff_mesh* mesh);