Normand Briere
2019-09-28 57f4646563d6757f65ddc00ca38975c352d76de7
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
 
class Klein extends Biparam implements java.io.Serializable
{
//    void DrawNode(CameraPane display, Object3D /*Composite*/ root, boolean selected) {}
    
    static final long serialVersionUID = 0;
 
    double radius = 4;
    
    Klein()
    {
        this(true);
    }
 
    Klein(Klein s)
    {
        s.deepCopySelf(this);
    }
 
    public Klein(boolean recalc)
    {
        toParent = LA.newMatrix();
        fromParent = LA.newMatrix();
        
        name = "Klein";
        //uDivs = vDivs = 16;
        uDivs = 32;
        vDivs = 32;
        minUDivs = 3;
        minVDivs = 2;
        //center = new cVector();
        if (recalc)
        {
            retile();
            recalculate();
        }
    }
 
    void refreshCenter(cVector c)
    {
        /**/
        toParent[3][0] = c.x; // - center.x;
        toParent[3][1] = c.y; // - center.y;
        toParent[3][2] = c.z; // - center.z;
 
        fromParent[3][0] = -c.x; // + center.x;
        fromParent[3][1] = -c.y; // + center.y;
        fromParent[3][2] = -c.z; // + center.z;
    /**/
    //center.set(0,0,0);
    }
 
    cVector getCenter()
    {
        //assert(center.x == 0);
        //assert(center.y == 0);
        //assert(center.z == 0);
        
     //   if (center == null)
     //       center = new cVector();
        
        cVector c = new cVector(center);
 
        for (int i=GetTransformCount(); --i>=0;)
            LA.xformPos(c, toParent, c);
 
        return c;
    }
 
    Object3D deepCopy()
    {
        Sphere e = new Sphere();
        deepCopySelf(e);
        return e;
    }
 
    protected void deepCopyNode(Object3D other)
    {
        super.deepCopyNode(other);
        Klein e = (Klein) other;
        if (center != null)
        {
            e.center = new cVector(center);
            //LA.vecCopy(center, e.center);
        }
        e.radius = radius;
    }
 
    void generatePOV(StringBuffer buffer)
    {
        generateNameComment(buffer);
        generateIndent(buffer);
        buffer.append("sphere { ");
     //   LA.toPOVRay(center, buffer);
        buffer.append(", ");
     //   buffer.append(radius);
        buffer.append("\n");
        generateTransform(buffer);
        generateIndent(buffer);
        buffer.append("}\n");
    }
 
    Vertex biparamFunction(double u, double v)
    {
        // KLEIN #!
        /*
        double uAng = LA.toRadians(-180*(1-u) + 180*u);
        double vAng = LA.toRadians(-180*(1-v) + 180*v);
        
        // x = cos(u) (a + sin(v) cos(u/2) - sin(2v) sin(u/2)/2)
        // y = sin(u) (a + sin(v) cos(u/2) - sin(2v) sin(u/2)/2)
        // z = sin(u/2) sin(v) + cos(u/2) sin(2v)/2
        //where
        //   -pi <= u <= pi
        //and
        //   -pi <= v <= pi
        double cosu  = Math.cos(uAng);
        double sinu  = Math.sin(uAng);
        double cosu2 = Math.cos(uAng/2);
        double sinu2 = Math.sin(uAng/2);
        double sinv  = Math.sin(vAng);
        double sin2v = Math.sin(vAng/2);
 
        double x = cosu * (handle + sinv * cosu2 - sin2v * sinu2 / 2);
        double y = sinu * (handle + sinv * cosu2 - sin2v * sinu2 / 2);
        double z = sinu2 * sinv + cosu2 * sin2v / 2;
         */
        
        double uAng = LA.toRadians(u*360);
        double vAng = LA.toRadians(v*360);
        
        double cosu  = Math.cos(uAng);
        double sinu  = Math.sin(uAng);
        double cosv  = Math.cos(vAng);
        double sinv  = Math.sin(vAng);
 
        double r = radius * (1 - cosu / 2);
        
        double x = 6 * cosu * (1 + sinu);
        double y = 16 * sinu;
        double z = r * sinv;
        
        if (u < 0.5)
        {
            x += r * cosu * cosv;
            y += r * sinu * cosv;
        }
        else
        {
            x += r * Math.cos(vAng + Math.PI);
        }
        
        // y = -16 .. 20
        r = radius * (20 - y)/36 * (1 - cosu / 2);
        
        x = 6 * cosu * (1 + sinu);
        y = 16 * sinu;
        z = r * sinv;
        
        if (u < 0.5)
        {
            x += r * cosu * cosv;
            y += r * sinu * cosv;
        }
        else
        {
            x += r * Math.cos(vAng + Math.PI);
        }
        
        Vertex temp = new Vertex();
        temp.norm = LA.newVector(x, y, z);
        //temp.pos = new cVector();
        if (center != null)
            LA.vecAdd(temp.norm, center, temp/*.pos*/);
        else
            LA.vecCopy(temp.norm, temp/*.pos*/);
        LA.vecNormalize(temp.norm);
        
        return temp;
    }
 
    void createEditWindow(GroupEditor callee, boolean newWindow)
    {
        //editWindow = (new SphereEditor(this, deepCopy(), callee)).GetEditor();
        if (newWindow)
        {
            objectUI = new KleinEditor(this, deepCopy(), callee);
        } else
        {
            objectUI = new KleinEditor(this, callee);
        }
        
        editWindow = objectUI.GetEditor();
    }
 
    boolean inside(double x, double y, double z, boolean xform)
    {
        return false;
    }
}