par_streamlines.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. // STREAMLINES :: https://prideout.net/blog/par_streamlines/
  2. // Simple C library for triangulating wide lines, curves, and streamlines.
  3. //
  4. // Usage example:
  5. //
  6. // #define PAR_STREAMLINES_IMPLEMENTATION
  7. // #include "par_streamlines.h"
  8. //
  9. // parsl_context* ctx = parsl_create_context({ .thickness = 3 });
  10. // parsl_position vertices[] = { {0, 0}, {2, 1}, {4, 0} };
  11. // uint16_t spine_lengths[] = { 3 };
  12. // parsl_mesh* mesh = parsl_mesh_from_lines(ctx, {
  13. // .num_vertices = sizeof(vertices) / sizeof(parsl_position),
  14. // .num_spines = sizeof(spine_lengths) / sizeof(uint16_t),
  15. // .vertices = vertices,
  16. // .spine_lengths = spine_lengths
  17. // });
  18. // ...
  19. // parsl_destroy_context(ctx);
  20. //
  21. // Distributed under the MIT License, see bottom of file.
  22. #ifndef PAR_STREAMLINES_H
  23. #define PAR_STREAMLINES_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. // Configures how the library assigns UV coordinates.
  30. typedef enum {
  31. PAR_U_MODE_NORMALIZED_DISTANCE, // this is the default
  32. PAR_U_MODE_DISTANCE, // non-normalized distance along the curve
  33. PAR_U_MODE_SEGMENT_INDEX, // starts at zero for each curve, counts up
  34. PAR_U_MODE_SEGMENT_FRACTION, // 0.0, 1.0 / COUNT, 2.0 / COUNT, etc...
  35. } parsl_u_mode;
  36. // Layout for generated vertex attributes.
  37. typedef struct {
  38. float u_along_curve; // longitudinal coordinate (see parsl_u_mode)
  39. float v_across_curve; // either + or - depending on the side
  40. float spine_to_edge_x; // normalized vector from spine to edge
  41. float spine_to_edge_y; // normalized vector from spine to edge
  42. } parsl_annotation;
  43. // Simple two-tuple math type used for mesh and spine vertices.
  44. typedef struct {
  45. float x;
  46. float y;
  47. } parsl_position;
  48. // Triangle mesh generated by the library. The vertex data is owned by
  49. // streamlines context and becomes invalid on any subsequent call to the API.
  50. // The annotations, spine_lengths, and random_offsets fields are null unless
  51. // their corresponding flags have been set in parsl_config.
  52. typedef struct {
  53. uint32_t num_vertices;
  54. uint32_t num_triangles;
  55. uint32_t* triangle_indices;
  56. parsl_position* positions;
  57. parsl_annotation* annotations;
  58. float* spine_lengths;
  59. float* random_offsets;
  60. } parsl_mesh;
  61. // Viewport for streamline seed placement.
  62. typedef struct {
  63. float left;
  64. float top;
  65. float right;
  66. float bottom;
  67. } parsl_viewport;
  68. #define PARSL_FLAG_WIREFRAME (1 << 0) // enables 4 indices per triangle
  69. #define PARSL_FLAG_ANNOTATIONS (1 << 1) // populates mesh.annotations
  70. #define PARSL_FLAG_SPINE_LENGTHS (1 << 2) // populates mesh.lengths
  71. #define PARSL_FLAG_RANDOM_OFFSETS (1 << 3) // populates mesh.random_offsets
  72. #define PARSL_FLAG_CURVE_GUIDES (1 << 4) // draws control points
  73. // Immutable configuration for a streamlines context.
  74. typedef struct {
  75. float thickness;
  76. uint32_t flags;
  77. parsl_u_mode u_mode;
  78. float curves_max_flatness;
  79. float streamlines_seed_spacing;
  80. parsl_viewport streamlines_seed_viewport;
  81. float miter_limit;
  82. } parsl_config;
  83. // Client-owned list of line strips that will be tessellated.
  84. typedef struct {
  85. uint32_t num_vertices;
  86. uint16_t num_spines;
  87. parsl_position* vertices;
  88. uint16_t* spine_lengths;
  89. bool closed;
  90. } parsl_spine_list;
  91. // Opaque handle to a streamlines context and its memory arena.
  92. typedef struct parsl_context_s parsl_context;
  93. // Client function that moves a streamline particle by a single time step.
  94. typedef void (*parsl_advection_callback)(parsl_position* point, void* userdata);
  95. parsl_context* parsl_create_context(parsl_config config);
  96. void parsl_destroy_context(parsl_context* ctx);
  97. // Low-level function that simply generates two triangles for each line segment.
  98. parsl_mesh* parsl_mesh_from_lines(parsl_context* ctx, parsl_spine_list spines);
  99. // High-level function that can be used to visualize a vector field.
  100. parsl_mesh* parsl_mesh_from_streamlines(parsl_context* context,
  101. parsl_advection_callback advect, uint32_t first_tick, uint32_t num_ticks,
  102. void* userdata);
  103. // High-level function that tessellates a series of curves into triangles,
  104. // where each spine is a series of chained cubic Bézier curves.
  105. //
  106. // The first curve of each spine is defined by an endpoint, followed by two
  107. // control points, followed by an endpoint. Every subsequent curve in the spine
  108. // is defined by a single control point followed by an endpoint. Only one
  109. // control point is required because the first control point is computed via
  110. // reflection over the endpoint.
  111. //
  112. // The number of vertices in each spine should be 4+(n-1)*2 where n is the
  113. // number of piecewise curves.
  114. //
  115. // Each spine is equivalent to an SVG path that looks like M C S S S.
  116. parsl_mesh* parsl_mesh_from_curves_cubic(parsl_context* context,
  117. parsl_spine_list spines);
  118. // High-level function that tessellates a series of curves into triangles,
  119. // where each spine is a series of chained quadratic Bézier curves.
  120. //
  121. // The first curve of each spine is defined by an endpoint, followed by one
  122. // control point, followed by an endpoint. Every subsequent curve in the spine
  123. // is defined by a single control point followed by an endpoint.
  124. //
  125. // The number of vertices in each spine should be 3+(n-1)*2 where n is the
  126. // number of piecewise curves.
  127. //
  128. // Each spine is equivalent to an SVG path that looks like M Q M Q M Q.
  129. parsl_mesh* parsl_mesh_from_curves_quadratic(parsl_context* context,
  130. parsl_spine_list spines);
  131. #ifdef __cplusplus
  132. }
  133. #endif
  134. // -----------------------------------------------------------------------------
  135. // END PUBLIC API
  136. // -----------------------------------------------------------------------------
  137. #ifdef PAR_STREAMLINES_IMPLEMENTATION
  138. #include <assert.h>
  139. #include <limits.h>
  140. #include <math.h>
  141. #include <memory.h>
  142. #include <stdlib.h>
  143. static float parsl__dot(parsl_position a, parsl_position b) {
  144. return a.x * b.x + a.y * b.y;
  145. }
  146. static parsl_position parsl__sub(parsl_position a, parsl_position b) {
  147. return (parsl_position) { a.x - b.x, a.y - b.y };
  148. }
  149. static parsl_position parsl__add(parsl_position a, parsl_position b) {
  150. return (parsl_position) { a.x + b.x, a.y + b.y };
  151. }
  152. static parsl_position parsl_mul(parsl_position v, float s) {
  153. return (parsl_position) { v.x * s, v.y * s };
  154. }
  155. #define PARSL_MAX_RECURSION 16
  156. #ifndef PAR_PI
  157. #define PAR_PI (3.14159265359)
  158. #define PAR_MIN(a, b) (a > b ? b : a)
  159. #define PAR_MAX(a, b) (a > b ? a : b)
  160. #define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
  161. #define PAR_SWAP(T, A, B) { T tmp = B; B = A; A = tmp; }
  162. #define PAR_SQR(a) ((a) * (a))
  163. #endif
  164. #ifndef PAR_MALLOC
  165. #define PAR_MALLOC(T, N) ((T*) malloc(N * sizeof(T)))
  166. #define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
  167. #define PAR_REALLOC(T, BUF, N) ((T*) realloc(BUF, sizeof(T) * (N)))
  168. #define PAR_FREE(BUF) free(BUF)
  169. #endif
  170. #ifndef PAR_ARRAY
  171. #define PAR_ARRAY
  172. #define pa_free(a) ((a) ? PAR_FREE(pa___raw(a)), 0 : 0)
  173. #define pa_push(a, v) (pa___maybegrow(a, (int) 1), (a)[pa___n(a)++] = (v))
  174. #define pa_count(a) ((a) ? pa___n(a) : 0)
  175. #define pa_add(a, n) (pa___maybegrow(a, (int) n), pa___n(a) += (n))
  176. #define pa_last(a) ((a)[pa___n(a) - 1])
  177. #define pa_end(a) (a + pa_count(a))
  178. #define pa_clear(arr) if (arr) pa___n(arr) = 0
  179. #define pa___raw(a) ((int*) (a) -2)
  180. #define pa___m(a) pa___raw(a)[0]
  181. #define pa___n(a) pa___raw(a)[1]
  182. #define pa___needgrow(a, n) ((a) == 0 || pa___n(a) + ((int) n) >= pa___m(a))
  183. #define pa___maybegrow(a, n) (pa___needgrow(a, (n)) ? pa___grow(a, n) : 0)
  184. #define pa___grow(a, n) (*((void**)& (a)) = pa___growf((void*) (a), (n), \
  185. sizeof(*(a))))
  186. // ptr[-2] is capacity, ptr[-1] is size.
  187. static void* pa___growf(void* arr, int increment, int itemsize)
  188. {
  189. int dbl_cur = arr ? 2 * pa___m(arr) : 0;
  190. int min_needed = pa_count(arr) + increment;
  191. int m = dbl_cur > min_needed ? dbl_cur : min_needed;
  192. int* p = (int *) PAR_REALLOC(uint8_t, arr ? pa___raw(arr) : 0,
  193. itemsize * m + sizeof(int) * 2);
  194. if (p) {
  195. if (!arr) {
  196. p[1] = 0;
  197. }
  198. p[0] = m;
  199. return p + 2;
  200. }
  201. return (void*) (2 * sizeof(int));
  202. }
  203. #endif
  204. struct parsl_context_s {
  205. parsl_config config;
  206. parsl_mesh result;
  207. parsl_position* streamline_seeds;
  208. parsl_position* streamline_points;
  209. parsl_spine_list streamline_spines;
  210. parsl_spine_list curve_spines;
  211. uint16_t guideline_start;
  212. };
  213. parsl_context* parsl_create_context(parsl_config config)
  214. {
  215. parsl_context* context = PAR_CALLOC(parsl_context, 1);
  216. context->config = config;
  217. return context;
  218. }
  219. void parsl_destroy_context(parsl_context* context)
  220. {
  221. pa_free(context->result.triangle_indices);
  222. pa_free(context->result.spine_lengths);
  223. pa_free(context->result.annotations);
  224. pa_free(context->result.positions);
  225. pa_free(context->result.random_offsets);
  226. pa_free(context->streamline_seeds);
  227. pa_free(context->streamline_points);
  228. pa_free(context->streamline_spines.spine_lengths);
  229. pa_free(context->streamline_spines.vertices);
  230. pa_free(context->curve_spines.spine_lengths);
  231. pa_free(context->curve_spines.vertices);
  232. PAR_FREE(context);
  233. }
  234. parsl_mesh* parsl_mesh_from_lines(parsl_context* context,
  235. parsl_spine_list spines)
  236. {
  237. typedef parsl_position Position;
  238. typedef parsl_annotation Annotation;
  239. parsl_mesh* mesh = &context->result;
  240. const bool closed = spines.closed;
  241. const bool wireframe = context->config.flags & PARSL_FLAG_WIREFRAME;
  242. const bool has_annotations = context->config.flags & PARSL_FLAG_ANNOTATIONS;
  243. const bool has_lengths = context->config.flags & PARSL_FLAG_SPINE_LENGTHS;
  244. const float miter_limit = context->config.miter_limit ?
  245. context->config.miter_limit : (context->config.thickness * 2);
  246. const float miter_acos_max = +1.0;
  247. const float miter_acos_min = -1.0;
  248. const uint32_t ind_per_tri = wireframe ? 4 : 3;
  249. mesh->num_vertices = 0;
  250. mesh->num_triangles = 0;
  251. for (uint32_t spine = 0; spine < spines.num_spines; spine++) {
  252. assert(spines.spine_lengths[spine] > 1);
  253. mesh->num_vertices += 2 * spines.spine_lengths[spine];
  254. mesh->num_triangles += 2 * (spines.spine_lengths[spine] - 1);
  255. if (closed) {
  256. mesh->num_vertices += 2;
  257. mesh->num_triangles += 2;
  258. }
  259. }
  260. pa_clear(mesh->spine_lengths);
  261. pa_clear(mesh->annotations);
  262. pa_clear(mesh->positions);
  263. pa_clear(mesh->triangle_indices);
  264. if (has_lengths) {
  265. pa_add(mesh->spine_lengths, mesh->num_vertices);
  266. }
  267. if (has_annotations) {
  268. pa_add(mesh->annotations, mesh->num_vertices);
  269. }
  270. pa_add(mesh->positions, mesh->num_vertices);
  271. pa_add(mesh->triangle_indices, ind_per_tri * mesh->num_triangles);
  272. float* dst_lengths = mesh->spine_lengths;
  273. Annotation* dst_annotations = mesh->annotations;
  274. Position* dst_positions = mesh->positions;
  275. uint32_t* dst_indices = mesh->triangle_indices;
  276. const Position* src_position = spines.vertices;
  277. uint32_t base_index = 0;
  278. for (uint16_t spine = 0; spine < spines.num_spines; spine++) {
  279. const bool thin = context->guideline_start > 0 &&
  280. spine >= context->guideline_start;
  281. const float thickness = thin ? 1.0f : context->config.thickness;
  282. const uint16_t spine_length = spines.spine_lengths[spine];
  283. float dx = src_position[1].x - src_position[0].x;
  284. float dy = src_position[1].y - src_position[0].y;
  285. float segment_length = sqrtf(dx * dx + dy * dy);
  286. float invlen = segment_length ? 1.0f / segment_length : 0.0f;
  287. const float nx = -dy * invlen;
  288. const float ny = dx * invlen;
  289. const Position first_src_position = src_position[0];
  290. const Position last_src_position = src_position[spine_length - 1];
  291. float ex = nx * thickness / 2;
  292. float ey = ny * thickness / 2;
  293. if (closed) {
  294. const float dx = src_position[0].x - last_src_position.x;
  295. const float dy = src_position[0].y - last_src_position.y;
  296. const float segment_length = sqrtf(dx * dx + dy * dy);
  297. float invlen = segment_length ? 1.0f / segment_length : 0.0f;
  298. const float pnx = -dy * invlen;
  299. const float pny = dx * invlen;
  300. // NOTE: sin(pi / 2 - acos(X) / 2) == sqrt(1 + X) / sqrt(2)
  301. float extent = 0.5 * thickness;
  302. const float dotp = (pnx * nx + pny * ny);
  303. if (dotp < miter_acos_max && dotp > miter_acos_min) {
  304. const float phi = acos(dotp) / 2;
  305. const float theta = PAR_PI / 2 - phi;
  306. extent = PAR_CLAMP(extent / sin(theta), -miter_limit,
  307. miter_limit);
  308. }
  309. ex = pnx + nx;
  310. ey = pny + ny;
  311. const float len = sqrtf(ex * ex + ey * ey);
  312. invlen = len == 0.0 ? 0.0 : (1.0f / len);
  313. ex *= invlen * extent;
  314. ey *= invlen * extent;
  315. }
  316. dst_positions[0].x = src_position[0].x + ex;
  317. dst_positions[0].y = src_position[0].y + ey;
  318. dst_positions[1].x = src_position[0].x - ex;
  319. dst_positions[1].y = src_position[0].y - ey;
  320. float pnx = nx;
  321. float pny = ny;
  322. const Position first_dst_positions[2] = {
  323. dst_positions[0],
  324. dst_positions[1]
  325. };
  326. src_position++;
  327. dst_positions += 2;
  328. if (has_annotations) {
  329. dst_annotations[0].u_along_curve = 0;
  330. dst_annotations[1].u_along_curve = 0;
  331. dst_annotations[0].v_across_curve = 1;
  332. dst_annotations[1].v_across_curve = -1;
  333. dst_annotations[0].spine_to_edge_x = ex;
  334. dst_annotations[1].spine_to_edge_x = -ex;
  335. dst_annotations[0].spine_to_edge_y = ey;
  336. dst_annotations[1].spine_to_edge_y = -ey;
  337. dst_annotations += 2;
  338. }
  339. float distance_along_spine = segment_length;
  340. uint16_t segment_index = 1;
  341. for (; segment_index < spine_length - 1; segment_index++) {
  342. const float dx = src_position[1].x - src_position[0].x;
  343. const float dy = src_position[1].y - src_position[0].y;
  344. const float segment_length = sqrtf(dx * dx + dy * dy);
  345. float invlen = segment_length ? 1.0f / segment_length : 0.0f;
  346. const float nx = -dy * invlen;
  347. const float ny = dx * invlen;
  348. // NOTE: sin(pi / 2 - acos(X) / 2) == sqrt(1 + X) / sqrt(2)
  349. float extent = 0.5 * thickness;
  350. const float dotp = (pnx * nx + pny * ny);
  351. if (dotp < miter_acos_max && dotp > miter_acos_min) {
  352. const float phi = acos(dotp) / 2;
  353. const float theta = PAR_PI / 2 - phi;
  354. extent = PAR_CLAMP(extent / sin(theta), -miter_limit,
  355. miter_limit);
  356. }
  357. float ex = pnx + nx;
  358. float ey = pny + ny;
  359. const float len = sqrtf(ex * ex + ey * ey);
  360. invlen = len == 0.0 ? 0.0 : (1.0f / len);
  361. ex *= invlen * extent;
  362. ey *= invlen * extent;
  363. dst_positions[0].x = src_position[0].x + ex;
  364. dst_positions[0].y = src_position[0].y + ey;
  365. dst_positions[1].x = src_position[0].x - ex;
  366. dst_positions[1].y = src_position[0].y - ey;
  367. src_position++;
  368. dst_positions += 2;
  369. pnx = nx;
  370. pny = ny;
  371. if (has_annotations) {
  372. dst_annotations[0].u_along_curve = distance_along_spine;
  373. dst_annotations[1].u_along_curve = distance_along_spine;
  374. dst_annotations[0].v_across_curve = 1;
  375. dst_annotations[1].v_across_curve = -1;
  376. dst_annotations[0].spine_to_edge_x = ex;
  377. dst_annotations[1].spine_to_edge_x = -ex;
  378. dst_annotations[0].spine_to_edge_y = ey;
  379. dst_annotations[1].spine_to_edge_y = -ey;
  380. dst_annotations += 2;
  381. }
  382. distance_along_spine += segment_length;
  383. if (wireframe) {
  384. dst_indices[0] = base_index + (segment_index - 1) * 2;
  385. dst_indices[1] = base_index + (segment_index - 1) * 2 + 1;
  386. dst_indices[2] = base_index + (segment_index - 0) * 2;
  387. dst_indices[3] = base_index + (segment_index - 1) * 2;
  388. dst_indices[4] = base_index + (segment_index - 0) * 2;
  389. dst_indices[5] = base_index + (segment_index - 1) * 2 + 1;
  390. dst_indices[6] = base_index + (segment_index - 0) * 2 + 1;
  391. dst_indices[7] = base_index + (segment_index - 0) * 2;
  392. dst_indices += 8;
  393. } else {
  394. dst_indices[0] = base_index + (segment_index - 1) * 2;
  395. dst_indices[1] = base_index + (segment_index - 1) * 2 + 1;
  396. dst_indices[2] = base_index + (segment_index - 0) * 2;
  397. dst_indices[3] = base_index + (segment_index - 0) * 2;
  398. dst_indices[4] = base_index + (segment_index - 1) * 2 + 1;
  399. dst_indices[5] = base_index + (segment_index - 0) * 2 + 1;
  400. dst_indices += 6;
  401. }
  402. }
  403. ex = pnx * thickness / 2;
  404. ey = pny * thickness / 2;
  405. if (closed) {
  406. const float dx = first_src_position.x - src_position[0].x;
  407. const float dy = first_src_position.y - src_position[0].y;
  408. segment_length = sqrtf(dx * dx + dy * dy);
  409. float invlen = segment_length ? 1.0f / segment_length : 0.0f;
  410. const float nx = -dy * invlen;
  411. const float ny = dx * invlen;
  412. // NOTE: sin(pi / 2 - acos(X) / 2) == sqrt(1 + X) / sqrt(2)
  413. float extent = 0.5 * thickness;
  414. const float dotp = (pnx * nx + pny * ny);
  415. if (dotp < miter_acos_max && dotp > miter_acos_min) {
  416. const float phi = acos(dotp) / 2;
  417. const float theta = PAR_PI / 2 - phi;
  418. extent = PAR_CLAMP(extent / sin(theta), -miter_limit,
  419. miter_limit);
  420. }
  421. ex = pnx + nx;
  422. ey = pny + ny;
  423. const float len = sqrtf(ex * ex + ey * ey);
  424. invlen = len == 0.0 ? 0.0 : (1.0f / len);
  425. ex *= invlen * extent;
  426. ey *= invlen * extent;
  427. }
  428. dst_positions[0].x = src_position[0].x + ex;
  429. dst_positions[0].y = src_position[0].y + ey;
  430. dst_positions[1].x = src_position[0].x - ex;
  431. dst_positions[1].y = src_position[0].y - ey;
  432. src_position++;
  433. dst_positions += 2;
  434. pnx = nx;
  435. pny = ny;
  436. if (has_annotations) {
  437. dst_annotations[0].u_along_curve = distance_along_spine;
  438. dst_annotations[1].u_along_curve = distance_along_spine;
  439. dst_annotations[0].v_across_curve = 1;
  440. dst_annotations[1].v_across_curve = -1;
  441. dst_annotations[0].spine_to_edge_x = ex;
  442. dst_annotations[1].spine_to_edge_x = -ex;
  443. dst_annotations[0].spine_to_edge_y = ey;
  444. dst_annotations[1].spine_to_edge_y = -ey;
  445. dst_annotations += 2;
  446. }
  447. if (wireframe) {
  448. dst_indices[0] = base_index + (segment_index - 1) * 2;
  449. dst_indices[1] = base_index + (segment_index - 1) * 2 + 1;
  450. dst_indices[2] = base_index + (segment_index - 0) * 2;
  451. dst_indices[3] = base_index + (segment_index - 1) * 2;
  452. dst_indices[4] = base_index + (segment_index - 0) * 2;
  453. dst_indices[5] = base_index + (segment_index - 1) * 2 + 1;
  454. dst_indices[6] = base_index + (segment_index - 0) * 2 + 1;
  455. dst_indices[7] = base_index + (segment_index - 0) * 2;
  456. dst_indices += 8;
  457. } else {
  458. dst_indices[0] = base_index + (segment_index - 1) * 2;
  459. dst_indices[1] = base_index + (segment_index - 1) * 2 + 1;
  460. dst_indices[2] = base_index + (segment_index - 0) * 2;
  461. dst_indices[3] = base_index + (segment_index - 0) * 2;
  462. dst_indices[4] = base_index + (segment_index - 1) * 2 + 1;
  463. dst_indices[5] = base_index + (segment_index - 0) * 2 + 1;
  464. dst_indices += 6;
  465. }
  466. if (closed) {
  467. segment_index++;
  468. distance_along_spine += segment_length;
  469. dst_positions[0] = first_dst_positions[0];
  470. dst_positions[1] = first_dst_positions[1];
  471. dst_positions += 2;
  472. if (has_annotations) {
  473. dst_annotations[0].u_along_curve = distance_along_spine;
  474. dst_annotations[1].u_along_curve = distance_along_spine;
  475. dst_annotations[0].v_across_curve = 1;
  476. dst_annotations[1].v_across_curve = -1;
  477. dst_annotations[0].spine_to_edge_x = ex;
  478. dst_annotations[1].spine_to_edge_x = -ex;
  479. dst_annotations[0].spine_to_edge_y = ey;
  480. dst_annotations[1].spine_to_edge_y = -ey;
  481. dst_annotations += 2;
  482. }
  483. if (wireframe) {
  484. dst_indices[0] = base_index + (segment_index - 1) * 2;
  485. dst_indices[1] = base_index + (segment_index - 1) * 2 + 1;
  486. dst_indices[2] = base_index + (segment_index - 0) * 2;
  487. dst_indices[3] = base_index + (segment_index - 1) * 2;
  488. dst_indices[4] = base_index + (segment_index - 0) * 2;
  489. dst_indices[5] = base_index + (segment_index - 1) * 2 + 1;
  490. dst_indices[6] = base_index + (segment_index - 0) * 2 + 1;
  491. dst_indices[7] = base_index + (segment_index - 0) * 2;
  492. dst_indices += 8;
  493. } else {
  494. dst_indices[0] = base_index + (segment_index - 1) * 2;
  495. dst_indices[1] = base_index + (segment_index - 1) * 2 + 1;
  496. dst_indices[2] = base_index + (segment_index - 0) * 2;
  497. dst_indices[3] = base_index + (segment_index - 0) * 2;
  498. dst_indices[4] = base_index + (segment_index - 1) * 2 + 1;
  499. dst_indices[5] = base_index + (segment_index - 0) * 2 + 1;
  500. dst_indices += 6;
  501. }
  502. }
  503. base_index += spine_length * 2 + (closed ? 2 : 0);
  504. const uint16_t nverts = spine_length + (closed ? 1 : 0);
  505. if (has_lengths) {
  506. for (uint16_t i = 0; i < nverts; i++) {
  507. dst_lengths[0] = distance_along_spine;
  508. dst_lengths[1] = distance_along_spine;
  509. dst_lengths += 2;
  510. }
  511. }
  512. // Go back through the curve and fix up the U coordinates.
  513. if (has_annotations) {
  514. const float invlength = 1.0f / distance_along_spine;
  515. const float invcount = 1.0f / spine_length;
  516. switch (context->config.u_mode) {
  517. case PAR_U_MODE_DISTANCE:
  518. break;
  519. case PAR_U_MODE_NORMALIZED_DISTANCE:
  520. dst_annotations -= nverts * 2;
  521. for (uint16_t i = 0; i < nverts; i++) {
  522. dst_annotations[0].u_along_curve *= invlength;
  523. dst_annotations[1].u_along_curve *= invlength;
  524. dst_annotations += 2;
  525. }
  526. break;
  527. case PAR_U_MODE_SEGMENT_INDEX:
  528. dst_annotations -= nverts * 2;
  529. for (uint16_t i = 0; i < nverts; i++) {
  530. dst_annotations[0].u_along_curve = i;
  531. dst_annotations[1].u_along_curve = i;
  532. dst_annotations += 2;
  533. }
  534. break;
  535. case PAR_U_MODE_SEGMENT_FRACTION:
  536. dst_annotations -= nverts * 2;
  537. for (uint16_t i = 0; i < nverts; i++) {
  538. dst_annotations[0].u_along_curve = invcount * i;
  539. dst_annotations[1].u_along_curve = invcount * i;
  540. dst_annotations += 2;
  541. }
  542. break;
  543. }
  544. }
  545. }
  546. assert(src_position - spines.vertices == spines.num_vertices);
  547. assert(dst_positions - mesh->positions == mesh->num_vertices);
  548. assert(dst_indices - mesh->triangle_indices ==
  549. mesh->num_triangles * ind_per_tri);
  550. if (context->config.flags & PARSL_FLAG_RANDOM_OFFSETS) {
  551. pa_clear(mesh->random_offsets);
  552. pa_add(mesh->random_offsets, mesh->num_vertices);
  553. float* pvertex = mesh->random_offsets;
  554. for (uint16_t spine = 0; spine < spines.num_spines; spine++) {
  555. const uint16_t num_segments = spines.spine_lengths[spine];
  556. const float r = (float) rand() / RAND_MAX;
  557. for (uint16_t segment = 0; segment < num_segments; segment++) {
  558. *pvertex++ = r;
  559. }
  560. }
  561. }
  562. return mesh;
  563. }
  564. // This function is designed to be called in two passes. In the first pass, the
  565. // points pointer is null, so this simply determines the number of required
  566. // points to fulfill the flatness criterion. On the second pass, points is
  567. // non-null it actually writes out the point positions.
  568. static void parsl__tesselate_cubic(
  569. parsl_position* points, uint32_t* num_points,
  570. float x0, float y0, float x1, float y1,
  571. float x2, float y2, float x3, float y3,
  572. float max_flatness_squared, int recursion_depth)
  573. {
  574. float dx0 = x1-x0;
  575. float dy0 = y1-y0;
  576. float dx1 = x2-x1;
  577. float dy1 = y2-y1;
  578. float dx2 = x3-x2;
  579. float dy2 = y3-y2;
  580. float dx = x3-x0;
  581. float dy = y3-y0;
  582. float longlen = (float) (sqrt(dx0*dx0 + dy0*dy0) + sqrt(dx1*dx1 + dy1*dy1) +
  583. sqrt(dx2*dx2 + dy2*dy2));
  584. float shortlen = (float) sqrt(dx*dx+dy*dy);
  585. float flatness_squared = longlen*longlen - shortlen*shortlen;
  586. if (recursion_depth > PARSL_MAX_RECURSION) {
  587. return;
  588. }
  589. if (flatness_squared > max_flatness_squared) {
  590. const float x01 = (x0+x1) / 2;
  591. const float y01 = (y0+y1) / 2;
  592. const float x12 = (x1+x2) / 2;
  593. const float y12 = (y1+y2) / 2;
  594. const float x23 = (x2+x3) / 2;
  595. const float y23 = (y2+y3) / 2;
  596. const float xa = (x01+x12) / 2;
  597. const float ya = (y01+y12) / 2;
  598. const float xb = (x12+x23) / 2;
  599. const float yb = (y12+y23) / 2;
  600. const float mx = (xa+xb) / 2;
  601. const float my = (ya+yb) / 2;
  602. parsl__tesselate_cubic(points, num_points, x0,y0, x01,y01, xa,ya, mx,my,
  603. max_flatness_squared, recursion_depth + 1);
  604. parsl__tesselate_cubic(points, num_points, mx,my, xb,yb, x23,y23, x3,y3,
  605. max_flatness_squared, recursion_depth + 1);
  606. return;
  607. }
  608. int n = *num_points;
  609. if (points) {
  610. points[n].x = x3;
  611. points[n].y = y3;
  612. }
  613. *num_points = n + 1;
  614. }
  615. // This function is designed to be called in two passes. In the first pass, the
  616. // points pointer is null, so this simply determines the number of required
  617. // points to fulfill the flatness criterion. On the second pass, points is
  618. // non-null it actually writes out the point positions.
  619. static void parsl__tesselate_quadratic(
  620. parsl_position* points, uint32_t* num_points,
  621. float x0, float y0, float x1, float y1, float x2, float y2,
  622. float max_flatness_squared, int recursion_depth)
  623. {
  624. const float mx = (x0 + 2 * x1 + x2) / 4;
  625. const float my = (y0 + 2 * y1 + y2) / 4;
  626. const float dx = (x0 + x2) / 2 - mx;
  627. const float dy = (y0 + y2) / 2 - my;
  628. const float flatness_squared = dx * dx + dy * dy;
  629. if (recursion_depth++ > PARSL_MAX_RECURSION) {
  630. return;
  631. }
  632. if (flatness_squared > max_flatness_squared) {
  633. parsl__tesselate_quadratic(points, num_points, x0,y0,
  634. (x0 + x1) / 2.0f, (y0 + y1) / 2.0f,
  635. mx, my,
  636. max_flatness_squared, recursion_depth);
  637. parsl__tesselate_quadratic(points, num_points, mx,my,
  638. (x1 + x2) / 2.0f, (y1 + y2) / 2.0f,
  639. x2, y2,
  640. max_flatness_squared, recursion_depth);
  641. return;
  642. }
  643. int n = *num_points;
  644. if (points) {
  645. points[n].x = x2;
  646. points[n].y = y2;
  647. }
  648. *num_points = n + 1;
  649. }
  650. parsl_mesh* parsl_mesh_from_curves_cubic(parsl_context* context,
  651. parsl_spine_list source_spines)
  652. {
  653. float max_flatness = context->config.curves_max_flatness;
  654. if (max_flatness == 0) {
  655. max_flatness = 1.0f;
  656. }
  657. const float max_flatness_squared = max_flatness * max_flatness;
  658. parsl_spine_list* target_spines = &context->curve_spines;
  659. const bool has_guides = context->config.flags & PARSL_FLAG_CURVE_GUIDES;
  660. // Determine the number of spines in the target list.
  661. target_spines->num_spines = source_spines.num_spines;
  662. if (has_guides) {
  663. for (uint32_t spine = 0; spine < source_spines.num_spines; spine++) {
  664. uint32_t spine_length = source_spines.spine_lengths[spine];
  665. uint32_t num_piecewise = 1 + (spine_length - 4) / 2;
  666. target_spines->num_spines += num_piecewise * 2;
  667. }
  668. }
  669. pa_clear(target_spines->spine_lengths);
  670. pa_add(target_spines->spine_lengths, target_spines->num_spines);
  671. // First pass: determine the number of required vertices.
  672. uint32_t total_required_spine_points = 0;
  673. const parsl_position* psource = source_spines.vertices;
  674. for (uint32_t spine = 0; spine < source_spines.num_spines; spine++) {
  675. // Source vertices look like: P1 C1 C2 P2 [C2 P2]*
  676. uint32_t spine_length = source_spines.spine_lengths[spine];
  677. assert(spine_length >= 4);
  678. assert((spine_length % 2) == 0);
  679. uint32_t num_piecewise = 1 + (spine_length - 4) / 2;
  680. // First piecewise curve.
  681. uint32_t num_required_spine_points = 1;
  682. parsl__tesselate_cubic(NULL, &num_required_spine_points,
  683. psource[0].x, psource[0].y, psource[1].x, psource[1].y,
  684. psource[2].x, psource[2].y, psource[3].x, psource[3].y,
  685. max_flatness_squared, 0);
  686. psource += 4;
  687. // Subsequent piecewise curves.
  688. for (uint32_t piecewise = 1; piecewise < num_piecewise; piecewise++) {
  689. parsl_position p1 = psource[-1];
  690. parsl_position previous_c2 = psource[-2];
  691. parsl_position c1 = parsl__sub(p1, parsl__sub(previous_c2, p1));
  692. parsl_position c2 = psource[0];
  693. parsl_position p2 = psource[1];
  694. parsl__tesselate_cubic(NULL, &num_required_spine_points,
  695. p1.x, p1.y, c1.x, c1.y, c2.x, c2.y, p2.x, p2.y,
  696. max_flatness_squared, 0);
  697. psource += 2;
  698. }
  699. target_spines->spine_lengths[spine] = num_required_spine_points;
  700. total_required_spine_points += num_required_spine_points;
  701. }
  702. if (has_guides) {
  703. uint32_t nsrcspines = source_spines.num_spines;
  704. uint16_t* guide_lengths = &target_spines->spine_lengths[nsrcspines];
  705. for (uint32_t spine = 0; spine < nsrcspines; spine++) {
  706. uint32_t spine_length = source_spines.spine_lengths[spine];
  707. uint32_t num_piecewise = 1 + (spine_length - 4) / 2;
  708. for (uint32_t pw = 0; pw < num_piecewise; pw++) {
  709. guide_lengths[0] = 2;
  710. guide_lengths[1] = 2;
  711. guide_lengths += 2;
  712. total_required_spine_points += 4;
  713. }
  714. }
  715. }
  716. // Allocate memory.
  717. target_spines->num_vertices = total_required_spine_points;
  718. pa_clear(target_spines->vertices);
  719. pa_add(target_spines->vertices, total_required_spine_points);
  720. // Second pass: write out the data.
  721. psource = source_spines.vertices;
  722. parsl_position* ptarget = target_spines->vertices;
  723. for (uint32_t spine = 0; spine < source_spines.num_spines; spine++) {
  724. // Source vertices look like: P1 C1 C2 P2 [C2 P2]*
  725. uint32_t spine_length = source_spines.spine_lengths[spine];
  726. uint32_t num_piecewise = 1 + (spine_length - 4) / 2;
  727. __attribute__((unused))
  728. parsl_position* target_spine_start = ptarget;
  729. // First piecewise curve.
  730. ptarget[0].x = psource[0].x;
  731. ptarget[0].y = psource[0].y;
  732. ptarget++;
  733. uint32_t num_written_points = 0;
  734. parsl__tesselate_cubic(ptarget, &num_written_points,
  735. psource[0].x, psource[0].y, psource[1].x, psource[1].y,
  736. psource[2].x, psource[2].y, psource[3].x, psource[3].y,
  737. max_flatness_squared, 0);
  738. psource += 4;
  739. ptarget += num_written_points;
  740. // Subsequent piecewise curves.
  741. for (uint32_t piecewise = 1; piecewise < num_piecewise; piecewise++) {
  742. parsl_position p1 = psource[-1];
  743. parsl_position previous_c2 = psource[-2];
  744. parsl_position c1 = parsl__sub(p1, parsl__sub(previous_c2, p1));
  745. parsl_position c2 = psource[0];
  746. parsl_position p2 = psource[1];
  747. num_written_points = 0;
  748. parsl__tesselate_cubic(ptarget, &num_written_points,
  749. p1.x, p1.y, c1.x, c1.y, c2.x, c2.y, p2.x, p2.y,
  750. max_flatness_squared, 0);
  751. psource += 2;
  752. ptarget += num_written_points;
  753. }
  754. __attribute__((unused))
  755. uint32_t num_written = ptarget - target_spine_start;
  756. assert(num_written == (uint32_t) target_spines->spine_lengths[spine]);
  757. }
  758. // Source vertices look like: P1 C1 C2 P2 [C2 P2]*
  759. if (has_guides) {
  760. uint32_t nsrcspines = source_spines.num_spines;
  761. context->guideline_start = nsrcspines;
  762. psource = source_spines.vertices;
  763. for (uint32_t spine = 0; spine < nsrcspines; spine++) {
  764. uint32_t spine_length = source_spines.spine_lengths[spine];
  765. uint32_t num_piecewise = 1 + (spine_length - 4) / 2;
  766. *ptarget++ = psource[0];
  767. *ptarget++ = psource[1];
  768. *ptarget++ = psource[2];
  769. *ptarget++ = psource[3];
  770. psource += 4;
  771. for (uint32_t pw = 1; pw < num_piecewise; pw++) {
  772. parsl_position p1 = psource[-1];
  773. parsl_position previous_c2 = psource[-2];
  774. parsl_position c1 = parsl__sub(p1, parsl__sub(previous_c2, p1));
  775. parsl_position c2 = psource[0];
  776. parsl_position p2 = psource[1];
  777. *ptarget++ = p1;
  778. *ptarget++ = c1;
  779. *ptarget++ = p2;
  780. *ptarget++ = c2;
  781. psource += 2;
  782. }
  783. }
  784. }
  785. assert(ptarget - target_spines->vertices == total_required_spine_points);
  786. parsl_mesh_from_lines(context, context->curve_spines);
  787. context->guideline_start = 0;
  788. return &context->result;
  789. }
  790. parsl_mesh* parsl_mesh_from_curves_quadratic(parsl_context* context,
  791. parsl_spine_list source_spines)
  792. {
  793. float max_flatness = context->config.curves_max_flatness;
  794. if (max_flatness == 0) {
  795. max_flatness = 1.0f;
  796. }
  797. const float max_flatness_squared = max_flatness * max_flatness;
  798. parsl_spine_list* target_spines = &context->curve_spines;
  799. const bool has_guides = context->config.flags & PARSL_FLAG_CURVE_GUIDES;
  800. // Determine the number of spines in the target list.
  801. target_spines->num_spines = source_spines.num_spines;
  802. if (has_guides) {
  803. target_spines->num_spines += source_spines.num_spines;
  804. }
  805. pa_clear(target_spines->spine_lengths);
  806. pa_add(target_spines->spine_lengths, target_spines->num_spines);
  807. // First pass: determine the number of required vertices.
  808. uint32_t total_required_spine_points = 0;
  809. const parsl_position* psource = source_spines.vertices;
  810. for (uint32_t spine = 0; spine < source_spines.num_spines; spine++) {
  811. // Source vertices look like: PT C PT [C PT]*
  812. uint32_t spine_length = source_spines.spine_lengths[spine];
  813. assert(spine_length >= 3);
  814. assert((spine_length % 2) == 1);
  815. uint32_t num_piecewise = 1 + (spine_length - 3) / 2;
  816. // First piecewise curve.
  817. uint32_t num_required_spine_points = 1;
  818. parsl__tesselate_quadratic(NULL, &num_required_spine_points,
  819. psource[0].x, psource[0].y, psource[1].x, psource[1].y,
  820. psource[2].x, psource[2].y, max_flatness_squared, 0);
  821. psource += 3;
  822. // Subsequent piecewise curves.
  823. for (uint32_t piecewise = 1; piecewise < num_piecewise; piecewise++) {
  824. parsl_position p1 = psource[-1];
  825. parsl_position c1 = psource[0];
  826. parsl_position p2 = psource[1];
  827. parsl__tesselate_quadratic(NULL, &num_required_spine_points,
  828. p1.x, p1.y, c1.x, c1.y, p2.x, p2.y, max_flatness_squared, 0);
  829. psource += 2;
  830. }
  831. target_spines->spine_lengths[spine] = num_required_spine_points;
  832. total_required_spine_points += num_required_spine_points;
  833. }
  834. if (has_guides) {
  835. uint32_t nsrcspines = source_spines.num_spines;
  836. uint16_t* guide_lengths = &target_spines->spine_lengths[nsrcspines];
  837. for (uint32_t spine = 0; spine < nsrcspines; spine++) {
  838. uint32_t spine_length = source_spines.spine_lengths[spine];
  839. uint32_t num_piecewise = 1 + (spine_length - 3) / 2;
  840. guide_lengths[0] = 3 + (num_piecewise - 1) * 2;
  841. total_required_spine_points += guide_lengths[0];
  842. guide_lengths++;
  843. }
  844. }
  845. // Allocate memory.
  846. target_spines->num_vertices = total_required_spine_points;
  847. pa_clear(target_spines->vertices);
  848. pa_add(target_spines->vertices, total_required_spine_points);
  849. // Second pass: write out the data.
  850. psource = source_spines.vertices;
  851. parsl_position* ptarget = target_spines->vertices;
  852. for (uint32_t spine = 0; spine < source_spines.num_spines; spine++) {
  853. // Source vertices look like: PT C PT [C PT]*
  854. uint32_t spine_length = source_spines.spine_lengths[spine];
  855. uint32_t num_piecewise = 1 + (spine_length - 3) / 2;
  856. __attribute__((unused))
  857. parsl_position* target_spine_start = ptarget;
  858. // First piecewise curve.
  859. ptarget[0].x = psource[0].x;
  860. ptarget[0].y = psource[0].y;
  861. ptarget++;
  862. uint32_t num_written_points = 0;
  863. parsl__tesselate_quadratic(ptarget, &num_written_points,
  864. psource[0].x, psource[0].y, psource[1].x, psource[1].y,
  865. psource[2].x, psource[2].y, max_flatness_squared, 0);
  866. psource += 3;
  867. ptarget += num_written_points;
  868. // Subsequent piecewise curves.
  869. for (uint32_t piecewise = 1; piecewise < num_piecewise; piecewise++) {
  870. parsl_position p1 = psource[-1];
  871. parsl_position c1 = psource[0];
  872. parsl_position p2 = psource[1];
  873. num_written_points = 0;
  874. parsl__tesselate_quadratic(ptarget, &num_written_points,
  875. p1.x, p1.y, c1.x, c1.y, p2.x, p2.y, max_flatness_squared, 0);
  876. psource += 2;
  877. ptarget += num_written_points;
  878. }
  879. __attribute__((unused))
  880. uint32_t num_written = ptarget - target_spine_start;
  881. assert(num_written == (uint32_t) target_spines->spine_lengths[spine]);
  882. }
  883. // Source vertices look like: PT C PT [C PT]*
  884. if (has_guides) {
  885. uint32_t nsrcspines = source_spines.num_spines;
  886. context->guideline_start = nsrcspines;
  887. psource = source_spines.vertices;
  888. for (uint32_t spine = 0; spine < nsrcspines; spine++) {
  889. uint32_t spine_length = source_spines.spine_lengths[spine];
  890. uint32_t num_piecewise = 1 + (spine_length - 3) / 2;
  891. *ptarget++ = psource[0];
  892. *ptarget++ = psource[1];
  893. *ptarget++ = psource[2];
  894. psource += 3;
  895. for (uint32_t pw = 1; pw < num_piecewise; pw++) {
  896. *ptarget++ = psource[0];
  897. *ptarget++ = psource[1];
  898. psource += 2;
  899. }
  900. }
  901. }
  902. assert(ptarget - target_spines->vertices == total_required_spine_points);
  903. parsl_mesh_from_lines(context, context->curve_spines);
  904. context->guideline_start = 0;
  905. return &context->result;
  906. }
  907. static unsigned int par__randhash(unsigned int seed) {
  908. unsigned int i = (seed ^ 12345391u) * 2654435769u;
  909. i ^= (i << 6) ^ (i >> 26);
  910. i *= 2654435769u;
  911. i += (i << 5) ^ (i >> 12);
  912. return i;
  913. }
  914. static float par__randhashf(unsigned int seed, float a, float b) {
  915. return (b - a) * par__randhash(seed) / (float) UINT_MAX + a;
  916. }
  917. static parsl_position par__sample_annulus(float radius, parsl_position center,
  918. int* seedptr) {
  919. unsigned int seed = *seedptr;
  920. parsl_position r;
  921. float rscale = 1.0f / UINT_MAX;
  922. while (1) {
  923. r.x = 4 * rscale * par__randhash(seed++) - 2;
  924. r.y = 4 * rscale * par__randhash(seed++) - 2;
  925. float r2 = parsl__dot(r, r);
  926. if (r2 > 1 && r2 <= 4) {
  927. break;
  928. }
  929. }
  930. *seedptr = seed;
  931. return (parsl_position) {
  932. r.x * radius + center.x,
  933. r.y * radius + center.y
  934. };
  935. }
  936. #define GRIDF(vec) \
  937. grid [(int) (vec.x * invcell) + ncols * (int) (vec.y * invcell)]
  938. #define GRIDI(vec) grid [(int) vec.y * ncols + (int) vec.x]
  939. static parsl_position* par__generate_pts(float width, float height,
  940. float radius, int seed, parsl_position* result) {
  941. int maxattempts = 30;
  942. float rscale = 1.0f / UINT_MAX;
  943. parsl_position rvec;
  944. rvec.x = rvec.y = radius;
  945. float r2 = radius * radius;
  946. // Acceleration grid.
  947. float cellsize = radius / sqrtf(2);
  948. float invcell = 1.0f / cellsize;
  949. int ncols = ceil(width * invcell);
  950. int nrows = ceil(height * invcell);
  951. int maxcol = ncols - 1;
  952. int maxrow = nrows - 1;
  953. int ncells = ncols * nrows;
  954. int* grid = (int*) PAR_MALLOC(int, ncells);
  955. for (int i = 0; i < ncells; i++) {
  956. grid[i] = -1;
  957. }
  958. // Active list and resulting sample list.
  959. int* actives = (int*) PAR_MALLOC(int, ncells);
  960. int nactives = 0;
  961. pa_clear(result);
  962. pa_add(result, ncells);
  963. parsl_position* samples = result;
  964. int nsamples = 0;
  965. // First sample.
  966. parsl_position pt;
  967. pt.x = width * par__randhash(seed++) * rscale;
  968. pt.y = height * par__randhash(seed++) * rscale;
  969. GRIDF(pt) = actives[nactives++] = nsamples;
  970. samples[nsamples++] = pt;
  971. while (nsamples < ncells) {
  972. int aindex = PAR_MIN(par__randhashf(seed++, 0, nactives),
  973. nactives - 1.0f);
  974. int sindex = actives[aindex];
  975. int found = 0;
  976. parsl_position j, minj, maxj, delta;
  977. int attempt;
  978. for (attempt = 0; attempt < maxattempts && !found; attempt++) {
  979. pt = par__sample_annulus(radius, samples[sindex], &seed);
  980. // Check that this sample is within bounds.
  981. if (pt.x < 0 || pt.x >= width || pt.y < 0 || pt.y >= height) {
  982. continue;
  983. }
  984. // Test proximity to nearby samples.
  985. maxj = parsl_mul(parsl__add(pt, rvec), invcell);
  986. minj = parsl_mul(parsl__sub(pt, rvec), invcell);
  987. minj.x = PAR_CLAMP((int) minj.x, 0, maxcol);
  988. minj.y = PAR_CLAMP((int) minj.y, 0, maxrow);
  989. maxj.x = PAR_CLAMP((int) maxj.x, 0, maxcol);
  990. maxj.y = PAR_CLAMP((int) maxj.y, 0, maxrow);
  991. int reject = 0;
  992. for (j.y = minj.y; j.y <= maxj.y && !reject; j.y++) {
  993. for (j.x = minj.x; j.x <= maxj.x && !reject; j.x++) {
  994. int entry = GRIDI(j);
  995. if (entry > -1 && entry != sindex) {
  996. delta = parsl__sub(samples[entry], pt);
  997. if (parsl__dot(delta, delta) < r2) {
  998. reject = 1;
  999. }
  1000. }
  1001. }
  1002. }
  1003. if (reject) {
  1004. continue;
  1005. }
  1006. found = 1;
  1007. }
  1008. if (found) {
  1009. GRIDF(pt) = actives[nactives++] = nsamples;
  1010. samples[nsamples++] = pt;
  1011. } else {
  1012. if (--nactives <= 0) {
  1013. break;
  1014. }
  1015. actives[aindex] = actives[nactives];
  1016. }
  1017. }
  1018. pa___n(result) = nsamples * 2;
  1019. PAR_FREE(grid);
  1020. PAR_FREE(actives);
  1021. return result;
  1022. }
  1023. #undef GRIDF
  1024. #undef GRIDI
  1025. parsl_mesh* parsl_mesh_from_streamlines(parsl_context* context,
  1026. parsl_advection_callback advect, uint32_t first_tick, uint32_t num_ticks,
  1027. void* userdata)
  1028. {
  1029. const int seed = 42;
  1030. const parsl_viewport vp = context->config.streamlines_seed_viewport;
  1031. const float radius = context->config.streamlines_seed_spacing;
  1032. float width = vp.right - vp.left;
  1033. float height = vp.bottom - vp.top;
  1034. if (context->streamline_seeds == NULL) {
  1035. context->streamline_seeds = par__generate_pts(width, height, radius,
  1036. seed, context->streamline_seeds);
  1037. uint32_t num_points = pa_count(context->streamline_seeds);
  1038. for (uint32_t p = 0; p < num_points; ++p) {
  1039. context->streamline_seeds[p].x += vp.left;
  1040. context->streamline_seeds[p].y += vp.top;
  1041. }
  1042. }
  1043. uint32_t num_points = pa_count(context->streamline_seeds);
  1044. pa_clear(context->streamline_points);
  1045. pa_add(context->streamline_points, num_points);
  1046. parsl_position* points = context->streamline_points;
  1047. memcpy(points, context->streamline_seeds,
  1048. num_points * sizeof(parsl_position));
  1049. context->streamline_spines.num_spines = num_points;
  1050. pa_clear(context->streamline_spines.spine_lengths);
  1051. pa_add(context->streamline_spines.spine_lengths, num_points);
  1052. uint16_t* lengths = context->streamline_spines.spine_lengths;
  1053. for (uint32_t i = 0; i < num_points; i++) {
  1054. lengths[i] = num_ticks;
  1055. }
  1056. context->streamline_spines.num_vertices = num_points * num_ticks;
  1057. pa_clear(context->streamline_spines.vertices);
  1058. pa_add(context->streamline_spines.vertices, num_points * num_ticks);
  1059. parsl_position* vertices = context->streamline_spines.vertices;
  1060. for (uint32_t tick = 0; tick < first_tick; tick++) {
  1061. for (uint32_t i = 0; i < num_points; i++) {
  1062. advect(&points[i], userdata);
  1063. }
  1064. }
  1065. parsl_position* pvertices = vertices;
  1066. for (uint32_t i = 0; i < num_points; i++) {
  1067. for (uint32_t tick = 0; tick < num_ticks; ++tick) {
  1068. advect(&points[i], userdata);
  1069. *pvertices++ = points[i];
  1070. }
  1071. }
  1072. parsl_mesh_from_lines(context, context->streamline_spines);
  1073. return &context->result;
  1074. }
  1075. #endif // PAR_STREAMLINES_IMPLEMENTATION
  1076. #endif // PAR_STREAMLINES_H
  1077. // par_streamlines is distributed under the MIT license:
  1078. //
  1079. // Copyright (c) 2019 Philip Rideout
  1080. //
  1081. // Permission is hereby granted, free of charge, to any person obtaining a copy
  1082. // of this software and associated documentation files (the "Software"), to deal
  1083. // in the Software without restriction, including without limitation the rights
  1084. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  1085. // copies of the Software, and to permit persons to whom the Software is
  1086. // furnished to do so, subject to the following conditions:
  1087. //
  1088. // The above copyright notice and this permission notice shall be included in
  1089. // all copies or substantial portions of the Software.
  1090. //
  1091. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1092. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1093. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1094. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1095. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1096. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1097. // SOFTWARE.