gouminghao / geometry3d Goto Github PK
View Code? Open in Web Editor NEWGeometry3D: 3D Computational Geometrics Library
License: GNU General Public License v3.0
Geometry3D: 3D Computational Geometrics Library
License: GNU General Public License v3.0
Hello, dear developers of the Geometry3D library!
While using the intersection() function, an error was detected for a pair of triangles, which is probably related to incorrect rounding or overflow. The coordinates of the points in one set differ from the other by increasing each number by a factor of 10.
The file with the minimum reproducible code example is attached below. It follows from the first set of numbers that the triangles do not intersect, and from the second set it follows that they intersect.
Sincerely, Andrey Kazachkov.
import Geometry3D as geometry
def creating_points(numbers, n_triangles):
n_numbers = n_triangles*9
points = []
for i in range(0, n_numbers, 3):
points.append(geometry.Point(numbers[i], numbers[i+1], numbers[i+2]))
return points
def creating_triangles(points, n_triangles):
triangles = []
n_points = n_triangles*3
for i in range(0, n_points, 3):
triangles.append(geometry.ConvexPolygon((points[i], points[i+1], points[i+2])))
return triangles
def intersecting_triangles(triangles):
intersecting_indexes = []
n_intersections = 0
for i in range(len(triangles) - 1):
for j in range(i + 1, len(triangles)):
if geometry.intersection(triangles[i], triangles[j]):
if i not in intersecting_indexes:
intersecting_indexes.append(i)
if j not in intersecting_indexes:
intersecting_indexes.append(j)
n_intersections = n_intersections + 1
return intersecting_indexes, n_intersections
def main():
n_intersections = 0
n_triangles = 2
if 0:
numbers = [29.3505, 805.295, 397.635, -248.565, -404.623, -873.618, -386.796, -76.4378, -878.078,
-120.028, -992.064, 12.6612, -729.661, -244.151, -979.961, 997.444, 982.96, -259.114]
else:
numbers = [2.93505, 80.5295, 39.7635, -24.8565, -40.4623, -87.3618, -38.6796, -7.64378, -87.8078,
-12.0028, -99.2064, 1.26612, -72.9661, -24.4151, -97.9961, 99.7444, 98.296,-25.9114]
points = creating_points(numbers, n_triangles)
triangles = creating_triangles(points, n_triangles)
intersecting_indexes, n_intersections = intersecting_triangles(triangles)
print(f"n_intersections = {n_intersections}")
main()
Hello when I calculate the intersection between these two polyhedron:
a = [ConvexPolygon((Point(234.9075469970703, 178.4456329345703, -0.0), Point(20.74983215332029, 225.9232635498047, 190.0), Point(8.035964965820328, 228.74185180664062, 190.0))), ConvexPolygon((Point(12.42473398461375, 244.4606612527466, 190.0000072746582), Point(25.138601658898136, 241.6420728879935, 190.0000072746582), Point(239.29632568359352, 194.1644439697266, 2.0726259984083645e-13))), ConvexPolygon((Point(20.74983215332029, 225.9232635498047, 190.0), Point(25.138601658898136, 241.6420728879935, 190.0000072746582), Point(12.42473398461375, 244.4606612527466, 190.0000072746582), Point(8.035964965820328, 228.74185180664062, 190.0))), ConvexPolygon((Point(239.29632568359352, 194.1644439697266, 2.0726259984083645e-13), Point(25.138601658898136, 241.6420728879935, 190.0000072746582), Point(20.74983215332029, 225.9232635498047, 190.0), Point(234.9075469970703, 178.4456329345703, -0.0))), ConvexPolygon((Point(234.9075469970703, 178.4456329345703, -0.0), Point(8.035964965820328, 228.74185180664062, 190.0), Point(12.42473398461375, 244.4606612527466, 190.0000072746582), Point(239.29632568359352, 194.1644439697266, 2.0726259984083645e-13)))]#[ConvexPolygon((Point(234.0, 178.0, 0.0), Point(19.999999999999993, 224.99999999999997, 190.0), Point(7.9999999999999725, 228.0, 190.0))), ConvexPolygon((Point(12.999999999999918, 243.99999999999997, 190.0), Point(24.999999999999993, 240.99999999999997, 190.0), Point(239.0000000000007, 193.99999999999983, -6.173231218204781e-13))), ConvexPolygon((Point(19.999999999999993, 224.99999999999997, 190.0), Point(24.999999999999993, 240.99999999999997, 190.0), Point(12.999999999999918, 243.99999999999997, 190.0), Point(7.9999999999999725, 228.0, 190.0))), ConvexPolygon((Point(234.0, 178.0, 0.0), Point(239.0000000000007, 193.99999999999983, -6.173231218204781e-13), Point(24.999999999999993, 240.99999999999997, 190.0), Point(19.999999999999993, 224.99999999999997, 190.0))), ConvexPolygon((Point(234.0, 178.0, 0.0), Point(7.9999999999999725, 228.0, 190.0), Point(12.999999999999918, 243.99999999999997, 190.0), Point(239.0000000000007, 193.99999999999983, -6.173231218204781e-13)))]
b = [ConvexPolygon((Point(236.5274658203125, 184.2475128173828, 0.0), Point(-105.78327941894531, 343.14288330078125, 190.0), Point(-112.74012756347656, 346.37213134765625, 183.30928039550778))), ConvexPolygon((Point(-109.73899495224315, 357.12103180010325, 183.30928773788403), Point(-102.7821465290583, 353.891783623882, 190.0000076103702), Point(239.52861022949182, 194.9963989257814, 1.807429067882295e-13))), ConvexPolygon((Point(-102.7821465290583, 353.891783623882, 190.0000076103702), Point(-109.73899495224315, 357.12103180010325, 183.30928773788403), Point(-112.74012756347656, 346.37213134765625, 183.30928039550778), Point(-105.78327941894531, 343.14288330078125, 190.0))), ConvexPolygon((Point(-105.78327941894531, 343.14288330078125, 190.0), Point(236.5274658203125, 184.2475128173828, 0.0), Point(239.52861022949182, 194.9963989257814, 1.807429067882295e-13), Point(-102.7821465290583, 353.891783623882, 190.0000076103702))), ConvexPolygon((Point(236.5274658203125, 184.2475128173828, 0.0), Point(-112.74012756347656, 346.37213134765625, 183.30928039550778), Point(-109.73899495224315, 357.12103180010325, 183.30928773788403), Point(239.52861022949182, 194.9963989257814, 1.807429067882295e-13)))]#[ConvexPolygon((Point(236.0, 184.0, 0.0), Point(-104.99999999999999, 343.0, 190.0), Point(-112.0, 345.99999999999994, 183.0))), ConvexPolygon((Point(239.0, 194.0, 0.0), Point(-109.0, 355.99999999999994, 183.0), Point(-101.99999999999999, 353.0, 190.0))), ConvexPolygon((Point(-104.99999999999999, 343.0, 190.0), Point(-101.99999999999999, 353.0, 190.0), Point(-109.0, 355.99999999999994, 183.0), Point(-112.0, 345.99999999999994, 183.0))), ConvexPolygon((Point(-104.99999999999999, 343.0, 190.0), Point(236.0, 184.0, 0.0), Point(239.0, 194.0, 0.0), Point(-101.99999999999999, 353.0, 190.0))), ConvexPolygon((Point(236.0, 184.0, 0.0), Point(-112.0, 345.99999999999994, 183.0), Point(-109.0, 355.99999999999994, 183.0), Point(239.0, 194.0, 0.0)))]
The project report "TypeError: Bug detected! please contact the author", can you help me solve this problem? Thank you!
Hello, thank you for this amazing project, this is very helpful! right now I have two convex polyhedrons and I want to calculate their intersection, the two polyhedron are shown below:
a = [ConvexPolygon((Point(125.57989501953125, 33.03245544433594, 0.0), Point(-87.77513122558594, 348.1565246582031, 128.49752807617188), Point(-87.77513122558594, 348.1565246582031, 108.55316162109375))), ConvexPolygon((Point(134.3735809326172, 39.70722961425781, 0.0), Point(-78.9814453125, 354.831298828125, 108.55316162109374), Point(-78.9814453125, 354.831298828125, 128.4975280761719))), ConvexPolygon((Point(-87.77513122558594, 348.1565246582031, 128.49752807617188), Point(-78.9814453125, 354.831298828125, 128.4975280761719), Point(-78.9814453125, 354.831298828125, 108.55316162109374), Point(-87.77513122558594, 348.1565246582031, 108.55316162109375))), ConvexPolygon((Point(-87.77513122558594, 348.1565246582031, 128.49752807617188), Point(125.57989501953125, 33.03245544433594, 0.0), Point(134.3735809326172, 39.70722961425781, 0.0), Point(-78.9814453125, 354.831298828125, 128.4975280761719))), ConvexPolygon((Point(125.57989501953125, 33.03245544433594, 0.0), Point(-87.77513122558594, 348.1565246582031, 108.55316162109375), Point(-78.9814453125, 354.831298828125, 108.55316162109374), Point(134.3735809326172, 39.70722961425781, 0.0)))]#[ConvexPolygon((Point(-106.67681884765625, 302.38067626953125, 190.0), Point(-125.07760620117188, 319.8424072265625, 184.20480346679688), Point(154.2360382080078, 54.783660888671875, -0.0))), ConvexPolygon((Point(166.9486541748047, 64.43305969238281, -0.0), Point(-112.36498908762633, 329.491819597817, 184.20480836937483), Point(-93.96420124437704, 312.03008817604496, 190.00000505681606))), ConvexPolygon((Point(-125.07760620117188, 319.8424072265625, 184.20480346679688), Point(-106.67681884765625, 302.38067626953125, 190.0), Point(-93.96420124437704, 312.03008817604496, 190.00000505681606), Point(-112.36498908762633, 329.491819597817, 184.20480836937483))), ConvexPolygon((Point(-106.67681884765625, 302.38067626953125, 190.0), Point(154.2360382080078, 54.783660888671875, -0.0), Point(166.9486541748047, 64.43305969238281, -0.0), Point(-93.96420124437704, 312.03008817604496, 190.00000505681606))), ConvexPolygon((Point(154.2360382080078, 54.783660888671875, -0.0), Point(-125.07760620117188, 319.8424072265625, 184.20480346679688), Point(-112.36498908762633, 329.491819597817, 184.20480836937483), Point(166.9486541748047, 64.43305969238281, -0.0)))]
b = [ConvexPolygon((Point(-47.52482604980469, 113.43841552734375, 128.61720275878906), Point(-47.52482604980469, 113.43841552734375, 106.79966735839844), Point(59.184303283691406, 493.0682373046875, -0.0))), ConvexPolygon((Point(-56.86540222167969, 119.09527587890625, 128.61720275878906), Point(49.843727111816406, 498.72509765625, -0.0), Point(-56.86540222167969, 119.09527587890625, 106.79966735839844))), ConvexPolygon((Point(-47.52482604980469, 113.43841552734375, 128.61720275878906), Point(-56.86540222167969, 119.09527587890625, 128.61720275878906), Point(-56.86540222167969, 119.09527587890625, 106.79966735839844), Point(-47.52482604980469, 113.43841552734375, 106.79966735839844))), ConvexPolygon((Point(-47.52482604980469, 113.43841552734375, 128.61720275878906), Point(59.184303283691406, 493.0682373046875, -0.0), Point(49.843727111816406, 498.72509765625, -0.0), Point(-56.86540222167969, 119.09527587890625, 128.61720275878906))), ConvexPolygon((Point(59.184303283691406, 493.0682373046875, -0.0), Point(-47.52482604980469, 113.43841552734375, 106.79966735839844), Point(-56.86540222167969, 119.09527587890625, 106.79966735839844), Point(49.843727111816406, 498.72509765625, -0.0)))]#[ConvexPolygon((Point(228.86135864257812, 328.18670654296875, 0.0), Point(-94.588623046875, 219.96185302734375, 190.0), Point(-117.3912353515625, 212.33221435546875, 190.0))), ConvexPolygon((Point(224.04629516601562, 340.3481750488281, 0.0), Point(-122.20629882812509, 224.4936828613281, 190.00000000000003), Point(-99.40368652343749, 232.12332153320312, 190.00000000000003))), ConvexPolygon((Point(-94.588623046875, 219.96185302734375, 190.0), Point(-99.40368652343749, 232.12332153320312, 190.00000000000003), Point(-122.20629882812509, 224.4936828613281, 190.00000000000003), Point(-117.3912353515625, 212.33221435546875, 190.0))), ConvexPolygon((Point(228.86135864257812, 328.18670654296875, 0.0), Point(224.04629516601562, 340.3481750488281, 0.0), Point(-99.40368652343749, 232.12332153320312, 190.00000000000003), Point(-94.588623046875, 219.96185302734375, 190.0))), ConvexPolygon((Point(-117.3912353515625, 212.33221435546875, 190.0), Point(-122.20629882812509, 224.4936828613281, 190.00000000000003), Point(224.04629516601562, 340.3481750488281, 0.0), Point(228.86135864257812, 328.18670654296875, 0.0)))]
when I build this two polyhderon and run intersection:
intersection(a, b)
the code always gives me “ValueError: Check for the number of vertices, faces and edges fail, the polyhedron may not be closed”,I'm pretty sure these two polyhedrons have a 3-D intersection, so I think it is caused by the numerical accuracy; can anyone help me solve this problem? Thank you so much!!
I'm having problems with evaluating the intersection of a line and a cylinder. I use the following code:
from Geometry3D import *
def intersection_v2(xp,yp,zp,px,py,pz,xt,yt,zt,rt,ht):
part = Line(Point(xp,yp,zp),Vector(px,py,pz))
tank = Cylinder(Point(xt,yt,zt),191.6,302.4*x_unit_vector(),40)
inters = intersection(part,tank)
print(inters)
intersection_v2(100000000,100000000,100000000,-1,-1,-1,0,0,0,191.6,302.4)
I get that intersection is only a point (0,0,0) which is absurd. For smaller initial points (1000,1000,1000) the code behaves as intended. I tried to look for eventual limits on intersection but could not find them
Would it be possible to have 3D polygon's subtracting from each other?
It will be great to have a direct method for exporting geometries to 3D supported files (i.e .obj/.off/.mesh file types).
If I need your useful package in my package, I cannot configure any non-root logger normally.
Please create a new logger instance than using the root logger.
I was wondering if it is possible to extract the information from for instance a ConvexPolyhedron. I can print it and get "ConvexPolyhedron({Point(0, 3, 3), Point(3, 0, 0), Point(3, 3, 0), Point(0, 3, 0), Point(3, 0, 3), Point(0, 0, 3), Point(0, 0, 0), Point(3, 3, 3)})". However, I wish to have a numpy array with the points in it. Is there a way to do this?
There is currently a bug in the code for ConvexPolygon-Plane Intersection.
This bug is caused by a minor typo, and can be fixed relatively easily.
If you look at line 98-101 in "Geometry3D/calc/intersection.py", you'll find this code:
elif isinstance(a, Plane) and isinstance(b, ConvexPolygon):
return inter_plane_convexpolygon(a,b)
elif isinstance(a, ConvexPolygon) and isinstance(a, Plane):
return inter_plane_convexpolygon(b,a)
In the second elif
statement, you can see that we are checking the type of a
twice, without checking the type of b
.
This should be changed to the following:
elif isinstance(a, Plane) and isinstance(b, ConvexPolygon):
return inter_plane_convexpolygon(a,b)
elif isinstance(a, ConvexPolygon) and isinstance(b, Plane):
return inter_plane_convexpolygon(b,a)
Until then, the work-around is to simply always specify the Plane
first, and the ConvexPolygon
second, so that you trigger the first elif
, rather than the second.
Thanks in advance!
After moving a Segment by (0, 0, -1), the "in" and "intersection" operations still take the original position into account.
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Geometry3D.version
'0.2.4'
s = Segment(Point(0, 0, 1), Point(2, 0, 1))
s.move(-z_unit_vector())
Segment(Point(0, 0, 0), Point(2, 0, 0))
s
Segment(Point(0, 0, 0), Point(2, 0, 0))
s2 = Segment(Point(0, 0, 0), Point(2, 0, 0))
s == s2
True
Point(1, 0, 0) in s2
True
Point(1, 0, 0) in s
False # Should return True
Point(1, 0, 1) in s
True # Should return False
ground = Plane(origin(), x_unit_vector(), y_unit_vector())
print(intersection(s2, ground))
Segment(Point(0, 0, 0), Point(2, 0, 0))
print(intersection(s, ground))
None # Should return the segment
This may be caused by a failure to move the "line" attribute accordingly:
s.line
Line(sv=Vector(0, 0, 1),dv=Vector(2, 0, 0))
s2.line
Line(sv=Vector(0, 0, 0),dv=Vector(2, 0, 0))
Hi,
Really enjoy this library but I have a small problem and I can't figure out why it isn't working.
I want to calculate the intersection between two planes, but some calculations return a None even if the planes doe have an intersection?
Does someone have any ideas?
This is my code:
`from Geometry3D import *
plane1 = ConvexPolygon((Point(61.32, 0.0, 0.58), Point(-0.21, 50.58, -28.91), Point(-0.21, 0.0, -28.91)))
plane2 = ConvexPolygon((Point(-50.21, 20.0, -78.91), Point(-50.21, 20.0, 50.58), Point(111.32, 20.0, 50.58), Point(111.32, 20.0, -78.91)))
inter = intersection(plane1,plane2)
print(inter)
r = Renderer()
r.add((plane1,'r',2))
r.add((plane2,'b',2),normal_length = 0)
r.show()`
Hello, for the below two convex polygon there should have a intersect convex polygon, but the result is None,
c = ConvexPolygon((Point(25.917035916717097, 249.4823416602293, 140.95893683162333), Point(15.86926746447375, 234.15206098547773, 144.81489658305364), Point(16.802552489221853, 234.50594363623017, 127.52971818115088), Point(26.825470531894382, 249.82680154239767, 124.13400771198833)))
p = Plane(Point(152.0, 436.0, 0.0), Vector(-0.8398036066330478, 0.541808778472934, -0.03425127524329979))
print(c in p)
c1 = ConvexPolygon((Point(152.0, 436.0, 0.0), Point(-28.060504913330078, 168.9019012451172, 189.7555389404297), Point(-8.086238861083984, 199.8620147705078, 189.7555389404297)))
print(c1 in p)
print(intersection(c,c1))
r = Renderer()
r.add((c,'r',1),normal_length = 0)
r.add((c1,'g',1),normal_length = 0)
r.show()
thank you for your help!
When I execute the code below the program has an error
from Geometry3D import *
circle = Circle(Point(503.967529, 0, 370.521179),y_unit_vector(),400)
segment = Segment(Point(-353.2565, 0, -68.26225), Point(721.19, 0, 477.95))
print(circle.intersection(segment))
Hello, thank you for your amazing project! Recently I'm trying to use this project to generate an intersection between two polyhedrons, but I found an error:
"ValueError: Convex Check Fails Because Point(-39.35859231445235, 261.35056323400175, 129.2997523368219) Is Not On Plane(Point(-27.878684988415753, 258.162898458097, 127.72269713190062), Vector(0.2489692168272217, 0.48178113934707206, 0.8401793039833086))"
After look through your source code, I think this error is caused by the precision of the cross between two vectors, for example, if we have three points:
a = Point(-39.47570506286104, 247.07101597009853, 122.23513421678561)
b = Point(-34.157972900849515, 245.67527429293494, 121.54460938703097)
c = Point(-30.09033189524111, 244.60764322271578, 121.01641296281727)
if we generate a plane based on this three points, in the code (/geometry/plane) we'll do:
vab = b.pv() - a.pv()
vac = c.pv() - a.pv()
vec1 = vab.cross(vac)
plane = Plane(a, vec1)
after that, I found "a in plane" is true, but both "b in plane" and "c in plane" will return false because vec1 is not accurate enough, can you help me solve this problem? Thank you so much!
Hi
Thank you for the beautiful library.
When I run this code:
from Geometry3D import *
poly1=Sphere(Point(15.592607258702628, -66.15776443481445, 32.0),10,n1=7,n2=3)
poly2=Cylinder(Point(229.78870794537943, -64.86530276807025, 32.0),10,Vector(Point(229.78870794537943, -64.86530276807025, 32.0),Point(214.4889864671277, -64.95762145752087, 32.0)),10)
print(intersection(poly1,poly2))
I get the following output options:
It's strange, but the geometrical objects not even close:
Please, help
It seems line 27 of "renderer_matplotlib" shall be replaced by ax = fig.add_subplot(projection='3d') to get the correct display.
see discussion here : https://stackoverflow.com/questions/76217413/figure-size-800x800-with-0-axes-when-using-plt-show-jupyter-notebook
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.