initial commit of the fluid simulator.
[blender.git] / intern / elbeem / intern / ntl_geometrysphere.cpp
1 /******************************************************************************
2  *
3  * El'Beem - Free Surface Fluid Simulation with the Lattice Boltzmann Method
4  * Copyright 2003,2004 Nils Thuerey
5  *
6  * A simple sphere object
7  *
8  *****************************************************************************/
9
10 #include "ntl_geometrysphere.h"
11 #include "ntl_ray.h"
12 #include "ntl_scene.h"
13
14
15 /******************************************************************************
16  * Default Constructor 
17  *****************************************************************************/
18 ntlGeometrySphere::ntlGeometrySphere() :
19         ntlGeometryObject(),
20         mvCenter( 0.0 ),
21         mRadius( 1.0 ),
22         mRefPolar(5), mRefAzim(5)
23 {
24 }
25
26
27 /*****************************************************************************/
28 /* Init attributes */
29 /*****************************************************************************/
30 void ntlGeometrySphere::initialize(ntlRenderGlobals *glob) {
31         ntlGeometryObject::initialize(glob);
32
33         mvCenter    = vec2G(mpAttrs->readVec3d("center", vec2D(mvCenter)   ,"ntlGeometrySphere", "mvCenter", false));
34         mRadius     = mpAttrs->readFloat("radius", mRadius    ,"ntlGeometrySphere", "mRadius", false);
35         mRefPolar   = mpAttrs->readInt  ("refpolar", mRefPolar,"ntlGeometrySphere", "mRefPolar", false);
36         mRefAzim    = mpAttrs->readInt  ("refazim",  mRefAzim ,"ntlGeometrySphere", "mRefAzim", false);
37         if(mRefPolar<1) mRefPolar = 1;
38         if(mRefAzim<1) mRefAzim = 1;
39         mRefAzim *= 4;
40
41         mvBBStart = mvCenter - ntlVec3Gfx(mRadius);
42         mvBBEnd   = mvCenter + ntlVec3Gfx(mRadius);
43 }
44
45
46 /******************************************************************************
47  * 
48  *****************************************************************************/
49
50 ntlVec3Gfx getSphereCoord(gfxReal radius, gfxReal phi, gfxReal theta) {
51         return ntlVec3Gfx(
52                         radius * cos(theta) * sin(phi),
53                         radius * sin(theta) * sin(phi),
54                         radius * cos(phi)
55                         );
56 };
57
58 void 
59 ntlGeometrySphere::getTriangles( vector<ntlTriangle> *triangles, 
60                                                                                                                         vector<ntlVec3Gfx> *vertices, 
61                                                                                                                         vector<ntlVec3Gfx> *normals, int objectId )
62 {
63
64         gfxReal phiD   = 0.5* M_PI/ (gfxReal)mRefPolar;
65         gfxReal thetaD = 2.0* M_PI/ (gfxReal)mRefAzim;
66         gfxReal phi   = 0.0;
67         for(int i=0; i<mRefPolar; i++) {
68                 gfxReal theta = 0.0;
69                 for(int j=0; j<mRefAzim; j++) {
70                         ntlVec3Gfx p1,p2,p3;
71                         ntlVec3Gfx n1,n2,n3;
72
73                         p1 = getSphereCoord(mRadius, phi     , theta );
74                         p2 = getSphereCoord(mRadius, phi+phiD, theta );
75                         p3 = getSphereCoord(mRadius, phi+phiD, theta+thetaD );
76                         n1 = getNormalized(p1);
77                         n2 = getNormalized(p2);
78                         n3 = getNormalized(p3);
79                         //n3 = n2 = n1;
80                         p1 += mvCenter;
81                         p2 += mvCenter;
82                         p3 += mvCenter;
83                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
84
85                         n1[2] *= -1.0;
86                         n2[2] *= -1.0;
87                         n3[2] *= -1.0;
88                         p1[2] -= mvCenter[2];
89                         p2[2] -= mvCenter[2];
90                         p3[2] -= mvCenter[2];
91                         p1[2] *= -1.0;
92                         p2[2] *= -1.0;
93                         p3[2] *= -1.0;
94                         p1[2] += mvCenter[2];
95                         p2[2] += mvCenter[2];
96                         p3[2] += mvCenter[2];
97                         sceneAddTriangle( p1,p3,p2,  n1,n3,n2,  ntlVec3Gfx(0.0), 1 );
98
99                         p1 = getSphereCoord(mRadius, phi     , theta );
100                         p3 = getSphereCoord(mRadius, phi     , theta+thetaD );
101                         p2 = getSphereCoord(mRadius, phi+phiD, theta+thetaD );
102                         n1 = getNormalized(p1);
103                         n2 = getNormalized(p2);
104                         n3 = getNormalized(p3);
105                         //n3 = n2 = n1;
106                         p1 += mvCenter;
107                         p2 += mvCenter;
108                         p3 += mvCenter;
109                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
110
111                         n1[2] *= -1.0;
112                         n2[2] *= -1.0;
113                         n3[2] *= -1.0;
114                         p1[2] -= mvCenter[2];
115                         p2[2] -= mvCenter[2];
116                         p3[2] -= mvCenter[2];
117                         p1[2] *= -1.0;
118                         p2[2] *= -1.0;
119                         p3[2] *= -1.0;
120                         p1[2] += mvCenter[2];
121                         p2[2] += mvCenter[2];
122                         p3[2] += mvCenter[2];
123                         sceneAddTriangle( p1,p3,p2,  n1,n3,n2,  ntlVec3Gfx(0.0), 1 );
124                         
125                         theta += thetaD;
126                 }
127                 phi += phiD;
128         }
129         
130         int doBack = 0;
131         int doFront = 0;
132         int doTop = 0;
133         int doBottom = 0;
134         int doLeft = 0;
135         int doRight = 0;
136         ntlVec3Gfx mvStart = mvBBStart;
137         ntlVec3Gfx mvEnd   = mvBBEnd;
138         
139         gfxReal s0 = mvStart[0];
140         gfxReal s1 = mvStart[1];
141         gfxReal s2 = mvStart[2];
142         gfxReal e0 = mvEnd[0];
143         gfxReal e1 = mvEnd[1];
144         gfxReal e2 = mvEnd[2];
145         ntlVec3Gfx p1,p2,p3;
146         ntlVec3Gfx n1,n2,n3;
147
148                 /* front plane */
149                 if(doFront) {
150                         n1 = n2 = n3 = ntlVec3Gfx( 0.0, 0.0, -1.0 );
151                         p1 = ntlVec3Gfx( s0, s1, s2 );
152                         p3 = ntlVec3Gfx( e0, s1, s2 );
153                         p2 = ntlVec3Gfx( s0, e1, s2 );
154                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
155                         p1 = ntlVec3Gfx( e0, e1, s2 );
156                         p3 = ntlVec3Gfx( s0, e1, s2 );
157                         p2 = ntlVec3Gfx( e0, s1, s2 );
158                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
159                 }
160
161                 /* back plane k */
162                 if(doBack) {
163                         n1 = n2 = n3 = ntlVec3Gfx( 0.0, 0.0, 1.0 );
164                         p1 = ntlVec3Gfx( s0, s1, e2 );
165                         p3 = ntlVec3Gfx( s0, e1, e2 );
166                         p2 = ntlVec3Gfx( e0, s1, e2 );
167                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
168                         p1 = ntlVec3Gfx( s0, e1, e2 );
169                         p3 = ntlVec3Gfx( e0, e1, e2 );
170                         p2 = ntlVec3Gfx( e0, s1, e2 );
171                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
172                 }
173
174                 /* bottom plane k */
175                 if(doBottom) {
176                         n1 = n2 = n3 = ntlVec3Gfx( 0.0, -1.0, 0.0 );
177                         p1 = ntlVec3Gfx( e0, s1, s2 );
178                         p3 = ntlVec3Gfx( s0, s1, s2 );
179                         p2 = ntlVec3Gfx( s0, s1, e2 );
180                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
181                         p1 = ntlVec3Gfx( s0, s1, e2 );
182                         p3 = ntlVec3Gfx( e0, s1, e2 );
183                         p2 = ntlVec3Gfx( e0, s1, s2 );
184                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
185                 }
186
187                 /* top plane k */
188                 if(doTop) {
189                         n1 = n2 = n3 = ntlVec3Gfx( 0.0, 1.0, 0.0 );
190                         p1 = ntlVec3Gfx( e0, e1, e2 );
191                         p2 = ntlVec3Gfx( e0, e1, s2 );
192                         p3 = ntlVec3Gfx( s0, e1, e2 );
193                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
194                         p1 = ntlVec3Gfx( s0, e1, s2 );
195                         p2 = ntlVec3Gfx( s0, e1, e2 );
196                         p3 = ntlVec3Gfx( e0, e1, s2 );
197                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
198                 }
199
200                 /* left plane k */
201                 if(doLeft) {
202                         n1 = n2 = n3 = ntlVec3Gfx( -1.0, 0.0, 0.0 );
203                         p1 = ntlVec3Gfx( s0, s1, e2 );
204                         p3 = ntlVec3Gfx( s0, s1, s2 );
205                         p2 = ntlVec3Gfx( s0, e1, s2 );
206                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
207                         p1 = ntlVec3Gfx( s0, e1, s2 );
208                         p3 = ntlVec3Gfx( s0, e1, e2 );
209                         p2 = ntlVec3Gfx( s0, s1, e2 );
210                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
211                 }
212
213                 /* right plane k */
214                 if(doRight) {
215                         n1 = n2 = n3 = ntlVec3Gfx( 1.0, 0.0, 0.0 );
216                         p1 = ntlVec3Gfx( e0, e1, e2 );
217                         p3 = ntlVec3Gfx( e0, e1, s2 );
218                         p2 = ntlVec3Gfx( e0, s1, e2 );
219                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
220                         p1 = ntlVec3Gfx( e0, e1, s2 );
221                         p3 = ntlVec3Gfx( e0, s1, s2 );
222                         p2 = ntlVec3Gfx( e0, s1, e2 );
223                         sceneAddTriangle( p1,p2,p3,  n1,n2,n3,  ntlVec3Gfx(0.0), 1 );
224                 }
225
226
227 }
228
229