File: plane.h

package info (click to toggle)
goxel 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,232 kB
  • sloc: ansic: 87,714; cpp: 87,537; python: 128; makefile: 74; xml: 55
file content (102 lines) | stat: -rw-r--r-- 3,135 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Goxel 3D voxels editor
 *
 * copyright (c) 2015 Guillaume Chereau <guillaume@noctua-software.com>
 *
 * Goxel is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.

 * Goxel is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.

 * You should have received a copy of the GNU General Public License along with
 * goxel.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "goxel.h"

/*  A plane is defined as the 4x4 matrix to transform from the plane local
 *  coordinate into global coordinates:
 *
 *
 *  n
 *  ^    v
 *  |   ^ . . . . .
 *  |  /         .
 *  | /         .
 *  |/         .
 *  +---------> u
 *  P
 *
 *  Here the plane defined by the point P and vectors u and v with normal n,
 *  will have a matrix like this:
 *
 *     [ux  uy  yz  0]
 *     [vx  vy  vz  0]
 *     [nx  ny  nz  0]
 *     [px  py  pz  1]
 *
 *  This representation has several advantages: we can access the plane unitary
 *  vectors, normal, and origin without any computation.  I used an union so
 *  that those values can be access directly as u, v, n, and p.  For the
 *  vec4 version we can also use u4, v4, n4, and p4.
 *
 *  It is also trivial to transform a point in the plane into world
 *  coordinates, simply by using matrix computation.
 *
 */

static const float plane_null[4][4] = {};

static inline void plane_from_vectors(float plane[4][4],
        const float pos[3], const float u[3], const float v[3])
{
    mat4_set_identity(plane);
    vec3_copy(u, plane[0]);
    vec3_copy(v, plane[1]);
    vec3_cross(u, v, plane[2]);
    vec3_copy(pos, plane[3]);
}

static inline bool plane_is_null(const float p[4][4]) {
    return p[3][3] == 0;
}

// Check if a plane intersect a line.
// if out is set, it receive the position of the intersection in the
// plane local coordinates.  Apply the plane matrix on it to get the
// object coordinate position.
static inline bool plane_line_intersection(const float plane[4][4],
        const float p[3], const float n[3], float out[3])
{
    float v[3], m[4][4];
    mat4_set_identity(m);
    vec3_copy(plane[0], m[0]);
    vec3_copy(plane[1], m[1]);
    vec3_copy(n, m[2]);
    if (!mat4_invert(m, m)) return false;
    if (out) {
        vec3_sub(p, plane[3], v);
        mat4_mul_vec3(m, v, out);
        out[2] = 0;
    }
    return true;
}

static inline void plane_from_normal(float plane[4][4],
                                     const float pos[3], const float n[3])
{
    int i;
    const float AXES[][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
    mat4_set_identity(plane);
    vec3_copy(pos, plane[3]);
    vec3_normalize(n, plane[2]);
    for (i = 0; i < 3; i++) {
        vec3_cross(plane[2], AXES[i], plane[0]);
        if (vec3_norm2(plane[0]) > 0) break;
    }
    vec3_cross(plane[2], plane[0], plane[1]);
}