aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/jogamp/graph/curve/tess/Loop.java
blob: 807a07b6755c6bdb0c81c33df73e29cf2ee99fb2 (plain)
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
/**
 * Copyright 2010-2023 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */
package jogamp.graph.curve.tess;

import java.util.ArrayList;


import com.jogamp.graph.geom.Vertex;
import com.jogamp.math.Vec3f;
import com.jogamp.math.VectorUtil;
import com.jogamp.math.geom.AABBox;
import com.jogamp.math.geom.plane.Winding;
import com.jogamp.graph.geom.Triangle;

public class Loop {
    private HEdge root = null;
    private final AABBox box = new AABBox();
    private GraphOutline initialOutline = null;

    private Loop(final GraphOutline polyline, final int edgeType){
        initialOutline = polyline;
        this.root = initFromPolyline(initialOutline, edgeType);
    }

    public static Loop create(final GraphOutline polyline, final int edgeType) {
        final Loop res = new Loop(polyline, edgeType);
        if( null == res.root ) {
            return null;
        } else if( HEdge.HOLE == edgeType ) {
            res.addConstraintCurveImpl(polyline);
        }
        return res;
    }

    public HEdge getHEdge(){
        return root;
    }

    public Triangle cut(final boolean delaunay){
        if(isSimplex()){
            return new Triangle(root.getGraphPoint().getPoint(), root.getNext().getGraphPoint().getPoint(),
                                root.getNext().getNext().getGraphPoint().getPoint(), checkVerticesBoundary(root));
        }
        final HEdge prev = root.getPrev();
        final HEdge next1 = root.getNext();

        final HEdge next2;
        if( CDTriangulator2D.DEBUG ) {
            next2 = isValidNeighborDbg(next1.getNext(), delaunay);
        } else {
            next2 = isValidNeighbor(next1.getNext(), delaunay);
        }
        if(next2 == null){
            root = root.getNext();
            return null;
        }

        final GraphVertex v1 = root.getGraphPoint();
        final GraphVertex v2 = next1.getGraphPoint();
        final GraphVertex v3 = next2.getGraphPoint();

        final HEdge v3Edge = new HEdge(v3, HEdge.INNER);

        HEdge.connect(v3Edge, root);
        HEdge.connect(next1, v3Edge);

        HEdge v3EdgeSib = v3Edge.getSibling();
        if(v3EdgeSib == null){
            v3EdgeSib = new HEdge(v3Edge.getNext().getGraphPoint(), HEdge.INNER);
            HEdge.makeSiblings(v3Edge, v3EdgeSib);
        }

        HEdge.connect(prev, v3EdgeSib);
        HEdge.connect(v3EdgeSib, next2);

        final Triangle t = createTriangle(v1.getPoint(), v2.getPoint(), v3.getPoint(), root);
        this.root = next2;
        return t;
    }

    public boolean isSimplex(){
        return (root.getNext().getNext().getNext() == root);
    }

    /**
     * Create a connected list of half edges (loop)
     * from the boundary profile
     * @param edgeType either {@link HEdge#BOUNDARY} requiring {@link Winding#CCW} or {@link HEdge#HOLE} using {@link Winding#CW} or even {@link Winding#CCW}
     */
    private HEdge initFromPolyline(final GraphOutline outline, final int edgeType) {
        final ArrayList<GraphVertex> vertices = outline.getGraphPoint();

        if(vertices.size()<3) {
            System.err.println( "Graph: Loop.initFromPolyline: GraphOutline's vertices < 3: " + vertices.size() );
            if( GraphOutline.DEBUG ) {
                Thread.dumpStack();
            }
            return null;
        }
        final Winding edgeWinding = HEdge.BOUNDARY == edgeType ? Winding.CCW : Winding.CW;
        final Winding winding = CDTriangulator2D.FixedWindingRule ?  edgeWinding : outline.getOutline().getWinding();

        if( HEdge.BOUNDARY == edgeType && Winding.CCW != winding ) {
            // XXXX
            System.err.println("Loop.init.xx.01: BOUNDARY req CCW but has "+winding);
            // outline.getOutline().print(System.err);
            Thread.dumpStack();
        }
        HEdge firstEdge = null;
        HEdge lastEdge = null;

        if( winding == edgeWinding || HEdge.BOUNDARY == edgeType ) {
            // Correct Winding or skipped CW -> CCW (no inversion possible here, too late)
            final int max = vertices.size() - 1;
            for(int index = 0; index <= max; ++index) {
                final GraphVertex v1 = vertices.get(index);
                box.resize(v1.x(), v1.y(), v1.z());

                final HEdge edge = new HEdge(v1, edgeType);

                v1.addEdge(edge);
                if(lastEdge != null) {
                    lastEdge.setNext(edge);
                    edge.setPrev(lastEdge);
                } else {
                    firstEdge = edge;
                }
                if(index == max ) {
                    edge.setNext(firstEdge);
                    firstEdge.setPrev(edge);
                }
                lastEdge = edge;
            }
        } else { // if( winding == Winding.CW ) {
            // CCW <-> CW
            for(int index = vertices.size() - 1; index >= 0; --index) {
                final GraphVertex v1 = vertices.get(index);
                box.resize(v1.x(), v1.y(), v1.z());

                final HEdge edge = new HEdge(v1, edgeType);

                v1.addEdge(edge);
                if(lastEdge != null) {
                    lastEdge.setNext(edge);
                    edge.setPrev(lastEdge);
                } else {
                    firstEdge = edge;
                }

                if (index == 0) {
                    edge.setNext(firstEdge);
                    firstEdge.setPrev(edge);
                }
                lastEdge = edge;
            }
        }
        return firstEdge;
    }

    public void addConstraintCurve(final GraphOutline polyline, final int edgeType) {
        //        GraphOutline outline = new GraphOutline(polyline);
        /**needed to generate vertex references.*/
        if( null == initFromPolyline(polyline, edgeType) ) { // 'usually' HEdge.HOLE
            return;
        }
        if( HEdge.HOLE == edgeType ) {
            addConstraintCurveImpl(polyline);
        }
    }
    private void addConstraintCurveImpl(final GraphOutline polyline) {
        final GraphVertex v3 = locateClosestVertex(polyline);
        if( null == v3 ) {
            System.err.println( "Graph: Loop.locateClosestVertex returns null; root valid? "+(null!=root));
            if( GraphOutline.DEBUG ) {
                Thread.dumpStack();
            }
            return;
        }
        final HEdge v3Edge = v3.findBoundEdge();
        final HEdge v3EdgeP = v3Edge.getPrev();
        final HEdge crossEdge = new HEdge(root.getGraphPoint(), HEdge.INNER);

        HEdge.connect(root.getPrev(), crossEdge);
        HEdge.connect(crossEdge, v3Edge);

        HEdge crossEdgeSib = crossEdge.getSibling();
        if(crossEdgeSib == null) {
            crossEdgeSib = new HEdge(crossEdge.getNext().getGraphPoint(), HEdge.INNER);
            HEdge.makeSiblings(crossEdge, crossEdgeSib);
        }

        HEdge.connect(v3EdgeP, crossEdgeSib);
        HEdge.connect(crossEdgeSib, root);
    }

    /**
     * Locates the vertex and update the loops root
     * to have (root + vertex) as closest pair
     * @param polyline the control polyline to search for closestvertices in CW
     * @return the vertex that is closest to the newly set root Hedge.
     */
    private GraphVertex locateClosestVertex(final GraphOutline polyline) {
        HEdge closestE = null;
        GraphVertex closestV = null;

        // final Winding winding = polyline.getOutline().getWinding();
        // final Winding winding = getWinding( vertices ); // requires area-winding detection

        float minDistance = Float.MAX_VALUE;
        final ArrayList<GraphVertex> initVertices = initialOutline.getGraphPoint();
        final ArrayList<GraphVertex> vertices = polyline.getGraphPoint();

        for(int i=0; i< initVertices.size()-1; i++){
            final GraphVertex v = initVertices.get(i);
            final GraphVertex nextV = initVertices.get(i+1);
            for(int pos=0; pos<vertices.size(); pos++) {
                final GraphVertex cand = vertices.get(pos);
                final float distance = v.getCoord().dist( cand.getCoord() );
                if(distance < minDistance){
                    boolean inside = false;
                    for (final GraphVertex vert:vertices){
                        if(vert == v || vert == nextV || vert == cand) {
                            continue;
                        }
                        inside = VectorUtil.isInCircleVec2d(v.getPoint(), nextV.getPoint(), cand.getPoint(), vert.getPoint());
                        if(inside){
                            break;
                        }
                    }
                    if(!inside){
                        closestV = cand;
                        minDistance = distance;
                        closestE = v.findBoundEdge();
                    }
                }

            }
        }

        if(closestE != null){
            root = closestE;
        }

        return closestV;
    }

    private HEdge isValidNeighbor(final HEdge candEdge, final boolean delaunay) {
        final HEdge next = root.getNext();
        if( !VectorUtil.isCCW( root.getGraphPoint().getPoint(), next.getGraphPoint().getPoint(),
                               candEdge.getGraphPoint().getPoint()) ) {
            return null;
        }
        if( !delaunay ) {
            return candEdge;
        }
        final Vertex candPoint = candEdge.getGraphPoint().getPoint();
        HEdge e = candEdge.getNext();
        while (e != candEdge){
            final GraphVertex egp = e.getGraphPoint();
            if(egp != root.getGraphPoint() &&
               egp != next.getGraphPoint() &&
               egp.getPoint() != candPoint )
            {
                if( VectorUtil.isInCircleVec2d(root.getGraphPoint().getPoint(), next.getGraphPoint().getPoint(),
                                               candPoint, egp.getPoint()) ) {
                    return null;
                }
            }
            e = e.getNext();
        }
        return candEdge;
    }
    private HEdge isValidNeighborDbg(final HEdge candEdge, final boolean delaunay) {
        final HEdge next = root.getNext();
        if( !VectorUtil.isCCW( root.getGraphPoint().getPoint(), next.getGraphPoint().getPoint(),
                               candEdge.getGraphPoint().getPoint()) ) {
            return null;
        }
        if( !delaunay ) {
            return candEdge;
        }
        final Vertex candPoint = candEdge.getGraphPoint().getPoint();
        HEdge e = candEdge.getNext();
        while (e != candEdge){
            final GraphVertex egp = e.getGraphPoint();
            if(egp != root.getGraphPoint() &&
               egp != next.getGraphPoint() &&
               egp.getPoint() != candPoint )
            {
                final double v = VectorUtil.inCircleVec2dVal(root.getGraphPoint().getPoint(), next.getGraphPoint().getPoint(),
                                                             candPoint, egp.getPoint());
                if( v > VectorUtil.InCircleDThreshold ) {
                    System.err.printf("Loop.isInCircle.1: %30.30f: %s, of%n- %s%n- %s%n- %s%n",
                            v, candPoint, root.getGraphPoint().getPoint(), next.getGraphPoint().getPoint(), egp.getPoint());
                    return null;
                }
                System.err.printf("Loop.isInCircle.0: %30.30f: %s, of%n- %s%n- %s%n- %s%n",
                        v, candPoint, root.getGraphPoint().getPoint(), next.getGraphPoint().getPoint(), egp.getPoint());
            }
            e = e.getNext();
        }
        System.err.printf("Loop.isInCircle.0: %s%n", candPoint);
        return candEdge;
    }

    /** Create a triangle from the param vertices only if
     * the triangle is valid. IE not outside region.
     * @param v1 vertex 1
     * @param v2 vertex 2
     * @param v3 vertex 3
     * @param root and edge of this triangle
     * @return the triangle iff it satisfies, null otherwise
     */
    private Triangle createTriangle(final Vertex v1, final Vertex v2, final Vertex v3, final HEdge rootT){
        return new Triangle(v1, v2, v3, checkVerticesBoundary(rootT));
    }

    private boolean[] checkVerticesBoundary(final HEdge rootT) {
        final boolean[] boundary = new boolean[3];
        if(rootT.getGraphPoint().isBoundaryContained()){
                boundary[0] = true;
        }
        if(rootT.getNext().getGraphPoint().isBoundaryContained()){
                boundary[1] = true;
        }
        if(rootT.getNext().getNext().getGraphPoint().isBoundaryContained()){
                boundary[2] = true;
        }
        return boundary;
    }

    public boolean checkInside(final Vertex v) {
        if(!box.contains(v.x(), v.y(), v.z())){
            return false;
        }

        boolean inside = false;
        HEdge current = root;
        HEdge next = root.getNext();
        do {
            final Vertex v2 = current.getGraphPoint().getPoint();
            final Vertex v1 = next.getGraphPoint().getPoint();

            if ( ((v1.y() > v.y()) != (v2.y() > v.y())) &&
                  (v.x() < (v2.x() - v1.x()) * (v.y() - v1.y()) / (v2.y() - v1.y()) + v1.x()) ){
                inside = !inside;
            }

            current = next;
            next = current.getNext();

        } while(current != root);

        return inside;
    }

    public int computeLoopSize(){
        int size = 0;
        HEdge e = root;
        do{
            size++;
            e = e.getNext();
        }while(e != root);
        return size;
    }
}