Normand Briere
2019-09-04 c8bb659043bfa0ccf9436d7cbbc49255b4c82402
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
 
import java.awt.Rectangle;
import java.util.Enumeration;
import java.util.Vector;
 
class CSG extends Object3D // Composite
                    implements java.io.Serializable
{
 
    static final long serialVersionUID = 3639231904991892506L; // 6738468315356680055L;
 
    CSG()
    {
        csgType = 1;
        genType = 0;
        userType = 1; // No strip
        normals = true;
        cellSize = 2;
        cellSize2 = 2;
        tolerance = 0; //.1;
        name = "CSG";
        
        link2master = true; // display the mesh, not the primitives
        CreateMaterial();
    }
 
    Object3D deepCopy()
    {
        CSG csg = new CSG();
        deepCopySelf(csg);
        return csg;
    }
 
    protected void deepCopyNode(Object3D other)
    {
        super.deepCopyNode(other);
        CSG c = (CSG) other;
        c.csgType = csgType;
        c.genType = genType;
        c.userType = userType;
        c.normals = normals;
 
        c.cellSize = cellSize;
        c.cellSize2 = cellSize2;
        c.tolerance = tolerance;
    }
 
    void generatePOV(StringBuffer buffer)
    {
        generateNameComment(buffer);
        generateIndent(buffer);
        switch (csgType)
        {
            case 1: // '\001'
                buffer.append("union");
                break;
 
            case 2: // '\002'
                buffer.append("merge");
                break;
 
            case 3: // '\003'
                buffer.append("intersection");
                break;
 
            case 4: // '\004'
                buffer.append("difference");
                break;
        }
        buffer.append(" {\n");
        Object3D.povDepth += 4;
        Object3D child;
        for (Enumeration e = Children().elements(); e.hasMoreElements(); child.generatePOV(buffer))
        {
            child = (Object3D) e.nextElement();
        }
 
        Object3D.povDepth -= 4;
        generateTransform(buffer);
        generateIndent(buffer);
        buffer.append("}\n");
    }
 
    void createEditWindow(GroupEditor callee, boolean newWindow)
    {
        System.out.println("csg.tolerance2 = " + tolerance);
        //editWindow = new CSGEditor(this, deepCopy(), callee);
        if (newWindow)
        {
            objectUI = new CSGEditor(this, deepCopy(), callee);
        } else
        {
            objectUI = new CSGEditor(this, callee);
        }
        editWindow = objectUI.GetEditor();
    }
 
    boolean doSelection(ClickInfo info, Rectangle r, int level)
    {
        if (csgType == UNION) // || csgType == MERGE)
        {
            return super.doSelection(info, r, level);
        }
        if ((info.flags & 0x1) == 0 && level == 0) // ??
        {
            return super.doSelection(info, r, level);
        }
        if (Children().size() < 1) // 2)
        {
            return super.doSelection(info, r, level);
        } else
        {
            return doSelectionSelf(info, r, level);
        }
    }
 
    void draw(ClickInfo info, int level, boolean select)
    {
        if (csgType == UNION) // || csgType == MERGE)
        {
            super.draw(info, level, select);
        } else if ((info.flags & 0x1) == 0 && level == 0) // ??
        {
            super.draw(info, level, select);
        } else if (Children().size() < 1) // 2)
        {
            super.draw(info, level, select);
        } else
        {
            drawSelf(info, level, select);
        }
    }
 
    /**/
    void draw(iCameraPane display, Object3D /*Composite*/ root, boolean selected, boolean blocked)
    {
        if (csgType == UNION || !link2master) // || csgType == MERGE)
        {
            BoundaryRep keep = bRep;
            bRep = null;
            super.draw(display, root, selected, blocked);
            bRep = keep;
        } else //if ((info.flags & 0x1) == 0 && level == 0) // ??
        //    super.draw(gl);
        //else
        if (Children().size() < 1) // 2)
        {
            super.draw(display, root, selected, blocked);
        } else
        {
            //super.draw(display, root, selected); // Reset touched flag
            Draw(display, root, selected, blocked);
        }
    }
    /**/
 
    void PreprocessOcclusion(iCameraPane cp)
    {
        if (csgType == UNION) // || csgType == MERGE)
        {
            super.PreprocessOcclusion(cp);
            return;
        }
        
        if (count <= 1)
        {
            return;
        }
 
        System.out.println("processing " + this + ": " + (bRep.getRawVertices().length / 3) + " vertices...");
        bRep.PreprocessOcclusion(cp, GlobalTransformInv());
        Touch();
    }
 
    void Touch()
    {
        super.Touch();
    /*
    for(Object3D c : children)
    {
    if( c.touched )
    {
    retile0(true);
    //new Exception().printStackTrace();
    return;
    }
    }
     */
    //csgType = UNION;
    }
 
    void retile()
    {
        retile(true);
    }
 
    void retile(boolean forced)
    {
        //new Exception().printStackTrace();
        ClearList();
        retile0(forced);
    }
 
    void retile0(boolean forced)
    {
        if (csgType == UNION) // || csgType == 2)
        {
            bRep = null; // new BoundaryRep();
            return;
        }
        if (Children().size() < 1) // 2)
        {
            bRep = null; // new BoundaryRep();
            return;
        } else
        {
            if (forced || aNormals != normals || aStripify != userType || aGenType != genType || aCellSize != cellSize ||
                    aCellSize2 != cellSize2 || aTolerance != tolerance)
            {
                System.out.println("RETILE " + userType);
                bRep = (new ImplicitTiler(this, genType, normals, userType != 2, userType == 0, cellSize, cellSize + cellSize2, tolerance, true)).bRep;
            }
            aNormals = normals;
            aStripify = userType;
            aGenType = genType;
            aCellSize = cellSize;
            aCellSize2 = cellSize2;
            aTolerance = tolerance;
            return;
        }
    }
 
    void getBounds(cVector minima, cVector maxima, boolean xform)
    {
        if (Children().size() == 0)
        {
            minima.x = minima.y = minima.z = Double.MAX_VALUE;
            maxima.x = maxima.y = maxima.z = -Double.MAX_VALUE;
            return;
        }
 
        Object3D child = (Object3D) Children().get/*reserve*/(0);
        child.getBounds(minima, maxima, true); // xform);
        //children.release(0);
        if (csgType != DIFFERENCE)
        {
            cVector min = new cVector();
            cVector max = new cVector();
            for (int i = Children().size() - 1; i >= 1; i--)
            {
                child = (Object3D) Children().get(i); // reserve(i);
                if (child == null)
                    return;
                child.getBounds(min, max, true); // xform);
                //children.release(i);
                for (int xyz = 0; xyz < 3; xyz++)
                {
                    if (csgType == MERGE || csgType == UNION)
                    {
                        if (min.get(xyz) < minima.get(xyz))
                        {
                            minima.set(xyz, min.get(xyz));
                        }
                        if (max.get(xyz) > maxima.get(xyz))
                        {
                            maxima.set(xyz, max.get(xyz));
                        }
                    } else
                    {
                        // INTERSECTION
                        if (min.get(xyz) > minima.get(xyz))
                        {
                            minima.set(xyz, min.get(xyz));
                        }
                        if (max.get(xyz) < maxima.get(i))
                        {
                            maxima.set(xyz, max.get(xyz));
                        }
                    }
                }
 
            }
        }
        if (xform)
        {
            transformBounds(minima, maxima);
        }
    }
 
    boolean inside(double x, double y, double z, boolean xform)
    {
        //if (x == 0 && y == 0 && z == 0)
        //System.out.println("TEST INSIDE = " + this);
 
        if (xform && fromParent != null)
        {
            untransform(x, y, z, cStatic.point1);
            x = cStatic.point1.x;
            y = cStatic.point1.y;
            z = cStatic.point1.z;
        }
        //else
        //    LA.setVector(cStatic.point1, x, y, z);
 
        boolean inside = false;
 
        if (csgType == MERGE || csgType == UNION)
        {
            /*
            //Enumeration e = children.elements();
            for (int i=children.size()-1; i>=0; i--) // (e.hasMoreElements()) 
            {
            Object3D child = (Object3D) children.reserve(i); // e.nextElement();
            if (child.inside(x, y, z, xform))
            {
            inside = true;
            //break;
            }
            children.release(i);
            if (inside)
            break;
            }
             */
            BoundaryRep keep = bRep;
            bRep = null; // for merge
            inside = super.inside(x, y, z, false); // xform);
            bRep = keep;
        } else
        {
            //if (children.size() > 0)
            {
                //Enumeration e = children.elements();
                Object3D child = (Object3D) Children().get(0); // e.nextElement();
                if (child.inside(x, y, z, true)) // xform))
                {
                    inside = true;
 
                    if (csgType == DIFFERENCE)
                    {
                        for (int i = Children().size() - 1; i > 0; i--) // (e.hasMoreElements()) 
                        {
                            child = (Object3D) Children().get(i); // e.nextElement();
                            if (child.inside(x, y, z, true)) // xform))
                            {
                                inside = false;
                                break;
                            }
                        }
                    } else
                    {
                        // INTERSECTION
                        for (int i = Children().size() - 1; i > 0; i--) // (e.hasMoreElements()) 
                        {
                            child = (Object3D) Children().get(i); // e.nextElement();
                            if (!child.inside(x, y, z, true)) // xform))
                            {
                                inside = false;
                                break;
                            }
                        }
                    }
                }
            }
        }
 
        //if (x == 0 && y == 0 && z == 0)
        //System.out.println("result = " + inside);
        return inside;
    }
    public static final int UNION = 1;
    public static final int MERGE = 2;
    public static final int INTERSECTION = 3;
    public static final int DIFFERENCE = 4;
    public static final int DIFFERENCEBA = 5;
    int userType; // 0 == stripify, 1 == trimmed, 2 == no trim
    int csgType;
    int genType;
    boolean normals;
    //boolean stripify;
    transient int aGenType;
    transient boolean aNormals;
    transient int aStripify;
    transient int aCellSize;
    transient int aCellSize2;
    transient double aTolerance;
    int cellSize;
    int cellSize2;
    double tolerance;
}