Skip to content

Commit 173325d

Browse files
committed
Improve TGeoSphere creation - handle all parameters combinations
1 parent 1720a51 commit 173325d

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

changes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
1. Implement TGeoCtub, TGeoParaboloid and TGeoHype shapes
55
2. Support TGeoTube with Rmin==0
66
3. Exclude empty faces in TGeoArb8
7-
4. Improve TGeoSphere creation
7+
4. Improve TGeoSphere creation - handle all parameters combinations
88
5. Introduce JSROOT.cleanup() function to safely clear all drawn objects
99
6. Fix wrong resize method in 'tabs' and 'collapsible' layouts
1010
7. Fix canvas resize problem (issue #27)

scripts/JSRootGeoPainter.js

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -193,68 +193,78 @@
193193

194194

195195
JSROOT.GEO.createSphere = function( shape ) {
196-
197196
var outerRadius = shape.fRmax;
198197
var innerRadius = shape.fRmin;
199198
var phiStart = shape.fPhi1 + 180;
200199
var phiLength = shape.fPhi2 - shape.fPhi1;
201200
var thetaStart = shape.fTheta1;
202201
var thetaLength = shape.fTheta2 - shape.fTheta1;
203-
204-
var widthSegments = Math.floor(phiLength / 6);
205-
if (widthSegments < 8) widthSegments = 8;
206-
207-
var heightSegments = Math.floor(thetaLength / 6);
208-
if (heightSegments < 8) heightSegments = 8;
202+
var widthSegments = shape.fNseg;
203+
var heightSegments = shape.fNz;
209204

210205
var sphere = new THREE.SphereGeometry( outerRadius, widthSegments, heightSegments,
211206
phiStart*Math.PI/180, phiLength*Math.PI/180, thetaStart*Math.PI/180, thetaLength*Math.PI/180);
212207
sphere.applyMatrix( new THREE.Matrix4().makeRotationX( Math.PI / 2 ) );
213208

214-
// simple sphere without inner cut
215-
if ((innerRadius <= 0) && (thetaLength === 180) && (phiLength === 360)) return sphere;
216-
217-
if (innerRadius <= 0) innerRadius = 0.0000001;
218-
219209
var geometry = new THREE.Geometry();
220210
var color = new THREE.Color();
221211

222-
// add inner sphere
212+
// add outer sphere
223213
for (var n=0; n < sphere.vertices.length; ++n)
224214
geometry.vertices.push(sphere.vertices[n]);
225215

216+
// add faces
226217
for (var n=0; n < sphere.faces.length; ++n) {
227218
var face = sphere.faces[n];
228219
geometry.faces.push(new THREE.Face3( face.a, face.b, face.c, null, color, 0 ) );
229220
}
230221

231222
var shift = geometry.vertices.length;
232-
var k = innerRadius / outerRadius;
223+
var noInside = (innerRadius <= 0);
233224

234-
// add outer sphere
235-
for (var n=0; n < sphere.vertices.length; ++n) {
236-
var v = sphere.vertices[n];
237-
geometry.vertices.push(new THREE.Vector3(k*v.x, k*v.y, k*v.z));
238-
}
225+
if (noInside) {
226+
// simple sphere without inner cut
227+
if ((thetaLength === 180) && (phiLength === 360)) {
228+
geometry.computeFaceNormals();
229+
return geometry;
230+
}
239231

240-
for (var n=0; n < sphere.faces.length; ++n) {
241-
var face = sphere.faces[n];
242-
geometry.faces.push(new THREE.Face3( shift+face.a, shift+face.b, shift+face.c, null, color, 0 ) );
232+
geometry.vertices.push(new THREE.Vector3(0, 0, 0));
233+
} else {
234+
var k = innerRadius / outerRadius;
235+
236+
// add inner sphere
237+
for (var n=0; n < sphere.vertices.length; ++n) {
238+
var v = sphere.vertices[n];
239+
geometry.vertices.push(new THREE.Vector3(k*v.x, k*v.y, k*v.z));
240+
}
241+
for (var n=0; n < sphere.faces.length; ++n) {
242+
var face = sphere.faces[n];
243+
geometry.faces.push(new THREE.Face3( shift+face.a, shift+face.b, shift+face.c, null, color, 0 ) );
244+
}
243245
}
244246

245247
if (thetaLength !== 180) {
246248
// add top cap
247249
for (var i = 0; i < widthSegments; ++i) {
248-
geometry.faces.push( new THREE.Face3( i+0, i+1, i+shift, null, color, 0 ) );
249-
geometry.faces.push( new THREE.Face3( i+1, i+shift+1, i+shift, null, color, 0 ) );
250+
if (noInside) {
251+
geometry.faces.push( new THREE.Face3( i+0, i+1, shift, null, color, 0 ) );
252+
} else {
253+
geometry.faces.push( new THREE.Face3( i+0, i+1, i+shift, null, color, 0 ) );
254+
geometry.faces.push( new THREE.Face3( i+1, i+shift+1, i+shift, null, color, 0 ) );
255+
}
250256
}
251257

252258
var dshift = sphere.vertices.length - widthSegments - 1;
253259

254260
// add bottom cap
255261
for (var i = dshift; i < dshift + widthSegments; ++i) {
256-
geometry.faces.push( new THREE.Face3( i+0, i+1, i+shift, null, color, 0 ) );
257-
geometry.faces.push( new THREE.Face3( i+1, i+shift+1, i+shift, null, color, 0 ) );
262+
if (noInside) {
263+
geometry.faces.push( new THREE.Face3( i+0, i+1, shift, null, color, 0 ) );
264+
} else {
265+
geometry.faces.push( new THREE.Face3( i+0, i+1, i+shift, null, color, 0 ) );
266+
geometry.faces.push( new THREE.Face3( i+1, i+shift+1, i+shift, null, color, 0 ) );
267+
}
258268
}
259269
}
260270

@@ -263,15 +273,23 @@
263273
for (var j=0; j<heightSegments; j++) {
264274
var i1 = j*(widthSegments+1);
265275
var i2 = (j+1)*(widthSegments+1);
266-
geometry.faces.push( new THREE.Face3( i1, i2, i1+shift, null, color, 0 ) );
267-
geometry.faces.push( new THREE.Face3( i2, i2+shift, i1+shift, null, color, 0 ));
276+
if (noInside) {
277+
geometry.faces.push( new THREE.Face3( i1, i2, shift, null, color, 0 ) );
278+
} else {
279+
geometry.faces.push( new THREE.Face3( i1, i2, i1+shift, null, color, 0 ) );
280+
geometry.faces.push( new THREE.Face3( i2, i2+shift, i1+shift, null, color, 0 ));
281+
}
268282
}
269283
// another cuted side
270284
for (var j=0;j<heightSegments;j++) {
271285
var i1 = (j+1)*(widthSegments+1) - 1;
272286
var i2 = (j+2)*(widthSegments+1) - 1;
273-
geometry.faces.push( new THREE.Face3( i1, i2, i1+shift, null, color, 0 ) );
274-
geometry.faces.push( new THREE.Face3( i2, i2+shift, i1+shift, null, color, 0));
287+
if (noInside) {
288+
geometry.faces.push( new THREE.Face3( i1, i2, shift, null, color, 0 ) );
289+
} else {
290+
geometry.faces.push( new THREE.Face3( i1, i2, i1+shift, null, color, 0 ) );
291+
geometry.faces.push( new THREE.Face3( i2, i2+shift, i1+shift, null, color, 0));
292+
}
275293
}
276294
}
277295

0 commit comments

Comments
 (0)