rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/facebits.sc -rw-r--r-- 2.1 KiB View raw Log this file
3827f53bae88 — Leonard Ritter * more work on module system 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using import glm
using import itertools

using import ..tukan.raytrace
using import ..tukan.random

let
    SIMPLEX_P0 =
        arrayof ivec3
            ivec3 0 2 2 # 6
            ivec3 0 0 0 # 0
            ivec3 2 2 0 # 3
            ivec3 2 0 2 # 5
            ivec3 0 2 2 # 6
    SIMPLEX_P1 =
        arrayof ivec3
            ivec3 2 0 2 # 5
            ivec3 2 2 0 # 3
            ivec3 0 0 0 # 0
            ivec3 0 2 2 # 6
            ivec3 2 0 2 # 5
    SIMPLEX_P2 =
        arrayof ivec3
            ivec3 2 2 0 # 3
            ivec3 2 0 2 # 5
            ivec3 0 2 2 # 6
            ivec3 0 0 0 # 0
            ivec3 2 2 0 # 3
    SIMPLEX_P3 =
        arrayof ivec3
            ivec3 0 0 0 # 0
            ivec3 2 0 0 # 1
            ivec3 0 2 0 # 2
            ivec3 0 0 2 # 4
            ivec3 2 2 2 # 7

run-stage;

fn bary2 (p)

    # convert a position to barycentric coordinates in all 5 simplices
    q1 := (vec4 (1 - p.x) p.y p.z (p.x - p.y - p.z))
    q2 := (vec4 p.x (1 - p.y) p.z (p.y - p.x - p.z))
    q3 := (vec4 p.x p.y (1 - p.z) (p.z - p.x - p.y))
    q4 := (vec4 q1.x q2.y q3.z (p.x + p.y + p.z - 2))
    q0 := ((vec4 q1.w q2.w q3.w q4.w) * -0.5)

    # identify simplex
    let i =
        if (q1.w >= 0.0) 1
        elseif (q2.w >= 0.0) 2
        elseif (q3.w >= 0.0) 3
        elseif (q4.w >= 0.0) 4
        else 0

    local q_array =
        arrayof vec4 q0 q1 q2 q3 q4
    let q = (deref (q_array @ i))

    local simplex_p0 = SIMPLEX_P0
    let p0 = (deref (simplex_p0 @ i))
    local simplex_p1 = SIMPLEX_P1
    let p1 = (deref (simplex_p1 @ i))
    local simplex_p2 = SIMPLEX_P2
    let p2 = (deref (simplex_p2 @ i))
    local simplex_p3 = SIMPLEX_P3
    let p3 = (deref (simplex_p3 @ i))

    print p0 p1 p2 p3

    +
        q.x * (vec3 p0) * 0.5
        q.y * (vec3 p1) * 0.5
        q.z * (vec3 p2) * 0.5
        q.w * (vec3 p3) * 0.5

print
    bary2 (vec3 1 0 0)

print;
local rnd : (Random)
for i in (range 1000)
    let p =
        'random rnd vec3
    let rp = (bary2 p)
    let diff = (length (rp - p))
    assert (diff < 1e-5)
    print p rp (rp - p) diff