par_shapes.h 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  1. // SHAPES :: https://github.com/prideout/par
  2. // Simple C library for creation and manipulation of triangle meshes.
  3. //
  4. // The API is divided into three sections:
  5. //
  6. // - Generators. Create parametric surfaces, platonic solids, etc.
  7. // - Queries. Ask a mesh for its axis-aligned bounding box, etc.
  8. // - Transforms. Rotate a mesh, merge it with another, add normals, etc.
  9. //
  10. // In addition to the comment block above each function declaration, the API
  11. // has informal documentation here:
  12. //
  13. // https://prideout.net/shapes
  14. //
  15. // For our purposes, a "mesh" is a list of points and a list of triangles; the
  16. // former is a flattened list of three-tuples (32-bit floats) and the latter is
  17. // also a flattened list of three-tuples (16-bit uints). Triangles are always
  18. // oriented such that their front face winds counter-clockwise.
  19. //
  20. // Optionally, meshes can contain 3D normals (one per vertex), and 2D texture
  21. // coordinates (one per vertex). That's it! If you need something fancier,
  22. // look elsewhere.
  23. //
  24. // Distributed under the MIT License, see bottom of file.
  25. #ifndef PAR_SHAPES_H
  26. #define PAR_SHAPES_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include <stdint.h>
  31. #if !defined(_MSC_VER)
  32. # include <stdbool.h>
  33. #else // MSVC
  34. # if _MSC_VER >= 1800
  35. # include <stdbool.h>
  36. # else // stdbool.h missing prior to MSVC++ 12.0 (VS2013)
  37. # define bool int
  38. # define true 1
  39. # define false 0
  40. # endif
  41. #endif
  42. #ifndef PAR_SHAPES_T
  43. #define PAR_SHAPES_T uint16_t
  44. #endif
  45. typedef struct par_shapes_mesh_s {
  46. float* points; // Flat list of 3-tuples (X Y Z X Y Z...)
  47. int npoints; // Number of points
  48. PAR_SHAPES_T* triangles; // Flat list of 3-tuples (I J K I J K...)
  49. int ntriangles; // Number of triangles
  50. float* normals; // Optional list of 3-tuples (X Y Z X Y Z...)
  51. float* tcoords; // Optional list of 2-tuples (U V U V U V...)
  52. } par_shapes_mesh;
  53. void par_shapes_free_mesh(par_shapes_mesh*);
  54. // Generators ------------------------------------------------------------------
  55. // Instance a cylinder that sits on the Z=0 plane using the given tessellation
  56. // levels across the UV domain. Think of "slices" like a number of pizza
  57. // slices, and "stacks" like a number of stacked rings. Height and radius are
  58. // both 1.0, but they can easily be changed with par_shapes_scale.
  59. par_shapes_mesh* par_shapes_create_cylinder(int slices, int stacks);
  60. // Cone is similar to cylinder but the radius diminishes to zero as Z increases.
  61. // Again, height and radius are 1.0, but can be changed with par_shapes_scale.
  62. par_shapes_mesh* par_shapes_create_cone(int slices, int stacks);
  63. // Create a donut that sits on the Z=0 plane with the specified inner radius.
  64. // The outer radius can be controlled with par_shapes_scale.
  65. par_shapes_mesh* par_shapes_create_torus(int slices, int stacks, float radius);
  66. // Create a sphere with texture coordinates and small triangles near the poles.
  67. par_shapes_mesh* par_shapes_create_parametric_sphere(int slices, int stacks);
  68. // Approximate a sphere with a subdivided icosahedron, which produces a nice
  69. // distribution of triangles, but no texture coordinates. Each subdivision
  70. // level scales the number of triangles by four, so use a very low number.
  71. par_shapes_mesh* par_shapes_create_subdivided_sphere(int nsubdivisions);
  72. // More parametric surfaces.
  73. par_shapes_mesh* par_shapes_create_klein_bottle(int slices, int stacks);
  74. par_shapes_mesh* par_shapes_create_trefoil_knot(int slices, int stacks,
  75. float radius);
  76. par_shapes_mesh* par_shapes_create_hemisphere(int slices, int stacks);
  77. par_shapes_mesh* par_shapes_create_plane(int slices, int stacks);
  78. // Create a parametric surface from a callback function that consumes a 2D
  79. // point in [0,1] and produces a 3D point.
  80. typedef void (*par_shapes_fn)(float const*, float*, void*);
  81. par_shapes_mesh* par_shapes_create_parametric(par_shapes_fn, int slices,
  82. int stacks, void* userdata);
  83. // Generate points for a 20-sided polyhedron that fits in the unit sphere.
  84. // Texture coordinates and normals are not generated.
  85. par_shapes_mesh* par_shapes_create_icosahedron();
  86. // Generate points for a 12-sided polyhedron that fits in the unit sphere.
  87. // Again, texture coordinates and normals are not generated.
  88. par_shapes_mesh* par_shapes_create_dodecahedron();
  89. // More platonic solids.
  90. par_shapes_mesh* par_shapes_create_octahedron();
  91. par_shapes_mesh* par_shapes_create_tetrahedron();
  92. par_shapes_mesh* par_shapes_create_cube();
  93. // Generate an orientable disk shape in 3-space. Does not include normals or
  94. // texture coordinates.
  95. par_shapes_mesh* par_shapes_create_disk(float radius, int slices,
  96. float const* center, float const* normal);
  97. // Create an empty shape. Useful for building scenes with merge_and_free.
  98. par_shapes_mesh* par_shapes_create_empty();
  99. // Generate a rock shape that sits on the Y=0 plane, and sinks into it a bit.
  100. // This includes smooth normals but no texture coordinates. Each subdivision
  101. // level scales the number of triangles by four, so use a very low number.
  102. par_shapes_mesh* par_shapes_create_rock(int seed, int nsubdivisions);
  103. // Create trees or vegetation by executing a recursive turtle graphics program.
  104. // The program is a list of command-argument pairs. See the unit test for
  105. // an example. Texture coordinates and normals are not generated.
  106. par_shapes_mesh* par_shapes_create_lsystem(char const* program, int slices,
  107. int maxdepth);
  108. // Queries ---------------------------------------------------------------------
  109. // Dump out a text file conforming to the venerable OBJ format.
  110. void par_shapes_export(par_shapes_mesh const*, char const* objfile);
  111. // Take a pointer to 6 floats and set them to min xyz, max xyz.
  112. void par_shapes_compute_aabb(par_shapes_mesh const* mesh, float* aabb);
  113. // Make a deep copy of a mesh. To make a brand new copy, pass null to "target".
  114. // To avoid memory churn, pass an existing mesh to "target".
  115. par_shapes_mesh* par_shapes_clone(par_shapes_mesh const* mesh,
  116. par_shapes_mesh* target);
  117. // Transformations -------------------------------------------------------------
  118. void par_shapes_merge(par_shapes_mesh* dst, par_shapes_mesh const* src);
  119. void par_shapes_translate(par_shapes_mesh*, float x, float y, float z);
  120. void par_shapes_rotate(par_shapes_mesh*, float radians, float const* axis);
  121. void par_shapes_scale(par_shapes_mesh*, float x, float y, float z);
  122. void par_shapes_merge_and_free(par_shapes_mesh* dst, par_shapes_mesh* src);
  123. // Reverse the winding of a run of faces. Useful when drawing the inside of
  124. // a Cornell Box. Pass 0 for nfaces to reverse every face in the mesh.
  125. void par_shapes_invert(par_shapes_mesh*, int startface, int nfaces);
  126. // Remove all triangles whose area is less than minarea.
  127. void par_shapes_remove_degenerate(par_shapes_mesh*, float minarea);
  128. // Dereference the entire index buffer and replace the point list.
  129. // This creates an inefficient structure, but is useful for drawing facets.
  130. // If create_indices is true, a trivial "0 1 2 3..." index buffer is generated.
  131. void par_shapes_unweld(par_shapes_mesh* mesh, bool create_indices);
  132. // Merge colocated verts, build a new index buffer, and return the
  133. // optimized mesh. Epsilon is the maximum distance to consider when
  134. // welding vertices. The mapping argument can be null, or a pointer to
  135. // npoints integers, which gets filled with the mapping from old vertex
  136. // indices to new indices.
  137. par_shapes_mesh* par_shapes_weld(par_shapes_mesh const*, float epsilon,
  138. PAR_SHAPES_T* mapping);
  139. // Compute smooth normals by averaging adjacent facet normals.
  140. void par_shapes_compute_normals(par_shapes_mesh* m);
  141. // Global Config ---------------------------------------------------------------
  142. void par_shapes_set_epsilon_welded_normals(float epsilon);
  143. void par_shapes_set_epsilon_degenerate_sphere(float epsilon);
  144. // Advanced --------------------------------------------------------------------
  145. void par_shapes__compute_welded_normals(par_shapes_mesh* m);
  146. void par_shapes__connect(par_shapes_mesh* scene, par_shapes_mesh* cylinder,
  147. int slices);
  148. #ifndef PAR_PI
  149. #define PAR_PI (3.14159265359)
  150. #define PAR_MIN(a, b) (a > b ? b : a)
  151. #define PAR_MAX(a, b) (a > b ? a : b)
  152. #define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
  153. #define PAR_SWAP(T, A, B) { T tmp = B; B = A; A = tmp; }
  154. #define PAR_SQR(a) ((a) * (a))
  155. #endif
  156. #ifndef PAR_MALLOC
  157. #define PAR_MALLOC(T, N) ((T*) malloc(N * sizeof(T)))
  158. #define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
  159. #define PAR_REALLOC(T, BUF, N) ((T*) realloc(BUF, sizeof(T) * (N)))
  160. #define PAR_FREE(BUF) free(BUF)
  161. #endif
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. // -----------------------------------------------------------------------------
  166. // END PUBLIC API
  167. // -----------------------------------------------------------------------------
  168. #ifdef PAR_SHAPES_IMPLEMENTATION
  169. #include <stdlib.h>
  170. #include <stdio.h>
  171. #include <assert.h>
  172. #include <float.h>
  173. #include <string.h>
  174. #include <math.h>
  175. #include <errno.h>
  176. static float par_shapes__epsilon_welded_normals = 0.001;
  177. static float par_shapes__epsilon_degenerate_sphere = 0.0001;
  178. static void par_shapes__sphere(float const* uv, float* xyz, void*);
  179. static void par_shapes__hemisphere(float const* uv, float* xyz, void*);
  180. static void par_shapes__plane(float const* uv, float* xyz, void*);
  181. static void par_shapes__klein(float const* uv, float* xyz, void*);
  182. static void par_shapes__cylinder(float const* uv, float* xyz, void*);
  183. static void par_shapes__cone(float const* uv, float* xyz, void*);
  184. static void par_shapes__torus(float const* uv, float* xyz, void*);
  185. static void par_shapes__trefoil(float const* uv, float* xyz, void*);
  186. struct osn_context;
  187. static int par__simplex_noise(int64_t seed, struct osn_context** ctx);
  188. static void par__simplex_noise_free(struct osn_context* ctx);
  189. static double par__simplex_noise2(struct osn_context* ctx, double x, double y);
  190. static void par_shapes__copy3(float* result, float const* a)
  191. {
  192. result[0] = a[0];
  193. result[1] = a[1];
  194. result[2] = a[2];
  195. }
  196. static float par_shapes__dot3(float const* a, float const* b)
  197. {
  198. return b[0] * a[0] + b[1] * a[1] + b[2] * a[2];
  199. }
  200. static void par_shapes__transform3(float* p, float const* x, float const* y,
  201. float const* z)
  202. {
  203. float px = par_shapes__dot3(p, x);
  204. float py = par_shapes__dot3(p, y);
  205. float pz = par_shapes__dot3(p, z);
  206. p[0] = px;
  207. p[1] = py;
  208. p[2] = pz;
  209. }
  210. static void par_shapes__cross3(float* result, float const* a, float const* b)
  211. {
  212. float x = (a[1] * b[2]) - (a[2] * b[1]);
  213. float y = (a[2] * b[0]) - (a[0] * b[2]);
  214. float z = (a[0] * b[1]) - (a[1] * b[0]);
  215. result[0] = x;
  216. result[1] = y;
  217. result[2] = z;
  218. }
  219. static void par_shapes__mix3(float* d, float const* a, float const* b, float t)
  220. {
  221. float x = b[0] * t + a[0] * (1 - t);
  222. float y = b[1] * t + a[1] * (1 - t);
  223. float z = b[2] * t + a[2] * (1 - t);
  224. d[0] = x;
  225. d[1] = y;
  226. d[2] = z;
  227. }
  228. static void par_shapes__scale3(float* result, float a)
  229. {
  230. result[0] *= a;
  231. result[1] *= a;
  232. result[2] *= a;
  233. }
  234. static void par_shapes__normalize3(float* v)
  235. {
  236. float lsqr = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  237. if (lsqr > 0) {
  238. par_shapes__scale3(v, 1.0f / lsqr);
  239. }
  240. }
  241. static void par_shapes__subtract3(float* result, float const* a)
  242. {
  243. result[0] -= a[0];
  244. result[1] -= a[1];
  245. result[2] -= a[2];
  246. }
  247. static void par_shapes__add3(float* result, float const* a)
  248. {
  249. result[0] += a[0];
  250. result[1] += a[1];
  251. result[2] += a[2];
  252. }
  253. static float par_shapes__sqrdist3(float const* a, float const* b)
  254. {
  255. float dx = a[0] - b[0];
  256. float dy = a[1] - b[1];
  257. float dz = a[2] - b[2];
  258. return dx * dx + dy * dy + dz * dz;
  259. }
  260. void par_shapes__compute_welded_normals(par_shapes_mesh* m)
  261. {
  262. const float epsilon = par_shapes__epsilon_welded_normals;
  263. m->normals = PAR_MALLOC(float, m->npoints * 3);
  264. PAR_SHAPES_T* weldmap = PAR_MALLOC(PAR_SHAPES_T, m->npoints);
  265. par_shapes_mesh* welded = par_shapes_weld(m, epsilon, weldmap);
  266. par_shapes_compute_normals(welded);
  267. float* pdst = m->normals;
  268. for (int i = 0; i < m->npoints; i++, pdst += 3) {
  269. int d = weldmap[i];
  270. float const* pnormal = welded->normals + d * 3;
  271. pdst[0] = pnormal[0];
  272. pdst[1] = pnormal[1];
  273. pdst[2] = pnormal[2];
  274. }
  275. PAR_FREE(weldmap);
  276. par_shapes_free_mesh(welded);
  277. }
  278. par_shapes_mesh* par_shapes_create_cylinder(int slices, int stacks)
  279. {
  280. if (slices < 3 || stacks < 1) {
  281. return 0;
  282. }
  283. return par_shapes_create_parametric(par_shapes__cylinder, slices,
  284. stacks, 0);
  285. }
  286. par_shapes_mesh* par_shapes_create_cone(int slices, int stacks)
  287. {
  288. if (slices < 3 || stacks < 1) {
  289. return 0;
  290. }
  291. return par_shapes_create_parametric(par_shapes__cone, slices,
  292. stacks, 0);
  293. }
  294. par_shapes_mesh* par_shapes_create_parametric_sphere(int slices, int stacks)
  295. {
  296. if (slices < 3 || stacks < 3) {
  297. return 0;
  298. }
  299. par_shapes_mesh* m = par_shapes_create_parametric(par_shapes__sphere,
  300. slices, stacks, 0);
  301. par_shapes_remove_degenerate(m, par_shapes__epsilon_degenerate_sphere);
  302. return m;
  303. }
  304. par_shapes_mesh* par_shapes_create_hemisphere(int slices, int stacks)
  305. {
  306. if (slices < 3 || stacks < 3) {
  307. return 0;
  308. }
  309. par_shapes_mesh* m = par_shapes_create_parametric(par_shapes__hemisphere,
  310. slices, stacks, 0);
  311. par_shapes_remove_degenerate(m, par_shapes__epsilon_degenerate_sphere);
  312. return m;
  313. }
  314. par_shapes_mesh* par_shapes_create_torus(int slices, int stacks, float radius)
  315. {
  316. if (slices < 3 || stacks < 3) {
  317. return 0;
  318. }
  319. assert(radius <= 1.0 && "Use smaller radius to avoid self-intersection.");
  320. assert(radius >= 0.1 && "Use larger radius to avoid self-intersection.");
  321. void* userdata = (void*) &radius;
  322. return par_shapes_create_parametric(par_shapes__torus, slices,
  323. stacks, userdata);
  324. }
  325. par_shapes_mesh* par_shapes_create_klein_bottle(int slices, int stacks)
  326. {
  327. if (slices < 3 || stacks < 3) {
  328. return 0;
  329. }
  330. par_shapes_mesh* mesh = par_shapes_create_parametric(
  331. par_shapes__klein, slices, stacks, 0);
  332. int face = 0;
  333. for (int stack = 0; stack < stacks; stack++) {
  334. for (int slice = 0; slice < slices; slice++, face += 2) {
  335. if (stack < 27 * stacks / 32) {
  336. par_shapes_invert(mesh, face, 2);
  337. }
  338. }
  339. }
  340. par_shapes__compute_welded_normals(mesh);
  341. return mesh;
  342. }
  343. par_shapes_mesh* par_shapes_create_trefoil_knot(int slices, int stacks,
  344. float radius)
  345. {
  346. if (slices < 3 || stacks < 3) {
  347. return 0;
  348. }
  349. assert(radius <= 3.0 && "Use smaller radius to avoid self-intersection.");
  350. assert(radius >= 0.5 && "Use larger radius to avoid self-intersection.");
  351. void* userdata = (void*) &radius;
  352. return par_shapes_create_parametric(par_shapes__trefoil, slices,
  353. stacks, userdata);
  354. }
  355. par_shapes_mesh* par_shapes_create_plane(int slices, int stacks)
  356. {
  357. if (slices < 1 || stacks < 1) {
  358. return 0;
  359. }
  360. return par_shapes_create_parametric(par_shapes__plane, slices,
  361. stacks, 0);
  362. }
  363. par_shapes_mesh* par_shapes_create_parametric(par_shapes_fn fn,
  364. int slices, int stacks, void* userdata)
  365. {
  366. par_shapes_mesh* mesh = PAR_CALLOC(par_shapes_mesh, 1);
  367. // Generate verts.
  368. mesh->npoints = (slices + 1) * (stacks + 1);
  369. mesh->points = PAR_CALLOC(float, 3 * mesh->npoints);
  370. float uv[2];
  371. float xyz[3];
  372. float* points = mesh->points;
  373. for (int stack = 0; stack < stacks + 1; stack++) {
  374. uv[0] = (float) stack / stacks;
  375. for (int slice = 0; slice < slices + 1; slice++) {
  376. uv[1] = (float) slice / slices;
  377. fn(uv, xyz, userdata);
  378. *points++ = xyz[0];
  379. *points++ = xyz[1];
  380. *points++ = xyz[2];
  381. }
  382. }
  383. // Generate texture coordinates.
  384. mesh->tcoords = PAR_CALLOC(float, 2 * mesh->npoints);
  385. float* uvs = mesh->tcoords;
  386. for (int stack = 0; stack < stacks + 1; stack++) {
  387. uv[0] = (float) stack / stacks;
  388. for (int slice = 0; slice < slices + 1; slice++) {
  389. uv[1] = (float) slice / slices;
  390. *uvs++ = uv[0];
  391. *uvs++ = uv[1];
  392. }
  393. }
  394. // Generate faces.
  395. mesh->ntriangles = 2 * slices * stacks;
  396. mesh->triangles = PAR_CALLOC(PAR_SHAPES_T, 3 * mesh->ntriangles);
  397. int v = 0;
  398. PAR_SHAPES_T* face = mesh->triangles;
  399. for (int stack = 0; stack < stacks; stack++) {
  400. for (int slice = 0; slice < slices; slice++) {
  401. int next = slice + 1;
  402. *face++ = v + slice + slices + 1;
  403. *face++ = v + next;
  404. *face++ = v + slice;
  405. *face++ = v + slice + slices + 1;
  406. *face++ = v + next + slices + 1;
  407. *face++ = v + next;
  408. }
  409. v += slices + 1;
  410. }
  411. par_shapes__compute_welded_normals(mesh);
  412. return mesh;
  413. }
  414. void par_shapes_free_mesh(par_shapes_mesh* mesh)
  415. {
  416. PAR_FREE(mesh->points);
  417. PAR_FREE(mesh->triangles);
  418. PAR_FREE(mesh->normals);
  419. PAR_FREE(mesh->tcoords);
  420. PAR_FREE(mesh);
  421. }
  422. void par_shapes_export(par_shapes_mesh const* mesh, char const* filename)
  423. {
  424. FILE* objfile = fopen(filename, "wt");
  425. float const* points = mesh->points;
  426. float const* tcoords = mesh->tcoords;
  427. float const* norms = mesh->normals;
  428. PAR_SHAPES_T const* indices = mesh->triangles;
  429. if (tcoords && norms) {
  430. for (int nvert = 0; nvert < mesh->npoints; nvert++) {
  431. fprintf(objfile, "v %f %f %f\n", points[0], points[1], points[2]);
  432. fprintf(objfile, "vt %f %f\n", tcoords[0], tcoords[1]);
  433. fprintf(objfile, "vn %f %f %f\n", norms[0], norms[1], norms[2]);
  434. points += 3;
  435. norms += 3;
  436. tcoords += 2;
  437. }
  438. for (int nface = 0; nface < mesh->ntriangles; nface++) {
  439. int a = 1 + *indices++;
  440. int b = 1 + *indices++;
  441. int c = 1 + *indices++;
  442. fprintf(objfile, "f %d/%d/%d %d/%d/%d %d/%d/%d\n",
  443. a, a, a, b, b, b, c, c, c);
  444. }
  445. } else if (norms) {
  446. for (int nvert = 0; nvert < mesh->npoints; nvert++) {
  447. fprintf(objfile, "v %f %f %f\n", points[0], points[1], points[2]);
  448. fprintf(objfile, "vn %f %f %f\n", norms[0], norms[1], norms[2]);
  449. points += 3;
  450. norms += 3;
  451. }
  452. for (int nface = 0; nface < mesh->ntriangles; nface++) {
  453. int a = 1 + *indices++;
  454. int b = 1 + *indices++;
  455. int c = 1 + *indices++;
  456. fprintf(objfile, "f %d//%d %d//%d %d//%d\n", a, a, b, b, c, c);
  457. }
  458. } else if (tcoords) {
  459. for (int nvert = 0; nvert < mesh->npoints; nvert++) {
  460. fprintf(objfile, "v %f %f %f\n", points[0], points[1], points[2]);
  461. fprintf(objfile, "vt %f %f\n", tcoords[0], tcoords[1]);
  462. points += 3;
  463. tcoords += 2;
  464. }
  465. for (int nface = 0; nface < mesh->ntriangles; nface++) {
  466. int a = 1 + *indices++;
  467. int b = 1 + *indices++;
  468. int c = 1 + *indices++;
  469. fprintf(objfile, "f %d/%d %d/%d %d/%d\n", a, a, b, b, c, c);
  470. }
  471. } else {
  472. for (int nvert = 0; nvert < mesh->npoints; nvert++) {
  473. fprintf(objfile, "v %f %f %f\n", points[0], points[1], points[2]);
  474. points += 3;
  475. }
  476. for (int nface = 0; nface < mesh->ntriangles; nface++) {
  477. int a = 1 + *indices++;
  478. int b = 1 + *indices++;
  479. int c = 1 + *indices++;
  480. fprintf(objfile, "f %d %d %d\n", a, b, c);
  481. }
  482. }
  483. fclose(objfile);
  484. }
  485. static void par_shapes__sphere(float const* uv, float* xyz, void* userdata)
  486. {
  487. float phi = uv[0] * PAR_PI;
  488. float theta = uv[1] * 2 * PAR_PI;
  489. xyz[0] = cosf(theta) * sinf(phi);
  490. xyz[1] = sinf(theta) * sinf(phi);
  491. xyz[2] = cosf(phi);
  492. }
  493. static void par_shapes__hemisphere(float const* uv, float* xyz, void* userdata)
  494. {
  495. float phi = uv[0] * PAR_PI;
  496. float theta = uv[1] * PAR_PI;
  497. xyz[0] = cosf(theta) * sinf(phi);
  498. xyz[1] = sinf(theta) * sinf(phi);
  499. xyz[2] = cosf(phi);
  500. }
  501. static void par_shapes__plane(float const* uv, float* xyz, void* userdata)
  502. {
  503. xyz[0] = uv[0];
  504. xyz[1] = uv[1];
  505. xyz[2] = 0;
  506. }
  507. static void par_shapes__klein(float const* uv, float* xyz, void* userdata)
  508. {
  509. float u = uv[0] * PAR_PI;
  510. float v = uv[1] * 2 * PAR_PI;
  511. u = u * 2;
  512. if (u < PAR_PI) {
  513. xyz[0] = 3 * cosf(u) * (1 + sinf(u)) + (2 * (1 - cosf(u) / 2)) *
  514. cosf(u) * cosf(v);
  515. xyz[2] = -8 * sinf(u) - 2 * (1 - cosf(u) / 2) * sinf(u) * cosf(v);
  516. } else {
  517. xyz[0] = 3 * cosf(u) * (1 + sinf(u)) + (2 * (1 - cosf(u) / 2)) *
  518. cosf(v + PAR_PI);
  519. xyz[2] = -8 * sinf(u);
  520. }
  521. xyz[1] = -2 * (1 - cosf(u) / 2) * sinf(v);
  522. }
  523. static void par_shapes__cylinder(float const* uv, float* xyz, void* userdata)
  524. {
  525. float theta = uv[1] * 2 * PAR_PI;
  526. xyz[0] = sinf(theta);
  527. xyz[1] = cosf(theta);
  528. xyz[2] = uv[0];
  529. }
  530. static void par_shapes__cone(float const* uv, float* xyz, void* userdata)
  531. {
  532. float r = 1.0f - uv[0];
  533. float theta = uv[1] * 2 * PAR_PI;
  534. xyz[0] = r * sinf(theta);
  535. xyz[1] = r * cosf(theta);
  536. xyz[2] = uv[0];
  537. }
  538. static void par_shapes__torus(float const* uv, float* xyz, void* userdata)
  539. {
  540. float major = 1;
  541. float minor = *((float*) userdata);
  542. float theta = uv[0] * 2 * PAR_PI;
  543. float phi = uv[1] * 2 * PAR_PI;
  544. float beta = major + minor * cosf(phi);
  545. xyz[0] = cosf(theta) * beta;
  546. xyz[1] = sinf(theta) * beta;
  547. xyz[2] = sinf(phi) * minor;
  548. }
  549. static void par_shapes__trefoil(float const* uv, float* xyz, void* userdata)
  550. {
  551. float minor = *((float*) userdata);
  552. const float a = 0.5f;
  553. const float b = 0.3f;
  554. const float c = 0.5f;
  555. const float d = minor * 0.1f;
  556. const float u = (1 - uv[0]) * 4 * PAR_PI;
  557. const float v = uv[1] * 2 * PAR_PI;
  558. const float r = a + b * cos(1.5f * u);
  559. const float x = r * cos(u);
  560. const float y = r * sin(u);
  561. const float z = c * sin(1.5f * u);
  562. float q[3];
  563. q[0] =
  564. -1.5f * b * sin(1.5f * u) * cos(u) - (a + b * cos(1.5f * u)) * sin(u);
  565. q[1] =
  566. -1.5f * b * sin(1.5f * u) * sin(u) + (a + b * cos(1.5f * u)) * cos(u);
  567. q[2] = 1.5f * c * cos(1.5f * u);
  568. par_shapes__normalize3(q);
  569. float qvn[3] = {q[1], -q[0], 0};
  570. par_shapes__normalize3(qvn);
  571. float ww[3];
  572. par_shapes__cross3(ww, q, qvn);
  573. xyz[0] = x + d * (qvn[0] * cos(v) + ww[0] * sin(v));
  574. xyz[1] = y + d * (qvn[1] * cos(v) + ww[1] * sin(v));
  575. xyz[2] = z + d * ww[2] * sin(v);
  576. }
  577. void par_shapes_set_epsilon_welded_normals(float epsilon) {
  578. par_shapes__epsilon_welded_normals = epsilon;
  579. }
  580. void par_shapes_set_epsilon_degenerate_sphere(float epsilon) {
  581. par_shapes__epsilon_degenerate_sphere = epsilon;
  582. }
  583. void par_shapes_merge(par_shapes_mesh* dst, par_shapes_mesh const* src)
  584. {
  585. PAR_SHAPES_T offset = dst->npoints;
  586. int npoints = dst->npoints + src->npoints;
  587. int vecsize = sizeof(float) * 3;
  588. dst->points = PAR_REALLOC(float, dst->points, 3 * npoints);
  589. memcpy(dst->points + 3 * dst->npoints, src->points, vecsize * src->npoints);
  590. dst->npoints = npoints;
  591. if (src->normals || dst->normals) {
  592. dst->normals = PAR_REALLOC(float, dst->normals, 3 * npoints);
  593. if (src->normals) {
  594. memcpy(dst->normals + 3 * offset, src->normals,
  595. vecsize * src->npoints);
  596. }
  597. }
  598. if (src->tcoords || dst->tcoords) {
  599. int uvsize = sizeof(float) * 2;
  600. dst->tcoords = PAR_REALLOC(float, dst->tcoords, 2 * npoints);
  601. if (src->tcoords) {
  602. memcpy(dst->tcoords + 2 * offset, src->tcoords,
  603. uvsize * src->npoints);
  604. }
  605. }
  606. int ntriangles = dst->ntriangles + src->ntriangles;
  607. dst->triangles = PAR_REALLOC(PAR_SHAPES_T, dst->triangles, 3 * ntriangles);
  608. PAR_SHAPES_T* ptriangles = dst->triangles + 3 * dst->ntriangles;
  609. PAR_SHAPES_T const* striangles = src->triangles;
  610. for (int i = 0; i < src->ntriangles; i++) {
  611. *ptriangles++ = offset + *striangles++;
  612. *ptriangles++ = offset + *striangles++;
  613. *ptriangles++ = offset + *striangles++;
  614. }
  615. dst->ntriangles = ntriangles;
  616. }
  617. par_shapes_mesh* par_shapes_create_disk(float radius, int slices,
  618. float const* center, float const* normal)
  619. {
  620. par_shapes_mesh* mesh = PAR_CALLOC(par_shapes_mesh, 1);
  621. mesh->npoints = slices + 1;
  622. mesh->points = PAR_MALLOC(float, 3 * mesh->npoints);
  623. float* points = mesh->points;
  624. *points++ = 0;
  625. *points++ = 0;
  626. *points++ = 0;
  627. for (int i = 0; i < slices; i++) {
  628. float theta = i * PAR_PI * 2 / slices;
  629. *points++ = radius * cos(theta);
  630. *points++ = radius * sin(theta);
  631. *points++ = 0;
  632. }
  633. float nnormal[3] = {normal[0], normal[1], normal[2]};
  634. par_shapes__normalize3(nnormal);
  635. mesh->normals = PAR_MALLOC(float, 3 * mesh->npoints);
  636. float* norms = mesh->normals;
  637. for (int i = 0; i < mesh->npoints; i++) {
  638. *norms++ = nnormal[0];
  639. *norms++ = nnormal[1];
  640. *norms++ = nnormal[2];
  641. }
  642. mesh->ntriangles = slices;
  643. mesh->triangles = PAR_MALLOC(PAR_SHAPES_T, 3 * mesh->ntriangles);
  644. PAR_SHAPES_T* triangles = mesh->triangles;
  645. for (int i = 0; i < slices; i++) {
  646. *triangles++ = 0;
  647. *triangles++ = 1 + i;
  648. *triangles++ = 1 + (i + 1) % slices;
  649. }
  650. float k[3] = {0, 0, -1};
  651. float axis[3];
  652. par_shapes__cross3(axis, nnormal, k);
  653. par_shapes__normalize3(axis);
  654. par_shapes_rotate(mesh, acos(nnormal[2]), axis);
  655. par_shapes_translate(mesh, center[0], center[1], center[2]);
  656. return mesh;
  657. }
  658. par_shapes_mesh* par_shapes_create_empty()
  659. {
  660. return PAR_CALLOC(par_shapes_mesh, 1);
  661. }
  662. void par_shapes_translate(par_shapes_mesh* m, float x, float y, float z)
  663. {
  664. float* points = m->points;
  665. for (int i = 0; i < m->npoints; i++) {
  666. *points++ += x;
  667. *points++ += y;
  668. *points++ += z;
  669. }
  670. }
  671. void par_shapes_rotate(par_shapes_mesh* mesh, float radians, float const* axis)
  672. {
  673. float s = sinf(radians);
  674. float c = cosf(radians);
  675. float x = axis[0];
  676. float y = axis[1];
  677. float z = axis[2];
  678. float xy = x * y;
  679. float yz = y * z;
  680. float zx = z * x;
  681. float oneMinusC = 1.0f - c;
  682. float col0[3] = {
  683. (((x * x) * oneMinusC) + c),
  684. ((xy * oneMinusC) + (z * s)), ((zx * oneMinusC) - (y * s))
  685. };
  686. float col1[3] = {
  687. ((xy * oneMinusC) - (z * s)),
  688. (((y * y) * oneMinusC) + c), ((yz * oneMinusC) + (x * s))
  689. };
  690. float col2[3] = {
  691. ((zx * oneMinusC) + (y * s)),
  692. ((yz * oneMinusC) - (x * s)), (((z * z) * oneMinusC) + c)
  693. };
  694. float* p = mesh->points;
  695. for (int i = 0; i < mesh->npoints; i++, p += 3) {
  696. float x = col0[0] * p[0] + col1[0] * p[1] + col2[0] * p[2];
  697. float y = col0[1] * p[0] + col1[1] * p[1] + col2[1] * p[2];
  698. float z = col0[2] * p[0] + col1[2] * p[1] + col2[2] * p[2];
  699. p[0] = x;
  700. p[1] = y;
  701. p[2] = z;
  702. }
  703. float* n = mesh->normals;
  704. if (n) {
  705. for (int i = 0; i < mesh->npoints; i++, n += 3) {
  706. float x = col0[0] * n[0] + col1[0] * n[1] + col2[0] * n[2];
  707. float y = col0[1] * n[0] + col1[1] * n[1] + col2[1] * n[2];
  708. float z = col0[2] * n[0] + col1[2] * n[1] + col2[2] * n[2];
  709. n[0] = x;
  710. n[1] = y;
  711. n[2] = z;
  712. }
  713. }
  714. }
  715. void par_shapes_scale(par_shapes_mesh* m, float x, float y, float z)
  716. {
  717. float* points = m->points;
  718. for (int i = 0; i < m->npoints; i++) {
  719. *points++ *= x;
  720. *points++ *= y;
  721. *points++ *= z;
  722. }
  723. float* n = m->normals;
  724. if (n && (x != y || x != z || y != z)) {
  725. x = 1.0f / x;
  726. y = 1.0f / y;
  727. z = 1.0f / z;
  728. for (int i = 0; i < m->npoints; i++, n += 3) {
  729. n[0] *= x;
  730. n[1] *= y;
  731. n[2] *= z;
  732. par_shapes__normalize3(n);
  733. }
  734. }
  735. }
  736. void par_shapes_merge_and_free(par_shapes_mesh* dst, par_shapes_mesh* src)
  737. {
  738. par_shapes_merge(dst, src);
  739. par_shapes_free_mesh(src);
  740. }
  741. void par_shapes_compute_aabb(par_shapes_mesh const* m, float* aabb)
  742. {
  743. float* points = m->points;
  744. aabb[0] = aabb[3] = points[0];
  745. aabb[1] = aabb[4] = points[1];
  746. aabb[2] = aabb[5] = points[2];
  747. points += 3;
  748. for (int i = 1; i < m->npoints; i++, points += 3) {
  749. aabb[0] = PAR_MIN(points[0], aabb[0]);
  750. aabb[1] = PAR_MIN(points[1], aabb[1]);
  751. aabb[2] = PAR_MIN(points[2], aabb[2]);
  752. aabb[3] = PAR_MAX(points[0], aabb[3]);
  753. aabb[4] = PAR_MAX(points[1], aabb[4]);
  754. aabb[5] = PAR_MAX(points[2], aabb[5]);
  755. }
  756. }
  757. void par_shapes_invert(par_shapes_mesh* m, int face, int nfaces)
  758. {
  759. nfaces = nfaces ? nfaces : m->ntriangles;
  760. PAR_SHAPES_T* tri = m->triangles + face * 3;
  761. for (int i = 0; i < nfaces; i++) {
  762. PAR_SWAP(PAR_SHAPES_T, tri[0], tri[2]);
  763. tri += 3;
  764. }
  765. }
  766. par_shapes_mesh* par_shapes_create_icosahedron()
  767. {
  768. static float verts[] = {
  769. 0.000, 0.000, 1.000,
  770. 0.894, 0.000, 0.447,
  771. 0.276, 0.851, 0.447,
  772. -0.724, 0.526, 0.447,
  773. -0.724, -0.526, 0.447,
  774. 0.276, -0.851, 0.447,
  775. 0.724, 0.526, -0.447,
  776. -0.276, 0.851, -0.447,
  777. -0.894, 0.000, -0.447,
  778. -0.276, -0.851, -0.447,
  779. 0.724, -0.526, -0.447,
  780. 0.000, 0.000, -1.000
  781. };
  782. static PAR_SHAPES_T faces[] = {
  783. 0,1,2,
  784. 0,2,3,
  785. 0,3,4,
  786. 0,4,5,
  787. 0,5,1,
  788. 7,6,11,
  789. 8,7,11,
  790. 9,8,11,
  791. 10,9,11,
  792. 6,10,11,
  793. 6,2,1,
  794. 7,3,2,
  795. 8,4,3,
  796. 9,5,4,
  797. 10,1,5,
  798. 6,7,2,
  799. 7,8,3,
  800. 8,9,4,
  801. 9,10,5,
  802. 10,6,1
  803. };
  804. par_shapes_mesh* mesh = PAR_CALLOC(par_shapes_mesh, 1);
  805. mesh->npoints = sizeof(verts) / sizeof(verts[0]) / 3;
  806. mesh->points = PAR_MALLOC(float, sizeof(verts) / 4);
  807. memcpy(mesh->points, verts, sizeof(verts));
  808. mesh->ntriangles = sizeof(faces) / sizeof(faces[0]) / 3;
  809. mesh->triangles = PAR_MALLOC(PAR_SHAPES_T, sizeof(faces) / 2);
  810. memcpy(mesh->triangles, faces, sizeof(faces));
  811. return mesh;
  812. }
  813. par_shapes_mesh* par_shapes_create_dodecahedron()
  814. {
  815. static float verts[20 * 3] = {
  816. 0.607, 0.000, 0.795,
  817. 0.188, 0.577, 0.795,
  818. -0.491, 0.357, 0.795,
  819. -0.491, -0.357, 0.795,
  820. 0.188, -0.577, 0.795,
  821. 0.982, 0.000, 0.188,
  822. 0.304, 0.934, 0.188,
  823. -0.795, 0.577, 0.188,
  824. -0.795, -0.577, 0.188,
  825. 0.304, -0.934, 0.188,
  826. 0.795, 0.577, -0.188,
  827. -0.304, 0.934, -0.188,
  828. -0.982, 0.000, -0.188,
  829. -0.304, -0.934, -0.188,
  830. 0.795, -0.577, -0.188,
  831. 0.491, 0.357, -0.795,
  832. -0.188, 0.577, -0.795,
  833. -0.607, 0.000, -0.795,
  834. -0.188, -0.577, -0.795,
  835. 0.491, -0.357, -0.795,
  836. };
  837. static PAR_SHAPES_T pentagons[12 * 5] = {
  838. 0,1,2,3,4,
  839. 5,10,6,1,0,
  840. 6,11,7,2,1,
  841. 7,12,8,3,2,
  842. 8,13,9,4,3,
  843. 9,14,5,0,4,
  844. 15,16,11,6,10,
  845. 16,17,12,7,11,
  846. 17,18,13,8,12,
  847. 18,19,14,9,13,
  848. 19,15,10,5,14,
  849. 19,18,17,16,15
  850. };
  851. int npentagons = sizeof(pentagons) / sizeof(pentagons[0]) / 5;
  852. par_shapes_mesh* mesh = PAR_CALLOC(par_shapes_mesh, 1);
  853. int ncorners = sizeof(verts) / sizeof(verts[0]) / 3;
  854. mesh->npoints = ncorners;
  855. mesh->points = PAR_MALLOC(float, mesh->npoints * 3);
  856. memcpy(mesh->points, verts, sizeof(verts));
  857. PAR_SHAPES_T const* pentagon = pentagons;
  858. mesh->ntriangles = npentagons * 3;
  859. mesh->triangles = PAR_MALLOC(PAR_SHAPES_T, mesh->ntriangles * 3);
  860. PAR_SHAPES_T* tris = mesh->triangles;
  861. for (int p = 0; p < npentagons; p++, pentagon += 5) {
  862. *tris++ = pentagon[0];
  863. *tris++ = pentagon[1];
  864. *tris++ = pentagon[2];
  865. *tris++ = pentagon[0];
  866. *tris++ = pentagon[2];
  867. *tris++ = pentagon[3];
  868. *tris++ = pentagon[0];
  869. *tris++ = pentagon[3];
  870. *tris++ = pentagon[4];
  871. }
  872. return mesh;
  873. }
  874. par_shapes_mesh* par_shapes_create_octahedron()
  875. {
  876. static float verts[6 * 3] = {
  877. 0.000, 0.000, 1.000,
  878. 1.000, 0.000, 0.000,
  879. 0.000, 1.000, 0.000,
  880. -1.000, 0.000, 0.000,
  881. 0.000, -1.000, 0.000,
  882. 0.000, 0.000, -1.000
  883. };
  884. static PAR_SHAPES_T triangles[8 * 3] = {
  885. 0,1,2,
  886. 0,2,3,
  887. 0,3,4,
  888. 0,4,1,
  889. 2,1,5,
  890. 3,2,5,
  891. 4,3,5,
  892. 1,4,5,
  893. };
  894. int ntris = sizeof(triangles) / sizeof(triangles[0]) / 3;
  895. par_shapes_mesh* mesh = PAR_CALLOC(par_shapes_mesh, 1);
  896. int ncorners = sizeof(verts) / sizeof(verts[0]) / 3;
  897. mesh->npoints = ncorners;
  898. mesh->points = PAR_MALLOC(float, mesh->npoints * 3);
  899. memcpy(mesh->points, verts, sizeof(verts));
  900. PAR_SHAPES_T const* triangle = triangles;
  901. mesh->ntriangles = ntris;
  902. mesh->triangles = PAR_MALLOC(PAR_SHAPES_T, mesh->ntriangles * 3);
  903. PAR_SHAPES_T* tris = mesh->triangles;
  904. for (int p = 0; p < ntris; p++) {
  905. *tris++ = *triangle++;
  906. *tris++ = *triangle++;
  907. *tris++ = *triangle++;
  908. }
  909. return mesh;
  910. }
  911. par_shapes_mesh* par_shapes_create_tetrahedron()
  912. {
  913. static float verts[4 * 3] = {
  914. 0.000, 1.333, 0,
  915. 0.943, 0, 0,
  916. -0.471, 0, 0.816,
  917. -0.471, 0, -0.816,
  918. };
  919. static PAR_SHAPES_T triangles[4 * 3] = {
  920. 2,1,0,
  921. 3,2,0,
  922. 1,3,0,
  923. 1,2,3,
  924. };
  925. int ntris = sizeof(triangles) / sizeof(triangles[0]) / 3;
  926. par_shapes_mesh* mesh = PAR_CALLOC(par_shapes_mesh, 1);
  927. int ncorners = sizeof(verts) / sizeof(verts[0]) / 3;
  928. mesh->npoints = ncorners;
  929. mesh->points = PAR_MALLOC(float, mesh->npoints * 3);
  930. memcpy(mesh->points, verts, sizeof(verts));
  931. PAR_SHAPES_T const* triangle = triangles;
  932. mesh->ntriangles = ntris;
  933. mesh->triangles = PAR_MALLOC(PAR_SHAPES_T, mesh->ntriangles * 3);
  934. PAR_SHAPES_T* tris = mesh->triangles;
  935. for (int p = 0; p < ntris; p++) {
  936. *tris++ = *triangle++;
  937. *tris++ = *triangle++;
  938. *tris++ = *triangle++;
  939. }
  940. return mesh;
  941. }
  942. par_shapes_mesh* par_shapes_create_cube()
  943. {
  944. static float verts[8 * 3] = {
  945. 0, 0, 0, // 0
  946. 0, 1, 0, // 1
  947. 1, 1, 0, // 2
  948. 1, 0, 0, // 3
  949. 0, 0, 1, // 4
  950. 0, 1, 1, // 5
  951. 1, 1, 1, // 6
  952. 1, 0, 1, // 7
  953. };
  954. static PAR_SHAPES_T quads[6 * 4] = {
  955. 7,6,5,4, // front
  956. 0,1,2,3, // back
  957. 6,7,3,2, // right
  958. 5,6,2,1, // top
  959. 4,5,1,0, // left
  960. 7,4,0,3, // bottom
  961. };
  962. int nquads = sizeof(quads) / sizeof(quads[0]) / 4;
  963. par_shapes_mesh* mesh = PAR_CALLOC(par_shapes_mesh, 1);
  964. int ncorners = sizeof(verts) / sizeof(verts[0]) / 3;
  965. mesh->npoints = ncorners;
  966. mesh->points = PAR_MALLOC(float, mesh->npoints * 3);
  967. memcpy(mesh->points, verts, sizeof(verts));
  968. PAR_SHAPES_T const* quad = quads;
  969. mesh->ntriangles = nquads * 2;
  970. mesh->triangles = PAR_MALLOC(PAR_SHAPES_T, mesh->ntriangles * 3);
  971. PAR_SHAPES_T* tris = mesh->triangles;
  972. for (int p = 0; p < nquads; p++, quad += 4) {
  973. *tris++ = quad[0];
  974. *tris++ = quad[1];
  975. *tris++ = quad[2];
  976. *tris++ = quad[2];
  977. *tris++ = quad[3];
  978. *tris++ = quad[0];
  979. }
  980. return mesh;
  981. }
  982. typedef struct {
  983. char* cmd;
  984. char* arg;
  985. } par_shapes__command;
  986. typedef struct {
  987. char const* name;
  988. int weight;
  989. int ncommands;
  990. par_shapes__command* commands;
  991. } par_shapes__rule;
  992. typedef struct {
  993. int pc;
  994. float position[3];
  995. float scale[3];
  996. par_shapes_mesh* orientation;
  997. par_shapes__rule* rule;
  998. } par_shapes__stackframe;
  999. static par_shapes__rule* par_shapes__pick_rule(const char* name,
  1000. par_shapes__rule* rules, int nrules)
  1001. {
  1002. par_shapes__rule* rule = 0;
  1003. int total = 0;
  1004. for (int i = 0; i < nrules; i++) {
  1005. rule = rules + i;
  1006. if (!strcmp(rule->name, name)) {
  1007. total += rule->weight;
  1008. }
  1009. }
  1010. float r = (float) rand() / RAND_MAX;
  1011. float t = 0;
  1012. for (int i = 0; i < nrules; i++) {
  1013. rule = rules + i;
  1014. if (!strcmp(rule->name, name)) {
  1015. t += (float) rule->weight / total;
  1016. if (t >= r) {
  1017. return rule;
  1018. }
  1019. }
  1020. }
  1021. return rule;
  1022. }
  1023. static par_shapes_mesh* par_shapes__create_turtle()
  1024. {
  1025. const float xaxis[] = {1, 0, 0};
  1026. const float yaxis[] = {0, 1, 0};
  1027. const float zaxis[] = {0, 0, 1};
  1028. par_shapes_mesh* turtle = PAR_CALLOC(par_shapes_mesh, 1);
  1029. turtle->npoints = 3;
  1030. turtle->points = PAR_CALLOC(float, turtle->npoints * 3);
  1031. par_shapes__copy3(turtle->points + 0, xaxis);
  1032. par_shapes__copy3(turtle->points + 3, yaxis);
  1033. par_shapes__copy3(turtle->points + 6, zaxis);
  1034. return turtle;
  1035. }
  1036. static par_shapes_mesh* par_shapes__apply_turtle(par_shapes_mesh* mesh,
  1037. par_shapes_mesh* turtle, float const* pos, float const* scale)
  1038. {
  1039. par_shapes_mesh* m = par_shapes_clone(mesh, 0);
  1040. for (int p = 0; p < m->npoints; p++) {
  1041. float* pt = m->points + p * 3;
  1042. pt[0] *= scale[0];
  1043. pt[1] *= scale[1];
  1044. pt[2] *= scale[2];
  1045. par_shapes__transform3(pt,
  1046. turtle->points + 0, turtle->points + 3, turtle->points + 6);
  1047. pt[0] += pos[0];
  1048. pt[1] += pos[1];
  1049. pt[2] += pos[2];
  1050. }
  1051. return m;
  1052. }
  1053. void par_shapes__connect(par_shapes_mesh* scene, par_shapes_mesh* cylinder,
  1054. int slices)
  1055. {
  1056. int stacks = 1;
  1057. int npoints = (slices + 1) * (stacks + 1);
  1058. assert(scene->npoints >= npoints && "Cannot connect to empty scene.");
  1059. // Create the new point list.
  1060. npoints = scene->npoints + (slices + 1);
  1061. float* points = PAR_MALLOC(float, npoints * 3);
  1062. memcpy(points, scene->points, sizeof(float) * scene->npoints * 3);
  1063. float* newpts = points + scene->npoints * 3;
  1064. memcpy(newpts, cylinder->points + (slices + 1) * 3,
  1065. sizeof(float) * (slices + 1) * 3);
  1066. PAR_FREE(scene->points);
  1067. scene->points = points;
  1068. // Create the new triangle list.
  1069. int ntriangles = scene->ntriangles + 2 * slices * stacks;
  1070. PAR_SHAPES_T* triangles = PAR_MALLOC(PAR_SHAPES_T, ntriangles * 3);
  1071. memcpy(triangles, scene->triangles,
  1072. sizeof(PAR_SHAPES_T) * scene->ntriangles * 3);
  1073. int v = scene->npoints - (slices + 1);
  1074. PAR_SHAPES_T* face = triangles + scene->ntriangles * 3;
  1075. for (int stack = 0; stack < stacks; stack++) {
  1076. for (int slice = 0; slice < slices; slice++) {
  1077. int next = slice + 1;
  1078. *face++ = v + slice + slices + 1;
  1079. *face++ = v + next;
  1080. *face++ = v + slice;
  1081. *face++ = v + slice + slices + 1;
  1082. *face++ = v + next + slices + 1;
  1083. *face++ = v + next;
  1084. }
  1085. v += slices + 1;
  1086. }
  1087. PAR_FREE(scene->triangles);
  1088. scene->triangles = triangles;
  1089. scene->npoints = npoints;
  1090. scene->ntriangles = ntriangles;
  1091. }
  1092. par_shapes_mesh* par_shapes_create_lsystem(char const* text, int slices,
  1093. int maxdepth)
  1094. {
  1095. char* program;
  1096. program = PAR_MALLOC(char, strlen(text) + 1);
  1097. // The first pass counts the number of rules and commands.
  1098. strcpy(program, text);
  1099. char *cmd = strtok(program, " ");
  1100. int nrules = 1;
  1101. int ncommands = 0;
  1102. while (cmd) {
  1103. char *arg = strtok(0, " ");
  1104. if (!arg) {
  1105. puts("lsystem error: unexpected end of program.");
  1106. break;
  1107. }
  1108. if (!strcmp(cmd, "rule")) {
  1109. nrules++;
  1110. } else {
  1111. ncommands++;
  1112. }
  1113. cmd = strtok(0, " ");
  1114. }
  1115. // Allocate space.
  1116. par_shapes__rule* rules = PAR_MALLOC(par_shapes__rule, nrules);
  1117. par_shapes__command* commands = PAR_MALLOC(par_shapes__command, ncommands);
  1118. // Initialize the entry rule.
  1119. par_shapes__rule* current_rule = &rules[0];
  1120. par_shapes__command* current_command = &commands[0];
  1121. current_rule->name = "entry";
  1122. current_rule->weight = 1;
  1123. current_rule->ncommands = 0;
  1124. current_rule->commands = current_command;
  1125. // The second pass fills in the structures.
  1126. strcpy(program, text);
  1127. cmd = strtok(program, " ");
  1128. while (cmd) {
  1129. char *arg = strtok(0, " ");
  1130. if (!strcmp(cmd, "rule")) {
  1131. current_rule++;
  1132. // Split the argument into a rule name and weight.
  1133. char* dot = strchr(arg, '.');
  1134. if (dot) {
  1135. current_rule->weight = atoi(dot + 1);
  1136. *dot = 0;
  1137. } else {
  1138. current_rule->weight = 1;
  1139. }
  1140. current_rule->name = arg;
  1141. current_rule->ncommands = 0;
  1142. current_rule->commands = current_command;
  1143. } else {
  1144. current_rule->ncommands++;
  1145. current_command->cmd = cmd;
  1146. current_command->arg = arg;
  1147. current_command++;
  1148. }
  1149. cmd = strtok(0, " ");
  1150. }
  1151. // For testing purposes, dump out the parsed program.
  1152. #ifdef TEST_PARSE
  1153. for (int i = 0; i < nrules; i++) {
  1154. par_shapes__rule rule = rules[i];
  1155. printf("rule %s.%d\n", rule.name, rule.weight);
  1156. for (int c = 0; c < rule.ncommands; c++) {
  1157. par_shapes__command cmd = rule.commands[c];
  1158. printf("\t%s %s\n", cmd.cmd, cmd.arg);
  1159. }
  1160. }
  1161. #endif
  1162. // Instantiate the aggregated shape and the template shapes.
  1163. par_shapes_mesh* scene = PAR_CALLOC(par_shapes_mesh, 1);
  1164. par_shapes_mesh* tube = par_shapes_create_cylinder(slices, 1);
  1165. par_shapes_mesh* turtle = par_shapes__create_turtle();
  1166. // We're not attempting to support texture coordinates and normals
  1167. // with L-systems, so remove them from the template shape.
  1168. PAR_FREE(tube->normals);
  1169. PAR_FREE(tube->tcoords);
  1170. tube->normals = 0;
  1171. tube->tcoords = 0;
  1172. const float xaxis[] = {1, 0, 0};
  1173. const float yaxis[] = {0, 1, 0};
  1174. const float zaxis[] = {0, 0, 1};
  1175. const float units[] = {1, 1, 1};
  1176. // Execute the L-system program until the stack size is 0.
  1177. par_shapes__stackframe* stack =
  1178. PAR_CALLOC(par_shapes__stackframe, maxdepth);
  1179. int stackptr = 0;
  1180. stack[0].orientation = turtle;
  1181. stack[0].rule = &rules[0];
  1182. par_shapes__copy3(stack[0].scale, units);
  1183. while (stackptr >= 0) {
  1184. par_shapes__stackframe* frame = &stack[stackptr];
  1185. par_shapes__rule* rule = frame->rule;
  1186. par_shapes_mesh* turtle = frame->orientation;
  1187. float* position = frame->position;
  1188. float* scale = frame->scale;
  1189. if (frame->pc >= rule->ncommands) {
  1190. par_shapes_free_mesh(turtle);
  1191. stackptr--;
  1192. continue;
  1193. }
  1194. par_shapes__command* cmd = rule->commands + (frame->pc++);
  1195. #ifdef DUMP_TRACE
  1196. printf("%5s %5s %5s:%d %03d\n", cmd->cmd, cmd->arg, rule->name,
  1197. frame->pc - 1, stackptr);
  1198. #endif
  1199. float value;
  1200. if (!strcmp(cmd->cmd, "shape")) {
  1201. par_shapes_mesh* m = par_shapes__apply_turtle(tube, turtle,
  1202. position, scale);
  1203. if (!strcmp(cmd->arg, "connect")) {
  1204. par_shapes__connect(scene, m, slices);
  1205. } else {
  1206. par_shapes_merge(scene, m);
  1207. }
  1208. par_shapes_free_mesh(m);
  1209. } else if (!strcmp(cmd->cmd, "call") && stackptr < maxdepth - 1) {
  1210. rule = par_shapes__pick_rule(cmd->arg, rules, nrules);
  1211. frame = &stack[++stackptr];
  1212. frame->rule = rule;
  1213. frame->orientation = par_shapes_clone(turtle, 0);
  1214. frame->pc = 0;
  1215. par_shapes__copy3(frame->scale, scale);
  1216. par_shapes__copy3(frame->position, position);
  1217. continue;
  1218. } else {
  1219. value = atof(cmd->arg);
  1220. if (!strcmp(cmd->cmd, "rx")) {
  1221. par_shapes_rotate(turtle, value * PAR_PI / 180.0, xaxis);
  1222. } else if (!strcmp(cmd->cmd, "ry")) {
  1223. par_shapes_rotate(turtle, value * PAR_PI / 180.0, yaxis);
  1224. } else if (!strcmp(cmd->cmd, "rz")) {
  1225. par_shapes_rotate(turtle, value * PAR_PI / 180.0, zaxis);
  1226. } else if (!strcmp(cmd->cmd, "tx")) {
  1227. float vec[3] = {value, 0, 0};
  1228. float t[3] = {
  1229. par_shapes__dot3(turtle->points + 0, vec),
  1230. par_shapes__dot3(turtle->points + 3, vec),
  1231. par_shapes__dot3(turtle->points + 6, vec)
  1232. };
  1233. par_shapes__add3(position, t);
  1234. } else if (!strcmp(cmd->cmd, "ty")) {
  1235. float vec[3] = {0, value, 0};
  1236. float t[3] = {
  1237. par_shapes__dot3(turtle->points + 0, vec),
  1238. par_shapes__dot3(turtle->points + 3, vec),
  1239. par_shapes__dot3(turtle->points + 6, vec)
  1240. };
  1241. par_shapes__add3(position, t);
  1242. } else if (!strcmp(cmd->cmd, "tz")) {
  1243. float vec[3] = {0, 0, value};
  1244. float t[3] = {
  1245. par_shapes__dot3(turtle->points + 0, vec),
  1246. par_shapes__dot3(turtle->points + 3, vec),
  1247. par_shapes__dot3(turtle->points + 6, vec)
  1248. };
  1249. par_shapes__add3(position, t);
  1250. } else if (!strcmp(cmd->cmd, "sx")) {
  1251. scale[0] *= value;
  1252. } else if (!strcmp(cmd->cmd, "sy")) {
  1253. scale[1] *= value;
  1254. } else if (!strcmp(cmd->cmd, "sz")) {
  1255. scale[2] *= value;
  1256. } else if (!strcmp(cmd->cmd, "sa")) {
  1257. scale[0] *= value;
  1258. scale[1] *= value;
  1259. scale[2] *= value;
  1260. }
  1261. }
  1262. }
  1263. PAR_FREE(stack);
  1264. PAR_FREE(program);
  1265. PAR_FREE(rules);
  1266. PAR_FREE(commands);
  1267. return scene;
  1268. }
  1269. void par_shapes_unweld(par_shapes_mesh* mesh, bool create_indices)
  1270. {
  1271. int npoints = mesh->ntriangles * 3;
  1272. float* points = PAR_MALLOC(float, 3 * npoints);
  1273. float* dst = points;
  1274. PAR_SHAPES_T const* index = mesh->triangles;
  1275. for (int i = 0; i < npoints; i++) {
  1276. float const* src = mesh->points + 3 * (*index++);
  1277. *dst++ = src[0];
  1278. *dst++ = src[1];
  1279. *dst++ = src[2];
  1280. }
  1281. PAR_FREE(mesh->points);
  1282. mesh->points = points;
  1283. mesh->npoints = npoints;
  1284. if (create_indices) {
  1285. PAR_SHAPES_T* tris = PAR_MALLOC(PAR_SHAPES_T, 3 * mesh->ntriangles);
  1286. PAR_SHAPES_T* index = tris;
  1287. for (int i = 0; i < mesh->ntriangles * 3; i++) {
  1288. *index++ = i;
  1289. }
  1290. PAR_FREE(mesh->triangles);
  1291. mesh->triangles = tris;
  1292. }
  1293. }
  1294. void par_shapes_compute_normals(par_shapes_mesh* m)
  1295. {
  1296. PAR_FREE(m->normals);
  1297. m->normals = PAR_CALLOC(float, m->npoints * 3);
  1298. PAR_SHAPES_T const* triangle = m->triangles;
  1299. float next[3], prev[3], cp[3];
  1300. for (int f = 0; f < m->ntriangles; f++, triangle += 3) {
  1301. float const* pa = m->points + 3 * triangle[0];
  1302. float const* pb = m->points + 3 * triangle[1];
  1303. float const* pc = m->points + 3 * triangle[2];
  1304. par_shapes__copy3(next, pb);
  1305. par_shapes__subtract3(next, pa);
  1306. par_shapes__copy3(prev, pc);
  1307. par_shapes__subtract3(prev, pa);
  1308. par_shapes__cross3(cp, next, prev);
  1309. par_shapes__add3(m->normals + 3 * triangle[0], cp);
  1310. par_shapes__copy3(next, pc);
  1311. par_shapes__subtract3(next, pb);
  1312. par_shapes__copy3(prev, pa);
  1313. par_shapes__subtract3(prev, pb);
  1314. par_shapes__cross3(cp, next, prev);
  1315. par_shapes__add3(m->normals + 3 * triangle[1], cp);
  1316. par_shapes__copy3(next, pa);
  1317. par_shapes__subtract3(next, pc);
  1318. par_shapes__copy3(prev, pb);
  1319. par_shapes__subtract3(prev, pc);
  1320. par_shapes__cross3(cp, next, prev);
  1321. par_shapes__add3(m->normals + 3 * triangle[2], cp);
  1322. }
  1323. float* normal = m->normals;
  1324. for (int p = 0; p < m->npoints; p++, normal += 3) {
  1325. par_shapes__normalize3(normal);
  1326. }
  1327. }
  1328. static void par_shapes__subdivide(par_shapes_mesh* mesh)
  1329. {
  1330. assert(mesh->npoints == mesh->ntriangles * 3 && "Must be unwelded.");
  1331. int ntriangles = mesh->ntriangles * 4;
  1332. int npoints = ntriangles * 3;
  1333. float* points = PAR_CALLOC(float, npoints * 3);
  1334. float* dpoint = points;
  1335. float const* spoint = mesh->points;
  1336. for (int t = 0; t < mesh->ntriangles; t++, spoint += 9, dpoint += 3) {
  1337. float const* a = spoint;
  1338. float const* b = spoint + 3;
  1339. float const* c = spoint + 6;
  1340. float const* p0 = dpoint;
  1341. float const* p1 = dpoint + 3;
  1342. float const* p2 = dpoint + 6;
  1343. par_shapes__mix3(dpoint, a, b, 0.5);
  1344. par_shapes__mix3(dpoint += 3, b, c, 0.5);
  1345. par_shapes__mix3(dpoint += 3, a, c, 0.5);
  1346. par_shapes__add3(dpoint += 3, a);
  1347. par_shapes__add3(dpoint += 3, p0);
  1348. par_shapes__add3(dpoint += 3, p2);
  1349. par_shapes__add3(dpoint += 3, p0);
  1350. par_shapes__add3(dpoint += 3, b);
  1351. par_shapes__add3(dpoint += 3, p1);
  1352. par_shapes__add3(dpoint += 3, p2);
  1353. par_shapes__add3(dpoint += 3, p1);
  1354. par_shapes__add3(dpoint += 3, c);
  1355. }
  1356. PAR_FREE(mesh->points);
  1357. mesh->points = points;
  1358. mesh->npoints = npoints;
  1359. mesh->ntriangles = ntriangles;
  1360. }
  1361. par_shapes_mesh* par_shapes_create_subdivided_sphere(int nsubd)
  1362. {
  1363. par_shapes_mesh* mesh = par_shapes_create_icosahedron();
  1364. par_shapes_unweld(mesh, false);
  1365. PAR_FREE(mesh->triangles);
  1366. mesh->triangles = 0;
  1367. while (nsubd--) {
  1368. par_shapes__subdivide(mesh);
  1369. }
  1370. for (int i = 0; i < mesh->npoints; i++) {
  1371. par_shapes__normalize3(mesh->points + i * 3);
  1372. }
  1373. mesh->triangles = PAR_MALLOC(PAR_SHAPES_T, 3 * mesh->ntriangles);
  1374. for (int i = 0; i < mesh->ntriangles * 3; i++) {
  1375. mesh->triangles[i] = i;
  1376. }
  1377. par_shapes_mesh* tmp = mesh;
  1378. mesh = par_shapes_weld(mesh, 0.01, 0);
  1379. par_shapes_free_mesh(tmp);
  1380. par_shapes_compute_normals(mesh);
  1381. return mesh;
  1382. }
  1383. par_shapes_mesh* par_shapes_create_rock(int seed, int subd)
  1384. {
  1385. par_shapes_mesh* mesh = par_shapes_create_subdivided_sphere(subd);
  1386. struct osn_context* ctx;
  1387. par__simplex_noise(seed, &ctx);
  1388. for (int p = 0; p < mesh->npoints; p++) {
  1389. float* pt = mesh->points + p * 3;
  1390. float a = 0.25, f = 1.0;
  1391. double n = a * par__simplex_noise2(ctx, f * pt[0], f * pt[2]);
  1392. a *= 0.5; f *= 2;
  1393. n += a * par__simplex_noise2(ctx, f * pt[0], f * pt[2]);
  1394. pt[0] *= 1 + 2 * n;
  1395. pt[1] *= 1 + n;
  1396. pt[2] *= 1 + 2 * n;
  1397. if (pt[1] < 0) {
  1398. pt[1] = -pow(-pt[1], 0.5) / 2;
  1399. }
  1400. }
  1401. par__simplex_noise_free(ctx);
  1402. par_shapes_compute_normals(mesh);
  1403. return mesh;
  1404. }
  1405. par_shapes_mesh* par_shapes_clone(par_shapes_mesh const* mesh,
  1406. par_shapes_mesh* clone)
  1407. {
  1408. if (!clone) {
  1409. clone = PAR_CALLOC(par_shapes_mesh, 1);
  1410. }
  1411. clone->npoints = mesh->npoints;
  1412. clone->points = PAR_REALLOC(float, clone->points, 3 * clone->npoints);
  1413. memcpy(clone->points, mesh->points, sizeof(float) * 3 * clone->npoints);
  1414. clone->ntriangles = mesh->ntriangles;
  1415. clone->triangles = PAR_REALLOC(PAR_SHAPES_T, clone->triangles, 3 *
  1416. clone->ntriangles);
  1417. memcpy(clone->triangles, mesh->triangles,
  1418. sizeof(PAR_SHAPES_T) * 3 * clone->ntriangles);
  1419. if (mesh->normals) {
  1420. clone->normals = PAR_REALLOC(float, clone->normals, 3 * clone->npoints);
  1421. memcpy(clone->normals, mesh->normals,
  1422. sizeof(float) * 3 * clone->npoints);
  1423. }
  1424. if (mesh->tcoords) {
  1425. clone->tcoords = PAR_REALLOC(float, clone->tcoords, 2 * clone->npoints);
  1426. memcpy(clone->tcoords, mesh->tcoords,
  1427. sizeof(float) * 2 * clone->npoints);
  1428. }
  1429. return clone;
  1430. }
  1431. static struct {
  1432. float const* points;
  1433. int gridsize;
  1434. } par_shapes__sort_context;
  1435. static int par_shapes__cmp1(const void *arg0, const void *arg1)
  1436. {
  1437. const int g = par_shapes__sort_context.gridsize;
  1438. // Convert arg0 into a flattened grid index.
  1439. PAR_SHAPES_T d0 = *(const PAR_SHAPES_T*) arg0;
  1440. float const* p0 = par_shapes__sort_context.points + d0 * 3;
  1441. int i0 = (int) p0[0];
  1442. int j0 = (int) p0[1];
  1443. int k0 = (int) p0[2];
  1444. int index0 = i0 + g * j0 + g * g * k0;
  1445. // Convert arg1 into a flattened grid index.
  1446. PAR_SHAPES_T d1 = *(const PAR_SHAPES_T*) arg1;
  1447. float const* p1 = par_shapes__sort_context.points + d1 * 3;
  1448. int i1 = (int) p1[0];
  1449. int j1 = (int) p1[1];
  1450. int k1 = (int) p1[2];
  1451. int index1 = i1 + g * j1 + g * g * k1;
  1452. // Return the ordering.
  1453. if (index0 < index1) return -1;
  1454. if (index0 > index1) return 1;
  1455. return 0;
  1456. }
  1457. static void par_shapes__sort_points(par_shapes_mesh* mesh, int gridsize,
  1458. PAR_SHAPES_T* sortmap)
  1459. {
  1460. // Run qsort over a list of consecutive integers that get deferenced
  1461. // within the comparator function; this creates a reorder mapping.
  1462. for (int i = 0; i < mesh->npoints; i++) {
  1463. sortmap[i] = i;
  1464. }
  1465. par_shapes__sort_context.gridsize = gridsize;
  1466. par_shapes__sort_context.points = mesh->points;
  1467. qsort(sortmap, mesh->npoints, sizeof(PAR_SHAPES_T), par_shapes__cmp1);
  1468. // Apply the reorder mapping to the XYZ coordinate data.
  1469. float* newpts = PAR_MALLOC(float, mesh->npoints * 3);
  1470. PAR_SHAPES_T* invmap = PAR_MALLOC(PAR_SHAPES_T, mesh->npoints);
  1471. float* dstpt = newpts;
  1472. for (int i = 0; i < mesh->npoints; i++) {
  1473. invmap[sortmap[i]] = i;
  1474. float const* srcpt = mesh->points + 3 * sortmap[i];
  1475. *dstpt++ = *srcpt++;
  1476. *dstpt++ = *srcpt++;
  1477. *dstpt++ = *srcpt++;
  1478. }
  1479. PAR_FREE(mesh->points);
  1480. mesh->points = newpts;
  1481. // Apply the inverse reorder mapping to the triangle indices.
  1482. PAR_SHAPES_T* newinds = PAR_MALLOC(PAR_SHAPES_T, mesh->ntriangles * 3);
  1483. PAR_SHAPES_T* dstind = newinds;
  1484. PAR_SHAPES_T const* srcind = mesh->triangles;
  1485. for (int i = 0; i < mesh->ntriangles * 3; i++) {
  1486. *dstind++ = invmap[*srcind++];
  1487. }
  1488. PAR_FREE(mesh->triangles);
  1489. mesh->triangles = newinds;
  1490. // Cleanup.
  1491. memcpy(sortmap, invmap, sizeof(PAR_SHAPES_T) * mesh->npoints);
  1492. PAR_FREE(invmap);
  1493. }
  1494. static void par_shapes__weld_points(par_shapes_mesh* mesh, int gridsize,
  1495. float epsilon, PAR_SHAPES_T* weldmap)
  1496. {
  1497. // Each bin contains a "pointer" (really an index) to its first point.
  1498. // We add 1 because 0 is reserved to mean that the bin is empty.
  1499. // Since the points are spatially sorted, there's no need to store
  1500. // a point count in each bin.
  1501. PAR_SHAPES_T* bins = PAR_CALLOC(PAR_SHAPES_T,
  1502. gridsize * gridsize * gridsize);
  1503. int prev_binindex = -1;
  1504. for (int p = 0; p < mesh->npoints; p++) {
  1505. float const* pt = mesh->points + p * 3;
  1506. int i = (int) pt[0];
  1507. int j = (int) pt[1];
  1508. int k = (int) pt[2];
  1509. int this_binindex = i + gridsize * j + gridsize * gridsize * k;
  1510. if (this_binindex != prev_binindex) {
  1511. bins[this_binindex] = 1 + p;
  1512. }
  1513. prev_binindex = this_binindex;
  1514. }
  1515. // Examine all bins that intersect the epsilon-sized cube centered at each
  1516. // point, and check for colocated points within those bins.
  1517. float const* pt = mesh->points;
  1518. int nremoved = 0;
  1519. for (int p = 0; p < mesh->npoints; p++, pt += 3) {
  1520. // Skip if this point has already been welded.
  1521. if (weldmap[p] != p) {
  1522. continue;
  1523. }
  1524. // Build a list of bins that intersect the epsilon-sized cube.
  1525. int nearby[8];
  1526. int nbins = 0;
  1527. int minp[3], maxp[3];
  1528. for (int c = 0; c < 3; c++) {
  1529. minp[c] = (int) (pt[c] - epsilon);
  1530. maxp[c] = (int) (pt[c] + epsilon);
  1531. }
  1532. for (int i = minp[0]; i <= maxp[0]; i++) {
  1533. for (int j = minp[1]; j <= maxp[1]; j++) {
  1534. for (int k = minp[2]; k <= maxp[2]; k++) {
  1535. int binindex = i + gridsize * j + gridsize * gridsize * k;
  1536. PAR_SHAPES_T binvalue = *(bins + binindex);
  1537. if (binvalue > 0) {
  1538. if (nbins == 8) {
  1539. printf("Epsilon value is too large.\n");
  1540. break;
  1541. }
  1542. nearby[nbins++] = binindex;
  1543. }
  1544. }
  1545. }
  1546. }
  1547. // Check for colocated points in each nearby bin.
  1548. for (int b = 0; b < nbins; b++) {
  1549. int binindex = nearby[b];
  1550. PAR_SHAPES_T binvalue = bins[binindex];
  1551. PAR_SHAPES_T nindex = binvalue - 1;
  1552. assert(nindex < mesh->npoints);
  1553. while (true) {
  1554. // If this isn't "self" and it's colocated, then weld it!
  1555. if (nindex != p && weldmap[nindex] == nindex) {
  1556. float const* thatpt = mesh->points + nindex * 3;
  1557. float dist2 = par_shapes__sqrdist3(thatpt, pt);
  1558. if (dist2 < epsilon) {
  1559. weldmap[nindex] = p;
  1560. nremoved++;
  1561. }
  1562. }
  1563. // Advance to the next point if possible.
  1564. if (++nindex >= mesh->npoints) {
  1565. break;
  1566. }
  1567. // If the next point is outside the bin, then we're done.
  1568. float const* nextpt = mesh->points + nindex * 3;
  1569. int i = (int) nextpt[0];
  1570. int j = (int) nextpt[1];
  1571. int k = (int) nextpt[2];
  1572. int nextbinindex = i + gridsize * j + gridsize * gridsize * k;
  1573. if (nextbinindex != binindex) {
  1574. break;
  1575. }
  1576. }
  1577. }
  1578. }
  1579. PAR_FREE(bins);
  1580. // Apply the weldmap to the vertices.
  1581. int npoints = mesh->npoints - nremoved;
  1582. float* newpts = PAR_MALLOC(float, 3 * npoints);
  1583. float* dst = newpts;
  1584. PAR_SHAPES_T* condensed_map = PAR_MALLOC(PAR_SHAPES_T, mesh->npoints);
  1585. PAR_SHAPES_T* cmap = condensed_map;
  1586. float const* src = mesh->points;
  1587. int ci = 0;
  1588. for (int p = 0; p < mesh->npoints; p++, src += 3) {
  1589. if (weldmap[p] == p) {
  1590. *dst++ = src[0];
  1591. *dst++ = src[1];
  1592. *dst++ = src[2];
  1593. *cmap++ = ci++;
  1594. } else {
  1595. *cmap++ = condensed_map[weldmap[p]];
  1596. }
  1597. }
  1598. assert(ci == npoints);
  1599. PAR_FREE(mesh->points);
  1600. memcpy(weldmap, condensed_map, mesh->npoints * sizeof(PAR_SHAPES_T));
  1601. PAR_FREE(condensed_map);
  1602. mesh->points = newpts;
  1603. mesh->npoints = npoints;
  1604. // Apply the weldmap to the triangle indices and skip the degenerates.
  1605. PAR_SHAPES_T const* tsrc = mesh->triangles;
  1606. PAR_SHAPES_T* tdst = mesh->triangles;
  1607. int ntriangles = 0;
  1608. for (int i = 0; i < mesh->ntriangles; i++, tsrc += 3) {
  1609. PAR_SHAPES_T a = weldmap[tsrc[0]];
  1610. PAR_SHAPES_T b = weldmap[tsrc[1]];
  1611. PAR_SHAPES_T c = weldmap[tsrc[2]];
  1612. if (a != b && a != c && b != c) {
  1613. assert(a < mesh->npoints);
  1614. assert(b < mesh->npoints);
  1615. assert(c < mesh->npoints);
  1616. *tdst++ = a;
  1617. *tdst++ = b;
  1618. *tdst++ = c;
  1619. ntriangles++;
  1620. }
  1621. }
  1622. mesh->ntriangles = ntriangles;
  1623. }
  1624. par_shapes_mesh* par_shapes_weld(par_shapes_mesh const* mesh, float epsilon,
  1625. PAR_SHAPES_T* weldmap)
  1626. {
  1627. par_shapes_mesh* clone = par_shapes_clone(mesh, 0);
  1628. float aabb[6];
  1629. int gridsize = 20;
  1630. float maxcell = gridsize - 1;
  1631. par_shapes_compute_aabb(clone, aabb);
  1632. float scale[3] = {
  1633. aabb[3] == aabb[0] ? 1.0f : maxcell / (aabb[3] - aabb[0]),
  1634. aabb[4] == aabb[1] ? 1.0f : maxcell / (aabb[4] - aabb[1]),
  1635. aabb[5] == aabb[2] ? 1.0f : maxcell / (aabb[5] - aabb[2]),
  1636. };
  1637. par_shapes_translate(clone, -aabb[0], -aabb[1], -aabb[2]);
  1638. par_shapes_scale(clone, scale[0], scale[1], scale[2]);
  1639. PAR_SHAPES_T* sortmap = PAR_MALLOC(PAR_SHAPES_T, mesh->npoints);
  1640. par_shapes__sort_points(clone, gridsize, sortmap);
  1641. bool owner = false;
  1642. if (!weldmap) {
  1643. owner = true;
  1644. weldmap = PAR_MALLOC(PAR_SHAPES_T, mesh->npoints);
  1645. }
  1646. for (int i = 0; i < mesh->npoints; i++) {
  1647. weldmap[i] = i;
  1648. }
  1649. par_shapes__weld_points(clone, gridsize, epsilon, weldmap);
  1650. if (owner) {
  1651. PAR_FREE(weldmap);
  1652. } else {
  1653. PAR_SHAPES_T* newmap = PAR_MALLOC(PAR_SHAPES_T, mesh->npoints);
  1654. for (int i = 0; i < mesh->npoints; i++) {
  1655. newmap[i] = weldmap[sortmap[i]];
  1656. }
  1657. memcpy(weldmap, newmap, sizeof(PAR_SHAPES_T) * mesh->npoints);
  1658. PAR_FREE(newmap);
  1659. }
  1660. PAR_FREE(sortmap);
  1661. par_shapes_scale(clone, 1.0 / scale[0], 1.0 / scale[1], 1.0 / scale[2]);
  1662. par_shapes_translate(clone, aabb[0], aabb[1], aabb[2]);
  1663. return clone;
  1664. }
  1665. // -----------------------------------------------------------------------------
  1666. // BEGIN OPEN SIMPLEX NOISE
  1667. // -----------------------------------------------------------------------------
  1668. #define STRETCH_CONSTANT_2D (-0.211324865405187) // (1 / sqrt(2 + 1) - 1 ) / 2;
  1669. #define SQUISH_CONSTANT_2D (0.366025403784439) // (sqrt(2 + 1) -1) / 2;
  1670. #define STRETCH_CONSTANT_3D (-1.0 / 6.0) // (1 / sqrt(3 + 1) - 1) / 3;
  1671. #define SQUISH_CONSTANT_3D (1.0 / 3.0) // (sqrt(3+1)-1)/3;
  1672. #define STRETCH_CONSTANT_4D (-0.138196601125011) // (1 / sqrt(4 + 1) - 1) / 4;
  1673. #define SQUISH_CONSTANT_4D (0.309016994374947) // (sqrt(4 + 1) - 1) / 4;
  1674. #define NORM_CONSTANT_2D (47.0)
  1675. #define NORM_CONSTANT_3D (103.0)
  1676. #define NORM_CONSTANT_4D (30.0)
  1677. #define DEFAULT_SEED (0LL)
  1678. struct osn_context {
  1679. int16_t* perm;
  1680. int16_t* permGradIndex3D;
  1681. };
  1682. #define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0]))
  1683. /*
  1684. * Gradients for 2D. They approximate the directions to the
  1685. * vertices of an octagon from the center.
  1686. */
  1687. static const int8_t gradients2D[] = {
  1688. 5, 2, 2, 5, -5, 2, -2, 5, 5, -2, 2, -5, -5, -2, -2, -5,
  1689. };
  1690. /*
  1691. * Gradients for 3D. They approximate the directions to the
  1692. * vertices of a rhombicuboctahedron from the center, skewed so
  1693. * that the triangular and square facets can be inscribed inside
  1694. * circles of the same radius.
  1695. */
  1696. static const signed char gradients3D[] = {
  1697. -11, 4, 4, -4, 11, 4, -4, 4, 11, 11, 4, 4, 4, 11, 4, 4, 4, 11, -11, -4, 4,
  1698. -4, -11, 4, -4, -4, 11, 11, -4, 4, 4, -11, 4, 4, -4, 11, -11, 4, -4, -4, 11,
  1699. -4, -4, 4, -11, 11, 4, -4, 4, 11, -4, 4, 4, -11, -11, -4, -4, -4, -11, -4,
  1700. -4, -4, -11, 11, -4, -4, 4, -11, -4, 4, -4, -11,
  1701. };
  1702. /*
  1703. * Gradients for 4D. They approximate the directions to the
  1704. * vertices of a disprismatotesseractihexadecachoron from the center,
  1705. * skewed so that the tetrahedral and cubic facets can be inscribed inside
  1706. * spheres of the same radius.
  1707. */
  1708. static const signed char gradients4D[] = {
  1709. 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 3, -3, 1, 1, 1, -1, 3, 1, 1,
  1710. -1, 1, 3, 1, -1, 1, 1, 3, 3, -1, 1, 1, 1, -3, 1, 1, 1, -1, 3, 1, 1, -1, 1,
  1711. 3, -3, -1, 1, 1, -1, -3, 1, 1, -1, -1, 3, 1, -1, -1, 1, 3, 3, 1, -1, 1, 1,
  1712. 3, -1, 1, 1, 1, -3, 1, 1, 1, -1, 3, -3, 1, -1, 1, -1, 3, -1, 1, -1, 1, -3,
  1713. 1, -1, 1, -1, 3, 3, -1, -1, 1, 1, -3, -1, 1, 1, -1, -3, 1, 1, -1, -1, 3, -3,
  1714. -1, -1, 1, -1, -3, -1, 1, -1, -1, -3, 1, -1, -1, -1, 3, 3, 1, 1, -1, 1, 3,
  1715. 1, -1, 1, 1, 3, -1, 1, 1, 1, -3, -3, 1, 1, -1, -1, 3, 1, -1, -1, 1, 3, -1,
  1716. -1, 1, 1, -3, 3, -1, 1, -1, 1, -3, 1, -1, 1, -1, 3, -1, 1, -1, 1, -3, -3,
  1717. -1, 1, -1, -1, -3, 1, -1, -1, -1, 3, -1, -1, -1, 1, -3, 3, 1, -1, -1, 1, 3,
  1718. -1, -1, 1, 1, -3, -1, 1, 1, -1, -3, -3, 1, -1, -1, -1, 3, -1, -1, -1, 1, -3,
  1719. -1, -1, 1, -1, -3, 3, -1, -1, -1, 1, -3, -1, -1, 1, -1, -3, -1, 1, -1, -1,
  1720. -3, -3, -1, -1, -1, -1, -3, -1, -1, -1, -1, -3, -1, -1, -1, -1, -3,
  1721. };
  1722. static double extrapolate2(
  1723. struct osn_context* ctx, int xsb, int ysb, double dx, double dy)
  1724. {
  1725. int16_t* perm = ctx->perm;
  1726. int index = perm[(perm[xsb & 0xFF] + ysb) & 0xFF] & 0x0E;
  1727. return gradients2D[index] * dx + gradients2D[index + 1] * dy;
  1728. }
  1729. static inline int fastFloor(double x)
  1730. {
  1731. int xi = (int) x;
  1732. return x < xi ? xi - 1 : xi;
  1733. }
  1734. static int allocate_perm(struct osn_context* ctx, int nperm, int ngrad)
  1735. {
  1736. PAR_FREE(ctx->perm);
  1737. PAR_FREE(ctx->permGradIndex3D);
  1738. ctx->perm = PAR_MALLOC(int16_t, nperm);
  1739. if (!ctx->perm) {
  1740. return -ENOMEM;
  1741. }
  1742. ctx->permGradIndex3D = PAR_MALLOC(int16_t, ngrad);
  1743. if (!ctx->permGradIndex3D) {
  1744. PAR_FREE(ctx->perm);
  1745. return -ENOMEM;
  1746. }
  1747. return 0;
  1748. }
  1749. static int par__simplex_noise(int64_t seed, struct osn_context** ctx)
  1750. {
  1751. int rc;
  1752. int16_t source[256];
  1753. int i;
  1754. int16_t* perm;
  1755. int16_t* permGradIndex3D;
  1756. *ctx = PAR_MALLOC(struct osn_context, 1);
  1757. if (!(*ctx)) {
  1758. return -ENOMEM;
  1759. }
  1760. (*ctx)->perm = NULL;
  1761. (*ctx)->permGradIndex3D = NULL;
  1762. rc = allocate_perm(*ctx, 256, 256);
  1763. if (rc) {
  1764. PAR_FREE(*ctx);
  1765. return rc;
  1766. }
  1767. perm = (*ctx)->perm;
  1768. permGradIndex3D = (*ctx)->permGradIndex3D;
  1769. for (i = 0; i < 256; i++) {
  1770. source[i] = (int16_t) i;
  1771. }
  1772. seed = seed * 6364136223846793005LL + 1442695040888963407LL;
  1773. seed = seed * 6364136223846793005LL + 1442695040888963407LL;
  1774. seed = seed * 6364136223846793005LL + 1442695040888963407LL;
  1775. for (i = 255; i >= 0; i--) {
  1776. seed = seed * 6364136223846793005LL + 1442695040888963407LL;
  1777. int r = (int) ((seed + 31) % (i + 1));
  1778. if (r < 0)
  1779. r += (i + 1);
  1780. perm[i] = source[r];
  1781. permGradIndex3D[i] =
  1782. (short) ((perm[i] % (ARRAYSIZE(gradients3D) / 3)) * 3);
  1783. source[r] = source[i];
  1784. }
  1785. return 0;
  1786. }
  1787. static void par__simplex_noise_free(struct osn_context* ctx)
  1788. {
  1789. if (!ctx)
  1790. return;
  1791. if (ctx->perm) {
  1792. PAR_FREE(ctx->perm);
  1793. ctx->perm = NULL;
  1794. }
  1795. if (ctx->permGradIndex3D) {
  1796. PAR_FREE(ctx->permGradIndex3D);
  1797. ctx->permGradIndex3D = NULL;
  1798. }
  1799. PAR_FREE(ctx);
  1800. }
  1801. static double par__simplex_noise2(struct osn_context* ctx, double x, double y)
  1802. {
  1803. // Place input coordinates onto grid.
  1804. double stretchOffset = (x + y) * STRETCH_CONSTANT_2D;
  1805. double xs = x + stretchOffset;
  1806. double ys = y + stretchOffset;
  1807. // Floor to get grid coordinates of rhombus (stretched square) super-cell
  1808. // origin.
  1809. int xsb = fastFloor(xs);
  1810. int ysb = fastFloor(ys);
  1811. // Skew out to get actual coordinates of rhombus origin. We'll need these
  1812. // later.
  1813. double squishOffset = (xsb + ysb) * SQUISH_CONSTANT_2D;
  1814. double xb = xsb + squishOffset;
  1815. double yb = ysb + squishOffset;
  1816. // Compute grid coordinates relative to rhombus origin.
  1817. double xins = xs - xsb;
  1818. double yins = ys - ysb;
  1819. // Sum those together to get a value that determines which region we're in.
  1820. double inSum = xins + yins;
  1821. // Positions relative to origin point.
  1822. double dx0 = x - xb;
  1823. double dy0 = y - yb;
  1824. // We'll be defining these inside the next block and using them afterwards.
  1825. double dx_ext, dy_ext;
  1826. int xsv_ext, ysv_ext;
  1827. double value = 0;
  1828. // Contribution (1,0)
  1829. double dx1 = dx0 - 1 - SQUISH_CONSTANT_2D;
  1830. double dy1 = dy0 - 0 - SQUISH_CONSTANT_2D;
  1831. double attn1 = 2 - dx1 * dx1 - dy1 * dy1;
  1832. if (attn1 > 0) {
  1833. attn1 *= attn1;
  1834. value += attn1 * attn1 * extrapolate2(ctx, xsb + 1, ysb + 0, dx1, dy1);
  1835. }
  1836. // Contribution (0,1)
  1837. double dx2 = dx0 - 0 - SQUISH_CONSTANT_2D;
  1838. double dy2 = dy0 - 1 - SQUISH_CONSTANT_2D;
  1839. double attn2 = 2 - dx2 * dx2 - dy2 * dy2;
  1840. if (attn2 > 0) {
  1841. attn2 *= attn2;
  1842. value += attn2 * attn2 * extrapolate2(ctx, xsb + 0, ysb + 1, dx2, dy2);
  1843. }
  1844. if (inSum <= 1) { // We're inside the triangle (2-Simplex) at (0,0)
  1845. double zins = 1 - inSum;
  1846. if (zins > xins || zins > yins) {
  1847. if (xins > yins) {
  1848. xsv_ext = xsb + 1;
  1849. ysv_ext = ysb - 1;
  1850. dx_ext = dx0 - 1;
  1851. dy_ext = dy0 + 1;
  1852. } else {
  1853. xsv_ext = xsb - 1;
  1854. ysv_ext = ysb + 1;
  1855. dx_ext = dx0 + 1;
  1856. dy_ext = dy0 - 1;
  1857. }
  1858. } else { //(1,0) and (0,1) are the closest two vertices.
  1859. xsv_ext = xsb + 1;
  1860. ysv_ext = ysb + 1;
  1861. dx_ext = dx0 - 1 - 2 * SQUISH_CONSTANT_2D;
  1862. dy_ext = dy0 - 1 - 2 * SQUISH_CONSTANT_2D;
  1863. }
  1864. } else { // We're inside the triangle (2-Simplex) at (1,1)
  1865. double zins = 2 - inSum;
  1866. if (zins < xins || zins < yins) {
  1867. if (xins > yins) {
  1868. xsv_ext = xsb + 2;
  1869. ysv_ext = ysb + 0;
  1870. dx_ext = dx0 - 2 - 2 * SQUISH_CONSTANT_2D;
  1871. dy_ext = dy0 + 0 - 2 * SQUISH_CONSTANT_2D;
  1872. } else {
  1873. xsv_ext = xsb + 0;
  1874. ysv_ext = ysb + 2;
  1875. dx_ext = dx0 + 0 - 2 * SQUISH_CONSTANT_2D;
  1876. dy_ext = dy0 - 2 - 2 * SQUISH_CONSTANT_2D;
  1877. }
  1878. } else { //(1,0) and (0,1) are the closest two vertices.
  1879. dx_ext = dx0;
  1880. dy_ext = dy0;
  1881. xsv_ext = xsb;
  1882. ysv_ext = ysb;
  1883. }
  1884. xsb += 1;
  1885. ysb += 1;
  1886. dx0 = dx0 - 1 - 2 * SQUISH_CONSTANT_2D;
  1887. dy0 = dy0 - 1 - 2 * SQUISH_CONSTANT_2D;
  1888. }
  1889. // Contribution (0,0) or (1,1)
  1890. double attn0 = 2 - dx0 * dx0 - dy0 * dy0;
  1891. if (attn0 > 0) {
  1892. attn0 *= attn0;
  1893. value += attn0 * attn0 * extrapolate2(ctx, xsb, ysb, dx0, dy0);
  1894. }
  1895. // Extra Vertex
  1896. double attn_ext = 2 - dx_ext * dx_ext - dy_ext * dy_ext;
  1897. if (attn_ext > 0) {
  1898. attn_ext *= attn_ext;
  1899. value += attn_ext * attn_ext *
  1900. extrapolate2(ctx, xsv_ext, ysv_ext, dx_ext, dy_ext);
  1901. }
  1902. return value / NORM_CONSTANT_2D;
  1903. }
  1904. void par_shapes_remove_degenerate(par_shapes_mesh* mesh, float mintriarea)
  1905. {
  1906. int ntriangles = 0;
  1907. PAR_SHAPES_T* triangles = PAR_MALLOC(PAR_SHAPES_T, mesh->ntriangles * 3);
  1908. PAR_SHAPES_T* dst = triangles;
  1909. PAR_SHAPES_T const* src = mesh->triangles;
  1910. float next[3], prev[3], cp[3];
  1911. float mincplen2 = (mintriarea * 2) * (mintriarea * 2);
  1912. for (int f = 0; f < mesh->ntriangles; f++, src += 3) {
  1913. float const* pa = mesh->points + 3 * src[0];
  1914. float const* pb = mesh->points + 3 * src[1];
  1915. float const* pc = mesh->points + 3 * src[2];
  1916. par_shapes__copy3(next, pb);
  1917. par_shapes__subtract3(next, pa);
  1918. par_shapes__copy3(prev, pc);
  1919. par_shapes__subtract3(prev, pa);
  1920. par_shapes__cross3(cp, next, prev);
  1921. float cplen2 = par_shapes__dot3(cp, cp);
  1922. if (cplen2 >= mincplen2) {
  1923. *dst++ = src[0];
  1924. *dst++ = src[1];
  1925. *dst++ = src[2];
  1926. ntriangles++;
  1927. }
  1928. }
  1929. mesh->ntriangles = ntriangles;
  1930. PAR_FREE(mesh->triangles);
  1931. mesh->triangles = triangles;
  1932. }
  1933. #endif // PAR_SHAPES_IMPLEMENTATION
  1934. #endif // PAR_SHAPES_H
  1935. // par_shapes is distributed under the MIT license:
  1936. //
  1937. // Copyright (c) 2019 Philip Rideout
  1938. //
  1939. // Permission is hereby granted, free of charge, to any person obtaining a copy
  1940. // of this software and associated documentation files (the "Software"), to deal
  1941. // in the Software without restriction, including without limitation the rights
  1942. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  1943. // copies of the Software, and to permit persons to whom the Software is
  1944. // furnished to do so, subject to the following conditions:
  1945. //
  1946. // The above copyright notice and this permission notice shall be included in
  1947. // all copies or substantial portions of the Software.
  1948. //
  1949. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1950. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1951. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1952. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1953. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1954. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1955. // SOFTWARE.