Skip to content

Commit 02b8023

Browse files
committed
Tweaks p3Dv (+ stress tests)
1 parent 3c630f9 commit 02b8023

File tree

2 files changed

+106
-10
lines changed

2 files changed

+106
-10
lines changed

src/osut/osut.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,7 +2737,10 @@ def p3Dv(pts=None) -> openstudio.Point3dVector:
27372737
elif isinstance(pts, openstudio.Point3dVector):
27382738
return pts
27392739
elif isinstance(pts, openstudio.model.PlanarSurface):
2740-
pts = list(pts.vertices())
2740+
for vt in pts.vertices():
2741+
pt = openstudio.Point3d(vt.x(), vt.y(), vt.z())
2742+
v.append(pt)
2743+
return v
27412744

27422745
try:
27432746
pts = list(pts)
@@ -3056,7 +3059,7 @@ def nextUp(pts=None, pt=None):
30563059
None: If invalid inputs (see logs).
30573060
30583061
"""
3059-
mth = "osut.nextUP"
3062+
mth = "osut.nextUp"
30603063
pts = p3Dv(pts)
30613064
cl = openstudio.Point3d
30623065

@@ -3341,7 +3344,7 @@ def isPointAlongSegment(p0=None, sg=[]) -> bool:
33413344
33423345
Returns:
33433346
bool: Whether a 3D point lies ~along a 3D point segment.
3344-
False: If invalid inputs.
3347+
False: If invalid inputs (see logs).
33453348
33463349
"""
33473350
mth = "osut.isPointAlongSegment"
@@ -3350,12 +3353,10 @@ def isPointAlongSegment(p0=None, sg=[]) -> bool:
33503353

33513354
if not isinstance(p0, cl1):
33523355
return oslg.mismatch("point", p0, cl1, mth, CN.DBG, False)
3353-
if not isSegment(sg):
3354-
return oslg.mismatch("segment", sg, cl2, mth, CN.DBG, False)
3355-
3356+
if not isSegment(sg): return False
33563357
if holds(sg, p0): return True
33573358

3358-
a = sg[0]
3359+
a = sg[ 0]
33593360
b = sg[-1]
33603361
ab = b - a
33613362
abn = b - a
@@ -3397,7 +3398,7 @@ def isPointAlongSegments(p0=None, sgs=[]) -> bool:
33973398
if not sgs:
33983399
return oslg.empty("segments", mth, CN.DBG, False)
33993400
if not isinstance(p0, cl1):
3400-
return oslg.mismatch("point", p0, cl, mth, CN.DBG, False)
3401+
return oslg.mismatch("point", p0, cl1, mth, CN.DBG, False)
34013402

34023403
for sg in sgs:
34033404
if isPointAlongSegment(p0, sg): return True

tests/test_osut.py

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test03_dictionaries(self):
6666
self.assertTrue("skylight" in osut.film())
6767
self.assertTrue("skylight" in osut.uo())
6868
self.assertEqual(osut.film().keys(), osut.uo().keys())
69-
69+
7070
def test04_materials(self):
7171
material = osut.mats()["material"]
7272
sand = osut.mats()["sand"]
@@ -3308,6 +3308,84 @@ def test25_segments_triads_orientation(self):
33083308
p7 = openstudio.Point3d(14, 20, -5)
33093309
p8 = openstudio.Point3d(-9, -9, -5)
33103310

3311+
# Stress test 'to_p3Dv'. 4 valid input cases.
3312+
# Valid case #1: a single Point3d.
3313+
vtx = osut.p3Dv(p0)
3314+
self.assertTrue(isinstance(vtx, openstudio.Point3dVector))
3315+
self.assertEqual(vtx[0], p0) # same object ID
3316+
3317+
# Valid case #2: a Point3dVector.
3318+
vtxx = openstudio.Point3dVector()
3319+
vtxx.append(p0)
3320+
vtxx.append(p1)
3321+
vtxx.append(p2)
3322+
vtxx.append(p3)
3323+
vtx = osut.p3Dv(vtxx)
3324+
self.assertTrue(isinstance(vtx, openstudio.Point3dVector))
3325+
self.assertEqual(vtx[ 0], p0) # same object ID
3326+
self.assertEqual(vtx[ 1], p1) # same object ID
3327+
self.assertEqual(vtx[ 2], p2) # same object ID
3328+
self.assertEqual(vtx[-1], p3) # same object ID
3329+
3330+
# Valid case #3: Surface vertices.
3331+
model = openstudio.model.Model()
3332+
surface = openstudio.model.Surface(vtxx, model)
3333+
self.assertTrue(isinstance(surface.vertices(), tuple)) # ! Point3dVector
3334+
self.assertEqual(len(surface.vertices()), 4)
3335+
vtx = osut.p3Dv(vtxx)
3336+
self.assertTrue(isinstance(vtx, openstudio.Point3dVector))
3337+
self.assertEqual(len(vtx), 4)
3338+
self.assertEqual(vtx[0], p0)
3339+
self.assertEqual(vtx[1], p1)
3340+
self.assertEqual(vtx[2], p2)
3341+
self.assertEqual(vtx[3], p3)
3342+
3343+
# Valid case #4: Array.
3344+
vtx = osut.p3Dv([p0, p1, p2, p3])
3345+
self.assertTrue(isinstance(vtx, openstudio.Point3dVector))
3346+
self.assertEqual(len(vtx), 4)
3347+
self.assertEqual(vtx[0], p0)
3348+
self.assertEqual(vtx[1], p1)
3349+
self.assertEqual(vtx[2], p2)
3350+
self.assertEqual(vtx[3], p3)
3351+
3352+
# Stress test 'nextUp'.
3353+
m0 = "Invalid 'points (2+)' arg #1 (osut.nextUp)"
3354+
3355+
# Invalid case.
3356+
pt = osut.nextUp([], p0)
3357+
self.assertFalse(pt)
3358+
self.assertTrue(o.is_warn())
3359+
self.assertEqual(len(o.logs()), 1)
3360+
self.assertEqual(o.logs()[0]["message"], m0)
3361+
self.assertEqual(o.clean(), DBG)
3362+
3363+
# Valid case.
3364+
pt = osut.nextUp([p0, p1, p2, p3], p0)
3365+
self.assertTrue(isinstance(pt, openstudio.Point3d))
3366+
self.assertEqual(pt, p1)
3367+
3368+
pt = osut.nextUp([p0, p0, p0], p0)
3369+
self.assertTrue(isinstance(pt, openstudio.Point3d))
3370+
self.assertEqual(pt, p0)
3371+
3372+
# Stress test 'segments'. Invalid case.
3373+
sgs = osut.segments(p3)
3374+
self.assertTrue(isinstance(sgs, openstudio.Point3dVectorVector))
3375+
self.assertFalse(sgs)
3376+
self.assertEqual(o.status(), 0) # nothing logged
3377+
3378+
sgs = osut.segments([p3, p3])
3379+
self.assertTrue(isinstance(sgs, openstudio.Point3dVectorVector))
3380+
self.assertFalse(sgs)
3381+
self.assertEqual(o.status(), 0) # nothing logged
3382+
3383+
# Valid case.
3384+
sgs = osut.segments([p0, p1, p2, p3])
3385+
self.assertTrue(isinstance(sgs, openstudio.Point3dVectorVector))
3386+
self.assertEqual(len(sgs), 4)
3387+
self.assertTrue(isinstance(sgs[-1], tuple)) # ! Point3dVector
3388+
33113389
# Stress test 'uniques'.
33123390
m0 = "'n points' str? expecting int (osut.uniques)"
33133391

@@ -3375,6 +3453,7 @@ def test25_segments_triads_orientation(self):
33753453
self.assertEqual(len(collinears), 2)
33763454
self.assertTrue(osut.areSame(collinears[0], p0))
33773455
self.assertTrue(osut.areSame(collinears[1], p1))
3456+
self.assertTrue(osut.isPointAlongSegment(p0, sgs[0]))
33783457

33793458
# Only 2 collinears, so request for first 3 is ignored.
33803459
collinears = osut.collinears([p0, p1, p2, p3, p8], 3)
@@ -3409,6 +3488,22 @@ def test25_segments_triads_orientation(self):
34093488
self.assertTrue(osut.areSame(collinears[0], p0))
34103489
self.assertTrue(osut.areSame(collinears[1], p1))
34113490

3491+
# Stress test isPointAlongSegment.
3492+
m0 = "'point' str? expecting Point3d (osut.p3Dv)"
3493+
3494+
# Invalid case.
3495+
self.assertFalse(osut.isPointAlongSegment(p3, "osut"))
3496+
self.assertTrue(o.is_debug())
3497+
self.assertEqual(len(o.logs()), 1)
3498+
self.assertEqual(o.logs()[0]["message"], m0)
3499+
self.assertEqual(o.clean(), DBG)
3500+
3501+
# Valid case.
3502+
pts = openstudio.Point3dVector()
3503+
pts.append(p0)
3504+
pts.append(p1)
3505+
self.assertFalse(osut.isPointAlongSegment(p3, pts))
3506+
34123507
# CASE a1: 2x end-to-end line segments (returns matching endpoints).
34133508
self.assertTrue(osut.doesLineIntersect([p0, p1], [p1, p2]))
34143509
pt = osut.lineIntersection([p0, p1], [p1, p2])
@@ -4999,7 +5094,7 @@ def test33_leader_line_anchors_inserts(self):
49995094
# [ 4, 14, 0] ... vs [16, 2, 20]
50005095
# [ 0, 14, 0] ... vs [20, 2, 20]
50015096
# [ 0, 0, 0] ... vs [20, 16, 20]
5002-
5097+
50035098
def test34_generated_skylight_wells(self):
50045099
o = osut.oslg
50055100
self.assertEqual(o.status(), 0)

0 commit comments

Comments
 (0)