Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions par_shapes.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void par_shapes__connect(par_shapes_mesh* scene, par_shapes_mesh* cylinder,
#ifndef PAR_MALLOC
#define PAR_MALLOC(T, N) ((T*) malloc(N * sizeof(T)))
#define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
#define PAR_REALLOC(T, BUF, N) ((T*) realloc(BUF, sizeof(T) * (N)))
#define PAR_REALLOC(T, BUF, N, OLD_SZ) ((T*) realloc(BUF, sizeof(T) * (N)))
#define PAR_FREE(BUF) free(BUF)
#endif

Expand Down Expand Up @@ -664,28 +664,29 @@ void par_shapes_set_epsilon_degenerate_sphere(float epsilon) {
void par_shapes_merge(par_shapes_mesh* dst, par_shapes_mesh const* src)
{
PAR_SHAPES_T offset = dst->npoints;
int old_dst_npoints = dst->npoints;
int npoints = dst->npoints + src->npoints;
int vecsize = sizeof(float) * 3;
dst->points = PAR_REALLOC(float, dst->points, 3 * npoints);
dst->points = PAR_REALLOC(float, dst->points, 3 * npoints, 3 * old_dst_npoints);
memcpy(dst->points + 3 * dst->npoints, src->points, vecsize * src->npoints);
dst->npoints = npoints;
if (src->normals || dst->normals) {
dst->normals = PAR_REALLOC(float, dst->normals, 3 * npoints);
dst->normals = PAR_REALLOC(float, dst->normals, 3 * npoints, 3 * old_dst_npoints);
if (src->normals) {
memcpy(dst->normals + 3 * offset, src->normals,
vecsize * src->npoints);
}
}
if (src->tcoords || dst->tcoords) {
int uvsize = sizeof(float) * 2;
dst->tcoords = PAR_REALLOC(float, dst->tcoords, 2 * npoints);
dst->tcoords = PAR_REALLOC(float, dst->tcoords, 2 * npoints, 2 * old_dst_npoints);
if (src->tcoords) {
memcpy(dst->tcoords + 2 * offset, src->tcoords,
uvsize * src->npoints);
}
}
int ntriangles = dst->ntriangles + src->ntriangles;
dst->triangles = PAR_REALLOC(PAR_SHAPES_T, dst->triangles, 3 * ntriangles);
dst->triangles = PAR_REALLOC(PAR_SHAPES_T, dst->triangles, 3 * ntriangles, 3 * dst->ntriangles);
PAR_SHAPES_T* ptriangles = dst->triangles + 3 * dst->ntriangles;
PAR_SHAPES_T const* striangles = src->triangles;
for (int i = 0; i < src->ntriangles; i++) {
Expand Down Expand Up @@ -1532,21 +1533,23 @@ par_shapes_mesh* par_shapes_clone(par_shapes_mesh const* mesh,
if (!clone) {
clone = PAR_CALLOC(par_shapes_mesh, 1);
}
int old_clone_npoints = clone->npoints;
clone->npoints = mesh->npoints;
clone->points = PAR_REALLOC(float, clone->points, 3 * clone->npoints);
clone->points = PAR_REALLOC(float, clone->points, 3 * clone->npoints, 3 * old_clone_npoints);
memcpy(clone->points, mesh->points, sizeof(float) * 3 * clone->npoints);
int old_clone_ntriangles = clone->ntriangles;
clone->ntriangles = mesh->ntriangles;
clone->triangles = PAR_REALLOC(PAR_SHAPES_T, clone->triangles, 3 *
clone->ntriangles);
clone->ntriangles, 3 * old_clone_ntriangles);
memcpy(clone->triangles, mesh->triangles,
sizeof(PAR_SHAPES_T) * 3 * clone->ntriangles);
if (mesh->normals) {
clone->normals = PAR_REALLOC(float, clone->normals, 3 * clone->npoints);
clone->normals = PAR_REALLOC(float, clone->normals, 3 * clone->npoints, 3 * old_clone_npoints);
memcpy(clone->normals, mesh->normals,
sizeof(float) * 3 * clone->npoints);
}
if (mesh->tcoords) {
clone->tcoords = PAR_REALLOC(float, clone->tcoords, 2 * clone->npoints);
clone->tcoords = PAR_REALLOC(float, clone->tcoords, 2 * clone->npoints, 3 * old_clone_npoints);
memcpy(clone->tcoords, mesh->tcoords,
sizeof(float) * 2 * clone->npoints);
}
Expand Down
14 changes: 14 additions & 0 deletions test/test_shapes.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#include "describe.h"

#define PAR_SHAPES_IMPLEMENTATION

// Used to test PAR_REALLOC
void* test_realloc(void* buf, int new_sz, int old_sz) {
void* new_buf = malloc(new_sz);
if (buf && old_sz) { memcpy(new_buf, buf, old_sz); }
if (buf) { free(buf); }
return new_buf;
}

#define PAR_MALLOC(T, N) ((T*) malloc(N * sizeof(T)))
#define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
#define PAR_REALLOC(T, BUF, N, OLD_SZ) ((T*)test_realloc(BUF, sizeof(T) * N, sizeof(T) * OLD_SZ))
#define PAR_FREE(BUF) free(BUF)

#include "par_shapes.h"

#include <fcntl.h>
Expand Down