Skip to content

thi-ng/c-thing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

c.thi.ng

Contents

Overview

WIP

Data structures

Memory management

Math

Simulation

Geometry

…

TBD

Examples

Build all examples

premake gmake
make config=release_sse

1D/2D Cellular automata

Source (1D) | Source (2D)

./assets/ca1d.png

# build library & example app
make config=release_sse ex-ca1d

# 1D sample invocation w/ Wolfram Rule 105
# Result will be written to /assets/ca-105.svg
# args: rule states kernelwidth res
bin/release/ex-ca1d 105 2 1

# Output all 255 wolfram rules
for i in {1..255}; do bin/release/ex-ca1d $i 2 1 128; done

# Another example w/ bigger kernel size
bin/release/ex-ca1d 522530942 32 3 128

# build 2D example app
make config=release_sse ex-ca2d

# frames will be written to /assets
bin/release/ex-ca2d

Diffusion-Limited Aggregation

Source

./assets/dla.png

# build library & example app
make config=release_sse ex-dla && bin/release/ex-dla > assets/dla.svg

Poisson disc sampling image conversion

Source

./assets/iris-poisson.png

# build library & example app
make config=release_sse ex-poisson

# show usage
bin/release/ex-poisson
# Missing input file
# Usage:  ex-poisson [options] image [ > out.svg ]
#   -b HEX    bg color (default: ffffff)
#   -f HEX    fg color (default: 0000ff)
#   -g FLOAT  gamma (default: 3.00)
#   -i        invert (also swaps fg/bg) (default: no)
#   -m FLOAT  min distance (default: 2.00)
#   -x FLOAT  max distance (default: 10.00)
#   -r FLOAT  dot radius (default: 1.00)
#   -q INT    quality (default: 100)
#   -t        output points as text only (default: no)

# concrete example (for image size roughly 700x1000px)
bin/release/ex-poisson -g 1.25 -q 500 -m 1.5 -x 16 assets/iris.jpg > assets/iris.svg

Polygon clipping (Greiner-Hormann)

Source

./assets/polyclip.png

# build library & example app
make config=release_sse ex-polyclip && bin/release/ex-polyclip > polyclip.svg

Polygon offsetting

Source

./assets/polyoffset-all-small.png

# build library & example app
make config=release_sse ex-polyclip && bin/release/ex-polyoffset > polyoffset.svg

Verlet physics

Source | Video example 1 | Video example 2

./assets/verlet.png

# build library & example app
make config=release_sse ex-verlet && bin/release/ex-verlet

# ouputs are stored as SVG sequence in /assets
# use script below to convert to mp4 (requires rsvg & ffmpeg) -> out.mp4
./makevideo verlet

Voronoi

Source

./assets/poisson-voronoi.png

# build library & example app
make config=release_sse ex-voronoi && bin/release/ex-voronoi

Piping in points from external process

./assets/iris-voronoi.png

The image above was generated by first sampling a JPG with the poisson example tool (configured to output points as text) and then piping these points into the voronoi example.

bin/release/ex-poisson -b 00ffff -f 0000ff -q 500 -m 2 -x 16 -t assets/iris.jpg | \
bin/release/ex-voronoi -p -w 757 -h 450 > iris-voronoi.svg

Requirements

Dependencies

The library itself has no 3rd party dependencies. Some of the examples however use these additional libs:

Build & test

Get further help for autogenerated Makefile:

# generate Makefiles
premake5 gmake

make help
# Usage: make [config=name] [target]
# 
# CONFIGURATIONS:
#   debug_sse
#   debug_no_sse
#   release_sse
#   release_no_sse
# 
# TARGETS:
#    all (default)
#    clean
#    test
#    lib
#    ex-poisson
#    ex-dla
# build & run tests manually
make config=debug_sse test && bin/debug/test
# or
make config=release_sse test && bin/release/test

# ...or use auto test w/ file watcher
# tests re-run automatically if files in /src or /test are changed
# if no args given, compiles w/ address sanitizer enabled
./autotest
# ...or provide build config (target config profile)
# (only test_msan requires linux & clang, other profiles also build w/ gcc etc.)
./autotest test_msan
./autotest test_asan debug
./autotest test release no_sse

Build static library

make config=debug_sse lib
# or
make config=release_sse lib

Build source x-ref

brew install cscope

./browse

Disassemble & list symbols

# display disassembly (OSX)
otool -jtV bin/release/libcthing.a | less

# display global symbols defined in lib
nm -g -j bin/release/libcthing.a | grep --color=never _ct_

Compile with emscripten

# help / usage
./compile -h
# Usage:
#   -a     : separate asm.js output
#   -c     : enable Closure compiler step
#   -d     : remove duplicate functions
#   -D SYM : add define
#   -h     : show this help
#   -k     : enable runtime checks
#   -m     : enable memory checks
#   -s     : enable SSE (SIMD.js)
#   -t     : include tests

# compile with tests, closure pass & remove duplicate fns
./compile -tdc

python3 -m http.server

# in browser dev console - http://localhost:8000/
cthing(); cthing._main()

Build customization

The following defines can be used to customize behavior:

CT_FEATURE_LOGenable logging (if NDEBUG is defined, the debug log level is disabled)
CT_FEATURE_ANSIuse ANSI colors in log messages
CT_FEATURE_CHECKSenable runtime assertions (via CT_CHECK macro)
CT_FEATURE_CHECK_MEMenable runtime memory checks (via CT_CHECK_MEM macro)
CT_FEATURE_SSEenable SSE version

Contributors

NameRoleWebsite
Karsten Schmidtinitiator & principal developerthi.ng

License

This project is open source and licensed under the Apache Software License 2.0.