Normand Briere
2019-08-28 547c9203ab5d8e4bee36d1cbb453dfa36bbec4ef
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
import java.awt.*;
import java.util.Vector;
 
class BezierSurface extends Composite implements BezierElement, java.io.Serializable
{
    static final long serialVersionUID = 3659371981955525551L;
 
    BezierSurface()
    {
        this(7,7);
    }
    
    BezierSurface(int nx, int nz)
    {
        name = "BezierSurface";
 
       ctrlPnts = new Composite();
       
       //int nx = 8, nz = 8;
       float fnx = nx, fnz = nz;
       
       float incx = 3.0f/(nx+2);
       float incz = 3.0f/(nz+2);
 
                int m = 0;
       for( float i=-1.5f; i<1.51; i+=incx, m++)
                {
                    int n = 0;
           for( float j=-1.5f; j<1.51; j+=incz, n++)
                        {
                            Sphere s = new Sphere(j,0,i);
                            s.handle = true;
                            s.name = "CP " + m + n;
               ctrlPnts.addElement(s);
                        }
                }
 
       for (int i=0; i<nx; i+=1) // bezier = 3)
       {
           for (int j=0; j<nz; j+=1) // bezier = 3)
           {
                            // Checker
                            //if (((i^j)&1) == 0)
                            //    continue;
                            
               Composite tmp = new Composite();
           
               for (int k=0; k<4; k++)
               {
                   for (int l=0; l<4; l++)
                   {
                       tmp.addElement(ctrlPnts.get((i+k)*(nz+3) + j+l));
                   }
               }
               
               addChild(new BezierPatch(tmp, j/fnz, (j+1)/fnz, (nx - i - 1)/fnx, (nx - i)/fnx));
           }
       }
       
        recalculate();
    }
 
    Object3D deepCopy()
    {
        BezierSurface l = new BezierSurface();
        deepCopySelf(l);
        return l;
    }
 
   void draw(iCameraPane display, Object3D /*Composite*/ root, boolean selected, boolean blocked)
   {
   //    ((BezierSurface)parent.parent).currentHandle = (Sphere)this;
        //        parent.parent.recalculate();
            recalculate();
                
       super.draw(display, root, selected, blocked);
       
       //if (IsSelected())
       //    ctrlPnts.draw(display, true);
   }
   
    void createEditWindow(GroupEditor callee, boolean newWindow)
    {
        if(newWindow)
            objectUI = new BezierEditor(this, deepCopy(), callee);
        else
            objectUI = new BezierEditor(this, callee);
        
        editWindow = objectUI.GetEditor();
    }
 
    void getBounds(cVector minima, cVector maxima, boolean xform)
    {
        //spline.getBounds(minima, maxima, true);
        maxima.z = maxima.x;
        minima.z = minima.x = -maxima.x;
        if (xform)
            transformBounds(minima, maxima);
    }
 
    boolean inside(double x, double y, double z, boolean xform)
    {
       assert false;
       
       return false;
    }
 
    void draw(ClickInfo info, int level, boolean select)
    {
        cVector sample;
        info.g.setColor(Color.black);
        int nPoints = ctrlPnts.size();
 
        for (int i=0; i < nPoints; i++)
        {
           sample = ((Sphere)ctrlPnts.get(i)).getCenter();
            Point prev = new Point(0, 0);
            Point curr = new Point(0, 0);
            Rectangle dummy = new Rectangle();
            calcHotSpot(sample, //info,
                    prev, dummy);
           sample = ((Sphere)ctrlPnts.get(i)).getCenter();
            calcHotSpot(sample, //info,
                    curr, dummy);
            info.g.drawLine(prev.x, prev.y, curr.x, curr.y);
            prev.x = curr.x;
            prev.y = curr.y;
        }
    }
 
    void drawEditHandles(//ClickInfo info,
            int level)
    {
        clickInfo.g.setColor(Color.red);
        int count = ctrlPnts.size();
        for (int i=0; i < count; i++)
        {
            cVector p = ((Sphere)ctrlPnts.elementAt(i)).getCenter();
            Rectangle spot = calcHotSpot(p); //, info);
            clickInfo.g.fillRect(spot.x, spot.y, spot.width, spot.height);
        }
 
    }
 
    boolean doEditClick(//ClickInfo info,
            int level)
    {
        startX = clickInfo.x;
        startY = clickInfo.y;
        int nPoints = ctrlPnts.size();
        hitSomething = false;
        for (int i=0; i < nPoints; i++)
        {
            cVector p = ((Sphere)ctrlPnts.elementAt(i)).getCenter();
            Rectangle r = calcHotSpot(p); //, info);
            if (r.contains(clickInfo.x, clickInfo.y))
            {
                hitSomething = true;
                hitIndex = i;
       currentHandle = (Sphere)ctrlPnts.get(hitIndex);
                LA.vecCopy(p, startVec);
            }
        }
 
       return hitSomething;
    }
 
    void doEditDrag(ClickInfo info)
    {
        if (!hitSomething)
            return;
        cVector delta = LA.newVector(info.x - startX, startY - info.y, 0);
        LA.xformDir(delta, info.camera.fromScreen, delta);
       delta.x /= 100 * info.camera.SCALE / info.camera.Distance();
       delta.y /= 100 * info.camera.SCALE / info.camera.Distance();
       delta.z /= 100 * info.camera.SCALE / info.camera.Distance();
        cVector p = ((Sphere)ctrlPnts.get(hitIndex)).getCenter();
        LA.vecCopy(startVec, p);
        LA.vecAdd(p, delta, p);
       
       currentHandle = (Sphere)ctrlPnts.get(hitIndex);
       currentHandle.refreshCenter(p);
       
        recalculate();
       
       currentHandle = null;
    }
 
    public void recalculate()
    {
       int nb = children.size();
       for (int i=0; i<nb; i++)
       {
           BezierPatch child = (BezierPatch) children.reserve(i);
           child.depth = depth;
   // SPEED        if (currentHandle == null || child.ctrlPnts.contains(currentHandle))
           {
               //System.out.println("RECAL " + currentHandle);
               child.recalculate();
           }
           children.release(i);
       }
       //System.out.println();
       
       super.recalculate();
   }
   
   /*
    void recalculate()
    {
       float[] vertices = new float[4*4*3];
       
       for( int i=0, i3=0; i<ctrlPnts.size(); i++, i3+=3 )
       {
           cVector v = ((Sphere)ctrlPnts.get(i)).getCenter();
           vertices[i3] = (float)v.x;
           vertices[i3+1] = (float)v.y;
           vertices[i3+2] = (float)v.z;
       System.out.println("vertices = " + vertices[i3] + " " + vertices[i3+1] + " " + vertices[i3+2]);
       }
       
       bRep = cBezier.GenPatch(vertices, depth);
 
       if (!bRep.trimmed)
           bRep.Trim(false, false);
       
       super.recalculate();
   }
   */
   
    private static int startX;
    private static int startY;
    private static boolean hitSomething;
    private static int hitIndex;
    private static cVector startVec = new cVector();
   
   Sphere currentHandle;
   
    Composite ctrlPnts;
   
   int depth = 2;
   
   public int GetDepth()
   {
       return depth;
   }
 
   public void SetDepth(int d)
   {
       depth = d;
   }
}