2 Copyright (c) 2022 Ashley Rose Hale (LadyHavoc)
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:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
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
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
33 CONVEX_MAX_CORNERS = 256,
34 CONVEX_MAX_FACES = 1024,
37 typedef struct convex_corner_s
46 typedef struct convex_face_s
48 // plane equation: a * x + b * y + c * z + d * w = 0.0f
56 typedef struct convex_builder_state_s
58 // axially aligned bounding box
62 convex_corner_t corners[CONVEX_MAX_CORNERS];
65 convex_face_t faces[CONVEX_MAX_FACES];
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'
72 convex_builder_state_t;
74 // set up a builer state to receive points
75 void convex_builder_initialize(convex_builder_state_t* b, float epsilon);
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
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);
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
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);
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);