Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (775)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Split triangle with plane  (Read 2287 times)
0 Members and 1 Guest are viewing this topic.
Offline KaiHH

JGO Kernel


Medals: 636



« Posted 2018-09-10 20:16:20 »

Here is a class I use to split triangles with a 3D plane. This can be used as a building block for creating spatial acceleration structures, such as Bounding Volume Hierarchies, from a list of triangles, where triangles can have large extents and should be split at the plane to reduce overlap when subdividing a tree node.
The class itself uses an abstract interface to define necessary operations of a triangle in terms of abstract methods, allowing the user application to use any custom triangle data structure it wants. It also uses functional interfaces / callbacks to let the user application decide how to store the generated split triangles, for all cases (triangle lies behind, on or infront of the split plane). I've tried to make it as efficient as possible, but improvements are welcome!

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  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312  
313  
314  
315  
316  
317  
318  
319  
320  
321  
322  
323  
324  
325  
326  
327  
328  
329  
330  
331  
332  
333  
334  
335  
336  
337  
338  
339  
340  
341  
342  
343  
344  
345  
346  
347  
348  
349  
350  
351  
352  
353  
354  
355  
356  
357  
358  
359  
360  
361  
362  
363  
364  
365  
366  
367  
368  
369  
370  
371  
372  
373  
374  
375  
376  
377  
378  
379  
380  
381  
382  
383  
384  
385  
386  
387  
388  
389  
390  
391  
392  
393  
394  
395  
396  
397  
398  
399  
400  
401  
402  
403  
404  
405  
406  
407  
408  
409  
410  
411  
412  
413  
414  
415  
416  
417  
418  
419  
420  
421  
422  
423  
424  
425  
426  
427  
428  
429  
430  
431  
432  
433  
434  
435  
436  
437  
438  
439  
440  
441  
442  
443  
444  
445  
446  
447  
448  
449  
450  
451  
452  
453  
454  
455  
456  
457  
458  
459  
460  
461  
462  
463  
464  
465  
466  
467  
468  
469  
470  
471  
472  
473  
474  
475  
476  
477  
478  
479  
480  
481  
482  
483  
484  
485  
486  
487  
488  
489  
490  
491  
492  
493  
494  
495  
496  
497  
498  
499  
500  
501  
502  
503  
504  
505  
506  
507  
508  
509  
510  
511  
512  
513  
514  
515  
516  
517  
518  
519  
520  
521  
522  
523  
524  
525  
526  
527  
528  
529  
530  
531  
532  
533  
534  
535  
536  
537  
538  
539  
540  
541  
542  
543  
544  
545  
546  
547  
548  
549  
550  
551  
552  
553  
554  
555  
556  
557  
558  
559  
560  
561  
562  
563  
564  
565  
566  
567  
568  
569  
570  
571  
572  
573  
574  
575  
import java.util.*;

/**
 * Methods to split triangles with a plane.
 *
 * @author Kai Burjack
 */

public class TriangleSplit {
    /**
     * Describes a vertex of the triangle with its position.
     */

    public static class Vertex {
        public float x, y, z;

        Vertex() {
        }

        Vertex(Vertex v) {
            this.x = v.x;
            this.y = v.y;
            this.z = v.z;
        }

        Vertex lerp(float x, float y, float z, float t) {
            this.x = this.x + (x - this.x) * t;
            this.y = this.y + (y - this.y) * t;
            this.z = this.z + (z - this.z) * t;
            return this;
        }

        Vertex set(Vertex v) {
            this.x = v.x;
            this.y = v.y;
            this.z = v.z;
            return this;
        }
    }

    /**
     * General contract of a triangle to be processed by methods in the
     * {@link TriangleSplit} class.
     * <p>
     * User applications can define own triangle data structures but must
     * provide methods to obtain the three vertices of the triangle and to
     * generate a new triangle from a given triangle. The latter is to ensure
     * that any auxiliary information such as material associated with the
     * original triangle is retained in a splitted triangles.
     */

    public static interface ITriangle {
        /**
         * Get the first vertex of `this` triangle and store it into the given
         * vector `v`.
         */

        void v0(Vertex v);

        /**
         * Get the second vertex of `this` triangle and store it into the given
         * vector `v`.
         */

        void v1(Vertex v);

        /**
         * Get the third vertex of `this` triangle and store it into the given
         * vector `v`.
         */

        void v2(Vertex v);

        /**
         * Generate a new triangle from `this` triangle using the given
         * vertices.
         * <p>
         * This method exists to ensure that any auxiliary information such as
         * material associated with `this` triangle is retained in a splitted
         * triangles.
         *
         * @param v0 the first vertex. The data of the provided {@link Vertex}
         *           instance MUST be copied during this method and the method
         *           MUST NOT store a reference to this vector
         * @param v1 the second vertex. The data of the provided {@link Vertex}
         *           instance MUST be copied during this method and the method
         *           MUST NOT store a reference to this vector
         * @param v2 the third vertex. The data of the provided {@link Vertex}
         *           instance MUST be copied during this method and the method
         *           MUST NOT store a reference to this vector
         * @return the new triangle
         */

        ITriangle split(Vertex v0, Vertex v1, Vertex v2);
    }

    /**
     * Functional interface for triangle callbacks. See the methods in the
     * {@link TriangleSplit} class.
     *
     * @param <T> the type of the triangle class used
     */

    @FunctionalInterface
    public static interface ITriangleCallback<T extends ITriangle> {
        /**
         * Will be called when processing the given triangle, providing the
         * triangle instance as well as the index of that triangle.
         *
         * @param t     the triangle
         * @param index the triangle's index
         */

        void onTriangle(T t, int index);
    }

    /**
     * Will hold the number of triangles behind and infront of the split plane.
     * See the methods in the {@link TriangleSplit} class.
     */

    public static class SplitInfo {
        public int behind;
        public int infront;

        @Override
        public String toString() {
            return "SplitInfo [behind=" + behind + ", infront=" + infront + "]";
        }
    }

    private static byte encodeSplitCase(byte a, byte b, byte c) {
        return (byte) (a + 1 << 4 | b + 1 << 2 | c + 1);
    }

    private static float fraction(float a, float b) {
        return java.lang.Math.abs(a)
                / (java.lang.Math.abs(a) + java.lang.Math.abs(b));
    }

    private static float dist(float a, float b, float c, float d,
            float invDenom, float x, float y, float z) {
        return (a * x + b * y + c * z + d) * invDenom;
    }

    private static byte signd(float a, float b, float c, float d, float x,
            float y, float z) {
        return (byte) java.lang.Math.signum(a * x + b * y + c * z + d);
    }

    /**
     * Determine the number of split triangles behind and infront of the split
     * plane and write those numbers to the provided {@link SplitInfo}
     * parameter.
     *
     * @param a         the `a` factor in the plane equation `a*x + b*y + c*z +
     *                  d = 0`
     * @param b         the `b` factor in the plane equation `a*x + b*y + c*z +
     *                  d = 0`
     * @param c         the `c` factor in the plane equation `a*x + b*y + c*z +
     *                  d = 0`
     * @param d         the `d` factor in the plane equation `a*x + b*y + c*z +
     *                  d = 0`
     * @param triangles the list of triangles to test for splits
     * @param out       will hold the number of split triangles behind and
     *                  infront of the split plane
     */

    public static <T extends ITriangle, I extends Iterable<T>> void testSplit(
            float a, float b, float c, float d, I triangles, SplitInfo out) {
        int behind = 0, infront = 0;
        Vertex v0 = new Vertex(), v1 = new Vertex(), v2 = new Vertex();
        for (T t : triangles) {
            t.v0(v0);
            t.v1(v1);
            t.v2(v2);
            byte sa = signd(a, b, c, d, v0.x, v0.y, v0.z);
            byte sb = signd(a, b, c, d, v1.x, v1.y, v1.z);
            byte sc = signd(a, b, c, d, v2.x, v2.y, v2.z);
            switch (encodeSplitCase(sa, sb, sc)) {
            case 0 /* (-1,-1,-1) */:
            case 1 /* (-1,-1,0) */:
            case 4 /* (-1,0,-1) */:
            case 5 /* (-1,0,0) */:
            case 16 /* (0,-1,-1) */:
            case 17 /* (0,-1,0) */:
            case 20 /* (0,0,-1) */:
                behind++;
                break;
            case 22 /* (0,0,1) */:
            case 25 /* (0,1,0) */:
            case 26 /* (0,1,1) */:
            case 37 /* (1,0,0) */:
            case 38 /* (1,0,1) */:
            case 41 /* (1,1,0) */:
            case 42 /* (1,1,1) */:
                infront++;
                break;
            case 21 /* (0,0,0) */:
                break;
            case 2 /* (-1,-1,1) */:
            case 8 /* (-1,1,-1) */:
            case 32 /* (1,-1,-1) */:
                infront++;
                behind += 2;
                break;
            case 6 /* (-1,0,1) */:
            case 9 /* (-1,1,0) */:
            case 18 /* (0,-1,1) */:
            case 24 /* (0,1,-1) */:
            case 33 /* (1,-1,0) */:
            case 36 /* (1,0,-1) */:
                behind++;
                infront++;
                break;
            case 10 /* (-1,1,1) */:
            case 34 /* (1,-1,1) */:
            case 40 /* (1,1,-1) */:
                behind++;
                infront += 2;
                break;
            }
        }
        out.behind = behind;
        out.infront = infront;
    }

    /**
     * Split the given triangle using the plane specified as the coefficients in
     * the general plane equation `a*x + b*y + c*z + d = 0` and call the
     * respective {@link ITriangleCallback} when that triangle (or a potentially
     * generated splitted triangle) lies behind, on or infront of the split
     * plane, allowing the caller to process the triangle in each case.
     *
     * @param a            the `a` factor in the plane equation `a*x + b*y + c*z
     *                     + d = 0`
     * @param b            the `b` factor in the plane equation `a*x + b*y + c*z
     *                     + d = 0`
     * @param c            the `c` factor in the plane equation `a*x + b*y + c*z
     *                     + d = 0`
     * @param d            the `d` factor in the plane equation `a*x + b*y + c*z
     *                     + d = 0`
     * @param t            the triangle to split
     * @param index        any caller-defined index of the triangle reported to
     *                     any {@link ITriangleCallback}
     * @param behind       callback to invoke for each triangle (original or
     *                     splitted) that lies behind the split plane
     * @param on           callback to invoke when the triangle lies on the
     *                     split plane
     * @param infront      callback to invoke for each triangle (original or
     *                     splitted) that lies infront of the split plane
     * @param newTriangles callback to invoke for each newly generated splitted
     *                     triangle. This callback will always be invoked before
     *                     calling any of the behind, on or infront callbacks
     *                     for a generated splitted triangle
     */

    @SuppressWarnings("unchecked")
    public static <T extends ITriangle> void split(float a, float b, float c,
            float d, T t, int index, ITriangleCallback<T> behind,
            ITriangleCallback<T> on, ITriangleCallback<T> infront,
            ITriangleCallback<T> newTriangles) {
        float invDenom = (float) (1.0
                / java.lang.Math.sqrt(a * a + b * b + c * c));
        Vertex v0 = new Vertex(), v1 = new Vertex(), v2 = new Vertex();
        t.v0(v0);
        t.v1(v1);
        t.v2(v2);
        float dA = dist(a, b, c, d, invDenom, v0.x, v0.y, v0.z);
        float dB = dist(a, b, c, d, invDenom, v1.x, v1.y, v1.z);
        float dC = dist(a, b, c, d, invDenom, v2.x, v2.y, v2.z);
        byte sa = (byte) java.lang.Math.signum(dA);
        byte sb = (byte) java.lang.Math.signum(dB);
        byte sc = (byte) java.lang.Math.signum(dC);
        Vertex a0 = null, a1 = null, a2 = null;
        T t0, t1, t2;
        if (sa * sb == -1)
            a0 = new Vertex(v0).lerp(v1.x, v1.y, v1.z, fraction(dA, dB));
        if (sb * sc == -1)
            a1 = new Vertex(v1).lerp(v2.x, v2.y, v2.z, fraction(dB, dC));
        if (sc * sa == -1)
            a2 = new Vertex(v2).lerp(v0.x, v0.y, v0.z, fraction(dC, dA));
        switch (encodeSplitCase(sa, sb, sc)) {
        case 0 /* (-1,-1,-1) */:
        case 1 /* (-1,-1,0) */:
        case 4 /* (-1,0,-1) */:
        case 5 /* (-1,0,0) */:
        case 16 /* (0,-1,-1) */:
        case 17 /* (0,-1,0) */:
        case 20 /* (0,0,-1) */:
            behind.onTriangle(t, index);
            break;
        case 22 /* (0,0,1) */:
        case 25 /* (0,1,0) */:
        case 26 /* (0,1,1) */:
        case 37 /* (1,0,0) */:
        case 38 /* (1,0,1) */:
        case 41 /* (1,1,0) */:
        case 42 /* (1,1,1) */:
            infront.onTriangle(t, index);
            break;
        case 2 /* (-1,-1,1) */:
            newTriangles.onTriangle(t0 = (T) t.split(v2, a2, a1), index + 1);
            infront.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v0, a1, a2), index + 2);
            behind.onTriangle(t1, index + 2);
            newTriangles.onTriangle(t2 = (T) t.split(v0, v1, a1), index + 3);
            behind.onTriangle(t2, index + 3);
            break;
        case 6 /* (-1,0,1) */:
            newTriangles.onTriangle(t0 = (T) t.split(v0, v1, a2), index + 1);
            behind.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v1, v2, a2), index + 2);
            infront.onTriangle(t1, index + 2);
            break;
        case 8 /* (-1,1,-1) */:
            newTriangles.onTriangle(t0 = (T) t.split(v1, a1, a0), index + 1);
            infront.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v2, a0, a1), index + 2);
            behind.onTriangle(t1, index + 2);
            newTriangles.onTriangle(t2 = (T) t.split(v2, v0, a0), index + 3);
            behind.onTriangle(t2, index + 3);
            break;
        case 9 /* (-1,1,0) */:
            newTriangles.onTriangle(t0 = (T) t.split(v2, v0, a0), index + 1);
            behind.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v1, v2, a0), index + 2);
            infront.onTriangle(t1, index + 2);
            break;
        case 10 /* (-1,1,1) */:
            newTriangles.onTriangle(t0 = (T) t.split(v0, a0, a2), index + 1);
            behind.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v1, a2, a0), index + 2);
            infront.onTriangle(t1, index + 2);
            newTriangles.onTriangle(t2 = (T) t.split(v1, v2, a2), index + 3);
            infront.onTriangle(t2, index + 3);
            break;
        case 18 /* (0,-1,1) */:
            newTriangles.onTriangle(t0 = (T) t.split(v0, v1, a1), index + 1);
            behind.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v2, v0, a1), index + 2);
            infront.onTriangle(t1, index + 2);
            break;
        case 21 /* (0,0,0) */:
            on.onTriangle(t, index);
            break;
        case 24 /* (0,1,-1) */:
            newTriangles.onTriangle(t0 = (T) t.split(v2, v0, a1), index + 1);
            behind.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v0, v1, a1), index + 1);
            infront.onTriangle(t1, index + 2);
            break;
        case 32 /* (1,-1,-1) */:
            newTriangles.onTriangle(t0 = (T) t.split(v0, a0, a2), index + 1);
            infront.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v1, a2, a0), index + 2);
            behind.onTriangle(t1, index + 2);
            newTriangles.onTriangle(t2 = (T) t.split(v1, v2, a2), index + 3);
            behind.onTriangle(t2, index + 3);
            break;
        case 33 /* (1,-1,0) */:
            newTriangles.onTriangle(t0 = (T) t.split(v1, v2, a0), index + 1);
            behind.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v2, v0, a0), index + 2);
            infront.onTriangle(t1, index + 3);
            break;
        case 34 /* (1,-1,1) */:
            newTriangles.onTriangle(t0 = (T) t.split(v1, a1, a0), index + 1);
            behind.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v0, a0, a1), index + 2);
            infront.onTriangle(t1, index + 2);
            newTriangles.onTriangle(t2 = (T) t.split(v2, v0, a1), index + 3);
            infront.onTriangle(t2, index + 3);
            break;
        case 36 /* (1,0,-1) */:
            newTriangles.onTriangle(t0 = (T) t.split(v1, v2, a2), index + 1);
            behind.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v0, v1, a2), index + 2);
            infront.onTriangle(t1, index + 2);
            break;
        case 40 /* (1,1,-1) */:
            newTriangles.onTriangle(t0 = (T) t.split(v2, a2, a1), index + 1);
            behind.onTriangle(t0, index + 1);
            newTriangles.onTriangle(t1 = (T) t.split(v0, a1, a2), index + 2);
            infront.onTriangle(t1, index + 2);
            newTriangles.onTriangle(t2 = (T) t.split(v0, v1, a1), index + 3);
            infront.onTriangle(t2, index + 3);
            break;
        }
    }

    /**
     * Split the given list of triangles using the plane specified as the
     * coefficients in the general plane equation `a*x + b*y + c*z + d = 0` and
     * call the respective {@link ITriangleCallback} when a triangle (or any
     * generated splitted triangle) lies behind, on or infront of the split
     * plane, allowing the caller to process the triangle in each case.
     *
     * @param a            the `a` factor in the plane equation `a*x + b*y + c*z
     *                     + d = 0`
     * @param b            the `b` factor in the plane equation `a*x + b*y + c*z
     *                     + d = 0`
     * @param c            the `c` factor in the plane equation `a*x + b*y + c*z
     *                     + d = 0`
     * @param d            the `d` factor in the plane equation `a*x + b*y + c*z
     *                     + d = 0`
     * @param triangles    the list of triangles to split
     * @param behind       callback to invoke for each triangle (original or
     *                     splitted) that lies behind the split plane
     * @param on           callback to invoke for each original triangle that
     *                     lies on the split plane
     * @param infront      callback to invoke for each triangle (original or
     *                     splitted) that lies infront of the split plane
     * @param newTriangles callback to invoke for each newly generated splitted
     *                     triangle. This callback will always be invoked before
     *                     calling any of the behind, on or infront callbacks
     *                     for a generated splitted triangle
     */

    @SuppressWarnings("unchecked")
    public static <T extends ITriangle, I extends Collection<T>> void split(
            float a, float b, float c, float d, I triangles,
            ITriangleCallback<T> behind, ITriangleCallback<T> on,
            ITriangleCallback<T> infront, ITriangleCallback<T> newTriangles) {
        float invDenom = (float) (1.0
                / java.lang.Math.sqrt(a * a + b * b + c * c));
        int lastIndex = triangles.size() - 1;
        Vertex v0 = new Vertex(), v1 = new Vertex(), v2 = new Vertex();
        Vertex a0 = new Vertex(), a1 = new Vertex(), a2 = new Vertex();
        int i = 0;
        for (T t : triangles) {
            t.v0(v0);
            t.v1(v1);
            t.v2(v2);
            float dA = dist(a, b, c, d, invDenom, v0.x, v0.y, v0.z);
            float dB = dist(a, b, c, d, invDenom, v1.x, v1.y, v1.z);
            float dC = dist(a, b, c, d, invDenom, v2.x, v2.y, v2.z);
            byte sa = (byte) java.lang.Math.signum(dA);
            byte sb = (byte) java.lang.Math.signum(dB);
            byte sc = (byte) java.lang.Math.signum(dC);
            T t0, t1, t2;
            if (sa * sb == -1)
                a0.set(v0).lerp(v1.x, v1.y, v1.z, fraction(dA, dB));
            if (sb * sc == -1)
                a1.set(v1).lerp(v2.x, v2.y, v2.z, fraction(dB, dC));
            if (sc * sa == -1)
                a2.set(v2).lerp(v0.x, v0.y, v0.z, fraction(dC, dA));
            switch (encodeSplitCase(sa, sb, sc)) {
            case 0 /* (-1,-1,-1) */:
            case 1 /* (-1,-1,0) */:
            case 4 /* (-1,0,-1) */:
            case 5 /* (-1,0,0) */:
            case 16 /* (0,-1,-1) */:
            case 17 /* (0,-1,0) */:
            case 20 /* (0,0,-1) */:
                behind.onTriangle(t, i);
                break;
            case 22 /* (0,0,1) */:
            case 25 /* (0,1,0) */:
            case 26 /* (0,1,1) */:
            case 37 /* (1,0,0) */:
            case 38 /* (1,0,1) */:
            case 41 /* (1,1,0) */:
            case 42 /* (1,1,1) */:
                infront.onTriangle(t, i);
                break;
            case 2 /* (-1,-1,1) */:
                newTriangles.onTriangle(t0 = (T) t.split(v2, a2, a1),
                        ++lastIndex);
                infront.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v0, a1, a2),
                        ++lastIndex);
                behind.onTriangle(t1, lastIndex);
                newTriangles.onTriangle(t2 = (T) t.split(v0, v1, a1),
                        ++lastIndex);
                behind.onTriangle(t2, lastIndex);
                break;
            case 6 /* (-1,0,1) */:
                newTriangles.onTriangle(t0 = (T) t.split(v0, v1, a2),
                        ++lastIndex);
                behind.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v1, v2, a2),
                        ++lastIndex);
                infront.onTriangle(t1, lastIndex);
                break;
            case 8 /* (-1,1,-1) */:
                newTriangles.onTriangle(t0 = (T) t.split(v1, a1, a0),
                        ++lastIndex);
                infront.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v2, a0, a1),
                        ++lastIndex);
                behind.onTriangle(t1, lastIndex);
                newTriangles.onTriangle(t2 = (T) t.split(v2, v0, a0),
                        ++lastIndex);
                behind.onTriangle(t2, lastIndex);
                break;
            case 9 /* (-1,1,0) */:
                newTriangles.onTriangle(t0 = (T) t.split(v2, v0, a0),
                        ++lastIndex);
                behind.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v1, v2, a0),
                        ++lastIndex);
                infront.onTriangle(t1, lastIndex);
                break;
            case 10 /* (-1,1,1) */:
                newTriangles.onTriangle(t0 = (T) t.split(v0, a0, a2),
                        ++lastIndex);
                behind.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v1, a2, a0),
                        ++lastIndex);
                infront.onTriangle(t1, lastIndex);
                newTriangles.onTriangle(t2 = (T) t.split(v1, v2, a2),
                        ++lastIndex);
                infront.onTriangle(t2, lastIndex);
                break;
            case 18 /* (0,-1,1) */:
                newTriangles.onTriangle(t0 = (T) t.split(v0, v1, a1),
                        ++lastIndex);
                behind.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v2, v0, a1),
                        ++lastIndex);
                infront.onTriangle(t1, lastIndex);
                break;
            case 21 /* (0,0,0) */:
                on.onTriangle(t, i);
                break;
            case 24 /* (0,1,-1) */:
                newTriangles.onTriangle(t0 = (T) t.split(v2, v0, a1),
                        ++lastIndex);
                behind.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v0, v1, a1),
                        ++lastIndex);
                infront.onTriangle(t1, lastIndex);
                break;
            case 32 /* (1,-1,-1) */:
                newTriangles.onTriangle(t0 = (T) t.split(v0, a0, a2),
                        ++lastIndex);
                infront.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v1, a2, a0),
                        ++lastIndex);
                behind.onTriangle(t1, lastIndex);
                newTriangles.onTriangle(t2 = (T) t.split(v1, v2, a2),
                        ++lastIndex);
                behind.onTriangle(t2, lastIndex);
                break;
            case 33 /* (1,-1,0) */:
                newTriangles.onTriangle(t0 = (T) t.split(v1, v2, a0),
                        ++lastIndex);
                behind.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v2, v0, a0),
                        ++lastIndex);
                infront.onTriangle(t1, lastIndex);
                break;
            case 34 /* (1,-1,1) */:
                newTriangles.onTriangle(t0 = (T) t.split(v1, a1, a0),
                        ++lastIndex);
                behind.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v0, a0, a1),
                        ++lastIndex);
                infront.onTriangle(t1, lastIndex);
                newTriangles.onTriangle(t2 = (T) t.split(v2, v0, a1),
                        ++lastIndex);
                infront.onTriangle(t2, lastIndex);
                break;
            case 36 /* (1,0,-1) */:
                newTriangles.onTriangle(t0 = (T) t.split(v1, v2, a2),
                        ++lastIndex);
                behind.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v0, v1, a2),
                        ++lastIndex);
                infront.onTriangle(t1, lastIndex);
                break;
            case 40 /* (1,1,-1) */:
                newTriangles.onTriangle(t0 = (T) t.split(v2, a2, a1),
                        ++lastIndex);
                behind.onTriangle(t0, lastIndex);
                newTriangles.onTriangle(t1 = (T) t.split(v0, a1, a2),
                        ++lastIndex);
                infront.onTriangle(t1, lastIndex);
                newTriangles.onTriangle(t2 = (T) t.split(v0, v1, a1),
                        ++lastIndex);
                infront.onTriangle(t2, lastIndex);
                break;
            }
            i++;
        }
    }
}
Offline abcdef
« Reply #1 - Posted 2018-09-12 08:15:01 »

Kai

Do you have a use case example for this that you can talk through? I'm not sure I understand the terminology you are using.

Thanks
Offline KaiHH

JGO Kernel


Medals: 636



« Reply #2 - Posted 2018-09-12 08:19:07 »

It is useful for building acceleration structures for ray tracing for example when you want to avoid putting the same (large) triangle over and over again into subdivided tree nodes, and to reduce overlap in bounding volume hierarchy structures.
In that case it is often better to subdivide a (large) triangle into multiple pieces at the boudaries of a plane when using any binary space partitioning tree structure (such as kd-tree or general BSP-trees) or at the boundaries of any other clipping geometry, such as an octree node or a bounding volume/AABB when for example a SAH (Surface Area Heuristics) value would be optimal in that case and would reduce overlap with other BVH nodes.
You give the split() method a collection of triangles and it will feed back to you the resulting splitted triangles which you can then insert into any other data structure (such as another collection) if you like.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Archive
« Reply #3 - Posted 2018-09-13 14:59:56 »

I haven't taken much time to understand what your code is doing exactly, but when I did polygon splitting I didn't have so many switch cases...

In fact the only switch statement I had contained 4 cases:
IN_FRONT
BEHIND
COPLANAR
default

What algorithm are you using for yours? Is it faster?

Offline KaiHH

JGO Kernel


Medals: 636



« Reply #4 - Posted 2018-09-13 15:38:57 »

You need that many cases. The algorithm is for clipping (not for culling).
You need to differentiate between which parts of the triangle is on which side of the plane in order to create additional triangles at the intersection points of the original triangle and the clipping plane.
Offline Archive
« Reply #5 - Posted 2018-09-15 04:01:36 »

:thinking_face:
 Huh

I've done convex polygon splitting by an arbitrary plane before. It's never been this complicated.
You just loop over the vertices of the polygon and test to see if the line made by the current vertex and the previous one intersects the plane.
If it does, you insert two vertices where it intersects (one for the polygon in front and one for behind)
At the end, you can triangulate the convex polygon easily with this algorithm:
1  
2  
3  
4  
5  
for (int i = 2; i < num_vertices; i++) {
   int v1i = i - 1;
   int v2i = i;
   //create new triangle made from vertices { 0, v1i, v2i } in that order
}

Offline KaiHH

JGO Kernel


Medals: 636



« Reply #6 - Posted 2018-09-15 06:17:58 »

Yes, the algorithm I described is just the unrolled variant of yours where you use an ordered list of vertices to triangulate the resulting triangles later, whereas I do not use an ordered list of vertices but use the 27 different possibilities of intersections to know in which order new and existing vertices form the triangles. It's pretty much the same.
Offline Archive
« Reply #7 - Posted 2018-09-15 15:54:19 »

Aha okay. Thanks for clarifying

Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1869 views)
2018-06-10 19:43:48

EgonOlsen (1901 views)
2018-06-10 19:43:44

EgonOlsen (1258 views)
2018-06-10 19:43:20

DesertCoockie (1703 views)
2018-05-13 18:23:11

nelsongames (1378 views)
2018-04-24 18:15:36

nelsongames (2001 views)
2018-04-24 18:14:32

ivj94 (2753 views)
2018-03-24 14:47:39

ivj94 (1955 views)
2018-03-24 14:46:31

ivj94 (3045 views)
2018-03-24 14:43:53

Solater (1090 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!