Files
2026-09-24 10:40:57 +08:00

63 lines
3.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Independent transverse contour measurements from BRep, no operation trace."""
import math
from .root_sections import root_band_measurements
import numpy as np
from build123d import Plane,Vector,section
def measure_radial_sections(shape, origin, axis, offsets):
origin = list(origin) if isinstance(origin, Vector) else origin
axis = list(axis) if isinstance(axis, Vector) else axis
if any(not isinstance(v,(list,tuple)) or len(v)!=3 or any(type(x) not in (float,int) or not math.isfinite(x) for x in v) for v in (origin,axis)):
raise ValueError('SECTION_FRAME: origin和axis必须为有限三维向量')
if Vector(axis).length<1e-9:raise ValueError('SECTION_FRAME: axis不能为零')
if not isinstance(offsets,list) or any(type(x) not in (float,int) or not math.isfinite(x) for x in offsets):raise ValueError('SECTION_OFFSETS: offsets为有限数值列表')
if len(offsets)<1 or len(offsets)>12:raise ValueError('SECTION_COUNT: 需要1–12个截面')
axis=Vector(axis).normalized();origin=Vector(origin)
ref=Vector(1,0,0) if abs(axis.X)<.9 else Vector(0,1,0)
u=(ref-axis*ref.dot(axis)).normalized();v=axis.cross(u)
rows=[]
for offset in offsets:
center=origin+axis*offset
cut=section(shape,section_by=Plane(origin=center,x_dir=u,z_dir=axis))
faces=list(cut.faces())
if len(faces)!=1:
rows.append({'offset_mm':offset,'unavailable':'截面未形成唯一连通区域','regions':len(faces)});continue
wire=faces[0].outer_wire();data=[]
for edge in wire.edges():
for t in np.linspace(0,1,65):
p=edge.position_at(float(t))-center
x,y=p.dot(u),p.dot(v)
data.append((math.atan2(y,x)%(2*math.pi),math.hypot(x,y)))
data.sort();a=[];r=[]
for angle,radius in data:
if a and abs(angle-a[-1])<1e-8:r[-1]=max(r[-1],radius)
else:a.append(angle);r.append(radius)
target=np.linspace(0,2*math.pi,4096,endpoint=False)
radii=np.interp(target,[a[-1]-2*math.pi,*a,a[0]+2*math.pi],[r[-1],*r,r[0]])
lo,hi=float(min(radii)),float(max(radii));peaks=[]
if hi-lo>max(.005,hi*.002):
mask=radii>lo+.8*(hi-lo)
starts=np.where(mask & ~np.roll(mask,1))[0]
for start in starts:
indices=[];i=int(start)
while mask[i] and len(indices)<len(mask):
indices.append(i);i=(i+1)%len(mask)
weights=radii[indices]-lo
z=np.sum(weights*np.exp(1j*target[indices]))
peaks.append(float(np.angle(z)%(2*math.pi)))
peaks.sort();gaps=[(peaks[(i+1)%len(peaks)]-a)%(2*math.pi) for i,a in enumerate(peaks)] if peaks else []
periodic=len(peaks)>=4 and max(abs(g-2*math.pi/len(peaks)) for g in gaps)<.025
phase=float(np.angle(np.mean(np.exp(1j*len(peaks)*np.array(peaks))))/len(peaks)*180/math.pi) if periodic else None
try:
root_data=root_band_measurements(wire,center,u,v,lo,hi)
except Exception as exc:
root_data={'unavailable': '截面曲率/切向求值失败: '+str(exc)[:240]}
rows.append({'root_band':root_data,'offset_mm':offset,'outer_radius_range_mm':[lo,hi],'outer_lobe_count':len(peaks),
'equally_spaced':bool(periodic),'phase_modulo_pitch_deg':phase,
'peak_angles_deg':[math.degrees(a) for a in peaks],
'max_spacing_error_deg':math.degrees(max(abs(g-2*math.pi/len(peaks)) for g in gaps)) if gaps else None})
return {'axis':list(axis),'origin':list(origin),'angle_zero_direction':list(u),'sections':rows,
'note':'由实际实体与截面求交后采样外轮廓;齿状峰数量不是渐开线或压力角证明。相位按一个齿距取模,跨截面比较需避免超过半齿距的混叠。截面必须避开轮毂/支架遮挡,不能用生成参数充当实测。'}