Problem 1777

Summary: G4ExtrudedSolid shows wrong shape to tracking with specific set of points in specific order
Product: Geant4 Reporter: perl
Component: geometry/solidsAssignee: Evgueni.Tcherniaev
Status: RESOLVED FIXED    
Severity: major CC: tatiana.nikitina
Priority: P5    
Version: 10.1   
Hardware: All   
OS: All   
Attachments: Left: OpenGL image (no problem), Center: RayTracer (with problem), Right RayTracer (no problem)
Simple program to remove cases of collinear points

Description perl 2015-08-27 09:33:10 CEST
Created attachment 354 [details]
Left: OpenGL image (no problem), Center: RayTracer (with problem), Right RayTracer (no problem)

When creating a G4ExtrudedSolid with a particular set of points, the created
shape appears fine in regular visualization (such as OpenGL), but is wrong in
tracking (it has an unphysical extension in one area, shown in the attached
RayTracer image).

We see this with the simplest form of G4ExtrudedSolid, just a simple polygon
that is extruded in Z with no variation of scale and no x and y offset,
so just a straight extrusion in Z.

If one modifies this set of points by simply cutting off the first point from
the list and instead appending this as the last point (which should make the
same solid), the bug vanishes. That is, it visualizes fine in RayTracer.

I attach the OpenGL image and the two RayTracer images, one with the bug, the
other without (caused by moving the first point of the list to the last point).

The list of points follows:
186 points. Unit in our test was mm.

6.8 -17.9
6.0 -18.3
5.6 -18.5
4.8 -18.8
4.3 -18.9
3.4 -19.0
1.9 -19.1
1.2 -19.0
-3.5 -18.3
-4.1 -18.2
-4.8 -18.0
-5.6 -17.6
-6.9 -16.9
-7.5 -16.5
-9.1 -15.3
-10.3 -14.1
-10.9 -13.5
-11.5 -12.8
-11.7 -12.3
-12.1 -11.6
-12.4 -11.0
-12.6 -10.2
-12.8 -9.6
-12.9 -9.0
-13.0 -8.1
-13.1 -6.8
-13.2 -6.0
-13.6 -5.2
-14.3 -4.7
-15.8 -4.6
-16.4 -4.5
-17.2 -4.3
-18.0 -4.0
-18.8 -3.6
-19.3 -3.3
-20.0 -2.8
-20.3 -2.6
-21.0 -2.0
-21.2 -1.6
-21.7 -1.1
-22.1 -0.4
-22.6 0.4
-23.0 1.2
-23.3 1.9
-23.7 3.1
-23.9 3.9
-24.1 4.7
-24.1 6.1
-23.9 7.2
-23.7 8.0
-23.0 10.2
-22.6 11.2
-22.6 11.3
-22.1 12.0
-21.7 12.7
-21.2 13.4
-20.8 13.9
-20.2 14.4
-19.6 15.0
-18.8 15.5
-16.8 16.8
-16.0 17.2
-13.8 18.1
-12.3 18.5
-11.8 18.6
-11.0 18.6
-10.3 18.6
-9.8 18.5
-8.7 18.2
-6.3 17.2
-5.5 16.9
-0.3 16.1
0.5 15.9
1.3 15.6
4.3 14.3
6.6 13.1
9.4 11.3
11.4 9.7
15.3 6.3
17.2 4.8
20.4 2.7
21.7 1.3
22.3 0.4
22.8 -1.0
22.9 -1.4
22.8 -3.8
22.6 -4.5
22.3 -5.3
22.2 -5.4
21.6 -6.3
21.4 -6.6
20.7 -7.3
14.7 -12.2
Comment 1 Ivana.Hrivnacova 2015-08-28 18:36:50 CEST
Thank you for the report. I can reproduce the problem and will investigate it.
Comment 2 Ivana.Hrivnacova 2015-09-14 12:34:16 CEST
I checked the definition of the solid and the decomposition of the polygone is ok.
When comparing the response of the "good" solid, defined with swapped points in the polygon definition and the "bad" one, the difference happens in G4TessellatedSolid::DistanceToIn(p,v); so the problem is most probably in G4TessellatedSolid implementation.
Comment 3 Evgueni.Tcherniaev 2016-02-24 17:15:15 CET
More accurate comparison of the angle returned by GetAngle() with pi has been introduced in the triangulation algorithm: (angle >= pi - kAngTolerance ).
Comment 4 perl 2016-02-24 19:45:07 CET
This is great news.
Is there a particular reference tag or a class that I should take to try this out on our problematic examples?
Comment 5 Evgueni.Tcherniaev 2016-02-24 19:58:57 CET
G4ExtrudedSolid.cc with bug fix is available in the repository. It will be included in the upcoming patch at the end of this week.
Comment 6 Gabriele Cosmo 2016-02-25 11:48:26 CET
Created attachment 389 [details]
Simple program to remove cases of collinear points

Posting text from Evgueni...

Hi Joseph,

The problem was caused by first three points in the polygon that you sent to us:
6.8 -17.9
6.0 -18.3
5.6 -18.5

The points are collinear, they lay exactly on the same line. Nevertheless the points were joined in a triangular facet which then was accepted by G4TesselatedSolid, it  managed to calculate the area, the normal and all other attributes of the facet required for tracking. This explains the appearance of an  unphysical extension of the shape. 

In the attachment I send you a small test of the polygon from bug 1777.  The test contains a function removeCollinearPoints() that removes coinciding and collinear points from a 2D polygon. You can use this function in your code. It will help to optimize polygons by removing all unnecessary points from them.

For example, the polygon specified in bugzilla 1777, can be reduced by 5 points (see output of the attached test):

N of points in original polygon = 93
N of points after optimization = 88
Point 1 removed
Point 15 removed
Point 45 removed
Point 65 removed
Point 89 removed

Cheers,
Evgueni