commit 505d4e4c0c19044aeae8638f794a3006cf961b58
parent c24315e59e5f23e7595a83fcd60a476cf32d0448
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 25 Nov 2016 15:41:43 +0100
Refactoring
Diffstat:
3 files changed, 42 insertions(+), 33 deletions(-)
diff --git a/src/s3dut_mesh.c b/src/s3dut_mesh.c
@@ -75,7 +75,12 @@ s3dut_mesh_get_data(struct s3dut_mesh* mesh, struct s3dut_mesh_data* data)
* Local function
******************************************************************************/
res_T
-mesh_create(struct mem_allocator* allocator, struct s3dut_mesh** out_mesh)
+mesh_create
+ (struct mem_allocator* allocator,
+ const enum s3dut_mesh_type type,
+ const size_t nvertices,
+ const size_t ntriangles,
+ struct s3dut_mesh** out_mesh)
{
struct s3dut_mesh* mesh = NULL;
struct mem_allocator* mem_allocator;
@@ -93,10 +98,16 @@ mesh_create(struct mem_allocator* allocator, struct s3dut_mesh** out_mesh)
goto error;
}
mesh->allocator = mem_allocator;
+ mesh->type = type;
ref_init(&mesh->ref);
darray_double_init(mem_allocator, &mesh->coords);
darray_size_t_init(mem_allocator, &mesh->ids);
+ res = darray_double_resize(&mesh->coords, nvertices*3/*#coords*/);
+ if(res != RES_OK) goto error;
+ res = darray_size_t_resize(&mesh->ids, ntriangles*3/*#ids per triangle*/);
+ if(res != RES_OK) goto error;
+
exit:
if(out_mesh) *out_mesh = mesh;
return res;
diff --git a/src/s3dut_mesh.h b/src/s3dut_mesh.h
@@ -36,6 +36,9 @@ struct s3dut_mesh {
extern LOCAL_SYM res_T
mesh_create
(struct mem_allocator* allocator,
+ const enum s3dut_mesh_type type,
+ const size_t nvertices,
+ const size_t ntriangles,
struct s3dut_mesh** out_mesh);
#endif /* S3DUT_MESH_H */
diff --git a/src/s3dut_sphere.c b/src/s3dut_sphere.c
@@ -22,7 +22,7 @@
static res_T
setup_sphere_coords
(struct mem_allocator* allocator,
- struct darray_double* coords,
+ double* coords,
const double radius,
const unsigned nthetas, /* # subdivisions around the Z axis */
const unsigned nphis) /* # subdivisions along the Z axis */
@@ -66,21 +66,21 @@ setup_sphere_coords
const double* theta = darray_double_cdata_get(&sincos_theta) + itheta*2;
FOR_EACH(iphi, 0, nphis-1) {
const double* phi = darray_double_cdata_get(&sincos_phi) + iphi*2;
- darray_double_data_get(coords)[i++] = radius * COS[theta] * COS[phi];
- darray_double_data_get(coords)[i++] = radius * SIN[theta] * COS[phi];
- darray_double_data_get(coords)[i++] = radius * SIN[phi];
+ coords[i++] = radius * COS[theta] * COS[phi];
+ coords[i++] = radius * SIN[theta] * COS[phi];
+ coords[i++] = radius * SIN[phi];
}
}
/* Setup the bottom polar vertex */
- darray_double_data_get(coords)[i++] = 0;
- darray_double_data_get(coords)[i++] = 0;
- darray_double_data_get(coords)[i++] = -radius;
+ coords[i++] = 0;
+ coords[i++] = 0;
+ coords[i++] = -radius;
/* Setup the top polar vertex */
- darray_double_data_get(coords)[i++] = 0;
- darray_double_data_get(coords)[i++] = 0;
- darray_double_data_get(coords)[i++] = radius;
+ coords[i++] = 0;
+ coords[i++] = 0;
+ coords[i++] = radius;
exit:
darray_double_release(&sincos_theta);
@@ -92,7 +92,7 @@ error:
static void
setup_sphere_indices
- (struct darray_size_t* ids,
+ (size_t* ids,
const unsigned nthetas, /* # subdivisions around the Z axis */
const unsigned nphis) /* # subdivisions along the Z axis */
{
@@ -108,13 +108,13 @@ setup_sphere_indices
const size_t iphi0 = iphi + 0;
const size_t iphi1 = iphi + 1;
- darray_size_t_data_get(ids)[i++] = itheta0 + iphi0;
- darray_size_t_data_get(ids)[i++] = itheta0 + iphi1;
- darray_size_t_data_get(ids)[i++] = itheta1 + iphi0;
+ ids[i++] = itheta0 + iphi0;
+ ids[i++] = itheta0 + iphi1;
+ ids[i++] = itheta1 + iphi0;
- darray_size_t_data_get(ids)[i++] = itheta1 + iphi0;
- darray_size_t_data_get(ids)[i++] = itheta0 + iphi1;
- darray_size_t_data_get(ids)[i++] = itheta1 + iphi1;
+ ids[i++] = itheta1 + iphi0;
+ ids[i++] = itheta0 + iphi1;
+ ids[i++] = itheta1 + iphi1;
}
}
@@ -123,13 +123,13 @@ setup_sphere_indices
const size_t itheta0 = itheta * (nphis - 1);
const size_t itheta1 = ((itheta + 1) % nthetas) * (nphis - 1);
- darray_size_t_data_get(ids)[i++] = nthetas * (nphis - 1);
- darray_size_t_data_get(ids)[i++] = itheta0;
- darray_size_t_data_get(ids)[i++] = itheta1;
+ ids[i++] = nthetas * (nphis - 1);
+ ids[i++] = itheta0;
+ ids[i++] = itheta1;
- darray_size_t_data_get(ids)[i++] = nthetas * (nphis - 1) + 1;
- darray_size_t_data_get(ids)[i++] = itheta1 + (nphis - 2);
- darray_size_t_data_get(ids)[i++] = itheta0 + (nphis - 2);
+ ids[i++] = nthetas * (nphis - 1) + 1;
+ ids[i++] = itheta1 + (nphis - 2);
+ ids[i++] = itheta0 + (nphis - 2);
}
}
@@ -153,23 +153,18 @@ s3dut_create_sphere
res = RES_BAD_ARG;
goto error;
}
- res = mesh_create(allocator, &sphere);
- if(res != RES_OK) goto error;
- sphere->type = S3DUT_MESH_SPHERE;
nverts = nslices*(nstacks-1)/* #contour verts*/ + 2/*polar verts*/;
ntris = 2*nslices*(nstacks-2)/* #contour tris*/ + 2*nslices/* #polar tris*/;
- res = darray_double_resize(&sphere->coords, nverts*3/*#coords*/);
- if(res != RES_OK) goto error;
- res = darray_size_t_resize(&sphere->ids, ntris*3/*#ids per triangle*/);
+ res = mesh_create(allocator, S3DUT_MESH_SPHERE, nverts, ntris, &sphere);
if(res != RES_OK) goto error;
- res = setup_sphere_coords
- (allocator, &sphere->coords, radius, nslices, nstacks);
+ res = setup_sphere_coords(allocator, darray_double_data_get(&sphere->coords),
+ radius, nslices, nstacks);
if(res != RES_OK) goto error;
- setup_sphere_indices(&sphere->ids, nslices, nstacks);
+ setup_sphere_indices(darray_size_t_data_get(&sphere->ids), nslices, nstacks);
exit:
if(mesh) *mesh = sphere;