# Computation of a levelset field from a contour

This test case illustrates how to import a contour, represented using a vector graphics file format, here an encapsulated Postscript file and represent it within Basilisk as a distance (i.e. levelset) field.

We will also use this distance function to compute a VOF representation of the contour, and test the tag() function.

``````#include "utils.h"
#include "distance.h"
#include "fractions.h"
#include "tag.h"
#include "view.h"

int main()
{``````

The .eps file was created using inkscape (or any other vector graphics editor). The .gnu file is obtained from the .eps file using

``pstoedit -f gnuplot -flat 0.1 basilisk.eps basilisk.gnu``

This gives the following curve The pairs of coordinates defining the (oriented) segments are then read using:

``  coord * p = input_xy (fopen ("basilisk.gnu", "r"));``

We can optionally add noise to the representation, to check the robustness of the reconstruction for self-intersecting, non-closed curves.

``````#if 0
double amp = .0;
coord * i = p;
while (i->x != nodata) {
i->x += amp*noise(), i->y += amp*noise();
printf ("%g %g\n", i->x, i->y);
i++;
i->x += amp*noise(), i->y += amp*noise();
printf ("%g %g\n\n", i->x, i->y);
i++;
}
fflush (stdout);
#endif

init_grid (8);
size (105);
origin (-5, -5);``````

We initialise the distance field d and refine the mesh according to the error on this field.

``````  scalar d[];
distance (d, p);
while (adapt_wavelet ({d}, (double[]){1e-2}, 12).nf);``````

We tag each letter (counting the dots on the i’s). Tagged regions.

``````  scalar tt[];
foreach()
tt[] = d[] < 0;
boundary ({tt});
int n = tag (tt);
output_ppm (tt, file = "tag.png", n = 512, box = {{-5,-5},{100,30}},
map = randomap, min = 0, max = n);
assert (n == 8);``````

We initialise a vertex distance field by interpolating the centered distance field, and use this to compute VOF fractions and facets.

``````  vertex scalar φ[];
scalar f[];
foreach_vertex()
φ[] = (d[] + d[-1] + d[0,-1] + d[-1,-1])/4.;
boundary ({φ});
face vector s[];
fractions (φ, f, s);
output_facets (f, stderr, s);``````

Here we compute the number of segments intersected by the neighborhood (a sphere of diameter $3\Delta$) of each cell.

``````  scalar nt[], surface = d.surface;
foreach() {
nt[] = 0;
if (surface[]) {
coord ** p = (coord **) double_to_pointer (surface[]);
while (*p)
nt[]++, p++;
}
}
boundary ({nt});``````

We display the resulting curves and meshes.

``````  view (fov = 6.98459, tx = -0.444569, ty = -0.137936,
width = 640, height = 242);
squares ("level", min = 2, max = 12);
for (double val = -20; val <= 20; val += 2.)
isoline ("phi", val, lw = 0.5);
save ("mesh.png");`````` Adaptive mesh and isolines of distance function.

Note that the input curve is self-intersecting. The algorithm used to compute the distance function is robust but gives the artefact illustrated below.

``````  view (fov = 1.64446, tx = -0.309322, ty = -0.0840683,
width = 449, height = 303);
draw_vof ("f", "s");
save ("self.png");`````` The input curve represented through its VOF discretisation.

``}``
{"ft_lang_label":"__label__en","ft_lang_prob":0.68392897,"math_prob":0.9905052,"size":2834,"snap":"2019-13-2019-22","text_gpt3_token_len":827,"char_repetition_ratio":0.08975265,"word_repetition_ratio":0.017278617,"special_character_ratio":0.35815102,"punctuation_ratio":0.22093023,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9991451,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,4,null,4,null,4,null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-03-25T11:33:11Z\",\"WARC-Record-ID\":\"<urn:uuid:af38cca8-7698-4925-8cd0-5b2b28056e5d>\",\"Content-Length\":\"17114\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7eca05ba-31f2-4a0a-a77d-76ef3633649b>\",\"WARC-Concurrent-To\":\"<urn:uuid:a470171b-1336-420c-9e65-2634a6f3239c>\",\"WARC-IP-Address\":\"192.162.68.18\",\"WARC-Target-URI\":\"http://basilisk.fr/src/test/basilisk.c\",\"WARC-Payload-Digest\":\"sha1:QRQCMDKE2P4QRNCC3DNWDVGXEAMJ5OXP\",\"WARC-Block-Digest\":\"sha1:M2A4NCWOA4LBLWTJ6ULPRJQHWOK4ESLC\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-13/CC-MAIN-2019-13_segments_1552912203947.59_warc_CC-MAIN-20190325112917-20190325134917-00177.warc.gz\"}"}