]> git.xonotic.org Git - xonotic/darkplaces.git/blob - convex.h
Merge PR 'Use the text from modinfo.txt as the mod menu entry'
[xonotic/darkplaces.git] / convex.h
1 /*
2 Copyright (c) 2022 Ashley Rose Hale (LadyHavoc)
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 // This module is a variant of the QuickHull algorithm intended to create hulls
24 // (brushes, aka n-sided polytopes or hulls) from a series of points provided by
25 // the caller
26
27 #pragma once
28
29 #ifndef CONVEX_H
30
31 enum convex_enums
32 {
33         CONVEX_MAX_CORNERS = 256,
34         CONVEX_MAX_FACES = 1024,
35 };
36
37 typedef struct convex_corner_s
38 {
39         float x;
40         float y;
41         float z;
42         float w; // 1.0f
43 }
44 convex_corner_t;
45
46 typedef struct convex_face_s
47 {
48         // plane equation: a * x + b * y + c * z + d * w = 0.0f
49         float x;
50         float y;
51         float z;
52         float w;
53 }
54 convex_face_t;
55
56 typedef struct convex_builder_state_s
57 {
58         // axially aligned bounding box
59         float extents[2][3];
60
61         int numcorners;
62         convex_corner_t corners[CONVEX_MAX_CORNERS];
63
64         int numfaces;
65         convex_face_t faces[CONVEX_MAX_FACES];
66
67         // we consider points to be equivalent if they are within this distance
68         // suggested value is maxextent / 1048576.0f, which is a way of saying 
69         // 'equivalent within 20 bits of precision'
70         float epsilon;
71 }
72 convex_builder_state_t;
73
74 // set up a builer state to receive points
75 void convex_builder_initialize(convex_builder_state_t* b, float epsilon);
76
77 // this is a variant of QuickHull that relies on the caller to provide points
78 // in a reasonable order - the result will be the same regardless of point order
79 // but it's more efficient if the furthest points are provided first
80 //
81 // this could be a little more efficient if we kept track of edges during the
82 // build, but I think it may be more numerically stable this way
83 void convex_builder_add_point(convex_builder_state_t* b, float x, float y, float z);
84
85 // returns computed faces in array of vec4
86 // positivew=0 is for plane equations of the form a*x+b*y+c*z+w, which is the
87 // internal format
88 // positivew=1 is for plane equations of the form a*x+b*y+c*z-w, which tend to
89 // be less friendly in terms of vector ops
90 int convex_builder_get_planes4f(convex_builder_state_t* b, float* outplanes4f, int maxplanes, int positivew);
91
92 // returns the points as an array of vec3
93 // internal format is vec4, so this is just repacking the data
94 int convex_builder_get_points3f(convex_builder_state_t* b, float* outpoints3f, int maxpoints);
95
96 #endif