thliebig / CSXCAD

A C++ library to describe geometrical objects and their physical or non-physical properties.
http://openEMS.de
GNU Lesser General Public License v3.0
34 stars 38 forks source link

Bug: Polyhedron IsInside() only works after calling Update()? #30

Closed lemoer closed 1 year ago

lemoer commented 2 years ago

Hi,

I just discovered, that polyhedron.IsInside(coord) returns False even if the coord is inside the polyhedron.

Only after calling polyhedron.Update(), the function works as expected.

I think, this may be a bug.

Script to reproduce:

#!/usr/bin/python

import numpy as np
import CSXCAD

cs = CSXCAD.ContinuousStructure()

metal = cs.AddMetal("METAL")
ph = metal.AddPolyhedron()

for phi in [0, 2*np.pi/3, -2*np.pi/3]:
    ph.AddVertex(np.cos(phi), np.sin(phi), -1.0)

ph.AddVertex(0, 0, 1)

ph.AddFace([0, 1, 2])
ph.AddFace([0, 3, 1])
ph.AddFace([0, 2, 3])
ph.AddFace([1, 3, 2])

cs.Write2XML('bug.xml')

cs2 = CSXCAD.ContinuousStructure()

cs2.ReadFromXML('bug.xml')

ph2 = cs2.GetAllPrimitives()[0]

print("Before Update(): " + str(ph2.IsInside([0, 0, 0])) + " (Should be True)")

ph2.Update()

print("After ph2.Update(): " + str(ph2.IsInside([0, 0, 0])) + " (Should be True)")

Result:

Before Update(): False (Should be True)
After ph2.Update(): True (Should be True)
thliebig commented 1 year ago

I can confirm this. It is only partially a bug, more like a conceptional problem. The "Update()" method is numerically expensive and thus cannot and should not be executed every time "IsInside" is called. openEMS always updates everything before calling "IsInside" and a user should do it too.

I will think about a flag like (is_uptodate) and if it is not set during"IsInside" an Update is called once...

thliebig commented 1 year ago

Well I think only the Polyhedron has this problem as its data structures are very complex...

thliebig commented 1 year ago

this should be fixed, but I found more issues like this, e.g. with GetDimension on all types of primitives... we will see.

This should be fixed...

lemoer commented 1 year ago

Thank you for the fixes!